嵌套集模型或其他一些用于表示层次结构的模型?

发布于 2024-10-30 08:33:28 字数 975 浏览 1 评论 0原文

我有一个项目,希望将分层​​信息(特别是类别和子类别)存储在基本上是平面数据库系统(简而言之,它是 XML 记录的集合)中。我想在系统中存储有关类别和子类别的信息:

  • 动物
    • 无脊椎动物
    • 脊椎动物
  • 天气
  • 建筑物
    • 摩天大楼
    • 历史建筑

……等等。

系统中的所有内容,无论好坏,都存储为 XML 记录;这就是存储系统的工作原理。

这意味着系统中的每个类别也存储为 XML 记录,如下所示:

<record id="12345">
   <attribute name="Skyscrapers" />
   <attribute type="Category" />
</record>

所以我想知道如何在这些约束下实现层次结构。

我习惯于关系数据库中的数据存储。在这些情况下,我几乎总是使用嵌套集合模型。在这种情况下,这似乎不是一个好的选择,因为:

  1. 每次插入一个项目时,您 必须更改正确和/或 许多节点的 left 值。 我无法对 XML 进行批量更新 文件,所以我必须更新每个 单独一个。
  2. 虽然有搜索功能可以让我过滤 “小于”或“大于”(所以我 理论上只能拉 相关子节点或父节点 给定类别),我无法订购 按属性的 XML 记录。所以 我必须取回所有 文件,将它们转换成 可排序的对象列表 (在本例中使用Python)然后 使用 lambda 函数对它们进行排序。

由于我的数据存储模型与使用 NoSQL 存储数据没有显着不同,我想知道是否有人使用该存储机制提出了处理和存储分层数据的好技巧。

I have a project where I hope to store hierarchical information (specifically, categories and subcategories) in what is basically a flat database system (in short, it's a collection of XML records). I'd like to store information about categories and subcategories in the system:

  • Animals
    • Invertebrates
    • Vertebrates
  • Weather
  • Buildings
    • Skyscrapers
    • Historic buildings

...and so forth.

Everything in the system, for better or worse, is stored as an XML record; this is just how the storage system works.

Which means that each category in the system is also stored as an XML record, like so:

<record id="12345">
   <attribute name="Skyscrapers" />
   <attribute type="Category" />
</record>

So I am wondering how to implement a hierarchy under these constraints.

I'm used to data storage in a relational database. In those cases I pretty much always use the nested set model. It seems like this wouldn't be a good choice in this case because:

  1. Each time you insert an item, you
    have to change the right and/or
    left values for many of the nodes.
    I can't do a bulk update on the XML
    files, so I'd have to update each
    one individually.
  2. Although there are search functions that let me filter by
    "less-than" or "greater-than" (so I
    could in theory pull only the
    relevant child nodes or parent nodes
    of a given category), I can't order
    the XML records by attributes. So
    I'd have to retrieve all of the
    documents, transform them into a
    list of objects that can be sorted
    (in this case with Python) and then
    sort them using a lambda function.

Since my data storage model isn't significantly different than storing data using NoSQL I was wondering if anyone using that storage mechanism has come up with a good trick for handling and storing hierarchical data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫南 2024-11-06 08:33:28

此类(基于 Peewee ORM)允许您使用平面关系数据库处理分层数据(支持 PostgreSQL、MySQL 和 SQLite):

https://github.com /mathieurodic/peewee-tree/blob/master/node.py

您可以在类方法中进行一些更改,以便这些更改也适用于您正在操作的 XML 文件。

This class (based on the Peewee ORM) allows you to handle hierarchical data with a flat relational database (PostgreSQL, MySQL and SQLite are supported):

https://github.com/mathieurodic/peewee-tree/blob/master/node.py

You can make a few changes in the class methods so the changes are also applied to the XML file you are manipulating.

转瞬即逝 2024-11-06 08:33:28

不确定这对您的用例有多适用,但作为一个想法,也许使用 Beautiful Soup 会有帮助。也许它的默认分层表示足以满足您的需求。

Not sure how applicable this would be for your use case, but as a thought, maybe using Beautiful Soup will help. Perhaps it's default hierarchical representation will be sufficient for your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文