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);
}
}
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();
}
Aggregations