repo_cache_bug.go

  1package cache
  2
  3import (
  4	"bytes"
  5	"encoding/gob"
  6	"errors"
  7	"fmt"
  8	"os"
  9	"path"
 10	"sort"
 11	"time"
 12
 13	"github.com/MichaelMure/git-bug/bug"
 14	"github.com/MichaelMure/git-bug/entity"
 15	"github.com/MichaelMure/git-bug/query"
 16	"github.com/MichaelMure/git-bug/repository"
 17)
 18
 19const bugCacheFile = "bug-cache"
 20
 21var errBugNotInCache = errors.New("bug missing from cache")
 22
 23func bugCacheFilePath(repo repository.Repo) string {
 24	return path.Join(repo.GetPath(), "git-bug", bugCacheFile)
 25}
 26
 27// bugUpdated is a callback to trigger when the excerpt of a bug changed,
 28// that is each time a bug is updated
 29func (c *RepoCache) bugUpdated(id entity.Id) error {
 30	c.muBug.Lock()
 31	b, ok := c.bugs[id]
 32	if !ok {
 33		c.muBug.Unlock()
 34
 35		// if the bug is not loaded at this point, it means it was loaded before
 36		// but got evicted. Which means we potentially have multiple copies in
 37		// memory and thus concurrent write.
 38		// Failing immediately here is the simple and safe solution to avoid
 39		// complicated data loss.
 40		return errBugNotInCache
 41	}
 42	c.loadedBugs.Get(id)
 43	c.bugExcerpts[id] = NewBugExcerpt(b.bug, b.Snapshot())
 44	c.muBug.Unlock()
 45
 46	// we only need to write the bug cache
 47	return c.writeBugCache()
 48}
 49
 50// load will try to read from the disk the bug cache file
 51func (c *RepoCache) loadBugCache() error {
 52	c.muBug.Lock()
 53	defer c.muBug.Unlock()
 54
 55	f, err := os.Open(bugCacheFilePath(c.repo))
 56	if err != nil {
 57		return err
 58	}
 59
 60	decoder := gob.NewDecoder(f)
 61
 62	aux := struct {
 63		Version  uint
 64		Excerpts map[entity.Id]*BugExcerpt
 65	}{}
 66
 67	err = decoder.Decode(&aux)
 68	if err != nil {
 69		return err
 70	}
 71
 72	if aux.Version != formatVersion {
 73		return fmt.Errorf("unknown cache format version %v", aux.Version)
 74	}
 75
 76	c.bugExcerpts = aux.Excerpts
 77	return nil
 78}
 79
 80// write will serialize on disk the bug cache file
 81func (c *RepoCache) writeBugCache() error {
 82	c.muBug.RLock()
 83	defer c.muBug.RUnlock()
 84
 85	var data bytes.Buffer
 86
 87	aux := struct {
 88		Version  uint
 89		Excerpts map[entity.Id]*BugExcerpt
 90	}{
 91		Version:  formatVersion,
 92		Excerpts: c.bugExcerpts,
 93	}
 94
 95	encoder := gob.NewEncoder(&data)
 96
 97	err := encoder.Encode(aux)
 98	if err != nil {
 99		return err
100	}
101
102	f, err := os.Create(bugCacheFilePath(c.repo))
103	if err != nil {
104		return err
105	}
106
107	_, err = f.Write(data.Bytes())
108	if err != nil {
109		return err
110	}
111
112	return f.Close()
113}
114
115// ResolveBugExcerpt retrieve a BugExcerpt matching the exact given id
116func (c *RepoCache) ResolveBugExcerpt(id entity.Id) (*BugExcerpt, error) {
117	c.muBug.RLock()
118	defer c.muBug.RUnlock()
119
120	excerpt, ok := c.bugExcerpts[id]
121	if !ok {
122		return nil, bug.ErrBugNotExist
123	}
124
125	return excerpt, nil
126}
127
128// ResolveBug retrieve a bug matching the exact given id
129func (c *RepoCache) ResolveBug(id entity.Id) (*BugCache, error) {
130	c.muBug.RLock()
131	cached, ok := c.bugs[id]
132	if ok {
133		c.loadedBugs.Get(id)
134		c.muBug.RUnlock()
135		return cached, nil
136	}
137	c.muBug.RUnlock()
138
139	b, err := bug.ReadLocalWithResolver(c.repo, newIdentityCacheResolver(c), id)
140	if err != nil {
141		return nil, err
142	}
143
144	cached = NewBugCache(c, b)
145
146	c.muBug.Lock()
147	c.bugs[id] = cached
148	c.loadedBugs.Add(id)
149	c.muBug.Unlock()
150
151	c.evictIfNeeded()
152
153	return cached, nil
154}
155
156// evictIfNeeded will evict a bug from the cache if needed
157// it also removes references of the bug from the bugs
158func (c *RepoCache) evictIfNeeded() {
159	c.muBug.Lock()
160	defer c.muBug.Unlock()
161	if c.loadedBugs.Len() <= c.maxLoadedBugs {
162		return
163	}
164
165	for _, id := range c.loadedBugs.GetOldestToNewest() {
166		b := c.bugs[id]
167		if b.NeedCommit() {
168			continue
169		}
170
171		b.mu.Lock()
172		c.loadedBugs.Remove(id)
173		delete(c.bugs, id)
174
175		if c.loadedBugs.Len() <= c.maxLoadedBugs {
176			return
177		}
178	}
179}
180
181// ResolveBugExcerptPrefix retrieve a BugExcerpt matching an id prefix. It fails if multiple
182// bugs match.
183func (c *RepoCache) ResolveBugExcerptPrefix(prefix string) (*BugExcerpt, error) {
184	return c.ResolveBugExcerptMatcher(func(excerpt *BugExcerpt) bool {
185		return excerpt.Id.HasPrefix(prefix)
186	})
187}
188
189// ResolveBugPrefix retrieve a bug matching an id prefix. It fails if multiple
190// bugs match.
191func (c *RepoCache) ResolveBugPrefix(prefix string) (*BugCache, error) {
192	return c.ResolveBugMatcher(func(excerpt *BugExcerpt) bool {
193		return excerpt.Id.HasPrefix(prefix)
194	})
195}
196
197// ResolveBugCreateMetadata retrieve a bug that has the exact given metadata on
198// its Create operation, that is, the first operation. It fails if multiple bugs
199// match.
200func (c *RepoCache) ResolveBugCreateMetadata(key string, value string) (*BugCache, error) {
201	return c.ResolveBugMatcher(func(excerpt *BugExcerpt) bool {
202		return excerpt.CreateMetadata[key] == value
203	})
204}
205
206func (c *RepoCache) ResolveBugExcerptMatcher(f func(*BugExcerpt) bool) (*BugExcerpt, error) {
207	id, err := c.resolveBugMatcher(f)
208	if err != nil {
209		return nil, err
210	}
211	return c.ResolveBugExcerpt(id)
212}
213
214func (c *RepoCache) ResolveBugMatcher(f func(*BugExcerpt) bool) (*BugCache, error) {
215	id, err := c.resolveBugMatcher(f)
216	if err != nil {
217		return nil, err
218	}
219	return c.ResolveBug(id)
220}
221
222func (c *RepoCache) resolveBugMatcher(f func(*BugExcerpt) bool) (entity.Id, error) {
223	c.muBug.RLock()
224	defer c.muBug.RUnlock()
225
226	// preallocate but empty
227	matching := make([]entity.Id, 0, 5)
228
229	for _, excerpt := range c.bugExcerpts {
230		if f(excerpt) {
231			matching = append(matching, excerpt.Id)
232		}
233	}
234
235	if len(matching) > 1 {
236		return entity.UnsetId, bug.NewErrMultipleMatchBug(matching)
237	}
238
239	if len(matching) == 0 {
240		return entity.UnsetId, bug.ErrBugNotExist
241	}
242
243	return matching[0], nil
244}
245
246// QueryBugs return the id of all Bug matching the given Query
247func (c *RepoCache) QueryBugs(q *query.Query) []entity.Id {
248	c.muBug.RLock()
249	defer c.muBug.RUnlock()
250
251	if q == nil {
252		return c.AllBugsIds()
253	}
254
255	matcher := compileMatcher(q.Filters)
256
257	var filtered []*BugExcerpt
258
259	for _, excerpt := range c.bugExcerpts {
260		if matcher.Match(excerpt, c) {
261			filtered = append(filtered, excerpt)
262		}
263	}
264
265	var sorter sort.Interface
266
267	switch q.OrderBy {
268	case query.OrderById:
269		sorter = BugsById(filtered)
270	case query.OrderByCreation:
271		sorter = BugsByCreationTime(filtered)
272	case query.OrderByEdit:
273		sorter = BugsByEditTime(filtered)
274	default:
275		panic("missing sort type")
276	}
277
278	switch q.OrderDirection {
279	case query.OrderAscending:
280		// Nothing to do
281	case query.OrderDescending:
282		sorter = sort.Reverse(sorter)
283	default:
284		panic("missing sort direction")
285	}
286
287	sort.Sort(sorter)
288
289	result := make([]entity.Id, len(filtered))
290
291	for i, val := range filtered {
292		result[i] = val.Id
293	}
294
295	return result
296}
297
298// AllBugsIds return all known bug ids
299func (c *RepoCache) AllBugsIds() []entity.Id {
300	c.muBug.RLock()
301	defer c.muBug.RUnlock()
302
303	result := make([]entity.Id, len(c.bugExcerpts))
304
305	i := 0
306	for _, excerpt := range c.bugExcerpts {
307		result[i] = excerpt.Id
308		i++
309	}
310
311	return result
312}
313
314// ValidLabels list valid labels
315//
316// Note: in the future, a proper label policy could be implemented where valid
317// labels are defined in a configuration file. Until that, the default behavior
318// is to return the list of labels already used.
319func (c *RepoCache) ValidLabels() []bug.Label {
320	c.muBug.RLock()
321	defer c.muBug.RUnlock()
322
323	set := map[bug.Label]interface{}{}
324
325	for _, excerpt := range c.bugExcerpts {
326		for _, l := range excerpt.Labels {
327			set[l] = nil
328		}
329	}
330
331	result := make([]bug.Label, len(set))
332
333	i := 0
334	for l := range set {
335		result[i] = l
336		i++
337	}
338
339	// Sort
340	sort.Slice(result, func(i, j int) bool {
341		return string(result[i]) < string(result[j])
342	})
343
344	return result
345}
346
347// NewBug create a new bug
348// The new bug is written in the repository (commit)
349func (c *RepoCache) NewBug(title string, message string) (*BugCache, *bug.CreateOperation, error) {
350	return c.NewBugWithFiles(title, message, nil)
351}
352
353// NewBugWithFiles create a new bug with attached files for the message
354// The new bug is written in the repository (commit)
355func (c *RepoCache) NewBugWithFiles(title string, message string, files []repository.Hash) (*BugCache, *bug.CreateOperation, error) {
356	author, err := c.GetUserIdentity()
357	if err != nil {
358		return nil, nil, err
359	}
360
361	return c.NewBugRaw(author, time.Now().Unix(), title, message, files, nil)
362}
363
364// NewBugWithFilesMeta create a new bug with attached files for the message, as
365// well as metadata for the Create operation.
366// The new bug is written in the repository (commit)
367func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title string, message string, files []repository.Hash, metadata map[string]string) (*BugCache, *bug.CreateOperation, error) {
368	b, op, err := bug.CreateWithFiles(author.Identity, unixTime, title, message, files)
369	if err != nil {
370		return nil, nil, err
371	}
372
373	for key, value := range metadata {
374		op.SetMetadata(key, value)
375	}
376
377	err = b.Commit(c.repo)
378	if err != nil {
379		return nil, nil, err
380	}
381
382	c.muBug.Lock()
383	if _, has := c.bugs[b.Id()]; has {
384		c.muBug.Unlock()
385		return nil, nil, fmt.Errorf("bug %s already exist in the cache", b.Id())
386	}
387
388	cached := NewBugCache(c, b)
389	c.bugs[b.Id()] = cached
390	c.loadedBugs.Add(b.Id())
391	c.muBug.Unlock()
392
393	c.evictIfNeeded()
394
395	// force the write of the excerpt
396	err = c.bugUpdated(b.Id())
397	if err != nil {
398		return nil, nil, err
399	}
400
401	return cached, op, nil
402}
403
404// RemoveBug removes a bug from the cache and repo given a bug id prefix
405func (c *RepoCache) RemoveBug(prefix string) error {
406	c.muBug.RLock()
407
408	b, err := c.ResolveBugPrefix(prefix)
409	if err != nil {
410		c.muBug.RUnlock()
411		return err
412	}
413	c.muBug.RUnlock()
414
415	c.muBug.Lock()
416	err = bug.RemoveBug(c.repo, b.Id())
417
418	delete(c.bugs, b.Id())
419	delete(c.bugExcerpts, b.Id())
420	c.loadedBugs.Remove(b.Id())
421
422	c.muBug.Unlock()
423
424	return c.writeBugCache()
425}