diff --git a/src/database/key_value/rooms/short.rs b/src/database/key_value/rooms/short.rs index b0b7fb6e..e0c3daac 100644 --- a/src/database/key_value/rooms/short.rs +++ b/src/database/key_value/rooms/short.rs @@ -21,6 +21,38 @@ impl service::rooms::short::Data for KeyValueDatabase { Ok(short) } + fn multi_get_or_create_shorteventid(&self, event_ids: &[&EventId]) -> Result> { + let mut ret: Vec = Vec::with_capacity(event_ids.len()); + let keys = event_ids + .iter() + .map(|id| id.as_bytes()) + .collect::>(); + for (i, short) in self + .eventid_shorteventid + .multi_get(&keys)? + .iter() + .enumerate() + { + match short { + Some(short) => ret.push( + utils::u64_from_bytes(short).map_err(|_| Error::bad_database("Invalid shorteventid in db."))?, + ), + None => { + let short = services().globals.next_count()?; + self.eventid_shorteventid + .insert(keys[i], &short.to_be_bytes())?; + self.shorteventid_eventid + .insert(&short.to_be_bytes(), keys[i])?; + + debug_assert!(ret.len() == i, "position of result must match input"); + ret.push(short); + }, + } + } + + Ok(ret) + } + fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> Result> { let mut statekey_vec = event_type.to_string().as_bytes().to_vec(); statekey_vec.push(0xFF); diff --git a/src/service/rooms/short/data.rs b/src/service/rooms/short/data.rs index aa891e7a..d0e2085f 100644 --- a/src/service/rooms/short/data.rs +++ b/src/service/rooms/short/data.rs @@ -7,6 +7,8 @@ use crate::Result; pub trait Data: Send + Sync { fn get_or_create_shorteventid(&self, event_id: &EventId) -> Result; + fn multi_get_or_create_shorteventid(&self, event_id: &[&EventId]) -> Result>; + fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> Result>; fn get_or_create_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> Result; diff --git a/src/service/rooms/short/mod.rs b/src/service/rooms/short/mod.rs index 7e8623d2..1490b38a 100644 --- a/src/service/rooms/short/mod.rs +++ b/src/service/rooms/short/mod.rs @@ -15,6 +15,10 @@ impl Service { self.db.get_or_create_shorteventid(event_id) } + pub fn multi_get_or_create_shorteventid(&self, event_ids: &[&EventId]) -> Result> { + self.db.multi_get_or_create_shorteventid(event_ids) + } + pub fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> Result> { self.db.get_shortstatekey(event_type, state_key) }