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
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
84
85
86
87
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#[macro_use]
extern crate serde_derive;
extern crate bson;
extern crate mongodb;
extern crate serde;
extern crate serde_json;
use bson::ordered::OrderedDocument;
use mongodb::{Client, ClientInner, ThreadedClient};
use mongodb::db::ThreadedDatabase;
use std::env::current_dir;
use std::fs;
use std::fs::{File};
use std::path::PathBuf;
use std::process;
use std::sync::Arc;
pub mod util;
const ASSEMBLY: &'static str = "assembly";
const DAIL: &'static str = "dail";
const WESTMINSTER: &'static str = "westminster";
const CONSTITUENCIES: &'static str = "constituencies";
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
#[serde(default)]
struct Area {
area_type: String,
candidates: Vec<Candidate>,
counts_held: Option<i8>,
description: String,
election_type: String,
electorate: Option<i32>,
name: String,
quota: Option<i32>,
spoilt: Option<i16>,
turnout: Option<i32>,
valid: Option<i32>,
year: i16,
}
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
#[serde(default)]
struct Candidate {
counts: Vec<i32>,
elected: bool,
first_pref_pc: Option<f32>,
full_name: String,
party: String,
transfers: Option<i32>,
transfers_pc: Option<f32>,
}
pub enum ElectionDatabase {
Assembly(Arc<ClientInner>, String),
Dail(Arc<ClientInner>, String),
Westminster(Arc<ClientInner>, String),
}
impl ElectionDatabase {
fn from_str(input: &str, db_name: Option<&str>) -> Option<Self> {
let mut db = String::new();
match db_name {
Some(s) => db.clone_from(&s.to_string()),
None => {
let unique = util::mini_hash();
db.clone_from(&format!("{}_{}", input, unique));
}
};
let client = Client::with_uri("mongodb://127.0.0.1:27017")
.expect("Failed to initialize standalone client.");
match input.as_ref() {
ASSEMBLY => Some(ElectionDatabase::Assembly(client, db)),
DAIL => Some(ElectionDatabase::Dail(client, db)),
WESTMINSTER => Some(ElectionDatabase::Westminster(client, db)),
_ => None,
}
}
fn get_database(&self) -> String {
match *self {
ElectionDatabase::Assembly(_, ref database) => database.to_string(),
ElectionDatabase::Dail(_, ref database) => database.to_string(),
ElectionDatabase::Westminster(_, ref database) => database.to_string(),
}
}
}
trait CreateDatabase {
fn create_constituencies(&self);
fn walk_constituency_subdirs(&self, dir: &PathBuf) -> Option<Vec<Area>>;
fn walk_json_files(&self, subdir: &PathBuf) -> Option<Vec<Area>>;
fn load_json(&self, jsonfile: &PathBuf) -> Option<Area>;
fn create_documents<'a,T>(&self, areas: &'a Vec<T>) where T: serde::Serialize;
}
impl CreateDatabase for ElectionDatabase {
fn create_constituencies(&self) {
let constituencies_dir = current_dir().unwrap().join(CONSTITUENCIES);
match constituencies_dir.exists() {
true => match self.walk_constituency_subdirs(&constituencies_dir) {
Some(ref a) => self.create_documents(a),
None => {
eprintln!("{}","[mongoloid] No constituencies to create.");
process::exit(1);
}
},
false => {
eprintln!("[mongoloid] Current working directory is missing '{}' sub-directory. Exiting.", CONSTITUENCIES);
process::exit(1);
},
}
}
fn walk_constituency_subdirs(&self, constituencies_dir: &PathBuf) -> Option<Vec<Area>> {
let dir = fs::read_dir(constituencies_dir).unwrap();
let areas: Vec<Area> = dir.filter_map(|entry| {
let entry = entry.unwrap();
let subdir: PathBuf = entry.path();
match subdir.is_dir() {
true => self.walk_json_files(&subdir),
false => None
}
}).collect::<Vec<Vec<Area>>>()
.iter()
.flat_map(|a| a.iter().map(Clone::clone))
.collect();
match areas.is_empty() {
true => None,
false => Some(areas)
}
}
fn walk_json_files(&self, subdir: &PathBuf) -> Option<Vec<Area>> {
let mut areas: Vec<Area> = Vec::new();
match fs::read_dir(subdir) {
Err(why) => panic!("[mongoloid] Couldn't read directory. {}", why),
Ok(dir) => {
let a = dir.filter_map(|entry| {
let entry = entry.unwrap();
let jsonfile: PathBuf = entry.path();
match jsonfile.is_file() {
true => self.load_json(&jsonfile),
false => None
}
}).collect::<Vec<Area>>();
if !a.is_empty() {
areas.extend(a);
}
},
};
match areas.is_empty() {
true => None,
false => Some(areas)
}
}
fn load_json(&self, jsonfile: &PathBuf) -> Option<Area>{
println!("[mongoloid] Processing {:?}", jsonfile);
match File::open(&jsonfile) {
Err(why) => panic!("Couldn't open file: {}", why),
Ok(file) => {
let area: Option<Area> = match serde_json::from_reader(file) {
Ok(a) => Some(a),
Err(why) => panic!("[mongoloid] Couldn't deserialise: {}", why),
};
return area;
},
}
}
fn create_documents<'a,T>(&self, areas: &'a Vec<T>) where T: serde::Serialize {
println!("[mongoloid] Creating database \"{}\"...", self.get_database());
let docs: Vec<OrderedDocument> = areas.iter().filter_map(|a| {
match bson::to_bson(a) {
Err(why) => {
eprintln!("[mongoloid] Couldn't convert row to BSON: {}", why);
None
},
Ok(d) => {
if let bson::Bson::Document(bson_doc) = d {
return Some(bson_doc);
}
None
},
}
}).collect();
if let ElectionDatabase::Dail(ref client, ref db) = *self {
let coll = client.db(db).collection("area");
coll.insert_many(docs, None)
.ok().expect("Failed to insert document.");
}
}
}
pub fn create_database(db_name: Option<&str>) -> Result<(),String> {
match ElectionDatabase::from_str(&util::get_cwd_name(), db_name){
Some(et) => {
println!("[mongoloid] {}", "Creating database");
et.create_constituencies();
println!("[mongoloid] Created database \"{}\"", et.get_database());
},
None => {
eprintln!("[mongoloid] Current working directory {:?} is not an expected election type. Exiting.", current_dir().unwrap());
process::exit(1);
},
};
Ok(())
}
#[cfg(test)]
mod tests {
extern crate bson;
extern crate mongodb;
use bson::{Bson, Document};
use mongodb::{Client, ThreadedClient};
use mongodb::db::ThreadedDatabase;
use std::env::set_current_dir;
#[test]
#[allow(unused_must_use)]
fn database_builds() {
let test_dir = super::current_dir().unwrap().join("test");
let dail_dir = test_dir.join("dail");
let client = Client::with_uri("mongodb://127.0.0.1:27017")
.expect("Failed to initialize standalone client.");
let coll = client.db("test").collection("area");
set_current_dir(&dail_dir);
coll.delete_many(Document::new(), None);
super::create_database(Some("test"));
let result = coll.find_one(None, None).expect(
"Failed to execute find command.",
);
match result.unwrap().get("valid") {
Some(&Bson::I32(valid)) => assert_eq!(44034 as i32, valid),
_ => panic!("Expected Bson::I32!"),
};
coll.delete_many(Document::new(), None);
}
}