在Firebase中查询分层数据结构 [英] Query hierarchical data structure in firebase

查看:96
本文介绍了在Firebase中查询分层数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将数据存储在树状结构中的应用程序,我想继续使用Firebase。



树中的每个节点都是这样的: / b>

 节点:$ b​​ $ b大小://项目数量
数据://数组项目
0://子节点0
1://子节点1
2://子节点2
3://子节点3

所以基本上,它是一个四叉树。问题是如果我加载树的根节点,它将是巨大的,因为firebase将创建一个包含整个树的快照。如果我可以读取树形结构,但是没有数据字段,然后我可以有选择地加载一些节点中的数据字段,这将是一件好事。

目前,似乎没有办法有效地做到这一点。我可以想出的唯一方法是通过节点查询树节点:$ b​​
$ b pre $ read $ node $ size
如果(节点有孩子)
for i = 0:3
read / node / i / size

然而,这种方法会使用太多的往返行程,并且没有办法确定一个节点是否有孩子而没有完全加载孩子(如果我在节点中添加一个hasChild字段,但似乎是多余的)。



我真诚地建议firebase dev团队可以实现一种方法,允许用户过滤返回数据中的字段。新的查询方法将是最好的。

  var query = rootRef.filter(* / size); 
query.on('value',function(snapshot){
//快照只包含'size'字段
});

过滤器参数可以是一个正则表达式,查询将返回任何匹配正则表达式的路径。我认为没有这样的查询方法,使用firebase来存储分层数据结构是非常重要的。当前的查询方法都假定存储在firebase中的数据是相当平坦的,以便高效。

问题:
$ b


  1. 我应该如何使用现有的Firebase基础架构来存储树结构,以便可以有效地查询?我需要能够向下和向上走树并检索与特定节点相关的数据。

  2. Firebase是否适合这种数据结构?什么替代策略更适合?

    $ b $ p感谢

    >解决方案

    我重新组织我的数据结构来分别存储'data'字段,使树结构足够小,完全适合客户端。对于树中的每个节点,我使用通往节点的路径作为存储数据的关键字。例如,

      root:
    2:
    0:
    3:
    size:123
    data:[//物品列表]

    存储为:

      root:
    2:
    0:
    3:
    size: 123
    data:
    203:[//物品清单]

    这样,我可以通过使用该节点的路径作为关键字来有选择地加载每个节点的数据。


    I have an app that stores data in a tree structure, which I'd like to persist to Firebase.

    Each node in the tree are like this:

        node:
            size: // the number of items
            data: // array of items
            0: // child node 0
            1: // child node 1
            2: // child node 2
            3: // child node 3
    

    So basically, its a quad-tree. The problem is if I load the root node of the tree, it will be huge, because firebase will create a snapshot that includes the entire tree. It would be good if I can read the tree structure but without the 'data' field first, and then I can selectively load the 'data' field in some nodes.

    Currently, there doesn't seem to be a way to do this efficiently. The only way I can come up with is to query the tree node by node:

        read /node/size
        if (node has children)
          for i=0:3
             read /node/i/size
    

    However, this approach will use too many round trips, and there's no way to tell whether a node has children without actually loading the children entirely (it can be done if I add a 'hasChild' field in nodes, but it seems redundant).

    I sincerely suggests firebase dev team can implement a method that allow a user to filter fields in the data that returns. A new query method would be the best.

        var query = rootRef.filter("*/size");
        query.on('value', function(snapshot) {
            // snapshot contains only 'size' fields.
        });
    

    The filter argument can be a regex, and the query will return any path that matches the regex. I think without a query method like this, it is important to use firebase to store hierarchical data structure. The current query methods all assume the data stored in firebase a rather 'flat' to be efficient.

    Question:

    1. How should I store the tree structure using existing firebase infrastructure so that it can be queried efficiently? I need the ability to walk the tree downward and upwards and retrieve data associated with specific nodes.

    2. Is firebase suitable for this kind of data structure? What alternative strategy is more suitable?

    Thanks

    解决方案

    I reorganized my data structure to store the 'data' field separately, to make the tree structure small enough to fit entirely on the client side. For each node in the tree, I use the path that leads to the node as the key to store the 'data'. For example,

        root:
          2:
            0:
              3:
                size: 123
                data: [ //list of items ]
    

    is stored as:

        root:
          2:
            0:
              3:
                size: 123
        data:
          203: [ // list of items ]
    

    In this way, I can selectively load each node's data by using the path to that node as the key.

    这篇关于在Firebase中查询分层数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆