jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1526761 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/plugins/memory/ main/java/org/apache/jackrabbit/oak/plugins/segment/ test/java/org/apache/jackrabbit/oak/plugins/segment/
Date Fri, 27 Sep 2013 00:44:43 GMT
Author: jukka
Date: Fri Sep 27 00:44:42 2013
New Revision: 1526761

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

Make SegmentBlob extend Record and thus use the built-in segment tracking mechanism

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Record.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentBlob.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStore.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentPropertyState.java
    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/memory/AbstractBlob.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java?rev=1526761&r1=1526760&r2=1526761&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
Fri Sep 27 00:44:42 2013
@@ -50,7 +50,7 @@ public abstract class AbstractBlob imple
 
     }
 
-    private static boolean equal(Blob a, Blob b) {
+    public static boolean equal(Blob a, Blob b) {
         try {
             return ByteStreams.equal(new BlobSupplier(a), new BlobSupplier(b));
         } catch (IOException e) {

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Record.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Record.java?rev=1526761&r1=1526760&r2=1526761&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Record.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/Record.java
Fri Sep 27 00:44:42 2013
@@ -135,4 +135,11 @@ class Record {
         return getOffset(bytes + ids * Segment.RECORD_ID_BYTES);
     }
 
+    //------------------------------------------------------------< Object >--
+
+    @Override
+    public String toString() {
+        return getRecordId().toString();
+    }
+
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentBlob.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentBlob.java?rev=1526761&r1=1526760&r2=1526761&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentBlob.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentBlob.java
Fri Sep 27 00:44:42 2013
@@ -16,26 +16,20 @@
  */
 package org.apache.jackrabbit.oak.plugins.segment;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-
 import javax.annotation.Nonnull;
 
+import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.plugins.memory.AbstractBlob;
 
-public class SegmentBlob extends AbstractBlob {
-
-    private final SegmentReader reader;
+class SegmentBlob extends Record implements Blob {
 
-    private final RecordId recordId;
-
-    SegmentBlob(SegmentReader reader, RecordId recordId) {
-        this.reader = checkNotNull(reader);
-        this.recordId = checkNotNull(recordId);
+    SegmentBlob(Segment segment, RecordId id) {
+        super(segment, id);
     }
 
     @Override @Nonnull
     public SegmentStream getNewStream() {
-        return reader.readStream(recordId);
+        return getSegment().readStream(getOffset());
     }
 
     @Override
@@ -48,15 +42,23 @@ public class SegmentBlob extends Abstrac
         }
     }
 
+    //------------------------------------------------------------< Object >--
+
     @Override
     public boolean equals(Object object) {
         if (object instanceof SegmentBlob) {
             SegmentBlob that = (SegmentBlob) object;
-            if (recordId.equals(that.recordId)) {
+            if (getRecordId().equals(that.getRecordId())) {
                 return true;
             }
         }
-        return super.equals(object);
+        return object instanceof Blob
+                && AbstractBlob.equal(this, (Blob) object);
+    }
+
+    @Override
+    public int hashCode() {
+        return 0;
     }
 
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStore.java?rev=1526761&r1=1526760&r2=1526761&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentNodeStore.java
Fri Sep 27 00:44:42 2013
@@ -130,7 +130,7 @@ public class SegmentNodeStore implements
 
     @Override
     public Blob createBlob(InputStream stream) throws IOException {
-        return new SegmentBlob(reader, store.getWriter().writeStream(stream));
+        return store.getWriter().writeStream(stream);
     }
 
     @Override @Nonnull

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentPropertyState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentPropertyState.java?rev=1526761&r1=1526760&r2=1526761&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentPropertyState.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentPropertyState.java
Fri Sep 27 00:44:42 2013
@@ -161,12 +161,11 @@ class SegmentPropertyState extends Abstr
             base = base.getBaseType();
         }
 
-        SegmentReader reader = new SegmentReader(store);
+        Segment segment = store.readSegment(recordId.getSegmentId());
         RecordId valueId = values.getEntry(index);
         if (type == Type.BINARY) {
-            return (T) new SegmentBlob(reader, valueId);
+            return (T) new SegmentBlob(segment, valueId);
         } else {
-            Segment segment = store.readSegment(recordId.getSegmentId());
             String value = segment.readString(valueId);
             if (type == Type.STRING || type == Type.URI
                     || type == Type.NAME || type == Type.PATH

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=1526761&r1=1526760&r2=1526761&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
Fri Sep 27 00:44:42 2013
@@ -59,6 +59,7 @@ import com.google.common.collect.Iterabl
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.io.ByteStreams;
+import com.google.common.io.Closeables;
 
 public class SegmentWriter {
 
@@ -109,8 +110,13 @@ public class SegmentWriter {
 
     private Segment currentSegment = null;
 
+    private final Segment dummySegment;
+
     public SegmentWriter(SegmentStore store) {
         this.store = store;
+        this.dummySegment = new Segment(
+                store, UUID.randomUUID(),
+                ByteBuffer.allocate(0), Collections.<UUID>emptyList());
     }
 
     public synchronized Segment getCurrentSegment(UUID id) {
@@ -128,6 +134,10 @@ public class SegmentWriter {
         }
     }
 
+    public Segment getDummySegment() {
+        return dummySegment;
+    }
+
     public synchronized void flush() {
         if (length > 0) {
             store.writeSegment(
@@ -459,7 +469,7 @@ public class SegmentWriter {
             if (id == null) {
                 byte[] data = string.getBytes(Charsets.UTF_8);
                 try {
-                    id = writeStream(new ByteArrayInputStream(data));
+                    id = writeStream(new ByteArrayInputStream(data)).getRecordId();
                 } catch (IOException e) {
                     throw new IllegalStateException("Unexpected IOException", e);
                 }
@@ -469,6 +479,14 @@ public class SegmentWriter {
         }
     }
 
+    public SegmentBlob writeBlob(Blob blob) throws IOException {
+        if (blob instanceof SegmentBlob) {
+            return (SegmentBlob) blob;
+        } else {
+            return writeStream(blob.getNewStream());
+        }
+    }
+
     /**
      * Writes a stream value record. The given stream is consumed
      * <em>and closed</em> by this method.
@@ -477,16 +495,18 @@ public class SegmentWriter {
      * @return value record identifier
      * @throws IOException if the stream could not be read
      */
-    public RecordId writeStream(InputStream stream) throws IOException {
+    public SegmentBlob writeStream(InputStream stream) throws IOException {
         RecordId id = SegmentStream.getRecordIdIfAvailable(stream);
         if (id == null) {
+            boolean threw = true;
             try {
                 id = internalWriteStream(stream);
+                threw = false;
             } finally {
-                stream.close();
+                Closeables.close(stream, threw);
             }
         }
-        return id;
+        return new SegmentBlob(dummySegment, id);
     }
 
     private RecordId internalWriteStream(InputStream stream)
@@ -556,8 +576,9 @@ public class SegmentWriter {
         for (int i = 0; i < count; i++) {
             if (type.tag() == PropertyType.BINARY) {
                 try {
-                    Blob blob = state.getValue(Type.BINARY, i);
-                    valueIds.add(writeStream(blob.getNewStream()));
+                    SegmentBlob blob =
+                            writeBlob(state.getValue(Type.BINARY, i));
+                    valueIds.add(blob.getRecordId());
                 } catch (IOException e) {
                     throw new IllegalStateException("Unexpected IOException", e);
                 }

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=1526761&r1=1526760&r2=1526761&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
Fri Sep 27 00:44:42 2013
@@ -26,15 +26,14 @@ import static org.junit.Assert.assertTru
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.nio.ByteBuffer;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
-import java.util.UUID;
 
+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;
@@ -52,12 +51,6 @@ public class RecordTest {
 
     private SegmentStore store = new MemoryStore();
 
-    private Segment segment = new Segment(
-            store, UUID.randomUUID(),
-            ByteBuffer.allocate(0), Collections.<UUID>emptyList());
-
-    private SegmentReader reader = new SegmentReader(store);
-
     private SegmentWriter writer = store.getWriter();
 
     private final Random random = new Random(0xcafefaceL);
@@ -65,7 +58,8 @@ public class RecordTest {
     @Test
     public void testBlockRecord() {
         RecordId blockId = writer.writeBlock(bytes, 0, bytes.length);
-        BlockRecord block = new BlockRecord(segment, blockId, bytes.length);
+        BlockRecord block = new BlockRecord(
+                writer.getDummySegment(), blockId, bytes.length);
 
         // Check reading with all valid positions and lengths
         for (int n = 1; n < bytes.length; n++) {
@@ -110,7 +104,8 @@ public class RecordTest {
 
     private ListRecord writeList(int size, RecordId id) {
         List<RecordId> list = Collections.nCopies(size, id);
-        return new ListRecord(segment, writer.writeList(list), size);
+        return new ListRecord(
+                writer.getDummySegment(), writer.writeList(list), size);
     }
 
     @Test
@@ -133,8 +128,8 @@ public class RecordTest {
         byte[] source = new byte[size];
         random.nextBytes(source);
 
-        RecordId valueId = writer.writeStream(new ByteArrayInputStream(source));
-        InputStream stream = reader.readStream(valueId);
+        Blob value = writer.writeStream(new ByteArrayInputStream(source));
+        InputStream stream = value.getNewStream();
         try {
             byte[] b = new byte[349]; // prime number
             int offset = 0;



Mime
View raw message