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
use bincode::{deserialize_from, serialize_into, ErrorKind}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::fs; use std::fs::rename; use std::fs::{File, OpenOptions}; use std::io::{BufReader, BufWriter}; use std::path::Path; use std::time::SystemTime; const LOCAL_STORAGE_FILE_NAME: &str = "data/onto-index"; #[derive(Serialize, Deserialize, PartialEq, Debug)] pub struct OntoIndex { pub data: HashMap<String, i64>, } impl OntoIndex { pub fn get_modified() -> Option<SystemTime> { if let Ok(m) = fs::metadata(LOCAL_STORAGE_FILE_NAME) { if let Ok(t) = m.modified() { return Some(t); } } None } pub fn load() -> Self { if let Ok(f) = File::open(LOCAL_STORAGE_FILE_NAME) { let mut r = BufReader::new(f); let res: Result<OntoIndex, _> = deserialize_from(&mut r); match res { Ok(d) => { info!("load {} onto elements", d.data.keys().len()); return d; } Err(e) => error!("fail load local storage, err={}", e), } } OntoIndex { data: HashMap::new(), } } pub fn exists(&self) -> bool { Path::new(LOCAL_STORAGE_FILE_NAME).exists() } pub fn dump(&self) -> Result<(), Box<ErrorKind>> { let tmp_file_name = LOCAL_STORAGE_FILE_NAME.to_owned() + ".tmp"; let file = OpenOptions::new().write(true).create(true).truncate(false).open(tmp_file_name.clone())?; serialize_into(&mut BufWriter::new(file), &self.data)?; rename(tmp_file_name, LOCAL_STORAGE_FILE_NAME)?; Ok(()) } pub fn len(&self) -> usize { self.data.keys().len() } pub fn is_empty(&self) -> bool { self.data.keys().len() > 0 } pub fn remove(&mut self, key: &str) -> Result<(), Box<ErrorKind>> { self.data.remove(key); self.dump() } pub fn set(&mut self, key: &str, val: &i64) -> Result<(), Box<ErrorKind>> { self.data.insert(key.to_owned(), val.to_owned()); self.dump() } }