Skip to content

Commit 2916687

Browse files
committed
Release 0.3.0
- Add new methods to EventQueue - Update depedencies
1 parent 5a5ef6d commit 2916687

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "spnr-lib"
3-
version = "0.2.3"
3+
version = "0.3.0"
44
edition = "2021"
55
authors = [ "spnr@spnr.app" ]
66
description = "Rust library for building smart contracts on the Internet Computer, by the Spinner.Cash team."
@@ -13,8 +13,8 @@ keywords = ["internet-computer", "types", "canister", "spnr", "spinner"]
1313
include = ["src", "Cargo.toml", "LICENSE", "README.md"]
1414

1515
[dependencies]
16-
candid = "0.7"
17-
ic-cdk = "0.5"
16+
candid = "0.10"
17+
ic-cdk = "0.17"
1818
serde = "1.0"
1919
serde_derive = "1.0"
2020

src/event_queue.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// An event queue ordered by expiry time.
22
///
33
/// Each event has an id and status, and they can be inserted/looked up/purged from the queue.
4-
use ic_cdk::export::candid::CandidType;
4+
use candid::CandidType;
55
use serde::{Deserialize, Serialize};
66
use std::collections::{btree_map::Entry, BTreeMap};
77
use std::{error, fmt};
@@ -126,7 +126,7 @@ impl<I: Ord, T> EventQueue<I, T> {
126126
}
127127

128128
/// Remove all events with an expiry less than the given expiry.
129-
/// This operation is `O(log(n))`.
129+
/// This operation is `O(log(n))+O(m)`, where `m` is the number of events removed.
130130
pub fn purge_expired(&mut self, expiry: u64)
131131
where
132132
I: Default,
@@ -135,7 +135,11 @@ impl<I: Ord, T> EventQueue<I, T> {
135135
expiry,
136136
nonce: I::default(),
137137
};
138-
self.events = self.events.split_off(&key);
138+
let to_keep = self.events.split_off(&key);
139+
for to_remove in self.events.keys() {
140+
self.index.remove(&to_remove.nonce);
141+
}
142+
self.events = to_keep;
139143
}
140144

141145
/// Lookup event status given an event id.
@@ -144,6 +148,17 @@ impl<I: Ord, T> EventQueue<I, T> {
144148
self.events.get(id)
145149
}
146150

151+
/// Lookup event status given an event id.
152+
/// This operation is `O(log(n))`.
153+
pub fn remove(&mut self, id: &EventId<I>) -> Option<T> {
154+
if let Some(value) = self.events.remove(id) {
155+
self.index.remove(&id.nonce);
156+
Some(value)
157+
} else {
158+
None
159+
}
160+
}
161+
147162
/// Modify the status of an event given its id.
148163
/// This operation is `O(log(n))`.
149164
pub fn modify<F: FnOnce(&mut T)>(&mut self, id: EventId<I>, f: F) {
@@ -158,12 +173,23 @@ impl<I: Ord, T> EventQueue<I, T> {
158173
.and_then(|id| self.events.get(id).map(|e| (id, e)))
159174
}
160175

176+
/// Return max capacity.
177+
pub fn capacity(&self) -> usize {
178+
self.capacity
179+
}
180+
161181
/// Return number of events in the queue.
162182
/// This operation is constant time.
163183
pub fn len(&self) -> usize {
164184
self.events.len()
165185
}
166186

187+
/// Return true if the queue is at its max capacity.
188+
/// This operation is constant time.
189+
pub fn is_full(&self) -> bool {
190+
self.events.len() == self.capacity
191+
}
192+
167193
/// Return true if the queue is empty.
168194
/// This operation is constant time.
169195
pub fn is_empty(&self) -> bool {
@@ -195,7 +221,7 @@ mod test {
195221
use super::*;
196222
use assert_matches::*;
197223

198-
#[derive(Debug, PartialEq, Eq)]
224+
#[derive(Clone, Debug, PartialEq, Eq)]
199225
enum Item {
200226
Begin(u8),
201227
Middle(u8),
@@ -255,6 +281,8 @@ mod test {
255281
assert!(q.selfcheck());
256282
assert_eq!(q.len(), 7);
257283

284+
let mut p = q.clone();
285+
258286
// test purge
259287
q.purge(1, is_begin);
260288
assert_eq!(q.len(), 6);
@@ -268,5 +296,15 @@ mod test {
268296
q.purge(2, is_begin);
269297
assert_eq!(q.len(), 0);
270298
assert!(q.selfcheck());
299+
300+
// test purge_expired
301+
println!("{:?}", p.iter().collect::<Vec<_>>());
302+
p.purge_expired(1);
303+
assert_eq!(p.len(), 5);
304+
assert!(p.selfcheck());
305+
306+
p.purge_expired(2);
307+
assert_eq!(p.len(), 0);
308+
assert!(p.selfcheck());
271309
}
272310
}

src/log.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! We can first seek to the right bucket, then seek to an index within the bucket.
77
//! During writes, if the current bucket is full, a new bucket will be created to store the next entry.
88
use crate::storage::*;
9-
use ic_cdk::export::candid::CandidType;
9+
use candid::CandidType;
1010
use serde::{Deserialize, Serialize};
1111

1212
/// The state and configuration of the log.

src/storage.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ impl Default for StableStorage {
7272
fn default() -> Self {
7373
let mut storage = Self {
7474
offset: 0,
75-
capacity: stable::stable64_size(),
75+
capacity: stable::stable_size(),
7676
};
7777
if storage.capacity > 0 {
7878
let cap = storage.capacity << 16;
7979
let mut bytes = [0; 8];
80-
stable::stable64_read(cap - 8, &mut bytes);
80+
stable::stable_read(cap - 8, &mut bytes);
8181
storage.offset = u64::from_le_bytes(bytes);
8282
}
8383
storage
@@ -88,7 +88,7 @@ impl StableStorage {
8888
/// Attempt to grow the memory by adding new pages.
8989
fn grow(&mut self, added_pages: u64) -> Result<(), StorageError> {
9090
let old_page_count =
91-
stable::stable64_grow(added_pages).map_err(|_| StorageError::OutOfMemory)?;
91+
stable::stable_grow(added_pages).map_err(|_| StorageError::OutOfMemory)?;
9292
self.capacity = old_page_count + added_pages;
9393
Ok(())
9494
}
@@ -108,7 +108,7 @@ impl StableStorage {
108108
}
109109
let bytes = self.offset.to_le_bytes();
110110
io::Write::write(&mut self, &bytes)?;
111-
stable::stable64_write(cap - 8, &bytes);
111+
stable::stable_write(cap - 8, &bytes);
112112
Ok(())
113113
}
114114
}
@@ -119,7 +119,7 @@ impl io::Write for StableStorage {
119119
self.grow((buf.len() >> 16) as u64 + 1)?
120120
}
121121

122-
stable::stable64_write(self.offset, buf);
122+
stable::stable_write(self.offset, buf);
123123
self.offset += buf.len() as u64;
124124
Ok(buf.len())
125125
}
@@ -187,7 +187,7 @@ impl StorageStack for StableStorage {
187187
self.seek_prev()?;
188188
let size = (end - self.offset) as usize;
189189
let mut bytes = vec![0; size];
190-
stable::stable64_read(self.offset, &mut bytes);
190+
stable::stable_read(self.offset, &mut bytes);
191191
let mut de = candid::de::IDLDeserialize::new(&bytes).map_err(StorageError::Candid)?;
192192
let res = candid::utils::ArgumentDecoder::decode(&mut de).map_err(StorageError::Candid)?;
193193
Ok(res)
@@ -199,7 +199,7 @@ impl StorageStack for StableStorage {
199199
}
200200
let mut bytes = [0; 8];
201201
let end = self.offset - 8;
202-
stable::stable64_read(end, &mut bytes);
202+
stable::stable_read(end, &mut bytes);
203203
let start = u64::from_le_bytes(bytes);
204204
if start > end {
205205
return Err(StorageError::OutOfBounds.into());
@@ -221,7 +221,7 @@ impl io::Read for StableStorage {
221221
} else {
222222
buf
223223
};
224-
stable::stable64_read(self.offset, read_buf);
224+
stable::stable_read(self.offset, read_buf);
225225
self.offset += read_buf.len() as u64;
226226
Ok(read_buf.len())
227227
}

0 commit comments

Comments
 (0)