profile.rs (3654B)
1 use crate::ndb_profile::{ 2 root_as_ndb_profile_record, root_as_ndb_profile_record_unchecked, NdbProfileRecord, 3 }; 4 use crate::{Error, Result, Transaction}; 5 6 pub struct TransactionalProfileRecord<'a> { 7 pub record: NdbProfileRecord<'a>, 8 pub primary_key: ProfileKey, 9 pub transaction: &'a Transaction, 10 } 11 12 pub enum ProfileRecord<'a> { 13 Transactional(TransactionalProfileRecord<'a>), 14 Owned(NdbProfileRecord<'a>), 15 } 16 17 #[derive(Debug, Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)] 18 pub struct ProfileKey(u64); 19 20 impl ProfileKey { 21 pub fn as_u64(&self) -> u64 { 22 self.0 23 } 24 25 pub fn new(key: u64) -> Self { 26 ProfileKey(key) 27 } 28 } 29 30 impl<'a> ProfileRecord<'a> { 31 pub fn record(&self) -> NdbProfileRecord<'a> { 32 match self { 33 ProfileRecord::Transactional(tr) => tr.record, 34 ProfileRecord::Owned(r) => *r, 35 } 36 } 37 38 pub fn key(&self) -> Option<ProfileKey> { 39 match self { 40 ProfileRecord::Transactional(tr) => Some(tr.primary_key), 41 ProfileRecord::Owned(_) => None, 42 } 43 } 44 45 pub fn new_owned(root: &'a [u8]) -> Result<ProfileRecord<'a>> { 46 let record = root_as_ndb_profile_record(root).map_err(|_| Error::DecodeError)?; 47 Ok(ProfileRecord::Owned(record)) 48 } 49 50 pub(crate) fn new_transactional( 51 ptr: *mut ::std::os::raw::c_void, 52 len: usize, 53 primary_key: ProfileKey, 54 transaction: &'a Transaction, 55 ) -> ProfileRecord<'a> { 56 let record = unsafe { 57 let bytes = std::slice::from_raw_parts(ptr as *const u8, len); 58 root_as_ndb_profile_record_unchecked(bytes) 59 }; 60 ProfileRecord::Transactional(TransactionalProfileRecord { 61 record, 62 transaction, 63 primary_key, 64 }) 65 } 66 } 67 68 #[cfg(test)] 69 mod tests { 70 use super::*; 71 72 #[test] 73 fn profile_record_words() { 74 use crate::config::Config; 75 use crate::ndb::Ndb; 76 use crate::test_util; 77 78 let db = "target/testdbs/profile_record_works"; 79 80 { 81 let cfg = Config::new(); 82 let ndb = Ndb::new(&db, &cfg).unwrap(); 83 let _ = ndb.process_event(r#"["EVENT","nostril-query",{"content":"{\"nip05\":\"_@jb55.com\",\"website\":\"https://damus.io\",\"name\":\"jb55\",\"about\":\"I made damus, npubs and zaps. banned by apple & the ccp. my notes are not for sale.\",\"lud16\":\"jb55@sendsats.lol\",\"banner\":\"https://nostr.build/i/3d6f22d45d95ecc2c19b1acdec57aa15f2dba9c423b536e26fc62707c125f557.jpg\",\"display_name\":\"Will\",\"picture\":\"https://cdn.jb55.com/img/red-me.jpg\"}","created_at":1700855305,"id":"cad04d11f7fa9c36d57400baca198582dfeb94fa138366c4469e58da9ed60051","kind":0,"pubkey":"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245","sig":"7a15e379ff27318460172b4a1d55a13e064c5007d05d5a188e7f60e244a9ed08996cb7676058b88c7a91ae9488f8edc719bc966cb5bf1eb99be44cdb745f915f","tags":[]}]"#); 84 } 85 86 // Initialize ndb 87 { 88 let cfg = Config::new(); 89 let ndb = Ndb::new(&db, &cfg).expect("db open"); 90 let mut txn = Transaction::new(&ndb).expect("new txn"); 91 92 let pk = 93 hex::decode("32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245") 94 .expect("hex decode"); 95 let pr = ndb 96 .get_profile_by_pubkey(&mut txn, &pk.try_into().expect("bytes")) 97 .expect("profile record"); 98 99 let profile = pr.record().profile().unwrap(); 100 assert_eq!(Some("jb55"), profile.name()); 101 } 102 103 test_util::cleanup_db(db); 104 } 105 }