From 72545189d259b960ea12b36cc924e94011674310 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Tue, 9 Apr 2024 11:01:05 +1200 Subject: [PATCH] migration 5: Simplify with deleted_ids table (no pubkey) --- chorus-lib/src/store/migrations.rs | 32 ++++++++++++++++++++++++++++-- chorus-lib/src/store/mod.rs | 28 +++++++++----------------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/chorus-lib/src/store/migrations.rs b/chorus-lib/src/store/migrations.rs index 293cc3c..cb21914 100644 --- a/chorus-lib/src/store/migrations.rs +++ b/chorus-lib/src/store/migrations.rs @@ -1,10 +1,11 @@ use super::Store; use crate::error::Error; +use crate::types::Id; use heed::byteorder::BigEndian; -use heed::types::{Unit, U64}; +use heed::types::{UnalignedSlice, Unit, U64}; use heed::RwTxn; -pub const CURRENT_MIGRATION_LEVEL: u32 = 4; +pub const CURRENT_MIGRATION_LEVEL: u32 = 5; impl Store { pub fn migrate(&self) -> Result<(), Error> { @@ -43,6 +44,7 @@ impl Store { 2 => self.migrate_to_2(txn)?, 3 => self.migrate_to_3(txn)?, 4 => self.migrate_to_4(txn)?, + 5 => self.migrate_to_5(txn)?, _ => panic!("Unknown migration level {level}"), } @@ -122,4 +124,30 @@ impl Store { deleted_offsets.clear(txn)?; Ok(()) } + + // Move data from deleted_events to deleted_ids + fn migrate_to_5(&self, txn: &mut RwTxn<'_>) -> Result<(), Error> { + let deleted_events = self + .env + .database_options() + .types::, Unit>() + .name("deleted-events") + .create(txn)?; + + let mut ids: Vec = Vec::new(); + + for i in deleted_events.iter(txn)? { + let (key, _val) = i?; + let id = Id(key[0..32].try_into().unwrap()); + ids.push(id); + } + + for id in ids.drain(..) { + self.deleted_ids.put(txn, id.as_slice(), &())?; + } + + deleted_events.clear(txn)?; + + Ok(()) + } } diff --git a/chorus-lib/src/store/mod.rs b/chorus-lib/src/store/mod.rs index 605c7ab..3fadfd2 100644 --- a/chorus-lib/src/store/mod.rs +++ b/chorus-lib/src/store/mod.rs @@ -28,7 +28,7 @@ pub struct Store { ktc_index: Database, OwnedType>, // this is for events deleted by other events - deleted_events: Database, Unit>, + deleted_ids: Database, Unit>, approved_events: Database, U8>, approved_pubkeys: Database, U8>, ip_data: Database, UnalignedSlice>, @@ -96,10 +96,10 @@ impl Store { .types::, OwnedType>() .name("ktci") .create(&mut txn)?; - let deleted_events = env + let deleted_ids = env .database_options() .types::, Unit>() - .name("deleted-events") + .name("deleted-ids") .create(&mut txn)?; let approved_events = env .database_options() @@ -164,7 +164,7 @@ impl Store { ); } - if let Ok(count) = deleted_events.len(&txn) { + if let Ok(count) = deleted_ids.len(&txn) { log::info!("{} deleted events", count); } if let Ok(count) = ip_data.len(&txn) { @@ -187,7 +187,7 @@ impl Store { akc_index, atc_index, ktc_index, - deleted_events, + deleted_ids, approved_events, approved_pubkeys, ip_data, @@ -225,8 +225,7 @@ impl Store { if self.i_index.get(&txn, event.id().0.as_slice())?.is_none() { // Reject event if it was deleted { - let deleted_key = Self::key_deleted_events(event.id(), event.pubkey()); - if self.deleted_events.get(&txn, &deleted_key)?.is_some() { + if self.deleted_ids.get(&txn, event.id().as_slice())?.is_some() { return Err(ChorusError::Deleted.into()); } } @@ -265,8 +264,7 @@ impl Store { if let Some(id_hex) = tag.next() { if let Ok(id) = Id::read_hex(id_hex) { // Add deletion pair to the event_deleted table - let deleted_key = Self::key_deleted_events(id, event.pubkey()); - self.deleted_events.put(txn, &deleted_key, &())?; + self.deleted_ids.put(txn, id.as_slice(), &())?; // Delete pair if let Some(target) = self.get_event_by_id(id)? { @@ -674,7 +672,7 @@ impl Store { /// /// This deindexes the event. /// - /// This does not add to the deleted_events record, which is for events + /// This does not add to the deleted_ids record, which is for events /// that are deleted by other events fn delete_by_id(&self, txn: &mut RwTxn<'_>, id: Id) -> Result<(), Error> { if let Some(offset) = self.i_index.get(txn, id.0.as_slice())? { @@ -688,7 +686,7 @@ impl Store { /// /// This deindexes the event. /// - /// This does not add to the deleted_events record, which is for events + /// This does not add to the deleted_ids record, which is for events /// that are deleted by other events fn delete_by_offset(&self, txn: &mut RwTxn<'_>, offset: usize) -> Result<(), Error> { // Get event @@ -1121,12 +1119,4 @@ impl Store { key.extend(id.as_slice()); key } - - fn key_deleted_events(id: Id, pubkey: Pubkey) -> Vec { - let mut key: Vec = - Vec::with_capacity(std::mem::size_of::() + std::mem::size_of::()); - key.extend(id.as_slice()); - key.extend(pubkey.as_slice()); - key - } }