You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by GitBox <gi...@apache.org> on 2022/10/05 19:56:09 UTC

[GitHub] [cassandra] belliottsmith commented on a diff in pull request #1845: accord metadata persistence

belliottsmith commented on code in PR #1845:
URL: https://github.com/apache/cassandra/pull/1845#discussion_r985268509


##########
src/java/org/apache/cassandra/service/accord/serializers/CommandSerializers.java:
##########
@@ -0,0 +1,269 @@
+/*
+ * 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 com.google.common.base.Preconditions;
+
+import accord.local.Node;
+import accord.local.Status;
+import accord.primitives.Ballot;
+import accord.primitives.Deps;
+import accord.primitives.Keys;
+import accord.primitives.Timestamp;
+import accord.primitives.TxnId;
+import accord.txn.Txn;
+import accord.txn.Writes;
+import org.apache.cassandra.db.TypeSizes;
+import org.apache.cassandra.db.marshal.ValueAccessor;
+import org.apache.cassandra.io.IVersionedSerializer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+import org.apache.cassandra.service.accord.db.AccordQuery;
+import org.apache.cassandra.service.accord.db.AccordRead;
+import org.apache.cassandra.service.accord.db.AccordUpdate;
+import org.apache.cassandra.service.accord.db.AccordWrite;
+
+public class CommandSerializers
+{
+    private CommandSerializers() {}
+
+    public static final TimestampSerializer<TxnId> txnId = new TimestampSerializer<>(TxnId::new);
+    public static final TimestampSerializer<Timestamp> timestamp = new TimestampSerializer<>(Timestamp::new);
+    public static final TimestampSerializer<Ballot> ballot = new TimestampSerializer<>(Ballot::new);
+
+    public static class TimestampSerializer<T extends Timestamp> implements IVersionedSerializer<T>
+    {
+        interface Factory<T extends Timestamp>
+        {
+            T create(long epoch, long real, int logical, Node.Id node);
+        }
+
+        private final TimestampSerializer.Factory<T> factory;
+
+        private TimestampSerializer(TimestampSerializer.Factory<T> factory)
+        {
+            this.factory = factory;
+        }
+
+        @Override
+        public void serialize(T ts, DataOutputPlus out, int version) throws IOException
+        {
+            out.writeLong(ts.epoch);
+            out.writeLong(ts.real);
+            out.writeInt(ts.logical);
+            TopologySerializers.nodeId.serialize(ts.node, out, version);
+        }
+
+        public <V> int serialize(T ts, V dst, ValueAccessor<V> accessor, int offset)
+        {
+            int position = offset;
+            position += accessor.putLong(dst, position, ts.epoch);
+            position += accessor.putLong(dst, position, ts.real);
+            position += accessor.putInt(dst, position, ts.logical);
+            position += TopologySerializers.nodeId.serialize(ts.node, dst, accessor, position);
+            int size = position - offset;
+            Preconditions.checkState(size == serializedSize());
+            return size;
+        }
+
+        @Override
+        public T deserialize(DataInputPlus in, int version) throws IOException
+        {
+            return factory.create(in.readLong(),
+                                  in.readLong(),
+                                  in.readInt(),
+                                  TopologySerializers.nodeId.deserialize(in, version));
+        }
+
+        public <V> T deserialize(V src, ValueAccessor<V> accessor, int offset)
+        {
+            long epoch = accessor.getLong(src, offset);
+            offset += TypeSizes.LONG_SIZE;
+            long real = accessor.getLong(src, offset);
+            offset += TypeSizes.LONG_SIZE;
+            int logical = accessor.getInt(src, offset);
+            offset += TypeSizes.INT_SIZE;
+            Node.Id node = TopologySerializers.nodeId.deserialize(src, accessor, offset);
+            return factory.create(epoch, real, logical, node);
+        }
+
+        @Override
+        public long serializedSize(T ts, int version)
+        {
+            return serializedSize();
+        }
+
+        public int serializedSize()
+        {
+            return TypeSizes.LONG_SIZE +  // ts.epoch
+                   TypeSizes.LONG_SIZE +  // ts.real
+                   TypeSizes.INT_SIZE +   // ts.logical
+                   TopologySerializers.nodeId.serializedSize();   // ts.node
+        }
+    }
+
+    public static final IVersionedSerializer<Txn> txn = new IVersionedSerializer<Txn>()
+    {
+        @Override
+        public void serialize(Txn txn, DataOutputPlus out, int version) throws IOException
+        {
+            KeySerializers.keys.serialize(txn.keys(), out, version);
+            AccordRead.serializer.serialize((AccordRead) txn.read(), out, version);
+            AccordQuery.serializer.serialize((AccordQuery) txn.query(), out, version);
+            out.writeBoolean(txn.update() != null);
+            if (txn.update() != null)
+                AccordUpdate.serializer.serialize((AccordUpdate) txn.update(), out, version);
+
+        }
+
+        @Override
+        public Txn deserialize(DataInputPlus in, int version) throws IOException
+        {
+            Keys keys = KeySerializers.keys.deserialize(in, version);
+            AccordRead read = AccordRead.serializer.deserialize(in, version);
+            AccordQuery query = AccordQuery.serializer.deserialize(in, version);
+            if (in.readBoolean())
+                return new Txn.InMemory(keys, read, query, AccordUpdate.serializer.deserialize(in, version));
+            else
+                return new Txn.InMemory(keys, read, query);
+        }
+
+        @Override
+        public long serializedSize(Txn txn, int version)
+        {
+            long size = KeySerializers.keys.serializedSize(txn.keys(), version);
+            size += AccordRead.serializer.serializedSize((AccordRead) txn.read(), version);
+            size += AccordQuery.serializer.serializedSize((AccordQuery) txn.query(), version);
+            size += TypeSizes.sizeof(txn.update() != null);
+            if (txn.update() != null)
+                size += AccordUpdate.serializer.serializedSize((AccordUpdate) txn.update(), version);
+            return size;
+        }
+    };
+
+    public static final IVersionedSerializer<Status> status = new IVersionedSerializer<Status>()
+    {
+        @Override
+        public void serialize(Status status, DataOutputPlus out, int version) throws IOException
+        {
+            out.writeInt(status.ordinal());
+
+        }
+
+        @Override
+        public Status deserialize(DataInputPlus in, int version) throws IOException
+        {
+            return Status.values()[in.readInt()];
+        }
+
+        @Override
+        public long serializedSize(Status status, int version)
+        {
+            return TypeSizes.sizeof(status.ordinal());
+        }
+    };
+
+    public static final IVersionedSerializer<Deps> deps = new IVersionedSerializer<Deps>()
+    {
+        @Override
+        public void serialize(Deps deps, DataOutputPlus out, int version) throws IOException
+        {
+            Keys keys = deps.keys();
+            KeySerializers.keys.serialize(keys, out, version);
+
+            int txnIdCount = deps.txnIdCount();

Review Comment:
   vint?



##########
src/java/org/apache/cassandra/service/accord/serializers/CommandSerializers.java:
##########
@@ -0,0 +1,269 @@
+/*
+ * 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 com.google.common.base.Preconditions;
+
+import accord.local.Node;
+import accord.local.Status;
+import accord.primitives.Ballot;
+import accord.primitives.Deps;
+import accord.primitives.Keys;
+import accord.primitives.Timestamp;
+import accord.primitives.TxnId;
+import accord.txn.Txn;
+import accord.txn.Writes;
+import org.apache.cassandra.db.TypeSizes;
+import org.apache.cassandra.db.marshal.ValueAccessor;
+import org.apache.cassandra.io.IVersionedSerializer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+import org.apache.cassandra.service.accord.db.AccordQuery;
+import org.apache.cassandra.service.accord.db.AccordRead;
+import org.apache.cassandra.service.accord.db.AccordUpdate;
+import org.apache.cassandra.service.accord.db.AccordWrite;
+
+public class CommandSerializers
+{
+    private CommandSerializers() {}
+
+    public static final TimestampSerializer<TxnId> txnId = new TimestampSerializer<>(TxnId::new);
+    public static final TimestampSerializer<Timestamp> timestamp = new TimestampSerializer<>(Timestamp::new);
+    public static final TimestampSerializer<Ballot> ballot = new TimestampSerializer<>(Ballot::new);
+
+    public static class TimestampSerializer<T extends Timestamp> implements IVersionedSerializer<T>
+    {
+        interface Factory<T extends Timestamp>
+        {
+            T create(long epoch, long real, int logical, Node.Id node);
+        }
+
+        private final TimestampSerializer.Factory<T> factory;
+
+        private TimestampSerializer(TimestampSerializer.Factory<T> factory)
+        {
+            this.factory = factory;
+        }
+
+        @Override
+        public void serialize(T ts, DataOutputPlus out, int version) throws IOException
+        {
+            out.writeLong(ts.epoch);
+            out.writeLong(ts.real);
+            out.writeInt(ts.logical);
+            TopologySerializers.nodeId.serialize(ts.node, out, version);
+        }
+
+        public <V> int serialize(T ts, V dst, ValueAccessor<V> accessor, int offset)
+        {
+            int position = offset;
+            position += accessor.putLong(dst, position, ts.epoch);
+            position += accessor.putLong(dst, position, ts.real);
+            position += accessor.putInt(dst, position, ts.logical);
+            position += TopologySerializers.nodeId.serialize(ts.node, dst, accessor, position);
+            int size = position - offset;
+            Preconditions.checkState(size == serializedSize());
+            return size;
+        }
+
+        @Override
+        public T deserialize(DataInputPlus in, int version) throws IOException
+        {
+            return factory.create(in.readLong(),
+                                  in.readLong(),
+                                  in.readInt(),
+                                  TopologySerializers.nodeId.deserialize(in, version));
+        }
+
+        public <V> T deserialize(V src, ValueAccessor<V> accessor, int offset)
+        {
+            long epoch = accessor.getLong(src, offset);
+            offset += TypeSizes.LONG_SIZE;
+            long real = accessor.getLong(src, offset);
+            offset += TypeSizes.LONG_SIZE;
+            int logical = accessor.getInt(src, offset);
+            offset += TypeSizes.INT_SIZE;
+            Node.Id node = TopologySerializers.nodeId.deserialize(src, accessor, offset);
+            return factory.create(epoch, real, logical, node);
+        }
+
+        @Override
+        public long serializedSize(T ts, int version)
+        {
+            return serializedSize();
+        }
+
+        public int serializedSize()
+        {
+            return TypeSizes.LONG_SIZE +  // ts.epoch
+                   TypeSizes.LONG_SIZE +  // ts.real
+                   TypeSizes.INT_SIZE +   // ts.logical
+                   TopologySerializers.nodeId.serializedSize();   // ts.node
+        }
+    }
+
+    public static final IVersionedSerializer<Txn> txn = new IVersionedSerializer<Txn>()
+    {
+        @Override
+        public void serialize(Txn txn, DataOutputPlus out, int version) throws IOException
+        {
+            KeySerializers.keys.serialize(txn.keys(), out, version);
+            AccordRead.serializer.serialize((AccordRead) txn.read(), out, version);
+            AccordQuery.serializer.serialize((AccordQuery) txn.query(), out, version);
+            out.writeBoolean(txn.update() != null);
+            if (txn.update() != null)
+                AccordUpdate.serializer.serialize((AccordUpdate) txn.update(), out, version);
+
+        }
+
+        @Override
+        public Txn deserialize(DataInputPlus in, int version) throws IOException
+        {
+            Keys keys = KeySerializers.keys.deserialize(in, version);
+            AccordRead read = AccordRead.serializer.deserialize(in, version);
+            AccordQuery query = AccordQuery.serializer.deserialize(in, version);
+            if (in.readBoolean())
+                return new Txn.InMemory(keys, read, query, AccordUpdate.serializer.deserialize(in, version));
+            else
+                return new Txn.InMemory(keys, read, query);
+        }
+
+        @Override
+        public long serializedSize(Txn txn, int version)
+        {
+            long size = KeySerializers.keys.serializedSize(txn.keys(), version);
+            size += AccordRead.serializer.serializedSize((AccordRead) txn.read(), version);
+            size += AccordQuery.serializer.serializedSize((AccordQuery) txn.query(), version);
+            size += TypeSizes.sizeof(txn.update() != null);
+            if (txn.update() != null)
+                size += AccordUpdate.serializer.serializedSize((AccordUpdate) txn.update(), version);
+            return size;
+        }
+    };
+
+    public static final IVersionedSerializer<Status> status = new IVersionedSerializer<Status>()
+    {
+        @Override
+        public void serialize(Status status, DataOutputPlus out, int version) throws IOException
+        {
+            out.writeInt(status.ordinal());

Review Comment:
   vint?



##########
src/java/org/apache/cassandra/service/accord/serializers/CommandSerializers.java:
##########
@@ -0,0 +1,269 @@
+/*
+ * 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 com.google.common.base.Preconditions;
+
+import accord.local.Node;
+import accord.local.Status;
+import accord.primitives.Ballot;
+import accord.primitives.Deps;
+import accord.primitives.Keys;
+import accord.primitives.Timestamp;
+import accord.primitives.TxnId;
+import accord.txn.Txn;
+import accord.txn.Writes;
+import org.apache.cassandra.db.TypeSizes;
+import org.apache.cassandra.db.marshal.ValueAccessor;
+import org.apache.cassandra.io.IVersionedSerializer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+import org.apache.cassandra.service.accord.db.AccordQuery;
+import org.apache.cassandra.service.accord.db.AccordRead;
+import org.apache.cassandra.service.accord.db.AccordUpdate;
+import org.apache.cassandra.service.accord.db.AccordWrite;
+
+public class CommandSerializers
+{
+    private CommandSerializers() {}
+
+    public static final TimestampSerializer<TxnId> txnId = new TimestampSerializer<>(TxnId::new);
+    public static final TimestampSerializer<Timestamp> timestamp = new TimestampSerializer<>(Timestamp::new);
+    public static final TimestampSerializer<Ballot> ballot = new TimestampSerializer<>(Ballot::new);
+
+    public static class TimestampSerializer<T extends Timestamp> implements IVersionedSerializer<T>
+    {
+        interface Factory<T extends Timestamp>
+        {
+            T create(long epoch, long real, int logical, Node.Id node);
+        }
+
+        private final TimestampSerializer.Factory<T> factory;
+
+        private TimestampSerializer(TimestampSerializer.Factory<T> factory)
+        {
+            this.factory = factory;
+        }
+
+        @Override
+        public void serialize(T ts, DataOutputPlus out, int version) throws IOException
+        {
+            out.writeLong(ts.epoch);
+            out.writeLong(ts.real);
+            out.writeInt(ts.logical);
+            TopologySerializers.nodeId.serialize(ts.node, out, version);
+        }
+
+        public <V> int serialize(T ts, V dst, ValueAccessor<V> accessor, int offset)
+        {
+            int position = offset;
+            position += accessor.putLong(dst, position, ts.epoch);
+            position += accessor.putLong(dst, position, ts.real);
+            position += accessor.putInt(dst, position, ts.logical);
+            position += TopologySerializers.nodeId.serialize(ts.node, dst, accessor, position);
+            int size = position - offset;
+            Preconditions.checkState(size == serializedSize());
+            return size;
+        }
+
+        @Override
+        public T deserialize(DataInputPlus in, int version) throws IOException
+        {
+            return factory.create(in.readLong(),
+                                  in.readLong(),
+                                  in.readInt(),
+                                  TopologySerializers.nodeId.deserialize(in, version));
+        }
+
+        public <V> T deserialize(V src, ValueAccessor<V> accessor, int offset)
+        {
+            long epoch = accessor.getLong(src, offset);
+            offset += TypeSizes.LONG_SIZE;
+            long real = accessor.getLong(src, offset);
+            offset += TypeSizes.LONG_SIZE;
+            int logical = accessor.getInt(src, offset);
+            offset += TypeSizes.INT_SIZE;
+            Node.Id node = TopologySerializers.nodeId.deserialize(src, accessor, offset);
+            return factory.create(epoch, real, logical, node);
+        }
+
+        @Override
+        public long serializedSize(T ts, int version)
+        {
+            return serializedSize();
+        }
+
+        public int serializedSize()
+        {
+            return TypeSizes.LONG_SIZE +  // ts.epoch
+                   TypeSizes.LONG_SIZE +  // ts.real
+                   TypeSizes.INT_SIZE +   // ts.logical
+                   TopologySerializers.nodeId.serializedSize();   // ts.node
+        }
+    }
+
+    public static final IVersionedSerializer<Txn> txn = new IVersionedSerializer<Txn>()
+    {
+        @Override
+        public void serialize(Txn txn, DataOutputPlus out, int version) throws IOException
+        {
+            KeySerializers.keys.serialize(txn.keys(), out, version);
+            AccordRead.serializer.serialize((AccordRead) txn.read(), out, version);
+            AccordQuery.serializer.serialize((AccordQuery) txn.query(), out, version);
+            out.writeBoolean(txn.update() != null);
+            if (txn.update() != null)
+                AccordUpdate.serializer.serialize((AccordUpdate) txn.update(), out, version);
+
+        }
+
+        @Override
+        public Txn deserialize(DataInputPlus in, int version) throws IOException
+        {
+            Keys keys = KeySerializers.keys.deserialize(in, version);
+            AccordRead read = AccordRead.serializer.deserialize(in, version);
+            AccordQuery query = AccordQuery.serializer.deserialize(in, version);
+            if (in.readBoolean())
+                return new Txn.InMemory(keys, read, query, AccordUpdate.serializer.deserialize(in, version));
+            else
+                return new Txn.InMemory(keys, read, query);
+        }
+
+        @Override
+        public long serializedSize(Txn txn, int version)
+        {
+            long size = KeySerializers.keys.serializedSize(txn.keys(), version);
+            size += AccordRead.serializer.serializedSize((AccordRead) txn.read(), version);
+            size += AccordQuery.serializer.serializedSize((AccordQuery) txn.query(), version);
+            size += TypeSizes.sizeof(txn.update() != null);
+            if (txn.update() != null)
+                size += AccordUpdate.serializer.serializedSize((AccordUpdate) txn.update(), version);
+            return size;
+        }
+    };
+
+    public static final IVersionedSerializer<Status> status = new IVersionedSerializer<Status>()
+    {
+        @Override
+        public void serialize(Status status, DataOutputPlus out, int version) throws IOException
+        {
+            out.writeInt(status.ordinal());
+
+        }
+
+        @Override
+        public Status deserialize(DataInputPlus in, int version) throws IOException
+        {
+            return Status.values()[in.readInt()];
+        }
+
+        @Override
+        public long serializedSize(Status status, int version)
+        {
+            return TypeSizes.sizeof(status.ordinal());
+        }
+    };
+
+    public static final IVersionedSerializer<Deps> deps = new IVersionedSerializer<Deps>()
+    {
+        @Override
+        public void serialize(Deps deps, DataOutputPlus out, int version) throws IOException
+        {
+            Keys keys = deps.keys();
+            KeySerializers.keys.serialize(keys, out, version);
+
+            int txnIdCount = deps.txnIdCount();
+            out.writeInt(txnIdCount);
+            for (int i=0; i<txnIdCount; i++)
+                CommandSerializers.txnId.serialize(deps.txnId(i), out, version);
+
+            int keyToTxnIdCount = deps.keyToTxnIdCount();
+            out.writeInt(keyToTxnIdCount);
+            for (int i=0; i<keyToTxnIdCount; i++)
+                out.writeInt(deps.keyToTxnId(i));

Review Comment:
   vint?



##########
src/java/org/apache/cassandra/service/accord/db/AbstractKeyIndexed.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.db;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.List;
+import java.util.Map;
+import java.util.NavigableMap;
+import java.util.Objects;
+import java.util.TreeMap;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import com.google.common.base.Preconditions;
+
+import org.apache.cassandra.db.TypeSizes;
+import org.apache.cassandra.db.marshal.ByteBufferAccessor;
+import org.apache.cassandra.io.IVersionedSerializer;
+import org.apache.cassandra.io.util.DataInputBuffer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputBuffer;
+import org.apache.cassandra.io.util.DataOutputPlus;
+import org.apache.cassandra.net.MessagingService;
+import org.apache.cassandra.service.accord.api.AccordKey.PartitionKey;
+import org.apache.cassandra.utils.ByteBufferUtil;
+
+/**
+ * Contains a map of objects serialized to byte buffers
+ * @param <T>
+ */
+public abstract class AbstractKeyIndexed<T>
+{
+    final NavigableMap<PartitionKey, ByteBuffer> serialized;
+
+    abstract void serialize(T t, DataOutputPlus out, int version) throws IOException;
+    abstract T deserialize(DataInputPlus in, int version) throws IOException;
+    abstract long serializedSize(T t, int version);
+    abstract long emptySizeOnHeap();
+
+    protected ByteBuffer serialize(T item)
+    {
+        int version = MessagingService.current_version;
+        long size = serializedSize(item, version) + TypeSizes.INT_SIZE;
+        try (DataOutputBuffer out = new DataOutputBuffer((int) size))
+        {
+            out.writeInt(version);
+            serialize(item, out, version);
+            return out.buffer(false);
+        }
+        catch (IOException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    protected T deserialize(ByteBuffer bytes)
+    {
+        try (DataInputBuffer in = new DataInputBuffer(bytes, true))
+        {
+            int version = in.readInt();
+            return deserialize(in, version);
+        }
+        catch (IOException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Override
+    public boolean equals(Object o)
+    {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        AbstractKeyIndexed<?> that = (AbstractKeyIndexed<?>) o;
+        return serialized.equals(that.serialized);
+    }
+
+    @Override
+    public int hashCode()
+    {
+        return Objects.hash(serialized);
+    }
+
+    @Override
+    public String toString()
+    {
+        return getClass().getSimpleName() + serialized.entrySet().stream()
+                         .map(e -> e.getKey() + "=" + deserialize(e.getValue()))
+                         .collect(Collectors.joining(", ", "{", "}"));
+    }
+
+    public AbstractKeyIndexed(List<T> items, Function<T, PartitionKey> keyFunction)
+    {
+        this.serialized = new TreeMap<>();
+        for (int i=0, mi=items.size(); i<mi; i++)
+        {
+            T item = items.get(i);
+            PartitionKey key = keyFunction.apply(item);
+            // TODO: support multiple reads/writes per key
+            Preconditions.checkArgument(!this.serialized.containsKey(key));
+            this.serialized.put(key, serialize(item));
+        }
+    }
+
+    public AbstractKeyIndexed(NavigableMap<PartitionKey, ByteBuffer> serialized)
+    {
+        this.serialized = serialized;
+    }
+
+    public T getDeserialized(PartitionKey key)
+    {
+        ByteBuffer bytes = serialized.get(key);
+        if (bytes == null)
+            return null;
+        return deserialize(bytes);
+    }
+
+    public long estimatedSizeOnHeap()
+    {
+        long size = emptySizeOnHeap();
+        for (Map.Entry<PartitionKey, ByteBuffer> entry : serialized.entrySet())
+        {
+            size += entry.getKey().estimatedSizeOnHeap();
+            size += ByteBufferUtil.EMPTY_SIZE_ON_HEAP + ByteBufferAccessor.instance.size(entry.getValue());
+        }
+        return size;
+    }
+
+    static class Serializer<V, S extends AbstractKeyIndexed<V>> implements IVersionedSerializer<S>
+    {
+        private final Function<NavigableMap<PartitionKey, ByteBuffer>, S> factory;
+
+        Serializer(Function<NavigableMap<PartitionKey, ByteBuffer>, S> factory)
+        {
+            this.factory = factory;
+        }
+
+        @Override
+        public void serialize(S items, DataOutputPlus out, int version) throws IOException
+        {
+            out.writeInt(items.serialized.size());

Review Comment:
   vint?



##########
src/java/org/apache/cassandra/service/accord/serializers/CommandSerializers.java:
##########
@@ -0,0 +1,269 @@
+/*
+ * 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 com.google.common.base.Preconditions;
+
+import accord.local.Node;
+import accord.local.Status;
+import accord.primitives.Ballot;
+import accord.primitives.Deps;
+import accord.primitives.Keys;
+import accord.primitives.Timestamp;
+import accord.primitives.TxnId;
+import accord.txn.Txn;
+import accord.txn.Writes;
+import org.apache.cassandra.db.TypeSizes;
+import org.apache.cassandra.db.marshal.ValueAccessor;
+import org.apache.cassandra.io.IVersionedSerializer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+import org.apache.cassandra.service.accord.db.AccordQuery;
+import org.apache.cassandra.service.accord.db.AccordRead;
+import org.apache.cassandra.service.accord.db.AccordUpdate;
+import org.apache.cassandra.service.accord.db.AccordWrite;
+
+public class CommandSerializers
+{
+    private CommandSerializers() {}
+
+    public static final TimestampSerializer<TxnId> txnId = new TimestampSerializer<>(TxnId::new);
+    public static final TimestampSerializer<Timestamp> timestamp = new TimestampSerializer<>(Timestamp::new);
+    public static final TimestampSerializer<Ballot> ballot = new TimestampSerializer<>(Ballot::new);
+
+    public static class TimestampSerializer<T extends Timestamp> implements IVersionedSerializer<T>
+    {
+        interface Factory<T extends Timestamp>
+        {
+            T create(long epoch, long real, int logical, Node.Id node);
+        }
+
+        private final TimestampSerializer.Factory<T> factory;
+
+        private TimestampSerializer(TimestampSerializer.Factory<T> factory)
+        {
+            this.factory = factory;
+        }
+
+        @Override
+        public void serialize(T ts, DataOutputPlus out, int version) throws IOException
+        {
+            out.writeLong(ts.epoch);
+            out.writeLong(ts.real);
+            out.writeInt(ts.logical);
+            TopologySerializers.nodeId.serialize(ts.node, out, version);
+        }
+
+        public <V> int serialize(T ts, V dst, ValueAccessor<V> accessor, int offset)
+        {
+            int position = offset;
+            position += accessor.putLong(dst, position, ts.epoch);
+            position += accessor.putLong(dst, position, ts.real);
+            position += accessor.putInt(dst, position, ts.logical);
+            position += TopologySerializers.nodeId.serialize(ts.node, dst, accessor, position);
+            int size = position - offset;
+            Preconditions.checkState(size == serializedSize());
+            return size;
+        }
+
+        @Override
+        public T deserialize(DataInputPlus in, int version) throws IOException
+        {
+            return factory.create(in.readLong(),
+                                  in.readLong(),
+                                  in.readInt(),
+                                  TopologySerializers.nodeId.deserialize(in, version));
+        }
+
+        public <V> T deserialize(V src, ValueAccessor<V> accessor, int offset)
+        {
+            long epoch = accessor.getLong(src, offset);
+            offset += TypeSizes.LONG_SIZE;
+            long real = accessor.getLong(src, offset);
+            offset += TypeSizes.LONG_SIZE;
+            int logical = accessor.getInt(src, offset);
+            offset += TypeSizes.INT_SIZE;
+            Node.Id node = TopologySerializers.nodeId.deserialize(src, accessor, offset);
+            return factory.create(epoch, real, logical, node);
+        }
+
+        @Override
+        public long serializedSize(T ts, int version)
+        {
+            return serializedSize();
+        }
+
+        public int serializedSize()
+        {
+            return TypeSizes.LONG_SIZE +  // ts.epoch
+                   TypeSizes.LONG_SIZE +  // ts.real
+                   TypeSizes.INT_SIZE +   // ts.logical
+                   TopologySerializers.nodeId.serializedSize();   // ts.node
+        }
+    }
+
+    public static final IVersionedSerializer<Txn> txn = new IVersionedSerializer<Txn>()
+    {
+        @Override
+        public void serialize(Txn txn, DataOutputPlus out, int version) throws IOException
+        {
+            KeySerializers.keys.serialize(txn.keys(), out, version);
+            AccordRead.serializer.serialize((AccordRead) txn.read(), out, version);
+            AccordQuery.serializer.serialize((AccordQuery) txn.query(), out, version);
+            out.writeBoolean(txn.update() != null);
+            if (txn.update() != null)
+                AccordUpdate.serializer.serialize((AccordUpdate) txn.update(), out, version);
+
+        }
+
+        @Override
+        public Txn deserialize(DataInputPlus in, int version) throws IOException
+        {
+            Keys keys = KeySerializers.keys.deserialize(in, version);
+            AccordRead read = AccordRead.serializer.deserialize(in, version);
+            AccordQuery query = AccordQuery.serializer.deserialize(in, version);
+            if (in.readBoolean())
+                return new Txn.InMemory(keys, read, query, AccordUpdate.serializer.deserialize(in, version));
+            else
+                return new Txn.InMemory(keys, read, query);
+        }
+
+        @Override
+        public long serializedSize(Txn txn, int version)
+        {
+            long size = KeySerializers.keys.serializedSize(txn.keys(), version);
+            size += AccordRead.serializer.serializedSize((AccordRead) txn.read(), version);
+            size += AccordQuery.serializer.serializedSize((AccordQuery) txn.query(), version);
+            size += TypeSizes.sizeof(txn.update() != null);
+            if (txn.update() != null)
+                size += AccordUpdate.serializer.serializedSize((AccordUpdate) txn.update(), version);
+            return size;
+        }
+    };
+
+    public static final IVersionedSerializer<Status> status = new IVersionedSerializer<Status>()
+    {
+        @Override
+        public void serialize(Status status, DataOutputPlus out, int version) throws IOException
+        {
+            out.writeInt(status.ordinal());
+
+        }
+
+        @Override
+        public Status deserialize(DataInputPlus in, int version) throws IOException
+        {
+            return Status.values()[in.readInt()];
+        }
+
+        @Override
+        public long serializedSize(Status status, int version)
+        {
+            return TypeSizes.sizeof(status.ordinal());
+        }
+    };
+
+    public static final IVersionedSerializer<Deps> deps = new IVersionedSerializer<Deps>()
+    {
+        @Override
+        public void serialize(Deps deps, DataOutputPlus out, int version) throws IOException
+        {
+            Keys keys = deps.keys();
+            KeySerializers.keys.serialize(keys, out, version);
+
+            int txnIdCount = deps.txnIdCount();
+            out.writeInt(txnIdCount);
+            for (int i=0; i<txnIdCount; i++)
+                CommandSerializers.txnId.serialize(deps.txnId(i), out, version);
+
+            int keyToTxnIdCount = deps.keyToTxnIdCount();
+            out.writeInt(keyToTxnIdCount);

Review Comment:
   vint?



-- 
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-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org