aboutsummaryrefslogtreecommitdiffstats
path: root/cache
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2022-11-19 11:33:12 +0100
committerMichael Muré <batolettre@gmail.com>2022-11-28 17:20:25 +0100
commit0ac39a7ab5db077fcf0df827e32bf6e625e980da (patch)
treee453d6fd244cb322bdc6305c0088aa3c0331b075 /cache
parentc6bb6b9c7ecddb679966b1561e2e909a9ee5e8cd (diff)
downloadgit-bug-0ac39a7ab5db077fcf0df827e32bf6e625e980da.tar.gz
WIP
Diffstat (limited to 'cache')
-rw-r--r--cache/bug_cache.go137
-rw-r--r--cache/bug_excerpt.go39
-rw-r--r--cache/bug_subcache.go296
-rw-r--r--cache/cached.go133
-rw-r--r--cache/identity_cache.go18
-rw-r--r--cache/identity_excerpt.go12
-rw-r--r--cache/lru_id_cache.go36
-rw-r--r--cache/multi_repo_cache.go2
-rw-r--r--cache/repo_cache.go29
-rw-r--r--cache/repo_cache_bug.go556
-rw-r--r--cache/repo_cache_common.go6
-rw-r--r--cache/repo_cache_identity.go222
-rw-r--r--cache/repo_cache_test.go3
-rw-r--r--cache/subcache.go362
14 files changed, 908 insertions, 943 deletions
diff --git a/cache/bug_cache.go b/cache/bug_cache.go
index 65e2068f..0fd52ff1 100644
--- a/cache/bug_cache.go
+++ b/cache/bug_cache.go
@@ -2,10 +2,10 @@ package cache
import (
"fmt"
- "sync"
"time"
"github.com/MichaelMure/git-bug/entities/bug"
+ "github.com/MichaelMure/git-bug/entities/identity"
"github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/entity/dag"
"github.com/MichaelMure/git-bug/repository"
@@ -19,63 +19,26 @@ var ErrNoMatchingOp = fmt.Errorf("no matching operation found")
// 2. Maintain an up-to-date Snapshot available.
// 3. Deal with concurrency.
type BugCache struct {
- repoCache *RepoCache
- mu sync.RWMutex
- bug *bug.WithSnapshot
+ CachedEntityBase[*bug.Snapshot, bug.Operation]
}
-func NewBugCache(repoCache *RepoCache, b *bug.Bug) *BugCache {
+func NewBugCache(subcache *RepoCacheBug, getUserIdentity func() (identity.Interface, error), b *bug.Bug) *BugCache {
return &BugCache{
- repoCache: repoCache,
- bug: &bug.WithSnapshot{Bug: b},
+ CachedEntityBase: CachedEntityBase[*bug.Snapshot, bug.Operation]{
+ entityUpdated: subcache.entityUpdated,
+ getUserIdentity: getUserIdentity,
+ repo: subcache.repo,
+ entity: &bug.WithSnapshot{Bug: b},
+ },
}
}
-func (c *BugCache) Snapshot() *bug.Snapshot {
- c.mu.RLock()
- defer c.mu.RUnlock()
- return c.bug.Compile()
-}
-
-func (c *BugCache) Id() entity.Id {
- return c.bug.Id()
-}
-
-func (c *BugCache) notifyUpdated() error {
- return c.repoCache.bugUpdated(c.bug.Id())
-}
-
-// ResolveOperationWithMetadata will find an operation that has the matching metadata
-func (c *BugCache) ResolveOperationWithMetadata(key string, value string) (entity.Id, error) {
- c.mu.RLock()
- defer c.mu.RUnlock()
- // preallocate but empty
- matching := make([]entity.Id, 0, 5)
-
- for _, op := range c.bug.Operations() {
- opValue, ok := op.GetMetadata(key)
- if ok && value == opValue {
- matching = append(matching, op.Id())
- }
- }
-
- if len(matching) == 0 {
- return "", ErrNoMatchingOp
- }
-
- if len(matching) > 1 {
- return "", bug.NewErrMultipleMatchOp(matching)
- }
-
- return matching[0], nil
-}
-
func (c *BugCache) AddComment(message string) (entity.CombinedId, *bug.AddCommentOperation, error) {
return c.AddCommentWithFiles(message, nil)
}
func (c *BugCache) AddCommentWithFiles(message string, files []repository.Hash) (entity.CombinedId, *bug.AddCommentOperation, error) {
- author, err := c.repoCache.GetUserIdentity()
+ author, err := c.getUserIdentity()
if err != nil {
return entity.UnsetCombinedId, nil, err
}
@@ -83,9 +46,9 @@ func (c *BugCache) AddCommentWithFiles(message string, files []repository.Hash)
return c.AddCommentRaw(author, time.Now().Unix(), message, files, nil)
}
-func (c *BugCache) AddCommentRaw(author *IdentityCache, unixTime int64, message string, files []repository.Hash, metadata map[string]string) (entity.CombinedId, *bug.AddCommentOperation, error) {
+func (c *BugCache) AddCommentRaw(author identity.Interface, unixTime int64, message string, files []repository.Hash, metadata map[string]string) (entity.CombinedId, *bug.AddCommentOperation, error) {
c.mu.Lock()
- commentId, op, err := bug.AddComment(c.bug, author, unixTime, message, files, metadata)
+ commentId, op, err := bug.AddComment(c.entity, author, unixTime, message, files, metadata)
c.mu.Unlock()
if err != nil {
return entity.UnsetCombinedId, nil, err
@@ -94,7 +57,7 @@ func (c *BugCache) AddCommentRaw(author *IdentityCache, unixTime int64, message
}
func (c *BugCache) ChangeLabels(added []string, removed []string) ([]bug.LabelChangeResult, *bug.LabelChangeOperation, error) {
- author, err := c.repoCache.GetUserIdentity()
+ author, err := c.getUserIdentity()
if err != nil {
return nil, nil, err
}
@@ -102,9 +65,9 @@ func (c *BugCache) ChangeLabels(added []string, removed []string) ([]bug.LabelCh
return c.ChangeLabelsRaw(author, time.Now().Unix(), added, removed, nil)
}
-func (c *BugCache) ChangeLabelsRaw(author *IdentityCache, unixTime int64, added []string, removed []string, metadata map[string]string) ([]bug.LabelChangeResult, *bug.LabelChangeOperation, error) {
+func (c *BugCache) ChangeLabelsRaw(author identity.Interface, unixTime int64, added []string, removed []string, metadata map[string]string) ([]bug.LabelChangeResult, *bug.LabelChangeOperation, error) {
c.mu.Lock()
- changes, op, err := bug.ChangeLabels(c.bug, author.Identity, unixTime, added, removed, metadata)
+ changes, op, err := bug.ChangeLabels(c.entity, author, unixTime, added, removed, metadata)
c.mu.Unlock()
if err != nil {
return changes, nil, err
@@ -113,7 +76,7 @@ func (c *BugCache) ChangeLabelsRaw(author *IdentityCache, unixTime int64, added
}
func (c *BugCache) ForceChangeLabels(added []string, removed []string) (*bug.LabelChangeOperation, error) {
- author, err := c.repoCache.GetUserIdentity()
+ author, err := c.getUserIdentity()
if err != nil {
return nil, err
}
@@ -121,9 +84,9 @@ func (c *BugCache) ForceChangeLabels(added []string, removed []string) (*bug.Lab
return c.ForceChangeLabelsRaw(author, time.Now().Unix(), added, removed, nil)
}
-func (c *BugCache) ForceChangeLabelsRaw(author *IdentityCache, unixTime int64, added []string, removed []string, metadata map[string]string) (*bug.LabelChangeOperation, error) {
+func (c *BugCache) ForceChangeLabelsRaw(author identity.Interface, unixTime int64, added []string, removed []string, metadata map[string]string) (*bug.LabelChangeOperation, error) {
c.mu.Lock()
- op, err := bug.ForceChangeLabels(c.bug, author.Identity, unixTime, added, removed, metadata)
+ op, err := bug.ForceChangeLabels(c.entity, author, unixTime, added, removed, metadata)
c.mu.Unlock()
if err != nil {
return nil, err
@@ -132,7 +95,7 @@ func (c *BugCache) ForceChangeLabelsRaw(author *IdentityCache, unixTime int64, a
}
func (c *BugCache) Open() (*bug.SetStatusOperation, error) {
- author, err := c.repoCache.GetUserIdentity()
+ author, err := c.getUserIdentity()
if err != nil {
return nil, err
}
@@ -140,9 +103,9 @@ func (c *BugCache) Open() (*bug.SetStatusOperation, error) {
return c.OpenRaw(author, time.Now().Unix(), nil)
}
-func (c *BugCache) OpenRaw(author *IdentityCache, unixTime int64, metadata map[string]string) (*bug.SetStatusOperation, error) {
+func (c *BugCache) OpenRaw(author identity.Interface, unixTime int64, metadata map[string]string) (*bug.SetStatusOperation, error) {
c.mu.Lock()
- op, err := bug.Open(c.bug, author.Identity, unixTime, metadata)
+ op, err := bug.Open(c.entity, author, unixTime, metadata)
c.mu.Unlock()
if err != nil {
return nil, err
@@ -151,7 +114,7 @@ func (c *BugCache) OpenRaw(author *IdentityCache, unixTime int64, metadata map[s
}
func (c *BugCache) Close() (*bug.SetStatusOperation, error) {
- author, err := c.repoCache.GetUserIdentity()
+ author, err := c.getUserIdentity()
if err != nil {
return nil, err
}
@@ -159,9 +122,9 @@ func (c *BugCache) Close() (*bug.SetStatusOperation, error) {
return c.CloseRaw(author, time.Now().Unix(), nil)
}
-func (c *BugCache) CloseRaw(author *IdentityCache, unixTime int64, metadata map[string]string) (*bug.SetStatusOperation, error) {
+func (c *BugCache) CloseRaw(author identity.Interface, unixTime int64, metadata map[string]string) (*bug.SetStatusOperation, error) {
c.mu.Lock()
- op, err := bug.Close(c.bug, author.Identity, unixTime, metadata)
+ op, err := bug.Close(c.entity, author, unixTime, metadata)
c.mu.Unlock()
if err != nil {
return nil, err
@@ -170,7 +133,7 @@ func (c *BugCache) CloseRaw(author *IdentityCache, unixTime int64, metadata map[
}
func (c *BugCache) SetTitle(title string) (*bug.SetTitleOperation, error) {
- author, err := c.repoCache.GetUserIdentity()
+ author, err := c.getUserIdentity()
if err != nil {
return nil, err
}
@@ -178,9 +141,9 @@ func (c *BugCache) SetTitle(title string) (*bug.SetTitleOperation, error) {
return c.SetTitleRaw(author, time.Now().Unix(), title, nil)
}
-func (c *BugCache) SetTitleRaw(author *IdentityCache, unixTime int64, title string, metadata map[string]string) (*bug.SetTitleOperation, error) {
+func (c *BugCache) SetTitleRaw(author identity.Interface, unixTime int64, title string, metadata map[string]string) (*bug.SetTitleOperation, error) {
c.mu.Lock()
- op, err := bug.SetTitle(c.bug, author.Identity, unixTime, title, metadata)
+ op, err := bug.SetTitle(c.entity, author, unixTime, title, metadata)
c.mu.Unlock()
if err != nil {
return nil, err
@@ -190,7 +153,7 @@ func (c *BugCache) SetTitleRaw(author *IdentityCache, unixTime int64, title stri
// EditCreateComment is a convenience function to edit the body of a bug (the first comment)
func (c *BugCache) EditCreateComment(body string) (entity.CombinedId, *bug.EditCommentOperation, error) {
- author, err := c.repoCache.GetUserIdentity()
+ author, err := c.getUserIdentity()
if err != nil {
return entity.UnsetCombinedId, nil, err
}
@@ -199,9 +162,9 @@ func (c *BugCache) EditCreateComment(body string) (entity.CombinedId, *bug.EditC
}
// EditCreateCommentRaw is a convenience function to edit the body of a bug (the first comment)
-func (c *BugCache) EditCreateCommentRaw(author *IdentityCache, unixTime int64, body string, metadata map[string]string) (entity.CombinedId, *bug.EditCommentOperation, error) {
+func (c *BugCache) EditCreateCommentRaw(author identity.Interface, unixTime int64, body string, metadata map[string]string) (entity.CombinedId, *bug.EditCommentOperation, error) {
c.mu.Lock()
- commentId, op, err := bug.EditCreateComment(c.bug, author.Identity, unixTime, body, nil, metadata)
+ commentId, op, err := bug.EditCreateComment(c.entity, author, unixTime, body, nil, metadata)
c.mu.Unlock()
if err != nil {
return entity.UnsetCombinedId, nil, err
@@ -210,7 +173,7 @@ func (c *BugCache) EditCreateCommentRaw(author *IdentityCache, unixTime int64, b
}
func (c *BugCache) EditComment(target entity.CombinedId, message string) (*bug.EditCommentOperation, error) {
- author, err := c.repoCache.GetUserIdentity()
+ author, err := c.getUserIdentity()
if err != nil {
return nil, err
}
@@ -218,14 +181,14 @@ func (c *BugCache) EditComment(target entity.CombinedId, message string) (*bug.E
return c.EditCommentRaw(author, time.Now().Unix(), target, message, nil)
}
-func (c *BugCache) EditCommentRaw(author *IdentityCache, unixTime int64, target entity.CombinedId, message string, metadata map[string]string) (*bug.EditCommentOperation, error) {
+func (c *BugCache) EditCommentRaw(author identity.Interface, unixTime int64, target entity.CombinedId, message string, metadata map[string]string) (*bug.EditCommentOperation, error) {
comment, err := c.Snapshot().SearchComment(target)
if err != nil {
return nil, err
}
c.mu.Lock()
- commentId, op, err := bug.EditComment(c.bug, author.Identity, unixTime, comment.TargetId(), message, nil, metadata)
+ commentId, op, err := bug.EditComment(c.entity, author, unixTime, comment.TargetId(), message, nil, metadata)
c.mu.Unlock()
if err != nil {
return nil, err
@@ -237,7 +200,7 @@ func (c *BugCache) EditCommentRaw(author *IdentityCache, unixTime int64, target
}
func (c *BugCache) SetMetadata(target entity.Id, newMetadata map[string]string) (*dag.SetMetadataOperation[*bug.Snapshot], error) {
- author, err := c.repoCache.GetUserIdentity()
+ author, err := c.getUserIdentity()
if err != nil {
return nil, err
}
@@ -245,40 +208,12 @@ func (c *BugCache) SetMetadata(target entity.Id, newMetadata map[string]string)
return c.SetMetadataRaw(author, time.Now().Unix(), target, newMetadata)
}
-func (c *BugCache) SetMetadataRaw(author *IdentityCache, unixTime int64, target entity.Id, newMetadata map[string]string) (*dag.SetMetadataOperation[*bug.Snapshot], error) {
+func (c *BugCache) SetMetadataRaw(author identity.Interface, unixTime int64, target entity.Id, newMetadata map[string]string) (*dag.SetMetadataOperation[*bug.Snapshot], error) {
c.mu.Lock()
- op, err := bug.SetMetadata(c.bug, author.Identity, unixTime, target, newMetadata)
+ op, err := bug.SetMetadata(c.entity, author, unixTime, target, newMetadata)
c.mu.Unlock()
if err != nil {
return nil, err
}
return op, c.notifyUpdated()
}
-
-func (c *BugCache) Commit() error {
- c.mu.Lock()
- err := c.bug.Commit(c.repoCache.repo)
- if err != nil {
- c.mu.Unlock()
- return err
- }
- c.mu.Unlock()
- return c.notifyUpdated()
-}
-
-func (c *BugCache) CommitAsNeeded() error {
- c.mu.Lock()
- err := c.bug.CommitAsNeeded(c.repoCache.repo)
- if err != nil {
- c.mu.Unlock()
- return err
- }
- c.mu.Unlock()
- return c.notifyUpdated()
-}
-
-func (c *BugCache) NeedCommit() bool {
- c.mu.RLock()
- defer c.mu.RUnlock()
- return c.bug.NeedCommit()
-}
diff --git a/cache/bug_excerpt.go b/cache/bug_excerpt.go
index 7e3bcad4..b4748fd2 100644
--- a/cache/bug_excerpt.go
+++ b/cache/bug_excerpt.go
@@ -2,12 +2,10 @@ package cache
import (
"encoding/gob"
- "fmt"
"time"
"github.com/MichaelMure/git-bug/entities/bug"
"github.com/MichaelMure/git-bug/entities/common"
- "github.com/MichaelMure/git-bug/entities/identity"
"github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/util/lamport"
)
@@ -20,7 +18,7 @@ func init() {
// BugExcerpt hold a subset of the bug values to be able to sort and filter bugs
// efficiently without having to read and compile each raw bugs.
type BugExcerpt struct {
- Id entity.Id
+ id entity.Id
CreateLamportTime lamport.Time
EditLamportTime lamport.Time
@@ -38,25 +36,6 @@ type BugExcerpt struct {
CreateMetadata map[string]string
}
-// identity.Bare data are directly embedded in the bug excerpt
-type LegacyAuthorExcerpt struct {
- Name string
- Login string
-}
-
-func (l LegacyAuthorExcerpt) DisplayName() string {
- switch {
- case l.Name == "" && l.Login != "":
- return l.Login
- case l.Name != "" && l.Login == "":
- return l.Name
- case l.Name != "" && l.Login != "":
- return fmt.Sprintf("%s (%s)", l.Name, l.Login)
- }
-
- panic("invalid person data")
-}
-
func NewBugExcerpt(b bug.Interface, snap *bug.Snapshot) *BugExcerpt {
participantsIds := make([]entity.Id, 0, len(snap.Participants))
for _, participant := range snap.Participants {
@@ -69,11 +48,12 @@ func NewBugExcerpt(b bug.Interface, snap *bug.Snapshot) *BugExcerpt {
}
e := &BugExcerpt{
- Id: b.Id(),
+ id: b.Id(),
CreateLamportTime: b.CreateLamportTime(),
EditLamportTime: b.EditLamportTime(),
CreateUnixTime: b.FirstOp().Time().Unix(),
EditUnixTime: snap.EditTime().Unix(),
+ AuthorId: snap.Author.Id(),
Status: snap.Status,
Labels: snap.Labels,
Actors: actorsIds,
@@ -83,16 +63,13 @@ func NewBugExcerpt(b bug.Interface, snap *bug.Snapshot) *BugExcerpt {
CreateMetadata: b.FirstOp().AllMetadata(),
}
- switch snap.Author.(type) {
- case *identity.Identity, *identity.IdentityStub, *IdentityCache:
- e.AuthorId = snap.Author.Id()
- default:
- panic("unhandled identity type")
- }
-
return e
}
+func (b *BugExcerpt) Id() entity.Id {
+ return b.id
+}
+
func (b *BugExcerpt) CreateTime() time.Time {
return time.Unix(b.CreateUnixTime, 0)
}
@@ -112,7 +89,7 @@ func (b BugsById) Len() int {
}
func (b BugsById) Less(i, j int) bool {
- return b[i].Id < b[j].Id
+ return b[i].id < b[j].id
}
func (b BugsById) Swap(i, j int) {
diff --git a/cache/bug_subcache.go b/cache/bug_subcache.go
new file mode 100644
index 00000000..a0c8d84c
--- /dev/null
+++ b/cache/bug_subcache.go
@@ -0,0 +1,296 @@
+package cache
+
+import (
+ "errors"
+ "fmt"
+ "sort"
+ "strings"
+ "time"
+ "unicode/utf8"
+
+ "github.com/blevesearch/bleve"
+
+ "github.com/MichaelMure/git-bug/entities/bug"
+ "github.com/MichaelMure/git-bug/entities/identity"
+ "github.com/MichaelMure/git-bug/entity"
+ "github.com/MichaelMure/git-bug/query"
+ "github.com/MichaelMure/git-bug/repository"
+)
+
+type RepoCacheBug struct {
+ SubCache[*BugExcerpt, *BugCache, bug.Interface]
+}
+
+// ResolveBugCreateMetadata retrieve a bug that has the exact given metadata on
+// its Create operation, that is, the first operation. It fails if multiple bugs
+// match.
+func (c *RepoCacheBug) ResolveBugCreateMetadata(key string, value string) (*BugCache, error) {
+ return c.ResolveMatcher(func(excerpt *BugExcerpt) bool {
+ return excerpt.CreateMetadata[key] == value
+ })
+}
+
+// ResolveComment search for a Bug/Comment combination matching the merged
+// bug/comment Id prefix. Returns the Bug containing the Comment and the Comment's
+// Id.
+func (c *RepoCacheBug) ResolveComment(prefix string) (*BugCache, entity.CombinedId, error) {
+ bugPrefix, _ := entity.SeparateIds(prefix)
+ bugCandidate := make([]entity.Id, 0, 5)
+
+ // build a list of possible matching bugs
+ c.mu.RLock()
+ for _, excerpt := range c.excerpts {
+ if excerpt.Id().HasPrefix(bugPrefix) {
+ bugCandidate = append(bugCandidate, excerpt.Id())
+ }
+ }
+ c.mu.RUnlock()
+
+ matchingBugIds := make([]entity.Id, 0, 5)
+ matchingCommentId := entity.UnsetCombinedId
+ var matchingBug *BugCache
+
+ // search for matching comments
+ // searching every bug candidate allow for some collision with the bug prefix only,
+ // before being refined with the full comment prefix
+ for _, bugId := range bugCandidate {
+ b, err := c.Resolve(bugId)
+ if err != nil {
+ return nil, entity.UnsetCombinedId, err
+ }
+
+ for _, comment := range b.Snapshot().Comments {
+ if comment.CombinedId().HasPrefix(prefix) {
+ matchingBugIds = append(matchingBugIds, bugId)
+ matchingBug = b
+ matchingCommentId = comment.CombinedId()
+ }
+ }
+ }
+
+ if len(matchingBugIds) > 1 {
+ return nil, entity.UnsetCombinedId, entity.NewErrMultipleMatch("bug/comment", matchingBugIds)
+ } else if len(matchingBugIds) == 0 {
+ return nil, entity.UnsetCombinedId, errors.New("comment doesn't exist")
+ }
+
+ return matchingBug, matchingCommentId, nil
+}
+
+// QueryBugs return the id of all Bug matching the given Query
+func (c *RepoCacheBug) QueryBugs(q *query.Query) ([]entity.Id, error) {
+ c.mu.RLock()
+ defer c.mu.RUnlock()
+
+ if q == nil {
+ return c.AllIds(), nil
+ }
+
+ matcher := compileMatcher(q.Filters)
+
+ var filtered []*BugExcerpt
+ var foundBySearch map[entity.Id]*BugExcerpt
+
+ if q.Search != nil {
+ foundBySearch = map[entity.Id]*BugExcerpt{}
+
+ terms := make([]string, len(q.Search))
+ copy(terms, q.Search)
+ for i, search := range q.Search {
+ if strings.Contains(search, " ") {
+ terms[i] = fmt.Sprintf("\"%s\"", search)
+ }
+ }
+
+ bleveQuery := bleve.NewQueryStringQuery(strings.Join(terms, " "))
+ bleveSearch := bleve.NewSearchRequest(bleveQuery)
+
+ index, err := c.repo.GetBleveIndex("bug")
+ if err != nil {
+ return nil, err
+ }
+
+ searchResults, err := index.Search(bleveSearch)
+ if err != nil {
+ return nil, err
+ }
+
+ for _, hit := range searchResults.Hits {
+ foundBySearch[entity.Id(hit.ID)] = c.excerpts[entity.Id(hit.ID)]
+ }
+ } else {
+ foundBySearch = c.excerpts
+ }
+
+ for _, excerpt := range foundBySearch {
+ if matcher.Match(excerpt, c) {
+ filtered = append(filtered, excerpt)
+ }
+ }
+
+ var sorter sort.Interface
+
+ switch q.OrderBy {
+ case query.OrderById:
+ sorter = BugsById(filtered)
+ case query.OrderByCreation:
+ sorter = BugsByCreationTime(filtered)
+ case query.OrderByEdit:
+ sorter = BugsByEditTime(filtered)
+ default:
+ return nil, errors.New("missing sort type")
+ }
+
+ switch q.OrderDirection {
+ case query.OrderAscending:
+ // Nothing to do
+ case query.OrderDescending:
+ sorter = sort.Reverse(sorter)
+ default:
+ return nil, errors.New("missing sort direction")
+ }
+
+ sort.Sort(sorter)
+
+ result := make([]entity.Id, len(filtered))
+
+ for i, val := range filtered {
+ result[i] = val.Id()
+ }
+
+ return result, nil
+}
+
+// ValidLabels list valid labels
+//
+// Note: in the future, a proper label policy could be implemented where valid
+// labels are defined in a configuration file. Until that, the default behavior
+// is to return the list of labels already used.
+func (c *RepoCacheBug) ValidLabels() []bug.Label {
+ c.mu.RLock()
+ defer c.mu.RUnlock()
+
+ set := map[bug.Label]interface{}{}
+
+ for _, excerpt := range c.excerpts {
+ for _, l := range excerpt.Labels {
+ set[l] = nil
+ }
+ }
+
+ result := make([]bug.Label, len(set))
+
+ i := 0
+ for l := range set {
+ result[i] = l
+ i++
+ }
+
+ // Sort
+ sort.Slice(result, func(i, j int) bool {
+ return string(result[i]) < string(result[j])
+ })
+
+ return result
+}
+
+// New create a new bug
+// The new bug is written in the repository (commit)
+func (c *RepoCacheBug) New(title string, message string) (*BugCache, *bug.CreateOperation, error) {
+ return c.NewWithFiles(title, message, nil)
+}
+
+// NewWithFiles create a new bug with attached files for the message
+// The new bug is written in the repository (commit)
+func (c *RepoCacheBug) NewWithFiles(title string, message string, files []repository.Hash) (*BugCache, *bug.CreateOperation, error) {
+ author, err := c.getUserIdentity()
+ if err != nil {
+ return nil, nil, err
+ }
+
+ return c.NewRaw(author, time.Now().Unix(), title, message, files, nil)
+}
+
+// NewRaw create a new bug with attached files for the message, as
+// well as metadata for the Create operation.
+// The new bug is written in the repository (commit)
+func (c *RepoCacheBug) NewRaw(author identity.Interface, unixTime int64, title string, message string, files []repository.Hash, metadata map[string]string) (*BugCache, *bug.CreateOperation, error) {
+ b, op, err := bug.Create(author, unixTime, title, message, files, metadata)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ err = b.Commit(c.repo)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ cached, err := c.add(b)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ return cached, op, nil
+}
+
+// Remove removes a bug from the cache and repo given a bug id prefix
+func (c *RepoCacheBug) Remove(prefix string) error {
+ b, err := c.ResolveBugPrefix(prefix)
+ if err != nil {
+ return err
+ }
+
+ c.muBug.Lock()
+
+ err = bug.Remove(c.repo, b.Id())
+ if err != nil {
+ c.muBug.Unlock()
+
+ return err
+ }
+
+ delete(c.bugs, b.Id())
+ delete(c.bugExcerpts, b.Id())
+ c.loadedBugs.Remove(b.Id())
+
+ c.muBug.Unlock()
+
+ return c.writeBugCache()
+}
+
+func (c *RepoCacheBug) addBugToSearchIndex(snap *bug.Snapshot) error {
+ searchableBug := struct {
+ Text []string
+ }{}
+
+ // See https://github.com/blevesearch/bleve/issues/1576
+ var sb strings.Builder
+ normalize := func(text string) string {
+ sb.Reset()
+ for _, field := range strings.Fields(text) {
+ if utf8.RuneCountInString(field) < 100 {
+ sb.WriteString(field)
+ sb.WriteRune(' ')
+ }
+ }
+ return sb.String()
+ }
+
+ for _, comment := range snap.Comments {
+ searchableBug.Text = append(searchableBug.Text, normalize(comment.Message))
+ }
+
+ searchableBug.Text = append(searchableBug.Text, normalize(snap.Title))
+
+ index, err := c.repo.GetBleveIndex("bug")
+ if err != nil {
+ return err
+ }
+
+ err = index.Index(snap.Id().String(), searchableBug)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
diff --git a/cache/cached.go b/cache/cached.go
new file mode 100644
index 00000000..5e24e732
--- /dev/null
+++ b/cache/cached.go
@@ -0,0 +1,133 @@
+package cache
+
+import (
+ "sync"
+
+ "github.com/MichaelMure/git-bug/entities/bug"
+ "github.com/MichaelMure/git-bug/entities/identity"
+ "github.com/MichaelMure/git-bug/entity"
+ "github.com/MichaelMure/git-bug/entity/dag"
+ "github.com/MichaelMure/git-bug/repository"
+)
+
+// type withSnapshot[SnapT dag.Snapshot, OpT dag.OperationWithApply[SnapT]] struct {
+// dag.Interface[SnapT, OpT]
+// snap dag.Snapshot
+// }
+//
+//
+// func (ws *withSnapshot[SnapT, OpT]) Compile() dag.Snapshot {
+// if ws.snap == nil {
+// snap := ws.Interface.Compile()
+// ws.snap = snap
+// }
+// return ws.snap
+// }
+//
+// // Append intercept Bug.Append() to update the snapshot efficiently
+// func (ws *withSnapshot[SnapT, OpT]) Append(op OpT) {
+// ws.Interface.Append(op)
+//
+// if ws.snap == nil {
+// return
+// }
+//
+// op.Apply(ws.snap)
+// ws.snap. = append(ws.snap.Operations, op)
+// }
+//
+// // Commit intercept Bug.Commit() to update the snapshot efficiently
+// func (ws *withSnapshot[SnapT, OpT]) Commit(repo repository.ClockedRepo) error {
+// err := ws.Interface.Commit(repo)
+//
+// if err != nil {
+// ws.snap = nil
+// return err
+// }
+//
+// // Commit() shouldn't change anything of the bug state apart from the
+// // initial ID set
+//
+// if ws.snap == nil {
+// return nil
+// }
+//
+// ws.snap.id = ws.Interface.Id()
+// return nil
+// }
+
+type CachedEntityBase[SnapT dag.Snapshot, OpT dag.Operation] struct {
+ entityUpdated func(id entity.Id) error
+ getUserIdentity func() (identity.Interface, error)
+ repo repository.ClockedRepo
+
+ mu sync.RWMutex
+ entity dag.Interface[SnapT, OpT]
+}
+
+func (e *CachedEntityBase[SnapT, OpT]) Id() entity.Id {
+ return e.entity.Id()
+}
+
+func (e *CachedEntityBase[SnapT, OpT]) Snapshot() SnapT {
+ e.mu.RLock()
+ defer e.mu.RUnlock()
+ return e.entity.Compile()
+}
+
+func (e *CachedEntityBase[SnapT, OpT]) notifyUpdated() error {
+ return e.entityUpdated(e.entity.Id())
+}
+
+// ResolveOperationWithMetadata will find an operation that has the matching metadata
+func (e *CachedEntityBase[SnapT, OpT]) ResolveOperationWithMetadata(key string, value string) (entity.Id, error) {
+ e.mu.RLock()
+ defer e.mu.RUnlock()
+ // preallocate but empty
+ matching := make([]entity.Id, 0, 5)
+
+ for _, op := range e.entity.Operations() {
+ opValue, ok := op.GetMetadata(key)
+ if ok && value == opValue {
+ matching = append(matching, op.Id())
+ }
+ }
+
+ if len(matching) == 0 {
+ return "", ErrNoMatchingOp
+ }
+
+ if len(matching) > 1 {
+ return "", bug.NewErrMultipleMatchOp(matching)
+ }
+
+ return matching[0], nil
+}
+
+func (e *CachedEntityBase[SnapT, OpT]) Commit() error {
+ e.mu.Lock()
+ err := e.entity.Commit(e.repo)
+ if err != nil {
+ e.mu.Unlock()
+ return err
+ }
+ e.mu.Unlock()
+ return e.notifyUpdated()
+}
+
+func (e *CachedEntityBase[SnapT, OpT]) CommitAsNeeded() error {
+ e.mu.Lock()
+ err := e.entity.CommitAsNeeded(e.repo)
+ if err != nil {
+ e.mu.Unlock()
+ return err
+ }
+ e.mu.Unlock()
+ return e.notifyUpdated()
+}
+
+func (e *CachedEntityBase[SnapT, OpT]) NeedCommit() bool {
+ e.mu.RLock()
+ defer e.mu.RUnlock()
+ return e.entity.NeedCommit()
+}
diff --git a/cache/identity_cache.go b/cache/identity_cache.go
index 3b7bb818..a6c929ad 100644
--- a/cache/identity_cache.go
+++ b/cache/identity_cache.go
@@ -2,6 +2,7 @@ package cache
import (
"github.com/MichaelMure/git-bug/entities/identity"
+ "github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/repository"
)
@@ -9,19 +10,22 @@ var _ identity.Interface = &IdentityCache{}
// IdentityCache is a wrapper around an Identity for caching.
type IdentityCache struct {
+ entityUpdated func(id entity.Id) error
+ repo repository.ClockedRepo
+
*identity.Identity
- repoCache *RepoCache
}
-func NewIdentityCache(repoCache *RepoCache, id *identity.Identity) *IdentityCache {
+func NewIdentityCache(subcache *RepoCacheIdentity, id *identity.Identity) *IdentityCache {
return &IdentityCache{
- Identity: id,
- repoCache: repoCache,
+ entityUpdated: subcache.entityUpdated,
+ repo: subcache.repo,
+ Identity: id,
}
}
func (i *IdentityCache) notifyUpdated() error {
- return i.repoCache.identityUpdated(i.Identity.Id())
+ return i.entityUpdated(i.Identity.Id())
}
func (i *IdentityCache) Mutate(repo repository.RepoClock, f func(*identity.Mutator)) error {
@@ -33,7 +37,7 @@ func (i *IdentityCache) Mutate(repo repository.RepoClock, f func(*identity.Mutat
}
func (i *IdentityCache) Commit() error {
- err := i.Identity.Commit(i.repoCache.repo)
+ err := i.Identity.Commit(i.repo)
if err != nil {
return err
}
@@ -41,7 +45,7 @@ func (i *IdentityCache) Commit() error {
}
func (i *IdentityCache) CommitAsNeeded() error {
- err := i.Identity.CommitAsNeeded(i.repoCache.repo)
+ err := i.Identity.CommitAsNeeded(i.repo)
if err != nil {
return err
}
diff --git a/cache/identity_excerpt.go b/cache/identity_excerpt.go
index 0166f493..126c712b 100644
--- a/cache/identity_excerpt.go
+++ b/cache/identity_excerpt.go
@@ -18,7 +18,7 @@ func init() {
// filter identities efficiently without having to read and compile each raw
// identity.
type IdentityExcerpt struct {
- Id entity.Id
+ id entity.Id
Name string
Login string
@@ -27,13 +27,17 @@ type IdentityExcerpt struct {
func NewIdentityExcerpt(i *identity.Identity) *IdentityExcerpt {
return &IdentityExcerpt{
- Id: i.Id(),
+ id: i.Id(),
Name: i.Name(),
Login: i.Login(),
ImmutableMetadata: i.ImmutableMetadata(),
}
}
+func (i *IdentityExcerpt) Id() entity.Id {
+ return i.id
+}
+
// DisplayName return a non-empty string to display, representing the
// identity, based on the non-empty values.
func (i *IdentityExcerpt) DisplayName() string {
@@ -51,7 +55,7 @@ func (i *IdentityExcerpt) DisplayName() string {
// Match matches a query with the identity name, login and ID prefixes
func (i *IdentityExcerpt) Match(query string) bool {
- return i.Id.HasPrefix(query) ||
+ return i.id.HasPrefix(query) ||
strings.Contains(strings.ToLower(i.Name), query) ||
strings.Contains(strings.ToLower(i.Login), query)
}
@@ -67,7 +71,7 @@ func (b IdentityById) Len() int {
}
func (b IdentityById) Less(i, j int) bool {
- return b[i].Id < b[j].Id
+ return b[i].id < b[j].id
}
func (b IdentityById) Swap(i, j int) {
diff --git a/cache/lru_id_cache.go b/cache/lru_id_cache.go
index fda12ca6..0e5e31a7 100644
--- a/cache/lru_id_cache.go
+++ b/cache/lru_id_cache.go
@@ -8,49 +8,49 @@ import (
"github.com/MichaelMure/git-bug/entity"
)
-type LRUIdCache struct {
- parentCache *lru.Cache
+type lruIdCache struct {
+ lru *lru.Cache
}
-func NewLRUIdCache() *LRUIdCache {
+func newLRUIdCache() *lruIdCache {
// we can ignore the error here as it would only fail if the size is negative.
cache, _ := lru.New(math.MaxInt32)
- return &LRUIdCache{
+ return &lruIdCache{
cache,
}
}
-func (c *LRUIdCache) Add(id entity.Id) bool {
- return c.parentCache.Add(id, nil)
+func (c *lruIdCache) Add(id entity.Id) bool {
+ return c.lru.Add(id, nil)
}
-func (c *LRUIdCache) Contains(id entity.Id) bool {
- return c.parentCache.Contains(id)
+func (c *lruIdCache) Contains(id entity.Id) bool {
+ return c.lru.Contains(id)
}
-func (c *LRUIdCache) Get(id entity.Id) bool {
- _, present := c.parentCache.Get(id)
+func (c *lruIdCache) Get(id entity.Id) bool {
+ _, present := c.lru.Get(id)
return present
}
-func (c *LRUIdCache) GetOldest() (entity.Id, bool) {
- id, _, present := c.parentCache.GetOldest()
+func (c *lruIdCache) GetOldest() (entity.Id, bool) {
+ id, _, present := c.lru.GetOldest()
return id.(entity.Id), present
}
-func (c *LRUIdCache) GetOldestToNewest() (ids []entity.Id) {
- interfaceKeys := c.parentCache.Keys()
+func (c *lruIdCache) GetOldestToNewest() (ids []entity.Id) {
+ interfaceKeys := c.lru.Keys()
for _, id := range interfaceKeys {
ids = append(ids, id.(entity.Id))
}
return
}
-func (c *LRUIdCache) Len() int {
- return c.parentCache.Len()
+func (c *lruIdCache) Len() int {
+ return c.lru.Len()
}
-func (c *LRUIdCache) Remove(id entity.Id) bool {
- return c.parentCache.Remove(id)
+func (c *lruIdCache) Remove(id entity.Id) bool {
+ return c.lru.Remove(id)
}
diff --git a/cache/multi_repo_cache.go b/cache/multi_repo_cache.go
index 659cd5e6..98f868e9 100644
--- a/cache/multi_repo_cache.go
+++ b/cache/multi_repo_cache.go
@@ -31,7 +31,7 @@ func (c *MultiRepoCache) RegisterRepository(ref string, repo repository.ClockedR
return r, nil
}
-// RegisterDefaultRepository register a unnamed repository. Use this for mono-repo setup
+// RegisterDefaultRepository register an unnamed repository. Use this for mono-repo setup
func (c *MultiRepoCache) RegisterDefaultRepository(repo repository.ClockedRepo) (*RepoCache, error) {
r, err := NewRepoCache(repo)
if err != nil {
diff --git a/cache/repo_cache.go b/cache/repo_cache.go
index 71abf968..c1646d3b 100644
--- a/cache/repo_cache.go
+++ b/cache/repo_cache.go
@@ -52,16 +52,7 @@ type RepoCache struct {
// resolvers for all known entities
resolvers entity.Resolvers
- // maximum number of loaded bugs
- maxLoadedBugs int
-
- muBug sync.RWMutex
- // excerpt of bugs data for all bugs
- bugExcerpts map[entity.Id]*BugExcerpt
- // bug loaded in memory
- bugs map[entity.Id]*BugCache
- // loadedBugs is an LRU cache that records which bugs the cache has loaded in
- loadedBugs *LRUIdCache
+ bugs *RepoCacheBug
muIdentity sync.RWMutex
// excerpt of identities data for all identities
@@ -79,12 +70,13 @@ func NewRepoCache(r repository.ClockedRepo) (*RepoCache, error) {
func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, error) {
c := &RepoCache{
- repo: r,
- name: name,
- maxLoadedBugs: defaultMaxLoadedBugs,
- bugs: make(map[entity.Id]*BugCache),
- loadedBugs: NewLRUIdCache(),
- identities: make(map[entity.Id]*IdentityCache),
+ repo: r,
+ name: name,
+ bugs: NewCache(r),
+ // maxLoadedBugs: defaultMaxLoadedBugs,
+ // bugs: make(map[entity.Id]*BugCache),
+ // loadedBugs: newLRUIdCache(),
+ // identities: make(map[entity.Id]*IdentityCache),
}
c.resolvers = makeResolvers(c)
@@ -108,6 +100,11 @@ func NewNamedRepoCache(r repository.ClockedRepo, name string) (*RepoCache, error
return c, c.write()
}
+// Bugs gives access to the Bug entities
+func (c *RepoCache) Bugs() *RepoCacheBug {
+ return c.bugs
+}
+
// setCacheSize change the maximum number of loaded bugs
func (c *RepoCache) setCacheSize(size int) {
c.maxLoadedBugs = size
diff --git a/cache/repo_cache_bug.go b/cache/repo_cache_bug.go
deleted file mode 100644
index 2992421c..00000000
--- a/cache/repo_cache_bug.go
+++ /dev/null
@@ -1,556 +0,0 @@
-package cache
-
-import (
- "bytes"
- "encoding/gob"
- "errors"
- "fmt"
- "sort"
- "strings"
- "time"
- "unicode/utf8"
-
- "github.com/blevesearch/bleve"
-
- "github.com/MichaelMure/git-bug/entities/bug"
- "github.com/MichaelMure/git-bug/entity"
- "github.com/MichaelMure/git-bug/query"
- "github.com/MichaelMure/git-bug/repository"
-)
-
-const bugCacheFile = "bug-cache"
-
-var errBugNotInCache = errors.New("bug missing from cache")
-
-// 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 {
- c.muBug.Lock()
- b, ok := c.bugs[id]
- if !ok {
- c.muBug.Unlock()
-
- // if the bug is not loaded at this point, it means it was loaded before
- // but got evicted. Which means we potentially have multiple copies in
- // memory and thus concurrent write.
- // Failing immediately here is the simple and safe solution to avoid
- // complicated data loss.
- return errBugNotInCache
- }
- c.loadedBugs.Get(id)
- c.bugExcerpts[id] = NewBugExcerpt(b.bug, b.Snapshot())
- c.muBug.Unlock()
-
- if err := c.addBugToSearchIndex(b.Snapshot()); err != nil {
- return err
- }
-
- // we only need to write the bug cache
- return c.writeBugCache()
-}
-
-// load will try to read from the disk the bug cache file
-func (c *RepoCache) loadBugCache() error {
- c.muBug.Lock()
- defer c.muBug.Unlock()
-
- f, err := c.repo.LocalStorage().Open(bugCacheFile)
- if err != nil {
- return err
- }
-
- decoder := gob.NewDecoder(f)
-
- aux := struct {
- Version uint
- Excerpts map[entity.Id]*BugExcerpt
- }{}
-
- err = decoder.Decode(&aux)
- if err != nil {
- return err
- }
-
- if aux.Version != formatVersion {
- return fmt.Errorf("unknown cache format version %v", aux.Version)
- }
-
- c.bugExcerpts = aux.Excerpts
-
- index, err := c.repo.GetBleveIndex("bug")
- if err != nil {
- return err
- }
-
- // simple heuristic to detect a mismatch between the index and the bugs
- count, err := index.DocCount()
- if err != nil {
- return err
- }
- if count != uint64(len(c.bugExcerpts)) {
- return fmt.Errorf("count mismatch between bleve and bug excerpts")
- }
-
- return nil
-}
-
-// write will serialize on disk the bug cache file
-func (c *RepoCache) writeBugCache() error {
- c.muBug.RLock()
- defer c.muBug.RUnlock()
-
- var data bytes.Buffer
-
- aux := struct {
- Version uint
- Excerpts map[entity.Id]*BugExcerpt
- }{
- Version: formatVersion,
- Excerpts: c.bugExcerpts,
- }
-
- encoder := gob.NewEncoder(&data)
-
- err := encoder.Encode(aux)
- if err != nil {
- return err
- }
-
- f, err := c.repo.LocalStorage().Create(bugCacheFile)
- if err != nil {
- return err
- }
-
- _, err = f.Write(data.Bytes())
- if err != nil {
- return err
- }
-
- return f.Close()
-}
-
-// ResolveBugExcerpt retrieve a BugExcerpt matching the exact given id
-func (c *RepoCache) ResolveBugExcerpt(id entity.Id) (*BugExcerpt, error) {
- c.muBug.RLock()
- defer c.muBug.RUnlock()
-
- excerpt, ok := c.bugExcerpts[id]
- if !ok {
- return nil, bug.ErrBugNotExist
- }
-
- return excerpt, nil
-}
-
-// ResolveBug retrieve a bug matching the exact given id
-func (c *RepoCache) ResolveBug(id entity.Id) (*BugCache, error) {
- c.muBug.RLock()
- cached, ok := c.bugs[id]
- if ok {
- c.loadedBugs.Get(id)
- c.muBug.RUnlock()
- return cached, nil
- }
- c.muBug.RUnlock()
-
- b, err := bug.ReadWithResolver(c.repo, c.resolvers, id)
- if err != nil {
- return nil, err
- }
-
- cached = NewBugCache(c, b)
-
- c.muBug.Lock()
- c.bugs[id] = cached
- c.loadedBugs.Add(id)
- c.muBug.Unlock()
-
- c.evictIfNeeded()
-
- return cached, nil
-}
-
-// evictIfNeeded will evict a bug from the cache if needed
-// it also removes references of the bug from the bugs
-func (c *RepoCache) evictIfNeeded() {
- c.muBug.Lock()
- defer c.muBug.Unlock()
- if c.loadedBugs.Len() <= c.maxLoadedBugs {
- return
- }
-
- for _, id := range c.loadedBugs.GetOldestToNewest() {
- b := c.bugs[id]
- if b.NeedCommit() {
- continue
- }
-
- b.mu.Lock()
- c.loadedBugs.Remove(id)
- delete(c.bugs, id)
-
- if c.loadedBugs.Len() <= c.maxLoadedBugs {
- return
- }
- }
-}
-
-// ResolveBugExcerptPrefix retrieve a BugExcerpt matching an id prefix. It fails if multiple
-// bugs match.
-func (c *RepoCache) ResolveBugExcerptPrefix(prefix string) (*BugExcerpt, error) {
- return c.ResolveBugExcerptMatcher(func(excerpt *BugExcerpt) bool {
- return excerpt.Id.HasPrefix(prefix)
- })
-}
-
-// ResolveBugPrefix retrieve a bug matching an id prefix. It fails if multiple
-// bugs match.
-func (c *RepoCache) ResolveBugPrefix(prefix string) (*BugCache, error) {
- return c.ResolveBugMatcher(func(excerpt *BugExcerpt) bool {
- return excerpt.Id.HasPrefix(prefix)
- })
-}
-
-// ResolveBugCreateMetadata retrieve a bug that has the exact given metadata on
-// its Create operation, that is, the first operation. It fails if multiple bugs
-// match.
-func (c *RepoCache) ResolveBugCreateMetadata(key string, value string) (*BugCache, error) {
- return c.ResolveBugMatcher(func(excerpt *BugExcerpt) bool {
- return excerpt.CreateMetadata[key] == value
- })
-}
-
-func (c *RepoCache) ResolveBugExcerptMatcher(f func(*BugExcerpt) bool) (*BugExcerpt, error) {
- id, err := c.resolveBugMatcher(f)
- if err != nil {
- return nil, err
- }
- return c.ResolveBugExcerpt(id)
-}
-
-func (c *RepoCache) ResolveBugMatcher(f func(*BugExcerpt) bool) (*BugCache, error) {
- id, err := c.resolveBugMatcher(f)
- if err != nil {
- return nil, err
- }
- return c.ResolveBug(id)
-}
-
-func (c *RepoCache) resolveBugMatcher(f func(*BugExcerpt) bool) (entity.Id, error) {
- c.muBug.RLock()
- defer c.muBug.RUnlock()
-
- // preallocate but empty
- matching := make([]entity.Id, 0, 5)
-
- for _, excerpt := range c.bugExcerpts {
- if f(excerpt) {
- matching = append(matching, excerpt.Id)
- }
- }
-
- if len(matching) > 1 {
- return entity.UnsetId, bug.NewErrMultipleMatchBug(matching)
- }
-
- if len(matching) == 0 {
- return entity.UnsetId, bug.ErrBugNotExist
- }
-
- return matching[0], nil
-}
-
-// ResolveComment search for a Bug/Comment combination matching the merged
-// bug/comment Id prefix. Returns the Bug containing the Comment and the Comment's
-// Id.
-func (c *RepoCache) ResolveComment(prefix string) (*BugCache, entity.CombinedId, error) {
- bugPrefix, _ := entity.SeparateIds(prefix)
- bugCandidate := make([]entity.Id, 0, 5)
-
- // build a list of possible matching bugs
- c.muBug.RLock()
- for _, excerpt := range c.bugExcerpts {
- if excerpt.Id.HasPrefix(bugPrefix) {
- bugCandidate = append(bugCandidate, excerpt.Id)
- }
- }
- c.muBug.RUnlock()
-
- matchingBugIds := make([]entity.Id, 0, 5)
- matchingCommentId := entity.UnsetCombinedId
- var matchingBug *BugCache
-
- // search for matching comments
- // searching every bug candidate allow for some collision with the bug prefix only,
- // before being refined with the full comment prefix
- for _, bugId := range bugCandidate {
- b, err := c.ResolveBug(bugId)
- if err != nil {
- return nil, entity.UnsetCombinedId, err
- }
-
- for _, comment := range b.Snapshot().Comments {
- if comment.CombinedId().HasPrefix(prefix) {
- matchingBugIds = append(matchingBugIds, bugId)
- matchingBug = b
- matchingCommentId = comment.CombinedId()
- }
- }
- }
-
- if len(matchingBugIds) > 1 {
- return nil, entity.UnsetCombinedId, entity.NewErrMultipleMatch("bug/comment", matchingBugIds)
- } else if len(matchingBugIds) == 0 {
- return nil, entity.UnsetCombinedId, errors.New("comment doesn't exist")
- }
-
- return matchingBug, matchingCommentId, nil
-}
-
-// QueryBugs return the id of all Bug matching the given Query
-func (c *RepoCache) QueryBugs(q *query.Query) ([]entity.Id, error) {
- c.muBug.RLock()
- defer c.muBug.RUnlock()
-
- if q == nil {
- return c.AllBugsIds(), nil
- }
-
- matcher := compileMatcher(q.Filters)
-
- var filtered []*BugExcerpt
- var foundBySearch map[entity.Id]*BugExcerpt
-
- if q.Search != nil {
- foundBySearch = map[entity.Id]*BugExcerpt{}
-
- terms := make([]string, len(q.Search))
- copy(terms, q.Search)
- for i, search := range q.Search {
- if strings.Contains(search, " ") {
- terms[i] = fmt.Sprintf("\"%s\"", search)
- }
- }
-
- bleveQuery := bleve.NewQueryStringQuery(strings.Join(terms, " "))
- bleveSearch := bleve.NewSearchRequest(bleveQuery)
-
- index, err := c.repo.GetBleveIndex("bug")
- if err != nil {
- return nil, err
- }
-
- searchResults, err := index.Search(bleveSearch)
- if err != nil {
- return nil, err
- }
-
- for _, hit := range searchResults.Hits {
- foundBySearch[entity.Id(hit.ID)] = c.bugExcerpts[entity.Id(hit.ID)]
- }
- } else {
- foundBySearch = c.bugExcerpts
- }
-
- for _, excerpt := range foundBySearch {
- if matcher.Match(excerpt, c) {
- filtered = append(filtered, excerpt)
- }
- }
-
- var sorter sort.Interface
-
- switch q.OrderBy {
- case query.OrderById:
- sorter = BugsById(filtered)
- case query.OrderByCreation:
- sorter = BugsByCreationTime(filtered)
- case query.OrderByEdit:
- sorter = BugsByEditTime(filtered)
- default:
- return nil, errors.New("missing sort type")
- }
-
- switch q.OrderDirection {
- case query.OrderAscending:
- // Nothing to do
- case query.OrderDescending:
- sorter = sort.Reverse(sorter)
- default:
- return nil, errors.New("missing sort direction")
- }
-
- sort.Sort(sorter)
-
- result := make([]entity.Id, len(filtered))
-
- for i, val := range filtered {
- result[i] = val.Id
- }
-
- return result, nil
-}
-
-// AllBugsIds return all known bug ids
-func (c *RepoCache) AllBugsIds() []entity.Id {
- c.muBug.RLock()
- defer c.muBug.RUnlock()
-
- result := make([]entity.Id, len(c.bugExcerpts))
-
- i := 0
- for _, excerpt := range c.bugExcerpts {
- result[i] = excerpt.Id
- i++
- }
-
- return result
-}
-
-// ValidLabels list valid labels
-//
-// Note: in the future, a proper label policy could be implemented where valid
-// labels are defined in a configuration file. Until that, the default behavior
-// is to return the list of labels already used.
-func (c *RepoCache) ValidLabels() []bug.Label {
- c.muBug.RLock()
- defer c.muBug.RUnlock()
-
- set := map[bug.Label]interface{}{}
-
- for _, excerpt := range c.bugExcerpts {
- for _, l := range excerpt.Labels {
- set[l] = nil
- }
- }
-
- result := make([]bug.Label, len(set))
-
- i := 0
- for l := range set {
- result[i] = l
- i++
- }
-
- // Sort
- sort.Slice(result, func(i, j int) bool {
- return string(result[i]) < string(result[j])
- })
-
- return result
-}
-
-// NewBug create a new bug
-// The new bug is written in the repository (commit)
-func (c *RepoCache) NewBug(title string, message string) (*BugCache, *bug.CreateOperation, error) {
- return c.NewBugWithFiles(title, message, nil)
-}
-
-// NewBugWithFiles create a new bug with attached files for the message
-// The new bug is written in the repository (commit)
-func (c *RepoCache) NewBugWithFiles(title string, message string, files []repository.Hash) (*BugCache, *bug.CreateOperation, error) {
- author, err := c.GetUserIdentity()
- if err != nil {
- return nil, nil, err
- }
-
- return c.NewBugRaw(author, time.Now().Unix(), title, message, files, nil)
-}
-
-// NewBugRaw create a new bug with attached files for the message, as
-// well as metadata for the Create operation.
-// The new bug is written in the repository (commit)
-func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title string, message string, files []repository.Hash, metadata map[string]string) (*BugCache, *bug.CreateOperation, error) {
- b, op, err := bug.Create(author.Identity, unixTime, title, message, files, metadata)
- if err != nil {
- return nil, nil, err
- }
-
- err = b.Commit(c.repo)
- if err != nil {
- return nil, nil, err
- }
-
- c.muBug.Lock()
- if _, has := c.bugs[b.Id()]; has {
- c.muBug.Unlock()
- return nil, nil, fmt.Errorf("bug %s already exist in the cache", b.Id())
- }
-
- cached := NewBugCache(c, b)
- c.bugs[b.Id()] = cached
- c.loadedBugs.Add(b.Id())
- c.muBug.Unlock()
-
- c.evictIfNeeded()
-
- // force the write of the excerpt
- err = c.bugUpdated(b.Id())
- if err != nil {
- return nil, nil, err
- }
-
- return cached, op, nil
-}
-
-// RemoveBug removes a bug from the cache and repo given a bug id prefix
-func (c *RepoCache) RemoveBug(prefix string) error {
- b, err := c.ResolveBugPrefix(prefix)
- if err != nil {
- return err
- }
-
- c.muBug.Lock()
-
- err = bug.Remove(c.repo, b.Id())
- if err != nil {
- c.muBug.Unlock()
-
- return err
- }
-
- delete(c.bugs, b.Id())
- delete(c.bugExcerpts, b.Id())
- c.loadedBugs.Remove(b.Id())
-
- c.muBug.Unlock()
-
- return c.writeBugCache()
-}
-
-func (c *RepoCache) addBugToSearchIndex(snap *bug.Snapshot) error {
- searchableBug := struct {
- Text []string
- }{}
-
- // See https://github.com/blevesearch/bleve/issues/1576
- var sb strings.Builder
- normalize := func(text string) string {
- sb.Reset()
- for _, field := range strings.Fields(text) {
- if utf8.RuneCountInString(field) < 100 {
- sb.WriteString(field)
- sb.WriteRune(' ')
- }
- }
- return sb.String()
- }
-
- for _, comment := range snap.Comments {
- searchableBug.Text = append(searchableBug.Text, normalize(comment.Message))
- }
-
- searchableBug.Text = append(searchableBug.Text, normalize(snap.Title))
-
- index, err := c.repo.GetBleveIndex("bug")
- if err != nil {
- return err
- }
-
- err = index.Index(snap.Id().String(), searchableBug)
- if err != nil {
- return err
- }
-
- return nil
-}
diff --git a/cache/repo_cache_common.go b/cache/repo_cache_common.go
index 43ac6beb..18ba52f3 100644
--- a/cache/repo_cache_common.go
+++ b/cache/repo_cache_common.go
@@ -126,9 +126,13 @@ func (c *RepoCache) MergeAll(remote string) <-chan entity.MergeResult {
continue
}
+ // TODO: have subcache do the merging?
switch result.Status {
- case entity.MergeStatusNew, entity.MergeStatusUpdated:
+ case entity.MergeStatusNew:
b := result.Entity.(*bug.Bug)
+ _, err := c.bugs.add(b)
+ case entity.MergeStatusUpdated:
+ _, err := c.bugs.entityUpdated(b)
snap := b.Compile()
c.muBug.Lock()
c.bugExcerpts[result.Id] = NewBugExcerpt(b, snap)
diff --git a/cache/repo_cache_identity.go b/cache/repo_cache_identity.go
index 4f612280..a99c7687 100644
--- a/cache/repo_cache_identity.go
+++ b/cache/repo_cache_identity.go
@@ -1,220 +1,28 @@
package cache
import (
- "bytes"
- "encoding/gob"
"fmt"
"github.com/MichaelMure/git-bug/entities/identity"
- "github.com/MichaelMure/git-bug/entity"
)
-const identityCacheFile = "identity-cache"
-
-// identityUpdated is a callback to trigger when the excerpt of an identity
-// changed, that is each time an identity is updated
-func (c *RepoCache) identityUpdated(id entity.Id) error {
- c.muIdentity.Lock()
-
- i, ok := c.identities[id]
- if !ok {
- c.muIdentity.Unlock()
- panic("missing identity in the cache")
- }
-
- c.identitiesExcerpts[id] = NewIdentityExcerpt(i.Identity)
- c.muIdentity.Unlock()
-
- // we only need to write the identity cache
- return c.writeIdentityCache()
-}
-
-// load will try to read from the disk the identity cache file
-func (c *RepoCache) loadIdentityCache() error {
- c.muIdentity.Lock()
- defer c.muIdentity.Unlock()
-
- f, err := c.repo.LocalStorage().Open(identityCacheFile)
- if err != nil {
- return err
- }
-
- decoder := gob.NewDecoder(f)
-
- aux := struct {
- Version uint
- Excerpts map[entity.Id]*IdentityExcerpt
- }{}
-
- err = decoder.Decode(&aux)
- if err != nil {
- return err
- }
-
- if aux.Version != formatVersion {
- return fmt.Errorf("unknown cache format version %v", aux.Version)
- }
-
- c.identitiesExcerpts = aux.Excerpts
- return nil
-}
-
-// write will serialize on disk the identity cache file
-func (c *RepoCache) writeIdentityCache() error {
- c.muIdentity.RLock()
- defer c.muIdentity.RUnlock()
-
- var data bytes.Buffer
-
- aux := struct {
- Version uint
- Excerpts map[entity.Id]*IdentityExcerpt
- }{
- Version: formatVersion,
- Excerpts: c.identitiesExcerpts,
- }
-
- encoder := gob.NewEncoder(&data)
-
- err := encoder.Encode(aux)
- if err != nil {
- return err
- }
-
- f, err := c.repo.LocalStorage().Create(identityCacheFile)
- if err != nil {
- return err
- }
-
- _, err = f.Write(data.Bytes())
- if err != nil {
- return err
- }
-
- return f.Close()
-}
-
-// ResolveIdentityExcerpt retrieve a IdentityExcerpt matching the exact given id
-func (c *RepoCache) ResolveIdentityExcerpt(id entity.Id) (*IdentityExcerpt, error) {
- c.muIdentity.RLock()
- defer c.muIdentity.RUnlock()
-
- e, ok := c.identitiesExcerpts[id]
- if !ok {
- return nil, identity.ErrIdentityNotExist
- }
-
- return e, nil
-}
-
-// ResolveIdentity retrieve an identity matching the exact given id
-func (c *RepoCache) ResolveIdentity(id entity.Id) (*IdentityCache, error) {
- c.muIdentity.RLock()
- cached, ok := c.identities[id]
- c.muIdentity.RUnlock()
- if ok {
- return cached, nil
- }
-
- i, err := identity.ReadLocal(c.repo, id)
- if err != nil {
- return nil, err
- }
-
- cached = NewIdentityCache(c, i)
-
- c.muIdentity.Lock()
- c.identities[id] = cached
- c.muIdentity.Unlock()
-
- return cached, nil
-}
-
-// ResolveIdentityExcerptPrefix retrieve a IdentityExcerpt matching an id prefix.
-// It fails if multiple identities match.
-func (c *RepoCache) ResolveIdentityExcerptPrefix(prefix string) (*IdentityExcerpt, error) {
- return c.ResolveIdentityExcerptMatcher(func(excerpt *IdentityExcerpt) bool {
- return excerpt.Id.HasPrefix(prefix)
- })
-}
-
-// ResolveIdentityPrefix retrieve an Identity matching an id prefix.
-// It fails if multiple identities match.
-func (c *RepoCache) ResolveIdentityPrefix(prefix string) (*IdentityCache, error) {
- return c.ResolveIdentityMatcher(func(excerpt *IdentityExcerpt) bool {
- return excerpt.Id.HasPrefix(prefix)
- })
+type RepoCacheIdentity struct {
+ SubCache[*IdentityExcerpt, *IdentityCache]
}
// ResolveIdentityImmutableMetadata retrieve an Identity that has the exact given metadata on
// one of its version. If multiple version have the same key, the first defined take precedence.
-func (c *RepoCache) ResolveIdentityImmutableMetadata(key string, value string) (*IdentityCache, error) {
- return c.ResolveIdentityMatcher(func(excerpt *IdentityExcerpt) bool {
+func (c *RepoCacheIdentity) ResolveIdentityImmutableMetadata(key string, value string) (*IdentityCache, error) {
+ return c.ResolveMatcher(func(excerpt *IdentityExcerpt) bool {
return excerpt.ImmutableMetadata[key] == value
})
}
-func (c *RepoCache) ResolveIdentityExcerptMatcher(f func(*IdentityExcerpt) bool) (*IdentityExcerpt, error) {
- id, err := c.resolveIdentityMatcher(f)
- if err != nil {
- return nil, err
- }
- return c.ResolveIdentityExcerpt(id)
-}
-
-func (c *RepoCache) ResolveIdentityMatcher(f func(*IdentityExcerpt) bool) (*IdentityCache, error) {
- id, err := c.resolveIdentityMatcher(f)
- if err != nil {
- return nil, err
- }
- return c.ResolveIdentity(id)
-}
-
-func (c *RepoCache) resolveIdentityMatcher(f func(*IdentityExcerpt) bool) (entity.Id, error) {
- c.muIdentity.RLock()
- defer c.muIdentity.RUnlock()
-
- // preallocate but empty
- matching := make([]entity.Id, 0, 5)
-
- for _, excerpt := range c.identitiesExcerpts {
- if f(excerpt) {
- matching = append(matching, excerpt.Id)
- }
- }
-
- if len(matching) > 1 {
- return entity.UnsetId, identity.NewErrMultipleMatch(matching)
- }
-
- if len(matching) == 0 {
- return entity.UnsetId, identity.ErrIdentityNotExist
- }
-
- return matching[0], nil
-}
-
-// AllIdentityIds return all known identity ids
-func (c *RepoCache) AllIdentityIds() []entity.Id {
- c.muIdentity.RLock()
- defer c.muIdentity.RUnlock()
-
- result := make([]entity.Id, len(c.identitiesExcerpts))
-
- i := 0
- for _, excerpt := range c.identitiesExcerpts {
- result[i] = excerpt.Id
- i++
- }
-
- return result
-}
-
-func (c *RepoCache) NewIdentityFromGitUser() (*IdentityCache, error) {
+func (c *RepoCacheIdentity) NewIdentityFromGitUser() (*IdentityCache, error) {
return c.NewIdentityFromGitUserRaw(nil)
}
-func (c *RepoCache) NewIdentityFromGitUserRaw(metadata map[string]string) (*IdentityCache, error) {
+func (c *RepoCacheIdentity) NewIdentityFromGitUserRaw(metadata map[string]string) (*IdentityCache, error) {
i, err := identity.NewFromGitUser(c.repo)
if err != nil {
return nil, err
@@ -224,17 +32,17 @@ func (c *RepoCache) NewIdentityFromGitUserRaw(metadata map[string]string) (*Iden
// NewIdentity create a new identity
// The new identity is written in the repository (commit)
-func (c *RepoCache) NewIdentity(name string, email string) (*IdentityCache, error) {
+func (c *RepoCacheIdentity) NewIdentity(name string, email string) (*IdentityCache, error) {
return c.NewIdentityRaw(name, email, "", "", nil, nil)
}
// NewIdentityFull create a new identity
// The new identity is written in the repository (commit)
-func (c *RepoCache) NewIdentityFull(name string, email string, login string, avatarUrl string, keys []*identity.Key) (*IdentityCache, error) {
+func (c *RepoCacheIdentity) NewIdentityFull(name string, email string, login string, avatarUrl string, keys []*identity.Key) (*IdentityCache, error) {
return c.NewIdentityRaw(name, email, login, avatarUrl, keys, nil)
}
-func (c *RepoCache) NewIdentityRaw(name string, email string, login string, avatarUrl string, keys []*identity.Key, metadata map[string]string) (*IdentityCache, error) {
+func (c *RepoCacheIdentity) NewIdentityRaw(name string, email string, login string, avatarUrl string, keys []*identity.Key, metadata map[string]string) (*IdentityCache, error) {
i, err := identity.NewIdentityFull(c.repo, name, email, login, avatarUrl, keys)
if err != nil {
return nil, err
@@ -242,7 +50,7 @@ func (c *RepoCache) NewIdentityRaw(name string, email string, login string, avat
return c.finishIdentity(i, metadata)
}
-func (c *RepoCache) finishIdentity(i *identity.Identity, metadata map[string]string) (*IdentityCache, error) {
+func (c *RepoCacheIdentity) finishIdentity(i *identity.Identity, metadata map[string]string) (*IdentityCache, error) {
for key, value := range metadata {
i.SetMetadata(key, value)
}
@@ -252,17 +60,17 @@ func (c *RepoCache) finishIdentity(i *identity.Identity, metadata map[string]str
return nil, err
}
- c.muIdentity.Lock()
- if _, has := c.identities[i.Id()]; has {
+ c.mu.Lock()
+ if _, has := c.cached[i.Id()]; has {
return nil, fmt.Errorf("identity %s already exist in the cache", i.Id())
}
cached := NewIdentityCache(c, i)
- c.identities[i.Id()] = cached
- c.muIdentity.Unlock()
+ c.cached[i.Id()] = cached
+ c.mu.Unlock()
// force the write of the excerpt
- err = c.identityUpdated(i.Id())
+ err = c.entityUpdated(i.Id())
if err != nil {
return nil, err
}
diff --git a/cache/repo_cache_test.go b/cache/repo_cache_test.go
index a9557ff0..58ade144 100644
--- a/cache/repo_cache_test.go
+++ b/cache/repo_cache_test.go
@@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/MichaelMure/git-bug/entities/bug"
+ "github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/query"
"github.com/MichaelMure/git-bug/repository"
)
@@ -213,7 +214,7 @@ func TestRemove(t *testing.T) {
assert.Equal(t, 1, len(repoCache.bugExcerpts))
_, err = repoCache.ResolveBug(b1.Id())
- assert.Error(t, bug.ErrBugNotExist, err)
+ assert.ErrorIs(t, entity.ErrNotFound{}, err)
}
func TestCacheEviction(t *testing.T) {
diff --git a/cache/subcache.go b/cache/subcache.go
new file mode 100644
index 00000000..658781d9
--- /dev/null
+++ b/cache/subcache.go
@@ -0,0 +1,362 @@
+package cache
+
+import (
+ "bytes"
+ "encoding/gob"
+ "fmt"
+ "sync"
+
+ "github.com/pkg/errors"
+
+ "github.com/MichaelMure/git-bug/entities/bug"
+ "github.com/MichaelMure/git-bug/entities/identity"
+ "github.com/MichaelMure/git-bug/entity"
+ "github.com/MichaelMure/git-bug/repository"
+)
+
+type Excerpt interface {
+ Id() entity.Id
+}
+
+type CacheEntity interface {
+ NeedCommit() bool
+}
+
+type getUserIdentityFunc func() (identity.Interface, error)
+
+type SubCache[ExcerptT Excerpt, CacheT CacheEntity, EntityT entity.Interface] struct {
+ repo repository.ClockedRepo
+ resolvers entity.Resolvers
+
+ getUserIdentity getUserIdentityFunc
+ readWithResolver func(repository.ClockedRepo, entity.Resolvers, entity.Id) (EntityT, error)
+ makeCached func(*SubCache[ExcerptT, CacheT, EntityT], getUserIdentityFunc, EntityT) CacheT
+ makeExcerpt func() Excerpt
+
+ typename string
+ namespace string
+ version uint
+ maxLoaded int
+
+ mu sync.RWMutex
+ excerpts map[entity.Id]ExcerptT
+ cached map[entity.Id]CacheT
+ lru *lruIdCache
+}
+
+func NewSubCache[ExcerptT Excerpt, CacheT CacheEntity, EntityT entity.Interface](
+ repo repository.ClockedRepo,
+ resolvers entity.Resolvers,
+ getUserIdentity func() (identity.Interface, error),
+ typename, namespace string,
+ version uint, maxLoaded int) *SubCache[ExcerptT, CacheT, EntityT] {
+ return &SubCache[ExcerptT, CacheT, EntityT]{
+ repo: repo,
+ resolvers: resolvers,
+ getUserIdentity: getUserIdentity,
+ typename: typename,
+ namespace: namespace,
+ version: version,
+ maxLoaded: maxLoaded,
+ excerpts: make(map[entity.Id]ExcerptT),
+ cached: make(map[entity.Id]CacheT),
+ lru: newLRUIdCache(),
+ }
+}
+
+// Load will try to read from the disk the entity cache file
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) Load() error {
+ sc.mu.Lock()
+ defer sc.mu.Unlock()
+
+ f, err := sc.repo.LocalStorage().Open(sc.namespace + "-file")
+ if err != nil {
+ return err
+ }
+
+ decoder := gob.NewDecoder(f)
+
+ aux := struct {
+ Version uint
+ Excerpts map[entity.Id]ExcerptT
+ }{}
+
+ err = decoder.Decode(&aux)
+ if err != nil {
+ return err
+ }
+
+ if aux.Version != sc.version {
+ return fmt.Errorf("unknown %s cache format version %v", sc.namespace, aux.Version)
+ }
+
+ sc.excerpts = aux.Excerpts
+
+ index, err := sc.repo.GetBleveIndex("bug")
+ if err != nil {
+ return err
+ }
+
+ // simple heuristic to detect a mismatch between the index and the entities
+ count, err := index.DocCount()
+ if err != nil {
+ return err
+ }
+ if count != uint64(len(sc.excerpts)) {
+ return fmt.Errorf("count mismatch between bleve and %s excerpts", sc.namespace)
+ }
+
+ return nil
+}
+
+// Write will serialize on disk the entity cache file
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) Write() error {
+ sc.mu.RLock()
+ defer sc.mu.RUnlock()
+
+ var data bytes.Buffer
+
+ aux := struct {
+ Version uint
+ Excerpts map[entity.Id]ExcerptT
+ }{
+ Version: sc.version,
+ Excerpts: sc.excerpts,
+ }
+
+ encoder := gob.NewEncoder(&data)
+
+ err := encoder.Encode(aux)
+ if err != nil {
+ return err
+ }
+
+ f, err := sc.repo.LocalStorage().Create(sc.namespace + "-file")
+ if err != nil {
+ return err
+ }
+
+ _, err = f.Write(data.Bytes())
+ if err != nil {
+ return err
+ }
+
+ return f.Close()
+}
+
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) Build() {
+
+}
+
+// AllIds return all known bug ids
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) AllIds() []entity.Id {
+ sc.mu.RLock()
+ defer sc.mu.RUnlock()
+
+ result := make([]entity.Id, len(sc.excerpts))
+
+ i := 0
+ for _, excerpt := range sc.excerpts {
+ result[i] = excerpt.Id()
+ i++
+ }
+
+ return result
+}
+
+// Resolve retrieve an entity matching the exact given id
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) Resolve(id entity.Id) (CacheT, error) {
+ sc.mu.RLock()
+ cached, ok := sc.cached[id]
+ if ok {
+ sc.lru.Get(id)
+ sc.mu.RUnlock()
+ return cached, nil
+ }
+ sc.mu.RUnlock()
+
+ b, err := sc.readWithResolver(sc.repo, sc.resolvers, id)
+ if err != nil {
+ return nil, err
+ }
+
+ cached = sc.makeCached(sc, sc.getUserIdentity, b)
+
+ sc.mu.Lock()
+ sc.cached[id] = cached
+ sc.lru.Add(id)
+ sc.mu.Unlock()
+
+ sc.evictIfNeeded()
+
+ return cached, nil
+}
+
+// ResolvePrefix retrieve an entity matching an id prefix. It fails if multiple
+// entity match.
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) ResolvePrefix(prefix string) (CacheT, error) {
+ return sc.ResolveMatcher(func(excerpt ExcerptT) bool {
+ return excerpt.Id().HasPrefix(prefix)
+ })
+}
+
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) ResolveMatcher(f func(ExcerptT) bool) (CacheT, error) {
+ id, err := sc.resolveMatcher(f)
+ if err != nil {
+ return nil, err
+ }
+ return sc.Resolve(id)
+}
+
+// ResolveExcerpt retrieve an Excerpt matching the exact given id
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) ResolveExcerpt(id entity.Id) (ExcerptT, error) {
+ sc.mu.RLock()
+ defer sc.mu.RUnlock()
+
+ excerpt, ok := sc.excerpts[id]
+ if !ok {
+ return nil, entity.NewErrNotFound(sc.typename)
+ }
+
+ return excerpt, nil
+}
+
+// ResolveExcerptPrefix retrieve an Excerpt matching an id prefix. It fails if multiple
+// entity match.
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) ResolveExcerptPrefix(prefix string) (ExcerptT, error) {
+ return sc.ResolveExcerptMatcher(func(excerpt ExcerptT) bool {
+ return excerpt.Id().HasPrefix(prefix)
+ })
+}
+
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) ResolveExcerptMatcher(f func(ExcerptT) bool) (ExcerptT, error) {
+ id, err := sc.resolveMatcher(f)
+ if err != nil {
+ return nil, err
+ }
+ return sc.ResolveExcerpt(id)
+}
+
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) resolveMatcher(f func(ExcerptT) bool) (entity.Id, error) {
+ sc.mu.RLock()
+ defer sc.mu.RUnlock()
+
+ // preallocate but empty
+ matching := make([]entity.Id, 0, 5)
+
+ for _, excerpt := range sc.excerpts {
+ if f(excerpt) {
+ matching = append(matching, excerpt.Id())
+ }
+ }
+
+ if len(matching) > 1 {
+ return entity.UnsetId, entity.NewErrMultipleMatch(sc.typename, matching)
+ }
+
+ if len(matching) == 0 {
+ return entity.UnsetId, entity.NewErrNotFound(sc.typename)
+ }
+
+ return matching[0], nil
+}
+
+var errNotInCache = errors.New("entity missing from cache")
+
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) add(e EntityT) (CacheT, error) {
+ sc.mu.Lock()
+ if _, has := sc.cached[e.Id()]; has {
+ sc.mu.Unlock()
+ return nil, fmt.Errorf("entity %s already exist in the cache", e.Id())
+ }
+
+ cached := sc.makeCached(sc, sc.getUserIdentity, e)
+ sc.cached[e.Id()] = cached
+ sc.lru.Add(e.Id())
+ sc.mu.Unlock()
+
+ sc.evictIfNeeded()
+
+ // force the write of the excerpt
+ err := sc.entityUpdated(e.Id())
+ if err != nil {
+ return nil, err
+ }
+
+ return cached, nil
+}
+
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) Remove(prefix string) error {
+ e, err := sc.ResolvePrefix(prefix)
+ if err != nil {
+ return err
+ }
+
+ sc.mu.Lock()
+
+ err = bug.Remove(c.repo, b.Id())
+ if err != nil {
+ c.muBug.Unlock()
+
+ return err
+ }
+
+ delete(c.bugs, b.Id())
+ delete(c.bugExcerpts, b.Id())
+ c.loadedBugs.Remove(b.Id())
+
+ c.muBug.Unlock()
+
+ return c.writeBugCache()
+}
+
+// entityUpdated is a callback to trigger when the excerpt of an entity changed
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) entityUpdated(id entity.Id) error {
+ sc.mu.Lock()
+ b, ok := sc.cached[id]
+ if !ok {
+ sc.mu.Unlock()
+
+ // if the bug is not loaded at this point, it means it was loaded before
+ // but got evicted. Which means we potentially have multiple copies in
+ // memory and thus concurrent write.
+ // Failing immediately here is the simple and safe solution to avoid
+ // complicated data loss.
+ return errNotInCache
+ }
+ sc.lru.Get(id)
+ // sc.excerpts[id] = bug2.NewBugExcerpt(b.bug, b.Snapshot())
+ sc.excerpts[id] = bug2.NewBugExcerpt(b.bug, b.Snapshot())
+ sc.mu.Unlock()
+
+ if err := sc.addBugToSearchIndex(b.Snapshot()); err != nil {
+ return err
+ }
+
+ // we only need to write the bug cache
+ return sc.Write()
+}
+
+// evictIfNeeded will evict an entity from the cache if needed
+func (sc *SubCache[ExcerptT, CacheT, EntityT]) evictIfNeeded() {
+ sc.mu.Lock()
+ defer sc.mu.Unlock()
+ if sc.lru.Len() <= sc.maxLoaded {
+ return
+ }
+
+ for _, id := range sc.lru.GetOldestToNewest() {
+ b := sc.cached[id]
+ if b.NeedCommit() {
+ continue
+ }
+
+ b.Lock()
+ sc.lru.Remove(id)
+ delete(sc.cached, id)
+
+ if sc.lru.Len() <= sc.maxLoaded {
+ return
+ }
+ }
+}