consolidate key/value types; consistent interface arguments

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-02 09:51:00 +00:00
parent 46423cab4f
commit a2d25215a3
8 changed files with 172 additions and 79 deletions

View file

@ -466,47 +466,53 @@ async fn db_lt_8(db: &Arc<Database>, _config: &Config) -> Result<()> {
info!("Migration: 8");
}
// Update pduids db layout
let mut batch = pduid_pdu.iter().filter_map(|(key, v)| {
if !key.starts_with(b"!") {
return None;
}
let mut parts = key.splitn(2, |&b| b == 0xFF);
let room_id = parts.next().unwrap();
let count = parts.next().unwrap();
let batch = pduid_pdu
.iter()
.filter_map(|(key, v)| {
if !key.starts_with(b"!") {
return None;
}
let mut parts = key.splitn(2, |&b| b == 0xFF);
let room_id = parts.next().unwrap();
let count = parts.next().unwrap();
let short_room_id = roomid_shortroomid
.get(room_id)
.unwrap()
.expect("shortroomid should exist");
let short_room_id = roomid_shortroomid
.get(room_id)
.unwrap()
.expect("shortroomid should exist");
let mut new_key = short_room_id.to_vec();
new_key.extend_from_slice(count);
let mut new_key = short_room_id.to_vec();
new_key.extend_from_slice(count);
Some((new_key, v))
});
Some(database::OwnedKeyVal(new_key, v))
})
.collect::<Vec<_>>();
pduid_pdu.insert_batch(&mut batch)?;
pduid_pdu.insert_batch(batch.iter().map(database::KeyVal::from))?;
let mut batch2 = eventid_pduid.iter().filter_map(|(k, value)| {
if !value.starts_with(b"!") {
return None;
}
let mut parts = value.splitn(2, |&b| b == 0xFF);
let room_id = parts.next().unwrap();
let count = parts.next().unwrap();
let batch2 = eventid_pduid
.iter()
.filter_map(|(k, value)| {
if !value.starts_with(b"!") {
return None;
}
let mut parts = value.splitn(2, |&b| b == 0xFF);
let room_id = parts.next().unwrap();
let count = parts.next().unwrap();
let short_room_id = roomid_shortroomid
.get(room_id)
.unwrap()
.expect("shortroomid should exist");
let short_room_id = roomid_shortroomid
.get(room_id)
.unwrap()
.expect("shortroomid should exist");
let mut new_value = short_room_id.to_vec();
new_value.extend_from_slice(count);
let mut new_value = short_room_id.to_vec();
new_value.extend_from_slice(count);
Some((k, new_value))
});
Some(database::OwnedKeyVal(k, new_value))
})
.collect::<Vec<_>>();
eventid_pduid.insert_batch(&mut batch2)?;
eventid_pduid.insert_batch(batch2.iter().map(database::KeyVal::from))?;
services().globals.bump_database_version(8)?;
info!("Migration: 7 -> 8 finished");
@ -538,12 +544,13 @@ async fn db_lt_9(db: &Arc<Database>, _config: &Config) -> Result<()> {
new_key.extend_from_slice(word);
new_key.push(0xFF);
new_key.extend_from_slice(pdu_id_count);
Some((new_key, Vec::new()))
Some(database::OwnedKeyVal(new_key, Vec::<u8>::new()))
})
.peekable();
while iter.peek().is_some() {
tokenids.insert_batch(&mut iter.by_ref().take(1000))?;
let batch = iter.by_ref().take(1000).collect::<Vec<_>>();
tokenids.insert_batch(batch.iter().map(database::KeyVal::from))?;
debug!("Inserted smaller batch");
}

View file

@ -20,15 +20,18 @@ impl Data {
}
pub(super) fn index_pdu(&self, shortroomid: u64, pdu_id: &[u8], message_body: &str) -> Result<()> {
let mut batch = tokenize(message_body).map(|word| {
let mut key = shortroomid.to_be_bytes().to_vec();
key.extend_from_slice(word.as_bytes());
key.push(0xFF);
key.extend_from_slice(pdu_id); // TODO: currently we save the room id a second time here
(key, Vec::new())
});
let batch = tokenize(message_body)
.map(|word| {
let mut key = shortroomid.to_be_bytes().to_vec();
key.extend_from_slice(word.as_bytes());
key.push(0xFF);
key.extend_from_slice(pdu_id); // TODO: currently we save the room id a second time here
(key, Vec::<u8>::new())
})
.collect::<Vec<_>>();
self.tokenids.insert_batch(&mut batch)
self.tokenids
.insert_batch(batch.iter().map(database::KeyVal::from))
}
pub(super) fn deindex_pdu(&self, shortroomid: u64, pdu_id: &[u8], message_body: &str) -> Result<()> {

View file

@ -268,9 +268,9 @@ impl Data {
}
self.userroomid_notificationcount
.increment_batch(&mut notifies_batch.into_iter())?;
.increment_batch(notifies_batch.iter().map(Vec::as_slice))?;
self.userroomid_highlightcount
.increment_batch(&mut highlights_batch.into_iter())?;
.increment_batch(highlights_batch.iter().map(Vec::as_slice))?;
Ok(())
}
}

View file

@ -87,7 +87,7 @@ impl Data {
keys.push(key);
}
self.servernameevent_data
.insert_batch(&mut batch.into_iter())?;
.insert_batch(batch.iter().map(database::KeyVal::from))?;
Ok(keys)
}