use of org.apache.ignite.lang.TableAlreadyExistsException in project ignite-3 by apache.
the class DdlCommandHandler method handleCreateTable.
/**
* Handles create table command.
*/
private void handleCreateTable(CreateTableCommand cmd) {
final PrimaryKeyDefinitionBuilder pkeyDef = SchemaBuilders.primaryKey();
pkeyDef.withColumns(IgniteObjectName.quoteNames(cmd.primaryKeyColumns()));
pkeyDef.withColocationColumns(IgniteObjectName.quoteNames(cmd.affColumns()));
final IgniteTypeFactory typeFactory = Commons.typeFactory();
final List<org.apache.ignite.schema.definition.ColumnDefinition> colsInner = new ArrayList<>();
for (ColumnDefinition col : cmd.columns()) {
ColumnDefinitionBuilder col0 = SchemaBuilders.column(IgniteObjectName.quote(col.name()), typeFactory.columnType(col.type())).asNullable(col.nullable()).withDefaultValueExpression(col.defaultValue());
colsInner.add(col0.build());
}
Consumer<TableChange> tblChanger = tblCh -> {
TableChange conv = convert(SchemaBuilders.tableBuilder(IgniteObjectName.quote(cmd.schemaName()), IgniteObjectName.quote(cmd.tableName())).columns(colsInner).withPrimaryKey(pkeyDef.build()).build(), tblCh);
if (cmd.partitions() != null) {
conv.changePartitions(cmd.partitions());
}
if (cmd.replicas() != null) {
conv.changeReplicas(cmd.replicas());
}
};
String fullName = TableDefinitionImpl.canonicalName(IgniteObjectName.quote(cmd.schemaName()), IgniteObjectName.quote(cmd.tableName()));
try {
tableManager.createTable(fullName, tblChanger);
} catch (TableAlreadyExistsException ex) {
if (!cmd.ifTableExists()) {
throw ex;
}
}
}
use of org.apache.ignite.lang.TableAlreadyExistsException in project ignite-3 by apache.
the class TableManager method createTableAsyncInternal.
/**
* Internal method that creates a new table with the given {@code name} asynchronously. If a table with the same name already exists,
* a future will be completed with {@link TableAlreadyExistsException}.
*
* @param name Table name.
* @param tableInitChange Table changer.
* @return Future representing pending completion of the operation.
* @throws IgniteException If an unspecified platform exception has happened internally. Is thrown when:
* <ul>
* <li>the node is stopping.</li>
* </ul>
* @see TableAlreadyExistsException
*/
private CompletableFuture<Table> createTableAsyncInternal(String name, Consumer<TableChange> tableInitChange) {
CompletableFuture<Table> tblFut = new CompletableFuture<>();
tableAsyncInternal(name).thenAccept(tbl -> {
if (tbl != null) {
tblFut.completeExceptionally(new TableAlreadyExistsException(name));
} else {
tablesCfg.tables().change(change -> {
if (change.get(name) != null) {
throw new TableAlreadyExistsException(name);
}
change.create(name, (ch) -> {
tableInitChange.accept(ch);
var extConfCh = ((ExtendedTableChange) ch);
tableCreateFuts.put(extConfCh.id(), tblFut);
// Affinity assignments calculation.
extConfCh.changeAssignments(ByteUtils.toBytes(AffinityUtils.calculateAssignments(baselineMgr.nodes(), ch.partitions(), ch.replicas()))).changeSchemas(schemasCh -> schemasCh.create(String.valueOf(INITIAL_SCHEMA_VERSION), schemaCh -> {
SchemaDescriptor schemaDesc;
// prepareSchemaDescriptor() method.
try {
schemaDesc = SchemaUtils.prepareSchemaDescriptor(((ExtendedTableView) ch).schemas().size(), ch);
} catch (IllegalArgumentException ex) {
throw new ConfigurationValidationException(ex.getMessage());
}
schemaCh.changeSchema(SchemaSerializerImpl.INSTANCE.serialize(schemaDesc));
}));
});
}).exceptionally(t -> {
Throwable ex = getRootCause(t);
if (ex instanceof TableAlreadyExistsException) {
tblFut.completeExceptionally(ex);
} else {
LOG.error(IgniteStringFormatter.format("Table wasn't created [name={}]", name), ex);
tblFut.completeExceptionally(ex);
tableCreateFuts.values().removeIf(fut -> fut == tblFut);
}
return null;
});
}
});
return tblFut;
}
Aggregations