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