Search in sources :

Example 1 with ColumnDefinition

use of org.apache.ignite.schema.definition.ColumnDefinition in project ignite-3 by apache.

the class ItDataSchemaSyncTest method test.

/**
 * The test executes various operation over the lagging node.
 * The operations can be executed only the node overtakes a distributed cluster state.
 */
@Test
public void test() throws Exception {
    Ignite ignite0 = clusterNodes.get(0);
    final IgniteImpl ignite1 = (IgniteImpl) clusterNodes.get(1);
    createTable(ignite0, SCHEMA, SHORT_TABLE_NAME);
    TableImpl table = (TableImpl) ignite0.tables().table(TABLE_NAME);
    assertEquals(1, table.schemaView().schema().version());
    for (int i = 0; i < 10; i++) {
        table.recordView().insert(null, Tuple.create().set("key", (long) i).set("valInt", i).set("valStr", "str_" + i));
    }
    WatchListenerInhibitor listenerInhibitor = WatchListenerInhibitor.metastorageEventsInhibitor(ignite1);
    listenerInhibitor.startInhibit();
    ColumnDefinition columnDefinition = SchemaBuilders.column("valStr2", ColumnType.string()).withDefaultValueExpression("default").build();
    ignite0.tables().alterTable(TABLE_NAME, tblChanger -> tblChanger.changeColumns(cols -> cols.create(columnDefinition.name(), colChg -> convert(columnDefinition, colChg))));
    for (Ignite node : clusterNodes) {
        if (node == ignite1) {
            continue;
        }
        TableImpl tableOnNode = (TableImpl) node.tables().table(TABLE_NAME);
        IgniteTestUtils.waitForCondition(() -> tableOnNode.schemaView().lastSchemaVersion() == 2, 10_000);
    }
    TableImpl table1 = (TableImpl) ignite1.tables().table(TABLE_NAME);
    for (int i = 10; i < 20; i++) {
        table.recordView().insert(null, Tuple.create().set("key", (long) i).set("valInt", i).set("valStr", "str_" + i).set("valStr2", "str2_" + i));
    }
    final CompletableFuture insertFut = IgniteTestUtils.runAsync(() -> table1.recordView().insert(null, Tuple.create().set("key", 0L).set("valInt", 0).set("valStr", "str_" + 0).set("valStr2", "str2_" + 0)));
    final CompletableFuture getFut = IgniteTestUtils.runAsync(() -> {
        table1.recordView().get(null, Tuple.create().set("key", 10L));
    });
    final CompletableFuture checkDefaultFut = IgniteTestUtils.runAsync(() -> {
        assertEquals("default", table1.recordView().get(null, Tuple.create().set("key", 0L)).value("valStr2"));
    });
    assertEquals(1, table1.schemaView().lastSchemaVersion());
    assertFalse(getFut.isDone());
    assertFalse(insertFut.isDone());
    assertFalse(checkDefaultFut.isDone());
    listenerInhibitor.stopInhibit();
    getFut.get(10, TimeUnit.SECONDS);
    insertFut.get(10, TimeUnit.SECONDS);
    checkDefaultFut.get(10, TimeUnit.SECONDS);
    for (Ignite node : clusterNodes) {
        Table tableOnNode = node.tables().table(TABLE_NAME);
        for (int i = 0; i < 20; i++) {
            Tuple row = tableOnNode.recordView().get(null, Tuple.create().set("key", (long) i));
            assertNotNull(row);
            assertEquals(i, row.intValue("valInt"));
            assertEquals("str_" + i, row.value("valStr"));
            assertEquals(i < 10 ? "default" : ("str2_" + i), row.value("valStr2"));
        }
    }
}
Also used : IgniteImpl(org.apache.ignite.internal.app.IgniteImpl) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) SchemaConfigurationConverter.convert(org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter.convert) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) WatchListenerInhibitor(org.apache.ignite.internal.test.WatchListenerInhibitor) Lists(com.google.common.collect.Lists) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) TableImpl(org.apache.ignite.internal.table.TableImpl) Map(java.util.Map) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TableDefinition(org.apache.ignite.schema.definition.TableDefinition) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition) Ignite(org.apache.ignite.Ignite) ColumnType(org.apache.ignite.schema.definition.ColumnType) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) IgnitionManager(org.apache.ignite.IgnitionManager) IgniteTestUtils(org.apache.ignite.internal.testframework.IgniteTestUtils) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest) SchemaBuilders(org.apache.ignite.schema.SchemaBuilders) WorkDirectoryExtension(org.apache.ignite.internal.testframework.WorkDirectoryExtension) Table(org.apache.ignite.table.Table) Tuple(org.apache.ignite.table.Tuple) CompletableFuture(java.util.concurrent.CompletableFuture) Table(org.apache.ignite.table.Table) IgniteImpl(org.apache.ignite.internal.app.IgniteImpl) TableImpl(org.apache.ignite.internal.table.TableImpl) Ignite(org.apache.ignite.Ignite) WatchListenerInhibitor(org.apache.ignite.internal.test.WatchListenerInhibitor) Tuple(org.apache.ignite.table.Tuple) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 2 with ColumnDefinition

use of org.apache.ignite.schema.definition.ColumnDefinition in project ignite-3 by apache.

the class ItTablesApiTest method addColumn.

/**
 * Adds an index.
 *
 * @param node           Cluster node.
 * @param schemaName     Schema name.
 * @param shortTableName Table name.
 */
protected void addColumn(Ignite node, String schemaName, String shortTableName) {
    ColumnDefinition col = SchemaBuilders.column("valStrNew", ColumnType.string()).asNullable(true).withDefaultValueExpression("default").build();
    addColumnInternal(node, schemaName, shortTableName, col);
}
Also used : ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition)

Example 3 with ColumnDefinition

use of org.apache.ignite.schema.definition.ColumnDefinition 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 4 with ColumnDefinition

use of org.apache.ignite.schema.definition.ColumnDefinition 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 5 with ColumnDefinition

use of org.apache.ignite.schema.definition.ColumnDefinition in project ignite-3 by apache.

the class SchemaDescriptorConverter method convert.

/**
 * Build schema descriptor by table schema.
 *
 * @param schemaVer Schema version.
 * @param tblCfg    Table schema.
 * @return SchemaDescriptor.
 */
public static SchemaDescriptor convert(int schemaVer, TableDefinition tblCfg) {
    Set<String> keyColumnsNames = tblCfg.keyColumns();
    List<Column> keyCols = new ArrayList<>(keyColumnsNames.size());
    List<Column> valCols = new ArrayList<>(tblCfg.columns().size() - keyColumnsNames.size());
    int idx = 0;
    for (ColumnDefinition col : tblCfg.columns()) {
        if (keyColumnsNames.contains(col.name())) {
            keyCols.add(convert(idx, col));
        } else {
            valCols.add(convert(idx, col));
        }
        idx++;
    }
    return new SchemaDescriptor(schemaVer, keyCols.toArray(Column[]::new), tblCfg.colocationColumns().toArray(String[]::new), valCols.toArray(Column[]::new));
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) ArrayList(java.util.ArrayList) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition)

Aggregations

ColumnDefinition (org.apache.ignite.schema.definition.ColumnDefinition)12 Test (org.junit.jupiter.api.Test)5 Ignite (org.apache.ignite.Ignite)4 Column (org.apache.ignite.internal.schema.Column)4 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)4 TableDefinition (org.apache.ignite.schema.definition.TableDefinition)4 Tuple (org.apache.ignite.table.Tuple)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)2 ColumnView (org.apache.ignite.configuration.schemas.table.ColumnView)2 IndexColumnView (org.apache.ignite.configuration.schemas.table.IndexColumnView)2 SchemaBuilders (org.apache.ignite.schema.SchemaBuilders)2 ColumnType (org.apache.ignite.schema.definition.ColumnType)2 ColumnDefinitionBuilder (org.apache.ignite.schema.definition.builder.ColumnDefinitionBuilder)2 HashIndexDefinition (org.apache.ignite.schema.definition.index.HashIndexDefinition)2 IndexColumnDefinition (org.apache.ignite.schema.definition.index.IndexColumnDefinition)2 IndexDefinition (org.apache.ignite.schema.definition.index.IndexDefinition)2 PartialIndexDefinition (org.apache.ignite.schema.definition.index.PartialIndexDefinition)2 SortedIndexColumnDefinition (org.apache.ignite.schema.definition.index.SortedIndexColumnDefinition)2 SortedIndexDefinition (org.apache.ignite.schema.definition.index.SortedIndexDefinition)2