mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
remove async dependencies
This commit is contained in:
parent
e2b0cd6b0d
commit
b187f36028
33 changed files with 120 additions and 129 deletions
|
|
@ -10,11 +10,11 @@ use memory::Memory;
|
|||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use sqlite::Transaction;
|
||||
use std::sync::RwLock;
|
||||
use std::cell::RefCell;
|
||||
|
||||
pub struct Bookmark {
|
||||
database: Database, // permanent storage
|
||||
memory: RwLock<Memory>, // fast search index
|
||||
database: Database, // permanent storage
|
||||
memory: RefCell<Memory>, // fast search index
|
||||
}
|
||||
|
||||
impl Bookmark {
|
||||
|
|
@ -24,11 +24,11 @@ impl Bookmark {
|
|||
pub fn build(database_pool: &Pool<SqliteConnectionManager>, profile_id: i64) -> Result<Self> {
|
||||
// Init children components
|
||||
let database = Database::new(database_pool, profile_id);
|
||||
let memory = RwLock::new(Memory::new());
|
||||
let memory = RefCell::new(Memory::new());
|
||||
|
||||
// Build initial index
|
||||
{
|
||||
let mut memory = memory.write().unwrap();
|
||||
let mut memory = memory.borrow_mut();
|
||||
for item in database.records(None, None)? {
|
||||
memory.add(item);
|
||||
}
|
||||
|
|
@ -43,7 +43,7 @@ impl Bookmark {
|
|||
/// Toggle bookmark in `database` and `memory` index
|
||||
/// * return `true` on bookmark create, `false` on delete
|
||||
pub fn toggle(&self, request: &str, title: Option<&str>) -> Result<bool> {
|
||||
let mut memory = self.memory.write().unwrap();
|
||||
let mut memory = self.memory.borrow_mut();
|
||||
Ok(match memory.delete_by_request(request) {
|
||||
Some(item) => {
|
||||
self.database.delete(item.id)?;
|
||||
|
|
@ -64,20 +64,17 @@ impl Bookmark {
|
|||
|
||||
/// Check `request` exists in the memory index
|
||||
pub fn is_match_request(&self, request: &str) -> bool {
|
||||
self.memory.write().unwrap().is_match_request(request)
|
||||
self.memory.borrow_mut().is_match_request(request)
|
||||
}
|
||||
|
||||
/// Find Items match `request`
|
||||
pub fn contains_request(&self, request: &str, limit: Option<usize>) -> Vec<Item> {
|
||||
self.memory
|
||||
.write()
|
||||
.unwrap()
|
||||
.contains_request(request, limit)
|
||||
self.memory.borrow_mut().contains_request(request, limit)
|
||||
}
|
||||
|
||||
/// Get recent Items vector from `memory`, sorted by `ID` DESC
|
||||
pub fn recent(&self, limit: Option<usize>) -> Vec<Item> {
|
||||
self.memory.read().unwrap().recent(limit)
|
||||
self.memory.borrow().recent(limit)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,11 +10,11 @@ use memory::Memory;
|
|||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use sqlite::Transaction;
|
||||
use std::sync::RwLock;
|
||||
use std::cell::RefCell;
|
||||
|
||||
pub struct History {
|
||||
database: Database, // permanent storage
|
||||
memory: RwLock<Memory>, // fast search index
|
||||
database: Database, // permanent storage
|
||||
memory: RefCell<Memory>, // fast search index
|
||||
}
|
||||
|
||||
impl History {
|
||||
|
|
@ -24,10 +24,10 @@ impl History {
|
|||
pub fn build(database_pool: &Pool<SqliteConnectionManager>, profile_id: i64) -> Result<Self> {
|
||||
// Init children components
|
||||
let database = Database::build(database_pool, profile_id);
|
||||
let memory = RwLock::new(Memory::new());
|
||||
let memory = RefCell::new(Memory::new());
|
||||
|
||||
for item in database.records(None, None)? {
|
||||
memory.write().unwrap().add(item)
|
||||
memory.borrow_mut().add(item)
|
||||
}
|
||||
|
||||
// Return new `Self`
|
||||
|
|
@ -37,7 +37,7 @@ impl History {
|
|||
// Actions
|
||||
|
||||
pub fn save(&self) -> Result<()> {
|
||||
for item in self.memory.read().unwrap().items() {
|
||||
for item in self.memory.borrow().items() {
|
||||
if !item.is_saved {
|
||||
match item.id {
|
||||
Some(_) => {
|
||||
|
|
@ -56,7 +56,7 @@ impl History {
|
|||
|
||||
/// Create new history record
|
||||
pub fn open(&self, request: GString, title: Option<GString>) {
|
||||
let mut memory = self.memory.write().unwrap();
|
||||
let mut memory = self.memory.borrow_mut();
|
||||
if !memory.open(&request) {
|
||||
memory.add(Item {
|
||||
id: None,
|
||||
|
|
@ -71,24 +71,24 @@ impl History {
|
|||
|
||||
/// Close existing history record
|
||||
pub fn close(&self, request: &str) {
|
||||
self.memory.write().unwrap().close(request)
|
||||
self.memory.borrow_mut().close(request)
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
/// Get recently `opened` Items vector from the memory index, sorted by ASC
|
||||
pub fn recently_opened(&self, limit: Option<usize>) -> Vec<Item> {
|
||||
self.memory.read().unwrap().recently_opened(limit)
|
||||
self.memory.borrow().recently_opened(limit)
|
||||
}
|
||||
|
||||
/// Get recently `closed` Items vector from the memory index, sorted by ASC
|
||||
pub fn recently_closed(&self, limit: Option<usize>) -> Vec<Item> {
|
||||
self.memory.read().unwrap().recently_closed(limit)
|
||||
self.memory.borrow().recently_closed(limit)
|
||||
}
|
||||
|
||||
/// Get unordered Items vector that contains given `request`
|
||||
pub fn contains_request(&self, request: &str, limit: Option<usize>) -> Vec<Item> {
|
||||
self.memory.read().unwrap().contains_request(request, limit)
|
||||
self.memory.borrow().contains_request(request, limit)
|
||||
} // @TODO close memory member
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@ pub mod auth;
|
|||
|
||||
use anyhow::{Result, bail};
|
||||
pub use auth::Auth;
|
||||
use std::{collections::HashMap, sync::RwLock};
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
|
||||
/// Reduce disk usage by cache Auth index in memory
|
||||
pub struct Memory {
|
||||
index: RwLock<HashMap<String, i64>>,
|
||||
index: RefCell<HashMap<String, i64>>,
|
||||
}
|
||||
|
||||
impl Default for Memory {
|
||||
|
|
@ -21,7 +21,7 @@ impl Memory {
|
|||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
index: RwLock::new(HashMap::new()),
|
||||
index: RefCell::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -31,7 +31,7 @@ impl Memory {
|
|||
/// * validate record with same key does not exist yet
|
||||
pub fn add(&self, scope: String, profile_identity_id: i64) -> Result<()> {
|
||||
// Borrow shared index access
|
||||
let mut index = self.index.write().unwrap();
|
||||
let mut index = self.index.borrow_mut();
|
||||
|
||||
// Prevent existing key overwrite
|
||||
if index.contains_key(&scope) {
|
||||
|
|
@ -47,7 +47,7 @@ impl Memory {
|
|||
|
||||
/// Cleanup index
|
||||
pub fn clear(&self) -> Result<()> {
|
||||
let mut index = self.index.write().unwrap();
|
||||
let mut index = self.index.borrow_mut();
|
||||
index.clear();
|
||||
if index.is_empty() {
|
||||
Ok(())
|
||||
|
|
@ -62,7 +62,7 @@ impl Memory {
|
|||
pub fn match_scope(&self, scope: &str) -> Option<Auth> {
|
||||
let mut result = Vec::new();
|
||||
|
||||
for (value, &profile_identity_id) in self.index.read().unwrap().iter() {
|
||||
for (value, &profile_identity_id) in self.index.borrow().iter() {
|
||||
if scope.starts_with(value) {
|
||||
result.push(Auth {
|
||||
profile_identity_id,
|
||||
|
|
@ -83,7 +83,7 @@ impl Memory {
|
|||
/// * see also parent `is_match_request`
|
||||
pub fn total(&self, profile_identity_id: i64) -> usize {
|
||||
let mut total = 0;
|
||||
for (_, _profile_identity_id) in self.index.read().unwrap().iter() {
|
||||
for (_, _profile_identity_id) in self.index.borrow().iter() {
|
||||
if *_profile_identity_id == profile_identity_id {
|
||||
total += 1
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
use anyhow::{Result, bail};
|
||||
use std::{collections::HashMap, sync::RwLock};
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
|
||||
/// Reduce disk usage by cache index in memory
|
||||
pub struct Memory {
|
||||
index: RwLock<HashMap<i64, String>>,
|
||||
index: RefCell<HashMap<i64, String>>,
|
||||
}
|
||||
|
||||
impl Default for Memory {
|
||||
|
|
@ -18,7 +18,7 @@ impl Memory {
|
|||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
index: RwLock::new(HashMap::new()),
|
||||
index: RefCell::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -28,7 +28,7 @@ impl Memory {
|
|||
/// * validate record with same key does not exist yet
|
||||
pub fn add(&self, profile_identity_id: i64, pem: String) -> Result<()> {
|
||||
// Borrow shared index access
|
||||
let mut index = self.index.write().unwrap();
|
||||
let mut index = self.index.borrow_mut();
|
||||
|
||||
// Prevent existing key overwrite
|
||||
if index.contains_key(&profile_identity_id) {
|
||||
|
|
@ -44,7 +44,7 @@ impl Memory {
|
|||
|
||||
/// Get `pem` clone by `id` from memory index
|
||||
pub fn get(&self, id: i64) -> Result<String> {
|
||||
match self.index.read().unwrap().get(&id) {
|
||||
match self.index.borrow().get(&id) {
|
||||
Some(pem) => Ok(pem.clone()),
|
||||
None => bail!("Record `{id}` not found in memory index"),
|
||||
}
|
||||
|
|
@ -52,7 +52,7 @@ impl Memory {
|
|||
|
||||
/// Cleanup index
|
||||
pub fn clear(&self) -> Result<()> {
|
||||
let mut index = self.index.write().unwrap();
|
||||
let mut index = self.index.borrow_mut();
|
||||
index.clear();
|
||||
if index.is_empty() {
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
use super::database::Row;
|
||||
use std::sync::RwLock;
|
||||
use std::cell::RefCell;
|
||||
|
||||
/// Reduce disk usage by cache Bookmarks index in memory
|
||||
pub struct Memory {
|
||||
index: RwLock<Vec<Row>>,
|
||||
index: RefCell<Vec<Row>>,
|
||||
}
|
||||
|
||||
impl Memory {
|
||||
|
|
@ -12,7 +12,7 @@ impl Memory {
|
|||
/// Create new `Self`
|
||||
pub fn init() -> Self {
|
||||
Self {
|
||||
index: RwLock::new(Vec::new()),
|
||||
index: RefCell::new(Vec::new()),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -20,7 +20,7 @@ impl Memory {
|
|||
|
||||
/// Add new record
|
||||
pub fn push(&self, id: i64, query: String, is_default: bool) {
|
||||
self.index.write().unwrap().push(Row {
|
||||
self.index.borrow_mut().push(Row {
|
||||
id,
|
||||
query,
|
||||
is_default,
|
||||
|
|
@ -29,19 +29,19 @@ impl Memory {
|
|||
|
||||
/// Clear all records
|
||||
pub fn clear(&self) {
|
||||
self.index.write().unwrap().clear()
|
||||
self.index.borrow_mut().clear()
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
/// Get all records
|
||||
pub fn records(&self) -> Vec<Row> {
|
||||
self.index.read().unwrap().clone()
|
||||
self.index.borrow().clone()
|
||||
}
|
||||
|
||||
/// Get all records
|
||||
pub fn default(&self) -> Option<Row> {
|
||||
for record in self.index.read().unwrap().iter() {
|
||||
for record in self.index.borrow().iter() {
|
||||
if record.is_default {
|
||||
return Some(record.clone());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,14 +9,14 @@ use r2d2::Pool;
|
|||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use sourceview::prelude::TlsCertificateExt;
|
||||
use sqlite::Transaction;
|
||||
use std::sync::RwLock;
|
||||
use std::cell::RefCell;
|
||||
|
||||
/// TOFU wrapper for the Gemini protocol
|
||||
///
|
||||
/// https://geminiprotocol.net/docs/protocol-specification.gmi#tls-server-certificate-validation
|
||||
pub struct Tofu {
|
||||
database: Database,
|
||||
memory: RwLock<Vec<Certificate>>,
|
||||
memory: RefCell<Vec<Certificate>>,
|
||||
}
|
||||
|
||||
impl Tofu {
|
||||
|
|
@ -26,11 +26,11 @@ impl Tofu {
|
|||
let database = Database::init(database_pool, profile_id);
|
||||
|
||||
let records = database.records()?;
|
||||
let memory = RwLock::new(Vec::with_capacity(records.len()));
|
||||
let memory = RefCell::new(Vec::with_capacity(records.len()));
|
||||
|
||||
{
|
||||
// build in-memory index...
|
||||
let mut m = memory.write().unwrap();
|
||||
let mut m = memory.borrow_mut();
|
||||
for r in records {
|
||||
m.push(Certificate::from_db(Some(r.id), &r.pem, r.time)?)
|
||||
}
|
||||
|
|
@ -43,8 +43,7 @@ impl Tofu {
|
|||
|
||||
pub fn add(&self, tls_certificate: TlsCertificate) -> Result<()> {
|
||||
self.memory
|
||||
.write()
|
||||
.unwrap()
|
||||
.borrow_mut()
|
||||
.push(Certificate::from_tls_certificate(tls_certificate)?);
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -59,7 +58,7 @@ impl Tofu {
|
|||
}
|
||||
if let Some(h) = uri.host() {
|
||||
let k = f(&h);
|
||||
let m = self.memory.read().unwrap();
|
||||
let m = self.memory.borrow();
|
||||
let b: Vec<TlsCertificate> = m
|
||||
.iter()
|
||||
.filter_map(|certificate| {
|
||||
|
|
@ -80,7 +79,7 @@ impl Tofu {
|
|||
|
||||
/// Save in-memory index to the permanent database (on app close)
|
||||
pub fn save(&self) -> Result<()> {
|
||||
for c in self.memory.read().unwrap().iter() {
|
||||
for c in self.memory.borrow().iter() {
|
||||
if c.id().is_none() {
|
||||
self.database.add(c.time(), &c.pem())?;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue