use of com.cetsoft.imcache.offheap.bytebuffer.Pointer in project imcache by Cetsoft.
the class OffHeapByteBufferStoreTest method storeBufferOverFlow.
/**
* Store buffer over flow.
*/
@Test
public void storeBufferOverFlow() {
int size = 100;
byte[] expectedBytes = new byte[size];
random.nextBytes(expectedBytes);
doReturn(buffer).when(bufferStore).currentBuffer();
doThrow(new BufferOverflowException()).doReturn(pointer).when(buffer).store(expectedBytes);
Pointer actualPointer = bufferStore.store(expectedBytes);
assertEquals(pointer, actualPointer);
verify(bufferStore, times(2)).currentBuffer();
verify(buffer, times(2)).store(expectedBytes);
}
use of com.cetsoft.imcache.offheap.bytebuffer.Pointer in project imcache by Cetsoft.
the class OffHeapByteBufferTest method store.
/**
* Store.
*/
@Test
public void store() {
int size = 100;
byte[] expectedBytes = new byte[size];
random.nextBytes(expectedBytes);
Pointer pointer = buffer.store(expectedBytes);
byte[] actualBytes = buffer.retrieve(pointer);
assertArrayEquals(expectedBytes, actualBytes);
}
use of com.cetsoft.imcache.offheap.bytebuffer.Pointer in project imcache by Cetsoft.
the class OffHeapByteBufferTest method remove.
/**
* Removes the.
*/
@Test
public void remove() {
int size = 100;
byte[] expectedBytes = new byte[size];
random.nextBytes(expectedBytes);
Pointer pointer = buffer.store(expectedBytes);
byte[] actualBytes = buffer.remove(pointer);
assertArrayEquals(expectedBytes, actualBytes);
}
use of com.cetsoft.imcache.offheap.bytebuffer.Pointer in project imcache by Cetsoft.
the class PointerTest method copy.
@Test
public void copy() {
int position = 100;
long accessTime = 1212;
Pointer pointer = new Pointer(0, 0, null);
Pointer pointerToCopy = new Pointer(0, 0, null);
pointerToCopy.setAccessTime(accessTime);
pointerToCopy.setPosition(position);
pointerToCopy.setOffHeapByteBuffer(offHeapByteBuffer);
pointer.copy(pointerToCopy);
assertEquals(position, pointer.getPosition());
assertEquals(accessTime, pointer.getAccessTime());
assertEquals(offHeapByteBuffer, pointer.getOffHeapByteBuffer());
}
use of com.cetsoft.imcache.offheap.bytebuffer.Pointer 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);
}
}
Aggregations