Trie within a Trie

I need to create a one to many relationship in stable memory for my motoko based backend canister. Would it be good practice to use a Trie within a Trie (I want to just use Motoko base data structures)?

For example, say we are trying to keep track of all posts associated with a single user. Is it common practice to create a Tree<Text, Trie>. Where we can access all user posts by querying the outer Trie by userId and then can return all elements of the inner Trie to get all posts or perform a search on the inner Trie for a specific post.

Thoughts on this method? Motoko base data structures like Trie should be efficient within a large scale database right?

Yes, in theory that should work. Note that you’ll be limited to 2-3 GB of data this way on 32-bit and (initially) 64-bit (more capacity on 64-bit coming later), but perhaps that’s sufficient for your purposes.

I’d certainly recommend stress testing your solution though (including upgrades).

Perhaps others on the forum have real experience of this approach and could provide advice.

2 Likes

https://mops.one/icrc79-mo

It might be worth taking a look at this repository. In version 0.1.1 We use map of maps to the number of places To keep indexes. We change this to BTrees in v0.2.0 Because we needed the order to be deterministic when we were running queries.

I’m not sure that one is better than another, it just depends on your used case. I pretty much don’t use the base TRIE anywhere as these libraries are more efficient and faster in searching.

One thing to to keep in mind. When you loop through these kinds of collections, make sure you dump them to an array first or you may iterate over any data that you add in your loop causing strange errors.

2 Likes