Skip to content

Commit

Permalink
feat: introduce basic vector and set collections to storage (#914)
Browse files Browse the repository at this point in the history
Co-authored-by: Miraculous Owonubi <[email protected]>
  • Loading branch information
chefsale and miraclx authored Nov 4, 2024
1 parent c291850 commit a687c2a
Show file tree
Hide file tree
Showing 14 changed files with 1,136 additions and 280 deletions.
9 changes: 4 additions & 5 deletions apps/kv-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ use std::collections::BTreeMap;

use calimero_sdk::types::Error;
use calimero_sdk::{app, env};
use calimero_storage::address::Path;
use calimero_storage::collections::UnorderedMap;
use calimero_storage::entities::Element;
use calimero_storage::types::Map;
use calimero_storage::AtomicUnit;

#[app::state(emits = for<'a> Event<'a>)]
#[derive(AtomicUnit, Clone, Debug, PartialEq, PartialOrd)]
#[root]
#[type_id(1)]
pub struct KvStore {
items: Map<String, String>,
items: UnorderedMap<String, String>,
#[storage]
storage: Element,
}
Expand All @@ -32,7 +31,7 @@ impl KvStore {
#[app::init]
pub fn init() -> KvStore {
KvStore {
items: Map::new(&Path::new("::items").unwrap()).unwrap(),
items: UnorderedMap::new().unwrap(),
storage: Element::root(),
}
}
Expand Down Expand Up @@ -87,7 +86,7 @@ impl KvStore {
self.get(key)?.ok_or_else(|| Error::msg("Key not found."))
}

pub fn remove(&mut self, key: &str) -> Result<Option<String>, Error> {
pub fn remove(&mut self, key: &str) -> Result<bool, Error> {
env::log(&format!("Removing key: {:?}", key));

app::emit!(Event::Removed { key });
Expand Down
20 changes: 10 additions & 10 deletions crates/storage-macros/tests/atomic_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Private {
Self {
public: String::new(),
private: String::new(),
storage: Element::new(path),
storage: Element::new(path, None),
}
}
}
Expand All @@ -68,7 +68,7 @@ impl Simple {
Self {
name: String::new(),
value: 0,
storage: Element::new(path),
storage: Element::new(path, None),
}
}
}
Expand All @@ -89,7 +89,7 @@ impl Skipped {
Self {
included: String::new(),
skipped: String::new(),
storage: Element::new(path),
storage: Element::new(path, None),
}
}
}
Expand Down Expand Up @@ -251,8 +251,8 @@ mod hashing {

let mut hasher = Sha256::new();
hasher.update(unit.id().as_bytes());
hasher.update(&to_vec(&unit.public).unwrap());
hasher.update(&to_vec(&unit.element().metadata()).unwrap());
hasher.update(to_vec(&unit.public).unwrap());
hasher.update(to_vec(&unit.element().metadata()).unwrap());
let expected_hash: [u8; 32] = hasher.finalize().into();

assert_eq!(unit.calculate_merkle_hash().unwrap(), expected_hash);
Expand All @@ -274,9 +274,9 @@ mod hashing {

let mut hasher = Sha256::new();
hasher.update(unit.id().as_bytes());
hasher.update(&to_vec(&unit.name).unwrap());
hasher.update(&to_vec(&unit.value).unwrap());
hasher.update(&to_vec(&unit.element().metadata()).unwrap());
hasher.update(to_vec(&unit.name).unwrap());
hasher.update(to_vec(&unit.value).unwrap());
hasher.update(to_vec(&unit.element().metadata()).unwrap());
let expected_hash: [u8; 32] = hasher.finalize().into();

assert_eq!(unit.calculate_merkle_hash().unwrap(), expected_hash);
Expand All @@ -294,8 +294,8 @@ mod hashing {

let mut hasher = Sha256::new();
hasher.update(unit.id().as_bytes());
hasher.update(&to_vec(&unit.included).unwrap());
hasher.update(&to_vec(&unit.element().metadata()).unwrap());
hasher.update(to_vec(&unit.included).unwrap());
hasher.update(to_vec(&unit.element().metadata()).unwrap());
let expected_hash: [u8; 32] = hasher.finalize().into();

assert_eq!(unit.calculate_merkle_hash().unwrap(), expected_hash);
Expand Down
16 changes: 8 additions & 8 deletions crates/storage-macros/tests/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Child {
fn new(path: &Path) -> Self {
Self {
content: String::new(),
storage: Element::new(path),
storage: Element::new(path, None),
}
}
}
Expand All @@ -53,7 +53,7 @@ impl Child {
struct Group;

impl Group {
fn new() -> Self {
const fn new() -> Self {
Self {}
}
}
Expand All @@ -74,7 +74,7 @@ impl Parent {
Self {
title: String::new(),
children: Group::new(),
storage: Element::new(path),
storage: Element::new(path, None),
}
}
}
Expand All @@ -94,7 +94,7 @@ impl Simple {
Self {
name: String::new(),
value: 0,
storage: Element::new(path),
storage: Element::new(path, None),
}
}
}
Expand Down Expand Up @@ -135,8 +135,8 @@ mod hashing {

let mut hasher = Sha256::new();
hasher.update(child.id().as_bytes());
hasher.update(&to_vec(&child.content).unwrap());
hasher.update(&to_vec(&child.element().metadata()).unwrap());
hasher.update(to_vec(&child.content).unwrap());
hasher.update(to_vec(&child.element().metadata()).unwrap());
let expected_hash: [u8; 32] = hasher.finalize().into();

assert_eq!(child.calculate_merkle_hash().unwrap(), expected_hash);
Expand All @@ -149,8 +149,8 @@ mod hashing {

let mut hasher = Sha256::new();
hasher.update(parent.id().as_bytes());
hasher.update(&to_vec(&parent.title).unwrap());
hasher.update(&to_vec(&parent.element().metadata()).unwrap());
hasher.update(to_vec(&parent.title).unwrap());
hasher.update(to_vec(&parent.element().metadata()).unwrap());
let expected_hash: [u8; 32] = hasher.finalize().into();

assert_eq!(parent.calculate_merkle_hash().unwrap(), expected_hash);
Expand Down
2 changes: 1 addition & 1 deletion crates/storage-macros/tests/compile_fail/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() {
fn child_type_specification() {
let parent: Parent = Parent {
group: Group {},
storage: Element::new(&Path::new("::root::node").unwrap()),
storage: Element::new(&Path::new("::root::node").unwrap(), None),
};
let _: Vec<Child> = Interface::children_of(parent.id(), &parent.group).unwrap();

Expand Down
14 changes: 14 additions & 0 deletions crates/storage/src/collections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! High-level data structures for storage.
/// This module provides functionality for hashmap data structures.
pub mod unordered_map;
pub use unordered_map::UnorderedMap;
/// This module provides functionality for hashset data structures.
pub mod unordered_set;
pub use unordered_set::UnorderedSet;
/// This module provides functionality for vector data structures.
pub mod vector;
pub use vector::Vector;
/// This module provides functionality for handling errors.
pub mod error;
pub use error::StoreError;
17 changes: 17 additions & 0 deletions crates/storage/src/collections/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use thiserror::Error;

// fixme! macro expects `calimero_storage` to be in deps
use crate::address::PathError;
use crate::interface::StorageError;

/// General error type for storage operations while interacting with complex collections.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum StoreError {
/// Error while interacting with storage.
#[error(transparent)]
StorageError(#[from] StorageError),
/// Error while interacting with a path.
#[error(transparent)]
PathError(#[from] PathError),
}
Loading

0 comments on commit a687c2a

Please sign in to comment.