use super::database::Row;
use std::sync::RwLock;
pub struct Memory {
index: RwLock<Vec<Row>>,
}
impl Memory {
pub fn init() -> Self {
Self {
index: RwLock::new(Vec::new()),
}
}
pub fn push(&self, id: i64, query: String, is_default: bool) {
self.index.write().unwrap().push(Row {
id,
query,
is_default,
})
}
pub fn clear(&self) {
self.index.write().unwrap().clear()
}
pub fn records(&self) -> Vec<Row> {
self.index.read().unwrap().clone()
}
pub fn default(&self) -> Option<Row> {
for record in self.index.read().unwrap().iter() {
if record.is_default {
return Some(record.clone());
}
}
None
}
}