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