Skip to content

Commit

Permalink
add get_note_by_key
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Feb 8, 2024
1 parent 1a09eed commit 8de35b0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 33 additions & 3 deletions src/ndb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Note<'a>> {
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,
Expand All @@ -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,
Expand All @@ -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
Expand Down
21 changes: 17 additions & 4 deletions src/note.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,7 +32,7 @@ pub enum Note<'a> {
Transactional {
ptr: *mut bindings::ndb_note,
size: usize,
key: u64,
key: NoteKey,
transaction: &'a Transaction,
},
}
Expand All @@ -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 {
Expand All @@ -53,9 +66,9 @@ impl<'a> Note<'a> {
}
}

pub fn key(&self) -> Option<u64> {
pub fn key(&self) -> Option<NoteKey> {
match self {
Note::Transactional { key, .. } => Some(*key),
Note::Transactional { key, .. } => Some(NoteKey::new(key.as_u64())),
_ => None,
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/query.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand All @@ -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),
}
}
}

0 comments on commit 8de35b0

Please sign in to comment.