pkolaczk commented on code in PR #2556: URL: https://github.com/apache/cassandra/pull/2556#discussion_r1384695845
########## src/java/org/apache/cassandra/index/sai/memory/MemtableKeyRangeIterator.java: ########## @@ -0,0 +1,142 @@ +/* + * 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.index.sai.memory; + +import java.io.IOException; + +import org.apache.cassandra.db.Clustering; +import org.apache.cassandra.db.DataRange; +import org.apache.cassandra.db.PartitionPosition; +import org.apache.cassandra.db.Slice; +import org.apache.cassandra.db.Slices; +import org.apache.cassandra.db.filter.ClusteringIndexSliceFilter; +import org.apache.cassandra.db.filter.ColumnFilter; +import org.apache.cassandra.db.memtable.Memtable; +import org.apache.cassandra.db.partitions.UnfilteredPartitionIterator; +import org.apache.cassandra.db.rows.Row; +import org.apache.cassandra.db.rows.Unfiltered; +import org.apache.cassandra.db.rows.UnfilteredRowIterator; +import org.apache.cassandra.dht.AbstractBounds; +import org.apache.cassandra.dht.IPartitioner; +import org.apache.cassandra.dht.Token; +import org.apache.cassandra.index.sai.iterators.KeyRangeIterator; +import org.apache.cassandra.index.sai.utils.PrimaryKey; +import org.apache.cassandra.schema.TableMetadata; + +/** + * Iterates over primary keys in a memtable + */ +public class MemtableKeyRangeIterator extends KeyRangeIterator +{ + private final Memtable memtable; + private final PrimaryKey.Factory pkFactory; + private final AbstractBounds<PartitionPosition> keyRange; + private final ColumnFilter columns; + private UnfilteredPartitionIterator partitionIterator; + private UnfilteredRowIterator rowIterator; + + + private MemtableKeyRangeIterator(Memtable memtable, + PrimaryKey.Factory pkFactory, + AbstractBounds<PartitionPosition> keyRange) + { + super(pkFactory.createTokenOnly(keyRange.left.getToken()), + pkFactory.createTokenOnly(maxToken(keyRange, memtable.metadata().partitioner)), + memtable.operationCount()); + + TableMetadata metadata = memtable.metadata(); + this.memtable = memtable; + this.pkFactory = pkFactory; + this.keyRange = keyRange; + this.columns = ColumnFilter.selectionBuilder() + .addAll(metadata.partitionKeyColumns()) + .addAll(metadata.clusteringColumns()) + .build(); + + DataRange dataRange = new DataRange(keyRange, new ClusteringIndexSliceFilter(Slices.ALL, false)); + this.partitionIterator = memtable.partitionIterator(columns, dataRange, null); + this.rowIterator = null; + } + + private static Token maxToken(AbstractBounds<PartitionPosition> keyRange, IPartitioner partitioner) + { + return keyRange.right.getToken().isMinimum() ? partitioner.getMaximumToken() : keyRange.right.getToken(); + } + + public static MemtableKeyRangeIterator create(Memtable memtable, AbstractBounds<PartitionPosition> keyRange) + { + PrimaryKey.Factory pkFactory = new PrimaryKey.Factory(memtable.metadata().comparator); + return new MemtableKeyRangeIterator(memtable, pkFactory, keyRange); + } + + @Override + protected void performSkipTo(PrimaryKey nextKey) + { + AbstractBounds<PartitionPosition> keyRange = AbstractBounds.bounds(nextKey.partitionKey(), + true, Review Comment: lol, the inlay hint from Idea fooled me on this one. I stared at the code for good 10 seconds thinking wtf, they *are* aligned, and then noticed the hint that was present only at this line but not others ;) -- 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]

