From ac7886155c8ea6c8d46bab2aa34a7f444bef6498 Mon Sep 17 00:00:00 2001 From: Marko Budiselic Date: Mon, 20 Dec 2021 13:46:37 +0100 Subject: [PATCH 1/4] Add initial working example of rust random walk --- rust/rsmgp-random-walk/Cargo.toml | 13 ++++++ rust/rsmgp-random-walk/src/lib.rs | 67 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 rust/rsmgp-random-walk/Cargo.toml create mode 100644 rust/rsmgp-random-walk/src/lib.rs diff --git a/rust/rsmgp-random-walk/Cargo.toml b/rust/rsmgp-random-walk/Cargo.toml new file mode 100644 index 000000000..817d9a75d --- /dev/null +++ b/rust/rsmgp-random-walk/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "rsmgp-random-walk" +version = "0.1.0" +edition = "2018" + +[dependencies] +c_str_macro = "1.0.2" +rsmgp-sys = { path = "../rsmgp-sys" } +rand = "0.8.4" + +[lib] +name = "rust_random_walk" +crate-type = ["cdylib"] diff --git a/rust/rsmgp-random-walk/src/lib.rs b/rust/rsmgp-random-walk/src/lib.rs new file mode 100644 index 000000000..25a3501fe --- /dev/null +++ b/rust/rsmgp-random-walk/src/lib.rs @@ -0,0 +1,67 @@ +use c_str_macro::c_str; +use rsmgp_sys::edge::*; +use rsmgp_sys::memgraph::*; +use rsmgp_sys::mgp::*; +use rsmgp_sys::path::*; +use rsmgp_sys::result::*; +use rsmgp_sys::rsmgp::*; +use rsmgp_sys::value::*; +use rsmgp_sys::{close_module, define_procedure, define_type, init_module}; +use std::ffi::CString; +use std::os::raw::c_int; +use std::panic; +extern crate rand; +use rand::Rng; + +define_procedure!(get, |memgraph: &Memgraph| -> Result<()> { + let args = memgraph.args()?; + let result = memgraph.result_record()?; + let input_start = args.value_at(0)?; + let input_length = args.value_at(1)?; + let path = if let Value::Vertex(ref vertex) = input_start { + Path::make_with_start(&vertex, &memgraph) + } else { + panic!("Failed to create path from the start vertex."); + }?; + let length = if let Value::Int(value) = input_length { + value + } else { + panic!("Failed to read desired path length."); + }; + + let mut vertex = if let Value::Vertex(vertex) = input_start { + vertex + } else { + panic!("Failed to read start vertex."); + }; + let mut rng = rand::thread_rng(); + for _ in 0..length { + let edges: Vec = vertex.out_edges()?.collect(); + if edges.len() == 0 { + break; + } + let num = rng.gen_range(0..edges.len()); + let edge = &edges[0]; + vertex = edge.to_vertex()?; + path.expand(&edge)?; + } + + result.insert_path(c_str!("path"), &path)?; + Ok(()) +}); + +init_module!(|memgraph: &Memgraph| -> Result<()> { + memgraph.add_read_procedure( + get, + c_str!("get"), + &[ + define_type!("start", Type::Vertex), + define_type!("length", Type::Int), + ], + &[], + &[define_type!("path", Type::Path)], + )?; + Ok(()) +}); + +close_module!(|| -> Result<()> { Ok(()) }); From e61d2d57ea3cf104bfa381464097a57c0f01653d Mon Sep 17 00:00:00 2001 From: Marko Budiselic Date: Mon, 20 Dec 2021 13:53:29 +0100 Subject: [PATCH 2/4] Fix clippy issues --- rust/rsmgp-random-walk/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rust/rsmgp-random-walk/src/lib.rs b/rust/rsmgp-random-walk/src/lib.rs index 25a3501fe..f6a2fa4c4 100644 --- a/rust/rsmgp-random-walk/src/lib.rs +++ b/rust/rsmgp-random-walk/src/lib.rs @@ -19,7 +19,7 @@ define_procedure!(get, |memgraph: &Memgraph| -> Result<()> { let input_start = args.value_at(0)?; let input_length = args.value_at(1)?; let path = if let Value::Vertex(ref vertex) = input_start { - Path::make_with_start(&vertex, &memgraph) + Path::make_with_start(vertex, memgraph) } else { panic!("Failed to create path from the start vertex."); }?; @@ -37,13 +37,13 @@ define_procedure!(get, |memgraph: &Memgraph| -> Result<()> { let mut rng = rand::thread_rng(); for _ in 0..length { let edges: Vec = vertex.out_edges()?.collect(); - if edges.len() == 0 { + if edges.is_empty() { break; } let num = rng.gen_range(0..edges.len()); - let edge = &edges[0]; + let edge = &edges[num]; vertex = edge.to_vertex()?; - path.expand(&edge)?; + path.expand(edge)?; } result.insert_path(c_str!("path"), &path)?; From 1c92a6ff9b5e594cc3f39c75e9798dc24118cfd9 Mon Sep 17 00:00:00 2001 From: Marko Budiselic Date: Mon, 20 Dec 2021 15:51:45 +0100 Subject: [PATCH 3/4] Remove extern crate --- rust/rsmgp-random-walk/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rust/rsmgp-random-walk/src/lib.rs b/rust/rsmgp-random-walk/src/lib.rs index f6a2fa4c4..d5c6999a5 100644 --- a/rust/rsmgp-random-walk/src/lib.rs +++ b/rust/rsmgp-random-walk/src/lib.rs @@ -1,4 +1,5 @@ use c_str_macro::c_str; +use rand::Rng; use rsmgp_sys::edge::*; use rsmgp_sys::memgraph::*; use rsmgp_sys::mgp::*; @@ -10,8 +11,6 @@ use rsmgp_sys::{close_module, define_procedure, define_type, init_module}; use std::ffi::CString; use std::os::raw::c_int; use std::panic; -extern crate rand; -use rand::Rng; define_procedure!(get, |memgraph: &Memgraph| -> Result<()> { let args = memgraph.args()?; From d8c58db78e040fa23a398bff9e70ceb48d2624aa Mon Sep 17 00:00:00 2001 From: Marko Budiselic Date: Mon, 20 Dec 2021 18:06:23 +0100 Subject: [PATCH 4/4] Remove redundant code --- rust/rsmgp-random-walk/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rust/rsmgp-random-walk/src/lib.rs b/rust/rsmgp-random-walk/src/lib.rs index d5c6999a5..503e22ef6 100644 --- a/rust/rsmgp-random-walk/src/lib.rs +++ b/rust/rsmgp-random-walk/src/lib.rs @@ -39,8 +39,7 @@ define_procedure!(get, |memgraph: &Memgraph| -> Result<()> { if edges.is_empty() { break; } - let num = rng.gen_range(0..edges.len()); - let edge = &edges[num]; + let edge = &edges[rng.gen_range(0..edges.len())]; vertex = edge.to_vertex()?; path.expand(edge)?; }