flatsql 3.11.0 → 3.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -795
- package/bin/flatsql.mjs +76 -0
- package/cpp/README.md +18 -0
- package/cpp/include/flatsql/flatsql.h +162 -0
- package/cpp/include/flatsql/flatsql_io.h +159 -0
- package/docs/API-3.12.md +114 -0
- package/package.json +28 -123
- package/wasm/flatsql.wasm +0 -0
- package/wasm/index.d.ts +61 -548
- package/wasm/index.js +280 -1281
- package/wasm/integrity.json +6 -26
- package/wasm/runtime/LICENSE.sdk.txt +190 -0
- package/wasm/runtime/NOTICE.txt +2 -0
- package/wasm/runtime/browser-guest.mjs +1 -0
- package/wasm/runtime/browser-host.mjs +1 -0
- package/wasm/runtime/node-guest.mjs +2 -0
- package/wasm/runtime/node-host.mjs +5 -0
- package/wasm/runtime/opfsIoWorker.mjs +1 -0
- package/wasm/runtime/wasiThreadBrowserWorker.mjs +1 -0
- package/dist/artifacts/index.d.ts +0 -3
- package/dist/artifacts/index.d.ts.map +0 -1
- package/dist/artifacts/index.js +0 -3
- package/dist/artifacts/index.js.map +0 -1
- package/dist/artifacts/transport.d.ts +0 -5
- package/dist/artifacts/transport.d.ts.map +0 -1
- package/dist/artifacts/transport.js +0 -45
- package/dist/artifacts/transport.js.map +0 -1
- package/dist/artifacts/types.d.ts +0 -34
- package/dist/artifacts/types.d.ts.map +0 -1
- package/dist/artifacts/types.js +0 -2
- package/dist/artifacts/types.js.map +0 -1
- package/dist/btree/btree.d.ts +0 -34
- package/dist/btree/btree.d.ts.map +0 -1
- package/dist/btree/btree.js +0 -234
- package/dist/btree/btree.js.map +0 -1
- package/dist/btree/index.d.ts +0 -3
- package/dist/btree/index.d.ts.map +0 -1
- package/dist/btree/index.js +0 -3
- package/dist/btree/index.js.map +0 -1
- package/dist/btree/types.d.ts +0 -36
- package/dist/btree/types.d.ts.map +0 -1
- package/dist/btree/types.js +0 -55
- package/dist/btree/types.js.map +0 -1
- package/dist/cluster/index.d.ts +0 -11
- package/dist/cluster/index.d.ts.map +0 -1
- package/dist/cluster/index.js +0 -59
- package/dist/cluster/index.js.map +0 -1
- package/dist/response/artifact.d.ts +0 -4
- package/dist/response/artifact.d.ts.map +0 -1
- package/dist/response/artifact.js +0 -102
- package/dist/response/artifact.js.map +0 -1
- package/dist/response/cache-key.d.ts +0 -6
- package/dist/response/cache-key.d.ts.map +0 -1
- package/dist/response/cache-key.js +0 -114
- package/dist/response/cache-key.js.map +0 -1
- package/dist/response/index.d.ts +0 -5
- package/dist/response/index.d.ts.map +0 -1
- package/dist/response/index.js +0 -5
- package/dist/response/index.js.map +0 -1
- package/dist/response/memory-cache.d.ts +0 -17
- package/dist/response/memory-cache.d.ts.map +0 -1
- package/dist/response/memory-cache.js +0 -76
- package/dist/response/memory-cache.js.map +0 -1
- package/dist/response/types.d.ts +0 -57
- package/dist/response/types.d.ts.map +0 -1
- package/dist/response/types.js +0 -2
- package/dist/response/types.js.map +0 -1
- package/dist/schema/index.d.ts +0 -3
- package/dist/schema/index.d.ts.map +0 -1
- package/dist/schema/index.js +0 -3
- package/dist/schema/index.js.map +0 -1
- package/dist/schema/parser.d.ts +0 -25
- package/dist/schema/parser.d.ts.map +0 -1
- package/dist/schema/parser.js +0 -474
- package/dist/schema/parser.js.map +0 -1
- package/dist/schema/types.d.ts +0 -33
- package/dist/schema/types.d.ts.map +0 -1
- package/dist/schema/types.js +0 -29
- package/dist/schema/types.js.map +0 -1
- package/dist/standalone/index.d.ts +0 -3
- package/dist/standalone/index.d.ts.map +0 -1
- package/dist/standalone/index.js +0 -3
- package/dist/standalone/index.js.map +0 -1
- package/dist/standalone/process-client.d.ts +0 -85
- package/dist/standalone/process-client.d.ts.map +0 -1
- package/dist/standalone/process-client.js +0 -537
- package/dist/standalone/process-client.js.map +0 -1
- package/dist/standalone/wasmedge-runner.d.ts +0 -19
- package/dist/standalone/wasmedge-runner.d.ts.map +0 -1
- package/dist/standalone/wasmedge-runner.js +0 -108
- package/dist/standalone/wasmedge-runner.js.map +0 -1
- package/dist/storage/index.d.ts +0 -3
- package/dist/storage/index.d.ts.map +0 -1
- package/dist/storage/index.js +0 -3
- package/dist/storage/index.js.map +0 -1
- package/dist/storage/stacked-flatbuffers.d.ts +0 -38
- package/dist/storage/stacked-flatbuffers.d.ts.map +0 -1
- package/dist/storage/stacked-flatbuffers.js +0 -253
- package/dist/storage/stacked-flatbuffers.js.map +0 -1
- package/dist/storage/types.d.ts +0 -23
- package/dist/storage/types.d.ts.map +0 -1
- package/dist/storage/types.js +0 -27
- package/dist/storage/types.js.map +0 -1
- package/dist/stress/aggregate.d.ts +0 -3
- package/dist/stress/aggregate.d.ts.map +0 -1
- package/dist/stress/aggregate.js +0 -61
- package/dist/stress/aggregate.js.map +0 -1
- package/dist/stress/docker-compose.d.ts +0 -17
- package/dist/stress/docker-compose.d.ts.map +0 -1
- package/dist/stress/docker-compose.js +0 -156
- package/dist/stress/docker-compose.js.map +0 -1
- package/dist/stress/index.d.ts +0 -10
- package/dist/stress/index.d.ts.map +0 -1
- package/dist/stress/index.js +0 -10
- package/dist/stress/index.js.map +0 -1
- package/dist/stress/metrics.d.ts +0 -3
- package/dist/stress/metrics.d.ts.map +0 -1
- package/dist/stress/metrics.js +0 -83
- package/dist/stress/metrics.js.map +0 -1
- package/dist/stress/native-publish-event-workload.d.ts +0 -3
- package/dist/stress/native-publish-event-workload.d.ts.map +0 -1
- package/dist/stress/native-publish-event-workload.js +0 -465
- package/dist/stress/native-publish-event-workload.js.map +0 -1
- package/dist/stress/production-gate.d.ts +0 -4
- package/dist/stress/production-gate.d.ts.map +0 -1
- package/dist/stress/production-gate.js +0 -12
- package/dist/stress/production-gate.js.map +0 -1
- package/dist/stress/report.d.ts +0 -3
- package/dist/stress/report.d.ts.map +0 -1
- package/dist/stress/report.js +0 -174
- package/dist/stress/report.js.map +0 -1
- package/dist/stress/sds-discovery.d.ts +0 -4
- package/dist/stress/sds-discovery.d.ts.map +0 -1
- package/dist/stress/sds-discovery.js +0 -40
- package/dist/stress/sds-discovery.js.map +0 -1
- package/dist/stress/smoke-runner.d.ts +0 -3
- package/dist/stress/smoke-runner.d.ts.map +0 -1
- package/dist/stress/smoke-runner.js +0 -64
- package/dist/stress/smoke-runner.js.map +0 -1
- package/dist/stress/types.d.ts +0 -219
- package/dist/stress/types.d.ts.map +0 -1
- package/dist/stress/types.js +0 -2
- package/dist/stress/types.js.map +0 -1
- package/dist/stress/workload.d.ts +0 -6
- package/dist/stress/workload.d.ts.map +0 -1
- package/dist/stress/workload.js +0 -144
- package/dist/stress/workload.js.map +0 -1
- package/docs/PARTITION-STORE-WASM.md +0 -338
- package/docs/PARTITION-STORE.md +0 -2380
- package/docs/agents/response-artifacts.md +0 -65
- package/docs/response-artifacts.md +0 -86
- package/scripts/stress-docker.sh +0 -279
- package/sdm/flatsql-spatial.wasm +0 -0
- package/sdm/index.d.ts +0 -88
- package/sdm/index.js +0 -253
- package/sdm/manifest.json +0 -402
- package/stress/docker/Dockerfile +0 -11
- package/stress/docker/docker-compose.yml +0 -51
- package/stress/sds/aggregate.mjs +0 -97
- package/stress/sds/run.mjs +0 -105
- package/stress/sds/write-compose.mjs +0 -96
- package/wasm/flatsql-client.js +0 -280
- package/wasm/flatsql-io.d.ts +0 -97
- package/wasm/flatsql-io.js +0 -621
- package/wasm/flatsql-p4-threads.wasm +0 -0
- package/wasm/flatsql-ps-threads.wasm +0 -0
- package/wasm/flatsql-wasi-noeh.wasm +0 -0
- package/wasm/flatsql-wasi.wasm +0 -0
- package/wasm/flatsql.js +0 -2
- package/wasm/flatsql.worker.js +0 -209
- package/wasm/native/flatsql_wasmedge_runner.cpp +0 -858
- package/wasm/ps-node-fault.d.mts +0 -23
- package/wasm/ps-node-fault.mjs +0 -381
- package/wasm/ps-node-guest.mjs +0 -173
- package/wasm/ps-node-host.d.mts +0 -95
- package/wasm/ps-node-host.mjs +0 -476
- package/wasm/ps-node-imports.mjs +0 -847
- package/wasm/ps.d.ts +0 -21
- package/wasm/ps.js +0 -75
- package/wasm/wasi.d.ts +0 -11
- package/wasm/wasi.js +0 -51
|
@@ -1,858 +0,0 @@
|
|
|
1
|
-
#include <wasmedge/wasmedge.h>
|
|
2
|
-
|
|
3
|
-
#include <cstdint>
|
|
4
|
-
#include <cstring>
|
|
5
|
-
#include <exception>
|
|
6
|
-
#include <iostream>
|
|
7
|
-
#include <limits>
|
|
8
|
-
#include <stdexcept>
|
|
9
|
-
#include <string>
|
|
10
|
-
#include <string_view>
|
|
11
|
-
#include <type_traits>
|
|
12
|
-
#include <utility>
|
|
13
|
-
#include <vector>
|
|
14
|
-
#include <unistd.h>
|
|
15
|
-
|
|
16
|
-
namespace {
|
|
17
|
-
|
|
18
|
-
constexpr uint32_t OP_CREATE_DB = 1;
|
|
19
|
-
constexpr uint32_t OP_DESTROY_DB = 2;
|
|
20
|
-
constexpr uint32_t OP_REGISTER_FILE_ID = 3;
|
|
21
|
-
constexpr uint32_t OP_ENABLE_DEMO_EXTRACTORS = 4;
|
|
22
|
-
constexpr uint32_t OP_INGEST = 5;
|
|
23
|
-
constexpr uint32_t OP_QUERY = 6;
|
|
24
|
-
constexpr uint32_t OP_QUERY_MANY = 7;
|
|
25
|
-
constexpr uint32_t OP_REGISTER_QUERY_TEMPLATE = 8;
|
|
26
|
-
constexpr uint32_t OP_QUERY_TEMPLATE = 9;
|
|
27
|
-
constexpr uint32_t OP_CLEAR_QUERY_CACHE = 10;
|
|
28
|
-
constexpr uint32_t OP_QUERY_CACHE_STATS = 11;
|
|
29
|
-
constexpr uint32_t OP_GET_FLATBUFFER_BY_INDEX = 12;
|
|
30
|
-
constexpr uint32_t OP_EXPORT_DATA = 13;
|
|
31
|
-
constexpr uint32_t OP_LOAD_AND_REBUILD = 14;
|
|
32
|
-
constexpr uint32_t OP_BUILD_RESPONSE_ARTIFACT_CACHE_KEY = 15;
|
|
33
|
-
constexpr uint32_t OP_QUERY_RAW_FLATBUFFER_STREAM = 16;
|
|
34
|
-
constexpr uint32_t OP_RESERVE_STORAGE = 17;
|
|
35
|
-
constexpr uint32_t OP_LOAD_FROM_DB = 18;
|
|
36
|
-
constexpr uint32_t OP_CONFIGURE_QUERY_CACHE = 19;
|
|
37
|
-
|
|
38
|
-
int g_protocolFd = STDOUT_FILENO;
|
|
39
|
-
|
|
40
|
-
bool readExact(std::istream& input, uint8_t* data, size_t length) {
|
|
41
|
-
input.read(reinterpret_cast<char*>(data), static_cast<std::streamsize>(length));
|
|
42
|
-
return input.good() || static_cast<size_t>(input.gcount()) == length;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
uint32_t readU32FromStream(std::istream& input, bool& ok) {
|
|
46
|
-
uint8_t bytes[4];
|
|
47
|
-
if (!readExact(input, bytes, sizeof(bytes))) {
|
|
48
|
-
ok = false;
|
|
49
|
-
return 0;
|
|
50
|
-
}
|
|
51
|
-
ok = true;
|
|
52
|
-
return static_cast<uint32_t>(bytes[0]) |
|
|
53
|
-
(static_cast<uint32_t>(bytes[1]) << 8) |
|
|
54
|
-
(static_cast<uint32_t>(bytes[2]) << 16) |
|
|
55
|
-
(static_cast<uint32_t>(bytes[3]) << 24);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
class Reader {
|
|
59
|
-
public:
|
|
60
|
-
explicit Reader(const std::vector<uint8_t>& data) : data_(data) {}
|
|
61
|
-
|
|
62
|
-
uint8_t u8() {
|
|
63
|
-
require(1);
|
|
64
|
-
return data_[offset_++];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
uint32_t u32() {
|
|
68
|
-
require(4);
|
|
69
|
-
uint32_t value = static_cast<uint32_t>(data_[offset_]) |
|
|
70
|
-
(static_cast<uint32_t>(data_[offset_ + 1]) << 8) |
|
|
71
|
-
(static_cast<uint32_t>(data_[offset_ + 2]) << 16) |
|
|
72
|
-
(static_cast<uint32_t>(data_[offset_ + 3]) << 24);
|
|
73
|
-
offset_ += 4;
|
|
74
|
-
return value;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
std::string string() {
|
|
78
|
-
auto bytes = byteVector();
|
|
79
|
-
return std::string(reinterpret_cast<const char*>(bytes.data()), bytes.size());
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
std::vector<uint8_t> byteVector() {
|
|
83
|
-
const uint32_t length = u32();
|
|
84
|
-
require(length);
|
|
85
|
-
std::vector<uint8_t> bytes(data_.begin() + static_cast<std::ptrdiff_t>(offset_),
|
|
86
|
-
data_.begin() + static_cast<std::ptrdiff_t>(offset_ + length));
|
|
87
|
-
offset_ += length;
|
|
88
|
-
return bytes;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
void finish() {
|
|
92
|
-
if (offset_ != data_.size()) {
|
|
93
|
-
throw std::runtime_error("request has trailing bytes");
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
private:
|
|
98
|
-
void require(size_t length) {
|
|
99
|
-
if (offset_ > data_.size() || length > data_.size() - offset_) {
|
|
100
|
-
throw std::runtime_error("malformed runner request");
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const std::vector<uint8_t>& data_;
|
|
105
|
-
size_t offset_ = 0;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
class Writer {
|
|
109
|
-
public:
|
|
110
|
-
void u8(uint8_t value) {
|
|
111
|
-
data_.push_back(value);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
void u32(uint32_t value) {
|
|
115
|
-
data_.push_back(static_cast<uint8_t>(value & 0xff));
|
|
116
|
-
data_.push_back(static_cast<uint8_t>((value >> 8) & 0xff));
|
|
117
|
-
data_.push_back(static_cast<uint8_t>((value >> 16) & 0xff));
|
|
118
|
-
data_.push_back(static_cast<uint8_t>((value >> 24) & 0xff));
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
void f64(double value) {
|
|
122
|
-
static_assert(sizeof(double) == 8);
|
|
123
|
-
uint8_t bytes[8];
|
|
124
|
-
std::memcpy(bytes, &value, sizeof(bytes));
|
|
125
|
-
data_.insert(data_.end(), bytes, bytes + sizeof(bytes));
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
void string(std::string_view value) {
|
|
129
|
-
if (value.size() > std::numeric_limits<uint32_t>::max()) {
|
|
130
|
-
throw std::runtime_error("string too large for runner response");
|
|
131
|
-
}
|
|
132
|
-
u32(static_cast<uint32_t>(value.size()));
|
|
133
|
-
data_.insert(data_.end(), value.begin(), value.end());
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
void bytes(const std::vector<uint8_t>& value) {
|
|
137
|
-
if (value.size() > std::numeric_limits<uint32_t>::max()) {
|
|
138
|
-
throw std::runtime_error("byte payload too large for runner response");
|
|
139
|
-
}
|
|
140
|
-
u32(static_cast<uint32_t>(value.size()));
|
|
141
|
-
data_.insert(data_.end(), value.begin(), value.end());
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const std::vector<uint8_t>& data() const {
|
|
145
|
-
return data_;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
private:
|
|
149
|
-
std::vector<uint8_t> data_;
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
void writeFrame(const std::vector<uint8_t>& payload) {
|
|
153
|
-
if (payload.size() > std::numeric_limits<uint32_t>::max()) {
|
|
154
|
-
throw std::runtime_error("runner response frame too large");
|
|
155
|
-
}
|
|
156
|
-
const uint32_t length = static_cast<uint32_t>(payload.size());
|
|
157
|
-
uint8_t header[4] = {
|
|
158
|
-
static_cast<uint8_t>(length & 0xff),
|
|
159
|
-
static_cast<uint8_t>((length >> 8) & 0xff),
|
|
160
|
-
static_cast<uint8_t>((length >> 16) & 0xff),
|
|
161
|
-
static_cast<uint8_t>((length >> 24) & 0xff),
|
|
162
|
-
};
|
|
163
|
-
if (::write(g_protocolFd, header, sizeof(header)) != static_cast<ssize_t>(sizeof(header))) {
|
|
164
|
-
throw std::runtime_error("failed to write runner frame header");
|
|
165
|
-
}
|
|
166
|
-
if (!payload.empty() &&
|
|
167
|
-
::write(g_protocolFd, payload.data(), payload.size()) != static_cast<ssize_t>(payload.size())) {
|
|
168
|
-
throw std::runtime_error("failed to write runner frame payload");
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
std::vector<uint8_t> makeOkFrame(const Writer& body) {
|
|
173
|
-
Writer frame;
|
|
174
|
-
frame.u8(1);
|
|
175
|
-
const auto& payload = body.data();
|
|
176
|
-
auto result = frame.data();
|
|
177
|
-
result.insert(result.end(), payload.begin(), payload.end());
|
|
178
|
-
return result;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
std::vector<uint8_t> makeErrorFrame(const std::string& message) {
|
|
182
|
-
Writer frame;
|
|
183
|
-
frame.u8(0);
|
|
184
|
-
frame.string(message);
|
|
185
|
-
return frame.data();
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
WasmEdge_String wrapName(const char* name) {
|
|
189
|
-
return WasmEdge_StringWrap(name, static_cast<uint32_t>(std::strlen(name)));
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
void requireResult(WasmEdge_Result result, const std::string& operation) {
|
|
193
|
-
if (!WasmEdge_ResultOK(result)) {
|
|
194
|
-
throw std::runtime_error(operation + " failed: " + WasmEdge_ResultGetMessage(result));
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
class WasmEdgeRuntime {
|
|
199
|
-
public:
|
|
200
|
-
explicit WasmEdgeRuntime(const std::string& wasmPath) {
|
|
201
|
-
configure_ = WasmEdge_ConfigureCreate();
|
|
202
|
-
if (!configure_) {
|
|
203
|
-
throw std::runtime_error("failed to create WasmEdge configure context");
|
|
204
|
-
}
|
|
205
|
-
WasmEdge_ConfigureAddProposal(configure_, WasmEdge_Proposal_ExceptionHandling);
|
|
206
|
-
WasmEdge_ConfigureAddHostRegistration(configure_, WasmEdge_HostRegistration_Wasi);
|
|
207
|
-
|
|
208
|
-
vm_ = WasmEdge_VMCreate(configure_, nullptr);
|
|
209
|
-
if (!vm_) {
|
|
210
|
-
throw std::runtime_error("failed to create WasmEdge VM");
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
requireResult(WasmEdge_VMLoadWasmFromFile(vm_, wasmPath.c_str()), "load wasm");
|
|
214
|
-
requireResult(WasmEdge_VMValidate(vm_), "validate wasm");
|
|
215
|
-
requireResult(WasmEdge_VMInstantiate(vm_), "instantiate wasm");
|
|
216
|
-
callVoid("_initialize");
|
|
217
|
-
|
|
218
|
-
const WasmEdge_ModuleInstanceContext* module = WasmEdge_VMGetActiveModule(vm_);
|
|
219
|
-
if (!module) {
|
|
220
|
-
throw std::runtime_error("WasmEdge VM has no active module");
|
|
221
|
-
}
|
|
222
|
-
auto memoryName = wrapName("memory");
|
|
223
|
-
memory_ = WasmEdge_ModuleInstanceFindMemory(module, memoryName);
|
|
224
|
-
if (!memory_) {
|
|
225
|
-
throw std::runtime_error("FlatSQL WASI module does not export memory");
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
WasmEdgeRuntime(const WasmEdgeRuntime&) = delete;
|
|
230
|
-
WasmEdgeRuntime& operator=(const WasmEdgeRuntime&) = delete;
|
|
231
|
-
|
|
232
|
-
~WasmEdgeRuntime() {
|
|
233
|
-
if (vm_) {
|
|
234
|
-
WasmEdge_VMDelete(vm_);
|
|
235
|
-
}
|
|
236
|
-
if (configure_) {
|
|
237
|
-
WasmEdge_ConfigureDelete(configure_);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
uint32_t callI32(const char* name, const std::vector<WasmEdge_Value>& params = {}) {
|
|
242
|
-
WasmEdge_Value result[1];
|
|
243
|
-
auto funcName = wrapName(name);
|
|
244
|
-
requireResult(
|
|
245
|
-
WasmEdge_VMExecute(
|
|
246
|
-
vm_,
|
|
247
|
-
funcName,
|
|
248
|
-
params.empty() ? nullptr : params.data(),
|
|
249
|
-
static_cast<uint32_t>(params.size()),
|
|
250
|
-
result,
|
|
251
|
-
1
|
|
252
|
-
),
|
|
253
|
-
name
|
|
254
|
-
);
|
|
255
|
-
return static_cast<uint32_t>(WasmEdge_ValueGetI32(result[0]));
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
double callF64(const char* name, const std::vector<WasmEdge_Value>& params = {}) {
|
|
259
|
-
WasmEdge_Value result[1];
|
|
260
|
-
auto funcName = wrapName(name);
|
|
261
|
-
requireResult(
|
|
262
|
-
WasmEdge_VMExecute(
|
|
263
|
-
vm_,
|
|
264
|
-
funcName,
|
|
265
|
-
params.empty() ? nullptr : params.data(),
|
|
266
|
-
static_cast<uint32_t>(params.size()),
|
|
267
|
-
result,
|
|
268
|
-
1
|
|
269
|
-
),
|
|
270
|
-
name
|
|
271
|
-
);
|
|
272
|
-
return WasmEdge_ValueGetF64(result[0]);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
void callVoid(const char* name, const std::vector<WasmEdge_Value>& params = {}) {
|
|
276
|
-
auto funcName = wrapName(name);
|
|
277
|
-
requireResult(
|
|
278
|
-
WasmEdge_VMExecute(
|
|
279
|
-
vm_,
|
|
280
|
-
funcName,
|
|
281
|
-
params.empty() ? nullptr : params.data(),
|
|
282
|
-
static_cast<uint32_t>(params.size()),
|
|
283
|
-
nullptr,
|
|
284
|
-
0
|
|
285
|
-
),
|
|
286
|
-
name
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
uint32_t mallocBytes(size_t size) {
|
|
291
|
-
if (size == 0) {
|
|
292
|
-
return 0;
|
|
293
|
-
}
|
|
294
|
-
if (size > std::numeric_limits<uint32_t>::max()) {
|
|
295
|
-
throw std::runtime_error("allocation request exceeds wasm32 address space");
|
|
296
|
-
}
|
|
297
|
-
const uint32_t ptr = callI32("malloc", {WasmEdge_ValueGenI32(static_cast<int32_t>(size))});
|
|
298
|
-
if (ptr == 0) {
|
|
299
|
-
throw std::runtime_error("FlatSQL WASM allocation failed");
|
|
300
|
-
}
|
|
301
|
-
return ptr;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
void freeBytes(uint32_t ptr) {
|
|
305
|
-
if (ptr != 0) {
|
|
306
|
-
callVoid("free", {WasmEdge_ValueGenI32(static_cast<int32_t>(ptr))});
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
template <typename Callback>
|
|
311
|
-
auto withBytes(const std::vector<uint8_t>& bytes, Callback callback) -> decltype(callback(uint32_t{})) {
|
|
312
|
-
const uint32_t ptr = mallocBytes(bytes.size());
|
|
313
|
-
try {
|
|
314
|
-
if (!bytes.empty()) {
|
|
315
|
-
requireResult(
|
|
316
|
-
WasmEdge_MemoryInstanceSetData(memory_, bytes.data(), ptr, static_cast<uint32_t>(bytes.size())),
|
|
317
|
-
"write wasm memory"
|
|
318
|
-
);
|
|
319
|
-
}
|
|
320
|
-
if constexpr (std::is_void_v<decltype(callback(ptr))>) {
|
|
321
|
-
callback(ptr);
|
|
322
|
-
freeBytes(ptr);
|
|
323
|
-
} else {
|
|
324
|
-
auto result = callback(ptr);
|
|
325
|
-
freeBytes(ptr);
|
|
326
|
-
return result;
|
|
327
|
-
}
|
|
328
|
-
} catch (...) {
|
|
329
|
-
freeBytes(ptr);
|
|
330
|
-
throw;
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
template <typename Callback>
|
|
335
|
-
auto withString(const std::string& value, Callback callback) -> decltype(callback(uint32_t{})) {
|
|
336
|
-
std::vector<uint8_t> bytes(value.begin(), value.end());
|
|
337
|
-
bytes.push_back(0);
|
|
338
|
-
return withBytes(bytes, callback);
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
std::string readCString(uint32_t ptr) {
|
|
342
|
-
if (ptr == 0) {
|
|
343
|
-
return "";
|
|
344
|
-
}
|
|
345
|
-
const uint32_t pageCount = WasmEdge_MemoryInstanceGetPageSize(memory_);
|
|
346
|
-
const uint64_t memorySize = static_cast<uint64_t>(pageCount) * 65536ULL;
|
|
347
|
-
if (ptr >= memorySize) {
|
|
348
|
-
throw std::runtime_error("WASM string pointer is outside memory");
|
|
349
|
-
}
|
|
350
|
-
const auto available = static_cast<uint32_t>(std::min<uint64_t>(memorySize - ptr, 16ULL * 1024ULL * 1024ULL));
|
|
351
|
-
const uint8_t* raw = WasmEdge_MemoryInstanceGetPointerConst(memory_, ptr, available);
|
|
352
|
-
if (!raw) {
|
|
353
|
-
throw std::runtime_error("failed to read WASM string pointer");
|
|
354
|
-
}
|
|
355
|
-
size_t length = 0;
|
|
356
|
-
while (length < available && raw[length] != 0) {
|
|
357
|
-
length++;
|
|
358
|
-
}
|
|
359
|
-
if (length == available) {
|
|
360
|
-
throw std::runtime_error("unterminated WASM string");
|
|
361
|
-
}
|
|
362
|
-
return std::string(reinterpret_cast<const char*>(raw), length);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
std::vector<uint8_t> readBytes(uint32_t ptr, uint32_t length) {
|
|
366
|
-
std::vector<uint8_t> bytes(length);
|
|
367
|
-
if (length == 0) {
|
|
368
|
-
return bytes;
|
|
369
|
-
}
|
|
370
|
-
requireResult(WasmEdge_MemoryInstanceGetData(memory_, bytes.data(), ptr, length), "read wasm memory");
|
|
371
|
-
return bytes;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
std::string lastError() {
|
|
375
|
-
return readCString(callI32("flatsql_get_error"));
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
private:
|
|
379
|
-
WasmEdge_ConfigureContext* configure_ = nullptr;
|
|
380
|
-
WasmEdge_VMContext* vm_ = nullptr;
|
|
381
|
-
WasmEdge_MemoryInstanceContext* memory_ = nullptr;
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
void checkFlatSQL(WasmEdgeRuntime& runtime, uint32_t success) {
|
|
385
|
-
if (success == 0) {
|
|
386
|
-
std::string error = runtime.lastError();
|
|
387
|
-
if (error.empty()) {
|
|
388
|
-
error = "FlatSQL operation failed";
|
|
389
|
-
}
|
|
390
|
-
throw std::runtime_error(error);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
std::string joinStrings(const std::vector<std::string>& values, char delimiter) {
|
|
395
|
-
std::string out;
|
|
396
|
-
for (size_t index = 0; index < values.size(); index++) {
|
|
397
|
-
if (index > 0) {
|
|
398
|
-
out.push_back(delimiter);
|
|
399
|
-
}
|
|
400
|
-
out += values[index];
|
|
401
|
-
}
|
|
402
|
-
return out;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
void encodeQueryResult(WasmEdgeRuntime& runtime, Writer& writer) {
|
|
406
|
-
const uint32_t columnCount = runtime.callI32("flatsql_result_column_count");
|
|
407
|
-
const uint32_t rowCount = runtime.callI32("flatsql_result_row_count");
|
|
408
|
-
|
|
409
|
-
writer.u32(columnCount);
|
|
410
|
-
for (uint32_t column = 0; column < columnCount; column++) {
|
|
411
|
-
const uint32_t ptr = runtime.callI32("flatsql_result_column_name", {
|
|
412
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(column)),
|
|
413
|
-
});
|
|
414
|
-
writer.string(runtime.readCString(ptr));
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
writer.u32(rowCount);
|
|
418
|
-
for (uint32_t row = 0; row < rowCount; row++) {
|
|
419
|
-
for (uint32_t column = 0; column < columnCount; column++) {
|
|
420
|
-
const uint32_t type = runtime.callI32("flatsql_result_cell_type", {
|
|
421
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(row)),
|
|
422
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(column)),
|
|
423
|
-
});
|
|
424
|
-
writer.u8(static_cast<uint8_t>(type));
|
|
425
|
-
switch (type) {
|
|
426
|
-
case 0:
|
|
427
|
-
break;
|
|
428
|
-
case 1:
|
|
429
|
-
writer.u8(runtime.callF64("flatsql_result_cell_number", {
|
|
430
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(row)),
|
|
431
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(column)),
|
|
432
|
-
}) != 0.0 ? 1 : 0);
|
|
433
|
-
break;
|
|
434
|
-
case 2:
|
|
435
|
-
case 3:
|
|
436
|
-
case 4:
|
|
437
|
-
writer.f64(runtime.callF64("flatsql_result_cell_number", {
|
|
438
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(row)),
|
|
439
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(column)),
|
|
440
|
-
}));
|
|
441
|
-
break;
|
|
442
|
-
case 5: {
|
|
443
|
-
const uint32_t ptr = runtime.callI32("flatsql_result_cell_string", {
|
|
444
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(row)),
|
|
445
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(column)),
|
|
446
|
-
});
|
|
447
|
-
writer.string(runtime.readCString(ptr));
|
|
448
|
-
break;
|
|
449
|
-
}
|
|
450
|
-
case 6: {
|
|
451
|
-
const uint32_t ptr = runtime.callI32("flatsql_result_cell_blob", {
|
|
452
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(row)),
|
|
453
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(column)),
|
|
454
|
-
});
|
|
455
|
-
const uint32_t size = runtime.callI32("flatsql_result_cell_blob_size", {
|
|
456
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(row)),
|
|
457
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(column)),
|
|
458
|
-
});
|
|
459
|
-
writer.bytes(runtime.readBytes(ptr, size));
|
|
460
|
-
break;
|
|
461
|
-
}
|
|
462
|
-
default:
|
|
463
|
-
throw std::runtime_error("unsupported FlatSQL cell type");
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
std::vector<uint8_t> handleRequest(WasmEdgeRuntime& runtime, const std::vector<uint8_t>& request) {
|
|
470
|
-
Reader reader(request);
|
|
471
|
-
const uint32_t op = reader.u32();
|
|
472
|
-
Writer body;
|
|
473
|
-
|
|
474
|
-
switch (op) {
|
|
475
|
-
case OP_CREATE_DB: {
|
|
476
|
-
const std::string schema = reader.string();
|
|
477
|
-
const std::string name = reader.string();
|
|
478
|
-
reader.finish();
|
|
479
|
-
const uint32_t handle = runtime.withString(schema, [&](uint32_t schemaPtr) {
|
|
480
|
-
return runtime.withString(name, [&](uint32_t namePtr) {
|
|
481
|
-
return runtime.callI32("flatsql_create_db", {
|
|
482
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(schemaPtr)),
|
|
483
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(namePtr)),
|
|
484
|
-
});
|
|
485
|
-
});
|
|
486
|
-
});
|
|
487
|
-
if (handle == 0) {
|
|
488
|
-
throw std::runtime_error("failed to create FlatSQL database");
|
|
489
|
-
}
|
|
490
|
-
body.u32(handle);
|
|
491
|
-
break;
|
|
492
|
-
}
|
|
493
|
-
case OP_DESTROY_DB: {
|
|
494
|
-
const uint32_t handle = reader.u32();
|
|
495
|
-
reader.finish();
|
|
496
|
-
runtime.callVoid("flatsql_destroy_db", {WasmEdge_ValueGenI32(static_cast<int32_t>(handle))});
|
|
497
|
-
break;
|
|
498
|
-
}
|
|
499
|
-
case OP_REGISTER_FILE_ID: {
|
|
500
|
-
const uint32_t handle = reader.u32();
|
|
501
|
-
const std::string fileId = reader.string();
|
|
502
|
-
const std::string tableName = reader.string();
|
|
503
|
-
reader.finish();
|
|
504
|
-
runtime.withString(fileId, [&](uint32_t fileIdPtr) {
|
|
505
|
-
runtime.withString(tableName, [&](uint32_t tablePtr) {
|
|
506
|
-
runtime.callVoid("flatsql_register_file_id", {
|
|
507
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
508
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(fileIdPtr)),
|
|
509
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(tablePtr)),
|
|
510
|
-
});
|
|
511
|
-
});
|
|
512
|
-
});
|
|
513
|
-
break;
|
|
514
|
-
}
|
|
515
|
-
case OP_ENABLE_DEMO_EXTRACTORS: {
|
|
516
|
-
const uint32_t handle = reader.u32();
|
|
517
|
-
reader.finish();
|
|
518
|
-
runtime.callVoid("flatsql_enable_demo_extractors", {WasmEdge_ValueGenI32(static_cast<int32_t>(handle))});
|
|
519
|
-
break;
|
|
520
|
-
}
|
|
521
|
-
case OP_INGEST: {
|
|
522
|
-
const uint32_t handle = reader.u32();
|
|
523
|
-
const uint8_t hasSource = reader.u8();
|
|
524
|
-
const std::string source = hasSource != 0 ? reader.string() : "";
|
|
525
|
-
const std::vector<uint8_t> data = reader.byteVector();
|
|
526
|
-
reader.finish();
|
|
527
|
-
const double count = runtime.withBytes(data, [&](uint32_t dataPtr) {
|
|
528
|
-
if (hasSource != 0) {
|
|
529
|
-
return runtime.withString(source, [&](uint32_t sourcePtr) {
|
|
530
|
-
return runtime.callF64("flatsql_ingest_with_source", {
|
|
531
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
532
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(dataPtr)),
|
|
533
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(data.size())),
|
|
534
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(sourcePtr)),
|
|
535
|
-
});
|
|
536
|
-
});
|
|
537
|
-
}
|
|
538
|
-
return runtime.callF64("flatsql_ingest", {
|
|
539
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
540
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(dataPtr)),
|
|
541
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(data.size())),
|
|
542
|
-
});
|
|
543
|
-
});
|
|
544
|
-
body.f64(count);
|
|
545
|
-
break;
|
|
546
|
-
}
|
|
547
|
-
case OP_QUERY: {
|
|
548
|
-
const uint32_t handle = reader.u32();
|
|
549
|
-
const std::string sql = reader.string();
|
|
550
|
-
const uint32_t paramCount = reader.u32();
|
|
551
|
-
const std::vector<uint8_t> params = reader.byteVector();
|
|
552
|
-
reader.finish();
|
|
553
|
-
const uint32_t success = runtime.withString(sql, [&](uint32_t sqlPtr) {
|
|
554
|
-
return runtime.withBytes(params, [&](uint32_t paramsPtr) {
|
|
555
|
-
return runtime.callI32("flatsql_query_params", {
|
|
556
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
557
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(sqlPtr)),
|
|
558
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(paramsPtr)),
|
|
559
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(params.size())),
|
|
560
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(paramCount)),
|
|
561
|
-
});
|
|
562
|
-
});
|
|
563
|
-
});
|
|
564
|
-
checkFlatSQL(runtime, success);
|
|
565
|
-
encodeQueryResult(runtime, body);
|
|
566
|
-
break;
|
|
567
|
-
}
|
|
568
|
-
case OP_QUERY_MANY: {
|
|
569
|
-
const uint32_t handle = reader.u32();
|
|
570
|
-
const uint32_t requestCount = reader.u32();
|
|
571
|
-
const std::vector<uint8_t> requestBytes = reader.byteVector();
|
|
572
|
-
reader.finish();
|
|
573
|
-
const uint32_t success = runtime.withBytes(requestBytes, [&](uint32_t ptr) {
|
|
574
|
-
return runtime.callI32("flatsql_query_many", {
|
|
575
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
576
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(ptr)),
|
|
577
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(requestBytes.size())),
|
|
578
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(requestCount)),
|
|
579
|
-
});
|
|
580
|
-
});
|
|
581
|
-
checkFlatSQL(runtime, success);
|
|
582
|
-
const uint32_t resultCount = runtime.callI32("flatsql_batch_result_count");
|
|
583
|
-
body.u32(resultCount);
|
|
584
|
-
for (uint32_t index = 0; index < resultCount; index++) {
|
|
585
|
-
checkFlatSQL(runtime, runtime.callI32("flatsql_select_batch_result", {
|
|
586
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(index)),
|
|
587
|
-
}));
|
|
588
|
-
encodeQueryResult(runtime, body);
|
|
589
|
-
}
|
|
590
|
-
break;
|
|
591
|
-
}
|
|
592
|
-
case OP_QUERY_RAW_FLATBUFFER_STREAM: {
|
|
593
|
-
const uint32_t handle = reader.u32();
|
|
594
|
-
const std::string sql = reader.string();
|
|
595
|
-
const uint32_t paramCount = reader.u32();
|
|
596
|
-
const std::vector<uint8_t> params = reader.byteVector();
|
|
597
|
-
reader.finish();
|
|
598
|
-
const uint32_t success = runtime.withString(sql, [&](uint32_t sqlPtr) {
|
|
599
|
-
return runtime.withBytes(params, [&](uint32_t paramsPtr) {
|
|
600
|
-
return runtime.callI32("flatsql_query_raw_flatbuffer_stream", {
|
|
601
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
602
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(sqlPtr)),
|
|
603
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(paramsPtr)),
|
|
604
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(params.size())),
|
|
605
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(paramCount)),
|
|
606
|
-
});
|
|
607
|
-
});
|
|
608
|
-
});
|
|
609
|
-
checkFlatSQL(runtime, success);
|
|
610
|
-
const uint32_t ptr = runtime.callI32("flatsql_response_artifact_data");
|
|
611
|
-
const uint32_t size = runtime.callI32("flatsql_response_artifact_size");
|
|
612
|
-
body.bytes(runtime.readBytes(ptr, size));
|
|
613
|
-
break;
|
|
614
|
-
}
|
|
615
|
-
case OP_REGISTER_QUERY_TEMPLATE: {
|
|
616
|
-
const uint32_t handle = reader.u32();
|
|
617
|
-
const std::string queryId = reader.string();
|
|
618
|
-
const std::string sql = reader.string();
|
|
619
|
-
const uint8_t cacheable = reader.u8();
|
|
620
|
-
reader.finish();
|
|
621
|
-
const uint32_t success = runtime.withString(queryId, [&](uint32_t queryIdPtr) {
|
|
622
|
-
return runtime.withString(sql, [&](uint32_t sqlPtr) {
|
|
623
|
-
return runtime.callI32("flatsql_register_query_template", {
|
|
624
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
625
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(queryIdPtr)),
|
|
626
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(sqlPtr)),
|
|
627
|
-
WasmEdge_ValueGenI32(cacheable != 0 ? 1 : 0),
|
|
628
|
-
});
|
|
629
|
-
});
|
|
630
|
-
});
|
|
631
|
-
checkFlatSQL(runtime, success);
|
|
632
|
-
break;
|
|
633
|
-
}
|
|
634
|
-
case OP_QUERY_TEMPLATE: {
|
|
635
|
-
const uint32_t handle = reader.u32();
|
|
636
|
-
const std::string queryId = reader.string();
|
|
637
|
-
const uint32_t paramCount = reader.u32();
|
|
638
|
-
const std::vector<uint8_t> params = reader.byteVector();
|
|
639
|
-
reader.finish();
|
|
640
|
-
const uint32_t success = runtime.withString(queryId, [&](uint32_t queryIdPtr) {
|
|
641
|
-
return runtime.withBytes(params, [&](uint32_t paramsPtr) {
|
|
642
|
-
return runtime.callI32("flatsql_query_template", {
|
|
643
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
644
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(queryIdPtr)),
|
|
645
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(paramsPtr)),
|
|
646
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(params.size())),
|
|
647
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(paramCount)),
|
|
648
|
-
});
|
|
649
|
-
});
|
|
650
|
-
});
|
|
651
|
-
checkFlatSQL(runtime, success);
|
|
652
|
-
encodeQueryResult(runtime, body);
|
|
653
|
-
break;
|
|
654
|
-
}
|
|
655
|
-
case OP_CLEAR_QUERY_CACHE: {
|
|
656
|
-
const uint32_t handle = reader.u32();
|
|
657
|
-
reader.finish();
|
|
658
|
-
runtime.callVoid("flatsql_clear_query_cache", {WasmEdge_ValueGenI32(static_cast<int32_t>(handle))});
|
|
659
|
-
break;
|
|
660
|
-
}
|
|
661
|
-
case OP_CONFIGURE_QUERY_CACHE: {
|
|
662
|
-
const uint32_t handle = reader.u32();
|
|
663
|
-
const uint32_t maxEntries = reader.u32();
|
|
664
|
-
const uint32_t maxRows = reader.u32();
|
|
665
|
-
reader.finish();
|
|
666
|
-
const uint32_t success = runtime.callI32("flatsql_configure_query_cache", {
|
|
667
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
668
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(maxEntries)),
|
|
669
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(maxRows)),
|
|
670
|
-
});
|
|
671
|
-
checkFlatSQL(runtime, success);
|
|
672
|
-
break;
|
|
673
|
-
}
|
|
674
|
-
case OP_QUERY_CACHE_STATS: {
|
|
675
|
-
const uint32_t handle = reader.u32();
|
|
676
|
-
reader.finish();
|
|
677
|
-
body.f64(runtime.callF64("flatsql_query_cache_hits", {WasmEdge_ValueGenI32(static_cast<int32_t>(handle))}));
|
|
678
|
-
body.f64(runtime.callF64("flatsql_query_cache_misses", {WasmEdge_ValueGenI32(static_cast<int32_t>(handle))}));
|
|
679
|
-
body.f64(runtime.callF64("flatsql_query_cache_size", {WasmEdge_ValueGenI32(static_cast<int32_t>(handle))}));
|
|
680
|
-
body.f64(runtime.callF64("flatsql_query_cache_generation", {WasmEdge_ValueGenI32(static_cast<int32_t>(handle))}));
|
|
681
|
-
body.f64(runtime.callF64("flatsql_query_cache_max_entries", {WasmEdge_ValueGenI32(static_cast<int32_t>(handle))}));
|
|
682
|
-
body.f64(runtime.callF64("flatsql_query_cache_max_rows", {WasmEdge_ValueGenI32(static_cast<int32_t>(handle))}));
|
|
683
|
-
break;
|
|
684
|
-
}
|
|
685
|
-
case OP_GET_FLATBUFFER_BY_INDEX: {
|
|
686
|
-
const uint32_t handle = reader.u32();
|
|
687
|
-
const std::string table = reader.string();
|
|
688
|
-
const std::string index = reader.string();
|
|
689
|
-
const uint32_t paramCount = reader.u32();
|
|
690
|
-
const std::vector<uint8_t> params = reader.byteVector();
|
|
691
|
-
reader.finish();
|
|
692
|
-
const uint32_t ptr = runtime.withString(table, [&](uint32_t tablePtr) {
|
|
693
|
-
return runtime.withString(index, [&](uint32_t indexPtr) {
|
|
694
|
-
return runtime.withBytes(params, [&](uint32_t paramsPtr) {
|
|
695
|
-
return runtime.callI32("flatsql_get_flatbuffer_by_index", {
|
|
696
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
697
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(tablePtr)),
|
|
698
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(indexPtr)),
|
|
699
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(paramsPtr)),
|
|
700
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(params.size())),
|
|
701
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(paramCount)),
|
|
702
|
-
});
|
|
703
|
-
});
|
|
704
|
-
});
|
|
705
|
-
});
|
|
706
|
-
const uint32_t size = runtime.callI32("flatsql_get_raw_flatbuffer_size");
|
|
707
|
-
if (ptr == 0 || size == 0) {
|
|
708
|
-
body.u8(0);
|
|
709
|
-
} else {
|
|
710
|
-
body.u8(1);
|
|
711
|
-
body.bytes(runtime.readBytes(ptr, size));
|
|
712
|
-
}
|
|
713
|
-
break;
|
|
714
|
-
}
|
|
715
|
-
case OP_EXPORT_DATA: {
|
|
716
|
-
const uint32_t handle = reader.u32();
|
|
717
|
-
reader.finish();
|
|
718
|
-
const uint32_t ptr = runtime.callI32("flatsql_export_data", {
|
|
719
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
720
|
-
});
|
|
721
|
-
const uint32_t size = runtime.callI32("flatsql_export_size");
|
|
722
|
-
body.bytes(runtime.readBytes(ptr, size));
|
|
723
|
-
break;
|
|
724
|
-
}
|
|
725
|
-
case OP_LOAD_AND_REBUILD: {
|
|
726
|
-
const uint32_t handle = reader.u32();
|
|
727
|
-
const std::vector<uint8_t> data = reader.byteVector();
|
|
728
|
-
reader.finish();
|
|
729
|
-
runtime.withBytes(data, [&](uint32_t dataPtr) {
|
|
730
|
-
runtime.callVoid("flatsql_load_and_rebuild", {
|
|
731
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
732
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(dataPtr)),
|
|
733
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(data.size())),
|
|
734
|
-
});
|
|
735
|
-
});
|
|
736
|
-
break;
|
|
737
|
-
}
|
|
738
|
-
case OP_RESERVE_STORAGE: {
|
|
739
|
-
const uint32_t handle = reader.u32();
|
|
740
|
-
const uint32_t bytes = reader.u32();
|
|
741
|
-
reader.finish();
|
|
742
|
-
runtime.callVoid("flatsql_reserve_storage", {
|
|
743
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
744
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(bytes)),
|
|
745
|
-
});
|
|
746
|
-
break;
|
|
747
|
-
}
|
|
748
|
-
case OP_LOAD_FROM_DB: {
|
|
749
|
-
const uint32_t handle = reader.u32();
|
|
750
|
-
const uint32_t sourceHandle = reader.u32();
|
|
751
|
-
reader.finish();
|
|
752
|
-
runtime.callVoid("flatsql_load_from_db", {
|
|
753
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(handle)),
|
|
754
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(sourceHandle)),
|
|
755
|
-
});
|
|
756
|
-
break;
|
|
757
|
-
}
|
|
758
|
-
case OP_BUILD_RESPONSE_ARTIFACT_CACHE_KEY: {
|
|
759
|
-
const std::string schemaName = reader.string();
|
|
760
|
-
const std::string schemaVersion = reader.string();
|
|
761
|
-
const std::string sql = reader.string();
|
|
762
|
-
const std::string format = reader.string();
|
|
763
|
-
const std::string publishEventKey = reader.string();
|
|
764
|
-
const uint32_t projectionCount = reader.u32();
|
|
765
|
-
std::vector<std::string> projection;
|
|
766
|
-
projection.reserve(projectionCount);
|
|
767
|
-
for (uint32_t index = 0; index < projectionCount; index++) {
|
|
768
|
-
projection.push_back(reader.string());
|
|
769
|
-
}
|
|
770
|
-
const uint32_t paramCount = reader.u32();
|
|
771
|
-
const std::vector<uint8_t> params = reader.byteVector();
|
|
772
|
-
reader.finish();
|
|
773
|
-
|
|
774
|
-
const std::string projectionList = joinStrings(projection, '\n');
|
|
775
|
-
const uint32_t keyPtr = runtime.withString(schemaName, [&](uint32_t schemaNamePtr) {
|
|
776
|
-
return runtime.withString(schemaVersion, [&](uint32_t schemaVersionPtr) {
|
|
777
|
-
return runtime.withString(sql, [&](uint32_t sqlPtr) {
|
|
778
|
-
return runtime.withString(format, [&](uint32_t formatPtr) {
|
|
779
|
-
return runtime.withString(publishEventKey, [&](uint32_t publishEventKeyPtr) {
|
|
780
|
-
return runtime.withString(projectionList, [&](uint32_t projectionListPtr) {
|
|
781
|
-
return runtime.withBytes(params, [&](uint32_t paramsPtr) {
|
|
782
|
-
return runtime.callI32("flatsql_build_response_artifact_cache_key", {
|
|
783
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(schemaNamePtr)),
|
|
784
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(schemaVersionPtr)),
|
|
785
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(sqlPtr)),
|
|
786
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(formatPtr)),
|
|
787
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(publishEventKeyPtr)),
|
|
788
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(projectionListPtr)),
|
|
789
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(paramsPtr)),
|
|
790
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(params.size())),
|
|
791
|
-
WasmEdge_ValueGenI32(static_cast<int32_t>(paramCount)),
|
|
792
|
-
});
|
|
793
|
-
});
|
|
794
|
-
});
|
|
795
|
-
});
|
|
796
|
-
});
|
|
797
|
-
});
|
|
798
|
-
});
|
|
799
|
-
});
|
|
800
|
-
if (keyPtr == 0) {
|
|
801
|
-
std::string error = runtime.lastError();
|
|
802
|
-
if (error.empty()) {
|
|
803
|
-
error = "failed to build response artifact cache key";
|
|
804
|
-
}
|
|
805
|
-
throw std::runtime_error(error);
|
|
806
|
-
}
|
|
807
|
-
body.string(runtime.readCString(keyPtr));
|
|
808
|
-
break;
|
|
809
|
-
}
|
|
810
|
-
default:
|
|
811
|
-
throw std::runtime_error("unknown runner opcode");
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
return makeOkFrame(body);
|
|
815
|
-
}
|
|
816
|
-
|
|
817
|
-
} // namespace
|
|
818
|
-
|
|
819
|
-
int main(int argc, char** argv) {
|
|
820
|
-
if (argc != 2) {
|
|
821
|
-
std::cerr << "usage: " << argv[0] << " <flatsql-wasi.wasm>\n";
|
|
822
|
-
return 64;
|
|
823
|
-
}
|
|
824
|
-
|
|
825
|
-
try {
|
|
826
|
-
g_protocolFd = ::dup(STDOUT_FILENO);
|
|
827
|
-
if (g_protocolFd < 0) {
|
|
828
|
-
throw std::runtime_error("failed to duplicate runner stdout");
|
|
829
|
-
}
|
|
830
|
-
WasmEdge_LogOff();
|
|
831
|
-
|
|
832
|
-
WasmEdgeRuntime runtime(argv[1]);
|
|
833
|
-
|
|
834
|
-
while (true) {
|
|
835
|
-
bool ok = false;
|
|
836
|
-
const uint32_t length = readU32FromStream(std::cin, ok);
|
|
837
|
-
if (!ok) {
|
|
838
|
-
break;
|
|
839
|
-
}
|
|
840
|
-
|
|
841
|
-
std::vector<uint8_t> request(length);
|
|
842
|
-
if (!readExact(std::cin, request.data(), request.size())) {
|
|
843
|
-
break;
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
try {
|
|
847
|
-
writeFrame(handleRequest(runtime, request));
|
|
848
|
-
} catch (const std::exception& error) {
|
|
849
|
-
writeFrame(makeErrorFrame(error.what()));
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
return 0;
|
|
854
|
-
} catch (const std::exception& error) {
|
|
855
|
-
std::cerr << "flatsql-wasmedge-runner: " << error.what() << "\n";
|
|
856
|
-
return 1;
|
|
857
|
-
}
|
|
858
|
-
}
|