jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1528504 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment: MapLeaf.java SegmentWriter.java
Date Wed, 02 Oct 2013 14:49:16 GMT
Author: jukka
Date: Wed Oct  2 14:49:15 2013
New Revision: 1528504

URL: http://svn.apache.org/r1528504
Log:
OAK-1031: SegmentMK: Fewer segment lookups

Simplify the getKeys() and getEntries() methods in MapLeaf to increase locality of access

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

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MapLeaf.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MapLeaf.java?rev=1528504&r1=1528503&r2=1528504&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MapLeaf.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/MapLeaf.java
Wed Oct  2 14:49:15 2013
@@ -19,11 +19,7 @@ package org.apache.jackrabbit.oak.plugin
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
-import java.util.Iterator;
-import java.util.Map;
-import java.util.NoSuchElementException;
-
-import com.google.common.collect.Maps;
+import java.util.Arrays;
 
 class MapLeaf extends MapRecord {
 
@@ -39,28 +35,6 @@ class MapLeaf extends MapRecord {
         checkArgument(size <= BUCKETS_PER_LEVEL || level == MAX_NUMBER_OF_LEVELS);
     }
 
-    Map<String, MapEntry> getMapEntries() {
-        RecordId[] keys = new RecordId[size];
-        RecordId[] values = new RecordId[size];
-
-        Segment segment = getSegment();
-        int bytes = 4 + size * 4;
-        int ids = 0;
-        for (int i = 0; i < size; i++) {
-            keys[i] = segment.readRecordId(getOffset(bytes, ids++));
-        }
-        for (int i = 0; i < size; i++) {
-            values[i] = segment.readRecordId(getOffset(bytes, ids++));
-        }
-
-        Map<String, MapEntry> entries = Maps.newHashMapWithExpectedSize(size);
-        for (int i = 0; i < size; i++) {
-            String name = segment.readString(keys[i]);
-            entries.put(name, new MapEntry(segment, name, keys[i], values[i]));
-        }
-        return entries;
-    }
-
     @Override
     RecordId getEntry(String key) {
         checkNotNull(key);
@@ -86,17 +60,40 @@ class MapLeaf extends MapRecord {
 
     @Override
     Iterable<String> getKeys() {
-        return new Iterable<String>() {
-            @Override
-            public Iterator<String> iterator() {
-                return getKeyIterator();
-            }
-        };
+        Segment segment = getSegment();
+
+        RecordId[] ids = new RecordId[size];
+        for (int i = 0; i < size; i++) {
+            ids[i] = segment.readRecordId(getOffset(4 + size * 4, i));
+        }
+
+        String[] keys = new String[size];
+        for (int i = 0; i < size; i++) {
+            keys[i] = segment.readString(ids[i]);
+        }
+        return Arrays.asList(keys);
     }
 
     @Override
     Iterable<MapEntry> getEntries() {
-        return getMapEntries().values();
+        Segment segment = getSegment();
+
+        RecordId[] keys = new RecordId[size];
+        for (int i = 0; i < size; i++) {
+            keys[i] = segment.readRecordId(getOffset(4 + size * 4, i));
+        }
+
+        RecordId[] values = new RecordId[size];
+        for (int i = 0; i < size; i++) {
+            values[i] = segment.readRecordId(getOffset(4 + size * 4, size + i));
+        }
+
+        MapEntry[] entries = new MapEntry[size];
+        for (int i = 0; i < size; i++) {
+            String name = segment.readString(keys[i]);
+            entries[i] = new MapEntry(segment, name, keys[i], values[i]);
+        }
+        return Arrays.asList(entries);
     }
 
     @Override
@@ -176,30 +173,6 @@ class MapLeaf extends MapRecord {
 
     //-----------------------------------------------------------< private >--
 
-    private Iterator<String> getKeyIterator() {
-        return new Iterator<String>() {
-            private final Segment segment = getSegment();
-            private int index = 0;
-            @Override
-            public boolean hasNext() {
-                return index < size;
-            }
-            @Override
-            public String next() {
-                int i = index++;
-                if (i < size) {
-                    return getKey(segment, i);
-                } else {
-                    throw new NoSuchElementException();
-                }
-            }
-            @Override
-            public void remove() {
-                throw new UnsupportedOperationException();
-            }
-        };
-    }
-
     private int getHash(Segment segment, int index) {
         return checkNotNull(segment).readInt(getOffset() + 4 + index * 4);
     }

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=1528504&r1=1528503&r2=1528504&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 Oct  2 14:49:15 2013
@@ -24,6 +24,7 @@ import static com.google.common.base.Pre
 import static com.google.common.base.Preconditions.checkPositionIndexes;
 import static com.google.common.base.Preconditions.checkState;
 import static com.google.common.collect.Lists.newArrayList;
+import static com.google.common.collect.Maps.newHashMap;
 import static java.util.Collections.emptyMap;
 import static org.apache.jackrabbit.oak.api.Type.NAME;
 import static org.apache.jackrabbit.oak.api.Type.NAMES;
@@ -304,7 +305,10 @@ public class SegmentWriter {
             // FIXME: messy code with lots of duplication
             MapRecord base = dummySegment.readMap(baseId);
             if (base instanceof MapLeaf) {
-                Map<String, MapEntry> map = ((MapLeaf) base).getMapEntries();
+                Map<String, MapEntry> map = newHashMap();
+                for (MapEntry entry : base.getEntries()) {
+                    map.put(entry.getName(), entry);
+                }
                 for (MapEntry entry : entries) {
                     if (entry.getValue() != null) {
                         map.put(entry.getName(), entry);



Mime
View raw message