Add treecache to node

This commit is contained in:
mcrakhman 2022-09-17 22:18:11 +02:00 committed by Mikhail Iudin
parent 212553d63d
commit f3a2944d60
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
6 changed files with 84 additions and 19 deletions

View File

@ -2,7 +2,7 @@ package cache
import ( import (
"context" "context"
"github.com/anytypeio/go-anytype-infrastructure-experiments/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/synctree"
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree" "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/tree"
) )
@ -17,7 +17,11 @@ type TreeResult struct {
TreeContainer TreeContainer TreeContainer TreeContainer
} }
type BuildFunc = func(ctx context.Context, id string, listener synctree.UpdateListener) (tree.ObjectTree, error)
type TreeCache interface { type TreeCache interface {
app.ComponentRunnable
GetTree(ctx context.Context, id string) (TreeResult, error) GetTree(ctx context.Context, id string) (TreeResult, error)
SetBuildFunc(f BuildFunc)
Close() error
} }

View File

@ -21,22 +21,23 @@ func New() Service {
} }
type Service interface { type Service interface {
CreateSpace(ctx context.Context, id string) (sp Space, err error) CreateSpace(ctx context.Context, id string, deps SpaceDeps) (sp Space, err error)
app.Component app.Component
} }
type service struct { type service struct {
config config.Space config config.Space
configurationService nodeconf.Service configurationService nodeconf.Service
storage storage.Storage }
cache cache.TreeCache
type SpaceDeps struct {
Cache cache.TreeCache
Storage storage.Storage
} }
func (s *service) Init(a *app.App) (err error) { func (s *service) Init(a *app.App) (err error) {
s.config = a.MustComponent(config.CName).(*config.Config).Space s.config = a.MustComponent(config.CName).(*config.Config).Space
s.configurationService = a.MustComponent(nodeconf.CName).(nodeconf.Service) s.configurationService = a.MustComponent(nodeconf.CName).(nodeconf.Service)
s.storage = a.MustComponent(storage.CName).(storage.Storage)
s.cache = a.MustComponent(cache.CName).(cache.TreeCache)
return nil return nil
} }
@ -44,17 +45,17 @@ func (s *service) Name() (name string) {
return CName return CName
} }
func (s *service) CreateSpace(ctx context.Context, id string) (Space, error) { func (s *service) CreateSpace(ctx context.Context, id string, deps SpaceDeps) (Space, error) {
lastConfiguration := s.configurationService.GetLast() lastConfiguration := s.configurationService.GetLast()
diffService := diffservice.NewDiffService(id, s.config.SyncPeriod, s.storage, lastConfiguration, s.cache, log) diffService := diffservice.NewDiffService(id, s.config.SyncPeriod, deps.Storage, lastConfiguration, deps.Cache, log)
syncService := syncservice.NewSyncService(id, diffService, s.cache, lastConfiguration) syncService := syncservice.NewSyncService(id, diffService, deps.Cache, lastConfiguration)
sp := &space{ sp := &space{
id: id, id: id,
conf: s.config, conf: s.config,
syncService: syncService, syncService: syncService,
diffService: diffService, diffService: diffService,
cache: s.cache, cache: deps.Cache,
storage: s.storage, storage: deps.Storage,
} }
if err := sp.Init(ctx); err != nil { if err := sp.Init(ctx); err != nil {
return nil, err return nil, err

View File

@ -50,6 +50,7 @@ func (s *space) Init(ctx context.Context) error {
s.rpc = &rpcHandler{s: s} s.rpc = &rpcHandler{s: s}
s.diffService.Init(s.getObjectIds()) s.diffService.Init(s.getObjectIds())
s.syncService.Init() s.syncService.Init()
s.cache.SetBuildFunc(s.BuildTree)
return nil return nil
} }
@ -125,5 +126,6 @@ func (s *space) getObjectIds() []string {
func (s *space) Close() error { func (s *space) Close() error {
s.diffService.Close() s.diffService.Close()
s.cache.Close()
return s.syncService.Close() return s.syncService.Close()
} }

View File

@ -1,13 +1,11 @@
package storage package storage
import ( import (
"github.com/anytypeio/go-anytype-infrastructure-experiments/app"
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage" "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage"
) )
type Storage interface { type Storage interface {
storage.Provider storage.Provider
app.ComponentRunnable
} }
const CName = "commonspace.storage" const CName = "commonspace.storage"

View File

@ -0,0 +1,54 @@
package nodecache
import (
"context"
"github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger"
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/cache"
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache"
"time"
)
var log = logger.NewNamed("treecache")
type treeCache struct {
gcttl int
cache ocache.OCache
}
func NewNodeCache(ttl int) cache.TreeCache {
return &treeCache{
gcttl: ttl,
}
}
func (c *treeCache) SetBuildFunc(buildFunc cache.BuildFunc) {
c.cache = ocache.New(
func(ctx context.Context, id string) (value ocache.Object, err error) {
return buildFunc(ctx, id, nil)
},
ocache.WithLogger(log.Sugar()),
ocache.WithGCPeriod(time.Minute),
ocache.WithTTL(time.Duration(c.gcttl)*time.Second),
ocache.WithRefCounter(false),
)
}
func (c *treeCache) Close() (err error) {
return c.cache.Close()
}
func (c *treeCache) GetTree(ctx context.Context, id string) (res cache.TreeResult, err error) {
var cacheRes ocache.Object
cacheRes, err = c.cache.Get(ctx, id)
if err != nil {
return cache.TreeResult{}, err
}
res = cache.TreeResult{
Release: func() {
c.cache.Release(id)
},
TreeContainer: cacheRes.(cache.TreeContainer),
}
return
}

View File

@ -8,6 +8,8 @@ import (
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto"
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/server" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/server"
"github.com/anytypeio/go-anytype-infrastructure-experiments/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/config"
"github.com/anytypeio/go-anytype-infrastructure-experiments/node/nodespace/nodecache"
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage"
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache" "github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache"
"time" "time"
) )
@ -27,16 +29,20 @@ type Service interface {
type service struct { type service struct {
conf config.Space conf config.Space
cache ocache.OCache spaceCache ocache.OCache
commonSpace commonspace.Service commonSpace commonspace.Service
} }
func (s *service) Init(a *app.App) (err error) { func (s *service) Init(a *app.App) (err error) {
s.conf = a.MustComponent(config.CName).(*config.Config).Space s.conf = a.MustComponent(config.CName).(*config.Config).Space
s.commonSpace = a.MustComponent(commonspace.CName).(commonspace.Service) s.commonSpace = a.MustComponent(commonspace.CName).(commonspace.Service)
s.cache = ocache.New( s.spaceCache = ocache.New(
func(ctx context.Context, id string) (value ocache.Object, err error) { func(ctx context.Context, id string) (value ocache.Object, err error) {
return s.commonSpace.CreateSpace(ctx, id) deps := commonspace.SpaceDeps{
Cache: nodecache.NewNodeCache(s.conf.GCTTL),
Storage: storage.NewInMemoryTreeStorageProvider(),
}
return s.commonSpace.CreateSpace(ctx, id, deps)
}, },
ocache.WithLogger(log.Sugar()), ocache.WithLogger(log.Sugar()),
ocache.WithGCPeriod(time.Minute), ocache.WithGCPeriod(time.Minute),
@ -59,7 +65,7 @@ func (s *service) Run(ctx context.Context) (err error) {
} }
func (s *service) GetSpace(ctx context.Context, id string) (commonspace.Space, error) { func (s *service) GetSpace(ctx context.Context, id string) (commonspace.Space, error) {
v, err := s.cache.Get(ctx, id) v, err := s.spaceCache.Get(ctx, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -67,5 +73,5 @@ func (s *service) GetSpace(ctx context.Context, id string) (commonspace.Space, e
} }
func (s *service) Close(ctx context.Context) (err error) { func (s *service) Close(ctx context.Context) (err error) {
return s.cache.Close() return s.spaceCache.Close()
} }