main.rs 8.23 KB
Newer Older
1
2
3
extern crate clap;
extern crate dbcop;

Ranadeep Biswas's avatar
Ranadeep Biswas committed
4
5
6
// use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

use clap::{App, AppSettings, Arg, SubCommand};
7
use std::fs::File;
Ranadeep Biswas's avatar
Ranadeep Biswas committed
8
9
10
use std::io::{BufReader, BufWriter};

use std::path::Path;
11

Ranadeep Biswas's avatar
Ranadeep Biswas committed
12
13
14
15
16
use std::fs;

use dbcop::db::history::generate_mult_histories;
use dbcop::db::history::History;
use dbcop::verifier::Verifier;
17
18

fn main() {
Ranadeep Biswas's avatar
Ranadeep Biswas committed
19
    let app = App::new("dbcop")
20
21
        .version("1.0")
        .author("Ranadeep")
Ranadeep Biswas's avatar
Ranadeep Biswas committed
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
        .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
49
                        .long("nvar")
Ranadeep Biswas's avatar
Ranadeep Biswas committed
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
82
83
                        .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
84
85
86
87
88
89
90
91
92
93
94
95
96
                        .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(
Ranadeep Biswas's avatar
Misc    
Ranadeep Biswas committed
97
98
99
100
101
                    Arg::with_name("consistency")
                        .long("cons")
                        .short("c")
                        .takes_value(true)
                        .help("Check for mentioned consistency"),
Ranadeep Biswas's avatar
Ranadeep Biswas committed
102
103
104
105
106
107
108
                )
                .about("Verifies histories"),
        ])
        .setting(AppSettings::SubcommandRequired);

    let app_matches = app.get_matches();

Ranadeep Biswas's avatar
Ranadeep Biswas committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    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
Misc    
Ranadeep Biswas committed
126
                let file = File::create(dir.join(format!("hist-{:05}.bincode", hist.get_id())))
Ranadeep Biswas's avatar
Ranadeep Biswas committed
127
                    .expect("couldn't create bincode file");
Ranadeep Biswas's avatar
Misc    
Ranadeep Biswas committed
128
                let buf_writer = BufWriter::new(file);
Ranadeep Biswas's avatar
Ranadeep Biswas committed
129
130
                bincode::serialize_into(buf_writer, &hist)
                    .expect("dumping history to bincode file went wrong");
Ranadeep Biswas's avatar
Ranadeep Biswas committed
131
            }
Ranadeep Biswas's avatar
Ranadeep Biswas committed
132
        }
Ranadeep Biswas's avatar
Ranadeep Biswas committed
133
        ("verify", Some(matches)) => {
Ranadeep Biswas's avatar
Misc    
Ranadeep Biswas committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
            // let v_dir = Path::new(matches.value_of("v_directory").unwrap());
            //
            // let histories: Vec<History> = fs::read_dir(v_dir)
            //     .expect("couldn't read history directory")
            //     .filter_map(|entry_res| match entry_res {
            //         Ok(ref entry) if entry.path().is_dir() => {
            //             let file = File::open(entry.path().join("history.bincode")).unwrap();
            //             let buf_reader = BufReader::new(file);
            //             Some(bincode::deserialize_from(buf_reader).unwrap())
            //         }
            //         _ => 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");
            //     }
            //
            //     verifier.sat(matches.is_present("sat"));
            //     verifier.bicomponent(matches.is_present("bicomponent"));
            //
            //     if !verifier.transactional_history_verify(hist.get_data()) {
            //         println!("hist-{:05} failed", hist.get_id());
            //     } else {
            //         println!("hist-{:05} done", hist.get_id());
            //     }
            // });

            let v_path =
                Path::new(matches.value_of("v_directory").unwrap()).join("history.bincode");
            let file = File::open(v_path).unwrap();
            let buf_reader = BufReader::new(file);
            let hist: History = bincode::deserialize_from(buf_reader).unwrap();
Ranadeep Biswas's avatar
Ranadeep Biswas committed
182
183
184
185
186
187
188

            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");
            }

Ranadeep Biswas's avatar
Misc    
Ranadeep Biswas committed
189
            // let curr_dir = o_dir.join(format!("hist-{:05}", hist.get_id()));
Ranadeep Biswas's avatar
Ranadeep Biswas committed
190

Ranadeep Biswas's avatar
Misc    
Ranadeep Biswas committed
191
            let mut verifier = Verifier::new(o_dir.to_path_buf());
Ranadeep Biswas's avatar
Ranadeep Biswas committed
192

Ranadeep Biswas's avatar
Misc    
Ranadeep Biswas committed
193
194
195
196
197
198
199
            match matches.value_of("consistency") {
                Some("cc") => verifier.model("cc"),
                Some("si") => verifier.model("si"),
                Some("ser") => verifier.model("ser"),
                None => verifier.model(""),
                _ => unreachable!(),
            };
Ranadeep Biswas's avatar
Ranadeep Biswas committed
200

Ranadeep Biswas's avatar
Misc    
Ranadeep Biswas committed
201
202
            verifier.sat(matches.is_present("sat"));
            verifier.bicomponent(matches.is_present("bicomponent"));
Ranadeep Biswas's avatar
Ranadeep Biswas committed
203

Ranadeep Biswas's avatar
Misc    
Ranadeep Biswas committed
204
205
206
207
208
209
210
211
            match verifier.verify(hist.get_data()) {
                Some(level) => println!(
                    "hist-{:05} failed - minimum level failed {:?}",
                    hist.get_id(),
                    level
                ),
                None => println!("hist-{:05} done", hist.get_id()),
            }
Ranadeep Biswas's avatar
Ranadeep Biswas committed
212
213
        }
        _ => unreachable!(),
Ranadeep Biswas's avatar
Ranadeep Biswas committed
214
    }
215
}