Skip to content

Commit 8b06023

Browse files
committed
feat: add cursor for memstore && replace tempdir with tempfile
1 parent 9f166aa commit 8b06023

File tree

9 files changed

+373
-242
lines changed

9 files changed

+373
-242
lines changed

Diff for: Cargo.lock

+19-81
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ tonic-build = "0.12.3"
3939

4040
[dev-dependencies]
4141
bollard = "0.18.1"
42-
tempdir = "0.3.7"
42+
tempfile = "3.16.0"
4343

4444
[profile.release]
4545
lto = true

Diff for: src/key/store.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ mod tests {
244244
use super::*;
245245
use crate::net::utils::Address;
246246
use energon::drand::schemes::DefaultScheme;
247-
use tempdir;
247+
use tempfile::tempdir;
248248

249249
// #Required permissions
250250
//
@@ -261,7 +261,7 @@ mod tests {
261261
#[test]
262262
fn check_permissions_and_data() {
263263
// Build absolute path for base folder 'testnet'
264-
let temp_dir = tempdir::TempDir::new("permission_test").unwrap();
264+
let temp_dir = tempdir().unwrap();
265265
let base_path = temp_dir
266266
.path()
267267
.join("testnet")

Diff for: src/store/memstore/cursor.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use tonic::async_trait;
2+
3+
use super::MemStore;
4+
5+
use crate::store::{Beacon, BeaconCursor, StorageError, Store};
6+
7+
pub struct MemDbCursor<'a> {
8+
// beacon_id is the id of the beacon
9+
pos: usize,
10+
// db is the database connection
11+
store: &'a MemStore,
12+
}
13+
14+
impl<'a> MemDbCursor<'a> {
15+
pub fn new(store: &'a MemStore) -> Self {
16+
MemDbCursor { pos: 0, store }
17+
}
18+
}
19+
20+
#[async_trait]
21+
impl BeaconCursor for MemDbCursor<'_> {
22+
async fn first(&mut self) -> Result<Option<Beacon>, StorageError> {
23+
match self.store.first().await {
24+
Ok(beacon) => {
25+
self.pos = 0;
26+
Ok(Some(beacon))
27+
}
28+
Err(StorageError::NotFound) => Ok(None),
29+
Err(e) => Err(e),
30+
}
31+
}
32+
33+
async fn next(&mut self) -> Result<Option<Beacon>, StorageError> {
34+
match self.store.get_at_position(self.pos + 1).await {
35+
Ok(beacon) => {
36+
self.pos += 1;
37+
Ok(Some(beacon))
38+
}
39+
Err(StorageError::NotFound) => Ok(None),
40+
Err(e) => Err(e),
41+
}
42+
}
43+
44+
async fn seek(&mut self, round: u64) -> Result<Option<Beacon>, StorageError> {
45+
match self.store.get_beacon(round).await {
46+
Ok(beacon) => {
47+
self.pos = self.store.get_pos(beacon.round).await?;
48+
Ok(Some(beacon))
49+
}
50+
Err(StorageError::NotFound) => Ok(None),
51+
Err(e) => Err(e),
52+
}
53+
}
54+
55+
async fn last(&mut self) -> Result<Option<Beacon>, StorageError> {
56+
match self.store.last().await {
57+
Ok(beacon) => {
58+
self.pos = self.store.len().await? - 1;
59+
Ok(Some(beacon))
60+
}
61+
Err(StorageError::NotFound) => Ok(None),
62+
Err(e) => Err(e),
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)