From edf50b4da57e61e964c51cf49c2b7ac4ed6924d9 Mon Sep 17 00:00:00 2001 From: Fabian Murariu Date: Thu, 4 Jul 2024 13:53:36 +0100 Subject: [PATCH] add test for edge deletions --- raphtory/src/db/api/view/serialise.rs | 68 ++++++++++++++++++++++++--- raphtory/src/graph.proto | 28 +++++++---- 2 files changed, 79 insertions(+), 17 deletions(-) diff --git a/raphtory/src/db/api/view/serialise.rs b/raphtory/src/db/api/view/serialise.rs index 23dabadc8..87e96e3c5 100644 --- a/raphtory/src/db/api/view/serialise.rs +++ b/raphtory/src/db/api/view/serialise.rs @@ -16,7 +16,8 @@ use crate::{ db::{ api::{ mutation::internal::{ - DelegatePropertyAdditionOps, InternalAdditionOps, InternalPropertyAdditionOps, + DelegatePropertyAdditionOps, InternalAdditionOps, InternalDeletionOps, + InternalPropertyAdditionOps, }, storage::nodes::node_storage_ops::NodeStorageOps, }, @@ -27,7 +28,7 @@ use crate::{ self, graph::{ properties_meta::{self, PropName}, - AddEdge, AddNode, GraphConstProps, Node, PropPair, UpdateEdgeConstProps, + AddEdge, AddNode, DelEdge, GraphConstProps, Node, PropPair, UpdateEdgeConstProps, }, lifespan, prop, Dict, NdTime, }, @@ -205,13 +206,14 @@ impl<'graph, G: GraphViewOps<'graph>> StableEncoder for G { ref mut const_edges_props, ref mut nodes, ref mut edges, + ref mut del_edges, .. } = graph; *nodes = self .nodes() .into_iter() - .map(|n| { + .map(|n: crate::db::graph::node::NodeView| { let gid = n.id(); let vid = n.node; let node = self.core_node_entry(vid); @@ -312,6 +314,15 @@ impl<'graph, G: GraphViewOps<'graph>> StableEncoder for G { }); } } + + for time in ee.deletions() { + del_edges.push(DelEdge { + src, + dst, + time, + layer_id: Some(layer_id as u64), + }); + } } } } @@ -320,8 +331,13 @@ impl<'graph, G: GraphViewOps<'graph>> StableEncoder for G { } } -impl<'graph, G: InternalAdditionOps + GraphViewOps<'graph> + DelegatePropertyAdditionOps> - StableDecode for G +impl< + 'graph, + G: InternalAdditionOps + + GraphViewOps<'graph> + + DelegatePropertyAdditionOps + + InternalDeletionOps, + > StableDecode for G { fn decode_from_bytes(buf: &[u8], graph: &Self) -> Result<(), GraphError> { let g = serialise::Graph::decode(&buf[..]).expect("Failed to decode graph"); @@ -453,6 +469,23 @@ impl<'graph, G: InternalAdditionOps + GraphViewOps<'graph> + DelegatePropertyAdd )?; } + for DelEdge { + src, + dst, + time, + layer_id, + } in &g.del_edges + { + let src = VID(*src as usize); + let dst = VID(*dst as usize); + graph.internal_delete_edge( + TimeIndexEntry::from(*time), + src, + dst, + layer_id.map(|id| id as usize).unwrap(), + )?; + } + for UpdateEdgeConstProps { src, dst, @@ -645,9 +678,13 @@ fn as_proto_prop(prop: &Prop) -> Result { #[cfg(test)] mod proto_test { - use chrono::{DateTime, Datelike, NaiveDate, NaiveDateTime, NaiveTime, Timelike}; + use chrono::{DateTime, NaiveDateTime}; - use crate::{core::DocumentInput, db::api::properties::internal::ConstPropertiesOps}; + use crate::{ + core::DocumentInput, + db::api::{mutation::DeletionOps, properties::internal::ConstPropertiesOps}, + prelude::*, + }; use super::*; @@ -706,6 +743,23 @@ mod proto_test { assert_eq!(&g1, &g2); } + #[test] + fn edge_no_props_delete() { + let temp_file = tempfile::NamedTempFile::new().unwrap(); + let g1 = Graph::new().persistent_graph(); + g1.add_edge(3, "Alice", "Bob", NO_PROPS, None).unwrap(); + g1.delete_edge(19, "Alice", "Bob", None).unwrap(); + g1.stable_serialise(&temp_file).unwrap(); + let g2 = PersistentGraph::new(); + PersistentGraph::decode(&temp_file, &g2).unwrap(); + assert_eq!(&g1, &g2); + + let edge = g2.edge("Alice", "Bob").expect("Failed to get edge"); + + let deletions = edge.deletions().iter().copied().collect::>(); + assert_eq!(deletions, vec![19]); + } + #[test] fn edge_t_props() { let temp_file = tempfile::NamedTempFile::new().unwrap(); diff --git a/raphtory/src/graph.proto b/raphtory/src/graph.proto index 8eeca99f9..23d3e5012 100644 --- a/raphtory/src/graph.proto +++ b/raphtory/src/graph.proto @@ -65,16 +65,17 @@ message Lifespan { } message Graph { - repeated AddNode add_nodes = 1; - repeated AddEdge edges = 2; - repeated UpdateNodeConstProps constNodesProps = 3; - repeated UpdateEdgeConstProps constEdgesProps = 4; - GraphConstProps constProperties = 5; - GraphTempProps tempProperties = 6; - PropertiesMeta meta = 7; - repeated string layers = 8; - repeated string node_types = 9; - repeated Node nodes = 10; + GraphConstProps constProperties = 1; + GraphTempProps tempProperties = 2; + PropertiesMeta meta = 3; + repeated AddNode add_nodes = 4; + repeated AddEdge edges = 5; + repeated DelEdge del_edges = 6; + repeated UpdateNodeConstProps constNodesProps = 7; + repeated UpdateEdgeConstProps constEdgesProps = 8; + repeated string layers = 9; + repeated string node_types = 10; + repeated Node nodes = 11; message Node{ uint64 gid = 1; @@ -126,6 +127,13 @@ message Graph { optional uint64 layer_id = 5; } + message DelEdge{ + uint64 src = 1; + uint64 dst = 2; + int64 time = 3; + optional uint64 layer_id = 4; + } + message AddNode { uint64 id = 1; optional PropPair properties = 3;