Author: mduerig
Date: Wed May 27 10:14:30 2015
New Revision: 1681955

URL: http://svn.apache.org/r1681955
Log:
OAK-2880: NPE in SegmentWriter.writeMap
Test case and fix: ignore null keys that are not present in the base map

Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentWriter.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/segment/RecordTest.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentWriter.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentWriter.java?rev=1681955&r1=1681954&r2=1681955&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentWriter.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentWriter.java
 Wed May 27 10:14:30 2015
@@ -701,11 +701,13 @@ public class SegmentWriter {
                     keyId = e.getKey();
                 }
             }
-            if (keyId == null) {
+            if (keyId == null && entry.getValue() != null) {
                 keyId = writeString(key);
             }
 
-            entries.add(new MapEntry(key, keyId, entry.getValue()));
+            if (keyId != null) {
+                entries.add(new MapEntry(key, keyId, entry.getValue()));
+            }
         }
 
         return writeMapBucket(base, entries, 0);

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/segment/RecordTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/segment/RecordTest.java?rev=1681955&r1=1681954&r2=1681955&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/segment/RecordTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/segment/RecordTest.java
 Wed May 27 10:14:30 2015
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.oak.plugins.segment;
 
 import static com.google.common.collect.Lists.newArrayList;
+import static com.google.common.collect.Maps.newHashMap;
 import static java.util.Collections.singletonList;
 import static junit.framework.Assert.assertNotNull;
 import static junit.framework.Assert.fail;
@@ -40,16 +41,14 @@ import java.util.List;
 import java.util.Map;
 import java.util.Random;
 
+import com.google.common.base.Charsets;
+import com.google.common.collect.ImmutableMap;
 import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.plugins.segment.memory.MemoryStore;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.junit.Test;
 
-import com.google.common.base.Charsets;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
-
 public class RecordTest {
 
     private String hello = "Hello, World!";
@@ -199,7 +198,7 @@ public class RecordTest {
                 null, ImmutableMap.of("one", blockId));
         MapRecord two = writer.writeMap(
                 null, ImmutableMap.of("one", blockId, "two", blockId));
-        Map<String, RecordId> map = Maps.newHashMap();
+        Map<String, RecordId> map = newHashMap();
         for (int i = 0; i < 1000; i++) {
             map.put("key" + i, blockId);
         }
@@ -241,7 +240,7 @@ public class RecordTest {
         assertFalse(iterator.hasNext());
         assertNull(many.getEntry("foo"));
 
-        Map<String, RecordId> changes = Maps.newHashMap();
+        Map<String, RecordId> changes = newHashMap();
         changes.put("key0", null);
         changes.put("key1000", blockId);
         MapRecord modified = writer.writeMap(many, changes);
@@ -257,9 +256,19 @@ public class RecordTest {
     }
 
     @Test
+    public void testMapRemoveNonExisting() {
+        RecordId blockId = writer.writeBlock(bytes, 0, bytes.length);
+
+        Map<String, RecordId> changes = newHashMap();
+        changes.put("one", null);
+        MapRecord zero = writer.writeMap(null, changes);
+        assertEquals(0, zero.size());
+    }
+
+    @Test
     public void testWorstCaseMap() {
         RecordId blockId = writer.writeBlock(bytes, 0, bytes.length);
-        Map<String, RecordId> map = Maps.newHashMap();
+        Map<String, RecordId> map = newHashMap();
         char[] key = new char[2];
         for (int i = 0; i <= MapRecord.BUCKETS_PER_LEVEL; i++) {
             key[0] = (char) ('A' + i);


Reply via email to