maedhroz commented on code in PR #2267: URL: https://github.com/apache/cassandra/pull/2267#discussion_r1167166246
########## src/java/org/apache/cassandra/io/sstable/format/bti/BtiTableScanner.java: ########## @@ -0,0 +1,348 @@ +/* + * 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.io.sstable.format.bti; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterators; + +import org.apache.cassandra.db.DataRange; +import org.apache.cassandra.db.DecoratedKey; +import org.apache.cassandra.db.PartitionPosition; +import org.apache.cassandra.db.filter.ClusteringIndexFilter; +import org.apache.cassandra.db.filter.ColumnFilter; +import org.apache.cassandra.db.rows.LazilyInitializedUnfilteredRowIterator; +import org.apache.cassandra.db.rows.UnfilteredRowIterator; +import org.apache.cassandra.dht.AbstractBounds; +import org.apache.cassandra.dht.AbstractBounds.Boundary; +import org.apache.cassandra.dht.Bounds; +import org.apache.cassandra.dht.Range; +import org.apache.cassandra.dht.Token; +import org.apache.cassandra.io.sstable.CorruptSSTableException; +import org.apache.cassandra.io.sstable.ISSTableScanner; +import org.apache.cassandra.io.sstable.SSTableReadsListener; +import org.apache.cassandra.io.sstable.format.SSTableReader; +import org.apache.cassandra.io.util.FileUtils; +import org.apache.cassandra.io.util.RandomAccessReader; +import org.apache.cassandra.schema.TableMetadata; +import org.apache.cassandra.utils.AbstractIterator; +import org.apache.cassandra.utils.CloseableIterator; + +import static org.apache.cassandra.dht.AbstractBounds.isEmpty; +import static org.apache.cassandra.dht.AbstractBounds.maxLeft; +import static org.apache.cassandra.dht.AbstractBounds.minRight; + +public class BtiTableScanner implements ISSTableScanner +{ + private final AtomicBoolean isClosed = new AtomicBoolean(false); + protected final RandomAccessReader dfile; + public final BtiTableReader sstable; + + private final Iterator<AbstractBounds<PartitionPosition>> rangeIterator; + + private final ColumnFilter columns; + private final DataRange dataRange; + private final SSTableReadsListener listener; + private long startScan = -1; + private long bytesScanned = 0; + + protected CloseableIterator<UnfilteredRowIterator> iterator; + + // Full scan of the sstables + public static ISSTableScanner getScanner(BtiTableReader sstable) + { + return getScanner(sstable, Iterators.singletonIterator(fullRange(sstable))); + } + + public static ISSTableScanner getScanner(BtiTableReader sstable, + ColumnFilter columns, + DataRange dataRange, + SSTableReadsListener listener) + { + return new BtiTableScanner(sstable, columns, dataRange, makeBounds(sstable, dataRange).iterator(), listener); + } + + public static ISSTableScanner getScanner(BtiTableReader sstable, Collection<Range<Token>> tokenRanges) + { + return getScanner(sstable, makeBounds(sstable, tokenRanges).iterator()); + } + + public static ISSTableScanner getScanner(BtiTableReader sstable, Iterator<AbstractBounds<PartitionPosition>> rangeIterator) + { + return new BtiTableScanner(sstable, ColumnFilter.all(sstable.metadata()), null, rangeIterator, SSTableReadsListener.NOOP_LISTENER); + } + + private BtiTableScanner(BtiTableReader sstable, + ColumnFilter columns, + DataRange dataRange, + Iterator<AbstractBounds<PartitionPosition>> rangeIterator, + SSTableReadsListener listener) + { + assert sstable != null; + + this.dfile = sstable.openDataReader(); + this.sstable = sstable; + this.columns = columns; + this.dataRange = dataRange; + this.rangeIterator = rangeIterator; + this.listener = listener; + } + + public static List<AbstractBounds<PartitionPosition>> makeBounds(SSTableReader sstable, Collection<Range<Token>> tokenRanges) + { + List<AbstractBounds<PartitionPosition>> boundsList = new ArrayList<>(tokenRanges.size()); + for (Range<Token> range : Range.normalize(tokenRanges)) + addRange(sstable, Range.makeRowRange(range), boundsList); + return boundsList; + } + + static List<AbstractBounds<PartitionPosition>> makeBounds(SSTableReader sstable, DataRange dataRange) + { + List<AbstractBounds<PartitionPosition>> boundsList = new ArrayList<>(2); + addRange(sstable, dataRange.keyRange(), boundsList); + return boundsList; + } + + static AbstractBounds<PartitionPosition> fullRange(SSTableReader sstable) + { + return new Bounds<>(sstable.first, sstable.last); + } + + private static void addRange(SSTableReader sstable, AbstractBounds<PartitionPosition> requested, List<AbstractBounds<PartitionPosition>> boundsList) + { + if (requested instanceof Range && ((Range<?>) requested).isWrapAround()) + { + if (requested.right.compareTo(sstable.first) >= 0) + { + // since we wrap, we must contain the whole sstable prior to stopKey() + Boundary<PartitionPosition> left = new Boundary<>(sstable.first, true); + Boundary<PartitionPosition> right; + right = requested.rightBoundary(); + right = minRight(right, sstable.last, true); + if (!isEmpty(left, right)) + boundsList.add(AbstractBounds.bounds(left, right)); + } + if (requested.left.compareTo(sstable.last) <= 0) + { + // since we wrap, we must contain the whole sstable after dataRange.startKey() + Boundary<PartitionPosition> right = new Boundary<>(sstable.last, true); + Boundary<PartitionPosition> left; + left = requested.leftBoundary(); + left = maxLeft(left, sstable.first, true); + if (!isEmpty(left, right)) + boundsList.add(AbstractBounds.bounds(left, right)); + } + } + else + { + assert !AbstractBounds.strictlyWrapsAround(requested.left, requested.right); + Boundary<PartitionPosition> left, right; + left = requested.leftBoundary(); + right = requested.rightBoundary(); + left = maxLeft(left, sstable.first, true); + // apparently isWrapAround() doesn't count Bounds that extend to the limit (min) as wrapping + right = requested.right.isMinimum() ? new Boundary<>(sstable.last, true) + : minRight(right, sstable.last, true); + if (!isEmpty(left, right)) + boundsList.add(AbstractBounds.bounds(left, right)); + } + } + + public void close() + { + try + { + if (isClosed.compareAndSet(false, true)) + { + FileUtils.close(dfile); + if (iterator != null) + iterator.close(); + } + } + catch (IOException e) + { + sstable.markSuspect(); + throw new CorruptSSTableException(e, sstable.getFilename()); + } + } + + public long getBytesScanned() + { + return bytesScanned; + } + + @Override + public long getLengthInBytes() + { + return sstable.uncompressedLength(); + } + + + public long getCompressedLengthInBytes() + { + return sstable.onDiskLength(); + } + + @Override + public long getCurrentPosition() + { + return dfile.getFilePointer(); + } + + @Override + public Set<SSTableReader> getBackingSSTables() + { + return ImmutableSet.of(sstable); + } + + public int level() Review Comment: nit: unused method -- 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]

