main.rs 6.92 KB
Newer Older
Ranadeep Biswas's avatar
Ranadeep Biswas committed
1
extern crate bincode;
Ranadeep Biswas's avatar
Ranadeep Biswas committed
2
extern crate chrono;
3
4
extern crate clap;
extern crate dbcop;
Ranadeep Biswas's avatar
Ranadeep Biswas committed
5
extern crate rayon;
6
7
extern crate serde_yaml;

Ranadeep Biswas's avatar
Ranadeep Biswas committed
8
9
10
// use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

use clap::{App, AppSettings, Arg, SubCommand};
11
use std::fs::File;
Ranadeep Biswas's avatar
Ranadeep Biswas committed
12
13
14
use std::io::{BufReader, BufWriter};

use std::path::Path;
15

Ranadeep Biswas's avatar
Ranadeep Biswas committed
16
17
18
19
20
use std::fs;

use dbcop::db::history::generate_mult_histories;
use dbcop::db::history::History;
use dbcop::verifier::Verifier;
21
22

fn main() {
Ranadeep Biswas's avatar
Ranadeep Biswas committed
23
    let app = App::new("dbcop")
24
25
        .version("1.0")
        .author("Ranadeep")
Ranadeep Biswas's avatar
Ranadeep Biswas committed
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
        .about("Generates histories or verifies executed histories")
        .subcommands(vec![
            SubCommand::with_name("generate")
                .arg(
                    Arg::with_name("g_directory")
                        .long("gen_dir")
                        .short("d")
                        .takes_value(true)
                        .required(true)
                        .help("Directory to generate histories"),
                )
                .arg(
                    Arg::with_name("n_history")
                        .long("nhist")
                        .short("h")
                        .default_value("10")
                        .help("Number of histories to generate"),
                )
                .arg(
                    Arg::with_name("n_node")
                        .long("nnode")
                        .short("n")
                        .default_value("3")
                        .help("Number of nodes per history"),
                )
                .arg(
                    Arg::with_name("n_variable")
Ranadeep Biswas's avatar
Ranadeep Biswas committed
53
                        .long("nvar")
Ranadeep Biswas's avatar
Ranadeep Biswas committed
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
82
83
84
85
86
87
                        .short("v")
                        .default_value("5")
                        .help("Number of variables per history"),
                )
                .arg(
                    Arg::with_name("n_transaction")
                        .long("ntxn")
                        .short("t")
                        .default_value("5")
                        .help("Number of transactions per history"),
                )
                .arg(
                    Arg::with_name("n_event")
                        .long("nevt")
                        .short("e")
                        .default_value("2")
                        .help("Number of events per transactions"),
                )
                .about("Generate histories"),
            SubCommand::with_name("verify")
                .arg(
                    Arg::with_name("v_directory")
                        .long("ver_dir")
                        .short("d")
                        .takes_value(true)
                        .required(true)
                        .help("Directory containing executed histories"),
                )
                .arg(
                    Arg::with_name("o_directory")
                        .long("out_dir")
                        .short("o")
                        .takes_value(true)
                        .required(true)
Ranadeep Biswas's avatar
Ranadeep Biswas committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
                        .help("Directory to output the results"),
                )
                .arg(
                    Arg::with_name("sat")
                        .long("sat")
                        .help("Use MiniSAT as backend"),
                )
                .arg(
                    Arg::with_name("bicomponent")
                        .long("bic")
                        .help("Use BiComponent"),
                )
                .arg(
                    Arg::with_name("serializable")
                        .long("ser")
                        .help("Check for Serializablity"),
                )
                .arg(
                    Arg::with_name("snapshot_isolation")
                        .long("si")
                        .help("Check for Snapshot Isolation"),
                )
                .arg(
                    Arg::with_name("causal")
                        .long("cc")
                        .help("Check for Causality"),
Ranadeep Biswas's avatar
Ranadeep Biswas committed
114
115
116
117
118
119
120
                )
                .about("Verifies histories"),
        ])
        .setting(AppSettings::SubcommandRequired);

    let app_matches = app.get_matches();

Ranadeep Biswas's avatar
Ranadeep Biswas committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    match app_matches.subcommand() {
        ("generate", Some(matches)) => {
            let dir = Path::new(matches.value_of("g_directory").unwrap());

            if !dir.is_dir() {
                fs::create_dir_all(dir).expect("failed to create directory");
            }

            let mut histories = generate_mult_histories(
                matches.value_of("n_history").unwrap().parse().unwrap(),
                matches.value_of("n_node").unwrap().parse().unwrap(),
                matches.value_of("n_variable").unwrap().parse().unwrap(),
                matches.value_of("n_transaction").unwrap().parse().unwrap(),
                matches.value_of("n_event").unwrap().parse().unwrap(),
            );

            for hist in histories.drain(..) {
Ranadeep Biswas's avatar
Ranadeep Biswas committed
138
139
                let mut file = File::create(dir.join(format!("hist-{:05}.bincode", hist.get_id())))
                    .expect("couldn't create bincode file");
Ranadeep Biswas's avatar
Ranadeep Biswas committed
140
                let mut buf_writer = BufWriter::new(file);
Ranadeep Biswas's avatar
Ranadeep Biswas committed
141
142
                bincode::serialize_into(buf_writer, &hist)
                    .expect("dumping history to bincode file went wrong");
Ranadeep Biswas's avatar
Ranadeep Biswas committed
143
            }
Ranadeep Biswas's avatar
Ranadeep Biswas committed
144
        }
Ranadeep Biswas's avatar
Ranadeep Biswas committed
145
146
147
148
        ("verify", Some(matches)) => {
            let v_dir = Path::new(matches.value_of("v_directory").unwrap());

            let histories: Vec<History> = fs::read_dir(v_dir)
Ranadeep Biswas's avatar
Ranadeep Biswas committed
149
                .expect("couldn't read history directory")
Ranadeep Biswas's avatar
Ranadeep Biswas committed
150
151
                .filter_map(|entry_res| match entry_res {
                    Ok(ref entry) if entry.path().is_dir() => {
Ranadeep Biswas's avatar
Ranadeep Biswas committed
152
                        let file = File::open(entry.path().join("history.bincode")).unwrap();
Ranadeep Biswas's avatar
Ranadeep Biswas committed
153
                        let buf_reader = BufReader::new(file);
Ranadeep Biswas's avatar
Ranadeep Biswas committed
154
                        Some(bincode::deserialize_from(buf_reader).unwrap())
Ranadeep Biswas's avatar
Ranadeep Biswas committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
                    }
                    _ => None,
                })
                .collect();

            let o_dir = Path::new(matches.value_of("o_directory").unwrap());

            if !o_dir.is_dir() {
                fs::create_dir_all(o_dir).expect("failed to create directory");
            }

            histories.iter().for_each(|ref hist| {
                let curr_dir = o_dir.join(format!("hist-{:05}", hist.get_id()));

                let mut verifier = Verifier::new(curr_dir.to_path_buf());

                if matches.is_present("causal") {
                    verifier.model("cc");
                } else if matches.is_present("snapshot_isolation") {
                    verifier.model("si");
                } else if matches.is_present("serializable") {
                    verifier.model("ser");
Ranadeep Biswas's avatar
Ranadeep Biswas committed
177
178
                }

Ranadeep Biswas's avatar
Ranadeep Biswas committed
179
180
                verifier.sat(matches.is_present("sat"));
                verifier.bicomponent(matches.is_present("bicomponent"));
Ranadeep Biswas's avatar
Ranadeep Biswas committed
181

Ranadeep Biswas's avatar
Ranadeep Biswas committed
182
183
184
185
186
187
                if !verifier.transactional_history_verify(hist.get_data()) {
                    println!("hist-{:05} failed", hist.get_id());
                }
            });
        }
        _ => unreachable!(),
Ranadeep Biswas's avatar
Ranadeep Biswas committed
188
    }
189
}