1

在这里,我正在尝试创建其结构如下所示的图形数据库。随着我不断添加更多节点,我看不到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}
4

1 回答 1

0

首先,不清楚您要使用txn.stats.

其次,str.encode是“穷人”打包功能,你需要类似 python-lexode.

如果你想从父母到孩子再回来,给定以下树:

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查询具有前缀 (startswith) 的键的范围a.e.

https://stackoverflow.com/a/69317404/140837

真的,.作为分隔符的点是一个黑客,看看python-lexode.

于 2021-10-02T18:10:54.007 回答