Repository navigation
Expand file tree
/
Copy pathnode_query_result.cpp
More file actions
361 lines (322 loc) · 13.6 KB
/
Copy pathnode_query_result.cpp
File metadata and controls
361 lines (322 loc) · 13.6 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "include/node_query_result.h"
#include <stdexcept>
#include <thread>
#include "include/node_util.h"
#include "main/lbug.h"
#include "main/query_result/arrow_query_result.h"
using namespace lbug::main;
Napi::FunctionReference NodeQueryResult::constructor;
Napi::Object NodeQueryResult::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function t = DefineClass(env, "NodeQueryResult",
{InstanceMethod("resetIterator", &NodeQueryResult::ResetIterator),
InstanceMethod("hasNext", &NodeQueryResult::HasNext),
InstanceMethod("hasNextQueryResult", &NodeQueryResult::HasNextQueryResult),
InstanceMethod("getNextQueryResultAsync", &NodeQueryResult::GetNextQueryResultAsync),
InstanceMethod("getNextQueryResultSync", &NodeQueryResult::GetNextQueryResultSync),
InstanceMethod("getNumTuples", &NodeQueryResult::GetNumTuples),
InstanceMethod("getNextSync", &NodeQueryResult::GetNextSync),
InstanceMethod("getNextAsync", &NodeQueryResult::GetNextAsync),
InstanceMethod("getColumnDataTypesAsync", &NodeQueryResult::GetColumnDataTypesAsync),
InstanceMethod("getColumnDataTypesSync", &NodeQueryResult::GetColumnDataTypesSync),
InstanceMethod("getColumnNamesAsync", &NodeQueryResult::GetColumnNamesAsync),
InstanceMethod("getColumnNamesSync", &NodeQueryResult::GetColumnNamesSync),
InstanceMethod("getQuerySummaryAsync", &NodeQueryResult::GetQuerySummaryAsync),
InstanceMethod("getQuerySummarySync", &NodeQueryResult::GetQuerySummarySync),
InstanceMethod("getCSRSync", &NodeQueryResult::GetCSRSync),
InstanceMethod("close", &NodeQueryResult::Close)});
constructor = Napi::Persistent(t);
constructor.SuppressDestruct();
exports.Set("NodeQueryResult", t);
return exports;
}
Napi::Object NodeQueryResult::NewInstance(
Napi::Env /*env*/, std::unique_ptr<QueryResult> queryResult,
std::shared_ptr<Connection> connection, std::shared_ptr<Database> database) {
auto obj = constructor.New({});
auto* nodeQueryResult = Napi::ObjectWrap<NodeQueryResult>::Unwrap(obj);
nodeQueryResult->AdoptQueryResult(
std::move(queryResult), std::move(connection), std::move(database));
return obj;
}
NodeQueryResult::NodeQueryResult(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<NodeQueryResult>(info) {}
NodeQueryResult::~NodeQueryResult() {
this->Close();
}
void NodeQueryResult::AdoptQueryResult(
std::unique_ptr<QueryResult> queryResult, std::shared_ptr<Connection> connection,
std::shared_ptr<Database> database) {
ThrowIfAsyncOperationInFlight("replace");
if (this->database) {
DatabaseDependent::Unregister(this->database.get(), this);
}
columnNames.reset();
ownedQueryResult = std::move(queryResult);
this->connection = std::move(connection);
this->database = std::move(database);
if (this->database) {
DatabaseDependent::Register(this->database.get(), this);
}
}
std::unique_ptr<QueryResult> NodeQueryResult::DetachNextQueryResult() {
if (ownedQueryResult == nullptr) {
return nullptr;
}
return ownedQueryResult->moveNextResult();
}
QueryResult& NodeQueryResult::GetQueryResult() const {
if (ownedQueryResult == nullptr) {
throw std::runtime_error("Query result is closed.");
}
return *ownedQueryResult;
}
void NodeQueryResult::AcquireAsyncUse() {
activeAsyncUses.fetch_add(1, std::memory_order_relaxed);
}
void NodeQueryResult::ReleaseAsyncUse() {
activeAsyncUses.fetch_sub(1, std::memory_order_relaxed);
}
void NodeQueryResult::ThrowIfAsyncOperationInFlight(const char* operation) const {
if (activeAsyncUses.load(std::memory_order_acquire) != 0) {
throw std::runtime_error(std::string("Cannot ") + operation +
" QueryResult while an async operation is in flight.");
}
}
void NodeQueryResult::ResetIterator(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
GetQueryResult().resetIterator();
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
}
Napi::Value NodeQueryResult::HasNext(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
return Napi::Boolean::New(env, GetQueryResult().hasNext());
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
return info.Env().Undefined();
}
Napi::Value NodeQueryResult::HasNextQueryResult(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
return Napi::Boolean::New(env, GetQueryResult().hasNextQueryResult());
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
return info.Env().Undefined();
}
Napi::Value NodeQueryResult::GetNextQueryResultAsync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto callback = info[0].As<Napi::Function>();
auto* asyncWorker = new NodeQueryResultGetNextQueryResultAsyncWorker(callback, this);
asyncWorker->Queue();
return info.Env().Undefined();
}
Napi::Value NodeQueryResult::GetNextQueryResultSync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
auto nextOwnedResult = DetachNextQueryResult();
if (nextOwnedResult == nullptr) {
return env.Undefined();
}
if (!nextOwnedResult->isSuccess()) {
Napi::Error::New(env, nextOwnedResult->getErrorMessage())
.ThrowAsJavaScriptException();
return env.Undefined();
}
return NewInstance(env, std::move(nextOwnedResult), connection, database);
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
return info.Env().Undefined();
}
Napi::Value NodeQueryResult::GetNumTuples(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
return Napi::Number::New(env, GetQueryResult().getNumTuples());
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
return info.Env().Undefined();
}
Napi::Value NodeQueryResult::GetNextAsync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto callback = info[0].As<Napi::Function>();
auto* asyncWorker = new NodeQueryResultGetNextAsyncWorker(callback, this);
asyncWorker->Queue();
return info.Env().Undefined();
}
Napi::Value NodeQueryResult::GetNextSync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
auto& queryResult = GetQueryResult();
if (!queryResult.hasNext()) {
return env.Null();
}
auto cppTuple = queryResult.getNext();
Napi::Object nodeTuple = Napi::Object::New(env);
PopulateColumnNames();
for (auto i = 0u; i < cppTuple->len(); ++i) {
Napi::Value value = Util::ConvertToNapiObject(*cppTuple->getValue(i), env);
nodeTuple.Set(columnNames->at(i), value);
}
return nodeTuple;
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
return env.Undefined();
}
Napi::Value NodeQueryResult::GetColumnDataTypesAsync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto callback = info[0].As<Napi::Function>();
auto* asyncWorker = new NodeQueryResultGetColumnMetadataAsyncWorker(callback, this,
GetColumnMetadataType::DATA_TYPE);
asyncWorker->Queue();
return info.Env().Undefined();
}
Napi::Value NodeQueryResult::GetColumnDataTypesSync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
auto columnDataTypes = GetQueryResult().getColumnDataTypes();
Napi::Array nodeColumnDataTypes = Napi::Array::New(env, columnDataTypes.size());
for (auto i = 0u; i < columnDataTypes.size(); ++i) {
nodeColumnDataTypes.Set(i, Napi::String::New(env, columnDataTypes[i].toString()));
}
return nodeColumnDataTypes;
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
return env.Undefined();
}
Napi::Value NodeQueryResult::GetColumnNamesAsync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto callback = info[0].As<Napi::Function>();
auto* asyncWorker = new NodeQueryResultGetColumnMetadataAsyncWorker(callback, this,
GetColumnMetadataType::NAME);
asyncWorker->Queue();
return info.Env().Undefined();
}
Napi::Value NodeQueryResult::GetColumnNamesSync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
PopulateColumnNames();
try {
Napi::Array nodeColumnNames = Napi::Array::New(env, columnNames->size());
for (auto i = 0u; i < columnNames->size(); ++i) {
nodeColumnNames.Set(i, Napi::String::New(env, columnNames->at(i)));
}
return nodeColumnNames;
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
return env.Undefined();
}
namespace {
struct CSRArrayBufferHolder {
explicit CSRArrayBufferHolder(ArrowQueryResult::CSRArrowArray array)
: array{std::move(array)} {}
ArrowQueryResult::CSRArrowArray array;
};
Napi::BigUint64Array WrapCSRArray(Napi::Env env, ArrowQueryResult::CSRArrowArray array) {
auto length = static_cast<size_t>(array.array.length);
auto* data = const_cast<void*>(array.array.buffers[1]);
auto* holder = new CSRArrayBufferHolder(std::move(array));
auto buffer = Napi::ArrayBuffer::New(env, data, length * sizeof(uint64_t),
[](Napi::Env, void*, CSRArrayBufferHolder* holder) { delete holder; }, holder);
return Napi::BigUint64Array::New(env, length, buffer, 0, napi_biguint64_array);
}
} // namespace
Napi::Value NodeQueryResult::GetCSRSync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
auto& queryResult = GetQueryResult();
auto* arrowQueryResult = dynamic_cast<ArrowQueryResult*>(&queryResult);
if (arrowQueryResult == nullptr || !arrowQueryResult->hasCSRMetadata()) {
throw std::runtime_error("CSR export is only supported for Arrow query results "
"with native CSR metadata.");
}
auto csr = arrowQueryResult->getCSRArrowArrays();
Napi::Object result = Napi::Object::New(env);
result.Set("indptr", WrapCSRArray(env, std::move(csr.indptr)));
result.Set("indices", WrapCSRArray(env, std::move(csr.indices)));
if (csr.edgeIDs.has_value()) {
result.Set("edgeIds", WrapCSRArray(env, std::move(*csr.edgeIDs)));
} else {
result.Set("edgeIds", env.Null());
}
return result;
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
return env.Undefined();
}
void NodeQueryResult::PopulateColumnNames() {
if (this->columnNames != nullptr) {
return;
}
this->columnNames = std::make_unique<std::vector<std::string>>(GetQueryResult().getColumnNames());
}
Napi::Value NodeQueryResult::GetQuerySummaryAsync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
auto callback = info[0].As<Napi::Function>();
auto* asyncWorker = new NodeQueryResultGetQuerySummaryAsyncWorker(callback, this);
asyncWorker->Queue();
return info.Env().Undefined();
}
Napi::Value NodeQueryResult::GetQuerySummarySync(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
Napi::Object summary = Napi::Object::New(env);
auto cppSummary = GetQueryResult().getQuerySummary();
summary.Set("compilingTime", Napi::Number::New(env, cppSummary->getCompilingTime()));
summary.Set("executionTime", Napi::Number::New(env, cppSummary->getExecutionTime()));
return summary;
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
return env.Undefined();
}
void NodeQueryResult::Close(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
ThrowIfAsyncOperationInFlight("close");
this->Close();
} catch (const std::exception& exc) {
Napi::Error::New(env, std::string(exc.what())).ThrowAsJavaScriptException();
}
}
void NodeQueryResult::ReleaseDatabase() {
// A result with an async operation in flight is being read by a worker thread: leave it.
// It still holds the database, which is then destroyed when the result is closed.
if (activeAsyncUses.load(std::memory_order_acquire) == 0) {
Close();
}
}
void NodeQueryResult::Close() {
if (database) {
DatabaseDependent::Unregister(database.get(), this);
}
columnNames.reset();
ownedQueryResult.reset();
connection.reset();
database.reset();
}