Search in sources :

Example 21 with ColumnMetadata

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

the class CellTest method testValidate.

@Test
public void testValidate() {
    ColumnMetadata c;
    // Valid cells
    c = fakeColumn("c", Int32Type.instance);
    assertValid(BufferCell.live(c, 0, ByteBufferUtil.EMPTY_BYTE_BUFFER));
    assertValid(BufferCell.live(c, 0, ByteBufferUtil.bytes(4)));
    assertValid(BufferCell.expiring(c, 0, 4, 4, ByteBufferUtil.EMPTY_BYTE_BUFFER));
    assertValid(BufferCell.expiring(c, 0, 4, 4, ByteBufferUtil.bytes(4)));
    assertValid(BufferCell.tombstone(c, 0, 4));
    // Invalid value (we don't all empty values for smallint)
    c = fakeColumn("c", ShortType.instance);
    assertInvalid(BufferCell.live(c, 0, ByteBufferUtil.EMPTY_BYTE_BUFFER));
    // But this should be valid even though the underlying value is an empty BB (catches bug #11618)
    assertValid(BufferCell.tombstone(c, 0, 4));
    // And of course, this should be valid with a proper value
    assertValid(BufferCell.live(c, 0, bbs(4)));
    // Invalid ttl
    assertInvalid(BufferCell.expiring(c, 0, -4, 4, bbs(4)));
    // Invalid local deletion times
    assertInvalid(BufferCell.expiring(c, 0, 4, -5, bbs(4)));
    assertInvalid(BufferCell.expiring(c, 0, 4, Cell.NO_DELETION_TIME, bbs(4)));
    c = fakeColumn("c", MapType.getInstance(Int32Type.instance, Int32Type.instance, true));
    // Valid cell path
    assertValid(BufferCell.live(c, 0, ByteBufferUtil.bytes(4), CellPath.create(ByteBufferUtil.bytes(4))));
    // Invalid cell path (int values should be 0 or 4 bytes)
    assertInvalid(BufferCell.live(c, 0, ByteBufferUtil.bytes(4), CellPath.create(ByteBufferUtil.bytes((long) 4))));
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) Test(org.junit.Test)

Example 22 with ColumnMetadata

use of org.apache.cassandra.schema.ColumnMetadata 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)

Example 23 with ColumnMetadata

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

the class CounterCacheTest method testCounterCacheInvalidate.

@Test
public void testCounterCacheInvalidate() {
    ColumnFamilyStore cfs = Keyspace.open(KEYSPACE1).getColumnFamilyStore(COUNTER1);
    cfs.truncateBlocking();
    CacheService.instance.invalidateCounterCache();
    Clustering c1 = CBuilder.create(cfs.metadata().comparator).add(ByteBufferUtil.bytes(1)).build();
    Clustering c2 = CBuilder.create(cfs.metadata().comparator).add(ByteBufferUtil.bytes(2)).build();
    ColumnMetadata cd = cfs.metadata().getColumn(ByteBufferUtil.bytes("c"));
    assertEquals(0, CacheService.instance.counterCache.size());
    assertNull(cfs.getCachedCounter(bytes(1), c1, cd, null));
    assertNull(cfs.getCachedCounter(bytes(1), c2, cd, null));
    assertNull(cfs.getCachedCounter(bytes(2), c1, cd, null));
    assertNull(cfs.getCachedCounter(bytes(2), c2, cd, null));
    assertNull(cfs.getCachedCounter(bytes(3), c1, cd, null));
    assertNull(cfs.getCachedCounter(bytes(3), c2, cd, null));
    cfs.putCachedCounter(bytes(1), c1, cd, null, ClockAndCount.create(1L, 1L));
    cfs.putCachedCounter(bytes(1), c2, cd, null, ClockAndCount.create(1L, 2L));
    cfs.putCachedCounter(bytes(2), c1, cd, null, ClockAndCount.create(2L, 1L));
    cfs.putCachedCounter(bytes(2), c2, cd, null, ClockAndCount.create(2L, 2L));
    cfs.putCachedCounter(bytes(3), c1, cd, null, ClockAndCount.create(3L, 1L));
    cfs.putCachedCounter(bytes(3), c2, cd, null, ClockAndCount.create(3L, 2L));
    assertEquals(ClockAndCount.create(1L, 1L), cfs.getCachedCounter(bytes(1), c1, cd, null));
    assertEquals(ClockAndCount.create(1L, 2L), cfs.getCachedCounter(bytes(1), c2, cd, null));
    assertEquals(ClockAndCount.create(2L, 1L), cfs.getCachedCounter(bytes(2), c1, cd, null));
    assertEquals(ClockAndCount.create(2L, 2L), cfs.getCachedCounter(bytes(2), c2, cd, null));
    assertEquals(ClockAndCount.create(3L, 1L), cfs.getCachedCounter(bytes(3), c1, cd, null));
    assertEquals(ClockAndCount.create(3L, 2L), cfs.getCachedCounter(bytes(3), c2, cd, null));
    cfs.invalidateCounterCache(Collections.singleton(new Bounds<Token>(cfs.decorateKey(bytes(1)).getToken(), cfs.decorateKey(bytes(2)).getToken())));
    assertEquals(2, CacheService.instance.counterCache.size());
    assertNull(cfs.getCachedCounter(bytes(1), c1, cd, null));
    assertNull(cfs.getCachedCounter(bytes(1), c2, cd, null));
    assertNull(cfs.getCachedCounter(bytes(2), c1, cd, null));
    assertNull(cfs.getCachedCounter(bytes(2), c2, cd, null));
    assertEquals(ClockAndCount.create(3L, 1L), cfs.getCachedCounter(bytes(3), c1, cd, null));
    assertEquals(ClockAndCount.create(3L, 2L), cfs.getCachedCounter(bytes(3), c2, cd, null));
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) Bounds(org.apache.cassandra.dht.Bounds) Test(org.junit.Test)

Example 24 with ColumnMetadata

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

the class CounterCellTest method createCounterCell.

private Cell createCounterCell(ColumnFamilyStore cfs, ByteBuffer colName, CounterId id, long count, long ts) {
    ColumnMetadata cDef = cfs.metadata().getColumn(colName);
    ByteBuffer val = CounterContext.instance().createGlobal(id, ts, count);
    return BufferCell.live(cDef, ts, val);
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) ByteBuffer(java.nio.ByteBuffer)

Example 25 with ColumnMetadata

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

the class SecondaryIndexTest method testIndexCreate.

@Test
public void testIndexCreate() throws IOException, InterruptedException, ExecutionException {
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    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
    new RowUpdateBuilder(cfs.metadata(), 0, "k1").clustering("c").add("birthdate", 1L).build().applyUnsafe();
    String indexName = "birthdate_index";
    ColumnMetadata old = cfs.metadata().getColumn(ByteBufferUtil.bytes("birthdate"));
    IndexMetadata indexDef = IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(old.name, IndexTarget.Type.VALUES)), indexName, IndexMetadata.Kind.COMPOSITES, Collections.EMPTY_MAP);
    TableMetadata current = cfs.metadata();
    TableMetadata updated = current.unbuild().indexes(current.indexes.with(indexDef)).build();
    MigrationManager.announceTableUpdate(updated, true);
    // fait for the index to be built
    Index index = cfs.indexManager.getIndex(indexDef);
    do {
        TimeUnit.MILLISECONDS.sleep(100);
    } while (!cfs.indexManager.isIndexQueryable(index));
    // 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
    ColumnFamilyStore indexCfs = cfs.indexManager.getIndex(indexDef).getBackingTable().orElseThrow(throwAssert("Index not found"));
    assertFalse(indexCfs.getLiveSSTables().isEmpty());
    assertIndexedOne(cfs, ByteBufferUtil.bytes("birthdate"), 1L);
    // validate that drop clears it out & rebuild works (CASSANDRA-2320)
    assertTrue(cfs.getBuiltIndexes().contains(indexName));
    cfs.indexManager.removeIndex(indexDef.name);
    assertFalse(cfs.getBuiltIndexes().contains(indexName));
    // rebuild & re-query
    Future future = cfs.indexManager.addIndex(indexDef);
    future.get();
    assertIndexedOne(cfs, ByteBufferUtil.bytes("birthdate"), 1L);
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) IndexTarget(org.apache.cassandra.cql3.statements.IndexTarget) Future(java.util.concurrent.Future) Index(org.apache.cassandra.index.Index) IndexMetadata(org.apache.cassandra.schema.IndexMetadata) Test(org.junit.Test)

Aggregations

ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)123 Test (org.junit.Test)35 ByteBuffer (java.nio.ByteBuffer)23 Row (org.apache.cassandra.db.rows.Row)17 TableMetadata (org.apache.cassandra.schema.TableMetadata)16 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)15 AbstractType (org.apache.cassandra.db.marshal.AbstractType)8 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)5 java.util (java.util)4 IndexTarget (org.apache.cassandra.cql3.statements.IndexTarget)4 CollectionType (org.apache.cassandra.db.marshal.CollectionType)4 IndexMetadata (org.apache.cassandra.schema.IndexMetadata)4 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)3 ColumnFilter (org.apache.cassandra.db.filter.ColumnFilter)3 ImmutableBTreePartition (org.apache.cassandra.db.partitions.ImmutableBTreePartition)3 Cell (org.apache.cassandra.db.rows.Cell)3 RowIterator (org.apache.cassandra.db.rows.RowIterator)3 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)3