Commit 14ea63ba authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent b4b07b83
......@@ -90,6 +90,7 @@ class Tree(object):
# .keyv () of keys
# .children () of children len(.children) == len(.keyv) + 1
def __init__(t, keyv, *children):
keyv = tuple(keyv)
assert len(children) == len(keyv) + 1, (keyv, children)
_assertIncv(keyv)
if len(children) > 0:
......@@ -103,13 +104,13 @@ class Tree(object):
assert childtype in (Tree, Bucket), childtype
# assert children keys are consistent
v = [-inf] + keyv + [+inf]
v = (-inf,) + keyv + (+inf,)
for i, (klo, khi) in enumerate(zip(v[:-1], v[1:])): # (klo, khi) = [] of (k_i, k_{i+1})
for k in children[i].keyv:
if not (klo <= k < khi):
panic("children[%d] key %d is outside key range [%s, %s)" % (i, k, klo, khi))
t.keyv = tuple(keyv)
t.keyv = keyv
t.children = tuple(children)
def __hash__(t):
......@@ -131,6 +132,10 @@ class Tree(object):
__repr__ = __str__
def copy(t):
return Tree(t.keyv, *[_.copy() for _ in t.children])
# Bucket represents a bucket node.
class Bucket(object):
# .keyv () of keys
......@@ -151,6 +156,9 @@ class Bucket(object):
return "B(" + ','.join(['%s' % _ for _ in b.keyv]) + ")"
__repr__ = __str__
def copy(b):
return Bucket(*b.keyv)
# StructureOf returns internal structure of a BTree.
#
......@@ -214,6 +222,12 @@ def Restructure(ztree, newStructure):
# D(A,B) = ────────────── + ──────────────
# N(A) N(B)
#
tnew = newStructure.copy()
zlevelv = _zwalkBFS(ztree)
levelv = _walkBFS(tnew)
_bcheck(ztree) # verify ztree after our tweaks
tstruct = StructureOf(ztree)
......@@ -360,6 +374,37 @@ def _walkBFS(tree): # i[] of [](of nodes on each level)
if isinstance(node, Tree):
nextq += node.children
# _zwalkBFS, similarly to _walkBFS, walks ZODB BTree in breadth-first order layer by layer.
def _zwalkBFS(ztree): # i[] of [](of nodes on each level)
# assert istree(ztree)
currentq = []
nextq = [ztree]
while len(nextq) > 0:
yield tuple(nextq)
currentq = nextq
nextq = []
while len(currentq) > 0:
znode = currentq.pop(0)
assert istree(znode) or isbucket(znode)
if istree(znode):
kind, keys, kids = zbcheck.crack_btree(znode)
if kind == zbcheck.BTREE_EMPTY:
b = znode._bucket_type()
children = [b]
elif kind == zbcheck.BTREE_ONE:
b = znode._bucket_type()
b.__setstate__(keys)
children = [b]
elif kind == zbcheck.BTREE_NORMAL:
children = kids
else:
panic("bad tree kind %r" % kind)
nextq += children
# TopoDecode decodes topology-encoded text into Tree structure.
......
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