Add tree cache service

This commit is contained in:
mcrakhman 2022-07-14 21:23:40 +02:00 committed by Mikhail Iudin
parent d2f2cdb40c
commit d0dfb9d8be
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
3 changed files with 47 additions and 18 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger"
"github.com/anytypeio/go-anytype-infrastructure-experiments/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/config"
"github.com/anytypeio/go-anytype-infrastructure-experiments/service/account" "github.com/anytypeio/go-anytype-infrastructure-experiments/service/account"
"github.com/anytypeio/go-anytype-infrastructure-experiments/service/treecache"
"go.uber.org/zap" "go.uber.org/zap"
"os" "os"
"os/signal" "os/signal"
@ -55,6 +56,7 @@ func main() {
// bootstrap components // bootstrap components
a.Register(conf) a.Register(conf)
a.Register(acc) a.Register(acc)
a.Register(treecache.NewTreeCache())
Bootstrap(a) Bootstrap(a)
// start app // start app

View File

@ -27,6 +27,12 @@ type TreeUpdateListener interface {
Rebuild(tree ACLTree) Rebuild(tree ACLTree)
} }
type NoOpListener struct{}
func (n NoOpListener) Update(tree ACLTree) {}
func (n NoOpListener) Rebuild(tree ACLTree) {}
type ACLTree interface { type ACLTree interface {
ACLState() *ACLState ACLState() *ACLState
AddContent(ctx context.Context, f func(builder ChangeBuilder) error) (*Change, error) AddContent(ctx context.Context, f func(builder ChangeBuilder) error) (*Change, error)
@ -195,6 +201,7 @@ func (a *aclTree) rebuildFromStorage(fromStart bool) error {
} }
func (a *aclTree) ACLState() *ACLState { func (a *aclTree) ACLState() *ACLState {
// TODO: probably locks should be happening outside because we are using object cache
a.RLock() a.RLock()
defer a.RUnlock() defer a.RUnlock()
return a.aclState return a.aclState

View File

@ -6,39 +6,59 @@ import (
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/acltree" "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/acltree"
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treestorage" "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/treestorage"
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache" "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache"
"github.com/anytypeio/go-anytype-infrastructure-experiments/service/account"
) )
type TreeCache interface { const CName = "treecache"
Do(ctx context.Context, treeId string, f func(tree acltree.ACLTree)) error
type Service interface {
Do(ctx context.Context, treeId string, f func(tree acltree.ACLTree) error) error
} }
type service struct { type service struct {
treeProvider treestorage.Provider treeProvider treestorage.Provider
account account.Service
cache ocache.OCache cache ocache.OCache
} }
func NewTreeCache() app.ComponentRunnable {
return &service{}
}
func (s *service) Do(ctx context.Context, treeId string, f func(tree acltree.ACLTree) error) error {
tree, err := s.cache.Get(ctx, treeId)
defer s.cache.Release(treeId)
if err != nil {
return err
}
return f(tree.(acltree.ACLTree))
}
func (s *service) Init(ctx context.Context, a *app.App) (err error) { func (s *service) Init(ctx context.Context, a *app.App) (err error) {
s.cache = ocache.New() s.cache = ocache.New(s.loadTree)
} s.account = a.MustComponent(account.CName).(account.Service)
// TODO: for test we should load some predefined keys
func (s *service) Name() (name string) {
//TODO implement me
panic("implement me")
}
func (s *service) Run(ctx context.Context) (err error) {
//TODO implement me
panic("implement me")
}
func (s *service) Close(ctx context.Context) (err error) {
return nil return nil
} }
func (s *service) loadTree(ctx context.Context, id string) (value ocache.Object, err error) { func (s *service) Name() (name string) {
return CName
}
func (s *service) Run(ctx context.Context) (err error) {
return nil
}
func (s *service) Close(ctx context.Context) (err error) {
return s.cache.Close()
}
func (s *service) loadTree(ctx context.Context, id string) (ocache.Object, error) {
tree, err := s.treeProvider.TreeStorage(id) tree, err := s.treeProvider.TreeStorage(id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// TODO: should probably accept nil listeners
aclTree, err := acltree.BuildACLTree(tree, s.account.Account(), acltree.NoOpListener{})
return aclTree, err
} }