Search in sources :

Example 11 with ConcurrentLongPairSet

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

the class ConcurrentLongPairSetTest method testItems.

@Test
public void testItems() {
    ConcurrentLongPairSet set = new ConcurrentLongPairSet();
    int n = 100;
    int limit = 10;
    for (int i = 0; i < n; i++) {
        set.add(i, i);
    }
    Set<LongPair> items = set.items();
    Set<LongPair> limitItems = set.items(limit);
    assertEquals(items.size(), n);
    assertEquals(limitItems.size(), limit);
    int totalRemovedItems = set.removeIf((first, second) -> limitItems.contains((new LongPair(first, second))));
    assertEquals(limitItems.size(), totalRemovedItems);
    assertEquals(set.size(), n - limit);
}
Also used : ConcurrentLongPairSet(org.apache.pulsar.common.util.collections.ConcurrentLongPairSet) LongPair(org.apache.pulsar.common.util.collections.ConcurrentLongPairSet.LongPair) Test(org.testng.annotations.Test)

Example 12 with ConcurrentLongPairSet

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

the class ConcurrentLongPairSetTest method testRehashingWithDeletes.

@Test
public void testRehashingWithDeletes() {
    int n = 16;
    ConcurrentLongPairSet set = new ConcurrentLongPairSet(n / 2, 1);
    assertEquals(set.capacity(), n);
    assertEquals(set.size(), 0);
    for (int i = 0; i < n / 2; i++) {
        set.add(i, i);
    }
    for (int i = 0; i < n / 2; i++) {
        assertTrue(set.remove(i, i));
    }
    assertEquals(set.capacity(), n);
    assertEquals(set.size(), 0);
    for (int i = n; i < (2 * n); i++) {
        set.add(i, i);
    }
    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 13 with ConcurrentLongPairSet

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

the class ConcurrentLongPairSetTest method testRemove.

@Test
public void testRemove() {
    ConcurrentLongPairSet set = new ConcurrentLongPairSet();
    assertTrue(set.isEmpty());
    assertTrue(set.add(1, 1));
    assertFalse(set.isEmpty());
    assertFalse(set.remove(1, 0));
    assertFalse(set.isEmpty());
    assertTrue(set.remove(1, 1));
    assertTrue(set.isEmpty());
}
Also used : ConcurrentLongPairSet(org.apache.pulsar.common.util.collections.ConcurrentLongPairSet) Test(org.testng.annotations.Test)

Example 14 with ConcurrentLongPairSet

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

the class ConcurrentLongPairSetTest method testIfRemoval.

@Test
public void testIfRemoval() {
    ConcurrentLongPairSet set = new ConcurrentLongPairSet();
    set.add(0, 0);
    set.add(1, 1);
    set.add(3, 3);
    set.add(6, 6);
    set.add(7, 7);
    List<LongPair> values = new ArrayList<>(set.items());
    values.sort(null);
    assertEquals(values, Lists.newArrayList(new LongPair(0, 0), new LongPair(1, 1), new LongPair(3, 3), new LongPair(6, 6), new LongPair(7, 7)));
    int removeItems = set.removeIf((first, second) -> first < 5);
    assertEquals(3, removeItems);
    assertEquals(set.size(), values.size() - 3);
    values = new ArrayList<>(set.items());
    values.sort(null);
    assertEquals(values, Lists.newArrayList(new LongPair(6, 6), new LongPair(7, 7)));
}
Also used : ConcurrentLongPairSet(org.apache.pulsar.common.util.collections.ConcurrentLongPairSet) LongPair(org.apache.pulsar.common.util.collections.ConcurrentLongPairSet.LongPair) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 15 with ConcurrentLongPairSet

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

the class ConcurrentLongPairSetTest method testHashConflictWithDeletion.

@Test
public void testHashConflictWithDeletion() {
    final int Buckets = 16;
    ConcurrentLongPairSet set = new ConcurrentLongPairSet(Buckets, 1);
    // Pick 2 keys that fall into the same bucket
    long key1 = 1;
    long key2 = 538515;
    int bucket1 = ConcurrentLongPairSet.signSafeMod(ConcurrentLongPairSet.hash(key1, key1), Buckets);
    int bucket2 = ConcurrentLongPairSet.signSafeMod(ConcurrentLongPairSet.hash(key2, key2), Buckets);
    assertEquals(bucket1, bucket2);
    assertTrue(set.add(key1, key1));
    assertTrue(set.add(key2, key2));
    assertEquals(set.size(), 2);
    assertTrue(set.remove(key1, key1));
    assertEquals(set.size(), 1);
    assertTrue(set.add(key1, key1));
    assertEquals(set.size(), 2);
    assertTrue(set.remove(key1, key1));
    assertEquals(set.size(), 1);
    assertFalse(set.add(key2, key2));
    assertTrue(set.contains(key2, key2));
    assertEquals(set.size(), 1);
    assertTrue(set.remove(key2, key2));
    assertTrue(set.isEmpty());
}
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