1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00

Change interfaces

This commit is contained in:
mcrakhman 2024-12-16 20:54:53 +01:00
parent cb9438f72d
commit 0a650f202f
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
3 changed files with 26 additions and 17 deletions

View file

@ -1,6 +1,10 @@
package objecttree
import "github.com/anyproto/any-sync/util/slice"
import (
"go.uber.org/zap"
"github.com/anyproto/any-sync/util/slice"
)
type (
hasChangesFunc func(ids ...string) bool
@ -15,23 +19,26 @@ type ChangeDiffer struct {
visitedBuf []*Change
}
func NewChangeDiffer(tree *Tree, hasChanges hasChangesFunc) *ChangeDiffer {
func NewChangeDiffer(tree ReadableObjectTree, hasChanges hasChangesFunc) (*ChangeDiffer, error) {
diff := &ChangeDiffer{
hasChanges: hasChanges,
attached: make(map[string]*Change),
waitList: make(map[string][]*Change),
}
if tree == nil {
return diff
return diff, nil
}
tree.IterateSkip(tree.RootId(), func(c *Change) (isContinue bool) {
err := tree.IterateRoot(nil, func(c *Change) (isContinue bool) {
diff.add(&Change{
Id: c.Id,
PreviousIds: c.PreviousIds,
})
return true
})
return diff
if err != nil {
return nil, err
}
return diff, nil
}
func (d *ChangeDiffer) RemoveBefore(ids []string) (removed []string, notFound []string) {
@ -140,7 +147,10 @@ func NewDiffManager(initHeads, curHeads []string, treeBuilder treeBuilderFunc, o
if err != nil {
return nil, err
}
differ := NewChangeDiffer(readableTree.Tree(), readableTree.HasChanges)
differ, err := NewChangeDiffer(readableTree, readableTree.HasChanges)
if err != nil {
return nil, err
}
return &DiffManager{
differ: differ,
heads: curHeads,
@ -173,7 +183,7 @@ func (d *DiffManager) Update(objTree ObjectTree) {
toAdd = make([]*Change, 0, objTree.Len())
toRemove []string
)
objTree.Tree().iterate(objTree.Root(), func(ch *Change) bool {
err := objTree.IterateRoot(nil, func(ch *Change) bool {
if ch.IsNew {
toAdd = append(toAdd, &Change{
Id: ch.Id,
@ -186,6 +196,10 @@ func (d *DiffManager) Update(objTree ObjectTree) {
}
return true
})
if err != nil {
log.Warn("error while iterating over object tree", zap.Error(err))
return
}
d.differ.Add(toAdd...)
d.heads = make([]string, 0, len(d.heads))
d.heads = append(d.heads, objTree.Heads()...)

View file

@ -19,7 +19,7 @@ func TestChangeDiffer_Add(t *testing.T) {
newChange("6", "0", "5"),
newChange("7", "0", "3", "6"),
}
differ := NewChangeDiffer(nil, func(ids ...string) bool {
differ, _ := NewChangeDiffer(nil, func(ids ...string) bool {
return false
})
differ.Add(changes...)
@ -38,7 +38,7 @@ func TestChangeDiffer_Add(t *testing.T) {
newChange("6", "0", "5"),
newChange("7", "0", "3", "6"),
}
differ := NewChangeDiffer(nil, func(ids ...string) bool {
differ, _ := NewChangeDiffer(nil, func(ids ...string) bool {
return false
})
differ.Add(changes...)
@ -56,7 +56,7 @@ func TestChangeDiffer_Add(t *testing.T) {
newChange("2", "0", "0"),
newChange("3", "0", "1", "2"),
}
differ := NewChangeDiffer(nil, func(ids ...string) bool {
differ, _ := NewChangeDiffer(nil, func(ids ...string) bool {
return false
})
differ.Add(changes...)
@ -75,7 +75,7 @@ func TestChangeDiffer_Add(t *testing.T) {
require.Equal(t, len(changes), len(res))
})
t.Run("remove not found", func(t *testing.T) {
differ := NewChangeDiffer(nil, func(ids ...string) bool {
differ, _ := NewChangeDiffer(nil, func(ids ...string) bool {
return false
})
_, notFound := differ.RemoveBefore([]string{"3", "4", "5"})
@ -83,7 +83,7 @@ func TestChangeDiffer_Add(t *testing.T) {
})
t.Run("exists in storage", func(t *testing.T) {
storedIds := []string{"3", "4", "5"}
differ := NewChangeDiffer(nil, func(ids ...string) bool {
differ, _ := NewChangeDiffer(nil, func(ids ...string) bool {
for _, id := range ids {
if !slices.Contains(storedIds, id) {
return false

View file

@ -68,7 +68,6 @@ type ReadableObjectTree interface {
Root() *Change
Len() int
IsDerived() bool
Tree() *Tree
AclList() list.AclList
@ -174,10 +173,6 @@ func (ot *objectTree) Len() int {
return ot.tree.Len()
}
func (ot *objectTree) Tree() *Tree {
return ot.tree
}
func (ot *objectTree) AclList() list.AclList {
return ot.aclList
}