dcapwell commented on code in PR #1962:
URL: https://github.com/apache/cassandra/pull/1962#discussion_r1042826551


##########
src/java/org/apache/cassandra/service/accord/txn/TxnDataName.java:
##########
@@ -0,0 +1,225 @@
+/*
+ * 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.txn;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+import org.apache.cassandra.db.DecoratedKey;
+import org.apache.cassandra.db.TypeSizes;
+import org.apache.cassandra.io.IVersionedSerializer;
+import org.apache.cassandra.io.util.DataInputPlus;
+import org.apache.cassandra.io.util.DataOutputPlus;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.utils.ByteBufferUtil;
+
+public class TxnDataName implements Comparable<TxnDataName>
+{
+    private static final TxnDataName RETURNING = new 
TxnDataName(Kind.RETURNING);
+
+    public enum Kind
+    {
+        USER((byte) 1),
+        RETURNING((byte) 2),
+        AUTO_READ((byte) 3);
+
+        private final byte value;
+
+        Kind(byte value)
+        {
+            this.value = value;
+        }
+
+        public static Kind from(byte b)
+        {
+            switch (b)
+            {
+                case 1:
+                    return USER;
+                case 2:
+                    return RETURNING;
+                case 3:
+                    return AUTO_READ;
+                default:
+                    throw new IllegalArgumentException("Unknown kind: " + b);
+            }
+        }
+    }
+
+    private final Kind kind;
+    private final String[] parts;
+
+    public TxnDataName(Kind kind, String... parts)
+    {
+        this.kind = kind;
+        this.parts = parts;
+    }
+
+    public static TxnDataName user(String name)
+    {
+        return new TxnDataName(Kind.USER, name);
+    }
+
+    public static TxnDataName returning()
+    {
+        return RETURNING;
+    }
+
+    public static TxnDataName partitionRead(TableMetadata metadata, 
DecoratedKey key, int index)
+    {
+        return new TxnDataName(Kind.AUTO_READ, metadata.keyspace, 
metadata.name, bytesToString(key.getKey()), String.valueOf(index));
+    }
+
+    private static String bytesToString(ByteBuffer bytes)
+    {
+        return ByteBufferUtil.bytesToHex(bytes);
+    }
+
+    private static ByteBuffer stringToBytes(String string)
+    {
+        return ByteBufferUtil.hexToBytes(string);
+    }
+
+    public Kind getKind()
+    {
+        return kind;
+    }
+
+    public List<String> getParts()
+    {
+        return Collections.unmodifiableList(Arrays.asList(parts));
+    }
+
+    public boolean isFor(TableMetadata metadata)
+    {
+        if (kind != Kind.AUTO_READ)
+            return false;
+        return metadata.keyspace.equals(parts[0])
+               && metadata.name.equals(parts[1]);
+    }
+
+    public DecoratedKey getDecoratedKey(TableMetadata metadata)
+    {
+        checkKind(Kind.AUTO_READ);
+        ByteBuffer data = stringToBytes(parts[2]);
+        return metadata.partitioner.decorateKey(data);
+    }
+
+    public boolean atIndex(int index)
+    {
+        checkKind(Kind.AUTO_READ);
+        return Integer.parseInt(parts[3]) == index;
+    }
+
+    private void checkKind(Kind expected)
+    {
+        if (kind != expected)
+            throw new IllegalStateException("Expected kind " + expected + " 
but is " + kind);
+    }
+
+    public long estimatedSizeOnHeap()
+    {
+        long size = 0;
+        for (String part : parts)
+            size += part.length();

Review Comment:
   pushed in feedback branch: used `size += ObjectSizes.sizeOf(part);`



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to