Skip to content

Commit

Permalink
Search edges (#1091)
Browse files Browse the repository at this point in the history
Fix issue that was reloading the graph on every graphql query
Resolve the duplication issue by changing way we index vertices and adding multiple values to the same document
Add indexing for edges
  • Loading branch information
fabianmurariu authored Jul 6, 2023
1 parent 431b2cc commit 3832ebf
Show file tree
Hide file tree
Showing 15 changed files with 666 additions and 144 deletions.
51 changes: 38 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions raphtory-graphql/src/data.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use raphtory::{prelude::{Graph, GraphViewOps}, search::IndexedGraph};
use raphtory::{prelude::{Graph, GraphViewOps}, search::IndexedGraph, db::api::view::internal::{IntoDynamic, DynamicGraph}};
use std::{
collections::{HashMap, HashSet},
path::Path,
};
use walkdir::WalkDir;

pub(crate) struct Data {
pub(crate) graphs: HashMap<String, IndexedGraph<Graph>>,
pub(crate) graphs: HashMap<String, IndexedGraph<DynamicGraph>>,
}

impl Data {
Expand Down Expand Up @@ -36,7 +36,7 @@ impl Data {
}
};

let graphs: HashMap<String, IndexedGraph<Graph>> = valid_paths
let graphs: HashMap<String, IndexedGraph<DynamicGraph>> = valid_paths
.into_iter()
.map(|path| {
println!("loading graph from {path}");
Expand All @@ -54,7 +54,7 @@ impl Data {
(graph_name.to_string(), graph)
}
};
}).map(|(name, g)| (name, IndexedGraph::from_graph(&g).expect("Unable to index graph")))
}).map(|(name, g)| (name, IndexedGraph::from_graph(&g.into_dynamic()).expect("Unable to index graph")))
.collect();

Self { graphs }
Expand Down
10 changes: 5 additions & 5 deletions raphtory-graphql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod data;
mod graphql_test {
use super::*;
use dynamic_graphql::{dynamic::DynamicRequestExt, App, FieldValue};
use raphtory::prelude::*;
use raphtory::{prelude::*, db::api::view::internal::IntoDynamic};
use std::collections::HashMap;

#[tokio::test]
Expand All @@ -24,7 +24,7 @@ mod graphql_test {
.add_vertex(0, "Frodo", [("kind".to_string(), Prop::str("Hobbit"))])
.expect("Could not add vertex!");

let graphs = HashMap::from([("lotr".to_string(), graph.into())]);
let graphs = HashMap::from([("lotr".to_string(), graph.into_dynamic().into())]);
let data = data::Data { graphs };

#[derive(App)]
Expand Down Expand Up @@ -66,7 +66,7 @@ mod graphql_test {
let graph = Graph::new();
graph.add_vertex(0, 11, []).expect("Could not add vertex!");

let graphs = HashMap::from([("lotr".to_string(), graph.into())]);
let graphs = HashMap::from([("lotr".to_string(), graph.into_dynamic().into())]);
let data = data::Data { graphs };

#[derive(App)]
Expand Down Expand Up @@ -116,7 +116,7 @@ mod graphql_test {
panic!("Could not add vertex! {:?}", err);
}

let graphs = HashMap::from([("lotr".to_string(), graph.into())]);
let graphs = HashMap::from([("lotr".to_string(), graph.into_dynamic().into())]);
let data = data::Data { graphs };

#[derive(App)]
Expand Down Expand Up @@ -204,7 +204,7 @@ mod graphql_test {
panic!("Could not add vertex! {:?}", err);
}

let graphs = HashMap::from([("lotr".to_string(), graph.into())]);
let graphs = HashMap::from([("lotr".to_string(), graph.into_dynamic().into())]);
let data = data::Data { graphs };

#[derive(App)]
Expand Down
18 changes: 18 additions & 0 deletions raphtory-graphql/src/model/graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,16 @@ impl<G: GraphViewOps + IntoDynamic> From<G> for GqlGraph {
}
}

impl GqlGraph {

pub(crate) fn new(graph: IndexedGraph<DynamicGraph>) -> Self {
Self { graph }
}
}

#[ResolvedObjectFields]
impl GqlGraph {

async fn window(&self, t_start: i64, t_end: i64) -> GqlGraph {
let w = self.graph.window(t_start, t_end);
w.into()
Expand Down Expand Up @@ -97,6 +105,16 @@ impl GqlGraph {
.collect()
}


async fn search_edges(&self, query: String, limit: usize, offset: usize) -> Vec<Edge> {
self.graph
.search_edges(&query, limit, offset)
.into_iter()
.flat_map(|vv| vv)
.map(|vv| vv.into())
.collect()
}

async fn edges<'a>(&self, filter: Option<EdgeFilter>) -> Vec<Edge> {
match filter {
Some(filter) => self
Expand Down
3 changes: 1 addition & 2 deletions raphtory-graphql/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ impl QueryRoot {
async fn graph<'a>(ctx: &Context<'a>, name: &str) -> Option<GqlGraph> {
let data = ctx.data_unchecked::<Data>();
let g = data.graphs.get(name)?;
let graph = g.deref();
Some(graph.clone().into())
Some(GqlGraph::new(g.clone()))
}

async fn graphs<'a>(ctx: &Context<'a>) -> Vec<GraphMeta> {
Expand Down
4 changes: 4 additions & 0 deletions raphtory/src/core/entities/graph/tgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ impl<const N: usize> InnerTemporalGraph<N> {
self.vertex_meta.get_all_property_names(is_static)
}

pub(crate) fn get_all_edge_property_names(&self, is_static: bool) -> Vec<String> {
self.edge_meta.get_all_property_names(is_static)
}

pub(crate) fn get_all_layers(&self) -> Vec<usize> {
self.edge_meta.get_all_layers()
}
Expand Down
1 change: 1 addition & 0 deletions raphtory/src/core/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod timeindex;
use self::iter::Iter;
use locked_view::LockedView;
use parking_lot::{RwLock, RwLockReadGuard};
use rayon::prelude::{IndexedParallelIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
use std::{
fmt::Debug,
Expand Down
10 changes: 10 additions & 0 deletions raphtory/src/db/api/view/internal/core_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ pub trait CoreGraphOps {
/// A vector of strings representing the names of the temporal properties
fn all_vertex_prop_names(&self, is_static: bool) -> Vec<String>;

/// Returns a vector of all names of temporal properties that exist on at least one vertex
///
/// # Returns
///
/// A vector of strings representing the names of the temporal properties
fn all_edge_prop_names(&self, is_static:bool)-> Vec<String>;
/// Returns the static edge property with the given name for the
/// given edge reference.
///
Expand Down Expand Up @@ -250,6 +256,10 @@ impl<G: DelegateCoreOps + ?Sized> CoreGraphOps for G {
self.graph().all_vertex_prop_names(is_static)
}

fn all_edge_prop_names(&self, is_static: bool) -> Vec<String> {
self.graph().all_edge_prop_names(is_static)
}

fn static_edge_prop(&self, e: EdgeRef, name: &str) -> Option<Prop> {
self.graph().static_edge_prop(e, name)
}
Expand Down
8 changes: 7 additions & 1 deletion raphtory/src/db/api/view/internal/graph_ops.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
core::{
entities::{edges::edge_ref::EdgeRef, vertices::vertex_ref::VertexRef, VID},
entities::{edges::edge_ref::EdgeRef, vertices::vertex_ref::VertexRef, VID, EID},
Direction,
},
db::api::view::internal::Base,
Expand All @@ -12,6 +12,8 @@ pub trait GraphOps: Send + Sync {
/// Check if a vertex exists locally and returns local reference.
fn local_vertex_ref(&self, v: VertexRef) -> Option<VID>;

fn find_edge_id(&self, e_id: EID) -> Option<EdgeRef>;

/// Get all layer ids
fn get_unique_layers_internal(&self) -> Vec<usize>;

Expand Down Expand Up @@ -144,6 +146,10 @@ impl<G: DelegateGraphOps + Send + Sync + ?Sized> GraphOps for G {
self.graph().local_vertex_ref(v)
}

fn find_edge_id(&self, e_id: EID) -> Option<EdgeRef> {
self.graph().find_edge_id(e_id)
}

fn get_unique_layers_internal(&self) -> Vec<usize> {
self.graph().get_unique_layers_internal()
}
Expand Down
14 changes: 13 additions & 1 deletion raphtory/src/db/graph/views/layer_graph.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
core::{
entities::{edges::edge_ref::EdgeRef, vertices::vertex_ref::VertexRef, VID},
entities::{edges::edge_ref::EdgeRef, vertices::vertex_ref::VertexRef, VID, EID},
Direction,
},
db::api::view::internal::{
Expand Down Expand Up @@ -45,6 +45,18 @@ impl<G: GraphViewOps> LayeredGraph<G> {
}

impl<G: GraphViewOps> GraphOps for LayeredGraph<G> {

fn find_edge_id(&self, e_id: EID) -> Option<EdgeRef> {
let edge_ref = self.graph.find_edge_id(e_id)?;
let edge_ref_in_layer = self.graph.has_edge_ref(edge_ref.src(), edge_ref.dst(), self.layer);

if edge_ref_in_layer {
Some(edge_ref)
} else {
None
}
}

fn local_vertex_ref(&self, v: VertexRef) -> Option<VID> {
self.graph.local_vertex_ref(v)
}
Expand Down
15 changes: 14 additions & 1 deletion raphtory/src/db/graph/views/vertex_subgraph.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
core::{
entities::{edges::edge_ref::EdgeRef, vertices::vertex_ref::VertexRef, VID},
entities::{edges::edge_ref::EdgeRef, vertices::vertex_ref::VertexRef, VID, EID},
Direction,
},
db::api::view::internal::{
Expand Down Expand Up @@ -41,6 +41,19 @@ impl<G: GraphViewOps> VertexSubgraph<G> {
}

impl<G: GraphViewOps> GraphOps for VertexSubgraph<G> {

fn find_edge_id(&self, e_id: EID) -> Option<EdgeRef> {
let edge_ref = self.graph.find_edge_id(e_id)?;
let vid_src = self.local_vertex_ref(edge_ref.src())?;
let vid_dst = self.local_vertex_ref(edge_ref.dst())?;

if self.vertices.contains(&vid_src) && self.vertices.contains(&vid_dst) {
Some(edge_ref)
} else {
None
}
}

fn local_vertex_ref(&self, v: VertexRef) -> Option<VID> {
self.graph
.local_vertex_ref(v)
Expand Down
Loading

0 comments on commit 3832ebf

Please sign in to comment.