Skip to content

Commit

Permalink
String deduplication (#1269)
Browse files Browse the repository at this point in the history
* eliminate a lot of arc clones

* replace String by ArcStr (wrapped Arc<str>) for cheap clone and to make it possible to support string deduplication

* test string deduplication

* implement string deduplication for property values

* clean up warnings

* expose meta data in core ops and minor cleanup
  • Loading branch information
ljeub-pometry committed Sep 20, 2023
1 parent 29d4c06 commit 349da6e
Show file tree
Hide file tree
Showing 64 changed files with 868 additions and 715 deletions.
9 changes: 3 additions & 6 deletions examples/rust/src/bin/lotr/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,22 @@ fn main() {
g.add_vertex(
lotr.time,
lotr.src_id.clone(),
[("type".to_string(), Prop::Str("Character".to_string()))],
[("type", Prop::str("Character"))],
)
.expect("Failed to add vertex");

g.add_vertex(
lotr.time,
lotr.dst_id.clone(),
[("type".to_string(), Prop::Str("Character".to_string()))],
[("type", Prop::str("Character"))],
)
.expect("Failed to add vertex");

g.add_edge(
lotr.time,
lotr.src_id.clone(),
lotr.dst_id.clone(),
[(
"type".to_string(),
Prop::Str("Character Co-occurrence".to_string()),
)],
[("type", Prop::str("Character Co-occurrence"))],
None,
)
.expect("Failed to add edge");
Expand Down
4 changes: 2 additions & 2 deletions js-raphtory/src/graph/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use raphtory::db::{
api::view::*,
graph::{edge::EdgeView, graph::Graph as TGraph},
};
use std::{ops::Deref, sync::Arc};
use std::sync::Arc;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
Expand Down Expand Up @@ -39,7 +39,7 @@ impl Edge {
let t_props = self.0.properties();
let obj = js_sys::Map::new();
for (k, v) in t_props.iter() {
obj.set(&k.deref().into(), &JsProp(v).into());
obj.set(&k.to_string().into(), &JsProp(v).into());
}
obj
}
Expand Down
4 changes: 2 additions & 2 deletions js-raphtory/src/graph/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl From<JsProp> for JsValue {
match value.0 {
raphtory::core::Prop::U8(v) => v.into(),
raphtory::core::Prop::U16(v) => v.into(),
raphtory::core::Prop::Str(v) => v.into(),
raphtory::core::Prop::Str(v) => v.to_string().into(),
raphtory::core::Prop::I32(v) => v.into(),
raphtory::core::Prop::I64(v) => v.into(),
raphtory::core::Prop::U32(v) => v.into(),
Expand Down Expand Up @@ -68,6 +68,6 @@ impl From<JsObjectEntry> for Option<(String, Prop)> {

let key = arr.at(0).as_string().unwrap();
let value = arr.at(1).as_string().unwrap();
Some((key, Prop::Str(value)))
Some((key, Prop::str(value)))
}
}
2 changes: 1 addition & 1 deletion js-raphtory/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl Graph {
#[wasm_bindgen(js_name = addVertex)]
pub fn add_vertex_js(&self, t: i64, id: JsValue, js_props: Object) -> Result<Vertex, JSError> {
let rust_props = if js_props.is_string() {
vec![("name".to_string(), Prop::Str(js_props.as_string().unwrap()))]
vec![("name".to_string(), Prop::str(js_props.as_string().unwrap()))]
} else if js_props.is_object() {
Object::entries(&js_props)
.iter()
Expand Down
2 changes: 1 addition & 1 deletion js-raphtory/src/graph/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Vertex {
pub fn properties(&self) -> js_sys::Map {
let obj = js_sys::Map::new();
for (k, v) in self.0.properties() {
obj.set(&k.into(), &JsProp(v).into());
obj.set(&k.to_string().into(), &JsProp(v).into());
}
obj
}
Expand Down
2 changes: 1 addition & 1 deletion raphtory-graphql/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Data {
println!("loading graph from {path}");
let graph = Graph::load_from_file(&path).expect("Unable to load from graph");
graph
.add_constant_properties([("path".to_string(), Prop::Str(path.clone()))])
.add_constant_properties([("path".to_string(), Prop::str(path.clone()))])
.expect("Failed to add static property");
let maybe_graph_name = graph.properties().get("name");

Expand Down
12 changes: 2 additions & 10 deletions raphtory-graphql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,10 @@ mod graphql_test {
if let Err(err) = graph.add_vertex(0, "gandalf", NO_PROPS) {
panic!("Could not add vertex! {:?}", err);
}
if let Err(err) = graph.add_vertex(
0,
"bilbo",
[("food".to_string(), Prop::Str("lots".to_string()))],
) {
if let Err(err) = graph.add_vertex(0, "bilbo", [("food".to_string(), Prop::str("lots"))]) {
panic!("Could not add vertex! {:?}", err);
}
if let Err(err) = graph.add_vertex(
0,
"frodo",
[("food".to_string(), Prop::Str("some".to_string()))],
) {
if let Err(err) = graph.add_vertex(0, "frodo", [("food".to_string(), Prop::str("some"))]) {
panic!("Could not add vertex! {:?}", err);
}

Expand Down
3 changes: 1 addition & 2 deletions raphtory-graphql/src/model/filters/edge_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ impl EdgeFilter {
return edge
.ee
.layer_names()
.iter()
.any(|name| name_filter.contains(name));
.any(|name| name_filter.contains(&name));
}

if let Some(property_has_filter) = &self.property_has {
Expand Down
2 changes: 1 addition & 1 deletion raphtory-graphql/src/model/filters/property_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn valid_prop(prop: Prop, value_str: &Option<String>, num_filter: &Option<Number

fn value_neq_str_prop(value: &str, prop: &Prop) -> bool {
if let Prop::Str(prop_str) = prop {
return value != prop_str;
return prop_str != value;
}

false
Expand Down
3 changes: 2 additions & 1 deletion raphtory-graphql/src/model/graph/edge.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::model::graph::node::Node;
use dynamic_graphql::{ResolvedObject, ResolvedObjectFields};
use itertools::Itertools;
use raphtory::db::{
api::view::{
internal::{DynamicGraph, IntoDynamic},
Expand Down Expand Up @@ -47,7 +48,7 @@ impl Edge {
}

async fn layers(&self) -> Vec<String> {
self.ee.layer_names()
self.ee.layer_names().map_into().collect()
}

async fn history(&self) -> Vec<i64> {
Expand Down
6 changes: 3 additions & 3 deletions raphtory-graphql/src/model/graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl GraphMeta {
.properties()
.constant()
.into_iter()
.map(|(k, v)| Property::new(k, v))
.map(|(k, v)| Property::new(k.into(), v))
.collect()
}

Expand Down Expand Up @@ -87,15 +87,15 @@ impl GqlGraph {
}

async fn layer_names(&self) -> Vec<String> {
self.graph.unique_layers()
self.graph.unique_layers().map_into().collect()
}

async fn static_properties(&self) -> Vec<Property> {
self.graph
.properties()
.constant()
.into_iter()
.map(|(k, v)| Property::new(k, v))
.map(|(k, v)| Property::new(k.into(), v))
.collect()
}

Expand Down
3 changes: 2 additions & 1 deletion raphtory-graphql/src/model/graph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use itertools::Itertools;
use raphtory::{
core::ArcStr,
db::{
api::view::internal::DynamicGraph,
graph::{edge::EdgeView, vertex::VertexView},
Expand All @@ -26,7 +27,7 @@ fn get_expanded_edges(

let mut filtered_fetched_edges = match maybe_layers {
Some(layers) => {
let layer_set: HashSet<String> = layers.into_iter().collect();
let layer_set: HashSet<ArcStr> = layers.into_iter().map_into().collect();
fetched_edges
.filter(|e| {
e.layer_names()
Expand Down
2 changes: 1 addition & 1 deletion raphtory-graphql/src/model/graph/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Node {
self.vv
.properties()
.iter()
.map(|(k, v)| Property::new(k.clone(), v))
.map(|(k, v)| Property::new(k.into(), v))
.collect(),
)
}
Expand Down
4 changes: 2 additions & 2 deletions raphtory-graphql/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl Mut {

let subgraph = data.get(&graph_name).ok_or("Graph not found")?;
let mut path = subgraph
.static_prop(&"path".to_string())
.constant_prop(&"path".to_string())
.expect("Path is missing")
.to_string();

Expand Down Expand Up @@ -197,7 +197,7 @@ impl Mut {
.add_constant_properties([("name".to_string(), Prop::Str(new_graph_name.clone()))])
.expect("Failed to add static property");
new_subgraph
.add_constant_properties([("uiProps".to_string(), Prop::Str(props))])
.add_constant_properties([("uiProps".to_string(), Prop::str(props))])
.expect("Failed to add static property");

new_subgraph
Expand Down
8 changes: 3 additions & 5 deletions raphtory-graphql/src/model/schema/edge_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ impl<G: GraphViewOps> EdgeSchema<G> {
}

fn collect_edge_schema<G: GraphViewOps>(edge: EdgeView<G>) -> SchemaAggregate {
let pairs = edge
.properties()
edge.properties()
.iter()
.map(|(key, value)| (key.to_owned(), HashSet::from([value.to_string()])))
.collect_vec();
HashMap::from_iter(pairs)
.map(|(key, value)| (key.to_string(), HashSet::from([value.to_string()])))
.collect()
}
1 change: 0 additions & 1 deletion raphtory-graphql/src/model/schema/graph_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ impl GraphSchema {

let layers = graph
.unique_layers()
.iter()
.map(|layer_name| graph.layer(layer_name).unwrap().into())
.collect_vec();

Expand Down
11 changes: 7 additions & 4 deletions raphtory-graphql/src/model/schema/layer_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ impl<G: GraphViewOps> From<LayeredGraph<G>> for LayerSchema<G> {
impl<G: GraphViewOps> LayerSchema<G> {
/// Returns the name of the layer with this schema
async fn name(&self) -> String {
match &self.graph.unique_layers()[..] {
[layer] => layer.clone(),
_ => panic!("Layered graph outputted more than one layer name"),
}
let mut layers = self.graph.unique_layers();
let layer = layers.next().expect("Layered graph has a layer");
debug_assert!(
layers.next().is_none(),
"Layered graph outputted more than one layer name"
);
layer.into()
}
/// Returns the list of edge schemas for this edge layer
async fn edges(&self) -> Vec<EdgeSchema<LayeredGraph<G>>> {
Expand Down
7 changes: 3 additions & 4 deletions raphtory-graphql/src/model/schema/node_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ impl NodeSchema {
}

fn collect_vertex_schema(vertex: VertexView<DynamicGraph>) -> SchemaAggregate {
let pairs = vertex
vertex
.properties()
.iter()
.map(|(key, value)| (key.to_owned(), HashSet::from([value.to_string()])))
.collect_vec();
HashMap::from_iter(pairs)
.map(|(key, value)| (key.to_string(), HashSet::from([value.to_string()])))
.collect()
}
Loading

0 comments on commit 349da6e

Please sign in to comment.