diff options
author | Michael Muré <batolettre@gmail.com> | 2020-12-08 14:42:13 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-12-08 14:42:13 +0100 |
commit | c884d557bf5e0ebdbe6e3b20535af24c2e97d29f (patch) | |
tree | 3bb95fcf0d6a2a19e6915cdcc1da4d70bc448bb4 /cache | |
parent | 71e1303234ef227851998f08c98c7c69670c9966 (diff) | |
download | git-bug-c884d557bf5e0ebdbe6e3b20535af24c2e97d29f.tar.gz |
repo: move bleve there
Diffstat (limited to 'cache')
-rw-r--r-- | cache/repo_cache.go | 14 | ||||
-rw-r--r-- | cache/repo_cache_bug.go | 60 | ||||
-rw-r--r-- | cache/repo_cache_test.go | 4 |
3 files changed, 32 insertions, 46 deletions
diff --git a/cache/repo_cache.go b/cache/repo_cache.go index a08744cc..b5b9ee54 100644 --- a/cache/repo_cache.go +++ b/cache/repo_cache.go @@ -13,7 +13,6 @@ import ( "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/repository" "github.com/MichaelMure/git-bug/util/process" - "github.com/blevesearch/bleve" ) // 1: original format @@ -55,8 +54,6 @@ type RepoCache struct { muBug sync.RWMutex // excerpt of bugs data for all bugs bugExcerpts map[entity.Id]*BugExcerpt - // searchable cache of all bugs - searchCache bleve.Index // bug loaded in memory bugs map[entity.Id]*BugCache // loadedBugs is an LRU cache that records which bugs the cache has loaded in @@ -161,9 +158,9 @@ func (c *RepoCache) Close() error { c.bugs = make(map[entity.Id]*BugCache) c.bugExcerpts = nil - if c.searchCache != nil { - c.searchCache.Close() - c.searchCache = nil + err := c.repo.Close() + if err != nil { + return err } return c.repo.LocalStorage().Remove(lockfile) @@ -199,9 +196,10 @@ func (c *RepoCache) buildCache() error { allBugs := bug.ReadAllLocal(c.repo) - err := c.createBleveIndex() + // wipe the index just to be sure + err := c.repo.ClearBleveIndex("bug") if err != nil { - return fmt.Errorf("Unable to create search cache. Error: %v", err) + return err } for b := range allBugs { diff --git a/cache/repo_cache_bug.go b/cache/repo_cache_bug.go index f540e51b..1701f66d 100644 --- a/cache/repo_cache_bug.go +++ b/cache/repo_cache_bug.go @@ -23,10 +23,6 @@ const ( var errBugNotInCache = errors.New("bug missing from cache") -func searchCacheDirPath(repo repository.Repo) string { - return path.Join(repo.GetPath(), "git-bug", searchCacheDir) -} - // bugUpdated is a callback to trigger when the excerpt of a bug changed, // that is each time a bug is updated func (c *RepoCache) bugUpdated(id entity.Id) error { @@ -82,14 +78,13 @@ func (c *RepoCache) loadBugCache() error { c.bugExcerpts = aux.Excerpts - blevePath := searchCacheDirPath(c.repo) - searchCache, err := bleve.Open(blevePath) + index, err := c.repo.GetBleveIndex("bug") if err != nil { - return fmt.Errorf("Unable to open search cache. Error: %v", err) + return err } - c.searchCache = searchCache - count, err := c.searchCache.DocCount() + // simple heuristic to detect a mismatch between the index and the bugs + count, err := index.DocCount() if err != nil { return err } @@ -100,26 +95,6 @@ func (c *RepoCache) loadBugCache() error { return nil } -func (c *RepoCache) createBleveIndex() error { - blevePath := searchCacheDirPath(c.repo) - - _ = os.RemoveAll(blevePath) - - mapping := bleve.NewIndexMapping() - mapping.DefaultAnalyzer = "en" - - dir := searchCacheDirPath(c.repo) - - bleveIndex, err := bleve.New(dir, mapping) - if err != nil { - return err - } - - c.searchCache = bleveIndex - - return nil -} - // write will serialize on disk the bug cache file func (c *RepoCache) writeBugCache() error { c.muBug.RLock() @@ -287,12 +262,12 @@ func (c *RepoCache) resolveBugMatcher(f func(*BugExcerpt) bool) (entity.Id, erro } // QueryBugs return the id of all Bug matching the given Query -func (c *RepoCache) QueryBugs(q *query.Query) []entity.Id { +func (c *RepoCache) QueryBugs(q *query.Query) ([]entity.Id, error) { c.muBug.RLock() defer c.muBug.RUnlock() if q == nil { - return c.AllBugsIds() + return c.AllBugsIds(), nil } matcher := compileMatcher(q.Filters) @@ -313,9 +288,15 @@ func (c *RepoCache) QueryBugs(q *query.Query) []entity.Id { bleveQuery := bleve.NewQueryStringQuery(strings.Join(terms, " ")) bleveSearch := bleve.NewSearchRequest(bleveQuery) - searchResults, err := c.searchCache.Search(bleveSearch) + + index, err := c.repo.GetBleveIndex("bug") + if err != nil { + return nil, err + } + + searchResults, err := index.Search(bleveSearch) if err != nil { - panic("bleve search failed") + return nil, err } for _, hit := range searchResults.Hits { @@ -341,7 +322,7 @@ func (c *RepoCache) QueryBugs(q *query.Query) []entity.Id { case query.OrderByEdit: sorter = BugsByEditTime(filtered) default: - panic("missing sort type") + return nil, errors.New("missing sort type") } switch q.OrderDirection { @@ -350,7 +331,7 @@ func (c *RepoCache) QueryBugs(q *query.Query) []entity.Id { case query.OrderDescending: sorter = sort.Reverse(sorter) default: - panic("missing sort direction") + return nil, errors.New("missing sort direction") } sort.Sort(sorter) @@ -361,7 +342,7 @@ func (c *RepoCache) QueryBugs(q *query.Query) []entity.Id { result[i] = val.Id } - return result + return result, nil } // AllBugsIds return all known bug ids @@ -504,7 +485,12 @@ func (c *RepoCache) addBugToSearchIndex(snap *bug.Snapshot) error { searchableBug.Text = append(searchableBug.Text, snap.Title) - err := c.searchCache.Index(snap.Id().String(), searchableBug) + index, err := c.repo.GetBleveIndex("bug") + if err != nil { + return err + } + + err = index.Index(snap.Id().String(), searchableBug) if err != nil { return err } diff --git a/cache/repo_cache_test.go b/cache/repo_cache_test.go index 1c5c41d2..bd06e84d 100644 --- a/cache/repo_cache_test.go +++ b/cache/repo_cache_test.go @@ -73,7 +73,9 @@ func TestCache(t *testing.T) { // Querying q, err := query.Parse("status:open author:descartes sort:edit-asc") require.NoError(t, err) - require.Len(t, cache.QueryBugs(q), 2) + res, err := cache.QueryBugs(q) + require.NoError(t, err) + require.Len(t, res, 2) // Close require.NoError(t, cache.Close()) |