Search in sources :

Example 1 with PutIfAbsent

use of org.infinispan.jcache.embedded.functions.PutIfAbsent in project infinispan by infinispan.

the class JCache method loadAllFromJCacheLoader.

private void loadAllFromJCacheLoader(Set<? extends K> keys, boolean replaceExistingValues, CompletionListener listener) {
    AtomicInteger countDown = new AtomicInteger(1);
    BiConsumer<Object, Throwable> completionAction = (nil, t) -> {
        if (t != null) {
            if (countDown.getAndSet(0) != 0) {
                setListenerException(listener, t);
            }
            return;
        }
        if (countDown.decrementAndGet() == 0) {
            setListenerCompletion(listener);
        }
    };
    try {
        Map<K, V> loaded = loadAllKeys(keys);
        for (Map.Entry<K, V> entry : loaded.entrySet()) {
            K loadedKey = entry.getKey();
            V loadedValue = entry.getValue();
            if (loadedValue == null) {
                continue;
            }
            countDown.incrementAndGet();
            if (replaceExistingValues) {
                rwMap.withParams(Param.PersistenceMode.SKIP).eval(loadedKey, loadedValue, new Put<>()).whenComplete(completionAction);
            } else {
                rwMap.withParams(Param.PersistenceMode.SKIP).eval(loadedKey, loadedValue, new PutIfAbsent<>()).whenComplete(completionAction);
            }
        }
        completionAction.accept(null, null);
    } catch (Throwable t) {
        completionAction.accept(null, t);
    }
}
Also used : Remove(org.infinispan.jcache.embedded.functions.Remove) LogFactory(org.infinispan.util.logging.LogFactory) Replace(org.infinispan.jcache.embedded.functions.Replace) Put(org.infinispan.jcache.embedded.functions.Put) EntryProcessor(javax.cache.processor.EntryProcessor) EntryView(org.infinispan.functional.EntryView) FailureEntryProcessorResult(org.infinispan.jcache.FailureEntryProcessorResult) AdvancedCache(org.infinispan.AdvancedCache) CacheEntryListenerConfiguration(javax.cache.configuration.CacheEntryListenerConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JCacheEntry(org.infinispan.jcache.JCacheEntry) Map(java.util.Map) Cache(javax.cache.Cache) MutableConfiguration(javax.cache.configuration.MutableConfiguration) AbstractJCacheListenerAdapter(org.infinispan.jcache.AbstractJCacheListenerAdapter) InfinispanCollections(org.infinispan.commons.util.InfinispanCollections) RemoveConditionally(org.infinispan.jcache.embedded.functions.RemoveConditionally) PersistenceManagerImpl(org.infinispan.persistence.manager.PersistenceManagerImpl) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) AbstractJCache(org.infinispan.jcache.AbstractJCache) Collectors(java.util.stream.Collectors) FunctionalMapImpl(org.infinispan.functional.impl.FunctionalMapImpl) CacheStatisticsMXBean(javax.cache.management.CacheStatisticsMXBean) List(java.util.List) Flag(org.infinispan.context.Flag) Param(org.infinispan.functional.Param) GetAndPut(org.infinispan.jcache.embedded.functions.GetAndPut) CacheManager(javax.cache.CacheManager) GetAndReplace(org.infinispan.jcache.embedded.functions.GetAndReplace) CacheJmxRegistration(org.infinispan.jmx.CacheJmxRegistration) PersistenceManager(org.infinispan.persistence.manager.PersistenceManager) ExpirationConfiguration(org.infinispan.configuration.cache.ExpirationConfiguration) PutIfAbsent(org.infinispan.jcache.embedded.functions.PutIfAbsent) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionStages(org.infinispan.util.concurrent.CompletionStages) Function(java.util.function.Function) CacheLoader(javax.cache.integration.CacheLoader) SuccessEntryProcessorResult(org.infinispan.jcache.SuccessEntryProcessorResult) Invoke(org.infinispan.jcache.embedded.functions.Invoke) MBeanServer(javax.management.MBeanServer) BiConsumer(java.util.function.BiConsumer) Log(org.infinispan.jcache.embedded.logging.Log) NoSuchElementException(java.util.NoSuchElementException) SimpleEntry(java.util.AbstractMap.SimpleEntry) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Iterator(java.util.Iterator) CompletionListener(javax.cache.integration.CompletionListener) ReadWriteMapImpl(org.infinispan.functional.impl.ReadWriteMapImpl) Exceptions(org.infinispan.jcache.Exceptions) ReplaceConditionally(org.infinispan.jcache.embedded.functions.ReplaceConditionally) ReflectionUtil(org.infinispan.commons.util.ReflectionUtil) ReadWriteMap(org.infinispan.functional.FunctionalMap.ReadWriteMap) Configuration(org.infinispan.configuration.cache.Configuration) ReadWithExpiry(org.infinispan.jcache.embedded.functions.ReadWithExpiry) CacheWriter(javax.cache.integration.CacheWriter) Collections(java.util.Collections) GetAndRemove(org.infinispan.jcache.embedded.functions.GetAndRemove) AggregateCompletionStage(org.infinispan.util.concurrent.AggregateCompletionStage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PutIfAbsent(org.infinispan.jcache.embedded.functions.PutIfAbsent) Map(java.util.Map) HashMap(java.util.HashMap) ReadWriteMap(org.infinispan.functional.FunctionalMap.ReadWriteMap) Put(org.infinispan.jcache.embedded.functions.Put) GetAndPut(org.infinispan.jcache.embedded.functions.GetAndPut)

Example 2 with PutIfAbsent

use of org.infinispan.jcache.embedded.functions.PutIfAbsent in project infinispan by infinispan.

the class LifecycleCallbacks method cacheManagerStarting.

@Override
public void cacheManagerStarting(GlobalComponentRegistry gcr, GlobalConfiguration globalConfiguration) {
    SerializationContextRegistry ctxRegistry = gcr.getComponent(SerializationContextRegistry.class);
    PersistenceContextInitializerImpl sci = new PersistenceContextInitializerImpl();
    ctxRegistry.addContextInitializer(SerializationContextRegistry.MarshallerType.PERSISTENCE, sci);
    ctxRegistry.addContextInitializer(SerializationContextRegistry.MarshallerType.GLOBAL, sci);
    Map<Integer, AdvancedExternalizer<?>> map = globalConfiguration.serialization().advancedExternalizers();
    add(map, new SuppliedExternalizer<>(ExternalizerIds.READ_WITH_EXPIRY, ReadWithExpiry.class, ReadWithExpiry::new));
    add(map, new SuppliedExternalizer<>(ExternalizerIds.GET_AND_PUT, GetAndPut.class, GetAndPut::new));
    add(map, new SuppliedExternalizer<>(ExternalizerIds.GET_AND_REPLACE, GetAndReplace.class, GetAndReplace::new));
    add(map, new Invoke.Externalizer());
    add(map, new SuppliedExternalizer<>(ExternalizerIds.PUT, Put.class, Put::new));
    add(map, new SuppliedExternalizer<>(ExternalizerIds.PUT_IF_ABSENT, PutIfAbsent.class, PutIfAbsent::new));
    add(map, new SingletonExternalizer<>(ExternalizerIds.REMOVE, Remove.getInstance()));
    add(map, new SuppliedExternalizer<>(ExternalizerIds.REMOVE_CONDITIONALLY, RemoveConditionally.class, RemoveConditionally::new));
    add(map, new SuppliedExternalizer<>(ExternalizerIds.REPLACE, Replace.class, Replace::new));
    add(map, new SingletonExternalizer<>(ExternalizerIds.GET_AND_REMOVE, GetAndRemove.getInstance()));
    add(map, new ReplaceConditionally.Externalizer());
    // that cannot be classloaded.
    if (canLoad("javax.cache.processor.MutableEntry", MutableEntrySnapshot.Externalizer.class.getClassLoader())) {
        add(map, new MutableEntrySnapshot.Externalizer());
    }
    gcr.getCacheManager().getClassAllowList().addClasses(DefaultCacheKey.class);
}
Also used : ReplaceConditionally(org.infinispan.jcache.embedded.functions.ReplaceConditionally) Replace(org.infinispan.jcache.embedded.functions.Replace) GetAndReplace(org.infinispan.jcache.embedded.functions.GetAndReplace) GetAndPut(org.infinispan.jcache.embedded.functions.GetAndPut) RemoveConditionally(org.infinispan.jcache.embedded.functions.RemoveConditionally) MutableEntrySnapshot(org.infinispan.jcache.embedded.functions.MutableEntrySnapshot) PutIfAbsent(org.infinispan.jcache.embedded.functions.PutIfAbsent) ReadWithExpiry(org.infinispan.jcache.embedded.functions.ReadWithExpiry) Put(org.infinispan.jcache.embedded.functions.Put) GetAndPut(org.infinispan.jcache.embedded.functions.GetAndPut) Invoke(org.infinispan.jcache.embedded.functions.Invoke) SingletonExternalizer(org.infinispan.commons.marshall.SingletonExternalizer) AdvancedExternalizer(org.infinispan.commons.marshall.AdvancedExternalizer) SuppliedExternalizer(org.infinispan.commons.marshall.SuppliedExternalizer) SerializationContextRegistry(org.infinispan.marshall.protostream.impl.SerializationContextRegistry) GetAndReplace(org.infinispan.jcache.embedded.functions.GetAndReplace) AdvancedExternalizer(org.infinispan.commons.marshall.AdvancedExternalizer)

Aggregations

GetAndPut (org.infinispan.jcache.embedded.functions.GetAndPut)2 GetAndReplace (org.infinispan.jcache.embedded.functions.GetAndReplace)2 Invoke (org.infinispan.jcache.embedded.functions.Invoke)2 Put (org.infinispan.jcache.embedded.functions.Put)2 PutIfAbsent (org.infinispan.jcache.embedded.functions.PutIfAbsent)2 ReadWithExpiry (org.infinispan.jcache.embedded.functions.ReadWithExpiry)2 RemoveConditionally (org.infinispan.jcache.embedded.functions.RemoveConditionally)2 Replace (org.infinispan.jcache.embedded.functions.Replace)2 ReplaceConditionally (org.infinispan.jcache.embedded.functions.ReplaceConditionally)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1