use of org.apache.calcite.avatica.proto.Common.WireMessage in project calcite-avatica by apache.
the class ProtobufSerializationTest method testExecuteSerialization.
@Test
public void testExecuteSerialization() throws Exception {
Service.ExecuteRequest executeRequest = new Service.ExecuteRequest(new StatementHandle("connection", 12345, getSignature()), getTypedValues(), 0);
Requests.ExecuteRequest pbExecuteRequest = executeRequest.serialize();
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
pbExecuteRequest.writeTo(baos);
byte[] serialized = baos.toByteArray();
baos.reset();
WireMessage wireMsg = WireMessage.newBuilder().setName(Requests.ExecuteRequest.class.getName()).setWrappedMessage(UnsafeByteOperations.unsafeWrap(serialized)).build();
wireMsg.writeTo(baos);
serialized = baos.toByteArray();
ProtobufTranslation translator = new ProtobufTranslationImpl();
Request newRequest = translator.parseRequest(serialized);
Assert.assertEquals(executeRequest, newRequest);
}
use of org.apache.calcite.avatica.proto.Common.WireMessage in project calcite-avatica by apache.
the class ProtobufTranslationImpl method parseRequest.
@Override
public Request parseRequest(byte[] bytes) throws IOException {
ByteString byteString = UnsafeByteOperations.unsafeWrap(bytes);
CodedInputStream inputStream = byteString.newCodedInput();
// Enable aliasing to avoid an extra copy to get at the serialized Request inside of the
// WireMessage.
inputStream.enableAliasing(true);
WireMessage wireMsg = WireMessage.parseFrom(inputStream);
String serializedMessageClassName = wireMsg.getName();
try {
RequestTranslator translator = getParserForRequest(serializedMessageClassName);
// The ByteString should be logical offsets into the original byte array
return translator.transform(wireMsg.getWrappedMessage());
} catch (RuntimeException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Failed to parse request message '{}'", TextFormat.shortDebugString(wireMsg));
}
throw e;
}
}
use of org.apache.calcite.avatica.proto.Common.WireMessage in project calcite-avatica by apache.
the class ProtobufTranslationImpl method serializeMessage.
void serializeMessage(OutputStream out, Message msg) throws IOException {
// Serialize the protobuf message
UnsynchronizedBuffer buffer = threadLocalBuffer.get();
ByteString serializedMsg;
try {
msg.writeTo(buffer);
// Make a bytestring from it
serializedMsg = UnsafeByteOperations.unsafeWrap(buffer.toArray());
} finally {
buffer.reset();
}
// Wrap the serialized message in a WireMessage
WireMessage wireMsg = WireMessage.newBuilder().setNameBytes(getClassNameBytes(msg.getClass())).setWrappedMessage(serializedMsg).build();
// Write the WireMessage to the provided OutputStream
wireMsg.writeTo(out);
}
use of org.apache.calcite.avatica.proto.Common.WireMessage in project calcite-avatica by apache.
the class ProtobufTranslationImpl method parseResponse.
@Override
public Response parseResponse(byte[] bytes) throws IOException {
ByteString byteString = UnsafeByteOperations.unsafeWrap(bytes);
CodedInputStream inputStream = byteString.newCodedInput();
// Enable aliasing to avoid an extra copy to get at the serialized Response inside of the
// WireMessage.
inputStream.enableAliasing(true);
WireMessage wireMsg = WireMessage.parseFrom(inputStream);
String serializedMessageClassName = wireMsg.getName();
try {
ResponseTranslator translator = getParserForResponse(serializedMessageClassName);
return translator.transform(wireMsg.getWrappedMessage());
} catch (RuntimeException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Failed to parse response message '{}'", TextFormat.shortDebugString(wireMsg));
}
throw e;
}
}
Aggregations