Commit abf4ac63 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent bb76489c
...@@ -26,6 +26,7 @@ import ( ...@@ -26,6 +26,7 @@ import (
"lab.nexedi.com/kirr/neo/go/zodb" "lab.nexedi.com/kirr/neo/go/zodb"
) )
const debugPPSet = false
// PPTreeSubSet repesents PP-connected subset of tree node objects. // PPTreeSubSet repesents PP-connected subset of tree node objects.
// //
...@@ -55,66 +56,6 @@ type nodeInTree struct { ...@@ -55,66 +56,6 @@ type nodeInTree struct {
// XXX + [lo,hi) range this node is coming under in its parent // XXX + [lo,hi) range this node is coming under in its parent
} }
// ΔPPTreeSubSet represents a change to PPTreeSubSet.
//
// It can be applied via PPTreeSubSet.ApplyΔ .
//
// The result B of applying δ to A is:
//
// B = A.xDifference(δ.Del).xUnion(δ.Add) (*)
//
// (*) NOTE δ.Del and δ.Add might have their leafs starting from non-leaf nodes in A/B.
// This situation arises when δ represents a change in path to particular
// node, but that node itself does not change, for example:
//
// c* c
// / \ /
// 41* 42 41
// | | | \
// 22 43 46 43
// | | |
// 44 22 44
//
// Here nodes {c, 41} are changed, node 42 is unlinked, and node 46 is added.
// Nodes 43 and 44 stay unchanged.
//
// δ.Del = c-42-43 | c-41-22
// δ.Add = c-41-43 | c-41-46-22
//
// The second component with "-22" builds from leaf, but the first
// component with "-43" builds from non-leaf node.
//
// δnchildNonLeafs = {43: +1}
//
// Only complete result of applying all
//
// - xfixup(-1, δnchildNonLeafs)
// - δ.Del,
// - δ.Add, and
// - xfixup(+1, δnchildNonLeafs)
//
// produce correctly PP-connected set.
type ΔPPTreeSubSet struct {
Del PPTreeSubSet
Add PPTreeSubSet
δnchildNonLeafs map[zodb.Oid]int
}
// Update updates δ to be combination of δ+δ2.
func (δ *ΔPPTreeSubSet) Update(δ2 *ΔPPTreeSubSet) {
δ.Del.UnionInplace(δ2.Del)
δ.Add.UnionInplace(δ2.Add)
for oid, δnc := range δ2.δnchildNonLeafs {
δ.δnchildNonLeafs[oid] += δnc
}
}
// Reverse changes δ=diff(A->B) to δ'=diff(A<-B).
func (δ *ΔPPTreeSubSet) Reverse() {
δ.Del, δ.Add = δ.Add, δ.Del
// δnchildNonLeafs stays the same
}
// verify checks internal consistency of S. // verify checks internal consistency of S.
func (S PPTreeSubSet) verify() { func (S PPTreeSubSet) verify() {
...@@ -316,29 +257,6 @@ func (A PPTreeSubSet) UnionInplace(B PPTreeSubSet) { ...@@ -316,29 +257,6 @@ func (A PPTreeSubSet) UnionInplace(B PPTreeSubSet) {
A.xUnionInplace(B) A.xUnionInplace(B)
} }
// ApplyΔ applies δ to S.
//
// See ΔPPTreeSubSet documentation for details.
func (S PPTreeSubSet) ApplyΔ(δ *ΔPPTreeSubSet) {
if debugPPSet {
fmt.Printf("\n\nApplyΔ\n")
fmt.Printf(" A: %s\n", S)
fmt.Printf(" -: %s\n", δ.Del)
fmt.Printf(" +: %s\n", δ.Add)
fmt.Printf(" x: %v\n", δ.δnchildNonLeafs)
defer fmt.Printf("\n->B: %s\n", S)
}
S.verify()
δ.Del.verify()
δ.Add.verify()
defer S.verify()
S.xfixup(-1, δ.δnchildNonLeafs)
S.xDifferenceInplace(δ.Del)
S.xUnionInplace(δ.Add)
S.xfixup(+1, δ.δnchildNonLeafs)
}
// Path returns path leading to node specified by oid. // Path returns path leading to node specified by oid.
// //
...@@ -461,4 +379,91 @@ func (t nodeInTree) String() string { ...@@ -461,4 +379,91 @@ func (t nodeInTree) String() string {
return fmt.Sprintf("{p%s c%d}", t.parent, t.nchild) return fmt.Sprintf("{p%s c%d}", t.parent, t.nchild)
} }
const debugPPSet = false
// ---- diff/patch ----
// ΔPPTreeSubSet represents a change to PPTreeSubSet.
//
// It can be applied via PPTreeSubSet.ApplyΔ .
//
// The result B of applying δ to A is:
//
// B = A.xDifference(δ.Del).xUnion(δ.Add) (*)
//
// (*) NOTE δ.Del and δ.Add might have their leafs starting from non-leaf nodes in A/B.
// This situation arises when δ represents a change in path to particular
// node, but that node itself does not change, for example:
//
// c* c
// / \ /
// 41* 42 41
// | | | \
// 22 43 46 43
// | | |
// 44 22 44
//
// Here nodes {c, 41} are changed, node 42 is unlinked, and node 46 is added.
// Nodes 43 and 44 stay unchanged.
//
// δ.Del = c-42-43 | c-41-22
// δ.Add = c-41-43 | c-41-46-22
//
// The second component with "-22" builds from leaf, but the first
// component with "-43" builds from non-leaf node.
//
// δnchildNonLeafs = {43: +1}
//
// Only complete result of applying all
//
// - xfixup(-1, δnchildNonLeafs)
// - δ.Del,
// - δ.Add, and
// - xfixup(+1, δnchildNonLeafs)
//
// produce correctly PP-connected set.
type ΔPPTreeSubSet struct {
Del PPTreeSubSet
Add PPTreeSubSet
δnchildNonLeafs map[zodb.Oid]int
}
// Update updates δ to be combination of δ+δ2.
func (δ *ΔPPTreeSubSet) Update(δ2 *ΔPPTreeSubSet) {
δ.Del.UnionInplace(δ2.Del)
δ.Add.UnionInplace(δ2.Add)
for oid, δnc := range δ2.δnchildNonLeafs {
δ.δnchildNonLeafs[oid] += δnc
}
}
// Reverse changes δ=diff(A->B) to δ'=diff(A<-B).
func (δ *ΔPPTreeSubSet) Reverse() {
δ.Del, δ.Add = δ.Add, δ.Del
// δnchildNonLeafs stays the same
}
// ApplyΔ applies δ to S.
//
// See ΔPPTreeSubSet documentation for details.
func (S PPTreeSubSet) ApplyΔ(δ *ΔPPTreeSubSet) {
if debugPPSet {
fmt.Printf("\n\nApplyΔ\n")
fmt.Printf(" A: %s\n", S)
fmt.Printf(" -: %s\n", δ.Del)
fmt.Printf(" +: %s\n", δ.Add)
fmt.Printf(" x: %v\n", δ.δnchildNonLeafs)
defer fmt.Printf("\n->B: %s\n", S)
}
S.verify()
δ.Del.verify()
δ.Add.verify()
defer S.verify()
S.xfixup(-1, δ.δnchildNonLeafs)
S.xDifferenceInplace(δ.Del)
S.xUnionInplace(δ.Add)
S.xfixup(+1, δ.δnchildNonLeafs)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment