nostrdb-rs

nostrdb in rust!
git clone git://jb55.com/nostrdb-rs
Log | Files | Refs | Submodules | README | LICENSE

commit cb1c7683dd9871b3a717f704a4a2fb701bb42431
parent 7c6ab4134fce4bb72cfcbdb3cbb07ca5c24bb15d
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 16 Dec 2023 16:25:21 -0800

context: make Sync, Send, and Clonable

Transaction should get a similar treatment... but will likely need to be
in a different style. Will ponder.

Diffstat:
Msrc/ndb.rs | 39++++++++++++++++++++++++++-------------
1 file changed, 26 insertions(+), 13 deletions(-)

diff --git a/src/ndb.rs b/src/ndb.rs @@ -10,10 +10,31 @@ use crate::result::Result; use crate::transaction::Transaction; use std::fs; use std::path::Path; +use std::sync::Arc; + +#[derive(Debug)] +struct NdbRef { + ndb: *mut bindings::ndb, +} + +/// It's safe to have multi-threaded references to this because thread safety +/// is guaranteed by LMDB +unsafe impl Send for NdbRef {} +unsafe impl Sync for NdbRef {} + +/// The database is automatically closed when [Ndb] is [Drop]ped. +impl Drop for NdbRef { + fn drop(&mut self) { + unsafe { + bindings::ndb_destroy(self.ndb); + } + } +} /// A nostrdb context. Construct one of these with [Ndb::new]. +#[derive(Debug, Clone)] pub struct Ndb { - ndb: *mut bindings::ndb, + refs: Arc<NdbRef>, } impl Ndb { @@ -37,7 +58,8 @@ impl Ndb { return Err(Error::DbOpenFailed); } - Ok(Ndb { ndb }) + let refs = Arc::new(NdbRef { ndb }); + Ok(Ndb { refs }) } /// Ingest a relay-sent event in the form `["EVENT","subid", {"id:"...}]` @@ -51,7 +73,7 @@ impl Ndb { // Get the length of the string let len = json.len() as libc::c_int; - let res = unsafe { bindings::ndb_process_event(self.ndb, c_json_ptr, len) }; + let res = unsafe { bindings::ndb_process_event(self.as_ptr(), c_json_ptr, len) }; if res == 0 { return Err(Error::NoteProcessFailed); @@ -89,16 +111,7 @@ impl Ndb { /// Get the underlying pointer to the context in C pub fn as_ptr(&self) -> *mut bindings::ndb { - return self.ndb; - } -} - -/// The database is automatically closed when [Ndb] is [Drop]ped. -impl Drop for Ndb { - fn drop(&mut self) { - unsafe { - bindings::ndb_destroy(self.ndb); - } + return self.refs.ndb; } }