Commit 13f4b6df authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 126bbb7a
......@@ -164,21 +164,31 @@ class Tree(object):
class Bucket(object):
# .keyv () of keys
# .valuev None | () of values len(.valuev) == len(.keyv)
def __init__(b, *keyv): # XXX +values
def __init__(b, keyv, valuev): # XXX syntax sugar for values? (lack of values)?
_assertIncv(keyv)
b.keyv = tuple(keyv)
if valuev is None:
b.valuev = None
else:
assert len(valuev) == len(keyv)
b.valuev = tuple(valuev)
def __hash__(b):
return hash(b.keyv) # XXX +values
return hash(b.keyv) ^ hash(b.valuev)
def __ne__(a, b):
return not (a == b)
def __eq__(a, b):
if not isinstance(b, Bucket):
return False
return a.keyv == b.keyv
return (a.keyv == b.keyv and a.valuev == b.valuev)
def __str__(b): # XXX +values
return "B(" + ','.join(['%s' % _ for _ in b.keyv]) + ")"
def __str__(b):
if b.values is None:
kvv = ['%s' % k for k in b.keyv]
else:
assert len(b.keyv) == len(b.valuev)
kvv = ['%s:%s' % (k,v) for (k,v) in zip(b.keyv, b.valuev)]
return "B" + ','.join(kvv)
__repr__ = __str__
def copy(b):
......@@ -194,14 +204,14 @@ def StructureOf(znode):
if ztype.is_zbucket:
keys, values = zbcheck.crack_bucket(znode, ztype.is_map)
if not ztype.is_map:
return Bucket(*keys)
return Bucket(keys, None)
else:
return Bucket(keys, values) # XXX
return Bucket(keys, values)
if ztype.is_ztree:
kind, keys, children = zbcheck.crack_btree(znode, ztype.is_map)
if kind == zbcheck.BTREE_EMPTY:
return Tree([], Bucket()) # XXX values !None for map
return Tree([], Bucket([], [] if ztype.is_map else None))
if kind == zbcheck.BTREE_ONE:
b = znode._bucket_type()
......@@ -802,22 +812,23 @@ def TopoDecode(text, vdecode=int):
raise TopoDecodeError("incorrect node %s: unknown prefix" % qq(tnode))
tkeys = tnode[1:] # e.g. '7,8,9' or '1:a,3:def' or ''
if tkeys == '':
keyv = []
tkeyv = []
else:
withV = (typ is Bucket and ':' in tkeys)
tkeyv = tkeys.split(',') # e.g. 7 8 9
keyv = []
valuev= [] if withV else None
for tkey in tkeyv:
ktxt = tkey
if withV:
ktxt, vtxt = tkey.split(':')
v = vdecode(vtxt)
valuev.append(v)
k = int(ktxt)
keyv.append(k)
withV = (typ is Bucket and ':' in tkeys)
keyv = []
valuev= [] if withV else None
for tkey in tkeyv:
ktxt = tkey
if withV:
ktxt, vtxt = tkey.split(':')
v = vdecode(vtxt)
valuev.append(v)
k = int(ktxt)
keyv.append(k)
if typ is Bucket:
node = Bucket(*keyv) # XXX valuev
node = Bucket(keyv, valuev)
else:
# Tree
nchild = len(keyv) + 1
......
......@@ -34,6 +34,11 @@ import transaction
from pytest import raises
inf = float('inf')
# T/B are shorthands for Tree and Bucket without values.
# Bv is shorthand for Bucket with values.
T = xbtree.Tree
B = lambda *keyv: xbtree.Bucket(keyv, None)
Bv = lambda keyv, *valuev: xbtree.Bucket(keyv, valuev)
# build ztree with known degenerate topology, see:
# https://github.com/zopefoundation/ZODB/commit/6cd24e99f89b
......@@ -42,8 +47,6 @@ def buildDegenerateZTree():
ztree, keys = testBTrees.DegenerateBTree("testBasicOps")._build_degenerate_tree()
assert keys == [1, 3, 5, 7, 11]
T = xbtree.Tree
B = xbtree.Bucket
assert xbtree.StructureOf(ztree) == T([4],
T([2],
T([], B(1)), T([], B(3))),
......@@ -56,17 +59,14 @@ def buildDegenerateZTree():
def test_structureOf():
T = xbtree.Tree
B = xbtree.Bucket
# empty tree
t = LOBTree()
assert xbtree.StructureOf(t) == T([], B())
assert xbtree.StructureOf(t) == T([], Bv([]))
# tree with 1 k->v
t = LOBTree()
t[10] = 'hello'
assert xbtree.StructureOf(t) == T([], B(10))
assert xbtree.StructureOf(t) == T([], Bv([10], 'hello'))
# known degenerate topology
t = buildDegenerateZTree()
......@@ -80,8 +80,6 @@ def test_structureOf():
T([], B(11))) ))
def test_topoEncoding():
T = xbtree.Tree
B = xbtree.Bucket
def X(tree):
topo = xbtree.TopoEncode(tree)
t2 = xbtree.TopoDecode(topo)
......@@ -115,8 +113,6 @@ def test_topoEncoding():
def test_allStructs():
T = xbtree.Tree
B = xbtree.Bucket
# X = AllStructs(..., allowEmptyBuckets=True)
# Y = AllStructs(..., allowEmptyBuckets=False)
# XY = X = Y + assert X == Y
......@@ -707,8 +703,6 @@ def test_restructure():
def test_walkBFS():
T = xbtree.Tree
B = xbtree.Bucket
R = xbtree._Range
# XXX ø
......
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