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
extern crate base64; use base64::{encode, decode}; use std::collections::HashMap; use std::fs::File; use std::{fs, io}; use std::io::prelude::*; use std::path::Path; pub static STORE_DIR: &str = "./simplestore"; pub trait F { fn get(&self, table: &str, id: &str) -> Result<String, String>; fn put(&self, table: &str, id: &str, data: String) -> bool; fn fetch(&self, table: &str) -> Result<HashMap<String, String>, String>; } pub struct Store { pub store_dir: &'static str, } impl Store { pub fn new() -> Store { Store { store_dir: STORE_DIR, } } pub fn ssd(&mut self, s: &'static str) { self.store_dir = s; } fn calc_id(&self, table: &str, id: &str) -> String { String::from(format!("{}/{}/{}", STORE_DIR, table, id)) } fn calc_table(&self, table: &str) -> String { String::from(format!("{}/{}", STORE_DIR, table)) } fn is_table_exists(&self, table: &str) -> bool { Path::new(&self.calc_table(table)).exists() } fn is_id_exists(&self, table: &str, id: &str) -> bool { Path::new(&self.calc_id(table, id)).exists() } fn create_resource(&self, table: &str) { if !self.is_table_exists(table) { fs::create_dir(self.calc_table(table)).unwrap(); } } } impl F for Store { fn get(&self, table: &str, id: &str) -> Result<String, String> { if !self.is_id_exists(table, id) { return Err(String::from("id not found")) } let filename = self.calc_id(table, id); let mut data = String::new(); let mut f = File::open(filename).expect("Unable to open file"); f.read_to_string(&mut data).expect("Unable to read string"); let decoded = decode(data).unwrap(); Ok(decoded.iter().map(|&c| c as char).collect::<String>()) } fn put(&self, table: &str, id: &str, data: String) -> bool { self.create_resource(table); let f = self.calc_id(table, id); let encoded = encode(data); fs::write(f, encoded).expect("Unable to write file"); return true; } fn fetch(&self, table: &str,) -> Result<HashMap<String, String>, String> { self.create_resource(table); let mut map = HashMap::<String, String>::new(); let d = self.calc_table(table); let mut entries = fs::read_dir(d).unwrap() .map(|res| res.map(|e| e.path())) .collect::<Result<Vec<_>, io::Error>>().unwrap(); for entry in entries { match entry.to_str() { Some(filename) => { let mut data = String::new(); let mut f = File::open(String::from(filename)).expect("Unable to open file"); f.read_to_string(&mut data).expect("Unable to read string"); let decoded = decode(data).unwrap(); map.insert(String::from(split_last(filename)), decoded.iter().map(|&c| c as char).collect::<String>()); }, None => println!("err"), } } Ok(map) } } fn split_last(path: &str) -> &str { let chunks: Vec<&str> = path.split("/").collect(); chunks.last().unwrap() } #[cfg(test)] mod tests { use super::*; use std::time::{Instant}; #[test] fn test_calc_file() { let s = Store::new(); let n = s.calc_id("asd", "asd"); let expected = String::from("/opt/simplestore/asd/asd"); if expected != n { panic!("string mismatch") } } #[test] fn test_put() { let s = Store::new(); let start = Instant::now(); s.put("user", "12345", String::from("Feri")); let duration = start.elapsed(); println!("Time elapsed in s.put() is: {:?}", duration); let start = Instant::now(); match s.get("user", "12345") { Ok(data) => println!("{}", data), Err(err) => println!("{}", err), } let duration = start.elapsed(); println!("Time elapsed in s.get() is: {:?}", duration); } #[test] fn test_fetch() { let s = Store::new(); let start = Instant::now(); s.put("user", "12345", String::from("Feri")); s.put("user", "12346", String::from("Feri")); s.put("user", "12347", String::from("Feri")); s.put("user", "12348", String::from("Feri")); s.put("user", "12349", String::from("Feri")); let duration = start.elapsed(); println!("Time elapsed in s.put() is: {:?}", duration); let start = Instant::now(); match s.fetch("user") { Ok(data) => println!("{:?}", data), Err(_) => println!("err") } let duration = start.elapsed(); println!("Time elapsed in s.fetch() is: {:?}", duration); } }