Skip to content

Commit 0ed6cce

Browse files
committed
add compiler
1 parent 64b7343 commit 0ed6cce

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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)

python-pybind/src/dictionary/py_dictionary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* * keyvi - A key value store.
1+
/* keyvi - A key value store.
22
*
33
* Copyright 2024 Hendrik Muhs<hendrik.muhs@gmail.com>
44
*

python-pybind/src/py_keyvi.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
namespace py = pybind11;
2424

2525
void init_keyvi_dictionary(const py::module_ &);
26+
void init_keyvi_dictionary_compilers(const py::module_ &);
2627
void init_keyvi_match(const py::module_ &);
2728

2829
PYBIND11_MODULE(keyvi_scikit_core, m) {
@@ -40,6 +41,8 @@ PYBIND11_MODULE(keyvi_scikit_core, m) {
4041
init_keyvi_match(m);
4142
py::module keyvi_dictionary = m.def_submodule("dictionary", "keyvi_scikit_core.dictionary");
4243
init_keyvi_dictionary(keyvi_dictionary);
44+
py::module keyvi_compilers = m.def_submodule("compiler", "keyvi_scikit_core.compiler");
45+
init_keyvi_dictionary_compilers(keyvi_compilers);
4346

4447
#ifdef VERSION_INFO
4548
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);

0 commit comments

Comments
 (0)