You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directmemory.apache.org by ol...@apache.org on 2012/02/26 15:55:00 UTC
svn commit: r1293849 - in
/incubator/directmemory/trunk/directmemory-cache/src:
main/java/org/apache/directmemory/cache/CacheServiceImpl.java
test/java/org/apache/directmemory/cache/CacheServiceImplTest.java
Author: olamy
Date: Sun Feb 26 14:55:00 2012
New Revision: 1293849
URL: http://svn.apache.org/viewvc?rev=1293849&view=rev
Log:
[DIRECTMEMORY-73] NPE on put method in CacheServiceImpl when cache is full.
Submitted by Michael André Pearce.
Patch modified with code formatting rule we use.
Added:
incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java (with props)
Modified:
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/CacheServiceImpl.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=1293849&r1=1293848&r2=1293849&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 Sun Feb 26 14:55:00 2012
@@ -19,15 +19,7 @@ package org.apache.directmemory.cache;
* under the License.
*/
-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 com.google.common.collect.MapMaker;
import org.apache.directmemory.measures.Every;
import org.apache.directmemory.measures.Ram;
import org.apache.directmemory.memory.MemoryManagerService;
@@ -38,7 +30,14 @@ import org.apache.directmemory.serializa
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.collect.MapMaker;
+import java.io.EOFException;
+import java.io.IOException;
+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;
public class CacheServiceImpl<K, V>
implements CacheService<K, V>
@@ -142,11 +141,13 @@ public class CacheServiceImpl<K, V>
{
byte[] payload = serializer.serialize( value );
Pointer<V> ptr = store( key, payload, expiresIn );
+ if ( ptr != null )
+ {
+ @SuppressWarnings( "unchecked" ) // type driven by the compiler
+ Class<? extends V> clazz = (Class<? extends V>) value.getClass();
- @SuppressWarnings( "unchecked" ) // type driven by the compiler
- Class<? extends V> clazz = (Class<? extends V>) value.getClass();
-
- ptr.setClazz( clazz );
+ ptr.setClazz( clazz );
+ }
return ptr;
}
catch ( IOException e )
@@ -174,7 +175,10 @@ public class CacheServiceImpl<K, V>
else
{
pointer = memoryManager.store( payload, expiresIn );
- map.put( key, pointer );
+ if ( pointer != null )
+ {
+ map.put( key, pointer );
+ }
return pointer;
}
}
Added: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java
URL: http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java?rev=1293849&view=auto
==============================================================================
--- incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java (added)
+++ incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java Sun Feb 26 14:55:00 2012
@@ -0,0 +1,56 @@
+package org.apache.directmemory.cache;
+
+/*
+ * 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.AllocationPolicy;
+import org.apache.directmemory.memory.MemoryManagerService;
+import org.apache.directmemory.memory.MemoryManagerServiceWithAllocationPolicyImpl;
+import org.apache.directmemory.memory.Pointer;
+import org.apache.directmemory.memory.RoundRobinAllocationPolicy;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CacheServiceImplTest
+{
+
+ @Test
+ public void testOffHeapExceedMemoryReturnNullWhenTrue()
+ {
+ AllocationPolicy<byte[]> allocationPolicy = new RoundRobinAllocationPolicy<byte[]>();
+ MemoryManagerService<byte[]> memoryManager =
+ new MemoryManagerServiceWithAllocationPolicyImpl<byte[]>( allocationPolicy, true );
+ CacheService<Integer, byte[]> cache = new CacheServiceImpl<Integer, byte[]>( memoryManager );
+ cache.init( 1, (int) ( Ram.Mb( 1 ) ) );
+
+ for ( int i = 0; i < 1000; i++ )
+ {
+ Pointer<byte[]> pointer = cache.put( i, new byte[1024] );
+ if ( ( i % 100 ) == 0 )
+ {
+ System.out.println( pointer );
+ }
+ }
+ Assert.assertTrue( "This test ensures that no unexpected errors/behaviours occurs when heap space is full",
+ true );
+
+ }
+
+}
Propchange: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision