1package cache
2
3import (
4 "fmt"
5 "io"
6 "io/ioutil"
7 "os"
8 "path"
9 "path/filepath"
10 "strconv"
11 "sync"
12
13 "github.com/MichaelMure/git-bug/bug"
14 "github.com/MichaelMure/git-bug/entity"
15 "github.com/MichaelMure/git-bug/identity"
16 "github.com/MichaelMure/git-bug/repository"
17 "github.com/MichaelMure/git-bug/util/process"
18)
19
20// 1: original format
21// 2: added cache for identities with a reference in the bug cache
22const formatVersion = 2
23
24const lruCacheSize = 100
25
26var _ repository.RepoCommon = &RepoCache{}
27
28// RepoCache is a cache for a Repository. This cache has multiple functions:
29//
30// 1. After being loaded, a Bug is kept in memory in the cache, allowing for fast
31// access later.
32// 2. The cache maintain in memory and on disk a pre-digested excerpt for each bug,
33// allowing for fast querying the whole set of bugs without having to load
34// them individually.
35// 3. The cache guarantee that a single instance of a Bug is loaded at once, avoiding
36// loss of data that we could have with multiple copies in the same process.
37// 4. The same way, the cache maintain in memory a single copy of the loaded identities.
38//
39// The cache also protect the on-disk data by locking the git repository for its
40// own usage, by writing a lock file. Of course, normal git operations are not
41// affected, only git-bug related one.
42type RepoCache struct {
43 // the underlying repo
44 repo repository.ClockedRepo
45
46 // the name of the repository, as defined in the MultiRepoCache
47 name string
48
49 muBug sync.RWMutex
50 // excerpt of bugs data for all bugs
51 bugExcerpts map[entity.Id]*BugExcerpt
52 // bug loaded in memory
53 bugs map[entity.Id]*BugCache
54
55 // presentBugs is an LRU cache that records which bugs the cache has loaded in
56 presentBugs *LRUIdCache
57
58 muIdentity sync.RWMutex
59 // excerpt of identities data for all identities
60 identitiesExcerpts map[entity.Id]*IdentityExcerpt
61 // identities loaded in memory
62 identities map[entity.Id]*IdentityCache
63
64 // the user identity's id, if known
65 userIdentityId entity.Id
66}
67
68func NewRepoCache(r repository.ClockedRepo) (*RepoCache, error) {
69 return NewNamedRepoCache(r, "")
70}
71
72func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, error) {
73 c := &RepoCache{
74 repo: r,
75 name: name,
76 bugs: make(map[entity.Id]*BugCache),
77 identities: make(map[entity.Id]*IdentityCache),
78 }
79
80 err := c.lock()
81 if err != nil {
82 return &RepoCache{}, err
83 }
84
85 presentBugs := NewLRUIdCache(lruCacheSize)
86 c.presentBugs = presentBugs
87
88 err = c.load()
89 if err == nil {
90 return c, nil
91 }
92
93 // Cache is either missing, broken or outdated. Rebuilding.
94 err = c.buildCache()
95 if err != nil {
96 return nil, err
97 }
98
99 return c, c.write()
100}
101
102// load will try to read from the disk all the cache files
103func (c *RepoCache) load() error {
104 err := c.loadBugCache()
105 if err != nil {
106 return err
107 }
108 return c.loadIdentityCache()
109}
110
111// write will serialize on disk all the cache files
112func (c *RepoCache) write() error {
113 err := c.writeBugCache()
114 if err != nil {
115 return err
116 }
117 return c.writeIdentityCache()
118}
119
120func (c *RepoCache) lock() error {
121 lockPath := repoLockFilePath(c.repo)
122
123 err := repoIsAvailable(c.repo)
124 if err != nil {
125 return err
126 }
127
128 err = os.MkdirAll(filepath.Dir(lockPath), 0777)
129 if err != nil {
130 return err
131 }
132
133 f, err := os.Create(lockPath)
134 if err != nil {
135 return err
136 }
137
138 pid := fmt.Sprintf("%d", os.Getpid())
139 _, err = f.WriteString(pid)
140 if err != nil {
141 return err
142 }
143
144 return f.Close()
145}
146
147func (c *RepoCache) Close() error {
148 c.muBug.Lock()
149 defer c.muBug.Unlock()
150 c.muIdentity.Lock()
151 defer c.muIdentity.Unlock()
152
153 c.identities = make(map[entity.Id]*IdentityCache)
154 c.identitiesExcerpts = nil
155 c.bugs = nil
156 c.bugExcerpts = nil
157
158 lockPath := repoLockFilePath(c.repo)
159 return os.Remove(lockPath)
160}
161
162func (c *RepoCache) buildCache() error {
163 c.muBug.Lock()
164 defer c.muBug.Unlock()
165 c.muIdentity.Lock()
166 defer c.muIdentity.Unlock()
167
168 _, _ = fmt.Fprintf(os.Stderr, "Building identity cache... ")
169
170 c.identitiesExcerpts = make(map[entity.Id]*IdentityExcerpt)
171
172 allIdentities := identity.ReadAllLocalIdentities(c.repo)
173
174 for i := range allIdentities {
175 if i.Err != nil {
176 return i.Err
177 }
178
179 c.identitiesExcerpts[i.Identity.Id()] = NewIdentityExcerpt(i.Identity)
180 }
181
182 _, _ = fmt.Fprintln(os.Stderr, "Done.")
183
184 _, _ = fmt.Fprintf(os.Stderr, "Building bug cache... ")
185
186 c.bugExcerpts = make(map[entity.Id]*BugExcerpt)
187
188 allBugs := bug.ReadAllLocalBugs(c.repo)
189
190 for b := range allBugs {
191 if b.Err != nil {
192 return b.Err
193 }
194
195 snap := b.Bug.Compile()
196 c.bugExcerpts[b.Bug.Id()] = NewBugExcerpt(b.Bug, &snap)
197 }
198
199 _, _ = fmt.Fprintln(os.Stderr, "Done.")
200 return nil
201}
202
203func repoLockFilePath(repo repository.Repo) string {
204 return path.Join(repo.GetPath(), "git-bug", lockfile)
205}
206
207// repoIsAvailable check is the given repository is locked by a Cache.
208// Note: this is a smart function that will cleanup the lock file if the
209// corresponding process is not there anymore.
210// If no error is returned, the repo is free to edit.
211func repoIsAvailable(repo repository.Repo) error {
212 lockPath := repoLockFilePath(repo)
213
214 // Todo: this leave way for a racey access to the repo between the test
215 // if the file exist and the actual write. It's probably not a problem in
216 // practice because using a repository will be done from user interaction
217 // or in a context where a single instance of git-bug is already guaranteed
218 // (say, a server with the web UI running). But still, that might be nice to
219 // have a mutex or something to guard that.
220
221 // Todo: this will fail if somehow the filesystem is shared with another
222 // computer. Should add a configuration that prevent the cleaning of the
223 // lock file
224
225 f, err := os.Open(lockPath)
226
227 if err != nil && !os.IsNotExist(err) {
228 return err
229 }
230
231 if err == nil {
232 // lock file already exist
233 buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
234 if err != nil {
235 return err
236 }
237 if len(buf) == 10 {
238 return fmt.Errorf("the lock file should be < 10 bytes")
239 }
240
241 pid, err := strconv.Atoi(string(buf))
242 if err != nil {
243 return err
244 }
245
246 if process.IsRunning(pid) {
247 return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
248 }
249
250 // The lock file is just laying there after a crash, clean it
251
252 fmt.Println("A lock file is present but the corresponding process is not, removing it.")
253 err = f.Close()
254 if err != nil {
255 return err
256 }
257
258 err = os.Remove(lockPath)
259 if err != nil {
260 return err
261 }
262 }
263
264 return nil
265}