belliottsmith commented on code in PR #4362:
URL: https://github.com/apache/cassandra/pull/4362#discussion_r2321548762
##########
src/java/org/apache/cassandra/service/accord/txn/TxnUpdate.java:
##########
@@ -77,13 +92,325 @@
public class TxnUpdate extends AccordUpdate
{
- private static final long EMPTY_SIZE = ObjectSizes.measure(new
TxnUpdate(TableMetadatas.none(), null, new ByteBuffer[0], null, null,
PreserveTimestamp.no));
+ static class ConditionalBlock
+ {
+ public static final UnversionedSerializer<ConditionalBlock> serializer
= new UnversionedSerializer<>()
+ {
+ @Override
+ public void serialize(ConditionalBlock t, DataOutputPlus out)
throws IOException
+ {
+ out.writeUnsignedVInt32(t.id);
+ if (t.condition != null)
+ {
+ writeWithVIntLength(t.condition.bytes(), out);
+ }
+ else
+ {
+ out.writeUnsignedVInt32(0);
+ }
+ serializeArrayUnsignedVInt(t.fragments, out);
+ }
+
+ @Override
+ public ConditionalBlock deserialize(DataInputPlus in) throws
IOException
+ {
+ int id = in.readUnsignedVInt32();
+ ByteBuffer conditionBytes = readWithVIntLength(in);
+ SerializedTxnCondition condition = conditionBytes.remaining()
> 0
+ ? new
SerializedTxnCondition(conditionBytes)
+ : null;
+
+ // Deserialize mutations
+ int[] mutations = deserializeArrayUnsignedVInt(in);
+ return new ConditionalBlock(id, condition, mutations);
+ }
+
+ @Override
+ public void skip(DataInputPlus in) throws IOException
+ {
+ in.readUnsignedVInt32();
+ skipWithVIntLength(in);
+ skipArrayUnsignedVInt(in);
+ }
+
+ @Override
+ public long serializedSize(ConditionalBlock t)
+ {
+ long size = TypeSizes.sizeofUnsignedVInt(t.id);
+ if (t.condition != null)
+ {
+ size += serializedSizeWithVIntLength(t.condition.bytes());
+ }
+ else
+ {
+ size += TypeSizes.sizeofUnsignedVInt(0);
+ }
+ size += serializedArrayUnsignedVIntSize(t.fragments);
+ return size;
+ }
+ };
+ final int id;
+ /**
+ * If null, this block always executes (ELSE / unconditional).
+ */
+ @Nullable
+ final SerializedTxnCondition condition;
+ final int[] fragments;
+
+ ConditionalBlock(int id, @Nullable SerializedTxnCondition condition,
int[] fragments)
+ {
+ this.id = id;
+ this.condition = condition;
+ this.fragments = fragments;
+ }
+
+ public long estimatedSizeOnHeap()
+ {
+ long size = 0; //TODO (correctness): EMPTY_SIZE
+ if (condition != null)
+ size += condition.estimatedSizeOnHeap();
+ size += ObjectSizes.sizeOfArray(fragments);
+ return size;
+ }
+
+ @Override
+ public boolean equals(Object o)
+ {
+ if (o == null || getClass() != o.getClass()) return false;
+ ConditionalBlock that = (ConditionalBlock) o;
+ return id == that.id && Objects.equals(condition, that.condition)
&& Arrays.equals(fragments, that.fragments);
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return Objects.hash(id, condition, Arrays.hashCode(fragments));
+ }
+
+ public void toString(StringBuilder sb, TableMetadatas tables, Keys
keys, Block block)
+ {
+ sb.append("{condition=")
+ .append(condition == null ? "null" :
condition.deserialize(tables))
+ .append(", fragments=")
+ .append(deserialize(keys, tables, block, fragments))
+ .append('}');
+ }
+
+ public ConditionalBlock merge(ConditionalBlock that)
+ {
+ requireArgument(this.id == that.id, "Tried to merge different
blocks; expected %d but given %d", this.id, that.id);
+ IntArrayList merged = new IntArrayList(Math.max(fragments.length,
that.fragments.length));
+ int[] self = fragments;
+ int[] other = that.fragments;
+ int i = 0, m = 0;
+ while (true)
+ {
+ if (i == self.length && m == other.length) break;
+ if (i < self.length && m < other.length)
+ {
+ // pick the smallest value
+ int lid = self[i];
+ int rid = other[m];
+ if (lid < rid)
+ {
+ merged.add(lid);
+ i++;
+ }
+ else if (rid < lid)
+ {
+ merged.add(rid);
+ m++;
+ }
+ else
+ {
+ merged.add(lid);
+ i++;
+ m++;
+ }
+ }
+ else if (i < self.length)
+ merged.add(self[i++]);
+ else if (m < other.length)
+ merged.add(other[m++]);
+ }
+ return new ConditionalBlock(id, condition, merged.toArray());
+ }
+ }
+
+ static class Block
+ {
+ private static SimpleBitSet bitset(Keys outter, Keys inner)
Review Comment:
outter is a typo, but I prefer superset/subset for this kind of logic as it
makes clearer the relationship. Also, we already have this logic in various
places. The most obvious is `AbstractWithKeysSerializer`, though this is more
annoying to use that we might like. Surely we at least have the subset bitset
logic somewhere already? If not, let's at least consider adding that, but it's
a shame this form of logic is getting fragmented and reproduced in different
ways.
--
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]