absurdfarce commented on code in PR #1952: URL: https://github.com/apache/cassandra-java-driver/pull/1952#discussion_r1926367439
########## core/src/main/java/com/datastax/oss/driver/internal/core/type/codec/VectorCodec.java: ########## @@ -113,40 +139,63 @@ public CqlVector<SubtypeT> decode( if (bytes == null || bytes.remaining() == 0) { return null; } + boolean isVarSized = !subtypeCodec.serializedSize().isPresent(); + if (isVarSized) { + ByteBuffer input = bytes.duplicate(); + List<SubtypeT> rv = new ArrayList<SubtypeT>(cqlType.getDimensions()); + for (int i = 0; i < cqlType.getDimensions(); ++i) { + int size = VIntCoding.getUnsignedVInt32(input, input.position()); + input.position(input.position() + VIntCoding.computeUnsignedVIntSize(size)); Review Comment: Okay, I can make it at least a bit clearer with this cleaned up (and already formatted!) version: ``` @Nullable @Override public CqlVector<SubtypeT> decode( @Nullable ByteBuffer bytes, @NonNull ProtocolVersion protocolVersion) { if (bytes == null || bytes.remaining() == 0) { return null; } // Upfront check for fixed-size types only subtypeCodec .serializedSize() .ifPresent( (fixed_size) -> { if (bytes.remaining() != cqlType.getDimensions() * fixed_size) { throw new IllegalArgumentException( String.format( "Expected elements of uniform size, observed %d elements with total bytes %d", cqlType.getDimensions(), bytes.remaining())); } }); ; ByteBuffer slice = bytes.slice(); List<SubtypeT> rv = new ArrayList<SubtypeT>(cqlType.getDimensions()); for (int i = 0; i < cqlType.getDimensions(); ++i) { int size = subtypeCodec .serializedSize() .orElseGet(() -> VIntCoding.getUnsignedVInt32(slice, slice.position())); // If we aren't dealing with a fixed-size type we need to move the current slice position // beyond the vint-encoded size of the current element. Ideally this would be // serializedSize().ifNotPresent(Consumer) but the Optional API isn't doing us any favors // there. if (!subtypeCodec.serializedSize().isPresent()) slice.position(slice.position() + VIntCoding.computeUnsignedVIntSize(size)); int originalPosition = slice.position(); slice.limit(originalPosition + size); rv.add(this.subtypeCodec.decode(slice, protocolVersion)); // Move to the start of the next element slice.position(originalPosition + size); // Reset the limit to the end of the buffer slice.limit(slice.capacity()); } // if too many elements, throw if (slice.hasRemaining()) { throw new IllegalArgumentException( String.format( "Too many elements; must provide elements for %d dimensions", cqlType.getDimensions())); } return CqlVector.newInstance(rv); } ``` -- 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-unsubscr...@cassandra.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For additional commands, e-mail: pr-h...@cassandra.apache.org