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