use of org.apache.cassandra.schema.TableMetadata in project cassandra by apache.
the class NativeSSTableLoaderClient method init.
public void init(String keyspace) {
Cluster.Builder builder = Cluster.builder().addContactPoints(hosts).withPort(port);
if (sslOptions != null)
builder.withSSL(sslOptions);
if (authProvider != null)
builder = builder.withAuthProvider(authProvider);
try (Cluster cluster = builder.build();
Session session = cluster.connect()) {
Metadata metadata = cluster.getMetadata();
Set<TokenRange> tokenRanges = metadata.getTokenRanges();
IPartitioner partitioner = FBUtilities.newPartitioner(metadata.getPartitioner());
TokenFactory tokenFactory = partitioner.getTokenFactory();
for (TokenRange tokenRange : tokenRanges) {
Set<Host> endpoints = metadata.getReplicas(Metadata.quote(keyspace), tokenRange);
Range<Token> range = new Range<>(tokenFactory.fromString(tokenRange.getStart().getValue().toString()), tokenFactory.fromString(tokenRange.getEnd().getValue().toString()));
for (Host endpoint : endpoints) addRangeForEndpoint(range, endpoint.getAddress());
}
Types types = fetchTypes(keyspace, session);
tables.putAll(fetchTables(keyspace, session, partitioner, types));
// We only need the TableMetadata for the views, so we only load that.
tables.putAll(fetchViews(keyspace, session, partitioner, types));
}
}
use of org.apache.cassandra.schema.TableMetadata in project cassandra by apache.
the class ViewSchemaTest method testAccessAndSchema.
@Test
public void testAccessAndSchema() throws Throwable {
createTable("CREATE TABLE %s (" + "k int, " + "asciival ascii, " + "bigintval bigint, " + "PRIMARY KEY((k, asciival)))");
execute("USE " + keyspace());
executeNet(protocolVersion, "USE " + keyspace());
createView("mv1_test", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE bigintval IS NOT NULL AND k IS NOT NULL AND asciival IS NOT NULL PRIMARY KEY (bigintval, k, asciival)");
updateView("INSERT INTO %s(k,asciival,bigintval)VALUES(?,?,?)", 0, "foo", 1L);
try {
updateView("INSERT INTO mv1_test(k,asciival,bigintval) VALUES(?,?,?)", 1, "foo", 2L);
Assert.fail("Shouldn't be able to modify a MV directly");
} catch (Exception e) {
}
try {
executeNet(protocolVersion, "ALTER TABLE mv1_test ADD foo text");
Assert.fail("Should not be able to use alter table with MV");
} catch (Exception e) {
}
try {
executeNet(protocolVersion, "ALTER TABLE mv1_test WITH compaction = { 'class' : 'LeveledCompactionStrategy' }");
Assert.fail("Should not be able to use alter table with MV");
} catch (Exception e) {
}
executeNet(protocolVersion, "ALTER MATERIALIZED VIEW mv1_test WITH compaction = { 'class' : 'LeveledCompactionStrategy' }");
//Test alter add
executeNet(protocolVersion, "ALTER TABLE %s ADD foo text");
TableMetadata metadata = Schema.instance.getTableMetadata(keyspace(), "mv1_test");
Assert.assertNotNull(metadata.getColumn(ByteBufferUtil.bytes("foo")));
updateView("INSERT INTO %s(k,asciival,bigintval,foo)VALUES(?,?,?,?)", 0, "foo", 1L, "bar");
assertRows(execute("SELECT foo from %s"), row("bar"));
//Test alter rename
executeNet(protocolVersion, "ALTER TABLE %s RENAME asciival TO bar");
assertRows(execute("SELECT bar from %s"), row("foo"));
metadata = Schema.instance.getTableMetadata(keyspace(), "mv1_test");
Assert.assertNotNull(metadata.getColumn(ByteBufferUtil.bytes("bar")));
}
use of org.apache.cassandra.schema.TableMetadata in project cassandra by apache.
the class ClusteringColumnRestrictionsTest method testBoundsAsClusteringWithSingleEqAndMultiEqRestrictions.
/**
* Test mixing single and multi equals restrictions (e.g. clustering_0 = 1 AND (clustering_1, clustering_2) = (2, 3))
*/
@Test
public void testBoundsAsClusteringWithSingleEqAndMultiEqRestrictions() {
TableMetadata tableMetadata = newTableMetadata(Sort.ASC, Sort.ASC, Sort.ASC, Sort.ASC);
ByteBuffer value1 = ByteBufferUtil.bytes(1);
ByteBuffer value2 = ByteBufferUtil.bytes(2);
ByteBuffer value3 = ByteBufferUtil.bytes(3);
ByteBuffer value4 = ByteBufferUtil.bytes(4);
// clustering_0 = 1 AND (clustering_1, clustering_2) = (2, 3)
Restriction singleEq = newSingleEq(tableMetadata, 0, value1);
Restriction multiEq = newMultiEq(tableMetadata, 1, value2, value3);
ClusteringColumnRestrictions restrictions = new ClusteringColumnRestrictions(tableMetadata);
restrictions = restrictions.mergeWith(singleEq).mergeWith(multiEq);
SortedSet<ClusteringBound> bounds = restrictions.boundsAsClustering(Bound.START, QueryOptions.DEFAULT);
assertEquals(1, bounds.size());
assertStartBound(get(bounds, 0), true, value1, value2, value3);
bounds = restrictions.boundsAsClustering(Bound.END, QueryOptions.DEFAULT);
assertEquals(1, bounds.size());
assertEndBound(get(bounds, 0), true, value1, value2, value3);
// clustering_0 = 1 AND clustering_1 = 2 AND (clustering_2, clustering_3) = (3, 4)
singleEq = newSingleEq(tableMetadata, 0, value1);
Restriction singleEq2 = newSingleEq(tableMetadata, 1, value2);
multiEq = newMultiEq(tableMetadata, 2, value3, value4);
restrictions = new ClusteringColumnRestrictions(tableMetadata);
restrictions = restrictions.mergeWith(singleEq).mergeWith(singleEq2).mergeWith(multiEq);
bounds = restrictions.boundsAsClustering(Bound.START, QueryOptions.DEFAULT);
assertEquals(1, bounds.size());
assertStartBound(get(bounds, 0), true, value1, value2, value3, value4);
bounds = restrictions.boundsAsClustering(Bound.END, QueryOptions.DEFAULT);
assertEquals(1, bounds.size());
assertEndBound(get(bounds, 0), true, value1, value2, value3, value4);
// (clustering_0, clustering_1) = (1, 2) AND clustering_2 = 3
singleEq = newSingleEq(tableMetadata, 2, value3);
multiEq = newMultiEq(tableMetadata, 0, value1, value2);
restrictions = new ClusteringColumnRestrictions(tableMetadata);
restrictions = restrictions.mergeWith(singleEq).mergeWith(multiEq);
bounds = restrictions.boundsAsClustering(Bound.START, QueryOptions.DEFAULT);
assertEquals(1, bounds.size());
assertStartBound(get(bounds, 0), true, value1, value2, value3);
bounds = restrictions.boundsAsClustering(Bound.END, QueryOptions.DEFAULT);
assertEquals(1, bounds.size());
assertEndBound(get(bounds, 0), true, value1, value2, value3);
// clustering_0 = 1 AND (clustering_1, clustering_2) = (2, 3) AND clustering_3 = 4
singleEq = newSingleEq(tableMetadata, 0, value1);
singleEq2 = newSingleEq(tableMetadata, 3, value4);
multiEq = newMultiEq(tableMetadata, 1, value2, value3);
restrictions = new ClusteringColumnRestrictions(tableMetadata);
restrictions = restrictions.mergeWith(singleEq).mergeWith(multiEq).mergeWith(singleEq2);
bounds = restrictions.boundsAsClustering(Bound.START, QueryOptions.DEFAULT);
assertEquals(1, bounds.size());
assertStartBound(get(bounds, 0), true, value1, value2, value3, value4);
bounds = restrictions.boundsAsClustering(Bound.END, QueryOptions.DEFAULT);
assertEquals(1, bounds.size());
assertEndBound(get(bounds, 0), true, value1, value2, value3, value4);
}
use of org.apache.cassandra.schema.TableMetadata in project cassandra by apache.
the class ClusteringColumnRestrictionsTest method testBoundsAsClusteringWithMultiSliceRestrictionsWithAscendingDescendingColumnMix.
/**
* Test multi-column slice restrictions with ascending, descending, ascending and descending columns
* (e.g '(clustering_0, clustering1, clustering_3, clustering4) > (1, 2, 3, 4)')
*/
@Test
public void testBoundsAsClusteringWithMultiSliceRestrictionsWithAscendingDescendingColumnMix() {
TableMetadata tableMetadata = newTableMetadata(Sort.ASC, Sort.DESC, Sort.ASC, Sort.DESC);
ByteBuffer value1 = ByteBufferUtil.bytes(1);
ByteBuffer value2 = ByteBufferUtil.bytes(2);
ByteBuffer value3 = ByteBufferUtil.bytes(3);
ByteBuffer value4 = ByteBufferUtil.bytes(4);
// (clustering_0, clustering1, clustering_2, clustering_3) > (1, 2, 3, 4)
Restriction slice = newMultiSlice(tableMetadata, 0, Bound.START, false, value1, value2, value3, value4);
ClusteringColumnRestrictions restrictions = new ClusteringColumnRestrictions(tableMetadata);
restrictions = restrictions.mergeWith(slice);
SortedSet<ClusteringBound> bounds = restrictions.boundsAsClustering(Bound.START, QueryOptions.DEFAULT);
assertEquals(4, bounds.size());
assertStartBound(get(bounds, 0), true, value1);
assertStartBound(get(bounds, 1), true, value1, value2, value3);
assertStartBound(get(bounds, 2), false, value1, value2, value3);
assertStartBound(get(bounds, 3), false, value1);
bounds = restrictions.boundsAsClustering(Bound.END, QueryOptions.DEFAULT);
assertEquals(4, bounds.size());
assertEndBound(get(bounds, 0), false, value1, value2);
assertEndBound(get(bounds, 1), false, value1, value2, value3, value4);
assertEndBound(get(bounds, 2), true, value1, value2);
assertEmptyEnd(get(bounds, 3));
// clustering_0 = 1 AND (clustering_1, clustering_2, clustering_3) > (2, 3, 4)
Restriction eq = newSingleEq(tableMetadata, 0, value1);
slice = newMultiSlice(tableMetadata, 1, Bound.START, false, value2, value3, value4);
restrictions = new ClusteringColumnRestrictions(tableMetadata);
restrictions = restrictions.mergeWith(slice);
restrictions = restrictions.mergeWith(eq);
bounds = restrictions.boundsAsClustering(Bound.START, QueryOptions.DEFAULT);
assertEquals(3, bounds.size());
assertStartBound(get(bounds, 0), true, value1);
assertStartBound(get(bounds, 1), true, value1, value2, value3);
assertStartBound(get(bounds, 2), false, value1, value2, value3);
bounds = restrictions.boundsAsClustering(Bound.END, QueryOptions.DEFAULT);
assertEquals(3, bounds.size());
assertEndBound(get(bounds, 0), false, value1, value2);
assertEndBound(get(bounds, 1), false, value1, value2, value3, value4);
assertEndBound(get(bounds, 2), true, value1, value2);
// (clustering_0, clustering1) >= (1, 2)
slice = newMultiSlice(tableMetadata, 0, Bound.START, true, value1, value2);
restrictions = new ClusteringColumnRestrictions(tableMetadata);
restrictions = restrictions.mergeWith(slice);
bounds = restrictions.boundsAsClustering(Bound.START, QueryOptions.DEFAULT);
assertEquals(2, bounds.size());
assertStartBound(get(bounds, 0), true, value1);
assertStartBound(get(bounds, 1), false, value1);
bounds = restrictions.boundsAsClustering(Bound.END, QueryOptions.DEFAULT);
assertEquals(2, bounds.size());
assertEndBound(get(bounds, 0), true, value1, value2);
assertEmptyEnd(get(bounds, 1));
// (clustering_0, clustering1, clustering_2, clustering_3) >= (1, 2, 3, 4)
slice = newMultiSlice(tableMetadata, 0, Bound.START, true, value1, value2, value3, value4);
restrictions = new ClusteringColumnRestrictions(tableMetadata);
restrictions = restrictions.mergeWith(slice);
bounds = restrictions.boundsAsClustering(Bound.START, QueryOptions.DEFAULT);
assertEquals(4, bounds.size());
assertStartBound(get(bounds, 0), true, value1);
assertStartBound(get(bounds, 1), true, value1, value2, value3);
assertStartBound(get(bounds, 2), false, value1, value2, value3);
assertStartBound(get(bounds, 3), false, value1);
bounds = restrictions.boundsAsClustering(Bound.END, QueryOptions.DEFAULT);
assertEquals(4, bounds.size());
assertEndBound(get(bounds, 0), false, value1, value2);
assertEndBound(get(bounds, 1), true, value1, value2, value3, value4);
assertEndBound(get(bounds, 2), true, value1, value2);
assertEmptyEnd(get(bounds, 3));
// (clustering_0, clustering1, clustering_2, clustering_3) <= (1, 2, 3, 4)
slice = newMultiSlice(tableMetadata, 0, Bound.END, true, value1, value2, value3, value4);
restrictions = new ClusteringColumnRestrictions(tableMetadata);
restrictions = restrictions.mergeWith(slice);
bounds = restrictions.boundsAsClustering(Bound.START, QueryOptions.DEFAULT);
assertEquals(4, bounds.size());
assertEmptyStart(get(bounds, 0));
assertStartBound(get(bounds, 1), true, value1, value2);
assertStartBound(get(bounds, 2), true, value1, value2, value3, value4);
assertStartBound(get(bounds, 3), false, value1, value2);
bounds = restrictions.boundsAsClustering(Bound.END, QueryOptions.DEFAULT);
assertEquals(4, bounds.size());
assertEndBound(get(bounds, 0), false, value1);
assertEndBound(get(bounds, 1), false, value1, value2, value3);
assertEndBound(get(bounds, 2), true, value1, value2, value3);
assertEndBound(get(bounds, 3), true, value1);
// (clustering_0, clustering1, clustering_2, clustering_3) < (1, 2, 3, 4)
slice = newMultiSlice(tableMetadata, 0, Bound.END, false, value1, value2, value3, value4);
restrictions = new ClusteringColumnRestrictions(tableMetadata);
restrictions = restrictions.mergeWith(slice);
bounds = restrictions.boundsAsClustering(Bound.START, QueryOptions.DEFAULT);
assertEquals(4, bounds.size());
assertEmptyStart(get(bounds, 0));
assertStartBound(get(bounds, 1), true, value1, value2);
assertStartBound(get(bounds, 2), false, value1, value2, value3, value4);
assertStartBound(get(bounds, 3), false, value1, value2);
bounds = restrictions.boundsAsClustering(Bound.END, QueryOptions.DEFAULT);
assertEquals(4, bounds.size());
assertEndBound(get(bounds, 0), false, value1);
assertEndBound(get(bounds, 1), false, value1, value2, value3);
assertEndBound(get(bounds, 2), true, value1, value2, value3);
assertEndBound(get(bounds, 3), true, value1);
// (clustering_0, clustering1, clustering_2, clustering_3) > (1, 2, 3, 4) AND (clustering_0, clustering_1) < (2, 3)
slice = newMultiSlice(tableMetadata, 0, Bound.START, false, value1, value2, value3, value4);
Restriction slice2 = newMultiSlice(tableMetadata, 0, Bound.END, false, value2, value3);
restrictions = new ClusteringColumnRestrictions(tableMetadata);
restrictions = restrictions.mergeWith(slice).mergeWith(slice2);
bounds = restrictions.boundsAsClustering(Bound.START, QueryOptions.DEFAULT);
assertEquals(5, bounds.size());
assertStartBound(get(bounds, 0), true, value1);
assertStartBound(get(bounds, 1), true, value1, value2, value3);
assertStartBound(get(bounds, 2), false, value1, value2, value3);
assertStartBound(get(bounds, 3), false, value1);
assertStartBound(get(bounds, 4), false, value2, value3);
bounds = restrictions.boundsAsClustering(Bound.END, QueryOptions.DEFAULT);
assertEquals(5, bounds.size());
assertEndBound(get(bounds, 0), false, value1, value2);
assertEndBound(get(bounds, 1), false, value1, value2, value3, value4);
assertEndBound(get(bounds, 2), true, value1, value2);
assertEndBound(get(bounds, 3), false, value2);
assertEndBound(get(bounds, 4), true, value2);
// (clustering_0, clustering1, clustering_2, clustering_3) >= (1, 2, 3, 4) AND (clustering_0, clustering1, clustering_2, clustering_3) <= (4, 3, 2, 1)
slice = newMultiSlice(tableMetadata, 0, Bound.START, true, value1, value2, value3, value4);
slice2 = newMultiSlice(tableMetadata, 0, Bound.END, true, value4, value3, value2, value1);
restrictions = new ClusteringColumnRestrictions(tableMetadata);
restrictions = restrictions.mergeWith(slice).mergeWith(slice2);
bounds = restrictions.boundsAsClustering(Bound.START, QueryOptions.DEFAULT);
assertEquals(7, bounds.size());
assertStartBound(get(bounds, 0), true, value1);
assertStartBound(get(bounds, 1), true, value1, value2, value3);
assertStartBound(get(bounds, 2), false, value1, value2, value3);
assertStartBound(get(bounds, 3), false, value1);
assertStartBound(get(bounds, 4), true, value4, value3);
assertStartBound(get(bounds, 5), true, value4, value3, value2, value1);
assertStartBound(get(bounds, 6), false, value4, value3);
bounds = restrictions.boundsAsClustering(Bound.END, QueryOptions.DEFAULT);
assertEquals(7, bounds.size());
assertEndBound(get(bounds, 0), false, value1, value2);
assertEndBound(get(bounds, 1), true, value1, value2, value3, value4);
assertEndBound(get(bounds, 2), true, value1, value2);
assertEndBound(get(bounds, 3), false, value4);
assertEndBound(get(bounds, 4), false, value4, value3, value2);
assertEndBound(get(bounds, 5), true, value4, value3, value2);
assertEndBound(get(bounds, 6), true, value4);
}
use of org.apache.cassandra.schema.TableMetadata in project cassandra by apache.
the class ClusteringColumnRestrictionsTest method testBoundsAsClusteringWithOneInRestrictionsAndOneClusteringColumn.
/**
* Test 'clustering_0 IN (1, 2, 3)' with only one clustering column
*/
@Test
public void testBoundsAsClusteringWithOneInRestrictionsAndOneClusteringColumn() {
ByteBuffer value1 = ByteBufferUtil.bytes(1);
ByteBuffer value2 = ByteBufferUtil.bytes(2);
ByteBuffer value3 = ByteBufferUtil.bytes(3);
TableMetadata tableMetadata = newTableMetadata(Sort.ASC, Sort.ASC);
Restriction in = newSingleIN(tableMetadata, 0, value1, value2, value3);
ClusteringColumnRestrictions restrictions = new ClusteringColumnRestrictions(tableMetadata);
restrictions = restrictions.mergeWith(in);
SortedSet<ClusteringBound> bounds = restrictions.boundsAsClustering(Bound.START, QueryOptions.DEFAULT);
assertEquals(3, bounds.size());
assertStartBound(get(bounds, 0), true, value1);
assertStartBound(get(bounds, 1), true, value2);
assertStartBound(get(bounds, 2), true, value3);
bounds = restrictions.boundsAsClustering(Bound.END, QueryOptions.DEFAULT);
assertEquals(3, bounds.size());
assertEndBound(get(bounds, 0), true, value1);
assertEndBound(get(bounds, 1), true, value2);
assertEndBound(get(bounds, 2), true, value3);
}
Aggregations