create types.rs file

feat/orm
Laurin Weger 2 weeks ago
parent d0725e2fb7
commit 6094b073ec
No known key found for this signature in database
GPG Key ID: 9B372BB0B792770F
  1. 78
      ng-net/src/orm.rs
  2. 1
      ng-verifier/src/orm/orm.rs
  3. 37
      ng-verifier/src/orm/orm_add_remove_triples.rs
  4. 1
      ng-verifier/src/orm/orm_validation.rs
  5. 78
      ng-verifier/src/orm/types.rs
  6. 2
      ng-verifier/src/verifier.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<bool>,
}
/// 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<String, Arc<OrmTrackedPredicate>>,
/// If this is a nested subject, this records the parents
/// and if they are currently tracking this subject.
pub parents: HashMap<String, Weak<OrmTrackedSubject>>,
/// 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<OrmSchemaShape>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum OrmTrackedSubjectValidity {
Valid,
Invalid,
Pending,
Untracked,
}
#[derive(Clone, Debug)]
pub struct OrmTrackedPredicate {
/// The predicate schema
pub schema: Arc<OrmSchemaPredicate>,
/// If the schema is a nested object, the children.
pub tracked_children: Vec<Weak<OrmTrackedSubject>>,
/// 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<Vec<BasicType>>,
}
// 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<String, OrmTrackedPredicateChanges>,
}
pub struct OrmTrackedPredicateChanges {
/// The tracked predicate for which those changes were recorded.
pub tracked_predicate: Weak<OrmTrackedPredicate>,
pub values_added: Vec<BasicType>,
pub values_removed: Vec<BasicType>,
}
#[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<AppResponse>,
pub tracked_subjects: HashMap<SubjectIri, HashMap<ShapeIri, Arc<OrmTrackedSubject>>>,
}
type ShapeIri = String;
type SubjectIri = String;
impl Default for OrmSchemaDataType {
fn default() -> Self {
Self {

@ -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::*;

@ -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::<bool>() {
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::<f64>() {
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())
}
}
}

@ -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::*;

@ -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<String, Arc<OrmTrackedPredicate>>,
/// If this is a nested subject, this records the parents
/// and if they are currently tracking this subject.
pub parents: HashMap<String, Weak<OrmTrackedSubject>>,
/// 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<OrmSchemaShape>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum OrmTrackedSubjectValidity {
Valid,
Invalid,
Pending,
Untracked,
}
#[derive(Clone, Debug)]
pub struct OrmTrackedPredicate {
/// The predicate schema
pub schema: Arc<OrmSchemaPredicate>,
/// If the schema is a nested object, the children.
pub tracked_children: Vec<Weak<OrmTrackedSubject>>,
/// 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<Vec<BasicType>>,
}
// 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<String, OrmTrackedPredicateChanges>,
}
pub struct OrmTrackedPredicateChanges {
/// The tracked predicate for which those changes were recorded.
pub tracked_predicate: Weak<OrmTrackedPredicate>,
pub values_added: Vec<BasicType>,
pub values_removed: Vec<BasicType>,
}
#[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<AppResponse>,
pub tracked_subjects: HashMap<SubjectIri, HashMap<ShapeIri, Arc<OrmTrackedSubject>>>,
}
type ShapeIri = String;
type SubjectIri = String;

@ -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::*;

Loading…
Cancel
Save