From 508c7bf1770e703921a0713af60c3df303176819 Mon Sep 17 00:00:00 2001 From: Niko PLP Date: Tue, 30 Apr 2024 18:58:36 +0300 Subject: [PATCH] CoreStorage saves Topic, Repo, Overlay and Commit --- ng-broker/src/rocksdb_server_storage.rs | 7 +- ng-broker/src/server_broker.rs | 32 +- ng-broker/src/server_storage/admin/account.rs | 2 +- .../src/server_storage/admin/invitation.rs | 2 +- ng-broker/src/server_storage/admin/wallet.rs | 2 +- ng-broker/src/server_storage/core/commit.rs | 133 +++++++++ ng-broker/src/server_storage/core/mod.rs | 3 + ng-broker/src/server_storage/core/overlay.rs | 281 +++++------------- ng-broker/src/server_storage/core/repo.rs | 12 +- ng-broker/src/server_storage/core/topic.rs | 41 ++- ng-repo/src/kcv_storage.rs | 98 ++++++ ng-verifier/src/user_storage/branch.rs | 2 +- ng-verifier/src/user_storage/repo.rs | 2 +- 13 files changed, 377 insertions(+), 240 deletions(-) create mode 100644 ng-broker/src/server_storage/core/commit.rs diff --git a/ng-broker/src/rocksdb_server_storage.rs b/ng-broker/src/rocksdb_server_storage.rs index 4e6012e..5ea9431 100644 --- a/ng-broker/src/rocksdb_server_storage.rs +++ b/ng-broker/src/rocksdb_server_storage.rs @@ -126,14 +126,17 @@ impl RocksDbServerStorage { // check unicity of class prefixes, by storage #[cfg(debug_assertions)] { + // TODO: refactor the wallet and accounts with Class and the new OKM mechanism, then include them uncomment the following lines //log_debug!("CHECKING..."); // wallet_storage.add_class(&Wallet::CLASS); // wallet_storage.check_prefixes(); // accounts_storage.add_class(&Account::CLASS); // accounts_storage.add_class(&Invitation::CLASS); // accounts_storage.check_prefixes(); - core_storage.add_class(&Topic::CLASS); - core_storage.add_class(&RepoOKM::CLASS); + core_storage.add_class(&TopicStorage::CLASS); + core_storage.add_class(&RepoHashStorage::CLASS); + core_storage.add_class(&OverlayStorage::CLASS); + core_storage.add_class(&CommitStorage::CLASS); core_storage.check_prefixes(); } diff --git a/ng-broker/src/server_broker.rs b/ng-broker/src/server_broker.rs index 1c87231..6cfaf48 100644 --- a/ng-broker/src/server_broker.rs +++ b/ng-broker/src/server_broker.rs @@ -18,6 +18,7 @@ use ng_repo::{ errors::{NgError, ProtocolError, ServerError}, types::*, }; +use serde::{Deserialize, Serialize}; use crate::rocksdb_server_storage::RocksDbServerStorage; @@ -43,21 +44,38 @@ pub struct RepoInfo { pub topics: HashSet, } -pub struct OverlayInfo { - inner: Option, +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct EventInfo { + pub event: Event, + pub blocks: Vec, +} - overlay_topic: Option, +pub struct CommitInfo { + pub event: Option, + pub home_pinned: bool, + pub acks: HashSet, + pub deps: HashSet, + pub futures: HashSet, + pub files: HashSet, +} - topics: HashMap, +#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub enum OverlayType { + Outer(OverlayId), // the ID of the inner overlay corresponding to this outer. + Inner(OverlayId), // the ID of the outer overlay corresponding to the inner + InnerOnly, +} - repos: HashMap, +pub struct OverlayInfo { + pub overlay_type: OverlayType, + pub overlay_topic: Option, + pub topics: HashMap, + pub repos: HashMap, } pub struct ServerBroker { storage: RocksDbServerStorage, - overlays: HashMap, - inner_overlays: HashMap>, } diff --git a/ng-broker/src/server_storage/admin/account.rs b/ng-broker/src/server_storage/admin/account.rs index 4602e16..ffa0534 100644 --- a/ng-broker/src/server_storage/admin/account.rs +++ b/ng-broker/src/server_storage/admin/account.rs @@ -7,7 +7,7 @@ // notice may not be copied, modified, or distributed except // according to those terms. -//! User account OKM (Object Key/Col/Value Mapping) +//! User account Storage (Object Key/Col/Value Mapping) use std::collections::hash_map::DefaultHasher; use std::hash::Hash; diff --git a/ng-broker/src/server_storage/admin/invitation.rs b/ng-broker/src/server_storage/admin/invitation.rs index 40eaed4..46716a1 100644 --- a/ng-broker/src/server_storage/admin/invitation.rs +++ b/ng-broker/src/server_storage/admin/invitation.rs @@ -7,7 +7,7 @@ // notice may not be copied, modified, or distributed except // according to those terms. -//! User account OKM (Object Key/Col/Value Mapping) +//! User account Storage (Object Key/Col/Value Mapping) use std::collections::hash_map::DefaultHasher; use std::hash::Hash; diff --git a/ng-broker/src/server_storage/admin/wallet.rs b/ng-broker/src/server_storage/admin/wallet.rs index 151e9c7..ceb8469 100644 --- a/ng-broker/src/server_storage/admin/wallet.rs +++ b/ng-broker/src/server_storage/admin/wallet.rs @@ -7,7 +7,7 @@ // notice may not be copied, modified, or distributed except // according to those terms. -//! Broker Wallet OKM (Object Key/Col/Value Mapping), persists to storage all the SymKeys needed to open other storages +//! Broker Wallet Storage (Object Key/Col/Value Mapping), persists to storage all the SymKeys needed to open other storages use ng_net::types::*; use ng_repo::errors::StorageError; diff --git a/ng-broker/src/server_storage/core/commit.rs b/ng-broker/src/server_storage/core/commit.rs new file mode 100644 index 0000000..be61c44 --- /dev/null +++ b/ng-broker/src/server_storage/core/commit.rs @@ -0,0 +1,133 @@ +// Copyright (c) 2022-2024 Niko Bonnieure, Par le Peuple, NextGraph.org developers +// All rights reserved. +// Licensed under the Apache License, Version 2.0 +// +// or the MIT license , +// at your option. All files in the project carrying such +// notice may not be copied, modified, or distributed except +// according to those terms. + +//! Commit Storage (Object Key/Col/Value Mapping) + +use std::collections::HashMap; +use std::collections::HashSet; + +use ng_net::types::*; +use ng_repo::errors::StorageError; +use ng_repo::kcv_storage::*; +use ng_repo::types::*; + +use serde_bare::to_vec; + +use crate::server_broker::CommitInfo; +use crate::server_broker::EventInfo; + +pub struct CommitStorage<'a> { + key: Vec, + event: ExistentialValue>, + storage: &'a dyn KCVStorage, +} + +impl<'a> IModel for CommitStorage<'a> { + fn key(&self) -> &Vec { + &self.key + } + fn storage(&self) -> &dyn KCVStorage { + self.storage + } + fn class(&self) -> &Class { + &Self::CLASS + } + fn existential(&mut self) -> Option<&mut dyn IExistentialValue> { + Some(&mut self.event) + } +} + +impl<'a> CommitStorage<'a> { + const PREFIX: u8 = b'e'; + + // Topic properties + pub const EVENT: ExistentialValueColumn = ExistentialValueColumn::new(b'e'); + pub const HOME_PINNED: SingleValueColumn = SingleValueColumn::new(b'p'); + + // Commit -> Acks + pub const ACKS: MultiValueColumn = MultiValueColumn::new(b'a'); + // Commit -> Deps + pub const DEPS: MultiValueColumn = MultiValueColumn::new(b'd'); + // Commit -> Files + pub const FILES: MultiValueColumn = MultiValueColumn::new(b'f'); + // Commit -> Causal future commits + pub const FUTURES: MultiValueColumn = MultiValueColumn::new(b'c'); + + pub const CLASS: Class<'a> = Class::new( + "Commit", + Some(Self::PREFIX), + Some(&Self::EVENT), + &[&Self::HOME_PINNED as &dyn ISingleValueColumn], + &[ + &Self::ACKS as &dyn IMultiValueColumn, + &Self::DEPS, + &Self::FILES, + &Self::FUTURES, + ], + ); + + pub fn new(id: &ObjectId, overlay: &OverlayId, storage: &'a dyn KCVStorage) -> Self { + let mut key: Vec = Vec::with_capacity(33 + 33); + key.append(&mut to_vec(overlay).unwrap()); + key.append(&mut to_vec(id).unwrap()); + CommitStorage { + key, + event: ExistentialValue::>::new(), + storage, + } + } + + pub fn load( + id: &ObjectId, + overlay: &OverlayId, + storage: &'a dyn KCVStorage, + ) -> Result { + let mut opening = CommitStorage::new(id, overlay, storage); + let props = opening.load_props()?; + let existential = col(&Self::EVENT, &props)?; + opening.event.set(&existential)?; + Ok(CommitInfo { + event: existential, + home_pinned: col(&Self::HOME_PINNED, &props).unwrap_or(false), + acks: Self::ACKS.get_all(&mut opening)?, + deps: Self::DEPS.get_all(&mut opening)?, + files: Self::FILES.get_all(&mut opening)?, + futures: Self::FUTURES.get_all(&mut opening)?, + }) + } + + pub fn open( + id: &ObjectId, + overlay: &OverlayId, + storage: &'a dyn KCVStorage, + ) -> Result, StorageError> { + let mut opening = CommitStorage::new(id, overlay, storage); + opening.check_exists()?; + Ok(opening) + } + pub fn create( + id: &ObjectId, + overlay: &OverlayId, + event: &Option, + storage: &'a mut dyn KCVStorage, + ) -> Result, StorageError> { + let mut creating = CommitStorage::new(id, overlay, storage); + if creating.exists() { + return Err(StorageError::AlreadyExists); + } + creating.event.set(event)?; + ExistentialValue::save(&creating, event)?; + + Ok(creating) + } + + pub fn event(&mut self) -> &Option { + self.event.get().unwrap() + } +} diff --git a/ng-broker/src/server_storage/core/mod.rs b/ng-broker/src/server_storage/core/mod.rs index b29d247..bb68d39 100644 --- a/ng-broker/src/server_storage/core/mod.rs +++ b/ng-broker/src/server_storage/core/mod.rs @@ -9,3 +9,6 @@ pub use topic::*; pub mod repo; pub use repo::*; + +pub mod commit; +pub use commit::*; diff --git a/ng-broker/src/server_storage/core/overlay.rs b/ng-broker/src/server_storage/core/overlay.rs index d8b4daa..3709133 100644 --- a/ng-broker/src/server_storage/core/overlay.rs +++ b/ng-broker/src/server_storage/core/overlay.rs @@ -7,229 +7,112 @@ // notice may not be copied, modified, or distributed except // according to those terms. -//! Overlay OKM (Object Key/Col/Value Mapping) +//! Overlay Storage (Object Key/Col/Value Mapping) + +use std::collections::HashMap; +use std::collections::HashSet; use ng_net::types::*; use ng_repo::errors::StorageError; -use ng_repo::kcv_storage::KCVStorage; +use ng_repo::kcv_storage::*; use ng_repo::types::*; -use ng_repo::utils::now_timestamp; -use serde::{Deserialize, Serialize}; -use serde_bare::{from_slice, to_vec}; - -// TODO: versioning V0 -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] -pub struct OverlayMeta { - pub users: u32, - pub last_used: Timestamp, -} -pub struct Overlay<'a> { - /// Overlay ID - id: OverlayId, +use serde_bare::to_vec; + +use crate::server_broker::OverlayInfo; +use crate::server_broker::OverlayType; + +pub struct OverlayStorage<'a> { + key: Vec, + overlay_type: ExistentialValue, storage: &'a dyn KCVStorage, } -impl<'a> Overlay<'a> { - const PREFIX: u8 = b"o"[0]; - - // propertie's suffixes - const SECRET: u8 = b"s"[0]; - const PEER: u8 = b"p"[0]; - const TOPIC: u8 = b"t"[0]; - const META: u8 = b"m"[0]; - const REPO: u8 = b"r"[0]; - - const ALL_PROPERTIES: [u8; 5] = [ - Self::SECRET, - Self::PEER, - Self::TOPIC, - Self::META, - Self::REPO, - ]; - - const SUFFIX_FOR_EXIST_CHECK: u8 = Self::SECRET; - - pub fn open(id: &OverlayId, storage: &'a dyn KCVStorage) -> Result, StorageError> { - let opening = Overlay { - id: id.clone(), - storage, - }; - if !opening.exists() { - return Err(StorageError::NotFound); - } - Ok(opening) +impl<'a> IModel for OverlayStorage<'a> { + fn key(&self) -> &Vec { + &self.key } - pub fn create( - id: &OverlayId, - secret: &SymKey, - repo: Option, - storage: &'a dyn KCVStorage, - ) -> Result, StorageError> { - let acc = Overlay { - id: id.clone(), - storage, - }; - if acc.exists() { - return Err(StorageError::BackendError); - } - storage.write_transaction(&mut |tx| { - tx.put( - Self::PREFIX, - &to_vec(&id)?, - Some(Self::SECRET), - &to_vec(&secret)?, - &None, - )?; - if repo.is_some() { - tx.put( - Self::PREFIX, - &to_vec(&id)?, - Some(Self::REPO), - &to_vec(&repo.unwrap())?, - &None, - )?; - } - let meta = OverlayMeta { - users: 1, - last_used: now_timestamp(), - }; - tx.put( - Self::PREFIX, - &to_vec(&id)?, - Some(Self::META), - &to_vec(&meta)?, - &None, - )?; - Ok(()) - })?; - Ok(acc) - } - pub fn exists(&self) -> bool { + fn storage(&self) -> &dyn KCVStorage { self.storage - .get( - Self::PREFIX, - &to_vec(&self.id).unwrap(), - Some(Self::SUFFIX_FOR_EXIST_CHECK), - &None, - ) - .is_ok() } - pub fn id(&self) -> OverlayId { - self.id - } - pub fn add_peer(&self, peer: &PeerId) -> Result<(), StorageError> { - if !self.exists() { - return Err(StorageError::BackendError); - } - self.storage.put( - Self::PREFIX, - &to_vec(&self.id)?, - Some(Self::PEER), - &to_vec(peer)?, - &None, - ) + fn class(&self) -> &Class { + &Self::CLASS } - pub fn remove_peer(&self, peer: &PeerId) -> Result<(), StorageError> { - self.storage.del_property_value( - Self::PREFIX, - &to_vec(&self.id)?, - Some(Self::PEER), - &to_vec(peer)?, - &None, - ) - } - - pub fn has_peer(&self, peer: &PeerId) -> Result<(), StorageError> { - self.storage.has_property_value( - Self::PREFIX, - &to_vec(&self.id)?, - Some(Self::PEER), - &to_vec(peer)?, - &None, - ) + fn existential(&mut self) -> Option<&mut dyn IExistentialValue> { + Some(&mut self.overlay_type) } + // fn name(&self) -> String { + // format_type_of(self) + // } +} - pub fn add_topic(&self, topic: &TopicId) -> Result<(), StorageError> { - if !self.exists() { - return Err(StorageError::BackendError); +impl<'a> OverlayStorage<'a> { + const PREFIX: u8 = b'o'; + + // Overlay properties + pub const TYPE: ExistentialValueColumn = ExistentialValueColumn::new(b'y'); + pub const TOPIC: SingleValueColumn = SingleValueColumn::new(b't'); + + // Overlay <-> Block refcount + pub const BLOCKS: MultiCounterColumn = MultiCounterColumn::new(b'b'); + // Overlay <-> Object refcount + pub const OBJECTS: MultiCounterColumn = MultiCounterColumn::new(b'j'); + + pub const CLASS: Class<'a> = Class::new( + "Overlay", + Some(Self::PREFIX), + Some(&Self::TYPE), + &[&Self::TOPIC as &dyn ISingleValueColumn], + &[&Self::BLOCKS as &dyn IMultiValueColumn, &Self::OBJECTS], + ); + + pub fn new(id: &OverlayId, storage: &'a dyn KCVStorage) -> Self { + OverlayStorage { + key: to_vec(id).unwrap(), + overlay_type: ExistentialValue::::new(), + storage, } - self.storage.put( - Self::PREFIX, - &to_vec(&self.id)?, - Some(Self::TOPIC), - &to_vec(topic)?, - &None, - ) - } - pub fn remove_topic(&self, topic: &TopicId) -> Result<(), StorageError> { - self.storage.del_property_value( - Self::PREFIX, - &to_vec(&self.id)?, - Some(Self::TOPIC), - &to_vec(topic)?, - &None, - ) } - pub fn has_topic(&self, topic: &TopicId) -> Result<(), StorageError> { - self.storage.has_property_value( - Self::PREFIX, - &to_vec(&self.id)?, - Some(Self::TOPIC), - &to_vec(topic)?, - &None, - ) + pub fn load(id: &OverlayId, storage: &'a dyn KCVStorage) -> Result { + let mut opening = OverlayStorage::new(id, storage); + let props = opening.load_props()?; + let existential = col(&Self::TYPE, &props)?; + opening.overlay_type.set(&existential)?; + let loading = OverlayInfo { + overlay_type: existential, + overlay_topic: col(&Self::TOPIC, &props).ok(), + topics: HashMap::new(), + repos: HashMap::new(), + }; + Ok(loading) } - pub fn secret(&self) -> Result { - match self - .storage - .get(Self::PREFIX, &to_vec(&self.id)?, Some(Self::SECRET), &None) - { - Ok(secret) => Ok(from_slice::(&secret)?), - Err(e) => Err(e), - } + pub fn open( + id: &OverlayId, + storage: &'a dyn KCVStorage, + ) -> Result, StorageError> { + let mut opening = OverlayStorage::new(id, storage); + opening.check_exists()?; + Ok(opening) } - pub fn metadata(&self) -> Result { - match self - .storage - .get(Self::PREFIX, &to_vec(&self.id)?, Some(Self::META), &None) - { - Ok(meta) => Ok(from_slice::(&meta)?), - Err(e) => Err(e), - } - } - pub fn set_metadata(&self, meta: &OverlayMeta) -> Result<(), StorageError> { - if !self.exists() { - return Err(StorageError::BackendError); + pub fn create( + id: &OverlayId, + overlay_type: &OverlayType, + storage: &'a mut dyn KCVStorage, + ) -> Result, StorageError> { + let mut overlay = OverlayStorage::new(id, storage); + if overlay.exists() { + return Err(StorageError::AlreadyExists); } - self.storage.replace( - Self::PREFIX, - &to_vec(&self.id)?, - Some(Self::META), - &to_vec(meta)?, - &None, - ) - } + overlay.overlay_type.set(overlay_type)?; + ExistentialValue::save(&overlay, overlay_type)?; - pub fn repo(&self) -> Result { - match self - .storage - .get(Self::PREFIX, &to_vec(&self.id)?, Some(Self::REPO), &None) - { - Ok(repo) => Ok(from_slice::(&repo)?), - Err(e) => Err(e), - } + Ok(overlay) } - pub fn del(&self) -> Result<(), StorageError> { - self.storage.del_all( - Self::PREFIX, - &to_vec(&self.id)?, - &Self::ALL_PROPERTIES, - &None, - ) + pub fn overlay_type(&mut self) -> &OverlayType { + self.overlay_type.get().unwrap() } } diff --git a/ng-broker/src/server_storage/core/repo.rs b/ng-broker/src/server_storage/core/repo.rs index e6c8957..1ca5088 100644 --- a/ng-broker/src/server_storage/core/repo.rs +++ b/ng-broker/src/server_storage/core/repo.rs @@ -7,7 +7,7 @@ // notice may not be copied, modified, or distributed except // according to those terms. -//! Repo OKM (Object Key/Col/Value Mapping) +//! Repo Storage (Object Key/Col/Value Mapping) use std::collections::HashMap; use std::collections::HashSet; @@ -21,12 +21,12 @@ use serde_bare::to_vec; use crate::server_broker::RepoInfo; -pub struct RepoOKM<'a> { +pub struct RepoHashStorage<'a> { key: Vec, storage: &'a dyn KCVStorage, } -impl<'a> IModel for RepoOKM<'a> { +impl<'a> IModel for RepoHashStorage<'a> { fn key(&self) -> &Vec { &self.key } @@ -41,7 +41,7 @@ impl<'a> IModel for RepoOKM<'a> { } } -impl<'a> RepoOKM<'a> { +impl<'a> RepoHashStorage<'a> { // RepoHash <-> Topic : list of topics of a repo that was pinned on the broker pub const TOPICS: MultiValueColumn = MultiValueColumn::new(b'r'); // RepoHash <-> User : list of users who asked to expose the repo to the outer overlay @@ -80,7 +80,7 @@ impl<'a> RepoOKM<'a> { repo: &RepoHash, overlay: &OverlayId, storage: &'a dyn KCVStorage, - ) -> Result, StorageError> { + ) -> Result, StorageError> { let mut opening = Self::new(repo, overlay, storage); Ok(opening) } @@ -88,7 +88,7 @@ impl<'a> RepoOKM<'a> { repo: &RepoHash, overlay: &OverlayId, storage: &'a mut dyn KCVStorage, - ) -> Result, StorageError> { + ) -> Result, StorageError> { let mut creating = Self::new(repo, overlay, storage); Ok(creating) } diff --git a/ng-broker/src/server_storage/core/topic.rs b/ng-broker/src/server_storage/core/topic.rs index e963b03..88722eb 100644 --- a/ng-broker/src/server_storage/core/topic.rs +++ b/ng-broker/src/server_storage/core/topic.rs @@ -7,7 +7,7 @@ // notice may not be copied, modified, or distributed except // according to those terms. -//! Topic OKM (Object Key/Col/Value Mapping) +//! Topic Storage (Object Key/Col/Value Mapping) use std::collections::HashMap; use std::collections::HashSet; @@ -21,13 +21,13 @@ use serde_bare::to_vec; use crate::server_broker::TopicInfo; -pub struct Topic<'a> { +pub struct TopicStorage<'a> { key: Vec, repo: ExistentialValue, storage: &'a dyn KCVStorage, } -impl<'a> IModel for Topic<'a> { +impl<'a> IModel for TopicStorage<'a> { fn key(&self) -> &Vec { &self.key } @@ -45,7 +45,7 @@ impl<'a> IModel for Topic<'a> { // } } -impl<'a> Topic<'a> { +impl<'a> TopicStorage<'a> { const PREFIX: u8 = b't'; // Topic properties @@ -66,16 +66,26 @@ impl<'a> Topic<'a> { &[&Self::USERS as &dyn IMultiValueColumn, &Self::HEADS], ); + pub fn new(id: &TopicId, overlay: &OverlayId, storage: &'a dyn KCVStorage) -> Self { + let mut key: Vec = Vec::with_capacity(33 + 33); + key.append(&mut to_vec(overlay).unwrap()); + key.append(&mut to_vec(id).unwrap()); + TopicStorage { + key, + repo: ExistentialValue::::new(), + storage, + } + } + pub fn load( id: &TopicId, overlay: &OverlayId, storage: &'a dyn KCVStorage, ) -> Result { - let mut opening = Topic::new(id, overlay, storage); + let mut opening = TopicStorage::new(id, overlay, storage); let props = opening.load_props()?; let existential = col(&Self::REPO, &props)?; opening.repo.set(&existential)?; - //ExistentialValue::save(&opening, &existential)?; let ti = TopicInfo { repo: existential, publisher_advert: col(&Self::ADVERT, &props).ok(), @@ -86,23 +96,12 @@ impl<'a> Topic<'a> { Ok(ti) } - pub fn new(id: &TopicId, overlay: &OverlayId, storage: &'a dyn KCVStorage) -> Self { - let mut key: Vec = Vec::with_capacity(33 + 33); - key.append(&mut to_vec(overlay).unwrap()); - key.append(&mut to_vec(id).unwrap()); - Topic { - key, - repo: ExistentialValue::::new(), - storage, - } - } - pub fn open( id: &TopicId, overlay: &OverlayId, storage: &'a dyn KCVStorage, - ) -> Result, StorageError> { - let mut opening = Topic::new(id, overlay, storage); + ) -> Result, StorageError> { + let mut opening = TopicStorage::new(id, overlay, storage); opening.check_exists()?; Ok(opening) } @@ -111,8 +110,8 @@ impl<'a> Topic<'a> { overlay: &OverlayId, repo: &RepoHash, storage: &'a mut dyn KCVStorage, - ) -> Result, StorageError> { - let mut topic = Topic::new(id, overlay, storage); + ) -> Result, StorageError> { + let mut topic = TopicStorage::new(id, overlay, storage); if topic.exists() { return Err(StorageError::AlreadyExists); } diff --git a/ng-repo/src/kcv_storage.rs b/ng-repo/src/kcv_storage.rs index 3937430..12049aa 100644 --- a/ng-repo/src/kcv_storage.rs +++ b/ng-repo/src/kcv_storage.rs @@ -264,6 +264,7 @@ impl< Ok(res) } } + impl< Model: IModel, Column: Eq + PartialEq + Hash + Serialize + Default + for<'d> Deserialize<'d>, @@ -397,6 +398,103 @@ impl< } } +pub struct MultiCounterColumn< + Model: IModel, + Column: Eq + PartialEq + Hash + Serialize + Default + for<'a> Deserialize<'a>, +> { + prefix: u8, + phantom_column: PhantomData, + phantom_model: PhantomData, +} + +impl< + Model: IModel, + Column: Eq + PartialEq + Hash + Serialize + Default + for<'d> Deserialize<'d>, + > MultiCounterColumn +{ + pub const fn new(prefix: u8) -> Self { + MultiCounterColumn { + prefix, + phantom_column: PhantomData, + phantom_model: PhantomData, + } + } + pub fn increment(&self, model: &mut Model, column: &Column) -> Result<(), StorageError> { + let key = MultiValueColumn::compute_key(model, column)?; + model.storage().write_transaction(&mut |tx| { + let mut val: u64 = match tx.get(self.prefix, &key, None, &None) { + Ok(val_ser) => from_slice(&val_ser)?, + Err(StorageError::NotFound) => 0, + Err(e) => return Err(e), + }; + val += 1; + let val_ser = to_vec(&val)?; + tx.put(self.prefix, &key, None, &val_ser, &None)?; + Ok(()) + }) + } + /// returns true if the counter reached zero (and the key was removed from KVC store) + pub fn decrement(&self, model: &mut Model, column: &Column) -> Result { + let key = MultiValueColumn::compute_key(model, column)?; + let mut ret: bool = false; + model.storage().write_transaction(&mut |tx| { + let val_ser = tx.get(self.prefix, &key, None, &None)?; + let mut val: u64 = from_slice(&val_ser)?; + val -= 1; + ret = val == 0; + if ret { + tx.del(self.prefix, &key, None, &None)?; + } else { + let val_ser = to_vec(&val)?; + tx.put(self.prefix, &key, None, &val_ser, &None)?; + } + Ok(()) + })?; + Ok(ret) + } + + pub fn get(&self, model: &mut Model, column: &Column) -> Result { + let key = MultiValueColumn::compute_key(model, column)?; + let val_ser = model.storage().get(self.prefix, &key, None, &None)?; + let val: u64 = from_slice(&val_ser)?; + Ok(val) + } + + pub fn get_all(&self, model: &mut Model) -> Result, StorageError> { + model.check_exists()?; + let key_prefix = model.key(); + let key_prefix_len = key_prefix.len(); + let mut res: HashMap = HashMap::new(); + let total_size = key_prefix_len + self.value_size()?; + for val in model.storage().get_all_keys_and_values( + self.prefix, + total_size, + key_prefix.to_vec(), + None, + &None, + )? { + if val.0.len() == total_size + 1 { + let col: Column = from_slice(&val.0[1 + key_prefix_len..total_size + 1])?; + let val = from_slice(&val.1)?; + res.insert(col, val); + } + } + Ok(res) + } +} +impl< + Model: IModel, + Column: Eq + PartialEq + Hash + Serialize + Default + for<'d> Deserialize<'d>, + > IMultiValueColumn for MultiCounterColumn +{ + fn value_size(&self) -> Result { + Ok(to_vec(&(0 as u64))?.len()) + } + fn prefix(&self) -> u8 { + self.prefix + } +} + pub trait ISingleValueColumn { fn suffix(&self) -> u8; } diff --git a/ng-verifier/src/user_storage/branch.rs b/ng-verifier/src/user_storage/branch.rs index 78a4215..ed28b66 100644 --- a/ng-verifier/src/user_storage/branch.rs +++ b/ng-verifier/src/user_storage/branch.rs @@ -7,7 +7,7 @@ // notice may not be copied, modified, or distributed except // according to those terms. -//! Branch storage OKM (Object Key/Col/Value Mapping) +//! Branch Storage (Object Key/Col/Value Mapping) use std::collections::hash_map::DefaultHasher; use std::collections::HashMap; diff --git a/ng-verifier/src/user_storage/repo.rs b/ng-verifier/src/user_storage/repo.rs index 548fc3e..c99e090 100644 --- a/ng-verifier/src/user_storage/repo.rs +++ b/ng-verifier/src/user_storage/repo.rs @@ -7,7 +7,7 @@ // notice may not be copied, modified, or distributed except // according to those terms. -//! Repo storage OKM (Object Key/Col/Value Mapping) +//! Repo Storage (Object Key/Col/Value Mapping) use std::collections::hash_map::DefaultHasher; use std::collections::HashMap;