Add PduId wrapper struct

Death to Vec<u8>
This commit is contained in:
Lambda 2024-08-26 16:47:50 +00:00
parent 341f4213d0
commit 26322d5a95
15 changed files with 110 additions and 71 deletions

View file

@ -1,6 +1,10 @@
use ruma::RoomId;
use crate::{database::KeyValueDatabase, service, services, utils, Result};
use crate::{
database::KeyValueDatabase,
service::{self, rooms::timeline::PduId},
services, utils, Result,
};
/// Splits a string into tokens used as keys in the search inverted index
///
@ -18,7 +22,7 @@ impl service::rooms::search::Data for KeyValueDatabase {
fn index_pdu(
&self,
shortroomid: u64,
pdu_id: &[u8],
pdu_id: &PduId,
message_body: &str,
) -> Result<()> {
let mut batch = tokenize(message_body).map(|word| {
@ -26,7 +30,7 @@ impl service::rooms::search::Data for KeyValueDatabase {
key.extend_from_slice(word.as_bytes());
key.push(0xFF);
// TODO: currently we save the room id a second time here
key.extend_from_slice(pdu_id);
key.extend_from_slice(pdu_id.as_bytes());
(key, Vec::new())
});
@ -37,7 +41,7 @@ impl service::rooms::search::Data for KeyValueDatabase {
fn deindex_pdu(
&self,
shortroomid: u64,
pdu_id: &[u8],
pdu_id: &PduId,
message_body: &str,
) -> Result<()> {
let batch = tokenize(message_body).map(|word| {
@ -45,7 +49,7 @@ impl service::rooms::search::Data for KeyValueDatabase {
key.extend_from_slice(word.as_bytes());
key.push(0xFF);
// TODO: currently we save the room id a second time here
key.extend_from_slice(pdu_id);
key.extend_from_slice(pdu_id.as_bytes());
key
});
@ -62,7 +66,7 @@ impl service::rooms::search::Data for KeyValueDatabase {
&'a self,
room_id: &RoomId,
search_string: &str,
) -> Result<Option<(Box<dyn Iterator<Item = Vec<u8>> + 'a>, Vec<String>)>>
) -> Result<Option<(Box<dyn Iterator<Item = PduId> + 'a>, Vec<String>)>>
{
let prefix = services()
.rooms
@ -87,12 +91,14 @@ impl service::rooms::search::Data for KeyValueDatabase {
// Newest pdus first
.iter_from(&last_possible_id, true)
.take_while(move |(k, _)| k.starts_with(&prefix2))
.map(move |(key, _)| key[prefix3.len()..].to_vec())
.map(move |(key, _)| PduId::new(key[prefix3.len()..].to_vec()))
});
// We compare b with a because we reversed the iterator earlier
let Some(common_elements) =
utils::common_elements(iterators, |a, b| b.cmp(a))
utils::common_elements(iterators, |a, b| {
b.as_bytes().cmp(a.as_bytes())
})
else {
return Ok(None);
};