aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjan <jan@ruken.pw>2016-10-01 17:25:51 (UTC)
committerjan <jan@ruken.pw>2016-10-01 17:26:01 (UTC)
commitf6fcd9bd7c17b1ac09e34a64c2545bc5a59da0a4 (patch)
treeb318b349e260e46247a34fb059c4231f3d4d2881
parent463513b6c24d2a261413879233b3b66d1a303659 (diff)
multithreading
-rw-r--r--src/main.rs53
1 files changed, 37 insertions, 16 deletions
diff --git a/src/main.rs b/src/main.rs
index c91861a..cb8b02d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,12 +6,14 @@ extern crate regex;
6extern crate yaml_rust; 6extern crate yaml_rust;
7 7
8extern crate walkdir; 8extern crate walkdir;
9use walkdir::WalkDir; 9use walkdir::{WalkDir, DirEntry};
10 10
11use std::io::prelude::*; 11use std::io::prelude::*;
12use std::fs::File; 12use std::fs::File;
13use std::env; 13use std::env;
14use std::path::Path; 14use std::path::Path;
15use std::sync::{Arc, Mutex};
16use std::thread;
15 17
16mod pre_process; 18mod pre_process;
17mod section; 19mod section;
@@ -23,6 +25,8 @@ mod tags;
23mod dl_list; 25mod dl_list;
24mod tiles; 26mod tiles;
25 27
28static MAX_THREADS: u32 = 12;
29
26fn main() { 30fn main() {
27 let raw_files = env::var("RAW_FILES").unwrap_or("characters".into()); 31 let raw_files = env::var("RAW_FILES").unwrap_or("characters".into());
28 let out_files = env::var("OUT").unwrap_or("json".into()); 32 let out_files = env::var("OUT").unwrap_or("json".into());
@@ -30,24 +34,41 @@ fn main() {
30 let out_path = Path::new(&out_files); 34 let out_path = Path::new(&out_files);
31 35
32 36
33 for entry in WalkDir::new(base_path).min_depth(1).into_iter().filter_map(|e| e.ok()) { 37
34 let mut f = File::open(entry.path()).expect("could not open file"); 38 let files: Arc<Mutex<Vec<DirEntry>>> = Arc::new(Mutex::new(WalkDir::new(base_path).min_depth(1).into_iter().filter_map(|e| e.ok()).collect()));
35 let mut buf = String::new(); 39
36 if let Err(_) = f.read_to_string(&mut buf) { 40 for 1..MAX_THREADS {
37 println!("invalid file: {}", entry.path().to_str().unwrap()); 41
38 continue; 42 std::thread(move || {
39 } 43 let entry: Option<DirEntry> = None;
44 {
45 entry = files.lock().unwrap().pop();
46
47 if entry.is_none() {
48 println!("thread finished");
49 return;
50 }
51 }
52 let entry = entry.unwrap();
53
54 let mut f = File::open(entry.path()).expect("could not open file");
55 let mut buf = String::new();
56 if let Err(_) = f.read_to_string(&mut buf) {
57 println!("invalid file: {}", entry.path().to_str().unwrap());
58 continue;
59 }
40 60
41 let buf = pre_process::strip_irrelevant_content(&buf); 61 let buf = pre_process::strip_irrelevant_content(&buf);
42 62
43 let mut char = Character::new(); 63 let mut char = Character::new();
44 char.parse(&buf); 64 char.parse(&buf);
45 65
46 let json = serde_json::to_string(&char).unwrap(); 66 let json = serde_json::to_string(&char).unwrap();
47 67
48 let out_file = out_path.join(entry.file_name().to_str().unwrap().replace("html", "json")); 68 let out_file = out_path.join(entry.file_name().to_str().unwrap().replace("html", "json"));
49 let mut o = File::create(&out_file).unwrap(); 69 let mut o = File::create(&out_file).unwrap();
50 o.write_all(json.as_bytes()).unwrap(); 70 o.write_all(json.as_bytes()).unwrap();
51 println!("{:?}", out_file); 71 println!("{:?}", out_file);
72 });
52 } 73 }
53} 74}