|
|
|
@ -725,11 +725,72 @@ impl Verifier { |
|
|
|
|
let subs = self.orm_subscriptions.get(&scope).unwrap(); |
|
|
|
|
for sub in subs.iter() { |
|
|
|
|
// TODO: This if-condition is wrong (intended to not re-apply changes coming from the same subscription).
|
|
|
|
|
if sub.session_id != session_id { |
|
|
|
|
if sub.session_id == session_id { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
// Create diff from changes & subscription.
|
|
|
|
|
|
|
|
|
|
let mut patches: OrmDiff = vec![]; |
|
|
|
|
let mut path: Vec<String> = vec![]; |
|
|
|
|
fn create_patches_for_nested_object( |
|
|
|
|
pred_shape: &OrmSchemaPredicate, |
|
|
|
|
tracked_subjects: &HashMap< |
|
|
|
|
String, |
|
|
|
|
HashMap<String, Arc<RwLock<OrmTrackedSubject>>>, |
|
|
|
|
>, |
|
|
|
|
patches: &mut Vec<OrmDiffOp>, |
|
|
|
|
path: &mut Vec<String>, |
|
|
|
|
object_iri: &String, |
|
|
|
|
orm_changes: &OrmChanges, |
|
|
|
|
sub: &OrmSubscription, |
|
|
|
|
) { |
|
|
|
|
// Object was added. That means, we need to add a basic object with no value,
|
|
|
|
|
// Then add further predicates to it in a recursive call.
|
|
|
|
|
patches.push(OrmDiffOp { |
|
|
|
|
op: OrmDiffOpType::add, |
|
|
|
|
valType: Some(OrmDiffType::object), |
|
|
|
|
path: format!("/{}", path.join("/")), |
|
|
|
|
value: None, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Get the shape IRI for a nested object that is valid.
|
|
|
|
|
let object_shape_iri = { |
|
|
|
|
// Get the tracked subject for this object IRI
|
|
|
|
|
let tracked_subjects_for_obj = tracked_subjects |
|
|
|
|
.get(object_iri) |
|
|
|
|
.expect("Object should be tracked"); |
|
|
|
|
|
|
|
|
|
// Find the first valid shape for this object from the allowed shapes
|
|
|
|
|
let allowed_shape_iris: Vec<&String> = pred_shape |
|
|
|
|
.dataTypes |
|
|
|
|
.iter() |
|
|
|
|
.filter_map(|dt| dt.shape.as_ref()) |
|
|
|
|
.collect(); |
|
|
|
|
|
|
|
|
|
allowed_shape_iris |
|
|
|
|
.iter() |
|
|
|
|
.find(|shape_iri| { |
|
|
|
|
tracked_subjects_for_obj |
|
|
|
|
.get(**shape_iri) |
|
|
|
|
.map(|ts| { |
|
|
|
|
ts.read().unwrap().valid == OrmTrackedSubjectValidity::Valid |
|
|
|
|
}) |
|
|
|
|
.unwrap_or(false) |
|
|
|
|
}) |
|
|
|
|
.unwrap() |
|
|
|
|
.to_string() |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Apply changes for nested object.
|
|
|
|
|
create_patches_for_changed_subj( |
|
|
|
|
orm_changes, |
|
|
|
|
patches, |
|
|
|
|
&object_shape_iri, |
|
|
|
|
&object_iri, |
|
|
|
|
sub, |
|
|
|
|
path, |
|
|
|
|
tracked_subjects, |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn create_patches_for_changed_subj( |
|
|
|
|
orm_changes: &OrmChanges, |
|
|
|
|
patches: &mut OrmDiff, |
|
|
|
@ -773,7 +834,7 @@ impl Verifier { |
|
|
|
|
patches.push(OrmDiffOp { |
|
|
|
|
op: OrmDiffOpType::remove, |
|
|
|
|
valType: Some(OrmDiffType::object), |
|
|
|
|
path: path.join("/"), |
|
|
|
|
path: format!("/{}", path.join("/")), |
|
|
|
|
value: None, |
|
|
|
|
}); |
|
|
|
|
return; |
|
|
|
@ -786,9 +847,16 @@ impl Verifier { |
|
|
|
|
patches.push(OrmDiffOp { |
|
|
|
|
op: OrmDiffOpType::add, |
|
|
|
|
valType: Some(OrmDiffType::object), |
|
|
|
|
path: path.join("/"), |
|
|
|
|
path: format!("/{}", path.join("/")), |
|
|
|
|
value: None, |
|
|
|
|
}) |
|
|
|
|
}); |
|
|
|
|
// And add the id field.
|
|
|
|
|
patches.push(OrmDiffOp { |
|
|
|
|
op: OrmDiffOpType::add, |
|
|
|
|
valType: None, |
|
|
|
|
path: format!("/{}/{}", path.join("/"), subject_iri), |
|
|
|
|
value: None, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -802,11 +870,10 @@ impl Verifier { |
|
|
|
|
|
|
|
|
|
let is_multi = |
|
|
|
|
pred_shape.maxCardinality > 1 || pred_shape.maxCardinality == -1; |
|
|
|
|
let is_object = |
|
|
|
|
pred_shape.dataTypes.iter().any(|dt| !dt.shape.is_none()); |
|
|
|
|
let is_object = pred_shape.dataTypes.iter().any(|dt| !dt.shape.is_none()); |
|
|
|
|
let pred_name = pred_shape.readablePredicate.clone(); |
|
|
|
|
path.push(pred_name); |
|
|
|
|
let path_str = path.join("/"); |
|
|
|
|
let path_str = format!("/{}", path.join("/")); |
|
|
|
|
|
|
|
|
|
// Depending on the predicate type (multi / single, object / not object),
|
|
|
|
|
// add the respective diff operation.
|
|
|
|
@ -848,78 +915,25 @@ impl Verifier { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} else if is_object { |
|
|
|
|
fn create_patches_for_nested_object( |
|
|
|
|
object_iri: &String, |
|
|
|
|
pred_shape: &OrmSchemaPredicate, |
|
|
|
|
tracked_subjects: &HashMap< |
|
|
|
|
String, |
|
|
|
|
HashMap<String, Arc<RwLock<OrmTrackedSubject>>>, |
|
|
|
|
>, |
|
|
|
|
patches: &mut Vec<OrmDiffOp>, |
|
|
|
|
path: &mut Vec<String>, |
|
|
|
|
pred_change: &OrmTrackedPredicateChanges, |
|
|
|
|
orm_changes: &mut OrmChanges, |
|
|
|
|
sub: &OrmSubscription, |
|
|
|
|
) { |
|
|
|
|
// Object was added. That means, we need to add a basic object with no value,
|
|
|
|
|
// Then add further predicates to it in a recursive call.
|
|
|
|
|
patches.push(OrmDiffOp { |
|
|
|
|
op: OrmDiffOpType::add, |
|
|
|
|
valType: Some(OrmDiffType::object), |
|
|
|
|
path: path.join("/"), |
|
|
|
|
value: None, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Change in single object property.
|
|
|
|
|
if !is_multi { |
|
|
|
|
let object_iri = match &pred_change.values_added[0] { |
|
|
|
|
BasicType::Str(iri) => iri, |
|
|
|
|
_ => panic!("object not an IRI"), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/// Get the shape IRI for a nested object that is valid.
|
|
|
|
|
let object_shape_iri = { |
|
|
|
|
// Get the tracked subject for this object IRI
|
|
|
|
|
let tracked_subjects_for_obj = tracked_subjects |
|
|
|
|
.get(object_iri) |
|
|
|
|
.expect("Object should be tracked"); |
|
|
|
|
|
|
|
|
|
// Find the first valid shape for this object from the allowed shapes
|
|
|
|
|
let allowed_shape_iris: Vec<&String> = pred_shape |
|
|
|
|
.dataTypes |
|
|
|
|
.iter() |
|
|
|
|
.filter_map(|dt| dt.shape.as_ref()) |
|
|
|
|
.collect(); |
|
|
|
|
|
|
|
|
|
allowed_shape_iris |
|
|
|
|
.iter() |
|
|
|
|
.find(|shape_iri| { |
|
|
|
|
tracked_subjects_for_obj |
|
|
|
|
.get(**shape_iri) |
|
|
|
|
.map(|ts| { |
|
|
|
|
ts.read().unwrap().valid |
|
|
|
|
== OrmTrackedSubjectValidity::Valid |
|
|
|
|
}) |
|
|
|
|
.unwrap_or(false) |
|
|
|
|
}) |
|
|
|
|
.unwrap() |
|
|
|
|
.to_string() |
|
|
|
|
_ => panic!("Object no IRI"), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Apply changes for nested object.
|
|
|
|
|
create_patches_for_changed_subj( |
|
|
|
|
orm_changes, |
|
|
|
|
// Single object.
|
|
|
|
|
if pred_change.values_added.len() > 0 { |
|
|
|
|
create_patches_for_nested_object( |
|
|
|
|
pred_shape, |
|
|
|
|
tracked_subjects, |
|
|
|
|
patches, |
|
|
|
|
&object_shape_iri, |
|
|
|
|
&object_iri, |
|
|
|
|
sub, |
|
|
|
|
path, |
|
|
|
|
tracked_subjects, |
|
|
|
|
object_iri, |
|
|
|
|
orm_changes, |
|
|
|
|
sub, |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if !is_multi { |
|
|
|
|
// Single object.
|
|
|
|
|
if pred_change.values_added.len() > 0 { |
|
|
|
|
} else { |
|
|
|
|
if pred_change.values_removed.len() > 0 { |
|
|
|
|
// Object is removed.
|
|
|
|
|
patches.push(OrmDiffOp { |
|
|
|
|
op: OrmDiffOpType::remove, |
|
|
|
@ -929,16 +943,41 @@ impl Verifier { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// is_multi && is_object
|
|
|
|
|
// Change(s) in multi object property.
|
|
|
|
|
|
|
|
|
|
// Add every new object.
|
|
|
|
|
for object_iri_added in pred_change.values_added.iter() { |
|
|
|
|
// If object did not exist before, we create a root patch.
|
|
|
|
|
for obj_iri_bt in pred_change.values_added.iter() { |
|
|
|
|
let obj_iri = match obj_iri_bt { |
|
|
|
|
BasicType::Str(iri) => iri, |
|
|
|
|
_ => panic!("Object no IRI"), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// We also need to check if the object existed before.
|
|
|
|
|
// First, we create a root object (if the object existed before, this has no effect).
|
|
|
|
|
patches.push(OrmDiffOp { |
|
|
|
|
op: OrmDiffOpType::add, |
|
|
|
|
valType: Some(OrmDiffType::object), |
|
|
|
|
path: path_str.clone(), |
|
|
|
|
value: None, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Add escaped object IRI to path.
|
|
|
|
|
path.push(escape_json_pointer(obj_iri)); |
|
|
|
|
|
|
|
|
|
create_patches_for_nested_object( |
|
|
|
|
pred_shape, |
|
|
|
|
tracked_subjects, |
|
|
|
|
patches, |
|
|
|
|
path, |
|
|
|
|
obj_iri, |
|
|
|
|
orm_changes, |
|
|
|
|
sub, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// Remove object IRI from stack again.
|
|
|
|
|
path.pop(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Delete every object removed.
|
|
|
|
|
// Delete objects.
|
|
|
|
|
// If there are no more predicates, delete the whole object.
|
|
|
|
|
if pred_change |
|
|
|
|
.tracked_predicate |
|
|
|
@ -956,13 +995,12 @@ impl Verifier { |
|
|
|
|
value: None, |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
for object_iri_removed in pred_change.values_removed.iter() |
|
|
|
|
{ |
|
|
|
|
for object_iri_removed in pred_change.values_removed.iter() { |
|
|
|
|
patches.push(OrmDiffOp { |
|
|
|
|
op: OrmDiffOpType::remove, |
|
|
|
|
valType: Some(OrmDiffType::object), |
|
|
|
|
path: format!( |
|
|
|
|
"{}/{}", |
|
|
|
|
"/{}/{}", |
|
|
|
|
path_str, |
|
|
|
|
match object_iri_removed { |
|
|
|
|
BasicType::Str(iri) => iri, |
|
|
|
@ -976,29 +1014,47 @@ impl Verifier { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Remove to this predicate name from the path again.
|
|
|
|
|
// Remove this predicate name from the path again.
|
|
|
|
|
path.pop(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let mut patches: OrmDiff = vec![]; |
|
|
|
|
let mut path: Vec<String> = Vec::with_capacity(4); |
|
|
|
|
|
|
|
|
|
// Iterate over each root subject with the right shape
|
|
|
|
|
// For each tracked subject that has the subscription's shape, call fn above
|
|
|
|
|
for subject_iri in sub.tracked_subjects.iter() { |
|
|
|
|
// Path.push
|
|
|
|
|
// Create changes
|
|
|
|
|
// path.pop
|
|
|
|
|
for (subject_iri, tracked_subjects_by_shape) in sub.tracked_subjects.iter() { |
|
|
|
|
for (shape_iri, tracked_subject) in tracked_subjects_by_shape.iter() { |
|
|
|
|
if *shape_iri != sub.shape_type.shape { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
// Found a root subject for this shape.
|
|
|
|
|
|
|
|
|
|
// Add subject IRI as first part of path pointer.
|
|
|
|
|
path.push(escape_json_pointer(subject_iri)); |
|
|
|
|
create_patches_for_changed_subj( |
|
|
|
|
&orm_changes, |
|
|
|
|
&mut patches, |
|
|
|
|
shape_iri, |
|
|
|
|
subject_iri, |
|
|
|
|
sub, |
|
|
|
|
&mut path, |
|
|
|
|
&sub.tracked_subjects, |
|
|
|
|
); |
|
|
|
|
path.pop(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
let orm_diff: OrmDiff = vec![]; |
|
|
|
|
// Send response with patches.
|
|
|
|
|
let _ = sub |
|
|
|
|
.sender |
|
|
|
|
.clone() |
|
|
|
|
.send(AppResponse::V0(AppResponseV0::OrmUpdate(orm_diff.to_vec()))) |
|
|
|
|
.send(AppResponse::V0(AppResponseV0::OrmUpdate(patches.to_vec()))) |
|
|
|
|
.await; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// After creating new objects (without an id) in JS-land,
|
|
|
|
|
/// we send the generated id for those back.
|
|
|
|
@ -1143,3 +1199,5 @@ impl Verifier { |
|
|
|
|
Ok((rx, close)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Btw, orm/mod.rs is exceeding 1200 lines again. Is that a good practice? I have the feeling, we could separate a couple of things..
|
|
|
|
|