在这里我试图创建图形数据库,其结构将如下所示。当我继续添加更多的节点时,我看不到depth of the tree increasing
。谁能指出我哪里做错了吗?
A:1
/
B:2 C:3
/
D:4 E:5
>>> import lmdb
>>> env = lmdb.open("treedb.lmdb")
>>> txn = env.begin(write = True)
>>> txn.stat()
{'psize': 4096, 'depth': 0, 'branch_pages': 0, 'leaf_pages': 0, 'overflow_pages': 0, 'entries': 0}
>>> txn.put('root'.encode('utf-8'),json.dumps({'A':1}).encode('utf-8'))
True
>>> txn.stat()
{'psize': 4096, 'depth': 1, 'branch_pages': 0, 'leaf_pages': 1, 'overflow_pages': 0, 'entries': 1}
>>> txn.put('A'.encode('utf-8'), json.dumps({'A':{'B':2}}).encode('utf-8'))
True
>>> txn.stat()
{'psize': 4096, 'depth': 1, 'branch_pages': 0, 'leaf_pages': 1, 'overflow_pages': 0, 'entries': 2}
>>>
>>> txn.put('A'.encode('utf-8'), json.dumps({'A':{'C':3}}).encode('utf-8'))
True
>>>
>>> txn.stat()
{'psize': 4096, 'depth': 1, 'branch_pages': 0, 'leaf_pages': 1, 'overflow_pages': 0, 'entries': 3}
>>>
>>> txn.put('B'.encode('utf-8'), json.dumps({'B':{'D':4}}).encode('utf-8'))
True
>>> txn.put('C'.encode('utf-8'), json.dumps({'C':{'E':5}}).encode('utf-8'))
>>> txn.stat()
{'psize': 4096, 'depth': 1, 'branch_pages': 0, 'leaf_pages': 1, 'overflow_pages': 0, 'entries': 5}
首先,不清楚您试图通过txn.stats
实现什么。
第二,str.encode
是"穷人";打包函数,你需要按字典顺序打包成字节,保持自然的类型顺序(ref: python:fdb.tuples.pack
)
如果你想从父节点到子节点再返回,给出下面的树:
a
|_b
|_c
|_d
|_e
|_f
|_g
|_h
给定以下符号key -> value
,您可以构建以下okvs模式:
a -> properties
a.b -> properties
a.c -> properties
a.d -> properties
a.e -> properties
a.e.f -> properties
a.e.g -> properties
a.e.h -> properties
如果你有节点a.e.g
,你可以通过删除最后一个组件来获取a.e
来检索父节点,然后要查询a.e
的所有子节点,你可以查询前缀(以)a.e.
的键的范围。
见https://stackoverflow.com/a/69317404/140837
真的点.
作为分隔符是一个hack,看看python-lexode
。