Skip to content

Commit

Permalink
feat(cognitarium): maintain namespace counter in state
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Jun 1, 2023
1 parent e30e507 commit 24e4c45
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
13 changes: 9 additions & 4 deletions contracts/okp4-cognitarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(),
}
);
Expand Down Expand Up @@ -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!(
Expand Down
1 change: 1 addition & 0 deletions contracts/okp4-cognitarium/src/state/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ impl From<StoreLimits> 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,
}
21 changes: 13 additions & 8 deletions contracts/okp4-cognitarium/src/state/storer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}?;
Expand All @@ -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<Triple> {
Ok(Triple {
subject: self.rio_to_subject(triple.subject)?,
Expand Down

0 comments on commit 24e4c45

Please sign in to comment.