-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rust: initial api for Ndb and NdbConfig
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
Showing
8 changed files
with
119 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub enum Error { | ||
DbOpenFailed, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |