1717
1818#include < pybind11/functional.h>
1919#include < pybind11/pybind11.h>
20+ #include < pybind11/stl.h>
21+
22+ #include < string>
23+ #include < vector>
2024
2125#include " keyvi/dictionary/dictionary_types.h"
2226
2327namespace py = pybind11;
2428namespace kd = keyvi::dictionary;
2529
26- void init_keyvi_dictionary_compilers (const py::module_ &m ) {
30+ void init_keyvi_dictionary_compilers (const py::module_ &module ) {
2731#define CREATE_COMPILER (compiler, name ) \
28- py::class_<compiler>(m , name) \
32+ py::class_<compiler>(module , name) \
2933 .def (py::init<>()) \
34+ .def (py::init<const keyvi::util::parameters_t &>()) \
3035 .def (" __enter__" , [](compiler &c) { return &c; }) \
3136 .def (" __exit__" , [](compiler &c, void *exc_type, void *exc_value, void *traceback) { c.Compile (); }) \
3237 .def (" __setitem__" , &compiler::Add) \
@@ -47,9 +52,67 @@ void init_keyvi_dictionary_compilers(const py::module_ &m) {
4752 }, \
4853 py::arg (" progress_callback" ) = static_cast <std::function<void (const size_t a, const size_t b)> *>(nullptr )) \
4954 .def (" set_manifest" , &compiler::SetManifest) \
50- .def (" write_to_file" , &compiler::WriteToFile);
51-
55+ .def (" write_to_file" , &compiler::WriteToFile, py::call_guard<py::gil_scoped_release>());
56+ #define CREATE_SK_COMPILER (compiler, name ) \
57+ py::class_<compiler>(module , name) \
58+ .def (py::init<const std::vector<std::string> &>()) \
59+ .def (py::init<const std::vector<std::string> &, const keyvi::util::parameters_t &>()) \
60+ .def (" __enter__" , [](compiler &c) { return &c; }) \
61+ .def (" __exit__" , [](compiler &c, void *exc_type, void *exc_value, void *traceback) { c.Compile (); }) \
62+ .def (" __setitem__" , &compiler::Add) \
63+ .def (" add" , &compiler::Add) \
64+ .def ( \
65+ " compile" , \
66+ [](compiler &c, std::function<void (const size_t a, const size_t b)> progress_callback) { \
67+ pybind11::gil_scoped_release release_gil; \
68+ if (progress_callback == nullptr ) { \
69+ c.Compile (); \
70+ return ; \
71+ } \
72+ auto progress_compiler_callback = [](size_t a, size_t b, void *user_data) { \
73+ auto py_callback = *reinterpret_cast <std::function<void (const size_t , const size_t )> *>(user_data); \
74+ pybind11::gil_scoped_acquire acquire_gil; \
75+ py_callback (a, b); \
76+ }; \
77+ void *user_data = reinterpret_cast <void *>(&progress_callback); \
78+ c.Compile (progress_compiler_callback, user_data); \
79+ }, \
80+ py::arg (" progress_callback" ) = static_cast <std::function<void (const size_t a, const size_t b)> *>(nullptr )) \
81+ .def (" set_manifest" , &compiler::SetManifest) \
82+ .def (" write_to_file" , &compiler::WriteToFile, py::call_guard<py::gil_scoped_release>());
83+ #define CREATE_MERGER (merger, name ) \
84+ py::class_<merger>(module , name) \
85+ .def (py::init<>()) \
86+ .def (py::init<const keyvi::util::parameters_t &>()) \
87+ .def (" __enter__" , [](merger &m) { return &m; }) \
88+ .def (" __exit__" , [](merger &m, void *exc_type, void *exc_value, void *traceback) { m.Merge (); }) \
89+ .def (" add" , &merger::Add) \
90+ .def (" merge" , \
91+ [](merger &m) { \
92+ pybind11::gil_scoped_release release_gil; \
93+ m.Merge (); \
94+ }) \
95+ .def (" merge" , \
96+ [](merger &m, const std::string &filename) { \
97+ pybind11::gil_scoped_release release_gil; \
98+ m.Merge (filename); \
99+ }) \
100+ .def (" set_manifest" , &merger::SetManifest) \
101+ .def (" write_to_file" , &merger::WriteToFile, py::call_guard<py::gil_scoped_release>());
52102 CREATE_COMPILER (kd::CompletionDictionaryCompiler, " CompletionDictionaryCompiler" );
103+ CREATE_COMPILER (kd::FloatVectorDictionaryCompiler, " FloatVectorDictionaryCompiler" );
104+ CREATE_COMPILER (kd::IntDictionaryCompiler, " IntDictionaryCompiler" );
105+ CREATE_COMPILER (kd::JsonDictionaryCompiler, " JsonDictionaryCompiler" );
106+ CREATE_COMPILER (kd::KeyOnlyDictionaryCompiler, " KeyOnlyDictionaryCompiler" );
107+ CREATE_COMPILER (kd::StringDictionaryCompiler, " StringDictionaryCompiler" );
108+ CREATE_SK_COMPILER (kd::SecondaryKeyCompletionDictionaryCompiler, " SecondaryKeyCompletionDictionaryCompiler" );
109+ CREATE_SK_COMPILER (kd::SecondaryKeyFloatVectorDictionaryCompiler, " SecondaryKeyFloatVectorDictionaryCompiler" );
110+ CREATE_SK_COMPILER (kd::SecondaryKeyIntDictionaryCompiler, " SecondaryKeyIntDictionaryCompiler" );
111+ CREATE_SK_COMPILER (kd::SecondaryKeyJsonDictionaryCompiler, " SecondaryKeyJsonDictionaryCompiler" );
112+ CREATE_SK_COMPILER (kd::SecondaryKeyKeyOnlyDictionaryCompiler, " SecondaryKeyKeyOnlyDictionaryCompiler" );
113+ CREATE_SK_COMPILER (kd::SecondaryKeyStringDictionaryCompiler, " SecondaryKeyStringDictionaryCompiler" );
114+ CREATE_MERGER (kd::CompletionDictionaryMerger, " CompletionDictionaryMerger" );
115+ CREATE_MERGER (kd::IntDictionaryMerger, " IntDictionaryMerger" );
53116
54117#undef CREATE_COMPILER
55118}
0 commit comments