mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-03-31 16:45:27 +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 memory::Memory;
|
||||||
use r2d2::Pool;
|
use r2d2::Pool;
|
||||||
use r2d2_sqlite::SqliteConnectionManager;
|
use r2d2_sqlite::SqliteConnectionManager;
|
||||||
use std::{cell::RefCell, collections::HashSet};
|
use std::{cell::RefCell, collections::HashMap};
|
||||||
|
|
||||||
pub struct Misc {
|
pub struct Misc {
|
||||||
database: Database,
|
database: Database,
|
||||||
memory: RefCell<HashSet<Memory>>,
|
memory: RefCell<HashMap<String, Memory>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Misc {
|
impl Misc {
|
||||||
|
|
@ -20,17 +20,19 @@ impl Misc {
|
||||||
let database = Database::init(database_pool, profile_id);
|
let database = Database::init(database_pool, profile_id);
|
||||||
|
|
||||||
let rows = database.rows()?;
|
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...
|
// build in-memory index...
|
||||||
let mut m = memory.borrow_mut();
|
let mut m = memory.borrow_mut();
|
||||||
// create initial preset (populate index with the default values)
|
// 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)
|
// update values from the DB (if exists)
|
||||||
for row in rows {
|
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
|
// * panics if the DB was malformed or changed unexpectedly
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -41,22 +43,21 @@ impl Misc {
|
||||||
// Setters
|
// Setters
|
||||||
|
|
||||||
pub fn save(&self) -> Result<()> {
|
pub fn save(&self) -> Result<()> {
|
||||||
for k in self.memory.take() {
|
for (_, m) in self.memory.take() {
|
||||||
self.database.set(k.into_db_row())?;
|
self.database.set(m.into_db_row())?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_highlight_request_entry(&self, value: bool) -> bool {
|
pub fn set_highlight_request_entry(&self, value: bool) -> Option<Memory> {
|
||||||
self.memory
|
let v = Memory::highlight_request_entry(value);
|
||||||
.borrow_mut()
|
self.memory.borrow_mut().insert(v.key().to_string(), v)
|
||||||
.insert(Memory::highlight_request_entry(value))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
|
|
||||||
pub fn is_highlight_request_entry(&self) -> bool {
|
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 {
|
match k {
|
||||||
Memory::HighlightRequestEntry(v) => return v.is_true(),
|
Memory::HighlightRequestEntry(v) => return v.is_true(),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ use bool::Bool;
|
||||||
|
|
||||||
const HIGHLIGHT_REQUEST_ENTRY: &str = "highlight_request_entry";
|
const HIGHLIGHT_REQUEST_ENTRY: &str = "highlight_request_entry";
|
||||||
|
|
||||||
#[derive(Eq, Hash, PartialEq)]
|
|
||||||
pub enum Memory {
|
pub enum Memory {
|
||||||
HighlightRequestEntry(Bool),
|
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 TRUE: &str = "1";
|
||||||
const FALSE: &str = "0";
|
const FALSE: &str = "0";
|
||||||
|
|
||||||
#[derive(Eq, Hash, PartialEq, Default)]
|
#[derive(Default)]
|
||||||
pub enum Bool {
|
pub enum Bool {
|
||||||
True,
|
True,
|
||||||
#[default]
|
#[default]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue