mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-18 16:21:24 +01:00
sending.rs: add RequestKey
Much easier to reason about than with a bunch of Vec<u8> everywhere.
This commit is contained in:
parent
507de063f5
commit
e294543ddb
3 changed files with 50 additions and 27 deletions
|
|
@ -4,7 +4,7 @@ use crate::{
|
|||
database::KeyValueDatabase,
|
||||
service::{
|
||||
self,
|
||||
sending::{OutgoingKind, SendingEventType},
|
||||
sending::{OutgoingKind, RequestKey, SendingEventType},
|
||||
},
|
||||
services, utils, Error, Result,
|
||||
};
|
||||
|
|
@ -13,10 +13,12 @@ impl service::sending::Data for KeyValueDatabase {
|
|||
fn active_requests<'a>(
|
||||
&'a self,
|
||||
) -> Box<
|
||||
dyn Iterator<Item = Result<(Vec<u8>, OutgoingKind, SendingEventType)>>
|
||||
+ 'a,
|
||||
dyn Iterator<
|
||||
Item = Result<(RequestKey, OutgoingKind, SendingEventType)>,
|
||||
> + 'a,
|
||||
> {
|
||||
Box::new(self.servercurrentevent_data.iter().map(|(key, v)| {
|
||||
let key = RequestKey::new(key);
|
||||
parse_servercurrentevent(&key, v).map(|(k, e)| (key, k, e))
|
||||
}))
|
||||
}
|
||||
|
|
@ -24,16 +26,19 @@ impl service::sending::Data for KeyValueDatabase {
|
|||
fn active_requests_for<'a>(
|
||||
&'a self,
|
||||
outgoing_kind: &OutgoingKind,
|
||||
) -> Box<dyn Iterator<Item = Result<(Vec<u8>, SendingEventType)>> + 'a>
|
||||
) -> Box<dyn Iterator<Item = Result<(RequestKey, SendingEventType)>> + 'a>
|
||||
{
|
||||
let prefix = outgoing_kind.get_prefix();
|
||||
Box::new(self.servercurrentevent_data.scan_prefix(prefix).map(
|
||||
|(key, v)| parse_servercurrentevent(&key, v).map(|(_, e)| (key, e)),
|
||||
|(key, v)| {
|
||||
let key = RequestKey::new(key);
|
||||
parse_servercurrentevent(&key, v).map(|(_, e)| (key, e))
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
fn delete_active_request(&self, key: Vec<u8>) -> Result<()> {
|
||||
self.servercurrentevent_data.remove(&key)
|
||||
fn delete_active_request(&self, key: RequestKey) -> Result<()> {
|
||||
self.servercurrentevent_data.remove(key.as_bytes())
|
||||
}
|
||||
|
||||
fn delete_all_active_requests_for(
|
||||
|
|
@ -51,7 +56,7 @@ impl service::sending::Data for KeyValueDatabase {
|
|||
fn queue_requests(
|
||||
&self,
|
||||
requests: &[(&OutgoingKind, SendingEventType)],
|
||||
) -> Result<Vec<Vec<u8>>> {
|
||||
) -> Result<Vec<RequestKey>> {
|
||||
let mut batch = Vec::new();
|
||||
let mut keys = Vec::new();
|
||||
for (outgoing_kind, event) in requests {
|
||||
|
|
@ -69,7 +74,7 @@ impl service::sending::Data for KeyValueDatabase {
|
|||
&[]
|
||||
};
|
||||
batch.push((key.clone(), value.to_owned()));
|
||||
keys.push(key);
|
||||
keys.push(RequestKey::new(key));
|
||||
}
|
||||
self.servernameevent_data.insert_batch(&mut batch.into_iter())?;
|
||||
Ok(keys)
|
||||
|
|
@ -78,17 +83,20 @@ impl service::sending::Data for KeyValueDatabase {
|
|||
fn queued_requests<'a>(
|
||||
&'a self,
|
||||
outgoing_kind: &OutgoingKind,
|
||||
) -> Box<dyn Iterator<Item = Result<(SendingEventType, Vec<u8>)>> + 'a>
|
||||
) -> Box<dyn Iterator<Item = Result<(SendingEventType, RequestKey)>> + 'a>
|
||||
{
|
||||
let prefix = outgoing_kind.get_prefix();
|
||||
return Box::new(self.servernameevent_data.scan_prefix(prefix).map(
|
||||
|(k, v)| parse_servercurrentevent(&k, v).map(|(_, ev)| (ev, k)),
|
||||
|(k, v)| {
|
||||
let k = RequestKey::new(k);
|
||||
parse_servercurrentevent(&k, v).map(|(_, ev)| (ev, k))
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
fn mark_as_active(
|
||||
&self,
|
||||
events: &[(SendingEventType, Vec<u8>)],
|
||||
events: &[(SendingEventType, RequestKey)],
|
||||
) -> Result<()> {
|
||||
for (e, key) in events {
|
||||
let value = if let SendingEventType::Edu(value) = &e {
|
||||
|
|
@ -96,8 +104,8 @@ impl service::sending::Data for KeyValueDatabase {
|
|||
} else {
|
||||
&[]
|
||||
};
|
||||
self.servercurrentevent_data.insert(key, value)?;
|
||||
self.servernameevent_data.remove(key)?;
|
||||
self.servercurrentevent_data.insert(key.as_bytes(), value)?;
|
||||
self.servernameevent_data.remove(key.as_bytes())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
@ -126,9 +134,10 @@ impl service::sending::Data for KeyValueDatabase {
|
|||
|
||||
#[tracing::instrument(skip(key))]
|
||||
fn parse_servercurrentevent(
|
||||
key: &[u8],
|
||||
key: &RequestKey,
|
||||
value: Vec<u8>,
|
||||
) -> Result<(OutgoingKind, SendingEventType)> {
|
||||
let key = key.as_bytes();
|
||||
// Appservices start with a plus
|
||||
Ok::<_, Error>(if key.starts_with(b"+") {
|
||||
let mut parts = key[1..].splitn(2, |&b| b == 0xFF);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue