Change objecttree to set mode Rebuild if we update the snapshot after current heads

This commit is contained in:
mcrakhman 2023-01-14 20:59:54 +01:00 committed by Mikhail Iudin
parent 11d50d087a
commit 7a57d7b0c4
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
2 changed files with 14 additions and 2 deletions

View File

@ -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)

View File

@ -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())