Search in sources :

Example 1 with WireMessage

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);
}
Also used : StatementHandle(org.apache.calcite.avatica.Meta.StatementHandle) Request(org.apache.calcite.avatica.remote.Service.Request) WireMessage(org.apache.calcite.avatica.proto.Common.WireMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Requests(org.apache.calcite.avatica.proto.Requests) Test(org.junit.Test)

Example 2 with WireMessage

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;
    }
}
Also used : ByteString(com.google.protobuf.ByteString) CodedInputStream(com.google.protobuf.CodedInputStream) WireMessage(org.apache.calcite.avatica.proto.Common.WireMessage) ByteString(com.google.protobuf.ByteString)

Example 3 with WireMessage

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);
}
Also used : UnsynchronizedBuffer(org.apache.calcite.avatica.util.UnsynchronizedBuffer) ByteString(com.google.protobuf.ByteString) WireMessage(org.apache.calcite.avatica.proto.Common.WireMessage)

Example 4 with WireMessage

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;
    }
}
Also used : ByteString(com.google.protobuf.ByteString) CodedInputStream(com.google.protobuf.CodedInputStream) WireMessage(org.apache.calcite.avatica.proto.Common.WireMessage) ByteString(com.google.protobuf.ByteString)

Aggregations

WireMessage (org.apache.calcite.avatica.proto.Common.WireMessage)4 ByteString (com.google.protobuf.ByteString)3 CodedInputStream (com.google.protobuf.CodedInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StatementHandle (org.apache.calcite.avatica.Meta.StatementHandle)1 Requests (org.apache.calcite.avatica.proto.Requests)1 Request (org.apache.calcite.avatica.remote.Service.Request)1 UnsynchronizedBuffer (org.apache.calcite.avatica.util.UnsynchronizedBuffer)1 Test (org.junit.Test)1