c4lliope

c4lliope

Pointers shared across function bounds in Rustler?

Hello! I think the Rust [sparrowdb] crate is a superb choice for modern graph dbs; I’m hoping to package this up in an elixir pkg.

[sparrowdb]: crates.io: Rust Package Registry

Here’s the sample code from their README:

    let db = GraphDb::open(std::path::Path::new("social.db"))?;

    db.execute("CREATE (alice:Person {name: 'Alice', age: 30})")?;
    db.execute("CREATE (bob:Person   {name: 'Bob',   age: 25})")?;
    db.execute("MATCH (a:Person {name:'Alice'}), (b:Person {name:'Bob'}) CREATE (a)-[:KNOWS]->(b)")?;

    // Who does Alice know? Who do *they* know?
    let fof = db.execute("MATCH (a:Person {name:'Alice'})-[:KNOWS*1..2]->(f) RETURN DISTINCT f.name")?;
    // -> [["Bob"], ["Carol"]]  (Carol is a friend-of-friend)
    let _ = fof;
    Ok(())

I can think of some simple changes to make this more approachable as an elixir module. Primarily, loading the db variable should be done in one function call, and each call to execute should be another function call.

I’ve come across this in another rust db package; how does one keep a reference to a rust object, such as db, loaded from a rustler function call? How do future rustler-bound functions refer to the same rust object (db) without leaking or cleaning up the memory address?

I’m new to Rust so I probably need to learn some lifetime attribute concepts. If there are common themes from other rustler packages that I can learn from, I’d be thrilled to find something that I can repurpose across the language boundary.

Marked As Solved

kevinschweikert

kevinschweikert

The rustler API seems to have changed a bit. Functions now get auto registered when defining the #[rustler::nif] macro.

You can only define traits for structs when you either define the trait or define the struct. But here the trait is defined by rustler and the struct defined by sparrowdb. So we have to wrap it in our own type.

rustler also converts a Result type automatically to a tagged tuple in Elixir. Either {:ok, _} or {:error, _}.

use rustler::{Resource, ResourceArc};
use sparrowdb::GraphDb;

rustler::init!("Elixir.Spare.Native");

struct GraphDbResource(GraphDb);

#[rustler::resource_impl]
impl Resource for GraphDbResource {}

#[rustler::nif]
fn open(base: &str) -> Result<ResourceArc<GraphDbResource>, String> {
    match GraphDb::open(std::path::Path::new(base)) {
        Ok(graph) => Ok(ResourceArc::new(GraphDbResource(graph))),
        Err(e) => Err(e.to_string()),
    }
}

#[rustler::nif]
fn execute(graph_resource: ResourceArc<GraphDbResource>, cypher: &str) -> Result<String, String> {
    let db = &graph_resource.0;
    match db.execute(cypher) {
        Ok(result) => Ok(format!("{:?}", result)),
        Err(e) => Err(e.to_string()),
    }
}

With the example from the sparrowdb README:

iex(1)> {:ok, db} = Spare.Native.open("social.db")
{:ok, #Reference<0.3497539546.3541958666.218184>}
iex(2)> Spare.Native.execute(db, "CREATE (alice:Person {name: 'Alice', age: 30})")
{:ok, "QueryResult { columns: [], rows: [] }"}
iex(3)> Spare.Native.execute(db, "CREATE (bob:Person   {name: 'Bob',   age: 25})")
{:ok, "QueryResult { columns: [], rows: [] }"}
iex(4)> Spare.Native.execute(db, "MATCH (a:Person {name:'Alice'}), (b:Person {name:'Bob'}) CREATE (a)-[:KNOWS]->(b)")
{:ok, "QueryResult { columns: [], rows: [] }"}
iex(5)> Spare.Native.execute(db, "MATCH (a:Person {name:'Alice'})-[:KNOWS*1..2]->(f) RETURN DISTINCT f.name")
{:ok,
 "QueryResult { columns: [\"f.name\"], rows: [[String(\"Bob\")]] }"}

Happy hacking!

Also Liked

kevinschweikert

kevinschweikert

That’s a resource in rustler. Have a look at this thread: Rustler return and pass reference from Rust to Elixir and back to Rust

Last Post!

c4lliope

c4lliope

Amazing! I copied your code and was able to spin it up on a greyhound bus. I found a helpful example also in ryugraph_ex, and I’m bringing in pieces that seem useful. sparrow seems to miss some of the cypher spec, so I could perhaps make a benchmark using the opencypher cucumber specs. I’d like to also produce a Neo4j-compatible harness, to make use of their explorer and visualization ecology.

For people going through the same burrow, my first rabbit hole with rust embedded databases was fjall crates.io: Rust Package Registry - I assume this approach will be useful there also.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement