1package cache
2
3import (
4 "fmt"
5 "io"
6 "io/ioutil"
7 "os"
8 "strconv"
9 "sync"
10
11 "github.com/MichaelMure/git-bug/entities/bug"
12 "github.com/MichaelMure/git-bug/entities/identity"
13 "github.com/MichaelMure/git-bug/entity"
14 "github.com/MichaelMure/git-bug/repository"
15 "github.com/MichaelMure/git-bug/util/process"
16)
17
18// 1: original format
19// 2: added cache for identities with a reference in the bug cache
20// 3: no more legacy identity
21// 4: entities make their IDs from data, not git commit
22const formatVersion = 4
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 // resolvers for all known entities
53 resolvers entity.Resolvers
54
55 // maximum number of loaded bugs
56 maxLoadedBugs int
57
58 muBug sync.RWMutex
59 // excerpt of bugs data for all bugs
60 bugExcerpts map[entity.Id]*BugExcerpt
61 // bug loaded in memory
62 bugs map[entity.Id]*BugCache
63 // loadedBugs is an LRU cache that records which bugs the cache has loaded in
64 loadedBugs *LRUIdCache
65
66 muIdentity sync.RWMutex
67 // excerpt of identities data for all identities
68 identitiesExcerpts map[entity.Id]*IdentityExcerpt
69 // identities loaded in memory
70 identities map[entity.Id]*IdentityCache
71
72 // the user identity's id, if known
73 userIdentityId entity.Id
74
75 // the io.Writer where messages to (human) users should be written
76 stderr io.Writer
77}
78
79func NewRepoCache(r repository.ClockedRepo, stderr io.Writer) (*RepoCache, error) {
80 return NewNamedRepoCache(r, "", stderr)
81}
82
83func NewNamedRepoCache(r repository.ClockedRepo, name string, stderr io.Writer) (*RepoCache, error) {
84 c := &RepoCache{
85 repo: r,
86 name: name,
87 maxLoadedBugs: defaultMaxLoadedBugs,
88 bugs: make(map[entity.Id]*BugCache),
89 loadedBugs: NewLRUIdCache(),
90 identities: make(map[entity.Id]*IdentityCache),
91 stderr: stderr,
92 }
93
94 c.resolvers = makeResolvers(c)
95
96 err := c.lock()
97 if err != nil {
98 return &RepoCache{}, err
99 }
100
101 err = c.load()
102 if err == nil {
103 return c, nil
104 }
105
106 // Cache is either missing, broken or outdated. Rebuilding.
107 err = c.buildCache()
108 if err != nil {
109 return nil, err
110 }
111
112 return c, c.write()
113}
114
115// setCacheSize change the maximum number of loaded bugs
116func (c *RepoCache) setCacheSize(size int) {
117 c.maxLoadedBugs = size
118 c.evictIfNeeded()
119}
120
121// load will try to read from the disk all the cache files
122func (c *RepoCache) load() error {
123 err := c.loadBugCache()
124 if err != nil {
125 return err
126 }
127
128 return c.loadIdentityCache()
129}
130
131// write will serialize on disk all the cache files
132func (c *RepoCache) write() error {
133 err := c.writeBugCache()
134 if err != nil {
135 return err
136 }
137 return c.writeIdentityCache()
138}
139
140func (c *RepoCache) lock() error {
141 err := repoIsAvailable(c.repo)
142 if err != nil {
143 return err
144 }
145
146 f, err := c.repo.LocalStorage().Create(lockfile)
147 if err != nil {
148 return err
149 }
150
151 pid := fmt.Sprintf("%d", os.Getpid())
152 _, err = f.Write([]byte(pid))
153 if err != nil {
154 return err
155 }
156
157 return f.Close()
158}
159
160func (c *RepoCache) Close() error {
161 c.muBug.Lock()
162 defer c.muBug.Unlock()
163 c.muIdentity.Lock()
164 defer c.muIdentity.Unlock()
165
166 c.identities = make(map[entity.Id]*IdentityCache)
167 c.identitiesExcerpts = nil
168 c.bugs = make(map[entity.Id]*BugCache)
169 c.bugExcerpts = nil
170
171 err := c.repo.Close()
172 if err != nil {
173 return err
174 }
175
176 return c.repo.LocalStorage().Remove(lockfile)
177}
178
179func (c *RepoCache) buildCache() error {
180 _, _ = fmt.Fprintf(c.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(c.stderr, "Done.")
195
196 _, _ = fmt.Fprintf(c.stderr, "Building bug cache... ")
197
198 c.bugExcerpts = make(map[entity.Id]*BugExcerpt)
199
200 allBugs := bug.ReadAllWithResolver(c.repo, c.resolvers)
201
202 // wipe the index just to be sure
203 err := c.repo.ClearBleveIndex("bug")
204 if err != nil {
205 return err
206 }
207
208 for b := range allBugs {
209 if b.Err != nil {
210 return b.Err
211 }
212
213 snap := b.Bug.Compile()
214 c.bugExcerpts[b.Bug.Id()] = NewBugExcerpt(b.Bug, snap)
215
216 if err := c.addBugToSearchIndex(snap); err != nil {
217 return err
218 }
219 }
220
221 _, _ = fmt.Fprintln(c.stderr, "Done.")
222
223 return nil
224}
225
226// repoIsAvailable check is the given repository is locked by a Cache.
227// Note: this is a smart function that will clean the lock file if the
228// corresponding process is not there anymore.
229// If no error is returned, the repo is free to edit.
230func repoIsAvailable(repo repository.RepoStorage) error {
231 // Todo: this leave way for a racey access to the repo between the test
232 // if the file exist and the actual write. It's probably not a problem in
233 // practice because using a repository will be done from user interaction
234 // or in a context where a single instance of git-bug is already guaranteed
235 // (say, a server with the web UI running). But still, that might be nice to
236 // have a mutex or something to guard that.
237
238 // Todo: this will fail if somehow the filesystem is shared with another
239 // computer. Should add a configuration that prevent the cleaning of the
240 // lock file
241
242 f, err := repo.LocalStorage().Open(lockfile)
243 if err != nil && !os.IsNotExist(err) {
244 return err
245 }
246
247 if err == nil {
248 // lock file already exist
249 buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
250 if err != nil {
251 return err
252 }
253 if len(buf) == 10 {
254 return fmt.Errorf("the lock file should be < 10 bytes")
255 }
256
257 pid, err := strconv.Atoi(string(buf))
258 if err != nil {
259 return err
260 }
261
262 if process.IsRunning(pid) {
263 return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
264 }
265
266 // The lock file is just laying there after a crash, clean it
267
268 fmt.Println("A lock file is present but the corresponding process is not, removing it.")
269 err = f.Close()
270 if err != nil {
271 return err
272 }
273
274 err = repo.LocalStorage().Remove(lockfile)
275 if err != nil {
276 return err
277 }
278 }
279
280 return nil
281}