use record id as hashmap value

This commit is contained in:
yggverse 2024-11-14 12:26:02 +02:00
parent f338b93217
commit d3f3475f15
5 changed files with 37 additions and 37 deletions

View File

@ -70,7 +70,7 @@ impl Navigation {
let request = self.request.widget().gobject().text(); let request = self.request.widget().gobject().text();
self.bookmark self.bookmark
.update(self.profile.bookmark.has_request(&request, true)); .update(self.profile.bookmark.get(&request).is_ok());
self.history.update(); self.history.update();
self.home.update(self.request.uri()); self.home.update(self.request.uri());
self.reload.update(!request.is_empty()); self.reload.update(!request.is_empty());

View File

@ -26,7 +26,7 @@ impl Bookmark {
// Build initial index // Build initial index
for record in database.records(None) { for record in database.records(None) {
if memory.add(record.request, record.time).is_err() { if memory.add(record.request, record.id).is_err() {
todo!() todo!()
} }
} }
@ -37,25 +37,19 @@ impl Bookmark {
// Actions // Actions
/// Check request exist in: /// Get record `id` by `request` from memory index
/// * memory if `is_memory` is `true` (fast) pub fn get(&self, request: &str) -> Result<i64, Error> {
/// * database if `is_memory` is `false` (slow) match self.memory.get(request) {
pub fn has_request(&self, request: &str, is_memory: bool) -> bool { Ok(id) => Ok(id),
if is_memory { Err(_) => Err(Error::MemoryNotFound),
self.memory.is_exist(request)
} else {
!self.database.records(Some(request)).is_empty()
} }
} }
/// Toggle record in `database` and `memory` index /// Toggle record in `database` and `memory` index
pub fn toggle(&self, request: &str) -> Result<(), Error> { pub fn toggle(&self, request: &str) -> Result<(), Error> {
// Get current timestamp for new record
let time = DateTime::now_local().unwrap();
// Delete record if exists // Delete record if exists
if self.has_request(request, false) { if let Ok(id) = self.get(request) {
match self.database.delete(request) { match self.database.delete(id) {
Ok(_) => match self.memory.delete(request) { Ok(_) => match self.memory.delete(request) {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(_) => Err(Error::MemoryDelete), Err(_) => Err(Error::MemoryDelete),
@ -64,8 +58,11 @@ impl Bookmark {
} }
// Otherwise, create new record // Otherwise, create new record
} else { } else {
match self.database.add(time.clone(), request.into()) { match self
Ok(_) => match self.memory.add(request.into(), time) { .database
.add(DateTime::now_local().unwrap(), request.into())
{
Ok(id) => match self.memory.add(request.into(), id) {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(_) => Err(Error::MemoryAdd), Err(_) => Err(Error::MemoryAdd),
}, },

View File

@ -5,7 +5,7 @@ use std::{rc::Rc, sync::RwLock};
pub struct Table { pub struct Table {
pub id: i64, pub id: i64,
//pub profile_id: i64, //pub profile_id: i64,
pub time: DateTime, //pub time: DateTime,
pub request: String, pub request: String,
} }
@ -52,21 +52,20 @@ impl Database {
} }
} }
pub fn delete(&self, request: &str) -> Result<(), ()> { pub fn delete(&self, id: i64) -> Result<(), ()> {
// Begin new transaction // Begin new transaction
let mut writable = self.connection.write().unwrap(); let mut writable = self.connection.write().unwrap();
let tx = writable.transaction().unwrap(); let tx = writable.transaction().unwrap();
// Delete records match request // Delete records match request
for record in select(&tx, self.profile_id, Some(request)).unwrap() { match delete(&tx, id) {
let _ = delete(&tx, record.id); Ok(_) => match tx.commit() {
}
// Done
match tx.commit() {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(_) => Err(()), Err(_) => Err(()),
} // @TODO handle result },
Err(_) => Err(()),
}
// @TODO handle result
} }
} }
@ -116,7 +115,7 @@ pub fn select(
Ok(Table { Ok(Table {
id: row.get(0)?, id: row.get(0)?,
//profile_id: row.get(1)?, //profile_id: row.get(1)?,
time: DateTime::from_unix_local(row.get(2)?).unwrap(), //time: DateTime::from_unix_local(row.get(2)?).unwrap(),
request: row.get(3)?, request: row.get(3)?,
}) })
})?; })?;

View File

@ -3,4 +3,5 @@ pub enum Error {
DatabaseDelete, DatabaseDelete,
MemoryAdd, MemoryAdd,
MemoryDelete, MemoryDelete,
MemoryNotFound,
} }

View File

@ -1,12 +1,11 @@
mod error; mod error;
use error::Error; use error::Error;
use gtk::glib::DateTime;
use std::{cell::RefCell, collections::HashMap}; use std::{cell::RefCell, collections::HashMap};
/// Reduce disk usage by cache Bookmarks index in memory /// Reduce disk usage by cache Bookmarks index in memory
pub struct Memory { pub struct Memory {
index: RefCell<HashMap<String, DateTime>>, index: RefCell<HashMap<String, i64>>,
} }
impl Memory { impl Memory {
@ -21,16 +20,17 @@ impl Memory {
// Actions // Actions
/// Add new record for given `request` /// Add new record with `request` as key and `id` as value
/// * validates record with same key does not exist yet /// * validate record with same key does not exist yet
pub fn add(&self, request: String, time: DateTime) -> Result<(), Error> { pub fn add(&self, request: String, id: i64) -> Result<(), Error> {
match self.index.borrow_mut().insert(request, time) { match self.index.borrow_mut().insert(request, id) {
Some(_) => Err(Error::Overwrite), Some(_) => Err(Error::Overwrite),
None => Ok(()), None => Ok(()),
} }
} }
/// Delete record from index by `request` /// Delete record from index by `request`
/// * validate record key is exist
pub fn delete(&self, request: &str) -> Result<(), Error> { pub fn delete(&self, request: &str) -> Result<(), Error> {
match self.index.borrow_mut().remove(request) { match self.index.borrow_mut().remove(request) {
Some(_) => Ok(()), Some(_) => Ok(()),
@ -38,8 +38,11 @@ impl Memory {
} }
} }
/// Check `request` exist in memory index /// Get `id` by `request` from memory index
pub fn is_exist(&self, request: &str) -> bool { pub fn get(&self, request: &str) -> Result<i64, Error> {
self.index.borrow().get(request).is_some() match self.index.borrow().get(request) {
Some(&id) => Ok(id),
None => Err(Error::NotFound),
}
} }
} }