【问题标题】:how to form tree database structure using lmdb如何使用 lmdb 形成树形数据库结构
【发布时间】:2021-08-20 01:05:29
【问题描述】:

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

【问题讨论】:

  • 我不断添加更多孩子,但树仍然保持平坦。几乎它只是一个键:值数据库而不是图形。我一定遗漏了一些我不明白的东西。
  • 只需要遍历查询吗?或者你还需要按属性查询?
  • 只遍历查询

标签: python graph lmdb okvs


【解决方案1】:

首先不清楚你想用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的所有子节点,你查询具有前缀的键的范围(开头)a.e..

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多