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