Skip to content

Commit

Permalink
Merge pull request #460 from CosmWasm/add-remove-to-bucket
Browse files Browse the repository at this point in the history
Add remove to bucket
  • Loading branch information
webmaster128 authored Jul 7, 2020
2 parents 6d9bdc8 + aa93a4d commit 56220ae
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 0.9.3 (unreleased)

- `cosmwasm-storage`: Add `.remove()` method to `Bucket` and `Singleton`

## 0.9.2 (2020-06-29)

- Downgrade wasmer to 0.17.0.
Expand Down
28 changes: 27 additions & 1 deletion packages/storage/src/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cosmwasm_std::{Order, KV};
use crate::length_prefixed::{to_length_prefixed, to_length_prefixed_nested};
#[cfg(feature = "iterator")]
use crate::namespace_helpers::range_with_prefix;
use crate::namespace_helpers::{get_with_prefix, set_with_prefix};
use crate::namespace_helpers::{get_with_prefix, remove_with_prefix, set_with_prefix};
#[cfg(feature = "iterator")]
use crate::type_helpers::deserialize_kv;
use crate::type_helpers::{may_deserialize, must_deserialize};
Expand Down Expand Up @@ -66,6 +66,10 @@ where
Ok(())
}

pub fn remove(&mut self, key: &[u8]) {
remove_with_prefix(self.storage, &self.prefix, key)
}

/// load will return an error if no data is set at the given key, or on parse error
pub fn load(&self, key: &[u8]) -> StdResult<T> {
let value = get_with_prefix(self.storage, &self.prefix, key);
Expand Down Expand Up @@ -194,6 +198,28 @@ mod test {
assert_eq!(data, loaded);
}

#[test]
fn remove_works() {
let mut store = MockStorage::new();
let mut bucket = bucket::<_, Data>(b"data", &mut store);

// save data
let data = Data {
name: "Maria".to_string(),
age: 42,
};
bucket.save(b"maria", &data).unwrap();
assert_eq!(data, bucket.load(b"maria").unwrap());

// deleting random key does nothing
bucket.remove(b"foobar");
assert_eq!(data, bucket.load(b"maria").unwrap());

// deleting maria removes the data
bucket.remove(b"maria");
assert_eq!(None, bucket.may_load(b"maria").unwrap());
}

#[test]
fn readonly_works() {
let mut store = MockStorage::new();
Expand Down
26 changes: 26 additions & 0 deletions packages/storage/src/singleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ where
Ok(())
}

pub fn remove(&mut self) {
self.storage.remove(&self.key)
}

/// load will return an error if no data is set at the given key, or on parse error
pub fn load(&self) -> StdResult<T> {
let value = self.storage.get(&self.key);
Expand Down Expand Up @@ -154,6 +158,28 @@ mod test {
assert_eq!(cfg, single.load().unwrap());
}

#[test]
fn remove_works() {
let mut store = MockStorage::new();
let mut single = Singleton::<_, Config>::new(&mut store, b"config");

// store data
let cfg = Config {
owner: "admin".to_string(),
max_tokens: 1234,
};
single.save(&cfg).unwrap();
assert_eq!(cfg, single.load().unwrap());

// remove it and loads None
single.remove();
assert_eq!(None, single.may_load().unwrap());

// safe to remove 2 times
single.remove();
assert_eq!(None, single.may_load().unwrap());
}

#[test]
fn isolated_reads() {
let mut store = MockStorage::new();
Expand Down

0 comments on commit 56220ae

Please sign in to comment.