-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrap.cpp
221 lines (172 loc) · 6.12 KB
/
wrap.cpp
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
#include <arrow/buffer.h>
#include <arrow/io/type_fwd.h>
#include <arrow/io/memory.h>
#include <arrow/ipc/reader.h>
#include <arrow/ipc/writer.h>
#include <arrow/record_batch.h>
#include <arrow/table.h>
#include <arrow/status.h>
#include <arrow/type_fwd.h>
#include <arrow/util/string_view.h>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <iostream>
#include <jlcxx/array.hpp>
#include <julia.h>
#include <memory>
#include <string>
#include <vector>
#include "jlcxx/jlcxx.hpp"
#include "jlcxx/const_array.hpp"
#include "jlcxx/stl.hpp"
#include "deephaven/client/client.h"
#include "deephaven/client/utility/utility.h"
using deephaven::client::Client;
using deephaven::client::TableHandleManager;
using deephaven::client::TableHandle;
using deephaven::client::internal::TableHandleStreamAdaptor;
using deephaven::client::utility::okOrThrow;
std::string greet() {
return "hello, world!";
}
int connect_to_server() {
const char *server = "localhost:10000";
try {
auto client = Client::connect(server);
auto manager = client.getManager();
auto table = manager.emptyTable(10);
auto t2 = table.update("ABC = ii + 100");
std::cout << t2.stream(true) << '\n';
} catch (const std::exception &e) {
std::cerr << "Caught exception: " << e.what() << '\n';
}
return 0;
}
std::string showTable(const TableHandle &table, bool wantHeaders) {
std::ostringstream s;
s << table.stream(wantHeaders);
return s.str();
}
class ChunkInterface {
public:
ChunkInterface(const TableHandle &tableHandle) :
reader(tableHandle.getFlightStreamReader())
{
Next();
}
void Next() {
okOrThrow(DEEPHAVEN_EXPR_MSG(reader->Next(&chunk)));
}
jlcxx::ConstArray< int64_t, 1 > columnAsInt64(int col) {
auto column = chunk.data->column(col);
auto arr_int64 = std::dynamic_pointer_cast<arrow::NumericArray<arrow::Int64Type>>(column);
if (arr_int64 != nullptr) {
if (arr_int64->null_count() != 0) {
std::cout << "TODO: NULLS";
}
return jlcxx::make_const_array(arr_int64->raw_values(), arr_int64->length());
} else {
return nullptr;
}
}
jlcxx::ConstArray< int32_t, 1 > columnAsInt32(int col) {
auto column = chunk.data->column(col);
auto arr_int32 = std::dynamic_pointer_cast<arrow::NumericArray<arrow::Int32Type>>(column);
if (arr_int32 != nullptr) {
if (arr_int32->null_count() != 0) {
std::cout << "TODO: NULLS";
}
return jlcxx::make_const_array(arr_int32->raw_values(), arr_int32->length());
} else {
return nullptr;
}
}
private:
std::shared_ptr<arrow::flight::FlightStreamReader> reader;
arrow::flight::FlightStreamChunk chunk;
};
std::shared_ptr<arrow::Table> deserializeTable(jlcxx::ArrayRef<uint8_t, 1> data) {
std::vector<unsigned char> d2;
for (int8_t elem : data) {
d2.push_back(elem);
}
auto buf_reader = arrow::io::BufferReader(d2.data(), d2.size());
auto str_reader = arrow::ipc::RecordBatchStreamReader::Open(&buf_reader).ValueOrDie();
std::shared_ptr<arrow::Table> tbl = nullptr;
okOrThrow(DEEPHAVEN_EXPR_MSG(str_reader->ReadAll(&tbl)));
return tbl;
}
TableHandle uploadTable(const TableHandleManager &manager, arrow::Table &table) {
auto wrapper = manager.createFlightWrapper();
auto [result, fd] = manager.newTableHandleAndFlightDescriptor();
arrow::flight::FlightCallOptions options;
wrapper.addAuthHeaders(&options);
std::unique_ptr<arrow::flight::FlightStreamWriter> fsw;
std::unique_ptr<arrow::flight::FlightMetadataReader> fmr;
okOrThrow(DEEPHAVEN_EXPR_MSG(wrapper.flightClient()->DoPut(options, fd, table.schema(), &fsw, &fmr)));
okOrThrow(DEEPHAVEN_EXPR_MSG(fsw->WriteTable(table)));
okOrThrow(DEEPHAVEN_EXPR_MSG(fsw->DoneWriting()));
std::shared_ptr<arrow::Buffer> buf;
okOrThrow(DEEPHAVEN_EXPR_MSG(fmr->ReadMetadata(&buf)));
okOrThrow(DEEPHAVEN_EXPR_MSG(fsw->Close()));
return result;
}
TableHandle deserializeAndUpload(const TableHandleManager &manager, jlcxx::ArrayRef<uint8_t, 1> data) {
auto tbl = deserializeTable(data);
return uploadTable(manager, *tbl);
}
class TableSerializer {
public:
TableSerializer(const TableHandle &tableHandle) {
auto s = arrow::io::BufferOutputStream::Create();
auto stream = s.ValueOrDie();
auto fsr = tableHandle.getFlightStreamReader();
auto schema = fsr->GetSchema().ValueOrDie();
auto writer = arrow::ipc::MakeStreamWriter(stream, schema).ValueOrDie();
while (true) {
arrow::flight::FlightStreamChunk chunk;
okOrThrow(DEEPHAVEN_EXPR_MSG(fsr->Next(&chunk)));
if (chunk.data == nullptr) {
break;
}
okOrThrow(DEEPHAVEN_EXPR_MSG(writer->WriteRecordBatch(*chunk.data)));
}
okOrThrow(DEEPHAVEN_EXPR_MSG(writer->Close()));
result = stream->Finish().ValueOrDie();
}
jlcxx::ConstArray<uint8_t, 1> DataArr() {
return jlcxx::make_const_array(this->Data(), this->DataLen());
}
const uint8_t *Data() {
return result->data();
}
size_t DataLen() {
return result->size();
}
private:
std::shared_ptr<arrow::Buffer> result;
};
JLCXX_MODULE define_julia_module(jlcxx::Module &mod) {
mod.add_type<TableHandle>("TableHandle")
.method("update", static_cast<TableHandle (TableHandle::*)(std::vector<std::string>) const>(&TableHandle::update))
.method("show", &showTable);
mod.add_type<TableHandleManager>("TableHandleManager")
.method("emptyTable", &TableHandleManager::emptyTable);
mod.add_type<Client>("Client")
.method("getManager", &Client::getManager);
mod.add_type<ChunkInterface>("ChunkInterface")
.constructor<const TableHandle&>(true)
.method("Next", &ChunkInterface::Next)
.method("columnasint64", &ChunkInterface::columnAsInt64)
.method("columnasint32", &ChunkInterface::columnAsInt32);
mod.add_type<TableSerializer>("TableSerializer")
.constructor<const TableHandle&>(true)
.method("dataarr", &TableSerializer::DataArr);
/* .method("connect", &Client::connect);*/
mod.method("clientconnect", &Client::connect);
mod.method("deserializeandupload", &deserializeAndUpload);
mod.method("greet", &greet);
mod.method("connect_to_server", &connect_to_server);
//mod.method("dumpsymbolcolumn", &dumpSymbolColumn);
}