adelapena commented on code in PR #1739:
URL: https://github.com/apache/cassandra/pull/1739#discussion_r931189836
##########
src/java/org/apache/cassandra/serializers/MapSerializer.java:
##########
@@ -243,6 +247,96 @@ public ByteBuffer getSliceFromSerialized(ByteBuffer
collection,
}
}
+ @Override
+ public int getIndexFromSerialized(ByteBuffer collection, ByteBuffer key,
AbstractType<?> comparator)
+ {
+ try
+ {
+ ByteBuffer input = collection.duplicate();
+ int n = readCollectionSize(input, ByteBufferAccessor.instance,
ProtocolVersion.V3);
+ int offset = sizeOfCollectionSize(n, ProtocolVersion.V3);
+ for (int i = 0; i < n; i++)
+ {
+ ByteBuffer kbb = readValue(input, ByteBufferAccessor.instance,
offset, ProtocolVersion.V3);
+ offset += sizeOfValue(kbb, ByteBufferAccessor.instance,
ProtocolVersion.V3);
+ int comparison = comparator.compareForCQL(kbb, key);
+
+ if (comparison == 0)
+ return i;
+
+ if (comparison > 0)
+ // since the map is in sorted order, we know we've gone
too far and the element doesn't exist
+ return -1;
+
+ // comparison < 0
+ offset += skipValue(input, ByteBufferAccessor.instance,
offset, ProtocolVersion.V3);
+ }
+ return -1;
+ }
+ catch (BufferUnderflowException e)
+ {
+ throw new MarshalException("Not enough bytes to read a map");
+ }
+ }
+
+ @Override
+ public Range<Integer> getIndexesRangeFromSerialized(ByteBuffer collection,
+ ByteBuffer from,
+ ByteBuffer to,
+ AbstractType<?>
comparator)
+ {
+ if (from == ByteBufferUtil.UNSET_BYTE_BUFFER && to ==
ByteBufferUtil.UNSET_BYTE_BUFFER)
+ return Range.closed(0, Integer.MAX_VALUE);
+
+ try
+ {
+ ByteBuffer input = collection.duplicate();
+ int n = readCollectionSize(input, ByteBufferAccessor.instance,
ProtocolVersion.V3);
+ input.position(input.position() + sizeOfCollectionSize(n,
ProtocolVersion.V3));
+ int start = from == ByteBufferUtil.UNSET_BYTE_BUFFER ? 0 : -1;
+ int end = to == ByteBufferUtil.UNSET_BYTE_BUFFER ? n : -1;
+
+ for (int i = 0; i < n; i++)
+ {
+ if (start >= 0 && end >= 0)
+ break;
+ else if (i > 0)
+ skipValue(input, ProtocolVersion.V3);
Review Comment:
Yep, those methods are derived from the previously existent
`getSliceFromSerialized`. That method also has a considerable duplication
between `SetSerializer` and `MapSerializer`.
I have moved the three `getSliceFromSerialized`, `getIndexFromSerialized`
and `getIndexesRangeFromSerialized` methods to a common abstract superclass,
since sets and maps have so much in common. That removes a good bunch of code
duplication, although I'm not very sure about how to call that class. I have
used `SetIndexedCollectionSerializer`, but any ideas are welcome.
--
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]