blambov commented on code in PR #3695:
URL: https://github.com/apache/cassandra/pull/3695#discussion_r1867304834


##########
src/java/org/apache/cassandra/io/sstable/format/SSTableReader.java:
##########
@@ -728,27 +728,104 @@ public List<PartitionPositionBounds> 
getPositionsForRanges(Collection<Range<Toke
         for (Range<Token> range : Range.normalize(ranges))
         {
             assert !range.isWrapAround() || range.right.isMinimum();
-            // truncate the range so it at most covers the sstable
             AbstractBounds<PartitionPosition> bounds = 
Range.makeRowRange(range);
-            PartitionPosition leftBound = bounds.left.compareTo(first) > 0 ? 
bounds.left : first.getToken().minKeyBound();
-            PartitionPosition rightBound = bounds.right.isMinimum() ? 
last.getToken().maxKeyBound() : bounds.right;
+            PartitionPositionBounds pb = getPositionsForBounds(bounds);
+            if (pb != null)
+                positions.add(pb);
+        }
+        return positions;
+    }
 
-            if (leftBound.compareTo(last) > 0 || rightBound.compareTo(first) < 
0)
-                continue;
+    /**
+     * Get a list of data positions in this SSTable that correspond to the 
given list of bounds. This method will remove
+     * non-covered intervals, but will not correct order or overlap in the 
supplied list, e.g. if bounds overlap, the
+     * result will be sections of the data file that repeat the same positions.
+     *
+     * @return A sorted list of [offset,end) pairs corresponding to the given 
boundsList in the datafile for this
+     *         SSTable.
+     */
+    public List<PartitionPositionBounds> 
getPositionsForBoundsIterator(Iterator<AbstractBounds<PartitionPosition>> 
boundsList)
+    {
+        // use the index to determine a minimal section for each range
+        List<PartitionPositionBounds> positions = new ArrayList<>();
+        while (boundsList.hasNext())
+        {
+            AbstractBounds<PartitionPosition> bounds = boundsList.next();
+            PartitionPositionBounds pb = getPositionsForBounds(bounds);
+            if (pb != null)
+                positions.add(pb);
+        }
+        return positions;
+    }
 
-            long left = getPosition(leftBound, Operator.GT);
-            long right = (rightBound.compareTo(last) > 0)
-                         ? uncompressedLength()
-                         : getPosition(rightBound, Operator.GT);
+    /**
+     * Determine the data positions in this SSTable that cover the given 
bounds.
+     *
+     * @return An [offset,end) pair that cover the given bounds in the 
datafile for this SSTable.

Review Comment:
   Added.



##########
src/java/org/apache/cassandra/io/sstable/format/SSTableReader.java:
##########
@@ -728,27 +728,104 @@ public List<PartitionPositionBounds> 
getPositionsForRanges(Collection<Range<Toke
         for (Range<Token> range : Range.normalize(ranges))
         {
             assert !range.isWrapAround() || range.right.isMinimum();
-            // truncate the range so it at most covers the sstable
             AbstractBounds<PartitionPosition> bounds = 
Range.makeRowRange(range);
-            PartitionPosition leftBound = bounds.left.compareTo(first) > 0 ? 
bounds.left : first.getToken().minKeyBound();
-            PartitionPosition rightBound = bounds.right.isMinimum() ? 
last.getToken().maxKeyBound() : bounds.right;
+            PartitionPositionBounds pb = getPositionsForBounds(bounds);
+            if (pb != null)
+                positions.add(pb);
+        }
+        return positions;
+    }
 
-            if (leftBound.compareTo(last) > 0 || rightBound.compareTo(first) < 
0)
-                continue;
+    /**
+     * Get a list of data positions in this SSTable that correspond to the 
given list of bounds. This method will remove
+     * non-covered intervals, but will not correct order or overlap in the 
supplied list, e.g. if bounds overlap, the
+     * result will be sections of the data file that repeat the same positions.
+     *
+     * @return A sorted list of [offset,end) pairs corresponding to the given 
boundsList in the datafile for this
+     *         SSTable.
+     */
+    public List<PartitionPositionBounds> 
getPositionsForBoundsIterator(Iterator<AbstractBounds<PartitionPosition>> 
boundsList)
+    {
+        // use the index to determine a minimal section for each range
+        List<PartitionPositionBounds> positions = new ArrayList<>();
+        while (boundsList.hasNext())
+        {
+            AbstractBounds<PartitionPosition> bounds = boundsList.next();
+            PartitionPositionBounds pb = getPositionsForBounds(bounds);
+            if (pb != null)
+                positions.add(pb);
+        }
+        return positions;
+    }
 
-            long left = getPosition(leftBound, Operator.GT);
-            long right = (rightBound.compareTo(last) > 0)
-                         ? uncompressedLength()
-                         : getPosition(rightBound, Operator.GT);
+    /**
+     * Determine the data positions in this SSTable that cover the given 
bounds.
+     *
+     * @return An [offset,end) pair that cover the given bounds in the 
datafile for this SSTable.
+     */
+    public PartitionPositionBounds 
getPositionsForBounds(AbstractBounds<PartitionPosition> bounds)
+    {
+        long left = getPosition(bounds.left, bounds.inclusiveLeft() ? 
Operator.GE : Operator.GT);
+        // Note: getPosition will apply a moved start if the sstable is in 
MOVED_START state.
+        if (left < 0) // empty range
+            return null;
 
-            if (left == right)
-                // empty range
-                continue;
+        long right = bounds.right.isMinimum() ? -1
+                                              : getPosition(bounds.right, 
bounds.inclusiveRight() ? Operator.GT
+                                                                               
                   : Operator.GE);
+        if (right < 0) // right is beyond end
+            right = uncompressedLength();   // this should also be correct for 
EARLY readers
 
-            assert left < right : String.format("Range=%s openReason=%s 
first=%s last=%s left=%d right=%d", range, openReason, first, last, left, 
right);
-            positions.add(new PartitionPositionBounds(left, right));
+        if (left >= right)
+        {
+            // empty range
+            return null;
+        }
+
+        return new PartitionPositionBounds(left, right);
+    }
+
+    /**
+     * Return a (offset, end] pair that covers the whole file.
+     */
+    public PartitionPositionBounds getPositionsForFullRange()
+    {
+        if (openReason != OpenReason.MOVED_START)
+            return new PartitionPositionBounds(0, uncompressedLength());
+        else
+        {
+            // query a full range, so that the required adjustments can be 
applied
+            PartitionPosition minToken = 
getPartitioner().getMinimumToken().minKeyBound();
+            return getPositionsForBounds(new Range<>(minToken, minToken));

Review Comment:
   Because I'm not sure we can't move an sstable beyond its last key, I changed 
the caller to make sure we can work with that and changed the doc to state that 
possibility.



##########
src/java/org/apache/cassandra/io/sstable/format/SSTableReader.java:
##########
@@ -728,27 +728,104 @@ public List<PartitionPositionBounds> 
getPositionsForRanges(Collection<Range<Toke
         for (Range<Token> range : Range.normalize(ranges))
         {
             assert !range.isWrapAround() || range.right.isMinimum();
-            // truncate the range so it at most covers the sstable
             AbstractBounds<PartitionPosition> bounds = 
Range.makeRowRange(range);
-            PartitionPosition leftBound = bounds.left.compareTo(first) > 0 ? 
bounds.left : first.getToken().minKeyBound();
-            PartitionPosition rightBound = bounds.right.isMinimum() ? 
last.getToken().maxKeyBound() : bounds.right;
+            PartitionPositionBounds pb = getPositionsForBounds(bounds);
+            if (pb != null)
+                positions.add(pb);
+        }
+        return positions;
+    }
 
-            if (leftBound.compareTo(last) > 0 || rightBound.compareTo(first) < 
0)
-                continue;
+    /**
+     * Get a list of data positions in this SSTable that correspond to the 
given list of bounds. This method will remove
+     * non-covered intervals, but will not correct order or overlap in the 
supplied list, e.g. if bounds overlap, the
+     * result will be sections of the data file that repeat the same positions.
+     *
+     * @return A sorted list of [offset,end) pairs corresponding to the given 
boundsList in the datafile for this
+     *         SSTable.
+     */
+    public List<PartitionPositionBounds> 
getPositionsForBoundsIterator(Iterator<AbstractBounds<PartitionPosition>> 
boundsList)
+    {
+        // use the index to determine a minimal section for each range
+        List<PartitionPositionBounds> positions = new ArrayList<>();
+        while (boundsList.hasNext())
+        {
+            AbstractBounds<PartitionPosition> bounds = boundsList.next();
+            PartitionPositionBounds pb = getPositionsForBounds(bounds);
+            if (pb != null)
+                positions.add(pb);
+        }
+        return positions;
+    }
 
-            long left = getPosition(leftBound, Operator.GT);
-            long right = (rightBound.compareTo(last) > 0)
-                         ? uncompressedLength()
-                         : getPosition(rightBound, Operator.GT);
+    /**
+     * Determine the data positions in this SSTable that cover the given 
bounds.
+     *
+     * @return An [offset,end) pair that cover the given bounds in the 
datafile for this SSTable.
+     */
+    public PartitionPositionBounds 
getPositionsForBounds(AbstractBounds<PartitionPosition> bounds)
+    {
+        long left = getPosition(bounds.left, bounds.inclusiveLeft() ? 
Operator.GE : Operator.GT);
+        // Note: getPosition will apply a moved start if the sstable is in 
MOVED_START state.
+        if (left < 0) // empty range
+            return null;
 
-            if (left == right)
-                // empty range
-                continue;
+        long right = bounds.right.isMinimum() ? -1
+                                              : getPosition(bounds.right, 
bounds.inclusiveRight() ? Operator.GT
+                                                                               
                   : Operator.GE);
+        if (right < 0) // right is beyond end
+            right = uncompressedLength();   // this should also be correct for 
EARLY readers
 
-            assert left < right : String.format("Range=%s openReason=%s 
first=%s last=%s left=%d right=%d", range, openReason, first, last, left, 
right);
-            positions.add(new PartitionPositionBounds(left, right));
+        if (left >= right)
+        {
+            // empty range
+            return null;
+        }
+
+        return new PartitionPositionBounds(left, right);
+    }
+
+    /**
+     * Return a (offset, end] pair that covers the whole file.
+     */
+    public PartitionPositionBounds getPositionsForFullRange()
+    {
+        if (openReason != OpenReason.MOVED_START)
+            return new PartitionPositionBounds(0, uncompressedLength());
+        else
+        {
+            // query a full range, so that the required adjustments can be 
applied
+            PartitionPosition minToken = 
getPartitioner().getMinimumToken().minKeyBound();
+            return getPositionsForBounds(new Range<>(minToken, minToken));
         }
-        return positions;
+    }
+
+    /**
+     * Calculate a total on-disk (compressed) size for the given partition 
positions.

Review Comment:
   Added



##########
src/java/org/apache/cassandra/io/sstable/format/SSTableSimpleScanner.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import com.google.common.collect.ImmutableSet;
+
+import org.apache.cassandra.db.DecoratedKey;
+import org.apache.cassandra.db.rows.UnfilteredRowIterator;
+import org.apache.cassandra.io.sstable.CorruptSSTableException;
+import org.apache.cassandra.io.sstable.ISSTableScanner;
+import org.apache.cassandra.io.sstable.SSTableIdentityIterator;
+import org.apache.cassandra.io.util.RandomAccessReader;
+import org.apache.cassandra.schema.TableMetadata;
+
+import static 
org.apache.cassandra.io.sstable.format.SSTableReader.PartitionPositionBounds;
+
+/// Simple SSTable scanner that reads sequentially through an SSTable without 
using the index.
+///
+/// This is a significant improvement for the performance of compaction over 
using the full-blown DataRange-capable
+/// [SSTableScanner] and enables correct calculation of data sizes to process.
+public class SSTableSimpleScanner
+implements ISSTableScanner
+{
+    private final AtomicBoolean isClosed = new AtomicBoolean(false);
+    private final RandomAccessReader dfile;
+    private final SSTableReader sstable;
+
+    private final Iterator<PartitionPositionBounds> rangeIterator;
+
+    private long bytesScannedInPreviousRanges;
+
+    private final long sizeInBytes;
+    private final long compressedSizeInBytes;
+
+    private long currentEndPosition;
+    private long currentStartPosition;
+
+    private SSTableIdentityIterator currentIterator;
+    private DecoratedKey lastKey;
+
+    public SSTableSimpleScanner(SSTableReader sstable,

Review Comment:
   Added.



##########
src/java/org/apache/cassandra/io/sstable/format/SSTableSimpleScanner.java:
##########
@@ -0,0 +1,185 @@
+/*
+ * 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;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import com.google.common.collect.ImmutableSet;
+
+import org.apache.cassandra.db.DecoratedKey;
+import org.apache.cassandra.db.rows.UnfilteredRowIterator;
+import org.apache.cassandra.io.sstable.CorruptSSTableException;
+import org.apache.cassandra.io.sstable.ISSTableScanner;
+import org.apache.cassandra.io.sstable.SSTableIdentityIterator;
+import org.apache.cassandra.io.util.RandomAccessReader;
+import org.apache.cassandra.schema.TableMetadata;
+
+import static 
org.apache.cassandra.io.sstable.format.SSTableReader.PartitionPositionBounds;
+
+/// Simple SSTable scanner that reads sequentially through an SSTable without 
using the index.

Review Comment:
   Sent a [DISCUSS] message to the dev mailing list to ask about this.



##########
src/java/org/apache/cassandra/io/sstable/format/bti/BtiTableReader.java:
##########
@@ -382,27 +380,6 @@ public UnfilteredRowIterator rowIterator(FileDataInput 
dataFileInput,
             return new SSTableIterator(this, dataFileInput, key, indexEntry, 
slices, selectedColumns, rowIndexFile);
     }
 
-    @Override

Review Comment:
   Removed the methods in two commits: removing the factory methods and the 
necessary `makeBounds` helpers in one, and removing the ability to iterate 
multiple ranges in the second. If you prefer to leave the latter in place, let 
me know to revert the second commit.



-- 
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]

Reply via email to