Search in sources :

Example 1 with IntSets

use of org.infinispan.commons.util.IntSets in project infinispan by infinispan.

the class SimpleClusterPublisherManagerTest method insert.

private Map<Integer, String> insert(Cache<Integer, String> cache) {
    int amount = 12;
    Map<Integer, String> values = new HashMap<>(amount);
    Map<Integer, IntSet> keysBySegment = log.isTraceEnabled() ? new HashMap<>() : null;
    KeyPartitioner kp = TestingUtil.extractComponent(cache, KeyPartitioner.class);
    IntStream.range(0, amount).forEach(i -> {
        values.put(i, "value-" + i);
        if (keysBySegment != null) {
            int segment = kp.getSegment(i);
            IntSet keys = keysBySegment.computeIfAbsent(segment, IntSets::mutableEmptySet);
            keys.set(i);
        }
    });
    if (keysBySegment != null) {
        log.tracef("Keys by segment are: " + keysBySegment);
    }
    cache.putAll(values);
    return values;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) HashMap(java.util.HashMap) IntSets(org.infinispan.commons.util.IntSets) IntSet(org.infinispan.commons.util.IntSet)

Example 2 with IntSets

use of org.infinispan.commons.util.IntSets in project infinispan by infinispan.

the class BaseStreamTest method testObjFlatMapIterator.

public void testObjFlatMapIterator() {
    Cache<Integer, String> cache = getCache(0);
    int range = 10;
    Map<Integer, IntSet> keysBySegment = log.isTraceEnabled() ? new TreeMap<>() : null;
    KeyPartitioner kp = TestingUtil.extractComponent(cache, KeyPartitioner.class);
    // First populate the cache with a bunch of values
    IntStream.range(0, range).boxed().forEach(i -> {
        if (keysBySegment != null) {
            int segment = kp.getSegment(i);
            IntSet keys = keysBySegment.computeIfAbsent(segment, IntSets::mutableEmptySet);
            keys.set(i);
        }
        cache.put(i, i + "-value" + i);
    });
    if (keysBySegment != null) {
        log.tracef("Keys by segment are: " + keysBySegment);
    }
    CacheSet<Map.Entry<Integer, String>> entrySet = cache.entrySet();
    StringJoiner stringJoiner = new StringJoiner(" ");
    // Rxjava requets 128 by default for many operations - thus we have a number larger than that
    int explosionCount = 293;
    for (int i = 0; i < explosionCount; ++i) {
        stringJoiner.add("special-" + String.valueOf(i));
    }
    String specialString = stringJoiner.toString();
    Iterator<String> iterator = createStream(entrySet).distributedBatchSize(1).flatMap(e -> {
        if (e.getKey() == 2) {
            // Make sure to test an empty stream as well
            return Stream.empty();
        }
        if (e.getKey() == 5) {
            // Make sure we also test a very large resulting stream without the key in it
            return Arrays.stream(specialString.split(" "));
        }
        return Arrays.stream(e.getValue().split("a"));
    }).iterator();
    List<String> list = new ArrayList<>(range * 2);
    iterator.forEachRemaining(list::add);
    if (keysBySegment != null) {
        log.tracef("Returned values are: %s", list);
    }
    assertEquals((range - 2) * 2 + explosionCount, list.size());
}
Also used : Arrays(java.util.Arrays) CacheStream(org.infinispan.CacheStream) IntSets(org.infinispan.commons.util.IntSets) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) Spliterators(java.util.Spliterators) ObjectOutput(java.io.ObjectOutput) IntConsumer(java.util.function.IntConsumer) Test(org.testng.annotations.Test) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) Cache(org.infinispan.Cache) AssertJUnit.assertTrue(org.testng.AssertJUnit.assertTrue) DoubleConsumer(java.util.function.DoubleConsumer) SerializableToIntFunction(org.infinispan.util.function.SerializableToIntFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TestingUtil(org.infinispan.test.TestingUtil) SerializableToLongFunction(org.infinispan.util.function.SerializableToLongFunction) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CacheSet(org.infinispan.CacheSet) Collectors(java.util.stream.Collectors) DoubleStream(java.util.stream.DoubleStream) IntSet(org.infinispan.commons.util.IntSet) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) ObjectInput(java.io.ObjectInput) Queue(java.util.Queue) SerializeWith(org.infinispan.commons.marshall.SerializeWith) Spliterator(java.util.Spliterator) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) IntStream(java.util.stream.IntStream) Supplier(java.util.function.Supplier) ImmortalCacheEntry(org.infinispan.container.entries.ImmortalCacheEntry) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) SerializableToDoubleFunction(org.infinispan.util.function.SerializableToDoubleFunction) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) CacheCollection(org.infinispan.CacheCollection) StreamSupport(java.util.stream.StreamSupport) LongSummaryStatistics(java.util.LongSummaryStatistics) LongStream(java.util.stream.LongStream) Iterator(java.util.Iterator) MultipleCacheManagersTest(org.infinispan.test.MultipleCacheManagersTest) ToIntFunction(java.util.function.ToIntFunction) TransactionMode(org.infinispan.transaction.TransactionMode) AssertJUnit.assertFalse(org.testng.AssertJUnit.assertFalse) IOException(java.io.IOException) AssertJUnit.fail(org.testng.AssertJUnit.fail) LongConsumer(java.util.function.LongConsumer) PrimitiveIterator(java.util.PrimitiveIterator) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) TreeMap(java.util.TreeMap) StringJoiner(java.util.StringJoiner) TestCacheManagerFactory(org.infinispan.test.fwk.TestCacheManagerFactory) IntSummaryStatistics(java.util.IntSummaryStatistics) AssertJUnit.assertEquals(org.testng.AssertJUnit.assertEquals) IntSets(org.infinispan.commons.util.IntSets) IntSet(org.infinispan.commons.util.IntSet) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ImmortalCacheEntry(org.infinispan.container.entries.ImmortalCacheEntry) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) StringJoiner(java.util.StringJoiner)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IntSet (org.infinispan.commons.util.IntSet)2 IntSets (org.infinispan.commons.util.IntSets)2 KeyPartitioner (org.infinispan.distribution.ch.KeyPartitioner)2 IOException (java.io.IOException)1 ObjectInput (java.io.ObjectInput)1 ObjectOutput (java.io.ObjectOutput)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 DoubleSummaryStatistics (java.util.DoubleSummaryStatistics)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 IntSummaryStatistics (java.util.IntSummaryStatistics)1 Iterator (java.util.Iterator)1 List (java.util.List)1 LongSummaryStatistics (java.util.LongSummaryStatistics)1 Map (java.util.Map)1 Optional (java.util.Optional)1 PrimitiveIterator (java.util.PrimitiveIterator)1 Queue (java.util.Queue)1