belliottsmith commented on code in PR #4201: URL: https://github.com/apache/cassandra/pull/4201#discussion_r2164383992
########## src/java/org/apache/cassandra/service/accord/serializers/ReadDataSerializer.java: ########## @@ -0,0 +1,362 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.service.accord.serializers; + +import java.io.IOException; + +import accord.api.Data; +import accord.coordinate.ExecuteFlag.ExecuteFlags; +import accord.messages.ApplyThenWaitUntilApplied; +import accord.messages.Commit; +import accord.messages.ReadData; +import accord.messages.ReadData.CommitOrReadNack; +import accord.messages.ReadData.ReadOk; +import accord.messages.ReadData.ReadOkWithFutureEpoch; +import accord.messages.ReadData.ReadReply; +import accord.messages.ReadData.ReadType; +import accord.messages.ReadEphemeralTxnData; +import accord.messages.ReadTxnData; +import accord.messages.StableThenRead; +import accord.messages.WaitUntilApplied; +import accord.primitives.FullRoute; +import accord.primitives.PartialDeps; +import accord.primitives.PartialTxn; +import accord.primitives.Participants; +import accord.primitives.Ranges; +import accord.primitives.Timestamp; +import accord.primitives.TxnId; +import accord.primitives.Writes; +import accord.utils.UnhandledEnum; +import org.apache.cassandra.db.TypeSizes; +import org.apache.cassandra.io.VersionedSerializer; +import org.apache.cassandra.io.util.DataInputPlus; +import org.apache.cassandra.io.util.DataOutputPlus; +import org.apache.cassandra.service.accord.serializers.CommandSerializers.ExecuteAtSerializer; +import org.apache.cassandra.service.accord.txn.TxnData; +import org.apache.cassandra.utils.vint.VIntCoding; + +import static org.apache.cassandra.utils.NullableSerializer.deserializeNullable; +import static org.apache.cassandra.utils.NullableSerializer.serializeNullable; +import static org.apache.cassandra.utils.NullableSerializer.serializedNullableSize; + +public class ReadDataSerializer implements IVersionedSerializer<ReadData> +{ + public static final ReadDataSerializer request = new ReadDataSerializer(); + + private static final Commit.Kind[] KINDS = Commit.Kind.values(); + private static final ReadType[] TYPES = ReadType.values(); + private static final int HAS_TXN = 0x20; + private static final int HAS_EXECUTE_AT = 0x40; + private static final int HAS_EXECUTE_AT_EPOCH = 0x80; + + private static final int ATWUA_HAS_MIN_EPOCH = 0x1; + private static final int ATWUA_HAS_WRITES = 0x2; + + private static final int STR_HAS_DEPS = 0x1; + private static final int STR_HAS_FULL_ROUTE = 0x2; + + @Override + public void serialize(ReadData read, DataOutputPlus out, Version version) throws IOException + { + ReadType type = read.kind(); + boolean hasTxn = read.partialTxn() != null; + boolean hasExecuteAt = read.executeAt() != null; + boolean hasExecuteAtEpoch = (read.executeAt() != null ? read.executeAt() : read.txnId).epoch() != read.executeAtEpoch; + + out.writeUnsignedVInt32(type.ordinal() | (read.flags.bits() << 3) + | (hasTxn ? HAS_TXN : 0) + | (hasExecuteAt ? HAS_EXECUTE_AT : 0) + | (hasExecuteAtEpoch ? HAS_EXECUTE_AT_EPOCH : 0) + ); + CommandSerializers.txnId.serialize(read.txnId, out); + KeySerializers.participants.serialize(read.scope, out); + if (hasTxn) + CommandSerializers.partialTxn.serialize(read.partialTxn(), out, version); + if (hasExecuteAt) + ExecuteAtSerializer.serialize(read.txnId, read.executeAt(), out); + if (hasExecuteAtEpoch) + out.writeVInt(read.executeAtEpoch - (read.executeAt() != null ? read.executeAt() : read.txnId).epoch()); + + switch (type) + { + default: throw new UnhandledEnum(type); + case readTxnData: + { + break; + } + case waitUntilApplied: + { + out.writeVInt(((WaitUntilApplied)read).minEpoch() - read.txnId.epoch()); + break; + } + case readEphemeral: + { + ReadEphemeralTxnData re = (ReadEphemeralTxnData) read; + DepsSerializers.partialDeps.serialize(re.partialDeps(), out); + KeySerializers.fullRoute.serialize(re.route(), out); + break; + } + case stableThenRead: + { + StableThenRead str = (StableThenRead) read; + out.writeUnsignedVInt32(str.kind.ordinal()); + out.writeVInt(str.minEpoch - read.txnId.epoch()); + boolean hasDeps = str.partialDeps() != null; + boolean hasFullRoute = str.route != null; + out.writeUnsignedVInt32((hasDeps ? STR_HAS_DEPS : 0) + | (hasFullRoute? STR_HAS_FULL_ROUTE : 0)); + if (hasDeps) + DepsSerializers.partialDeps.serialize(str.partialDeps(), out); + if (hasFullRoute) + KeySerializers.fullRoute.serialize(str.route, out); + break; + } + case applyThenWaitUntilApplied: + { + ApplyThenWaitUntilApplied msg = (ApplyThenWaitUntilApplied) read; + boolean hasMinEpoch = msg.minEpoch() != read.txnId.epoch(); + boolean hasWrites = msg.writes != null; + out.writeUnsignedVInt32((hasMinEpoch ? ATWUA_HAS_MIN_EPOCH : 0) + | (hasWrites ? ATWUA_HAS_WRITES : 0)); + if (hasMinEpoch) + out.writeVInt(read.txnId.epoch() - msg.minEpoch()); + DepsSerializers.partialDeps.serialize(msg.deps, out); + KeySerializers.fullRoute.serialize(msg.route, out); + if (hasWrites) + CommandSerializers.writes.serialize(msg.writes, out, version); + break; + } + } + } + + @Override + public ReadData deserialize(DataInputPlus in, Version version) throws IOException + { + ReadType type; + ExecuteFlags flags; + boolean hasTxn, hasExecuteAt, hasExecuteAtEpoch; + { + int tmp = in.readUnsignedVInt32(); + type = TYPES[tmp & 0x7]; + flags = ExecuteFlags.get((tmp >>> 2) & 0x3); Review Comment: well spotted, thanks for the suggested fixes -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For additional commands, e-mail: pr-h...@cassandra.apache.org