Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query hierarchical data structure in firebase

Tags:

firebase

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

like image 871
Chaoran Avatar asked Oct 11 '25 18:10

Chaoran


1 Answers

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.

like image 60
Chaoran Avatar answered Oct 14 '25 07:10

Chaoran