Best way to query user data in stable memory

What Motoko based data structure are people using to query user data by Principal ID in stable memory? I was thinking about using HashMap or RBTree, but apparently I am not allowed to use those structures for stable memory - what is the reason for that too? Anyway, if anyone has an efficient way to query user data by Principal ID please let me know, and if that is not the best method please let me know what you are using instead. Thanks

1 Like

Have you looked into stable-trie by @timo?

There are other packages as well, maybe Motoko-hashtable is a good fit as well?

What exactly is your user data? To answer the question it is important to know whether it is constant size (same size for each user) or variable size (size differs from user to user). And also if user data can be overwritten later in a way that the size changes.

It is not clear to me from your question if you want a data structure that lives in stable memory (in Regions) or if you want one that lives in heap and gets serialized to stable memory during upgrades.

For the first kind (Regions) there are only two that I know of:

For the second kind there are many options. If you want that kind please respond and I can list more. For this kind you can also use standard RBTree that you mentioned. It has share/unshare functions that you can put in preupgrade/postupgrade.

1 Like