nostrdb-rs

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

commit b01a0fbffead3acffdb23ef6e478f3ffa894a856
parent be8969a1c2563f24cecaf6ec2c2b253c9697a577
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  2 Sep 2024 14:19:48 -0700

api: pass arrays instead of vecs in a few places

passing vecs requires api consumers to clone more then they need to

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

Diffstat:
Msrc/filter.rs | 2+-
Msrc/ndb.rs | 34+++++++++++++++++-----------------
Msrc/subscription.rs | 15++++++++++-----
Msrc/tags.rs | 4++--
Msrc/util/nip10.rs | 30+++++++++++++++---------------
5 files changed, 45 insertions(+), 40 deletions(-)

diff --git a/src/filter.rs b/src/filter.rs @@ -1102,7 +1102,7 @@ mod tests { let mut filter = Filter::new().ids([&id, &id, &id]).build(); // mutate - filter = filter.since(3); + filter = filter.since_mut(3); for element in &filter { if let FilterField::Since(s) = element { diff --git a/src/ndb.rs b/src/ndb.rs @@ -84,7 +84,7 @@ impl Ndb { pub fn query<'a>( &self, txn: &'a Transaction, - filters: Vec<Filter>, + filters: &[Filter], max_results: i32, ) -> Result<Vec<QueryResult<'a>>> { let mut ndb_filters: Vec<bindings::ndb_filter> = filters.iter().map(|a| a.data).collect(); @@ -115,8 +115,8 @@ impl Ndb { unsafe { bindings::ndb_num_subscriptions(self.as_ptr()) as u32 } } - pub fn unsubscribe(&self, sub_id: u64) -> Result<()> { - let r = unsafe { bindings::ndb_unsubscribe(self.as_ptr(), sub_id) }; + pub fn unsubscribe(&self, sub: Subscription) -> Result<()> { + let r = unsafe { bindings::ndb_unsubscribe(self.as_ptr(), sub.id()) }; if r == 0 { Err(Error::SubscriptionError) @@ -125,7 +125,7 @@ impl Ndb { } } - pub fn subscribe(&self, filters: Vec<Filter>) -> Result<Subscription> { + pub fn subscribe(&self, filters: &[Filter]) -> Result<Subscription> { unsafe { let mut ndb_filters: Vec<bindings::ndb_filter> = filters.iter().map(|a| a.data).collect(); @@ -137,19 +137,19 @@ impl Ndb { if id == 0 { Err(Error::SubscriptionError) } else { - Ok(Subscription { filters, id }) + Ok(Subscription::new(id)) } } } - pub fn poll_for_notes(&self, sub_id: u64, max_notes: u32) -> Vec<NoteKey> { + pub fn poll_for_notes(&self, sub: Subscription, max_notes: u32) -> Vec<NoteKey> { let mut vec = vec![]; vec.reserve_exact(max_notes as usize); unsafe { let res = bindings::ndb_poll_for_notes( self.as_ptr(), - sub_id, + sub.id(), vec.as_mut_ptr(), max_notes as c_int, ); @@ -159,7 +159,7 @@ impl Ndb { vec.into_iter().map(NoteKey::new).collect() } - pub async fn wait_for_notes(&self, sub_id: u64, max_notes: u32) -> Result<Vec<NoteKey>> { + pub async fn wait_for_notes(&self, sub_id: Subscription, max_notes: u32) -> Result<Vec<NoteKey>> { let ndb = self.clone(); let handle = task::spawn_blocking(move || { let mut vec: Vec<u64> = vec![]; @@ -167,7 +167,7 @@ impl Ndb { let res = unsafe { bindings::ndb_wait_for_notes( ndb.as_ptr(), - sub_id, + sub_id.id(), vec.as_mut_ptr(), max_notes as c_int, ) @@ -367,13 +367,13 @@ mod tests { let filter = Filter::new().kinds(vec![1]).build(); let filters = vec![filter]; - let sub = ndb.subscribe(filters.clone()).expect("sub_id"); - let waiter = ndb.wait_for_notes(sub.id, 1); + let sub = ndb.subscribe(&filters).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"); assert_eq!(res, vec![NoteKey::new(1)]); let txn = Transaction::new(&ndb).expect("txn"); - let res = ndb.query(&txn, filters, 1).expect("query ok"); + let res = ndb.query(&txn, &filters, 1).expect("query ok"); assert_eq!(res.len(), 1); assert_eq!( hex::encode(res[0].note.id()), @@ -392,8 +392,8 @@ mod tests { let filter = Filter::new().kinds(vec![1]).build(); - let sub = ndb.subscribe(vec![filter]).expect("sub_id"); - let waiter = ndb.wait_for_notes(sub.id, 1); + 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"); assert_eq!(res, vec![NoteKey::new(1)]); @@ -410,15 +410,15 @@ mod tests { let filter = Filter::new().kinds(vec![1]).build(); - let sub = ndb.subscribe(vec![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.id, 1); + let res = ndb.poll_for_notes(sub, 1); assert_eq!(res, vec![]); std::thread::sleep(std::time::Duration::from_millis(100)); // now we should have something - let res = ndb.poll_for_notes(sub.id, 1); + let res = ndb.poll_for_notes(sub, 1); assert_eq!(res, vec![NoteKey::new(1)]); } } diff --git a/src/subscription.rs b/src/subscription.rs @@ -1,7 +1,12 @@ -use crate::Filter; -#[derive(Debug, Clone)] -pub struct Subscription { - pub filters: Vec<Filter>, - pub id: u64, +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub struct Subscription(u64); + +impl Subscription { + pub fn new(id: u64) -> Self { + Self(id) + } + pub fn id(self) -> u64 { + self.0 + } } diff --git a/src/tags.rs b/src/tags.rs @@ -188,7 +188,7 @@ mod tests { { let ndb = Ndb::new(db, &Config::new()).expect("ndb"); let sub = ndb - .subscribe(vec![Filter::new() + .subscribe(&[Filter::new() .ids([&[ 0xc5, 0xd9, 0x8c, 0xbf, 0x4b, 0xcd, 0x81, 0x1e, 0x28, 0x66, 0x77, 0x0c, 0x3d, 0x38, 0x0c, 0x02, 0x84, 0xce, 0x1d, 0xaf, 0x3a, 0xe9, 0x98, 0x3d, @@ -196,7 +196,7 @@ mod tests { ]]) .build()]) .expect("sub"); - let waiter = ndb.wait_for_notes(sub.id, 1); + let waiter = ndb.wait_for_notes(sub, 1); ndb.process_event(r#"["EVENT","s",{"id": "c5d98cbf4bcd811e2866770c3d380c0284ce1daf3ae9983d22565cb066cf2a19","pubkey": "083727b7a6051673f399102dc48c229c0ec08186ecd7e54ad0e9116d38429c4f","created_at": 1712517119,"kind": 1,"tags": [["e","b9e548b4aa30fa4ce9edf552adaf458385716704994fbaa9e0aa0042a5a5e01e"],["p","140ee9ff21da6e6671f750a0a747c5a3487ee8835159c7ca863e867a1c537b4f"],["hi","3"]],"content": "hi","sig": "1eed792e4db69c2bde2f5be33a383ef8b17c6afd1411598d0c4618fbdf4dbcb9689354276a74614511907a45eec234e0786733e8a6fbb312e6abf153f15fd437"}]"#).expect("process ok"); let res = waiter.await.expect("await ok"); assert_eq!(res.len(), 1); diff --git a/src/util/nip10.rs b/src/util/nip10.rs @@ -278,8 +278,8 @@ mod test { .unwrap() .try_into() .unwrap(); - let sub = ndb.subscribe(vec![filter.clone()]).expect("sub_id"); - let waiter = ndb.wait_for_notes(sub.id, 1); + let sub = ndb.subscribe(&[filter.clone()]).expect("sub_id"); + let waiter = ndb.wait_for_notes(sub, 1); ndb.process_event(r#" [ @@ -302,7 +302,7 @@ mod test { let res = waiter.await.expect("await ok"); assert_eq!(res, vec![NoteKey::new(1)]); let txn = Transaction::new(&ndb).unwrap(); - let res = ndb.query(&txn, vec![filter], 1).expect("note"); + let res = ndb.query(&txn, &[filter], 1).expect("note"); let note_reply = NoteReply::new(res[0].note.tags()); assert_eq!(*note_reply.root.unwrap().id, root_id); @@ -332,8 +332,8 @@ mod test { .unwrap() .try_into() .unwrap(); - let sub = ndb.subscribe(vec![filter.clone()]).expect("sub_id"); - let waiter = ndb.wait_for_notes(sub.id, 1); + let sub = ndb.subscribe(&[filter.clone()]).expect("sub_id"); + let waiter = ndb.wait_for_notes(sub, 1); ndb.process_event(r#" [ @@ -362,7 +362,7 @@ mod test { let res = waiter.await.expect("await ok"); assert_eq!(res, vec![NoteKey::new(1)]); let txn = Transaction::new(&ndb).unwrap(); - let res = ndb.query(&txn, vec![filter], 1).expect("note"); + let res = ndb.query(&txn, &[filter], 1).expect("note"); let note_reply = NoteReply::new(res[0].note.tags()); assert_eq!(*note_reply.root.unwrap().id, root_id); @@ -390,8 +390,8 @@ mod test { .unwrap() .try_into() .unwrap(); - let sub = ndb.subscribe(vec![filter.clone()]).expect("sub_id"); - let waiter = ndb.wait_for_notes(sub.id, 1); + let sub = ndb.subscribe(&[filter.clone()]).expect("sub_id"); + let waiter = ndb.wait_for_notes(sub, 1); ndb.process_event(r#" [ @@ -424,7 +424,7 @@ mod test { let res = waiter.await.expect("await ok"); assert_eq!(res, vec![NoteKey::new(1)]); let txn = Transaction::new(&ndb).unwrap(); - let res = ndb.query(&txn, vec![filter], 1).expect("note"); + let res = ndb.query(&txn, &[filter], 1).expect("note"); let note_reply = NoteReply::new(res[0].note.tags()); assert_eq!(*note_reply.reply_to_root().unwrap().id, root_id); @@ -453,8 +453,8 @@ mod test { .unwrap() .try_into() .unwrap(); - let sub = ndb.subscribe(vec![filter.clone()]).expect("sub_id"); - let waiter = ndb.wait_for_notes(sub.id, 1); + let sub = ndb.subscribe(&[filter.clone()]).expect("sub_id"); + let waiter = ndb.wait_for_notes(sub, 1); ndb.process_event(r#" [ @@ -504,7 +504,7 @@ mod test { let res = waiter.await.expect("await ok"); assert_eq!(res, vec![NoteKey::new(1)]); let txn = Transaction::new(&ndb).unwrap(); - let res = ndb.query(&txn, vec![filter], 1).expect("note"); + let res = ndb.query(&txn, &[filter], 1).expect("note"); let note = &res[0].note; let note_reply = NoteReply::new(note.tags()); @@ -535,8 +535,8 @@ mod test { .unwrap() .try_into() .unwrap(); - let sub = ndb.subscribe(vec![filter.clone()]).expect("sub_id"); - let waiter = ndb.wait_for_notes(sub.id, 1); + let sub = ndb.subscribe(&[filter.clone()]).expect("sub_id"); + let waiter = ndb.wait_for_notes(sub, 1); ndb.process_event(r#" [ @@ -561,7 +561,7 @@ mod test { let res = waiter.await.expect("await ok"); assert_eq!(res, vec![NoteKey::new(1)]); let txn = Transaction::new(&ndb).unwrap(); - let res = ndb.query(&txn, vec![filter], 1).expect("note"); + let res = ndb.query(&txn, &[filter], 1).expect("note"); let note = &res[0].note; let note_reply = NoteReply::new(note.tags());