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