mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 09:10:08 +00:00
implement memory pool for bookmark index
This commit is contained in:
parent
74e9daeddb
commit
7b9bd95c09
@ -1,21 +1,49 @@
|
||||
mod database;
|
||||
mod memory;
|
||||
|
||||
use database::Database;
|
||||
use memory::Memory;
|
||||
|
||||
use sqlite::{Connection, Transaction};
|
||||
use std::{rc::Rc, sync::RwLock};
|
||||
|
||||
pub struct Bookmark {
|
||||
pub database: Rc<Database>,
|
||||
database: Rc<Database>,
|
||||
memory: Rc<Memory>,
|
||||
}
|
||||
|
||||
impl Bookmark {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self`
|
||||
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: i64) -> Self {
|
||||
Self {
|
||||
database: Rc::new(Database::new(connection, profile_id)),
|
||||
// Init children components
|
||||
let database = Rc::new(Database::new(connection, profile_id));
|
||||
let memory = Rc::new(Memory::new());
|
||||
|
||||
// Build initial index
|
||||
for record in database.records(None) {
|
||||
memory.set(record.request, record.time)
|
||||
}
|
||||
|
||||
// Return new `Self`
|
||||
Self { database, memory }
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/// Check request exist in:
|
||||
/// * memory if `is_memory` is `true` (fast)
|
||||
/// * database if `is_memory` is `false` (slow)
|
||||
pub fn has_request(&self, request: &str, is_memory: bool) -> bool {
|
||||
if is_memory {
|
||||
self.memory.is_exist(request)
|
||||
} else {
|
||||
!self.database.records(Some(request)).is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
// @TODO add new record with index update
|
||||
}
|
||||
|
||||
// Tools
|
||||
|
38
src/profile/bookmark/memory.rs
Normal file
38
src/profile/bookmark/memory.rs
Normal file
@ -0,0 +1,38 @@
|
||||
use gtk::glib::DateTime;
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
|
||||
/// Reduce disk usage by cache results in memory
|
||||
pub struct Memory {
|
||||
index: RefCell<HashMap<String, DateTime>>,
|
||||
}
|
||||
|
||||
impl Memory {
|
||||
// Constructors
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
index: RefCell::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/// Set new record
|
||||
/// * replace existing record with new value
|
||||
pub fn set(&self, request: String, time: DateTime) {
|
||||
// Borrow index to update
|
||||
let mut index = self.index.borrow_mut();
|
||||
|
||||
// Cleanup previous record
|
||||
if index.get(&request).is_some() {
|
||||
index.remove(&request);
|
||||
}
|
||||
|
||||
// Insert new record with actual data
|
||||
index.insert(request, time);
|
||||
}
|
||||
|
||||
pub fn is_exist(&self, request: &str) -> bool {
|
||||
self.index.borrow().get(request).is_some()
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user