From 1cdfd3edf0848462c0756f2e12dc2eaa675f57bc Mon Sep 17 00:00:00 2001 From: Congyu WANG Date: Sat, 30 Mar 2024 13:38:34 +0800 Subject: [PATCH 1/3] update to pyo3 v0.21 --- Cargo.toml | 9 ++++--- src/encoder.rs | 38 ++++++++++++++--------------- src/iter.rs | 10 ++++---- src/lib.rs | 5 ++-- src/options.rs | 24 +++++++++---------- src/rdict.rs | 54 +++++++++++++++++++++++------------------- src/snapshot.rs | 8 +++---- src/sst_file_writer.rs | 6 ++--- src/write_batch.rs | 20 +++++++++------- 9 files changed, 91 insertions(+), 83 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3a8921e..fd0ff3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,15 +13,14 @@ crate-type = ["cdylib"] [dependencies] rocksdb = { git = "https://github.com/Congyuwang/rust-rocksdb", tag = "v0.22.0+8.10.0" } librocksdb-sys = { git = "https://github.com/Congyuwang/rust-rocksdb", tag = "v0.22.0+8.10.0" } -pyo3-log = "0.9" log = "0.4" serde = { version = "1", features = ["derive"] } -serde_json = "1.0.87" -num-bigint = "^0.4.3" -libc = "0.2.112" +serde_json = "1" +num-bigint = "0.4" +libc = "0.2" [dependencies.pyo3] -version = "0.20" +version = "0.21" features = ["extension-module", "num-bigint"] [profile.release] diff --git a/src/encoder.rs b/src/encoder.rs index 8a7f7d2..3f36047 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -10,7 +10,7 @@ pub(crate) enum ValueTypes<'a, 'b> { Int(BigInt), Float(f64), Bool(bool), - Any(&'b PyAny), + Any(&'a Bound<'b, PyAny>), } #[inline(always)] @@ -26,9 +26,9 @@ pub(crate) fn encoding_byte(v_type: &ValueTypes) -> u8 { } #[inline(always)] -pub(crate) fn encode_key(key: &PyAny, raw_mode: bool) -> PyResult> { +pub(crate) fn encode_key<'a>(key: &'a Bound, raw_mode: bool) -> PyResult> { if raw_mode { - return if let Ok(value) = ::try_from(key) { + return if let Ok(value) = key.downcast::() { Ok(Cow::Borrowed(value.as_bytes())) } else { Err(PyKeyError::new_err("raw mode only support bytes")) @@ -65,12 +65,12 @@ pub(crate) fn encode_key(key: &PyAny, raw_mode: bool) -> PyResult> { /// #[inline(always)] pub(crate) fn encode_value<'a>( - value: &'a PyAny, + value: &'a Bound, dumps: &PyObject, raw_mode: bool, ) -> PyResult> { if raw_mode { - if let Ok(value) = ::try_from(value) { + if let Ok(value) = value.downcast::() { Ok(Cow::Borrowed(value.as_bytes())) } else { Err(PyValueError::new_err("raw mode only support bytes")) @@ -101,47 +101,47 @@ pub(crate) fn encode_value<'a>( } #[inline(always)] -fn py_to_value_types(value: &PyAny) -> PyResult { - if let Ok(value) = ::try_from(value) { +fn py_to_value_types<'a, 'b>(value: &'a Bound<'b, PyAny>) -> PyResult> { + if let Ok(value) = value.downcast::() { return Ok(ValueTypes::Bool(value.extract()?)); } - if let Ok(value) = ::try_from(value) { + if let Ok(value) = value.downcast::() { return Ok(ValueTypes::Bytes(value.as_bytes())); } - if let Ok(value) = ::try_from(value) { + if let Ok(value) = value.downcast::() { return Ok(ValueTypes::String(value.to_string())); } - if let Ok(value) = ::try_from(value) { + if let Ok(value) = value.downcast::() { return Ok(ValueTypes::Int(value.extract()?)); } - if let Ok(value) = ::try_from(value) { - return Ok(ValueTypes::Float(value.extract()?)); + if let Ok(value) = value.downcast::() { + return Ok(ValueTypes::Float(value.value())); } Ok(ValueTypes::Any(value)) } /// this function is used for decoding value from bytes #[inline(always)] -pub(crate) fn decode_value( +pub(crate) fn decode_value<'a>( py: Python, - bytes: &[u8], + bytes: &'a [u8], loads: &PyObject, raw_mode: bool, ) -> PyResult { // directly return bytes if raw_mode is true if raw_mode { - return Ok(PyBytes::new(py, bytes).to_object(py)); + return Ok(PyBytes::new_bound(py, bytes).to_object(py)); } match bytes.first() { None => Err(PyException::new_err("Unknown value type")), Some(byte) => match byte { - 1 => Ok(PyBytes::new(py, &bytes[1..]).to_object(py)), + 1 => Ok(PyBytes::new_bound(py, &bytes[1..]).to_object(py)), 2 => { let string = match String::from_utf8(bytes[1..].to_vec()) { Ok(s) => s, Err(_) => return Err(PyException::new_err("utf-8 decoding error")), }; - Ok(string.into_py(py)) + Ok(PyString::new_bound(py, &string).to_object(py)) } 3 => { let big_int = BigInt::from_signed_bytes_be(&bytes[1..]); @@ -151,8 +151,8 @@ pub(crate) fn decode_value( let float: f64 = f64::from_be_bytes(bytes[1..].try_into().unwrap()); Ok(float.into_py(py)) } - 5 => Ok((bytes[1] != 0).to_object(py)), - 6 => loads.call1(py, (PyBytes::new(py, &bytes[1..]),)), + 5 => Ok(PyBool::new_bound(py, bytes[1] != 0).to_object(py)), + 6 => loads.call1(py, (PyBytes::new_bound(py, &bytes[1..]),)), _ => Err(PyException::new_err("Unknown value type")), }, } diff --git a/src/iter.rs b/src/iter.rs index 5db026a..a3783ca 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -191,8 +191,8 @@ impl RdictIter { /// /// del iter, db /// Rdict.destroy(path, Options()) - pub fn seek(&mut self, key: &PyAny) -> PyResult<()> { - let key = encode_key(key, self.raw_mode)?; + pub fn seek(&mut self, key: &Bound) -> PyResult<()> { + let key = encode_key(&key, self.raw_mode)?; unsafe { librocksdb_sys::rocksdb_iter_seek( self.inner, @@ -224,8 +224,8 @@ impl RdictIter { /// /// del iter, db /// Rdict.destroy(path, Options()) - pub fn seek_for_prev(&mut self, key: &PyAny) -> PyResult<()> { - let key = encode_key(key, self.raw_mode)?; + pub fn seek_for_prev(&mut self, key: &Bound) -> PyResult<()> { + let key = encode_key(&key, self.raw_mode)?; unsafe { librocksdb_sys::rocksdb_iter_seek_for_prev( self.inner, @@ -321,7 +321,7 @@ macro_rules! impl_iter { } impl $iter_name { - pub(crate) fn new(inner: RdictIter, backwards: bool, from_key: Option<&PyAny>) -> PyResult { + pub(crate) fn new(inner: RdictIter, backwards: bool, from_key: Option<&Bound>) -> PyResult { let mut inner = inner; if let Some(from_key) = from_key { if backwards { diff --git a/src/lib.rs b/src/lib.rs index 6fd9c2a..0c9f975 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,7 +107,7 @@ use pyo3::prelude::*; /// supports `pickle`. /// #[pymodule] -fn rocksdict(py: Python, m: &PyModule) -> PyResult<()> { +fn rocksdict(py: Python, m: &Bound) -> PyResult<()> { m.add_class::()?; m.add_class::()?; m.add_class::()?; @@ -144,8 +144,7 @@ fn rocksdict(py: Python, m: &PyModule) -> PyResult<()> { m.add_class::()?; m.add_class::()?; - m.add("DbClosedError", py.get_type::())?; + m.add("DbClosedError", py.get_type_bound::())?; - pyo3_log::init(); Ok(()) } diff --git a/src/options.rs b/src/options.rs index 35f60ff..7d9bba2 100644 --- a/src/options.rs +++ b/src/options.rs @@ -614,11 +614,11 @@ impl OptionsPy { let (options, column_families) = OptionsPy::load_latest_inner(path, env, ignore_unknown_options, cache)?; let options = Py::new(py, options)?; - let columns = PyDict::new(py); + let columns = PyDict::new_bound(py); for (name, opt) in column_families { columns.set_item(name, Py::new(py, opt)?)? } - let returned_tuple = PyTuple::new(py, [options.to_object(py), columns.to_object(py)]); + let returned_tuple = PyTuple::new_bound(py, [options.to_object(py), columns.to_object(py)]); Ok(returned_tuple.to_object(py)) } @@ -745,10 +745,10 @@ impl OptionsPy { /// flash_path = DBPath("/flash_path", 10 * 1024 * 1024 * 1024) # 10 GB /// hard_drive = DBPath("/hard_drive", 2 * 1024 * 1024 * 1024 * 1024) # 2 TB /// opt.set_db_paths([flash_path, hard_drive]) - pub fn set_db_paths(&mut self, paths: &PyList) -> PyResult<()> { + pub fn set_db_paths(&mut self, paths: &Bound) -> PyResult<()> { let mut db_paths = Vec::with_capacity(paths.len()); for p in paths.iter() { - let path: &PyCell = PyTryFrom::try_from(p)?; + let path: &Bound = p.downcast()?; db_paths.push( match DBPath::new(&path.borrow().path, path.borrow().target_size) { Ok(p) => p, @@ -805,10 +805,10 @@ impl OptionsPy { /// DBCompressionType.snappy(), /// DBCompressionType.snappy() /// ]) - pub fn set_compression_per_level(&mut self, level_types: &PyList) -> PyResult<()> { + pub fn set_compression_per_level(&mut self, level_types: &Bound) -> PyResult<()> { let mut result = Vec::with_capacity(level_types.len()); for py_any in level_types.iter() { - let level_type: &PyCell = PyTryFrom::try_from(py_any)?; + let level_type: &Bound = py_any.downcast()?; result.push(level_type.borrow().0) } self.inner_opt.set_compression_per_level(&result); @@ -1960,13 +1960,13 @@ impl ReadOptionsPy { } /// Sets the upper bound for an iterator. - pub fn set_iterate_upper_bound(&mut self, key: &PyAny, py: Python) -> PyResult<()> { + pub fn set_iterate_upper_bound(&mut self, key: &Bound, py: Python) -> PyResult<()> { self.iterate_upper_bound = key.to_object(py); Ok(()) } /// Sets the lower bound for an iterator. - pub fn set_iterate_lower_bound(&mut self, key: &PyAny, py: Python) -> PyResult<()> { + pub fn set_iterate_lower_bound(&mut self, key: &Bound, py: Python) -> PyResult<()> { self.iterate_upper_bound = key.to_object(py); Ok(()) } @@ -2075,11 +2075,11 @@ impl ReadOptionsPy { let mut opt = ReadOptions::default(); opt.fill_cache(self.fill_cache); if !self.iterate_lower_bound.is_none(py) { - let lower_bound = encode_key(self.iterate_lower_bound.as_ref(py), raw_mode)?; + let lower_bound = encode_key(self.iterate_lower_bound.bind(py), raw_mode)?; opt.set_iterate_lower_bound(lower_bound); } if !self.iterate_upper_bound.is_none(py) { - let upper_bound = encode_key(self.iterate_upper_bound.as_ref(py), raw_mode)?; + let upper_bound = encode_key(self.iterate_upper_bound.bind(py), raw_mode)?; opt.set_iterate_upper_bound(upper_bound); } opt.set_prefix_same_as_start(self.prefix_same_as_start); @@ -2098,7 +2098,7 @@ impl ReadOptionsPy { pub(crate) fn to_read_opt(&self, raw_mode: bool, py: Python) -> PyResult { let opt = unsafe { ReadOpt(librocksdb_sys::rocksdb_readoptions_create()) }; if !self.iterate_lower_bound.is_none(py) { - let lower_bound = encode_key(self.iterate_lower_bound.as_ref(py), raw_mode)?; + let lower_bound = encode_key(self.iterate_lower_bound.bind(py), raw_mode)?; unsafe { librocksdb_sys::rocksdb_readoptions_set_iterate_lower_bound( @@ -2109,7 +2109,7 @@ impl ReadOptionsPy { } } if !self.iterate_upper_bound.is_none(py) { - let upper_bound = encode_key(self.iterate_upper_bound.as_ref(py), raw_mode)?; + let upper_bound = encode_key(self.iterate_upper_bound.bind(py), raw_mode)?; unsafe { librocksdb_sys::rocksdb_readoptions_set_iterate_upper_bound( diff --git a/src/rdict.rs b/src/rdict.rs index 6f0f30e..aa78b06 100644 --- a/src/rdict.rs +++ b/src/rdict.rs @@ -171,7 +171,7 @@ impl Rdict { access_type: AccessType, py: Python, ) -> PyResult { - let pickle = PyModule::import(py, "pickle")?.to_object(py); + let pickle = PyModule::import_bound(py, "pickle")?.to_object(py); // create db path if missing fs::create_dir_all(path).map_err(|e| PyException::new_err(e.to_string()))?; // load options @@ -332,7 +332,7 @@ impl Rdict { } /// Use list of keys for batch get. - fn __getitem__(&self, key: &PyAny, py: Python) -> PyResult { + fn __getitem__(&self, key: &Bound, py: Python) -> PyResult { match self.get(key, None, None, py) { Ok(Some(v)) => Ok(v), Ok(None) => Err(PyKeyError::new_err(format!("key {key} not found"))), @@ -354,8 +354,8 @@ impl Rdict { #[pyo3(signature = (key, default = None, read_opt = None))] fn get( &self, - key: &PyAny, - default: Option<&PyAny>, + key: &Bound, + default: Option<&Bound>, read_opt: Option<&ReadOptionsPy>, py: Python, ) -> PyResult> { @@ -375,7 +375,7 @@ impl Rdict { } Some(cf) => cf.clone(), }; - if let Ok(keys) = PyTryFrom::try_from(key) { + if let Ok(keys) = key.downcast() { return Ok(Some( get_batch_inner( db, @@ -411,7 +411,7 @@ impl Rdict { } } - fn __setitem__(&self, key: &PyAny, value: &PyAny) -> PyResult<()> { + fn __setitem__(&self, key: &Bound, value: &Bound) -> PyResult<()> { self.put(key, value, None) } @@ -424,7 +424,12 @@ impl Rdict { /// (or use Rdict.set_write_options to preset a write options used by default). #[inline] #[pyo3(signature = (key, value, write_opt = None))] - fn put(&self, key: &PyAny, value: &PyAny, write_opt: Option<&WriteOptionsPy>) -> PyResult<()> { + fn put( + &self, + key: &Bound, + value: &Bound, + write_opt: Option<&WriteOptionsPy>, + ) -> PyResult<()> { let db = self.get_db()?; let key = encode_key(key, self.opt_py.raw_mode)?; let value = encode_value(value, &self.dumps, self.opt_py.raw_mode)?; @@ -441,7 +446,7 @@ impl Rdict { .map_err(|e| PyException::new_err(e.to_string())) } - fn __contains__(&self, key: &PyAny) -> PyResult { + fn __contains__(&self, key: &Bound) -> PyResult { let db = self.get_db()?; let key = encode_key(key, self.opt_py.raw_mode)?; let may_exist = if let Some(cf) = &self.column_family { @@ -501,7 +506,7 @@ impl Rdict { #[pyo3(signature = (key, fetch = false, read_opt = None))] fn key_may_exist( &self, - key: &PyAny, + key: &Bound, fetch: bool, read_opt: Option<&ReadOptionsPy>, py: Python, @@ -540,7 +545,7 @@ impl Rdict { } } - fn __delitem__(&self, key: &PyAny) -> PyResult<()> { + fn __delitem__(&self, key: &Bound) -> PyResult<()> { self.delete(key, None) } @@ -552,7 +557,7 @@ impl Rdict { /// (or use Rdict.set_write_options to preset a write options used by default). #[inline] #[pyo3(signature = (key, write_opt = None))] - fn delete(&self, key: &PyAny, write_opt: Option<&WriteOptionsPy>) -> PyResult<()> { + fn delete(&self, key: &Bound, write_opt: Option<&WriteOptionsPy>) -> PyResult<()> { let db = self.get_db()?; let key = encode_key(key, self.opt_py.raw_mode)?; @@ -646,7 +651,7 @@ impl Rdict { fn items( &self, backwards: bool, - from_key: Option<&PyAny>, + from_key: Option<&Bound>, read_opt: Option<&ReadOptionsPy>, py: Python, ) -> PyResult { @@ -670,7 +675,7 @@ impl Rdict { fn keys( &self, backwards: bool, - from_key: Option<&PyAny>, + from_key: Option<&Bound>, read_opt: Option<&ReadOptionsPy>, py: Python, ) -> PyResult { @@ -694,7 +699,7 @@ impl Rdict { fn values( &self, backwards: bool, - from_key: Option<&PyAny>, + from_key: Option<&Bound>, read_opt: Option<&ReadOptionsPy>, py: Python, ) -> PyResult { @@ -949,8 +954,8 @@ impl Rdict { /// write_opt: WriteOptions pub fn delete_range( &self, - begin: &PyAny, - end: &PyAny, + begin: &Bound, + end: &Bound, write_opt: Option<&WriteOptionsPy>, ) -> PyResult<()> { let db = self.get_db()?; @@ -1033,8 +1038,8 @@ impl Rdict { #[pyo3(signature = (begin, end, compact_opt = Python::with_gil(|py| Py::new(py, CompactOptionsPy::default()).unwrap())))] fn compact_range( &self, - begin: &PyAny, - end: &PyAny, + begin: &Bound, + end: &Bound, compact_opt: Py, py: Python, ) -> PyResult<()> { @@ -1109,7 +1114,7 @@ impl Rdict { let db = self.get_db()?; match db.live_files() { Ok(lfs) => { - let result = PyList::empty(py); + let result = PyList::empty_bound(py); for lf in lfs { result.append(display_live_file_dict( lf, @@ -1168,7 +1173,7 @@ fn display_live_file_dict( pickle_loads: &PyObject, raw_mode: bool, ) -> PyResult { - let result = PyDict::new(py); + let result = PyDict::new_bound(py); let start_key = match lf.start_key { None => py.None(), Some(k) => decode_value(py, &k, pickle_loads, raw_mode)?, @@ -1189,19 +1194,20 @@ fn display_live_file_dict( fn get_batch_inner<'a>( db: &DB, - key_list: &'a PyList, + key_list: &Bound, py: Python<'a>, read_opt: &ReadOptions, loads: &PyObject, cf: &Arc, raw_mode: bool, -) -> PyResult<&'a PyList> { +) -> PyResult> { + let keys_py = key_list.iter().collect::>(); let mut keys: Vec> = Vec::with_capacity(key_list.len()); - for key in key_list { + for key in keys_py.iter() { keys.push(encode_key(key, raw_mode)?); } let values = py.allow_threads(|| db.batched_multi_get_cf_opt(cf, &keys, false, read_opt)); - let result = PyList::empty(py); + let result = PyList::empty_bound(py); for v in values { match v { Ok(value) => match value { diff --git a/src/snapshot.rs b/src/snapshot.rs index 95cceab..a3db549 100644 --- a/src/snapshot.rs +++ b/src/snapshot.rs @@ -86,7 +86,7 @@ impl Snapshot { fn items( &self, backwards: bool, - from_key: Option<&PyAny>, + from_key: Option<&Bound>, read_opt: Option<&ReadOptionsPy>, py: Python, ) -> PyResult { @@ -105,7 +105,7 @@ impl Snapshot { fn keys( &self, backwards: bool, - from_key: Option<&PyAny>, + from_key: Option<&Bound>, read_opt: Option<&ReadOptionsPy>, py: Python, ) -> PyResult { @@ -124,7 +124,7 @@ impl Snapshot { fn values( &self, backwards: bool, - from_key: Option<&PyAny>, + from_key: Option<&Bound>, read_opt: Option<&ReadOptionsPy>, py: Python, ) -> PyResult { @@ -132,7 +132,7 @@ impl Snapshot { } /// read from snapshot - fn __getitem__(&self, key: &PyAny, py: Python) -> PyResult { + fn __getitem__(&self, key: &Bound, py: Python) -> PyResult { let db = self.get_db(); let key = encode_key(key, self.raw_mode)?; let value_result = if let Some(cf) = &self.column_family { diff --git a/src/sst_file_writer.rs b/src/sst_file_writer.rs index 22e2e44..eaacc07 100644 --- a/src/sst_file_writer.rs +++ b/src/sst_file_writer.rs @@ -78,7 +78,7 @@ impl SstFileWriterPy { let raw_mode = options.raw_mode; let options = &options.inner_opt; let writer = Self::create_raw(options, &env_options); - let pickle = PyModule::import(py, "pickle")?.to_object(py); + let pickle = PyModule::import_bound(py, "pickle")?.to_object(py); let pickle_dumps = pickle.getattr(py, "dumps")?; Ok(Self { @@ -112,7 +112,7 @@ impl SstFileWriterPy { /// Adds a Put key with value to currently opened file /// REQUIRES: key is after any previously added key according to comparator. - fn __setitem__(&mut self, key: &PyAny, value: &PyAny) -> PyResult<()> { + fn __setitem__(&mut self, key: &Bound, value: &Bound) -> PyResult<()> { let key = encode_key(key, self.raw_mode)?; let value = encode_value(value, &self.dumps, self.raw_mode)?; self.setitem_raw(&key, &value) @@ -120,7 +120,7 @@ impl SstFileWriterPy { /// Adds a deletion key to currently opened file /// REQUIRES: key is after any previously added key according to comparator. - fn __delitem__(&mut self, key: &PyAny) -> PyResult<()> { + fn __delitem__(&mut self, key: &Bound) -> PyResult<()> { let key = encode_key(key, self.raw_mode)?; self.delitem_raw(&key) } diff --git a/src/write_batch.rs b/src/write_batch.rs index 156332a..81f5303 100644 --- a/src/write_batch.rs +++ b/src/write_batch.rs @@ -33,7 +33,7 @@ impl WriteBatchPy { #[new] #[pyo3(signature = (raw_mode = false))] pub fn default(py: Python, raw_mode: bool) -> PyResult { - let pickle = PyModule::import(py, "pickle")?.to_object(py); + let pickle = PyModule::import_bound(py, "pickle")?.to_object(py); Ok(WriteBatchPy { inner: Some(WriteBatch::default()), default_column_family: None, @@ -51,7 +51,7 @@ impl WriteBatchPy { self.len() } - pub fn __setitem__(&mut self, key: &PyAny, value: &PyAny) -> PyResult<()> { + pub fn __setitem__(&mut self, key: &Bound, value: &Bound) -> PyResult<()> { if let Some(inner) = &mut self.inner { let key = encode_key(key, self.raw_mode)?; let value = encode_value(value, &self.dumps, self.raw_mode)?; @@ -67,7 +67,7 @@ impl WriteBatchPy { } } - pub fn __delitem__(&mut self, key: &PyAny) -> PyResult<()> { + pub fn __delitem__(&mut self, key: &Bound) -> PyResult<()> { if let Some(inner) = &mut self.inner { let key = encode_key(key, self.raw_mode)?; match &self.default_column_family { @@ -142,8 +142,8 @@ impl WriteBatchPy { #[pyo3(signature = (key, value, column_family = None))] pub fn put( &mut self, - key: &PyAny, - value: &PyAny, + key: &Bound, + value: &Bound, column_family: Option, ) -> PyResult<()> { if let Some(inner) = &mut self.inner { @@ -167,7 +167,11 @@ impl WriteBatchPy { /// Args: /// column_family: override the default column family set by set_default_column_family #[pyo3(signature = (key, column_family = None))] - pub fn delete(&mut self, key: &PyAny, column_family: Option) -> PyResult<()> { + pub fn delete( + &mut self, + key: &Bound, + column_family: Option, + ) -> PyResult<()> { if let Some(inner) = &mut self.inner { let key = encode_key(key, self.raw_mode)?; match column_family { @@ -196,8 +200,8 @@ impl WriteBatchPy { #[pyo3(signature = (begin, end, column_family = None))] pub fn delete_range( &mut self, - begin: &PyAny, - end: &PyAny, + begin: &Bound, + end: &Bound, column_family: Option, ) -> PyResult<()> { if let Some(inner) = &mut self.inner { From f6c88fbffb2304c8ea52e29b9afacd395a1d5a10 Mon Sep 17 00:00:00 2001 From: Congyu WANG Date: Sat, 30 Mar 2024 13:39:52 +0800 Subject: [PATCH 2/3] fix clippy --- src/encoder.rs | 4 ++-- src/iter.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/encoder.rs b/src/encoder.rs index 3f36047..2edeee2 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -122,9 +122,9 @@ fn py_to_value_types<'a, 'b>(value: &'a Bound<'b, PyAny>) -> PyResult( +pub(crate) fn decode_value( py: Python, - bytes: &'a [u8], + bytes: &[u8], loads: &PyObject, raw_mode: bool, ) -> PyResult { diff --git a/src/iter.rs b/src/iter.rs index a3783ca..8a943c1 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -192,7 +192,7 @@ impl RdictIter { /// del iter, db /// Rdict.destroy(path, Options()) pub fn seek(&mut self, key: &Bound) -> PyResult<()> { - let key = encode_key(&key, self.raw_mode)?; + let key = encode_key(key, self.raw_mode)?; unsafe { librocksdb_sys::rocksdb_iter_seek( self.inner, @@ -225,7 +225,7 @@ impl RdictIter { /// del iter, db /// Rdict.destroy(path, Options()) pub fn seek_for_prev(&mut self, key: &Bound) -> PyResult<()> { - let key = encode_key(&key, self.raw_mode)?; + let key = encode_key(key, self.raw_mode)?; unsafe { librocksdb_sys::rocksdb_iter_seek_for_prev( self.inner, From c606edf6be46de3642a8867b55ef0822125f5428 Mon Sep 17 00:00:00 2001 From: Congyu WANG Date: Sat, 30 Mar 2024 13:51:02 +0800 Subject: [PATCH 3/3] remove encode_value with_gil --- src/encoder.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/encoder.rs b/src/encoder.rs index 2edeee2..93e4eb4 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -91,9 +91,10 @@ pub(crate) fn encode_value<'a>( concat_type_encoding(type_encoding, if value { &[1u8] } else { &[0u8] }) } ValueTypes::Any(value) => { - let pickle_bytes: Vec = - Python::with_gil(|py| dumps.call1(py, (value,))?.extract(py))?; - concat_type_encoding(type_encoding, &pickle_bytes[..]) + let py = value.py(); + let pickle_bytes = dumps.call1(py, (value,))?; + let bytes: &[u8] = pickle_bytes.downcast_bound::(py)?.as_bytes(); + concat_type_encoding(type_encoding, bytes) } }; Ok(Cow::Owned(owned_bytes))