diff --git a/crates/nostr-sdk-db/Cargo.toml b/crates/nostr-sdk-db/Cargo.toml index 67915a985..d400b09f9 100644 --- a/crates/nostr-sdk-db/Cargo.toml +++ b/crates/nostr-sdk-db/Cargo.toml @@ -17,3 +17,6 @@ nostr = { version = "0.24", path = "../nostr", default-features = false, feature thiserror = { workspace = true } tokio = { workspace = true, features = ["sync"] } tracing = { workspace = true, features = ["std"] } + +[dev-dependencies] +tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] } diff --git a/crates/nostr-sdk-db/examples/memory.rs b/crates/nostr-sdk-db/examples/memory.rs new file mode 100644 index 000000000..2cfc06e63 --- /dev/null +++ b/crates/nostr-sdk-db/examples/memory.rs @@ -0,0 +1,51 @@ +// Copyright (c) 2022-2023 Yuki Kishimoto +// Distributed under the MIT software license + +use std::time::{Duration, Instant}; + +use nostr::{EventBuilder, Filter, Keys, Kind, Metadata, Tag}; +use nostr_sdk_db::memory::MemoryDatabase; +use nostr_sdk_db::NostrDatabase; + +#[tokio::main] +async fn main() { + let keys = Keys::generate(); + let database = MemoryDatabase::new(); + + for i in 0..50_000 { + let event = EventBuilder::new_text_note(format!("Event #{i}"), &[]) + .to_event(&keys) + .unwrap(); + database.save_event(&event).await.unwrap(); + + let event = EventBuilder::new_text_note( + format!("Reply to event #{i}"), + &[ + Tag::Event(event.id, None, None), + Tag::PubKey(event.pubkey, None), + ], + ) + .to_event(&keys) + .unwrap(); + database.save_event(&event).await.unwrap(); + } + + for i in 0..10 { + let metadata = Metadata::new().name(format!("Name #{i}")); + let event = EventBuilder::set_metadata(metadata) + .to_event(&keys) + .unwrap(); + database.save_event(&event).await.unwrap(); + tokio::time::sleep(Duration::from_secs(1)).await; + } + + let now = Instant::now(); + let events = database + .query(vec![Filter::new() + .kind(Kind::Metadata) + .author(keys.public_key().to_string())]) + .await + .unwrap(); + println!("{events:?}"); + println!("Time: {} ns", now.elapsed().as_nanos()); +}