Mercurial > lbo > hg > leveldb-rs
changeset 510:4346e63d9f99
Add word-analyze example to create more random read/write load
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Mon, 17 Feb 2020 15:19:49 +0100 |
parents | d2ba412467c3 |
children | 778233f8ebba |
files | Cargo.toml examples/word-analyze/Cargo.toml examples/word-analyze/src/main.rs |
diffstat | 3 files changed, 55 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/Cargo.toml Sun Feb 16 21:15:52 2020 +0100 +++ b/Cargo.toml Mon Feb 17 15:19:49 2020 +0100 @@ -28,5 +28,5 @@ path = "src/benches/maps_bench.rs" [workspace] -members = ["examples/write-a-lot", "examples/leveldb-tool"] +members = ["examples/write-a-lot", "examples/leveldb-tool", "examples/word-analyze"]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/word-analyze/Cargo.toml Mon Feb 17 15:19:49 2020 +0100 @@ -0,0 +1,11 @@ +[package] +name = "word-analyze" +version = "0.1.0" +authors = ["Lewin Bormann <lewin@lewin-bormann.info>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rusty-leveldb = "0.3" +integer-encoding = "1"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/word-analyze/src/main.rs Mon Feb 17 15:19:49 2020 +0100 @@ -0,0 +1,43 @@ + +use rusty_leveldb as leveldb; + +use std::fs::OpenOptions; +use std::io::{self, BufRead}; +use std::path::Path; + +fn update_count(w: &str, db: &mut leveldb::DB) -> Option<()> { + let mut count: usize = 0; + if let Some(v) = db.get(w.as_bytes()) { + let s = String::from_utf8(v).unwrap(); + count = usize::from_str_radix(&s, 10).unwrap(); + } + count += 1; + let s = count.to_string(); + db.put(w.as_bytes(), s.as_bytes()).unwrap(); + Some(()) +} + +fn run(mut db: leveldb::DB) -> io::Result<()> { + let files = std::env::args().skip(1); + + for f in files { + let f = OpenOptions::new().read(true).open(Path::new(&f))?; + for line in io::BufReader::new(f).lines() { + for word in line.unwrap().split_whitespace() { + let mut word = word.to_ascii_lowercase(); + word.retain(|c| c.is_ascii_alphanumeric()); + update_count(&word, &mut db); + } + } + } + + Ok(()) +} + +fn main() { + let mut opts = leveldb::Options::default(); + opts.compression_type = leveldb::CompressionType::CompressionNone; + let db = leveldb::DB::open("wordsdb", opts).unwrap(); + + run(db).unwrap(); +}