Search in sources :

Example 31 with ColumnIdentifier

use of org.apache.cassandra.cql3.ColumnIdentifier in project cassandra by apache.

the class SerializationHeaderTest method testWrittenAsDifferentKind.

@Test
public void testWrittenAsDifferentKind() throws Exception {
    final String tableName = "testWrittenAsDifferentKind";
    // final String schemaCqlWithStatic = String.format("CREATE TABLE %s (k int, c int, v int static, PRIMARY KEY(k, c))", tableName);
    // final String schemaCqlWithRegular = String.format("CREATE TABLE %s (k int, c int, v int, PRIMARY KEY(k, c))", tableName);
    ColumnIdentifier v = ColumnIdentifier.getInterned("v", false);
    TableMetadata schemaWithStatic = TableMetadata.builder(KEYSPACE, tableName).addPartitionKeyColumn("k", Int32Type.instance).addClusteringColumn("c", Int32Type.instance).addStaticColumn("v", Int32Type.instance).build();
    TableMetadata schemaWithRegular = TableMetadata.builder(KEYSPACE, tableName).addPartitionKeyColumn("k", Int32Type.instance).addClusteringColumn("c", Int32Type.instance).addRegularColumn("v", Int32Type.instance).build();
    ColumnMetadata columnStatic = schemaWithStatic.getColumn(v);
    ColumnMetadata columnRegular = schemaWithRegular.getColumn(v);
    schemaWithStatic = schemaWithStatic.unbuild().recordColumnDrop(columnRegular, 0L).build();
    schemaWithRegular = schemaWithRegular.unbuild().recordColumnDrop(columnStatic, 0L).build();
    final AtomicInteger generation = new AtomicInteger();
    File dir = new File(Files.createTempDir());
    try {
        BiFunction<TableMetadata, Function<ByteBuffer, Clustering<?>>, Callable<Descriptor>> writer = (schema, clusteringFunction) -> () -> {
            Descriptor descriptor = new Descriptor(BigFormat.latestVersion, dir, schema.keyspace, schema.name, generation.incrementAndGet(), SSTableFormat.Type.BIG);
            SerializationHeader header = SerializationHeader.makeWithoutStats(schema);
            try (LifecycleTransaction txn = LifecycleTransaction.offline(OperationType.WRITE);
                SSTableWriter sstableWriter = BigTableWriter.create(TableMetadataRef.forOfflineTools(schema), descriptor, 1, 0L, null, false, 0, header, Collections.emptyList(), txn)) {
                ColumnMetadata cd = schema.getColumn(v);
                for (int i = 0; i < 5; ++i) {
                    final ByteBuffer value = Int32Type.instance.decompose(i);
                    Cell<?> cell = BufferCell.live(cd, 1L, value);
                    Clustering<?> clustering = clusteringFunction.apply(value);
                    Row row = BTreeRow.singleCellRow(clustering, cell);
                    sstableWriter.append(PartitionUpdate.singleRowUpdate(schema, value, row).unfilteredIterator());
                }
                sstableWriter.finish(false);
                txn.finish();
            }
            return descriptor;
        };
        Descriptor sstableWithRegular = writer.apply(schemaWithRegular, BufferClustering::new).call();
        Descriptor sstableWithStatic = writer.apply(schemaWithStatic, value -> Clustering.STATIC_CLUSTERING).call();
        SSTableReader readerWithStatic = SSTableReader.openNoValidation(sstableWithStatic, TableMetadataRef.forOfflineTools(schemaWithRegular));
        SSTableReader readerWithRegular = SSTableReader.openNoValidation(sstableWithRegular, TableMetadataRef.forOfflineTools(schemaWithStatic));
        try (ISSTableScanner partitions = readerWithStatic.getScanner()) {
            for (int i = 0; i < 5; ++i) {
                UnfilteredRowIterator partition = partitions.next();
                Assert.assertFalse(partition.hasNext());
                long value = Int32Type.instance.compose(partition.staticRow().getCell(columnStatic).buffer());
                Assert.assertEquals(value, (long) i);
            }
            Assert.assertFalse(partitions.hasNext());
        }
        try (ISSTableScanner partitions = readerWithRegular.getScanner()) {
            for (int i = 0; i < 5; ++i) {
                UnfilteredRowIterator partition = partitions.next();
                long value = Int32Type.instance.compose(((Row) partition.next()).getCell(columnRegular).buffer());
                Assert.assertEquals(value, (long) i);
                Assert.assertTrue(partition.staticRow().isEmpty());
                Assert.assertFalse(partition.hasNext());
            }
            Assert.assertFalse(partitions.hasNext());
        }
    } finally {
        FileUtils.deleteRecursive(dir);
    }
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) BiFunction(java.util.function.BiFunction) File(org.apache.cassandra.io.util.File) Callable(java.util.concurrent.Callable) BTreeRow(org.apache.cassandra.db.rows.BTreeRow) Function(java.util.function.Function) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ByteBuffer(java.nio.ByteBuffer) Int32Type(org.apache.cassandra.db.marshal.Int32Type) BufferCell(org.apache.cassandra.db.rows.BufferCell) Row(org.apache.cassandra.db.rows.Row) Files(com.google.common.io.Files) OperationType(org.apache.cassandra.db.compaction.OperationType) UnfilteredRowIterator(org.apache.cassandra.db.rows.UnfilteredRowIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Descriptor(org.apache.cassandra.io.sstable.Descriptor) SSTableFormat(org.apache.cassandra.io.sstable.format.SSTableFormat) BigFormat(org.apache.cassandra.io.sstable.format.big.BigFormat) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) BigTableWriter(org.apache.cassandra.io.sstable.format.big.BigTableWriter) ISSTableScanner(org.apache.cassandra.io.sstable.ISSTableScanner) Test(org.junit.Test) LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) FileUtils(org.apache.cassandra.io.util.FileUtils) TableMetadataRef(org.apache.cassandra.schema.TableMetadataRef) Cell(org.apache.cassandra.db.rows.Cell) TableMetadata(org.apache.cassandra.schema.TableMetadata) Assert(org.junit.Assert) SSTableWriter(org.apache.cassandra.io.sstable.format.SSTableWriter) Collections(java.util.Collections) ISSTableScanner(org.apache.cassandra.io.sstable.ISSTableScanner) UnfilteredRowIterator(org.apache.cassandra.db.rows.UnfilteredRowIterator) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) SSTableWriter(org.apache.cassandra.io.sstable.format.SSTableWriter) LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) ByteBuffer(java.nio.ByteBuffer) Callable(java.util.concurrent.Callable) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Descriptor(org.apache.cassandra.io.sstable.Descriptor) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) BTreeRow(org.apache.cassandra.db.rows.BTreeRow) Row(org.apache.cassandra.db.rows.Row) File(org.apache.cassandra.io.util.File) BufferCell(org.apache.cassandra.db.rows.BufferCell) Cell(org.apache.cassandra.db.rows.Cell) Test(org.junit.Test)

Example 32 with ColumnIdentifier

use of org.apache.cassandra.cql3.ColumnIdentifier in project cassandra by apache.

the class KeyspaceTest method testGetRowSingleColumn.

@Test
public void testGetRowSingleColumn() throws Throwable {
    createTable("CREATE TABLE %s (a text, b int, c int, PRIMARY KEY (a, b))");
    for (int i = 0; i < 2; i++) execute("INSERT INTO %s (a, b, c) VALUES (?, ?, ?)", "0", i, i);
    final ColumnFamilyStore cfs = getCurrentColumnFamilyStore();
    for (int round = 0; round < 2; round++) {
        // slice with limit 1
        Row row = Util.getOnlyRow(Util.cmd(cfs, "0").columns("c").withLimit(1).build());
        assertEquals(ByteBufferUtil.bytes(0), row.getCell(cfs.metadata().getColumn(new ColumnIdentifier("c", false))).buffer());
        // fetch each row by name
        for (int i = 0; i < 2; i++) {
            row = Util.getOnlyRow(Util.cmd(cfs, "0").columns("c").includeRow(i).build());
            assertEquals(ByteBufferUtil.bytes(i), row.getCell(cfs.metadata().getColumn(new ColumnIdentifier("c", false))).buffer());
        }
        // fetch each row by slice
        for (int i = 0; i < 2; i++) {
            row = Util.getOnlyRow(Util.cmd(cfs, "0").columns("c").fromIncl(i).toIncl(i).build());
            assertEquals(ByteBufferUtil.bytes(i), row.getCell(cfs.metadata().getColumn(new ColumnIdentifier("c", false))).buffer());
        }
        if (round == 0)
            cfs.forceBlockingFlush();
    }
}
Also used : ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) Row(org.apache.cassandra.db.rows.Row) Test(org.junit.Test)

Example 33 with ColumnIdentifier

use of org.apache.cassandra.cql3.ColumnIdentifier in project cassandra by apache.

the class KeyspaceTest method assertRowsInSlice.

private static void assertRowsInSlice(ColumnFamilyStore cfs, String key, int sliceStart, int sliceEnd, int limit, boolean reversed, String columnValuePrefix) {
    Clustering<?> startClustering = Clustering.make(ByteBufferUtil.bytes(sliceStart));
    Clustering<?> endClustering = Clustering.make(ByteBufferUtil.bytes(sliceEnd));
    Slices slices = Slices.with(cfs.getComparator(), Slice.make(startClustering, endClustering));
    ClusteringIndexSliceFilter filter = new ClusteringIndexSliceFilter(slices, reversed);
    SinglePartitionReadCommand command = singlePartitionSlice(cfs, key, filter, limit);
    try (ReadExecutionController executionController = command.executionController();
        PartitionIterator iterator = command.executeInternal(executionController)) {
        try (RowIterator rowIterator = iterator.next()) {
            if (reversed) {
                for (int i = sliceEnd; i >= sliceStart; i--) {
                    Row row = rowIterator.next();
                    Cell<?> cell = row.getCell(cfs.metadata().getColumn(new ColumnIdentifier("c", false)));
                    assertEquals(ByteBufferUtil.bytes(columnValuePrefix + i), cell.buffer());
                }
            } else {
                for (int i = sliceStart; i <= sliceEnd; i++) {
                    Row row = rowIterator.next();
                    Cell<?> cell = row.getCell(cfs.metadata().getColumn(new ColumnIdentifier("c", false)));
                    assertEquals(ByteBufferUtil.bytes(columnValuePrefix + i), cell.buffer());
                }
            }
            assertFalse(rowIterator.hasNext());
        }
    }
}
Also used : PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) RowIterator(org.apache.cassandra.db.rows.RowIterator) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) Row(org.apache.cassandra.db.rows.Row)

Example 34 with ColumnIdentifier

use of org.apache.cassandra.cql3.ColumnIdentifier in project cassandra by apache.

the class KeyspaceTest method assertRowsInResult.

private static void assertRowsInResult(ColumnFamilyStore cfs, SinglePartitionReadCommand command, int... columnValues) {
    try (ReadExecutionController executionController = command.executionController();
        PartitionIterator iterator = command.executeInternal(executionController)) {
        if (columnValues.length == 0) {
            if (iterator.hasNext())
                fail("Didn't expect any results, but got rows starting with: " + iterator.next().next().toString(cfs.metadata()));
            return;
        }
        try (RowIterator rowIterator = iterator.next()) {
            for (int expected : columnValues) {
                Row row = rowIterator.next();
                Cell<?> cell = row.getCell(cfs.metadata().getColumn(new ColumnIdentifier("c", false)));
                assertEquals(String.format("Expected %s, but got %s", ByteBufferUtil.bytesToHex(ByteBufferUtil.bytes(expected)), ByteBufferUtil.bytesToHex(cell.buffer())), ByteBufferUtil.bytes(expected), cell.buffer());
            }
            assertFalse(rowIterator.hasNext());
        }
    }
}
Also used : PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) RowIterator(org.apache.cassandra.db.rows.RowIterator) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) Row(org.apache.cassandra.db.rows.Row)

Example 35 with ColumnIdentifier

use of org.apache.cassandra.cql3.ColumnIdentifier in project cassandra by apache.

the class RowsTest method testLegacyCellIterator.

@Test
public void testLegacyCellIterator() {
    // Creates a table with
    // - 3 Simple columns: a, c and e
    // - 2 Complex columns: b and d
    TableMetadata metadata = TableMetadata.builder("dummy_ks", "dummy_tbl").addPartitionKeyColumn("k", BytesType.instance).addRegularColumn("a", BytesType.instance).addRegularColumn("b", MapType.getInstance(Int32Type.instance, BytesType.instance, true)).addRegularColumn("c", BytesType.instance).addRegularColumn("d", MapType.getInstance(Int32Type.instance, BytesType.instance, true)).addRegularColumn("e", BytesType.instance).build();
    ColumnMetadata a = metadata.getColumn(new ColumnIdentifier("a", false));
    ColumnMetadata b = metadata.getColumn(new ColumnIdentifier("b", false));
    ColumnMetadata c = metadata.getColumn(new ColumnIdentifier("c", false));
    ColumnMetadata d = metadata.getColumn(new ColumnIdentifier("d", false));
    ColumnMetadata e = metadata.getColumn(new ColumnIdentifier("e", false));
    Row row;
    // Row with only simple columns
    row = makeDummyRow(liveCell(a), liveCell(c), liveCell(e));
    assertCellOrder(row.cellsInLegacyOrder(metadata, false), liveCell(a), liveCell(c), liveCell(e));
    assertCellOrder(row.cellsInLegacyOrder(metadata, true), liveCell(e), liveCell(c), liveCell(a));
    // Row with only complex columns
    row = makeDummyRow(liveCell(b, 1), liveCell(b, 2), liveCell(d, 3), liveCell(d, 4));
    assertCellOrder(row.cellsInLegacyOrder(metadata, false), liveCell(b, 1), liveCell(b, 2), liveCell(d, 3), liveCell(d, 4));
    assertCellOrder(row.cellsInLegacyOrder(metadata, true), liveCell(d, 4), liveCell(d, 3), liveCell(b, 2), liveCell(b, 1));
    // Row with mixed simple and complex columns
    row = makeDummyRow(liveCell(a), liveCell(c), liveCell(e), liveCell(b, 1), liveCell(b, 2), liveCell(d, 3), liveCell(d, 4));
    assertCellOrder(row.cellsInLegacyOrder(metadata, false), liveCell(a), liveCell(b, 1), liveCell(b, 2), liveCell(c), liveCell(d, 3), liveCell(d, 4), liveCell(e));
    assertCellOrder(row.cellsInLegacyOrder(metadata, true), liveCell(e), liveCell(d, 4), liveCell(d, 3), liveCell(c), liveCell(b, 2), liveCell(b, 1), liveCell(a));
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) Test(org.junit.Test)

Aggregations

ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)43 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)26 Test (org.junit.Test)15 TableMetadata (org.apache.cassandra.schema.TableMetadata)14 Row (org.apache.cassandra.db.rows.Row)10 ByteBuffer (java.nio.ByteBuffer)9 IndexTarget (org.apache.cassandra.cql3.statements.schema.IndexTarget)8 java.util (java.util)4 ArrayList (java.util.ArrayList)4 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)4 PartitionIterator (org.apache.cassandra.db.partitions.PartitionIterator)4 PartitionUpdate (org.apache.cassandra.db.partitions.PartitionUpdate)4 RowIterator (org.apache.cassandra.db.rows.RowIterator)4 ClientState (org.apache.cassandra.service.ClientState)4 Iterables (com.google.common.collect.Iterables)3 StatementRestrictions (org.apache.cassandra.cql3.restrictions.StatementRestrictions)3 UnfilteredRowIterator (org.apache.cassandra.db.rows.UnfilteredRowIterator)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Iterables.transform (com.google.common.collect.Iterables.transform)2 Lists (com.google.common.collect.Lists)2