Search in sources :

Example 21 with TableMetadata

use of org.apache.cassandra.schema.TableMetadata 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);
    // 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.IndexTarget) IndexMetadata(org.apache.cassandra.schema.IndexMetadata) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 22 with TableMetadata

use of org.apache.cassandra.schema.TableMetadata 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);
    Directories indexDirectories = new Directories(INDEX_CFM);
    // 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.getParentFile());
    // check if snapshot directory exists
    parentSnapshotDirectory.mkdirs();
    assertTrue(parentDirectories.snapshotExists("test"));
    assertTrue(indexDirectories.snapshotExists("test"));
    // check their creation time
    assertEquals(parentDirectories.snapshotCreationTime("test"), indexDirectories.snapshotCreationTime("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, Pair<Long, Long>> parentSnapshotDetail = parentDirectories.getSnapshotDetails();
    assertTrue(parentSnapshotDetail.containsKey("test"));
    assertEquals(30L, parentSnapshotDetail.get("test").right.longValue());
    Map<String, Pair<Long, Long>> indexSnapshotDetail = indexDirectories.getSnapshotDetails();
    assertTrue(indexSnapshotDetail.containsKey("test"));
    assertEquals(40L, indexSnapshotDetail.get("test").right.longValue());
    // check backup directory
    File parentBackupDirectory = Directories.getBackupsDirectory(parentDesc);
    File indexBackupDirectory = Directories.getBackupsDirectory(indexDesc);
    assertEquals(parentBackupDirectory, indexBackupDirectory.getParentFile());
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) IndexTarget(org.apache.cassandra.cql3.statements.IndexTarget) 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(java.io.File) Pair(org.apache.cassandra.utils.Pair) Test(org.junit.Test)

Example 23 with TableMetadata

use of org.apache.cassandra.schema.TableMetadata in project cassandra by apache.

the class DirectoriesTest method testTemporaryFile.

@Test
public void testTemporaryFile() throws IOException {
    for (TableMetadata cfm : CFM) {
        Directories directories = new Directories(cfm);
        File tempDir = directories.getTemporaryWriteableDirectoryAsFile(10);
        tempDir.mkdir();
        File tempFile = new File(tempDir, "tempFile");
        tempFile.createNewFile();
        assertTrue(tempDir.exists());
        assertTrue(tempFile.exists());
        //make sure temp dir/file will not affect existing sstable listing
        checkFiles(cfm, directories);
        directories.removeTemporaryDirectories();
        //make sure temp dir/file deletion will not affect existing sstable listing
        checkFiles(cfm, directories);
        assertFalse(tempDir.exists());
        assertFalse(tempFile.exists());
    }
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) File(java.io.File) Test(org.junit.Test)

Example 24 with TableMetadata

use of org.apache.cassandra.schema.TableMetadata in project cassandra by apache.

the class CreateTest method assertTriggerDoesNotExists.

private void assertTriggerDoesNotExists(String name) {
    TableMetadata metadata = Schema.instance.getTableMetadata(keyspace(), currentTable());
    assertFalse("the trigger exists", metadata.triggers.get(name).isPresent());
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata)

Example 25 with TableMetadata

use of org.apache.cassandra.schema.TableMetadata in project cassandra by apache.

the class ColumnsTest method mock.

private static TableMetadata mock(Columns columns) {
    if (columns.isEmpty())
        return TABLE_METADATA;
    TableMetadata.Builder builder = TableMetadata.builder(TABLE_METADATA.keyspace, TABLE_METADATA.name);
    boolean hasPartitionKey = false;
    for (ColumnMetadata def : columns) {
        switch(def.kind) {
            case PARTITION_KEY:
                builder.addPartitionKeyColumn(def.name, def.type);
                hasPartitionKey = true;
                break;
            case CLUSTERING:
                builder.addClusteringColumn(def.name, def.type);
                break;
            case REGULAR:
                builder.addRegularColumn(def.name, def.type);
                break;
        }
    }
    if (!hasPartitionKey)
        builder.addPartitionKeyColumn("219894021498309239rufejsfjdksfjheiwfhjes", UTF8Type.instance);
    return builder.build();
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata)

Aggregations

TableMetadata (org.apache.cassandra.schema.TableMetadata)129 Test (org.junit.Test)63 ByteBuffer (java.nio.ByteBuffer)29 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)17 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)13 File (java.io.File)10 PartitionUpdate (org.apache.cassandra.db.partitions.PartitionUpdate)10 Mutation (org.apache.cassandra.db.Mutation)8 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)8 KeyspaceMetadata (org.apache.cassandra.schema.KeyspaceMetadata)8 Descriptor (org.apache.cassandra.io.sstable.Descriptor)7 IndexMetadata (org.apache.cassandra.schema.IndexMetadata)6 IOException (java.io.IOException)5 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)5 IndexTarget (org.apache.cassandra.cql3.statements.IndexTarget)5 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)5 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)4 UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)4 AbstractType (org.apache.cassandra.db.marshal.AbstractType)4 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)4