migration 5: Simplify with deleted_ids table (no pubkey)

This commit is contained in:
Mike Dilger 2024-04-09 11:01:05 +12:00
parent 601c9fafd8
commit 72545189d2
2 changed files with 39 additions and 21 deletions

View File

@ -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::<UnalignedSlice<u8>, Unit>()
.name("deleted-events")
.create(txn)?;
let mut ids: Vec<Id> = 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(())
}
}

View File

@ -28,7 +28,7 @@ pub struct Store {
ktc_index: Database<UnalignedSlice<u8>, OwnedType<usize>>,
// this is for events deleted by other events
deleted_events: Database<UnalignedSlice<u8>, Unit>,
deleted_ids: Database<UnalignedSlice<u8>, Unit>,
approved_events: Database<UnalignedSlice<u8>, U8>,
approved_pubkeys: Database<UnalignedSlice<u8>, U8>,
ip_data: Database<UnalignedSlice<u8>, UnalignedSlice<u8>>,
@ -96,10 +96,10 @@ impl Store {
.types::<UnalignedSlice<u8>, OwnedType<usize>>()
.name("ktci")
.create(&mut txn)?;
let deleted_events = env
let deleted_ids = env
.database_options()
.types::<UnalignedSlice<u8>, 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<u8> {
let mut key: Vec<u8> =
Vec::with_capacity(std::mem::size_of::<Id>() + std::mem::size_of::<Pubkey>());
key.extend(id.as_slice());
key.extend(pubkey.as_slice());
key
}
}