nostrdb-rs

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

commit c1262af50b4462590ae7687de5bc32d4ebddd9d8
parent 86c355b78f182955609c696bd047c60eb130ea5f
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 24 Apr 2024 10:36:40 -0700

profile: add ProfileKey

This is a clear representation of the profile record key to make it
clear that it's separate from the profile note key.

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

Diffstat:
Msrc/lib.rs | 2+-
Msrc/ndb.rs | 6+++---
Msrc/profile.rs | 24++++++++++++++++++++++--
3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs @@ -30,7 +30,7 @@ pub use ndb::Ndb; pub use ndb_profile::{NdbProfile, NdbProfileRecord}; pub use ndb_str::{NdbStr, NdbStrVariant}; pub use note::{Note, NoteKey}; -pub use profile::ProfileRecord; +pub use profile::{ProfileKey, ProfileRecord}; pub use query::QueryResult; pub use result::Result; pub use subscription::Subscription; diff --git a/src/ndb.rs b/src/ndb.rs @@ -3,8 +3,8 @@ use std::ffi::CString; use std::ptr; use crate::{ - bindings, Blocks, Config, Error, Filter, Note, NoteKey, ProfileRecord, QueryResult, Result, - Subscription, Transaction, + bindings, Blocks, Config, Error, Filter, Note, NoteKey, ProfileKey, ProfileRecord, QueryResult, + Result, Subscription, Transaction, }; use std::fs; use std::os::raw::c_int; @@ -204,7 +204,7 @@ impl Ndb { Ok(ProfileRecord::new_transactional( profile_record_ptr, len, - primkey, + ProfileKey::new(primkey), transaction, )) } diff --git a/src/profile.rs b/src/profile.rs @@ -5,7 +5,7 @@ use crate::{Error, Result, Transaction}; pub struct TransactionalProfileRecord<'a> { pub record: NdbProfileRecord<'a>, - pub primary_key: u64, + pub primary_key: ProfileKey, pub transaction: &'a Transaction, } @@ -14,6 +14,19 @@ pub enum ProfileRecord<'a> { Owned(NdbProfileRecord<'a>), } +#[derive(Debug, Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)] +pub struct ProfileKey(u64); + +impl ProfileKey { + pub fn as_u64(&self) -> u64 { + self.0 + } + + pub fn new(key: u64) -> Self { + ProfileKey(key) + } +} + impl<'a> ProfileRecord<'a> { pub fn record(&self) -> NdbProfileRecord<'a> { match self { @@ -22,6 +35,13 @@ impl<'a> ProfileRecord<'a> { } } + pub fn key(&self) -> Option<ProfileKey> { + match self { + ProfileRecord::Transactional(tr) => Some(tr.primary_key), + ProfileRecord::Owned(_) => None, + } + } + pub fn new_owned(root: &'a [u8]) -> Result<ProfileRecord<'a>> { let record = root_as_ndb_profile_record(root).map_err(|_| Error::DecodeError)?; Ok(ProfileRecord::Owned(record)) @@ -30,7 +50,7 @@ impl<'a> ProfileRecord<'a> { pub(crate) fn new_transactional( ptr: *mut ::std::os::raw::c_void, len: usize, - primary_key: u64, + primary_key: ProfileKey, transaction: &'a Transaction, ) -> ProfileRecord<'a> { let record = unsafe {