reorganize clone semantics, implement recently closed tabs history

This commit is contained in:
yggverse 2025-01-12 04:02:41 +02:00
parent 3682b5bf3f
commit ba68019614
17 changed files with 176 additions and 52 deletions

View file

@ -19,7 +19,7 @@ impl Bookmark {
// Constructors
/// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: Rc<i64>) -> Self {
pub fn build(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Self {
// Init children components
let database = Rc::new(Database::new(connection, profile_id));
let memory = Rc::new(Memory::new());

View file

@ -18,10 +18,10 @@ impl Database {
// Constructors
/// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: Rc<i64>) -> Self {
pub fn new(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Self {
Self {
connection,
profile_id,
connection: connection.clone(),
profile_id: profile_id.clone(),
}
}

View file

@ -17,8 +17,10 @@ impl Database {
// Constructors
/// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>) -> Self {
Self { connection }
pub fn build(connection: &Rc<RwLock<Connection>>) -> Self {
Self {
connection: connection.clone(),
}
}
// Getters

View file

@ -1,18 +1,24 @@
mod database;
// mod database;
mod memory;
use sqlite::Transaction;
use memory::Memory;
// Tools
use sqlite::Connection;
use std::{rc::Rc, sync::RwLock};
pub fn migrate(tx: &Transaction) -> Result<(), String> {
// Migrate self components
if let Err(e) = database::init(tx) {
return Err(e.to_string());
}
// Delegate migration to childs
// nothing yet..
// Success
Ok(())
pub struct History {
pub memory: Rc<Memory>, // fast search index
}
impl History {
// Constructors
/// Create new `Self`
pub fn build(_connection: &Rc<RwLock<Connection>>, _profile_id: &Rc<i64>) -> Self {
// Init children components
let memory = Rc::new(Memory::new());
// Return new `Self`
Self { memory }
}
}

View file

@ -56,7 +56,7 @@ pub fn select(
WHERE `profile_id` = ? AND `request` LIKE ?",
)?;
let result = stmt.query_map((profile_id, request.unwrap_or("%")), |row| {
let result = stmt.query_map((profile_id, request.unwrap_or("%".to_string())), |row| {
Ok(Table {
id: row.get(0)?,
profile_id: row.get(1)?,

View file

@ -0,0 +1,24 @@
mod closed;
use closed::Closed;
/// Reduce disk usage by cache Bookmarks index in memory
pub struct Memory {
pub closed: Closed,
}
impl Default for Memory {
fn default() -> Self {
Self::new()
}
}
impl Memory {
// Constructors
/// Create new `Self`
pub fn new() -> Self {
Self {
closed: Closed::new(),
}
}
}

View file

@ -0,0 +1,53 @@
use gtk::glib::GString;
use itertools::Itertools;
use std::{cell::RefCell, collections::HashMap};
/// Reduce disk usage by cache Bookmarks index in memory
pub struct Closed {
index: RefCell<HashMap<GString, i64>>,
}
impl Default for Closed {
fn default() -> Self {
Self::new()
}
}
impl Closed {
// Constructors
/// Create new `Self`
pub fn new() -> Self {
Self {
index: RefCell::new(HashMap::new()),
}
}
// Actions
/// Add new record
/// * replace with new one if the record already exist
pub fn add(&self, request: GString, unix_timestamp: i64) {
self.index.borrow_mut().insert(request, unix_timestamp);
}
/// Get recent requests vector sorted by `ID` DESC
pub fn recent(&self, limit: usize) -> Vec<GString> {
let mut recent: Vec<GString> = Vec::new();
for (request, _) in self
.index
.borrow()
.iter()
.sorted_by(|a, b| Ord::cmp(&b.1, &a.1))
.take(limit)
{
recent.push(request.clone())
}
recent
}
/// Get records total
pub fn total(&self) -> usize {
self.index.borrow().len()
}
}

View file

@ -19,9 +19,9 @@ impl Identity {
// Constructors
/// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: Rc<i64>) -> Result<Self, Error> {
pub fn build(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Result<Self, Error> {
// Init identity database
let database = Rc::new(Database::new(connection.clone()));
let database = Rc::new(Database::build(connection));
// Get active identity set for profile or create new one
let profile_identity_id = Rc::new(match database.active() {
@ -36,7 +36,7 @@ impl Identity {
});
// Init gemini component
let gemini = Rc::new(match Gemini::new(connection, profile_identity_id) {
let gemini = Rc::new(match Gemini::build(connection, &profile_identity_id) {
Ok(result) => result,
Err(e) => return Err(Error::Gemini(e)),
});

View file

@ -15,8 +15,10 @@ impl Database {
// Constructors
/// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>) -> Self {
Self { connection }
pub fn build(connection: &Rc<RwLock<Connection>>) -> Self {
Self {
connection: connection.clone(),
}
}
// Getters
@ -37,7 +39,7 @@ impl Database {
// Setters
/// Create new record in `Self` database connected
pub fn add(&self, profile_id: Rc<i64>, is_active: bool) -> Result<i64, Error> {
pub fn add(&self, profile_id: &Rc<i64>, is_active: bool) -> Result<i64, Error> {
// Begin new transaction
let mut writable = self.connection.write().unwrap();
let tx = writable.transaction()?;
@ -80,7 +82,7 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
)
}
pub fn insert(tx: &Transaction, profile_id: Rc<i64>, is_active: bool) -> Result<usize, Error> {
pub fn insert(tx: &Transaction, profile_id: &Rc<i64>, is_active: bool) -> Result<usize, Error> {
tx.execute(
"INSERT INTO `profile_identity` (
`profile_id`,

View file

@ -29,16 +29,16 @@ impl Gemini {
// Constructors
/// Create new `Self`
pub fn new(
connection: Rc<RwLock<Connection>>,
profile_identity_id: Rc<i64>,
pub fn build(
connection: &Rc<RwLock<Connection>>,
profile_identity_id: &Rc<i64>,
) -> Result<Self, Error> {
// Init components
let auth = match Auth::new(connection.clone()) {
let auth = match Auth::new(connection) {
Ok(auth) => Rc::new(auth),
Err(e) => return Err(Error::Auth(e)),
};
let database = Rc::new(Database::new(connection, profile_identity_id.clone()));
let database = Rc::new(Database::build(connection, profile_identity_id));
let memory = Rc::new(Memory::new());
// Init `Self`

View file

@ -21,7 +21,7 @@ impl Auth {
// Constructors
/// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>) -> Result<Self, Error> {
pub fn new(connection: &Rc<RwLock<Connection>>) -> Result<Self, Error> {
// Init `Self`
let this = Self {
database: Rc::new(Database::new(connection)),

View file

@ -16,8 +16,10 @@ impl Database {
// Constructors
/// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>) -> Self {
Self { connection }
pub fn new(connection: &Rc<RwLock<Connection>>) -> Self {
Self {
connection: connection.clone(),
}
}
// Actions

View file

@ -17,10 +17,10 @@ impl Database {
// Constructors
/// Create new `Self`
pub fn new(connection: Rc<RwLock<Connection>>, profile_identity_id: Rc<i64>) -> Self {
pub fn build(connection: &Rc<RwLock<Connection>>, profile_identity_id: &Rc<i64>) -> Self {
Self {
connection,
profile_identity_id,
connection: connection.clone(),
profile_identity_id: profile_identity_id.clone(),
}
}