Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EPROD-1117 pop_first and pop_last for btreemap #240

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions ic-stable-structures/src/structure/cache/btreemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ where
self.inner.remove(key)
}

fn pop_first(&mut self) -> Option<(K, V)> {
let (k, v) = self.inner.pop_first()?;
self.cache.remove(&k);

Some((k, v))
}

fn pop_last(&mut self) -> Option<(K, V)> {
let (k, v) = self.inner.pop_last()?;
self.cache.remove(&k);

Some((k, v))
}

fn len(&self) -> u64 {
self.inner.len()
}
Expand Down Expand Up @@ -222,6 +236,35 @@ mod tests {
assert_eq!(None, map.get(&2));
}

#[test]
fn test_should_pop_first() {
let mut map = CachedStableBTreeMap::new(VectorMemory::default(), 10);

map.insert(0u32, 42u32);
map.insert(10, 100);

assert_eq!(map.pop_first(), Some((0, 42)));

// try to get
assert!(map.get(&0).is_none());

assert_eq!(map.len(), 1);
}

#[test]
fn test_should_pop_last() {
let mut map = CachedStableBTreeMap::new(VectorMemory::default(), 10);

map.insert(0u32, 42u32);
map.insert(10, 100);

assert_eq!(map.pop_last(), Some((10, 100)));

assert!(map.get(&10).is_none());

assert_eq!(map.len(), 1);
}

#[test]
fn should_replace_old_value() {
let cache_items = 2;
Expand Down
6 changes: 6 additions & 0 deletions ic-stable-structures/src/structure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ pub trait BTreeMapStructure<K, V> {
/// - `key.to_bytes().len() <= K::MAX_SIZE`
fn remove(&mut self, key: &K) -> Option<V>;

/// Removes and returns the first element in the map.
fn pop_first(&mut self) -> Option<(K, V)>;

/// Removes and returns the last element in the map.
fn pop_last(&mut self) -> Option<(K, V)>;

/// True if contains the key.
fn contains_key(&self, key: &K) -> bool;

Expand Down
34 changes: 34 additions & 0 deletions ic-stable-structures/src/structure/stable_storage/btreemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ where
self.0.remove(key)
}

fn pop_first(&mut self) -> Option<(K, V)> {
veeso marked this conversation as resolved.
Show resolved Hide resolved
self.0.pop_first()
}

fn pop_last(&mut self) -> Option<(K, V)> {
veeso marked this conversation as resolved.
Show resolved Hide resolved
self.0.pop_last()
}

fn len(&self) -> u64 {
self.0.len()
}
Expand Down Expand Up @@ -204,6 +212,32 @@ mod tests {
assert_eq!(map.len(), 2);
}

#[test]
fn test_should_pop_first() {
let mut map = StableBTreeMap::new(VectorMemory::default());

map.insert(0u32, 42u32);
map.insert(10, 100);

assert_eq!(map.pop_first(), Some((0, 42)));

assert_eq!(map.len(), 1);
assert!(map.get(&0).is_none());
}

#[test]
fn test_should_pop_last() {
let mut map = StableBTreeMap::new(VectorMemory::default());

map.insert(0u32, 42u32);
map.insert(10, 100);

assert_eq!(map.pop_last(), Some((10, 100)));

assert_eq!(map.len(), 1);
assert!(map.get(&10).is_none());
}

#[test]
fn iter_test() {
let mut map = StableBTreeMap::new(VectorMemory::default());
Expand Down
2 changes: 1 addition & 1 deletion ic-task-scheduler/tests/pocket_ic_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl PocketIcTestContext {
}
}

async fn deploy_dummy_scheduler_canister<'a>() -> anyhow::Result<PocketIcTestContext> {
async fn deploy_dummy_scheduler_canister() -> anyhow::Result<PocketIcTestContext> {
let client = init_pocket_ic().await.build_async().await;
println!("Creating dummy canister");

Expand Down
Loading