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
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 // bug loaded in memory
59 bugs map[entity.Id]*BugCache
60 // loadedBugs is an LRU cache that records which bugs the cache has loaded in
61 loadedBugs *LRUIdCache
62
63 muIdentity sync.RWMutex
64 // excerpt of identities data for all identities
65 identitiesExcerpts map[entity.Id]*IdentityExcerpt
66 // identities loaded in memory
67 identities map[entity.Id]*IdentityCache
68
69 // the user identity's id, if known
70 userIdentityId entity.Id
71}
72
73func NewRepoCache(r repository.ClockedRepo) (*RepoCache, error) {
74 return NewNamedRepoCache(r, "")
75}
76
77func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, error) {
78 c := &RepoCache{
79 repo: r,
80 name: name,
81 maxLoadedBugs: defaultMaxLoadedBugs,
82 bugs: make(map[entity.Id]*BugCache),
83 loadedBugs: NewLRUIdCache(),
84 identities: make(map[entity.Id]*IdentityCache),
85 }
86
87 err := c.lock()
88 if err != nil {
89 return &RepoCache{}, err
90 }
91
92 err = c.load()
93 if err == nil {
94 return c, nil
95 }
96
97 // Cache is either missing, broken or outdated. Rebuilding.
98 err = c.buildCache()
99 if err != nil {
100 return nil, err
101 }
102
103 return c, c.write()
104}
105
106// setCacheSize change the maximum number of loaded bugs
107func (c *RepoCache) setCacheSize(size int) {
108 c.maxLoadedBugs = size
109 c.evictIfNeeded()
110}
111
112// load will try to read from the disk all the cache files
113func (c *RepoCache) load() error {
114 err := c.loadBugCache()
115 if err != nil {
116 return err
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 lockPath := repoLockFilePath(c.repo)
132
133 err := repoIsAvailable(c.repo)
134 if err != nil {
135 return err
136 }
137
138 err = os.MkdirAll(filepath.Dir(lockPath), 0777)
139 if err != nil {
140 return err
141 }
142
143 f, err := os.Create(lockPath)
144 if err != nil {
145 return err
146 }
147
148 pid := fmt.Sprintf("%d", os.Getpid())
149 _, err = f.WriteString(pid)
150 if err != nil {
151 return err
152 }
153
154 return f.Close()
155}
156
157func (c *RepoCache) Close() error {
158 c.muBug.Lock()
159 defer c.muBug.Unlock()
160 c.muIdentity.Lock()
161 defer c.muIdentity.Unlock()
162
163 c.identities = make(map[entity.Id]*IdentityCache)
164 c.identitiesExcerpts = nil
165 c.bugs = make(map[entity.Id]*BugCache)
166 c.bugExcerpts = nil
167
168 lockPath := repoLockFilePath(c.repo)
169 return os.Remove(lockPath)
170}
171
172func (c *RepoCache) buildCache() error {
173 c.muBug.Lock()
174 defer c.muBug.Unlock()
175 c.muIdentity.Lock()
176 defer c.muIdentity.Unlock()
177
178 _, _ = fmt.Fprintf(os.Stderr, "Building identity cache... ")
179
180 c.identitiesExcerpts = make(map[entity.Id]*IdentityExcerpt)
181
182 allIdentities := identity.ReadAllLocal(c.repo)
183
184 for i := range allIdentities {
185 if i.Err != nil {
186 return i.Err
187 }
188
189 c.identitiesExcerpts[i.Identity.Id()] = NewIdentityExcerpt(i.Identity)
190 }
191
192 _, _ = fmt.Fprintln(os.Stderr, "Done.")
193
194 _, _ = fmt.Fprintf(os.Stderr, "Building bug cache... ")
195
196 c.bugExcerpts = make(map[entity.Id]*BugExcerpt)
197
198 allBugs := bug.ReadAllLocal(c.repo)
199
200 for b := range allBugs {
201 if b.Err != nil {
202 return b.Err
203 }
204
205 snap := b.Bug.Compile()
206 c.bugExcerpts[b.Bug.Id()] = NewBugExcerpt(b.Bug, &snap)
207 }
208
209 _, _ = fmt.Fprintln(os.Stderr, "Done.")
210 return nil
211}
212
213func repoLockFilePath(repo repository.Repo) string {
214 return path.Join(repo.GetPath(), "git-bug", lockfile)
215}
216
217// repoIsAvailable check is the given repository is locked by a Cache.
218// Note: this is a smart function that will cleanup the lock file if the
219// corresponding process is not there anymore.
220// If no error is returned, the repo is free to edit.
221func repoIsAvailable(repo repository.Repo) error {
222 lockPath := repoLockFilePath(repo)
223
224 // Todo: this leave way for a racey access to the repo between the test
225 // if the file exist and the actual write. It's probably not a problem in
226 // practice because using a repository will be done from user interaction
227 // or in a context where a single instance of git-bug is already guaranteed
228 // (say, a server with the web UI running). But still, that might be nice to
229 // have a mutex or something to guard that.
230
231 // Todo: this will fail if somehow the filesystem is shared with another
232 // computer. Should add a configuration that prevent the cleaning of the
233 // lock file
234
235 f, err := os.Open(lockPath)
236
237 if err != nil && !os.IsNotExist(err) {
238 return err
239 }
240
241 if err == nil {
242 // lock file already exist
243 buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
244 if err != nil {
245 return err
246 }
247 if len(buf) == 10 {
248 return fmt.Errorf("the lock file should be < 10 bytes")
249 }
250
251 pid, err := strconv.Atoi(string(buf))
252 if err != nil {
253 return err
254 }
255
256 if process.IsRunning(pid) {
257 return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
258 }
259
260 // The lock file is just laying there after a crash, clean it
261
262 fmt.Println("A lock file is present but the corresponding process is not, removing it.")
263 err = f.Close()
264 if err != nil {
265 return err
266 }
267
268 err = os.Remove(lockPath)
269 if err != nil {
270 return err
271 }
272 }
273
274 return nil
275}