c4lliope

c4lliope

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.

Showing Posts 1 to 4

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

c4lliope

c4lliope OP

At the risk of forking another thread to discuss the same problem, I’m including my progress from today. There seems to have been a bunch of discussion on this before!

In my case, I’m bouncing between two different errors as I decide if I need to impl Resource or if I need to make a new type that mimics GraphDb with the necessary trait (seems likely).

base code:

use rustler::ResourceArc;
use rustler::Resource;
use rustler::{Env};

use sparrowdb::GraphDb;

rustler::atoms! { ok, error }
rustler::init!("Elixir.Spare", [open]);

enum Response {
  Success(ResourceArc<sparrowdb::GraphDb>),
  Failure(String),
}

// #[rustler::nif(schedule = "DirtyCPU")]
// #[rustler::nif(schedule = "DirtyIo")]
#[rustler::nif]
fn open(base: &str) -> Response {
  match GraphDb::open(std::path::Path::new(base)) {
      Ok(graph) => Response.Success(ResourceArc::new(graph)),
      Error(e) => Response.Failure(e.to_string())
  }
}

I may be soooooo off here, this is a hodgepodge from combining examples.


error[E0277]: the trait bound `GraphDb: Resource` is not satisfied
  --> native/spare/src/lib.rs:20:54
   |
20 |       Ok(graph) => Response.Success(ResourceArc::new(graph)),
   |                                     ---------------- ^^^^^ the trait `Resource` is not implemented for `GraphDb`
   |                                     |
   |                                     required by a bound introduced by this call
   |
note: required by a bound in `ResourceArc::<T>::new`
  --> /home/calliope/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rustler-0.38.0/src/resource/arc.rs:39:8
   |
39 |     T: Resource,
   |        ^^^^^^^^ required by this bound in `ResourceArc::<T>::new`
...
43 |     pub fn new(data: T) -> Self {
   |            --- required by a bound in this associated function

error[E0277]: the trait bound `GraphDb: Resource` is not satisfied
  --> native/spare/src/lib.rs:20:37
   |
20 |       Ok(graph) => Response.Success(ResourceArc::new(graph)),
   |                                     ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Resource` is not implemented for `GraphDb`
   |
note: required by a bound in `ResourceArc`
  --> /home/calliope/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rustler-0.38.0/src/resource/arc.rs:27:8
   |
25 | pub struct ResourceArc<T>
   |            ----------- required by a bound in this struct
26 | where
27 |     T: Resource,
   |        ^^^^^^^^ required by this bound in `ResourceArc`


Then, if I include this impl Resource, the error changes:

#[rustler::resource_impl]
impl Resource for sparrowdb::GraphDb
{
  fn destructor(self, env: Env<'_>) {
    drop(self)
  }
}
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
  --> native/spare/src/lib.rs:26:1
   |
26 | impl Resource for sparrowdb::GraphDb
   | ^^^^^^^^^^^^^^^^^^------------------
   |                   |
   |                   `GraphDb` is not defined in the current crate
   |
   = note: impl doesn't have any local type before any uncovered type parameters
   = note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
   = note: define and implement a trait or new type instead
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!

c4lliope

c4lliope OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews