| 29 | } |
| 30 | |
| 31 | void ZVecPySchemas::bind_field_schema(py::module_ &m) { |
| 32 | py::class_<FieldSchema, FieldSchema::Ptr>(m, "_FieldSchema") |
| 33 | .def(py::init<const std::string &, DataType, uint32_t, bool, |
| 34 | const IndexParams::Ptr &>(), |
| 35 | py::arg("name"), py::arg("data_type"), py::arg("dimension") = 0, |
| 36 | py::arg("nullable") = false, py::arg("index_param") = nullptr) |
| 37 | .def_property_readonly("name", &FieldSchema::name) |
| 38 | .def_property_readonly("data_type", &FieldSchema::data_type) |
| 39 | .def_property_readonly("nullable", &FieldSchema::nullable) |
| 40 | .def_property_readonly("dimension", &FieldSchema::dimension) |
| 41 | .def_property_readonly("is_dense_vector", &FieldSchema::is_dense_vector) |
| 42 | .def_property_readonly("is_sparse_vector", &FieldSchema::is_sparse_vector) |
| 43 | .def_property_readonly("index_type", |
| 44 | [](const FieldSchema &self) { |
| 45 | return self.index_params() |
| 46 | ? self.index_type() |
| 47 | : IndexType::UNDEFINED; |
| 48 | }) |
| 49 | .def_property_readonly("index_param", |
| 50 | [](const FieldSchema &self) -> py::object { |
| 51 | if (self.index_params()) { |
| 52 | return py::cast(self.index_params()); |
| 53 | } |
| 54 | return py::none(); |
| 55 | }) |
| 56 | .def("__eq__", &FieldSchema::operator==) |
| 57 | .def("__ne__", &FieldSchema::operator!=) |
| 58 | .def(py::pickle( |
| 59 | [](const FieldSchema &self) { |
| 60 | return py::make_tuple(self.name(), self.data_type(), |
| 61 | self.dimension(), self.nullable(), |
| 62 | self.index_params() |
| 63 | ? py::cast(self.index_params()) |
| 64 | : py::none()); |
| 65 | }, |
| 66 | [](py::tuple t) { |
| 67 | if (t.size() != 5) { |
| 68 | throw std::runtime_error( |
| 69 | "Invalid tuple size for FieldSchema pickle"); |
| 70 | } |
| 71 | std::string name = t[0].cast<std::string>(); |
| 72 | DataType dtype = t[1].cast<DataType>(); |
| 73 | uint32_t dim = t[2].cast<uint32_t>(); |
| 74 | bool nullable = t[3].cast<bool>(); |
| 75 | |
| 76 | IndexParams::Ptr idx_params = nullptr; |
| 77 | if (!t[4].is_none()) { |
| 78 | idx_params = t[4].cast<IndexParams::Ptr>(); |
| 79 | } |
| 80 | |
| 81 | return std::make_shared<FieldSchema>(name, dtype, dim, nullable, |
| 82 | idx_params); |
| 83 | })); |
| 84 | } |
| 85 | |
| 86 | void ZVecPySchemas::bind_collection_schema(py::module_ &m) { |
| 87 | py::class_<CollectionSchema, CollectionSchema::Ptr>(m, "_CollectionSchema") |