From 6ef2ebc87177c29e7e9ef96f085e9b2d5e953ee6 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Tue, 5 Mar 2024 09:32:33 +1300 Subject: [PATCH] Add tc_index and ac_index (not utilized yet) --- chorus-lib/src/store/migrations.rs | 44 +++++++++++++++++- chorus-lib/src/store/mod.rs | 74 ++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 2 deletions(-) diff --git a/chorus-lib/src/store/migrations.rs b/chorus-lib/src/store/migrations.rs index 7760c09..ad4e4ff 100644 --- a/chorus-lib/src/store/migrations.rs +++ b/chorus-lib/src/store/migrations.rs @@ -2,7 +2,7 @@ use super::Store; use crate::error::Error; use heed::RwTxn; -pub const CURRENT_MIGRATION_LEVEL: u32 = 1; +pub const CURRENT_MIGRATION_LEVEL: u32 = 2; impl Store { pub fn migrate(&self) -> Result<(), Error> { @@ -38,14 +38,15 @@ impl Store { log::info!("Migrating database to {}", level); match level { 1 => self.migrate_to_1(txn)?, + 2 => self.migrate_to_2(txn)?, _ => panic!("Unknown migration level {level}"), } Ok(()) } + // Populate ci_index fn migrate_to_1(&self, txn: &mut RwTxn<'_>) -> Result<(), Error> { - // Build ci database let loop_txn = self.env.read_txn()?; let iter = self.i_index.iter(&loop_txn)?; for result in iter { @@ -62,4 +63,43 @@ impl Store { Ok(()) } + + // Populate tc_index and ac_index + fn migrate_to_2(&self, txn: &mut RwTxn<'_>) -> Result<(), Error> { + let loop_txn = self.env.read_txn()?; + let iter = self.i_index.iter(&loop_txn)?; + for result in iter { + let (_key, offset) = result?; + if let Some(event) = self.events.get_event_by_offset(offset)? { + // Add to ac_index + self.ac_index.put( + txn, + &Self::key_ac_index(event.pubkey(), event.created_at(), event.id()), + &offset, + )?; + + // Add to tc_index + for mut tsi in event.tags()?.iter() { + if let Some(tagname) = tsi.next() { + if tagname.len() == 1 { + if let Some(tagvalue) = tsi.next() { + self.tc_index.put( + txn, + &Self::key_tc_index( + tagname[0], + tagvalue, + event.created_at(), + event.id(), + ), + &offset, + )?; + } + } + } + } + } + } + + Ok(()) + } } diff --git a/chorus-lib/src/store/mod.rs b/chorus-lib/src/store/mod.rs index a4d8d55..559b516 100644 --- a/chorus-lib/src/store/mod.rs +++ b/chorus-lib/src/store/mod.rs @@ -23,6 +23,8 @@ pub struct Store { env: Env, i_index: Database, OwnedType>, ci_index: Database, OwnedType>, + tc_index: Database, OwnedType>, + ac_index: Database, OwnedType>, akc_index: Database, OwnedType>, atc_index: Database, OwnedType>, ktc_index: Database, OwnedType>, @@ -71,6 +73,16 @@ impl Store { .types::, OwnedType>() .name("ci") .create(&mut txn)?; + let tc_index = env + .database_options() + .types::, OwnedType>() + .name("tci") + .create(&mut txn)?; + let ac_index = env + .database_options() + .types::, OwnedType>() + .name("aci") + .create(&mut txn)?; let akc_index = env .database_options() .types::, OwnedType>() @@ -123,6 +135,8 @@ impl Store { env, i_index, ci_index, + tc_index, + ac_index, akc_index, atc_index, ktc_index, @@ -542,11 +556,29 @@ impl Store { &offset, )?; + self.ac_index.put( + txn, + &Self::key_ac_index(event.pubkey(), event.created_at(), event.id()), + &offset, + )?; + for mut tsi in event.tags()?.iter() { if let Some(tagname) = tsi.next() { // FIXME make sure it is a letter too if tagname.len() == 1 { if let Some(tagvalue) = tsi.next() { + // Index by tag (with created_at and id) + self.tc_index.put( + txn, + &Self::key_tc_index( + tagname[0], + tagvalue, + event.created_at(), + event.id(), + ), + &offset, + )?; + // Index by author and tag (with created_at and id) self.atc_index.put( txn, @@ -610,11 +642,27 @@ impl Store { event.id(), ), )?; + + // Index by tag (with created_at and id) + self.tc_index.delete( + txn, + &Self::key_tc_index( + tagname[0], + tagvalue, + event.created_at(), + event.id(), + ), + )?; } } } } + self.ac_index.delete( + txn, + &Self::key_ac_index(event.pubkey(), event.created_at(), event.id()), + )?; + self.ci_index .delete(txn, &Self::key_ci_index(event.created_at(), event.id()))?; @@ -750,6 +798,32 @@ impl Store { key } + fn key_tc_index(letter: u8, tag_value: &[u8], created_at: Time, id: Id) -> Vec { + const PADLEN: usize = 182; + let mut key: Vec = + Vec::with_capacity(PADLEN + std::mem::size_of::