Refactor commongetter to hide reserved objects
This commit is contained in:
parent
15e950ed44
commit
b0f684769a
@ -2,38 +2,51 @@ package commonspace
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/anytypeio/any-sync/commonspace/object/acl/syncacl"
|
||||
"github.com/anytypeio/any-sync/commonspace/object/syncobjectgetter"
|
||||
"github.com/anytypeio/any-sync/commonspace/object/tree/objecttree"
|
||||
"github.com/anytypeio/any-sync/commonspace/object/treegetter"
|
||||
"github.com/anytypeio/any-sync/commonspace/settings"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
type commonSpaceGetter struct {
|
||||
spaceId string
|
||||
aclList *syncacl.SyncAcl
|
||||
treeGetter treegetter.TreeGetter
|
||||
settings settings.SettingsObject
|
||||
type commonGetter struct {
|
||||
treegetter.TreeGetter
|
||||
spaceId string
|
||||
reservedObjects []syncobjectgetter.SyncObject
|
||||
}
|
||||
|
||||
func newCommonSpaceGetter(spaceId string, aclList *syncacl.SyncAcl, treeGetter treegetter.TreeGetter, settings settings.SettingsObject) syncobjectgetter.SyncObjectGetter {
|
||||
return &commonSpaceGetter{
|
||||
func newCommonGetter(spaceId string, getter treegetter.TreeGetter) *commonGetter {
|
||||
return &commonGetter{
|
||||
TreeGetter: getter,
|
||||
spaceId: spaceId,
|
||||
aclList: aclList,
|
||||
treeGetter: treeGetter,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *commonSpaceGetter) GetObject(ctx context.Context, objectId string) (obj syncobjectgetter.SyncObject, err error) {
|
||||
if c.aclList.Id() == objectId {
|
||||
obj = c.aclList
|
||||
return
|
||||
func (c *commonGetter) AddObject(object syncobjectgetter.SyncObject) {
|
||||
c.reservedObjects = append(c.reservedObjects, object)
|
||||
}
|
||||
|
||||
func (c *commonGetter) GetTree(ctx context.Context, spaceId, treeId string) (objecttree.ObjectTree, error) {
|
||||
if obj := c.getReservedObject(treeId); obj != nil {
|
||||
return obj.(objecttree.ObjectTree), nil
|
||||
}
|
||||
if c.settings.Id() == objectId {
|
||||
obj = c.settings.(syncobjectgetter.SyncObject)
|
||||
return
|
||||
return c.TreeGetter.GetTree(ctx, spaceId, treeId)
|
||||
}
|
||||
|
||||
func (c *commonGetter) getReservedObject(id string) syncobjectgetter.SyncObject {
|
||||
pos := slices.IndexFunc(c.reservedObjects, func(object syncobjectgetter.SyncObject) bool {
|
||||
return object.Id() == id
|
||||
})
|
||||
if pos == -1 {
|
||||
return nil
|
||||
}
|
||||
t, err := c.treeGetter.GetTree(ctx, c.spaceId, objectId)
|
||||
return c.reservedObjects[pos]
|
||||
}
|
||||
|
||||
func (c *commonGetter) GetObject(ctx context.Context, objectId string) (obj syncobjectgetter.SyncObject, err error) {
|
||||
if obj := c.getReservedObject(objectId); obj != nil {
|
||||
return obj, nil
|
||||
}
|
||||
t, err := c.TreeGetter.GetTree(ctx, c.spaceId, objectId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
)
|
||||
|
||||
type SyncObject interface {
|
||||
Id() string
|
||||
synchandler.SyncHandler
|
||||
}
|
||||
|
||||
|
||||
@ -22,7 +22,7 @@ type ObjectSync interface {
|
||||
StreamChecker() StreamChecker
|
||||
ActionQueue() ActionQueue
|
||||
|
||||
Init(getter syncobjectgetter.SyncObjectGetter)
|
||||
Init()
|
||||
Close() (err error)
|
||||
}
|
||||
|
||||
@ -40,7 +40,8 @@ type objectSync struct {
|
||||
|
||||
func NewObjectSync(
|
||||
spaceId string,
|
||||
confConnector confconnector.ConfConnector) (objectSync ObjectSync) {
|
||||
confConnector confconnector.ConfConnector,
|
||||
objectGetter syncobjectgetter.SyncObjectGetter) (objectSync ObjectSync) {
|
||||
streamPool := newStreamPool(func(ctx context.Context, senderId string, message *spacesyncproto.ObjectSyncMessage) (err error) {
|
||||
return objectSync.HandleMessage(ctx, senderId, message)
|
||||
})
|
||||
@ -58,6 +59,7 @@ func NewObjectSync(
|
||||
spaceId,
|
||||
streamPool,
|
||||
checker,
|
||||
objectGetter,
|
||||
syncCtx,
|
||||
cancel)
|
||||
return
|
||||
@ -67,21 +69,22 @@ func newObjectSync(
|
||||
spaceId string,
|
||||
streamPool StreamPool,
|
||||
checker StreamChecker,
|
||||
objectGetter syncobjectgetter.SyncObjectGetter,
|
||||
syncCtx context.Context,
|
||||
cancel context.CancelFunc,
|
||||
) *objectSync {
|
||||
return &objectSync{
|
||||
streamPool: streamPool,
|
||||
spaceId: spaceId,
|
||||
checker: checker,
|
||||
syncCtx: syncCtx,
|
||||
cancelSync: cancel,
|
||||
actionQueue: NewDefaultActionQueue(),
|
||||
objectGetter: objectGetter,
|
||||
streamPool: streamPool,
|
||||
spaceId: spaceId,
|
||||
checker: checker,
|
||||
syncCtx: syncCtx,
|
||||
cancelSync: cancel,
|
||||
actionQueue: NewDefaultActionQueue(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *objectSync) Init(objectGetter syncobjectgetter.SyncObjectGetter) {
|
||||
s.objectGetter = objectGetter
|
||||
func (s *objectSync) Init() {
|
||||
s.actionQueue.Run()
|
||||
go s.checker.CheckResponsiblePeers()
|
||||
}
|
||||
|
||||
@ -14,7 +14,6 @@ import (
|
||||
"github.com/anytypeio/any-sync/commonspace/object/tree/synctree/updatelistener"
|
||||
"github.com/anytypeio/any-sync/commonspace/object/tree/treechangeproto"
|
||||
"github.com/anytypeio/any-sync/commonspace/object/tree/treestorage"
|
||||
"github.com/anytypeio/any-sync/commonspace/object/treegetter"
|
||||
"github.com/anytypeio/any-sync/commonspace/objectsync"
|
||||
"github.com/anytypeio/any-sync/commonspace/settings"
|
||||
"github.com/anytypeio/any-sync/commonspace/settings/deletionstate"
|
||||
@ -107,7 +106,7 @@ type space struct {
|
||||
headSync headsync.HeadSync
|
||||
syncStatus syncstatus.StatusUpdater
|
||||
storage spacestorage.SpaceStorage
|
||||
cache treegetter.TreeGetter
|
||||
cache *commonGetter
|
||||
account accountservice.Service
|
||||
aclList *syncacl.SyncAcl
|
||||
configuration nodeconf.Configuration
|
||||
@ -175,6 +174,7 @@ func (s *space) Init(ctx context.Context) (err error) {
|
||||
return
|
||||
}
|
||||
s.aclList = syncacl.NewSyncAcl(aclList, s.objectSync.StreamPool())
|
||||
s.cache.AddObject(s.aclList)
|
||||
|
||||
deletionState := deletionstate.NewDeletionState(s.storage)
|
||||
deps := settings.Deps{
|
||||
@ -195,9 +195,8 @@ func (s *space) Init(ctx context.Context) (err error) {
|
||||
DeletionState: deletionState,
|
||||
}
|
||||
s.settingsObject = settings.NewSettingsObject(deps, s.id)
|
||||
|
||||
objectGetter := newCommonSpaceGetter(s.id, s.aclList, s.cache, s.settingsObject)
|
||||
s.objectSync.Init(objectGetter)
|
||||
s.cache.AddObject(s.settingsObject)
|
||||
s.objectSync.Init()
|
||||
s.headSync.Init(initialIds, deletionState)
|
||||
err = s.settingsObject.Init(ctx)
|
||||
if err != nil {
|
||||
|
||||
@ -115,7 +115,7 @@ func (s *spaceService) NewSpace(ctx context.Context, id string) (Space, error) {
|
||||
|
||||
lastConfiguration := s.configurationService.GetLast()
|
||||
confConnector := confconnector.NewConfConnector(lastConfiguration, s.pool)
|
||||
|
||||
getter := newCommonGetter(st.Id(), s.treeGetter)
|
||||
syncStatus := syncstatus.NewNoOpSyncStatus()
|
||||
// this will work only for clients, not the best solution, but...
|
||||
if !lastConfiguration.IsResponsible(st.Id()) {
|
||||
@ -123,14 +123,14 @@ func (s *spaceService) NewSpace(ctx context.Context, id string) (Space, error) {
|
||||
syncStatus = syncstatus.NewSyncStatusProvider(st.Id(), syncstatus.DefaultDeps(lastConfiguration, st))
|
||||
}
|
||||
|
||||
headSync := headsync.NewHeadSync(id, s.config.SyncPeriod, st, confConnector, s.treeGetter, syncStatus, log)
|
||||
objectSync := objectsync.NewObjectSync(id, confConnector)
|
||||
headSync := headsync.NewHeadSync(id, s.config.SyncPeriod, st, confConnector, getter, syncStatus, log)
|
||||
objectSync := objectsync.NewObjectSync(id, confConnector, getter)
|
||||
sp := &space{
|
||||
id: id,
|
||||
objectSync: objectSync,
|
||||
headSync: headSync,
|
||||
syncStatus: syncStatus,
|
||||
cache: s.treeGetter,
|
||||
cache: getter,
|
||||
account: s.account,
|
||||
configuration: lastConfiguration,
|
||||
storage: st,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user