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
//! Sink with msgpack backend

use serde::Serialize;
use std::sync::mpsc::{channel, Sender};
use std::thread::{spawn, JoinHandle};
use std::io::BufWriter;
use std::path::{Path, PathBuf};
use std::fs::File;

use super::Sink;

/// Sink with a file encoded by msgpack
///
/// Each data send to this sink will be serialized into msgpack binary,
/// and write to a file. You must use stream api to read this file.
#[derive(Debug, Clone)]
pub struct MsgpackSink {
    filename: PathBuf,
}

impl MsgpackSink {
    /// initialize with a path for msgpack file
    pub fn from_path(path: &Path) -> Self {
        Self {
            filename: PathBuf::from(path),
        }
    }

    /// initialize with a path for msgpack file
    pub fn from_str(path: &str) -> Self {
        Self {
            filename: From::from(path.to_string()),
        }
    }
}

impl<Doc: 'static + Send + Serialize> Sink<Doc> for MsgpackSink {
    fn run(self) -> (Sender<Doc>, JoinHandle<()>) {
        let (s, r) = channel::<Doc>();
        let th = spawn(move || {
            let mut buf = BufWriter::new(File::create(self.filename).ok().unwrap());
            let mut enc = ::rmp_serde::Serializer::new(&mut buf);
            loop {
                match r.recv() {
                    Ok(doc) => doc.serialize(&mut enc).unwrap(),
                    Err(_) => break,
                }
            }
        });
        (s, th)
    }
}