Search in sources :

Example 1 with LongPair

use of org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMap.LongPair in project bookkeeper by apache.

the class ConcurrentLongLongPairHashMapTest method simpleInsertions.

@Test
public void simpleInsertions() {
    ConcurrentLongLongPairHashMap map = new ConcurrentLongLongPairHashMap(16);
    assertTrue(map.isEmpty());
    assertTrue(map.put(1, 1, 11, 11));
    assertFalse(map.isEmpty());
    assertTrue(map.put(2, 2, 22, 22));
    assertTrue(map.put(3, 3, 33, 33));
    assertEquals(map.size(), 3);
    assertEquals(map.get(1, 1), new LongPair(11, 11));
    assertEquals(map.size(), 3);
    assertTrue(map.remove(1, 1));
    assertEquals(map.size(), 2);
    assertEquals(map.get(1, 1), null);
    assertEquals(map.get(5, 5), null);
    assertEquals(map.size(), 2);
    assertTrue(map.put(1, 1, 11, 11));
    assertEquals(map.size(), 3);
    assertTrue(map.put(1, 1, 111, 111));
    assertEquals(map.size(), 3);
}
Also used : LongPair(org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMap.LongPair) Test(org.junit.Test)

Example 2 with LongPair

use of org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMap.LongPair in project bookkeeper by apache.

the class ConcurrentLongLongPairHashMapTest method testPutIfAbsent.

@Test
public void testPutIfAbsent() {
    ConcurrentLongLongPairHashMap map = new ConcurrentLongLongPairHashMap();
    assertTrue(map.putIfAbsent(1, 1, 11, 11));
    assertEquals(map.get(1, 1), new LongPair(11, 11));
    assertFalse(map.putIfAbsent(1, 1, 111, 111));
    assertEquals(map.get(1, 1), new LongPair(11, 11));
}
Also used : LongPair(org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMap.LongPair) Test(org.junit.Test)

Example 3 with LongPair

use of org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMap.LongPair in project bookkeeper by apache.

the class ConcurrentLongLongPairHashMapTest method testAsMap.

@Test
public void testAsMap() {
    ConcurrentLongLongPairHashMap lmap = new ConcurrentLongLongPairHashMap(16, 1);
    lmap.put(1, 1, 11, 11);
    lmap.put(2, 2, 22, 22);
    lmap.put(3, 3, 33, 33);
    Map<LongPair, LongPair> map = Maps.newTreeMap();
    map.put(new LongPair(1, 1), new LongPair(11, 11));
    map.put(new LongPair(2, 2), new LongPair(22, 22));
    map.put(new LongPair(3, 3), new LongPair(33, 33));
    assertEquals(map, lmap.asMap());
}
Also used : LongPair(org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMap.LongPair) Test(org.junit.Test)

Example 4 with LongPair

use of org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMap.LongPair in project bookkeeper by apache.

the class WriteCache method get.

public ByteBuf get(long ledgerId, long entryId) {
    LongPair result = index.get(ledgerId, entryId);
    if (result == null) {
        return null;
    }
    long offset = result.first;
    int size = (int) result.second;
    ByteBuf entry = ByteBufAllocator.DEFAULT.buffer(size, size);
    int localOffset = (int) (offset & segmentOffsetMask);
    int segmentIdx = (int) (offset >>> segmentOffsetBits);
    entry.writeBytes(cacheSegments[segmentIdx], localOffset, size);
    return entry;
}
Also used : LongPair(org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMap.LongPair) ByteBuf(io.netty.buffer.ByteBuf)

Example 5 with LongPair

use of org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMap.LongPair in project bookkeeper by apache.

the class ReadCache method get.

public ByteBuf get(long ledgerId, long entryId) {
    lock.readLock().lock();
    try {
        // We need to check all the segments, starting from the current one and looking
        // backward to minimize the
        // checks for recently inserted entries
        int size = cacheSegments.size();
        for (int i = 0; i < size; i++) {
            int segmentIdx = (currentSegmentIdx + (size - i)) % size;
            LongPair res = cacheIndexes.get(segmentIdx).get(ledgerId, entryId);
            if (res != null) {
                int entryOffset = (int) res.first;
                int entryLen = (int) res.second;
                ByteBuf entry = ByteBufAllocator.DEFAULT.directBuffer(entryLen, entryLen);
                entry.writeBytes(cacheSegments.get(segmentIdx), entryOffset, entryLen);
                return entry;
            }
        }
    } finally {
        lock.readLock().unlock();
    }
    // Entry not found in any segment
    return null;
}
Also used : LongPair(org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMap.LongPair) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

LongPair (org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMap.LongPair)6 Test (org.junit.Test)3 ByteBuf (io.netty.buffer.ByteBuf)2 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)1 MessageIdData (org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData)1