summaryrefslogtreecommitdiff
path: root/meap/actionkv/src
diff options
context:
space:
mode:
authorAdam Carpenter <gitlab@53hor.net>2019-07-09 15:14:04 -0400
committerAdam Carpenter <gitlab@53hor.net>2019-07-09 15:14:04 -0400
commit7e8ee5ed9cad6484e9f13f81731b102ced58402e (patch)
tree5395402ab07bbb5a659dbd68c701e22a1227202f /meap/actionkv/src
downloadlearning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.tar.xz
learning-rust-7e8ee5ed9cad6484e9f13f81731b102ced58402e.zip
Init.
Diffstat (limited to 'meap/actionkv/src')
-rwxr-xr-xmeap/actionkv/src/akv_mem.rs52
-rwxr-xr-xmeap/actionkv/src/lib.rs7
2 files changed, 59 insertions, 0 deletions
diff --git a/meap/actionkv/src/akv_mem.rs b/meap/actionkv/src/akv_mem.rs
new file mode 100755
index 0000000..cbcf88c
--- /dev/null
+++ b/meap/actionkv/src/akv_mem.rs
@@ -0,0 +1,52 @@
+extern crate libactionkv;
+
+use libactionkv::ActionKv;
+
+#[cfg(target_os = "windows")]
+const USAGE: &'static str = "
+Usage:
+ akv_mem.exe FILE get KEY
+ akv_mem.exe FILE delete KEY
+ akv_mem.exe FILE insert KEY VALUE
+ akv_mem.exe FILE update KEY VALUE
+";
+
+#[cfg(not(target_os = "windows"))]
+const USAGE: &'static str = "
+Usage:
+ akv_mem FILE get KEY
+ akv_mem FILE delete KEY
+ akv_mem FILE insert KEY VALUE
+ akv_mem FILE update KEY VALUE
+";
+
+fn main() {
+ let args: Vec<String> = std::env::args().collect();
+ let fname = args.get(1).expect(&USAGE);
+ let action = args.get(2).expect(&USAGE).as_ref();
+ let key = args.get(3).expect(&USAGE).as_ref();
+ let maybe_value = args.get(4);
+
+ let path = std::path::Path::new(&fname);
+ let mut store = ActionKV::open(path).expect("unable to open file");
+ store.load().expect("unable to load data");
+
+ match action {
+ "get" => {
+ match store.get(key).unwrap() {
+ None => eprintln!("{:?} not found", key),
+ Some(value) => println!("{:?}", value),
+ }
+ },
+ "delete" => store.delete(key).unwrap(),
+ "insert" => {
+ let value = maybe_value.expect(&USAGE).as_ref();
+ store.insert(key, value).unwrap()
+ },
+ "update" => {
+ let value = maybe_value.expect(&USAGE).as_ref();
+ store.update(key, value).unwrap()
+ },
+ _ => eprintln!("{}", &USAGE),
+ }
+}
diff --git a/meap/actionkv/src/lib.rs b/meap/actionkv/src/lib.rs
new file mode 100755
index 0000000..31e1bb2
--- /dev/null
+++ b/meap/actionkv/src/lib.rs
@@ -0,0 +1,7 @@
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn it_works() {
+ assert_eq!(2 + 2, 4);
+ }
+}