mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-08 05:57:03 +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 {
|
func (t *Tree) AddFast(changes ...*Change) []*Change {
|
||||||
|
if len(changes) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
defer t.clearUnattached()
|
defer t.clearUnattached()
|
||||||
t.addedBuf = t.addedBuf[:0]
|
t.addedBuf = t.addedBuf[:0]
|
||||||
for _, c := range changes {
|
for _, c := range changes {
|
||||||
|
|
|
@ -49,6 +49,10 @@ func TestTree_Add(t *testing.T) {
|
||||||
assert.Equal(t, tr.root.Id, "root")
|
assert.Equal(t, tr.root.Id, "root")
|
||||||
assert.Equal(t, []string{"root"}, tr.Heads())
|
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) {
|
t.Run("linear add", func(t *testing.T) {
|
||||||
tr := new(Tree)
|
tr := new(Tree)
|
||||||
res, _ := tr.Add(
|
res, _ := tr.Add(
|
||||||
|
|
|
@ -17,7 +17,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
log = logger.NewNamedSugared("common.commonspace.objecttree")
|
log = logger.NewNamedSugared("common.commonspace.objecttree")
|
||||||
ErrEmpty = errors.New("logs empty")
|
ErrEmpty = errors.New("database is empty")
|
||||||
)
|
)
|
||||||
|
|
||||||
type treeBuilder struct {
|
type treeBuilder struct {
|
||||||
|
@ -146,6 +146,9 @@ func (tb *treeBuilder) build(opts treeBuilderOpts) (tr *Tree, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get changes after order: %w", err)
|
return nil, fmt.Errorf("failed to get changes after order: %w", err)
|
||||||
}
|
}
|
||||||
|
if len(changes) == 0 {
|
||||||
|
return nil, ErrEmpty
|
||||||
|
}
|
||||||
tr = &Tree{}
|
tr = &Tree{}
|
||||||
changes = append(changes, opts.newChanges...)
|
changes = append(changes, opts.newChanges...)
|
||||||
tr.AddFast(changes...)
|
tr.AddFast(changes...)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue