repo_cache_bug.go

  1package cache
  2
  3import (
  4	"bytes"
  5	"encoding/gob"
  6	"errors"
  7	"fmt"
  8	"sort"
  9	"strings"
 10	"time"
 11
 12	"github.com/MichaelMure/git-bug/bug"
 13	"github.com/MichaelMure/git-bug/entity"
 14	"github.com/MichaelMure/git-bug/query"
 15	"github.com/MichaelMure/git-bug/repository"
 16	"github.com/blevesearch/bleve"
 17)
 18
 19const (
 20	bugCacheFile   = "bug-cache"
 21	searchCacheDir = "search-cache"
 22)
 23
 24var errBugNotInCache = errors.New("bug missing from cache")
 25
 26// bugUpdated is a callback to trigger when the excerpt of a bug changed,
 27// that is each time a bug is updated
 28func (c *RepoCache) bugUpdated(id entity.Id) error {
 29	c.muBug.Lock()
 30	b, ok := c.bugs[id]
 31	if !ok {
 32		c.muBug.Unlock()
 33
 34		// if the bug is not loaded at this point, it means it was loaded before
 35		// but got evicted. Which means we potentially have multiple copies in
 36		// memory and thus concurrent write.
 37		// Failing immediately here is the simple and safe solution to avoid
 38		// complicated data loss.
 39		return errBugNotInCache
 40	}
 41	c.loadedBugs.Get(id)
 42	c.bugExcerpts[id] = NewBugExcerpt(b.bug, b.Snapshot())
 43	c.muBug.Unlock()
 44
 45	if err := c.addBugToSearchIndex(b.Snapshot()); err != nil {
 46		return err
 47	}
 48
 49	// we only need to write the bug cache
 50	return c.writeBugCache()
 51}
 52
 53// load will try to read from the disk the bug cache file
 54func (c *RepoCache) loadBugCache() error {
 55	c.muBug.Lock()
 56	defer c.muBug.Unlock()
 57
 58	f, err := c.repo.LocalStorage().Open(bugCacheFile)
 59	if err != nil {
 60		return err
 61	}
 62
 63	decoder := gob.NewDecoder(f)
 64
 65	aux := struct {
 66		Version  uint
 67		Excerpts map[entity.Id]*BugExcerpt
 68	}{}
 69
 70	err = decoder.Decode(&aux)
 71	if err != nil {
 72		return err
 73	}
 74
 75	if aux.Version != formatVersion {
 76		return fmt.Errorf("unknown cache format version %v", aux.Version)
 77	}
 78
 79	c.bugExcerpts = aux.Excerpts
 80
 81	index, err := c.repo.GetBleveIndex("bug")
 82	if err != nil {
 83		return err
 84	}
 85
 86	// simple heuristic to detect a mismatch between the index and the bugs
 87	count, err := index.DocCount()
 88	if err != nil {
 89		return err
 90	}
 91	if count != uint64(len(c.bugExcerpts)) {
 92		return fmt.Errorf("count mismatch between bleve and bug excerpts")
 93	}
 94
 95	return nil
 96}
 97
 98// write will serialize on disk the bug cache file
 99func (c *RepoCache) writeBugCache() error {
100	c.muBug.RLock()
101	defer c.muBug.RUnlock()
102
103	var data bytes.Buffer
104
105	aux := struct {
106		Version  uint
107		Excerpts map[entity.Id]*BugExcerpt
108	}{
109		Version:  formatVersion,
110		Excerpts: c.bugExcerpts,
111	}
112
113	encoder := gob.NewEncoder(&data)
114
115	err := encoder.Encode(aux)
116	if err != nil {
117		return err
118	}
119
120	f, err := c.repo.LocalStorage().Create(bugCacheFile)
121	if err != nil {
122		return err
123	}
124
125	_, err = f.Write(data.Bytes())
126	if err != nil {
127		return err
128	}
129
130	return f.Close()
131}
132
133// ResolveBugExcerpt retrieve a BugExcerpt matching the exact given id
134func (c *RepoCache) ResolveBugExcerpt(id entity.Id) (*BugExcerpt, error) {
135	c.muBug.RLock()
136	defer c.muBug.RUnlock()
137
138	excerpt, ok := c.bugExcerpts[id]
139	if !ok {
140		return nil, bug.ErrBugNotExist
141	}
142
143	return excerpt, nil
144}
145
146// ResolveBug retrieve a bug matching the exact given id
147func (c *RepoCache) ResolveBug(id entity.Id) (*BugCache, error) {
148	c.muBug.RLock()
149	cached, ok := c.bugs[id]
150	if ok {
151		c.loadedBugs.Get(id)
152		c.muBug.RUnlock()
153		return cached, nil
154	}
155	c.muBug.RUnlock()
156
157	b, err := bug.ReadLocalWithResolver(c.repo, newIdentityCacheResolver(c), id)
158	if err != nil {
159		return nil, err
160	}
161
162	cached = NewBugCache(c, b)
163
164	c.muBug.Lock()
165	c.bugs[id] = cached
166	c.loadedBugs.Add(id)
167	c.muBug.Unlock()
168
169	c.evictIfNeeded()
170
171	return cached, nil
172}
173
174// evictIfNeeded will evict a bug from the cache if needed
175// it also removes references of the bug from the bugs
176func (c *RepoCache) evictIfNeeded() {
177	c.muBug.Lock()
178	defer c.muBug.Unlock()
179	if c.loadedBugs.Len() <= c.maxLoadedBugs {
180		return
181	}
182
183	for _, id := range c.loadedBugs.GetOldestToNewest() {
184		b := c.bugs[id]
185		if b.NeedCommit() {
186			continue
187		}
188
189		b.mu.Lock()
190		c.loadedBugs.Remove(id)
191		delete(c.bugs, id)
192
193		if c.loadedBugs.Len() <= c.maxLoadedBugs {
194			return
195		}
196	}
197}
198
199// ResolveBugExcerptPrefix retrieve a BugExcerpt matching an id prefix. It fails if multiple
200// bugs match.
201func (c *RepoCache) ResolveBugExcerptPrefix(prefix string) (*BugExcerpt, error) {
202	return c.ResolveBugExcerptMatcher(func(excerpt *BugExcerpt) bool {
203		return excerpt.Id.HasPrefix(prefix)
204	})
205}
206
207// ResolveBugPrefix retrieve a bug matching an id prefix. It fails if multiple
208// bugs match.
209func (c *RepoCache) ResolveBugPrefix(prefix string) (*BugCache, error) {
210	return c.ResolveBugMatcher(func(excerpt *BugExcerpt) bool {
211		return excerpt.Id.HasPrefix(prefix)
212	})
213}
214
215// ResolveBugCreateMetadata retrieve a bug that has the exact given metadata on
216// its Create operation, that is, the first operation. It fails if multiple bugs
217// match.
218func (c *RepoCache) ResolveBugCreateMetadata(key string, value string) (*BugCache, error) {
219	return c.ResolveBugMatcher(func(excerpt *BugExcerpt) bool {
220		return excerpt.CreateMetadata[key] == value
221	})
222}
223
224func (c *RepoCache) ResolveBugExcerptMatcher(f func(*BugExcerpt) bool) (*BugExcerpt, error) {
225	id, err := c.resolveBugMatcher(f)
226	if err != nil {
227		return nil, err
228	}
229	return c.ResolveBugExcerpt(id)
230}
231
232func (c *RepoCache) ResolveBugMatcher(f func(*BugExcerpt) bool) (*BugCache, error) {
233	id, err := c.resolveBugMatcher(f)
234	if err != nil {
235		return nil, err
236	}
237	return c.ResolveBug(id)
238}
239
240func (c *RepoCache) resolveBugMatcher(f func(*BugExcerpt) bool) (entity.Id, error) {
241	c.muBug.RLock()
242	defer c.muBug.RUnlock()
243
244	// preallocate but empty
245	matching := make([]entity.Id, 0, 5)
246
247	for _, excerpt := range c.bugExcerpts {
248		if f(excerpt) {
249			matching = append(matching, excerpt.Id)
250		}
251	}
252
253	if len(matching) > 1 {
254		return entity.UnsetId, bug.NewErrMultipleMatchBug(matching)
255	}
256
257	if len(matching) == 0 {
258		return entity.UnsetId, bug.ErrBugNotExist
259	}
260
261	return matching[0], nil
262}
263
264// ResolveComment search for a Bug/Comment combination matching the merged
265// bug/comment Id prefix. Returns the Bug containing the Comment and the Comment's
266// Id.
267func (c *RepoCache) ResolveComment(prefix string) (*BugCache, entity.Id, error) {
268	bugPrefix, _ := entity.SeparateIds(prefix)
269	bugCandidate := make([]entity.Id, 0, 5)
270
271	// build a list of possible matching bugs
272	c.muBug.RLock()
273	for _, excerpt := range c.bugExcerpts {
274		if excerpt.Id.HasPrefix(bugPrefix) {
275			bugCandidate = append(bugCandidate, excerpt.Id)
276		}
277	}
278	c.muBug.RUnlock()
279
280	matchingBugIds := make([]entity.Id, 0, 5)
281	matchingCommentId := entity.UnsetId
282	var matchingBug *BugCache
283
284	// search for matching comments
285	// searching every bug candidate allow for some collision with the bug prefix only,
286	// before being refined with the full comment prefix
287	for _, bugId := range bugCandidate {
288		b, err := c.ResolveBug(bugId)
289		if err != nil {
290			return nil, entity.UnsetId, err
291		}
292
293		for _, comment := range b.Snapshot().Comments {
294			if comment.Id().HasPrefix(prefix) {
295				matchingBugIds = append(matchingBugIds, bugId)
296				matchingBug = b
297				matchingCommentId = comment.Id()
298			}
299		}
300	}
301
302	if len(matchingBugIds) > 1 {
303		return nil, entity.UnsetId, entity.NewErrMultipleMatch("bug/comment", matchingBugIds)
304	} else if len(matchingBugIds) == 0 {
305		return nil, entity.UnsetId, errors.New("comment doesn't exist")
306	}
307
308	return matchingBug, matchingCommentId, nil
309}
310
311// QueryBugs return the id of all Bug matching the given Query
312func (c *RepoCache) QueryBugs(q *query.Query) ([]entity.Id, error) {
313	c.muBug.RLock()
314	defer c.muBug.RUnlock()
315
316	if q == nil {
317		return c.AllBugsIds(), nil
318	}
319
320	matcher := compileMatcher(q.Filters)
321
322	var filtered []*BugExcerpt
323	var foundBySearch map[entity.Id]*BugExcerpt
324
325	if q.Search != nil {
326		foundBySearch = map[entity.Id]*BugExcerpt{}
327
328		terms := make([]string, len(q.Search))
329		copy(terms, q.Search)
330		for i, search := range q.Search {
331			if strings.Contains(search, " ") {
332				terms[i] = fmt.Sprintf("\"%s\"", search)
333			}
334		}
335
336		bleveQuery := bleve.NewQueryStringQuery(strings.Join(terms, " "))
337		bleveSearch := bleve.NewSearchRequest(bleveQuery)
338
339		index, err := c.repo.GetBleveIndex("bug")
340		if err != nil {
341			return nil, err
342		}
343
344		searchResults, err := index.Search(bleveSearch)
345		if err != nil {
346			return nil, err
347		}
348
349		for _, hit := range searchResults.Hits {
350			foundBySearch[entity.Id(hit.ID)] = c.bugExcerpts[entity.Id(hit.ID)]
351		}
352	} else {
353		foundBySearch = c.bugExcerpts
354	}
355
356	for _, excerpt := range foundBySearch {
357		if matcher.Match(excerpt, c) {
358			filtered = append(filtered, excerpt)
359		}
360	}
361
362	var sorter sort.Interface
363
364	switch q.OrderBy {
365	case query.OrderById:
366		sorter = BugsById(filtered)
367	case query.OrderByCreation:
368		sorter = BugsByCreationTime(filtered)
369	case query.OrderByEdit:
370		sorter = BugsByEditTime(filtered)
371	default:
372		return nil, errors.New("missing sort type")
373	}
374
375	switch q.OrderDirection {
376	case query.OrderAscending:
377		// Nothing to do
378	case query.OrderDescending:
379		sorter = sort.Reverse(sorter)
380	default:
381		return nil, errors.New("missing sort direction")
382	}
383
384	sort.Sort(sorter)
385
386	result := make([]entity.Id, len(filtered))
387
388	for i, val := range filtered {
389		result[i] = val.Id
390	}
391
392	return result, nil
393}
394
395// AllBugsIds return all known bug ids
396func (c *RepoCache) AllBugsIds() []entity.Id {
397	c.muBug.RLock()
398	defer c.muBug.RUnlock()
399
400	result := make([]entity.Id, len(c.bugExcerpts))
401
402	i := 0
403	for _, excerpt := range c.bugExcerpts {
404		result[i] = excerpt.Id
405		i++
406	}
407
408	return result
409}
410
411// ValidLabels list valid labels
412//
413// Note: in the future, a proper label policy could be implemented where valid
414// labels are defined in a configuration file. Until that, the default behavior
415// is to return the list of labels already used.
416func (c *RepoCache) ValidLabels() []bug.Label {
417	c.muBug.RLock()
418	defer c.muBug.RUnlock()
419
420	set := map[bug.Label]interface{}{}
421
422	for _, excerpt := range c.bugExcerpts {
423		for _, l := range excerpt.Labels {
424			set[l] = nil
425		}
426	}
427
428	result := make([]bug.Label, len(set))
429
430	i := 0
431	for l := range set {
432		result[i] = l
433		i++
434	}
435
436	// Sort
437	sort.Slice(result, func(i, j int) bool {
438		return string(result[i]) < string(result[j])
439	})
440
441	return result
442}
443
444// NewBug create a new bug
445// The new bug is written in the repository (commit)
446func (c *RepoCache) NewBug(title string, message string) (*BugCache, *bug.CreateOperation, error) {
447	return c.NewBugWithFiles(title, message, nil)
448}
449
450// NewBugWithFiles create a new bug with attached files for the message
451// The new bug is written in the repository (commit)
452func (c *RepoCache) NewBugWithFiles(title string, message string, files []repository.Hash) (*BugCache, *bug.CreateOperation, error) {
453	author, err := c.GetUserIdentity()
454	if err != nil {
455		return nil, nil, err
456	}
457
458	return c.NewBugRaw(author, time.Now().Unix(), title, message, files, nil)
459}
460
461// NewBugWithFilesMeta create a new bug with attached files for the message, as
462// well as metadata for the Create operation.
463// The new bug is written in the repository (commit)
464func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title string, message string, files []repository.Hash, metadata map[string]string) (*BugCache, *bug.CreateOperation, error) {
465	b, op, err := bug.CreateWithFiles(author.Identity, unixTime, title, message, files)
466	if err != nil {
467		return nil, nil, err
468	}
469
470	for key, value := range metadata {
471		op.SetMetadata(key, value)
472	}
473
474	err = b.Commit(c.repo)
475	if err != nil {
476		return nil, nil, err
477	}
478
479	c.muBug.Lock()
480	if _, has := c.bugs[b.Id()]; has {
481		c.muBug.Unlock()
482		return nil, nil, fmt.Errorf("bug %s already exist in the cache", b.Id())
483	}
484
485	cached := NewBugCache(c, b)
486	c.bugs[b.Id()] = cached
487	c.loadedBugs.Add(b.Id())
488	c.muBug.Unlock()
489
490	c.evictIfNeeded()
491
492	// force the write of the excerpt
493	err = c.bugUpdated(b.Id())
494	if err != nil {
495		return nil, nil, err
496	}
497
498	return cached, op, nil
499}
500
501// RemoveBug removes a bug from the cache and repo given a bug id prefix
502func (c *RepoCache) RemoveBug(prefix string) error {
503	c.muBug.RLock()
504
505	b, err := c.ResolveBugPrefix(prefix)
506	if err != nil {
507		c.muBug.RUnlock()
508		return err
509	}
510	c.muBug.RUnlock()
511
512	c.muBug.Lock()
513	err = bug.RemoveBug(c.repo, b.Id())
514
515	delete(c.bugs, b.Id())
516	delete(c.bugExcerpts, b.Id())
517	c.loadedBugs.Remove(b.Id())
518
519	c.muBug.Unlock()
520
521	return c.writeBugCache()
522}
523
524func (c *RepoCache) addBugToSearchIndex(snap *bug.Snapshot) error {
525	searchableBug := struct {
526		Text []string
527	}{}
528
529	for _, comment := range snap.Comments {
530		searchableBug.Text = append(searchableBug.Text, comment.Message)
531	}
532
533	searchableBug.Text = append(searchableBug.Text, snap.Title)
534
535	index, err := c.repo.GetBleveIndex("bug")
536	if err != nil {
537		return err
538	}
539
540	err = index.Index(snap.Id().String(), searchableBug)
541	if err != nil {
542		return err
543	}
544
545	return nil
546}