[broken] storage: switch to db_person_lists2

This commit is contained in:
Mike Dilger 2023-10-17 11:58:21 +13:00
parent 69bc9716da
commit 45731c596c
3 changed files with 24 additions and 85 deletions

View File

@ -1,4 +1,6 @@
use super::types::{Person2, PersonList1, PersonRelay1, Settings1, Settings2, Theme1, ThemeVariant1}; use super::types::{
Person2, PersonList1, PersonRelay1, Settings1, Settings2, Theme1, ThemeVariant1,
};
use super::Storage; use super::Storage;
use crate::error::{Error, ErrorKind}; use crate::error::{Error, ErrorKind};
use heed::types::UnalignedSlice; use heed::types::UnalignedSlice;
@ -575,10 +577,8 @@ impl Storage {
.iter() .iter()
.map(|u| (*u).into()) .map(|u| (*u).into())
.collect::<Vec<PersonList1>>(); .collect::<Vec<PersonList1>>();
let new_person_lists: HashMap<PersonList1, bool> = person_lists let new_person_lists: HashMap<PersonList1, bool> =
.drain(..) person_lists.drain(..).map(|l| (l, true)).collect();
.map(|l| (l, true))
.collect();
self.write_person_lists2(&pubkey, new_person_lists, Some(txn))?; self.write_person_lists2(&pubkey, new_person_lists, Some(txn))?;
} }

View File

@ -285,7 +285,7 @@ impl Storage {
#[inline] #[inline]
pub(crate) fn db_person_lists(&self) -> Result<RawDatabase, Error> { pub(crate) fn db_person_lists(&self) -> Result<RawDatabase, Error> {
self.db_person_lists1() self.db_person_lists2()
} }
// Database length functions --------------------------------- // Database length functions ---------------------------------
@ -2261,23 +2261,30 @@ impl Storage {
} }
/// Read person lists /// Read person lists
pub fn read_person_lists(&self, pubkey: &PublicKey) -> Result<Vec<PersonList>, Error> { pub fn read_person_lists(
self.read_person_lists1(pubkey) &self,
pubkey: &PublicKey,
) -> Result<HashMap<PersonList, bool>, Error> {
self.read_person_lists2(pubkey)
} }
/// Write person lists /// Write person lists
pub fn write_person_lists<'a>( pub fn write_person_lists<'a>(
&'a self, &'a self,
pubkey: &PublicKey, pubkey: &PublicKey,
lists: Vec<PersonList>, lists: HashMap<PersonList, bool>,
rw_txn: Option<&mut RwTxn<'a>>, rw_txn: Option<&mut RwTxn<'a>>,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.write_person_lists1(pubkey, lists, rw_txn) self.write_person_lists2(pubkey, lists, rw_txn)
} }
/// Get people in a person list /// Get people in a person list
pub fn get_people_in_list(&self, list: PersonList) -> Result<Vec<PublicKey>, Error> { pub fn get_people_in_list(
self.get_people_in_list1(list) &self,
list: PersonList,
public: Option<bool>,
) -> Result<Vec<PublicKey>, Error> {
self.get_people_in_list2(list, public)
} }
/// Empty a person list /// Empty a person list
@ -2287,13 +2294,13 @@ impl Storage {
list: PersonList, list: PersonList,
rw_txn: Option<&mut RwTxn<'a>>, rw_txn: Option<&mut RwTxn<'a>>,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.clear_person_list1(list, rw_txn) self.clear_person_list2(list, rw_txn)
} }
/// Is a person in a list? /// Is a person in a list?
pub fn is_person_in_list(&self, pubkey: &PublicKey, list: PersonList) -> Result<bool, Error> { pub fn is_person_in_list(&self, pubkey: &PublicKey, list: PersonList) -> Result<bool, Error> {
let lists = self.read_person_lists(pubkey)?; let lists = self.read_person_lists(pubkey)?;
Ok(lists.contains(&list)) Ok(lists.contains_key(&list))
} }
/// Add a person to a list /// Add a person to a list
@ -2301,12 +2308,11 @@ impl Storage {
&'a self, &'a self,
pubkey: &PublicKey, pubkey: &PublicKey,
list: PersonList, list: PersonList,
public: bool,
rw_txn: Option<&mut RwTxn<'a>>, rw_txn: Option<&mut RwTxn<'a>>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let mut lists = self.read_person_lists(pubkey)?; let mut lists = self.read_person_lists(pubkey)?;
if !lists.iter().any(|s| *s == list) { lists.insert(list, public);
lists.push(list.to_owned());
}
self.write_person_lists(pubkey, lists, rw_txn) self.write_person_lists(pubkey, lists, rw_txn)
} }
@ -2318,7 +2324,7 @@ impl Storage {
rw_txn: Option<&mut RwTxn<'a>>, rw_txn: Option<&mut RwTxn<'a>>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let mut lists = self.read_person_lists(pubkey)?; let mut lists = self.read_person_lists(pubkey)?;
let lists: Vec<PersonList> = lists.drain(..).filter(|s| *s != list).collect(); lists.remove(&list);
self.write_person_lists(pubkey, lists, rw_txn) self.write_person_lists(pubkey, lists, rw_txn)
} }
} }

View File

@ -43,15 +43,6 @@ impl Storage {
} }
} }
pub(crate) fn read_person_lists1(&self, pubkey: &PublicKey) -> Result<Vec<PersonList>, Error> {
let key: Vec<u8> = pubkey.to_bytes();
let txn = self.env.read_txn()?;
Ok(match self.db_person_lists1()?.get(&txn, &key)? {
Some(bytes) => bytes.iter().map(|u| (*u).into()).collect(),
None => vec![],
})
}
pub(crate) fn write_person_lists1<'a>( pub(crate) fn write_person_lists1<'a>(
&'a self, &'a self,
pubkey: &PublicKey, pubkey: &PublicKey,
@ -77,62 +68,4 @@ impl Storage {
Ok(()) Ok(())
} }
pub(crate) fn get_people_in_list1(&self, list: PersonList) -> Result<Vec<PublicKey>, Error> {
let txn = self.env.read_txn()?;
let mut pubkeys: Vec<PublicKey> = Vec::new();
for result in self.db_person_lists1()?.iter(&txn)? {
let (key, val) = result?;
let pubkey = PublicKey::from_bytes(key, true)?;
let person_lists = val.iter().map(|u| (*u).into()).collect::<Vec<PersonList>>();
if person_lists.iter().any(|s| *s == list) {
pubkeys.push(pubkey);
}
}
Ok(pubkeys)
}
pub(crate) fn clear_person_list1<'a>(
&'a self,
list: PersonList,
rw_txn: Option<&mut RwTxn<'a>>,
) -> Result<(), Error> {
let f = |txn: &mut RwTxn<'a>| -> Result<(), Error> {
let mut fixed: Vec<(PublicKey, Vec<u8>)> = Vec::new();
// Collect records that require changing
for result in self.db_person_lists1()?.iter(txn)? {
let (key, val) = result?;
let pubkey = PublicKey::from_bytes(key, true)?;
let mut person_lists = val.iter().map(|u| (*u).into()).collect::<Vec<PersonList>>();
if person_lists.contains(&list) {
person_lists = person_lists.drain(..).filter(|l| *l != list).collect();
let bytes = person_lists
.iter()
.map(|l| (*l).into())
.collect::<Vec<u8>>();
fixed.push((pubkey, bytes));
}
}
// Change them
for (pubkey, bytes) in fixed.drain(..) {
let key: Vec<u8> = pubkey.to_bytes();
self.db_person_lists1()?.put(txn, &key, &bytes)?;
}
Ok(())
};
match rw_txn {
Some(txn) => f(txn)?,
None => {
let mut txn = self.env.write_txn()?;
f(&mut txn)?;
txn.commit()?;
}
};
Ok(())
}
} }