|
| 1 | +/* |
| 2 | + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under |
| 3 | + * one or more contributor license agreements. See the NOTICE file distributed |
| 4 | + * with this work for additional information regarding copyright ownership. |
| 5 | + * Licensed under the Zeebe Community License 1.1. You may not use this file |
| 6 | + * except in compliance with the Zeebe Community License 1.1. |
| 7 | + */ |
| 8 | +package io.camunda.zeebe.protocol.impl.record.value.distribution; |
| 9 | + |
| 10 | +import io.camunda.zeebe.msgpack.MsgpackException; |
| 11 | +import io.camunda.zeebe.msgpack.property.EnumProperty; |
| 12 | +import io.camunda.zeebe.msgpack.property.IntegerProperty; |
| 13 | +import io.camunda.zeebe.msgpack.property.ObjectProperty; |
| 14 | +import io.camunda.zeebe.msgpack.spec.MsgPackReader; |
| 15 | +import io.camunda.zeebe.msgpack.spec.MsgPackWriter; |
| 16 | +import io.camunda.zeebe.protocol.impl.record.UnifiedRecordValue; |
| 17 | +import io.camunda.zeebe.protocol.impl.record.value.deployment.DeploymentRecord; |
| 18 | +import io.camunda.zeebe.protocol.record.RecordValue; |
| 19 | +import io.camunda.zeebe.protocol.record.ValueType; |
| 20 | +import io.camunda.zeebe.protocol.record.value.CommandDistributionRecordValue; |
| 21 | +import java.util.EnumMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.function.Supplier; |
| 24 | +import org.agrona.concurrent.UnsafeBuffer; |
| 25 | + |
| 26 | +public final class CommandDistributionRecord extends UnifiedRecordValue |
| 27 | + implements CommandDistributionRecordValue { |
| 28 | + |
| 29 | + private static final Map<ValueType, Supplier<UnifiedRecordValue>> RECORDS_BY_TYPE = |
| 30 | + new EnumMap<>(ValueType.class); |
| 31 | + |
| 32 | + // You'll need to register any of the records value's that you want to distribute |
| 33 | + static { |
| 34 | + RECORDS_BY_TYPE.put(ValueType.DEPLOYMENT, DeploymentRecord::new); |
| 35 | + } |
| 36 | + |
| 37 | + private final IntegerProperty partitionIdProperty = new IntegerProperty("partitionId"); |
| 38 | + private final EnumProperty<ValueType> valueTypeProperty = |
| 39 | + new EnumProperty<>("valueType", ValueType.class); |
| 40 | + private final ObjectProperty<UnifiedRecordValue> commandValueProperty = |
| 41 | + new ObjectProperty<>("commandValue", new UnifiedRecordValue()); |
| 42 | + private final MsgPackWriter recordValueWriter = new MsgPackWriter(); |
| 43 | + private final MsgPackReader recordValueReader = new MsgPackReader(); |
| 44 | + |
| 45 | + public CommandDistributionRecord() { |
| 46 | + declareProperty(partitionIdProperty) |
| 47 | + .declareProperty(valueTypeProperty) |
| 48 | + .declareProperty(commandValueProperty); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public int getPartitionId() { |
| 53 | + return partitionIdProperty.getValue(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public ValueType getValueType() { |
| 58 | + return valueTypeProperty.getValue(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public RecordValue getCommandValue() { |
| 63 | + // fetch a concrete instance of the record value by type |
| 64 | + if (!valueTypeProperty.hasValue()) { |
| 65 | + throw new MsgpackException("Expected to read the value type property, but it's not yet set"); |
| 66 | + } |
| 67 | + final var valueType = getValueType(); |
| 68 | + final var concrecteRecordValueSupplier = RECORDS_BY_TYPE.get(valueType); |
| 69 | + if (concrecteRecordValueSupplier == null) { |
| 70 | + throw new IllegalStateException( |
| 71 | + "Expected to read the record value, but it's type `" |
| 72 | + + valueType.name() |
| 73 | + + "` is unknown. Please add it to RecordDistributionRecord.RECORDS_BY_TYPE"); |
| 74 | + } |
| 75 | + final var concreteRecordValue = concrecteRecordValueSupplier.get(); |
| 76 | + |
| 77 | + // write the record value property's content into a buffer |
| 78 | + final var storedRecordValue = commandValueProperty.getValue(); |
| 79 | + final var recordValueBuffer = new UnsafeBuffer(0, 0); |
| 80 | + final int encodedLength = storedRecordValue.getEncodedLength(); |
| 81 | + recordValueBuffer.wrap(new byte[encodedLength]); |
| 82 | + storedRecordValue.write(recordValueWriter.wrap(recordValueBuffer, 0)); |
| 83 | + |
| 84 | + // read the value back from the buffer into the concrete record value |
| 85 | + concreteRecordValue.wrap(recordValueBuffer); |
| 86 | + return concreteRecordValue; |
| 87 | + } |
| 88 | + |
| 89 | + public CommandDistributionRecord setValueType(final ValueType valueType) { |
| 90 | + valueTypeProperty.setValue(valueType); |
| 91 | + return this; |
| 92 | + } |
| 93 | + |
| 94 | + public CommandDistributionRecord setPartitionId(final int partitionId) { |
| 95 | + partitionIdProperty.setValue(partitionId); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + public CommandDistributionRecord setRecordValue(final UnifiedRecordValue recordValue) { |
| 100 | + // inspired by IndexedRecord.setValue |
| 101 | + final var valueBuffer = new UnsafeBuffer(0, 0); |
| 102 | + final int encodedLength = recordValue.getLength(); |
| 103 | + valueBuffer.wrap(new byte[encodedLength]); |
| 104 | + |
| 105 | + recordValue.write(valueBuffer, 0); |
| 106 | + commandValueProperty.getValue().read(recordValueReader.wrap(valueBuffer, 0, encodedLength)); |
| 107 | + return this; |
| 108 | + } |
| 109 | +} |
0 commit comments