incubator-directmemory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From raffaelegu...@apache.org
Subject svn commit: r1211907 - in /incubator/directmemory/trunk: ./ directmemory-cache/src/main/java/org/apache/directmemory/cache/ directmemory-cache/src/main/java/org/apache/directmemory/memory/ directmemory-cache/src/test/java/org/apache/directmemory/memory...
Date Thu, 08 Dec 2011 14:26:29 GMT
Author: raffaeleguidi
Date: Thu Dec  8 14:26:28 2011
New Revision: 1211907

URL: http://svn.apache.org/viewvc?rev=1211907&view=rev
Log:
Added NIO support to the Cache implementation as well

Modified:
    incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java
    incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java
    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/OffHeapMemoryBuffer.java
    incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTests.java
    incubator/directmemory/trunk/pom.xml

Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java?rev=1211907&r1=1211906&r2=1211907&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java
(original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java
Thu Dec  8 14:26:28 2011
@@ -132,5 +132,9 @@ public class Cache {
   public static MemoryManagerService getMemoryManager(){
     return cacheService.getMemoryManager();
   }
+
+public static Pointer allocate(String key, int size) {
+	return cacheService.allocate(key, size);
+}
 	
 }

Modified: incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java?rev=1211907&r1=1211906&r2=1211907&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java
(original)
+++ incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheService.java
Thu Dec  8 14:26:28 2011
@@ -83,4 +83,6 @@ public interface CacheService {
 
   public void setMemoryManager(MemoryManagerService memoryManager);
 
+  public Pointer allocate(String key, int size);
+
 }

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=1211907&r1=1211906&r2=1211907&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
Thu Dec  8 14:26:28 2011
@@ -21,6 +21,7 @@ package org.apache.directmemory.cache;
 
 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;
@@ -166,6 +167,10 @@ public class CacheServiceImpl implements
       }
       return null;
     } 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);
       } catch (EOFException e) {
@@ -275,4 +280,12 @@ public class CacheServiceImpl implements
   public void setMemoryManager(MemoryManagerService memoryManager) {
     this.memoryManager = memoryManager;
   }
+
+  @Override
+  public Pointer allocate(String key, int size) {
+    Pointer ptr = memoryManager.allocate(size, -1, -1);
+    map.put(key, ptr);
+    ptr.clazz = ByteBuffer.class;
+    return ptr;
+  }
 }

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=1211907&r1=1211906&r2=1211907&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
Thu Dec  8 14:26:28 2011
@@ -105,24 +105,23 @@ public class OffHeapMemoryBuffer {
 	}
 	
 	public byte[] retrieve(Pointer pointer) {
-//		if (!pointer.expired()) {
-			pointer.lastHit = System.currentTimeMillis();
-			pointer.hits++;
-			
-			ByteBuffer buf = null;
+		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);
 			}
-			buf.position(pointer.start);
-			// not needed for reads
-			// buf.limit(pointer.end+pointer.start);
-			final byte[] swp = new byte[pointer.end-pointer.start];
-			buf.get(swp);
-			return swp;
-//		} else {
-//			free(pointer);
-//			return null;
-//		}
+		}
+		
+		final byte[] swp = new byte[pointer.end-pointer.start];
+		buf.get(swp);
+		return swp;
 	}
 	
 
@@ -310,6 +309,7 @@ public class OffHeapMemoryBuffer {
 		
 		fresh.directBuffer = buf.slice();
 		fresh.directBuffer.limit(size);
+		fresh.clazz = ByteBuffer.class;
 		pointers.add(fresh);
 		return fresh;	
 	}

Modified: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTests.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTests.java?rev=1211907&r1=1211906&r2=1211907&view=diff
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTests.java
(original)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/test/NIOTests.java
Thu Dec  8 14:26:28 2011
@@ -73,9 +73,11 @@ public class NIOTests {
 		assertEquals(0, b.position());
 		assertEquals(size, b.limit());
 		
-		//assertEquals(size,p.end);
+		byte[] check = MemoryManager.retrieve(p);
+		
+		assertNotNull(check);
+		
 		assertEquals(size,p.end-p.start); 
-//		assertEquals(size, MemoryManager.getActiveBuffer().used());
 		logger.info("end");
 	}
 

Modified: incubator/directmemory/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/pom.xml?rev=1211907&r1=1211906&r2=1211907&view=diff
==============================================================================
--- incubator/directmemory/trunk/pom.xml (original)
+++ incubator/directmemory/trunk/pom.xml Thu Dec  8 14:26:28 2011
@@ -368,6 +368,7 @@ under the License.
           </execution>
         </executions>
       </plugin>
+
       <plugin>
         <groupId>org.apache.rat</groupId>
         <artifactId>apache-rat-plugin</artifactId>



Mime
View raw message