Thank you, I ended up using this for now. As far as I can tell, HashMap is a far better structure than Trie to store principals as keys.
My worry are bugs and security vulnerabilities since it seems to be a little known repo, but really the natural structure is HashMap so I’ll use this for now.
/// Users
public type UserID = Principal;
public type Username = Text;
public type UserData = {
username : Username;
banned : Bool;
};
The HashMaps
/// We create an array as a stable var, since hashmaps are not stable
private stable var _users : [(UserID, UserData)] = [];
/// Then we create the hashmap and initialize it from the _users array
var users : HashMap.HashMap<UserID, UserData> = HashMap.fromIter(_users.vals(), 0, Principal.equal, Principal.hash);
And to keep them on every update
//State functions
system func preupgrade() {
/// Before an update we put all values from the hashmap to the array, so they can be preserved
_users := Iter.toArray(users.entries());
};
system func postupgrade() {
/// And since the hashmap is initialized with all the values from the stable array, we can reset the array so in the next update values don't duplicate
_users := [];
};
let users = HashMap.HashMap<Principal, User>(0, Principal.equal, Principal.hash);
private stable var _users : [(Principal, User)] = ;
//State functions
system func preupgrade() {
/// Before an update we put all values from the hashmap to the array, so they can be preserved
_users := Iter.toArray(users.entries());
};
system func postupgrade() {
/// And since the hashmap is initialized with all the values from the stable array, we can reset the array so in the next update values don’t duplicate
for ( (p, al ) in _users.vals() ) {
users.put(p, al);
};
_users := ;
};