Search in sources :

Example 6 with StubIndex

use of org.apache.cassandra.index.StubIndex in project cassandra by apache.

the class SecondaryIndexTest method testUpdatesToMemtableData.

@Test
public void testUpdatesToMemtableData() throws Throwable {
    // verify the contract specified by Index.Indexer::updateRow(oldRowData, newRowData),
    // when a row in the memtable is updated, the indexer should be informed of:
    // * new columns
    // * removed columns
    // * columns whose value, timestamp or ttl have been modified.
    // Any columns which are unchanged by the update are not passed to the Indexer
    // Note that for simplicity this test resets the index between each scenario
    createTable("CREATE TABLE %s (k int, c int, v1 int, v2 int, PRIMARY KEY (k,c))");
    createIndex(String.format("CREATE CUSTOM INDEX test_index ON %%s() USING '%s'", StubIndex.class.getName()));
    execute("INSERT INTO %s (k, c, v1, v2) VALUES (0, 0, 0, 0) USING TIMESTAMP 0");
    ColumnMetadata v1 = getCurrentColumnFamilyStore().metadata().getColumn(new ColumnIdentifier("v1", true));
    ColumnMetadata v2 = getCurrentColumnFamilyStore().metadata().getColumn(new ColumnIdentifier("v2", true));
    StubIndex index = (StubIndex) getCurrentColumnFamilyStore().indexManager.getIndexByName("test_index");
    assertEquals(1, index.rowsInserted.size());
    // Overwrite a single value, leaving the other untouched
    execute("UPDATE %s USING TIMESTAMP 1 SET v1=1 WHERE k=0 AND c=0");
    assertEquals(1, index.rowsUpdated.size());
    Row oldRow = index.rowsUpdated.get(0).left;
    assertEquals(1, oldRow.size());
    validateCell(oldRow.getCell(v1), v1, ByteBufferUtil.bytes(0), 0);
    Row newRow = index.rowsUpdated.get(0).right;
    assertEquals(1, newRow.size());
    validateCell(newRow.getCell(v1), v1, ByteBufferUtil.bytes(1), 1);
    index.reset();
    // Overwrite both values
    execute("UPDATE %s USING TIMESTAMP 2 SET v1=2, v2=2 WHERE k=0 AND c=0");
    assertEquals(1, index.rowsUpdated.size());
    oldRow = index.rowsUpdated.get(0).left;
    assertEquals(2, oldRow.size());
    validateCell(oldRow.getCell(v1), v1, ByteBufferUtil.bytes(1), 1);
    validateCell(oldRow.getCell(v2), v2, ByteBufferUtil.bytes(0), 0);
    newRow = index.rowsUpdated.get(0).right;
    assertEquals(2, newRow.size());
    validateCell(newRow.getCell(v1), v1, ByteBufferUtil.bytes(2), 2);
    validateCell(newRow.getCell(v2), v2, ByteBufferUtil.bytes(2), 2);
    index.reset();
    // Delete one value
    execute("DELETE v1 FROM %s USING TIMESTAMP 3 WHERE k=0 AND c=0");
    assertEquals(1, index.rowsUpdated.size());
    oldRow = index.rowsUpdated.get(0).left;
    assertEquals(1, oldRow.size());
    validateCell(oldRow.getCell(v1), v1, ByteBufferUtil.bytes(2), 2);
    newRow = index.rowsUpdated.get(0).right;
    assertEquals(1, newRow.size());
    Cell newCell = newRow.getCell(v1);
    assertTrue(newCell.isTombstone());
    assertEquals(3, newCell.timestamp());
    index.reset();
    // Modify the liveness of the primary key, the delta rows should contain
    // no cell data as only the pk was altered, but it should illustrate the
    // change to the liveness info
    execute("INSERT INTO %s(k, c) VALUES (0, 0) USING TIMESTAMP 4");
    assertEquals(1, index.rowsUpdated.size());
    oldRow = index.rowsUpdated.get(0).left;
    assertEquals(0, oldRow.size());
    assertEquals(0, oldRow.primaryKeyLivenessInfo().timestamp());
    newRow = index.rowsUpdated.get(0).right;
    assertEquals(0, newRow.size());
    assertEquals(4, newRow.primaryKeyLivenessInfo().timestamp());
}
Also used : StubIndex(org.apache.cassandra.index.StubIndex) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) Row(org.apache.cassandra.db.rows.Row) Cell(org.apache.cassandra.db.rows.Cell) Test(org.junit.Test)

Aggregations

StubIndex (org.apache.cassandra.index.StubIndex)6 Test (org.junit.Test)6 TableMetadata (org.apache.cassandra.schema.TableMetadata)4 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)3 ByteBuffer (java.nio.ByteBuffer)2 IndexTarget (org.apache.cassandra.cql3.statements.IndexTarget)2 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)2 Row (org.apache.cassandra.db.rows.Row)2 IndexMetadata (org.apache.cassandra.schema.IndexMetadata)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Iterators (com.google.common.collect.Iterators)1 java.util (java.util)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 SchemaLoader (org.apache.cassandra.SchemaLoader)1 SchemaLoader.standardCFMD (org.apache.cassandra.SchemaLoader.standardCFMD)1 UpdateBuilder (org.apache.cassandra.UpdateBuilder)1 Util (org.apache.cassandra.Util)1 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)1 CompactionManager (org.apache.cassandra.db.compaction.CompactionManager)1