Search in sources :

Example 6 with Pointer

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);
}
Also used : Pointer(com.cetsoft.imcache.offheap.bytebuffer.Pointer) BufferOverflowException(java.nio.BufferOverflowException) Test(org.junit.Test)

Example 7 with Pointer

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);
}
Also used : Pointer(com.cetsoft.imcache.offheap.bytebuffer.Pointer) Test(org.junit.Test)

Example 8 with Pointer

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);
}
Also used : Pointer(com.cetsoft.imcache.offheap.bytebuffer.Pointer) Test(org.junit.Test)

Example 9 with Pointer

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());
}
Also used : Pointer(com.cetsoft.imcache.offheap.bytebuffer.Pointer) Test(org.junit.Test)

Example 10 with Pointer

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

Aggregations

Pointer (com.cetsoft.imcache.offheap.bytebuffer.Pointer)13 Test (org.junit.Test)9 BufferOverflowException (java.nio.BufferOverflowException)2 OffHeapByteBuffer (com.cetsoft.imcache.offheap.bytebuffer.OffHeapByteBuffer)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