Introduce FileLockEntry cache

Holy shit, this compiles!

Have a look at the documentation from src/cache.rs - there's a rather
long description why we need this.
This commit is contained in:
Matthias Beyer 2017-01-21 17:35:48 +01:00
parent 7aed2d2689
commit e956c51412
3 changed files with 181 additions and 18 deletions

62
libimagruby/src/cache.rs Normal file
View file

@ -0,0 +1,62 @@
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
use std::collections::BTreeMap;
use std::sync::Arc;
use std::sync::Mutex;
use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreId;
lazy_static! {
/// A cache for FileLockEntry objects.
///
/// # Why do we need this?
///
/// "ruru" does us give the ability to move objects to the Ruby namespace by wrapping them into
/// a struct that can be handled by the Ruby VM. This is awesome.
/// Although, we cannot get the Object back into the Rust namespace (at least not as `Object`,
/// only as `&mut Object`).
///
/// Now, have a look at `Store::save_as()` for example. It has the type signature
/// `fn save_as(&self, FileLockEntry, StoreId) -> Result<()>;`.
/// This means that we need to _move_ a `FileLockEntry` into the Store.
///
/// But we cannot, if the FileLockEntry is in the Ruby namespace (we cannot get it back).
///
/// The solution is simple and complex at the same time: Do not move any object into the Ruby
/// namespace!
///
/// What we do: If the Ruby code wants us to get a `FileLockEntry`, we actually move the
/// `FileLockEntry` into this `FILE_LOCK_ENTRY_CACHE` and give the Ruby process a `Handle` for
/// the `FileLockEntry` object.
///
/// From the Ruby world, it looks like a `FileLockEntry`, but it is not. The implementations in
/// this very library fetch the `FileLockEntry` from this cache and operate on it, putting it
/// back into this cache afterwards.
///
/// # Performance?
///
/// I don't care right now. It is Ruby, it is slow anyways. If it works, I don't mind. And I
/// don't see why we couldn't change this implementation later under the hood...
pub static ref FILE_LOCK_ENTRY_CACHE: Arc<Mutex<BTreeMap<StoreId, FileLockEntry<'static>>>> = {
Arc::new(Mutex::new(BTreeMap::new()))
};
}

View file

@ -28,6 +28,8 @@ extern crate libimagstore;
extern crate libimagrt; extern crate libimagrt;
#[macro_use] extern crate libimagutil; #[macro_use] extern crate libimagutil;
mod cache;
pub mod imag; pub mod imag;
pub mod store; pub mod store;
pub mod toml_utils; pub mod toml_utils;

View file

@ -97,6 +97,48 @@ macro_rules! typecheck {
} }
/// Helper macro for operating on FILE_LOCK_ENTRY_CACHE object
///
/// This macro fetches an ARC from the FILE_LOCK_ENTRY_CACHE, then locks the Mutex inside it
/// and calls the $operation on the element inside the Mutex, this synchronizing the
/// operation on the FILE_LOCK_ENTRY_CACHE.
///
/// Yes, this is performance-wise not very elegant, but we're working with Ruby, and we need
/// to cache objects (why, see documentation for FILE_LOCK_ENTRY_CACHE).
///
macro_rules! operate_on_fle_cache {
(mut |$name: ident| $operation: block) => {{
use cache::FILE_LOCK_ENTRY_CACHE;
let arc = FILE_LOCK_ENTRY_CACHE.clone();
{
let lock = arc.lock();
match lock {
Ok(mut $name) => { $operation },
Err(e) => {
VM::raise(Class::from_existing("RuntimeError"), e.description());
NilClass::new().to_any_object()
}
}
}
}};
(|$name: ident| $operation: block) => {{
use cache::FILE_LOCK_ENTRY_CACHE;
let arc = FILE_LOCK_ENTRY_CACHE.clone();
{
let lock = arc.lock();
match lock {
Ok($name) => { $operation },
Err(e) => {
VM::raise(Class::from_existing("RuntimeError"), e.description());
NilClass::new().to_any_object()
}
}
}
}};
}
#[allow(unused_variables)] #[allow(unused_variables)]
pub mod storeid { pub mod storeid {
use std::path::PathBuf; use std::path::PathBuf;
@ -250,31 +292,40 @@ pub mod store {
use libimagstore::store::EntryHeader; use libimagstore::store::EntryHeader;
use libimagstore::store::EntryContent; use libimagstore::store::EntryContent;
use libimagstore::store::Entry; use libimagstore::store::Entry;
use libimagstore::storeid::StoreId;
use ruby_utils::IntoToml; use ruby_utils::IntoToml;
use toml_utils::IntoRuby; use toml_utils::IntoRuby;
use store::Wrap; use store::Wrap;
use store::Unwrap; use store::Unwrap;
use cache::FILE_LOCK_ENTRY_CACHE;
pub struct FLECustomWrapper(Box<FLE<'static>>); pub struct FileLockEntryHandle(StoreId);
impl Deref for FLECustomWrapper { impl FileLockEntryHandle {
type Target = Box<FLE<'static>>; pub fn new(id: StoreId) -> FileLockEntryHandle {
FileLockEntryHandle(id)
}
}
impl Deref for FileLockEntryHandle {
type Target = StoreId;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
} }
} }
impl DerefMut for FLECustomWrapper { impl DerefMut for FileLockEntryHandle {
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0 &mut self.0
} }
} }
wrappable_struct!(FLECustomWrapper, FileLockEntryWrapper, FLE_WRAPPER); wrappable_struct!(FileLockEntryHandle, FileLockEntryWrapper, FLE_WRAPPER);
class!(RFileLockEntry); class!(RFileLockEntry);
impl_unwrap!(RFileLockEntry, FLECustomWrapper, FLE_WRAPPER); impl_unwrap!(RFileLockEntry, FileLockEntryHandle, FLE_WRAPPER);
impl_wrap!(FileLockEntryHandle, FLE_WRAPPER);
impl_verified_object!(RFileLockEntry); impl_verified_object!(RFileLockEntry);
methods!( methods!(
@ -282,11 +333,29 @@ pub mod store {
itself, itself,
fn r_get_location() -> AnyObject { fn r_get_location() -> AnyObject {
itself.get_data(&*FLE_WRAPPER).get_location().clone().wrap() operate_on_fle_cache!(|hm| {
match hm.get(itself.get_data(&*FLE_WRAPPER)) {
Some(el) => el.get_location().clone().wrap(),
None => {
VM::raise(Class::from_existing("RuntimeError"),
"Tried to operate on non-existing object");
NilClass::new().to_any_object()
}
}
})
} }
fn r_get_header() -> AnyObject { fn r_get_header() -> AnyObject {
itself.get_data(&*FLE_WRAPPER).get_header().clone().wrap() operate_on_fle_cache!(|hm| {
match hm.get(itself.get_data(&*FLE_WRAPPER)) {
Some(el) => el.get_header().clone().wrap(),
None => {
VM::raise(Class::from_existing("RuntimeError"),
"Tried to operate on non-existing object");
NilClass::new().to_any_object()
}
}
})
} }
fn r_set_header(hdr: Hash) -> NilClass { fn r_set_header(hdr: Hash) -> NilClass {
@ -294,20 +363,38 @@ pub mod store {
use toml_utils::IntoRuby; use toml_utils::IntoRuby;
use toml::Value; use toml::Value;
let mut header = itself.get_data(&*FLE_WRAPPER).get_header_mut(); let entryheader = match typecheck!(hdr or return NilClass::new()).into_toml() {
Value::Table(t) => EntryHeader::from(t),
match typecheck!(hdr or return NilClass::new()).into_toml() {
Value::Table(t) => *header = EntryHeader::from(t),
_ => { _ => {
let ec = Class::from_existing("RuntimeError"); let ec = Class::from_existing("RuntimeError");
VM::raise(ec, "Something weird happened. Hash seems to be not a Hash"); VM::raise(ec, "Something weird happened. Hash seems to be not a Hash");
return NilClass::new();
}, },
}; };
operate_on_fle_cache!(mut |hm| {
match hm.get_mut(itself.get_data(&*FLE_WRAPPER)) {
Some(mut el) => {
*el.get_header_mut() = entryheader;
},
None => {
VM::raise(Class::from_existing("RuntimeError"),
"Tried to operate on non-existing object");
}
}
NilClass::new().to_any_object()
});
NilClass::new() NilClass::new()
} }
fn r_get_content() -> AnyObject { fn r_get_content() -> AnyObject {
itself.get_data(&*FLE_WRAPPER).get_content().clone().wrap() operate_on_fle_cache!(|hm| {
match hm.get(itself.get_data(&*FLE_WRAPPER)) {
Some(el) => el.get_content().clone().wrap(),
None => NilClass::new().to_any_object()
}
})
} }
fn r_set_content(ctt: RString) -> NilClass { fn r_set_content(ctt: RString) -> NilClass {
@ -315,15 +402,27 @@ pub mod store {
use toml_utils::IntoRuby; use toml_utils::IntoRuby;
use toml::Value; use toml::Value;
let mut content = itself.get_data(&*FLE_WRAPPER).get_content_mut(); let content = match typecheck!(ctt).into_toml() {
Value::String(s) => s,
match typecheck!(ctt).into_toml() {
Value::String(s) => *content = s,
_ => { _ => {
let ec = Class::from_existing("RuntimeError"); let ec = Class::from_existing("RuntimeError");
VM::raise(ec, "Something weird happened. String seems to be not a String"); VM::raise(ec, "Something weird happened. String seems to be not a String");
return NilClass::new();
}, },
} };
operate_on_fle_cache!(mut |hm| {
match hm.get_mut(itself.get_data(&*FLE_WRAPPER)) {
Some(el) => {
*el.get_content_mut() = content;
},
None => {
VM::raise(Class::from_existing("RuntimeError"),
"Tried to operate on non-existing object");
}
}
NilClass::new().to_any_object()
});
NilClass::new() NilClass::new()
} }