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"));
}
}
}
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);
}
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]);
}
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);
}
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));
}
Aggregations