yifan-c commented on code in PR #1739: URL: https://github.com/apache/cassandra/pull/1739#discussion_r931271260
########## src/java/org/apache/cassandra/serializers/SetIndexedCollectionSerializer.java: ########## @@ -0,0 +1,209 @@ +/* + * 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.serializers; + +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; + +import com.google.common.collect.Range; + +import org.apache.cassandra.db.marshal.AbstractType; +import org.apache.cassandra.db.marshal.ByteBufferAccessor; +import org.apache.cassandra.transport.ProtocolVersion; +import org.apache.cassandra.utils.ByteBufferUtil; + +/** + * Common superclass for {@link SetSerializer} and {@link MapSerializer}, considering a set as a map without values. + */ +abstract class SetIndexedCollectionSerializer<T> extends CollectionSerializer<T> +{ + private final boolean hasValues; + private final String name; + + protected SetIndexedCollectionSerializer(boolean hasValues) + { + this.hasValues = hasValues; + name = hasValues ? "map" : "set"; + } + + @Override + public ByteBuffer getSliceFromSerialized(ByteBuffer collection, + ByteBuffer from, + ByteBuffer to, + AbstractType<?> comparator, + boolean frozen) + { + if (from == ByteBufferUtil.UNSET_BYTE_BUFFER && to == ByteBufferUtil.UNSET_BYTE_BUFFER) + return collection; + + try + { + ByteBuffer input = collection.duplicate(); + int n = readCollectionSize(input, ByteBufferAccessor.instance, ProtocolVersion.V3); + input.position(input.position() + sizeOfCollectionSize(n, ProtocolVersion.V3)); + int startPos = input.position(); + int count = 0; + boolean inSlice = from == ByteBufferUtil.UNSET_BYTE_BUFFER; + + for (int i = 0; i < n; i++) + { + int pos = input.position(); + ByteBuffer key = readValue(input, ByteBufferAccessor.instance, 0, ProtocolVersion.V3); // key + input.position(input.position() + sizeOfValue(key, ByteBufferAccessor.instance, ProtocolVersion.V3)); + + // If we haven't passed the start already, check if we have now + if (!inSlice) + { + int comparison = comparator.compareForCQL(from, key); + if (comparison <= 0) + { + // We're now within the slice + inSlice = true; + startPos = pos; + } + else + { + // We're before the slice, so we know we don't care about this element + if (hasValues) + skipValue(input, ProtocolVersion.V3); + continue; + } + } + + // Now check if we're done + int comparison = to == ByteBufferUtil.UNSET_BYTE_BUFFER ? -1 : comparator.compareForCQL(key, to); + if (comparison > 0) + { + // We're done and shouldn't include the key we just read + input.position(pos); + break; + } + + // Otherwise, we'll include that element + if (hasValues) + skipValue(input, ProtocolVersion.V3); // value + ++count; + + // But if we know it was the last of the slice, we break early + if (comparison == 0) + break; + } + + if (count == 0 && !frozen) + return null; + + return copyAsNewCollection(collection, count, startPos, input.position(), ProtocolVersion.V3); + } + catch (BufferUnderflowException | IndexOutOfBoundsException e) + { + throw new MarshalException("Not enough bytes to read a " + name); + } + } + + @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 set is in sorted order, we know we've gone too far and the element doesn't exist + return -1; + + // comparison < 0 + if (hasValues) + offset += skipValue(input, ByteBufferAccessor.instance, offset, ProtocolVersion.V3); + } + return -1; + } + catch (BufferUnderflowException e) + { + throw new MarshalException("Not enough bytes to read a " + name); + } + } + + @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 (hasValues && i > 0) + skipValue(input, ProtocolVersion.V3); Review Comment: Super weak nit: ```java private void skipMapValue(ByteBuffer input, ProtocolVersion version) { if (hasValues) skipValue(input, version); } ``` -- 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]

