use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.
the class StorageService method maybeAddOrUpdateKeyspace.
/**
* Ensure the schema of a pseudo-system keyspace (a distributed system keyspace: traces, auth and the so-called distributedKeyspace),
* is up to date with what we expected (creating it if it doesn't exist and updating tables that may have been upgraded).
*/
private void maybeAddOrUpdateKeyspace(KeyspaceMetadata expected) {
// Note that want to deal with the keyspace and its table a bit differently: for the keyspace definition
// itself, we want to create it if it doesn't exist yet, but if it does exist, we don't want to modify it,
// because user can modify the definition to change the replication factor (#6016) and we don't want to
// override it. For the tables however, we have to deal with the fact that new version can add new columns
// (#8162 being an example), so even if the table definition exists, we still need to force the "current"
// version of the schema, the one the node will be expecting.
KeyspaceMetadata defined = Schema.instance.getKeyspaceMetadata(expected.name);
// If the keyspace doesn't exist, create it
if (defined == null) {
maybeAddKeyspace(expected);
defined = Schema.instance.getKeyspaceMetadata(expected.name);
}
// all the expected tables are present
for (TableMetadata expectedTable : expected.tables) {
TableMetadata definedTable = defined.tables.get(expectedTable.name).orElse(null);
if (definedTable == null || !definedTable.equals(expectedTable))
MigrationManager.forceAnnounceNewTable(expectedTable);
}
}
use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.
the class AlterTypeStatement method announceMigration.
public Event.SchemaChange announceMigration(QueryState queryState, boolean isLocalOnly) throws InvalidRequestException, ConfigurationException {
KeyspaceMetadata ksm = Schema.instance.getKeyspaceMetadata(name.getKeyspace());
if (ksm == null)
throw new InvalidRequestException(String.format("Cannot alter type in unknown keyspace %s", name.getKeyspace()));
UserType toUpdate = ksm.types.get(name.getUserTypeName()).orElseThrow(() -> new InvalidRequestException(String.format("No user type named %s exists.", name)));
UserType updated = makeUpdatedType(toUpdate, ksm);
// Now, we need to announce the type update to basically change it for new tables using this type,
// but we also need to find all existing user types and CF using it and change them.
MigrationManager.announceTypeUpdate(updated, isLocalOnly);
return new Event.SchemaChange(Event.SchemaChange.Change.UPDATED, Event.SchemaChange.Target.TYPE, keyspace(), name.getStringTypeName());
}
use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.
the class DropFunctionStatement method announceMigration.
public Event.SchemaChange announceMigration(QueryState queryState, boolean isLocalOnly) throws RequestValidationException {
Function old = findFunction();
if (old == null) {
if (ifExists)
return null;
else
throw new InvalidRequestException(getMissingFunctionError());
}
KeyspaceMetadata ksm = Schema.instance.getKeyspaceMetadata(old.name().keyspace);
Collection<UDAggregate> referrers = ksm.functions.aggregatesUsingFunction(old);
if (!referrers.isEmpty())
throw new InvalidRequestException(String.format("Function '%s' still referenced by %s", old, referrers));
MigrationManager.announceFunctionDrop((UDFunction) old, isLocalOnly);
return new Event.SchemaChange(Event.SchemaChange.Change.DROPPED, Event.SchemaChange.Target.FUNCTION, old.name().keyspace, old.name().name, AbstractType.asCQLTypeStringList(old.argTypes()));
}
use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.
the class DropTableStatement method announceMigration.
public Event.SchemaChange announceMigration(QueryState queryState, boolean isLocalOnly) throws ConfigurationException {
try {
KeyspaceMetadata ksm = Schema.instance.getKeyspaceMetadata(keyspace());
if (ksm == null)
throw new ConfigurationException(String.format("Cannot drop table in unknown keyspace '%s'", keyspace()));
TableMetadata metadata = ksm.getTableOrViewNullable(columnFamily());
if (metadata != null) {
if (metadata.isView())
throw new InvalidRequestException("Cannot use DROP TABLE on Materialized View");
boolean rejectDrop = false;
StringBuilder messageBuilder = new StringBuilder();
for (ViewMetadata def : ksm.views) {
if (def.baseTableId.equals(metadata.id)) {
if (rejectDrop)
messageBuilder.append(',');
rejectDrop = true;
messageBuilder.append(def.name);
}
}
if (rejectDrop) {
throw new InvalidRequestException(String.format("Cannot drop table when materialized views still depend on it (%s.{%s})", keyspace(), messageBuilder.toString()));
}
}
MigrationManager.announceTableDrop(keyspace(), columnFamily(), isLocalOnly);
return new Event.SchemaChange(Event.SchemaChange.Change.DROPPED, Event.SchemaChange.Target.TABLE, keyspace(), columnFamily());
} catch (ConfigurationException e) {
if (ifExists)
return null;
throw e;
}
}
use of org.apache.cassandra.schema.KeyspaceMetadata in project cassandra by apache.
the class CQLSSTableWriter method getUDType.
/**
* Returns the User Defined type, used in this SSTable Writer, that can
* be used to create UDTValue instances.
*
* @param dataType name of the User Defined type
* @return user defined type
*/
public com.datastax.driver.core.UserType getUDType(String dataType) {
KeyspaceMetadata ksm = Schema.instance.getKeyspaceMetadata(insert.keyspace());
UserType userType = ksm.types.getNullable(ByteBufferUtil.bytes(dataType));
return (com.datastax.driver.core.UserType) UDHelper.driverType(userType);
}
Aggregations