mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-08 14:07:02 +09:00
Clear unattached changes
This commit is contained in:
parent
d24f229f67
commit
e70272d79f
2 changed files with 9 additions and 29 deletions
|
@ -515,44 +515,18 @@ func (ot *objectTree) createAddResult(oldHeads []string, mode Mode, treeChangesA
|
||||||
// returns changes that we added to the tree as attached this round
|
// returns changes that we added to the tree as attached this round
|
||||||
// they can include not only the changes that were added now,
|
// they can include not only the changes that were added now,
|
||||||
// but also the changes that were previously in the tree
|
// but also the changes that were previously in the tree
|
||||||
getAddedChanges := func(toConvert []*Change) (added []*treechangeproto.RawTreeChangeWithId, err error) {
|
getAddedChanges := func() (added []*treechangeproto.RawTreeChangeWithId, err error) {
|
||||||
alreadyConverted := make(map[*Change]struct{})
|
|
||||||
|
|
||||||
// first we see if we have already unmarshalled those changes
|
|
||||||
for _, idx := range ot.notSeenIdxBuf {
|
for _, idx := range ot.notSeenIdxBuf {
|
||||||
rawChange := rawChanges[idx]
|
rawChange := rawChanges[idx]
|
||||||
if ch, exists := ot.tree.attached[rawChange.Id]; exists {
|
if _, exists := ot.tree.attached[rawChange.Id]; exists {
|
||||||
if len(toConvert) != 0 {
|
|
||||||
alreadyConverted[ch] = struct{}{}
|
|
||||||
}
|
|
||||||
added = append(added, rawChange)
|
added = append(added, rawChange)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// this will happen in case we called rebuild from storage
|
|
||||||
// or if all the changes that we added were contained in current add request
|
|
||||||
// (this what would happen in most cases)
|
|
||||||
if len(toConvert) == 0 || len(added) == len(toConvert) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// but in some cases it may happen that the changes that were added this round
|
|
||||||
// were contained in unattached from previous requests
|
|
||||||
for _, ch := range toConvert {
|
|
||||||
// if we got some changes that we need to convert to raw
|
|
||||||
if _, exists := alreadyConverted[ch]; !exists {
|
|
||||||
var raw *treechangeproto.RawTreeChangeWithId
|
|
||||||
raw, err = ot.changeBuilder.Marshall(ch)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
added = append(added, raw)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var added []*treechangeproto.RawTreeChangeWithId
|
var added []*treechangeproto.RawTreeChangeWithId
|
||||||
added, err = getAddedChanges(treeChangesAdded)
|
added, err = getAddedChanges()
|
||||||
if !ot.treeBuilder.keepInMemoryData {
|
if !ot.treeBuilder.keepInMemoryData {
|
||||||
for _, ch := range treeChangesAdded {
|
for _, ch := range treeChangesAdded {
|
||||||
ch.Data = nil
|
ch.Data = nil
|
||||||
|
|
|
@ -46,6 +46,7 @@ func (t *Tree) Root() *Change {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tree) AddFast(changes ...*Change) []*Change {
|
func (t *Tree) AddFast(changes ...*Change) []*Change {
|
||||||
|
defer t.clearUnattached()
|
||||||
t.addedBuf = t.addedBuf[:0]
|
t.addedBuf = t.addedBuf[:0]
|
||||||
for _, c := range changes {
|
for _, c := range changes {
|
||||||
// ignore existing
|
// ignore existing
|
||||||
|
@ -85,7 +86,12 @@ func (t *Tree) AddMergedHead(c *Change) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Tree) clearUnattached() {
|
||||||
|
t.unAttached = make(map[string]*Change)
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Tree) Add(changes ...*Change) (mode Mode, added []*Change) {
|
func (t *Tree) Add(changes ...*Change) (mode Mode, added []*Change) {
|
||||||
|
defer t.clearUnattached()
|
||||||
t.addedBuf = t.addedBuf[:0]
|
t.addedBuf = t.addedBuf[:0]
|
||||||
var (
|
var (
|
||||||
// this is previous head id which should have been iterated last
|
// this is previous head id which should have been iterated last
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue