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

The following examples show how to use org.apache.ignite.IgniteCache#containsKey() . 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: GridCacheAtomicPartitionedTckMetricsSelfTestImpl.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testPutIfAbsent() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);

    long hitCount = 0;
    long missCount = 0;
    long putCount = 0;

    boolean result = cache.putIfAbsent(1, 1);

    ++putCount;
    ++missCount;

    assertTrue(result);

    assertEquals(missCount, cache.localMetrics().getCacheMisses());
    assertEquals(hitCount, cache.localMetrics().getCacheHits());
    assertEquals(putCount, cache.localMetrics().getCachePuts());

    result = cache.putIfAbsent(1, 1);

    ++hitCount;

    cache.containsKey(123);

    assertFalse(result);
    assertEquals(hitCount, cache.localMetrics().getCacheHits());
    assertEquals(putCount, cache.localMetrics().getCachePuts());
    assertEquals(missCount, cache.localMetrics().getCacheMisses());
}
 
Example 2
Source File: HadoopIgfsDualAbstractSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Check how prefetch override works.
 *
 * @throws Exception IF failed.
 */
@Test
public void testOpenPrefetchOverride() throws Exception {
    create(igfsSecondary, paths(DIR, SUBDIR), paths(FILE));

    // Write enough data to the secondary file system.
    final int blockSize = IGFS_BLOCK_SIZE;

    IgfsOutputStream out = igfsSecondary.append(FILE, false);

    int totalWritten = 0;

    while (totalWritten < blockSize * 2 + chunk.length) {
        out.write(chunk);

        totalWritten += chunk.length;
    }

    out.close();

    awaitFileClose(igfsSecondary, FILE);

    // Instantiate file system with overridden "seq reads before prefetch" property.
    Configuration cfg = new Configuration();

    cfg.addResource(U.resolveIgniteUrl(PRIMARY_CFG));

    int seqReads = SEQ_READS_BEFORE_PREFETCH + 1;

    cfg.setInt(String.format(PARAM_IGFS_SEQ_READS_BEFORE_PREFETCH, "igfs@"), seqReads);

    FileSystem fs = FileSystem.get(new URI(PRIMARY_URI), cfg);

    // Read the first two blocks.
    Path fsHome = new Path(PRIMARY_URI);
    Path dir = new Path(fsHome, DIR.name());
    Path subdir = new Path(dir, SUBDIR.name());
    Path file = new Path(subdir, FILE.name());

    FSDataInputStream fsIn = fs.open(file);

    final byte[] readBuf = new byte[blockSize * 2];

    fsIn.readFully(0, readBuf, 0, readBuf.length);

    // Wait for a while for prefetch to finish (if any).
    IgfsMetaManager meta = igfs.context().meta();

    IgfsEntryInfo info = meta.info(meta.fileId(FILE));

    IgfsBlockKey key = new IgfsBlockKey(info.id(), info.affinityKey(), info.evictExclude(), 2);

    IgniteCache<IgfsBlockKey, byte[]> dataCache = igfs.context().kernalContext().cache().jcache(
        igfs.configuration().getDataCacheConfiguration().getName());

    for (int i = 0; i < 10; i++) {
        if (dataCache.containsKey(key))
            break;
        else
            U.sleep(100);
    }

    fsIn.close();

    // Remove the file from the secondary file system.
    igfsSecondary.delete(FILE, false);

    // Try reading the third block. Should fail.
    GridTestUtils.assertThrows(log, new Callable<Object>() {
        @Override public Object call() throws Exception {
            IgfsInputStream in0 = igfs.open(FILE);

            in0.seek(blockSize * 2);

            try {
                in0.read(readBuf);
            }
            finally {
                U.closeQuiet(in0);
            }

            return null;
        }
    }, IOException.class,
        "Failed to read data due to secondary file system exception: /dir/subdir/file");
}
 
Example 3
Source File: IgniteCacheConfigVariationsAbstractTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @param cache Cache.
 * @param key Key.
 * @return {@code True} if cache contains given key.
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
protected static boolean containsKey(IgniteCache cache, Object key) throws Exception {
    return cache.containsKey(key);
}
 
Example 4
Source File: GridCacheAbstractSelfTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @param cache Cache.
 * @param key Key.
 * @return {@code True} if cache contains given key.
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
protected boolean containsKey(IgniteCache cache, Object key) throws Exception {
    return cache.containsKey(key);
}