blerer commented on code in PR #2655:
URL: https://github.com/apache/cassandra/pull/2655#discussion_r1378739047
##########
src/java/org/apache/cassandra/serializers/CollectionSerializer.java:
##########
@@ -35,35 +37,106 @@
public abstract class CollectionSerializer<T> extends TypeSerializer<T>
{
protected abstract List<ByteBuffer> serializeValues(T value);
- protected abstract int getElementCount(T value);
@Override
public ByteBuffer serialize(T input)
{
List<ByteBuffer> values = serializeValues(input);
- return pack(values, ByteBufferAccessor.instance,
getElementCount(input));
+ return pack(values, ByteBufferAccessor.instance);
}
- public static ByteBuffer pack(Collection<ByteBuffer> values, int elements)
+ public ByteBuffer pack(Collection<ByteBuffer> values)
{
- return pack(values, ByteBufferAccessor.instance, elements);
+ return pack(values, ByteBufferAccessor.instance);
}
- public static <V> V pack(Collection<V> values, ValueAccessor<V> accessor,
int elements)
+ public <V> V pack(Collection<V> values, ValueAccessor<V> accessor)
{
int size = 0;
for (V value : values)
size += sizeOfValue(value, accessor);
ByteBuffer result = ByteBuffer.allocate(sizeOfCollectionSize() + size);
- writeCollectionSize(result, elements);
+ writeCollectionSize(result, collectionSize(values));
for (V value : values)
{
writeValue(result, value, accessor);
}
return accessor.valueOf((ByteBuffer) result.flip());
}
+ public List<ByteBuffer> unpack(ByteBuffer input)
+ {
+ return unpack(input, ByteBufferAccessor.instance);
+ }
+
+ public <V> List<V> unpack(V input, ValueAccessor<V> accessor)
+ {
+ try
+ {
+ int elements =
numberOfSerializedElements(readCollectionSize(input, accessor));
+ if (elements < 0)
+ throw new MarshalException("The data cannot be deserialized as
a " + getCollectionName());
+
+ // If the received bytes are not corresponding to a collection,
the size might be a huge number.
+ // In such a case we do not want to initialize the list with that
size as it can result
+ // in an OOM. On the other hand we do not want to have to resize
the list
+ // if we can avoid it, so we put a reasonable limit on the
initialCapacity.
+ List<V> values = new ArrayList<>(Math.min(elements, 256));
Review Comment:
I think I added it because it is a problem that we have in other places
--
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]