mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 09:05:27 +00:00
implement memory pool for bookmark index
This commit is contained in:
parent
74e9daeddb
commit
7b9bd95c09
2 changed files with 69 additions and 3 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue