repo_cache_bug.go

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