Add Envelope parsing caching

Concerns #28
master
Manos Pitsidianakis 2018-08-13 09:25:48 +03:00
parent 5889494e9e
commit 5d0b7fa903
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
8 changed files with 81 additions and 30 deletions

View File

@ -17,7 +17,8 @@ memmap = "0.5.2"
nom = "3.2.0" nom = "3.2.0"
notify = "4.0.1" notify = "4.0.1"
notify-rust = "^3" notify-rust = "^3"
serde = "^1.0.8"
serde_derive = "^1.0.8"
termion = "1.5.1" termion = "1.5.1"
xdg = "2.1.0" xdg = "2.1.0"
serde = "1.0.71"
serde_derive = "1.0.71"
bincode = "1.0.1"

View File

@ -19,6 +19,10 @@
* along with meli. If not, see <http://www.gnu.org/licenses/>. * along with meli. If not, see <http://www.gnu.org/licenses/>.
*/ */
extern crate xdg;
extern crate serde_derive;
extern crate bincode;
use async::*; use async::*;
use conf::AccountSettings; use conf::AccountSettings;
use error::{MeliError, Result}; use error::{MeliError, Result};
@ -43,6 +47,8 @@ extern crate crossbeam;
use memmap::{Mmap, Protection}; use memmap::{Mmap, Protection};
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;
use std::fs; use std::fs;
use std::io;
use std::io::Read;
use std::sync::{Mutex, Arc}; use std::sync::{Mutex, Arc};
use std::hash::Hasher; use std::hash::Hasher;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -169,6 +175,7 @@ impl<'a> BackendOp for MaildirOp {
/// Maildir backend https://cr.yp.to/proto/maildir.html /// Maildir backend https://cr.yp.to/proto/maildir.html
#[derive(Debug)] #[derive(Debug)]
pub struct MaildirType { pub struct MaildirType {
name: String,
folders: Vec<MaildirFolder>, folders: Vec<MaildirFolder>,
hash_index: Arc<Mutex<FnvHashMap<u64, (usize, String)>>>, hash_index: Arc<Mutex<FnvHashMap<u64, (usize, String)>>>,
@ -278,6 +285,7 @@ impl MaildirType {
} }
folders[0].children = recurse_folders(&mut folders, &path); folders[0].children = recurse_folders(&mut folders, &path);
MaildirType { MaildirType {
name: f.name().to_string(),
folders, folders,
hash_index: Arc::new(Mutex::new(FnvHashMap::with_capacity_and_hasher(0, Default::default()))), hash_index: Arc::new(Mutex::new(FnvHashMap::with_capacity_and_hasher(0, Default::default()))),
path: f.root_folder().to_string(), path: f.root_folder().to_string(),
@ -294,6 +302,7 @@ impl MaildirType {
pub fn multicore(&mut self, cores: usize, folder: &Folder) -> Async<Result<Vec<Envelope>>> { pub fn multicore(&mut self, cores: usize, folder: &Folder) -> Async<Result<Vec<Envelope>>> {
let mut w = AsyncBuilder::new(); let mut w = AsyncBuilder::new();
let cache_dir = xdg::BaseDirectories::with_profile("meli", &self.name).unwrap();
let handle = { let handle = {
let tx = w.tx(); let tx = w.tx();
// TODO: Avoid clone // TODO: Avoid clone
@ -306,6 +315,7 @@ impl MaildirType {
thread::Builder::new() thread::Builder::new()
.name(name) .name(name)
.spawn(move || { .spawn(move || {
let cache_dir = cache_dir.clone();
let mut path = PathBuf::from(path); let mut path = PathBuf::from(path);
path.push("cur"); path.push("cur");
let iter = path.read_dir()?; let iter = path.read_dir()?;
@ -322,12 +332,14 @@ impl MaildirType {
let mut threads = Vec::with_capacity(cores); let mut threads = Vec::with_capacity(cores);
if !files.is_empty() { if !files.is_empty() {
crossbeam::scope(|scope| { crossbeam::scope(|scope| {
let cache_dir = cache_dir.clone();
let chunk_size = if count / cores > 0 { let chunk_size = if count / cores > 0 {
count / cores count / cores
} else { } else {
count count
}; };
for chunk in files.chunks(chunk_size) { for chunk in files.chunks(chunk_size) {
let cache_dir = cache_dir.clone();
let mut tx = tx.clone(); let mut tx = tx.clone();
let map = map.clone(); let map = map.clone();
let s = scope.spawn(move || { let s = scope.spawn(move || {
@ -338,6 +350,24 @@ impl MaildirType {
let map = map.clone(); let map = map.clone();
let len = c.len(); let len = c.len();
for file in c { for file in c {
let ri = file.rfind("/").unwrap() + 1;
let file_name = &file[ri..];
if let Some(cached) = cache_dir.find_cache_file(file_name) {
// TODO:: error checking
let reader = io::BufReader::new(fs::File::open(cached).unwrap());
let env: Envelope = bincode::deserialize_from(reader).unwrap();
{
let mut map = map.lock().unwrap();
let hash = env.hash();
if (*map).contains_key(&hash) {
continue;
}
(*map).insert(hash, (0, file.to_string()));
local_r.push(env);
continue;
}
}
let e_copy = file.to_string(); let e_copy = file.to_string();
/* /*
* get hash * get hash
@ -350,10 +380,11 @@ impl MaildirType {
{ {
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();
let hash = { let hash = {
let slice = Mmap::open_path(&e_copy, Protection::Read).unwrap(); let mut buf = Vec::new();
let mut f = fs::File::open(&e_copy).unwrap();
f.read_to_end(&mut buf);
/* Unwrap is safe since we use ? above. */ /* Unwrap is safe since we use ? above. */
hasher.write( hasher.write(&buf);
unsafe { slice.as_slice() });
hasher.finish() hasher.finish()
}; };
{ {
@ -366,7 +397,19 @@ impl MaildirType {
// TODO: Check cache // TODO: Check cache
let op = Box::new(MaildirOp::new(hash, map.clone())); let op = Box::new(MaildirOp::new(hash, map.clone()));
if let Some(mut e) = Envelope::from_token(op, hash) { if let Some(mut e) = Envelope::from_token(op, hash) {
if let Ok(cached) = cache_dir.place_cache_file(file_name) {
let f = match fs::File::create(cached) {
Ok(f) => f,
Err(e) => {
panic!("{}",e);
}
};
let writer = io::BufWriter::new(f);
bincode::serialize_into(writer, &e).unwrap();
}
local_r.push(e); local_r.push(e);
} else { } else {
continue; continue;
} }

View File

@ -64,6 +64,7 @@ impl Backends {
} }
self.map[key]() self.map[key]()
} }
pub fn register(&mut self, key: String, backend: Box<Fn() -> BackendCreator>) -> () { pub fn register(&mut self, key: String, backend: Box<Fn() -> BackendCreator>) -> () {
if self.map.contains_key(&key) { if self.map.contains_key(&key) {
panic!("{} is an already registered backend", key); panic!("{} is an already registered backend", key);

View File

@ -2,7 +2,7 @@ use mailbox::email::parser::BytesExt;
use std::fmt::{Display, Formatter, Result as FmtResult}; use std::fmt::{Display, Formatter, Result as FmtResult};
use std::str; use std::str;
#[derive(Clone, Copy, Debug, PartialEq, Serialize)] #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum Charset { pub enum Charset {
Ascii, Ascii,
UTF8, UTF8,
@ -52,7 +52,7 @@ impl<'a> From<&'a [u8]> for Charset {
} }
} }
#[derive(Clone, Debug, PartialEq, Serialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum MultipartType { pub enum MultipartType {
Mixed, Mixed,
Alternative, Alternative,
@ -73,7 +73,7 @@ impl Display for MultipartType {
} }
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ContentType { pub enum ContentType {
Text { charset: Charset }, Text { charset: Charset },
Multipart { boundary: Vec<u8> }, Multipart { boundary: Vec<u8> },
@ -108,7 +108,7 @@ impl ContentType {
} }
} }
#[derive(Clone, Debug, PartialEq, Serialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum ContentSubType { pub enum ContentSubType {
Plain, Plain,
Html, Html,
@ -134,7 +134,7 @@ impl Display for ContentSubType {
} }
} }
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ContentTransferEncoding { pub enum ContentTransferEncoding {
_8Bit, _8Bit,
_7Bit, _7Bit,

View File

@ -25,7 +25,7 @@ use std::str;
pub use mailbox::email::attachment_types::*; pub use mailbox::email::attachment_types::*;
#[derive(Clone, Serialize)] #[derive(Clone, Serialize, Deserialize)]
pub enum AttachmentType { pub enum AttachmentType {
Data { Data {
tag: Vec<u8>, tag: Vec<u8>,
@ -71,7 +71,7 @@ pub struct AttachmentBuilder {
raw: Vec<u8>, raw: Vec<u8>,
} }
#[derive(Clone, Serialize)] #[derive(Clone, Serialize, Deserialize)]
pub struct Attachment { pub struct Attachment {
content_type: (ContentType, ContentSubType), content_type: (ContentType, ContentSubType),
content_transfer_encoding: ContentTransferEncoding, content_transfer_encoding: ContentTransferEncoding,

View File

@ -42,21 +42,21 @@ use std::string::String;
use chrono; use chrono;
use chrono::TimeZone; use chrono::TimeZone;
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GroupAddress { pub struct GroupAddress {
raw: Vec<u8>, raw: Vec<u8>,
display_name: StrBuilder, display_name: StrBuilder,
mailbox_list: Vec<Address>, mailbox_list: Vec<Address>,
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MailboxAddress { pub struct MailboxAddress {
raw: Vec<u8>, raw: Vec<u8>,
display_name: StrBuilder, display_name: StrBuilder,
address_spec: StrBuilder, address_spec: StrBuilder,
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Address { pub enum Address {
Mailbox(MailboxAddress), Mailbox(MailboxAddress),
Group(GroupAddress), Group(GroupAddress),
@ -108,7 +108,7 @@ impl fmt::Display for Address {
} }
/// Helper struct to return slices from a struct field on demand. /// Helper struct to return slices from a struct field on demand.
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
struct StrBuilder { struct StrBuilder {
offset: usize, offset: usize,
length: usize, length: usize,
@ -133,7 +133,7 @@ impl StrBuilder {
} }
/// `MessageID` is accessed through the `StrBuild` trait. /// `MessageID` is accessed through the `StrBuild` trait.
#[derive(Clone, Serialize)] #[derive(Clone, Serialize, Deserialize)]
pub struct MessageID(Vec<u8>, StrBuilder); pub struct MessageID(Vec<u8>, StrBuilder);
impl StrBuild for MessageID { impl StrBuild for MessageID {
@ -184,14 +184,14 @@ impl fmt::Debug for MessageID {
} }
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
struct References { struct References {
raw: Vec<u8>, raw: Vec<u8>,
refs: Vec<MessageID>, refs: Vec<MessageID>,
} }
bitflags! { bitflags! {
#[derive(Default, Serialize)] #[derive(Default, Serialize, Deserialize)]
pub struct Flag: u8 { pub struct Flag: u8 {
const PASSED = 0b00000001; const PASSED = 0b00000001;
const REPLIED = 0b00000010; const REPLIED = 0b00000010;
@ -236,7 +236,7 @@ impl EnvelopeBuilder {
/// Access to the underlying email object in the account's backend (for example the file or the /// Access to the underlying email object in the account's backend (for example the file or the
/// entry in an IMAP server) is given through `operation_token`. For more information see /// entry in an IMAP server) is given through `operation_token`. For more information see
/// `BackendOp`. /// `BackendOp`.
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Envelope { pub struct Envelope {
date: String, date: String,
from: Vec<Address>, from: Vec<Address>,

View File

@ -21,7 +21,7 @@
use super::*; use super::*;
use melib::mailbox::backends::BackendOp; //use melib::mailbox::backends::BackendOp;
mod compact; mod compact;
pub use self::compact::*; pub use self::compact::*;
@ -220,7 +220,7 @@ impl MailListing {
container, container,
&indentations, &indentations,
len, len,
context.accounts[self.cursor_pos.0].backend.operation(envelope.hash()) // context.accounts[self.cursor_pos.0].backend.operation(envelope.hash())
), ),
&mut content, &mut content,
fg_color, fg_color,
@ -421,7 +421,7 @@ impl MailListing {
container: &Container, container: &Container,
indentations: &[bool], indentations: &[bool],
idx_width: usize, idx_width: usize,
op: Box<BackendOp>, //op: Box<BackendOp>,
) -> String { ) -> String {
let has_sibling = container.has_sibling(); let has_sibling = container.has_sibling();
let has_parent = container.has_parent(); let has_parent = container.has_parent();
@ -458,10 +458,13 @@ impl MailListing {
if show_subject { if show_subject {
s.push_str(&format!("{:.85}", envelope.subject())); s.push_str(&format!("{:.85}", envelope.subject()));
} }
/*
* Very slow since we have to build all attachments
let attach_count = envelope.body(op).count_attachments(); let attach_count = envelope.body(op).count_attachments();
if attach_count > 1 { if attach_count > 1 {
s.push_str(&format!(" {}", attach_count - 1)); s.push_str(&format!(" {}", attach_count - 1));
} }
*/
s s
} }
fn format_date(envelope: &Envelope) -> String { fn format_date(envelope: &Envelope) -> String {

View File

@ -80,7 +80,7 @@ pub struct State<W: Write> {
startup_thread: Option<chan::Sender<bool>>, startup_thread: Option<chan::Sender<bool>>,
threads: FnvHashMap<thread::ThreadId, thread::JoinHandle<()>>, threads: FnvHashMap<thread::ThreadId, (chan::Sender<bool>, thread::JoinHandle<()>)>,
} }
impl<W: Write> Drop for State<W> { impl<W: Write> Drop for State<W> {
@ -131,12 +131,14 @@ impl State<std::io::Stdout> {
default => {}, default => {},
startup_rx.recv() -> _ => { startup_rx.recv() -> _ => {
sender.send(ThreadEvent::ThreadJoin(thread::current().id())); sender.send(ThreadEvent::ThreadJoin(thread::current().id()));
return; break;
} }
} }
sender.send(ThreadEvent::UIEvent(UIEventType::StartupCheck)); sender.send(ThreadEvent::UIEvent(UIEventType::StartupCheck));
thread::sleep(dur); thread::sleep(dur);
} }
startup_rx.recv();
return;
}) })
.unwrap() .unwrap()
}; };
@ -162,11 +164,11 @@ impl State<std::io::Stdout> {
input_thread, input_thread,
}, },
startup_thread: Some(startup_tx), startup_thread: Some(startup_tx.clone()),
threads: FnvHashMap::with_capacity_and_hasher(1, Default::default()), threads: FnvHashMap::with_capacity_and_hasher(1, Default::default()),
}; };
s.threads s.threads
.insert(startup_thread.thread().id(), startup_thread); .insert(startup_thread.thread().id(), (startup_tx.clone(), startup_thread));
write!( write!(
s.stdout(), s.stdout(),
"{}{}{}", "{}{}{}",
@ -218,15 +220,16 @@ impl State<std::io::Stdout> {
}) })
.unwrap() .unwrap()
}; };
self.startup_thread = Some(startup_tx); self.startup_thread = Some(startup_tx.clone());
self.threads self.threads
.insert(startup_thread.thread().id(), startup_thread); .insert(startup_thread.thread().id(), (startup_tx, startup_thread));
} }
/// If an owned thread returns a `ThreadEvent::ThreadJoin` event to `State` then it must remove /// If an owned thread returns a `ThreadEvent::ThreadJoin` event to `State` then it must remove
/// the thread from its list and `join` it. /// the thread from its list and `join` it.
pub fn join(&mut self, id: thread::ThreadId) { pub fn join(&mut self, id: thread::ThreadId) {
let handle = self.threads.remove(&id).unwrap(); let (tx, handle) = self.threads.remove(&id).unwrap();
tx.send(true);
handle.join().unwrap(); handle.join().unwrap();
} }