You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by sa...@apache.org on 2015/08/22 01:48:39 UTC
[08/15] cassandra git commit: New 2i API and implementations for
built in indexes
http://git-wip-us.apache.org/repos/asf/cassandra/blob/0626be86/test/unit/org/apache/cassandra/db/RangeTombstoneTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/db/RangeTombstoneTest.java b/test/unit/org/apache/cassandra/db/RangeTombstoneTest.java
index 70fbfe3..ffc43c5 100644
--- a/test/unit/org/apache/cassandra/db/RangeTombstoneTest.java
+++ b/test/unit/org/apache/cassandra/db/RangeTombstoneTest.java
@@ -22,37 +22,33 @@ import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
-import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterators;
-
-import org.apache.cassandra.io.sstable.format.SSTableReader;
import org.junit.BeforeClass;
import org.junit.Test;
import org.apache.cassandra.SchemaLoader;
-import org.apache.cassandra.config.*;
-import org.apache.cassandra.Util;
import org.apache.cassandra.UpdateBuilder;
+import org.apache.cassandra.Util;
+import org.apache.cassandra.config.ColumnDefinition;
+import org.apache.cassandra.cql3.statements.IndexTarget;
import org.apache.cassandra.db.compaction.CompactionManager;
-import org.apache.cassandra.db.filter.*;
-import org.apache.cassandra.db.rows.*;
-import org.apache.cassandra.db.partitions.*;
-import org.apache.cassandra.db.index.PerColumnSecondaryIndex;
-import org.apache.cassandra.db.index.SecondaryIndex;
-import org.apache.cassandra.db.index.SecondaryIndexSearcher;
-import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.db.filter.ColumnFilter;
import org.apache.cassandra.db.marshal.Int32Type;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.db.partitions.*;
+import org.apache.cassandra.db.rows.*;
import org.apache.cassandra.exceptions.ConfigurationException;
+import org.apache.cassandra.index.StubIndex;
+import org.apache.cassandra.io.sstable.format.SSTableReader;
import org.apache.cassandra.io.sstable.metadata.StatsMetadata;
import org.apache.cassandra.schema.IndexMetadata;
import org.apache.cassandra.schema.KeyspaceParams;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.FBUtilities;
-import org.apache.cassandra.utils.concurrent.OpOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -71,7 +67,7 @@ public class RangeTombstoneTest
KeyspaceParams.simple(1),
SchemaLoader.standardCFMD(KSNAME,
CFNAME,
- 0,
+ 1,
UTF8Type.instance,
Int32Type.instance,
Int32Type.instance));
@@ -468,22 +464,26 @@ public class RangeTombstoneTest
cfs.disableAutoCompaction();
ColumnDefinition cd = cfs.metadata.getColumnDefinition(indexedColumnName).copy();
- IndexMetadata indexDef = IndexMetadata.legacyIndex(cd,
- "test_index",
- IndexMetadata.IndexType.CUSTOM,
- ImmutableMap.of(SecondaryIndex.CUSTOM_INDEX_OPTION_NAME,
- TestIndex.class.getName()));
+ IndexMetadata indexDef = IndexMetadata.singleColumnIndex(cd,
+ "test_index",
+ IndexMetadata.IndexType.CUSTOM,
+ ImmutableMap.of(IndexTarget.CUSTOM_INDEX_OPTION_NAME,
+ StubIndex.class.getName()));
if (!cfs.metadata.getIndexes().get("test_index").isPresent())
cfs.metadata.indexes(cfs.metadata.getIndexes().with(indexDef));
- Future<?> rebuild = cfs.indexManager.addIndexedColumn(indexDef);
+ Future<?> rebuild = cfs.indexManager.addIndex(indexDef);
// If rebuild there is, wait for the rebuild to finish so it doesn't race with the following insertions
if (rebuild != null)
rebuild.get();
- TestIndex index = ((TestIndex)cfs.indexManager.getIndexForColumn(cd));
- index.resetCounts();
+ StubIndex index = (StubIndex)cfs.indexManager.listIndexes()
+ .stream()
+ .filter(i -> "test_index".equals(i.getIndexName()))
+ .findFirst()
+ .orElseThrow(() -> new RuntimeException(new AssertionError("Index not found")));
+ index.reset();
UpdateBuilder builder = UpdateBuilder.create(cfs.metadata, key).withTimestamp(0);
for (int i = 0; i < 10; i++)
@@ -494,14 +494,14 @@ public class RangeTombstoneTest
new RowUpdateBuilder(cfs.metadata, 0, key).addRangeTombstone(0, 7).build().applyUnsafe();
cfs.forceBlockingFlush();
- assertEquals(10, index.inserts.size());
+ assertEquals(10, index.rowsInserted.size());
CompactionManager.instance.performMaximal(cfs, false);
// compacted down to single sstable
assertEquals(1, cfs.getLiveSSTables().size());
- assertEquals(8, index.deletes.size());
+ assertEquals(8, index.rowsDeleted.size());
}
@Test
@@ -560,21 +560,26 @@ public class RangeTombstoneTest
cfs.disableAutoCompaction();
ColumnDefinition cd = cfs.metadata.getColumnDefinition(indexedColumnName).copy();
- IndexMetadata indexDef = IndexMetadata.legacyIndex(cd,
- "test_index",
- IndexMetadata.IndexType.CUSTOM,
- ImmutableMap.of(SecondaryIndex.CUSTOM_INDEX_OPTION_NAME,
- TestIndex.class.getName()));
+ IndexMetadata indexDef = IndexMetadata.singleColumnIndex(cd,
+ "test_index",
+ IndexMetadata.IndexType.CUSTOM,
+ ImmutableMap.of(IndexTarget.CUSTOM_INDEX_OPTION_NAME,
+ StubIndex.class.getName()));
if (!cfs.metadata.getIndexes().get("test_index").isPresent())
cfs.metadata.indexes(cfs.metadata.getIndexes().with(indexDef));
- Future<?> rebuild = cfs.indexManager.addIndexedColumn(indexDef);
+ Future<?> rebuild = cfs.indexManager.addIndex(indexDef);
// If rebuild there is, wait for the rebuild to finish so it doesn't race with the following insertions
if (rebuild != null)
rebuild.get();
- TestIndex index = ((TestIndex)cfs.indexManager.getIndexForColumn(cd));
- index.resetCounts();
+
+ StubIndex index = (StubIndex)cfs.indexManager.listIndexes()
+ .stream()
+ .filter(i -> "test_index".equals(i.getIndexName()))
+ .findFirst()
+ .orElseThrow(() -> new RuntimeException(new AssertionError("Index not found")));
+ index.reset();
UpdateBuilder.create(cfs.metadata, key).withTimestamp(0).newRow(1).add("val", 1).applyUnsafe();
@@ -588,8 +593,8 @@ public class RangeTombstoneTest
// We should have 1 insert and 1 update to the indexed "1" column
// CASSANDRA-6640 changed index update to just update, not insert then delete
- assertEquals(1, index.inserts.size());
- assertEquals(1, index.updates.size());
+ assertEquals(1, index.rowsInserted.size());
+ assertEquals(1, index.rowsUpdated.size());
}
private static ByteBuffer bb(int i)
@@ -601,68 +606,4 @@ public class RangeTombstoneTest
{
return ByteBufferUtil.toInt(bb);
}
-
- public static class TestIndex extends PerColumnSecondaryIndex
- {
- public List<Cell> inserts = new ArrayList<>();
- public List<Cell> deletes = new ArrayList<>();
- public List<Cell> updates = new ArrayList<>();
-
- public void resetCounts()
- {
- inserts.clear();
- deletes.clear();
- updates.clear();
- }
-
- public void delete(ByteBuffer rowKey, Clustering clustering, Cell cell, OpOrder.Group opGroup, int nowInSec)
- {
- deletes.add(cell);
- }
-
- @Override
- public void deleteForCleanup(ByteBuffer rowKey, Clustering clustering, Cell cell, OpOrder.Group opGroup, int nowInSec) {}
-
- public void insert(ByteBuffer rowKey, Clustering clustering, Cell cell, OpOrder.Group opGroup)
- {
- inserts.add(cell);
- }
-
- public void update(ByteBuffer rowKey, Clustering clustering, Cell oldCell, Cell cell, OpOrder.Group opGroup, int nowInSec)
- {
- updates.add(cell);
- }
-
- public void init(){}
-
- public void reload(){}
-
- public void validateOptions(CFMetaData cfm, IndexMetadata def) throws ConfigurationException{}
-
- public String getIndexName(){ return "TestIndex";}
-
- protected SecondaryIndexSearcher createSecondaryIndexSearcher(Set<ColumnDefinition> columns) { return null; };
-
- public void forceBlockingFlush(){}
-
- public ColumnFamilyStore getIndexCfs(){ return null; }
-
- public void removeIndex(ByteBuffer columnName){}
-
- public void invalidate(){}
-
- public void validate(DecoratedKey key) {}
- public void validate(ByteBuffer value, CellPath path) {}
- public void validate(Clustering clustering) {}
-
- public void truncateBlocking(long truncatedAt) { }
-
- public boolean indexes(ColumnDefinition name) { return true; }
-
- @Override
- public long estimateResultRows()
- {
- return 0;
- }
- }
}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/0626be86/test/unit/org/apache/cassandra/db/ScrubTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/db/ScrubTest.java b/test/unit/org/apache/cassandra/db/ScrubTest.java
index 52b2aa8..5887fd4 100644
--- a/test/unit/org/apache/cassandra/db/ScrubTest.java
+++ b/test/unit/org/apache/cassandra/db/ScrubTest.java
@@ -18,10 +18,7 @@
*/
package org.apache.cassandra.db;
-import java.io.File;
-import java.io.IOError;
-import java.io.IOException;
-import java.io.RandomAccessFile;
+import java.io.*;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.ExecutionException;
@@ -31,10 +28,7 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.apache.cassandra.OrderedJUnit4ClassRunner;
-import org.apache.cassandra.SchemaLoader;
-import org.apache.cassandra.UpdateBuilder;
-import org.apache.cassandra.Util;
+import org.apache.cassandra.*;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.cql3.Operator;
import org.apache.cassandra.cql3.QueryProcessor;
@@ -44,7 +38,7 @@ import org.apache.cassandra.db.compaction.OperationType;
import org.apache.cassandra.db.compaction.Scrubber;
import org.apache.cassandra.db.lifecycle.LifecycleTransaction;
import org.apache.cassandra.db.lifecycle.TransactionLog;
-import org.apache.cassandra.db.marshal.*;
+import org.apache.cassandra.db.marshal.LongType;
import org.apache.cassandra.db.marshal.UUIDType;
import org.apache.cassandra.db.partitions.Partition;
import org.apache.cassandra.db.partitions.PartitionUpdate;
@@ -60,7 +54,10 @@ import org.apache.cassandra.io.util.FileUtils;
import org.apache.cassandra.schema.KeyspaceParams;
import org.apache.cassandra.utils.ByteBufferUtil;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
@RunWith(OrderedJUnit4ClassRunner.class)
@@ -607,7 +604,7 @@ public class ScrubTest
assertOrdered(Util.cmd(cfs).filterOn(colName, Operator.EQ, 1L).build(), numRows / 2);
// scrub index
- Set<ColumnFamilyStore> indexCfss = cfs.indexManager.getIndexesBackedByCfs();
+ Set<ColumnFamilyStore> indexCfss = cfs.indexManager.getAllIndexColumnFamilyStores();
assertTrue(indexCfss.size() == 1);
for(ColumnFamilyStore indexCfs : indexCfss)
{
http://git-wip-us.apache.org/repos/asf/cassandra/blob/0626be86/test/unit/org/apache/cassandra/db/SecondaryIndexTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/db/SecondaryIndexTest.java b/test/unit/org/apache/cassandra/db/SecondaryIndexTest.java
index 115b13c..c017675 100644
--- a/test/unit/org/apache/cassandra/db/SecondaryIndexTest.java
+++ b/test/unit/org/apache/cassandra/db/SecondaryIndexTest.java
@@ -33,18 +33,20 @@ import org.apache.cassandra.SchemaLoader;
import org.apache.cassandra.Util;
import org.apache.cassandra.config.ColumnDefinition;
import org.apache.cassandra.cql3.Operator;
-import org.apache.cassandra.db.index.SecondaryIndex;
-import org.apache.cassandra.db.index.SecondaryIndexSearcher;
import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.db.partitions.*;
import org.apache.cassandra.db.rows.Row;
import org.apache.cassandra.exceptions.ConfigurationException;
+import org.apache.cassandra.index.Index;
import org.apache.cassandra.schema.IndexMetadata;
import org.apache.cassandra.schema.KeyspaceParams;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.FBUtilities;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class SecondaryIndexTest
@@ -111,9 +113,9 @@ public class SecondaryIndexTest
.columns("birthdate")
.filterOn("birthdate", Operator.EQ, 1L)
.build();
- List<SecondaryIndexSearcher> searchers = cfs.indexManager.getIndexSearchersFor(rc);
- assertEquals(searchers.size(), 1);
- try (ReadOrderGroup orderGroup = rc.startOrderGroup(); UnfilteredPartitionIterator pi = searchers.get(0).search(rc, orderGroup))
+
+ Index.Searcher searcher = cfs.indexManager.getBestIndexFor(rc).searcherFor(rc);
+ try (ReadOrderGroup orderGroup = rc.startOrderGroup(); UnfilteredPartitionIterator pi = searcher.search(orderGroup))
{
assertTrue(pi.hasNext());
pi.next().close();
@@ -198,7 +200,7 @@ public class SecondaryIndexTest
// verify that it's not being indexed under any other value either
ReadCommand rc = Util.cmd(cfs).build();
- assertEquals(0, cfs.indexManager.getIndexSearchersFor(rc).size());
+ assertNull(cfs.indexManager.getBestIndexFor(rc));
// resurrect w/ a newer timestamp
new RowUpdateBuilder(cfs.metadata, 2, "k1").clustering("c").add("birthdate", 1L).build().apply();;
@@ -213,14 +215,16 @@ public class SecondaryIndexTest
assertIndexedOne(cfs, col, 1L);
// delete the entire row (w/ newer timestamp this time)
+ // todo - checking the # of index searchers for the command is probably not the best thing to test here
RowUpdateBuilder.deleteRow(cfs.metadata, 3, "k1", "c").applyUnsafe();
rc = Util.cmd(cfs).build();
- assertEquals(0, cfs.indexManager.getIndexSearchersFor(rc).size());
+ assertNull(cfs.indexManager.getBestIndexFor(rc));
// make sure obsolete mutations don't generate an index entry
+ // todo - checking the # of index searchers for the command is probably not the best thing to test here
new RowUpdateBuilder(cfs.metadata, 3, "k1").clustering("c").add("birthdate", 1L).build().apply();;
rc = Util.cmd(cfs).build();
- assertEquals(0, cfs.indexManager.getIndexSearchersFor(rc).size());
+ assertNull(cfs.indexManager.getBestIndexFor(rc));
}
@Test
@@ -292,7 +296,9 @@ public class SecondaryIndexTest
keyspace.getColumnFamilyStore(WITH_KEYS_INDEX).forceBlockingFlush();
// now apply another update, but force the index update to be skipped
- keyspace.apply(new RowUpdateBuilder(cfs.metadata, 2, "k1").noRowMarker().add("birthdate", 2L).build(), true, false);
+ keyspace.apply(new RowUpdateBuilder(cfs.metadata, 2, "k1").noRowMarker().add("birthdate", 2L).build(),
+ true,
+ false);
// Now searching the index for either the old or new value should return 0 rows
// because the new value was not indexed and the old value should be ignored
@@ -303,7 +309,9 @@ public class SecondaryIndexTest
// now, reset back to the original value, still skipping the index update, to
// make sure the value was expunged from the index when it was discovered to be inconsistent
- keyspace.apply(new RowUpdateBuilder(cfs.metadata, 3, "k1").noRowMarker().add("birthdate", 1L).build(), true, false);
+ keyspace.apply(new RowUpdateBuilder(cfs.metadata, 3, "k1").noRowMarker().add("birthdate", 1L).build(),
+ true,
+ false);
assertIndexedNone(cfs, col, 1L);
}
@@ -328,7 +336,9 @@ public class SecondaryIndexTest
assertIndexedOne(cfs, col, 10l);
// now apply another update, but force the index update to be skipped
- keyspace.apply(new RowUpdateBuilder(cfs.metadata, 1, "k1").clustering("c").add("birthdate", 20l).build(), true, false);
+ keyspace.apply(new RowUpdateBuilder(cfs.metadata, 1, "k1").clustering("c").add("birthdate", 20l).build(),
+ true,
+ false);
// Now searching the index for either the old or new value should return 0 rows
// because the new value was not indexed and the old value should be ignored
@@ -422,34 +432,54 @@ public class SecondaryIndexTest
ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(COMPOSITE_INDEX_TO_BE_ADDED);
// create a row and update the birthdate value, test that the index query fetches the new version
- DecoratedKey dk = Util.dk("k1");
new RowUpdateBuilder(cfs.metadata, 0, "k1").clustering("c").add("birthdate", 1L).build().applyUnsafe();
ColumnDefinition old = cfs.metadata.getColumnDefinition(ByteBufferUtil.bytes("birthdate"));
- IndexMetadata indexDef = IndexMetadata.legacyIndex(old,
- "birthdate_index",
- IndexMetadata.IndexType.COMPOSITES,
- Collections.EMPTY_MAP);
+ IndexMetadata indexDef = IndexMetadata.singleColumnIndex(old,
+ "birthdate_index",
+ IndexMetadata.IndexType.COMPOSITES,
+ Collections.EMPTY_MAP);
cfs.metadata.indexes(cfs.metadata.getIndexes().with(indexDef));
- Future<?> future = cfs.indexManager.addIndexedColumn(indexDef);
+ Future<?> future = cfs.indexManager.addIndex(indexDef);
future.get();
+
// we had a bug (CASSANDRA-2244) where index would get created but not flushed -- check for that
+ // the way we find the index cfs is a bit convoluted at the moment
ColumnDefinition cDef = cfs.metadata.getColumnDefinition(ByteBufferUtil.bytes("birthdate"));
- assert cfs.indexManager.getIndexForColumn(cDef).getIndexCfs().getLiveSSTables().size() > 0;
-
+ String indexName = getIndexNameForColumn(cfs, cDef);
+ assertNotNull(indexName);
+ boolean flushed = false;
+ for (ColumnFamilyStore indexCfs : cfs.indexManager.getAllIndexColumnFamilyStores())
+ {
+ if (indexCfs.name.equals(indexName))
+ flushed = indexCfs.getLiveSSTables().size() > 0;
+ }
+ assertTrue(flushed);
assertIndexedOne(cfs, ByteBufferUtil.bytes("birthdate"), 1L);
// validate that drop clears it out & rebuild works (CASSANDRA-2320)
- SecondaryIndex indexedCfs = cfs.indexManager.getIndexForColumn(cDef);
- cfs.indexManager.removeIndexedColumn(ByteBufferUtil.bytes("birthdate"));
- assert !indexedCfs.isIndexBuilt(ByteBufferUtil.bytes("birthdate"));
+ assertTrue(cfs.getBuiltIndexes().contains(indexName));
+ cfs.indexManager.removeIndex(indexDef.name);
+ assertFalse(cfs.getBuiltIndexes().contains(indexName));
// rebuild & re-query
- future = cfs.indexManager.addIndexedColumn(indexDef);
+ future = cfs.indexManager.addIndex(indexDef);
future.get();
assertIndexedOne(cfs, ByteBufferUtil.bytes("birthdate"), 1L);
}
+ private String getIndexNameForColumn(ColumnFamilyStore cfs, ColumnDefinition column)
+ {
+ // this is mega-ugly because there is a mismatch between the name of an index
+ // stored in schema metadata & the name used to refer to that index in other
+ // places (such as system.IndexInfo). Hopefully this is temporary and
+ // Index.getIndexName() can be made equalivalent to Index.getIndexMetadata().name
+ // (ideally even removing the former completely)
+ Collection<IndexMetadata> indexes = cfs.metadata.getIndexes().get(column);
+ assertEquals(1, indexes.size());
+ return cfs.indexManager.getIndex(indexes.iterator().next()).getIndexName();
+ }
+
@Test
public void testKeysSearcherSimple() throws Exception
{
@@ -460,6 +490,7 @@ public class SecondaryIndexTest
for (int i = 0; i < 10; i++)
new RowUpdateBuilder(cfs.metadata, 0, "k" + i).noRowMarker().add("birthdate", 1l).build().applyUnsafe();
+ assertIndexedCount(cfs, ByteBufferUtil.bytes("birthdate"), 1l, 10);
cfs.forceBlockingFlush();
assertIndexedCount(cfs, ByteBufferUtil.bytes("birthdate"), 1l, 10);
}
@@ -477,13 +508,14 @@ public class SecondaryIndexTest
ColumnDefinition cdef = cfs.metadata.getColumnDefinition(col);
ReadCommand rc = Util.cmd(cfs).filterOn(cdef.name.toString(), Operator.EQ, ((AbstractType) cdef.cellValueType()).decompose(val)).build();
- List<SecondaryIndexSearcher> searchers = cfs.indexManager.getIndexSearchersFor(rc);
+ Index.Searcher searcher = cfs.indexManager.getBestIndexFor(rc).searcherFor(rc);
if (count != 0)
- assertTrue(searchers.size() > 0);
+ assertNotNull(searcher);
try (ReadOrderGroup orderGroup = rc.startOrderGroup();
- PartitionIterator iter = UnfilteredPartitionIterators.filter(searchers.get(0).search(rc, orderGroup),
- FBUtilities.nowInSeconds())) {
+ PartitionIterator iter = UnfilteredPartitionIterators.filter(searcher.search(orderGroup),
+ FBUtilities.nowInSeconds()))
+ {
assertEquals(count, Util.size(iter));
}
}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/0626be86/test/unit/org/apache/cassandra/db/index/PerRowSecondaryIndexTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/db/index/PerRowSecondaryIndexTest.java b/test/unit/org/apache/cassandra/db/index/PerRowSecondaryIndexTest.java
deleted file mode 100644
index 8996ded..0000000
--- a/test/unit/org/apache/cassandra/db/index/PerRowSecondaryIndexTest.java
+++ /dev/null
@@ -1,304 +0,0 @@
-/*
- * 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.db.index;
-
-import java.nio.ByteBuffer;
-import java.util.Arrays;
-import java.util.Set;
-
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-import junit.framework.Assert;
-import org.apache.cassandra.SchemaLoader;
-import org.apache.cassandra.config.CFMetaData;
-import org.apache.cassandra.config.ColumnDefinition;
-import org.apache.cassandra.config.Schema;
-import org.apache.cassandra.cql3.ColumnIdentifier;
-import org.apache.cassandra.cql3.QueryProcessor;
-import org.apache.cassandra.cql3.UntypedResultSet;
-import org.apache.cassandra.db.*;
-import org.apache.cassandra.db.rows.*;
-import org.apache.cassandra.db.filter.RowFilter;
-import org.apache.cassandra.db.partitions.SingletonUnfilteredPartitionIterator;
-import org.apache.cassandra.db.partitions.UnfilteredPartitionIterator;
-import org.apache.cassandra.db.partitions.PartitionUpdate;
-import org.apache.cassandra.exceptions.ConfigurationException;
-import org.apache.cassandra.exceptions.InvalidRequestException;
-import org.apache.cassandra.schema.IndexMetadata;
-import org.apache.cassandra.schema.KeyspaceParams;
-import org.apache.cassandra.utils.ByteBufferUtil;
-import org.apache.cassandra.utils.FBUtilities;
-import org.apache.cassandra.utils.concurrent.OpOrder;
-
-import static org.junit.Assert.*;
-
-public class PerRowSecondaryIndexTest
-{
-
- // test that when index(key) is called on a PRSI index,
- // the data to be indexed can be read using the supplied
- // key. TestIndex.index(key) simply reads the data to be
- // indexed & stashes it in a static variable for inspection
- // in the test.
-
- private static final String KEYSPACE1 = "PerRowSecondaryIndexTest";
- private static final String CF_INDEXED = "Indexed1";
-
- @BeforeClass
- public static void defineSchema() throws ConfigurationException
- {
- SchemaLoader.prepareServer();
- SchemaLoader.createKeyspace(KEYSPACE1,
- KeyspaceParams.simple(1),
- SchemaLoader.perRowIndexedCFMD(KEYSPACE1, CF_INDEXED));
- }
-
- @Before
- public void clearTestStub()
- {
- PerRowSecondaryIndexTest.TestIndex.reset();
- }
-
- @Test
- public void testIndexInsertAndUpdate()
- {
- int nowInSec = FBUtilities.nowInSeconds();
-
- // create a row then test that the configured index instance was able to read the row
- CFMetaData cfm = Schema.instance.getCFMetaData(KEYSPACE1, CF_INDEXED);
- ColumnDefinition cdef = cfm.getColumnDefinition(new ColumnIdentifier("indexed", true));
-
- RowUpdateBuilder builder = new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), "k1");
- builder.add("indexed", ByteBufferUtil.bytes("foo"));
- builder.build().apply();
-
-
- UnfilteredRowIterator indexedRow = PerRowSecondaryIndexTest.TestIndex.LAST_INDEXED_PARTITION;
- assertNotNull(indexedRow);
- assertEquals(ByteBufferUtil.bytes("foo"), UnfilteredRowIterators.filter(indexedRow, nowInSec).next().getCell(cdef).value());
-
- // update the row and verify what was indexed
- builder = new RowUpdateBuilder(cfm, FBUtilities.timestampMicros() + 1 , "k1");
- builder.add("indexed", ByteBufferUtil.bytes("bar"));
- builder.build().apply();
-
- indexedRow = PerRowSecondaryIndexTest.TestIndex.LAST_INDEXED_PARTITION;
- assertNotNull(indexedRow);
- assertEquals(ByteBufferUtil.bytes("bar"), UnfilteredRowIterators.filter(indexedRow, nowInSec).next().getCell(cdef).value());
- assertTrue(Arrays.equals("k1".getBytes(), PerRowSecondaryIndexTest.TestIndex.LAST_INDEXED_KEY.array()));
- }
-
- @Test
- public void testColumnDelete()
- {
- // issue a column delete and test that the configured index instance was notified to update
- CFMetaData cfm = Schema.instance.getCFMetaData(KEYSPACE1, CF_INDEXED);
-
- new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), "k2")
- .noRowMarker()
- .delete("indexed")
- .build()
- .apply();
-
- UnfilteredRowIterator indexedRow = PerRowSecondaryIndexTest.TestIndex.LAST_INDEXED_PARTITION;
- assertNotNull(indexedRow);
-
- //We filter tombstones now...
- Assert.assertFalse(UnfilteredRowIterators.filter(indexedRow, FBUtilities.nowInSeconds()).hasNext());
- assertTrue(Arrays.equals("k2".getBytes(), PerRowSecondaryIndexTest.TestIndex.LAST_INDEXED_KEY.array()));
- }
-
- @Test
- public void testRowDelete()
- {
- // issue a row level delete and test that the configured index instance was notified to update
- CFMetaData cfm = Schema.instance.getCFMetaData(KEYSPACE1, CF_INDEXED);
- RowUpdateBuilder.deleteRow(cfm, FBUtilities.timestampMicros(), "k3").apply();
-
- UnfilteredRowIterator indexedRow = PerRowSecondaryIndexTest.TestIndex.LAST_INDEXED_PARTITION;
- assertNotNull(indexedRow);
- assertNotNull(indexedRow.partitionLevelDeletion());
- Assert.assertFalse(UnfilteredRowIterators.filter(indexedRow, FBUtilities.nowInSeconds()).hasNext());
- assertTrue(Arrays.equals("k3".getBytes(), PerRowSecondaryIndexTest.TestIndex.LAST_INDEXED_KEY.array()));
- }
-
- @Test
- public void testInvalidSearch()
- {
-
- CFMetaData cfm = Schema.instance.getCFMetaData(KEYSPACE1, CF_INDEXED);
-
- RowUpdateBuilder builder = new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), "k1");
- builder.add("indexed", ByteBufferUtil.bytes("foo"));
- builder.build().apply();
-
-
- // test we can search:
- UntypedResultSet result = QueryProcessor.executeInternal(String.format("SELECT * FROM \"%s\".\"Indexed1\" WHERE indexed = 'foo'", KEYSPACE1));
- assertEquals(1, result.size());
-
- // test we can't search if the searcher doesn't validate the expression:
- try
- {
- QueryProcessor.executeInternal(String.format("SELECT * FROM \"%s\".\"Indexed1\" WHERE indexed = 'invalid'", KEYSPACE1));
- fail("Query should have been invalid!");
- }
- catch (Exception e)
- {
- assertTrue(e instanceof InvalidRequestException || (e.getCause() != null && (e.getCause() instanceof InvalidRequestException)));
- }
- }
-
- public static class TestIndex extends PerRowSecondaryIndex
- {
- public static UnfilteredRowIterator LAST_INDEXED_PARTITION;
- public static ByteBuffer LAST_INDEXED_KEY;
-
- public static void reset()
- {
- LAST_INDEXED_KEY = null;
- LAST_INDEXED_PARTITION = null;
- }
-
- @Override
- public void index(ByteBuffer rowKey, UnfilteredRowIterator cf)
- {
- LAST_INDEXED_PARTITION = cf;
- LAST_INDEXED_KEY = rowKey;
- }
-
- public void index(ByteBuffer rowKey, PartitionUpdate atoms)
- {
- LAST_INDEXED_PARTITION = atoms.unfilteredIterator();
- LAST_INDEXED_KEY = rowKey;
- }
-
- @Override
- public void delete(ByteBuffer key, OpOrder.Group opGroup)
- {
- }
-
- @Override
- public void init()
- {
- }
-
- @Override
- public void reload()
- {
- }
-
- @Override
- public void validateOptions(CFMetaData cfm, IndexMetadata def) throws ConfigurationException{}
- {
- }
-
- @Override
- public String getIndexName()
- {
- return null;
- }
-
- @Override
- protected SecondaryIndexSearcher createSecondaryIndexSearcher(Set<ColumnDefinition> columns)
- {
- return new SecondaryIndexSearcher(baseCfs.indexManager, columns)
- {
- @Override
- public UnfilteredPartitionIterator search(ReadCommand filter, ReadOrderGroup orderGroup)
- {
- return new SingletonUnfilteredPartitionIterator(LAST_INDEXED_PARTITION, false);
- }
-
- @Override
- public RowFilter.Expression primaryClause(ReadCommand command)
- {
- RowFilter.Expression expression = command.rowFilter().iterator().next();
-
- if (expression.getIndexValue().equals(ByteBufferUtil.bytes("invalid")))
- throw new InvalidRequestException("Invalid search!");
-
- return expression;
- }
-
- protected UnfilteredPartitionIterator queryDataFromIndex(AbstractSimplePerColumnSecondaryIndex index, DecoratedKey indexKey, RowIterator indexHits, ReadCommand command, ReadOrderGroup orderGroup)
- {
- // As we override 'search()' directly for the test, we don't care about this.
- throw new UnsupportedOperationException();
- }
- };
- }
-
- @Override
- public void forceBlockingFlush()
- {
- }
-
- @Override
- public ColumnFamilyStore getIndexCfs()
- {
- return baseCfs;
- }
-
- @Override
- public boolean indexes(ColumnDefinition name)
- {
- return true;
- }
-
- @Override
- public void validate(DecoratedKey partitionKey) throws InvalidRequestException
- {
-
- }
-
- @Override
- public void validate(Clustering clustering) throws InvalidRequestException
- {
-
- }
-
- @Override
- public void validate(ByteBuffer cellValue, CellPath path) throws InvalidRequestException
- {
-
- }
-
- @Override
- public void removeIndex(ByteBuffer columnName)
- {
- }
-
- @Override
- public void invalidate()
- {
- }
-
- @Override
- public void truncateBlocking(long truncatedAt)
- {
- }
-
- @Override
- public long estimateResultRows() {
- return 0;
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/0626be86/test/unit/org/apache/cassandra/index/StubIndex.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/index/StubIndex.java b/test/unit/org/apache/cassandra/index/StubIndex.java
new file mode 100644
index 0000000..b170c24
--- /dev/null
+++ b/test/unit/org/apache/cassandra/index/StubIndex.java
@@ -0,0 +1,186 @@
+/*
+ * 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;
+
+import java.util.*;
+import java.util.concurrent.Callable;
+import java.util.function.BiFunction;
+
+import org.apache.cassandra.config.ColumnDefinition;
+import org.apache.cassandra.cql3.ColumnIdentifier;
+import org.apache.cassandra.cql3.Operator;
+import org.apache.cassandra.db.*;
+import org.apache.cassandra.db.filter.RowFilter;
+import org.apache.cassandra.db.partitions.PartitionIterator;
+import org.apache.cassandra.db.partitions.PartitionUpdate;
+import org.apache.cassandra.db.rows.Row;
+import org.apache.cassandra.exceptions.InvalidRequestException;
+import org.apache.cassandra.index.transactions.IndexTransaction;
+import org.apache.cassandra.schema.IndexMetadata;
+import org.apache.cassandra.utils.Pair;
+import org.apache.cassandra.utils.concurrent.OpOrder;
+
+public class StubIndex implements Index
+{
+ public List<Row> rowsInserted = new ArrayList<>();
+ public List<Row> rowsDeleted = new ArrayList<>();
+ public List<Pair<Row,Row>> rowsUpdated = new ArrayList<>();
+ private IndexMetadata indexMetadata;
+ private ColumnFamilyStore baseCfs;
+
+ public void reset()
+ {
+ rowsInserted.clear();
+ rowsDeleted.clear();
+ }
+
+ public StubIndex(ColumnFamilyStore baseCfs, IndexMetadata metadata)
+ {
+ this.baseCfs = baseCfs;
+ this.indexMetadata = metadata;
+ }
+
+ public boolean indexes(PartitionColumns columns)
+ {
+ for (ColumnDefinition col : columns)
+ for (ColumnIdentifier indexed : indexMetadata.columns)
+ if (indexed.equals(col.name))
+ return true;
+ return false;
+ }
+
+ public boolean supportsExpression(ColumnDefinition column, Operator operator)
+ {
+ return operator == Operator.EQ;
+ }
+
+ public RowFilter getPostIndexQueryFilter(RowFilter filter)
+ {
+ return filter;
+ }
+
+ public Indexer indexerFor(DecoratedKey key,
+ int nowInSec,
+ OpOrder.Group opGroup,
+ IndexTransaction.Type transactionType)
+ {
+ return new Indexer()
+ {
+ public void begin()
+ {
+ }
+
+ public void partitionDelete(DeletionTime deletionTime)
+ {
+ }
+
+ public void rangeTombstone(RangeTombstone tombstone)
+ {
+ }
+
+ public void insertRow(Row row)
+ {
+ rowsInserted.add(row);
+ }
+
+ public void removeRow(Row row)
+ {
+ rowsDeleted.add(row);
+ }
+
+ public void updateRow(Row oldRowData, Row newRowData)
+ {
+ rowsUpdated.add(Pair.create(oldRowData, newRowData));
+ }
+
+ public void finish()
+ {
+ }
+ };
+ }
+
+ public Callable<?> getInitializationTask()
+ {
+ return null;
+ }
+
+ public IndexMetadata getIndexMetadata()
+ {
+ return indexMetadata;
+ }
+
+ public String getIndexName()
+ {
+ return indexMetadata != null ? indexMetadata.name : "default_test_index_name";
+ }
+
+ public void register(IndexRegistry registry){
+ registry.registerIndex(this);
+ }
+
+ public Optional<ColumnFamilyStore> getBackingTable()
+ {
+ return Optional.empty();
+ }
+
+ public Collection<ColumnDefinition> getIndexedColumns()
+ {
+ return Collections.emptySet();
+ }
+
+ public Callable<?> getBlockingFlushTask()
+ {
+ return null;
+ }
+
+ public Callable<?> getTruncateTask(long truncatedAt)
+ {
+ return null;
+ }
+
+ public Callable<?> getInvalidateTask()
+ {
+ return null;
+ }
+
+ public Callable<?> getMetadataReloadTask(IndexMetadata indexMetadata)
+ {
+ return null;
+ }
+
+ public long getEstimatedResultRows()
+ {
+ return 0;
+ }
+
+ public void validate(PartitionUpdate update) throws InvalidRequestException
+ {
+
+ }
+
+ public Searcher searcherFor(ReadCommand command)
+ {
+ return null;
+ }
+
+ public BiFunction<PartitionIterator, ReadCommand, PartitionIterator> postProcessorFor(ReadCommand readCommand)
+ {
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/0626be86/test/unit/org/apache/cassandra/index/internal/CassandraIndexTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/index/internal/CassandraIndexTest.java b/test/unit/org/apache/cassandra/index/internal/CassandraIndexTest.java
new file mode 100644
index 0000000..e938737
--- /dev/null
+++ b/test/unit/org/apache/cassandra/index/internal/CassandraIndexTest.java
@@ -0,0 +1,718 @@
+/*
+ * 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.internal;
+
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.*;
+import org.junit.Test;
+
+import org.apache.cassandra.config.CFMetaData;
+import org.apache.cassandra.config.ColumnDefinition;
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.cql3.UntypedResultSet;
+import org.apache.cassandra.cql3.restrictions.StatementRestrictions;
+import org.apache.cassandra.cql3.statements.SelectStatement;
+import org.apache.cassandra.db.*;
+import org.apache.cassandra.db.filter.ClusteringIndexFilter;
+import org.apache.cassandra.db.filter.ClusteringIndexSliceFilter;
+import org.apache.cassandra.db.filter.ColumnFilter;
+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.exceptions.InvalidRequestException;
+import org.apache.cassandra.utils.ByteBufferUtil;
+import org.apache.cassandra.utils.FBUtilities;
+
+import static org.apache.cassandra.Util.throwAssert;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+/**
+ * Smoke tests of built-in secondary index implementations
+ */
+public class CassandraIndexTest extends CQLTester
+{
+
+ @Test
+ public void indexOnRegularColumn() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, v int, PRIMARY KEY (k, c));")
+ .target("v")
+ .indexName("v_index")
+ .withFirstRow(row(0, 0, 0))
+ .withSecondRow(row(1, 1, 1))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("v=0")
+ .secondQueryExpression("v=1")
+ .updateExpression("SET v=2")
+ .postUpdateQueryExpression("v=2")
+ .run();
+ }
+
+ @Test
+ public void indexOnFirstClusteringColumn() throws Throwable
+ {
+ // No update allowed on primary key columns, so this script has no update expression
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, v int, PRIMARY KEY (k, c));")
+ .target("c")
+ .indexName("c_index")
+ .withFirstRow(row(0, 0, 0))
+ .withSecondRow(row(1, 1, 1))
+ .missingIndexMessage(SelectStatement.REQUIRES_ALLOW_FILTERING_MESSAGE)
+ .firstQueryExpression("c=0")
+ .secondQueryExpression("c=1")
+ .run();
+ }
+
+ @Test
+ public void indexOnSecondClusteringColumn() throws Throwable
+ {
+ // No update allowed on primary key columns, so this script has no update expression
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c1 int, c2 int, v int, PRIMARY KEY (k, c1, c2));")
+ .target("c2")
+ .indexName("c2_index")
+ .withFirstRow(row(0, 0, 0, 0))
+ .withSecondRow(row(1, 1, 1, 1))
+ .missingIndexMessage(String.format("PRIMARY KEY column \"%s\" cannot be restricted " +
+ "as preceding column \"%s\" is not restricted",
+ "c2", "c1"))
+ .firstQueryExpression("c2=0")
+ .secondQueryExpression("c2=1")
+ .run();
+ }
+
+ @Test
+ public void indexOnFirstPartitionKeyColumn() throws Throwable
+ {
+ // No update allowed on primary key columns, so this script has no update expression
+ new TestScript().tableDefinition("CREATE TABLE %s (k1 int, k2 int, c1 int, c2 int, v int, PRIMARY KEY ((k1, k2), c1, c2));")
+ .target("k1")
+ .indexName("k1_index")
+ .withFirstRow(row(0, 0, 0, 0, 0))
+ .withSecondRow(row(1, 1, 1, 1, 1))
+ .missingIndexMessage("Partition key parts: k2 must be restricted as other parts are")
+ .firstQueryExpression("k1=0")
+ .secondQueryExpression("k1=1")
+ .run();
+ }
+
+ @Test
+ public void indexOnSecondPartitionKeyColumn() throws Throwable
+ {
+ // No update allowed on primary key columns, so this script has no update expression
+ new TestScript().tableDefinition("CREATE TABLE %s (k1 int, k2 int, c1 int, c2 int, v int, PRIMARY KEY ((k1, k2), c1, c2));")
+ .target("k2")
+ .indexName("k2_index")
+ .withFirstRow(row(0, 0, 0, 0, 0))
+ .withSecondRow(row(1, 1, 1, 1, 1))
+ .missingIndexMessage("Partition key parts: k1 must be restricted as other parts are")
+ .firstQueryExpression("k2=0")
+ .secondQueryExpression("k2=1")
+ .run();
+ }
+
+ @Test
+ public void indexOnNonFrozenListWithReplaceOperation() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, l list<int>, PRIMARY KEY (k, c));")
+ .target("l")
+ .indexName("l_index1")
+ .withFirstRow(row(0, 0, Lists.newArrayList(10, 20, 30)))
+ .withSecondRow(row(1, 1, Lists.newArrayList(11, 21, 31)))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("l CONTAINS 10")
+ .secondQueryExpression("l CONTAINS 11")
+ .updateExpression("SET l = [40, 50, 60]")
+ .postUpdateQueryExpression("l CONTAINS 40")
+ .run();
+ }
+
+ @Test
+ public void indexOnNonFrozenListWithInPlaceOperation() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, l list<int>, PRIMARY KEY (k, c));")
+ .target("l")
+ .indexName("l_index2")
+ .withFirstRow(row(0, 0, Lists.newArrayList(10, 20, 30)))
+ .withSecondRow(row(1, 1, Lists.newArrayList(11, 21, 31)))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("l CONTAINS 10")
+ .secondQueryExpression("l CONTAINS 11")
+ .updateExpression("SET l = l - [10]")
+ .postUpdateQueryExpression("l CONTAINS 20")
+ .run();
+ }
+
+ @Test
+ public void indexOnNonFrozenSetWithReplaceOperation() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, s set<int>, PRIMARY KEY (k, c));")
+ .target("s")
+ .indexName("s_index1")
+ .withFirstRow(row(0, 0, Sets.newHashSet(10, 20, 30)))
+ .withSecondRow(row(1, 1, Sets.newHashSet(11, 21, 31)))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("s CONTAINS 10")
+ .secondQueryExpression("s CONTAINS 11")
+ .updateExpression("SET s = {40, 50, 60}")
+ .postUpdateQueryExpression("s CONTAINS 40")
+ .run();
+ }
+
+ @Test
+ public void indexOnNonFrozenSetWithInPlaceOperation() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, s set<int>, PRIMARY KEY (k, c));")
+ .target("s")
+ .indexName("s_index2")
+ .withFirstRow(row(0, 0, Sets.newHashSet(10, 20, 30)))
+ .withSecondRow(row(1, 1, Sets.newHashSet(11, 21, 31)))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("s CONTAINS 10")
+ .secondQueryExpression("s CONTAINS 11")
+ .updateExpression("SET s = s - {10}")
+ .postUpdateQueryExpression("s CONTAINS 20")
+ .run();
+ }
+
+ @Test
+ public void indexOnNonFrozenMapValuesWithReplaceOperation() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, m map<text,int>, PRIMARY KEY (k, c));")
+ .target("m")
+ .indexName("m_index1")
+ .withFirstRow(row(0, 0, ImmutableMap.of("a", 10, "b", 20, "c", 30)))
+ .withSecondRow(row(1, 1, ImmutableMap.of("d", 11, "e", 21, "f", 31)))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("m CONTAINS 10")
+ .secondQueryExpression("m CONTAINS 11")
+ .updateExpression("SET m = {'x':40, 'y':50, 'z':60}")
+ .postUpdateQueryExpression("m CONTAINS 40")
+ .run();
+ }
+
+ @Test
+ public void indexOnNonFrozenMapValuesWithInPlaceOperation() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, m map<text,int>, PRIMARY KEY (k, c));")
+ .target("m")
+ .indexName("m_index2")
+ .withFirstRow(row(0, 0, ImmutableMap.of("a", 10, "b", 20, "c", 30)))
+ .withSecondRow(row(1, 1, ImmutableMap.of("d", 11, "e", 21, "f", 31)))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("m CONTAINS 10")
+ .secondQueryExpression("m CONTAINS 11")
+ .updateExpression("SET m['a'] = 40")
+ .postUpdateQueryExpression("m CONTAINS 40")
+ .run();
+ }
+
+ @Test
+ public void indexOnNonFrozenMapKeysWithReplaceOperation() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, m map<text,int>, PRIMARY KEY (k, c));")
+ .target("keys(m)")
+ .indexName("m_index3")
+ .withFirstRow(row(0, 0, ImmutableMap.of("a", 10, "b", 20, "c", 30)))
+ .withSecondRow(row(1, 1, ImmutableMap.of("d", 11, "e", 21, "f", 31)))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("m CONTAINS KEY 'a'")
+ .secondQueryExpression("m CONTAINS KEY 'd'")
+ .updateExpression("SET m = {'x':40, 'y':50, 'z':60}")
+ .postUpdateQueryExpression("m CONTAINS KEY 'x'")
+ .run();
+ }
+
+ @Test
+ public void indexOnNonFrozenMapKeysWithInPlaceOperation() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, m map<text,int>, PRIMARY KEY (k, c));")
+ .target("keys(m)")
+ .indexName("m_index4")
+ .withFirstRow(row(0, 0, ImmutableMap.of("a", 10, "b", 20, "c", 30)))
+ .withSecondRow(row(1, 1, ImmutableMap.of("d", 11, "e", 21, "f", 31)))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("m CONTAINS KEY 'a'")
+ .secondQueryExpression("m CONTAINS KEY 'd'")
+ .updateExpression("SET m['a'] = NULL")
+ .postUpdateQueryExpression("m CONTAINS KEY 'b'")
+ .run();
+ }
+
+ @Test
+ public void indexOnNonFrozenMapEntriesWithReplaceOperation() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, m map<text,int>, PRIMARY KEY (k, c));")
+ .target("entries(m)")
+ .indexName("m_index5")
+ .withFirstRow(row(0, 0, ImmutableMap.of("a", 10, "b", 20, "c", 30)))
+ .withSecondRow(row(1, 1, ImmutableMap.of("d", 11, "e", 21, "f", 31)))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("m['a'] = 10")
+ .secondQueryExpression("m['d'] = 11")
+ .updateExpression("SET m = {'x':40, 'y':50, 'z':60}")
+ .postUpdateQueryExpression("m['x'] = 40")
+ .run();
+ }
+
+ @Test
+ public void indexOnNonFrozenMapEntriesWithInPlaceOperation() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, m map<text,int>, PRIMARY KEY (k, c));")
+ .target("entries(m)")
+ .indexName("m_index6")
+ .withFirstRow(row(0, 0, ImmutableMap.of("a", 10, "b", 20, "c", 30)))
+ .withSecondRow(row(1, 1, ImmutableMap.of("d", 11, "e", 21, "f", 31)))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("m['a'] = 10")
+ .secondQueryExpression("m['d'] = 11")
+ .updateExpression("SET m['a'] = 40")
+ .postUpdateQueryExpression("m['a'] = 40")
+ .run();
+ }
+
+ @Test
+ public void indexOnFrozenList() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, l frozen<list<int>>, PRIMARY KEY (k, c));")
+ .target("full(l)")
+ .indexName("fl_index")
+ .withFirstRow(row(0, 0, Lists.newArrayList(10, 20, 30)))
+ .withSecondRow(row(1, 1, Lists.newArrayList(11, 21, 31)))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("l = [10, 20, 30]")
+ .secondQueryExpression("l = [11, 21, 31]")
+ .updateExpression("SET l = [40, 50, 60]")
+ .postUpdateQueryExpression("l = [40, 50, 60]")
+ .run();
+ }
+
+ @Test
+ public void indexOnFrozenSet() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, s frozen<set<int>>, PRIMARY KEY (k, c));")
+ .target("full(s)")
+ .indexName("fs_index")
+ .withFirstRow(row(0, 0, Sets.newHashSet(10, 20, 30)))
+ .withSecondRow(row(1, 1, Sets.newHashSet(11, 21, 31)))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("s = {10, 20, 30}")
+ .secondQueryExpression("s = {11, 21, 31}")
+ .updateExpression("SET s = {40, 50, 60}")
+ .postUpdateQueryExpression("s = {40, 50, 60}")
+ .run();
+ }
+
+ @Test
+ public void indexOnFrozenMap() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, c int, m frozen<map<text,int>>, PRIMARY KEY (k, c));")
+ .target("full(m)")
+ .indexName("fm_index")
+ .withFirstRow(row(0, 0, ImmutableMap.of("a", 10, "b", 20, "c", 30)))
+ .withSecondRow(row(1, 1, ImmutableMap.of("d", 11, "e", 21, "f", 31)))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("m = {'a':10, 'b':20, 'c':30}")
+ .secondQueryExpression("m = {'d':11, 'e':21, 'f':31}")
+ .updateExpression("SET m = {'x':40, 'y':50, 'z':60}")
+ .postUpdateQueryExpression("m = {'x':40, 'y':50, 'z':60}")
+ .run();
+ }
+
+ @Test
+ public void indexOnRegularColumnWithCompactStorage() throws Throwable
+ {
+ new TestScript().tableDefinition("CREATE TABLE %s (k int, v int, PRIMARY KEY (k)) WITH COMPACT STORAGE;")
+ .target("v")
+ .indexName("cv_index")
+ .withFirstRow(row(0, 0))
+ .withSecondRow(row(1,1))
+ .missingIndexMessage(StatementRestrictions.NO_INDEX_FOUND_MESSAGE)
+ .firstQueryExpression("v=0")
+ .secondQueryExpression("v=1")
+ .updateExpression("SET v=2")
+ .postUpdateQueryExpression("v=2")
+ .run();
+ }
+
+ @Test
+ public void createIndexesOnMultipleMapDimensions() throws Throwable
+ {
+ Object[] row1 = row(0, 0, ImmutableMap.of("a", 10, "b", 20, "c", 30));
+ Object[] row2 = row(1, 1, ImmutableMap.of("d", 11, "e", 21, "f", 32));
+ createTable("CREATE TABLE %s (k int, c int, m map<text, int>, PRIMARY KEY(k, c))");
+ createIndex("CREATE INDEX mkey_index on %s(keys(m))");
+ createIndex("CREATE INDEX mval_index on %s(m)");
+
+ execute("INSERT INTO %s (k, c, m) VALUES (?, ?, ?)", row1);
+ execute("INSERT INTO %s (k, c, m) VALUES (?, ?, ?)", row2);
+
+ assertRows(execute("SELECT * FROM %s WHERE m CONTAINS KEY 'a'"), row1);
+ assertRows(execute("SELECT * FROM %s WHERE m CONTAINS 20"), row1);
+ assertRows(execute("SELECT * FROM %s WHERE m CONTAINS KEY 'f'"), row2);
+ assertRows(execute("SELECT * FROM %s WHERE m CONTAINS 32"), row2);
+ }
+
+ @Test
+ public void insertWithTombstoneRemovesEntryFromIndex() throws Throwable
+ {
+ int key = 0;
+ int indexedValue = 99;
+ createTable("CREATE TABLE %s (k int, v int, PRIMARY KEY(k))");
+ createIndex("CREATE INDEX v_index on %s(v)");
+ execute("INSERT INTO %s (k, v) VALUES (?, ?)", key, indexedValue);
+
+ assertRows(execute("SELECT * FROM %s WHERE v = ?", indexedValue), row(key, indexedValue));
+ execute("DELETE v FROM %s WHERE k=?", key);
+ assertEmpty(execute("SELECT * FROM %s WHERE v = ?", indexedValue));
+ }
+
+ @Test
+ public void updateTTLOnIndexedClusteringValue() throws Throwable
+ {
+ int basePk = 1;
+ int indexedVal = 2;
+ int initialTtl = 3600;
+ createTable("CREATE TABLE %s (k int, c int, v int, PRIMARY KEY(k,c))");
+ createIndex("CREATE INDEX c_index on %s(c)");
+ execute("INSERT INTO %s (k, c, v) VALUES (?, ?, 0) USING TTL ?", basePk, indexedVal, initialTtl);
+ ColumnFamilyStore baseCfs = getCurrentColumnFamilyStore();
+ ColumnFamilyStore indexCfs = baseCfs.indexManager.listIndexes()
+ .iterator()
+ .next()
+ .getBackingTable()
+ .orElseThrow(throwAssert("No index found"));
+ assertIndexRowTtl(indexCfs, indexedVal, initialTtl);
+
+ int updatedTtl = 9999;
+ execute("INSERT INTO %s (k, c ,v) VALUES (?, ?, 0) USING TTL ?", basePk, indexedVal, updatedTtl);
+
+ assertIndexRowTtl(indexCfs, indexedVal, updatedTtl);
+ }
+
+ // this is slightly annoying, but we cannot read rows from the methods in Util as
+ // ReadCommand#executeInternal uses metadata retrieved via the cfId, which the index
+ // CFS inherits from the base CFS. This has the 'wrong' partitioner (the index table
+ // uses LocalPartition, the base table a real one, so we cannot read from the index
+ // table with executeInternal
+ private void assertIndexRowTtl(ColumnFamilyStore indexCfs, int indexedValue, int ttl) throws Throwable
+ {
+ DecoratedKey indexKey = indexCfs.decorateKey(ByteBufferUtil.bytes(indexedValue));
+ ClusteringIndexFilter filter = new ClusteringIndexSliceFilter(Slices.with(indexCfs.metadata.comparator,
+ Slice.ALL),
+ false);
+ SinglePartitionReadCommand command = SinglePartitionReadCommand.create(indexCfs.metadata,
+ FBUtilities.nowInSeconds(),
+ indexKey,
+ ColumnFilter.all(indexCfs.metadata),
+ filter);
+ try (ReadOrderGroup orderGroup = ReadOrderGroup.forCommand(command);
+ UnfilteredRowIterator iter = command.queryMemtableAndDisk(indexCfs, orderGroup.indexReadOpOrderGroup()))
+ {
+ while( iter.hasNext())
+ {
+ Unfiltered unfiltered = iter.next();
+ assert (unfiltered.isRow());
+ Row indexRow = (Row) unfiltered;
+ assertEquals(ttl, indexRow.primaryKeyLivenessInfo().ttl());
+ }
+ }
+ }
+
+ private class TestScript
+ {
+ String tableDefinition;
+ String indexName;
+ String indexTarget;
+ String queryExpression1;
+ String queryExpression2;
+ String updateExpression;
+ String postUpdateQueryExpression;
+ String missingIndexMessage;
+
+ Object[] firstRow;
+ Object[] secondRow;
+
+ TestScript indexName(String indexName)
+ {
+ this.indexName = indexName;
+ return this;
+ }
+
+ TestScript target(String indexTarget)
+ {
+ this.indexTarget = indexTarget;
+ return this;
+ }
+
+ TestScript tableDefinition(String tableDefinition)
+ {
+ this.tableDefinition = tableDefinition;
+ return this;
+ }
+
+ TestScript withFirstRow(Object[] row)
+ {
+ this.firstRow = row;
+ return this;
+ }
+
+ TestScript withSecondRow(Object[] row)
+ {
+ this.secondRow = row;
+ return this;
+ }
+
+ TestScript firstQueryExpression(String queryExpression)
+ {
+ queryExpression1 = queryExpression;
+ return this;
+ }
+
+ TestScript secondQueryExpression(String queryExpression)
+ {
+ queryExpression2 = queryExpression;
+ return this;
+ }
+
+ TestScript updateExpression(String updateExpression)
+ {
+ this.updateExpression = updateExpression;
+ return this;
+ }
+
+ TestScript postUpdateQueryExpression(String queryExpression)
+ {
+ this.postUpdateQueryExpression = queryExpression;
+ return this;
+ }
+
+ TestScript missingIndexMessage(String missingIndexMessage)
+ {
+ this.missingIndexMessage = missingIndexMessage;
+ return this;
+ }
+
+ void run() throws Throwable
+ {
+ // check minimum required setup
+ assertNotNull(indexName);
+ assertNotNull(indexTarget);
+ assertNotNull(queryExpression1);
+ assertNotNull(queryExpression2);
+ assertNotNull(firstRow);
+ assertNotNull(secondRow);
+ assertNotNull(tableDefinition);
+ if (updateExpression != null)
+ assertNotNull(postUpdateQueryExpression);
+
+ // first, create the table as we need the CFMetaData to build the other cql statements
+ createTable(tableDefinition);
+
+ // now setup the cql statements the test will run through. Some are dependent on
+ // the table definition, others are not.
+ String createIndexCql = String.format("CREATE INDEX %s ON %%s(%s)", indexName, indexTarget);
+ String dropIndexCql = String.format("DROP INDEX %s.%s", KEYSPACE, indexName);
+
+ String selectFirstRowCql = String.format("SELECT * FROM %%s WHERE %s", queryExpression1);
+ String selectSecondRowCql = String.format("SELECT * FROM %%s WHERE %s", queryExpression2);
+ String insertCql = getInsertCql();
+ String deleteRowCql = getDeleteRowCql();
+ String deletePartitionCql = getDeletePartitionCql();
+
+ // everything setup, run through the smoke test
+ execute(insertCql, firstRow);
+ // before creating the index, check we cannot query on the indexed column
+ assertInvalidThrowMessage(missingIndexMessage, InvalidRequestException.class, selectFirstRowCql);
+
+ // create the index, wait for it to be built then validate the indexed value
+ createIndex(createIndexCql);
+ waitForIndexBuild();
+ assertRows(execute(selectFirstRowCql), firstRow);
+ assertEmpty(execute(selectSecondRowCql));
+
+ // flush and check again
+ flush();
+ assertRows(execute(selectFirstRowCql), firstRow);
+ assertEmpty(execute(selectSecondRowCql));
+
+ // force major compaction and query again
+ compact();
+ assertRows(execute(selectFirstRowCql), firstRow);
+ assertEmpty(execute(selectSecondRowCql));
+
+ // drop the index and assert we can no longer query using it
+ execute(dropIndexCql);
+ assertInvalidThrowMessage(missingIndexMessage, InvalidRequestException.class, selectFirstRowCql);
+ flush();
+ compact();
+
+ // insert second row, re-create the index and query for both indexed values
+ execute(insertCql, secondRow);
+ createIndex(createIndexCql);
+ waitForIndexBuild();
+ assertRows(execute(selectFirstRowCql), firstRow);
+ assertRows(execute(selectSecondRowCql), secondRow);
+
+ // modify the indexed value in the first row, assert we can query by the new value & not the original one
+ // note: this is not possible if the indexed column is part of the primary key, so we skip it in that case
+ if (includesUpdate())
+ {
+ execute(getUpdateCql(), getPrimaryKeyValues(firstRow));
+ assertEmpty(execute(selectFirstRowCql));
+ // update the select statement to query using the updated value
+ selectFirstRowCql = String.format("SELECT * FROM %%s WHERE %s", postUpdateQueryExpression);
+ // we can't check the entire row b/c we've modified something.
+ // so we just check the primary key columns, as they cannot have changed
+ assertPrimaryKeyColumnsOnly(execute(selectFirstRowCql), firstRow);
+ }
+
+ // delete row, check that it cannot be found via index query
+ execute(deleteRowCql, getPrimaryKeyValues(firstRow));
+ assertEmpty(execute(selectFirstRowCql));
+
+ // delete partition, check that its rows cannot be retrieved via index query
+ execute(deletePartitionCql, getPartitionKeyValues(secondRow));
+ assertEmpty(execute(selectSecondRowCql));
+
+ // flush & compact, then verify that deleted values stay gone
+ flush();
+ compact();
+ assertEmpty(execute(selectFirstRowCql));
+ assertEmpty(execute(selectSecondRowCql));
+
+ // add back both rows, reset the select for the first row to query on the original value & verify
+ execute(insertCql, firstRow);
+ selectFirstRowCql = String.format("SELECT * FROM %%s WHERE %s", queryExpression1);
+ assertRows(execute(selectFirstRowCql), firstRow);
+ execute(insertCql, secondRow);
+ assertRows(execute(selectSecondRowCql), secondRow);
+
+ // flush and compact, verify again & we're done
+ flush();
+ compact();
+ assertRows(execute(selectFirstRowCql), firstRow);
+ assertRows(execute(selectSecondRowCql), secondRow);
+ }
+
+ private void assertPrimaryKeyColumnsOnly(UntypedResultSet resultSet, Object[] row)
+ {
+ assertFalse(resultSet.isEmpty());
+ CFMetaData cfm = getCurrentColumnFamilyStore().metadata;
+ int columnCount = cfm.partitionKeyColumns().size();
+ if (cfm.isCompound())
+ columnCount += cfm.clusteringColumns().size();
+ Object[] expected = copyValuesFromRow(row, columnCount);
+ assertArrayEquals(expected, copyValuesFromRow(getRows(resultSet)[0], columnCount));
+ }
+
+ private String getInsertCql()
+ {
+ CFMetaData cfm = getCurrentColumnFamilyStore().metadata;
+ String columns = Joiner.on(", ")
+ .join(Iterators.transform(cfm.allColumnsInSelectOrder(),
+ (column) -> column.name.toString()));
+ String markers = Joiner.on(", ").join(Iterators.transform(cfm.allColumnsInSelectOrder(),
+ (column) -> {
+ return "?";
+ }));
+ return String.format("INSERT INTO %%s (%s) VALUES (%s)", columns, markers);
+ }
+
+ private String getUpdateCql()
+ {
+ String whereClause = getPrimaryKeyColumns().map(column -> column.name.toString() + "=?")
+ .collect(Collectors.joining(" AND "));
+ return String.format("UPDATE %%s %s WHERE %s", updateExpression, whereClause);
+ }
+
+ private String getDeleteRowCql()
+ {
+ return getPrimaryKeyColumns().map(column -> column.name.toString() + "=?")
+ .collect(Collectors.joining(" AND ", "DELETE FROM %s WHERE ", ""));
+ }
+
+ private String getDeletePartitionCql()
+ {
+ CFMetaData cfm = getCurrentColumnFamilyStore().metadata;
+ return StreamSupport.stream(cfm.partitionKeyColumns().spliterator(), false)
+ .map(column -> column.name.toString() + "=?")
+ .collect(Collectors.joining(" AND ", "DELETE FROM %s WHERE ", ""));
+ }
+
+ private Stream<ColumnDefinition> getPrimaryKeyColumns()
+ {
+ CFMetaData cfm = getCurrentColumnFamilyStore().metadata;
+ if (cfm.isCompactTable())
+ return cfm.partitionKeyColumns().stream();
+ else
+ return Stream.concat(cfm.partitionKeyColumns().stream(), cfm.clusteringColumns().stream());
+ }
+
+ private Object[] getPrimaryKeyValues(Object[] row)
+ {
+ CFMetaData cfm = getCurrentColumnFamilyStore().metadata;
+ if (cfm.isCompactTable())
+ return getPartitionKeyValues(row);
+
+ return copyValuesFromRow(row, cfm.partitionKeyColumns().size() + cfm.clusteringColumns().size());
+ }
+
+ private Object[] getPartitionKeyValues(Object[] row)
+ {
+ CFMetaData cfm = getCurrentColumnFamilyStore().metadata;
+ return copyValuesFromRow(row, cfm.partitionKeyColumns().size());
+ }
+
+ private Object[] copyValuesFromRow(Object[] row, int length)
+ {
+ Object[] values = new Object[length];
+ System.arraycopy(row, 0, values, 0, length);
+ return values;
+ }
+
+ private boolean includesUpdate()
+ {
+ return updateExpression != null;
+ }
+
+ // Spin waiting for named index to be built
+ private void waitForIndexBuild() throws Throwable
+ {
+ ColumnFamilyStore cfs = getCurrentColumnFamilyStore();
+ String fullIndexName = String.format("%s.%s", currentTable(), indexName);
+ long maxWaitMillis = 10000;
+ long startTime = System.currentTimeMillis();
+ while (! cfs.indexManager.getBuiltIndexNames().contains(fullIndexName))
+ {
+ Thread.sleep(100);
+ long wait = System.currentTimeMillis() - startTime;
+ if (wait > maxWaitMillis)
+ fail(String.format("Timed out waiting for index %s to build (%s)ms", fullIndexName, wait));
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/0626be86/test/unit/org/apache/cassandra/io/sstable/SSTableReaderTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/io/sstable/SSTableReaderTest.java b/test/unit/org/apache/cassandra/io/sstable/SSTableReaderTest.java
index 6f92bd3..793348a 100644
--- a/test/unit/org/apache/cassandra/io/sstable/SSTableReaderTest.java
+++ b/test/unit/org/apache/cassandra/io/sstable/SSTableReaderTest.java
@@ -19,15 +19,8 @@ package org.apache.cassandra.io.sstable;
import java.io.IOException;
import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-import java.util.Set;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
-import java.util.concurrent.ThreadPoolExecutor;
+import java.util.*;
+import java.util.concurrent.*;
import com.google.common.collect.Sets;
import org.junit.Assert;
@@ -40,19 +33,17 @@ import org.apache.cassandra.SchemaLoader;
import org.apache.cassandra.Util;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.cql3.Operator;
-import org.apache.cassandra.db.BufferDecoratedKey;
-import org.apache.cassandra.db.ColumnFamilyStore;
-import org.apache.cassandra.db.DecoratedKey;
-import org.apache.cassandra.db.Keyspace;
-import org.apache.cassandra.db.ReadCommand;
-import org.apache.cassandra.db.RowUpdateBuilder;
+import org.apache.cassandra.db.*;
import org.apache.cassandra.db.compaction.CompactionManager;
import org.apache.cassandra.db.compaction.OperationType;
import org.apache.cassandra.db.lifecycle.LifecycleTransaction;
-import org.apache.cassandra.db.index.SecondaryIndexSearcher;
+import org.apache.cassandra.db.partitions.UnfilteredPartitionIterators;
import org.apache.cassandra.db.rows.Row;
-import org.apache.cassandra.dht.*;
+import org.apache.cassandra.dht.IPartitioner;
import org.apache.cassandra.dht.LocalPartitioner.LocalToken;
+import org.apache.cassandra.dht.Range;
+import org.apache.cassandra.dht.Token;
+import org.apache.cassandra.index.Index;
import org.apache.cassandra.io.sstable.format.SSTableReader;
import org.apache.cassandra.io.util.FileDataInput;
import org.apache.cassandra.io.util.MmappedSegmentedFile;
@@ -64,8 +55,8 @@ import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.Pair;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import static org.apache.cassandra.utils.ByteBufferUtil.bytes;
@RunWith(OrderedJUnit4ClassRunner.class)
public class SSTableReaderTest
@@ -388,19 +379,23 @@ public class SSTableReaderTest
store.forceBlockingFlush();
- ColumnFamilyStore indexCfs = store.indexManager.getIndexForColumn(store.metadata.getColumnDefinition(bytes("birthdate"))).getIndexCfs();
- assert indexCfs.isIndex();
- SSTableReader sstable = indexCfs.getLiveSSTables().iterator().next();
- assert sstable.first.getToken() instanceof LocalToken;
-
- try(SegmentedFile.Builder ibuilder = SegmentedFile.getBuilder(DatabaseDescriptor.getIndexAccessMode(), false);
- SegmentedFile.Builder dbuilder = SegmentedFile.getBuilder(DatabaseDescriptor.getDiskAccessMode(), sstable.compression))
+ for(ColumnFamilyStore indexCfs : store.indexManager.getAllIndexColumnFamilyStores())
{
- sstable.saveSummary(ibuilder, dbuilder);
+ assert indexCfs.isIndex();
+ SSTableReader sstable = indexCfs.getLiveSSTables().iterator().next();
+ assert sstable.first.getToken() instanceof LocalToken;
+
+ try (SegmentedFile.Builder ibuilder = SegmentedFile.getBuilder(DatabaseDescriptor.getIndexAccessMode(),
+ false);
+ SegmentedFile.Builder dbuilder = SegmentedFile.getBuilder(DatabaseDescriptor.getDiskAccessMode(),
+ sstable.compression))
+ {
+ sstable.saveSummary(ibuilder, dbuilder);
+ }
+ SSTableReader reopened = SSTableReader.open(sstable.descriptor);
+ assert reopened.first.getToken() instanceof LocalToken;
+ reopened.selfRef().release();
}
- SSTableReader reopened = SSTableReader.open(sstable.descriptor);
- assert reopened.first.getToken() instanceof LocalToken;
- reopened.selfRef().release();
}
/** see CASSANDRA-5407 */
@@ -545,16 +540,18 @@ public class SSTableReaderTest
for (ColumnFamilyStore cfs : indexedCFS.concatWithIndexes())
clearAndLoad(cfs);
- ByteBuffer bBB = bytes("birthdate");
// query using index to see if sstable for secondary index opens
ReadCommand rc = Util.cmd(indexedCFS).fromKeyIncl("k1").toKeyIncl("k3")
.columns("birthdate")
.filterOn("birthdate", Operator.EQ, 1L)
.build();
- List<SecondaryIndexSearcher> searchers = indexedCFS.indexManager.getIndexSearchersFor(rc);
- assertEquals(searchers.size(), 1);
- assertEquals(1, Util.getAll(rc).size());
+ Index.Searcher searcher = indexedCFS.indexManager.getBestIndexFor(rc).searcherFor(rc);
+ assertNotNull(searcher);
+ try (ReadOrderGroup orderGroup = ReadOrderGroup.forCommand(rc))
+ {
+ assertEquals(1, Util.size(UnfilteredPartitionIterators.filter(searcher.search(orderGroup), rc.nowInSec())));
+ }
}
private List<Range<Token>> makeRanges(Token left, Token right)
http://git-wip-us.apache.org/repos/asf/cassandra/blob/0626be86/test/unit/org/apache/cassandra/schema/DefsTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/schema/DefsTest.java b/test/unit/org/apache/cassandra/schema/DefsTest.java
index c3bcf1a..5eed80f 100644
--- a/test/unit/org/apache/cassandra/schema/DefsTest.java
+++ b/test/unit/org/apache/cassandra/schema/DefsTest.java
@@ -40,11 +40,11 @@ import org.apache.cassandra.cql3.UntypedResultSet;
import org.apache.cassandra.db.ColumnFamilyStore;
import org.apache.cassandra.db.Directories;
import org.apache.cassandra.db.Keyspace;
-import org.apache.cassandra.db.index.SecondaryIndex;
import org.apache.cassandra.db.lifecycle.TransactionLog;
import org.apache.cassandra.db.marshal.BytesType;
import org.apache.cassandra.db.marshal.UTF8Type;
import org.apache.cassandra.exceptions.ConfigurationException;
+import org.apache.cassandra.index.Index;
import org.apache.cassandra.io.sstable.Component;
import org.apache.cassandra.io.sstable.Descriptor;
import org.apache.cassandra.locator.OldNetworkTopologyStrategy;
@@ -511,9 +511,18 @@ public class DefsTest
cfs.forceBlockingFlush();
ColumnDefinition indexedColumn = cfs.metadata.getColumnDefinition(ByteBufferUtil.bytes("birthdate"));
- SecondaryIndex index = cfs.indexManager.getIndexForColumn(indexedColumn);
- ColumnFamilyStore indexedCfs = index.getIndexCfs();
- Descriptor desc = indexedCfs.getLiveSSTables().iterator().next().descriptor;
+ IndexMetadata index = cfs.metadata.getIndexes()
+ .get(indexedColumn)
+ .iterator()
+ .next();
+ ColumnFamilyStore indexCfs = cfs.indexManager.listIndexes()
+ .stream()
+ .filter(i -> i.getIndexMetadata().equals(index))
+ .map(Index::getBackingTable)
+ .findFirst()
+ .orElseThrow(() -> new AssertionError("Index not found"))
+ .orElseThrow(() -> new AssertionError("Index has no backing table"));
+ Descriptor desc = indexCfs.getLiveSSTables().iterator().next().descriptor;
// drop the index
CFMetaData meta = cfs.metadata.copy();
@@ -536,7 +545,7 @@ public class DefsTest
MigrationManager.announceColumnFamilyUpdate(meta, false);
// check
- assertTrue(cfs.indexManager.getIndexes().isEmpty());
+ assertTrue(cfs.indexManager.listIndexes().isEmpty());
TransactionLog.waitForDeletions();
assertFalse(new File(desc.filenameFor(Component.DATA)).exists());
}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/0626be86/test/unit/org/apache/cassandra/schema/LegacySchemaMigratorTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/schema/LegacySchemaMigratorTest.java b/test/unit/org/apache/cassandra/schema/LegacySchemaMigratorTest.java
index a1f9570..73eed06 100644
--- a/test/unit/org/apache/cassandra/schema/LegacySchemaMigratorTest.java
+++ b/test/unit/org/apache/cassandra/schema/LegacySchemaMigratorTest.java
@@ -26,7 +26,9 @@ import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.apache.cassandra.SchemaLoader;
-import org.apache.cassandra.config.*;
+import org.apache.cassandra.config.CFMetaData;
+import org.apache.cassandra.config.ColumnDefinition;
+import org.apache.cassandra.config.Schema;
import org.apache.cassandra.cql3.CQLTester;
import org.apache.cassandra.cql3.ColumnIdentifier;
import org.apache.cassandra.cql3.functions.*;