Search in sources :

Example 1 with IndexDefinition

use of org.apache.ignite.schema.definition.index.IndexDefinition 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 2 with IndexDefinition

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

the class SchemaConfigurationConverter method convert.

/**
 * Convert table schema to table changer.
 *
 * @param tbl    Table schema to convert.
 * @param tblChg Change to fulfill.
 * @return TableChange to get result from.
 */
public static TableChange convert(TableDefinition tbl, TableChange tblChg) {
    tblChg.changeName(tbl.canonicalName());
    tblChg.changeIndices(idxsChg -> {
        for (IndexDefinition idx : tbl.indices()) {
            idxsChg.create(idx.name(), idxInit -> convert(idx, idxInit));
        }
    });
    tblChg.changeColumns(colsChg -> {
        for (ColumnDefinition col : tbl.columns()) {
            colsChg.create(col.name(), colChg -> convert(col, colChg));
        }
    });
    tblChg.changePrimaryKey(pkCng -> {
        pkCng.changeColumns(tbl.keyColumns().toArray(String[]::new)).changeColocationColumns(tbl.colocationColumns().toArray(String[]::new));
    });
    return tblChg;
}
Also used : 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) 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 IndexDefinition

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

the class ItTablesApiTest method addIndex.

/**
 * Adds a column.
 *
 * @param node           Cluster node.
 * @param schemaName     Schema name.
 * @param shortTableName Table name.
 */
protected void addIndex(Ignite node, String schemaName, String shortTableName) {
    IndexDefinition idx = SchemaBuilders.hashIndex("testHI").withColumns("valInt", "valStr").build();
    node.tables().alterTable(schemaName + "." + shortTableName, chng -> chng.changeIndices(idxes -> {
        if (idxes.get(idx.name()) != null) {
            log.info("Index already exists [naem={}]", idx.name());
            throw new IndexAlreadyExistsException(idx.name());
        }
        idxes.create(idx.name(), tableIndexChange -> convert(idx, tableIndexChange));
    }));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) IntStream(java.util.stream.IntStream) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) IgniteTablesInternal(org.apache.ignite.internal.table.IgniteTablesInternal) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) SchemaConfigurationConverter.convert(org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter.convert) TableAlreadyExistsException(org.apache.ignite.lang.TableAlreadyExistsException) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) WatchListenerInhibitor.metastorageEventsInhibitor(org.apache.ignite.internal.test.WatchListenerInhibitor.metastorageEventsInhibitor) ArrayList(java.util.ArrayList) WatchListenerInhibitor(org.apache.ignite.internal.test.WatchListenerInhibitor) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) TableImpl(org.apache.ignite.internal.table.TableImpl) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TableNotFoundException(org.apache.ignite.lang.TableNotFoundException) IndexDefinition(org.apache.ignite.schema.definition.index.IndexDefinition) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) ColumnType(org.apache.ignite.schema.definition.ColumnType) Collectors(java.util.stream.Collectors) TestInfo(org.junit.jupiter.api.TestInfo) ExecutionException(java.util.concurrent.ExecutionException) 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) ColumnAlreadyExistsException(org.apache.ignite.lang.ColumnAlreadyExistsException) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest) IndexAlreadyExistsException(org.apache.ignite.lang.IndexAlreadyExistsException) SchemaBuilders(org.apache.ignite.schema.SchemaBuilders) Table(org.apache.ignite.table.Table) ItUtils(org.apache.ignite.internal.ItUtils) Tuple(org.apache.ignite.table.Tuple) IndexDefinition(org.apache.ignite.schema.definition.index.IndexDefinition) IndexAlreadyExistsException(org.apache.ignite.lang.IndexAlreadyExistsException)

Example 4 with IndexDefinition

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

the class ItTablesApiTest method addIndexIfNotExists.

/**
 * Creates a table if it does not exist.
 *
 * @param node           Cluster node.
 * @param schemaName     Schema name.
 * @param shortTableName Table name.
 */
protected void addIndexIfNotExists(Ignite node, String schemaName, String shortTableName) {
    IndexDefinition idx = SchemaBuilders.hashIndex("testHI").withColumns("valInt", "valStr").build();
    node.tables().alterTable(schemaName + "." + shortTableName, chng -> chng.changeIndices(idxes -> {
        if (idxes.get(idx.name()) == null) {
            idxes.create(idx.name(), tableIndexChange -> convert(idx, tableIndexChange));
        } else {
            log.info("Index already exists [naem={}]", idx.name());
        }
    }));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) IntStream(java.util.stream.IntStream) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) IgniteTablesInternal(org.apache.ignite.internal.table.IgniteTablesInternal) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) SchemaConfigurationConverter.convert(org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter.convert) TableAlreadyExistsException(org.apache.ignite.lang.TableAlreadyExistsException) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) WatchListenerInhibitor.metastorageEventsInhibitor(org.apache.ignite.internal.test.WatchListenerInhibitor.metastorageEventsInhibitor) ArrayList(java.util.ArrayList) WatchListenerInhibitor(org.apache.ignite.internal.test.WatchListenerInhibitor) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) TableImpl(org.apache.ignite.internal.table.TableImpl) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TableNotFoundException(org.apache.ignite.lang.TableNotFoundException) IndexDefinition(org.apache.ignite.schema.definition.index.IndexDefinition) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) ColumnType(org.apache.ignite.schema.definition.ColumnType) Collectors(java.util.stream.Collectors) TestInfo(org.junit.jupiter.api.TestInfo) ExecutionException(java.util.concurrent.ExecutionException) 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) ColumnAlreadyExistsException(org.apache.ignite.lang.ColumnAlreadyExistsException) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest) IndexAlreadyExistsException(org.apache.ignite.lang.IndexAlreadyExistsException) SchemaBuilders(org.apache.ignite.schema.SchemaBuilders) Table(org.apache.ignite.table.Table) ItUtils(org.apache.ignite.internal.ItUtils) Tuple(org.apache.ignite.table.Tuple) IndexDefinition(org.apache.ignite.schema.definition.index.IndexDefinition)

Aggregations

ColumnDefinition (org.apache.ignite.schema.definition.ColumnDefinition)4 IndexDefinition (org.apache.ignite.schema.definition.index.IndexDefinition)4 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 UUID (java.util.UUID)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeUnit (java.util.concurrent.TimeUnit)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 Ignite (org.apache.ignite.Ignite)2 IgnitionManager (org.apache.ignite.IgnitionManager)2 ItUtils (org.apache.ignite.internal.ItUtils)2 SchemaConfigurationConverter.convert (org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter.convert)2 IgniteTablesInternal (org.apache.ignite.internal.table.IgniteTablesInternal)2 TableImpl (org.apache.ignite.internal.table.TableImpl)2 WatchListenerInhibitor (org.apache.ignite.internal.test.WatchListenerInhibitor)2 WatchListenerInhibitor.metastorageEventsInhibitor (org.apache.ignite.internal.test.WatchListenerInhibitor.metastorageEventsInhibitor)2