Skip to content

Commit

Permalink
storage: Switch interface to indexing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedilger committed Feb 2, 2024
1 parent ff839ad commit 618b25e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 34 deletions.
19 changes: 16 additions & 3 deletions gossip-lib/src/storage/events2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
63 changes: 34 additions & 29 deletions gossip-lib/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = 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(())
Expand All @@ -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<u8> = 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(())
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions gossip-lib/src/storage/unindexed_giftwraps1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?
Expand Down

0 comments on commit 618b25e

Please sign in to comment.