I’ve been taking heavy inspiration (borderline plagarism) from CanCan in terms of what data structures to use and how to model my app state with these data structures.
Basically, I’m using two fundamental data types: Maps (1-to-1 mappings) and Rels (many-to-many binary relations).
Maps are used to associate object IDs with object metadata, like user_id => user_profile.
Rels are used to associate object IDs with other object IDs, like user_id <=> video_id (i.e. what videos has this user watched? what users have watched this video?).
Maps can be implemented using
Trie—personally, I’m using
Trie because it’s purely functional and can be stored in stable memory, whereas object-oriented data structures like
HashMap cannot. Rels can similarly be implemented using two Maps.
So far, all of the above has just been data modeling, i.e. what tables and primary/foreign keys to use in SQL. Another important consideration is query performance, what indexes to create for each table. I’m thinking of augmenting each Map/Rel with additional data structures, one for each query I want to optimize. Haven’t decided on what exactly, but the overall setup will make it so that updating a user profile updates not only the user_id => user_profile Map but also a bunch of other “index” data structures, like