Few tree fixes
This commit is contained in:
parent
fb64d4d840
commit
a17b102756
@ -238,7 +238,10 @@ func (d *docTree) AddContent(ctx context.Context, aclList list.ACLList, content
|
|||||||
// clearing tree, because we already fixed everything in the last snapshot
|
// clearing tree, because we already fixed everything in the last snapshot
|
||||||
d.tree = &Tree{}
|
d.tree = &Tree{}
|
||||||
}
|
}
|
||||||
d.tree.AddFast(ch) // TODO: Add head
|
err = d.tree.AddMergedHead(ch)
|
||||||
|
if err != nil {
|
||||||
|
panic("error in adding head")
|
||||||
|
}
|
||||||
rawCh := &aclpb.RawChange{
|
rawCh := &aclpb.RawChange{
|
||||||
Payload: fullMarshalledChange,
|
Payload: fullMarshalledChange,
|
||||||
Signature: ch.Signature(),
|
Signature: ch.Signature(),
|
||||||
|
|||||||
@ -56,6 +56,31 @@ func (t *Tree) AddFast(changes ...*Change) {
|
|||||||
t.updateHeads()
|
t.updateHeads()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Tree) AddMergedHead(c *Change) error {
|
||||||
|
// check that it was not inserted previously
|
||||||
|
if _, ok := t.attached[c.Id]; ok {
|
||||||
|
return fmt.Errorf("change already exists")
|
||||||
|
} else if _, ok := t.unAttached[c.Id]; ok {
|
||||||
|
return fmt.Errorf("change already exists")
|
||||||
|
}
|
||||||
|
t.add(c)
|
||||||
|
|
||||||
|
// check that it was attached after adding
|
||||||
|
if _, ok := t.attached[c.Id]; !ok {
|
||||||
|
return fmt.Errorf("change is not attached")
|
||||||
|
}
|
||||||
|
|
||||||
|
// check that previous heads have new change as next
|
||||||
|
for _, prevHead := range t.headIds {
|
||||||
|
head := t.attached[prevHead]
|
||||||
|
if len(head.Next) != 1 || head.Next[0].Id != c.Id {
|
||||||
|
return fmt.Errorf("this is not a new head")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.headIds = []string{c.Id}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Tree) Add(changes ...*Change) (mode Mode) {
|
func (t *Tree) Add(changes ...*Change) (mode Mode) {
|
||||||
var beforeHeadIds = t.headIds
|
var beforeHeadIds = t.headIds
|
||||||
var attached bool
|
var attached bool
|
||||||
@ -263,7 +288,7 @@ func (t *Tree) dfs(startChange string) (uniqMap map[string]*Change) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tree) updateHeads() {
|
func (t *Tree) updateHeads() {
|
||||||
var newHeadIds, newMetaHeadIds []string
|
var newHeadIds []string
|
||||||
t.iterate(t.root, func(c *Change) (isContinue bool) {
|
t.iterate(t.root, func(c *Change) (isContinue bool) {
|
||||||
if len(c.Next) == 0 {
|
if len(c.Next) == 0 {
|
||||||
newHeadIds = append(newHeadIds, c.Id)
|
newHeadIds = append(newHeadIds, c.Id)
|
||||||
@ -271,9 +296,7 @@ func (t *Tree) updateHeads() {
|
|||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
t.headIds = newHeadIds
|
t.headIds = newHeadIds
|
||||||
t.metaHeadIds = newMetaHeadIds
|
|
||||||
sort.Strings(t.headIds)
|
sort.Strings(t.headIds)
|
||||||
sort.Strings(t.metaHeadIds)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tree) iterate(start *Change, f func(c *Change) (isContinue bool)) {
|
func (t *Tree) iterate(start *Change, f func(c *Change) (isContinue bool)) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user