1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-07 21:47:02 +09:00

Prevent cases when db returned empty changes

This commit is contained in:
Mikhail Rakhmanov 2025-05-22 14:00:44 +02:00
parent 20f79ee139
commit 4207317899
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
3 changed files with 11 additions and 1 deletions

View file

@ -53,6 +53,9 @@ func (t *Tree) Root() *Change {
}
func (t *Tree) AddFast(changes ...*Change) []*Change {
if len(changes) == 0 {
return nil
}
defer t.clearUnattached()
t.addedBuf = t.addedBuf[:0]
for _, c := range changes {

View file

@ -49,6 +49,10 @@ func TestTree_Add(t *testing.T) {
assert.Equal(t, tr.root.Id, "root")
assert.Equal(t, []string{"root"}, tr.Heads())
})
t.Run("empty tree add should not panic", func(t *testing.T) {
tr := &Tree{}
tr.AddFast()
})
t.Run("linear add", func(t *testing.T) {
tr := new(Tree)
res, _ := tr.Add(

View file

@ -17,7 +17,7 @@ import (
var (
log = logger.NewNamedSugared("common.commonspace.objecttree")
ErrEmpty = errors.New("logs empty")
ErrEmpty = errors.New("database is empty")
)
type treeBuilder struct {
@ -146,6 +146,9 @@ func (tb *treeBuilder) build(opts treeBuilderOpts) (tr *Tree, err error) {
if err != nil {
return nil, fmt.Errorf("failed to get changes after order: %w", err)
}
if len(changes) == 0 {
return nil, ErrEmpty
}
tr = &Tree{}
changes = append(changes, opts.newChanges...)
tr.AddFast(changes...)