mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 09:05:27 +00:00
reorganize bookmarks memory model
This commit is contained in:
parent
6f91efbc9c
commit
6526ca85d8
6 changed files with 93 additions and 99 deletions
|
|
@ -1,15 +1,9 @@
|
|||
use super::Item;
|
||||
use anyhow::Result;
|
||||
use gtk::glib::DateTime;
|
||||
use sqlite::{Connection, Transaction};
|
||||
use std::{rc::Rc, sync::RwLock};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
//pub profile_id: i64,
|
||||
//pub time: DateTime,
|
||||
pub request: String,
|
||||
}
|
||||
|
||||
pub struct Database {
|
||||
connection: Rc<RwLock<Connection>>,
|
||||
profile_id: Rc<i64>, // multi-profile relationship
|
||||
|
|
@ -29,7 +23,7 @@ impl Database {
|
|||
// Getters
|
||||
|
||||
/// Get bookmark records from database with optional filter by `request`
|
||||
pub fn records(&self, request: Option<&str>) -> Result<Vec<Table>> {
|
||||
pub fn records(&self, request: Option<&str>) -> Result<Vec<Item>> {
|
||||
let readable = self.connection.read().unwrap(); // @TODO
|
||||
let tx = readable.unchecked_transaction()?;
|
||||
select(&tx, *self.profile_id, request)
|
||||
|
|
@ -86,7 +80,7 @@ pub fn insert(tx: &Transaction, profile_id: i64, time: DateTime, request: &str)
|
|||
Ok(tx.last_insert_rowid())
|
||||
}
|
||||
|
||||
pub fn select(tx: &Transaction, profile_id: i64, request: Option<&str>) -> Result<Vec<Table>> {
|
||||
pub fn select(tx: &Transaction, profile_id: i64, request: Option<&str>) -> Result<Vec<Item>> {
|
||||
let mut stmt = tx.prepare(
|
||||
"SELECT `id`, `profile_id`, `time`, `request`
|
||||
FROM `profile_bookmark`
|
||||
|
|
@ -94,7 +88,7 @@ pub fn select(tx: &Transaction, profile_id: i64, request: Option<&str>) -> Resul
|
|||
)?;
|
||||
|
||||
let result = stmt.query_map((profile_id, request.unwrap_or("%")), |row| {
|
||||
Ok(Table {
|
||||
Ok(Item {
|
||||
id: row.get(0)?,
|
||||
//profile_id: row.get(1)?,
|
||||
//time: DateTime::from_unix_local(row.get(2)?).unwrap(),
|
||||
|
|
|
|||
5
src/profile/bookmark/item.rs
Normal file
5
src/profile/bookmark/item.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#[derive(Clone)]
|
||||
pub struct Item {
|
||||
pub id: i64,
|
||||
pub request: String,
|
||||
}
|
||||
|
|
@ -1,10 +1,56 @@
|
|||
use anyhow::Result;
|
||||
use super::Item;
|
||||
use itertools::Itertools;
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
|
||||
/// Reduce disk usage by cache Bookmarks index in memory
|
||||
pub struct Memory {
|
||||
index: RefCell<HashMap<String, i64>>,
|
||||
pub struct Memory(Vec<Item>);
|
||||
|
||||
impl Memory {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
Self(Vec::new())
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/// Add new item
|
||||
pub fn add(&mut self, item: Item) {
|
||||
self.0.push(item)
|
||||
}
|
||||
|
||||
/// Delete record from index by `request`
|
||||
pub fn delete_by_request(&mut self, request: &str) -> Option<Item> {
|
||||
for (i, item) in self.0.iter().enumerate() {
|
||||
if item.request == request {
|
||||
return Some(self.0.remove(i));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Check `request` exists in the memory index
|
||||
pub fn contains_request(&self, request: &str) -> bool {
|
||||
for item in self.0.iter() {
|
||||
if item.request == request {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Get recent Items vector sorted by `ID` DESC
|
||||
pub fn recent(&self) -> Vec<Item> {
|
||||
let mut recent: Vec<Item> = Vec::new();
|
||||
for item in self
|
||||
.0
|
||||
.iter()
|
||||
.sorted_by(|a, b| Ord::cmp(&b.request, &a.request))
|
||||
{
|
||||
recent.push(item.clone())
|
||||
}
|
||||
recent
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Memory {
|
||||
|
|
@ -12,62 +58,3 @@ impl Default for Memory {
|
|||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Memory {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
index: RefCell::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/// Add new record with `request` as key and `id` as value
|
||||
/// * validate record with same key does not exist yet
|
||||
pub fn add(&self, request: String, id: i64) -> Result<()> {
|
||||
// Borrow shared index access
|
||||
let mut index = self.index.borrow_mut();
|
||||
|
||||
// Prevent existing key overwrite
|
||||
if index.contains_key(&request) {
|
||||
panic!() // unexpected
|
||||
}
|
||||
|
||||
// Slot should be free, let check it twice
|
||||
match index.insert(request, id) {
|
||||
Some(_) => panic!(), // unexpected
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete record from index by `request`
|
||||
/// * validate record key is exist
|
||||
pub fn delete(&self, request: &str) -> Result<()> {
|
||||
match self.index.borrow_mut().remove(request) {
|
||||
Some(_) => Ok(()),
|
||||
None => panic!(), // unexpected
|
||||
}
|
||||
}
|
||||
|
||||
/// Get `id` by `request` from memory index
|
||||
pub fn get(&self, request: &str) -> Option<i64> {
|
||||
self.index.borrow().get(request).copied()
|
||||
}
|
||||
|
||||
/// Get recent requests vector sorted by `ID` DESC
|
||||
pub fn recent(&self) -> Vec<String> {
|
||||
let mut recent: Vec<String> = Vec::new();
|
||||
for (request, _) in self
|
||||
.index
|
||||
.borrow()
|
||||
.iter()
|
||||
.sorted_by(|a, b| Ord::cmp(&b.1, &a.1))
|
||||
{
|
||||
recent.push(request.to_string())
|
||||
}
|
||||
recent
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue