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