CosmWasm / cw-storage-plus

Storage abstractions for CosmWasm smart contracts
Apache License 2.0
36 stars 27 forks source link

Add soft delete to Deque #57

Open Grinion opened 1 year ago

Grinion commented 1 year ago

Problem

We have a lot of records in ### Deque. We want to clear all records from Deque In our cases we don't need to clear the data on the blockchain, we can overwrite it

Solution

I suggest adding a method clear for soft delete that sets Tail to 0.

OR

Change visibility methods from private to public

tail
head
set_tail
set_head

Temporary Solution

Now, I need to do it manually through the code

let full_key = namespaces_with_key(&["players".as_bytes()], b"t");
ctx.deps.storage.set(&full_key, &(0 as u32).to_be_bytes());
chipshort commented 1 year ago

Be careful with just setting the tail to 0! When popping items from the front, the head gets incremented and when pushing to the back, the tail incremented. This is done in a wrapping way, so if the tail ever passes u32::MAX, it will start again at 0. Because of that, the invariant for an empty Deque is not tail == 0, it is head == tail (if you never pop from / push to the front, those conditions are the same).

For that reason, I don't want to expose the setter functions (way too easy to mess up), but a clear function seems like something we should have