nostrdb-rs

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

commit 9a9c15709b8287ad2e08575c5b3b2c7143b49df1
parent 3dec9757d798553677f82d216ef71158de3610ae
Author: William Casarin <jb55@jb55.com>
Date:   Wed,  7 Feb 2024 14:17:04 -0800

temp fix for subscription memory issues

still need real fix

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
MCargo.toml | 4++--
Mshell.nix | 2+-
Msrc/filter.rs | 12+++++++++---
Msrc/ndb.rs | 15++++++---------
Msrc/subscription.rs | 4++--
5 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -18,11 +18,11 @@ bindgen = "0.69.1" bindgen = [] [dependencies] -env_logger = "0.10.1" flatbuffers = "23.5.26" libc = "0.2.151" -log = "0.4.20" tokio = { version = "1", features = ["rt-multi-thread", "macros"] } +tracing = "0.1.40" +tracing-subscriber = "0.3.18" [dev-dependencies] hex = "0.4.3" diff --git a/shell.nix b/shell.nix @@ -1,7 +1,7 @@ { pkgs ? import <nixpkgs> {} }: with pkgs; mkShell { - nativeBuildInputs = [ rustPlatform.bindgenHook cargo rustc rustfmt libiconv pkg-config ]; + nativeBuildInputs = [ rustPlatform.bindgenHook cargo rustc rustfmt libiconv pkg-config valgrind ]; LIBCLANG_PATH="${pkgs.llvmPackages.libclang.lib}/lib"; } diff --git a/src/filter.rs b/src/filter.rs @@ -1,8 +1,10 @@ use crate::bindings; use crate::Note; use std::ffi::CString; +use std::marker::PhantomPinned; use std::os::raw::c_char; use std::ptr::null_mut; +use tracing::debug; #[derive(Debug)] pub struct Filter { @@ -17,7 +19,7 @@ impl bindings::ndb_filter { impl Filter { pub fn new() -> Filter { - let null = std::ptr::null_mut(); + let null = null_mut(); let mut filter_data = bindings::ndb_filter { elem_buf: bindings::cursor { start: null, @@ -30,7 +32,7 @@ impl Filter { end: null, }, num_elements: 0, - current: std::ptr::null_mut(), + current: null_mut(), elements: [ null_mut(), null_mut(), @@ -115,7 +117,7 @@ impl Filter { unsafe { bindings::ndb_filter_end_field(self.as_mut_ptr()) } } - pub fn authors<'a>(self, authors: Vec<&'a [u8; 32]>) -> Filter { + pub fn authors(self, authors: Vec<&[u8; 32]>) -> Filter { self.start_authors_field(); for author in authors { self.add_id_element(author); @@ -170,8 +172,12 @@ impl Filter { } } +/* +// This is unsafe.. but we still need a way to free the memory on these impl Drop for Filter { fn drop(&mut self) { + debug!("dropping filter {:?}", self); unsafe { bindings::ndb_filter_destroy(self.as_mut_ptr()) }; } } +*/ diff --git a/src/ndb.rs b/src/ndb.rs @@ -81,7 +81,7 @@ impl Ndb { Ok(()) } - pub fn subscribe<'a>(&self, filter: &'a Filter) -> Result<Subscription<'a>> { + pub fn subscribe(&self, filter: Filter) -> Result<Subscription> { unsafe { let id = bindings::ndb_subscribe(self.as_ptr(), filter.as_mut_ptr(), 1); if id == 0 { @@ -92,7 +92,7 @@ impl Ndb { } } - pub fn poll_for_notes<'a>(&self, sub: &Subscription<'a>, max_notes: u32) -> Vec<u64> { + pub fn poll_for_notes(&self, sub: &Subscription, max_notes: u32) -> Vec<u64> { let mut vec = vec![]; vec.reserve_exact(max_notes as usize); let sub_id = sub.id; @@ -110,11 +110,7 @@ impl Ndb { vec } - pub async fn wait_for_notes<'a>( - &self, - sub: &Subscription<'a>, - max_notes: u32, - ) -> Result<Vec<u64>> { + pub async fn wait_for_notes(&self, sub: &Subscription, max_notes: u32) -> Result<Vec<u64>> { let ndb = self.clone(); let sub_id = sub.id; let handle = task::spawn_blocking(move || { @@ -256,11 +252,12 @@ mod tests { async fn subscribe_event_works() { let db = "target/testdbs/subscribe"; test_util::cleanup_db(&db); + tracing_subscriber::fmt::init(); { let ndb = Ndb::new(db, &Config::new()).expect("ndb"); let filter = Filter::new().kinds(vec![1]); - let sub = ndb.subscribe(&filter).expect("sub_id"); + let sub = ndb.subscribe(filter).expect("sub_id"); let waiter = ndb.wait_for_notes(&sub, 1); ndb.process_event(r#"["EVENT","b",{"id": "702555e52e82cc24ad517ba78c21879f6e47a7c0692b9b20df147916ae8731a3","pubkey": "32bf915904bfde2d136ba45dde32c88f4aca863783999faea2e847a8fafd2f15","created_at": 1702675561,"kind": 1,"tags": [],"content": "hello, world","sig": "2275c5f5417abfd644b7bc74f0388d70feb5d08b6f90fa18655dda5c95d013bfbc5258ea77c05b7e40e0ee51d8a2efa931dc7a0ec1db4c0a94519762c6625675"}]"#).expect("process ok"); let res = waiter.await.expect("await ok"); @@ -276,7 +273,7 @@ mod tests { { let ndb = Ndb::new(db, &Config::new()).expect("ndb"); let filter = Filter::new().kinds(vec![1]); - let sub = ndb.subscribe(&filter).expect("sub_id"); + let sub = ndb.subscribe(filter).expect("sub_id"); ndb.process_event(r#"["EVENT","b",{"id": "702555e52e82cc24ad517ba78c21879f6e47a7c0692b9b20df147916ae8731a3","pubkey": "32bf915904bfde2d136ba45dde32c88f4aca863783999faea2e847a8fafd2f15","created_at": 1702675561,"kind": 1,"tags": [],"content": "hello, world","sig": "2275c5f5417abfd644b7bc74f0388d70feb5d08b6f90fa18655dda5c95d013bfbc5258ea77c05b7e40e0ee51d8a2efa931dc7a0ec1db4c0a94519762c6625675"}]"#).expect("process ok"); // this is too fast, we should have nothing let res = ndb.poll_for_notes(&sub, 1); diff --git a/src/subscription.rs b/src/subscription.rs @@ -1,6 +1,6 @@ use crate::Filter; -pub struct Subscription<'a> { - pub filter: &'a Filter, +pub struct Subscription { + pub filter: Filter, pub id: u64, }