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