diff --git a/commonspace/object/tree/objecttree/objecttree.go b/commonspace/object/tree/objecttree/objecttree.go index 1809ec58..ac449de5 100644 --- a/commonspace/object/tree/objecttree/objecttree.go +++ b/commonspace/object/tree/objecttree/objecttree.go @@ -185,11 +185,16 @@ func (ot *objectTree) AddContent(ctx context.Context, content SignableChangeCont return } + mode := Append + if content.IsSnapshot { + mode = Rebuild + } + res = AddResult{ OldHeads: oldHeads, Heads: []string{objChange.Id}, Added: []*treechangeproto.RawTreeChangeWithId{rawChange}, - Mode: Append, + Mode: mode, } log.With("treeId", ot.id).With("head", objChange.Id). Debug("finished adding content") @@ -234,6 +239,7 @@ func (ot *objectTree) prepareBuilderContent(content SignableChangeContent) (cnt } func (ot *objectTree) AddRawChanges(ctx context.Context, changesPayload RawChangesPayload) (addResult AddResult, err error) { + lastHeadId := ot.tree.lastIteratedHeadId addResult, err = ot.addRawChanges(ctx, changesPayload) if err != nil { return @@ -242,6 +248,11 @@ func (ot *objectTree) AddRawChanges(ctx context.Context, changesPayload RawChang // reducing tree if we have new roots ot.tree.reduceTree() + // that means that we removed the ids while reducing + if _, exists := ot.tree.attached[lastHeadId]; !exists { + addResult.Mode = Rebuild + } + // adding to database all the added changes only after they are good for _, ch := range addResult.Added { err = ot.treeStorage.AddRawChange(ch) diff --git a/commonspace/object/tree/objecttree/objecttree_test.go b/commonspace/object/tree/objecttree/objecttree_test.go index 13956e2c..03f4322b 100644 --- a/commonspace/object/tree/objecttree/objecttree_test.go +++ b/commonspace/object/tree/objecttree/objecttree_test.go @@ -270,7 +270,8 @@ func TestObjectTree(t *testing.T) { assert.Equal(t, []string{"0"}, res.OldHeads) assert.Equal(t, []string{"4"}, res.Heads) assert.Equal(t, len(rawChanges), len(res.Added)) - assert.Equal(t, Append, res.Mode) + // here we have rebuild, because we reduced tree to new snapshot + assert.Equal(t, Rebuild, res.Mode) // check tree heads assert.Equal(t, []string{"4"}, objTree.Heads())