Search in sources :

Example 1 with ColumnView

use of org.apache.ignite.configuration.schemas.table.ColumnView in project ignite-3 by apache.

the class SortedIndexDescriptor method createSchemaDescriptor.

/**
 * Creates a {@link SchemaDescriptor} from a list of index key columns.
 */
private static SchemaDescriptor createSchemaDescriptor(List<ColumnView> indexKeyColumnViews) {
    Column[] keyColumns = new Column[indexKeyColumnViews.size()];
    for (int i = 0; i < indexKeyColumnViews.size(); ++i) {
        ColumnView columnView = indexKeyColumnViews.get(i);
        ColumnDefinition columnDefinition = SchemaConfigurationConverter.convert(columnView);
        keyColumns[i] = SchemaDescriptorConverter.convert(i, columnDefinition);
    }
    return new SchemaDescriptor(0, keyColumns, new Column[0]);
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) ColumnView(org.apache.ignite.configuration.schemas.table.ColumnView) IndexColumnView(org.apache.ignite.configuration.schemas.table.IndexColumnView) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition)

Example 2 with ColumnView

use of org.apache.ignite.configuration.schemas.table.ColumnView in project ignite-3 by apache.

the class SchemaConfigurationConverter method convert.

/**
 * Convert table configuration view to table schema.
 *
 * @param tblView TableView to convert.
 * @return Table schema.
 */
public static TableDefinitionImpl convert(TableView tblView) {
    String canonicalName = tblView.name();
    int sepPos = canonicalName.indexOf('.');
    String schemaName = canonicalName.substring(0, sepPos);
    String tableName = canonicalName.substring(sepPos + 1);
    NamedListView<? extends ColumnView> colsView = tblView.columns();
    var columns = new LinkedHashMap<String, ColumnDefinition>(capacity(colsView.size()));
    for (String key : colsView.namedListKeys()) {
        ColumnView colView = colsView.get(key);
        if (colView != null) {
            ColumnDefinition definition = convert(colView);
            columns.put(definition.name(), definition);
        }
    }
    NamedListView<? extends TableIndexView> idxsView = tblView.indices();
    var indices = new HashMap<String, IndexDefinition>(capacity(idxsView.size()));
    for (String key : idxsView.namedListKeys()) {
        IndexDefinition definition = convert(idxsView.get(key));
        indices.put(definition.name(), definition);
    }
    PrimaryKeyDefinition primaryKey = convert(tblView.primaryKey());
    return new TableDefinitionImpl(schemaName, tableName, columns, primaryKey, indices);
}
Also used : TableDefinitionImpl(org.apache.ignite.internal.schema.definition.TableDefinitionImpl) HashIndexDefinition(org.apache.ignite.schema.definition.index.HashIndexDefinition) SortedIndexDefinition(org.apache.ignite.schema.definition.index.SortedIndexDefinition) IndexDefinition(org.apache.ignite.schema.definition.index.IndexDefinition) PartialIndexDefinition(org.apache.ignite.schema.definition.index.PartialIndexDefinition) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PrimaryKeyDefinition(org.apache.ignite.schema.definition.PrimaryKeyDefinition) ColumnView(org.apache.ignite.configuration.schemas.table.ColumnView) IndexColumnView(org.apache.ignite.configuration.schemas.table.IndexColumnView) LinkedHashMap(java.util.LinkedHashMap) IndexColumnDefinition(org.apache.ignite.schema.definition.index.IndexColumnDefinition) SortedIndexColumnDefinition(org.apache.ignite.schema.definition.index.SortedIndexColumnDefinition) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition)

Example 3 with ColumnView

use of org.apache.ignite.configuration.schemas.table.ColumnView in project ignite-3 by apache.

the class SchemaUtils method columnMapper.

/**
 * Prepares column mapper.
 *
 * @param oldDesc Old schema descriptor.
 * @param oldTbl  Old table configuration.
 * @param newDesc New schema descriptor.
 * @param newTbl  New table configuration.
 * @return Column mapper.
 */
public static ColumnMapper columnMapper(SchemaDescriptor oldDesc, TableView oldTbl, SchemaDescriptor newDesc, TableChange newTbl) {
    ColumnMapper mapper = null;
    NamedListView<? extends ColumnView> newTblColumns = newTbl.columns();
    NamedListView<? extends ColumnView> oldTblColumns = oldTbl.columns();
    // because removed keys are simply replaced with nulls
    assert newTblColumns.size() >= oldTblColumns.size();
    for (int i = 0; i < newTblColumns.size(); ++i) {
        ColumnView newColView = newTblColumns.get(i);
        // new value can be null if a column has been deleted
        if (newColView == null) {
            continue;
        }
        if (i < oldTblColumns.size()) {
            ColumnView oldColView = oldTblColumns.get(i);
            Column newCol = newDesc.column(newColView.name());
            Column oldCol = oldDesc.column(oldColView.name());
            if (newCol.schemaIndex() == oldCol.schemaIndex()) {
                continue;
            }
            if (mapper == null) {
                mapper = ColumnMapping.createMapper(newDesc);
            }
            mapper.add(newCol.schemaIndex(), oldCol.schemaIndex());
        } else {
            // if the new Named List is larger than the old one, it can only mean that a new column has been added
            Column newCol = newDesc.column(newColView.name());
            assert !newDesc.isKeyColumn(newCol.schemaIndex());
            if (mapper == null) {
                mapper = ColumnMapping.createMapper(newDesc);
            }
            mapper.add(newCol);
        }
    }
    // since newTblColumns comes from a TableChange, it will contain nulls for removed columns
    Optional<Column> droppedKeyCol = newTblColumns.namedListKeys().stream().filter(k -> newTblColumns.get(k) == null).map(oldDesc::column).filter(c -> oldDesc.isKeyColumn(c.schemaIndex())).findAny();
    // TODO: configuration validators.
    assert droppedKeyCol.isEmpty() : IgniteStringFormatter.format("Dropping of key column is forbidden: [schemaVer={}, col={}]", newDesc.version(), droppedKeyCol.get());
    return mapper == null ? ColumnMapping.identityMapping() : mapper;
}
Also used : ColumnView(org.apache.ignite.configuration.schemas.table.ColumnView) SchemaDescriptorConverter(org.apache.ignite.internal.schema.configuration.SchemaDescriptorConverter) TableDefinition(org.apache.ignite.schema.definition.TableDefinition) SchemaConfigurationConverter(org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter) NamedListView(org.apache.ignite.configuration.NamedListView) Optional(java.util.Optional) ColumnMapper(org.apache.ignite.internal.schema.mapping.ColumnMapper) IgniteStringFormatter(org.apache.ignite.lang.IgniteStringFormatter) TableChange(org.apache.ignite.configuration.schemas.table.TableChange) ColumnMapping(org.apache.ignite.internal.schema.mapping.ColumnMapping) TableView(org.apache.ignite.configuration.schemas.table.TableView) ColumnView(org.apache.ignite.configuration.schemas.table.ColumnView) ColumnMapper(org.apache.ignite.internal.schema.mapping.ColumnMapper)

Example 4 with ColumnView

use of org.apache.ignite.configuration.schemas.table.ColumnView in project ignite-3 by apache.

the class TableValidatorImpl method validateNamedListKeys.

/**
 * Checks that columns and indices are stored under their names in the corresponding Named Lists, i.e. their Named List keys and names
 * are the same.
 *
 * @param table Table configuration view.
 * @param ctx Validation context.
 */
private static void validateNamedListKeys(TableView table, ValidationContext<NamedListView<TableView>> ctx) {
    NamedListView<? extends ColumnView> columns = table.columns();
    for (String key : columns.namedListKeys()) {
        ColumnView column = columns.get(key);
        if (!column.name().equals(key)) {
            var issue = new ValidationIssue(String.format("Column name \"%s\" does not match its Named List key: \"%s\"", column.name(), key));
            ctx.addIssue(issue);
        }
    }
    NamedListView<? extends TableIndexView> indices = table.indices();
    for (String key : indices.namedListKeys()) {
        TableIndexView index = indices.get(key);
        if (!index.name().equals(key)) {
            var issue = new ValidationIssue(String.format("Index name \"%s\" does not match its Named List key: \"%s\"", index.name(), key));
            ctx.addIssue(issue);
        }
    }
}
Also used : ColumnView(org.apache.ignite.configuration.schemas.table.ColumnView) TableIndexView(org.apache.ignite.configuration.schemas.table.TableIndexView) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue)

Aggregations

ColumnView (org.apache.ignite.configuration.schemas.table.ColumnView)4 IndexColumnView (org.apache.ignite.configuration.schemas.table.IndexColumnView)2 ColumnDefinition (org.apache.ignite.schema.definition.ColumnDefinition)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Optional (java.util.Optional)1 NamedListView (org.apache.ignite.configuration.NamedListView)1 TableChange (org.apache.ignite.configuration.schemas.table.TableChange)1 TableIndexView (org.apache.ignite.configuration.schemas.table.TableIndexView)1 TableView (org.apache.ignite.configuration.schemas.table.TableView)1 ValidationIssue (org.apache.ignite.configuration.validation.ValidationIssue)1 Column (org.apache.ignite.internal.schema.Column)1 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)1 SchemaConfigurationConverter (org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter)1 SchemaDescriptorConverter (org.apache.ignite.internal.schema.configuration.SchemaDescriptorConverter)1 TableDefinitionImpl (org.apache.ignite.internal.schema.definition.TableDefinitionImpl)1 ColumnMapper (org.apache.ignite.internal.schema.mapping.ColumnMapper)1 ColumnMapping (org.apache.ignite.internal.schema.mapping.ColumnMapping)1 IgniteStringFormatter (org.apache.ignite.lang.IgniteStringFormatter)1 PrimaryKeyDefinition (org.apache.ignite.schema.definition.PrimaryKeyDefinition)1