diff --git a/crates/nostr-sdk-db/examples/memory.rs b/crates/nostr-sdk-db/examples/memory.rs index 711de3ac3..12bd92bba 100644 --- a/crates/nostr-sdk-db/examples/memory.rs +++ b/crates/nostr-sdk-db/examples/memory.rs @@ -5,12 +5,13 @@ use std::time::{Duration, Instant}; use nostr::{EventBuilder, Filter, Keys, Kind, Metadata, Tag}; use nostr_sdk_db::memory::MemoryDatabase; -use nostr_sdk_db::NostrDatabase; +use nostr_sdk_db::{DatabaseOptions, NostrDatabase}; #[tokio::main] async fn main() { let keys = Keys::generate(); - let database = MemoryDatabase::new(true); + let opts = DatabaseOptions::default(); + let database = MemoryDatabase::new(opts); for i in 0..50_000 { let event = EventBuilder::new_text_note(format!("Event #{i}"), &[]) diff --git a/crates/nostr-sdk-db/src/lib.rs b/crates/nostr-sdk-db/src/lib.rs index 398b004d3..4744169c2 100644 --- a/crates/nostr-sdk-db/src/lib.rs +++ b/crates/nostr-sdk-db/src/lib.rs @@ -13,8 +13,10 @@ use nostr::{Event, EventId, Filter, Url}; mod error; pub mod memory; +mod options; pub use self::error::DatabaseError; +pub use self::options::DatabaseOptions; /// Backend pub enum Backend { @@ -44,6 +46,9 @@ pub trait NostrDatabase: AsyncTraitDeps { /// Name of the backend database used (ex. rocksdb, lmdb, sqlite, indexeddb, ...) fn backend(&self) -> Backend; + /// Database options + fn opts(&self) -> DatabaseOptions; + /// Save [`Event`] into store /// /// Return `true` if event was successfully saved into database. diff --git a/crates/nostr-sdk-db/src/memory.rs b/crates/nostr-sdk-db/src/memory.rs index b5f7d0ac3..32034f72f 100644 --- a/crates/nostr-sdk-db/src/memory.rs +++ b/crates/nostr-sdk-db/src/memory.rs @@ -11,7 +11,7 @@ use nostr::{Event, EventId, Filter, FiltersMatchEvent, Url}; use thiserror::Error; use tokio::sync::RwLock; -use crate::{Backend, DatabaseError, NostrDatabase}; +use crate::{Backend, DatabaseError, DatabaseOptions, NostrDatabase}; /// Memory Database Error #[derive(Debug, Error)] @@ -26,7 +26,7 @@ impl From for DatabaseError { /// Memory Database (RAM) #[derive(Debug)] pub struct MemoryDatabase { - store_events: bool, + opts: DatabaseOptions, seen_event_ids: Arc>>>, events: Arc>>, // TODO: add messages queue? (messages not sent) @@ -34,18 +34,15 @@ pub struct MemoryDatabase { impl Default for MemoryDatabase { fn default() -> Self { - Self::new(false) + Self::new(DatabaseOptions { events: false }) } } impl MemoryDatabase { /// New Memory database - /// - /// If `store_events` arg is set to `true`, the seen events will be stored in memory (a lot of it could be used). - /// If it's set to `false`, only the [`EventId`] will be stored (instead of the full [`Event`]) - pub fn new(store_events: bool) -> Self { + pub fn new(opts: DatabaseOptions) -> Self { Self { - store_events, + opts, seen_event_ids: Arc::new(RwLock::new(HashMap::new())), events: Arc::new(RwLock::new(HashMap::new())), } @@ -95,7 +92,7 @@ impl MemoryDatabase { ) -> Result { self.event_id_seen(event.id, None).await?; - if self.store_events { + if self.opts.events { if event.is_expired() || event.is_ephemeral() { tracing::warn!("Event {} not saved: expired or ephemeral", event.id); return Ok(false); @@ -156,6 +153,10 @@ impl NostrDatabase for MemoryDatabase { Backend::Memory } + fn opts(&self) -> DatabaseOptions { + self.opts + } + async fn save_event(&self, event: &Event) -> Result { let mut events = self.events.write().await; self._save_event(&mut events, event.clone()).await @@ -198,7 +199,7 @@ impl NostrDatabase for MemoryDatabase { } async fn event_by_id(&self, event_id: EventId) -> Result { - if self.store_events { + if self.opts.events { let events = self.events.read().await; events .get(&event_id) @@ -210,7 +211,7 @@ impl NostrDatabase for MemoryDatabase { } async fn query(&self, filters: Vec) -> Result, Self::Err> { - if self.store_events { + if self.opts.events { let events = self.events.read().await; self._query(&events, filters).await } else { @@ -219,7 +220,7 @@ impl NostrDatabase for MemoryDatabase { } async fn event_ids_by_filters(&self, filters: Vec) -> Result, Self::Err> { - if self.store_events { + if self.opts.events { let events = self.events.read().await; let mut list: Vec = Vec::new(); for event in events.values() { diff --git a/crates/nostr-sdk-db/src/options.rs b/crates/nostr-sdk-db/src/options.rs new file mode 100644 index 000000000..038682de2 --- /dev/null +++ b/crates/nostr-sdk-db/src/options.rs @@ -0,0 +1,24 @@ +// Copyright (c) 2022-2023 Yuki Kishimoto +// Distributed under the MIT software license + +//! Database options + +/// Database options +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct DatabaseOptions { + /// Store events (?) + pub events: bool, +} + +impl Default for DatabaseOptions { + fn default() -> Self { + Self { events: true } + } +} + +impl DatabaseOptions { + /// New default database options + pub fn new() -> Self { + Self::default() + } +}