Search in sources :

Example 6 with ConcurrentLongPairSet

use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet in project incubator-pulsar by apache.

the class ConcurrentLongPairSetTest method concurrentInsertions.

@Test
public void concurrentInsertions() throws Throwable {
    ConcurrentLongPairSet set = new ConcurrentLongPairSet();
    ExecutorService executor = Executors.newCachedThreadPool();
    final int nThreads = 16;
    final int N = 100_000;
    List<Future<?>> futures = new ArrayList<>();
    for (int i = 0; i < nThreads; i++) {
        final int threadIdx = i;
        futures.add(executor.submit(() -> {
            Random random = new Random();
            for (int j = 0; j < N; j++) {
                long key = random.nextLong();
                // Ensure keys are unique
                key -= key % (threadIdx + 1);
                key = Math.abs(key);
                set.add(key, key);
            }
        }));
    }
    for (Future<?> future : futures) {
        future.get();
    }
    assertEquals(set.size(), N * nThreads);
    executor.shutdown();
}
Also used : Random(java.util.Random) ConcurrentLongPairSet(org.apache.pulsar.common.util.collections.ConcurrentLongPairSet) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Test(org.testng.annotations.Test)

Example 7 with ConcurrentLongPairSet

use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet in project incubator-pulsar by apache.

the class ConcurrentLongPairSetTest method simpleInsertions.

@Test
public void simpleInsertions() {
    ConcurrentLongPairSet set = new ConcurrentLongPairSet(16);
    assertTrue(set.isEmpty());
    assertTrue(set.add(1, 1));
    assertFalse(set.isEmpty());
    assertTrue(set.add(2, 2));
    assertTrue(set.add(3, 3));
    assertEquals(set.size(), 3);
    assertTrue(set.contains(1, 1));
    assertEquals(set.size(), 3);
    assertTrue(set.remove(1, 1));
    assertEquals(set.size(), 2);
    assertFalse(set.contains(1, 1));
    assertFalse(set.contains(5, 5));
    assertEquals(set.size(), 2);
    assertTrue(set.add(1, 1));
    assertEquals(set.size(), 3);
    assertFalse(set.add(1, 1));
    assertEquals(set.size(), 3);
}
Also used : ConcurrentLongPairSet(org.apache.pulsar.common.util.collections.ConcurrentLongPairSet) Test(org.testng.annotations.Test)

Example 8 with ConcurrentLongPairSet

use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet in project incubator-pulsar by apache.

the class ConcurrentLongPairSetTest method testRehashing.

@Test
public void testRehashing() {
    int n = 16;
    ConcurrentLongPairSet set = new ConcurrentLongPairSet(n / 2, 1);
    assertEquals(set.capacity(), n);
    assertEquals(set.size(), 0);
    for (int i = 0; i < n; i++) {
        set.add(i, 1);
    }
    assertEquals(set.capacity(), 2 * n);
    assertEquals(set.size(), n);
}
Also used : ConcurrentLongPairSet(org.apache.pulsar.common.util.collections.ConcurrentLongPairSet) Test(org.testng.annotations.Test)

Example 9 with ConcurrentLongPairSet

use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet in project incubator-pulsar by apache.

the class ConcurrentLongPairSetTest method concurrentInsertionsAndReads.

@Test
public void concurrentInsertionsAndReads() throws Throwable {
    ConcurrentLongPairSet map = new ConcurrentLongPairSet();
    ExecutorService executor = Executors.newCachedThreadPool();
    final int nThreads = 16;
    final int N = 100_000;
    List<Future<?>> futures = new ArrayList<>();
    for (int i = 0; i < nThreads; i++) {
        final int threadIdx = i;
        futures.add(executor.submit(() -> {
            Random random = new Random();
            for (int j = 0; j < N; j++) {
                long key = random.nextLong();
                // Ensure keys are unique
                key -= key % (threadIdx + 1);
                key = Math.abs(key);
                map.add(key, key);
            }
        }));
    }
    for (Future<?> future : futures) {
        future.get();
    }
    assertEquals(map.size(), N * nThreads);
    executor.shutdown();
}
Also used : Random(java.util.Random) ConcurrentLongPairSet(org.apache.pulsar.common.util.collections.ConcurrentLongPairSet) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Test(org.testng.annotations.Test)

Example 10 with ConcurrentLongPairSet

use of org.apache.pulsar.common.util.collections.ConcurrentLongPairSet in project incubator-pulsar by apache.

the class ConcurrentLongPairSetTest method testToString.

@Test
public void testToString() {
    ConcurrentLongPairSet set = new ConcurrentLongPairSet();
    set.add(0, 0);
    set.add(1, 1);
    set.add(3, 3);
    final String toString = "{[3:3], [0:0], [1:1]}";
    assertEquals(set.toString(), toString);
}
Also used : ConcurrentLongPairSet(org.apache.pulsar.common.util.collections.ConcurrentLongPairSet) Test(org.testng.annotations.Test)

Aggregations

ConcurrentLongPairSet (org.apache.pulsar.common.util.collections.ConcurrentLongPairSet)15 Test (org.testng.annotations.Test)15 ArrayList (java.util.ArrayList)5 LongPair (org.apache.pulsar.common.util.collections.ConcurrentLongPairSet.LongPair)5 Random (java.util.Random)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 Field (java.lang.reflect.Field)1 HashSet (java.util.HashSet)1 PersistentDispatcherMultipleConsumers (org.apache.pulsar.broker.service.persistent.PersistentDispatcherMultipleConsumers)1 PersistentSubscription (org.apache.pulsar.broker.service.persistent.PersistentSubscription)1 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)1 MessageIdImpl (org.apache.pulsar.client.impl.MessageIdImpl)1