remove direct memory access

This commit is contained in:
yggverse 2025-03-14 12:51:33 +02:00
parent 33369e31ea
commit 19a07cdf1d
32 changed files with 112 additions and 111 deletions

View File

@ -10,12 +10,12 @@ use gtk::{
prelude::{ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt},
};
use sqlite::Transaction;
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
const APPLICATION_ID: &str = "io.github.yggverse.Yoda";
pub struct App {
profile: Rc<Profile>,
profile: Arc<Profile>,
application: Application,
}
@ -30,7 +30,7 @@ impl App {
.build();
// Init components
let profile = Rc::new(profile);
let profile = Arc::new(profile);
let browser = Rc::new(Browser::build(&profile));
// Prevent startup warning @TODO

View File

@ -18,7 +18,7 @@ use gtk::{
FileLauncher,
};
use sqlite::Transaction;
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
pub struct Browser {
pub action: Rc<Action>,
@ -30,7 +30,7 @@ impl Browser {
// Constructors
/// Build new `Self`
pub fn build(profile: &Rc<Profile>) -> Browser {
pub fn build(profile: &Arc<Profile>) -> Browser {
// Init components
let action = Rc::new(Action::new());
let window = Rc::new(Window::build(profile, &action));

View File

@ -11,7 +11,7 @@ use anyhow::Result;
use gtk::{prelude::BoxExt, Box, Orientation};
use header::Header;
use sqlite::Transaction;
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
use tab::Tab;
pub struct Window {
@ -24,7 +24,7 @@ impl Window {
// Constructors
/// Build new `Self`
pub fn build(profile: &Rc<Profile>, browser_action: &Rc<BrowserAction>) -> Self {
pub fn build(profile: &Arc<Profile>, browser_action: &Rc<BrowserAction>) -> Self {
// Init local actions
let action = Rc::new(Action::new());

View File

@ -4,12 +4,12 @@ use super::{Action as WindowAction, BrowserAction, Profile};
use adw::{TabView, ToolbarView};
use bar::Bar;
use gtk::Box;
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
pub trait Header {
fn header(
action: (&Rc<BrowserAction>, &Rc<WindowAction>),
profile: &Rc<Profile>,
profile: &Arc<Profile>,
tab_view: &TabView,
) -> Self;
}
@ -20,7 +20,7 @@ impl Header for ToolbarView {
/// Build new `Self`
fn header(
(browser_action, window_action): (&Rc<BrowserAction>, &Rc<WindowAction>),
profile: &Rc<Profile>,
profile: &Arc<Profile>,
tab_view: &TabView,
) -> Self {
let toolbar_view = ToolbarView::builder().build();

View File

@ -9,12 +9,12 @@ use tab::Tab;
use super::{BrowserAction, Profile, WindowAction};
use adw::{TabBar, TabView};
use gtk::{prelude::BoxExt, Box, MenuButton, Orientation};
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
pub trait Bar {
fn bar(
action: (&Rc<BrowserAction>, &Rc<WindowAction>),
profile: &Rc<Profile>,
profile: &Arc<Profile>,
view: &TabView,
) -> Self;
}
@ -25,7 +25,7 @@ impl Bar for Box {
/// Build new `Self`
fn bar(
(browser_action, window_action): (&Rc<BrowserAction>, &Rc<WindowAction>),
profile: &Rc<Profile>,
profile: &Arc<Profile>,
view: &TabView,
) -> Self {
let g_box = Box::builder()

View File

@ -6,13 +6,13 @@ use gtk::{
Align, MenuButton,
};
use indexmap::IndexMap;
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
// Config options
const LABEL_MAX_LENGTH: usize = 28;
pub trait Menu {
fn menu(action: (&Rc<BrowserAction>, &Rc<WindowAction>), profile: &Rc<Profile>) -> Self;
fn menu(action: (&Rc<BrowserAction>, &Rc<WindowAction>), profile: &Arc<Profile>) -> Self;
}
#[rustfmt::skip] // @TODO template builder?
@ -22,7 +22,7 @@ impl Menu for MenuButton {
/// Build new `Self`
fn menu(
(browser_action, window_action): (&Rc<BrowserAction>, &Rc<WindowAction>),
profile: &Rc<Profile>,
profile: &Arc<Profile>,
) -> Self {
// Main
let main = gio::Menu::new();

View File

@ -13,13 +13,13 @@ pub use item::Item;
use menu::Menu;
use sourceview::prelude::IsA;
use sqlite::Transaction;
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use std::{cell::RefCell, collections::HashMap, rc::Rc, sync::Arc};
// Main
pub struct Tab {
browser_action: Rc<BrowserAction>,
window_action: Rc<WindowAction>,
profile: Rc<Profile>,
profile: Arc<Profile>,
index: Rc<RefCell<HashMap<TabPage, Rc<Item>>>>,
pub action: Rc<Action>,
pub tab_view: TabView,
@ -30,7 +30,7 @@ impl Tab {
/// Build new `Self`
pub fn build(
profile: &Rc<Profile>,
profile: &Arc<Profile>,
(browser_action, window_action): (&Rc<BrowserAction>, &Rc<WindowAction>),
) -> Self {
let action = Rc::new(Action::new());

View File

@ -15,7 +15,7 @@ use gtk::{
};
use page::Page;
use sqlite::Transaction;
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
pub struct Item {
// Multi-protocol handler
@ -32,7 +32,7 @@ impl Item {
/// Build new `Self`
pub fn build(
(tab_page, target_child): (&TabPage, &Box),
profile: &Rc<Profile>,
profile: &Arc<Profile>,
(browser_action, window_action, tab_action): (
&Rc<BrowserAction>,
&Rc<WindowAction>,

View File

@ -9,21 +9,21 @@ use gtk::{
glib::{Uri, UriFlags},
prelude::{ActionExt, CancellableExt},
};
use std::{cell::Cell, rc::Rc};
use std::{cell::Cell, rc::Rc, sync::Arc};
/// Multi-protocol client API for tab `Item`
pub struct Client {
cancellable: Cell<Cancellable>,
driver: Rc<Driver>,
page: Rc<Page>,
profile: Rc<Profile>,
profile: Arc<Profile>,
}
impl Client {
// Constructors
/// Create new `Self`
pub fn init(profile: &Rc<Profile>, page: &Rc<Page>) -> Self {
pub fn init(profile: &Arc<Profile>, page: &Rc<Page>) -> Self {
Self {
cancellable: Cell::new(Cancellable::new()),
driver: Rc::new(Driver::build(page)),
@ -108,7 +108,7 @@ impl Client {
/// Create request using async DNS resolver (slow method)
/// * return suggestion [Uri](https://docs.gtk.org/glib/struct.Uri.html) on failure (to handle as redirect)
fn lookup(
profile: &Rc<Profile>,
profile: &Arc<Profile>,
query: &str,
cancellable: Cancellable,
callback: impl FnOnce(Rc<Feature>, Cancellable, Result<Uri, String>) + 'static,

View File

@ -12,10 +12,10 @@ use input::Input;
use navigation::Navigation;
use search::Search;
use sqlite::Transaction;
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
pub struct Page {
pub profile: Rc<Profile>,
pub profile: Arc<Profile>,
// Actions
pub browser_action: Rc<BrowserAction>,
pub item_action: Rc<ItemAction>,
@ -38,7 +38,7 @@ impl Page {
// Constructors
pub fn build(
profile: &Rc<Profile>,
profile: &Arc<Profile>,
(browser_action, window_action, tab_action, item_action): (
&Rc<BrowserAction>,
&Rc<WindowAction>,

View File

@ -18,7 +18,7 @@ use home::Home;
use reload::Reload;
use request::Request;
use sqlite::Transaction;
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
const MARGIN: i32 = 6;
const SPACING: i32 = 6;
@ -30,7 +30,7 @@ pub struct Navigation {
impl Navigation {
pub fn build(
profile: &Rc<Profile>,
profile: &Arc<Profile>,
(window_action, tab_action, item_action): (
&Rc<WindowAction>,
&Rc<TabAction>,

View File

@ -3,18 +3,18 @@ use gtk::{
prelude::{ActionExt, ButtonExt, EditableExt, WidgetExt},
Button, Entry,
};
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
const ICON_NAME: (&str, &str) = ("non-starred-symbolic", "starred-symbolic");
const TOOLTIP_TEXT: (&str, &str) = ("Add Bookmark", "Remove Bookmark");
pub trait Bookmark {
fn bookmark(action: &Rc<WindowAction>, profile: &Rc<Profile>, request: &Entry) -> Self;
fn bookmark(action: &Rc<WindowAction>, profile: &Arc<Profile>, request: &Entry) -> Self;
fn update(&self, profile: &Profile, request: &Entry);
}
impl Bookmark for Button {
fn bookmark(action: &Rc<WindowAction>, profile: &Rc<Profile>, request: &Entry) -> Self {
fn bookmark(action: &Rc<WindowAction>, profile: &Arc<Profile>, request: &Entry) -> Self {
let button = Button::builder()
.action_name(format!(
"{}.{}",

View File

@ -14,7 +14,7 @@ use gtk::{
};
use primary_icon::PrimaryIcon;
use sqlite::Transaction;
use std::{cell::Cell, rc::Rc};
use std::{cell::Cell, rc::Rc, sync::Arc};
use suggestion::Suggestion;
const PREFIX_DOWNLOAD: &str = "download:";
@ -23,14 +23,14 @@ const PREFIX_SOURCE: &str = "source:";
pub struct Request {
pub entry: Entry,
suggestion: Rc<Suggestion>,
profile: Rc<Profile>,
profile: Arc<Profile>,
}
impl Request {
// Constructors
/// Build new `Self`
pub fn build(item_action: &Rc<ItemAction>, profile: &Rc<Profile>) -> Self {
pub fn build(item_action: &Rc<ItemAction>, profile: &Arc<Profile>) -> Self {
// Init main widget
let entry = Entry::builder()
.placeholder_text("URL or search term...")
@ -327,7 +327,7 @@ fn update_secondary_icon(entry: &Entry) {
}
/// Present Identity [AlertDialog](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.AlertDialog.html) for `Self`
fn show_identity_dialog(entry: &Entry, profile: &Rc<Profile>) {
fn show_identity_dialog(entry: &Entry, profile: &Arc<Profile>) {
// connect identity traits
use identity::{Common, Unsupported};
if let Some(uri) = uri(entry) {
@ -353,7 +353,7 @@ fn show_identity_dialog(entry: &Entry, profile: &Rc<Profile>) {
}
/// Present Search providers [AlertDialog](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.AlertDialog.html) for `Self`
fn show_search_dialog(entry: &Entry, profile: &Rc<Profile>) {
fn show_search_dialog(entry: &Entry, profile: &Arc<Profile>) {
use search::Search;
AlertDialog::search(profile).present(Some(entry))
}

View File

@ -5,13 +5,16 @@ use crate::Profile;
use action::Action as WidgetAction;
use adw::AlertDialog;
use gtk::{glib::Uri, prelude::EditableExt};
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
// Select options
pub trait Common {
fn common(profile: &Rc<Profile>, request: &Uri, callback: &Rc<impl Fn(bool) + 'static>)
-> Self;
fn common(
profile: &Arc<Profile>,
request: &Uri,
callback: &Rc<impl Fn(bool) + 'static>,
) -> Self;
}
impl Common for AlertDialog {
@ -19,7 +22,7 @@ impl Common for AlertDialog {
/// Create new `Self`
fn common(
profile: &Rc<Profile>,
profile: &Arc<Profile>,
request: &Uri,
callback: &Rc<impl Fn(bool) + 'static>,
) -> Self {

View File

@ -19,7 +19,7 @@ use gtk::{
prelude::{BoxExt, WidgetExt},
Box, Button, Entry, Orientation,
};
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
pub struct Form {
// pub action_widget: Rc<Action>,
@ -30,14 +30,14 @@ pub struct Form {
pub name: Entry,
pub save: Rc<Save>,
pub g_box: Box,
profile: Rc<Profile>,
profile: Arc<Profile>,
}
impl Form {
// Constructors
/// Create new `Self`
pub fn build(widget_action: &Rc<WidgetAction>, profile: &Rc<Profile>, request: &Uri) -> Self {
pub fn build(widget_action: &Rc<WidgetAction>, profile: &Arc<Profile>, request: &Uri) -> Self {
// Init components
let list = Rc::new(List::build(widget_action, profile, request));
let file = Rc::new(File::build(widget_action));

View File

@ -9,7 +9,7 @@ use gtk::{
prelude::{ButtonExt, WidgetExt},
Button,
};
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
// Defaults
@ -30,7 +30,7 @@ impl Drop {
// Constructors
/// Create new `Self`
pub fn build(profile: &Rc<Profile>, list: &Rc<List>) -> Self {
pub fn build(profile: &Arc<Profile>, list: &Rc<List>) -> Self {
// Init main widget
let button = Button::builder()
.label(LABEL)

View File

@ -8,12 +8,12 @@ use gtk::{
prelude::{ButtonExt, WidgetExt},
Button,
};
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
pub trait Exit {
fn exit(
widget_action: &Rc<WidgetAction>,
profile: &Rc<Profile>,
profile: &Arc<Profile>,
list: &Rc<List>,
request: &Uri,
) -> Self;
@ -25,7 +25,7 @@ impl Exit for Button {
/// Create new `Self`
fn exit(
widget_action: &Rc<WidgetAction>,
profile: &Rc<Profile>,
profile: &Arc<Profile>,
list: &Rc<List>,
request: &Uri,
) -> Self {

View File

@ -1,5 +1,5 @@
pub mod item;
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
use item::Item;
@ -25,7 +25,7 @@ impl List {
// Constructors
/// Create new `Self`
pub fn build(widget_action: &Rc<WidgetAction>, profile: &Rc<Profile>, request: &Uri) -> Self {
pub fn build(widget_action: &Rc<WidgetAction>, profile: &Arc<Profile>, request: &Uri) -> Self {
// Init dropdown items
let guest_session = Item::new_guest_session();
let generate_pem = Item::new_generate_pem();

View File

@ -14,7 +14,7 @@ use gtk::{
gio::TlsCertificate,
glib::{self, Object},
};
use std::rc::Rc;
use std::sync::Arc;
glib::wrapper! {
pub struct Item(ObjectSubclass<imp::Item>);
@ -54,7 +54,7 @@ impl Item {
}
pub fn new_profile_identity_id(
profile: &Rc<Profile>,
profile: &Arc<Profile>,
profile_identity_id: i64,
auth_url: &str,
) -> Result<Self, Error> {
@ -96,7 +96,7 @@ impl Item {
// Actions
/// Update properties for `Self` for given `Profile` and `auth_url`
pub fn update(&self, profile: &Rc<Profile>, auth_url: &str) -> Result<(), Error> {
pub fn update(&self, profile: &Arc<Profile>, auth_url: &str) -> Result<(), Error> {
// Update item depending on value type
match self.value_enum() {
Value::ProfileIdentityId(profile_identity_id) => {

View File

@ -1,8 +1,8 @@
use crate::profile::Profile;
use std::rc::Rc;
use std::sync::Arc;
pub fn new_for_profile_identity_id(
profile: &Rc<Profile>,
profile: &Arc<Profile>,
profile_identity_id: i64,
auth_url: &str,
) -> bool {

View File

@ -9,7 +9,7 @@ use gtk::{
prelude::{ButtonExt, FileExt, OutputStreamExtManual, WidgetExt},
Button, FileDialog, FileFilter, Window,
};
use std::{path::MAIN_SEPARATOR, rc::Rc};
use std::{path::MAIN_SEPARATOR, rc::Rc, sync::Arc};
const LABEL: &str = "Export";
const TOOLTIP_TEXT: &str = "Export selected identity to file";
@ -24,7 +24,7 @@ impl Save {
// Constructors
/// Create new `Self`
pub fn build(profile: &Rc<Profile>, list: &Rc<List>) -> Self {
pub fn build(profile: &Arc<Profile>, list: &Rc<List>) -> Self {
// Init main widget
let button = Button::builder()
.label(LABEL)
@ -46,7 +46,7 @@ impl Save {
button.set_sensitive(false);
// Create PEM file based on option ID selected
match Certificate::build(profile.clone(), profile_identity_id) {
match Certificate::build(&profile, profile_identity_id) {
Ok(certificate) => {
// Init file filters related with PEM extension
let filters = ListStore::new::<FileFilter>();

View File

@ -2,7 +2,7 @@ use anyhow::{bail, Result};
use crate::profile::Profile;
use gtk::{gio::TlsCertificate, prelude::TlsCertificateExt};
use std::rc::Rc;
use std::sync::Arc;
/// Certificate details holder for export to file action
pub struct Certificate {
@ -14,7 +14,7 @@ impl Certificate {
// Constructors
/// Create new `Self`
pub fn build(profile: Rc<Profile>, profile_identity_id: i64) -> Result<Self> {
pub fn build(profile: &Arc<Profile>, profile_identity_id: i64) -> Result<Self> {
let record = profile.identity.database.record(profile_identity_id)?;
match record {
Some(identity) => Ok(Self {

View File

@ -10,6 +10,7 @@ use form::{list::item::Value, list::Item, Form, Query};
use gtk::prelude::{EditableExt, WidgetExt};
use sourceview::prelude::CastNone;
use std::rc::Rc;
use std::sync::Arc;
// Response variants
const RESPONSE_APPLY: (&str, &str) = ("apply", "Apply");
@ -17,14 +18,14 @@ const RESPONSE_CANCEL: (&str, &str) = ("cancel", "Cancel");
// const RESPONSE_MANAGE: (&str, &str) = ("manage", "Manage");
pub trait Search {
fn search(profile: &Rc<Profile>) -> Self;
fn search(profile: &Arc<Profile>) -> Self;
}
impl Search for AlertDialog {
// Constructors
/// Create new `Self`
fn search(profile: &Rc<Profile>) -> Self {
fn search(profile: &Arc<Profile>) -> Self {
// Init child container
let form = Rc::new(Form::build(profile));

View File

@ -7,7 +7,7 @@ use drop::Drop;
use gtk::{prelude::BoxExt, Box, Button, Entry, Orientation};
use list::List;
pub use query::Query;
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
pub struct Form {
pub drop: Button,
@ -20,7 +20,7 @@ impl Form {
// Constructors
/// Create new `Self`
pub fn build(profile: &Rc<Profile>) -> Self {
pub fn build(profile: &Arc<Profile>) -> Self {
// Init components
let list = Rc::new(List::build(profile));
let query = Entry::query();

View File

@ -9,17 +9,17 @@ use gtk::{
prelude::{ButtonExt, WidgetExt},
Button,
};
use std::rc::Rc;
use std::{rc::Rc, sync::Arc};
pub trait Drop {
fn drop(profile: &Rc<Profile>, list: &Rc<List>) -> Self;
fn drop(profile: &Arc<Profile>, list: &Rc<List>) -> Self;
}
impl Drop for Button {
// Constructors
/// Create new `Self`
fn drop(profile: &Rc<Profile>, list: &Rc<List>) -> Self {
fn drop(profile: &Arc<Profile>, list: &Rc<List>) -> Self {
// Defaults
const LABEL: &str = "Delete";

View File

@ -10,7 +10,7 @@ use gtk::{
Align, Box, DropDown, Label, ListItem, Orientation, SignalListItemFactory,
};
pub use item::Item;
use std::rc::Rc;
use std::sync::Arc;
pub struct List {
pub dropdown: DropDown,
@ -21,7 +21,7 @@ impl List {
// Constructors
/// Create new `Self`
pub fn build(profile: &Rc<Profile>) -> Self {
pub fn build(profile: &Arc<Profile>) -> Self {
// Init dropdown items
let new_search_provider = Item::add();

View File

@ -17,14 +17,14 @@ use gtk::{
};
pub use item::Item;
use sourceview::prelude::ListModelExt;
use std::{cell::RefCell, rc::Rc};
use std::{cell::RefCell, rc::Rc, sync::Arc};
pub struct Suggestion {
list_store: ListStore,
list_view: ListView,
single_selection: SingleSelection,
request: Entry,
profile: Rc<Profile>,
profile: Arc<Profile>,
popover: Popover,
pub signal_handler_id: Rc<RefCell<Option<SignalHandlerId>>>,
}
@ -33,7 +33,7 @@ impl Suggestion {
// Constructors
/// Create new `Self`
pub fn build(profile: &Rc<Profile>, request: &Entry) -> Self {
pub fn build(profile: &Arc<Profile>, request: &Entry) -> Self {
let signal_handler_id = Rc::new(RefCell::new(None));
let list_store = ListStore::new::<Item>();
let single_selection = {
@ -162,15 +162,12 @@ impl Suggestion {
let query = self.request.text();
let popover = self.popover.clone();
let list_store = self.list_store.clone();
let history = self.profile.history.memory.clone(); // @TODO
let bookmark = self.profile.bookmark.memory.clone(); // @TODO
let profile = self.profile.clone();
gtk::glib::spawn_future_local(async move {
let list_items = gtk::gio::spawn_blocking(move || {
let result = history
.read()
.unwrap()
let result = profile
.history
.contains_request(&query, limit)
.into_iter()
.sorted_by(|a, b| Ord::cmp(&b.opened.count, &a.opened.count));
@ -184,7 +181,7 @@ impl Suggestion {
list_items.push((
title,
subtitle,
bookmark.read().unwrap().is_match_request(&item.request),
profile.bookmark.is_match_request(&item.request),
item.request,
))
}

View File

@ -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
{

View File

@ -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)

View File

@ -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
}

View File

@ -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(())

View File

@ -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());
}