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 a cache on top of a raw repository.
76// The caller is expected to read all returned events before the cache is considered
77// ready to use.
78func NewRepoCache(r repository.ClockedRepo) (*RepoCache, chan BuildEvent) {
79 return NewNamedRepoCache(r, defaultRepoName)
80}
81
82// NewNamedRepoCache create or open a named cache on top of a raw repository.
83// The caller is expected to read all returned events before the cache is considered
84// ready to use.
85func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, chan BuildEvent) {
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)
106
107 go func() {
108 defer close(events)
109
110 err := c.lock(events)
111 if err != nil {
112 events <- BuildEvent{Err: err}
113 return
114 }
115
116 err = c.load()
117 if err == nil {
118 return
119 }
120
121 // Cache is either missing, broken or outdated. Rebuilding.
122 c.buildCache(events)
123 }()
124
125 return c, events
126}
127
128func NewRepoCacheNoEvents(r repository.ClockedRepo) (*RepoCache, error) {
129 cache, events := NewRepoCache(r)
130 for event := range events {
131 if event.Err != nil {
132 for range events {
133 }
134 return nil, event.Err
135 }
136 }
137 return cache, nil
138}
139
140// Bugs gives access to the Bug entities
141func (c *RepoCache) Bugs() *RepoCacheBug {
142 return c.bugs
143}
144
145// Identities gives access to the Identity entities
146func (c *RepoCache) Identities() *RepoCacheIdentity {
147 return c.identities
148}
149
150func (c *RepoCache) getResolvers() entity.Resolvers {
151 return c.resolvers
152}
153
154// setCacheSize change the maximum number of loaded bugs
155func (c *RepoCache) setCacheSize(size int) {
156 for _, subcache := range c.subcaches {
157 subcache.SetCacheSize(size)
158 }
159}
160
161// load will try to read from the disk all the cache files
162func (c *RepoCache) load() error {
163 var errWait multierr.ErrWaitGroup
164 for _, mgmt := range c.subcaches {
165 errWait.Go(mgmt.Load)
166 }
167 return errWait.Wait()
168}
169
170func (c *RepoCache) lock(events chan BuildEvent) error {
171 err := repoIsAvailable(c.repo, events)
172 if err != nil {
173 return err
174 }
175
176 f, err := c.repo.LocalStorage().Create(lockfile)
177 if err != nil {
178 return err
179 }
180
181 pid := fmt.Sprintf("%d", os.Getpid())
182 _, err = f.Write([]byte(pid))
183 if err != nil {
184 return err
185 }
186
187 return f.Close()
188}
189
190func (c *RepoCache) Close() error {
191 var errWait multierr.ErrWaitGroup
192 for _, mgmt := range c.subcaches {
193 errWait.Go(mgmt.Close)
194 }
195 err := errWait.Wait()
196 if err != nil {
197 return err
198 }
199
200 err = c.repo.Close()
201 if err != nil {
202 return err
203 }
204
205 return c.repo.LocalStorage().Remove(lockfile)
206}
207
208type BuildEventType int
209
210const (
211 _ BuildEventType = iota
212 BuildEventCacheIsBuilt
213 BuildEventRemoveLock
214 BuildEventStarted
215 BuildEventFinished
216)
217
218// BuildEvent carry an event happening during the cache build process.
219type BuildEvent struct {
220 // Err carry an error if the build process failed. If set, no other field matter.
221 Err error
222 // Typename is the name of the entity of which the event relate to. Can be empty if not particular entity is involved.
223 Typename string
224 // Event is the type of the event.
225 Event BuildEventType
226}
227
228func (c *RepoCache) buildCache(events chan BuildEvent) {
229 events <- BuildEvent{Event: BuildEventCacheIsBuilt}
230
231 var wg sync.WaitGroup
232 for _, subcache := range c.subcaches {
233 wg.Add(1)
234 go func(subcache cacheMgmt) {
235 defer wg.Done()
236 events <- BuildEvent{
237 Typename: subcache.Typename(),
238 Event: BuildEventStarted,
239 }
240
241 err := subcache.Build()
242 if err != nil {
243 events <- BuildEvent{
244 Typename: subcache.Typename(),
245 Err: err,
246 }
247 return
248 }
249
250 events <- BuildEvent{
251 Typename: subcache.Typename(),
252 Event: BuildEventFinished,
253 }
254 }(subcache)
255 }
256 wg.Wait()
257}
258
259// repoIsAvailable check is the given repository is locked by a Cache.
260// Note: this is a smart function that will clean the lock file if the
261// corresponding process is not there anymore.
262// If no error is returned, the repo is free to edit.
263func repoIsAvailable(repo repository.RepoStorage, events chan BuildEvent) error {
264 // Todo: this leave way for a racey access to the repo between the test
265 // if the file exist and the actual write. It's probably not a problem in
266 // practice because using a repository will be done from user interaction
267 // or in a context where a single instance of git-bug is already guaranteed
268 // (say, a server with the web UI running). But still, that might be nice to
269 // have a mutex or something to guard that.
270
271 // Todo: this will fail if somehow the filesystem is shared with another
272 // computer. Should add a configuration that prevent the cleaning of the
273 // lock file
274
275 f, err := repo.LocalStorage().Open(lockfile)
276 if err != nil && !os.IsNotExist(err) {
277 return err
278 }
279
280 if err == nil {
281 // lock file already exist
282 buf, err := ioutil.ReadAll(io.LimitReader(f, 10))
283 if err != nil {
284 return err
285 }
286 if len(buf) == 10 {
287 return fmt.Errorf("the lock file should be < 10 bytes")
288 }
289
290 pid, err := strconv.Atoi(string(buf))
291 if err != nil {
292 return err
293 }
294
295 if process.IsRunning(pid) {
296 return fmt.Errorf("the repository you want to access is already locked by the process pid %d", pid)
297 }
298
299 // The lock file is just laying there after a crash, clean it
300
301 events <- BuildEvent{Event: BuildEventRemoveLock}
302 err = f.Close()
303 if err != nil {
304 return err
305 }
306
307 err = repo.LocalStorage().Remove(lockfile)
308 if err != nil {
309 return err
310 }
311 }
312
313 return nil
314}