Search in sources :

Example 11 with IdentityQueryFilter

use of org.apache.cassandra.db.columniterator.IdentityQueryFilter in project eiger by wlloyd.

the class ColumnFamilyStoreTest method testIndexDeletions.

@Test
public void testIndexDeletions() throws IOException {
    ColumnFamilyStore cfs = Table.open("Keyspace3").getColumnFamilyStore("Indexed1");
    RowMutation rm;
    rm = new RowMutation("Keyspace3", ByteBufferUtil.bytes("k1"));
    rm.add(new QueryPath("Indexed1", null, ByteBufferUtil.bytes("birthdate")), ByteBufferUtil.bytes(1L), 0);
    rm.apply();
    IndexExpression expr = new IndexExpression(ByteBufferUtil.bytes("birthdate"), IndexOperator.EQ, ByteBufferUtil.bytes(1L));
    List<IndexExpression> clause = Arrays.asList(expr);
    IFilter filter = new IdentityQueryFilter();
    IPartitioner p = StorageService.getPartitioner();
    Range<RowPosition> range = Util.range("", "");
    List<Row> rows = cfs.search(clause, range, 100, filter);
    assert rows.size() == 1 : StringUtils.join(rows, ",");
    String key = ByteBufferUtil.string(rows.get(0).key.key);
    assert "k1".equals(key);
    // delete the column directly
    rm = new RowMutation("Keyspace3", ByteBufferUtil.bytes("k1"));
    rm.delete(new QueryPath("Indexed1", null, ByteBufferUtil.bytes("birthdate")), 1);
    rm.apply();
    rows = cfs.search(clause, range, 100, filter);
    assert rows.isEmpty();
    // verify that it's not being indexed under the deletion column value either
    IColumn deletion = rm.getColumnFamilies().iterator().next().iterator().next();
    ByteBuffer deletionLong = ByteBufferUtil.bytes((long) ByteBufferUtil.toInt(deletion.value()));
    IndexExpression expr0 = new IndexExpression(ByteBufferUtil.bytes("birthdate"), IndexOperator.EQ, deletionLong);
    List<IndexExpression> clause0 = Arrays.asList(expr0);
    rows = cfs.search(clause0, range, 100, filter);
    assert rows.isEmpty();
    // resurrect w/ a newer timestamp
    rm = new RowMutation("Keyspace3", ByteBufferUtil.bytes("k1"));
    rm.add(new QueryPath("Indexed1", null, ByteBufferUtil.bytes("birthdate")), ByteBufferUtil.bytes(1L), 2);
    rm.apply();
    rows = cfs.search(clause, range, 100, filter);
    assert rows.size() == 1 : StringUtils.join(rows, ",");
    key = ByteBufferUtil.string(rows.get(0).key.key);
    assert "k1".equals(key);
    // verify that row and delete w/ older timestamp does nothing
    rm = new RowMutation("Keyspace3", ByteBufferUtil.bytes("k1"));
    rm.delete(new QueryPath("Indexed1"), 1);
    rm.apply();
    rows = cfs.search(clause, range, 100, filter);
    assert rows.size() == 1 : StringUtils.join(rows, ",");
    key = ByteBufferUtil.string(rows.get(0).key.key);
    assert "k1".equals(key);
    // similarly, column delete w/ older timestamp should do nothing
    rm = new RowMutation("Keyspace3", ByteBufferUtil.bytes("k1"));
    rm.delete(new QueryPath("Indexed1", null, ByteBufferUtil.bytes("birthdate")), 1);
    rm.apply();
    rows = cfs.search(clause, range, 100, filter);
    assert rows.size() == 1 : StringUtils.join(rows, ",");
    key = ByteBufferUtil.string(rows.get(0).key.key);
    assert "k1".equals(key);
    // delete the entire row (w/ newer timestamp this time)
    rm = new RowMutation("Keyspace3", ByteBufferUtil.bytes("k1"));
    rm.delete(new QueryPath("Indexed1"), 3);
    rm.apply();
    rows = cfs.search(clause, range, 100, filter);
    assert rows.isEmpty() : StringUtils.join(rows, ",");
    // make sure obsolete mutations don't generate an index entry
    rm = new RowMutation("Keyspace3", ByteBufferUtil.bytes("k1"));
    rm.add(new QueryPath("Indexed1", null, ByteBufferUtil.bytes("birthdate")), ByteBufferUtil.bytes(1L), 3);
    rm.apply();
    rows = cfs.search(clause, range, 100, filter);
    assert rows.isEmpty() : StringUtils.join(rows, ",");
    // try insert followed by row delete in the same mutation
    rm = new RowMutation("Keyspace3", ByteBufferUtil.bytes("k1"));
    rm.add(new QueryPath("Indexed1", null, ByteBufferUtil.bytes("birthdate")), ByteBufferUtil.bytes(1L), 1);
    rm.delete(new QueryPath("Indexed1"), 2);
    rm.apply();
    rows = cfs.search(clause, range, 100, filter);
    assert rows.isEmpty() : StringUtils.join(rows, ",");
    // try row delete followed by insert in the same mutation
    rm = new RowMutation("Keyspace3", ByteBufferUtil.bytes("k1"));
    rm.delete(new QueryPath("Indexed1"), 3);
    rm.add(new QueryPath("Indexed1", null, ByteBufferUtil.bytes("birthdate")), ByteBufferUtil.bytes(1L), 4);
    rm.apply();
    rows = cfs.search(clause, range, 100, filter);
    assert rows.size() == 1 : StringUtils.join(rows, ",");
    key = ByteBufferUtil.string(rows.get(0).key.key);
    assert "k1".equals(key);
}
Also used : ByteBuffer(java.nio.ByteBuffer) IdentityQueryFilter(org.apache.cassandra.db.columniterator.IdentityQueryFilter) IPartitioner(org.apache.cassandra.dht.IPartitioner) Test(org.junit.Test)

Aggregations

IdentityQueryFilter (org.apache.cassandra.db.columniterator.IdentityQueryFilter)11 IPartitioner (org.apache.cassandra.dht.IPartitioner)9 Test (org.junit.Test)8 IndexExpression (org.apache.cassandra.thrift.IndexExpression)3 IFilter (org.apache.cassandra.db.filter.IFilter)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 QueryFilter (org.apache.cassandra.db.filter.QueryFilter)1 QueryPath (org.apache.cassandra.db.filter.QueryPath)1 SecondaryIndex (org.apache.cassandra.db.index.SecondaryIndex)1 BytesToken (org.apache.cassandra.dht.BytesToken)1 SSTable (org.apache.cassandra.io.sstable.SSTable)1 TokenMetadata (org.apache.cassandra.locator.TokenMetadata)1 CloseableIterator (org.apache.cassandra.utils.CloseableIterator)1 NodeId (org.apache.cassandra.utils.NodeId)1