From f41b7a57bd4c90fd439a044d5f0cf1a1496420d1 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 26 Jul 2025 08:55:53 +0300 Subject: [PATCH] use HashMap to store misc index --- src/profile/proxy/misc.rs | 25 +++++++++++++------------ src/profile/proxy/misc/memory.rs | 9 ++++++++- src/profile/proxy/misc/memory/bool.rs | 2 +- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/profile/proxy/misc.rs b/src/profile/proxy/misc.rs index 3aed0a0f..a5ef3406 100644 --- a/src/profile/proxy/misc.rs +++ b/src/profile/proxy/misc.rs @@ -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>, + memory: RefCell>, } 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 { + 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(), } diff --git a/src/profile/proxy/misc/memory.rs b/src/profile/proxy/misc/memory.rs index 4fe3f7b4..ffcf7b2e 100644 --- a/src/profile/proxy/misc/memory.rs +++ b/src/profile/proxy/misc/memory.rs @@ -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, + } + } } diff --git a/src/profile/proxy/misc/memory/bool.rs b/src/profile/proxy/misc/memory/bool.rs index bad945e8..9d6d76cc 100644 --- a/src/profile/proxy/misc/memory/bool.rs +++ b/src/profile/proxy/misc/memory/bool.rs @@ -1,7 +1,7 @@ const TRUE: &str = "1"; const FALSE: &str = "0"; -#[derive(Eq, Hash, PartialEq, Default)] +#[derive(Default)] pub enum Bool { True, #[default]