Skip to content

Commit 0252dd0

Browse files
committed
database: improve performance
* Remove `rayon` (causes very slow queries in some cases) * Add indexes for IDs, replaceable and param. replaceable events * Add support to query result order (ASC, DESC) * Refactoring * Improve unit tests
1 parent 5616542 commit 0252dd0

File tree

24 files changed

+720
-402
lines changed

24 files changed

+720
-402
lines changed

Cargo.lock

Lines changed: 0 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/nostr-sdk-ffi/src/database.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::ops::Deref;
66
use std::sync::Arc;
77

88
use nostr_ffi::{Event, EventId, Filter, PublicKey};
9-
use nostr_sdk::database::{DynNostrDatabase, IntoNostrDatabase, NostrDatabaseExt};
9+
use nostr_sdk::database::{DynNostrDatabase, IntoNostrDatabase, NostrDatabaseExt, Order};
1010
use nostr_sdk::{block_on, SQLiteDatabase};
1111
use uniffi::Object;
1212

@@ -78,7 +78,7 @@ impl NostrDatabase {
7878
.collect();
7979
Ok(self
8080
.inner
81-
.query(filters)
81+
.query(filters, Order::Desc)
8282
.await?
8383
.into_iter()
8484
.map(|e| Arc::new(e.into()))

bindings/nostr-sdk-js/src/database.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use nostr_js::error::{into_err, Result};
99
use nostr_js::event::{JsEvent, JsEventArray, JsEventId};
1010
use nostr_js::key::JsPublicKey;
1111
use nostr_js::message::JsFilter;
12-
use nostr_sdk::database::{DynNostrDatabase, IntoNostrDatabase, NostrDatabaseExt};
12+
use nostr_sdk::database::{DynNostrDatabase, IntoNostrDatabase, NostrDatabaseExt, Order};
1313
use nostr_sdk::WebDatabase;
1414
use wasm_bindgen::prelude::*;
1515

@@ -94,7 +94,7 @@ impl JsNostrDatabase {
9494
let filters = filters.into_iter().map(|f| f.inner()).collect();
9595
Ok(self
9696
.inner
97-
.query(filters)
97+
.query(filters, Order::Desc)
9898
.await
9999
.map_err(into_err)?
100100
.into_iter()

clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
too-many-arguments-threshold = 13

crates/nostr-database/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ flatbuf = ["dep:flatbuffers"]
2020
async-trait.workspace = true
2121
flatbuffers = { version = "23.5", optional = true }
2222
nostr = { workspace = true, features = ["std"] }
23-
rayon = "1.8"
2423
thiserror.workspace = true
2524
tokio = { workspace = true, features = ["sync"] }
2625
tracing = { workspace = true, features = ["std", "attributes"] }

crates/nostr-database/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
all: build
2-
31
flatbuf:
42
flatc --rust -o ./src/flatbuffers ./fbs/event.fbs
53
flatc --rust -o ./src/flatbuffers ./fbs/event_seen_by.fbs

crates/nostr-database/examples/indexes.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Distributed under the MIT software license
44

55
use nostr::prelude::*;
6-
use nostr_database::DatabaseIndexes;
6+
use nostr_database::{DatabaseIndexes, Order};
77
use tracing_subscriber::fmt::format::FmtSpan;
88

99
#[tokio::main]
@@ -59,12 +59,15 @@ async fn main() {
5959
}
6060

6161
let ids = index
62-
.query(vec![Filter::new()
63-
.kinds(vec![Kind::Metadata, Kind::Custom(123), Kind::TextNote])
64-
.limit(20)
65-
//.kind(Kind::Custom(123))
66-
//.identifier("myid5000")
67-
.author(keys_a.public_key())])
62+
.query(
63+
vec![Filter::new()
64+
.kinds(vec![Kind::Metadata, Kind::Custom(123), Kind::TextNote])
65+
.limit(20)
66+
//.kind(Kind::Custom(123))
67+
//.identifier("myid5000")
68+
.author(keys_a.public_key())],
69+
Order::Desc,
70+
)
6871
.await;
6972
println!("Got {} ids", ids.len());
7073

crates/nostr-database/examples/memory.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use nostr::prelude::*;
66
use nostr::{EventBuilder, Filter, Keys, Kind, Metadata, Tag};
77
use nostr_database::memory::MemoryDatabase;
8-
use nostr_database::{DatabaseOptions, NostrDatabase};
8+
use nostr_database::{DatabaseOptions, NostrDatabase, Order};
99
use tracing_subscriber::fmt::format::FmtSpan;
1010

1111
#[tokio::main]
@@ -63,12 +63,15 @@ async fn main() {
6363
}
6464

6565
let events = database
66-
.query(vec![Filter::new()
67-
.kinds(vec![Kind::Metadata, Kind::Custom(123), Kind::TextNote])
68-
.limit(20)
69-
//.kind(Kind::Custom(123))
70-
//.identifier("myid5000")
71-
.author(keys_a.public_key())])
66+
.query(
67+
vec![Filter::new()
68+
.kinds(vec![Kind::Metadata, Kind::Custom(123), Kind::TextNote])
69+
.limit(20)
70+
//.kind(Kind::Custom(123))
71+
//.identifier("myid5000")
72+
.author(keys_a.public_key())],
73+
Order::Desc,
74+
)
7275
.await
7376
.unwrap();
7477
println!("Got {} events", events.len());

0 commit comments

Comments
 (0)