conduwuit/src/database/key_value.rs

45 lines
1.4 KiB
Rust
Raw Normal View History

/// Checks if a room exists.
#[tracing::instrument(skip(self))]
pub fn exists(&self, room_id: &RoomId) -> Result<bool> {
2021-08-14 08:26:45 +02:00
let prefix = match self.get_shortroomid(room_id)? {
Some(b) => b.to_be_bytes().to_vec(),
None => return Ok(false),
};
// Look for PDUs in that room.
Ok(self
.pduid_pdu
2021-06-08 18:10:00 +02:00
.iter_from(&prefix, false)
.next()
.filter(|(k, _)| k.starts_with(&prefix))
.is_some())
}
2021-08-14 08:26:45 +02:00
pub fn get_shortroomid(&self, room_id: &RoomId) -> Result<Option<u64>> {
2021-08-14 19:47:49 +02:00
self.roomid_shortroomid
.get(room_id.as_bytes())?
2021-08-14 19:47:49 +02:00
.map(|bytes| {
utils::u64_from_bytes(&bytes)
.map_err(|_| Error::bad_database("Invalid shortroomid in db."))
})
.transpose()
2021-08-12 23:04:00 +02:00
}
pub fn get_or_create_shortroomid(
&self,
room_id: &RoomId,
globals: &super::globals::Globals,
) -> Result<u64> {
Ok(match self.roomid_shortroomid.get(room_id.as_bytes())? {
2021-08-12 23:04:00 +02:00
Some(short) => utils::u64_from_bytes(&short)
.map_err(|_| Error::bad_database("Invalid shortroomid in db."))?,
None => {
let short = globals.next_count()?;
self.roomid_shortroomid
.insert(room_id.as_bytes(), &short.to_be_bytes())?;
2021-08-12 23:04:00 +02:00
short
}
})
}