Search in sources :

Example 6 with IndexTarget

use of org.apache.cassandra.cql3.statements.schema.IndexTarget in project cassandra by apache.

the class RangeTombstoneTest method testOverwritesToDeletedColumns.

@Test
public void testOverwritesToDeletedColumns() throws Exception {
    Keyspace table = Keyspace.open(KSNAME);
    ColumnFamilyStore cfs = table.getColumnFamilyStore(CFNAME);
    ByteBuffer key = ByteBufferUtil.bytes("k6");
    ByteBuffer indexedColumnName = ByteBufferUtil.bytes("val");
    cfs.truncateBlocking();
    cfs.disableAutoCompaction();
    ColumnMetadata cd = cfs.metadata().getColumn(indexedColumnName).copy();
    IndexMetadata indexDef = IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(cd.name, IndexTarget.Type.VALUES)), "test_index", IndexMetadata.Kind.CUSTOM, ImmutableMap.of(IndexTarget.CUSTOM_INDEX_OPTION_NAME, StubIndex.class.getName()));
    TableMetadata current = cfs.metadata();
    if (!current.indexes.get("test_index").isPresent()) {
        TableMetadata updated = current.unbuild().indexes(current.indexes.with(indexDef)).build();
        MigrationManager.announceTableUpdate(updated, true);
    }
    Future<?> rebuild = cfs.indexManager.addIndex(indexDef, false);
    // If rebuild there is, wait for the rebuild to finish so it doesn't race with the following insertions
    if (rebuild != null)
        rebuild.get();
    StubIndex index = (StubIndex) cfs.indexManager.getIndexByName("test_index");
    index.reset();
    UpdateBuilder.create(cfs.metadata(), key).withTimestamp(0).newRow(1).add("val", 1).applyUnsafe();
    // add a RT which hides the column we just inserted
    new RowUpdateBuilder(cfs.metadata(), 1, key).addRangeTombstone(0, 1).build().applyUnsafe();
    // now re-insert that column
    UpdateBuilder.create(cfs.metadata(), key).withTimestamp(2).newRow(1).add("val", 1).applyUnsafe();
    cfs.forceBlockingFlush();
    // 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.rowsInserted.size());
    assertEquals(1, index.rowsUpdated.size());
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) StubIndex(org.apache.cassandra.index.StubIndex) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) IndexTarget(org.apache.cassandra.cql3.statements.schema.IndexTarget) IndexMetadata(org.apache.cassandra.schema.IndexMetadata) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 7 with IndexTarget

use of org.apache.cassandra.cql3.statements.schema.IndexTarget in project cassandra by apache.

the class DirectoriesTest method testSecondaryIndexDirectories.

@Test
public void testSecondaryIndexDirectories() {
    TableMetadata.Builder builder = TableMetadata.builder(KS, "cf").addPartitionKeyColumn("thekey", UTF8Type.instance).addClusteringColumn("col", UTF8Type.instance);
    ColumnIdentifier col = ColumnIdentifier.getInterned("col", true);
    IndexMetadata indexDef = IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(col, IndexTarget.Type.VALUES)), "idx", IndexMetadata.Kind.KEYS, Collections.emptyMap());
    builder.indexes(Indexes.of(indexDef));
    TableMetadata PARENT_CFM = builder.build();
    TableMetadata INDEX_CFM = CassandraIndex.indexCfsMetadata(PARENT_CFM, indexDef);
    Directories parentDirectories = new Directories(PARENT_CFM, toDataDirectories(tempDataDir));
    Directories indexDirectories = new Directories(INDEX_CFM, toDataDirectories(tempDataDir));
    // secondary index has its own directory
    for (File dir : indexDirectories.getCFDirectories()) {
        assertEquals(cfDir(INDEX_CFM), dir);
    }
    Descriptor parentDesc = new Descriptor(parentDirectories.getDirectoryForNewSSTables(), KS, PARENT_CFM.name, 0, SSTableFormat.Type.BIG);
    Descriptor indexDesc = new Descriptor(indexDirectories.getDirectoryForNewSSTables(), KS, INDEX_CFM.name, 0, SSTableFormat.Type.BIG);
    // snapshot dir should be created under its parent's
    File parentSnapshotDirectory = Directories.getSnapshotDirectory(parentDesc, "test");
    File indexSnapshotDirectory = Directories.getSnapshotDirectory(indexDesc, "test");
    assertEquals(parentSnapshotDirectory, indexSnapshotDirectory.parent());
    // check if snapshot directory exists
    parentSnapshotDirectory.tryCreateDirectories();
    assertTrue(parentDirectories.snapshotExists("test"));
    assertTrue(indexDirectories.snapshotExists("test"));
    // check true snapshot size
    Descriptor parentSnapshot = new Descriptor(parentSnapshotDirectory, KS, PARENT_CFM.name, 0, SSTableFormat.Type.BIG);
    createFile(parentSnapshot.filenameFor(Component.DATA), 30);
    Descriptor indexSnapshot = new Descriptor(indexSnapshotDirectory, KS, INDEX_CFM.name, 0, SSTableFormat.Type.BIG);
    createFile(indexSnapshot.filenameFor(Component.DATA), 40);
    assertEquals(30, parentDirectories.trueSnapshotsSize());
    assertEquals(40, indexDirectories.trueSnapshotsSize());
    // check snapshot details
    Map<String, TableSnapshot> parentSnapshotDetail = parentDirectories.listSnapshots();
    assertTrue(parentSnapshotDetail.containsKey("test"));
    assertEquals(30L, parentSnapshotDetail.get("test").computeTrueSizeBytes());
    Map<String, TableSnapshot> indexSnapshotDetail = indexDirectories.listSnapshots();
    assertTrue(indexSnapshotDetail.containsKey("test"));
    assertEquals(40L, indexSnapshotDetail.get("test").computeTrueSizeBytes());
    // check backup directory
    File parentBackupDirectory = Directories.getBackupsDirectory(parentDesc);
    File indexBackupDirectory = Directories.getBackupsDirectory(indexDesc);
    assertEquals(parentBackupDirectory, indexBackupDirectory.parent());
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) DataDirectories(org.apache.cassandra.db.Directories.DataDirectories) IndexTarget(org.apache.cassandra.cql3.statements.schema.IndexTarget) TableSnapshot(org.apache.cassandra.service.snapshot.TableSnapshot) Descriptor(org.apache.cassandra.io.sstable.Descriptor) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) IndexMetadata(org.apache.cassandra.schema.IndexMetadata) File(org.apache.cassandra.io.util.File) Test(org.junit.Test)

Example 8 with IndexTarget

use of org.apache.cassandra.cql3.statements.schema.IndexTarget in project cassandra by apache.

the class SchemaCQLHelperTest method testCfmIndexJson.

@Test
public void testCfmIndexJson() {
    String keyspace = "cql_test_keyspace_3";
    String table = "test_table_3";
    TableMetadata.Builder builder = TableMetadata.builder(keyspace, table).addPartitionKeyColumn("pk1", IntegerType.instance).addClusteringColumn("cl1", IntegerType.instance).addRegularColumn("reg1", AsciiType.instance);
    ColumnIdentifier reg1 = ColumnIdentifier.getInterned("reg1", true);
    builder.indexes(Indexes.of(IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(reg1, IndexTarget.Type.VALUES)), "indexName", IndexMetadata.Kind.COMPOSITES, Collections.emptyMap()), IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(reg1, IndexTarget.Type.KEYS)), "indexName2", IndexMetadata.Kind.COMPOSITES, Collections.emptyMap()), IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(reg1, IndexTarget.Type.KEYS_AND_VALUES)), "indexName3", IndexMetadata.Kind.COMPOSITES, Collections.emptyMap()), IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(reg1, IndexTarget.Type.KEYS_AND_VALUES)), "indexName4", IndexMetadata.Kind.CUSTOM, Collections.singletonMap(IndexTarget.CUSTOM_INDEX_OPTION_NAME, SASIIndex.class.getName())), IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(reg1, IndexTarget.Type.KEYS_AND_VALUES)), "indexName5", IndexMetadata.Kind.CUSTOM, ImmutableMap.of(IndexTarget.CUSTOM_INDEX_OPTION_NAME, SASIIndex.class.getName(), "is_literal", "false"))));
    SchemaLoader.createKeyspace(keyspace, KeyspaceParams.simple(1), builder);
    ColumnFamilyStore cfs = Keyspace.open(keyspace).getColumnFamilyStore(table);
    assertEquals(ImmutableList.of("CREATE INDEX \"indexName\" ON cql_test_keyspace_3.test_table_3 (values(reg1));", "CREATE INDEX \"indexName2\" ON cql_test_keyspace_3.test_table_3 (keys(reg1));", "CREATE INDEX \"indexName3\" ON cql_test_keyspace_3.test_table_3 (entries(reg1));", "CREATE CUSTOM INDEX \"indexName4\" ON cql_test_keyspace_3.test_table_3 (entries(reg1)) USING 'org.apache.cassandra.index.sasi.SASIIndex';", "CREATE CUSTOM INDEX \"indexName5\" ON cql_test_keyspace_3.test_table_3 (entries(reg1)) USING 'org.apache.cassandra.index.sasi.SASIIndex' WITH OPTIONS = {'is_literal': 'false'};"), SchemaCQLHelper.getIndexesAsCQL(cfs.metadata(), false).collect(Collectors.toList()));
}
Also used : IndexTarget(org.apache.cassandra.cql3.statements.schema.IndexTarget) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 9 with IndexTarget

use of org.apache.cassandra.cql3.statements.schema.IndexTarget in project cassandra by apache.

the class AbstractPendingAntiCompactionTest method setup.

@Before
public void setup() {
    ks = "ks_" + System.currentTimeMillis();
    cfm = CreateTableStatement.parse(String.format("CREATE TABLE %s.%s (k INT PRIMARY KEY, v INT)", ks, tbl), ks).build();
    Indexes.Builder indexes = Indexes.builder();
    indexes.add(IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(new ColumnIdentifier("v", true), IndexTarget.Type.VALUES)), tbl2 + "_idx", IndexMetadata.Kind.COMPOSITES, Collections.emptyMap()));
    TableMetadata cfm2 = CreateTableStatement.parse(String.format("CREATE TABLE %s.%s (k INT PRIMARY KEY, v INT)", ks, tbl2), ks).indexes(indexes.build()).build();
    SchemaLoader.createKeyspace(ks, KeyspaceParams.simple(1), cfm, cfm2);
    cfs = Schema.instance.getColumnFamilyStoreInstance(cfm.id);
    cfs2 = Schema.instance.getColumnFamilyStoreInstance(cfm2.id);
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) IndexTarget(org.apache.cassandra.cql3.statements.schema.IndexTarget) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) Indexes(org.apache.cassandra.schema.Indexes) Before(org.junit.Before)

Example 10 with IndexTarget

use of org.apache.cassandra.cql3.statements.schema.IndexTarget in project cassandra by apache.

the class SchemaLoader method compositeIndexCFMD.

public static TableMetadata.Builder compositeIndexCFMD(String ksName, String cfName, boolean withRegularIndex, boolean withStaticIndex) throws ConfigurationException {
    // the withIndex flag exists to allow tests index creation
    // on existing columns
    TableMetadata.Builder builder = TableMetadata.builder(ksName, cfName).addPartitionKeyColumn("key", AsciiType.instance).addClusteringColumn("c1", AsciiType.instance).addRegularColumn("birthdate", LongType.instance).addRegularColumn("notbirthdate", LongType.instance).addStaticColumn("static", LongType.instance).compression(getCompressionParameters());
    Indexes.Builder indexes = Indexes.builder();
    if (withRegularIndex) {
        indexes.add(IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(new ColumnIdentifier("birthdate", true), IndexTarget.Type.VALUES)), cfName + "_birthdate_key_index", IndexMetadata.Kind.COMPOSITES, Collections.EMPTY_MAP));
    }
    if (withStaticIndex) {
        indexes.add(IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(new ColumnIdentifier("static", true), IndexTarget.Type.VALUES)), cfName + "_static_index", IndexMetadata.Kind.COMPOSITES, Collections.EMPTY_MAP));
    }
    return builder.indexes(indexes.build());
}
Also used : IndexTarget(org.apache.cassandra.cql3.statements.schema.IndexTarget) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier)

Aggregations

IndexTarget (org.apache.cassandra.cql3.statements.schema.IndexTarget)10 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)7 TableMetadata (org.apache.cassandra.schema.TableMetadata)5 Test (org.junit.Test)5 IndexMetadata (org.apache.cassandra.schema.IndexMetadata)4 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)3 ByteBuffer (java.nio.ByteBuffer)2 java.util (java.util)2 Future (java.util.concurrent.Future)2 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)2 StubIndex (org.apache.cassandra.index.StubIndex)2 Strings (com.google.common.base.Strings)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.transform (com.google.common.collect.Iterables.transform)1 Iterables.tryFind (com.google.common.collect.Iterables.tryFind)1 Iterators (com.google.common.collect.Iterators)1 Lists (com.google.common.collect.Lists)1 ExecutionException (java.util.concurrent.ExecutionException)1 SchemaLoader (org.apache.cassandra.SchemaLoader)1