Search in sources :

Example 1 with OffHeapByteBuffer

use of com.cetsoft.imcache.offheap.bytebuffer.OffHeapByteBuffer in project imcache by Cetsoft.

the class OffHeapCache method cleanBuffers.

/**
     * Clean buffers.
     *
     * @param bufferCleanerThreshold the buffer cleaner threshold
     */
protected void cleanBuffers(final float bufferCleanerThreshold) {
    // Buffers can be fully dirty and we may not find any pointer to resolve that.
    // This case is so unlikely that we did not consider.
    Collection<Pointer> pointers = pointerMap.values();
    List<Pointer> pointersToBeRedistributed = new ArrayList<Pointer>();
    Map<OffHeapByteBuffer, Float> buffers = new HashMap<OffHeapByteBuffer, Float>();
    Set<Integer> buffersToBeCleaned = new HashSet<Integer>();
    // For all pointers we try to understand if there is a need for redistribution.
    for (Pointer pointer : pointers) {
        Float ratio = buffers.get(pointer.getOffHeapByteBuffer());
        if (ratio == null) {
            // calculate the ratio of the dirty
            ratio = getDirtyRatio(pointer);
            buffers.put(pointer.getOffHeapByteBuffer(), ratio);
        }
        if (ratio - bufferCleanerThreshold > DELTA) {
            pointersToBeRedistributed.add(pointer);
            buffersToBeCleaned.add(pointer.getOffHeapByteBuffer().getIndex());
        }
    }
    bufferStore.redistribute(pointersToBeRedistributed);
    for (Integer bufferIndex : buffersToBeCleaned) {
        bufferStore.free(bufferIndex);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) Pointer(com.cetsoft.imcache.offheap.bytebuffer.Pointer) OffHeapByteBuffer(com.cetsoft.imcache.offheap.bytebuffer.OffHeapByteBuffer) HashSet(java.util.HashSet)

Example 2 with OffHeapByteBuffer

use of com.cetsoft.imcache.offheap.bytebuffer.OffHeapByteBuffer in project imcache by Cetsoft.

the class OffHeapByteBufferStoreTest method storeWithBuffer.

/**
     * Store with buffer.
     */
@Test
public void storeWithBuffer() {
    int size = 100;
    byte[] bytes = new byte[size];
    random.nextBytes(bytes);
    doReturn(pointer).when(bufferStore).store(bytes);
    doReturn(buffer).doReturn(new OffHeapByteBuffer(0, 10)).when(bufferStore).currentBuffer();
    doNothing().when(bufferStore).nextBuffer();
    bufferStore.store(bytes, buffer);
    verify(bufferStore).store(bytes);
    verify(bufferStore).nextBuffer();
}
Also used : OffHeapByteBuffer(com.cetsoft.imcache.offheap.bytebuffer.OffHeapByteBuffer) Test(org.junit.Test)

Aggregations

OffHeapByteBuffer (com.cetsoft.imcache.offheap.bytebuffer.OffHeapByteBuffer)2 Pointer (com.cetsoft.imcache.offheap.bytebuffer.Pointer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1