Skip to content

Commit

Permalink
db: add save_events and rename event_id_already_seen and `save_ev…
Browse files Browse the repository at this point in the history
…ent_id_seen_by_relay` methods
  • Loading branch information
yukibtc committed Oct 12, 2023
1 parent 040cf3b commit f24f19d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 26 deletions.
3 changes: 3 additions & 0 deletions crates/nostr-sdk-db/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub enum DatabaseError {
/// An error happened in the underlying database backend.
#[error(transparent)]
Backend(Box<dyn std::error::Error + Send + Sync>),
/// Not supported
#[error("method not supported by current backend")]
NotSupported,
}

impl DatabaseError {
Expand Down
32 changes: 24 additions & 8 deletions crates/nostr-sdk-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#![warn(missing_docs)]
#![warn(rustdoc::bare_urls)]

use std::collections::HashSet;

use async_trait::async_trait;
use nostr::{Event, EventId, Filter, Url};

Expand Down Expand Up @@ -45,21 +47,35 @@ pub trait NostrDatabase: AsyncTraitDeps {
/// Save [`Event`] into store
async fn save_event(&self, event: &Event) -> Result<(), Self::Err>;

/// Check if [`EventId`] was already seen
async fn event_id_already_seen(&self, event_id: EventId) -> Result<bool, Self::Err>;
/// Save multiple [`Event`] into store
async fn save_events(&self, events: Vec<Event>) -> Result<(), Self::Err>;

/// Check if [`EventId`] has already been seen
async fn has_event_already_been_seen(&self, event_id: EventId) -> Result<bool, Self::Err>;

/// Save [`EventId`] seen by relay
/// Set [`EventId`] as seen
///
/// Useful for NIP65 (gossip)
async fn save_event_id_seen_by_relay(
/// Optionally, save also the relay url where the event has been seen (useful for NIP65, aka gossip)
async fn event_id_seen(
&self,
event_id: EventId,
relay_url: Url,
relay_url: Option<Url>,
) -> Result<(), Self::Err>;

/// Set multiple [`EventId`] as seen
///
/// Optionally, save also the relay url where the event has been seen (useful for NIP65, aka gossip)
async fn event_ids_seen(
&self,
event_ids: Vec<EventId>,
relay_url: Option<Url>,
) -> Result<(), Self::Err>;

/// Get list of relays that have seen the [`EventId`]
async fn event_recently_seen_on_relays(&self, event_id: EventId)
-> Result<Vec<Url>, Self::Err>;
async fn event_recently_seen_on_relays(
&self,
event_id: EventId,
) -> Result<Option<HashSet<Url>>, Self::Err>;

/// Query store with filters
async fn query(&self, filters: Vec<Filter>) -> Result<Vec<Event>, Self::Err>;
Expand Down
68 changes: 50 additions & 18 deletions crates/nostr-sdk-db/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ impl MemoryDatabase {
pub fn new() -> Self {
Self::default()
}

fn _event_id_seen(
&self,
seen_event_ids: &mut HashMap<EventId, HashSet<Url>>,
event_id: EventId,
relay_url: Option<Url>,
) {
seen_event_ids
.entry(event_id)
.and_modify(|set| {
if let Some(relay_url) = &relay_url {
set.insert(relay_url.clone());
}
})
.or_insert_with(|| match relay_url {
Some(relay_url) => {
let mut set = HashSet::with_capacity(1);
set.insert(relay_url);
set
}
None => HashSet::with_capacity(0),
});
}
}

#[async_trait]
Expand All @@ -48,42 +71,51 @@ impl NostrDatabase for MemoryDatabase {
Ok(())
}

async fn event_id_already_seen(&self, event_id: EventId) -> Result<bool, Self::Err> {
async fn save_events(&self, _events: Vec<Event>) -> Result<(), Self::Err> {
Ok(())
}

async fn has_event_already_been_seen(&self, event_id: EventId) -> Result<bool, Self::Err> {
let seen_event_ids = self.seen_event_ids.read().await;
Ok(seen_event_ids.contains_key(&event_id))
}

async fn save_event_id_seen_by_relay(
async fn event_id_seen(
&self,
event_id: EventId,
relay_url: Url,
relay_url: Option<Url>,
) -> Result<(), Self::Err> {
let mut seen_event_ids = self.seen_event_ids.write().await;
seen_event_ids
.entry(event_id)
.and_modify(|set| {
set.insert(relay_url.clone());
})
.or_insert_with(|| {
let mut set = HashSet::with_capacity(1);
set.insert(relay_url);
set
});
self._event_id_seen(&mut seen_event_ids, event_id, relay_url);
Ok(())
}

async fn event_ids_seen(
&self,
event_ids: Vec<EventId>,
relay_url: Option<Url>,
) -> Result<(), Self::Err> {
let mut seen_event_ids = self.seen_event_ids.write().await;
for event_id in event_ids.into_iter() {
self._event_id_seen(&mut seen_event_ids, event_id, relay_url.clone());
}

Ok(())
}

async fn event_recently_seen_on_relays(
&self,
_event_id: EventId,
) -> Result<Vec<Url>, Self::Err> {
todo!()
event_id: EventId,
) -> Result<Option<HashSet<Url>>, Self::Err> {
let seen_event_ids = self.seen_event_ids.read().await;
Ok(seen_event_ids.get(&event_id).cloned())
}

async fn query(&self, _filters: Vec<Filter>) -> Result<Vec<Event>, Self::Err> {
Ok(Vec::new())
Err(DatabaseError::NotSupported)
}

async fn event_ids_by_filters(&self, _filters: Vec<Filter>) -> Result<Vec<EventId>, Self::Err> {
Ok(Vec::new())
Err(DatabaseError::NotSupported)
}
}

0 comments on commit f24f19d

Please sign in to comment.