use of com.google.flatbuffers.FlatBufferBuilder in project deephaven-core by deephaven.
the class FlightMessageRoundTripTest method testDoExchangeSnapshot.
@Test
public void testDoExchangeSnapshot() throws Exception {
final String staticTableName = "flightInfoTest";
final Table table = TableTools.emptyTable(10).update("I = i", "J = i + 0.01");
try (final SafeCloseable ignored = LivenessScopeStack.open(scriptSession, false)) {
// stuff table into the scope
scriptSession.setVariable(staticTableName, table);
// build up a snapshot request
// equivalent to '0x6E687064' (ASCII "dphn")
byte[] magic = new byte[] { 100, 112, 104, 110 };
FlightDescriptor fd = FlightDescriptor.command(magic);
try (FlightClient.ExchangeReaderWriter erw = client.doExchange(fd);
final RootAllocator allocator = new RootAllocator(Integer.MAX_VALUE)) {
final FlatBufferBuilder metadata = new FlatBufferBuilder();
int optOffset = BarrageSnapshotOptions.createBarrageSnapshotOptions(metadata, ColumnConversionMode.Stringify, false, 1000);
final int ticOffset = BarrageSnapshotRequest.createTicketVector(metadata, ScopeTicketHelper.nameToBytes(staticTableName));
BarrageSnapshotRequest.startBarrageSnapshotRequest(metadata);
BarrageSnapshotRequest.addColumns(metadata, 0);
BarrageSnapshotRequest.addViewport(metadata, 0);
BarrageSnapshotRequest.addSnapshotOptions(metadata, optOffset);
BarrageSnapshotRequest.addTicket(metadata, ticOffset);
metadata.finish(BarrageSnapshotRequest.endBarrageSnapshotRequest(metadata));
final FlatBufferBuilder wrapper = new FlatBufferBuilder();
final int innerOffset = wrapper.createByteVector(metadata.dataBuffer());
wrapper.finish(BarrageMessageWrapper.createBarrageMessageWrapper(wrapper, // the numerical representation of the ASCII "dphn".
0x6E687064, BarrageMessageType.BarrageSnapshotRequest, innerOffset));
// extract the bytes and package them in an ArrowBuf for transmission
byte[] msg = wrapper.sizedByteArray();
ArrowBuf data = allocator.buffer(msg.length);
data.writeBytes(msg);
erw.getWriter().putMetadata(data);
erw.getWriter().completed();
// read everything from the server (expecting schema message and one data message)
int numMessages = 0;
while (erw.getReader().next()) {
++numMessages;
}
// only one data message
assertEquals(1, numMessages);
// at this point should have the data, verify it matches the created table
assertEquals(erw.getReader().getRoot().getRowCount(), table.size());
// check the values against the source table
org.apache.arrow.vector.IntVector iv = (org.apache.arrow.vector.IntVector) erw.getReader().getRoot().getVector(0);
for (int i = 0; i < table.size(); i++) {
assertEquals("int match:", table.getColumn(0).get(i), iv.get(i));
}
org.apache.arrow.vector.Float8Vector dv = (org.apache.arrow.vector.Float8Vector) erw.getReader().getRoot().getVector(1);
for (int i = 0; i < table.size(); i++) {
assertEquals("double match: ", table.getColumn(1).get(i), dv.get(i));
}
}
}
}
use of com.google.flatbuffers.FlatBufferBuilder in project deephaven-core by deephaven.
the class SchemaBytesImpl method toSchemaBytes.
@Override
public byte[] toSchemaBytes(TableHeader header) {
final Schema schema = SchemaAdapter.of(header);
final FlatBufferBuilder builder = new FlatBufferBuilder();
final int schemaOffset = schema.getSchema(builder);
final int messageOffset = MessageHelper.wrapInMessage(builder, schemaOffset, MessageHeader.Schema);
builder.finish(messageOffset);
return MessageHelper.toIpcBytes(builder);
}
use of com.google.flatbuffers.FlatBufferBuilder in project apisix-java-plugin-runner by apache.
the class A6ConfigHandlerTest method testAddFilter1.
@Test
@DisplayName("test add filter by prepare conf")
void testAddFilter1() {
FlatBufferBuilder builder = new FlatBufferBuilder();
int name = builder.createString("FooFilter");
int value = builder.createString("Bar");
int conf = TextEntry.createTextEntry(builder, name, value);
int confVector = Req.createConfVector(builder, new int[] { conf });
Req.startReq(builder);
Req.addConf(builder, confVector);
builder.finish(Req.endReq(builder));
Req req = Req.getRootAsReq(builder.dataBuffer());
A6ConfigRequest request = new A6ConfigRequest(req);
EmbeddedChannel channel = new EmbeddedChannel(new BinaryProtocolDecoder(), prepareConfHandler);
channel.writeInbound(request);
channel.finish();
A6ConfigResponse response = channel.readOutbound();
A6Conf config = cache.getIfPresent(response.getConfToken());
Assertions.assertNotNull(config.getChain());
Assertions.assertEquals(config.getChain().getFilters().size(), 1);
Assertions.assertEquals(config.getChain().getIndex(), 0);
Assertions.assertEquals(config.get("FooFilter"), "Bar");
}
use of com.google.flatbuffers.FlatBufferBuilder in project apisix-java-plugin-runner by apache.
the class A6ConfigHandlerTest method testAddFilter4.
@Test
@DisplayName("test receive undefined filter")
void testAddFilter4() {
FlatBufferBuilder builder = new FlatBufferBuilder();
int foo = builder.createString("UndefinedFilter");
int bar = builder.createString("Bar");
int filter = TextEntry.createTextEntry(builder, foo, bar);
int confVector = Req.createConfVector(builder, new int[] { filter });
Req.startReq(builder);
Req.addConf(builder, confVector);
builder.finish(Req.endReq(builder));
Req req = Req.getRootAsReq(builder.dataBuffer());
A6ConfigRequest request = new A6ConfigRequest(req);
EmbeddedChannel channel = new EmbeddedChannel(new BinaryProtocolDecoder(), prepareConfHandler);
channel.writeInbound(request);
channel.finish();
A6ConfigResponse response = channel.readOutbound();
A6Conf config = cache.getIfPresent(response.getConfToken());
Assertions.assertEquals(config.getChain().getFilters().size(), 0);
}
use of com.google.flatbuffers.FlatBufferBuilder in project apisix-java-plugin-runner by apache.
the class A6ConfigHandlerTest method testAddFilter5.
@Test
@DisplayName("test fetch conf more times")
void testAddFilter5() {
FlatBufferBuilder builder = new FlatBufferBuilder();
int name = builder.createString("FooFilter");
int value = builder.createString("Bar");
int conf = TextEntry.createTextEntry(builder, name, value);
int confVector = Req.createConfVector(builder, new int[] { conf });
Req.startReq(builder);
Req.addConf(builder, confVector);
builder.finish(Req.endReq(builder));
Req req = Req.getRootAsReq(builder.dataBuffer());
A6ConfigRequest request = new A6ConfigRequest(req);
EmbeddedChannel channel = new EmbeddedChannel(new BinaryProtocolDecoder(), prepareConfHandler);
channel.writeInbound(request);
channel.finish();
A6ConfigResponse response = channel.readOutbound();
A6Conf a6Conf = cache.getIfPresent(response.getConfToken());
Assertions.assertTrue(a6Conf.getConfig() instanceof HashMap);
for (int i = 0; i < 100; i++) {
Assertions.assertEquals(a6Conf.get("FooFilter"), "Bar");
}
}
Aggregations