Java Code Examples for org.apache.ignite.Ignite#set()

The following examples show how to use org.apache.ignite.Ignite#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IgniteSetExample.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize set.
 *
 * @param ignite Ignite.
 * @param setName Name of set.
 * @return Set.
 * @throws IgniteException If execution failed.
 */
private static IgniteSet<String> initializeSet(Ignite ignite, String setName) throws IgniteException {
    CollectionConfiguration setCfg = new CollectionConfiguration();

    setCfg.setAtomicityMode(TRANSACTIONAL);
    setCfg.setCacheMode(PARTITIONED);

    // Initialize new set.
    IgniteSet<String> set = ignite.set(setName, setCfg);

    // Initialize set items.
    for (int i = 0; i < 10; i++)
        set.add(Integer.toString(i));

    System.out.println("Set size after initializing: " + set.size());

    return set;
}
 
Example 2
Source File: IgniteClientReconnectCollectionsTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param colCfg Collection configuration.
 * @throws Exception If failed.
 */
private void setReconnectInProgress(final CollectionConfiguration colCfg) throws Exception {
    Ignite client = grid(serverCount());

    assertTrue(client.cluster().localNode().isClient());

    final Ignite srv = ignite(0);

    final String setName = "set-in-progress-" + colCfg.getAtomicityMode();

    final IgniteSet<String> clientSet = client.set(setName, colCfg);

    final IgniteSet<String> srvSet = srv.set(setName, null);

    assertTrue(clientSet.add("1"));

    assertFalse(srvSet.add("1"));

    BlockTcpCommunicationSpi commSpi = commSpi(srv);

    if (colCfg.getAtomicityMode() == ATOMIC)
        commSpi.blockMessage(GridNearAtomicUpdateResponse.class);
    else
        commSpi.blockMessage(GridNearTxPrepareResponse.class);

    final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {
        @Override public Object call() throws Exception {
            try {
                for (int i = 0; i < 100; i++)
                    clientSet.add("2");
            }
            catch (IgniteClientDisconnectedException e) {
                checkAndWait(e);

                return true;
            }

            return false;
        }
    });

    // Check that client waiting operation.
    GridTestUtils.assertThrows(log, new Callable<Object>() {
        @Override public Object call() throws Exception {
            return fut.get(200);
        }
    }, IgniteFutureTimeoutCheckedException.class, null);

    assertNotDone(fut);

    commSpi.unblockMessage();

    reconnectClientNode(client, srv, null);

    assertTrue((Boolean)fut.get(2, TimeUnit.SECONDS));

    assertTrue(clientSet.add("3"));

    assertFalse(srvSet.add("3"));

    srvSet.close();
}
 
Example 3
Source File: GridCacheSetAbstractSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that basic API works correctly when there are multiple structures in multiple groups.
 *
 * @throws Exception If failed.
 */
@Test
public void testMultipleStructuresInDifferentGroups() throws Exception {
    Ignite ignite = grid(0);

    CollectionConfiguration cfg1 = collectionConfiguration();
    CollectionConfiguration cfg2 = collectionConfiguration().setGroupName("grp2");

    IgniteSet<String> set1 = ignite.set("set1", cfg1);
    IgniteSet<String> set2 = ignite.set("set2", cfg1);
    IgniteSet<String> set3 = ignite.set("set3", cfg2);
    IgniteSet<String> set4 = ignite.set("set4", cfg2);

    assertTrue(set1.add("a"));
    assertTrue(set2.add("b"));
    assertTrue(set3.add("c"));
    assertTrue(set4.add("d"));

    assertFalse(set1.add("a"));
    assertFalse(set2.add("b"));
    assertFalse(set3.add("c"));
    assertFalse(set4.add("d"));

    assertTrue(set1.contains("a"));
    assertTrue(set2.contains("b"));
    assertTrue(set3.contains("c"));
    assertTrue(set4.contains("d"));

    assertEquals(1, set1.size());
    assertEquals(1, set2.size());
    assertEquals(1, set3.size());
    assertEquals(1, set4.size());

    assertFalse(set1.remove("z"));
    assertFalse(set2.remove("z"));
    assertFalse(set3.remove("z"));
    assertFalse(set4.remove("z"));

    assertTrue(set1.remove("a"));
    assertTrue(set2.remove("b"));
    assertTrue(set3.remove("c"));
    assertTrue(set4.remove("d"));

    assertTrue(set1.isEmpty());
    assertTrue(set2.isEmpty());
    assertTrue(set3.isEmpty());
    assertTrue(set4.isEmpty());

    set2.close();
    set4.close();

    assertTrue(set2.removed());
    assertTrue(set4.removed());

    assertFalse(set1.removed());
    assertFalse(set3.removed());

    assertNotNull(ignite.set("set1", null));
    assertNull(ignite.set("set2", null));

    set1.close();
    set3.close();
}
 
Example 4
Source File: IgniteClientReconnectCollectionsTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @param colCfg Collection configuration.
 * @throws Exception If failed.
 */
private void setReconnect(CollectionConfiguration colCfg) throws Exception {
    Ignite client = grid(serverCount());

    assertTrue(client.cluster().localNode().isClient());

    Ignite srv = ignite(0);

    final String setName = "set-" + colCfg.getAtomicityMode();

    IgniteSet<String> clientSet = client.set(setName, colCfg);

    final IgniteSet<String> srvSet = srv.set(setName, null);

    assertTrue(clientSet.add("1"));

    assertFalse(srvSet.add("1"));

    reconnectClientNode(client, srv, new Runnable() {
        @Override public void run() {
            assertTrue(srvSet.add("2"));
        }
    });

    assertFalse(clientSet.add("2"));

    assertTrue(clientSet.remove("2"));

    assertFalse(srvSet.contains("2"));
}
 
Example 5
Source File: IgnitePersistentStoreDataStructuresTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testSet() throws Exception {
    Ignite ignite = startGrids(4);

    ignite.active(true);

    IgniteSet<Object> set = ignite.set("testSet", new CollectionConfiguration());

    for (int i = 0; i < 100; i++)
        set.add(i);

    assertEquals(100, set.size());

    stopAllGrids();

    ignite = startGrids(4);

    ignite.active(true);

    set = ignite.set("testSet", null);

    assertFalse(set.add(99));

    for (int i = 0; i < 100; i++)
        assertTrue(set.contains(i));

    assertEquals(100, set.size());
}
 
Example 6
Source File: GridCacheSetAbstractSelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @param collocated Collocation flag.
 */
private void testCacheReuse(boolean collocated) {
    Ignite ignite = grid(0);

    CollectionConfiguration colCfg = collectionConfiguration().setCollocated(collocated);

    colCfg.setAtomicityMode(ATOMIC);
    colCfg.setGroupName("grp1");

    IgniteSet set1 = ignite.set("set1", colCfg);
    IgniteSet set2 = ignite.set("set2", colCfg);

    assertEquals(separated(set1), cctx(set1).cacheId() != cctx(set2).cacheId());

    colCfg.setAtomicityMode(TRANSACTIONAL);

    IgniteSet set3 = ignite.set("set3", colCfg);
    IgniteSet set4 = ignite.set("set4", colCfg);

    assertEquals(separated(set3), cctx(set3).cacheId() != cctx(set4).cacheId());

    assertTrue(cctx(set1).cacheId() != cctx(set3).cacheId());
    assertTrue(cctx(set1).groupId() == cctx(set3).groupId());

    colCfg.setGroupName("gtp2");

    IgniteSet set5 = ignite.set("set5", colCfg);
    IgniteSet set6 = ignite.set("set6", colCfg);

    assertEquals(separated(set5), cctx(set5).cacheId() != cctx(set6).cacheId());

    assertTrue(cctx(set1).groupId() != cctx(set5).groupId());

    Stream.of(set1, set2, set3, set4, set5, set6).forEach(IgniteSet::close);
}
 
Example 7
Source File: IgniteClientDataStructuresAbstractTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @param creator Creator node.
 * @param other Other node.
 * @throws Exception If failed.
 */
private void testSet(Ignite creator, Ignite other) throws Exception {
    assertNull(creator.set("set1", null));
    assertNull(other.set("set1", null));

    CollectionConfiguration colCfg = new CollectionConfiguration();

    try (IgniteSet<Integer> set = creator.set("set1", colCfg)) {
        assertNotNull(set);

        assertEquals(0, set.size());

        assertFalse(set.contains(1));

        assertTrue(set.add(1));

        assertTrue(set.contains(1));

        IgniteSet<Integer> set0 = other.set("set1", null);

        assertTrue(set0.contains(1));

        assertEquals(1, set0.size());

        assertTrue(set0.remove(1));

        assertFalse(set.contains(1));
    }

    assertNull(creator.set("set1", null));
    assertNull(other.set("set1", null));
}
 
Example 8
Source File: IgniteClientReconnectCollectionsTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @param colCfg Collection configuration.
 * @throws Exception If failed.
 */
private void setReconnectRemove(CollectionConfiguration colCfg) throws Exception {
    Ignite client = grid(serverCount());

    assertTrue(client.cluster().localNode().isClient());

    final Ignite srv = ignite(0);

    final String setName = "set-rm-" + colCfg.getAtomicityMode();

    final IgniteSet<String> clientSet = client.set(setName, colCfg);

    final IgniteSet<String> srvSet = srv.set(setName, null);

    assertTrue(clientSet.add("1"));

    assertFalse(srvSet.add("1"));

    reconnectClientNode(client, srv, new Runnable() {
        @Override public void run() {
            srvSet.close();
        }
    });

    GridTestUtils.assertThrows(log, new Callable<Object>() {
        @Override public Object call() throws Exception {
            clientSet.add("fail");

            return null;
        }
    }, IllegalStateException.class, null);

    IgniteSet<String> newClientSet = client.set(setName, colCfg);

    IgniteSet<String> newSrvSet = srv.set(setName, null);

    assertTrue(newClientSet.add("1"));

    assertFalse(newSrvSet.add("1"));

    newSrvSet.close();
}
 
Example 9
Source File: GridCacheSetAbstractSelfTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * Implementation of ignite data structures internally uses special system caches, need make sure
 * that transaction on these system caches do not intersect with transactions started by user.
 *
 * @throws Exception If failed.
 */
@Test
public void testIsolation() throws Exception {
    CollectionConfiguration colCfg = collectionConfiguration();

    Ignite ignite = grid(0);

    CacheConfiguration cfg = new CacheConfiguration(DEFAULT_CACHE_NAME);

    cfg.setName("myCache");
    cfg.setAtomicityMode(TRANSACTIONAL);
    cfg.setWriteSynchronizationMode(FULL_SYNC);

    IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(cfg);

    try {
        IgniteSet<Integer> set0 = ignite.set(SET_NAME, colCfg);

        assertNotNull(set0);

        try (Transaction tx = ignite.transactions().txStart()) {
            cache.put(1, 1);

            Collection<Integer> items = new ArrayList<>(100);

            for (int i = 0; i < 100; i++)
                items.add(i);

            set0.addAll(items);

            tx.rollback();
        }

        assertEquals(0, cache.size());

        assertEquals(100, set0.size());

        set0.close();
    }
    finally {
        ignite.destroyCache(cfg.getName());
    }
}