1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
use anyhow::{Result, anyhow};
use super::mailbox::MailMeta;
use super::snapshot::FrozenMailbox;
use super::unique_ident::UniqueIdent;
use super::uidindex::IndexEntry;
use futures::stream::{FuturesUnordered, StreamExt};
/// Query is in charge of fetching efficiently
/// requested data for a list of emails
pub struct Query<'a,'b> {
pub frozen: &'a FrozenMailbox,
pub emails: &'b [UniqueIdent],
}
impl<'a,'b> Query<'a,'b> {
pub fn index(&self) -> Result<Vec<IndexResult>> {
self
.emails
.iter()
.map(|uuid| {
self
.frozen
.snapshot
.table
.get(uuid)
.map(|index| IndexResult { uuid: *uuid, index })
.ok_or(anyhow!("missing email in index"))
})
.collect::<Result<Vec<_>, _>>()
}
pub async fn partial(&self) -> Result<Vec<PartialResult>> {
let meta = self.frozen.mailbox.fetch_meta(self.emails).await?;
let result = meta
.into_iter()
.zip(self.index()?)
.map(|(metadata, index)| PartialResult { uuid: index.uuid, index: index.index, metadata })
.collect::<Vec<_>>();
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
pub async fn full(&self) -> Result<Vec<FullResult>> {
let meta_list = self.partial().await?;
meta_list
.into_iter()
.map(|meta| async move {
let content = self.frozen.mailbox.fetch_full(meta.uuid, &meta.metadata.message_key).await?;
Ok(FullResult {
uuid: meta.uuid,
index: meta.index,
metadata: meta.metadata,
content,
})
})
.collect::<FuturesUnordered<_>>()
.collect::<Vec<_>>()
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()
}
}
pub struct IndexResult<'a> {
pub uuid: UniqueIdent,
pub index: &'a IndexEntry,
}
pub struct PartialResult<'a> {
pub uuid: UniqueIdent,
pub index: &'a IndexEntry,
pub metadata: MailMeta,
}
pub struct FullResult<'a> {
pub uuid: UniqueIdent,
pub index: &'a IndexEntry,
pub metadata: MailMeta,
pub content: Vec<u8>,
}
|