Search in sources :

Example 1 with FunctionalMapImpl

use of org.infinispan.functional.impl.FunctionalMapImpl in project bucket4j-spring-boot-starter by MarcGiffing.

the class InfinispanJCacheCacheResolver method resolve.

public ProxyManager<String> resolve(String cacheName) {
    Cache<Object, Object> cache = cacheContainer.getCache(cacheName);
    if (cache == null) {
        throw new JCacheNotFoundException(cacheName);
    }
    FunctionalMapImpl functionalMap = FunctionalMapImpl.create(cache.getAdvancedCache());
    return new InfinispanProxyManager<>(ReadWriteMapImpl.create(functionalMap));
}
Also used : FunctionalMapImpl(org.infinispan.functional.impl.FunctionalMapImpl) JCacheNotFoundException(com.giffing.bucket4j.spring.boot.starter.exception.JCacheNotFoundException) InfinispanProxyManager(io.github.bucket4j.grid.infinispan.InfinispanProxyManager)

Example 2 with FunctionalMapImpl

use of org.infinispan.functional.impl.FunctionalMapImpl in project infinispan by infinispan.

the class CacheOperationsTest method testFunctional.

private void testFunctional(String site) throws Exception {
    FunctionalMapImpl<Object, Object> fmap = FunctionalMapImpl.create(cache(site, 0).getAdvancedCache());
    WriteOnlyMap<Object, Object> wo = WriteOnlyMapImpl.create(fmap);
    ReadWriteMap<Object, Object> rw = ReadWriteMapImpl.create(fmap);
    Cache<Object, Object> backup = backup(site);
    Object[] keys = { new MagicKey("k0", cache(site, 0), cache(site, 1)), new MagicKey("k1", cache(site, 1), cache(site, 0)), new MagicKey("k2", cache(site, 1), cache(site, 2)) };
    for (Object key : keys) {
        wo.eval(key, "v0", MarshallableFunctions.setValueConsumer()).join();
        assertEquals("v0", backup.get(key));
    }
    for (Object key : keys) {
        wo.eval(key, MarshallableFunctions.removeConsumer()).join();
        assertEquals(null, backup.get(key));
    }
    wo.evalMany(map(keys, "v1"), MarshallableFunctions.setValueConsumer()).join();
    for (Object key : keys) {
        assertEquals("v1", backup.get(key));
    }
    for (Object key : keys) {
        rw.eval(key, view -> view.set(view.get() + "+2")).join();
        assertEquals("v1+2", backup.get(key));
    }
    rw.evalMany(Util.asSet(keys), view -> view.set(view.get() + "+3")).forEach(ret -> assertEquals(null, ret));
    for (Object key : keys) {
        assertEquals("v1+2+3", backup.get(key));
    }
    wo.evalMany(Util.asSet(keys), MarshallableFunctions.removeConsumer()).join();
    for (Object key : keys) {
        assertEquals(null, backup.get(key));
    }
    rw.evalMany(Util.asSet(keys), view -> view.find().orElse("none")).forEach(ret -> assertEquals("none", ret));
    for (Object key : keys) {
        assertEquals(null, backup.get(key));
    }
    if (transactional) {
        TransactionManager tm = cache(site, 0).getAdvancedCache().getTransactionManager();
        tm.begin();
        rw.eval(keys[0], "v4", MarshallableFunctions.setValueReturnPrevOrNull()).join();
        // read-only evalMany
        rw.evalMany(Util.asSet(keys[1], keys[2]), view -> view.find().orElse("none")).forEach(ret -> assertEquals("none", ret));
        tm.commit();
        assertEquals("v4", backup.get(keys[0]));
        assertEquals(null, backup.get(keys[1]));
        assertEquals(null, backup.get(keys[2]));
    }
}
Also used : MagicKey(org.infinispan.distribution.MagicKey) Factory(org.testng.annotations.Factory) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) Util(org.infinispan.commons.util.Util) ReadWriteMapImpl(org.infinispan.functional.impl.ReadWriteMapImpl) HashMap(java.util.HashMap) LockingMode(org.infinispan.transaction.LockingMode) Test(org.testng.annotations.Test) MarshallableFunctions(org.infinispan.marshall.core.MarshallableFunctions) Cache(org.infinispan.Cache) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) FunctionalMapImpl(org.infinispan.functional.impl.FunctionalMapImpl) CacheMode(org.infinispan.configuration.cache.CacheMode) Stream(java.util.stream.Stream) ReadWriteMap(org.infinispan.functional.FunctionalMap.ReadWriteMap) AssertJUnit.assertNull(org.testng.AssertJUnit.assertNull) Map(java.util.Map) AssertJUnit.assertEquals(org.testng.AssertJUnit.assertEquals) TransactionManager(javax.transaction.TransactionManager) WriteOnlyMap(org.infinispan.functional.FunctionalMap.WriteOnlyMap) WriteOnlyMapImpl(org.infinispan.functional.impl.WriteOnlyMapImpl) TransactionManager(javax.transaction.TransactionManager) MagicKey(org.infinispan.distribution.MagicKey)

Aggregations

FunctionalMapImpl (org.infinispan.functional.impl.FunctionalMapImpl)2 JCacheNotFoundException (com.giffing.bucket4j.spring.boot.starter.exception.JCacheNotFoundException)1 InfinispanProxyManager (io.github.bucket4j.grid.infinispan.InfinispanProxyManager)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 TransactionManager (javax.transaction.TransactionManager)1 Cache (org.infinispan.Cache)1 Util (org.infinispan.commons.util.Util)1 CacheMode (org.infinispan.configuration.cache.CacheMode)1 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)1 MagicKey (org.infinispan.distribution.MagicKey)1 ReadWriteMap (org.infinispan.functional.FunctionalMap.ReadWriteMap)1 WriteOnlyMap (org.infinispan.functional.FunctionalMap.WriteOnlyMap)1 ReadWriteMapImpl (org.infinispan.functional.impl.ReadWriteMapImpl)1 WriteOnlyMapImpl (org.infinispan.functional.impl.WriteOnlyMapImpl)1 MarshallableFunctions (org.infinispan.marshall.core.MarshallableFunctions)1 LockingMode (org.infinispan.transaction.LockingMode)1