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