You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by GitBox <gi...@apache.org> on 2022/08/16 02:23:14 UTC

[GitHub] [datasketches-cpp] AlexanderSaydakov commented on a diff in pull request #297: Add python serde support for varopt sketches

AlexanderSaydakov commented on code in PR #297:
URL: https://github.com/apache/datasketches-cpp/pull/297#discussion_r946279362


##########
python/src/py_serde.cpp:
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <cstring>
+#include "memory_operations.hpp"
+
+#include "py_serde.hpp"
+
+#include <pybind11/pybind11.h>
+
+namespace py = pybind11;

Review Comment:
   pybind11 prefix is also used below



##########
python/src/py_serde.cpp:
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <cstring>
+#include "memory_operations.hpp"
+
+#include "py_serde.hpp"
+
+#include <pybind11/pybind11.h>
+
+namespace py = pybind11;
+
+void init_serde(py::module& m) {
+  py::class_<datasketches::py_object_serde, datasketches::PyObjectSerDe /* <--- trampoline*/>(m, "PyObjectSerDe")
+    .def(py::init<>())
+    .def("get_size", &datasketches::py_object_serde::get_size, py::arg("item"),
+        "Returns the size in bytes of an item")
+    .def("to_bytes", &datasketches::py_object_serde::to_bytes, py::arg("item"),
+        "Retuns a bytes object with a serialized version of an item")
+    .def("from_bytes", &datasketches::py_object_serde::from_bytes, py::arg("data"), py::arg("offset"),
+        "Reads a bytes object starting from the given offest and returns a tuple of the reconstructed "
+        "object and the number of additional bytes read")
+    ;
+}    
+
+namespace datasketches {
+  size_t py_object_serde::size_of_item(const py::object& item) const {
+    return get_size(item);
+  }
+
+  size_t py_object_serde::serialize(void* ptr, size_t capacity, const py::object* items, unsigned num) const {
+    size_t bytes_written = 0;
+    pybind11::gil_scoped_acquire acquire;
+    for (unsigned i = 0; i < num; ++i) {
+      std::string bytes = to_bytes(items[i]); // implicit cast from py::bytes
+      check_memory_size(bytes_written + bytes.size(), capacity);
+      memcpy(ptr, bytes.c_str(), bytes.size());
+      ptr = static_cast<char*>(ptr) + bytes.size();
+      bytes_written += bytes.size();
+    }
+    pybind11::gil_scoped_release release;
+    return bytes_written;
+  }
+
+  size_t py_object_serde::deserialize(const void* ptr, size_t capacity, py::object* items, unsigned num) const {
+    size_t bytes_read = 0;
+    unsigned i = 0;
+    bool failure = false;
+    bool error_from_python = false;
+    pybind11::gil_scoped_acquire acquire;
+
+    // copy data into bytes only once
+    py::bytes bytes(static_cast<const char*>(ptr), capacity);
+    for (; i < num && !failure; ++i) {
+      py::tuple bytes_and_len;
+      try {
+        bytes_and_len = from_bytes(bytes, bytes_read);
+      } catch (py::error_already_set &e) {
+        failure = true;
+        error_from_python = true;
+        break;
+      }
+
+      // TODO: check bytes_and_len size is exactly 2?

Review Comment:
   are you going to remove this? I am not sure the check is necessary.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org