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
use crate::dtf::update::Update;

/// (`dbname`, Update)
/// (`dname`, Vec<Update>)
#[derive(Clone)]
pub enum InsertCommand {
    /// Add a single `Update` to database
    Add(String, Update),
    /// Add a batch of `Update`s to database
    BulkAdd(String, Vec<Update>),
}

impl InsertCommand {
    /// consumes InsertCommand and create into command string
    pub fn into_string(self) -> Vec<String> {
        match self {
            InsertCommand::Add(dbname, up) => {
                let is_trade = if up.is_trade {"t"} else {"f"};
                let is_bid = if up.is_bid {"t"} else {"f"};
                let s = format!("ADD {}, {}, {}, {}, {}, {}; INTO {}\n",
                                up.ts, up.seq, is_trade, is_bid, up.price, up.size, dbname
                );
                vec![s]
            },
            // TODO: phase out BulkAdd
            InsertCommand::BulkAdd(dbname, ups) => {
                let mut cmds = vec![];
                for up in ups {
                    let is_trade = if up.is_trade {"t"} else {"f"};
                    let is_bid = if up.is_bid {"t"} else {"f"};
                    cmds.push(format!("ADD {}, {}, {}, {}, {}, {}; INTO {}\n",
                            up.ts, up.seq, is_trade, is_bid, up.price, up.size, dbname));
                }
                cmds
            }
        }
    }
}