Skip to content

Commit

Permalink
Wrap Env and Cache using Arc
Browse files Browse the repository at this point in the history
  • Loading branch information
acrrd committed Apr 8, 2021
1 parent f15b437 commit b173321
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/db_options.rs
Expand Up @@ -15,7 +15,7 @@
use std::ffi::{CStr, CString};
use std::mem;
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;

use libc::{self, c_char, c_int, c_uchar, c_uint, c_void, size_t};

Expand Down Expand Up @@ -48,7 +48,7 @@ impl Drop for CacheWrapper {
}
}

pub struct Cache(pub(crate) Rc<CacheWrapper>);
pub struct Cache(pub(crate) Arc<CacheWrapper>);

impl Cache {
/// Create a lru cache with capacity
Expand All @@ -57,7 +57,7 @@ impl Cache {
if cache.is_null() {
Err(Error::new("Could not create Cache".to_owned()))
} else {
Ok(Cache(Rc::new(CacheWrapper { inner: cache })))
Ok(Cache(Arc::new(CacheWrapper { inner: cache })))
}
}

Expand Down Expand Up @@ -93,6 +93,8 @@ impl Cache {
///
/// Note: currently, C API behinds C++ API for various settings.
/// See also: `rocksdb/include/env.h`
pub struct Env(Arc<EnvWrapper>);

struct EnvWrapper {
inner: *mut ffi::rocksdb_env_t,
}
Expand All @@ -105,16 +107,14 @@ impl Drop for EnvWrapper {
}
}

pub struct Env(Rc<EnvWrapper>);

impl Env {
/// Returns default env
pub fn default() -> Result<Env, Error> {
let env = unsafe { ffi::rocksdb_create_default_env() };
if env.is_null() {
Err(Error::new("Could not create mem env".to_owned()))
} else {
Ok(Env(Rc::new(EnvWrapper { inner: env })))
Ok(Env(Arc::new(EnvWrapper { inner: env })))
}
}

Expand All @@ -125,7 +125,7 @@ impl Env {
if env.is_null() {
Err(Error::new("Could not create mem env".to_owned()))
} else {
Ok(Env(Rc::new(EnvWrapper { inner: env })))
Ok(Env(Arc::new(EnvWrapper { inner: env })))
}
}

Expand Down

0 comments on commit b173321

Please sign in to comment.