Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Slow query implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
whs committed Feb 5, 2016
1 parent e5e7d03 commit 76873d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 76 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ build = "build.rs"
[dependencies]
radix_trie = "*"
encoding = "0.2.32"
copperline = {version = "0.3.0", optional = true}
stopwatch = "0.0.6"

[profile.release]
lto = true

[features]
default = ["assertion", "wait_on_exit"]
assertion = []
dump_data = []
wait_on_exit = []
interactive = ["copperline"]
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,3 @@ $ cargo run --release SyllableDB-V1.dat
```

(Syllable database can be obtained from the class Facebook group)

Optional features:

- `assertion`: Show time used to perform certain operations
- `dump_data`: Print data while loading/searching
- `wait_on_exit`: Don't exit, instead wait for long time
- `interactive`: Interactive build: ask for file to read, ask for search queries

To enable features:

```
$ cargo run --features="assertion dump_data" SyllableDB-V1.dat
```
100 changes: 40 additions & 60 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern crate encoding;
extern crate radix_trie;
#[cfg(feature="interactive")]
extern crate copperline;
extern crate stopwatch;

mod model;

Expand All @@ -10,8 +9,11 @@ use std::process;
use std::thread::sleep;
use std::time::Duration;
use radix_trie::Trie;
#[cfg(feature="interactive")]
use copperline::Copperline;
use stopwatch::{Stopwatch};

const ITER_COUNT: i64 = 100;
const TOP_NO: usize = 30;
const LOOP_UPDATE_EVERY: f32 = 71.0;

macro_rules! print_err {
($($arg:tt)*) => (
Expand All @@ -34,24 +36,6 @@ fn usage(){
println!("Usage: {0} filename.dat", exe_name);
}

#[cfg(feature="interactive")]
fn get_args_fn() -> String{
let mut copperline = Copperline::new();
match env::args().nth(1) {
Some(file) => file,
None => {
match copperline.read_line_utf8("Input file to read: ") {
Ok(filename) => filename,
Err(x) => {
usage();
process::exit(1);
}
}
},
}
}

#[cfg(not(feature="interactive"))]
fn get_args_fn() -> String{
match env::args().nth(1) {
Some(file) => file,
Expand All @@ -62,42 +46,14 @@ fn get_args_fn() -> String{
}
}

fn search(trie: &Trie<String, u32>, query: &String){
let child = trie.get_descendant(&query);

if child.is_none() {
print_err!("Search found 0 item\n\n");
return;
}

let child = child.unwrap();
fn search(trie: &Trie<String, u32>, query: &String) -> u32{
let child = trie.get_descendant(&query).unwrap();
let mut count = 0;
for item in child.iter() {
count += 1;
if cfg!(feature="dump_data") || cfg!(feature="interactive") {
println!("#{} {} -> {}", count, item.0, item.1);
}
}
print_err!("Search found {} item\n\n", count);
}

#[cfg(feature="interactive")]
fn interactive(trie: &Trie<String, u32>){
let mut copperline = Copperline::new();
loop {
match copperline.read_line_utf8("Search: ") {
Ok(query) => {
copperline.add_history(query.clone());
search(&trie, &query);
},
Err(x) => break
};
}
return count;
}
#[cfg(not(feature="interactive"))]
#[allow(unused_variables)]
#[inline]
fn interactive(trie: &Trie<String, u32>){}

fn main(){
let file = get_args_fn();
Expand All @@ -109,13 +65,37 @@ fn main(){
}
print_err!("Input file loaded\n");

if cfg!(feature="interactive") {
interactive(&trie);
}else{
search(&trie, &String::from("สม"));
if cfg!(feature="wait_on_exit") {
println!("Run finished");
sleep(Duration::from_secs(10000));
let mut stopwatch = Stopwatch::new();
let total = trie.len() as f32;
let mut done = 0f32;
let mut data = Vec::<(&String, i64)>::new();

for key in trie.keys() {
done += 1.0;

if done % LOOP_UPDATE_EVERY == 0.0 {
print!("Processing {} of {} ({:.2}%)\r", done, total, (done/total)*100.0);
}

let mut total_time = 0;
for i in 0..ITER_COUNT {
stopwatch.restart();
search(&trie, key);
total_time += stopwatch.elapsed_ms();
}

data.push((key, total_time));
}
println!("\n\nDone! Processing data...");

data.sort_by_key(|i| i.1);

println!("Top {} slowest elements:", TOP_NO);
for item in data.iter().rev().take(TOP_NO) {
println!("{}\t\t{}ms", item.0, item.1);
}

if cfg!(feature="wait_on_exit") {
sleep(Duration::from_secs(10000));
}
}

0 comments on commit 76873d6

Please sign in to comment.