Java Code Examples for org.apache.ignite.IgniteCache#clear()

The following examples show how to use org.apache.ignite.IgniteCache#clear() . 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: IgniteTxConfigCacheSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Success if implicit tx fails.
 *
 * @param cache Cache name.
 * @throws Exception If failed.
 */
protected void checkImplicitTxTimeout(final IgniteCache<Object, Object> cache) throws Exception {
    TestCommunicationSpi.delay = true;

    Integer key = primaryKey(ignite(1).cache(CACHE_NAME));

    try {
        cache.put(key, 0);

        fail("Timeout exception must be thrown");
    }
    catch (CacheException ignored) {
        // No-op.
    }
    finally {
        TestCommunicationSpi.delay = false;
    }

    cache.clear();
}
 
Example 2
Source File: IgniteCacheInsertSqlQuerySelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 *
 */
@Test
public void testDuplicateKeysException() {
    final IgniteCache<Integer, Integer> p = ignite(0).cache("I2I");

    p.clear();

    p.put(3, 5);

    GridTestUtils.assertThrows(log, new Callable<Void>() {
        /** {@inheritDoc} */
        @Override public Void call() throws Exception {
            p.query(new SqlFieldsQuery("insert into Integer(_key, _val) values (1, ?), " +
                "(?, 4), (5, 6)").setArgs(2, 3));

            return null;
        }
    }, CacheException.class, "Failed to INSERT some keys because they are already in cache [keys=[3]]");

    assertEquals(2, (int)p.get(1));
    assertEquals(5, (int)p.get(3));
    assertEquals(6, (int)p.get(5));
}
 
Example 3
Source File: RestBinaryProtocolSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testReplace() throws Exception {
    assertFalse(client.cacheReplace(DEFAULT_CACHE_NAME, "key1", "val1"));

    IgniteCache<Object, Object> jcacheDflt = grid().cache(DEFAULT_CACHE_NAME);

    jcacheDflt.put("key1", "val1");
    assertTrue(client.cacheReplace(DEFAULT_CACHE_NAME, "key1", "val2"));

    assertFalse(client.cacheReplace(DEFAULT_CACHE_NAME, "key2", "val1"));
    jcacheDflt.put("key2", "val1");
    assertTrue(client.cacheReplace(DEFAULT_CACHE_NAME, "key2", "val2"));

    jcacheDflt.clear();

    assertFalse(client.cacheReplace(CACHE_NAME, "key1", "val1"));
    grid().cache(CACHE_NAME).put("key1", "val1");
    assertTrue(client.cacheReplace(CACHE_NAME, "key1", "val2"));
}
 
Example 4
Source File: CacheQueryExample.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Populate cache with test data.
 */
private static void initialize() {
    IgniteCache<Long, Organization> orgCache = Ignition.ignite().cache(ORG_CACHE);

    // Clear cache before running the example.
    orgCache.clear();

    // Organizations.
    Organization org1 = new Organization("ApacheIgnite");
    Organization org2 = new Organization("Other");

    orgCache.put(org1.id(), org1);
    orgCache.put(org2.id(), org2);

    IgniteCache<AffinityKey<Long>, Person> colPersonCache = Ignition.ignite().cache(PERSON_CACHE);

    // Clear caches before running the example.
    colPersonCache.clear();

    // People.
    Person p1 = new Person(org1, "John", "Doe", 2000, "John Doe has Master Degree.");
    Person p2 = new Person(org1, "Jane", "Doe", 1000, "Jane Doe has Bachelor Degree.");
    Person p3 = new Person(org2, "John", "Smith", 1000, "John Smith has Bachelor Degree.");
    Person p4 = new Person(org2, "Jane", "Smith", 2000, "Jane Smith has Master Degree.");

    // Note that in this example we use custom affinity key for Person objects
    // to ensure that all persons are collocated with their organizations.
    colPersonCache.put(p1.key(), p1);
    colPersonCache.put(p2.key(), p2);
    colPersonCache.put(p3.key(), p3);
    colPersonCache.put(p4.key(), p4);
}
 
Example 5
Source File: IgniteCacheContainsKeyAtomicTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
    super.afterTest();

    IgniteCache cache = ignite(0).cache(CACHE_NAME);

    if (cache != null)
        cache.clear();
}
 
Example 6
Source File: GridCacheWriteBehindStoreAbstractTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
    IgniteCache<?, ?> cache = jcache();

    if (cache != null)
        cache.clear();

    store.reset();
}
 
Example 7
Source File: SqlQueriesExample.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Populate cache with test data.
 */
private static void initialize() {
    IgniteCache<Long, Organization> orgCache = Ignition.ignite().cache(ORG_CACHE);

    // Clear cache before running the example.
    orgCache.clear();

    // Organizations.
    Organization org1 = new Organization("ApacheIgnite");
    Organization org2 = new Organization("Other");

    orgCache.put(org1.id(), org1);
    orgCache.put(org2.id(), org2);

    IgniteCache<AffinityKey<Long>, Person> colPersonCache = Ignition.ignite().cache(COLLOCATED_PERSON_CACHE);
    IgniteCache<Long, Person> personCache = Ignition.ignite().cache(PERSON_CACHE);

    // Clear caches before running the example.
    colPersonCache.clear();
    personCache.clear();

    // People.
    Person p1 = new Person(org1, "John", "Doe", 2000, "John Doe has Master Degree.");
    Person p2 = new Person(org1, "Jane", "Doe", 1000, "Jane Doe has Bachelor Degree.");
    Person p3 = new Person(org2, "John", "Smith", 1000, "John Smith has Bachelor Degree.");
    Person p4 = new Person(org2, "Jane", "Smith", 2000, "Jane Smith has Master Degree.");

    // Note that in this example we use custom affinity key for Person objects
    // to ensure that all persons are collocated with their organizations.
    colPersonCache.put(p1.key(), p1);
    colPersonCache.put(p2.key(), p2);
    colPersonCache.put(p3.key(), p3);
    colPersonCache.put(p4.key(), p4);

    // These Person objects are not collocated with their organizations.
    personCache.put(p1.id, p1);
    personCache.put(p2.id, p2);
    personCache.put(p3.id, p3);
    personCache.put(p4.id, p4);
}
 
Example 8
Source File: IgniteCacheSqlQueryMultiThreadedSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testQuery() throws Exception {
    final IgniteCache<Integer, Person> cache = grid(0).cache(DEFAULT_CACHE_NAME);

    cache.clear();

    for (int i = 0; i < 2000; i++)
        cache.put(i, new Person(i));

    GridTestUtils.runMultiThreaded(new Callable<Void>() {
        @Override public Void call() throws Exception {
            for (int i = 0; i < 100; i++) {
                QueryCursor<Cache.Entry<Integer, Person>> qry =
                    cache.query(new SqlQuery<Integer, Person>("Person", "age >= 0"));

                int cnt = 0;

                for (Cache.Entry<Integer, Person> e : qry)
                    cnt++;

                assertEquals(2000, cnt);
            }

            return null;
        }
    }, 16, "test");
}
 
Example 9
Source File: IgniteCacheJoinPartitionedAndReplicatedTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected void beforeTest() throws Exception {
    super.beforeTest();

    Ignite client = grid(2);

    IgniteCache<Object, Object> personCache = client.cache(PERSON_CACHE);
    IgniteCache<Object, Object> orgCache = client.cache(ORG_CACHE);
    IgniteCache<Object, Object> orgCacheRepl = client.cache(ORG_CACHE_REPLICATED);

    personCache.clear();
    orgCache.clear();
    orgCacheRepl.clear();
}
 
Example 10
Source File: GridCacheWriteBehindStoreLoadTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
    IgniteCache<Object, Object> cache = jcache();

    if (cache != null)
        cache.clear();
}
 
Example 11
Source File: GridCacheBasicStoreMultithreadedAbstractTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
    IgniteCache<?, ?> cache = jcache();

    if (cache != null)
        cache.clear();

    stopAllGrids();
}
 
Example 12
Source File: SqlQueryEmployees.java    From ignite-book-code-samples with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Let's fill ignite cache with test data. Data are taken from oracle's study test scheme EMP. The structure of
 * those scheme you can see below resources folder of this module.
 *
 * @throws InterruptedException In case of error.
 */
private static void initialize() throws InterruptedException {
    IgniteCache<Integer, Department> deptCache = Ignition.ignite().cache(DEPARTMENT_CACHE_NAME);
    IgniteCache<EmployeeKey, Employee> employeeCache = Ignition.ignite().cache(EMPLOYEE_CACHE_NAME);

    // Clear caches before start.
    deptCache.clear();
    employeeCache.clear();

    // Departments
    Department dept1 = new Department("Accounting", "New York");
    Department dept2 = new Department("Research", "Dallas");
    Department dept3 = new Department("Sales", "Chicago");
    Department dept4 = new Department("Operations", "Boston");

    // Employees
    Employee emp1 = new Employee("King", dept1, "President", null, localDateOf("17-11-1981"), 5000);
    Employee emp2 = new Employee("Blake", dept3, "Manager", emp1.getEmpno(), localDateOf("01-05-1981"), 2850);
    Employee emp3 = new Employee("Clark", dept1, "Manager", emp1.getEmpno(), localDateOf("09-06-1981"), 2450);
    Employee emp4 = new Employee("Jones", dept2, "Manager", emp1.getEmpno(), localDateOf("02-04-1981"), 2975);
    Employee emp5 = new Employee("Scott", dept2, "Analyst", emp4.getEmpno(), localDateOf("13-07-1987").minusDays(85), 3000);
    Employee emp6 = new Employee("Ford", dept2, "Analyst", emp4.getEmpno(), localDateOf("03-12-1981"), 3000);
    Employee emp7 = new Employee("Smith", dept2, "Clerk", emp6.getEmpno(), localDateOf("17-12-1980"), 800);
    Employee emp8 = new Employee("Allen", dept3, "Salesman", emp2.getEmpno(), localDateOf("20-02-1981"), 1600);
    Employee emp9 = new Employee("Ward", dept3, "Salesman", emp2.getEmpno(), localDateOf("22-02-1981"), 1250);
    Employee emp10 = new Employee("Martin", dept3, "Salesman", emp2.getEmpno(), localDateOf("28-09-1981"), 1250);
    Employee emp11 = new Employee("Turner", dept3, "Salesman", emp2.getEmpno(), localDateOf("08-09-1981"), 1500);
    Employee emp12 = new Employee("Adams", dept2, "Clerk", emp5.getEmpno(), localDateOf("13-07-1987").minusDays(51), 1100);
    Employee emp13 = new Employee("James", dept3, "Clerk", emp2.getEmpno(), localDateOf("03-12-1981"), 950);
    Employee emp14 = new Employee("Miller", dept1, "Clerk", emp3.getEmpno(), localDateOf("23-01-1982"), 1300);

    deptCache.put(dept1.getDeptno(), dept1);
    deptCache.put(dept2.getDeptno(), dept2);
    deptCache.put(dept3.getDeptno(), dept3);
    deptCache.put(dept4.getDeptno(), dept4);

    // Note that in this example we use custom affinity key for Employee objects
    // to ensure that all persons are collocated with their departments.
    employeeCache.put(emp1.getKey(), emp1);
    employeeCache.put(emp2.getKey(), emp2);
    employeeCache.put(emp3.getKey(), emp3);
    employeeCache.put(emp4.getKey(), emp4);
    employeeCache.put(emp5.getKey(), emp5);
    employeeCache.put(emp6.getKey(), emp6);
    employeeCache.put(emp7.getKey(), emp7);
    employeeCache.put(emp8.getKey(), emp8);
    employeeCache.put(emp9.getKey(), emp9);
    employeeCache.put(emp10.getKey(), emp10);
    employeeCache.put(emp11.getKey(), emp11);
    employeeCache.put(emp12.getKey(), emp12);
    employeeCache.put(emp13.getKey(), emp13);
    employeeCache.put(emp14.getKey(), emp14);

    // Wait 1 second to be sure that all nodes processed put requests.
    Thread.sleep(1000);
}
 
Example 13
Source File: JdbcThinAutoCloseServerCursorTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
    IgniteCache<Integer, Person> cache = grid(0).cache(CACHE_NAME);

    cache.clear();
}
 
Example 14
Source File: SqlTwoCachesInGroupWithSameEntryTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception On error.
 */
@SuppressWarnings("unchecked")
@Test
public void test() throws Exception {
    IgniteEx ign = startGrid(0);

    ign.cluster().active(true);

    IgniteCache cache0 = ign.createCache(new CacheConfiguration<>("cache0")
        .setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL)
        .setGroupName("grp0")
        .setSqlSchema("CACHE0")
        .setIndexedTypes(Integer.class, Integer.class));

    IgniteCache cache1 = ign.createCache(new CacheConfiguration<>("cache1")
        .setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL)
        .setGroupName("grp0")
        .setSqlSchema("CACHE1")
        .setIndexedTypes(Integer.class, Integer.class));

    for (int i = 0; i < KEYS; ++i) {
        cache0.put(i, i);
        cache1.put(i, i);
    }

    if (useOnlyPkHashIndex) {
        for (GridH2Table t : ((IgniteH2Indexing)grid(0).context().query().getIndexing()).schemaManager().dataTables())
            GridTestUtils.setFieldValue(t, "rebuildFromHashInProgress", 1);
    }

    assertEquals(KEYS, cache0.size());
    assertEquals(KEYS, cache1.size());
    assertEquals(KEYS, sql("select * FROM cache0.Integer").getAll().size());
    assertEquals(KEYS, sql("select * FROM cache1.Integer").getAll().size());

    cache0.clear();

    assertEquals(0, cache0.size());
    assertEquals(KEYS, cache1.size());
    assertEquals(0, sql("select * FROM cache0.Integer").getAll().size());
    assertEquals(KEYS, sql("select * FROM cache1.Integer").getAll().size());
}
 
Example 15
Source File: GridCacheFullTextQuerySelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * Clear cache with check.
 */
private static void clearCache(IgniteEx ignite) {
    IgniteCache<Integer, Person> cache = ignite.cache(PERSON_CACHE);

    cache.clear();

    List all = cache.query(new TextQuery<>(Person.class, "1*")).getAll();

    assertTrue(all.isEmpty());
}
 
Example 16
Source File: GridCacheLazyQueryPartitionsReleaseTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * Lazy query release partitions on cursor close test.
 *
 * @throws Exception If failed.
 */
@Test
public void testLazyQueryPartitionsReleaseOnClose() throws Exception {
    Ignite node1 = startGrid(0);

    IgniteCache<Integer, Person> cache = node1.cache(PERSON_CACHE);

    cache.clear();

    Affinity<Integer> aff = node1.affinity(PERSON_CACHE);

    int partsFilled = fillAllPartitions(cache, aff);

    SqlFieldsQuery qry = new SqlFieldsQuery("select name, age from person")
        .setPageSize(1);

    FieldsQueryCursor<List<?>> qryCursor = cache.query(qry);

    Iterator<List<?>> it = qryCursor.iterator();

    if (it.hasNext())
        it.next();
    else
        fail("No query results.");

    startGrid(1);

    // Close cursor. Partitions should be released now.
    qryCursor.close();

    for (Ignite ig : G.allGrids())
        ig.cache(PERSON_CACHE).rebalance().get();

    assertEquals("Wrong result set size", partsFilled, cache.query(qry).getAll().size());
}
 
Example 17
Source File: IgniteDbPutGetAbstractTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception if failed.
 */
@Test
public void testSizeClear() throws Exception {
    Assume.assumeFalse("https://issues.apache.org/jira/browse/IGNITE-7952", MvccFeatureChecker.forcedMvcc());

    final IgniteCache<Integer, DbValue> cache = cache(DEFAULT_CACHE_NAME);

    GridCacheAdapter<Integer, DbValue> internalCache = internalCache(cache);

    int cnt = 5000;

    X.println("Put start");

    for (int i = 0; i < cnt; i++) {
        DbValue v0 = new DbValue(i, "test-value", i);

        cache.put(i, v0);

        checkEmpty(internalCache, i);

        assertEquals(v0, cache.get(i));
    }

    awaitPartitionMapExchange();

    assertEquals(cnt, cache.size(CachePeekMode.OFFHEAP));

    X.println("Clear start.");

    cache.clear();

    assertEquals(0, cache.size(CachePeekMode.OFFHEAP));

    for (int i = 0; i < cnt; i++)
        assertNull(cache.get(i));
}
 
Example 18
Source File: GridCacheAbstractMetricsSelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * Test {@link CacheMetrics#getSize()} and {@link CacheMetrics#getCacheSize()} work equally.
 *
 * @throws Exception If failed.
 */
@Test
public void testCacheSizeWorksAsSize() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);

    cache.clear();

    awaitMetricsUpdate(1);

    assertEquals(0, cache.metrics().getCacheSize());

    for (int i = 0; i < KEY_CNT; i++)
        cache.put(i, i);

    awaitMetricsUpdate(1);

    assertEquals(KEY_CNT, cache.metrics().getCacheSize());

    assertEquals(cache.localSizeLong(CachePeekMode.PRIMARY), cache.localMetrics().getCacheSize());

    for (int i = 0; i < KEY_CNT / 2; i++)
        cache.remove(i, i);

    awaitMetricsUpdate(1);

    assertEquals(KEY_CNT / 2, cache.metrics().getCacheSize());

    assertEquals(cache.localSizeLong(CachePeekMode.PRIMARY), cache.localMetrics().getCacheSize());

    cache.removeAll();

    awaitMetricsUpdate(1);

    assertEquals(0, cache.metrics().getCacheSize());

    assertEquals(0, cache.localMetrics().getCacheSize());
}
 
Example 19
Source File: StoreArrayKeyTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 *
 */
@Test
public void shouldClearCache() {
    IgniteCache<Object, Object> cache = node1.getOrCreateCache(CACHE);

    cache.put(firstKey, "val");

    cache.clear();

    assertEquals(0, cache.size());

    Iterator<Cache.Entry<Object, Object>> it = cache.iterator();

    assertFalse(it.hasNext());
}
 
Example 20
Source File: IgnitePersistentStoreTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/** */
@Test
public void loadCacheTest() {
    Ignition.stopAll(true);

    LOGGER.info("Running loadCache test");

    LOGGER.info("Filling Cassandra table with test data");

    CacheStore store = CacheStoreHelper.createCacheStore("personTypes",
        new ClassPathResource("org/apache/ignite/tests/persistence/pojo/persistence-settings-3.xml"),
        CassandraHelper.getAdminDataSrc());

    Collection<CacheEntryImpl<PersonId, Person>> entries = TestsHelper.generatePersonIdsPersonsEntries();

    //noinspection unchecked
    store.writeAll(entries);

    LOGGER.info("Cassandra table filled with test data");

    LOGGER.info("Running loadCache test");

    try (Ignite ignite = Ignition.start("org/apache/ignite/tests/persistence/pojo/ignite-config.xml")) {
        CacheConfiguration<PersonId, Person> ccfg = new CacheConfiguration<>("cache3");

        IgniteCache<PersonId, Person> personCache3 = ignite.getOrCreateCache(ccfg);

        int size = personCache3.size(CachePeekMode.ALL);

        LOGGER.info("Initial cache size " + size);

        LOGGER.info("Loading cache data from Cassandra table");

        String qry = "select * from test1.pojo_test3 limit 3";

        personCache3.loadCache(null, qry);

        size = personCache3.size(CachePeekMode.ALL);
        Assert.assertEquals("Cache data was incorrectly loaded from Cassandra table by '" + qry + "'", 3, size);

        personCache3.clear();

        personCache3.loadCache(null, new SimpleStatement(qry));

        size = personCache3.size(CachePeekMode.ALL);
        Assert.assertEquals("Cache data was incorrectly loaded from Cassandra table by statement", 3, size);

        personCache3.clear();

        personCache3.loadCache(null);

        size = personCache3.size(CachePeekMode.ALL);
        Assert.assertEquals("Cache data was incorrectly loaded from Cassandra. " +
                "Expected number of records is " + TestsHelper.getBulkOperationSize() +
                ", but loaded number of records is " + size,
            TestsHelper.getBulkOperationSize(), size);

        LOGGER.info("Cache data loaded from Cassandra table");
    }

    LOGGER.info("loadCache test passed");
}