From 24e4c450e56e05fea0fbca593ed4a41d3f084469 Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Thu, 4 May 2023 11:26:07 +0200 Subject: [PATCH] feat(cognitarium): maintain namespace counter in state --- contracts/okp4-cognitarium/src/contract.rs | 13 ++++++++---- contracts/okp4-cognitarium/src/state/store.rs | 1 + .../okp4-cognitarium/src/state/storer.rs | 21 ++++++++++++------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/contracts/okp4-cognitarium/src/contract.rs b/contracts/okp4-cognitarium/src/contract.rs index 42ea01e0..ce0c5c9f 100644 --- a/contracts/okp4-cognitarium/src/contract.rs +++ b/contracts/okp4-cognitarium/src/contract.rs @@ -96,7 +96,7 @@ mod tests { use crate::msg::ExecuteMsg::InsertData; use crate::msg::{StoreLimitsInput, StoreLimitsInputBuilder}; use crate::state; - use crate::state::{namespaces, triples, Namespace, Node, Object, Subject, Triple}; + use crate::state::{namespaces, triples, Namespace, Node, Object, StoreStat, Subject, Triple}; use blake3::Hash; use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info}; use cosmwasm_std::{Attribute, Order, Uint128}; @@ -141,8 +141,9 @@ mod tests { ); assert_eq!( store.stat, - state::StoreStat { + StoreStat { triple_count: Uint128::zero(), + namespace_count: Uint128::zero(), byte_size: Uint128::zero(), } ); @@ -207,8 +208,12 @@ mod tests { 40 ); assert_eq!( - STORE.load(&deps.storage).unwrap().stat.triple_count, - Uint128::from(40u128), + STORE.load(&deps.storage).unwrap().stat, + StoreStat { + triple_count: 40u128.into(), + namespace_count: 17u128.into(), + byte_size: 7103u128.into(), + }, ); assert_eq!(NAMESPACE_KEY_INCREMENT.load(&deps.storage).unwrap(), 17u128); assert_eq!( diff --git a/contracts/okp4-cognitarium/src/state/store.rs b/contracts/okp4-cognitarium/src/state/store.rs index ae3584d8..75efce97 100644 --- a/contracts/okp4-cognitarium/src/state/store.rs +++ b/contracts/okp4-cognitarium/src/state/store.rs @@ -64,5 +64,6 @@ impl From for msg::StoreLimits { #[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq, Eq)] pub struct StoreStat { pub triple_count: Uint128, + pub namespace_count: Uint128, pub byte_size: Uint128, } diff --git a/contracts/okp4-cognitarium/src/state/storer.rs b/contracts/okp4-cognitarium/src/state/storer.rs index 21a0960b..87a99ed3 100644 --- a/contracts/okp4-cognitarium/src/state/storer.rs +++ b/contracts/okp4-cognitarium/src/state/storer.rs @@ -109,14 +109,7 @@ impl<'a> TripleStorer<'a> { } None => { let mut namespace = match namespaces().load(self.storage, ns_str.clone()) { - Err(StdError::NotFound { .. }) => { - let n = Namespace { - key: self.ns_key_inc_offset, - counter: 0u128, - }; - self.ns_key_inc_offset += 1; - Ok(n) - } + Err(StdError::NotFound { .. }) => Ok(self.allocate_namespace()), Ok(n) => Ok(n), Err(e) => Err(e), }?; @@ -128,6 +121,18 @@ impl<'a> TripleStorer<'a> { } } + fn allocate_namespace(&mut self) -> Namespace { + self.store.stat.namespace_count += Uint128::one(); + + let ns = Namespace { + key: self.ns_key_inc_offset, + counter: 0u128, + }; + self.ns_key_inc_offset += 1; + + ns + } + fn rio_to_triple(&mut self, triple: model::Triple) -> StdResult { Ok(Triple { subject: self.rio_to_subject(triple.subject)?,