memory.rs
1 use super::database::Row; 2 use std::cell::RefCell; 3 4 /// Reduce disk usage by cache Bookmarks index in memory 5 pub struct Memory { 6 index: RefCell<Vec<Row>>, 7 } 8 9 impl Memory { 10 // Constructors 11 12 /// Create new `Self` 13 pub fn init() -> Self { 14 Self { 15 index: RefCell::new(Vec::new()), 16 } 17 } 18 19 // Actions 20 21 /// Add new record 22 pub fn push(&self, id: i64, query: String, is_default: bool) { 23 self.index.borrow_mut().push(Row { 24 id, 25 query, 26 is_default, 27 }) 28 } 29 30 /// Clear all records 31 pub fn clear(&self) { 32 self.index.borrow_mut().clear() 33 } 34 35 // Getters 36 37 /// Get all records 38 pub fn records(&self) -> Vec<Row> { 39 self.index.borrow().clone() 40 } 41 42 /// Get all records 43 pub fn default(&self) -> Option<Row> { 44 for record in self.index.borrow().iter() { 45 if record.is_default { 46 return Some(record.clone()); 47 } 48 } 49 None 50 } 51 }