Search in sources :

Example 61 with Table

use of org.apache.ignite.table.Table 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)

Example 62 with Table

use of org.apache.ignite.table.Table in project ignite-3 by apache.

the class ItTablesApiTest method testCreateDropTable.

/**
 * Checks that if a table would be created/dropped in any cluster node, this action reflects on all others.
 * Table management operations should pass in linearize order: if an action completed in one node,
 * the result has to be visible to another one.
 *
 * @throws Exception If failed.
 */
@Test
public void testCreateDropTable() throws Exception {
    clusterNodes.forEach(ign -> assertNull(ign.tables().table(TABLE_NAME)));
    Ignite ignite1 = clusterNodes.get(1);
    WatchListenerInhibitor ignite1Inhibitor = metastorageEventsInhibitor(ignite1);
    ignite1Inhibitor.startInhibit();
    Table table = createTable(clusterNodes.get(0), SCHEMA, SHORT_TABLE_NAME);
    UUID tblId = ((TableImpl) table).tableId();
    CompletableFuture<Table> tableByNameFut = CompletableFuture.supplyAsync(() -> {
        return ignite1.tables().table(TABLE_NAME);
    });
    CompletableFuture<Table> tableByIdFut = CompletableFuture.supplyAsync(() -> {
        try {
            return ((IgniteTablesInternal) ignite1.tables()).table(tblId);
        } catch (NodeStoppingException e) {
            throw new AssertionError(e.getMessage());
        }
    });
    // Therefore the table still doesn't exists locally, but API prevents getting null and waits events.
    for (Ignite ignite : clusterNodes) {
        if (ignite != ignite1) {
            assertNotNull(ignite.tables().table(TABLE_NAME));
            assertNotNull(((IgniteTablesInternal) ignite.tables()).table(tblId));
        }
    }
    assertFalse(tableByNameFut.isDone());
    assertFalse(tableByIdFut.isDone());
    ignite1Inhibitor.stopInhibit();
    assertNotNull(tableByNameFut.get(10, TimeUnit.SECONDS));
    assertNotNull(tableByIdFut.get(10, TimeUnit.SECONDS));
    ignite1Inhibitor.startInhibit();
    clusterNodes.get(0).tables().dropTable(TABLE_NAME);
    // Therefore the table still exists locally, but API prevents getting it.
    for (Ignite ignite : clusterNodes) {
        assertNull(ignite.tables().table(TABLE_NAME));
        assertNull(((IgniteTablesInternal) ignite.tables()).table(tblId));
        assertThrows(TableNotFoundException.class, () -> dropTable(ignite, SCHEMA, SHORT_TABLE_NAME));
        dropTableIfExists(ignite, SCHEMA, SHORT_TABLE_NAME);
    }
    ignite1Inhibitor.stopInhibit();
}
Also used : Table(org.apache.ignite.table.Table) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) TableImpl(org.apache.ignite.internal.table.TableImpl) Ignite(org.apache.ignite.Ignite) WatchListenerInhibitor(org.apache.ignite.internal.test.WatchListenerInhibitor) UUID(java.util.UUID) IgniteTablesInternal(org.apache.ignite.internal.table.IgniteTablesInternal) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 63 with Table

use of org.apache.ignite.table.Table in project ignite-3 by apache.

the class ItTablesApiTest method testTableAlreadyCreated.

/**
 * Tries to create a table which is already created.
 */
@Test
public void testTableAlreadyCreated() {
    clusterNodes.forEach(ign -> assertNull(ign.tables().table(TABLE_NAME)));
    Ignite ignite0 = clusterNodes.get(0);
    Table tbl = createTable(ignite0, SCHEMA, SHORT_TABLE_NAME);
    assertThrows(TableAlreadyExistsException.class, () -> createTable(ignite0, SCHEMA, SHORT_TABLE_NAME));
    assertEquals(tbl, createTableIfNotExists(ignite0, SCHEMA, SHORT_TABLE_NAME));
}
Also used : Table(org.apache.ignite.table.Table) Ignite(org.apache.ignite.Ignite) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Example 64 with Table

use of org.apache.ignite.table.Table in project ignite-3 by apache.

the class ItThinClientConnectionTest method testThinClientConnectsToServerNodesAndExecutesBasicTableOperations.

/**
 * Check that thin client can connect to any server node and work with table API.
 */
@Test
void testThinClientConnectsToServerNodesAndExecutesBasicTableOperations() throws Exception {
    for (var addr : getNodeAddresses()) {
        try (var client = IgniteClient.builder().addresses(addr).build()) {
            List<Table> tables = client.tables().tables();
            assertEquals(1, tables.size());
            Table table = tables.get(0);
            assertEquals(String.format("%s.%s", SCHEMA_NAME, TABLE_NAME), table.name());
            var tuple = Tuple.create().set(COLUMN_KEY, 1).set(COLUMN_VAL, "Hello");
            var keyTuple = Tuple.create().set(COLUMN_KEY, 1);
            RecordView<Tuple> recView = table.recordView();
            recView.upsert(null, tuple);
            assertEquals("Hello", recView.get(null, keyTuple).stringValue(COLUMN_VAL));
            var kvView = table.keyValueView();
            assertEquals("Hello", kvView.get(null, keyTuple).stringValue(COLUMN_VAL));
            var pojoView = table.recordView(TestPojo.class);
            assertEquals("Hello", pojoView.get(null, new TestPojo(1)).val);
            assertTrue(recView.delete(null, keyTuple));
        }
    }
}
Also used : Table(org.apache.ignite.table.Table) Tuple(org.apache.ignite.table.Tuple) Test(org.junit.jupiter.api.Test)

Example 65 with Table

use of org.apache.ignite.table.Table in project ignite-3 by apache.

the class ItNoThreadsLeftTest method test.

/**
 * Starts one node and stops it and checks that the amount of thread equivalent as before.
 *
 * @param testInfo JUnit meta info for the test.
 * @throws Exception If failed.
 */
@Test
public void test(TestInfo testInfo) throws Exception {
    Set<Thread> threadsBefore = getCurrentThreads();
    String nodeName = IgniteTestUtils.testNodeName(testInfo, 0);
    Ignite ignite = IgnitionManager.start(nodeName, NODE_CONFIGURATION.apply(nodeName), workDir.resolve(nodeName));
    Table tbl = createTable(ignite, SCHEMA, SHORT_TABLE_NAME);
    assertNotNull(tbl);
    ignite.close();
    assertTrue(waitForCondition(() -> threadsBefore.size() == getCurrentThreads().size(), 3_000), getCurrentThreads().stream().filter(thread -> !threadsBefore.contains(thread)).map(Thread::getName).collect(joining(", ")));
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) SchemaConfigurationConverter.convert(org.apache.ignite.internal.schema.configuration.SchemaConfigurationConverter.convert) Set(java.util.Set) Ignite(org.apache.ignite.Ignite) ColumnType(org.apache.ignite.schema.definition.ColumnType) IgniteTestUtils.waitForCondition(org.apache.ignite.internal.testframework.IgniteTestUtils.waitForCondition) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) IgnitionManager(org.apache.ignite.IgnitionManager) IgniteTestUtils(org.apache.ignite.internal.testframework.IgniteTestUtils) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest) SchemaBuilders(org.apache.ignite.schema.SchemaBuilders) Table(org.apache.ignite.table.Table) NotNull(org.jetbrains.annotations.NotNull) Table(org.apache.ignite.table.Table) Ignite(org.apache.ignite.Ignite) Test(org.junit.jupiter.api.Test) IgniteAbstractTest(org.apache.ignite.internal.testframework.IgniteAbstractTest)

Aggregations

Table (org.apache.ignite.table.Table)65 Test (org.junit.jupiter.api.Test)51 Tuple (org.apache.ignite.table.Tuple)29 Ignite (org.apache.ignite.Ignite)15 TableDefinition (org.apache.ignite.schema.definition.TableDefinition)15 List (java.util.List)12 UUID (java.util.UUID)12 CompletableFuture (java.util.concurrent.CompletableFuture)12 IgniteAbstractTest (org.apache.ignite.internal.testframework.IgniteAbstractTest)12 ArrayList (java.util.ArrayList)11 NodeStoppingException (org.apache.ignite.lang.NodeStoppingException)11 TableImpl (org.apache.ignite.internal.table.TableImpl)10 Function (java.util.function.Function)9 Collectors (java.util.stream.Collectors)9 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)9 SchemaBuilders (org.apache.ignite.schema.SchemaBuilders)8 ColumnType (org.apache.ignite.schema.definition.ColumnType)8 NotNull (org.jetbrains.annotations.NotNull)8 Consumer (java.util.function.Consumer)7 DataStorageConfiguration (org.apache.ignite.configuration.schemas.store.DataStorageConfiguration)7