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

Code refactor db multithreaded #101

Merged
merged 8 commits into from
Dec 5, 2023
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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "RocksDict"
version = "0.3.19"
version = "0.3.20"
edition = "2021"
description = "Rocksdb Python Binding"

Expand All @@ -11,8 +11,8 @@ name = "rocksdict"
crate-type = ["cdylib"]

[dependencies]
rocksdb = { git = "https://github.com/Congyuwang/rust-rocksdb", tag = "v0.21.0+8.8.1" }
librocksdb-sys = { git = "https://github.com/Congyuwang/rust-rocksdb", tag = "v0.21.0+8.8.1" }
rocksdb = { git = "https://github.com/Congyuwang/rust-rocksdb", tag = "v0.23.0+8.8.1" }
librocksdb-sys = { git = "https://github.com/Congyuwang/rust-rocksdb", tag = "v0.23.0+8.8.1" }
pyo3-log = "0.9"
log = "0.4"
serde = { version = "1", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "rocksdict"
version = "0.3.19"
version = "0.3.20"
description = "Rocksdb Python Binding"
long_description_content_type="text/markdown"
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions python/rocksdict/rocksdict.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,10 @@ class Rdict:
def set_write_options(self, write_opt: WriteOptions) -> None: ...
def __contains__(self, key: Union[str, int, float, bytes, bool]) -> bool: ...
def __delitem__(self, key: Union[str, int, float, bytes, bool]) -> None: ...
def __getitem__(self, key: Union[str, int, float, bytes, bool, List[str], List[int], List[float], List[bytes], List[bool]]) -> Any: ...
def __getitem__(self, key: Union[str, int, float, bytes, bool, List[Union[str, int, float, bytes, bool]]]) -> Any | None: ...
def __setitem__(self, key: Union[str, int, float, bytes, bool], value: Any) -> None: ...
def get(self,
key: Union[str, int, float, bytes, bool, List[str], List[int], List[float], List[bytes], List[bool]],
key: Union[str, int, float, bytes, bool, List[Union[str, int, float, bytes, bool]]],
default: Any = None,
read_opt: Union[ReadOptions, None] = None) -> Any | None: ...
def put(self,
Expand Down
11 changes: 5 additions & 6 deletions src/db_reference.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use rocksdb::DB;
use std::cell::RefCell;
use rocksdb::{DBWithThreadMode, MultiThreaded};
use std::sync::Arc;

/// The type of a reference to a [rocksdb::DB] that is passed around the library.
pub(crate) type DbReference = Arc<RefCell<DB>>;
pub(crate) type DbReference = Arc<DBWithThreadMode<MultiThreaded>>;

/// A wrapper around [DbReference] that cancels all background work when dropped.
///
Expand All @@ -15,9 +14,9 @@ pub(crate) struct DbReferenceHolder {
}

impl DbReferenceHolder {
pub fn new(db: DB) -> Self {
pub fn new(db: DBWithThreadMode<MultiThreaded>) -> Self {
Self {
inner: Some(Arc::new(RefCell::new(db))),
inner: Some(Arc::new(db)),
}
}

Expand All @@ -27,7 +26,7 @@ impl DbReferenceHolder {

pub fn close(&mut self) {
if let Some(db) = self.inner.take().and_then(Arc::into_inner) {
db.borrow_mut().cancel_all_background_work(true);
db.cancel_all_background_work(true);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ pub(crate) fn encode_value<'a>(
value: &'a PyAny,
dumps: &PyObject,
raw_mode: bool,
py: Python,
) -> PyResult<Cow<'a, [u8]>> {
if raw_mode {
if let Ok(value) = <PyBytes as PyTryFrom>::try_from(value) {
Expand All @@ -92,7 +91,8 @@ pub(crate) fn encode_value<'a>(
concat_type_encoding(type_encoding, if value { &[1u8] } else { &[0u8] })
}
ValueTypes::Any(value) => {
let pickle_bytes: Vec<u8> = dumps.call1(py, (value,))?.extract(py)?;
let pickle_bytes: Vec<u8> =
Python::with_gil(|py| dumps.call1(py, (value,))?.extract(py))?;
concat_type_encoding(type_encoding, &pickle_bytes[..])
}
};
Expand Down
12 changes: 6 additions & 6 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::slice;
use libc::{c_char, c_uchar, size_t};
use pyo3::exceptions::PyException;
use pyo3::prelude::*;
use rocksdb::{AsColumnFamilyRef, ColumnFamily};
use rocksdb::{AsColumnFamilyRef, UnboundColumnFamily};
use std::ptr::null_mut;
use std::sync::Arc;

Expand Down Expand Up @@ -51,7 +51,7 @@ pub(crate) struct RdictValues {
impl RdictIter {
pub(crate) fn new(
db: &DbReferenceHolder,
cf: &Option<Arc<ColumnFamily>>,
cf: &Option<Arc<UnboundColumnFamily>>,
readopts: ReadOptionsPy,
pickle_loads: &PyObject,
raw_mode: bool,
Expand All @@ -62,7 +62,6 @@ impl RdictIter {
let db_inner = db
.get()
.ok_or_else(|| DbClosedError::new_err("DB instance already closed"))?
.borrow()
.inner();

Ok(RdictIter {
Expand Down Expand Up @@ -90,6 +89,7 @@ impl RdictIter {
/// To check whether the iterator encountered an error after `valid` has
/// returned `false`, use the [`status`](DBRawIteratorWithThreadMode::status) method. `status` will never
/// return an error when `valid` is `true`.
#[inline]
pub fn valid(&self) -> bool {
unsafe { librocksdb_sys::rocksdb_iter_valid(self.inner) != 0 }
}
Expand Down Expand Up @@ -305,15 +305,15 @@ macro_rules! impl_iter {
slf
}

fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<PyObject>> {
fn __next__(mut slf: PyRefMut<Self>, py: Python) -> PyResult<Option<PyObject>> {
if slf.inner.valid() {
$(let $field = Python::with_gil(|py| slf.inner.$field(py))?;)*
$(let $field = slf.inner.$field(py)?;)*
if slf.backwards {
slf.inner.prev();
} else {
slf.inner.next();
}
Ok(Some(Python::with_gil(|py| ($($field),*).to_object(py))))
Ok(Some(($($field),*).to_object(py)))
} else {
Ok(None)
}
Expand Down
1 change: 1 addition & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ pub(crate) struct BottommostLevelCompactionPy(BottommostLevelCompaction);
pub(crate) struct CompactOptionsPy(pub(crate) CompactOptions);

unsafe impl Send for CompactOptionsPy {}
unsafe impl Sync for CompactOptionsPy {}

impl OptionsPy {
/// function that sets prefix extractor according to slice transform type
Expand Down
Loading
Loading