Skip to content

Commit

Permalink
Merge pull request #216 from okp4/feat/cognitarium-store
Browse files Browse the repository at this point in the history
🤯 Cognitarium Store query
  • Loading branch information
amimart authored Jun 5, 2023
2 parents d20b1d4 + e641571 commit cb7a159
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 18 deletions.
91 changes: 81 additions & 10 deletions contracts/okp4-cognitarium/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::contract::execute::insert;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult};
use cosmwasm_std::{
to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult,
};
use cw2::set_contract_version;

use crate::error::ContractError;
Expand Down Expand Up @@ -71,21 +73,35 @@ pub mod execute {
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(_deps: Deps, _env: Env, _msg: QueryMsg) -> StdResult<Binary> {
Err(StdError::generic_err("Not implemented"))
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Store => to_binary(&query::store(deps)?),
_ => Err(StdError::generic_err("Not implemented")),
}
}

pub mod query {
use super::*;
use crate::msg::StoreResponse;

pub fn store(deps: Deps) -> StdResult<StoreResponse> {
STORE.load(deps.storage).map(|s| s.into())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::error::StoreError;
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::msg::{StoreLimitsInput, StoreLimitsInputBuilder, StoreResponse};
use crate::state::{
namespaces, triples, Namespace, Node, Object, StoreLimits, StoreStat, Subject, Triple,
};
use crate::{msg, state};
use blake3::Hash;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{Attribute, Order, Uint128};
use cosmwasm_std::{from_binary, Addr, Attribute, Order, Uint128};
use std::env;
use std::fs::File;
use std::io::Read;
Expand Down Expand Up @@ -127,8 +143,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 @@ -193,8 +210,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 Expand Up @@ -386,6 +407,56 @@ mod tests {
}
}

#[test]
fn proper_store() {
let mut deps = mock_dependencies();
STORE
.save(
deps.as_mut().storage,
&Store {
owner: Addr::unchecked("owner"),
limits: StoreLimits {
max_triple_count: 1u128.into(),
max_byte_size: 2u128.into(),
max_triple_byte_size: 3u128.into(),
max_query_limit: 4u32,
max_query_variable_count: 5u32,
max_insert_data_byte_size: 6u128.into(),
max_insert_data_triple_count: 7u128.into(),
},
stat: StoreStat {
triple_count: 1u128.into(),
namespace_count: 2u128.into(),
byte_size: 3u128.into(),
},
},
)
.unwrap();

let res = query(deps.as_ref(), mock_env(), QueryMsg::Store);
assert!(res.is_ok());
assert_eq!(
from_binary::<StoreResponse>(&res.unwrap()).unwrap(),
StoreResponse {
owner: "owner".to_string(),
limits: msg::StoreLimits {
max_triple_count: 1u128.into(),
max_byte_size: 2u128.into(),
max_triple_byte_size: 3u128.into(),
max_query_limit: 4u32,
max_query_variable_count: 5u32,
max_insert_data_byte_size: 6u128.into(),
max_insert_data_triple_count: 7u128.into(),
},
stat: msg::StoreStat {
triple_count: 1u128.into(),
namespace_count: 2u128.into(),
byte_size: 3u128.into(),
}
}
);
}

fn read_test_data(file: &str) -> Binary {
let mut bytes: Vec<u8> = Vec::new();

Expand Down
22 changes: 22 additions & 0 deletions contracts/okp4-cognitarium/src/state/store.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::msg;
use crate::msg::StoreResponse;
use cosmwasm_std::{Addr, Uint128};
use cw_storage_plus::Item;
use serde::{Deserialize, Serialize};
Expand All @@ -22,6 +23,16 @@ impl Store {
}
}

impl From<Store> for StoreResponse {
fn from(value: Store) -> Self {
Self {
owner: value.owner.into(),
limits: value.limits.into(),
stat: value.stat.into(),
}
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct StoreLimits {
pub max_triple_count: Uint128,
Expand Down Expand Up @@ -64,5 +75,16 @@ 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,
}

impl From<StoreStat> for msg::StoreStat {
fn from(value: StoreStat) -> Self {
Self {
triple_count: value.triple_count,
namespace_count: value.namespace_count,
byte_size: value.byte_size,
}
}
}
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 cb7a159

Please sign in to comment.