diff --git a/ng-net/src/orm.rs b/ng-net/src/orm.rs index 0764d91..2d16b93 100644 --- a/ng-net/src/orm.rs +++ b/ng-net/src/orm.rs @@ -11,18 +11,12 @@ #![allow(non_snake_case)] -use std::{ - collections::HashMap, - sync::{Arc, Weak}, -}; +use std::{collections::HashMap, sync::Arc}; use serde::{Deserialize, Serialize}; use serde_json::Value; -use crate::app_protocol::{AppResponse, NuriV0}; -use crate::utils::Sender; - #[derive(Clone, Debug, Serialize, Deserialize)] pub struct OrmShapeType { pub schema: OrmSchema, @@ -99,76 +93,6 @@ pub struct OrmSchemaPredicate { pub extra: Option, } -/// A struct for recording the state of subjects and its predicates -/// relevant to its shape. -#[derive(Clone, Debug)] -pub struct OrmTrackedSubject { - /// The known predicates (only those relevant to the shape). - /// If there are no triples with a predicate, they are discarded - pub tracked_predicates: HashMap>, - /// If this is a nested subject, this records the parents - /// and if they are currently tracking this subject. - pub parents: HashMap>, - /// Validity. When untracked, triple updates are not processed here. - pub valid: OrmTrackedSubjectValidity, - pub subject_iri: String, - /// The shape for which the predicates are tracked. - pub shape: Arc, -} - -#[derive(Clone, Debug, Eq, PartialEq)] -pub enum OrmTrackedSubjectValidity { - Valid, - Invalid, - Pending, - Untracked, -} - -#[derive(Clone, Debug)] -pub struct OrmTrackedPredicate { - /// The predicate schema - pub schema: Arc, - /// If the schema is a nested object, the children. - pub tracked_children: Vec>, - /// The count of triples for this subject and predicate. - pub current_cardinality: i32, - /// If schema is of type literal, the currently present ones. - pub current_literals: Option>, -} - -// Used only for tracking construction of new objects and diffs -// in parallel to modifying the tracked objects and predicates. -pub struct OrmTrackedSubjectChange { - pub subject_iri: String, - /// Predicates that were changed. - pub predicates: HashMap, -} -pub struct OrmTrackedPredicateChanges { - /// The tracked predicate for which those changes were recorded. - pub tracked_predicate: Weak, - pub values_added: Vec, - pub values_removed: Vec, -} - -#[derive(Clone, Debug)] -pub enum Term { - Str(String), - Num(f64), - Bool(bool), - Ref(String), -} - -#[derive(Clone, Debug)] -pub struct OrmSubscription { - pub shape_type: OrmShapeType, - pub session_id: u64, - pub nuri: NuriV0, - pub sender: Sender, - pub tracked_subjects: HashMap>>, -} -type ShapeIri = String; -type SubjectIri = String; - impl Default for OrmSchemaDataType { fn default() -> Self { Self { diff --git a/ng-verifier/src/orm/orm.rs b/ng-verifier/src/orm/orm.rs index 90bbdaa..ae43028 100644 --- a/ng-verifier/src/orm/orm.rs +++ b/ng-verifier/src/orm/orm.rs @@ -30,6 +30,7 @@ use serde_json::json; use serde_json::Value; use crate::orm::orm_add_remove_triples::add_remove_triples; +use crate::orm::types::*; use crate::types::*; use crate::verifier::*; diff --git a/ng-verifier/src/orm/orm_add_remove_triples.rs b/ng-verifier/src/orm/orm_add_remove_triples.rs index e1576d7..db40e83 100644 --- a/ng-verifier/src/orm/orm_add_remove_triples.rs +++ b/ng-verifier/src/orm/orm_add_remove_triples.rs @@ -13,6 +13,7 @@ use std::collections::HashMap; use std::sync::Arc; use std::sync::Weak; +use crate::orm::types::*; use ng_net::orm::*; /// Add all triples to `subject_changes` @@ -42,7 +43,7 @@ pub fn add_remove_triples( Arc::new(OrmTrackedSubject { tracked_predicates: HashMap::new(), parents: HashMap::new(), - valid: ng_net::orm::OrmTrackedSubjectValidity::Pending, + valid: OrmTrackedSubjectValidity::Pending, subject_iri: subject_iri.to_string(), shape: shape.clone(), }) @@ -236,33 +237,27 @@ pub fn add_remove_triples( fn oxrdf_term_to_orm_basic_type(term: &ng_oxigraph::oxrdf::Term) -> BasicType { match oxrdf_term_to_orm_term(term) { - ng_net::orm::Term::Str(s) => BasicType::Str(s), - ng_net::orm::Term::Num(n) => BasicType::Num(n), - ng_net::orm::Term::Bool(b) => BasicType::Bool(b), - ng_net::orm::Term::Ref(b) => BasicType::Str(b), // Treat IRIs as strings + Term::Str(s) => BasicType::Str(s), + Term::Num(n) => BasicType::Num(n), + Term::Bool(b) => BasicType::Bool(b), + Term::Ref(b) => BasicType::Str(b), // Treat IRIs as strings } } /// Converts an oxrdf::Term to an orm::Term -fn oxrdf_term_to_orm_term(term: &ng_oxigraph::oxrdf::Term) -> ng_net::orm::Term { +fn oxrdf_term_to_orm_term(term: &ng_oxigraph::oxrdf::Term) -> Term { match term { - ng_oxigraph::oxrdf::Term::NamedNode(node) => { - ng_net::orm::Term::Ref(node.as_str().to_string()) - } - ng_oxigraph::oxrdf::Term::BlankNode(node) => { - ng_net::orm::Term::Ref(node.as_str().to_string()) - } + ng_oxigraph::oxrdf::Term::NamedNode(node) => Term::Ref(node.as_str().to_string()), + ng_oxigraph::oxrdf::Term::BlankNode(node) => Term::Ref(node.as_str().to_string()), ng_oxigraph::oxrdf::Term::Literal(literal) => { // Check the datatype to determine how to convert match literal.datatype().as_str() { // Check for string first, this is the most common. - "http://www.w3.org/2001/XMLSchema#string" => { - ng_net::orm::Term::Str(literal.value().to_string()) - } + "http://www.w3.org/2001/XMLSchema#string" => Term::Str(literal.value().to_string()), "http://www.w3.org/2001/XMLSchema#boolean" => { match literal.value().parse::() { - Ok(b) => ng_net::orm::Term::Bool(b), - Err(_) => ng_net::orm::Term::Str(literal.value().to_string()), + Ok(b) => Term::Bool(b), + Err(_) => Term::Str(literal.value().to_string()), } } "http://www.w3.org/2001/XMLSchema#integer" @@ -278,16 +273,16 @@ fn oxrdf_term_to_orm_term(term: &ng_oxigraph::oxrdf::Term) -> ng_net::orm::Term | "http://www.w3.org/2001/XMLSchema#unsignedShort" | "http://www.w3.org/2001/XMLSchema#unsignedByte" => { match literal.value().parse::() { - Ok(n) => ng_net::orm::Term::Num(n), - Err(_) => ng_net::orm::Term::Str(literal.value().to_string()), + Ok(n) => Term::Num(n), + Err(_) => Term::Str(literal.value().to_string()), } } - _ => ng_net::orm::Term::Str(literal.value().to_string()), + _ => Term::Str(literal.value().to_string()), } } ng_oxigraph::oxrdf::Term::Triple(triple) => { // For RDF-star triples, convert to string representation - ng_net::orm::Term::Str(triple.to_string()) + Term::Str(triple.to_string()) } } } diff --git a/ng-verifier/src/orm/orm_validation.rs b/ng-verifier/src/orm/orm_validation.rs index fc3af88..dc18910 100644 --- a/ng-verifier/src/orm/orm_validation.rs +++ b/ng-verifier/src/orm/orm_validation.rs @@ -11,6 +11,7 @@ use std::collections::HashMap; use std::collections::HashSet; use std::sync::Arc; +use crate::orm::types::*; use crate::verifier::*; use ng_net::orm::*; diff --git a/ng-verifier/src/orm/types.rs b/ng-verifier/src/orm/types.rs index 2e4f5f3..a843924 100644 --- a/ng-verifier/src/orm/types.rs +++ b/ng-verifier/src/orm/types.rs @@ -6,3 +6,81 @@ // at your option. All files in the project carrying such // notice may not be copied, modified, or distributed except // according to those terms. + +use std::{ + collections::HashMap, + sync::{Arc, Weak}, +}; + +use ng_net::app_protocol::{AppResponse, NuriV0}; +use ng_net::{orm::*, utils::Sender}; + +/// A struct for recording the state of subjects and its predicates +/// relevant to its shape. +#[derive(Clone, Debug)] +pub struct OrmTrackedSubject { + /// The known predicates (only those relevant to the shape). + /// If there are no triples with a predicate, they are discarded + pub tracked_predicates: HashMap>, + /// If this is a nested subject, this records the parents + /// and if they are currently tracking this subject. + pub parents: HashMap>, + /// Validity. When untracked, triple updates are not processed here. + pub valid: OrmTrackedSubjectValidity, + pub subject_iri: String, + /// The shape for which the predicates are tracked. + pub shape: Arc, +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum OrmTrackedSubjectValidity { + Valid, + Invalid, + Pending, + Untracked, +} + +#[derive(Clone, Debug)] +pub struct OrmTrackedPredicate { + /// The predicate schema + pub schema: Arc, + /// If the schema is a nested object, the children. + pub tracked_children: Vec>, + /// The count of triples for this subject and predicate. + pub current_cardinality: i32, + /// If schema is of type literal, the currently present ones. + pub current_literals: Option>, +} + +// Used only for tracking construction of new objects and diffs +// in parallel to modifying the tracked objects and predicates. +pub struct OrmTrackedSubjectChange { + pub subject_iri: String, + /// Predicates that were changed. + pub predicates: HashMap, +} +pub struct OrmTrackedPredicateChanges { + /// The tracked predicate for which those changes were recorded. + pub tracked_predicate: Weak, + pub values_added: Vec, + pub values_removed: Vec, +} + +#[derive(Clone, Debug)] +pub enum Term { + Str(String), + Num(f64), + Bool(bool), + Ref(String), +} + +#[derive(Clone, Debug)] +pub struct OrmSubscription { + pub shape_type: OrmShapeType, + pub session_id: u64, + pub nuri: NuriV0, + pub sender: Sender, + pub tracked_subjects: HashMap>>, +} +type ShapeIri = String; +type SubjectIri = String; diff --git a/ng-verifier/src/verifier.rs b/ng-verifier/src/verifier.rs index 164c8a6..ed826f4 100644 --- a/ng-verifier/src/verifier.rs +++ b/ng-verifier/src/verifier.rs @@ -25,7 +25,6 @@ use async_std::stream::StreamExt; use async_std::sync::{Mutex, RwLockReadGuard}; use futures::channel::mpsc; use futures::SinkExt; -use ng_net::orm::OrmSubscription; use ng_oxigraph::oxigraph::sparql::Query; use ng_oxigraph::oxigraph::sparql::QueryResults; use ng_oxigraph::oxrdf::Term; @@ -67,6 +66,7 @@ use ng_net::{ }; use crate::commits::*; +use crate::orm::types::OrmSubscription; #[cfg(all(not(target_family = "wasm"), not(docsrs)))] use crate::rocksdb_user_storage::RocksDbUserStorage; use crate::types::*;