Skip to content

Commit

Permalink
db: add memory example
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Oct 20, 2023
1 parent 0940cdd commit fe4f33c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/nostr-sdk-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
51 changes: 51 additions & 0 deletions crates/nostr-sdk-db/examples/memory.rs
Original file line number Diff line number Diff line change
@@ -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());
}

0 comments on commit fe4f33c

Please sign in to comment.