mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 09:10:08 +00:00
use record id as hashmap value
This commit is contained in:
parent
f338b93217
commit
d3f3475f15
@ -70,7 +70,7 @@ impl Navigation {
|
||||
let request = self.request.widget().gobject().text();
|
||||
|
||||
self.bookmark
|
||||
.update(self.profile.bookmark.has_request(&request, true));
|
||||
.update(self.profile.bookmark.get(&request).is_ok());
|
||||
self.history.update();
|
||||
self.home.update(self.request.uri());
|
||||
self.reload.update(!request.is_empty());
|
||||
|
@ -26,7 +26,7 @@ impl Bookmark {
|
||||
|
||||
// Build initial index
|
||||
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!()
|
||||
}
|
||||
}
|
||||
@ -37,25 +37,19 @@ impl Bookmark {
|
||||
|
||||
// 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()
|
||||
/// Get record `id` by `request` from memory index
|
||||
pub fn get(&self, request: &str) -> Result<i64, Error> {
|
||||
match self.memory.get(request) {
|
||||
Ok(id) => Ok(id),
|
||||
Err(_) => Err(Error::MemoryNotFound),
|
||||
}
|
||||
}
|
||||
|
||||
/// Toggle record in `database` and `memory` index
|
||||
pub fn toggle(&self, request: &str) -> Result<(), Error> {
|
||||
// Get current timestamp for new record
|
||||
let time = DateTime::now_local().unwrap();
|
||||
|
||||
// Delete record if exists
|
||||
if self.has_request(request, false) {
|
||||
match self.database.delete(request) {
|
||||
if let Ok(id) = self.get(request) {
|
||||
match self.database.delete(id) {
|
||||
Ok(_) => match self.memory.delete(request) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(Error::MemoryDelete),
|
||||
@ -64,8 +58,11 @@ impl Bookmark {
|
||||
}
|
||||
// Otherwise, create new record
|
||||
} else {
|
||||
match self.database.add(time.clone(), request.into()) {
|
||||
Ok(_) => match self.memory.add(request.into(), time) {
|
||||
match self
|
||||
.database
|
||||
.add(DateTime::now_local().unwrap(), request.into())
|
||||
{
|
||||
Ok(id) => match self.memory.add(request.into(), id) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(Error::MemoryAdd),
|
||||
},
|
||||
|
@ -5,7 +5,7 @@ use std::{rc::Rc, sync::RwLock};
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
//pub profile_id: i64,
|
||||
pub time: DateTime,
|
||||
//pub time: DateTime,
|
||||
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
|
||||
let mut writable = self.connection.write().unwrap();
|
||||
let tx = writable.transaction().unwrap();
|
||||
|
||||
// Delete records match request
|
||||
for record in select(&tx, self.profile_id, Some(request)).unwrap() {
|
||||
let _ = delete(&tx, record.id);
|
||||
}
|
||||
|
||||
// Done
|
||||
match tx.commit() {
|
||||
Ok(_) => Ok(()),
|
||||
match delete(&tx, id) {
|
||||
Ok(_) => match tx.commit() {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(()),
|
||||
},
|
||||
Err(_) => Err(()),
|
||||
} // @TODO handle result
|
||||
}
|
||||
// @TODO handle result
|
||||
}
|
||||
}
|
||||
|
||||
@ -116,7 +115,7 @@ pub fn select(
|
||||
Ok(Table {
|
||||
id: row.get(0)?,
|
||||
//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)?,
|
||||
})
|
||||
})?;
|
||||
|
@ -3,4 +3,5 @@ pub enum Error {
|
||||
DatabaseDelete,
|
||||
MemoryAdd,
|
||||
MemoryDelete,
|
||||
MemoryNotFound,
|
||||
}
|
||||
|
@ -1,12 +1,11 @@
|
||||
mod error;
|
||||
use error::Error;
|
||||
|
||||
use gtk::glib::DateTime;
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
|
||||
/// Reduce disk usage by cache Bookmarks index in memory
|
||||
pub struct Memory {
|
||||
index: RefCell<HashMap<String, DateTime>>,
|
||||
index: RefCell<HashMap<String, i64>>,
|
||||
}
|
||||
|
||||
impl Memory {
|
||||
@ -21,16 +20,17 @@ impl Memory {
|
||||
|
||||
// Actions
|
||||
|
||||
/// Add new record for given `request`
|
||||
/// * validates record with same key does not exist yet
|
||||
pub fn add(&self, request: String, time: DateTime) -> Result<(), Error> {
|
||||
match self.index.borrow_mut().insert(request, time) {
|
||||
/// 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<(), Error> {
|
||||
match self.index.borrow_mut().insert(request, id) {
|
||||
Some(_) => Err(Error::Overwrite),
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete record from index by `request`
|
||||
/// * validate record key is exist
|
||||
pub fn delete(&self, request: &str) -> Result<(), Error> {
|
||||
match self.index.borrow_mut().remove(request) {
|
||||
Some(_) => Ok(()),
|
||||
@ -38,8 +38,11 @@ impl Memory {
|
||||
}
|
||||
}
|
||||
|
||||
/// Check `request` exist in memory index
|
||||
pub fn is_exist(&self, request: &str) -> bool {
|
||||
self.index.borrow().get(request).is_some()
|
||||
/// Get `id` by `request` from memory index
|
||||
pub fn get(&self, request: &str) -> Result<i64, Error> {
|
||||
match self.index.borrow().get(request) {
|
||||
Some(&id) => Ok(id),
|
||||
None => Err(Error::NotFound),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user