Search in sources :

Example 1 with UnpackedObject

use of io.zeebe.msgpack.UnpackedObject in project zeebe by zeebe-io.

the class ClientApiMessageHandler method handleExecuteCommandRequest.

private boolean handleExecuteCommandRequest(final ServerOutput output, final RemoteAddress requestAddress, final long requestId, final BrokerEventMetadata eventMetadata, final DirectBuffer buffer, final int messageOffset, final int messageLength) {
    executeCommandRequestDecoder.wrap(buffer, messageOffset + messageHeaderDecoder.encodedLength(), messageHeaderDecoder.blockLength(), messageHeaderDecoder.version());
    final int partitionId = executeCommandRequestDecoder.partitionId();
    final long key = executeCommandRequestDecoder.key();
    final LogStream logStream = logStreams.get(partitionId);
    if (logStream == null) {
        return errorResponseWriter.errorCode(ErrorCode.PARTITION_NOT_FOUND).errorMessage("Cannot execute command. Partition with id '%d' not found", partitionId).tryWriteResponseOrLogFailure(output, requestAddress.getStreamId(), requestId);
    }
    final EventType eventType = executeCommandRequestDecoder.eventType();
    final UnpackedObject event = eventsByType.get(eventType);
    if (event == null) {
        return errorResponseWriter.errorCode(ErrorCode.MESSAGE_NOT_SUPPORTED).errorMessage("Cannot execute command. Invalid event type '%s'.", eventType.name()).tryWriteResponseOrLogFailure(output, requestAddress.getStreamId(), requestId);
    }
    final int eventOffset = executeCommandRequestDecoder.limit() + ExecuteCommandRequestDecoder.commandHeaderLength();
    final int eventLength = executeCommandRequestDecoder.commandLength();
    event.reset();
    try {
        // verify that the event / command is valid
        event.wrap(buffer, eventOffset, eventLength);
    } catch (Throwable t) {
        return errorResponseWriter.errorCode(ErrorCode.INVALID_MESSAGE).errorMessage("Cannot deserialize command: '%s'.", concatErrorMessages(t)).tryWriteResponseOrLogFailure(output, requestAddress.getStreamId(), requestId);
    }
    eventMetadata.eventType(eventType);
    logStreamWriter.wrap(logStream);
    if (key != ExecuteCommandRequestDecoder.keyNullValue()) {
        logStreamWriter.key(key);
    } else {
        logStreamWriter.positionAsKey();
    }
    final long eventPosition = logStreamWriter.metadataWriter(eventMetadata).value(buffer, eventOffset, eventLength).tryWrite();
    return eventPosition >= 0;
}
Also used : LogStream(io.zeebe.logstreams.log.LogStream) UnpackedObject(io.zeebe.msgpack.UnpackedObject)

Example 2 with UnpackedObject

use of io.zeebe.msgpack.UnpackedObject in project zeebe by zeebe-io.

the class TypedStreamReaderImpl method readValue.

@Override
@SuppressWarnings({ "unchecked" })
public <T extends UnpackedObject> TypedEvent<T> readValue(long position, Class<T> eventClass) {
    final boolean success = reader.seek(position);
    if (!success) {
        throw new RuntimeException("Could not find an event at position " + position);
    }
    final LoggedEvent rawEvent = reader.next();
    metadata.reset();
    rawEvent.readMetadata(metadata);
    final UnpackedObject value = eventCache.get(eventClass);
    value.reset();
    rawEvent.readValue(value);
    event.wrap(rawEvent, metadata, value);
    return event;
}
Also used : LoggedEvent(io.zeebe.logstreams.log.LoggedEvent) UnpackedObject(io.zeebe.msgpack.UnpackedObject)

Example 3 with UnpackedObject

use of io.zeebe.msgpack.UnpackedObject in project zeebe by zeebe-io.

the class TypedStreamProcessor method onEvent.

@Override
public EventProcessor onEvent(LoggedEvent event) {
    metadata.reset();
    event.readMetadata(metadata);
    final EnumMap processorsForType = eventProcessors.get(metadata.getEventType());
    if (processorsForType == null || processorsForType.isEmpty()) {
        return null;
    }
    final UnpackedObject value = eventCache.get(metadata.getEventType());
    value.reset();
    event.readValue(value);
    final Enum state = getEventState(value);
    final TypedEventProcessor currentProcessor = (TypedEventProcessor) processorsForType.get(state);
    if (currentProcessor != null) {
        typedEvent.wrap(event, metadata, value);
        eventProcessorWrapper.wrap(currentProcessor, typedEvent);
        return eventProcessorWrapper;
    } else {
        return null;
    }
}
Also used : EnumMap(java.util.EnumMap) UnpackedObject(io.zeebe.msgpack.UnpackedObject)

Aggregations

UnpackedObject (io.zeebe.msgpack.UnpackedObject)3 LogStream (io.zeebe.logstreams.log.LogStream)1 LoggedEvent (io.zeebe.logstreams.log.LoggedEvent)1 EnumMap (java.util.EnumMap)1