incubator-directmemory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bperr...@apache.org
Subject svn commit: r1228131 - in /incubator/directmemory/trunk/directmemory-cache/src: main/java/org/apache/directmemory/cache/ main/java/org/apache/directmemory/memory/ main/java/org/apache/directmemory/misc/ test/java/org/apache/directmemory/memory/test/
Date Fri, 06 Jan 2012 11:51:51 GMT
Author: bperroud
Date: Fri Jan  6 11:51:50 2012
New Revision: 1228131

URL: http://svn.apache.org/viewvc?rev=1228131&view=rev
Log:
DIRECTMEMORY-48 : Add OffHeapMemoryBuffer interface and abstraction\n- OffHeapMemoryBuffer is now an interface, OffHeapMemoryBufferImpl the default implementation\n- MemoryManagerServiceImpl has an overridable instanciateOffHeapMemoryBuffer function\n- new class org.apache.directmemory.misc.Util that hold crc32\n- MemoryManagerServiceImpl to try the first buffer when the last is full\n

Added:
    incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/AbstractOffHeapMemoryBuffer.java
    incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBufferImpl.java
    incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/misc/Util.java
Modified:
    incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java
    incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java
    incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java
    incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java
    incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java
    incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/BaseTest.java
    incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Concurrent2Test.java
    incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Concurrent3Test.java
    incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/ConcurrentTest.java
    incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MallocTest.java
    incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerServiceImplTest.java
    incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerTest.java
    incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTest.java
    incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Starter.java

Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.java Fri Jan  6 11:51:50 2012
@@ -297,7 +297,7 @@ public class CacheServiceImpl
 
     public void dump( OffHeapMemoryBuffer mem )
     {
-        logger.info( Format.it( "off-heap - buffer: \t%1d", mem.bufferNumber ) );
+        logger.info( Format.it( "off-heap - buffer: \t%1d", mem.getBufferNumber() ) );
         logger.info( Format.it( "off-heap - allocated: \t%1s", Ram.inMb( mem.capacity() ) ) );
         logger.info( Format.it( "off-heap - used:      \t%1s", Ram.inMb( mem.used() ) ) );
         logger.info( Format.it( "heap 	- max: \t%1s", Ram.inMb( Runtime.getRuntime().maxMemory() ) ) );

Added: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/AbstractOffHeapMemoryBuffer.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/AbstractOffHeapMemoryBuffer.java?rev=1228131&view=auto
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/AbstractOffHeapMemoryBuffer.java (added)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/AbstractOffHeapMemoryBuffer.java Fri Jan  6 11:51:50 2012
@@ -0,0 +1,236 @@
+package org.apache.directmemory.memory;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.josql.Query;
+import org.josql.QueryExecutionException;
+import org.josql.QueryParseException;
+import org.josql.QueryResults;
+import org.slf4j.Logger;
+
+public abstract class AbstractOffHeapMemoryBuffer
+    implements OffHeapMemoryBuffer
+{
+
+    protected final ByteBuffer buffer;
+
+    protected final AtomicInteger used = new AtomicInteger();
+
+    protected final int bufferNumber;
+
+    protected int allocationErrors = 0;
+
+    public static int maxAllocationErrors = 0;
+
+    protected abstract Logger getLogger();
+
+    public int used()
+    {
+        return used.get();
+    }
+
+    public int capacity()
+    {
+        return buffer.capacity();
+    }
+
+    public int getBufferNumber()
+    {
+        return bufferNumber;
+    }
+
+    protected AbstractOffHeapMemoryBuffer( ByteBuffer buffer, int bufferNumber )
+    {
+        this.buffer = buffer;
+        this.bufferNumber = bufferNumber;
+        //		createAndAddFirstPointer();
+    }
+
+    protected abstract Pointer createAndAddFirstPointer();
+
+    public Pointer store( byte[] payload )
+    {
+        return store( payload, -1 );
+    }
+
+    protected void freePointer( Pointer pointer2free )
+    {
+        pointer2free.free = true;
+        pointer2free.created = 0;
+        pointer2free.lastHit = 0;
+        pointer2free.hits = 0;
+        pointer2free.expiresIn = 0;
+        pointer2free.clazz = null;
+        pointer2free.directBuffer = null;
+        used.addAndGet( -pointer2free.getCapacity() );
+    }
+
+    public Pointer store( byte[] payload, Date expires )
+    {
+        return store( payload, 0, expires.getTime() );
+    }
+
+    public Pointer store( byte[] payload, long expiresIn )
+    {
+        return store( payload, expiresIn, 0 );
+    }
+
+    protected abstract Pointer store( byte[] payload, long expiresIn, long expires );
+
+    protected QueryResults select( String whereClause, List<Pointer> pointers )
+        throws QueryParseException, QueryExecutionException
+    {
+        Query q = new Query();
+        q.parse( "SELECT * FROM " + Pointer.class.getCanonicalName() + "  WHERE " + whereClause );
+        QueryResults qr = q.execute( pointers );
+        return qr;
+    }
+
+    protected QueryResults selectOrderBy( String whereClause, String orderBy, String limit, List<Pointer> pointers )
+        throws QueryParseException, QueryExecutionException
+    {
+        Query q = new Query();
+        q.parse( "SELECT * FROM " + Pointer.class.getCanonicalName() + "  WHERE " + whereClause + " order by "
+            + orderBy + " " + limit );
+        QueryResults qr = q.execute( pointers );
+        return qr;
+    }
+
+    protected boolean inShortage()
+    {
+        // a place holder for a more refined version
+        return allocationErrors > AbstractOffHeapMemoryBuffer.maxAllocationErrors;
+    }
+
+    @SuppressWarnings("unchecked")
+    protected List<Pointer> filter( final String whereClause, List<Pointer> pointers )
+    {
+        try
+        {
+            return select( whereClause, pointers ).getResults();
+        }
+        catch ( QueryParseException e )
+        {
+            e.printStackTrace();
+        }
+        catch ( QueryExecutionException e )
+        {
+            e.printStackTrace();
+        }
+        return (List<Pointer>) new ArrayList<Pointer>();
+    }
+
+    protected long free( List<Pointer> pointers )
+    {
+        long howMuch = 0;
+        for ( Pointer expired : pointers )
+        {
+            howMuch += free( expired );
+        }
+        return howMuch;
+    }
+
+    protected abstract List<Pointer> getUsedPointers();
+
+    public void disposeExpiredRelative()
+    {
+        free( filter( "free=false and expiresIn > 0 and (expiresIn+created) <= " + System.currentTimeMillis(),
+                      getUsedPointers() ) );
+    }
+
+    public void disposeExpiredAbsolute()
+    {
+        free( filter( "free=false and expires > 0 and (expires) <= " + System.currentTimeMillis(), getUsedPointers() ) );
+    }
+
+    public long collectExpired()
+    {
+        int limit = 50;
+        long disposed = free( filter( "free=false and expiresIn > 0 and (expiresIn+created) <= "
+                                          + System.currentTimeMillis() + " limit 1, " + limit, getUsedPointers() ) );
+        disposed += free( filter( "free=false and expires > 0 and (expires) <= " + System.currentTimeMillis()
+            + " limit 1, 100" + limit, getUsedPointers() ) );
+        return disposed;
+    }
+
+    public long collectLFU( int limit )
+    {
+        if ( !inShortage() )
+        {
+            return 0;
+        }
+        if ( limit <= 0 )
+            limit = getUsedPointers().size() / 10;
+        QueryResults qr;
+        try
+        {
+            qr = selectOrderBy( "free=false", "frequency", "limit 1, " + limit, getUsedPointers() );
+            @SuppressWarnings("unchecked")
+            List<Pointer> result = qr.getResults();
+            if ( result.size() > 0 )
+            {
+                // reset allocation errors if we made some room
+                allocationErrors = 0;
+            }
+            return free( result );
+        }
+        catch ( QueryParseException e )
+        {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        catch ( QueryExecutionException e )
+        {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        return 0;
+    }
+
+    public Pointer update( Pointer pointer, byte[] payload )
+    {
+        if ( payload.length > pointer.getCapacity() )
+        {
+            throw new BufferOverflowException();
+        }
+        free( pointer );
+        return store( payload );
+    }
+
+    public abstract Pointer allocate( int size, long expiresIn, long expires );
+
+    protected void resetPointer( final Pointer pointer ) 
+    {
+        pointer.free = true;
+        pointer.created = 0;
+        pointer.lastHit = 0;
+        pointer.hits = 0;
+        pointer.expiresIn = 0;
+        pointer.clazz = null;
+        pointer.directBuffer = null;
+    }
+}

Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java Fri Jan  6 11:51:50 2012
@@ -103,6 +103,6 @@ public class MemoryManager
 
     public static Pointer allocate( int size )
     {
-        return memoryManager.allocate( size, -1, -1 ); //add a version with expiry
+        return memoryManager.allocate( size, -1, -1 ); //add a version with expiration
     }
 }

Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerService.java Fri Jan  6 11:51:50 2012
@@ -9,7 +9,7 @@ package org.apache.directmemory.memory;
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *  http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
@@ -25,13 +25,54 @@ import java.util.List;
 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
+	 *            : size in B of internal buckets
+	 */
     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 );
 
+	/**
+	 * 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 update( Pointer pointer, byte[] payload );
+	/**
+	 * Same function as {@link #store(byte[])}, but add an absolute expiration date
+	 * @param payload : the data to store
+	 * @param expires : the absolute date the data will expire
+	 * @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);
 
     byte[] retrieve( Pointer pointer );
 

Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java Fri Jan  6 11:51:50 2012
@@ -9,7 +9,7 @@ package org.apache.directmemory.memory;
  * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *  http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
@@ -19,24 +19,22 @@ package org.apache.directmemory.memory;
  * under the License.
  */
 
-
+import java.util.ArrayList;
+import java.util.List;
 import org.apache.directmemory.measures.Ram;
 import org.apache.directmemory.misc.Format;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.List;
-import java.util.Vector;
-
 public class MemoryManagerServiceImpl
     implements MemoryManagerService
 {
 
-    protected static final Logger LOG = LoggerFactory.getLogger( MemoryManager.class );
+    protected static Logger logger = LoggerFactory.getLogger( MemoryManager.class );
 
-    protected List<OffHeapMemoryBuffer> buffers = new Vector<OffHeapMemoryBuffer>();
+    private List<OffHeapMemoryBuffer> buffers = new ArrayList<OffHeapMemoryBuffer>();
 
-    protected OffHeapMemoryBuffer activeBuffer;
+    private int activeBufferIndex = 0;
 
     public MemoryManagerServiceImpl()
     {
@@ -44,29 +42,34 @@ public class MemoryManagerServiceImpl
 
     public void init( int numberOfBuffers, int size )
     {
+        buffers = new ArrayList<OffHeapMemoryBuffer>( numberOfBuffers );
+
         for ( int i = 0; i < numberOfBuffers; i++ )
         {
-            buffers.add( OffHeapMemoryBuffer.createNew( size, i ) );
+            final OffHeapMemoryBuffer offHeapMemoryBuffer = instanciateOffHeapMemoryBuffer( size, i );
+            buffers.add( offHeapMemoryBuffer );
         }
-        activeBuffer = buffers.get( 0 );
-        LOG.info( Format.it( "MemoryManager initialized - %d buffers, %s each", numberOfBuffers, Ram.inMb( size ) ) );
+
+        logger.info( Format.it( "MemoryManager initialized - %d buffers, %s each", numberOfBuffers, Ram.inMb( size ) ) );
+    }
+
+    protected OffHeapMemoryBuffer instanciateOffHeapMemoryBuffer( int size, int bufferNumber )
+    {
+        return OffHeapMemoryBufferImpl.createNew( size, bufferNumber );
+    }
+
+    public OffHeapMemoryBuffer getActiveBuffer()
+    {
+        return buffers.get( activeBufferIndex );
     }
 
     public Pointer store( byte[] payload, int expiresIn )
     {
-        Pointer p = activeBuffer.store( payload, expiresIn );
+        Pointer p = getActiveBuffer().store( payload, expiresIn );
         if ( p == null )
         {
-            if ( activeBuffer.bufferNumber + 1 == buffers.size() )
-            {
-                return null;
-            }
-            else
-            {
-                // try next buffer
-                activeBuffer = buffers.get( activeBuffer.bufferNumber + 1 );
-                p = activeBuffer.store( payload, expiresIn );
-            }
+            nextBuffer();
+            p = getActiveBuffer().store( payload, expiresIn );
         }
         return p;
     }
@@ -78,21 +81,7 @@ public class MemoryManagerServiceImpl
 
     public Pointer update( Pointer pointer, byte[] payload )
     {
-        Pointer p = activeBuffer.update( pointer, payload );
-        if ( p == null )
-        {
-            if ( activeBuffer.bufferNumber == buffers.size() )
-            {
-                return null;
-            }
-            else
-            {
-                // try next buffer
-                activeBuffer = buffers.get( activeBuffer.bufferNumber + 1 );
-                p = activeBuffer.store( payload );
-            }
-        }
-        return p;
+        return buffers.get( pointer.bufferNumber ).update( pointer, payload );
     }
 
     public byte[] retrieve( Pointer pointer )
@@ -111,7 +100,7 @@ public class MemoryManagerServiceImpl
         {
             buffer.clear();
         }
-        activeBuffer = buffers.get( 0 );
+        activeBufferIndex = 0;
     }
 
     public long capacity()
@@ -152,33 +141,21 @@ public class MemoryManagerServiceImpl
         this.buffers = buffers;
     }
 
-    public OffHeapMemoryBuffer getActiveBuffer()
-    {
-        return activeBuffer;
-    }
-
-    public void setActiveBuffer( OffHeapMemoryBuffer activeBuffer )
-    {
-        this.activeBuffer = activeBuffer;
-    }
-
     @Override
     public Pointer allocate( int size, long expiresIn, long expires )
     {
-        Pointer p = activeBuffer.allocate( size, expiresIn, expires );
+        Pointer p = getActiveBuffer().allocate( size, expiresIn, expires );
         if ( p == null )
         {
-            if ( activeBuffer.bufferNumber + 1 == buffers.size() )
-            {
-                return null;
-            }
-            else
-            {
-                // try next buffer
-                activeBuffer = buffers.get( activeBuffer.bufferNumber + 1 );
-                p = activeBuffer.allocate( size, expiresIn, expires );
-            }
+            nextBuffer();
+            p = getActiveBuffer().allocate( size, expiresIn, expires );
         }
         return p;
     }
+
+    protected void nextBuffer()
+    {
+        activeBufferIndex = ( activeBufferIndex + 1 ) % buffers.size();
+    }
+
 }

Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBuffer.java Fri Jan  6 11:51:50 2012
@@ -19,366 +19,106 @@ package org.apache.directmemory.memory;
  * under the License.
  */
 
-import org.apache.directmemory.measures.Ram;
-import org.apache.directmemory.misc.Format;
-import org.josql.Query;
-import org.josql.QueryExecutionException;
-import org.josql.QueryParseException;
-import org.josql.QueryResults;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.nio.BufferOverflowException;
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
 import java.util.Date;
-import java.util.List;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.zip.CRC32;
-import java.util.zip.Checksum;
 
-public class OffHeapMemoryBuffer
+public interface OffHeapMemoryBuffer
 {
-    protected static Logger logger = LoggerFactory.getLogger( OffHeapMemoryBuffer.class );
-
-    protected ByteBuffer buffer;
-
-    //TODO: Variable 'pointers' must be private and have accessor methods.
-    public List<Pointer> pointers = new ArrayList<Pointer>();
-
-    protected final AtomicInteger used = new AtomicInteger();
-
-    public int bufferNumber;
-
-    public int allocationErrors = 0;
-
-    public static int maxAllocationErrors = 0;
-
-    public int used()
-    {
-        return used.get();
-    }
-
-    public int capacity()
-    {
-        return buffer.capacity();
-    }
-
-    public static OffHeapMemoryBuffer createNew( int capacity, int bufferNumber )
-    {
-        logger.info(
-            Format.it( "Creating OffHeapMemoryBuffer %d with a capacity of %s", bufferNumber, Ram.inMb( capacity ) ) );
-        return new OffHeapMemoryBuffer( ByteBuffer.allocateDirect( capacity ), bufferNumber );
-    }
-
-    public static OffHeapMemoryBuffer createNew( int capacity )
-    {
-        return new OffHeapMemoryBuffer( ByteBuffer.allocateDirect( capacity ), -1 );
-    }
-
-    protected OffHeapMemoryBuffer( ByteBuffer buffer, int bufferNumber )
-    {
-        this.buffer = buffer;
-        this.bufferNumber = bufferNumber;
-        createAndAddFirstPointer();
-    }
-
-    private Pointer createAndAddFirstPointer()
-    {
-        Pointer first = new Pointer();
-        first.bufferNumber = bufferNumber;
-        first.start = 0;
-        first.free = true;
-        first.end = buffer.capacity() - 1;
-        pointers.add( first );
-        return first;
-    }
-
-    protected Pointer slice( Pointer existing, int capacity )
-    {
-        Pointer fresh = new Pointer();
-        fresh.bufferNumber = existing.bufferNumber;
-        fresh.start = existing.start;
-        fresh.end = fresh.start + capacity - 1; // 0 indexed
-        fresh.free = true;
-        existing.start = fresh.end + 1; // more readable
-        return fresh;
-    }
-
-
-    protected Pointer firstMatch( int capacity )
-    {
-        for ( Pointer ptr : pointers )
-        {
-            if (ptr.free && ptr.getCapacity() >= capacity)
-            {
-                return ptr;
-            }
-        }
-        return null;
-    }
-
-    public Pointer store( byte[] payload )
-    {
-        return store( payload, -1 );
-    }
-
-    public byte[] retrieve( Pointer pointer )
-    {
-        pointer.lastHit = System.currentTimeMillis();
-        pointer.hits++;
-
-        ByteBuffer buf;
-        if ( pointer.clazz == ByteBuffer.class )
-        {
-            buf = pointer.directBuffer;
-            buf.position( 0 );
-        }
-        else
-        {
-            synchronized ( buffer )
-            {
-                buf = buffer.duplicate();
-                buf.position( pointer.start );
-            }
-        }
-
-        final byte[] swp = new byte[pointer.getCapacity()];
-        buf.get( swp );
-        return swp;
-    }
-
-
-    public int free( Pointer pointer2free )
-    {
-        pointer2free.free = true;
-        pointer2free.created = 0;
-        pointer2free.lastHit = 0;
-        pointer2free.hits = 0;
-        pointer2free.expiresIn = 0;
-        pointer2free.clazz = null;
-        pointer2free.directBuffer = null;
-        used.addAndGet( - pointer2free.getCapacity() );
-        return pointer2free.getCapacity();
-    }
-
-    public void clear()
-    {
-        allocationErrors = 0;
-        pointers.clear();
-        createAndAddFirstPointer();
-        buffer.clear();
-        used.set( 0 );
-    }
-
-    public Pointer store( byte[] payload, Date expires )
-    {
-        return store( payload, 0, expires.getTime() );
-    }
-
-    public Pointer store( byte[] payload, long expiresIn )
-    {
-        return store( payload, expiresIn, 0 );
-    }
-
-    protected synchronized Pointer store( byte[] payload, long expiresIn, long expires )
-    {
-        Pointer goodOne = firstMatch( payload.length );
-
-        if ( goodOne == null )
-        {
-            allocationErrors++;
-            return null;
-        }
-
-        Pointer fresh = slice( goodOne, payload.length );
-
-        fresh.created = System.currentTimeMillis();
-        if ( expiresIn > 0 )
-        {
-            fresh.expiresIn = expiresIn;
-            fresh.expires = 0;
-        }
-        else if ( expires > 0 )
-        {
-            fresh.expiresIn = 0;
-            fresh.expires = expires;
-        }
-
-        fresh.free = false;
-        used.addAndGet( payload.length );
-        ByteBuffer buf = buffer.slice();
-        buf.position( fresh.start );
-        try
-        {
-            buf.put( payload );
-        }
-        catch ( BufferOverflowException e )
-        {
-            // RpG not convincing - let's fix it later
-            goodOne.start = fresh.start;
-            goodOne.end = buffer.limit();
-            return null;
-        }
-        pointers.add( fresh );
-        return fresh;
-    }
-
-    protected QueryResults select( String whereClause )
-        throws QueryParseException, QueryExecutionException
-    {
-        Query q = new Query();
-        q.parse( "SELECT * FROM " + Pointer.class.getCanonicalName() + "  WHERE " + whereClause );
-        return q.execute( pointers );
-    }
-
-    protected QueryResults selectOrderBy( String whereClause, String orderBy, String limit )
-        throws QueryParseException, QueryExecutionException
-    {
-        Query q = new Query();
-        q.parse( "SELECT * FROM " + Pointer.class.getCanonicalName() + "  WHERE " + whereClause + " order by " + orderBy
-                     + " " + limit );
-        return q.execute( pointers );
-    }
-
-    private boolean inShortage()
-    {
-        // a place holder for a more refined version
-        return allocationErrors > OffHeapMemoryBuffer.maxAllocationErrors;
-    }
-
-    public long collectLFU( int limit )
-    {
-        if ( !inShortage() )
-        {
-            return 0;
-        }
-        if ( limit <= 0 )
-        {
-            limit = pointers.size() / 10;
-        }
-        QueryResults qr;
-        try
-        {
-            qr = selectOrderBy( "free=false", "frequency", "limit 1, " + limit );
-            @SuppressWarnings( "unchecked" ) List<Pointer> result = qr.getResults();
-            if ( result.size() > 0 )
-            {
-                // reset allocation errors if we made some room
-                allocationErrors = 0;
-            }
-            return free( result );
-        }
-        catch ( QueryParseException e )
-        {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-        catch ( QueryExecutionException e )
-        {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
-        return 0;
-    }
-
-
-    @SuppressWarnings( "unchecked" )
-    private List<Pointer> filter( final String whereClause )
-    {
-        try
-        {
-            return select( whereClause ).getResults();
-        }
-        catch ( QueryParseException e )
-        {
-            e.printStackTrace();
-        }
-        catch ( QueryExecutionException e )
-        {
-            e.printStackTrace();
-        }
-        return new ArrayList<Pointer>();
-    }
-
-    private long free( List<Pointer> expiredPointers )
-    {
-        long howMuch = 0;
-        for ( Pointer expired : expiredPointers )
-        {
-            howMuch += free( expired );
-        }
-        return howMuch;
-    }
-
-    public void disposeExpiredRelative()
-    {
-        free( filter( "free=false and expiresIn > 0 and (expiresIn+created) <= " + System.currentTimeMillis() ) );
-    }
-
-    public void disposeExpiredAbsolute()
-    {
-        free( filter( "free=false and expires > 0 and (expires) <= " + System.currentTimeMillis() ) );
-    }
-
-    public long collectExpired()
-    {
-        int limit = 50;
-        long disposed = free( filter(
-            "free=false and expiresIn > 0 and (expiresIn+created) <= " + System.currentTimeMillis() + " limit 1, "
-                + limit ) );
-        disposed += free( filter(
-            "free=false and expires > 0 and (expires) <= " + System.currentTimeMillis() + " limit 1, 100" + limit ) );
-        return disposed;
-    }
-
-    public static long crc32( byte[] payload )
-    {
-        final Checksum checksum = new CRC32();
-        checksum.update( payload, 0, payload.length );
-        return checksum.getValue();
-    }
-
-    public Pointer update( Pointer pointer, byte[] payload )
-    {
-        free( pointer );
-        return store( payload );
-    }
-
-    public synchronized Pointer allocate( int size, long expiresIn, long expires )
-    {
-        Pointer goodOne = firstMatch( size );
-
-        if ( goodOne == null )
-        {
-            allocationErrors++;
-            return null;
-        }
-
-        Pointer fresh = slice( goodOne, size );
-
-        fresh.created = System.currentTimeMillis();
-        if ( expiresIn > 0 )
-        {
-            fresh.expiresIn = expiresIn;
-            fresh.expires = 0;
-        }
-        else if ( expires > 0 )
-        {
-            fresh.expiresIn = 0;
-            fresh.expires = expires;
-        }
-
-        fresh.free = false;
-        used.addAndGet( size );
-        ByteBuffer buf = buffer.slice();
-        buf.position( fresh.start );
-
-        fresh.directBuffer = buf.slice();
-        fresh.directBuffer.limit( size );
-        fresh.clazz = ByteBuffer.class;
-        pointers.add( fresh );
-        return fresh;
-    }
 
+    /**
+     * @return the current usage of the buffer
+     */
+    public int used();
+
+    /**
+     * @return the capacity of the buffer
+     */
+    public int capacity();
+
+    /**
+     * @return buffer identity number
+     */
+    public int getBufferNumber();
+
+    /**
+     * Store given payload in the buffer, returning the pointer on success, null is the storing failed (mostly buffer full)
+     * @param payload : the data to store
+     * @return the pointer where the data is stored, null in case of failure
+     */
+    public Pointer store( byte[] payload );
+
+    /**
+     * Same as {@link #store(byte[])}, with an absolute expiration date
+     * @param payload : the data to store
+     * @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 );
+
+    /**
+     * Same as {@link #store(byte[])}, with an relative expiration delta
+     * @param payload : the data to store
+     * @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 );
+
+    /**
+     * Return previously stored data associated to the given pointer
+     * @param pointer : presiously allocated pointer
+     * @return previously stored data
+     */
+    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 );
+
+    /**
+     * Completely empty the buffer
+     */
+    public void clear();
+
+    /**
+     * TODO
+     */
+    public void disposeExpiredRelative();
+
+    /**
+     * TODO
+     */
+    public void disposeExpiredAbsolute();
+
+    /**
+     * Iterate through the buffer and free data that have expired
+     * @return newly freed space
+     */
+    public long collectExpired();
+
+    /**
+     * Free the least recently used data
+     * @param limit : number of pointers to free. Setting a negative value will free 10% of the objects
+     * @return the amount of freed space
+     */
+    public long collectLFU( int limit );
+
+    /**
+     * Update the data at a given pointer
+     * @param pointer
+     * @param payload : the data to update
+     * @return the update pointer. It may be a new pointer.
+     */
+    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 );
 }

Added: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBufferImpl.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBufferImpl.java?rev=1228131&view=auto
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBufferImpl.java (added)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/OffHeapMemoryBufferImpl.java Fri Jan  6 11:51:50 2012
@@ -0,0 +1,316 @@
+package org.apache.directmemory.memory;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.zip.CRC32;
+import java.util.zip.Checksum;
+
+import org.apache.directmemory.measures.Ram;
+import org.apache.directmemory.misc.Format;
+import org.josql.Query;
+import org.josql.QueryExecutionException;
+import org.josql.QueryParseException;
+import org.josql.QueryResults;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class OffHeapMemoryBufferImpl
+    extends AbstractOffHeapMemoryBuffer
+{
+
+    protected static Logger logger = LoggerFactory.getLogger( OffHeapMemoryBufferImpl.class );
+
+    protected final List<Pointer> pointers = new ArrayList<Pointer>();
+
+    protected Logger getLogger()
+    {
+        return logger;
+    }
+
+    protected List<Pointer> getUsedPointers()
+    {
+        return pointers;
+    }
+
+    public List<Pointer> getPointers()
+    {
+        return pointers;
+    }
+
+    public static OffHeapMemoryBufferImpl createNew( int capacity, int bufferNumber )
+    {
+        logger.info( Format.it( "Creating OffHeapMemoryBuffer %d with a capacity of %s", bufferNumber,
+                                Ram.inMb( capacity ) ) );
+        return new OffHeapMemoryBufferImpl( ByteBuffer.allocateDirect( capacity ), bufferNumber );
+    }
+
+    public static OffHeapMemoryBufferImpl createNew( int capacity )
+    {
+        return new OffHeapMemoryBufferImpl( ByteBuffer.allocateDirect( capacity ), -1 );
+    }
+
+    protected OffHeapMemoryBufferImpl( ByteBuffer buffer, int bufferNumber )
+    {
+        super( buffer, bufferNumber );
+        createAndAddFirstPointer();
+    }
+
+    protected Pointer createAndAddFirstPointer()
+    {
+        Pointer first = new Pointer();
+        first.bufferNumber = bufferNumber;
+        first.start = 0;
+        first.free = true;
+        first.end = buffer.capacity() - 1;
+        pointers.add( first );
+        return first;
+    }
+
+    protected Pointer slice( Pointer existing, int capacity )
+    {
+        Pointer fresh = new Pointer();
+        fresh.bufferNumber = existing.bufferNumber;
+        fresh.start = existing.start;
+        fresh.end = fresh.start + capacity - 1; // 0 indexed
+        fresh.free = true;
+        existing.start = fresh.end + 1; // more readable
+        return fresh;
+    }
+
+    protected Pointer firstMatch( int capacity )
+    {
+        for ( Pointer ptr : pointers )
+        {
+            if ( ptr.free && ptr.getCapacity() >= capacity )
+            {
+                return ptr;
+            }
+        }
+        return null;
+    }
+
+    public Pointer store( byte[] payload )
+    {
+        return store( payload, -1 );
+    }
+
+    public byte[] retrieve( Pointer pointer )
+    {
+        pointer.lastHit = System.currentTimeMillis();
+        pointer.hits++;
+
+        ByteBuffer buf = null;
+        if ( pointer.clazz == ByteBuffer.class )
+        {
+            buf = pointer.directBuffer;
+            buf.position( 0 );
+        }
+        else
+        {
+            synchronized ( buffer )
+            {
+                buf = buffer.duplicate();
+                buf.position( pointer.start );
+            }
+        }
+
+        final byte[] swp = new byte[pointer.getCapacity()];
+        buf.get( swp );
+        return swp;
+    }
+
+    public synchronized int free( Pointer pointer2free )
+    {
+        resetPointer( pointer2free );
+        used.addAndGet( -pointer2free.getCapacity() );
+        return pointer2free.getCapacity();
+    }
+
+    public synchronized void clear()
+    {
+        allocationErrors = 0;
+        pointers.clear();
+        createAndAddFirstPointer();
+        buffer.clear();
+        used.set( 0 );
+    }
+
+    public Pointer store( byte[] payload, Date expires )
+    {
+        return store( payload, 0, expires.getTime() );
+    }
+
+    public Pointer store( byte[] payload, long expiresIn )
+    {
+        return store( payload, expiresIn, 0 );
+    }
+
+    protected synchronized Pointer store( byte[] payload, long expiresIn, long expires )
+    {
+        Pointer goodOne = firstMatch( payload.length );
+
+        if ( goodOne == null )
+        {
+            allocationErrors++;
+            return null;
+        }
+
+        Pointer fresh = slice( goodOne, payload.length );
+
+        fresh.created = System.currentTimeMillis();
+        if ( expiresIn > 0 )
+        {
+            fresh.expiresIn = expiresIn;
+            fresh.expires = 0;
+        }
+        else if ( expires > 0 )
+        {
+            fresh.expiresIn = 0;
+            fresh.expires = expires;
+        }
+
+        fresh.free = false;
+        used.addAndGet( payload.length );
+        ByteBuffer buf = buffer.slice();
+        buf.position( fresh.start );
+        try
+        {
+            buf.put( payload );
+        }
+        catch ( BufferOverflowException e )
+        {
+            // RpG not convincing - let's fix it later
+            goodOne.start = fresh.start;
+            goodOne.end = buffer.limit();
+            return null;
+        }
+        pointers.add( fresh );
+        return fresh;
+    }
+
+    protected QueryResults select( String whereClause )
+        throws QueryParseException, QueryExecutionException
+    {
+        Query q = new Query();
+        q.parse( "SELECT * FROM " + Pointer.class.getCanonicalName() + "  WHERE " + whereClause );
+        QueryResults qr = q.execute( pointers );
+        return qr;
+    }
+
+    protected QueryResults selectOrderBy( String whereClause, String orderBy, String limit )
+        throws QueryParseException, QueryExecutionException
+    {
+        Query q = new Query();
+        q.parse( "SELECT * FROM " + Pointer.class.getCanonicalName() + "  WHERE " + whereClause + " order by "
+            + orderBy + " " + limit );
+        QueryResults qr = q.execute( pointers );
+        return qr;
+    }
+
+    protected boolean inShortage()
+    {
+        // a place holder for a more refined version
+        return allocationErrors > OffHeapMemoryBufferImpl.maxAllocationErrors;
+    }
+
+    @SuppressWarnings("unchecked")
+    protected List<Pointer> filter( final String whereClause )
+    {
+        try
+        {
+            return select( whereClause ).getResults();
+        }
+        catch ( QueryParseException e )
+        {
+            e.printStackTrace();
+        }
+        catch ( QueryExecutionException e )
+        {
+            e.printStackTrace();
+        }
+        return (List<Pointer>) new ArrayList<Pointer>();
+    }
+
+    protected long free( List<Pointer> pointers )
+    {
+        long howMuch = 0;
+        for ( Pointer expired : pointers )
+        {
+            howMuch += free( expired );
+        }
+        return howMuch;
+    }
+
+    // TODO : This function should be put in an Util class. 
+    public static long crc32( byte[] payload )
+    {
+        final Checksum checksum = new CRC32();
+        checksum.update( payload, 0, payload.length );
+        return checksum.getValue();
+    }
+
+    public Pointer update( Pointer pointer, byte[] payload )
+    {
+        free( pointer );
+        return store( payload );
+    }
+
+    public synchronized Pointer allocate( int size, long expiresIn, long expires )
+    {
+        Pointer goodOne = firstMatch( size );
+
+        if ( goodOne == null )
+        {
+            allocationErrors++;
+            return null;
+        }
+
+        Pointer fresh = slice( goodOne, size );
+
+        fresh.created = System.currentTimeMillis();
+        if ( expiresIn > 0 )
+        {
+            fresh.expiresIn = expiresIn;
+            fresh.expires = 0;
+        }
+        else if ( expires > 0 )
+        {
+            fresh.expiresIn = 0;
+            fresh.expires = expires;
+        }
+
+        fresh.free = false;
+        used.addAndGet( size );
+        ByteBuffer buf = buffer.slice();
+        buf.position( fresh.start );
+
+        fresh.directBuffer = buf.slice();
+        fresh.directBuffer.limit( size );
+        fresh.clazz = ByteBuffer.class;
+        pointers.add( fresh );
+        return fresh;
+    }
+
+}

Added: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/misc/Util.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/misc/Util.java?rev=1228131&view=auto
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/misc/Util.java (added)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/misc/Util.java Fri Jan  6 11:51:50 2012
@@ -0,0 +1,35 @@
+package org.apache.directmemory.misc;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.zip.CRC32;
+import java.util.zip.Checksum;
+
+public class Util
+{
+
+    public static long crc32( byte[] payload )
+    {
+        final Checksum checksum = new CRC32();
+        checksum.update( payload, 0, payload.length );
+        return checksum.getValue();
+    }
+    
+}

Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/BaseTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/BaseTest.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/BaseTest.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/BaseTest.java Fri Jan  6 11:51:50 2012
@@ -24,7 +24,9 @@ import com.carrotsearch.junitbenchmarks.
 import com.google.common.collect.Maps;
 import org.apache.directmemory.measures.Ram;
 import org.apache.directmemory.memory.OffHeapMemoryBuffer;
+import org.apache.directmemory.memory.OffHeapMemoryBufferImpl;
 import org.apache.directmemory.memory.Pointer;
+import org.apache.directmemory.misc.Util;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
@@ -48,7 +50,7 @@ public class BaseTest
     @Test
     public void smokeTest()
     {
-        OffHeapMemoryBuffer mem = OffHeapMemoryBuffer.createNew( 1 * 1024 * 1024 );
+        OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 1 * 1024 * 1024 );
         logger.info( "buffer size=" + mem.capacity() );
         assertNotNull( mem );
 
@@ -115,7 +117,7 @@ public class BaseTest
     @Test
     public void aFewEntriesWithRead()
     {
-        OffHeapMemoryBuffer mem = OffHeapMemoryBuffer.createNew( 100 * 1024 * 1024 );
+        OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 100 * 1024 * 1024 );
         logger.info( "total capacity=" + Ram.inMb( mem.capacity() ) );
         assertNotNull( mem );
         int howMany = 10000;
@@ -129,8 +131,8 @@ public class BaseTest
             final byte[] check = mem.retrieve( p );
             assertNotNull( check );
             assertEquals( test + " - " + i, new String( check ) );
-            long crc1 = OffHeapMemoryBuffer.crc32( payload );
-            long crc2 = OffHeapMemoryBuffer.crc32( check );
+            long crc1 = Util.crc32( payload );
+            long crc2 = Util.crc32( check );
             assertEquals( crc1, crc2 );
         }
 
@@ -140,7 +142,7 @@ public class BaseTest
     @Test
     public void aFewEntriesWithCheck()
     {
-        OffHeapMemoryBuffer mem = OffHeapMemoryBuffer.createNew( 10 * 1024 * 1024 );
+        OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 10 * 1024 * 1024 );
         logger.info( "total capacity=" + Ram.inMb( mem.capacity() ) );
         assertNotNull( mem );
         int howMany = 10;
@@ -170,7 +172,7 @@ public class BaseTest
     public void checkExpiration()
         throws InterruptedException
     {
-        OffHeapMemoryBuffer mem = OffHeapMemoryBuffer.createNew( 10 * 1024 * 1024 );
+        OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 10 * 1024 * 1024 );
         assertNotNull( mem );
         int size = 400;
         int howMany = 5000;

Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Concurrent2Test.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Concurrent2Test.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Concurrent2Test.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Concurrent2Test.java Fri Jan  6 11:51:50 2012
@@ -206,7 +206,7 @@ public class Concurrent2Test
 
     private static void dump( OffHeapMemoryBuffer mem )
     {
-        logger.info( "off-heap - buffer: " + mem.bufferNumber );
+        logger.info( "off-heap - buffer: " + mem.getBufferNumber() );
         logger.info( "off-heap - allocated: " + Ram.inMb( mem.capacity() ) );
         logger.info( "off-heap - used:      " + Ram.inMb( mem.used() ) );
         logger.info( "heap 	  - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );

Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Concurrent3Test.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Concurrent3Test.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Concurrent3Test.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Concurrent3Test.java Fri Jan  6 11:51:50 2012
@@ -228,7 +228,7 @@ public class Concurrent3Test
 
     private static void dump( OffHeapMemoryBuffer mem )
     {
-        logger.info( "off-heap - buffer: " + mem.bufferNumber );
+        logger.info( "off-heap - buffer: " + mem.getBufferNumber() );
         logger.info( "off-heap - allocated: " + Ram.inMb( mem.capacity() ) );
         logger.info( "off-heap - used:      " + Ram.inMb( mem.used() ) );
         logger.info( "heap 	  - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );

Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/ConcurrentTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/ConcurrentTest.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/ConcurrentTest.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/ConcurrentTest.java Fri Jan  6 11:51:50 2012
@@ -28,6 +28,7 @@ import com.carrotsearch.junitbenchmarks.
 import com.google.common.collect.MapMaker;
 import org.apache.directmemory.measures.Ram;
 import org.apache.directmemory.memory.OffHeapMemoryBuffer;
+import org.apache.directmemory.memory.OffHeapMemoryBufferImpl;
 import org.apache.directmemory.memory.Pointer;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -63,7 +64,7 @@ public class ConcurrentTest
 
     private static AtomicInteger read = new AtomicInteger();
 
-    public static OffHeapMemoryBuffer mem = OffHeapMemoryBuffer.createNew( 512 * 1024 * 1024 );
+    public static OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 512 * 1024 * 1024 );
 
     public static ConcurrentMap<String, Pointer> map =
         new MapMaker().concurrencyLevel( 4 ).initialCapacity( 100000 ).makeMap();

Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MallocTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MallocTest.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MallocTest.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MallocTest.java Fri Jan  6 11:51:50 2012
@@ -28,6 +28,7 @@ import com.carrotsearch.junitbenchmarks.
 import com.google.common.collect.MapMaker;
 import org.apache.directmemory.measures.Ram;
 import org.apache.directmemory.memory.OffHeapMemoryBuffer;
+import org.apache.directmemory.memory.OffHeapMemoryBufferImpl;
 import org.apache.directmemory.memory.Pointer;
 import org.junit.After;
 import org.junit.Before;
@@ -67,7 +68,7 @@ public class MallocTest
         logger.info( "************************************************" );
     }
 
-    OffHeapMemoryBuffer mem = OffHeapMemoryBuffer.createNew( 512 * 1024 * 1024 );
+    OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( 512 * 1024 * 1024 );
 
     @Test
     public void oneMillionEntries()

Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerServiceImplTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerServiceImplTest.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerServiceImplTest.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerServiceImplTest.java Fri Jan  6 11:51:50 2012
@@ -2,25 +2,30 @@ package org.apache.directmemory.memory.t
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
+ * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
+ * regarding copyright ownership.  The ASF licenses this file
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- * 
- * http://www.apache.org/licenses/LICENSE-2.0
- * 
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
+ * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
 import junit.framework.Assert;
 
+import org.apache.directmemory.memory.MemoryManagerService;
 import org.apache.directmemory.memory.MemoryManagerServiceImpl;
 import org.apache.directmemory.memory.Pointer;
 import org.junit.Test;
@@ -28,17 +33,25 @@ import org.junit.Test;
 public class MemoryManagerServiceImplTest
 {
 
+    private static final Random R = new Random();
+
+    private static final char PAYLOAD_CHAR = 'X';
+
+    private static final String PAYLOAD_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
     private static final byte[] SMALL_PAYLOAD = "ABCD".getBytes();
 
     @Test
     public void testFirstMatchBorderCase()
     {
 
-        // Storing a first payload of 4 bytes, 1 byte remaining in the buffer. When storing a second 4 bytes payload, an BufferOverflowException is thrown instead of an easy check.
+        // Storing a first payload of 4 bytes, 1 byte remaining in the buffer.
+        // When storing a second 4 bytes payload, an BufferOverflowException is
+        // thrown instead of an easy check.
 
         final int BUFFER_SIZE = 5;
 
-        final MemoryManagerServiceImpl memoryManagerService = new MemoryManagerServiceImpl();
+        final MemoryManagerService memoryManagerService = new MemoryManagerServiceImpl();
 
         memoryManagerService.init( 1, BUFFER_SIZE );
 
@@ -54,7 +67,8 @@ public class MemoryManagerServiceImplTes
     public void testAllocateMultipleBuffers()
     {
 
-        // Initializing 4 buffers of 4 bytes, MemoryManagerService should search for available space in another buffer.
+        // Initializing 4 buffers of 4 bytes, MemoryManagerService should search
+        // for available space in another buffer.
 
         final int NUMBER_OF_OBJECTS = 4;
 
@@ -76,7 +90,8 @@ public class MemoryManagerServiceImplTes
     public void testByteLeaking()
     {
 
-        // Initializing 1 buffer of 10*4 bytes, should be able to allocate 10 objects of 4 bytes.
+        // Initializing 1 buffer of 10*4 bytes, should be able to allocate 10
+        // objects of 4 bytes.
 
         final int NUMBER_OF_OBJECTS = 10;
 
@@ -97,7 +112,8 @@ public class MemoryManagerServiceImplTes
     public void testReportCorrectUsedMemory()
     {
 
-        // Initializing 1 buffer of 4*4 bytes, storing and freeing and storing again should report correct numbers.
+        // Initializing 1 buffer of 4*4 bytes, storing and freeing and storing
+        // again should report correct numbers.
 
         final int NUMBER_OF_OBJECTS = 4;
         final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD.length;
@@ -128,4 +144,70 @@ public class MemoryManagerServiceImplTes
 
     }
 
+    @Test
+    public void testRandomPayload()
+    {
+
+        final int NUMBER_OF_OBJECTS = 10;
+        final int BUFFER_SIZE = NUMBER_OF_OBJECTS * SMALL_PAYLOAD.length;
+
+        final MemoryManagerServiceImpl memoryManagerService = new MemoryManagerServiceImpl();
+
+        memoryManagerService.init( 1, BUFFER_SIZE );
+
+        for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ )
+        {
+            byte[] payload = generateRandomPayload( SMALL_PAYLOAD.length );
+            Pointer pointer = memoryManagerService.store( payload );
+            Assert.assertNotNull( pointer );
+            byte[] fetchedPayload = memoryManagerService.retrieve( pointer );
+            Assert.assertEquals( new String( payload ), new String( fetchedPayload ) );
+            if ( R.nextBoolean() )
+            {
+                memoryManagerService.free( pointer );
+            }
+        }
+
+        memoryManagerService.clear();
+
+        for ( int i = 0; i < NUMBER_OF_OBJECTS; i++ )
+        {
+            byte[] payload = generateRandomPayload( SMALL_PAYLOAD.length );
+            Pointer pointer = memoryManagerService.store( payload );
+            Assert.assertNotNull( pointer );
+            byte[] fetchedPayload = memoryManagerService.retrieve( pointer );
+            Assert.assertEquals( new String( payload ), new String( fetchedPayload ) );
+            if ( R.nextBoolean() )
+            {
+                memoryManagerService.free( pointer );
+                i--;
+            }
+        }
+
+        memoryManagerService.clear();
+
+        Pointer pointer = null;
+        do
+        {
+            byte[] payload = generateRandomPayload( R.nextInt( BUFFER_SIZE / 4 + 1 ) );
+            pointer = memoryManagerService.store( payload );
+            if ( pointer != null && R.nextBoolean() )
+            {
+                memoryManagerService.free( pointer );
+            }
+        }
+        while ( pointer != null );
+
+    }
+
+    private static byte[] generateRandomPayload( int sizeInByte )
+    {
+        final StringBuilder sb = new StringBuilder( sizeInByte );
+        for ( int i = 0; i < sizeInByte; i++ )
+        {
+            sb.append( PAYLOAD_CHARS.charAt( R.nextInt( PAYLOAD_CHARS.length() ) ) );
+        }
+        return sb.toString().getBytes();
+    }
+
 }

Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerTest.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerTest.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/MemoryManagerTest.java Fri Jan  6 11:51:50 2012
@@ -24,6 +24,7 @@ import com.google.common.collect.Maps;
 import org.apache.directmemory.measures.Ram;
 import org.apache.directmemory.memory.MemoryManager;
 import org.apache.directmemory.memory.OffHeapMemoryBuffer;
+import org.apache.directmemory.memory.OffHeapMemoryBufferImpl;
 import org.apache.directmemory.memory.Pointer;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -90,7 +91,7 @@ public class MemoryManagerTest
     {
         for ( OffHeapMemoryBuffer buffer : MemoryManager.getBuffers() )
         {
-            for ( Pointer ptr : buffer.pointers )
+            for ( Pointer ptr : ((OffHeapMemoryBufferImpl)buffer).getPointers() )
             {
                 if ( !ptr.free )
                 {

Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTest.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTest.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTest.java Fri Jan  6 11:51:50 2012
@@ -1,88 +1,88 @@
-package org.apache.directmemory.memory.test;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.apache.directmemory.measures.Ram;
-import org.apache.directmemory.memory.MemoryManager;
-import org.apache.directmemory.memory.Pointer;
-import org.junit.BeforeClass;
-import org.junit.Ignore;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.nio.ByteBuffer;
-import java.util.Random;
-
-import static org.junit.Assert.*;
-
-@Ignore
-public class NIOTest
-{
-
-    private static Logger logger = LoggerFactory.getLogger( NIOTest.class );
-
-    @BeforeClass
-    public static void init()
-    {
-        byte[] payload = "012345678901234567890123456789012345678901234567890123456789".getBytes();
-
-        logger.info( "init" );
-        MemoryManager.init( 1, Ram.Mb( 100 ) );
-
-        logger.info( "payload size=" + Ram.inKb( payload.length ) );
-        long howMany = ( MemoryManager.capacity() / payload.length );
-        howMany = ( howMany * 50 ) / 100;
-
-        for ( int i = 0; i < howMany; i++ )
-        {
-            Pointer p = MemoryManager.store( payload );
-            assertNotNull( p );
-        }
-
-        logger.info( "" + howMany + " items stored" );
-    }
-
-    @Test
-    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 );
-        ByteBuffer b = p.directBuffer;
-        logger.info( "allocated" );
-        assertNotNull( p );
-        assertNotNull( b );
-
-        assertTrue( b.isDirect() );
-        assertEquals( 0, b.position() );
-        assertEquals( size, b.limit() );
-
-        byte[] check = MemoryManager.retrieve( p );
-
-        assertNotNull( check );
-
-        assertEquals( size, p.getCapacity() );
-        logger.info( "end" );
-    }
-
-
-}
+package org.apache.directmemory.memory.test;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.directmemory.measures.Ram;
+import org.apache.directmemory.memory.MemoryManager;
+import org.apache.directmemory.memory.Pointer;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.ByteBuffer;
+import java.util.Random;
+
+import static org.junit.Assert.*;
+
+@Ignore
+public class NIOTest
+{
+
+    private static Logger logger = LoggerFactory.getLogger( NIOTest.class );
+
+    @BeforeClass
+    public static void init()
+    {
+        byte[] payload = "012345678901234567890123456789012345678901234567890123456789".getBytes();
+
+        logger.info( "init" );
+        MemoryManager.init( 1, Ram.Mb( 100 ) );
+
+        logger.info( "payload size=" + Ram.inKb( payload.length ) );
+        long howMany = ( MemoryManager.capacity() / payload.length );
+        howMany = ( howMany * 50 ) / 100;
+
+        for ( int i = 0; i < howMany; i++ )
+        {
+            Pointer p = MemoryManager.store( payload );
+            assertNotNull( p );
+        }
+
+        logger.info( "" + howMany + " items stored" );
+    }
+
+    @Test
+    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 );
+        ByteBuffer b = p.directBuffer;
+        logger.info( "allocated" );
+        assertNotNull( p );
+        assertNotNull( b );
+
+        assertTrue( b.isDirect() );
+        assertEquals( 0, b.position() );
+        assertEquals( size, b.limit() );
+
+        byte[] check = MemoryManager.retrieve( p );
+
+        assertNotNull( check );
+
+        assertEquals( size, p.getCapacity() );
+        logger.info( "end" );
+    }
+
+
+}

Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Starter.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Starter.java?rev=1228131&r1=1228130&r2=1228131&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Starter.java (original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/Starter.java Fri Jan  6 11:51:50 2012
@@ -22,6 +22,7 @@ package org.apache.directmemory.memory.t
 import org.apache.directmemory.measures.Ram;
 import org.apache.directmemory.memory.MemoryManager;
 import org.apache.directmemory.memory.OffHeapMemoryBuffer;
+import org.apache.directmemory.memory.OffHeapMemoryBufferImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -60,7 +61,7 @@ public class Starter
 
     public void dump( OffHeapMemoryBuffer mem )
     {
-        logger.info( "off-heap - buffer: " + mem.pointers.get( 1 ).bufferNumber );
+        logger.info( "off-heap - buffer: " + mem.getBufferNumber() );
         logger.info( "off-heap - allocated: " + Ram.inMb( mem.capacity() ) );
         logger.info( "off-heap - used:      " + Ram.inMb( mem.used() ) );
         logger.info( "heap 	  - max: " + Ram.inMb( Runtime.getRuntime().maxMemory() ) );
@@ -71,7 +72,7 @@ public class Starter
 
     public void rawInsert( int megabytes, int howMany )
     {
-        OffHeapMemoryBuffer mem = OffHeapMemoryBuffer.createNew( megabytes * 1024 * 1024 );
+        OffHeapMemoryBuffer mem = OffHeapMemoryBufferImpl.createNew( megabytes * 1024 * 1024 );
         assertNotNull( mem );
         int size = mem.capacity() / ( howMany );
         size -= size / 100 * 1;



Mime
View raw message