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:
parent
20f79ee139
commit
4207317899
3 changed files with 11 additions and 1 deletions
|
@ -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 {
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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...)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue