mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-03-31 08:35:28 +00:00
use HashMap to store misc index
This commit is contained in:
parent
f944575784
commit
f41b7a57bd
3 changed files with 22 additions and 14 deletions
|
|
@ -6,11 +6,11 @@ use database::Database;
|
|||
use memory::Memory;
|
||||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use std::{cell::RefCell, collections::HashSet};
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
|
||||
pub struct Misc {
|
||||
database: Database,
|
||||
memory: RefCell<HashSet<Memory>>,
|
||||
memory: RefCell<HashMap<String, Memory>>,
|
||||
}
|
||||
|
||||
impl Misc {
|
||||
|
|
@ -20,17 +20,19 @@ impl Misc {
|
|||
let database = Database::init(database_pool, profile_id);
|
||||
|
||||
let rows = database.rows()?;
|
||||
let memory = RefCell::new(HashSet::with_capacity(rows.len()));
|
||||
let memory = RefCell::new(HashMap::with_capacity(rows.len()));
|
||||
|
||||
{
|
||||
// build in-memory index...
|
||||
let mut m = memory.borrow_mut();
|
||||
// create initial preset (populate index with the default values)
|
||||
assert!(m.insert(Memory::highlight_request_entry(true)));
|
||||
let v = Memory::highlight_request_entry(true);
|
||||
assert!(m.insert(v.key().to_string(), v).is_none());
|
||||
|
||||
// update values from the DB (if exists)
|
||||
for row in rows {
|
||||
assert!(!m.insert(Memory::from_db_row(&row.key, row.value).unwrap()));
|
||||
let v = Memory::from_db_row(&row.key, row.value).unwrap();
|
||||
assert!(m.insert(v.key().to_string(), v).is_some());
|
||||
// * panics if the DB was malformed or changed unexpectedly
|
||||
}
|
||||
}
|
||||
|
|
@ -41,22 +43,21 @@ impl Misc {
|
|||
// Setters
|
||||
|
||||
pub fn save(&self) -> Result<()> {
|
||||
for k in self.memory.take() {
|
||||
self.database.set(k.into_db_row())?;
|
||||
for (_, m) in self.memory.take() {
|
||||
self.database.set(m.into_db_row())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_highlight_request_entry(&self, value: bool) -> bool {
|
||||
self.memory
|
||||
.borrow_mut()
|
||||
.insert(Memory::highlight_request_entry(value))
|
||||
pub fn set_highlight_request_entry(&self, value: bool) -> Option<Memory> {
|
||||
let v = Memory::highlight_request_entry(value);
|
||||
self.memory.borrow_mut().insert(v.key().to_string(), v)
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
pub fn is_highlight_request_entry(&self) -> bool {
|
||||
if let Some(k) = self.memory.borrow().iter().next() {
|
||||
if let Some(k) = self.memory.borrow().values().next() {
|
||||
match k {
|
||||
Memory::HighlightRequestEntry(v) => return v.is_true(),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ use bool::Bool;
|
|||
|
||||
const HIGHLIGHT_REQUEST_ENTRY: &str = "highlight_request_entry";
|
||||
|
||||
#[derive(Eq, Hash, PartialEq)]
|
||||
pub enum Memory {
|
||||
HighlightRequestEntry(Bool),
|
||||
}
|
||||
|
|
@ -34,4 +33,12 @@ impl Memory {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
pub fn key(&self) -> &str {
|
||||
match self {
|
||||
Self::HighlightRequestEntry(..) => HIGHLIGHT_REQUEST_ENTRY,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
const TRUE: &str = "1";
|
||||
const FALSE: &str = "0";
|
||||
|
||||
#[derive(Eq, Hash, PartialEq, Default)]
|
||||
#[derive(Default)]
|
||||
pub enum Bool {
|
||||
True,
|
||||
#[default]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue