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