mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
remove direct memory access
This commit is contained in:
parent
33369e31ea
commit
19a07cdf1d
32 changed files with 112 additions and 111 deletions
|
|
@ -10,12 +10,12 @@ use memory::Memory;
|
|||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use sqlite::Transaction;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::sync::RwLock;
|
||||
|
||||
pub struct Bookmark {
|
||||
database: Database, // permanent storage
|
||||
pub memory: Arc<RwLock<Memory>>, // fast search index
|
||||
} // @TODO close memory member, Arc entire profile instead
|
||||
database: Database, // permanent storage
|
||||
memory: RwLock<Memory>, // fast search index
|
||||
}
|
||||
|
||||
impl Bookmark {
|
||||
// Constructors
|
||||
|
|
@ -24,7 +24,7 @@ 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 = Arc::new(RwLock::new(Memory::new()));
|
||||
let memory = RwLock::new(Memory::new());
|
||||
|
||||
// Build initial index
|
||||
{
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@ use memory::Memory;
|
|||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use sqlite::Transaction;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::sync::RwLock;
|
||||
|
||||
pub struct History {
|
||||
database: Database, // permanent storage
|
||||
pub memory: Arc<RwLock<Memory>>, // fast search index
|
||||
} // @TODO close memory member, Arc entire profile instead
|
||||
database: Database, // permanent storage
|
||||
memory: RwLock<Memory>, // fast search index
|
||||
}
|
||||
|
||||
impl History {
|
||||
// Constructors
|
||||
|
|
@ -24,7 +24,7 @@ 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 = Arc::new(RwLock::new(Memory::new()));
|
||||
let memory = RwLock::new(Memory::new());
|
||||
|
||||
for item in database.records(None, None)? {
|
||||
memory.write().unwrap().add(item)
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@ pub mod auth;
|
|||
|
||||
use anyhow::{bail, Result};
|
||||
pub use auth::Auth;
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
use std::{collections::HashMap, sync::RwLock};
|
||||
|
||||
/// Reduce disk usage by cache Auth index in memory
|
||||
pub struct Memory {
|
||||
index: RefCell<HashMap<String, i64>>,
|
||||
index: RwLock<HashMap<String, i64>>,
|
||||
}
|
||||
|
||||
impl Default for Memory {
|
||||
|
|
@ -21,7 +21,7 @@ impl Memory {
|
|||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
index: RefCell::new(HashMap::new()),
|
||||
index: RwLock::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.borrow_mut();
|
||||
let mut index = self.index.write().unwrap();
|
||||
|
||||
// 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.borrow_mut();
|
||||
let mut index = self.index.write().unwrap();
|
||||
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.borrow().iter() {
|
||||
for (value, &profile_identity_id) in self.index.read().unwrap().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.borrow().iter() {
|
||||
for (_, _profile_identity_id) in self.index.read().unwrap().iter() {
|
||||
if *_profile_identity_id == profile_identity_id {
|
||||
total += 1
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
use anyhow::{bail, Result};
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
use std::{collections::HashMap, sync::RwLock};
|
||||
|
||||
/// Reduce disk usage by cache index in memory
|
||||
pub struct Memory {
|
||||
index: RefCell<HashMap<i64, String>>,
|
||||
index: RwLock<HashMap<i64, String>>,
|
||||
}
|
||||
|
||||
impl Default for Memory {
|
||||
|
|
@ -18,7 +18,7 @@ impl Memory {
|
|||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
index: RefCell::new(HashMap::new()),
|
||||
index: RwLock::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.borrow_mut();
|
||||
let mut index = self.index.write().unwrap();
|
||||
|
||||
// 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.borrow().get(&id) {
|
||||
match self.index.read().unwrap().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.borrow_mut();
|
||||
let mut index = self.index.write().unwrap();
|
||||
index.clear();
|
||||
if index.is_empty() {
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
use super::database::Row;
|
||||
use std::cell::RefCell;
|
||||
use std::sync::RwLock;
|
||||
|
||||
/// Reduce disk usage by cache Bookmarks index in memory
|
||||
pub struct Memory {
|
||||
index: RefCell<Vec<Row>>,
|
||||
index: RwLock<Vec<Row>>,
|
||||
}
|
||||
|
||||
impl Memory {
|
||||
|
|
@ -12,7 +12,7 @@ impl Memory {
|
|||
/// Create new `Self`
|
||||
pub fn init() -> Self {
|
||||
Self {
|
||||
index: RefCell::new(Vec::new()),
|
||||
index: RwLock::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.borrow_mut().push(Row {
|
||||
self.index.write().unwrap().push(Row {
|
||||
id,
|
||||
query,
|
||||
is_default,
|
||||
|
|
@ -29,19 +29,19 @@ impl Memory {
|
|||
|
||||
/// Clear all records
|
||||
pub fn clear(&self) {
|
||||
self.index.borrow_mut().clear()
|
||||
self.index.write().unwrap().clear()
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
/// Get all records
|
||||
pub fn records(&self) -> Vec<Row> {
|
||||
self.index.borrow().clone()
|
||||
self.index.read().unwrap().clone()
|
||||
}
|
||||
|
||||
/// Get all records
|
||||
pub fn default(&self) -> Option<Row> {
|
||||
for record in self.index.borrow().iter() {
|
||||
for record in self.index.read().unwrap().iter() {
|
||||
if record.is_default {
|
||||
return Some(record.clone());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue