Search in sources :

Example 1 with Pointer

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

the class OffHeapCache method get.

/*
     * (non-Javadoc)
     * 
     * @see com.cetsoft.imcache.cache.Cache#get(java.lang.Object)
     */
public V get(K key) {
    Pointer pointer = pointerMap.get(key);
    if (pointer != null) {
        readLock(key);
        try {
            hit.incrementAndGet();
            synchronized (pointer) {
                byte[] payload = bufferStore.retrieve(pointer);
                return serializer.deserialize(payload);
            }
        } finally {
            readUnlock(key);
        }
    } else {
        miss.incrementAndGet();
        V value = cacheLoader.load(key);
        if (value != null) {
            put(key, value);
        }
        return value;
    }
}
Also used : Pointer(com.cetsoft.imcache.offheap.bytebuffer.Pointer)

Example 2 with Pointer

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

the class OffHeapCache method put.

/*
     * (non-Javadoc)
     * 
     * @see com.cetsoft.imcache.cache.Cache#put(java.lang.Object,
     * java.lang.Object)
     */
public void put(K key, V value) {
    writeLock(key);
    Pointer pointer = pointerMap.get(key);
    try {
        byte[] bytes = serializer.serialize(value);
        if (pointer == null) {
            pointer = bufferStore.store(bytes);
        } else {
            synchronized (pointer) {
                pointer = bufferStore.update(pointer, bytes);
            }
        }
        pointerMap.put(key, pointer);
    } finally {
        writeUnlock(key);
    }
}
Also used : Pointer(com.cetsoft.imcache.offheap.bytebuffer.Pointer)

Example 3 with Pointer

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

the class OffHeapCache method invalidate.

/*
     * (non-Javadoc)
     * 
     * @see com.cetsoft.imcache.cache.Cache#invalidate(java.lang.Object)
     */
public V invalidate(K key) {
    writeLock(key);
    try {
        Pointer pointer = pointerMap.get(key);
        if (pointer != null) {
            synchronized (pointer) {
                byte[] payload = bufferStore.remove(pointer);
                pointerMap.remove(key);
                return serializer.deserialize(payload);
            }
        }
    } finally {
        writeUnlock(key);
    }
    return null;
}
Also used : Pointer(com.cetsoft.imcache.offheap.bytebuffer.Pointer)

Example 4 with Pointer

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

the class OffHeapByteBufferStoreTest method storeBufferOverFlowNextBuffer.

/**
     * Store buffer over flow next buffer.
     */
@Test
public void storeBufferOverFlowNextBuffer() {
    int size = 100;
    byte[] expectedBytes = new byte[size];
    random.nextBytes(expectedBytes);
    doReturn(buffer).when(bufferStore).currentBuffer();
    doNothing().when(bufferStore).nextBuffer();
    doThrow(new BufferOverflowException()).doThrow(new BufferOverflowException()).doReturn(pointer).when(buffer).store(expectedBytes);
    Pointer actualPointer = bufferStore.store(expectedBytes);
    assertEquals(pointer, actualPointer);
    verify(bufferStore, times(3)).currentBuffer();
    verify(buffer, times(3)).store(expectedBytes);
}
Also used : Pointer(com.cetsoft.imcache.offheap.bytebuffer.Pointer) BufferOverflowException(java.nio.BufferOverflowException) Test(org.junit.Test)

Example 5 with Pointer

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

the class OffHeapByteBufferStoreTest method store.

/**
     * Store.
     */
@Test
public void store() {
    int size = 100;
    byte[] expectedBytes = new byte[size];
    random.nextBytes(expectedBytes);
    Pointer pointer = bufferStore.store(expectedBytes);
    byte[] actualBytes = bufferStore.retrieve(pointer);
    assertArrayEquals(expectedBytes, actualBytes);
}
Also used : Pointer(com.cetsoft.imcache.offheap.bytebuffer.Pointer) Test(org.junit.Test)

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