Java Code Examples for javax.cache.processor.MutableEntry#unwrap()

The following examples show how to use javax.cache.processor.MutableEntry#unwrap() . 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: PlatformCacheEntryProcessorImpl.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override public Object process(MutableEntry entry, Object... args)
    throws EntryProcessorException {
    try {
        Ignite ignite = (Ignite)entry.unwrap(Ignite.class);

        PlatformProcessor interopProc;

        try {
            interopProc = PlatformUtils.platformProcessor(ignite);
        }
        catch (IllegalStateException ex) {
            throw new EntryProcessorException(ex);
        }

        interopProc.awaitStart();

        return execute0(interopProc.context(), entry);
    }
    catch (IgniteCheckedException e) {
        throw U.convertException(e);
    }
}
 
Example 2
Source File: EntryProcessorResourceInjectorProxy.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override public T process(MutableEntry<K, V> entry, Object... arguments) throws EntryProcessorException {
    if (!injected) {
        GridCacheContext cctx = entry.unwrap(GridCacheContext.class);

        GridResourceProcessor rsrc = cctx.kernalContext().resource();

        try {
            rsrc.inject(delegate, GridResourceIoc.AnnotationSet.ENTRY_PROCESSOR, cctx.name());
        }
        catch (IgniteCheckedException e) {
            throw new IgniteException(e);
        }

        injected = true;
    }

    return delegate.process(entry, arguments);
}
 
Example 3
Source File: IgniteCacheInvokeAbstractTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override public Integer process(MutableEntry<Integer, Integer> e,
    Object... arguments) throws EntryProcessorException {
    Ignite ignite = e.unwrap(Ignite.class);

    assertNotNull(ignite);

    if (e.exists()) {
        Integer val = e.getValue();

        assertNotNull(val);

        e.setValue(val + 1);

        assertTrue(e.exists());

        assertEquals(val + 1, (int) e.getValue());

        return val;
    }
    else {
        e.setValue(1);

        return -1;
    }
}
 
Example 4
Source File: IgniteCacheCommitDelayTxRecoveryTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public Void process(MutableEntry<Integer, Integer> entry, Object... args) {
    Ignite ignite = entry.unwrap(Ignite.class);

    System.out.println(Thread.currentThread().getName() + " process [node=" + ignite.name() +
        ", commit=" + commit + ", skipFirst=" + skipFirst + ']');

    boolean skip = false;

    if (commit && ignite.name().equals(skipFirst)) {
        skipFirst = null;

        skip = true;
    }

    if (!skip && commit && nodeNames.contains(ignite.name())) {
        try {
            System.out.println(Thread.currentThread().getName() + " start process invoke.");

            assertTrue(commitStartedLatch != null && commitStartedLatch.getCount() > 0);

            commitStartedLatch.countDown();

            assertTrue(commitFinishLatch.await(10, SECONDS));

            System.out.println(Thread.currentThread().getName() + " end process invoke.");
        }
        catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    else
        System.out.println(Thread.currentThread().getName() + " invoke set value.");

    entry.setValue(1);

    return null;
}
 
Example 5
Source File: TransformingCacheProxy.java    From cache2k with Apache License 2.0 5 votes vote down vote up
private MutableEntry<K, V> wrapMutableEntry(final MutableEntry<K0, V0> entry) {
  return new MutableEntry<K, V>() {
    @Override
    public boolean exists() {
      return entry.exists();
    }

    @Override
    public void remove() {
      entry.remove();
    }

    @Override
    public void setValue(V value) {
      entry.setValue(valueTransformer.compact(value));
    }

    @Override
    public K getKey() {
      return keyTransformer.expand(entry.getKey());
    }

    @Override
    public V getValue() {
      return valueTransformer.expand(entry.getValue());
    }

    @Override
    public <T> T unwrap(Class<T> clazz) {
      return entry.unwrap(clazz);
    }
  };
}
 
Example 6
Source File: IgniteTransactionalWriteInvokeBenchmark.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public Object process(MutableEntry<String, Long> entry,
    Object... arguments) throws EntryProcessorException {
    if (entry.getValue() == null) {
        String cacheName = (String)arguments[0];

        IgniteKernal kernal = (IgniteKernal)entry.unwrap(Ignite.class);

        Affinity<String> aff = kernal.affinity(cacheName);

        final int partIdx = aff.partition(entry.getKey());

        final Collection<ClusterNode> nodes = aff.mapKeyToPrimaryAndBackups(entry.getKey());

        List<GridDhtLocalPartition> locPartitions = kernal.cachex(cacheName).context().topology().
            localPartitions();

        GridDhtLocalPartition part = null;

        for (GridDhtLocalPartition p : locPartitions) {
            if (p.id() == partIdx) {
                part = p;

                break;
            }
        }

        kernal.log().warning("Found unexpected null-value, debug info:"
                + "\n    entry=" + entry
                + "\n    key=" + entry.getKey()
                + "\n    locNodeId=" + kernal.cluster().localNode().id()
                + "\n    primaryAndBackupsNodes=" + nodes
                + "\n    part=" + part
                + "\n    partIdx=" + partIdx
                + "\n    locParts=" + locPartitions
                + "\n    allPartMap=" + kernal.cachex(cacheName).context().topology().partitionMap(true)
        );

        return new Object(); // non-null value.
    }

    entry.setValue(entry.getValue() + 1);

    return null;
}
 
Example 7
Source File: GridCacheRebalancingOrderingTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override public Result process(MutableEntry<IntegerKey, Integer> entry, Object... objects) {
    try {
        Ignite ignite = entry.unwrap(Ignite.class);

        PartitionObserver observer = ignite.services().service(PartitionObserver.class.getName());

        assertNotNull(observer);

        IgniteCache<IntegerKey, Integer> cache = ignite.cache(TEST_CACHE_NAME);

        Affinity<IntegerKey> affinity = ignite.affinity(TEST_CACHE_NAME);

        Set<IntegerKey> exp = this.keys;

        Set<IntegerKey> missing = getMissingKeys(cache, exp);

        IntegerKey key = entry.getKey();

        int part = affinity.partition(key);

        String ownership = affinity.isPrimary(ignite.cluster().localNode(), key) ? "primary" : "backup";

        // Wait for the local listener to sync past events.
        if (!observer.getIgniteLocalSyncListener().isSynced()) {
            ignite.log().info("Retrying validation for " + ownership + " partition " + part
                    + " due to initial sync");

            return Result.RETRY;
        }

        // Determine if the partition is being loaded and wait for it to load completely.
        if (observer.getLoadingMap().containsKey(part)) {
            ignite.log().info("Retrying validation due to forming partition [ownership=" + ownership +
                ", partition=" + part +
                ", expKeys=" + exp +
                ", loadedKeys=" + observer.getLoadingMap().get(part) +
                ", missingLocalKeys=" + missing + ']');

            return Result.RETRY;
        }

        if (!observer.getPartitionMap().containsKey(part)) {
            ignite.log().info("Retrying validation due to newly arrived partition [ownership=" + ownership +
                ", partition=" + part +
                ", missingLocalKeys=" + missing + ']');

            return Result.RETRY;
        }

        // Validate the key count.
        Set<IntegerKey> curr = observer.ensureKeySet(part);

        if (curr.equals(exp) && missing.isEmpty())
            return Result.OK;

        String msg = String.format("For %s partition %s:\n\texpected  %s,\n\t" +
            "but found %s;\n\tmissing local keys: %s",
            ownership, part, new TreeSet<>(exp), new TreeSet<>(curr), new TreeSet<>(missing));

        ignite.log().info(">>> " + msg);

        throw new EntryProcessorException(msg);
    }
    catch (NullPointerException e) {
        e.printStackTrace();

        throw e;
    }
}