2019-02-17 18:12:06 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2022-12-23 01:19:31 +03:00
|
|
|
"sync"
|
|
|
|
|
2022-08-19 00:34:05 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entities/identity"
|
2022-11-19 13:33:12 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2020-11-08 21:18:44 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2019-02-17 18:12:06 +03:00
|
|
|
)
|
|
|
|
|
2020-09-16 17:22:02 +03:00
|
|
|
var _ identity.Interface = &IdentityCache{}
|
2022-12-23 01:19:31 +03:00
|
|
|
var _ CacheEntity = &IdentityCache{}
|
2020-09-16 17:22:02 +03:00
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
// IdentityCache is a wrapper around an Identity for caching.
|
2019-02-17 18:12:06 +03:00
|
|
|
type IdentityCache struct {
|
2022-11-19 13:33:12 +03:00
|
|
|
repo repository.ClockedRepo
|
2022-12-19 20:09:59 +03:00
|
|
|
entityUpdated func(id entity.Id) error
|
2022-11-19 13:33:12 +03:00
|
|
|
|
2022-12-23 01:19:31 +03:00
|
|
|
mu sync.Mutex
|
2019-02-17 18:12:06 +03:00
|
|
|
*identity.Identity
|
|
|
|
}
|
|
|
|
|
2022-12-19 20:09:59 +03:00
|
|
|
func NewIdentityCache(i *identity.Identity, repo repository.ClockedRepo, entityUpdated func(id entity.Id) error) *IdentityCache {
|
2019-02-17 18:12:06 +03:00
|
|
|
return &IdentityCache{
|
2022-12-19 20:09:59 +03:00
|
|
|
repo: repo,
|
|
|
|
entityUpdated: entityUpdated,
|
|
|
|
Identity: i,
|
2019-02-17 18:12:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:11:37 +03:00
|
|
|
func (i *IdentityCache) notifyUpdated() error {
|
2022-11-19 13:33:12 +03:00
|
|
|
return i.entityUpdated(i.Identity.Id())
|
2019-02-18 16:11:37 +03:00
|
|
|
}
|
|
|
|
|
2020-11-08 21:18:44 +03:00
|
|
|
func (i *IdentityCache) Mutate(repo repository.RepoClock, f func(*identity.Mutator)) error {
|
2022-12-23 01:19:31 +03:00
|
|
|
i.mu.Lock()
|
2020-11-08 21:18:44 +03:00
|
|
|
err := i.Identity.Mutate(repo, f)
|
2022-12-23 01:19:31 +03:00
|
|
|
i.mu.Unlock()
|
2020-11-08 21:18:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-19 01:16:47 +03:00
|
|
|
return i.notifyUpdated()
|
|
|
|
}
|
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
func (i *IdentityCache) Commit() error {
|
2022-12-23 01:19:31 +03:00
|
|
|
i.mu.Lock()
|
2022-11-19 13:33:12 +03:00
|
|
|
err := i.Identity.Commit(i.repo)
|
2022-12-23 01:19:31 +03:00
|
|
|
i.mu.Unlock()
|
2019-02-19 01:16:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return i.notifyUpdated()
|
2019-02-17 18:12:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IdentityCache) CommitAsNeeded() error {
|
2022-12-23 01:19:31 +03:00
|
|
|
i.mu.Lock()
|
2022-11-19 13:33:12 +03:00
|
|
|
err := i.Identity.CommitAsNeeded(i.repo)
|
2022-12-23 01:19:31 +03:00
|
|
|
i.mu.Unlock()
|
2019-02-19 01:16:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return i.notifyUpdated()
|
2019-02-17 18:12:06 +03:00
|
|
|
}
|
2022-12-23 01:19:31 +03:00
|
|
|
|
|
|
|
func (i *IdentityCache) Lock() {
|
|
|
|
i.mu.Lock()
|
|
|
|
}
|