You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ju...@apache.org on 2013/09/25 21:57:48 UTC

svn commit: r1526277 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment: Segment.java SegmentStore.java SegmentWriter.java file/FileStore.java memory/MemoryStore.java mongo/MongoStore.java

Author: jukka
Date: Wed Sep 25 19:57:48 2013
New Revision: 1526277

URL: http://svn.apache.org/r1526277
Log:
OAK-1032: SegmentMK: One SegmentWriter per SegmentNodeStore

Simplify segment creation as an initial step. WIP... (has some negative performance impact)

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Segment.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentWriter.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Segment.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Segment.java?rev=1526277&r1=1526276&r2=1526277&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Segment.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Segment.java Wed Sep 25 19:57:48 2013
@@ -22,8 +22,7 @@ import static org.apache.jackrabbit.oak.
 
 import java.nio.ByteBuffer;
 import java.util.Arrays;
-import java.util.Collection;
-import java.util.Map;
+import java.util.List;
 import java.util.UUID;
 
 import org.apache.jackrabbit.oak.api.PropertyState;
@@ -89,34 +88,31 @@ public class Segment {
                 }
             };
 
-    private static final UUID[] NO_UUIDS = new UUID[0];
-
     private final SegmentStore store;
 
     private final UUID uuid;
 
     private final ByteBuffer data;
 
-    private final UUID[] uuids;
+    private final List<UUID> uuids;
 
     private final OffsetCache<String> strings;
 
     private final OffsetCache<Template> templates;
 
-    public Segment(SegmentStore store,
-            UUID uuid, ByteBuffer data, Collection<UUID> uuids,
-            Map<String, RecordId> strings, Map<Template, RecordId> templates) {
+    public Segment(
+            SegmentStore store, UUID uuid, ByteBuffer data, List<UUID> uuids) {
         this.store = checkNotNull(store);
         this.uuid = checkNotNull(uuid);
         this.data = checkNotNull(data);
-        this.uuids = checkNotNull(uuids).toArray(NO_UUIDS);
-        this.strings = new OffsetCache<String>(strings) {
+        this.uuids = checkNotNull(uuids);
+        this.strings = new OffsetCache<String>() {
             @Override
             protected String load(int offset) {
                 return loadString(offset);
             }
         };
-        this.templates = new OffsetCache<Template>(templates) {
+        this.templates = new OffsetCache<Template>() {
             @Override
             protected Template load(int offset) {
                 return loadTemplate(offset);
@@ -147,10 +143,6 @@ public class Segment {
         return data;
     }
 
-    public UUID[] getUUIDs() {
-        return uuids;
-    }
-
     public int size() {
         return data.limit();
     }
@@ -183,7 +175,7 @@ public class Segment {
 
     private RecordId internalReadRecordId(int pos) {
         return new RecordId(
-                uuids[data.get(pos) & 0xff],
+                uuids.get(data.get(pos) & 0xff),
                 (data.get(pos + 1) & 0xff) << (8 + Segment.RECORD_ALIGN_BITS)
                 | (data.get(pos + 2) & 0xff) << Segment.RECORD_ALIGN_BITS);
     }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentStore.java?rev=1526277&r1=1526276&r2=1526277&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentStore.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentStore.java Wed Sep 25 19:57:48 2013
@@ -16,8 +16,7 @@
  */
 package org.apache.jackrabbit.oak.plugins.segment;
 
-import java.util.Collection;
-import java.util.Map;
+import java.util.List;
 import java.util.UUID;
 
 public interface SegmentStore {
@@ -26,10 +25,18 @@ public interface SegmentStore {
 
     Segment readSegment(UUID segmentId);
 
-    void createSegment(
+    /**
+     * Writes the given segment to the segment store.
+     *
+     * @param segmentId segment identifier
+     * @param bytes byte buffer that contains the raw contents of the segment
+     * @param offset start offset within the byte buffer
+     * @param length length of the segment
+     * @param referencedSegmentIds identifiers of all the referenced segments
+     */
+    void writeSegment(
             UUID segmentId, byte[] bytes, int offset, int length,
-            Collection<UUID> referencedSegmentIds,
-            Map<String, RecordId> strings, Map<Template, RecordId> templates);
+            List<UUID> referencedSegmentIds);
 
     void deleteSegment(UUID segmentId);
 

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=1526277&r1=1526276&r2=1526277&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 Sep 25 19:57:48 2013
@@ -22,6 +22,7 @@ import static com.google.common.base.Pre
 import static com.google.common.base.Preconditions.checkPositionIndex;
 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 java.util.Collections.emptyMap;
 import static org.apache.jackrabbit.oak.plugins.segment.MapRecord.BUCKETS_PER_LEVEL;
 import static org.apache.jackrabbit.oak.plugins.segment.Segment.MAX_SEGMENT_SIZE;
@@ -101,9 +102,9 @@ public class SegmentWriter {
 
     public synchronized void flush() {
         if (length > 0) {
-            store.createSegment(
+            store.writeSegment(
                     uuid, buffer, buffer.length - length, length,
-                    uuids.keySet(), strings, templates);
+                    newArrayList(uuids.keySet()));
 
             uuid = UUID.randomUUID();
             length = 0;
@@ -498,11 +499,9 @@ public class SegmentWriter {
                 UUID segmentId = UUID.randomUUID();
                 int align = Segment.RECORD_ALIGN_BYTES - 1;
                 int bulkAlignLength = (bulkLength + align) & ~align;
-                store.createSegment(
+                store.writeSegment(
                         segmentId, bulk, 0, bulkAlignLength,
-                        Collections.<UUID>emptyList(),
-                        Collections.<String, RecordId>emptyMap(),
-                        Collections.<Template, RecordId>emptyMap());
+                        Collections.<UUID>emptyList());
                 for (int pos = Segment.MAX_SEGMENT_SIZE - bulkAlignLength;
                         pos < Segment.MAX_SEGMENT_SIZE;
                         pos += BLOCK_SIZE) {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java?rev=1526277&r1=1526276&r2=1526277&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/file/FileStore.java Wed Sep 25 19:57:48 2013
@@ -27,9 +27,9 @@ import static org.apache.jackrabbit.oak.
 import java.io.File;
 import java.io.IOException;
 import java.nio.ByteBuffer;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.Callable;
 import java.util.UUID;
@@ -157,8 +157,7 @@ public class FileStore implements Segmen
                 checkState(id.equals(new UUID(
                         buffer.getLong(), buffer.getLong())));
 
-                Collection<UUID> referencedIds =
-                        newArrayListWithCapacity(count);
+                List<UUID> referencedIds = newArrayListWithCapacity(count);
                 for (int i = 0; i < count; i++) {
                     referencedIds.add(new UUID(
                             buffer.getLong(), buffer.getLong()));
@@ -167,9 +166,7 @@ public class FileStore implements Segmen
                 buffer.limit(buffer.position() + length);
                 return new Segment(
                         FileStore.this, id,
-                        buffer.slice(), referencedIds,
-                        Collections.<String, RecordId>emptyMap(),
-                        Collections.<Template, RecordId>emptyMap());
+                        buffer.slice(), referencedIds);
             }
         }
 
@@ -177,10 +174,9 @@ public class FileStore implements Segmen
     }
 
     @Override
-    public synchronized void createSegment(
+    public synchronized void writeSegment(
             UUID segmentId, byte[] data, int offset, int length,
-            Collection<UUID> referencedSegmentIds,
-            Map<String, RecordId> strings, Map<Template, RecordId> templates) {
+            List<UUID> referencedSegmentIds) {
         int size = 8 + 4 + 4 + 16 + 16 * referencedSegmentIds.size() + length;
         ByteBuffer buffer = ByteBuffer.allocate(size);
 
@@ -204,10 +200,6 @@ public class FileStore implements Segmen
         }
 
         buffer.position(pos);
-
-        cache.addSegment(new Segment(
-                this, segmentId, buffer.slice(),
-                referencedSegmentIds, strings, templates));
     }
 
     private void writeEntry(UUID segmentId, byte[] buffer)

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java?rev=1526277&r1=1526276&r2=1526277&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/memory/MemoryStore.java Wed Sep 25 19:57:48 2013
@@ -19,19 +19,18 @@ package org.apache.jackrabbit.oak.plugin
 import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;
 
 import java.nio.ByteBuffer;
-import java.util.Collection;
+import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.ConcurrentMap;
 
 import org.apache.jackrabbit.oak.plugins.segment.Journal;
-import org.apache.jackrabbit.oak.plugins.segment.RecordId;
 import org.apache.jackrabbit.oak.plugins.segment.Segment;
 import org.apache.jackrabbit.oak.plugins.segment.SegmentStore;
-import org.apache.jackrabbit.oak.plugins.segment.Template;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 
+import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 
 public class MemoryStore implements SegmentStore {
@@ -76,15 +75,14 @@ public class MemoryStore implements Segm
     }
 
     @Override
-    public void createSegment(
+    public void writeSegment(
             UUID segmentId, byte[] data, int offset, int length,
-            Collection<UUID> referencedSegmentIds,
-            Map<String, RecordId> strings, Map<Template, RecordId> templates) {
+            List<UUID> referencedSegmentIds) {
         byte[] buffer = new byte[length];
         System.arraycopy(data, offset, buffer, 0, length);
         Segment segment = new Segment(
                 this, segmentId, ByteBuffer.wrap(buffer),
-                referencedSegmentIds, strings, templates);
+                Lists.newArrayList(referencedSegmentIds));
         if (segments.putIfAbsent(segment.getSegmentId(), segment) != null) {
             throw new IllegalStateException(
                     "Segment override: " + segment.getSegmentId());

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java?rev=1526277&r1=1526276&r2=1526277&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/mongo/MongoStore.java Wed Sep 25 19:57:48 2013
@@ -24,18 +24,15 @@ import static org.apache.jackrabbit.oak.
 
 import java.nio.ByteBuffer;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.Callable;
 
 import org.apache.jackrabbit.oak.plugins.segment.Journal;
-import org.apache.jackrabbit.oak.plugins.segment.RecordId;
 import org.apache.jackrabbit.oak.plugins.segment.Segment;
 import org.apache.jackrabbit.oak.plugins.segment.SegmentCache;
 import org.apache.jackrabbit.oak.plugins.segment.SegmentStore;
-import org.apache.jackrabbit.oak.plugins.segment.Template;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 
 import com.google.common.collect.Lists;
@@ -109,18 +106,11 @@ public class MongoStore implements Segme
     }
 
     @Override
-    public void createSegment(
+    public void writeSegment(
             UUID segmentId, byte[] data, int offset, int length,
-            Collection<UUID> referencedSegmentIds,
-            Map<String, RecordId> strings, Map<Template, RecordId> templates) {
+            List<UUID> referencedSegmentIds) {
         byte[] d = new byte[length];
         System.arraycopy(data, offset, d, 0, length);
-
-        cache.addSegment(new Segment(
-                this, segmentId, ByteBuffer.wrap(d), referencedSegmentIds,
-                Collections.<String, RecordId>emptyMap(),
-                Collections.<Template, RecordId>emptyMap()));
-
         insertSegment(segmentId, d, referencedSegmentIds);
     }
 
@@ -143,10 +133,7 @@ public class MongoStore implements Segme
         for (Object object : list) {
             uuids.add(UUID.fromString(object.toString()));
         }
-        return new Segment(
-                this, segmentId, ByteBuffer.wrap(data), uuids,
-                Collections.<String, RecordId>emptyMap(),
-                Collections.<Template, RecordId>emptyMap());
+        return new Segment(this, segmentId, ByteBuffer.wrap(data), uuids);
     }
 
     private void insertSegment(