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);
}
}
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();
}
}
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());
}
}
}
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());
}
}
}
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));
}
Aggregations