mirror of https://gitlab.com/famedly/conduit
refactor state accessor, state cache, user, uiaa
parent
3e22bbeecd
commit
82e7f57b38
@ -0,0 +1,8 @@
|
|||||||
|
impl service::room::state_cache::Data for KeyValueDatabase {
|
||||||
|
fn mark_as_once_joined(user_id: &UserId, room_id: &RoomId) -> Result<()> {
|
||||||
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
||||||
|
self.roomuseroncejoinedids.insert(&userroom_id, &[])?;
|
||||||
|
}
|
||||||
|
}
|
@ -1,160 +1,51 @@
|
|||||||
|
pub trait Data {
|
||||||
/// Builds a StateMap by iterating over all keys that start
|
/// Builds a StateMap by iterating over all keys that start
|
||||||
/// with state_hash, this gives the full state for the given state_hash.
|
/// with state_hash, this gives the full state for the given state_hash.
|
||||||
#[tracing::instrument(skip(self))]
|
async fn state_full_ids(&self, shortstatehash: u64) -> Result<BTreeMap<u64, Arc<EventId>>>;
|
||||||
pub async fn state_full_ids(&self, shortstatehash: u64) -> Result<BTreeMap<u64, Arc<EventId>>> {
|
|
||||||
let full_state = self
|
|
||||||
.load_shortstatehash_info(shortstatehash)?
|
|
||||||
.pop()
|
|
||||||
.expect("there is always one layer")
|
|
||||||
.1;
|
|
||||||
let mut result = BTreeMap::new();
|
|
||||||
let mut i = 0;
|
|
||||||
for compressed in full_state.into_iter() {
|
|
||||||
let parsed = self.parse_compressed_state_event(compressed)?;
|
|
||||||
result.insert(parsed.0, parsed.1);
|
|
||||||
|
|
||||||
i += 1;
|
async fn state_full(
|
||||||
if i % 100 == 0 {
|
|
||||||
tokio::task::yield_now().await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(skip(self))]
|
|
||||||
pub async fn state_full(
|
|
||||||
&self,
|
&self,
|
||||||
shortstatehash: u64,
|
shortstatehash: u64,
|
||||||
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>>;
|
||||||
let full_state = self
|
|
||||||
.load_shortstatehash_info(shortstatehash)?
|
|
||||||
.pop()
|
|
||||||
.expect("there is always one layer")
|
|
||||||
.1;
|
|
||||||
|
|
||||||
let mut result = HashMap::new();
|
|
||||||
let mut i = 0;
|
|
||||||
for compressed in full_state {
|
|
||||||
let (_, eventid) = self.parse_compressed_state_event(compressed)?;
|
|
||||||
if let Some(pdu) = self.get_pdu(&eventid)? {
|
|
||||||
result.insert(
|
|
||||||
(
|
|
||||||
pdu.kind.to_string().into(),
|
|
||||||
pdu.state_key
|
|
||||||
.as_ref()
|
|
||||||
.ok_or_else(|| Error::bad_database("State event has no state key."))?
|
|
||||||
.clone(),
|
|
||||||
),
|
|
||||||
pdu,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
i += 1;
|
|
||||||
if i % 100 == 0 {
|
|
||||||
tokio::task::yield_now().await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
||||||
#[tracing::instrument(skip(self))]
|
fn state_get_id(
|
||||||
pub fn state_get_id(
|
|
||||||
&self,
|
&self,
|
||||||
shortstatehash: u64,
|
shortstatehash: u64,
|
||||||
event_type: &StateEventType,
|
event_type: &StateEventType,
|
||||||
state_key: &str,
|
state_key: &str,
|
||||||
) -> Result<Option<Arc<EventId>>> {
|
) -> Result<Option<Arc<EventId>>>;
|
||||||
let shortstatekey = match self.get_shortstatekey(event_type, state_key)? {
|
|
||||||
Some(s) => s,
|
|
||||||
None => return Ok(None),
|
|
||||||
};
|
|
||||||
let full_state = self
|
|
||||||
.load_shortstatehash_info(shortstatehash)?
|
|
||||||
.pop()
|
|
||||||
.expect("there is always one layer")
|
|
||||||
.1;
|
|
||||||
Ok(full_state
|
|
||||||
.into_iter()
|
|
||||||
.find(|bytes| bytes.starts_with(&shortstatekey.to_be_bytes()))
|
|
||||||
.and_then(|compressed| {
|
|
||||||
self.parse_compressed_state_event(compressed)
|
|
||||||
.ok()
|
|
||||||
.map(|(_, id)| id)
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
||||||
#[tracing::instrument(skip(self))]
|
fn state_get(
|
||||||
pub fn state_get(
|
|
||||||
&self,
|
&self,
|
||||||
shortstatehash: u64,
|
shortstatehash: u64,
|
||||||
event_type: &StateEventType,
|
event_type: &StateEventType,
|
||||||
state_key: &str,
|
state_key: &str,
|
||||||
) -> Result<Option<Arc<PduEvent>>> {
|
) -> Result<Option<Arc<PduEvent>>>;
|
||||||
self.state_get_id(shortstatehash, event_type, state_key)?
|
|
||||||
.map_or(Ok(None), |event_id| self.get_pdu(&event_id))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the state hash for this pdu.
|
/// Returns the state hash for this pdu.
|
||||||
pub fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<Option<u64>> {
|
fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<Option<u64>>;
|
||||||
self.eventid_shorteventid
|
|
||||||
.get(event_id.as_bytes())?
|
|
||||||
.map_or(Ok(None), |shorteventid| {
|
|
||||||
self.shorteventid_shortstatehash
|
|
||||||
.get(&shorteventid)?
|
|
||||||
.map(|bytes| {
|
|
||||||
utils::u64_from_bytes(&bytes).map_err(|_| {
|
|
||||||
Error::bad_database(
|
|
||||||
"Invalid shortstatehash bytes in shorteventid_shortstatehash",
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.transpose()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the full room state.
|
/// Returns the full room state.
|
||||||
#[tracing::instrument(skip(self))]
|
async fn room_state_full(
|
||||||
pub async fn room_state_full(
|
|
||||||
&self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>>;
|
||||||
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
|
|
||||||
self.state_full(current_shortstatehash).await
|
|
||||||
} else {
|
|
||||||
Ok(HashMap::new())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
||||||
#[tracing::instrument(skip(self))]
|
fn room_state_get_id(
|
||||||
pub fn room_state_get_id(
|
|
||||||
&self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
event_type: &StateEventType,
|
event_type: &StateEventType,
|
||||||
state_key: &str,
|
state_key: &str,
|
||||||
) -> Result<Option<Arc<EventId>>> {
|
) -> Result<Option<Arc<EventId>>>;
|
||||||
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
|
|
||||||
self.state_get_id(current_shortstatehash, event_type, state_key)
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
||||||
#[tracing::instrument(skip(self))]
|
fn room_state_get(
|
||||||
pub fn room_state_get(
|
|
||||||
&self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
event_type: &StateEventType,
|
event_type: &StateEventType,
|
||||||
state_key: &str,
|
state_key: &str,
|
||||||
) -> Result<Option<Arc<PduEvent>>> {
|
) -> Result<Option<Arc<PduEvent>>>;
|
||||||
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
|
}
|
||||||
self.state_get(current_shortstatehash, event_type, state_key)
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
pub trait Data {
|
||||||
|
fn mark_as_once_joined(user_id: &UserId, room_id: &RoomId) -> Result<()>;
|
||||||
|
}
|
@ -1,114 +1,21 @@
|
|||||||
|
pub trait Data {
|
||||||
|
fn reset_notification_counts(&self, user_id: &UserId, room_id: &RoomId) -> Result<()>;
|
||||||
|
|
||||||
#[tracing::instrument(skip(self))]
|
fn notification_count(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64>;
|
||||||
pub fn reset_notification_counts(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
|
|
||||||
let mut userroom_id = user_id.as_bytes().to_vec();
|
|
||||||
userroom_id.push(0xff);
|
|
||||||
userroom_id.extend_from_slice(room_id.as_bytes());
|
|
||||||
|
|
||||||
self.userroomid_notificationcount
|
fn highlight_count(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64>;
|
||||||
.insert(&userroom_id, &0_u64.to_be_bytes())?;
|
|
||||||
self.userroomid_highlightcount
|
|
||||||
.insert(&userroom_id, &0_u64.to_be_bytes())?;
|
|
||||||
|
|
||||||
Ok(())
|
fn associate_token_shortstatehash(
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(skip(self))]
|
|
||||||
pub fn notification_count(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {
|
|
||||||
let mut userroom_id = user_id.as_bytes().to_vec();
|
|
||||||
userroom_id.push(0xff);
|
|
||||||
userroom_id.extend_from_slice(room_id.as_bytes());
|
|
||||||
|
|
||||||
self.userroomid_notificationcount
|
|
||||||
.get(&userroom_id)?
|
|
||||||
.map(|bytes| {
|
|
||||||
utils::u64_from_bytes(&bytes)
|
|
||||||
.map_err(|_| Error::bad_database("Invalid notification count in db."))
|
|
||||||
})
|
|
||||||
.unwrap_or(Ok(0))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(skip(self))]
|
|
||||||
pub fn highlight_count(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {
|
|
||||||
let mut userroom_id = user_id.as_bytes().to_vec();
|
|
||||||
userroom_id.push(0xff);
|
|
||||||
userroom_id.extend_from_slice(room_id.as_bytes());
|
|
||||||
|
|
||||||
self.userroomid_highlightcount
|
|
||||||
.get(&userroom_id)?
|
|
||||||
.map(|bytes| {
|
|
||||||
utils::u64_from_bytes(&bytes)
|
|
||||||
.map_err(|_| Error::bad_database("Invalid highlight count in db."))
|
|
||||||
})
|
|
||||||
.unwrap_or(Ok(0))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn associate_token_shortstatehash(
|
|
||||||
&self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
token: u64,
|
token: u64,
|
||||||
shortstatehash: u64,
|
shortstatehash: u64,
|
||||||
) -> Result<()> {
|
) -> Result<()>;
|
||||||
let shortroomid = self.get_shortroomid(room_id)?.expect("room exists");
|
|
||||||
|
|
||||||
let mut key = shortroomid.to_be_bytes().to_vec();
|
|
||||||
key.extend_from_slice(&token.to_be_bytes());
|
|
||||||
|
|
||||||
self.roomsynctoken_shortstatehash
|
|
||||||
.insert(&key, &shortstatehash.to_be_bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_token_shortstatehash(&self, room_id: &RoomId, token: u64) -> Result<Option<u64>> {
|
fn get_token_shortstatehash(&self, room_id: &RoomId, token: u64) -> Result<Option<u64>>;
|
||||||
let shortroomid = self.get_shortroomid(room_id)?.expect("room exists");
|
|
||||||
|
|
||||||
let mut key = shortroomid.to_be_bytes().to_vec();
|
fn get_shared_rooms<'a>(
|
||||||
key.extend_from_slice(&token.to_be_bytes());
|
|
||||||
|
|
||||||
self.roomsynctoken_shortstatehash
|
|
||||||
.get(&key)?
|
|
||||||
.map(|bytes| {
|
|
||||||
utils::u64_from_bytes(&bytes).map_err(|_| {
|
|
||||||
Error::bad_database("Invalid shortstatehash in roomsynctoken_shortstatehash")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.transpose()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(skip(self))]
|
|
||||||
pub fn get_shared_rooms<'a>(
|
|
||||||
&'a self,
|
&'a self,
|
||||||
users: Vec<Box<UserId>>,
|
users: Vec<Box<UserId>>,
|
||||||
) -> Result<impl Iterator<Item = Result<Box<RoomId>>> + 'a> {
|
) -> Result<impl Iterator<Item = Result<Box<RoomId>>> + 'a>;
|
||||||
let iterators = users.into_iter().map(move |user_id| {
|
}
|
||||||
let mut prefix = user_id.as_bytes().to_vec();
|
|
||||||
prefix.push(0xff);
|
|
||||||
|
|
||||||
self.userroomid_joined
|
|
||||||
.scan_prefix(prefix)
|
|
||||||
.map(|(key, _)| {
|
|
||||||
let roomid_index = key
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.find(|(_, &b)| b == 0xff)
|
|
||||||
.ok_or_else(|| Error::bad_database("Invalid userroomid_joined in db."))?
|
|
||||||
.0
|
|
||||||
+ 1; // +1 because the room id starts AFTER the separator
|
|
||||||
|
|
||||||
let room_id = key[roomid_index..].to_vec();
|
|
||||||
|
|
||||||
Ok::<_, Error>(room_id)
|
|
||||||
})
|
|
||||||
.filter_map(|r| r.ok())
|
|
||||||
});
|
|
||||||
|
|
||||||
// We use the default compare function because keys are sorted correctly (not reversed)
|
|
||||||
Ok(utils::common_elements(iterators, Ord::cmp)
|
|
||||||
.expect("users is not empty")
|
|
||||||
.map(|bytes| {
|
|
||||||
RoomId::parse(utils::string_from_bytes(&*bytes).map_err(|_| {
|
|
||||||
Error::bad_database("Invalid RoomId bytes in userroomid_joined")
|
|
||||||
})?)
|
|
||||||
.map_err(|_| Error::bad_database("Invalid RoomId in userroomid_joined."))
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue