diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java b/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java index 05b07dc..151b00f 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java @@ -27,7 +27,7 @@ public class Cache { - private static CacheService cacheService = new CacheServiceImpl(); + private static CacheService cacheService = new CacheServiceImpl(); // olamy chicken and eggs isssue // private static CacheService cacheService = new CacheServiceImpl( getMemoryManager()); @@ -52,22 +52,22 @@ public static void init( int numberOfBuffers, int size ) init( numberOfBuffers, size, CacheService.DEFAULT_INITIAL_CAPACITY, CacheService.DEFAULT_CONCURRENCY_LEVEL ); } - public static Pointer putByteArray( String key, byte[] payload, int expiresIn ) + public static Pointer putByteArray( String key, byte[] payload, int expiresIn ) { return cacheService.putByteArray( key, payload, expiresIn ); } - public static Pointer putByteArray( String key, byte[] payload ) + public static Pointer putByteArray( String key, byte[] payload ) { return cacheService.putByteArray( key, payload ); } - public static Pointer put( String key, Object object ) + public static Pointer put( String key, Object object ) { return cacheService.put( key, object ); } - public static Pointer put( String key, Object object, int expiresIn ) + public static Pointer put( String key, Object object, int expiresIn ) { return cacheService.put( key, object, expiresIn ); } @@ -82,7 +82,7 @@ public static Object retrieve( String key ) return cacheService.retrieve( key ); } - public static Pointer getPointer( String key ) + public static Pointer getPointer( String key ) { return cacheService.getPointer( key ); } @@ -92,7 +92,7 @@ public static void free( String key ) cacheService.free( key ); } - public static void free( Pointer pointer ) + public static void free( Pointer pointer ) { cacheService.free( pointer ); } @@ -123,7 +123,7 @@ public static long entries() return cacheService.entries(); } - public static void dump( OffHeapMemoryBuffer mem ) + public static void dump( OffHeapMemoryBuffer mem ) { cacheService.dump( mem ); } @@ -138,14 +138,14 @@ public static Serializer getSerializer() return cacheService.getSerializer(); } - public static MemoryManagerService getMemoryManager() + public static MemoryManagerService getMemoryManager() { return cacheService.getMemoryManager(); } - public static Pointer allocate( String key, int size ) + public static Pointer allocate( String key, int size ) { - return cacheService.allocate( key, size ); + return cacheService.allocate( key, Object.class, size ); } } diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java b/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java index 9028d10..18125dc 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java @@ -27,7 +27,7 @@ import java.util.concurrent.ConcurrentMap; -public interface CacheService +public interface CacheService { public static int DEFAULT_CONCURRENCY_LEVEL = 4; @@ -39,23 +39,23 @@ public interface CacheService void scheduleDisposalEvery( long l ); - Pointer putByteArray( String key, byte[] payload, int expiresIn ); + Pointer putByteArray( K key, byte[] payload, int expiresIn ); - Pointer putByteArray( String key, byte[] payload ); + Pointer putByteArray( K key, byte[] payload ); - Pointer put( String key, Object object ); + Pointer put( K key, V value ); - Pointer put( String key, Object object, int expiresIn ); + Pointer put( K key, V value, int expiresIn ); - byte[] retrieveByteArray( String key ); + byte[] retrieveByteArray( K key ); - Object retrieve( String key ); + Object retrieve( K key ); - Pointer getPointer( String key ); + Pointer getPointer( K key ); - void free( String key ); + void free( K key ); - void free( Pointer pointer ); + void free( Pointer pointer ); void collectExpired(); @@ -68,22 +68,22 @@ public interface CacheService long entries(); - void dump( OffHeapMemoryBuffer mem ); + void dump( OffHeapMemoryBuffer mem ); void dump(); - ConcurrentMap getMap(); + ConcurrentMap> getMap(); - void setMap( ConcurrentMap map ); + void setMap( ConcurrentMap> map ); Serializer getSerializer(); - MemoryManagerService getMemoryManager(); + MemoryManagerService getMemoryManager(); - void setMemoryManager( MemoryManagerService memoryManager ); + void setMemoryManager( MemoryManagerService memoryManager ); void setSerializer( Serializer serializer ); - Pointer allocate( String key, int size ); + Pointer allocate( K key, Class type, int size ); } diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java b/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java index 74821f6..102bcd2 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java @@ -19,7 +19,15 @@ * under the License. */ -import com.google.common.collect.MapMaker; +import static java.lang.String.format; +import static org.apache.directmemory.serialization.SerializerFactory.createNewSerializer; + +import java.io.EOFException; +import java.io.IOException; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.ConcurrentMap; + import org.apache.directmemory.measures.Every; import org.apache.directmemory.measures.Ram; import org.apache.directmemory.memory.MemoryManagerService; @@ -30,27 +38,19 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.EOFException; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.Timer; -import java.util.TimerTask; -import java.util.concurrent.ConcurrentMap; - -import static java.lang.String.format; -import static org.apache.directmemory.serialization.SerializerFactory.createNewSerializer; +import com.google.common.collect.MapMaker; -public class CacheServiceImpl - implements CacheService +public class CacheServiceImpl + implements CacheService { private static Logger logger = LoggerFactory.getLogger( CacheServiceImpl.class ); - private ConcurrentMap map; + private ConcurrentMap> map; private Serializer serializer = createNewSerializer(); - private MemoryManagerService memoryManager = new MemoryManagerServiceImpl(); + private MemoryManagerService memoryManager = new MemoryManagerServiceImpl(); private final Timer timer = new Timer(); @@ -67,11 +67,12 @@ public CacheServiceImpl() * * @param memoryManager */ - public CacheServiceImpl( MemoryManagerService memoryManager ) + public CacheServiceImpl( MemoryManagerService memoryManager ) { this.memoryManager = memoryManager; } + @Override public void scheduleDisposalEvery( long l ) { timer.schedule( new TimerTask() @@ -87,6 +88,7 @@ public void run() logger.info( "disposal scheduled every " + l + " milliseconds" ); } + @Override public void init( int numberOfBuffers, int size, int initialCapacity, int concurrencyLevel ) { map = new MapMaker().concurrencyLevel( concurrencyLevel ).initialCapacity( initialCapacity ).makeMap(); @@ -109,33 +111,42 @@ public void init( int numberOfBuffers, int size, int initialCapacity, int concur scheduleDisposalEvery( Every.seconds( 10 ) ); } + @Override public void init( int numberOfBuffers, int size ) { init( numberOfBuffers, size, DEFAULT_INITIAL_CAPACITY, DEFAULT_CONCURRENCY_LEVEL ); } - public Pointer putByteArray( String key, byte[] payload ) + @Override + public Pointer putByteArray( K key, byte[] payload ) { return store( key, payload, 0 ); } - public Pointer putByteArray( String key, byte[] payload, int expiresIn ) + @Override + public Pointer putByteArray( K key, byte[] payload, int expiresIn ) { return store( key, payload, expiresIn ); } - public Pointer put( String key, Object object ) + @Override + public Pointer put( K key, V value ) { - return put( key, object, 0 ); + return put( key, value, 0 ); } - public Pointer put( String key, Object object, int expiresIn ) + @Override + public Pointer put( K key, V value, int expiresIn ) { try { - byte[] payload = serializer.serialize( object ); - Pointer ptr = store( key, payload, expiresIn ); - ptr.clazz = object.getClass(); + byte[] payload = serializer.serialize( value ); + Pointer ptr = store( key, payload, expiresIn ); + + @SuppressWarnings( "unchecked" ) // type driven by the compiler + Class clazz = (Class) value.getClass(); + + ptr.clazz = clazz; return ptr; } catch ( IOException e ) @@ -153,9 +164,9 @@ public Pointer put( String key, Object object, int expiresIn ) } } - private Pointer store( String key, byte[] payload, int expiresIn ) + private Pointer store( K key, byte[] payload, int expiresIn ) { - Pointer pointer = map.get( key ); + Pointer pointer = map.get( key ); if ( pointer != null ) { return memoryManager.update( pointer, payload ); @@ -168,9 +179,10 @@ private Pointer store( String key, byte[] payload, int expiresIn ) } } - public byte[] retrieveByteArray( String key ) + @Override + public byte[] retrieveByteArray( K key ) { - Pointer ptr = getPointer( key ); + Pointer ptr = getPointer( key ); if ( ptr == null ) { return null; @@ -190,9 +202,10 @@ public byte[] retrieveByteArray( String key ) } } - public Object retrieve( String key ) + @Override + public V retrieve( K key ) { - Pointer ptr = getPointer( key ); + Pointer ptr = getPointer( key ); if ( ptr == null ) { return null; @@ -208,11 +221,6 @@ public Object retrieve( String key ) } else { - if ( ptr.clazz == ByteBuffer.class ) - { - // skip serialization if it is a bytebuffer - return ptr.directBuffer; - } try { return serializer.deserialize( memoryManager.retrieve( ptr ), ptr.clazz ); @@ -241,37 +249,43 @@ public Object retrieve( String key ) return null; } - public Pointer getPointer( String key ) + @Override + public Pointer getPointer( K key ) { return map.get( key ); } - public void free( String key ) + @Override + public void free( K key ) { - Pointer p = map.remove( key ); + Pointer p = map.remove( key ); if ( p != null ) { memoryManager.free( p ); } } - public void free( Pointer pointer ) + @Override + public void free( Pointer pointer ) { memoryManager.free( pointer ); } + @Override public void collectExpired() { memoryManager.collectExpired(); // still have to look for orphan (storing references to freed pointers) map entries } + @Override public void collectLFU() { memoryManager.collectLFU(); // can possibly clear one whole buffer if it's too fragmented - investigate } + @Override public void collectAll() { Thread thread = new Thread() @@ -288,6 +302,7 @@ public void run() } + @Override public void clear() { map.clear(); @@ -295,12 +310,14 @@ public void clear() logger.info( "Cache cleared" ); } + @Override public long entries() { return map.size(); } - public void dump( OffHeapMemoryBuffer mem ) + @Override + public void dump( OffHeapMemoryBuffer mem ) { logger.info( format( "off-heap - buffer: \t%1d", mem.getBufferNumber() ) ); logger.info( format( "off-heap - allocated: \t%1s", Ram.inMb( mem.capacity() ) ) ); @@ -311,6 +328,7 @@ public void dump( OffHeapMemoryBuffer mem ) logger.info( "************************************************" ); } + @Override public void dump() { if ( !logger.isInfoEnabled() ) @@ -320,48 +338,54 @@ public void dump() logger.info( "*** DirectMemory statistics ********************" ); - for ( OffHeapMemoryBuffer mem : memoryManager.getBuffers() ) + for ( OffHeapMemoryBuffer mem : memoryManager.getBuffers() ) { dump( mem ); } } - public ConcurrentMap getMap() + @Override + public ConcurrentMap> getMap() { return map; } - public void setMap( ConcurrentMap map ) + @Override + public void setMap( ConcurrentMap> map ) { this.map = map; } + @Override public Serializer getSerializer() { return serializer; } + @Override public void setSerializer( Serializer serializer ) { this.serializer = serializer; } - public MemoryManagerService getMemoryManager() + @Override + public MemoryManagerService getMemoryManager() { return memoryManager; } - public void setMemoryManager( MemoryManagerService memoryManager ) + @Override + public void setMemoryManager( MemoryManagerService memoryManager ) { this.memoryManager = memoryManager; } @Override - public Pointer allocate( String key, int size ) + public Pointer allocate( K key, Class type, int size ) { - Pointer ptr = memoryManager.allocate( size, -1, -1 ); + Pointer ptr = memoryManager.allocate( type, size, -1, -1 ); map.put( key, ptr ); - ptr.clazz = ByteBuffer.class; + ptr.clazz = type; return ptr; } } diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/memory/AbstractOffHeapMemoryBuffer.java b/directmemory-cache/src/main/java/org/apache/directmemory/memory/AbstractOffHeapMemoryBuffer.java index e70effe..8bebeb1 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/memory/AbstractOffHeapMemoryBuffer.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/memory/AbstractOffHeapMemoryBuffer.java @@ -34,8 +34,8 @@ import com.google.common.base.Predicate; -public abstract class AbstractOffHeapMemoryBuffer - implements OffHeapMemoryBuffer +public abstract class AbstractOffHeapMemoryBuffer + implements OffHeapMemoryBuffer { protected final ByteBuffer buffer; @@ -50,11 +50,11 @@ public abstract class AbstractOffHeapMemoryBuffer protected abstract Logger getLogger(); - private final Predicate relative = new Predicate() + private final Predicate> relative = new Predicate>() { @Override - public boolean apply( Pointer input ) + public boolean apply( Pointer input ) { return !input.free && input.expiresIn > 0 @@ -63,11 +63,11 @@ public boolean apply( Pointer input ) }; - private final Predicate absolute = new Predicate() + private final Predicate> absolute = new Predicate>() { @Override - public boolean apply( Pointer input ) + public boolean apply( Pointer input ) { return !input.free && input.expires > 0 @@ -95,17 +95,17 @@ protected AbstractOffHeapMemoryBuffer( ByteBuffer buffer, int bufferNumber ) { this.buffer = buffer; this.bufferNumber = bufferNumber; - // createAndAddFirstPointer(); + // createAndAddFirstPointer(); } - protected abstract Pointer createAndAddFirstPointer(); + protected abstract Pointer createAndAddFirstPointer(); - public Pointer store( byte[] payload ) + public Pointer store( byte[] payload ) { return store( payload, -1 ); } - protected void freePointer( Pointer pointer2free ) + protected void freePointer( Pointer pointer2free ) { pointer2free.free = true; pointer2free.created = 0; @@ -117,17 +117,17 @@ protected void freePointer( Pointer pointer2free ) used.addAndGet( -pointer2free.getCapacity() ); } - public Pointer store( byte[] payload, Date expires ) + public Pointer store( byte[] payload, Date expires ) { return store( payload, 0, expires.getTime() ); } - public Pointer store( byte[] payload, long expiresIn ) + public Pointer store( byte[] payload, long expiresIn ) { return store( payload, expiresIn, 0 ); } - protected abstract Pointer store( byte[] payload, long expiresIn, long expires ); + protected abstract Pointer store( byte[] payload, long expiresIn, long expires ); protected boolean inShortage() { @@ -135,22 +135,22 @@ protected boolean inShortage() return allocationErrors > AbstractOffHeapMemoryBuffer.maxAllocationErrors; } - protected long free( Predicate predicate ) + protected long free( Predicate> predicate ) { return free( filter( getUsedPointers(), predicate ) ); } - protected long free( Iterable pointers ) + protected long free( Iterable> pointers ) { long howMuch = 0; - for ( Pointer expired : pointers ) + for ( Pointer expired : pointers ) { howMuch += free( expired ); } return howMuch; } - protected abstract List getUsedPointers(); + protected abstract List> getUsedPointers(); public void disposeExpiredRelative() { @@ -180,10 +180,10 @@ public long collectLFU( int limit ) limit = getUsedPointers().size() / 10; } - Iterable result = from( new Comparator() + Iterable> result = from( new Comparator>() { - public int compare( Pointer o1, Pointer o2 ) + public int compare( Pointer o1, Pointer o2 ) { float f1 = o1.getFrequency(); float f2 = o2.getFrequency(); @@ -191,11 +191,11 @@ public int compare( Pointer o1, Pointer o2 ) return Float.compare( f1, f2 ); } - } ).sortedCopy( limit( filter( getUsedPointers(), new Predicate() + } ).sortedCopy( limit( filter( getUsedPointers(), new Predicate>() { @Override - public boolean apply( Pointer input ) + public boolean apply( Pointer input ) { return !input.free; } @@ -210,7 +210,7 @@ public boolean apply( Pointer input ) return free( result ); } - public Pointer update( Pointer pointer, byte[] payload ) + public Pointer update( Pointer pointer, byte[] payload ) { if ( payload.length > pointer.getCapacity() ) { @@ -220,9 +220,7 @@ public Pointer update( Pointer pointer, byte[] payload ) return store( payload ); } - public abstract Pointer allocate( int size, long expiresIn, long expires ); - - protected void resetPointer( final Pointer pointer ) + protected void resetPointer( final Pointer pointer ) { pointer.free = true; pointer.created = 0; @@ -233,7 +231,7 @@ protected void resetPointer( final Pointer pointer ) pointer.directBuffer = null; } - protected void setExpiration( final Pointer pointer, long expiresIn, long expires ) + protected void setExpiration( final Pointer pointer, long expiresIn, long expires ) { if ( expiresIn > 0 ) diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/memory/AllocationPolicy.java b/directmemory-cache/src/main/java/org/apache/directmemory/memory/AllocationPolicy.java index 6fb4568..f2db2f7 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/memory/AllocationPolicy.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/memory/AllocationPolicy.java @@ -30,7 +30,7 @@ * @author bperroud * */ -public interface AllocationPolicy +public interface AllocationPolicy { /** @@ -38,7 +38,7 @@ public interface AllocationPolicy * * @param buffers */ - void init( List buffers ); + void init( List> buffers ); /** * Returns the active buffer in which to allocate. @@ -47,7 +47,7 @@ public interface AllocationPolicy * @param allocationNumber : the number of time the allocation has already failed. * @return the buffer to allocate, or null if allocation has failed. */ - OffHeapMemoryBuffer getActiveBuffer( OffHeapMemoryBuffer previouslyAllocatedBuffer, int allocationNumber ); + OffHeapMemoryBuffer getActiveBuffer( OffHeapMemoryBuffer previouslyAllocatedBuffer, int allocationNumber ); /** * Reset internal state diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java b/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java index 1feb2d6..2333c55 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java @@ -28,7 +28,7 @@ public class MemoryManager { private static Logger logger = LoggerFactory.getLogger( MemoryManager.class ); - private static MemoryManagerService memoryManager = new MemoryManagerServiceImpl(); + private static MemoryManagerService memoryManager = new MemoryManagerServiceImpl(); private MemoryManager() { @@ -40,27 +40,27 @@ public static void init( int numberOfBuffers, int size ) memoryManager.init( numberOfBuffers, size ); } - public static Pointer store( byte[] payload, int expiresIn ) + public static Pointer store( byte[] payload, int expiresIn ) { return memoryManager.store( payload, expiresIn ); } - public static Pointer store( byte[] payload ) + public static Pointer store( byte[] payload ) { return store( payload, 0 ); } - public static Pointer update( Pointer pointer, byte[] payload ) + public static Pointer update( Pointer pointer, byte[] payload ) { return memoryManager.update( pointer, payload ); } - public static byte[] retrieve( Pointer pointer ) + public static byte[] retrieve( Pointer pointer ) { return memoryManager.retrieve( pointer ); } - public static void free( Pointer pointer ) + public static void free( Pointer pointer ) { memoryManager.free( pointer ); } @@ -85,24 +85,24 @@ public static void collectLFU() memoryManager.collectLFU(); } - public static List getBuffers() + public static List> getBuffers() { return memoryManager.getBuffers(); } - public static OffHeapMemoryBuffer getActiveBuffer() + public static OffHeapMemoryBuffer getActiveBuffer() { return memoryManager.getActiveBuffer(); } - public static MemoryManagerService getMemoryManager() + public static MemoryManagerService getMemoryManager() { return memoryManager; } - public static Pointer allocate( int size ) + public static Pointer allocate( int size ) { - return memoryManager.allocate( size, -1, -1 ); //add a version with expiration + return memoryManager.allocate( Object.class, size, -1, -1 ); //add a version with expiration } } diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java b/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java index bba9e0b..d7899e4 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java @@ -22,13 +22,13 @@ import java.util.List; -public interface MemoryManagerService +public interface MemoryManagerService { /** * Initialize the internal structure. Need to be called before the service * can be used. - * + * * @param numberOfBuffers * : number of internal bucket * @param size @@ -36,23 +36,23 @@ public interface MemoryManagerService */ void init( int numberOfBuffers, int size ); - + /** * Store function family. Store the given payload at a certain offset in a MemoryBuffer, returning the pointer to the value. - * + * * @param payload : the data to store * @return the pointer to the value, or null if not enough space has been found. */ - Pointer store( byte[] payload, int expiresIn ); + Pointer store( byte[] payload, int expiresIn ); /** * Same function as {@link #store(byte[])}, but add an relative expiration delta in milliseconds - * + * * @param payload : the data to store * @param expiresIn : relative amount of milliseconds the data will expire * @return the pointer to the value, or null if not enough space has been found. */ - Pointer store( byte[] payload ); + Pointer store( byte[] payload ); /** * Same function as {@link #store(byte[])}, but add an absolute expiration date @@ -61,22 +61,22 @@ public interface MemoryManagerService * @return the pointer to the value, or null if not enough space has been found. */ //public Pointer store(byte[] payload, Date expires); - + /** - * - * + * + * * Update value of a {@link Pointer * @param pointer * @param payload * @return * @throw BufferOverflowException if the size of the payload id bigger than the pointer capacity */ - Pointer update(Pointer pointer, byte[] payload); + Pointer update(Pointer pointer, byte[] payload); - byte[] retrieve( Pointer pointer ); + byte[] retrieve( Pointer pointer ); - void free( Pointer pointer ); + void free( Pointer pointer ); void clear(); @@ -86,10 +86,10 @@ public interface MemoryManagerService void collectLFU(); - List getBuffers(); + List> getBuffers(); - OffHeapMemoryBuffer getActiveBuffer(); + OffHeapMemoryBuffer getActiveBuffer(); - Pointer allocate( int size, long expiresIn, long expires ); + Pointer allocate( Class type, int size, long expiresIn, long expires ); } diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java b/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java index 4201240..c84246f 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java @@ -28,13 +28,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class MemoryManagerServiceImpl - implements MemoryManagerService +public class MemoryManagerServiceImpl + implements MemoryManagerService { protected static Logger logger = LoggerFactory.getLogger( MemoryManager.class ); - protected List buffers = new ArrayList(); + protected List> buffers = new ArrayList>(); protected int activeBufferIndex = 0; @@ -44,30 +44,30 @@ public MemoryManagerServiceImpl() public void init( int numberOfBuffers, int size ) { - buffers = new ArrayList( numberOfBuffers ); + buffers = new ArrayList>( numberOfBuffers ); for ( int i = 0; i < numberOfBuffers; i++ ) { - final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( size, i ); + final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( size, i ); buffers.add( offHeapMemoryBuffer ); } logger.info( format( "MemoryManager initialized - %d buffers, %s each", numberOfBuffers, Ram.inMb( size ) ) ); } - protected OffHeapMemoryBuffer instanciateOffHeapMemoryBuffer( int size, int bufferNumber ) + protected OffHeapMemoryBuffer instanciateOffHeapMemoryBuffer( int size, int bufferNumber ) { return OffHeapMemoryBufferImpl.createNew( size, bufferNumber ); } - public OffHeapMemoryBuffer getActiveBuffer() + public OffHeapMemoryBuffer getActiveBuffer() { return buffers.get( activeBufferIndex ); } - public Pointer store( byte[] payload, int expiresIn ) + public Pointer store( byte[] payload, int expiresIn ) { - Pointer p = getActiveBuffer().store( payload, expiresIn ); + Pointer p = getActiveBuffer().store( payload, expiresIn ); if ( p == null ) { nextBuffer(); @@ -76,29 +76,29 @@ public Pointer store( byte[] payload, int expiresIn ) return p; } - public Pointer store( byte[] payload ) + public Pointer store( byte[] payload ) { return store( payload, 0 ); } - public Pointer update( Pointer pointer, byte[] payload ) + public Pointer update( Pointer pointer, byte[] payload ) { return buffers.get( pointer.bufferNumber ).update( pointer, payload ); } - public byte[] retrieve( Pointer pointer ) + public byte[] retrieve( Pointer pointer ) { return buffers.get( pointer.bufferNumber ).retrieve( pointer ); } - public void free( Pointer pointer ) + public void free( Pointer pointer ) { buffers.get( pointer.bufferNumber ).free( pointer ); } public void clear() { - for ( OffHeapMemoryBuffer buffer : buffers ) + for ( OffHeapMemoryBuffer buffer : buffers ) { buffer.clear(); } @@ -108,7 +108,7 @@ public void clear() public long capacity() { long totalCapacity = 0; - for ( OffHeapMemoryBuffer buffer : buffers ) + for ( OffHeapMemoryBuffer buffer : buffers ) { totalCapacity += buffer.capacity(); } @@ -118,7 +118,7 @@ public long capacity() public long collectExpired() { long disposed = 0; - for ( OffHeapMemoryBuffer buffer : buffers ) + for ( OffHeapMemoryBuffer buffer : buffers ) { disposed += buffer.collectExpired(); } @@ -127,30 +127,29 @@ public long collectExpired() public void collectLFU() { - for ( OffHeapMemoryBuffer buf : buffers ) + for ( OffHeapMemoryBuffer buf : buffers ) { buf.collectLFU( -1 ); } } - public List getBuffers() + public List> getBuffers() { return buffers; } - public void setBuffers( List buffers ) + public void setBuffers( List> buffers ) { this.buffers = buffers; } - @Override - public Pointer allocate( int size, long expiresIn, long expires ) + public Pointer allocate( Class type, int size, long expiresIn, long expires ) { - Pointer p = getActiveBuffer().allocate( size, expiresIn, expires ); + Pointer p = getActiveBuffer().allocate( type, size, expiresIn, expires ); if ( p == null ) { nextBuffer(); - p = getActiveBuffer().allocate( size, expiresIn, expires ); + p = getActiveBuffer().allocate( type, size, expiresIn, expires ); } return p; } diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceWithAllocationPolicyImpl.java b/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceWithAllocationPolicyImpl.java index 5e9307b..802aff9 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceWithAllocationPolicyImpl.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceWithAllocationPolicyImpl.java @@ -24,11 +24,11 @@ * * @author bperroud */ -public class MemoryManagerServiceWithAllocationPolicyImpl - extends MemoryManagerServiceImpl +public class MemoryManagerServiceWithAllocationPolicyImpl + extends MemoryManagerServiceImpl { - protected AllocationPolicy allocationPolicy; + protected AllocationPolicy allocationPolicy; @Override public void init( int numberOfBuffers, int size ) @@ -37,22 +37,22 @@ public void init( int numberOfBuffers, int size ) allocationPolicy.init( getBuffers() ); } - public void setAllocationPolicy( final AllocationPolicy allocationPolicy ) + public void setAllocationPolicy( final AllocationPolicy allocationPolicy ) { this.allocationPolicy = allocationPolicy; } @Override - public OffHeapMemoryBuffer getActiveBuffer() + public OffHeapMemoryBuffer getActiveBuffer() { return allocationPolicy.getActiveBuffer( null, 0 ); } @Override - public Pointer store( byte[] payload, int expiresIn ) + public Pointer store( byte[] payload, int expiresIn ) { - Pointer p = null; - OffHeapMemoryBuffer buffer = null; + Pointer p = null; + OffHeapMemoryBuffer buffer = null; int allocationNumber = 1; do { @@ -76,10 +76,10 @@ public void clear() } @Override - public Pointer allocate( int size, long expiresIn, long expires ) + public Pointer allocate( Class type, int size, long expiresIn, long expires ) { - Pointer p = null; - OffHeapMemoryBuffer buffer = null; + Pointer p = null; + OffHeapMemoryBuffer buffer = null; int allocationNumber = 1; do { @@ -88,7 +88,7 @@ public Pointer allocate( int size, long expiresIn, long expires ) { return null; } - p = buffer.allocate( size, expiresIn, expires ); + p = buffer.allocate( type, size, expiresIn, expires ); allocationNumber++; } while ( p == null ); diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java b/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java index 4da6feb..0021447 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java @@ -21,7 +21,7 @@ import java.util.Date; -public interface OffHeapMemoryBuffer +public interface OffHeapMemoryBuffer { /** @@ -44,7 +44,7 @@ public interface OffHeapMemoryBuffer * @param payload : the data to store * @return the pointer where the data is stored, null in case of failure */ - public Pointer store( byte[] payload ); + public Pointer store( byte[] payload ); /** * Same as {@link #store(byte[])}, with an absolute expiration date @@ -52,7 +52,7 @@ public interface OffHeapMemoryBuffer * @param expires : an absolute expiration date * @return the pointer where the data is stored, null in case of failure */ - public Pointer store( byte[] payload, Date expires ); + public Pointer store( byte[] payload, Date expires ); /** * Same as {@link #store(byte[])}, with an relative expiration delta @@ -60,21 +60,21 @@ public interface OffHeapMemoryBuffer * @param expiresIn : a relative expiration delta * @return the pointer where the data is stored, null in case of failure */ - public Pointer store( byte[] payload, long expiresIn ); + public Pointer store( byte[] payload, long expiresIn ); /** * Return previously stored data associated to the given pointer * @param pointer : presiously allocated pointer * @return previously stored data */ - public byte[] retrieve( Pointer pointer ); + public byte[] retrieve( Pointer pointer ); /** * Release previously allocated memory * @param pointer2free : the pointer to free * @return the newly freed space */ - public int free( Pointer pointer2free ); + public int free( Pointer pointer2free ); /** * Completely empty the buffer @@ -110,15 +110,15 @@ public interface OffHeapMemoryBuffer * @param payload : the data to update * @return the update pointer. It may be a new pointer. */ - public Pointer update( Pointer pointer, byte[] payload ); + public Pointer update( Pointer pointer, byte[] payload ); /** * Allocate requested size and return a pointer and a ByteBuffer - * + * * @param size * @param expiresIn * @param expires * @return */ - public Pointer allocate( int size, long expiresIn, long expires ); + public Pointer allocate( Class type, int size, long expiresIn, long expires ); } diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBufferImpl.java b/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBufferImpl.java index 44e6087..3191a5f 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBufferImpl.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBufferImpl.java @@ -33,39 +33,39 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class OffHeapMemoryBufferImpl - extends AbstractOffHeapMemoryBuffer +public class OffHeapMemoryBufferImpl + extends AbstractOffHeapMemoryBuffer { protected static Logger logger = LoggerFactory.getLogger( OffHeapMemoryBufferImpl.class ); - protected final List pointers = new ArrayList(); + protected final List> pointers = new ArrayList>(); protected Logger getLogger() { return logger; } - protected List getUsedPointers() + protected List> getUsedPointers() { return pointers; } - public List getPointers() + public List> getPointers() { return pointers; } - public static OffHeapMemoryBufferImpl createNew( int capacity, int bufferNumber ) + public static OffHeapMemoryBufferImpl createNew( int capacity, int bufferNumber ) { logger.info( format( "Creating OffHeapMemoryBuffer %d with a capacity of %s", bufferNumber, Ram.inMb( capacity ) ) ); - return new OffHeapMemoryBufferImpl( ByteBuffer.allocateDirect( capacity ), bufferNumber ); + return new OffHeapMemoryBufferImpl( ByteBuffer.allocateDirect( capacity ), bufferNumber ); } - public static OffHeapMemoryBufferImpl createNew( int capacity ) + public static OffHeapMemoryBufferImpl createNew( int capacity ) { - return new OffHeapMemoryBufferImpl( ByteBuffer.allocateDirect( capacity ), -1 ); + return new OffHeapMemoryBufferImpl( ByteBuffer.allocateDirect( capacity ), -1 ); } protected OffHeapMemoryBufferImpl( ByteBuffer buffer, int bufferNumber ) @@ -74,9 +74,9 @@ protected OffHeapMemoryBufferImpl( ByteBuffer buffer, int bufferNumber ) createAndAddFirstPointer(); } - protected Pointer createAndAddFirstPointer() + protected Pointer createAndAddFirstPointer() { - Pointer first = new Pointer(); + Pointer first = new Pointer(); first.bufferNumber = bufferNumber; first.start = 0; first.free = true; @@ -85,9 +85,9 @@ protected Pointer createAndAddFirstPointer() return first; } - protected Pointer slice( Pointer existing, int capacity ) + protected Pointer slice( Pointer existing, int capacity ) { - Pointer fresh = new Pointer(); + Pointer fresh = new Pointer(); fresh.bufferNumber = existing.bufferNumber; fresh.start = existing.start; fresh.end = fresh.start + capacity - 1; // 0 indexed @@ -96,9 +96,9 @@ protected Pointer slice( Pointer existing, int capacity ) return fresh; } - protected Pointer firstMatch( int capacity ) + protected Pointer firstMatch( int capacity ) { - for ( Pointer ptr : pointers ) + for ( Pointer ptr : pointers ) { if ( ptr.free && ptr.getCapacity() >= capacity ) { @@ -108,12 +108,12 @@ protected Pointer firstMatch( int capacity ) return null; } - public Pointer store( byte[] payload ) + public Pointer store( byte[] payload ) { return store( payload, -1 ); } - public byte[] retrieve( Pointer pointer ) + public byte[] retrieve( Pointer pointer ) { pointer.lastHit = System.currentTimeMillis(); pointer.hits++; @@ -138,7 +138,7 @@ public byte[] retrieve( Pointer pointer ) return swp; } - public synchronized int free( Pointer pointer2free ) + public synchronized int free( Pointer pointer2free ) { resetPointer( pointer2free ); used.addAndGet( -pointer2free.getCapacity() ); @@ -147,7 +147,7 @@ public synchronized int free( Pointer pointer2free ) public synchronized void clear() { - for (final Pointer pointer : pointers) + for (final Pointer pointer : pointers) { pointer.free = true; } @@ -158,19 +158,19 @@ public synchronized void clear() used.set( 0 ); } - public Pointer store( byte[] payload, Date expires ) + public Pointer store( byte[] payload, Date expires ) { return store( payload, 0, expires.getTime() ); } - public Pointer store( byte[] payload, long expiresIn ) + public Pointer store( byte[] payload, long expiresIn ) { return store( payload, expiresIn, 0 ); } - protected synchronized Pointer store( byte[] payload, long expiresIn, long expires ) + protected synchronized Pointer store( byte[] payload, long expiresIn, long expires ) { - Pointer goodOne = firstMatch( payload.length ); + Pointer goodOne = firstMatch( payload.length ); if ( goodOne == null ) { @@ -178,7 +178,7 @@ protected synchronized Pointer store( byte[] payload, long expiresIn, long expir return null; } - Pointer fresh = slice( goodOne, payload.length ); + Pointer fresh = slice( goodOne, payload.length ); fresh.created = System.currentTimeMillis(); setExpiration( fresh, expiresIn, expires ); @@ -216,15 +216,15 @@ public static long crc32( byte[] payload ) return checksum.getValue(); } - public Pointer update( Pointer pointer, byte[] payload ) + public Pointer update( Pointer pointer, byte[] payload ) { free( pointer ); return store( payload ); } - public synchronized Pointer allocate( int size, long expiresIn, long expires ) + public synchronized Pointer allocate( Class type, int size, long expiresIn, long expires ) { - Pointer goodOne = firstMatch( size ); + Pointer goodOne = firstMatch( size ); if ( goodOne == null ) { @@ -232,7 +232,7 @@ public synchronized Pointer allocate( int size, long expiresIn, long expires ) return null; } - Pointer fresh = slice( goodOne, size ); + Pointer fresh = slice( goodOne, size ); fresh.created = System.currentTimeMillis(); setExpiration( fresh, expiresIn, expires ); @@ -244,7 +244,7 @@ public synchronized Pointer allocate( int size, long expiresIn, long expires ) buf.position( fresh.start ); fresh.directBuffer = buf.slice(); - fresh.clazz = ByteBuffer.class; + fresh.clazz = type; pointers.add( fresh ); return fresh; } diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMergingMemoryBufferImpl.java b/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMergingMemoryBufferImpl.java index 55edc77..9508b1e 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMergingMemoryBufferImpl.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMergingMemoryBufferImpl.java @@ -61,8 +61,8 @@ * @author bperroud * */ -public class OffHeapMergingMemoryBufferImpl - extends AbstractOffHeapMemoryBuffer +public class OffHeapMergingMemoryBufferImpl + extends AbstractOffHeapMemoryBuffer { protected static Logger logger = LoggerFactory.getLogger( OffHeapMemoryBufferImpl.class ); @@ -71,15 +71,15 @@ public class OffHeapMergingMemoryBufferImpl private final static Boolean DEFAULT_VALUE = Boolean.TRUE; // List of free pointers sorted by size - private final TreeMap freePointersBySizeDesc = new TreeMap( - new PointerBySizeDesc() ); + private final TreeMap, Boolean> freePointersBySizeDesc = new TreeMap, Boolean>( + new PointerBySizeDesc() ); // List of free pointers sorted by memory offset - private final TreeMap freePointersByMemoryOffsetAsc = new TreeMap( - new PointerByMemoryOffsetAsc() ); + private final TreeMap, Boolean> freePointersByMemoryOffsetAsc = new TreeMap, Boolean>( + new PointerByMemoryOffsetAsc() ); // Set of used pointers - private final Set usedPointers = Collections.newSetFromMap( new ConcurrentHashMap() ); + private final Set> usedPointers = Collections.newSetFromMap( new ConcurrentHashMap, Boolean>() ); // Lock used instead of synchronized block to guarantee consistency when manipulating list of pointers. private final Lock pointerManipulationLock = new ReentrantLock(); @@ -95,11 +95,11 @@ protected Logger getLogger() * @param bufferNumber : arbitrary number of the buffer. * @return an OffHeapMemoryBuffer with internal buffer allocated. */ - public static OffHeapMergingMemoryBufferImpl createNew( int capacity, int bufferNumber ) + public static OffHeapMergingMemoryBufferImpl createNew( int capacity, int bufferNumber ) { logger.info( format( "Creating OffHeapLinkedMemoryBuffer %d with a capacity of %s", bufferNumber, Ram.inMb( capacity ) ) ); - return new OffHeapMergingMemoryBufferImpl( ByteBuffer.allocateDirect( capacity ), bufferNumber ); + return new OffHeapMergingMemoryBufferImpl( ByteBuffer.allocateDirect( capacity ), bufferNumber ); } /** @@ -107,9 +107,9 @@ public static OffHeapMergingMemoryBufferImpl createNew( int capacity, int buffer * @param capacity : size in byte of the internal buffer * @return an OffHeapMemoryBuffer with internal buffer allocated. */ - public static OffHeapMergingMemoryBufferImpl createNew( int capacity ) + public static OffHeapMergingMemoryBufferImpl createNew( int capacity ) { - return new OffHeapMergingMemoryBufferImpl( ByteBuffer.allocateDirect( capacity ), -1 ); + return new OffHeapMergingMemoryBufferImpl( ByteBuffer.allocateDirect( capacity ), -1 ); } /** @@ -126,9 +126,9 @@ private OffHeapMergingMemoryBufferImpl( ByteBuffer buffer, int bufferNumber ) /** * Initialization function. Create an initial free {@link Pointer} mapping the whole buffer. */ - protected Pointer createAndAddFirstPointer() + protected Pointer createAndAddFirstPointer() { - Pointer first = new Pointer( 0, buffer.capacity() - 1 ); + Pointer first = new Pointer( 0, buffer.capacity() - 1 ); first.bufferNumber = bufferNumber; first.free = true; freePointersBySizeDesc.put( first, DEFAULT_VALUE ); @@ -136,7 +136,7 @@ protected Pointer createAndAddFirstPointer() return first; } - protected Pointer firstMatch( int capacity ) + protected Pointer firstMatch( int capacity ) { // check for empty instead of throwing an exception. if ( freePointersBySizeDesc.isEmpty() ) @@ -145,7 +145,7 @@ protected Pointer firstMatch( int capacity ) } try { - Pointer ptr = freePointersBySizeDesc.firstKey(); + Pointer ptr = freePointersBySizeDesc.firstKey(); if ( ptr.getCapacity() >= capacity ) { // 0 indexed @@ -160,7 +160,7 @@ protected Pointer firstMatch( int capacity ) } @Override - public byte[] retrieve( Pointer pointer ) + public byte[] retrieve( Pointer pointer ) { pointer.lastHit = System.currentTimeMillis(); pointer.hits++; @@ -174,7 +174,7 @@ public byte[] retrieve( Pointer pointer ) } @Override - public int free( Pointer pointer2free ) + public int free( Pointer pointer2free ) { // Avoid freeing twice the same pointer. Maybe atomic boolean is required here. @@ -190,10 +190,10 @@ public int free( Pointer pointer2free ) return 0; } - Pointer lowerPointerToMerge = pointer2free; + Pointer lowerPointerToMerge = pointer2free; // search for adjacent pointers lower than the current one - for ( Pointer adjacentPointer : freePointersByMemoryOffsetAsc.headMap( pointer2free, false ) + for ( Pointer adjacentPointer : freePointersByMemoryOffsetAsc.headMap( pointer2free, false ) .descendingKeySet() ) { @@ -205,10 +205,10 @@ public int free( Pointer pointer2free ) lowerPointerToMerge = adjacentPointer; } - Pointer higherPointerToMerge = pointer2free; + Pointer higherPointerToMerge = pointer2free; // search for adjacent pointers higher than the current one - for ( Pointer adjacentPointer : freePointersByMemoryOffsetAsc.tailMap( pointer2free, false ) + for ( Pointer adjacentPointer : freePointersByMemoryOffsetAsc.tailMap( pointer2free, false ) .navigableKeySet() ) { @@ -224,14 +224,14 @@ public int free( Pointer pointer2free ) if ( lowerPointerToMerge != higherPointerToMerge ) { - final Pointer mergedPointer = new Pointer( lowerPointerToMerge.start, higherPointerToMerge.end ); + final Pointer mergedPointer = new Pointer( lowerPointerToMerge.start, higherPointerToMerge.end ); mergedPointer.free = true; - final Iterator adjacentPointersIterator = freePointersByMemoryOffsetAsc + final Iterator> adjacentPointersIterator = freePointersByMemoryOffsetAsc .subMap( lowerPointerToMerge, true, higherPointerToMerge, true ).navigableKeySet().iterator(); while ( adjacentPointersIterator.hasNext() ) { - Pointer adjacentPointer = adjacentPointersIterator.next(); + Pointer adjacentPointer = adjacentPointersIterator.next(); adjacentPointer.free = true; // if a reference to the pointer is kept, we must not use it. freePointersBySizeDesc.remove( adjacentPointer ); adjacentPointersIterator.remove(); @@ -273,7 +273,7 @@ public void clear() { allocationErrors = 0; - for ( Pointer pointer : usedPointers ) + for ( Pointer pointer : usedPointers ) { pointer.free = true; // free( pointer ); // too costly to merge every pointers while the will be cleared in a row @@ -288,11 +288,11 @@ public void clear() } @Override - protected Pointer store( byte[] payload, long expiresIn, long expires ) + protected Pointer store( byte[] payload, long expiresIn, long expires ) { final int size = payload.length; - final Pointer allocatedPointer = allocatePointer( size ); + final Pointer allocatedPointer = allocatePointer( size ); if ( allocatedPointer != null ) { @@ -314,10 +314,9 @@ protected Pointer store( byte[] payload, long expiresIn, long expires ) return allocatedPointer; } - private Pointer allocatePointer( int size ) + private Pointer allocatePointer( int size ) { - - Pointer goodOne, fresh = null; + Pointer goodOne, fresh = null; try { @@ -342,13 +341,13 @@ private Pointer allocatePointer( int size ) if ( goodOne.getCapacity() != size ) { - fresh = new Pointer( goodOne.start, goodOne.start + size - 1 ); + fresh = new Pointer( goodOne.start, goodOne.start + size - 1 ); fresh.bufferNumber = getBufferNumber(); fresh.free = true; fresh.created = System.currentTimeMillis(); // create a new pointer for the remaining space - final Pointer newGoodOne = new Pointer( fresh.end + 1, goodOne.end ); + final Pointer newGoodOne = new Pointer( fresh.end + 1, goodOne.end ); newGoodOne.free = true; // and add it to the free lists @@ -369,10 +368,10 @@ private Pointer allocatePointer( int size ) } @Override - public Pointer allocate( int size, long expiresIn, long expires ) + public Pointer allocate( Class type, int size, long expiresIn, long expires ) { - final Pointer allocatedPointer = allocatePointer( size ); + final Pointer allocatedPointer = allocatePointer( size ); if ( allocatedPointer != null ) { @@ -394,12 +393,12 @@ public Pointer allocate( int size, long expiresIn, long expires ) /** * Sort {@link Pointer}s by size desc. */ - private static class PointerBySizeDesc - implements Comparator + private static class PointerBySizeDesc + implements Comparator> { @Override - public int compare( final Pointer pointer0, final Pointer pointer1 ) + public int compare( final Pointer pointer0, final Pointer pointer1 ) { final int size0 = pointer0.getCapacity(); final int size1 = pointer1.getCapacity(); @@ -425,12 +424,12 @@ public int compare( final Pointer pointer0, final Pointer pointer1 ) /** * Sort {@link Pointer}s by memory offset asc. */ - private static class PointerByMemoryOffsetAsc - implements Comparator + private static class PointerByMemoryOffsetAsc + implements Comparator> { @Override - public int compare( final Pointer pointer0, final Pointer pointer1 ) + public int compare( final Pointer pointer0, final Pointer pointer1 ) { final int offset0 = pointer0.start; final int offset1 = pointer1.start; @@ -454,13 +453,13 @@ public int compare( final Pointer pointer0, final Pointer pointer1 ) } @Override - protected List getUsedPointers() + protected List> getUsedPointers() { - return new ArrayList( usedPointers ); + return new ArrayList>( usedPointers ); } @Override - public Pointer update( Pointer pointer, byte[] payload ) + public Pointer update( Pointer pointer, byte[] payload ) { if ( payload.length > pointer.getCapacity() ) { @@ -476,9 +475,9 @@ public Pointer update( Pointer pointer, byte[] payload ) return pointer; } - public List getPointers() + public List> getPointers() { // TODO : remove this conversion from Set to List ... - return new ArrayList( usedPointers ); + return new ArrayList>( usedPointers ); } } diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/memory/Pointer.java b/directmemory-cache/src/main/java/org/apache/directmemory/memory/Pointer.java index c83ddc1..5fbcc6b 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/memory/Pointer.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/memory/Pointer.java @@ -24,7 +24,7 @@ import java.nio.ByteBuffer; -public class Pointer +public class Pointer { public int start; @@ -44,7 +44,7 @@ public class Pointer public int bufferNumber; - public Class clazz; + public Class clazz; public ByteBuffer directBuffer = null; diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/memory/RoundRobinAllocationPolicy.java b/directmemory-cache/src/main/java/org/apache/directmemory/memory/RoundRobinAllocationPolicy.java index 7c9c4da..d3bf13f 100644 --- a/directmemory-cache/src/main/java/org/apache/directmemory/memory/RoundRobinAllocationPolicy.java +++ b/directmemory-cache/src/main/java/org/apache/directmemory/memory/RoundRobinAllocationPolicy.java @@ -29,15 +29,15 @@ * * @author bperroud */ -public class RoundRobinAllocationPolicy - implements AllocationPolicy +public class RoundRobinAllocationPolicy + implements AllocationPolicy { // Increment the counter and get the value. Need to start at -1 to have 0'index at first call. private static final int BUFFERS_INDEX_INITIAL_VALUE = -1; // All the buffers to allocate - private List buffers; + private List> buffers; // Cyclic counter private AtomicInteger buffersIndexCounter = new AtomicInteger( BUFFERS_INDEX_INITIAL_VALUE ); @@ -54,13 +54,13 @@ public void setMaxAllocations( int maxAllocations ) } @Override - public void init( List buffers ) + public void init( List> buffers ) { this.buffers = buffers; } @Override - public OffHeapMemoryBuffer getActiveBuffer( OffHeapMemoryBuffer previouslyAllocatedBuffer, int allocationNumber ) + public OffHeapMemoryBuffer getActiveBuffer( OffHeapMemoryBuffer previouslyAllocatedBuffer, int allocationNumber ) { // If current allocation is more than the limit, return a null buffer. if ( allocationNumber > maxAllocations ) @@ -71,7 +71,7 @@ public OffHeapMemoryBuffer getActiveBuffer( OffHeapMemoryBuffer previouslyAlloca // Thread safely increment and get the next buffer's index int i = incrementAndGetBufferIndex(); - final OffHeapMemoryBuffer buffer = buffers.get( i ); + final OffHeapMemoryBuffer buffer = buffers.get( i ); return buffer; } diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheConcurrentTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheConcurrentTest.java index 9c76399..4bd0a2c 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheConcurrentTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheConcurrentTest.java @@ -98,7 +98,7 @@ public void retrieveCatchHalfOfThem() private void get( String key ) { - Pointer p = Cache.getPointer( key ); + Pointer p = Cache.getPointer( key ); @SuppressWarnings( "unused" ) byte[] check = Cache.retrieveByteArray( key ); read.incrementAndGet(); if ( p != null ) @@ -252,6 +252,6 @@ public static void dump() } } - - + + diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheLightConcurrentTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheLightConcurrentTest.java index e48af8b..5ea5899 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheLightConcurrentTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheLightConcurrentTest.java @@ -108,7 +108,7 @@ public void LFUEviction() private void getAndRetrieve( String key ) { - Pointer p = Cache.getPointer( key ); + Pointer p = Cache.getPointer( key ); @SuppressWarnings( "unused" ) byte[] check = Cache.retrieveByteArray( key ); read.incrementAndGet(); if ( p != null ) diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/AbstractOffHeapMemoryBufferTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/AbstractOffHeapMemoryBufferTest.java index 0ae5485..81f9e2d 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/AbstractOffHeapMemoryBufferTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/AbstractOffHeapMemoryBufferTest.java @@ -40,9 +40,9 @@ public abstract class AbstractOffHeapMemoryBufferTest protected static final int SMALL_PAYLOAD_LENGTH = 4; protected static final byte[] SMALL_PAYLOAD = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD_LENGTH ); - - protected abstract OffHeapMemoryBuffer instanciateOffHeapMemoryBuffer( int bufferSize ); - + + protected abstract OffHeapMemoryBuffer instanciateOffHeapMemoryBuffer( int bufferSize ); + /** * Test pointers allocation, when buffer size is not aligned with the size of stored objects. * Null {@link Pointer} should be returned to allow {@link MemoryManagerService} to go to next step with allocation policy. @@ -55,18 +55,18 @@ public void testNotEnoughFreeSpace() final int BUFFER_SIZE = SMALL_PAYLOAD_LENGTH + 1; - final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); + final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); - Pointer pointer1 = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); + Pointer pointer1 = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointer1 ); Assert.assertFalse( pointer1.free ); Assert.assertNull( pointer1.directBuffer ); - Pointer pointer2 = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); + Pointer pointer2 = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); Assert.assertNull( pointer2 ); } - + /** * Ensure no byte is leaking when allocating several objects. @@ -79,16 +79,16 @@ public void testByteLeaking() final int NUMBER_OF_OBJECTS = 10; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD_LENGTH; - - final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); + + final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { - Pointer pointer = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); + Pointer pointer = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointer ); } - Pointer pointerNull = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); + Pointer pointerNull = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); Assert.assertNull( pointerNull ); } @@ -104,12 +104,12 @@ public void testReportCorrectUsedMemory() final int NUMBER_OF_OBJECTS = 4; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD_LENGTH; - final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); + final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); - Pointer lastPointer = null; + Pointer lastPointer = null; for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { - Pointer pointer = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); + Pointer pointer = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointer ); lastPointer = pointer; } @@ -120,7 +120,7 @@ public void testReportCorrectUsedMemory() Assert.assertNotNull( lastPointer ); offHeapMemoryBuffer.free( lastPointer ); - Pointer pointerNotNull = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); + Pointer pointerNotNull = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointerNotNull ); // Buffer again fully used. @@ -129,7 +129,7 @@ public void testReportCorrectUsedMemory() } /** - * Completely fill the buffer, free some pointer, reallocated the freed space, clear the buffer. The entire space should be + * Completely fill the buffer, free some pointer, reallocated the freed space, clear the buffer. The entire space should be */ @Test public void testFullFillAndFreeAndClearBuffer() @@ -138,31 +138,31 @@ public void testFullFillAndFreeAndClearBuffer() final int NUMBER_OF_OBJECTS = 10; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD_LENGTH; - final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); + final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); - Pointer pointerFull = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); + Pointer pointerFull = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); Assert.assertNotNull( pointerFull ); offHeapMemoryBuffer.free( pointerFull ); final int size1 = R.nextInt( BUFFER_SIZE / 2 ) + 1; - Pointer pointer1 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( size1 ) ); + Pointer pointer1 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( size1 ) ); Assert.assertNotNull( "Cannot store " + size1 + " bytes", pointer1 ); final int size2 = R.nextInt( ( BUFFER_SIZE - size1 ) / 2 ) + 1; - Pointer pointer2 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( size2 ) ); + Pointer pointer2 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( size2 ) ); Assert.assertNotNull( "Cannot store " + size2 + " bytes", pointer2 ); final int size3 = R.nextInt( ( BUFFER_SIZE - size1 - size2 ) / 2 ) + 1; - Pointer pointer3 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( size3 ) ); + Pointer pointer3 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( size3 ) ); Assert.assertNotNull( "Cannot store " + size3 + " bytes", pointer3 ); final int size4 = BUFFER_SIZE - size1 - size2 - size3; - Pointer pointer4 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( size4 ) ); + Pointer pointer4 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( size4 ) ); Assert.assertNotNull( "Cannot store " + size4 + " bytes", pointer4 ); offHeapMemoryBuffer.free( pointer1 ); Assert.assertTrue( pointer1.free ); - + offHeapMemoryBuffer.free( pointer3 ); offHeapMemoryBuffer.free( pointer4 ); @@ -172,13 +172,13 @@ public void testFullFillAndFreeAndClearBuffer() Assert.assertEquals( 0, offHeapMemoryBuffer.used() ); // As all pointers have been freeed, we should be able to reallocate the whole buffer - Pointer pointer6 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); + Pointer pointer6 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); Assert.assertNotNull( "Cannot store " + BUFFER_SIZE + " bytes", pointer6 ); offHeapMemoryBuffer.clear(); // As all pointers have been cleared, we should be able to reallocate the whole buffer - Pointer pointer7 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); + Pointer pointer7 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); Assert.assertNotNull( "Cannot store " + BUFFER_SIZE + " bytes", pointer7 ); offHeapMemoryBuffer.clear(); @@ -186,14 +186,14 @@ public void testFullFillAndFreeAndClearBuffer() // As all pointers have been cleared, we should be able to reallocate the whole buffer for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { - Pointer pointer = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); + Pointer pointer = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointer ); } offHeapMemoryBuffer.clear(); // As all pointers have been cleared, we should be able to reallocate the whole buffer - Pointer pointer8 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); + Pointer pointer8 = offHeapMemoryBuffer.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); Assert.assertNotNull( "Cannot store " + BUFFER_SIZE + " bytes", pointer8 ); offHeapMemoryBuffer.free( pointer8 ); @@ -201,7 +201,7 @@ public void testFullFillAndFreeAndClearBuffer() // As all pointers have been cleared, we should be able to reallocate the whole buffer for ( int i = 0; i < NUMBER_OF_OBJECTS * 10; i++ ) { - Pointer pointer = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); + Pointer pointer = offHeapMemoryBuffer.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointer ); offHeapMemoryBuffer.free( pointer ); } @@ -209,7 +209,7 @@ public void testFullFillAndFreeAndClearBuffer() // After a clear occurs, pointers allocated before the clear should be set as "free" Assert.assertTrue( pointer6.free ); Assert.assertTrue( pointer7.free ); - + } @Test @@ -219,12 +219,12 @@ public void testRandomPayload() final int NUMBER_OF_OBJECTS = 10; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD_LENGTH; - final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); + final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { byte[] payload = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD_LENGTH ); - Pointer pointer = offHeapMemoryBuffer.store( payload ); + Pointer pointer = offHeapMemoryBuffer.store( payload ); Assert.assertNotNull( pointer ); byte[] fetchedPayload = offHeapMemoryBuffer.retrieve( pointer ); Assert.assertEquals( new String( payload ), new String( fetchedPayload ) ); @@ -239,7 +239,7 @@ public void testRandomPayload() for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { byte[] payload = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD_LENGTH ); - Pointer pointer = offHeapMemoryBuffer.store( payload ); + Pointer pointer = offHeapMemoryBuffer.store( payload ); Assert.assertNotNull( pointer ); byte[] fetchedPayload = offHeapMemoryBuffer.retrieve( pointer ); Assert.assertEquals( new String( payload ), new String( fetchedPayload ) ); @@ -259,13 +259,13 @@ public void testStoreAllocAndFree() final int NUMBER_OF_OBJECTS = 100; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD_LENGTH; - final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); + final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); - List pointers = new ArrayList( NUMBER_OF_OBJECTS ); + List> pointers = new ArrayList>( NUMBER_OF_OBJECTS ); for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { byte[] payload = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD_LENGTH ); - Pointer pointer = offHeapMemoryBuffer.store( payload ); + Pointer pointer = offHeapMemoryBuffer.store( payload ); Assert.assertNotNull( pointer ); pointers.add( pointer ); byte[] fetchedPayload = offHeapMemoryBuffer.retrieve( pointer ); @@ -275,35 +275,35 @@ public void testStoreAllocAndFree() // Free 1/4 of the pointers, from 1/4 of the address space to 1/2 for ( int i = NUMBER_OF_OBJECTS / 4; i < NUMBER_OF_OBJECTS / 2; i++ ) { - Pointer pointer = pointers.get( i ); + Pointer pointer = pointers.get( i ); offHeapMemoryBuffer.free( pointer ); } // Should be able to allocate NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD_LENGTH bytes - Pointer pointer1 = offHeapMemoryBuffer.allocate( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD_LENGTH, 0, 0 ); + Pointer pointer1 = offHeapMemoryBuffer.allocate( Object.class, NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD_LENGTH, 0, 0 ); Assert.assertNotNull( pointer1 ); int pointerToSkip = NUMBER_OF_OBJECTS / 2 + NUMBER_OF_OBJECTS / 10; for ( int i = NUMBER_OF_OBJECTS / 2; i < NUMBER_OF_OBJECTS * 3 / 4; i++ ) { - // skip one pointer + // skip one pointer if ( i == pointerToSkip ) { continue; } - Pointer pointer = pointers.get( i ); + Pointer pointer = pointers.get( i ); offHeapMemoryBuffer.free( pointer ); } // Should NOT be able to allocate NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD_LENGTH bytes - Pointer pointer2 = offHeapMemoryBuffer.allocate( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD_LENGTH, 0, 0 ); + Pointer pointer2 = offHeapMemoryBuffer.allocate( Object.class, NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD_LENGTH, 0, 0 ); Assert.assertNull( pointer2 ); // Freeing the previously skipped pointer should then merge the whole memory space offHeapMemoryBuffer.free( pointers.get( pointerToSkip ) ); // Should be able to allocate NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD_LENGTH bytes - Pointer pointer3 = offHeapMemoryBuffer.allocate( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD_LENGTH, 0, 0 ); + Pointer pointer3 = offHeapMemoryBuffer.allocate( Object.class, NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD_LENGTH, 0, 0 ); Assert.assertNotNull( pointer3 ); byte[] payload3 = MemoryTestUtils.generateRandomPayload( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD_LENGTH ); @@ -320,18 +320,18 @@ public void testRandomPayload2() final int NUMBER_OF_OBJECTS = 10; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD_LENGTH; - final OffHeapMergingMemoryBufferImpl offHeapLinkedMemoryBuffer = OffHeapMergingMemoryBufferImpl + final OffHeapMergingMemoryBufferImpl offHeapLinkedMemoryBuffer = OffHeapMergingMemoryBufferImpl .createNew( BUFFER_SIZE ); byte[] payload1 = MemoryTestUtils.generateRandomPayload( 2 * SMALL_PAYLOAD_LENGTH ); - Pointer pointer1 = offHeapLinkedMemoryBuffer.store( payload1 ); + Pointer pointer1 = offHeapLinkedMemoryBuffer.store( payload1 ); Assert.assertNotNull( pointer1 ); byte[] fetchedPayload1 = offHeapLinkedMemoryBuffer.retrieve( pointer1 ); Assert.assertEquals( new String( payload1 ), new String( fetchedPayload1 ) ); byte[] payload2 = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD_LENGTH ); - Pointer pointer2 = offHeapLinkedMemoryBuffer.store( payload2 ); + Pointer pointer2 = offHeapLinkedMemoryBuffer.store( payload2 ); Assert.assertNotNull( pointer2 ); byte[] fetchedPayload2 = offHeapLinkedMemoryBuffer.retrieve( pointer2 ); @@ -342,7 +342,7 @@ public void testRandomPayload2() offHeapLinkedMemoryBuffer.free( pointer1 ); byte[] payload3 = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD_LENGTH ); - Pointer pointer3 = offHeapLinkedMemoryBuffer.store( payload3 ); + Pointer pointer3 = offHeapLinkedMemoryBuffer.store( payload3 ); Assert.assertNotNull( pointer3 ); byte[] fetchedPayload3 = offHeapLinkedMemoryBuffer.retrieve( pointer3 ); @@ -357,23 +357,23 @@ public void testUpdate() final int NUMBER_OF_OBJECTS = 1; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD_LENGTH; - final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); + final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); final byte[] payload = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD_LENGTH ); - final Pointer pointer = offHeapMemoryBuffer.store( payload ); + final Pointer pointer = offHeapMemoryBuffer.store( payload ); Assert.assertNotNull( pointer ); Assert.assertEquals( new String( payload ), new String( offHeapMemoryBuffer.retrieve( pointer ) ) ); final byte[] otherPayload = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD_LENGTH ); - final Pointer otherPointer = offHeapMemoryBuffer.update( pointer, otherPayload ); + final Pointer otherPointer = offHeapMemoryBuffer.update( pointer, otherPayload ); Assert.assertNotNull( otherPointer ); Assert.assertEquals( pointer.start, otherPointer.start ); Assert.assertEquals( pointer.end, otherPointer.end ); Assert.assertEquals( new String( otherPayload ), new String( offHeapMemoryBuffer.retrieve( otherPointer ) ) ); final byte[] evenAnotherPayload = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD_LENGTH / 2 ); - final Pointer evenAnotherPointer = offHeapMemoryBuffer.update( pointer, evenAnotherPayload ); + final Pointer evenAnotherPointer = offHeapMemoryBuffer.update( pointer, evenAnotherPayload ); Assert.assertNotNull( evenAnotherPointer ); Assert.assertEquals( pointer.start, evenAnotherPointer.start ); Assert.assertEquals( pointer.end, evenAnotherPointer.end ); @@ -382,8 +382,8 @@ public void testUpdate() .startsWith( new String( evenAnotherPayload ) ) ); } - - + + @Test public void testAllocate() { @@ -391,28 +391,28 @@ public void testAllocate() final int NUMBER_OF_OBJECTS = 10; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD_LENGTH; - final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); + final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( BUFFER_SIZE ); final byte[] payload1 = MemoryTestUtils.generateRandomPayload( 8 * SMALL_PAYLOAD_LENGTH ); - final Pointer pointer1 = offHeapMemoryBuffer.store( payload1 ); + final Pointer pointer1 = offHeapMemoryBuffer.store( payload1 ); Assert.assertNotNull( pointer1 ); Assert.assertEquals( new String( payload1 ), new String( offHeapMemoryBuffer.retrieve( pointer1 ) ) ); final byte[] payload2 = MemoryTestUtils.generateRandomPayload( 2 * SMALL_PAYLOAD_LENGTH ); - final Pointer pointer2 = offHeapMemoryBuffer.store( payload2 ); + final Pointer pointer2 = offHeapMemoryBuffer.store( payload2 ); Assert.assertNotNull( pointer2 ); Assert.assertEquals( new String( payload2 ), new String( offHeapMemoryBuffer.retrieve( pointer2 ) ) ); offHeapMemoryBuffer.free( pointer1 ); - + final byte[] payload3 = MemoryTestUtils.generateRandomPayload( 2 * SMALL_PAYLOAD_LENGTH ); - final Pointer pointer3 = offHeapMemoryBuffer.store( payload3 ); + final Pointer pointer3 = offHeapMemoryBuffer.store( payload3 ); Assert.assertNotNull( pointer3 ); Assert.assertEquals( new String( payload3 ), new String( offHeapMemoryBuffer.retrieve( pointer3 ) ) ); final int size1 = 4 * SMALL_PAYLOAD_LENGTH; final byte[] allocatedPayload1 = MemoryTestUtils.generateRandomPayload( size1 ); - final Pointer allocatedPointer1 = offHeapMemoryBuffer.allocate( allocatedPayload1.length, -1, -1 ); + final Pointer allocatedPointer1 = offHeapMemoryBuffer.allocate( Object.class, allocatedPayload1.length, -1, -1 ); Assert.assertNotNull( allocatedPointer1 ); final ByteBuffer buffer1 = allocatedPointer1.directBuffer; Assert.assertNotNull( buffer1 ); @@ -424,7 +424,7 @@ public void testAllocate() final int size2 = 2 * SMALL_PAYLOAD_LENGTH; final byte[] allocatedPayload2 = MemoryTestUtils.generateRandomPayload( size2 ); - final Pointer allocatedPointer2 = offHeapMemoryBuffer.allocate( allocatedPayload2.length, -1, -1 ); + final Pointer allocatedPointer2 = offHeapMemoryBuffer.allocate( Object.class, allocatedPayload2.length, -1, -1 ); Assert.assertNotNull( allocatedPointer2 ); final ByteBuffer buffer2 = allocatedPointer2.directBuffer; Assert.assertNotNull( buffer2 ); @@ -433,11 +433,11 @@ public void testAllocate() buffer2.put( allocatedPayload2 ); Assert.assertEquals( new String( allocatedPayload2 ), new String( offHeapMemoryBuffer.retrieve( allocatedPointer2 ) ) ); - + // Ensure the new allocation has not overwritten other data Assert.assertEquals( new String( payload2 ), new String( offHeapMemoryBuffer.retrieve( pointer2 ) ) ); Assert.assertEquals( new String( payload3 ), new String( offHeapMemoryBuffer.retrieve( pointer3 ) ) ); } - + } diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseTest.java index 5d8596a..6e6ac46 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseTest.java @@ -50,7 +50,7 @@ public class BaseTest @Test public void smokeTest() { - OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 1 * 1024 * 1024 ); + OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 1 * 1024 * 1024 ); logger.info( "buffer size=" + mem.capacity() ); assertNotNull( mem ); @@ -60,7 +60,7 @@ public void smokeTest() logger.info( "size=" + size ); - Pointer p = mem.store( new byte[size] ); + Pointer p = mem.store( new byte[size] ); assertNotNull( p ); assertEquals( size, p.end ); assertEquals( size, mem.used() ); @@ -117,7 +117,7 @@ public void anyDuplicates() @Test public void aFewEntriesWithRead() { - OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 100 * 1024 * 1024 ); + OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 100 * 1024 * 1024 ); logger.info( "total capacity=" + Ram.inMb( mem.capacity() ) ); assertNotNull( mem ); int howMany = 10000; @@ -127,7 +127,7 @@ public void aFewEntriesWithRead() for ( int i = 0; i < howMany; i++ ) { final byte[] payload = ( test + " - " + i ).getBytes(); - Pointer p = mem.store( payload ); + Pointer p = mem.store( payload ); final byte[] check = mem.retrieve( p ); assertNotNull( check ); assertEquals( test + " - " + i, new String( check ) ); @@ -149,18 +149,18 @@ private static long crc32( byte[] payload ) @Test public void aFewEntriesWithCheck() { - OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 10 * 1024 * 1024 ); + OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 10 * 1024 * 1024 ); logger.info( "total capacity=" + Ram.inMb( mem.capacity() ) ); assertNotNull( mem ); int howMany = 10; logger.info( "payload size is variable" ); logger.info( "entries=" + howMany ); String test = "this is a nicely crafted test"; - Pointer lastP = null; + Pointer lastP = null; for ( int i = 0; i < howMany; i++ ) { byte[] payload = ( test + " - " + i ).getBytes(); - Pointer p = mem.store( payload ); + Pointer p = mem.store( payload ); logger.info( "p.start=" + p.start ); logger.info( "p.end=" + p.end ); if ( lastP != null ) @@ -179,7 +179,7 @@ public void aFewEntriesWithCheck() public void checkExpiration() throws InterruptedException { - OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 10 * 1024 * 1024 ); + OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 10 * 1024 * 1024 ); assertNotNull( mem ); int size = 400; int howMany = 5000; diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/Concurrent2Test.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/Concurrent2Test.java index 95a5c5d..8c2ead5 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/Concurrent2Test.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/Concurrent2Test.java @@ -63,7 +63,7 @@ public class Concurrent2Test private static AtomicInteger read = new AtomicInteger(); - public static ConcurrentMap map = + public static ConcurrentMap> map = new MapMaker().concurrencyLevel( 4 ).initialCapacity( 100000 ).makeMap(); @@ -80,7 +80,7 @@ public void store() public void retrieveCatchThemAll() { String key = "test-" + ( rndGen.nextInt( entries ) + 1 ); - Pointer p = map.get( key ); + Pointer p = map.get( key ); read.incrementAndGet(); if ( p != null ) { @@ -107,7 +107,7 @@ public void retrieveCatchThemAll() public void retrieveCatchHalfOfThem() { String key = "test-" + ( rndGen.nextInt( entries * 2 ) + 1 ); - Pointer p = map.get( key ); + Pointer p = map.get( key ); read.incrementAndGet(); if ( p != null ) { @@ -179,7 +179,7 @@ public void write1Read9() private void get( String key ) { - Pointer p = map.get( key ); + Pointer p = map.get( key ); read.incrementAndGet(); if ( p != null ) { @@ -204,7 +204,7 @@ private void get( String key ) private static Logger logger = LoggerFactory.getLogger( Concurrent2Test.class ); - private static void dump( OffHeapMemoryBuffer mem ) + private static void dump( OffHeapMemoryBuffer mem ) { logger.info( "off-heap - buffer: " + mem.getBufferNumber() ); logger.info( "off-heap - allocated: " + Ram.inMb( mem.capacity() ) ); @@ -225,7 +225,7 @@ public static void init() public static void dump() { - for ( OffHeapMemoryBuffer mem : MemoryManager.getBuffers() ) + for ( OffHeapMemoryBuffer mem : MemoryManager.getBuffers() ) { dump( mem ); } @@ -243,6 +243,3 @@ public static void dump() } } - - - diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/Concurrent3Test.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/Concurrent3Test.java index 5915ef4..cc5a14e 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/Concurrent3Test.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/Concurrent3Test.java @@ -63,7 +63,7 @@ public class Concurrent3Test private static AtomicInteger disposals = new AtomicInteger(); - public static ConcurrentMap map = + public static ConcurrentMap> map = new MapMaker().concurrencyLevel( 4 ).initialCapacity( 100000 ).makeMap(); @@ -101,7 +101,7 @@ public void retrieveCatchHalfOfThem() private void get( String key ) { - Pointer p = map.get( key ); + Pointer p = map.get( key ); read.incrementAndGet(); if ( p != null ) { @@ -226,7 +226,7 @@ public void write1Read9() private static Logger logger = LoggerFactory.getLogger( Concurrent3Test.class ); - private static void dump( OffHeapMemoryBuffer mem ) + private static void dump( OffHeapMemoryBuffer mem ) { logger.info( "off-heap - buffer: " + mem.getBufferNumber() ); logger.info( "off-heap - allocated: " + Ram.inMb( mem.capacity() ) ); @@ -247,7 +247,7 @@ public static void init() public static void dump() { - for ( OffHeapMemoryBuffer mem : MemoryManager.getBuffers() ) + for ( OffHeapMemoryBuffer mem : MemoryManager.getBuffers() ) { dump( mem ); } @@ -266,6 +266,6 @@ public static void dump() } } - - + + diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/ConcurrentTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/ConcurrentTest.java index 4d4e953..b0c7724 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/ConcurrentTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/ConcurrentTest.java @@ -64,9 +64,9 @@ public class ConcurrentTest private static AtomicInteger read = new AtomicInteger(); - public static OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 512 * 1024 * 1024 ); + public static OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 512 * 1024 * 1024 ); - public static ConcurrentMap map = + public static ConcurrentMap> map = new MapMaker().concurrencyLevel( 4 ).initialCapacity( 100000 ).makeMap(); @@ -83,7 +83,7 @@ public void store() public void retrieveCatchThemAll() { String key = "test-" + ( rndGen.nextInt( entries ) + 1 ); - Pointer p = map.get( key ); + Pointer p = map.get( key ); read.incrementAndGet(); if ( p != null ) { @@ -110,7 +110,7 @@ public void retrieveCatchThemAll() public void retrieveCatchHalfOfThem() { String key = "test-" + ( rndGen.nextInt( entries * 2 ) + 1 ); - Pointer p = map.get( key ); + Pointer p = map.get( key ); read.incrementAndGet(); if ( p != null ) { @@ -182,7 +182,7 @@ public void write1Read9() private void get( String key ) { - Pointer p = map.get( key ); + Pointer p = map.get( key ); read.incrementAndGet(); if ( p != null ) { @@ -229,6 +229,6 @@ public static void dump() } } - - + + diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocTest.java index c28625e..f3a4b5f 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocTest.java @@ -68,7 +68,7 @@ public void dump() logger.info( "************************************************" ); } - OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 512 * 1024 * 1024 ); + OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 512 * 1024 * 1024 ); @Test public void oneMillionEntries() @@ -144,7 +144,7 @@ public void fiveMillionEntries() public void withMap() { - ConcurrentMap map = new MapMaker().concurrencyLevel( 4 ).initialCapacity( 500000 ).makeMap(); + ConcurrentMap> map = new MapMaker().concurrencyLevel( 4 ).initialCapacity( 500000 ).makeMap(); String str = "This is the string to store into the off-heap memory"; @@ -155,7 +155,7 @@ public void withMap() logger.info( "adding " + howMany + " strings of " + size + " bytes..." ); for ( long i = 0; i < howMany; i++ ) { - Pointer p = mem.store( payload ); + Pointer p = mem.store( payload ); map.put( i, p ); } logger.info( "...done" ); @@ -183,7 +183,7 @@ public void oneMillionEntriesWithRead() byte[] payload = test.getBytes(); for ( int i = 0; i < howMany; i++ ) { - Pointer p = mem.store( payload ); + Pointer p = mem.store( payload ); byte[] check = mem.retrieve( p ); assertNotNull( check ); assertEquals( test, new String( check ) ); @@ -192,6 +192,3 @@ public void oneMillionEntriesWithRead() logger.info( "total used=" + Ram.inMb( mem.used() ) ); } } - - - diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplTest.java index d369939..6a69116 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplTest.java @@ -35,9 +35,9 @@ public class MemoryManagerServiceImplTest protected static final byte[] SMALL_PAYLOAD = "ABCD".getBytes(); - protected MemoryManagerService getMemoryManagerService() + protected MemoryManagerService getMemoryManagerService() { - return new MemoryManagerServiceImpl(); + return new MemoryManagerServiceImpl(); } @Test @@ -50,14 +50,14 @@ public void testFirstMatchBorderCase() final int BUFFER_SIZE = 5; - final MemoryManagerService memoryManagerService = getMemoryManagerService(); + final MemoryManagerService memoryManagerService = getMemoryManagerService(); memoryManagerService.init( 1, BUFFER_SIZE ); - Pointer pointer1 = memoryManagerService.store( SMALL_PAYLOAD ); + Pointer pointer1 = memoryManagerService.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointer1 ); - Pointer pointer2 = memoryManagerService.store( SMALL_PAYLOAD ); + Pointer pointer2 = memoryManagerService.store( SMALL_PAYLOAD ); Assert.assertNull( pointer2 ); } @@ -71,17 +71,17 @@ public void testAllocateMultipleBuffers() final int NUMBER_OF_OBJECTS = 4; - final MemoryManagerService memoryManagerService = getMemoryManagerService(); + final MemoryManagerService memoryManagerService = getMemoryManagerService(); memoryManagerService.init( NUMBER_OF_OBJECTS, SMALL_PAYLOAD.length ); for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { - Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); + Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointer ); } - Pointer pointerNull = memoryManagerService.store( SMALL_PAYLOAD ); + Pointer pointerNull = memoryManagerService.store( SMALL_PAYLOAD ); Assert.assertNull( pointerNull ); } @@ -94,16 +94,16 @@ public void testByteLeaking() final int NUMBER_OF_OBJECTS = 10; - final MemoryManagerService memoryManagerService = getMemoryManagerService(); + final MemoryManagerService memoryManagerService = getMemoryManagerService(); memoryManagerService.init( 1, NUMBER_OF_OBJECTS * SMALL_PAYLOAD.length ); for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { - Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); + Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointer ); } - Pointer pointerNull = memoryManagerService.store( SMALL_PAYLOAD ); + Pointer pointerNull = memoryManagerService.store( SMALL_PAYLOAD ); Assert.assertNull( pointerNull ); } @@ -117,14 +117,14 @@ public void testReportCorrectUsedMemory() final int NUMBER_OF_OBJECTS = 4; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD.length; - final MemoryManagerService memoryManagerService = getMemoryManagerService(); + final MemoryManagerService memoryManagerService = getMemoryManagerService(); memoryManagerService.init( 1, BUFFER_SIZE ); - Pointer lastPointer = null; + Pointer lastPointer = null; for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { - Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); + Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointer ); lastPointer = pointer; } @@ -135,7 +135,7 @@ public void testReportCorrectUsedMemory() Assert.assertNotNull( lastPointer ); memoryManagerService.free( lastPointer ); - Pointer pointerNotNull = memoryManagerService.store( SMALL_PAYLOAD ); + Pointer pointerNotNull = memoryManagerService.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointerNotNull ); // Buffer again fully used. @@ -150,14 +150,14 @@ public void testRandomPayload() final int NUMBER_OF_OBJECTS = 10; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD.length; - final MemoryManagerService memoryManagerService = getMemoryManagerService(); + final MemoryManagerService memoryManagerService = getMemoryManagerService(); memoryManagerService.init( 1, BUFFER_SIZE ); for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { byte[] payload = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD.length ); - Pointer pointer = memoryManagerService.store( payload ); + Pointer pointer = memoryManagerService.store( payload ); Assert.assertNotNull( pointer ); byte[] fetchedPayload = memoryManagerService.retrieve( pointer ); Assert.assertEquals( new String( payload ), new String( fetchedPayload ) ); @@ -172,7 +172,7 @@ public void testRandomPayload() for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { byte[] payload = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD.length ); - Pointer pointer = memoryManagerService.store( payload ); + Pointer pointer = memoryManagerService.store( payload ); Assert.assertNotNull( pointer ); byte[] fetchedPayload = memoryManagerService.retrieve( pointer ); Assert.assertEquals( new String( payload ), new String( fetchedPayload ) ); @@ -185,7 +185,7 @@ public void testRandomPayload() memoryManagerService.clear(); - Pointer pointer = null; + Pointer pointer = null; do { byte[] payload = MemoryTestUtils.generateRandomPayload( R.nextInt( BUFFER_SIZE / 4 + 1 ) ); diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplWithMerginOHMBAndAllocationPolicyTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplWithMerginOHMBAndAllocationPolicyTest.java index b3dc186..9ae827f 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplWithMerginOHMBAndAllocationPolicyTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplWithMerginOHMBAndAllocationPolicyTest.java @@ -37,19 +37,19 @@ public class MemoryManagerServiceImplWithMerginOHMBAndAllocationPolicyTest { @Override - protected MemoryManagerService getMemoryManagerService() + protected MemoryManagerService getMemoryManagerService() { // Initialize MemoryManagerService with OffHeapMergingMemoryBufferImpl and AllocationPolicy - final MemoryManagerServiceWithAllocationPolicyImpl mms = new MemoryManagerServiceWithAllocationPolicyImpl() + final MemoryManagerServiceWithAllocationPolicyImpl mms = new MemoryManagerServiceWithAllocationPolicyImpl() { @Override - protected OffHeapMemoryBuffer instanciateOffHeapMemoryBuffer( int size, int bufferNumber ) + protected OffHeapMemoryBuffer instanciateOffHeapMemoryBuffer( int size, int bufferNumber ) { return OffHeapMergingMemoryBufferImpl.createNew( size, bufferNumber ); } }; - - mms.setAllocationPolicy( new RoundRobinAllocationPolicy() ); + + mms.setAllocationPolicy( new RoundRobinAllocationPolicy() ); return mms; } @@ -60,28 +60,28 @@ public void testFullFillAndClearBuffer() final int NUMBER_OF_OBJECTS = 10; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD.length; - final MemoryManagerService memoryManagerService = getMemoryManagerService(); + final MemoryManagerService memoryManagerService = getMemoryManagerService(); memoryManagerService.init( 1, BUFFER_SIZE ); - Pointer pointerFull = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); + Pointer pointerFull = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); Assert.assertNotNull( pointerFull ); memoryManagerService.free( pointerFull ); final int size1 = R.nextInt( BUFFER_SIZE / 2 ) + 1; - Pointer pointer1 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size1 ) ); + Pointer pointer1 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size1 ) ); Assert.assertNotNull( "Cannot store " + size1 + " bytes", pointer1 ); final int size2 = R.nextInt( ( BUFFER_SIZE - size1 ) / 2 ) + 1; - Pointer pointer2 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size2 ) ); + Pointer pointer2 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size2 ) ); Assert.assertNotNull( "Cannot store " + size2 + " bytes", pointer2 ); final int size3 = R.nextInt( ( BUFFER_SIZE - size1 - size2 ) / 2 ) + 1; - Pointer pointer3 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size3 ) ); + Pointer pointer3 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size3 ) ); Assert.assertNotNull( "Cannot store " + size3 + " bytes", pointer3 ); final int size4 = BUFFER_SIZE - size1 - size2 - size3; - Pointer pointer4 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size4 ) ); + Pointer pointer4 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size4 ) ); Assert.assertNotNull( "Cannot store " + size4 + " bytes", pointer4 ); memoryManagerService.free( pointer1 ); @@ -96,14 +96,14 @@ public void testFullFillAndClearBuffer() // As all pointers have been freeed, we should be able to reallocate the // whole buffer - Pointer pointer6 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); + Pointer pointer6 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); Assert.assertNotNull( "Cannot store " + BUFFER_SIZE + " bytes", pointer6 ); memoryManagerService.clear(); // As all pointers have been cleared, we should be able to reallocate // the whole buffer - Pointer pointer7 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); + Pointer pointer7 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); Assert.assertNotNull( "Cannot store " + BUFFER_SIZE + " bytes", pointer7 ); memoryManagerService.clear(); @@ -112,7 +112,7 @@ public void testFullFillAndClearBuffer() // the whole buffer for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { - Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); + Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointer ); } @@ -120,7 +120,7 @@ public void testFullFillAndClearBuffer() // As all pointers have been cleared, we should be able to reallocate // the whole buffer - Pointer pointer8 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); + Pointer pointer8 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); Assert.assertNotNull( "Cannot store " + BUFFER_SIZE + " bytes", pointer8 ); memoryManagerService.free( pointer8 ); @@ -129,7 +129,7 @@ public void testFullFillAndClearBuffer() // the whole buffer for ( int i = 0; i < NUMBER_OF_OBJECTS * 10; i++ ) { - Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); + Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointer ); memoryManagerService.free( pointer ); } @@ -143,15 +143,15 @@ public void testStoreAllocAndFree() final int NUMBER_OF_OBJECTS = 100; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD.length; - final MemoryManagerService memoryManagerService = getMemoryManagerService(); + final MemoryManagerService memoryManagerService = getMemoryManagerService(); memoryManagerService.init( 1, BUFFER_SIZE ); - List pointers = new ArrayList( NUMBER_OF_OBJECTS ); + List> pointers = new ArrayList>( NUMBER_OF_OBJECTS ); for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { byte[] payload = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD.length ); - Pointer pointer = memoryManagerService.store( payload ); + Pointer pointer = memoryManagerService.store( payload ); Assert.assertNotNull( pointer ); pointers.add( pointer ); byte[] fetchedPayload = memoryManagerService.retrieve( pointer ); @@ -161,13 +161,13 @@ public void testStoreAllocAndFree() // Free 1/4 of the pointers, from 1/4 of the address space to 1/2 for ( int i = NUMBER_OF_OBJECTS / 4; i < NUMBER_OF_OBJECTS / 2; i++ ) { - Pointer pointer = pointers.get( i ); + Pointer pointer = pointers.get( i ); memoryManagerService.free( pointer ); } // Should be able to allocate NUMBER_OF_OBJECTS / 4 * // SMALL_PAYLOAD.length bytes - Pointer pointer1 = memoryManagerService.allocate( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length, 0, 0 ); + Pointer pointer1 = memoryManagerService.allocate( Object.class, NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length, 0, 0 ); Assert.assertNotNull( "Cannot store " + ( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length ) + " bytes", pointer1 ); int pointerToSkip = NUMBER_OF_OBJECTS / 2 + NUMBER_OF_OBJECTS / 10; @@ -178,13 +178,13 @@ public void testStoreAllocAndFree() { continue; } - Pointer pointer = pointers.get( i ); + Pointer pointer = pointers.get( i ); memoryManagerService.free( pointer ); } // Should NOT be able to allocate NUMBER_OF_OBJECTS / 4 * // SMALL_PAYLOAD.length bytes - Pointer pointer2 = memoryManagerService.allocate( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length, 0, 0 ); + Pointer pointer2 = memoryManagerService.allocate( Object.class, NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length, 0, 0 ); Assert.assertNull( pointer2 ); // Freeing the previously skipped pointer should then merge the whole @@ -193,7 +193,7 @@ public void testStoreAllocAndFree() // Should be able to allocate NUMBER_OF_OBJECTS / 4 * // SMALL_PAYLOAD.length bytes - Pointer pointer3 = memoryManagerService.allocate( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length, 0, 0 ); + Pointer pointer3 = memoryManagerService.allocate( Object.class, NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length, 0, 0 ); Assert.assertNotNull( pointer3 ); byte[] payload3 = MemoryTestUtils.generateRandomPayload( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length ); diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplWithMerginOHMBTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplWithMerginOHMBTest.java index cb86292..efd3ab6 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplWithMerginOHMBTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplWithMerginOHMBTest.java @@ -36,12 +36,12 @@ public class MemoryManagerServiceImplWithMerginOHMBTest { @Override - protected MemoryManagerService getMemoryManagerService() + protected MemoryManagerService getMemoryManagerService() { - return new MemoryManagerServiceImpl() + return new MemoryManagerServiceImpl() { @Override - protected OffHeapMemoryBuffer instanciateOffHeapMemoryBuffer( int size, int bufferNumber ) + protected OffHeapMemoryBuffer instanciateOffHeapMemoryBuffer( int size, int bufferNumber ) { return OffHeapMergingMemoryBufferImpl.createNew( size, bufferNumber ); } @@ -55,28 +55,28 @@ public void testFullFillAndClearBuffer() final int NUMBER_OF_OBJECTS = 10; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD.length; - final MemoryManagerService memoryManagerService = getMemoryManagerService(); + final MemoryManagerService memoryManagerService = getMemoryManagerService(); memoryManagerService.init( 1, BUFFER_SIZE ); - Pointer pointerFull = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); + Pointer pointerFull = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); Assert.assertNotNull( pointerFull ); memoryManagerService.free( pointerFull ); final int size1 = R.nextInt( BUFFER_SIZE / 2 ) + 1; - Pointer pointer1 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size1 ) ); + Pointer pointer1 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size1 ) ); Assert.assertNotNull( "Cannot store " + size1 + " bytes", pointer1 ); final int size2 = R.nextInt( ( BUFFER_SIZE - size1 ) / 2 ) + 1; - Pointer pointer2 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size2 ) ); + Pointer pointer2 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size2 ) ); Assert.assertNotNull( "Cannot store " + size2 + " bytes", pointer2 ); final int size3 = R.nextInt( ( BUFFER_SIZE - size1 - size2 ) / 2 ) + 1; - Pointer pointer3 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size3 ) ); + Pointer pointer3 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size3 ) ); Assert.assertNotNull( "Cannot store " + size3 + " bytes", pointer3 ); final int size4 = BUFFER_SIZE - size1 - size2 - size3; - Pointer pointer4 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size4 ) ); + Pointer pointer4 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( size4 ) ); Assert.assertNotNull( "Cannot store " + size4 + " bytes", pointer4 ); memoryManagerService.free( pointer1 ); @@ -91,14 +91,14 @@ public void testFullFillAndClearBuffer() // As all pointers have been freeed, we should be able to reallocate the // whole buffer - Pointer pointer6 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); + Pointer pointer6 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); Assert.assertNotNull( "Cannot store " + BUFFER_SIZE + " bytes", pointer6 ); memoryManagerService.clear(); // As all pointers have been cleared, we should be able to reallocate // the whole buffer - Pointer pointer7 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); + Pointer pointer7 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); Assert.assertNotNull( "Cannot store " + BUFFER_SIZE + " bytes", pointer7 ); memoryManagerService.clear(); @@ -107,7 +107,7 @@ public void testFullFillAndClearBuffer() // the whole buffer for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { - Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); + Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointer ); } @@ -115,7 +115,7 @@ public void testFullFillAndClearBuffer() // As all pointers have been cleared, we should be able to reallocate // the whole buffer - Pointer pointer8 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); + Pointer pointer8 = memoryManagerService.store( MemoryTestUtils.generateRandomPayload( BUFFER_SIZE ) ); Assert.assertNotNull( "Cannot store " + BUFFER_SIZE + " bytes", pointer8 ); memoryManagerService.free( pointer8 ); @@ -124,7 +124,7 @@ public void testFullFillAndClearBuffer() // the whole buffer for ( int i = 0; i < NUMBER_OF_OBJECTS * 10; i++ ) { - Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); + Pointer pointer = memoryManagerService.store( SMALL_PAYLOAD ); Assert.assertNotNull( pointer ); memoryManagerService.free( pointer ); } @@ -138,15 +138,15 @@ public void testStoreAllocAndFree() final int NUMBER_OF_OBJECTS = 100; final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD.length; - final MemoryManagerService memoryManagerService = getMemoryManagerService(); + final MemoryManagerService memoryManagerService = getMemoryManagerService(); memoryManagerService.init( 1, BUFFER_SIZE ); - List pointers = new ArrayList( NUMBER_OF_OBJECTS ); + List> pointers = new ArrayList>( NUMBER_OF_OBJECTS ); for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ ) { byte[] payload = MemoryTestUtils.generateRandomPayload( SMALL_PAYLOAD.length ); - Pointer pointer = memoryManagerService.store( payload ); + Pointer pointer = memoryManagerService.store( payload ); Assert.assertNotNull( pointer ); pointers.add( pointer ); byte[] fetchedPayload = memoryManagerService.retrieve( pointer ); @@ -156,13 +156,13 @@ public void testStoreAllocAndFree() // Free 1/4 of the pointers, from 1/4 of the address space to 1/2 for ( int i = NUMBER_OF_OBJECTS / 4; i < NUMBER_OF_OBJECTS / 2; i++ ) { - Pointer pointer = pointers.get( i ); + Pointer pointer = pointers.get( i ); memoryManagerService.free( pointer ); } // Should be able to allocate NUMBER_OF_OBJECTS / 4 * // SMALL_PAYLOAD.length bytes - Pointer pointer1 = memoryManagerService.allocate( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length, 0, 0 ); + Pointer pointer1 = memoryManagerService.allocate( Object.class, NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length, 0, 0 ); Assert.assertNotNull( "Cannot store " + ( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length ) + " bytes", pointer1 ); int pointerToSkip = NUMBER_OF_OBJECTS / 2 + NUMBER_OF_OBJECTS / 10; @@ -173,13 +173,13 @@ public void testStoreAllocAndFree() { continue; } - Pointer pointer = pointers.get( i ); + Pointer pointer = pointers.get( i ); memoryManagerService.free( pointer ); } // Should NOT be able to allocate NUMBER_OF_OBJECTS / 4 * // SMALL_PAYLOAD.length bytes - Pointer pointer2 = memoryManagerService.allocate( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length, 0, 0 ); + Pointer pointer2 = memoryManagerService.allocate( Object.class, NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length, 0, 0 ); Assert.assertNull( pointer2 ); // Freeing the previously skipped pointer should then merge the whole @@ -188,7 +188,7 @@ public void testStoreAllocAndFree() // Should be able to allocate NUMBER_OF_OBJECTS / 4 * // SMALL_PAYLOAD.length bytes - Pointer pointer3 = memoryManagerService.allocate( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length, 0, 0 ); + Pointer pointer3 = memoryManagerService.allocate( Object.class, NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length, 0, 0 ); Assert.assertNotNull( pointer3 ); byte[] payload3 = MemoryTestUtils.generateRandomPayload( NUMBER_OF_OBJECTS / 4 * SMALL_PAYLOAD.length ); diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerTest.java index 9322603..4af6301 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerTest.java @@ -55,7 +55,7 @@ public void smokeTest() Random rnd = new Random(); int size = rnd.nextInt( 10 ) * (int) MemoryManager.capacity() / 100; logger.info( "payload size=" + Ram.inKb( size ) ); - Pointer p = MemoryManager.store( new byte[size] ); + Pointer p = MemoryManager.store( new byte[size] ); logger.info( "stored" ); assertNotNull( p ); //assertEquals(size,p.end); @@ -78,7 +78,7 @@ public void fillupTest() for ( int i = 0; i < howMany; i++ ) { - Pointer p = MemoryManager.store( payload ); + Pointer p = MemoryManager.store( payload ); assertNotNull( p ); } @@ -89,9 +89,9 @@ public void fillupTest() @Test public void readTest() { - for ( OffHeapMemoryBuffer buffer : MemoryManager.getBuffers() ) + for ( OffHeapMemoryBuffer buffer : MemoryManager.getBuffers() ) { - for ( Pointer ptr : ((OffHeapMemoryBufferImpl)buffer).getPointers() ) + for ( Pointer ptr : ((OffHeapMemoryBufferImpl) buffer).getPointers() ) { if ( !ptr.free ) { diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/NIOTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/NIOTest.java index 2e6da59..2a28406 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/NIOTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/NIOTest.java @@ -53,7 +53,7 @@ public static void init() for ( int i = 0; i < howMany; i++ ) { - Pointer p = MemoryManager.store( payload ); + Pointer p = MemoryManager.store( payload ); assertNotNull( p ); } @@ -61,12 +61,12 @@ public static void init() } @Test - public void NIOTest() + public void nioTest() { Random rnd = new Random(); int size = rnd.nextInt( 10 ) * (int) MemoryManager.capacity() / 100; logger.info( "payload size=" + Ram.inKb( size ) ); - Pointer p = MemoryManager.allocate( size ); + Pointer p = MemoryManager.allocate( size ); ByteBuffer b = p.directBuffer; logger.info( "allocated" ); assertNotNull( p ); diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/OffHeapMemoryBufferTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/OffHeapMemoryBufferTest.java index 7cdc80f..35c304b 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/OffHeapMemoryBufferTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/OffHeapMemoryBufferTest.java @@ -27,7 +27,7 @@ public class OffHeapMemoryBufferTest extends AbstractOffHeapMemoryBufferTest { - protected OffHeapMemoryBuffer instanciateOffHeapMemoryBuffer( int bufferSize ) + protected OffHeapMemoryBuffer instanciateOffHeapMemoryBuffer( int bufferSize ) { return OffHeapMemoryBufferImpl.createNew( bufferSize ); } @@ -37,13 +37,13 @@ public void testFullFillAndFreeAndClearBuffer() { // DIRECTMEMORY-40 : Pointers merging with adjacent free pointers when freeing. } - + @Test public void testStoreAllocAndFree() { // DIRECTMEMORY-40 : Pointers merging with adjacent free pointers when freeing. } - + @Test public void testUpdate() { diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/OffHeapMergingMemoryBufferTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/OffHeapMergingMemoryBufferTest.java index e1f19c9..9cdf147 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/OffHeapMergingMemoryBufferTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/OffHeapMergingMemoryBufferTest.java @@ -26,7 +26,7 @@ public class OffHeapMergingMemoryBufferTest extends AbstractOffHeapMemoryBufferTest { - protected OffHeapMemoryBuffer instanciateOffHeapMemoryBuffer( int bufferSize ) + protected OffHeapMemoryBuffer instanciateOffHeapMemoryBuffer( int bufferSize ) { return OffHeapMergingMemoryBufferImpl.createNew( bufferSize ); } diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java index 6a86da7..5c2418b 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java @@ -41,22 +41,22 @@ public class RoundRobinAllocationPolicyTest private static final int NUMBER_OF_BUFFERS = 4; - List buffers; + List> buffers; - RoundRobinAllocationPolicy allocationPolicy; + RoundRobinAllocationPolicy allocationPolicy; @Before public void initAllocationPolicy() { - buffers = new ArrayList(); + buffers = new ArrayList>(); for ( int i = 0; i < NUMBER_OF_BUFFERS; i++ ) { buffers.add( new DummyOffHeapMemoryBufferImpl() ); } - allocationPolicy = new RoundRobinAllocationPolicy(); + allocationPolicy = new RoundRobinAllocationPolicy(); allocationPolicy.init( buffers ); } @@ -108,7 +108,7 @@ public void testMaxAllocation() * Dummy {@link OffHeapMemoryBuffer} that do nothing. */ private static class DummyOffHeapMemoryBufferImpl - implements OffHeapMemoryBuffer + implements OffHeapMemoryBuffer { @Override @@ -130,31 +130,31 @@ public int getBufferNumber() } @Override - public Pointer store( byte[] payload ) + public Pointer store( byte[] payload ) { return null; } @Override - public Pointer store( byte[] payload, Date expires ) + public Pointer store( byte[] payload, Date expires ) { return null; } @Override - public Pointer store( byte[] payload, long expiresIn ) + public Pointer store( byte[] payload, long expiresIn ) { return null; } @Override - public byte[] retrieve( Pointer pointer ) + public byte[] retrieve( Pointer pointer ) { return null; } @Override - public int free( Pointer pointer2free ) + public int free( Pointer pointer2free ) { return 0; } @@ -187,14 +187,15 @@ public long collectLFU( int limit ) } @Override - public Pointer update( Pointer pointer, byte[] payload ) + public Pointer update( Pointer pointer, byte[] payload ) { return null; } @Override - public Pointer allocate( int size, long expiresIn, long expires ) + public Pointer allocate( Class type, int size, long expiresIn, long expires ) { + // TODO Auto-generated method stub return null; } } diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/Starter.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/Starter.java index b90c7b6..aa0b1fb 100644 --- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/Starter.java +++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/Starter.java @@ -59,7 +59,7 @@ public static void main( String[] args ) starter.rawInsertMultipleBuffers( buffers, mb, entries ); } - public void dump( OffHeapMemoryBuffer mem ) + public void dump( OffHeapMemoryBuffer mem ) { logger.info( "off-heap - buffer: " + mem.getBufferNumber() ); logger.info( "off-heap - allocated: " + Ram.inMb( mem.capacity() ) ); @@ -72,7 +72,7 @@ public void dump( OffHeapMemoryBuffer mem ) public void rawInsert( int megabytes, int howMany ) { - OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( megabytes * 1024 * 1024 ); + OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( megabytes * 1024 * 1024 ); assertNotNull( mem ); int size = mem.capacity() / ( howMany ); size -= size / 100 * 1; @@ -116,7 +116,7 @@ public void rawInsertMultipleBuffers( int buffers, int megabytes, int howMany ) logger.info( "...done in " + ( System.currentTimeMillis() - start ) + " msecs." ); logger.info( "---------------------------------" ); - for ( OffHeapMemoryBuffer buf : MemoryManager.getBuffers() ) + for ( OffHeapMemoryBuffer buf : MemoryManager.getBuffers() ) { dump( buf ); }