Skip to content

Commit

Permalink
refactor(cognitarium): move query variables elements
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Jun 5, 2023
1 parent a96258f commit 1066d5a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 92 deletions.
93 changes: 1 addition & 92 deletions contracts/okp4-cognitarium/src/querier/engine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::querier::plan::{PatternValue, QueryNode, QueryPlan};
use crate::querier::variable::{ResolvedVariable, ResolvedVariables};
use crate::state::{triples, Literal, Object, Predicate, Subject, Triple};
use cosmwasm_std::{Order, StdResult, Storage};
use std::collections::VecDeque;
Expand Down Expand Up @@ -81,98 +82,6 @@ impl<'a> QueryEngine<'a> {

type ResolvedVariablesIterator<'a> = Box<dyn Iterator<Item = StdResult<ResolvedVariables>> + 'a>;

#[derive(Eq, PartialEq, Debug, Clone)]
pub enum ResolvedVariable {
Subject(Subject),
Predicate(Predicate),
Object(Object),
}

impl ResolvedVariable {
fn as_subject(&self) -> Option<Subject> {
Some(match self {
ResolvedVariable::Subject(s) => s.clone(),
ResolvedVariable::Predicate(p) => Subject::Named(p.clone()),
ResolvedVariable::Object(o) => match o {
Object::Named(node) => Subject::Named(node.clone()),
Object::Blank(node) => Subject::Blank(node.clone()),
Object::Literal(_) => None?,
},
})
}

fn as_predicate(&self) -> Option<Predicate> {
Some(match self {
ResolvedVariable::Subject(s) => match s {
Subject::Named(node) => node.clone(),
Subject::Blank(_) => None?,
},
ResolvedVariable::Predicate(p) => p.clone(),
ResolvedVariable::Object(o) => match o {
Object::Named(node) => node.clone(),
Object::Blank(_) => None?,
Object::Literal(_) => None?,
},
})
}

fn as_object(&self) -> Option<Object> {
Some(match self {
ResolvedVariable::Subject(s) => match s {
Subject::Named(node) => Object::Named(node.clone()),
Subject::Blank(node) => Object::Blank(node.clone()),
},
ResolvedVariable::Predicate(p) => Object::Named(p.clone()),
ResolvedVariable::Object(o) => o.clone(),
})
}
}

#[derive(Eq, PartialEq, Debug, Clone)]
pub struct ResolvedVariables {
variables: Vec<Option<ResolvedVariable>>,
}

impl ResolvedVariables {
pub fn with_capacity(cap: usize) -> Self {
let mut variables = Vec::with_capacity(cap);
for i in 0..cap {
variables.insert(i, None);
}

Self { variables }
}

/// Merge with another set of resolved variables, returns None if a variable is set on both side
/// with different values.
pub fn merge_with(&self, other: &Self) -> Option<Self> {
let mut merged = other.variables.clone();

for (key, var) in self.variables.iter().enumerate() {
if let Some(val) = var {
match &other.variables[key] {
Some(other_val) => {
if val != other_val {
return None;
}
}
None => merged[key] = Some(val.clone()),
}
}
}

Some(Self { variables: merged })
}

fn set(&mut self, index: usize, var: ResolvedVariable) {
self.variables[index] = Some(var)
}

pub fn get(&self, index: usize) -> &Option<ResolvedVariable> {
self.variables.get(index).unwrap_or(&None)
}
}

struct ForLoopJoinIterator<'a> {
left: ResolvedVariablesIterator<'a>,
right: Rc<dyn Fn(ResolvedVariables) -> ResolvedVariablesIterator<'a> + 'a>,
Expand Down
5 changes: 5 additions & 0 deletions contracts/okp4-cognitarium/src/querier/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
mod engine;
mod plan;
mod plan_builder;
mod variable;

pub use engine::*;
pub use plan::*;
pub use plan_builder::*;
102 changes: 102 additions & 0 deletions contracts/okp4-cognitarium/src/querier/variable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use crate::msg::Value;
use crate::state::{Literal, Object, Predicate, Subject};
use cosmwasm_std::{StdError, StdResult};

#[derive(Eq, PartialEq, Debug, Clone)]
pub enum ResolvedVariable {
Subject(Subject),
Predicate(Predicate),
Object(Object),
}

impl ResolvedVariable {
pub fn as_subject(&self) -> Option<Subject> {
Some(match self {
ResolvedVariable::Subject(s) => s.clone(),
ResolvedVariable::Predicate(p) => Subject::Named(p.clone()),
ResolvedVariable::Object(o) => match o {
Object::Named(node) => Subject::Named(node.clone()),
Object::Blank(node) => Subject::Blank(node.clone()),
Object::Literal(_) => None?,
},
})
}

pub fn as_predicate(&self) -> Option<Predicate> {
Some(match self {
ResolvedVariable::Subject(s) => match s {
Subject::Named(node) => node.clone(),
Subject::Blank(_) => None?,
},
ResolvedVariable::Predicate(p) => p.clone(),
ResolvedVariable::Object(o) => match o {
Object::Named(node) => node.clone(),
Object::Blank(_) => None?,
Object::Literal(_) => None?,
},
})
}

pub fn as_object(&self) -> Option<Object> {
Some(match self {
ResolvedVariable::Subject(s) => match s {
Subject::Named(node) => Object::Named(node.clone()),
Subject::Blank(node) => Object::Blank(node.clone()),
},
ResolvedVariable::Predicate(p) => Object::Named(p.clone()),
ResolvedVariable::Object(o) => o.clone(),
})
}

pub fn as_value<F>(&self, ns_fn: F) -> StdResult<Value>
where
F: n(u128) -> StdResult<String>,
{
Err(StdError::generic_err("mescouilles"))
}
}

#[derive(Eq, PartialEq, Debug, Clone)]
pub struct ResolvedVariables {
variables: Vec<Option<ResolvedVariable>>,
}

impl ResolvedVariables {
pub fn with_capacity(cap: usize) -> Self {
let mut variables = Vec::with_capacity(cap);
for i in 0..cap {
variables.insert(i, None);
}

Self { variables }
}

/// Merge with another set of resolved variables, returns None if a variable is set on both side
/// with different values.
pub fn merge_with(&self, other: &Self) -> Option<Self> {
let mut merged = other.variables.clone();

for (key, var) in self.variables.iter().enumerate() {
if let Some(val) = var {
match &other.variables[key] {
Some(other_val) => {
if val != other_val {
return None;
}
}
None => merged[key] = Some(val.clone()),
}
}
}

Some(Self { variables: merged })
}

pub fn set(&mut self, index: usize, var: ResolvedVariable) {
self.variables[index] = Some(var)
}

pub fn get(&self, index: usize) -> &Option<ResolvedVariable> {
self.variables.get(index).unwrap_or(&None)
}
}

0 comments on commit 1066d5a

Please sign in to comment.