Skip to content

Commit

Permalink
api: pass arrays instead of vecs in a few places
Browse files Browse the repository at this point in the history
passing vecs requires api consumers to clone more then they need to

Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Sep 2, 2024
1 parent be8969a commit b01a0fb
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 17 additions & 17 deletions src/ndb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -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();
Expand All @@ -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,
);
Expand All @@ -159,15 +159,15 @@ 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![];
vec.reserve_exact(max_notes as usize);
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,
)
Expand Down Expand Up @@ -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()),
Expand All @@ -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)]);
Expand All @@ -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)]);
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/subscription.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
4 changes: 2 additions & 2 deletions src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ 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,
0x22, 0x56, 0x5c, 0xb0, 0x66, 0xcf, 0x2a, 0x19,
]])
.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);
Expand Down
30 changes: 15 additions & 15 deletions src/util/nip10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
[
Expand All @@ -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);
Expand Down Expand Up @@ -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#"
[
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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#"
[
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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#"
[
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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#"
[
Expand All @@ -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());

Expand Down

0 comments on commit b01a0fb

Please sign in to comment.