Skip to content

Commit

Permalink
db: add NostrDatabase::has_event_already_been_saved method
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Nov 10, 2023
1 parent 85a227e commit 62c406a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
3 changes: 3 additions & 0 deletions crates/nostr-database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ pub trait NostrDatabase: AsyncTraitDeps {
/// Return `true` if event was successfully saved into database.
async fn save_event(&self, event: &Event) -> Result<bool, Self::Err>;

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

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

Expand Down
12 changes: 9 additions & 3 deletions crates/nostr-database/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ impl NostrDatabase for MemoryDatabase {
}

async fn save_event(&self, event: &Event) -> Result<bool, Self::Err> {
// Set event as seen
self.event_id_seen(event.id, None).await?;

if self.opts.events {
let EventIndexResult {
to_store,
Expand All @@ -113,6 +110,15 @@ impl NostrDatabase for MemoryDatabase {
}
}

async fn has_event_already_been_saved(&self, event_id: EventId) -> Result<bool, Self::Err> {
if self.opts.events {
let events = self.events.read().await;
Ok(events.contains_key(&event_id))
} else {
Ok(false)
}
}

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))
Expand Down
27 changes: 23 additions & 4 deletions crates/nostr-indexeddb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,12 @@ impl_nostr_database!({
}

async fn count(&self) -> Result<usize, IndexedDBError> {
Err(DatabaseError::NotSupported.into())
let tx = self
.db
.transaction_on_one_with_mode(EVENTS_CF, IdbTransactionMode::Readonly)?;
let store = tx.object_store(EVENTS_CF)?;
let count: u32 = store.count()?.await?;
Ok(count as usize)
}

#[tracing::instrument(skip_all, level = "trace")]
Expand Down Expand Up @@ -282,11 +287,25 @@ impl_nostr_database!({
}
}

async fn has_event_already_been_seen(
async fn has_event_already_been_saved(
&self,
_event_id: EventId,
event_id: EventId,
) -> Result<bool, IndexedDBError> {
todo!()
let tx = self
.db
.transaction_on_one_with_mode(EVENTS_CF, IdbTransactionMode::Readonly)?;
let store = tx.object_store(EVENTS_CF)?;
let key = JsValue::from(event_id.to_hex());
Ok(store.get(&key)?.await?.is_some())
}

async fn has_event_already_been_seen(&self, event_id: EventId) -> Result<bool, IndexedDBError> {
let tx = self
.db
.transaction_on_one_with_mode(EVENTS_SEEN_BY_RELAYS_CF, IdbTransactionMode::Readonly)?;
let store = tx.object_store(EVENTS_SEEN_BY_RELAYS_CF)?;
let key = JsValue::from(event_id.to_hex());
Ok(store.get(&key)?.await?.is_some())
}

async fn event_id_seen(
Expand Down
7 changes: 6 additions & 1 deletion crates/nostr-rocksdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,16 @@ impl NostrDatabase for RocksDatabase {
}
}

async fn has_event_already_been_seen(&self, event_id: EventId) -> Result<bool, Self::Err> {
async fn has_event_already_been_saved(&self, event_id: EventId) -> Result<bool, Self::Err> {
let cf = self.cf_handle(EVENTS_CF)?;
Ok(self.db.key_may_exist_cf(&cf, event_id.as_bytes()))
}

async fn has_event_already_been_seen(&self, event_id: EventId) -> Result<bool, Self::Err> {
let cf = self.cf_handle(EVENTS_SEEN_BY_RELAYS_CF)?;
Ok(self.db.key_may_exist_cf(&cf, event_id.as_bytes()))
}

async fn event_id_seen(
&self,
event_id: EventId,
Expand Down

0 comments on commit 62c406a

Please sign in to comment.