Skip to content

Commit

Permalink
rust: initial api for Ndb and NdbConfig
Browse files Browse the repository at this point in the history
This is the start of our rust library for nostrdb. Implement idiomatic
interfaces for Ndb and NdbConfig.

Changelog-Added: Add initial rust library
  • Loading branch information
jb55 committed Dec 14, 2023
1 parent c7e4303 commit 6279459
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 31 deletions.
2 changes: 1 addition & 1 deletion nostrdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3147,7 +3147,7 @@ static int ndb_run_migrations(struct ndb *ndb)
return 1;
}

int ndb_init(struct ndb **pndb, const char *filename, struct ndb_config *config)
int ndb_init(struct ndb **pndb, const char *filename, const struct ndb_config *config)
{
struct ndb *ndb;
//MDB_dbi ind_id; // TODO: ind_pk, etc
Expand Down
2 changes: 1 addition & 1 deletion nostrdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ int ndb_decode_key(const char *secstr, struct ndb_keypair *keypair);
int ndb_note_verify(void *secp_ctx, unsigned char pubkey[32], unsigned char id[32], unsigned char signature[64]);

// NDB
int ndb_init(struct ndb **ndb, const char *dbdir, struct ndb_config *);
int ndb_init(struct ndb **ndb, const char *dbdir, const struct ndb_config *);
int ndb_db_version(struct ndb *ndb);
int ndb_process_event(struct ndb *, const char *json, int len);
int ndb_process_events(struct ndb *, const char *ldjson, size_t len);
Expand Down
4 changes: 3 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ build = "build.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libc = "0.2.151"

[build-dependencies]
bindgen = "0.69.1"
cc = "1.0"

43 changes: 43 additions & 0 deletions rust/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::bindings;

// The Rust wrapper for ndb_config
pub struct NdbConfig {
pub config: bindings::ndb_config,
}

impl NdbConfig {
// Constructor
pub fn new() -> Self {
let mut config = bindings::ndb_config {
filter_context: std::ptr::null_mut(),
ingest_filter: None,
flags: 0,
ingester_threads: 0,
mapsize: 0,
};

unsafe {
bindings::ndb_default_config(&mut config);
}

NdbConfig { config }
}

// Example setter methods
pub fn set_flags(&mut self, flags: i32) -> &mut Self {
self.config.flags = flags;
self
}

pub fn set_ingester_threads(&mut self, threads: i32) -> &mut Self {
self.config.ingester_threads = threads;
self
}

// Add other setter methods as needed

// Internal method to get a raw pointer to the config, used in Ndb
pub fn as_ptr(&self) -> *const bindings::ndb_config {
&self.config
}
}
3 changes: 3 additions & 0 deletions rust/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub enum Error {
DbOpenFailed,
}
32 changes: 4 additions & 28 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,7 @@
#[allow(unused)]
mod bindings;

#[cfg(test)]
mod tests {
use super::*;
use bindings as ndb;
use std::ffi::CString;

#[test]
fn ndb_init_works() {
unsafe {
// Initialize ndb
let mut ndb_ptr: *mut bindings::ndb = std::ptr::null_mut();
let mut config = ndb::ndb_config {
filter_context: std::ptr::null_mut(),
ingest_filter: None,
flags: 0,
ingester_threads: 0,
mapsize: 0,
};

let path = CString::new(".").expect("Failed to create CString");
ndb::ndb_default_config(&mut config);
ndb::ndb_init(&mut ndb_ptr, path.as_ptr(), &mut config);

// Clean up
bindings::ndb_destroy(ndb_ptr);
}
}
}
mod config;
mod error;
mod ndb;
mod result;
61 changes: 61 additions & 0 deletions rust/src/ndb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::ffi::CString;
use std::ptr;

use crate::bindings;
use crate::config::NdbConfig;
use crate::error::Error;
use crate::result::Result;

pub struct Ndb {
ndb: *mut bindings::ndb,
}

impl Ndb {
// Constructor
pub fn new(db_dir: &str, config: &NdbConfig) -> Result<Self> {
let db_dir_cstr = match CString::new(db_dir) {
Ok(cstr) => cstr,
Err(_) => return Err(Error::DbOpenFailed),
};
let mut ndb: *mut bindings::ndb = ptr::null_mut();
let result = unsafe { bindings::ndb_init(&mut ndb, db_dir_cstr.as_ptr(), config.as_ptr()) };

if result != 0 {
return Err(Error::DbOpenFailed);
}

Ok(Ndb { ndb })
}

// Add other methods to interact with the library here
}

impl Drop for Ndb {
fn drop(&mut self) {
unsafe {
bindings::ndb_destroy(self.ndb);
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs;

fn cleanup() {
let _ = fs::remove_file("data.mdb");
let _ = fs::remove_file("lock.mdb");
}

#[test]
fn ndb_init_works() {
// Initialize ndb
{
let cfg = NdbConfig::new();
let _ = Ndb::new(".", &cfg);
}

cleanup();
}
}
3 changes: 3 additions & 0 deletions rust/src/result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use crate::error::Error;

pub type Result<T> = std::result::Result<T, Error>;

0 comments on commit 6279459

Please sign in to comment.