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 (
"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"
)
@ -17,7 +17,11 @@ type TreeResult struct {
TreeContainer TreeContainer
}
type BuildFunc = func(ctx context.Context, id string, listener synctree.UpdateListener) (tree.ObjectTree, error)
type TreeCache interface {
app.ComponentRunnable
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 {
CreateSpace(ctx context.Context, id string) (sp Space, err error)
CreateSpace(ctx context.Context, id string, deps SpaceDeps) (sp Space, err error)
app.Component
}
type service struct {
config config.Space
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) {
s.config = a.MustComponent(config.CName).(*config.Config).Space
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
}
@ -44,17 +45,17 @@ func (s *service) Name() (name string) {
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()
diffService := diffservice.NewDiffService(id, s.config.SyncPeriod, s.storage, lastConfiguration, s.cache, log)
syncService := syncservice.NewSyncService(id, diffService, s.cache, lastConfiguration)
diffService := diffservice.NewDiffService(id, s.config.SyncPeriod, deps.Storage, lastConfiguration, deps.Cache, log)
syncService := syncservice.NewSyncService(id, diffService, deps.Cache, lastConfiguration)
sp := &space{
id: id,
conf: s.config,
syncService: syncService,
diffService: diffService,
cache: s.cache,
storage: s.storage,
cache: deps.Cache,
storage: deps.Storage,
}
if err := sp.Init(ctx); err != nil {
return nil, err

View File

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

View File

@ -1,13 +1,11 @@
package storage
import (
"github.com/anytypeio/go-anytype-infrastructure-experiments/app"
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage"
)
type Storage interface {
storage.Provider
app.ComponentRunnable
}
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/net/rpc/server"
"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"
"time"
)
@ -27,16 +29,20 @@ type Service interface {
type service struct {
conf config.Space
cache ocache.OCache
spaceCache ocache.OCache
commonSpace commonspace.Service
}
func (s *service) Init(a *app.App) (err error) {
s.conf = a.MustComponent(config.CName).(*config.Config).Space
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) {
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.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) {
v, err := s.cache.Get(ctx, id)
v, err := s.spaceCache.Get(ctx, id)
if err != nil {
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) {
return s.cache.Close()
return s.spaceCache.Close()
}