Skip to content

Commit

Permalink
(fix):implemented missing aggregate + update version of pyo3, + some …
Browse files Browse the repository at this point in the history
…adaptation changes for the new version
  • Loading branch information
Senhaji-Rhazi-Hamza committed Nov 28, 2024
1 parent 752be98 commit f1daaef
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
needs: create-dist
strategy:
matrix:
os: [ubuntu-latest, macos-14, macos-13, windows-latest]
os: [ubuntu-latest, macos-14, macos-latest, windows-latest]
# os: [macos-14]
python-version: ["3.9"] # , "3.10"
# python-version: ["3.10"]
Expand All @@ -55,7 +55,7 @@ jobs:
exclude:
- os: windows-latest
architecture: arm64
- os: macos-13
- os: macos-latest
architecture: arm64 # macOS 13 on GitHub Actions is only x86_64 (Intel)
- os: macos-14
architecture: x86_6
Expand Down
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "py-binding-polodb"
version = "0.1.1"
version = "0.1.2"
edition = "2021"

[lib]
Expand All @@ -16,7 +16,7 @@ crate-type = ["cdylib", "rlib"]

polodb_core = "5.1.3"
# polodb_core = { path = "../src/polodb_core", default-features = false }
pyo3 = { version = "0.22.5", features = ["extension-module", "auto-initialize"] }
pyo3 = { version = "0.23.2", features = ["extension-module", "auto-initialize"] }

[profile.release]
lto = "thin"
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 = "polodb-python"
version = "0.1.13"
version = "0.1.14"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.9"
Expand Down
34 changes: 17 additions & 17 deletions src/helper_type_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn delete_result_to_pydict(
py: Python,
delete_result: results::DeleteResult,
) -> PyResult<Py<PyDict>> {
let py_dict = PyDict::new_bound(py);
let py_dict = PyDict::new(py);

// Insert matched_count and modified_count into the PyDict
py_dict.set_item("deleted_count", delete_result.deleted_count as i64)?;
Expand All @@ -109,7 +109,7 @@ pub fn update_result_to_pydict(
py: Python,
update_result: results::UpdateResult,
) -> PyResult<Py<PyDict>> {
let py_dict = PyDict::new_bound(py);
let py_dict = PyDict::new(py);

// Insert matched_count and modified_count into the PyDict
py_dict.set_item("matched_count", update_result.matched_count as i64)?;
Expand All @@ -118,7 +118,7 @@ pub fn update_result_to_pydict(
Ok(py_dict.into())
}
pub fn document_to_pydict(py: Python, doc: Document) -> PyResult<Py<PyDict>> {
let py_dict = PyDict::new_bound(py);
let py_dict = PyDict::new(py);
for (key, value) in doc {
let py_value = bson_to_py_obj(py, &value);
py_dict.set_item(key, py_value)?;
Expand All @@ -129,25 +129,25 @@ pub fn document_to_pydict(py: Python, doc: Document) -> PyResult<Py<PyDict>> {
pub fn bson_to_py_obj(py: Python, bson: &Bson) -> PyObject {
match bson {
Bson::Null => py.None(),
Bson::Int32(i) => i.into_py(py),
Bson::Int64(i) => i.into_py(py),
Bson::Double(f) => PyFloat::new_bound(py, *f).into_py(py),
Bson::String(s) => PyString::new_bound(py, s).into_py(py),
Bson::Boolean(b) => PyBool::new_bound(py, *b).into_py(py),
Bson::Int32(i) => i.into_pyobject(py).unwrap().into(),
Bson::Int64(i) => i.into_pyobject(py).unwrap().into(),
Bson::Double(f) => PyFloat::new(py, *f).into_pyobject(py).unwrap().into(),
Bson::String(s) => PyString::new(py, s).into_pyobject(py).unwrap().into(),
Bson::Boolean(b) => PyBool::new(py, *b).into_py(py),
Bson::Array(arr) => {
// Create an empty PyList without specifying a slice
let py_list = PyList::empty_bound(py); // Use empty method instead of new_bound
let py_list = PyList::empty(py); // Use empty method instead of new
for item in arr {
py_list.append(bson_to_py_obj(py, item)).unwrap();
}
py_list.into_py(py)
py_list.into_pyobject(py).unwrap().into()
}
Bson::Document(doc) => {
let py_dict = PyDict::new_bound(py);
let py_dict = PyDict::new(py);
for (key, value) in doc.iter() {
py_dict.set_item(key, bson_to_py_obj(py, value)).unwrap();
}
py_dict.into_py(py)
py_dict.into_pyobject(py).unwrap().into()
}
Bson::RegularExpression(regex) => {
let re_module = py.import_bound("re").unwrap();
Expand All @@ -157,10 +157,10 @@ pub fn bson_to_py_obj(py: Python, bson: &Bson) -> PyObject {
.to_object(py)
}
// Handle JavaScript code
Bson::JavaScriptCode(code) => PyString::new_bound(py, code).into_py(py),
Bson::Timestamp(ts) => (ts.time, ts.increment).into_py(py),
Bson::Binary(bin) => PyBytes::new_bound(py, &bin.bytes).into_py(py),
Bson::ObjectId(oid) => PyString::new_bound(py, &oid.to_hex()).into_py(py),
Bson::JavaScriptCode(code) => PyString::new(py, code).into_pyobject(py).unwrap().into(),
Bson::Timestamp(ts) => (ts.time, ts.increment).into_pyobject(py).unwrap().into(),
Bson::Binary(bin) => PyBytes::new(py, &bin.bytes).into_pyobject(py).unwrap().into(),
Bson::ObjectId(oid) => PyString::new(py, &oid.to_hex()).into_pyobject(py).unwrap().into(),
Bson::DateTime(dt) => {
let timestamp = dt.timestamp_millis() / 1000;
let datetime = py
Expand All @@ -170,7 +170,7 @@ pub fn bson_to_py_obj(py: Python, bson: &Bson) -> PyObject {
.unwrap();
datetime.call1((timestamp,)).unwrap().to_object(py)
}
Bson::Symbol(s) => PyString::new_bound(py, s).into_py(py),
Bson::Symbol(s) => PyString::new(py, s).into_pyobject(py).unwrap().into(),

// Handle undefined value (deprecated)
Bson::Undefined => py.None(),
Expand Down
31 changes: 26 additions & 5 deletions src/py_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl PyCollection {
match self.inner.insert_many(bson_vec_docs) {
Ok(result) => {
// Create a Python object from the Rust result and return it
let dict: Bound<'_, PyDict> = PyDict::new_bound(py);
let dict: Bound<'_, PyDict> = PyDict::new(py);

for (key, value) in &result.inserted_ids {
dict.set_item(key, bson_to_py_obj(py, value)).unwrap();
Expand Down Expand Up @@ -64,7 +64,7 @@ impl PyCollection {
Ok(result) => {
// Create a Python object from the Rust result and return it
let py_inserted_id = bson_to_py_obj(py, &result.inserted_id);
let dict = PyDict::new_bound(py);
let dict = PyDict::new(py);
let dict_ref = dict.borrow();
dict_ref.set_item("inserted_id", py_inserted_id)?;
Ok(dict.to_object(py))
Expand Down Expand Up @@ -137,7 +137,6 @@ impl PyCollection {
let filter_doc = convert_py_obj_to_document(filter.to_object(py).as_any())?;
let update_doc = convert_py_obj_to_document(update.to_object(py).as_any())?;

// Call the Rust method `find_one`
match self.inner.update_one_with_options(
filter_doc,
update_doc,
Expand All @@ -155,6 +154,29 @@ impl PyCollection {
}
}

fn aggregate(&self, pipeline: Py<PyList>) -> PyResult<PyObject> {
Python::with_gil(|py| {

let bson_vec_pipeline: Vec<Document> =
convert_py_list_to_vec_document(pipeline.to_object(py).as_any());
match self.inner.aggregate(bson_vec_pipeline).run() {
Ok(result) => {
let vec_result = result.collect::<Result<Vec<Document>, _>>().unwrap();

let py_result: Vec<Py<PyDict>> = vec_result
.into_iter()
.map(|x| document_to_pydict(py, x).unwrap())
.collect();
Ok(py_result.to_object(py))
}
Err(e) => {
// Raise a Python exception on error
Err(PyRuntimeError::new_err(format!("Aggregate error: {}", e)))
}
}
})
}

pub fn upsert_many(
&self,
py: Python,
Expand Down Expand Up @@ -211,7 +233,6 @@ impl PyCollection {

pub fn delete_many(&self, filter: Py<PyDict>) -> PyResult<PyObject> {
// Acquire the Python GIL (Global Interpreter Lock)
// let filter_doc = convert_py_obj_to_document(filter.to_object(py).as_any())?;
Python::with_gil(|py| {
let bson_doc: Document = match convert_py_obj_to_document(filter.to_object(py).as_any())
{
Expand Down Expand Up @@ -239,7 +260,7 @@ impl PyCollection {
// Acquire the Python GIL (Global Interpreter Lock)
Python::with_gil(|py| {
match self.inner.count_documents() {
Ok(result) => Ok(result.into_py(py)),
Ok(result) => Ok(result.into_pyobject(py).unwrap().into()),
Err(e) => {
// Raise a Python exception on error
Err(PyRuntimeError::new_err(format!(
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f1daaef

Please sign in to comment.