aboutsummaryrefslogtreecommitdiffstats
path: root/cache
diff options
context:
space:
mode:
Diffstat (limited to 'cache')
-rw-r--r--cache/filter.go65
-rw-r--r--cache/filter_test.go2
-rw-r--r--cache/repo_cache.go87
3 files changed, 123 insertions, 31 deletions
diff --git a/cache/filter.go b/cache/filter.go
index 9b1de1d5..f840067b 100644
--- a/cache/filter.go
+++ b/cache/filter.go
@@ -4,10 +4,17 @@ import (
"strings"
"github.com/MichaelMure/git-bug/bug"
+ "github.com/MichaelMure/git-bug/entity"
)
+// resolver has the resolving functions needed by filters.
+// This exist mainly to go through the functions of the cache with proper locking.
+type resolver interface {
+ ResolveIdentityExcerpt(id entity.Id) (*IdentityExcerpt, error)
+}
+
// Filter is a predicate that match a subset of bugs
-type Filter func(repoCache *RepoCache, excerpt *BugExcerpt) bool
+type Filter func(excerpt *BugExcerpt, resolver resolver) bool
// StatusFilter return a Filter that match a bug status
func StatusFilter(query string) (Filter, error) {
@@ -16,21 +23,21 @@ func StatusFilter(query string) (Filter, error) {
return nil, err
}
- return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
+ return func(excerpt *BugExcerpt, resolver resolver) bool {
return excerpt.Status == status
}, nil
}
// AuthorFilter return a Filter that match a bug author
func AuthorFilter(query string) Filter {
- return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
+ return func(excerpt *BugExcerpt, resolver resolver) bool {
query = strings.ToLower(query)
// Normal identity
if excerpt.AuthorId != "" {
- author, ok := repoCache.identitiesExcerpts[excerpt.AuthorId]
- if !ok {
- panic("missing identity in the cache")
+ author, err := resolver.ResolveIdentityExcerpt(excerpt.AuthorId)
+ if err != nil {
+ panic(err)
}
return author.Match(query)
@@ -43,7 +50,7 @@ func AuthorFilter(query string) Filter {
// LabelFilter return a Filter that match a label
func LabelFilter(label string) Filter {
- return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
+ return func(excerpt *BugExcerpt, resolver resolver) bool {
for _, l := range excerpt.Labels {
if string(l) == label {
return true
@@ -55,13 +62,13 @@ func LabelFilter(label string) Filter {
// ActorFilter return a Filter that match a bug actor
func ActorFilter(query string) Filter {
- return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
+ return func(excerpt *BugExcerpt, resolver resolver) bool {
query = strings.ToLower(query)
for _, id := range excerpt.Actors {
- identityExcerpt, ok := repoCache.identitiesExcerpts[id]
- if !ok {
- panic("missing identity in the cache")
+ identityExcerpt, err := resolver.ResolveIdentityExcerpt(id)
+ if err != nil {
+ panic(err)
}
if identityExcerpt.Match(query) {
@@ -74,13 +81,13 @@ func ActorFilter(query string) Filter {
// ParticipantFilter return a Filter that match a bug participant
func ParticipantFilter(query string) Filter {
- return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
+ return func(excerpt *BugExcerpt, resolver resolver) bool {
query = strings.ToLower(query)
for _, id := range excerpt.Participants {
- identityExcerpt, ok := repoCache.identitiesExcerpts[id]
- if !ok {
- panic("missing identity in the cache")
+ identityExcerpt, err := resolver.ResolveIdentityExcerpt(id)
+ if err != nil {
+ panic(err)
}
if identityExcerpt.Match(query) {
@@ -93,7 +100,7 @@ func ParticipantFilter(query string) Filter {
// TitleFilter return a Filter that match if the title contains the given query
func TitleFilter(query string) Filter {
- return func(repo *RepoCache, excerpt *BugExcerpt) bool {
+ return func(excerpt *BugExcerpt, resolver resolver) bool {
return strings.Contains(
strings.ToLower(excerpt.Title),
strings.ToLower(query),
@@ -103,7 +110,7 @@ func TitleFilter(query string) Filter {
// NoLabelFilter return a Filter that match the absence of labels
func NoLabelFilter() Filter {
- return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
+ return func(excerpt *BugExcerpt, resolver resolver) bool {
return len(excerpt.Labels) == 0
}
}
@@ -120,32 +127,32 @@ type Filters struct {
}
// Match check if a bug match the set of filters
-func (f *Filters) Match(repoCache *RepoCache, excerpt *BugExcerpt) bool {
- if match := f.orMatch(f.Status, repoCache, excerpt); !match {
+func (f *Filters) Match(excerpt *BugExcerpt, resolver resolver) bool {
+ if match := f.orMatch(f.Status, excerpt, resolver); !match {
return false
}
- if match := f.orMatch(f.Author, repoCache, excerpt); !match {
+ if match := f.orMatch(f.Author, excerpt, resolver); !match {
return false
}
- if match := f.orMatch(f.Participant, repoCache, excerpt); !match {
+ if match := f.orMatch(f.Participant, excerpt, resolver); !match {
return false
}
- if match := f.orMatch(f.Actor, repoCache, excerpt); !match {
+ if match := f.orMatch(f.Actor, excerpt, resolver); !match {
return false
}
- if match := f.andMatch(f.Label, repoCache, excerpt); !match {
+ if match := f.andMatch(f.Label, excerpt, resolver); !match {
return false
}
- if match := f.andMatch(f.NoFilters, repoCache, excerpt); !match {
+ if match := f.andMatch(f.NoFilters, excerpt, resolver); !match {
return false
}
- if match := f.andMatch(f.Title, repoCache, excerpt); !match {
+ if match := f.andMatch(f.Title, excerpt, resolver); !match {
return false
}
@@ -153,28 +160,28 @@ func (f *Filters) Match(repoCache *RepoCache, excerpt *BugExcerpt) bool {
}
// Check if any of the filters provided match the bug
-func (*Filters) orMatch(filters []Filter, repoCache *RepoCache, excerpt *BugExcerpt) bool {
+func (*Filters) orMatch(filters []Filter, excerpt *BugExcerpt, resolver resolver) bool {
if len(filters) == 0 {
return true
}
match := false
for _, f := range filters {
- match = match || f(repoCache, excerpt)
+ match = match || f(excerpt, resolver)
}
return match
}
// Check if all of the filters provided match the bug
-func (*Filters) andMatch(filters []Filter, repoCache *RepoCache, excerpt *BugExcerpt) bool {
+func (*Filters) andMatch(filters []Filter, excerpt *BugExcerpt, resolver resolver) bool {
if len(filters) == 0 {
return true
}
match := true
for _, f := range filters {
- match = match && f(repoCache, excerpt)
+ match = match && f(excerpt, resolver)
}
return match
diff --git a/cache/filter_test.go b/cache/filter_test.go
index a47d2ad7..2c7fabc0 100644
--- a/cache/filter_test.go
+++ b/cache/filter_test.go
@@ -28,7 +28,7 @@ func TestTitleFilter(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
filter := TitleFilter(tt.query)
excerpt := &BugExcerpt{Title: tt.title}
- assert.Equal(t, tt.match, filter(nil, excerpt))
+ assert.Equal(t, tt.match, filter(excerpt, nil))
})
}
}
diff --git a/cache/repo_cache.go b/cache/repo_cache.go
index 442a7c8b..c8d6bd69 100644
--- a/cache/repo_cache.go
+++ b/cache/repo_cache.go
@@ -10,6 +10,7 @@ import (
"path"
"sort"
"strconv"
+ "sync"
"time"
"github.com/pkg/errors"
@@ -57,11 +58,13 @@ type RepoCache struct {
// the underlying repo
repo repository.ClockedRepo
+ muBug sync.RWMutex
// excerpt of bugs data for all bugs
bugExcerpts map[entity.Id]*BugExcerpt
// bug loaded in memory
bugs map[entity.Id]*BugCache
+ muIdentity sync.RWMutex
// excerpt of identities data for all identities
identitiesExcerpts map[entity.Id]*IdentityExcerpt
// identities loaded in memory
@@ -157,6 +160,11 @@ func (c *RepoCache) lock() error {
}
func (c *RepoCache) Close() error {
+ c.muBug.Lock()
+ defer c.muBug.Unlock()
+ c.muIdentity.Lock()
+ defer c.muIdentity.Unlock()
+
c.identities = make(map[entity.Id]*IdentityCache)
c.identitiesExcerpts = nil
c.bugs = make(map[entity.Id]*BugCache)
@@ -169,12 +177,16 @@ func (c *RepoCache) Close() error {
// 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()
panic("missing bug in the cache")
}
c.bugExcerpts[id] = NewBugExcerpt(b.bug, b.Snapshot())
+ c.muBug.Unlock()
// we only need to write the bug cache
return c.writeBugCache()
@@ -183,12 +195,16 @@ func (c *RepoCache) bugUpdated(id entity.Id) error {
// 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()
@@ -205,6 +221,9 @@ func (c *RepoCache) load() error {
// 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 := os.Open(bugCacheFilePath(c.repo))
if err != nil {
return err
@@ -234,6 +253,9 @@ func (c *RepoCache) loadBugCache() error {
// 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 := os.Open(identityCacheFilePath(c.repo))
if err != nil {
return err
@@ -272,6 +294,9 @@ func (c *RepoCache) write() error {
// 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 {
@@ -304,6 +329,9 @@ func (c *RepoCache) writeBugCache() error {
// 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 {
@@ -343,6 +371,11 @@ func identityCacheFilePath(repo repository.Repo) string {
}
func (c *RepoCache) buildCache() error {
+ c.muBug.Lock()
+ defer c.muBug.Unlock()
+ c.muIdentity.Lock()
+ defer c.muIdentity.Unlock()
+
_, _ = fmt.Fprintf(os.Stderr, "Building identity cache... ")
c.identitiesExcerpts = make(map[entity.Id]*IdentityExcerpt)
@@ -380,6 +413,9 @@ func (c *RepoCache) buildCache() error {
// 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()
+
e, ok := c.bugExcerpts[id]
if !ok {
return nil, bug.ErrBugNotExist
@@ -390,7 +426,9 @@ func (c *RepoCache) ResolveBugExcerpt(id entity.Id) (*BugExcerpt, error) {
// 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]
+ c.muBug.RUnlock()
if ok {
return cached, nil
}
@@ -401,7 +439,10 @@ func (c *RepoCache) ResolveBug(id entity.Id) (*BugCache, error) {
}
cached = NewBugCache(c, b)
+
+ c.muBug.Lock()
c.bugs[id] = cached
+ c.muBug.Unlock()
return cached, nil
}
@@ -448,6 +489,9 @@ func (c *RepoCache) ResolveBugMatcher(f func(*BugExcerpt) bool) (*BugCache, erro
}
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)
@@ -470,6 +514,9 @@ 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(query *Query) []entity.Id {
+ c.muBug.RLock()
+ defer c.muBug.RUnlock()
+
if query == nil {
return c.AllBugsIds()
}
@@ -477,7 +524,7 @@ func (c *RepoCache) QueryBugs(query *Query) []entity.Id {
var filtered []*BugExcerpt
for _, excerpt := range c.bugExcerpts {
- if query.Match(c, excerpt) {
+ if query.Match(excerpt, c) {
filtered = append(filtered, excerpt)
}
}
@@ -512,6 +559,9 @@ func (c *RepoCache) QueryBugs(query *Query) []entity.Id {
// 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
@@ -529,6 +579,9 @@ func (c *RepoCache) AllBugsIds() []entity.Id {
// 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 {
@@ -588,12 +641,15 @@ func (c *RepoCache) NewBugRaw(author *IdentityCache, unixTime int64, title strin
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.muBug.Unlock()
// force the write of the excerpt
err = c.bugUpdated(b.Id())
@@ -639,7 +695,9 @@ func (c *RepoCache) MergeAll(remote string) <-chan entity.MergeResult {
switch result.Status {
case entity.MergeStatusNew, entity.MergeStatusUpdated:
i := result.Entity.(*identity.Identity)
+ c.muIdentity.Lock()
c.identitiesExcerpts[result.Id] = NewIdentityExcerpt(i)
+ c.muIdentity.Unlock()
}
}
@@ -655,7 +713,9 @@ func (c *RepoCache) MergeAll(remote string) <-chan entity.MergeResult {
case entity.MergeStatusNew, entity.MergeStatusUpdated:
b := result.Entity.(*bug.Bug)
snap := b.Compile()
+ c.muBug.Lock()
c.bugExcerpts[result.Id] = NewBugExcerpt(b, &snap)
+ c.muBug.Unlock()
}
}
@@ -771,6 +831,9 @@ func repoIsAvailable(repo repository.Repo) error {
// 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
@@ -781,7 +844,9 @@ func (c *RepoCache) ResolveIdentityExcerpt(id entity.Id) (*IdentityExcerpt, erro
// 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
}
@@ -792,7 +857,10 @@ func (c *RepoCache) ResolveIdentity(id entity.Id) (*IdentityCache, error) {
}
cached = NewIdentityCache(c, i)
+
+ c.muIdentity.Lock()
c.identities[id] = cached
+ c.muIdentity.Unlock()
return cached, nil
}
@@ -838,6 +906,9 @@ func (c *RepoCache) ResolveIdentityMatcher(f func(*IdentityExcerpt) bool) (*Iden
}
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)
@@ -860,6 +931,9 @@ func (c *RepoCache) resolveIdentityMatcher(f func(*IdentityExcerpt) bool) (entit
// 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
@@ -877,6 +951,9 @@ func (c *RepoCache) SetUserIdentity(i *IdentityCache) error {
return err
}
+ c.muIdentity.RLock()
+ defer c.muIdentity.RUnlock()
+
// Make sure that everything is fine
if _, ok := c.identities[i.Id()]; !ok {
panic("SetUserIdentity while the identity is not from the cache, something is wrong")
@@ -895,6 +972,9 @@ func (c *RepoCache) GetUserIdentity() (*IdentityCache, error) {
}
}
+ c.muIdentity.Lock()
+ defer c.muIdentity.Unlock()
+
i, err := identity.GetUserIdentity(c.repo)
if err != nil {
return nil, err
@@ -916,6 +996,9 @@ func (c *RepoCache) GetUserIdentityExcerpt() (*IdentityExcerpt, error) {
c.userIdentityId = id
}
+ c.muIdentity.RLock()
+ defer c.muIdentity.RUnlock()
+
excerpt, ok := c.identitiesExcerpts[c.userIdentityId]
if !ok {
return nil, fmt.Errorf("cache: missing identity excerpt %v", c.userIdentityId)
@@ -966,12 +1049,14 @@ func (c *RepoCache) finishIdentity(i *identity.Identity, metadata map[string]str
return nil, err
}
+ c.muIdentity.Lock()
if _, has := c.identities[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()
// force the write of the excerpt
err = c.identityUpdated(i.Id())