Skip to content

Commit

Permalink
refactor: move rdf i/o utils in its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Dec 1, 2023
1 parent 8cab0ad commit da83494
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 486 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ okp4-objectarium = { path = "contracts/okp4-objectarium", features = [
"library",
] }
okp4-objectarium-client = { path = "packages/okp4-objectarium-client" }
okp4-rdf = { path = "packages/okp4-rdf" }
rio_api = "0.8.4"
rio_turtle = "0.8.4"
rio_xml = "0.8.4"
schemars = "0.8.16"
serde = { version = "1.0.193", default-features = false, features = ["derive"] }
serde-json-wasm = "0.5.1"
Expand Down
7 changes: 4 additions & 3 deletions contracts/okp4-cognitarium/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ cw-storage-plus.workspace = true
cw2.workspace = true
derive_builder = "0.12.0"
either = "1.9.0"
rio_api = "0.8.4"
rio_turtle = "0.8.4"
rio_xml = "0.8.4"
rio_api.workspace = true
rio_turtle.workspace = true
rio_xml.workspace = true
okp4-rdf.workspace = true
schemars.workspace = true
serde.workspace = true
thiserror.workspace = true
Expand Down
12 changes: 7 additions & 5 deletions contracts/okp4-cognitarium/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ pub mod execute {
use super::*;
use crate::msg::{DataFormat, Prefix, TriplePattern, WhereClause};
use crate::querier::{PlanBuilder, QueryEngine};
use crate::rdf::{PrefixMap, TripleReader};
use crate::rdf::PrefixMap;
use crate::state::HasCachedNamespaces;
use crate::storer::StoreEngine;
use okp4_rdf::serde::TripleReader;
use std::io::BufReader;

pub fn verify_owner(deps: &DepsMut<'_>, info: &MessageInfo) -> Result<(), ContractError> {
Expand All @@ -73,7 +74,7 @@ pub mod execute {
verify_owner(&deps, &info)?;

let buf = BufReader::new(data.as_slice());
let mut reader = TripleReader::new(&format, buf);
let mut reader = TripleReader::new(&(&format).into(), buf);
let mut storer = StoreEngine::new(deps.storage)?;
let count = storer.store_all(&mut reader)?;

Expand Down Expand Up @@ -144,8 +145,9 @@ pub mod query {
VarOrNode, VarOrNodeOrLiteral, WhereCondition,
};
use crate::querier::{PlanBuilder, QueryEngine};
use crate::rdf::{PrefixMap, TripleWriter};
use crate::rdf::PrefixMap;
use crate::state::HasCachedNamespaces;
use okp4_rdf::serde::TripleWriter;

pub fn store(deps: Deps<'_>) -> StdResult<StoreResponse> {
STORE.load(deps.storage).map(Into::into)
Expand Down Expand Up @@ -231,7 +233,7 @@ pub mod query {
})?;

let out: Vec<u8> = Vec::default();
let mut writer = TripleWriter::new(&format, out);
let mut writer = TripleWriter::new(&(&format).into(), out);

for atom in &atoms {
let triple = atom.into();
Expand Down Expand Up @@ -285,7 +287,7 @@ pub mod query {
})?;

let out: Vec<u8> = Vec::default();
let mut writer = TripleWriter::new(&format, out);
let mut writer = TripleWriter::new(&(&format).into(), out);

for atom in &atoms {
let triple = atom.into();
Expand Down
11 changes: 11 additions & 0 deletions contracts/okp4-cognitarium/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ pub enum DataFormat {
NQuads,
}

impl From<&DataFormat> for okp4_rdf::serde::DataFormat {
fn from(value: &DataFormat) -> Self {
match value {
DataFormat::RDFXml => Self::RDFXml,
DataFormat::Turtle => Self::Turtle,
DataFormat::NTriples => Self::NTriples,
DataFormat::NQuads => Self::NQuads,
}
}
}

/// # StoreLimitsInput
/// Contains requested limitations regarding store usages.
#[cw_serde]
Expand Down
6 changes: 3 additions & 3 deletions contracts/okp4-cognitarium/src/querier/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,14 +858,14 @@ impl AtomTemplate {
#[cfg(test)]
mod test {
use super::*;
use crate::msg::{DataFormat, StoreLimitsInput};
use crate::rdf::TripleReader;
use crate::msg::StoreLimitsInput;
use crate::state;
use crate::state::Object::{Literal, Named};
use crate::state::{Node, Store, StoreStat, NAMESPACE_KEY_INCREMENT, STORE};
use crate::storer::StoreEngine;
use cosmwasm_std::testing::mock_dependencies;
use cosmwasm_std::{Addr, Uint128};
use okp4_rdf::serde::TripleReader;
use std::env;
use std::fs::File;
use std::io::{BufReader, Read};
Expand Down Expand Up @@ -900,7 +900,7 @@ mod test {
NAMESPACE_KEY_INCREMENT.save(storage, &0u128).unwrap();
let data = read_test_data("sample.rdf.xml");
let buf = BufReader::new(data.as_slice());
let mut reader = TripleReader::new(&DataFormat::RDFXml, buf);
let mut reader = TripleReader::new(&okp4_rdf::serde::DataFormat::RDFXml, buf);
let mut storer = StoreEngine::new(storage).unwrap();
let count = storer.store_all(&mut reader).unwrap();

Expand Down
6 changes: 3 additions & 3 deletions contracts/okp4-cognitarium/src/querier/mapper.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::msg::{Literal, Node, IRI};
use crate::rdf::expand_uri;
use crate::state;
use crate::state::{NamespaceResolver, Object, Predicate, Subject};
use crate::{rdf, state};
use cosmwasm_std::{StdError, StdResult, Storage};
use okp4_rdf::uri::{expand_uri, explode_iri};
use std::collections::HashMap;

pub fn node_as_subject(
Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn iri_as_node(
IRI::Prefixed(prefixed) => expand_uri(&prefixed, prefixes),
IRI::Full(full) => Ok(full),
}
.and_then(|iri| rdf::explode_iri(&iri))
.and_then(|iri| explode_iri(&iri))
.and_then(|(ns_key, v)| {
ns_resolver
.resolve_from_val(storage, ns_key)
Expand Down
Loading

0 comments on commit da83494

Please sign in to comment.