Search in sources :

Example 1 with ExpandableDirectBufferOutputStream

use of org.agrona.io.ExpandableDirectBufferOutputStream in project zeebe by zeebe-io.

the class CommandRequestHandler method serialize.

protected void serialize(EventImpl event) {
    int offset = 0;
    headerEncoder.wrap(serializedCommand, offset).blockLength(encoder.sbeBlockLength()).schemaId(encoder.sbeSchemaId()).templateId(encoder.sbeTemplateId()).version(encoder.sbeSchemaVersion());
    offset += headerEncoder.encodedLength();
    encoder.wrap(serializedCommand, offset);
    final EventMetadata metadata = event.getMetadata();
    if (metadata.getKey() < 0) {
        encoder.key(ExecuteCommandRequestEncoder.keyNullValue());
    } else {
        encoder.key(metadata.getKey());
    }
    encoder.partitionId(metadata.getPartitionId()).eventType(EventTypeMapping.mapEventType(metadata.getType())).position(metadata.getPosition());
    offset = encoder.limit();
    final int commandHeaderOffset = offset;
    final int serializedCommandOffset = commandHeaderOffset + commandHeaderLength();
    final ExpandableDirectBufferOutputStream out = new ExpandableDirectBufferOutputStream(serializedCommand, serializedCommandOffset);
    try {
        objectMapper.writeValue(out, event);
    } catch (final Throwable e) {
        throw new RuntimeException("Failed to serialize command", e);
    }
    // can only write the header after we have written the command, as we don't know the length beforehand
    final short commandLength = (short) out.position();
    serializedCommand.putShort(commandHeaderOffset, commandLength, java.nio.ByteOrder.LITTLE_ENDIAN);
    serializedCommandLength = serializedCommandOffset + out.position();
}
Also used : ExpandableDirectBufferOutputStream(org.agrona.io.ExpandableDirectBufferOutputStream) EventMetadata(io.zeebe.client.event.EventMetadata)

Example 2 with ExpandableDirectBufferOutputStream

use of org.agrona.io.ExpandableDirectBufferOutputStream in project zeebe by zeebe-io.

the class ControlMessageRequestHandler method serialize.

protected void serialize(ControlMessageRequest message) {
    int offset = 0;
    headerEncoder.wrap(serializedMessage, offset).blockLength(encoder.sbeBlockLength()).schemaId(encoder.sbeSchemaId()).templateId(encoder.sbeTemplateId()).version(encoder.sbeSchemaVersion());
    offset += headerEncoder.encodedLength();
    encoder.wrap(serializedMessage, offset);
    encoder.messageType(message.getType());
    encoder.partitionId(message.getTargetPartition());
    offset = encoder.limit();
    final int messageHeaderOffset = offset;
    final int serializedMessageOffset = messageHeaderOffset + ControlMessageRequestEncoder.dataHeaderLength();
    final ExpandableDirectBufferOutputStream out = new ExpandableDirectBufferOutputStream(serializedMessage, serializedMessageOffset);
    try {
        objectMapper.writeValue(out, message.getRequest());
    } catch (final Throwable e) {
        throw new RuntimeException("Failed to serialize message", e);
    }
    // can only write the header after we have written the message, as we don't know the length beforehand
    final short commandLength = (short) out.position();
    serializedMessage.putShort(messageHeaderOffset, commandLength, java.nio.ByteOrder.LITTLE_ENDIAN);
    serializedMessageLength = serializedMessageOffset + out.position();
}
Also used : ExpandableDirectBufferOutputStream(org.agrona.io.ExpandableDirectBufferOutputStream)

Aggregations

ExpandableDirectBufferOutputStream (org.agrona.io.ExpandableDirectBufferOutputStream)2 EventMetadata (io.zeebe.client.event.EventMetadata)1