diff --git a/src/filter.rs b/src/filter.rs index 3da542f..bfd42a0 100644 --- a/src/filter.rs +++ b/src/filter.rs @@ -3,7 +3,6 @@ use crate::Note; use std::ffi::CString; use std::os::raw::c_char; use std::ptr::null_mut; -use tracing::debug; #[derive(Debug, Clone)] pub struct Filter { diff --git a/src/lib.rs b/src/lib.rs index 7418815..77913e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,7 +25,7 @@ pub use config::Config; pub use error::Error; pub use filter::Filter; pub use ndb::Ndb; -pub use note::Note; +pub use note::{Note, NoteKey}; pub use profile::ProfileRecord; pub use query::QueryResult; pub use result::Result; diff --git a/src/ndb.rs b/src/ndb.rs index 19dad6d..fee2ae0 100644 --- a/src/ndb.rs +++ b/src/ndb.rs @@ -3,7 +3,7 @@ use std::ffi::CString; use std::ptr; use crate::{ - bindings, Blocks, Config, Error, Filter, Note, ProfileRecord, QueryResult, Result, + bindings, Blocks, Config, Error, Filter, Note, NoteKey, ProfileRecord, QueryResult, Result, Subscription, Transaction, }; use std::fs; @@ -235,6 +235,31 @@ impl Ndb { Ok(Blocks::new_transactional(blocks_ptr, txn)) } + pub fn get_note_by_key<'a>( + &self, + transaction: &'a Transaction, + note_key: NoteKey, + ) -> Result> { + let mut len: usize = 0; + + let note_ptr = unsafe { + bindings::ndb_get_note_by_key(transaction.as_mut_ptr(), note_key.as_u64(), &mut len) + }; + + if note_ptr.is_null() { + // Handle null pointer (e.g., note not found or error occurred) + return Err(Error::NotFound); + } + + // Convert the raw pointer to a Note instance + Ok(Note::new_transactional( + note_ptr, + len, + note_key, + transaction, + )) + } + /// Get a note from the database. Takes a [Transaction] and a 32-byte [Note] Id pub fn get_note_by_id<'a>( &self, @@ -246,7 +271,7 @@ impl Ndb { let note_ptr = unsafe { bindings::ndb_get_note_by_id( - transaction.as_ptr() as *mut bindings::ndb_txn, + transaction.as_mut_ptr(), id.as_ptr(), &mut len, &mut primkey, @@ -259,7 +284,12 @@ impl Ndb { } // Convert the raw pointer to a Note instance - Ok(Note::new_transactional(note_ptr, len, primkey, transaction)) + Ok(Note::new_transactional( + note_ptr, + len, + NoteKey::new(primkey), + transaction, + )) } /// Get the underlying pointer to the context in C diff --git a/src/note.rs b/src/note.rs index be970f0..6e7d7db 100644 --- a/src/note.rs +++ b/src/note.rs @@ -1,6 +1,19 @@ use crate::bindings; use crate::transaction::Transaction; +#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)] +pub struct NoteKey(u64); + +impl NoteKey { + pub fn as_u64(&self) -> u64 { + self.0 + } + + pub fn new(key: u64) -> Self { + NoteKey(key) + } +} + #[derive(Debug)] pub enum Note<'a> { /// A note in-memory outside of nostrdb. This note is a pointer to a note in @@ -19,7 +32,7 @@ pub enum Note<'a> { Transactional { ptr: *mut bindings::ndb_note, size: usize, - key: u64, + key: NoteKey, transaction: &'a Transaction, }, } @@ -42,7 +55,7 @@ impl<'a> Note<'a> { pub(crate) fn new_transactional( ptr: *mut bindings::ndb_note, size: usize, - key: u64, + key: NoteKey, transaction: &'a Transaction, ) -> Note<'a> { Note::Transactional { @@ -53,9 +66,9 @@ impl<'a> Note<'a> { } } - pub fn key(&self) -> Option { + pub fn key(&self) -> Option { match self { - Note::Transactional { key, .. } => Some(*key), + Note::Transactional { key, .. } => Some(NoteKey::new(key.as_u64())), _ => None, } } diff --git a/src/query.rs b/src/query.rs index 2fa24a6..490f368 100644 --- a/src/query.rs +++ b/src/query.rs @@ -1,10 +1,10 @@ -use crate::{bindings, Note, Transaction}; +use crate::{bindings, Note, NoteKey, Transaction}; #[derive(Debug)] pub struct QueryResult<'a> { pub note: Note<'a>, pub note_size: u64, - pub note_key: u64, + pub note_key: NoteKey, } impl<'a> QueryResult<'a> { @@ -13,11 +13,11 @@ impl<'a> QueryResult<'a> { note: Note::new_transactional( result.note, result.note_size as usize, - result.note_id, + NoteKey::new(result.note_id), txn, ), note_size: result.note_size, - note_key: result.note_id, + note_key: NoteKey::new(result.note_id), } } }