Skip to content
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

manage immutable Arc<Rkv> rather than interiorly mutable Arc<RwLock<Rkv>> #130

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions examples/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ fn main() {
fs::create_dir_all(root.path()).unwrap();
let p = root.path();

let created_arc = Manager::singleton().write().unwrap().get_or_create(p, Rkv::new).unwrap();
let k = created_arc.read().unwrap();
let k = Manager::singleton().write().unwrap().get_or_create(p, Rkv::new).unwrap();
let store = k.open_single("store", StoreOptions::create()).unwrap();

populate_store(&k, store).unwrap();
Expand Down
3 changes: 1 addition & 2 deletions examples/simple-store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ fn main() {
let p = root.path();

// The manager enforces that each process opens the same lmdb environment at most once
let created_arc = Manager::singleton().write().unwrap().get_or_create(p, Rkv::new).unwrap();
let k = created_arc.read().unwrap();
let k = Manager::singleton().write().unwrap().get_or_create(p, Rkv::new).unwrap();

// Creates a store called "store"
let store = k.open_single("store", StoreOptions::create()).unwrap();
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@
//! // at most once by caching a handle to each environment that it opens.
//! // Use it to retrieve the handle to an opened environment—or create one
//! // if it hasn't already been opened:
//! let created_arc = Manager::singleton().write().unwrap().get_or_create(path, Rkv::new).unwrap();
//! let env = created_arc.read().unwrap();
//! let env = Manager::singleton().write().unwrap().get_or_create(path, Rkv::new).unwrap();
//!
//! // Then you can use the environment handle to get a handle to a datastore:
//! let store: SingleStore = env.open_single("mydb", StoreOptions::create()).unwrap();
Expand Down
12 changes: 6 additions & 6 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ where
}

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

impl Manager {
Expand All @@ -73,7 +73,7 @@ impl Manager {
}

/// Return the open env 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>
pub fn get<'p, P>(&self, path: P) -> Result<Option<Arc<Rkv>>, ::std::io::Error>
where
P: Into<&'p Path>,
{
Expand All @@ -82,7 +82,7 @@ impl Manager {
}

/// Return the open env 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>
pub fn get_or_create<'p, F, P>(&mut self, path: P, f: F) -> Result<Arc<Rkv>, StoreError>
where
F: FnOnce(&Path) -> Result<Rkv, StoreError>,
P: Into<&'p Path>,
Expand All @@ -91,7 +91,7 @@ impl Manager {
Ok(match self.environments.entry(canonical) {
Entry::Occupied(e) => e.get().clone(),
Entry::Vacant(e) => {
let k = Arc::new(RwLock::new(f(e.key().as_path())?));
let k = Arc::new(f(e.key().as_path())?);
e.insert(k).clone()
},
})
Expand All @@ -104,7 +104,7 @@ impl Manager {
path: P,
capacity: c_uint,
f: F,
) -> Result<Arc<RwLock<Rkv>>, StoreError>
) -> Result<Arc<Rkv>, StoreError>
where
F: FnOnce(&Path, c_uint) -> Result<Rkv, StoreError>,
P: Into<&'p Path>,
Expand All @@ -113,7 +113,7 @@ impl Manager {
Ok(match self.environments.entry(canonical) {
Entry::Occupied(e) => e.get().clone(),
Entry::Vacant(e) => {
let k = Arc::new(RwLock::new(f(e.key().as_path(), capacity)?));
let k = Arc::new(f(e.key().as_path(), capacity)?);
e.insert(k).clone()
},
})
Expand Down