2024-01-05 16:46:16 +00:00
|
|
|
use super::mailbox::MailMeta;
|
|
|
|
use super::snapshot::FrozenMailbox;
|
2024-01-06 10:33:56 +00:00
|
|
|
use super::unique_ident::UniqueIdent;
|
2024-01-08 20:18:45 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use futures::stream::{FuturesOrdered, StreamExt};
|
2024-01-05 16:46:16 +00:00
|
|
|
|
|
|
|
/// Query is in charge of fetching efficiently
|
|
|
|
/// requested data for a list of emails
|
2024-01-06 10:33:56 +00:00
|
|
|
pub struct Query<'a, 'b> {
|
2024-01-05 16:46:16 +00:00
|
|
|
pub frozen: &'a FrozenMailbox,
|
|
|
|
pub emails: &'b [UniqueIdent],
|
2024-01-05 17:59:19 +00:00
|
|
|
pub scope: QueryScope,
|
|
|
|
}
|
|
|
|
|
2024-01-08 20:18:45 +00:00
|
|
|
#[derive(Debug)]
|
2024-01-05 17:59:19 +00:00
|
|
|
pub enum QueryScope {
|
|
|
|
Index,
|
|
|
|
Partial,
|
|
|
|
Full,
|
2024-01-05 16:46:16 +00:00
|
|
|
}
|
2024-01-06 17:01:44 +00:00
|
|
|
impl QueryScope {
|
|
|
|
pub fn union(&self, other: &QueryScope) -> QueryScope {
|
|
|
|
match (self, other) {
|
|
|
|
(QueryScope::Full, _) | (_, QueryScope::Full) => QueryScope::Full,
|
|
|
|
(QueryScope::Partial, _) | (_, QueryScope::Partial) => QueryScope::Partial,
|
|
|
|
(QueryScope::Index, QueryScope::Index) => QueryScope::Index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-05 16:46:16 +00:00
|
|
|
|
2024-01-06 10:33:56 +00:00
|
|
|
impl<'a, 'b> Query<'a, 'b> {
|
2024-01-08 20:18:45 +00:00
|
|
|
pub async fn fetch(&self) -> Result<Vec<QueryResult>> {
|
2024-01-05 17:59:19 +00:00
|
|
|
match self.scope {
|
2024-01-08 20:32:55 +00:00
|
|
|
QueryScope::Index => Ok(self
|
|
|
|
.emails
|
|
|
|
.iter()
|
|
|
|
.map(|&uuid| QueryResult::IndexResult { uuid })
|
|
|
|
.collect()),
|
|
|
|
QueryScope::Partial => self.partial().await,
|
2024-01-05 17:59:19 +00:00
|
|
|
QueryScope::Full => self.full().await,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- functions below are private *for reasons*
|
|
|
|
|
2024-01-08 20:18:45 +00:00
|
|
|
async fn partial(&self) -> Result<Vec<QueryResult>> {
|
2024-01-05 16:46:16 +00:00
|
|
|
let meta = self.frozen.mailbox.fetch_meta(self.emails).await?;
|
|
|
|
let result = meta
|
|
|
|
.into_iter()
|
2024-01-08 20:18:45 +00:00
|
|
|
.zip(self.emails.iter())
|
2024-01-08 20:32:55 +00:00
|
|
|
.map(|(metadata, &uuid)| QueryResult::PartialResult { uuid, metadata })
|
2024-01-05 16:46:16 +00:00
|
|
|
.collect::<Vec<_>>();
|
2024-01-08 20:18:45 +00:00
|
|
|
|
2024-01-05 16:46:16 +00:00
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @FIXME WARNING: THIS CAN ALLOCATE A LOT OF MEMORY
|
|
|
|
/// AND GENERATE SO MUCH NETWORK TRAFFIC.
|
|
|
|
/// THIS FUNCTION SHOULD BE REWRITTEN, FOR EXAMPLE WITH
|
|
|
|
/// SOMETHING LIKE AN ITERATOR
|
2024-01-08 20:18:45 +00:00
|
|
|
async fn full(&self) -> Result<Vec<QueryResult>> {
|
2024-01-05 16:46:16 +00:00
|
|
|
let meta_list = self.partial().await?;
|
|
|
|
meta_list
|
|
|
|
.into_iter()
|
2024-01-06 10:33:56 +00:00
|
|
|
.map(|meta| async move {
|
|
|
|
let content = self
|
|
|
|
.frozen
|
|
|
|
.mailbox
|
|
|
|
.fetch_full(
|
|
|
|
*meta.uuid(),
|
|
|
|
&meta
|
|
|
|
.metadata()
|
|
|
|
.expect("meta to be PartialResult")
|
|
|
|
.message_key,
|
|
|
|
)
|
|
|
|
.await?;
|
2024-01-05 17:59:19 +00:00
|
|
|
|
|
|
|
Ok(meta.into_full(content).expect("meta to be PartialResult"))
|
2024-01-05 16:46:16 +00:00
|
|
|
})
|
2024-01-08 20:18:45 +00:00
|
|
|
.collect::<FuturesOrdered<_>>()
|
2024-01-05 16:46:16 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.await
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Result<Vec<_>, _>>()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 20:18:45 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum QueryResult {
|
2024-01-05 17:59:19 +00:00
|
|
|
IndexResult {
|
|
|
|
uuid: UniqueIdent,
|
|
|
|
},
|
|
|
|
PartialResult {
|
|
|
|
uuid: UniqueIdent,
|
|
|
|
metadata: MailMeta,
|
|
|
|
},
|
|
|
|
FullResult {
|
|
|
|
uuid: UniqueIdent,
|
|
|
|
metadata: MailMeta,
|
|
|
|
content: Vec<u8>,
|
2024-01-06 10:33:56 +00:00
|
|
|
},
|
2024-01-05 16:46:16 +00:00
|
|
|
}
|
2024-01-08 20:18:45 +00:00
|
|
|
impl QueryResult {
|
2024-01-05 17:59:19 +00:00
|
|
|
pub fn uuid(&self) -> &UniqueIdent {
|
|
|
|
match self {
|
|
|
|
Self::IndexResult { uuid, .. } => uuid,
|
|
|
|
Self::PartialResult { uuid, .. } => uuid,
|
|
|
|
Self::FullResult { uuid, .. } => uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 20:18:45 +00:00
|
|
|
pub fn metadata(&self) -> Option<&MailMeta> {
|
2024-01-05 17:59:19 +00:00
|
|
|
match self {
|
|
|
|
Self::IndexResult { .. } => None,
|
|
|
|
Self::PartialResult { metadata, .. } => Some(metadata),
|
|
|
|
Self::FullResult { metadata, .. } => Some(metadata),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 10:07:53 +00:00
|
|
|
#[allow(dead_code)]
|
2024-01-08 20:18:45 +00:00
|
|
|
pub fn content(&self) -> Option<&[u8]> {
|
2024-01-05 17:59:19 +00:00
|
|
|
match self {
|
|
|
|
Self::FullResult { content, .. } => Some(content),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_full(self, content: Vec<u8>) -> Option<Self> {
|
|
|
|
match self {
|
2024-01-08 20:32:55 +00:00
|
|
|
Self::PartialResult { uuid, metadata } => Some(Self::FullResult {
|
2024-01-06 10:33:56 +00:00
|
|
|
uuid,
|
|
|
|
metadata,
|
|
|
|
content,
|
|
|
|
}),
|
2024-01-05 17:59:19 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2024-01-05 16:46:16 +00:00
|
|
|
}
|