Repository navigation
Expand file tree
/
Copy pathpy_database.cpp
More file actions
130 lines (120 loc) · 5.9 KB
/
Copy pathpy_database.cpp
File metadata and controls
130 lines (120 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "include/py_database.h"
#include <memory>
#include <mutex>
#include <optional>
#include "common/exception/runtime.h"
#include "extension/extension.h"
#include "include/cached_import/py_cached_import.h"
#include "main/version.h"
#include "pandas/pandas_scan.h"
using namespace lbug::common;
void PyDatabase::initialize(py::handle& m) {
py::class_<PyDatabase>(m, "Database")
.def(py::init<const std::string&, std::optional<uint64_t>, std::optional<uint64_t>,
std::optional<bool>, std::optional<bool>, std::optional<uint64_t>,
std::optional<bool>, std::optional<int64_t>, std::optional<bool>,
std::optional<bool>, std::optional<bool>>(),
py::arg("database_path"), py::arg("buffer_pool_size") = py::none(),
py::arg("max_num_threads") = py::none(), py::arg("compression") = py::none(),
py::arg("read_only") = py::none(), py::arg("max_db_size") = py::none(),
py::arg("auto_checkpoint") = py::none(), py::arg("checkpoint_threshold") = py::none(),
py::arg("throw_on_wal_replay_failure") = py::none(),
py::arg("enable_checksums") = py::none(),
py::arg("enable_multi_writes") = py::none())
.def("scan_node_table_as_int64", &PyDatabase::scanNodeTable<std::int64_t>,
py::arg("table_name"), py::arg("prop_name"), py::arg("indices"), py::arg("np_array"),
py::arg("num_threads"))
.def("scan_node_table_as_int32", &PyDatabase::scanNodeTable<std::int32_t>,
py::arg("table_name"), py::arg("prop_name"), py::arg("indices"), py::arg("np_array"),
py::arg("num_threads"))
.def("scan_node_table_as_int16", &PyDatabase::scanNodeTable<std::int16_t>,
py::arg("table_name"), py::arg("prop_name"), py::arg("indices"), py::arg("np_array"),
py::arg("num_threads"))
.def("scan_node_table_as_double", &PyDatabase::scanNodeTable<double>, py::arg("table_name"),
py::arg("prop_name"), py::arg("indices"), py::arg("np_array"), py::arg("num_threads"))
.def("scan_node_table_as_float", &PyDatabase::scanNodeTable<float>, py::arg("table_name"),
py::arg("prop_name"), py::arg("indices"), py::arg("np_array"), py::arg("num_threads"))
.def("scan_node_table_as_bool", &PyDatabase::scanNodeTable<bool>, py::arg("table_name"),
py::arg("prop_name"), py::arg("indices"), py::arg("np_array"), py::arg("num_threads"))
.def("close", &PyDatabase::close)
.def_static("get_version", &PyDatabase::getVersion)
.def_static("get_storage_version", &PyDatabase::getStorageVersion);
}
py::str PyDatabase::getVersion() {
return py::str(Version::getVersion());
}
uint64_t PyDatabase::getStorageVersion() {
return Version::getStorageVersion();
}
PyDatabase::PyDatabase(const std::string& databasePath, std::optional<uint64_t> bufferPoolSize,
std::optional<uint64_t> maxNumThreads, std::optional<bool> compression,
std::optional<bool> readOnly, std::optional<uint64_t> maxDBSize,
std::optional<bool> autoCheckpoint, std::optional<int64_t> checkpointThreshold,
std::optional<bool> throwOnWalReplayFailure, std::optional<bool> enableChecksums,
std::optional<bool> enableMultiWrites) {
// Start from the engine defaults and only apply explicitly set settings,
// so engine-side default changes propagate automatically.
auto systemConfig = SystemConfig();
if (bufferPoolSize.has_value()) {
systemConfig.bufferPoolSize = bufferPoolSize.value();
}
if (maxNumThreads.has_value()) {
systemConfig.maxNumThreads = maxNumThreads.value();
}
if (compression.has_value()) {
systemConfig.enableCompression = compression.value();
}
if (readOnly.has_value()) {
systemConfig.readOnly = readOnly.value();
}
if (maxDBSize.has_value()) {
systemConfig.maxDBSize = maxDBSize.value();
}
if (autoCheckpoint.has_value()) {
systemConfig.autoCheckpoint = autoCheckpoint.value();
}
if (checkpointThreshold.has_value() && checkpointThreshold.value() >= 0) {
systemConfig.checkpointThreshold =
static_cast<uint64_t>(checkpointThreshold.value());
}
if (throwOnWalReplayFailure.has_value()) {
systemConfig.throwOnWalReplayFailure = throwOnWalReplayFailure.value();
}
if (enableChecksums.has_value()) {
systemConfig.enableChecksums = enableChecksums.value();
}
if (enableMultiWrites.has_value()) {
systemConfig.enableMultiWrites = enableMultiWrites.value();
}
state = std::make_shared<PyDatabaseState>();
state->database = std::make_unique<Database>(databasePath, systemConfig);
lbug::extension::ExtensionUtils::addTableFunc<lbug::PandasScanFunction>(*state->database);
state->storageDriver = std::make_unique<StorageDriver>(state->database.get());
py::gil_scoped_acquire acquire;
// Taken with the GIL held: nothing inside may call into Python.
static std::mutex importCacheMutex;
std::lock_guard<std::mutex> lock(importCacheMutex);
if (lbug::importCache.get() == nullptr) {
lbug::importCache = std::make_shared<lbug::PythonCachedImport>();
}
}
PyDatabase::~PyDatabase() {
close();
}
void PyDatabase::close() {
state.reset();
}
template<class T>
void PyDatabase::scanNodeTable(const std::string& tableName, const std::string& propName,
const py::array_t<uint64_t>& indices, py::array_t<T>& result, int numThreads) {
auto indices_buffer_info = indices.request(false);
auto indices_buffer = static_cast<uint64_t*>(indices_buffer_info.ptr);
auto nodeOffsets = (offset_t*)indices_buffer;
auto result_buffer_info = result.request();
auto result_buffer = (uint8_t*)result_buffer_info.ptr;
auto size = indices.size();
if (state == nullptr) {
throw RuntimeException("Database is closed.");
}
state->storage().scan(tableName, propName, nodeOffsets, size, result_buffer, numThreads);
}