1+ /* keyvi - A key value store.
2+ *
3+ * Copyright 2024 Hendrik Muhs<hendrik.muhs@gmail.com>
4+ *
5+ * Licensed under the Apache License, Version 2.0 (the "License");
6+ * you may not use this file except in compliance with the License.
7+ * You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+
18+ #include < pybind11/pybind11.h>
19+ #include < pybind11/functional.h>
20+
21+ #include " keyvi/dictionary/dictionary_types.h"
22+
23+ namespace py = pybind11;
24+ namespace kd = keyvi::dictionary;
25+
26+ void init_keyvi_dictionary_compilers (const py::module_ &m) {
27+ #define CREATE_COMPILER (compiler, name ) \
28+ py::class_<compiler>(m, name) \
29+ .def (py::init<>()) \
30+ .def (" __enter__" , [](compiler &c) { return &c; }) \
31+ .def (" __exit__" , [](compiler &c, void *exc_type, void *exc_value, void *traceback) { c.Compile (); }) \
32+ .def (" __setitem__" , &compiler::Add) \
33+ .def (" add" , &compiler::Add) \
34+ .def (" compile" , [](compiler &c, std::function<void (const size_t a, const size_t b)> progress_callback) { \
35+ if (progress_callback == nullptr ) { \
36+ c.Compile (); \
37+ return ; \
38+ } \
39+ auto progress_compiler_callback = [](size_t a, size_t b, void *user_data) { \
40+ auto py_callback = *reinterpret_cast <std::function<void (const size_t , const size_t )>*>(user_data); \
41+ py_callback (a,b); \
42+ }; \
43+ void * user_data = reinterpret_cast <void *>(&progress_callback); \
44+ c.Compile (progress_compiler_callback, user_data); \
45+ }, py::arg (" progress_callback" ) = static_cast <std::function<void (const size_t a, const size_t b)> *>(nullptr )) \
46+ .def (" set_manifest" , &compiler::SetManifest) \
47+ .def (" write_to_file" , &compiler::WriteToFile);
48+
49+ CREATE_COMPILER (kd::CompletionDictionaryCompiler, " CompletionDictionaryCompiler" );
50+
51+ #undef CREATE_COMPILER
52+ }
53+
54+ // cdef void progress_compiler_callback(size_t a, size_t b, void* py_callback) noexcept with gil:
55+ // (<object>py_callback)(a, b)
0 commit comments