Spaces:
Running
Running
Download python/binding.cpp from pgg-engineering/sincekit: direct link, hf CLI and curl.
- Browser
- Download file 6.71 kB
-
https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/python/binding.cpp
- Command line
-
hf download hf://spaces/pgg-engineering/sincekit/python/binding.cpp
-
curl -L -o binding.cpp https://huggingface.co/spaces/pgg-engineering/sincekit/resolve/main/python/binding.cpp
6.71 kB
| namespace { | |
| constexpr const char* capsule_name = "sincekit.Index"; | |
| struct Release { | |
| void operator()(PyObject* obj) const { Py_XDECREF(obj); } | |
| }; | |
| using PyRef = std::unique_ptr<PyObject, Release>; | |
| sincekit::Index* index_from(PyObject* obj) { | |
| return static_cast<sincekit::Index*>(PyCapsule_GetPointer(obj, capsule_name)); | |
| } | |
| void destroy_index(PyObject* capsule) { | |
| delete index_from(capsule); | |
| } | |
| template <typename F> | |
| PyObject* translate(F&& fn) { | |
| try { | |
| return fn(); | |
| } catch (const std::bad_alloc&) { | |
| return PyErr_NoMemory(); | |
| } catch (const std::out_of_range& err) { | |
| PyErr_SetString(PyExc_ValueError, err.what()); | |
| } catch (const std::overflow_error& err) { | |
| PyErr_SetString(PyExc_OverflowError, err.what()); | |
| } catch (const std::exception& err) { | |
| PyErr_SetString(PyExc_RuntimeError, err.what()); | |
| } | |
| return nullptr; | |
| } | |
| bool read_integer(PyObject* obj, std::uint64_t& value) { | |
| if (!PyLong_Check(obj) || PyBool_Check(obj)) { | |
| PyErr_SetString(PyExc_TypeError, "expected an unsigned integer"); | |
| return false; | |
| } | |
| value = PyLong_AsUnsignedLongLong(obj); | |
| return !PyErr_Occurred(); | |
| } | |
| PyObject* create(PyObject*, PyObject* arg) { | |
| std::uint64_t count; | |
| if (!read_integer(arg, count)) return nullptr; | |
| if (count > std::numeric_limits<std::uint32_t>::max()) { | |
| PyErr_SetString(PyExc_OverflowError, "capacity exceeds the uint32 slot limit"); | |
| return nullptr; | |
| } | |
| return translate([&]() -> PyObject* { | |
| auto owner = std::make_unique<sincekit::Index>(static_cast<std::size_t>(count)); | |
| PyObject* capsule = PyCapsule_New(owner.get(), capsule_name, destroy_index); | |
| if (capsule) owner.release(); | |
| return capsule; | |
| }); | |
| } | |
| PyObject* capacity(PyObject*, PyObject* obj) { | |
| auto* index = index_from(obj); | |
| return index ? PyLong_FromSize_t(index->capacity()) : nullptr; | |
| } | |
| PyObject* revision(PyObject*, PyObject* obj) { | |
| auto* index = index_from(obj); | |
| return index ? PyLong_FromUnsignedLongLong(index->revision()) : nullptr; | |
| } | |
| struct Mutation { | |
| std::uint32_t id; | |
| bool deleted; | |
| }; | |
| PyObject* write_many(PyObject*, PyObject* args) { | |
| PyObject* capsule; | |
| PyObject* rows; | |
| if (!PyArg_ParseTuple(args, "OO!:write_many", &capsule, &PyTuple_Type, &rows)) return nullptr; | |
| auto* index = index_from(capsule); | |
| if (!index) return nullptr; | |
| return translate([&]() -> PyObject* { | |
| const auto n = PyTuple_GET_SIZE(rows); | |
| std::vector<Mutation> changes; | |
| changes.reserve(static_cast<std::size_t>(n)); | |
| for (Py_ssize_t i = 0; i < n; ++i) { | |
| PyObject* row = PyTuple_GET_ITEM(rows, i); | |
| if ((!PyTuple_Check(row) && !PyList_Check(row)) || PySequence_Fast_GET_SIZE(row) != 2) { | |
| PyErr_SetString(PyExc_TypeError, "each change must be a (slot_id, deleted_bool) pair"); | |
| return nullptr; | |
| } | |
| std::uint64_t id; | |
| if (!read_integer(PySequence_Fast_GET_ITEM(row, 0), id)) return nullptr; | |
| if (id >= index->capacity()) { | |
| PyErr_SetString(PyExc_ValueError, "slot outside capacity"); | |
| return nullptr; | |
| } | |
| PyObject* deleted = PySequence_Fast_GET_ITEM(row, 1); | |
| if (!PyBool_Check(deleted)) { | |
| PyErr_SetString(PyExc_TypeError, "deleted flag must be True or False"); | |
| return nullptr; | |
| } | |
| changes.push_back({static_cast<std::uint32_t>(id), deleted == Py_True}); | |
| } | |
| if (changes.size() > std::numeric_limits<std::uint64_t>::max() - index->revision()) { | |
| PyErr_SetString(PyExc_OverflowError, "revision exhausted"); | |
| return nullptr; | |
| } | |
| // Preallocate the return value. From here the GIL stays held, no | |
| // callbacks run, and each validated native publication is infallible. | |
| PyRef result(PyLong_FromUnsignedLongLong(index->revision() + changes.size())); | |
| if (!result) return nullptr; | |
| for (const auto& change : changes) { | |
| if (change.deleted) index->erase(change.id); | |
| else index->touch(change.id); | |
| } | |
| return result.release(); | |
| }); | |
| } | |
| PyObject* plan_result(const sincekit::BatchPlan& plan) { | |
| // Each count fits Py_ssize_t: readers came from a Python tuple and on | |
| // 32-bit builds a native row vector is limited by its element size. | |
| PyRef rows(PyTuple_New(static_cast<Py_ssize_t>(plan.rows.size()))); | |
| if (!rows) return nullptr; | |
| Py_ssize_t i = 0; | |
| for (const auto& row : plan.rows) { | |
| PyObject* item = Py_BuildValue("(IKO)", static_cast<unsigned int>(row.id), | |
| static_cast<unsigned long long>(row.revision), row.deleted ? Py_True : Py_False); | |
| if (!item) return nullptr; | |
| PyTuple_SET_ITEM(rows.get(), i++, item); | |
| } | |
| PyRef lengths(PyTuple_New(static_cast<Py_ssize_t>(plan.prefix_lengths.size()))); | |
| if (!lengths) return nullptr; | |
| i = 0; | |
| for (auto length : plan.prefix_lengths) { | |
| PyObject* item = PyLong_FromSize_t(length); | |
| if (!item) return nullptr; | |
| PyTuple_SET_ITEM(lengths.get(), i++, item); | |
| } | |
| return Py_BuildValue("(KOO)", static_cast<unsigned long long>(plan.revision), rows.get(), lengths.get()); | |
| } | |
| PyObject* since_many(PyObject*, PyObject* args) { | |
| PyObject* capsule; | |
| PyObject* readers; | |
| if (!PyArg_ParseTuple(args, "OO!:since_many", &capsule, &PyTuple_Type, &readers)) return nullptr; | |
| auto* index = index_from(capsule); | |
| if (!index) return nullptr; | |
| return translate([&]() -> PyObject* { | |
| std::vector<std::uint64_t> cursors; | |
| const auto n = PyTuple_GET_SIZE(readers); | |
| cursors.reserve(static_cast<std::size_t>(n)); | |
| for (Py_ssize_t i = 0; i < n; ++i) { | |
| std::uint64_t cursor; | |
| if (!read_integer(PyTuple_GET_ITEM(readers, i), cursor)) return nullptr; | |
| cursors.push_back(cursor); | |
| } | |
| return plan_result(sincekit::since_many(*index, cursors)); | |
| }); | |
| } | |
| PyMethodDef methods[] = { | |
| {"create", create, METH_O, nullptr}, | |
| {"capacity", capacity, METH_O, nullptr}, | |
| {"revision", revision, METH_O, nullptr}, | |
| {"write_many", write_many, METH_VARARGS, nullptr}, | |
| {"since_many", since_many, METH_VARARGS, nullptr}, | |
| {nullptr, nullptr, 0, nullptr} | |
| }; | |
| PyModuleDef module = { | |
| PyModuleDef_HEAD_INIT, "_sincekit", "Native SinceKit index", -1, methods, | |
| nullptr, nullptr, nullptr, nullptr | |
| }; | |
| } // namespace | |
| PyMODINIT_FUNC PyInit__sincekit() { return PyModule_Create(&module); } | |