Ic stable structure best practices

I store each type of data in a sperate memory to make it easy to migrate, here is an example of how I am using it.

thread_local! {
static CONTRACTS_STORE: RefCell<StableBTreeMap<String, StoredContractVec, Memory>> = RefCell::new(
        StableBTreeMap::init(
            MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(6))),
        )
    );


    static WALLETS_STORE: RefCell<StableBTreeMap<String, Wallet, Memory>> = RefCell::new(
        StableBTreeMap::init(
            MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(8))),
        )
    );
}

But I notice few people use only one static object.

struct AllMyData {
CONTRACTS_STORE:...
WALLETS_STORE: ...
}
static STORAGE: RefCell<StableBTreeMap<String, AllMyData, Memory>> = RefCell::new(
        StableBTreeMap::init(
            MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(0))),
        )
    );

What is the advantages of this practice ?

3 Likes

I think this depends on your use case really, I am not sure we can say there’s definitely one way that’s better than the other.

For example, the argument you mention about being able to migrate data is a good one. This way you can do changes to one of your data instead of needing to update all of it. Of course, this works well as long as your data is independent from each other. If there’s tighter coupling, then it might be best to combine in a single data structure, otherwise you might need to ensure you make consistent updates across multiple data structures.

3 Likes