Skip to content

Commit

Permalink
database: handle event deletions in DatabaseIndexes::index_event
Browse files Browse the repository at this point in the history
Add `DatabaseIndexes::has_been_deleted`
Check if `EventId` was deleted in `NostrDatabase::has_event_already_been_saved`
  • Loading branch information
yukibtc committed Nov 29, 2023
1 parent 4771936 commit f01d341
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
21 changes: 21 additions & 0 deletions crates/nostr-database/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub struct EventIndexResult {
#[derive(Debug, Clone, Default)]
pub struct DatabaseIndexes {
index: Arc<RwLock<BTreeSet<EventIndex>>>,
deleted: Arc<RwLock<HashSet<EventId>>>,
}

impl DatabaseIndexes {
Expand All @@ -148,6 +149,8 @@ impl DatabaseIndexes {
}

/// Index [`Event`]
///
/// **This method assume that [`Event`] was already verified**
#[tracing::instrument(skip_all, level = "trace")]
pub async fn index_event(&self, event: &Event) -> EventIndexResult {
// Check if it's expired or ephemeral
Expand Down Expand Up @@ -186,6 +189,18 @@ impl DatabaseIndexes {
}
None => should_insert = false,
}
} else if event.kind == Kind::EventDeletion {
let mut deleted = self.deleted.write().await;
let ids = event.event_ids().copied();
let filter = Filter::new().ids(ids);
let pubkey_prefix: PublicKeyPrefix = PublicKeyPrefix::from(event.pubkey);
for ev in self.internal_query(&index, filter).await {
if ev.pubkey == pubkey_prefix {
to_discard.insert(ev.event_id);
deleted.insert(ev.event_id);
}
}
// TODO: support event deletion by coordinate (`a` tag)
}

// Remove events
Expand Down Expand Up @@ -253,6 +268,12 @@ impl DatabaseIndexes {
matching_ids.into_iter().map(|e| e.event_id).collect()
}

/// Check if an event was deleted
pub async fn has_been_deleted(&self, event_id: &EventId) -> bool {
let deleted = self.deleted.read().await;
deleted.contains(event_id)
}

/// Clear indexes
pub async fn clear(&self) {
let mut index = self.index.write().await;
Expand Down
4 changes: 3 additions & 1 deletion crates/nostr-database/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ impl NostrDatabase for MemoryDatabase {
}

async fn has_event_already_been_saved(&self, event_id: EventId) -> Result<bool, Self::Err> {
if self.opts.events {
if self.indexes.has_been_deleted(&event_id).await {
Ok(true)
} else if self.opts.events {
let events = self.events.read().await;
Ok(events.contains_key(&event_id))
} else {
Expand Down
16 changes: 10 additions & 6 deletions crates/nostr-indexeddb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,16 @@ impl_nostr_database!({
&self,
event_id: EventId,
) -> Result<bool, IndexedDBError> {
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())
if self.indexes.has_been_deleted(&event_id).await {
Ok(true)
} else {
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> {
Expand Down
8 changes: 6 additions & 2 deletions crates/nostr-rocksdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,12 @@ impl NostrDatabase for RocksDatabase {
}

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()))
if self.indexes.has_been_deleted(&event_id).await {
Ok(true)
} else {
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> {
Expand Down
30 changes: 17 additions & 13 deletions crates/nostr-sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,23 @@ impl NostrDatabase for SQLiteDatabase {
}

async fn has_event_already_been_saved(&self, event_id: EventId) -> Result<bool, Self::Err> {
let conn = self.acquire().await?;
conn.interact(move |conn| {
let mut stmt = conn.prepare_cached(
"SELECT EXISTS(SELECT 1 FROM events WHERE event_id = ? LIMIT 1);",
)?;
let mut rows = stmt.query([event_id.to_hex()])?;
let exists: u8 = match rows.next()? {
Some(row) => row.get(0)?,
None => 0,
};
Ok(exists == 1)
})
.await?
if self.indexes.has_been_deleted(&event_id).await {
Ok(true)
} else {
let conn = self.acquire().await?;
conn.interact(move |conn| {
let mut stmt = conn.prepare_cached(
"SELECT EXISTS(SELECT 1 FROM events WHERE event_id = ? LIMIT 1);",
)?;
let mut rows = stmt.query([event_id.to_hex()])?;
let exists: u8 = match rows.next()? {
Some(row) => row.get(0)?,
None => 0,
};
Ok(exists == 1)
})
.await?
}
}

async fn has_event_already_been_seen(&self, event_id: EventId) -> Result<bool, Self::Err> {
Expand Down

0 comments on commit f01d341

Please sign in to comment.