Skip to content

make Manager consumable #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ backtrace = ["failure/backtrace", "failure/std"]
[dependencies]
arrayref = "0.3"
bincode = "0.9"
lazy_static = "1.0"
lmdb = "0.7"
ordered-float = "0.5"
uuid = "0.5"
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#[macro_use] extern crate arrayref;
#[macro_use] extern crate failure;
#[macro_use] extern crate lazy_static;

extern crate bincode;
extern crate lmdb;
Expand Down Expand Up @@ -48,7 +49,7 @@ pub use integer::{
};

pub use manager::{
Manager,
Manager
};

pub use readwrite::{
Expand Down
23 changes: 15 additions & 8 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use std::path::{

use std::sync::{
Arc,
Mutex,
RwLock,
};

Expand All @@ -39,31 +38,40 @@ use ::Rkv;

/// A process is only permitted to have one open handle to each database. This manager
/// exists to enforce that constraint: don't open databases directly.
lazy_static! {
static ref MANAGER: RwLock<Manager> = {
RwLock::new(Manager::new())
};
}

pub struct Manager {
stores: Mutex<BTreeMap<PathBuf, Arc<RwLock<Rkv>>>>,
stores: BTreeMap<PathBuf, Arc<RwLock<Rkv>>>,
}

impl Manager {
fn new() -> Manager {
Manager {
stores: Mutex::new(Default::default()),
stores: Default::default(),
}
}

pub fn singleton() -> &'static RwLock<Manager> {
&*MANAGER
}

/// Return the open store at `path`, returning `None` if it has not already been opened.
pub fn get<'p, P>(&self, path: P) -> Result<Option<Arc<RwLock<Rkv>>>, ::std::io::Error>
where P: Into<&'p Path> {
let canonical = path.into().canonicalize()?;
Ok(self.stores.lock().unwrap().get(&canonical).cloned())
Ok(self.stores.get(&canonical).cloned())
}

/// Return the open store at `path`, or create it by calling `f`.
pub fn get_or_create<'p, F, P>(&mut self, path: P, f: F) -> Result<Arc<RwLock<Rkv>>, StoreError>
where F: FnOnce(&Path) -> Result<Rkv, StoreError>,
P: Into<&'p Path> {
let canonical = path.into().canonicalize()?;
let mut map = self.stores.lock().unwrap();
Ok(match map.entry(canonical) {
Ok(match self.stores.entry(canonical) {
Entry::Occupied(e) => e.get().clone(),
Entry::Vacant(e) => {
let k = Arc::new(RwLock::new(f(e.key().as_path())?));
Expand All @@ -78,8 +86,7 @@ impl Manager {
where F: FnOnce(&Path, c_uint) -> Result<Rkv, StoreError>,
P: Into<&'p Path> {
let canonical = path.into().canonicalize()?;
let mut map = self.stores.lock().unwrap();
Ok(match map.entry(canonical) {
Ok(match self.stores.entry(canonical) {
Entry::Occupied(e) => e.get().clone(),
Entry::Vacant(e) => {
let k = Arc::new(RwLock::new(f(e.key().as_path(), capacity)?));
Expand Down
40 changes: 40 additions & 0 deletions tests/manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2018 Mozilla
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

extern crate rkv;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

License block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 272c689.

extern crate tempdir;

use rkv::{
Manager,
Rkv,
};

use self::tempdir::TempDir;

use std::fs;

use std::sync::{
Arc,
};

#[test]
// Identical to the same-named unit test, but this one confirms that it works
// via the public MANAGER singleton.
fn test_same() {
let root = TempDir::new("test_same_singleton").expect("tempdir");
fs::create_dir_all(root.path()).expect("dir created");

let p = root.path();
assert!(Manager::singleton().read().unwrap().get(p).expect("success").is_none());

let created_arc = Manager::singleton().write().unwrap().get_or_create(p, Rkv::new).expect("created");
let fetched_arc = Manager::singleton().read().unwrap().get(p).expect("success").expect("existed");
assert!(Arc::ptr_eq(&created_arc, &fetched_arc));
}