adelapena commented on code in PR #2556:
URL: https://github.com/apache/cassandra/pull/2556#discussion_r1290253338
##########
src/java/org/apache/cassandra/index/sai/IndexContext.java:
##########
@@ -180,6 +181,11 @@ public boolean isNonFrozenCollection()
return TypeUtil.isNonFrozenCollection(columnMetadata.type);
}
+ public boolean isCollection()
Review Comment:
This new method seems unused
##########
src/java/org/apache/cassandra/index/sai/disk/IndexSearchResultIterator.java:
##########
@@ -61,6 +62,27 @@ public static IndexSearchResultIterator build(Expression
expression,
Collection<SSTableIndex>
sstableIndexes,
AbstractBounds<PartitionPosition> keyRange,
QueryContext queryContext)
+ {
+ KeyRangeIterator keyIterator = buildKeyIterator(expression,
sstableIndexes, keyRange, queryContext);
+
+ // For NOT CONTAINS or NOT CONTAINS KEY it is not enought to just
return the primary keys
Review Comment:
```suggestion
// For NOT CONTAINS or NOT CONTAINS KEY it is not enough to just
return the primary keys
```
##########
src/java/org/apache/cassandra/index/sai/disk/v1/PerColumnIndexFiles.java:
##########
@@ -22,6 +22,7 @@
import java.util.EnumMap;
import java.util.Map;
+import jnr.ffi.annotations.In;
Review Comment:
Nit: Unneeded change
##########
src/java/org/apache/cassandra/index/sai/disk/v1/V1SSTableIndex.java:
##########
@@ -33,7 +34,10 @@
import org.apache.cassandra.index.sai.IndexContext;
import org.apache.cassandra.index.sai.QueryContext;
import org.apache.cassandra.index.sai.SSTableContext;
+import org.apache.cassandra.index.sai.disk.PrimaryKeyMap;
+import org.apache.cassandra.index.sai.disk.PrimaryKeyMapIterator;
import org.apache.cassandra.index.sai.disk.SSTableIndex;
+import org.apache.cassandra.index.sai.disk.format.IndexDescriptor;
Review Comment:
Nit: unused imports
##########
src/java/org/apache/cassandra/index/sai/disk/SSTableIndex.java:
##########
@@ -36,7 +37,9 @@
import org.apache.cassandra.index.sai.IndexContext;
import org.apache.cassandra.index.sai.QueryContext;
import org.apache.cassandra.index.sai.SSTableContext;
+import org.apache.cassandra.index.sai.disk.format.IndexDescriptor;
import org.apache.cassandra.index.sai.disk.format.Version;
+import org.apache.cassandra.index.sai.disk.v1.V1OnDiskFormat;
Review Comment:
Nit: unused imports breaking CI (`ant check`).
##########
src/java/org/apache/cassandra/index/sai/iterators/KeyRangeAntiJoinIterator.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.iterators;
+
+import java.io.IOException;
+
+import org.apache.cassandra.index.sai.utils.PrimaryKey;
+import org.apache.cassandra.io.util.FileUtils;
+
+/**
+ * An iterator wrapper that wraps two iterators (left and right) and returns
the primary keys from the left iterator
+ * that do not match the primary keys from the right iterator. The keys
returned by the wrapped iterators must
+ * follow token-clustering order.
+ */
+public class KeyRangeAntiJoinIterator extends KeyRangeIterator
+{
+ final KeyRangeIterator left;
+ final KeyRangeIterator right;
Review Comment:
Nit: Can be `private final`
##########
src/java/org/apache/cassandra/index/sai/iterators/KeyRangeAntiJoinIterator.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.iterators;
+
+import java.io.IOException;
+
+import org.apache.cassandra.index.sai.utils.PrimaryKey;
+import org.apache.cassandra.io.util.FileUtils;
+
+/**
+ * An iterator wrapper that wraps two iterators (left and right) and returns
the primary keys from the left iterator
+ * that do not match the primary keys from the right iterator. The keys
returned by the wrapped iterators must
+ * follow token-clustering order.
+ */
+public class KeyRangeAntiJoinIterator extends KeyRangeIterator
+{
+ final KeyRangeIterator left;
+ final KeyRangeIterator right;
+
+ private PrimaryKey nextKeyToSkip = null;
+
+ private KeyRangeAntiJoinIterator(KeyRangeIterator.Builder.Statistics
statistics, KeyRangeIterator left, KeyRangeIterator right)
+ {
+ super(statistics);
+ this.left = left;
+ this.right = right;
+ }
+
+ public static KeyRangeAntiJoinIterator create(KeyRangeIterator left,
KeyRangeIterator right)
+ {
+ AntiJoinStatistics statistics = new AntiJoinStatistics();
+ statistics.update(left);
+ return new KeyRangeAntiJoinIterator(statistics, left, right);
+ }
+
+ protected void performSkipTo(PrimaryKey nextKey)
+ {
+ left.performSkipTo(nextKey);
+ right.performSkipTo(nextKey);
+ }
+
+ public void close() throws IOException
+ {
+ FileUtils.closeQuietly(left);
+ FileUtils.closeQuietly(right);
+ }
+
+
+ protected PrimaryKey computeNext()
Review Comment:
Nit: add `@Override`
##########
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:
Nit: misaligned argument
##########
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,
+
this.keyRange.right,
+
this.keyRange.inclusiveRight());
+ DataRange dataRange = new DataRange(keyRange, new
ClusteringIndexSliceFilter(Slices.ALL, false));
+ this.partitionIterator = memtable.partitionIterator(columns,
dataRange, null);
Review Comment:
```suggestion
partitionIterator = memtable.partitionIterator(columns, dataRange,
null);
```
##########
src/java/org/apache/cassandra/index/sai/memory/TrieMemoryIndex.java:
##########
@@ -80,6 +80,8 @@ public TrieMemoryIndex(IndexContext indexContext)
this.analyzerFactory = indexContext.getAnalyzerFactory();
this.validator = indexContext.getValidator();
this.isLiteral = TypeUtil.isLiteral(validator);
+
+
Review Comment:
Unneeded change
##########
src/java/org/apache/cassandra/utils/btree/BTree.java:
##########
@@ -347,8 +347,8 @@ public static <Compare, Existing extends Compare, Insert
extends Compare> Object
if (isEmpty(toUpdate))
{
- if (isSimple(updateF))
- return insert; // if update is empty and updateF is trivial,
return our new input
+// if (isSimple(updateF))
+// return insert; // if update is empty and updateF is trivial,
return our new input
Review Comment:
Can this be removed?
##########
src/java/org/apache/cassandra/index/sai/disk/v1/RowAwarePrimaryKeyMap.java:
##########
@@ -161,6 +161,11 @@ public long rowIdFromPrimaryKey(PrimaryKey key)
return
triePrefixSearcher.prefixSearch(key.asComparableBytes(ByteComparable.Version.OSS50));
}
+ public long count()
Review Comment:
Nit: add `@Override`
##########
src/java/org/apache/cassandra/index/sai/iterators/KeyRangeAntiJoinIterator.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.iterators;
+
+import java.io.IOException;
+
+import org.apache.cassandra.index.sai.utils.PrimaryKey;
+import org.apache.cassandra.io.util.FileUtils;
+
+/**
+ * An iterator wrapper that wraps two iterators (left and right) and returns
the primary keys from the left iterator
+ * that do not match the primary keys from the right iterator. The keys
returned by the wrapped iterators must
+ * follow token-clustering order.
+ */
+public class KeyRangeAntiJoinIterator extends KeyRangeIterator
+{
+ final KeyRangeIterator left;
+ final KeyRangeIterator right;
+
+ private PrimaryKey nextKeyToSkip = null;
+
+ private KeyRangeAntiJoinIterator(KeyRangeIterator.Builder.Statistics
statistics, KeyRangeIterator left, KeyRangeIterator right)
+ {
+ super(statistics);
+ this.left = left;
+ this.right = right;
+ }
+
+ public static KeyRangeAntiJoinIterator create(KeyRangeIterator left,
KeyRangeIterator right)
+ {
+ AntiJoinStatistics statistics = new AntiJoinStatistics();
+ statistics.update(left);
+ return new KeyRangeAntiJoinIterator(statistics, left, right);
+ }
+
+ protected void performSkipTo(PrimaryKey nextKey)
Review Comment:
Nit: add `@Override`
##########
src/java/org/apache/cassandra/index/sai/iterators/KeyRangeAntiJoinIterator.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.iterators;
+
+import java.io.IOException;
+
+import org.apache.cassandra.index.sai.utils.PrimaryKey;
+import org.apache.cassandra.io.util.FileUtils;
+
+/**
+ * An iterator wrapper that wraps two iterators (left and right) and returns
the primary keys from the left iterator
+ * that do not match the primary keys from the right iterator. The keys
returned by the wrapped iterators must
+ * follow token-clustering order.
+ */
+public class KeyRangeAntiJoinIterator extends KeyRangeIterator
+{
+ final KeyRangeIterator left;
+ final KeyRangeIterator right;
+
+ private PrimaryKey nextKeyToSkip = null;
+
+ private KeyRangeAntiJoinIterator(KeyRangeIterator.Builder.Statistics
statistics, KeyRangeIterator left, KeyRangeIterator right)
+ {
+ super(statistics);
+ this.left = left;
+ this.right = right;
+ }
+
+ public static KeyRangeAntiJoinIterator create(KeyRangeIterator left,
KeyRangeIterator right)
+ {
+ AntiJoinStatistics statistics = new AntiJoinStatistics();
+ statistics.update(left);
+ return new KeyRangeAntiJoinIterator(statistics, left, right);
+ }
+
+ protected void performSkipTo(PrimaryKey nextKey)
+ {
+ left.performSkipTo(nextKey);
+ right.performSkipTo(nextKey);
+ }
+
+ public void close() throws IOException
Review Comment:
Nit: add `@Override`
##########
src/java/org/apache/cassandra/index/sai/iterators/KeyRangeIterator.java:
##########
@@ -131,6 +131,11 @@ protected final boolean tryToComputeNext()
return hasNext;
}
+ public PrimaryKey nextOrNull()
Review Comment:
Nit: add `@Nullable`
##########
src/java/org/apache/cassandra/index/sai/disk/PrimaryKeyMapIterator.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.disk;
+
+import java.io.IOException;
+
+import org.apache.cassandra.db.PartitionPosition;
+import org.apache.cassandra.dht.AbstractBounds;
+import org.apache.cassandra.dht.Token;
+import org.apache.cassandra.index.sai.SSTableContext;
+import org.apache.cassandra.index.sai.iterators.KeyRangeIterator;
+import org.apache.cassandra.index.sai.utils.PrimaryKey;
+
+/**
+ * Iterates keys in the PrimaryKeyMap of an SSTable.
Review Comment:
```suggestion
* Iterates keys in the {@link PrimaryKeyMap} of a SSTable.
```
##########
src/java/org/apache/cassandra/index/sai/memory/MemtableIndex.java:
##########
@@ -30,18 +30,23 @@
import org.apache.cassandra.index.sai.plan.Expression;
import org.apache.cassandra.index.sai.utils.PrimaryKeys;
import org.apache.cassandra.index.sai.iterators.KeyRangeIterator;
+import org.apache.cassandra.index.sai.utils.TypeUtil;
+import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.Pair;
import org.apache.cassandra.utils.bytecomparable.ByteComparable;
public class MemtableIndex
{
private final TrieMemoryIndex index;
+
+ private final IndexContext indexContext;
Review Comment:
Unused attribute
##########
src/java/org/apache/cassandra/index/sai/memory/MemtableIndex.java:
##########
@@ -30,18 +30,23 @@
import org.apache.cassandra.index.sai.plan.Expression;
import org.apache.cassandra.index.sai.utils.PrimaryKeys;
import org.apache.cassandra.index.sai.iterators.KeyRangeIterator;
+import org.apache.cassandra.index.sai.utils.TypeUtil;
+import org.apache.cassandra.utils.ByteBufferUtil;
Review Comment:
Nit: unused imports
##########
src/java/org/apache/cassandra/index/sai/iterators/KeyRangeAntiJoinIterator.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.iterators;
+
+import java.io.IOException;
+
+import org.apache.cassandra.index.sai.utils.PrimaryKey;
+import org.apache.cassandra.io.util.FileUtils;
+
+/**
+ * An iterator wrapper that wraps two iterators (left and right) and returns
the primary keys from the left iterator
+ * that do not match the primary keys from the right iterator. The keys
returned by the wrapped iterators must
+ * follow token-clustering order.
+ */
+public class KeyRangeAntiJoinIterator extends KeyRangeIterator
+{
+ final KeyRangeIterator left;
+ final KeyRangeIterator right;
+
+ private PrimaryKey nextKeyToSkip = null;
+
+ private KeyRangeAntiJoinIterator(KeyRangeIterator.Builder.Statistics
statistics, KeyRangeIterator left, KeyRangeIterator right)
+ {
+ super(statistics);
+ this.left = left;
+ this.right = right;
+ }
+
+ public static KeyRangeAntiJoinIterator create(KeyRangeIterator left,
KeyRangeIterator right)
+ {
+ AntiJoinStatistics statistics = new AntiJoinStatistics();
+ statistics.update(left);
+ return new KeyRangeAntiJoinIterator(statistics, left, right);
+ }
+
+ protected void performSkipTo(PrimaryKey nextKey)
+ {
+ left.performSkipTo(nextKey);
+ right.performSkipTo(nextKey);
+ }
+
+ public void close() throws IOException
+ {
+ FileUtils.closeQuietly(left);
+ FileUtils.closeQuietly(right);
+ }
+
+
+ protected PrimaryKey computeNext()
+ {
+ if (nextKeyToSkip == null)
+ nextKeyToSkip = right.nextOrNull();
+
+ PrimaryKey key = left.nextOrNull();
+ int cmp = compare(key, nextKeyToSkip);
+
+ while (key != null && cmp >= 0)
+ {
+ if (cmp == 0)
+ {
+ key = left.nextOrNull();
+ nextKeyToSkip = right.nextOrNull();
+ }
+ else
+ {
+ nextKeyToSkip = right.skipTo(key);
+ }
+ cmp = compare(key, nextKeyToSkip);
+ }
+
+ return key != null ? key : endOfData();
+ }
+
+ private int compare(PrimaryKey key1, PrimaryKey key2)
Review Comment:
Nit: can be `static`
##########
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;
+
+
Review Comment:
```suggestion
```
##########
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,
+
this.keyRange.right,
+
this.keyRange.inclusiveRight());
+ DataRange dataRange = new DataRange(keyRange, new
ClusteringIndexSliceFilter(Slices.ALL, false));
+ this.partitionIterator = memtable.partitionIterator(columns,
dataRange, null);
+ if (partitionIterator.hasNext())
+ {
+ this.rowIterator = partitionIterator.next();
+ if (!nextKey.hasEmptyClustering() &&
rowIterator.partitionKey().equals(nextKey.partitionKey()))
+ {
+ Slice slice = Slice.make(nextKey.clustering(),
Clustering.EMPTY);
+ Slices slices =
Slices.with(this.memtable.metadata().comparator, slice);
+ this.rowIterator =
memtable.rowIterator(nextKey.partitionKey(), slices, this.columns, false, null);
Review Comment:
```suggestion
Slices slices = Slices.with(memtable.metadata().comparator,
slice);
rowIterator = memtable.rowIterator(nextKey.partitionKey(),
slices, columns, false, null);
```
##########
src/java/org/apache/cassandra/index/sai/plan/Operation.java:
##########
@@ -41,7 +41,8 @@ public class Operation
{
public enum BooleanOperator
{
- AND((a, b) -> a & b);
+ AND((a, b) -> a & b),
+ AND_NOT((a, b) -> a & !b);
Review Comment:
`AND_NOT` is not used anywhere.
--
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]