From 618b25ebec4ba34325c85a4b3498d63bcfba5cf9 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Sat, 3 Feb 2024 03:56:45 +1300 Subject: [PATCH] storage: Switch interface to indexing functions --- gossip-lib/src/storage/events2.rs | 19 +++++- gossip-lib/src/storage/mod.rs | 63 ++++++++++--------- .../src/storage/unindexed_giftwraps1.rs | 9 ++- 3 files changed, 57 insertions(+), 34 deletions(-) diff --git a/gossip-lib/src/storage/events2.rs b/gossip-lib/src/storage/events2.rs index 11c7fc0de..8d6bb6f65 100644 --- a/gossip-lib/src/storage/events2.rs +++ b/gossip-lib/src/storage/events2.rs @@ -55,10 +55,23 @@ impl Storage { let f = |txn: &mut RwTxn<'a>| -> Result<(), Error> { self.db_events2()?.put(txn, event.id.as_slice(), &bytes)?; + // If giftwrap, index the inner rumor instead + let mut eventptr: &EventV2 = event; + let rumor: EventV2; + if let Some(r) = self.switch_to_rumor(event, txn)? { + rumor = r; + eventptr = &rumor; + } // also index the event - self.write_event_ek_pk_index(event, Some(txn))?; - self.write_event_ek_c_index(event, Some(txn))?; - self.write_event_tag_index(event, Some(txn))?; + self.write_event_ek_pk_index(eventptr.id, eventptr.kind, eventptr.pubkey, Some(txn))?; + self.write_event_ek_c_index( + eventptr.id, + eventptr.kind, + eventptr.created_at, + Some(txn), + )?; + self.write_event_tag_index(eventptr, Some(txn))?; + for hashtag in event.hashtags() { if hashtag.is_empty() { continue; diff --git a/gossip-lib/src/storage/mod.rs b/gossip-lib/src/storage/mod.rs index 5737fab79..aca31986a 100644 --- a/gossip-lib/src/storage/mod.rs +++ b/gossip-lib/src/storage/mod.rs @@ -1682,23 +1682,16 @@ impl Storage { // We don't call this externally. Whenever we write an event, we do this. fn write_event_ek_pk_index<'a>( &'a self, - event: &Event, + id: Id, + kind: EventKind, + pubkey: PublicKey, rw_txn: Option<&mut RwTxn<'a>>, ) -> Result<(), Error> { let f = |txn: &mut RwTxn<'a>| -> Result<(), Error> { - let mut event = event; - - // If giftwrap, index the inner rumor instead - let rumor_event: Event; - if let Some(rumor) = self.switch_to_rumor(event, txn)? { - rumor_event = rumor; - event = &rumor_event; - } - - let ek: u32 = event.kind.into(); + let ek: u32 = kind.into(); let mut key: Vec = ek.to_be_bytes().as_slice().to_owned(); // event kind - key.extend(event.pubkey.as_bytes()); // pubkey - let bytes = event.id.as_slice(); + key.extend(pubkey.as_bytes()); // pubkey + let bytes = id.as_slice(); self.db_event_ek_pk_index()?.put(txn, &key, bytes)?; Ok(()) @@ -1719,23 +1712,16 @@ impl Storage { // We don't call this externally. Whenever we write an event, we do this. fn write_event_ek_c_index<'a>( &'a self, - event: &Event, + id: Id, + kind: EventKind, + created_at: Unixtime, rw_txn: Option<&mut RwTxn<'a>>, ) -> Result<(), Error> { let f = |txn: &mut RwTxn<'a>| -> Result<(), Error> { - let mut event = event; - - // If giftwrap, index the inner rumor instead - let rumor_event: Event; - if let Some(rumor) = self.switch_to_rumor(event, txn)? { - rumor_event = rumor; - event = &rumor_event; - } - - let ek: u32 = event.kind.into(); + let ek: u32 = kind.into(); let mut key: Vec = ek.to_be_bytes().as_slice().to_owned(); // event kind - key.extend((i64::MAX - event.created_at.0).to_be_bytes().as_slice()); // reverse created_at - let bytes = event.id.as_slice(); + key.extend((i64::MAX - created_at.0).to_be_bytes().as_slice()); // reverse created_at + let bytes = id.as_slice(); self.db_event_ek_c_index()?.put(txn, &key, bytes)?; Ok(()) @@ -2327,9 +2313,28 @@ impl Storage { for result in self.db_events()?.iter(&loop_txn)? { let (_key, val) = result?; let event = Event::read_from_buffer(val)?; - self.write_event_ek_pk_index(&event, Some(txn))?; - self.write_event_ek_c_index(&event, Some(txn))?; - self.write_event_tag_index(&event, Some(txn))?; + + // If giftwrap, index the inner rumor instead + let mut eventptr: &Event = &event; + let rumor: Event; + if let Some(r) = self.switch_to_rumor(&event, txn)? { + rumor = r; + eventptr = &rumor; + } + + self.write_event_ek_pk_index( + eventptr.id, + eventptr.kind, + eventptr.pubkey, + Some(txn), + )?; + self.write_event_ek_c_index( + eventptr.id, + eventptr.kind, + eventptr.created_at, + Some(txn), + )?; + self.write_event_tag_index(&eventptr, Some(txn))?; for hashtag in event.hashtags() { if hashtag.is_empty() { continue; diff --git a/gossip-lib/src/storage/unindexed_giftwraps1.rs b/gossip-lib/src/storage/unindexed_giftwraps1.rs index ec0403f6e..53fd2c856 100644 --- a/gossip-lib/src/storage/unindexed_giftwraps1.rs +++ b/gossip-lib/src/storage/unindexed_giftwraps1.rs @@ -61,8 +61,13 @@ impl Storage { let mut txn = self.env.write_txn()?; for id in ids { if let Some(event) = self.read_event(id)? { - self.write_event_ek_pk_index(&event, Some(&mut txn))?; - self.write_event_ek_c_index(&event, Some(&mut txn))?; + self.write_event_ek_pk_index(event.id, event.kind, event.pubkey, Some(&mut txn))?; + self.write_event_ek_c_index( + event.id, + event.kind, + event.created_at, + Some(&mut txn), + )?; self.write_event_tag_index(&event, Some(&mut txn))?; } self.db_unindexed_giftwraps1()?