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 md...@apache.org on 2017/03/22 11:21:58 UTC

svn commit: r1788095 - in /jackrabbit/oak/trunk/oak-segment-tar/src: main/java/org/apache/jackrabbit/oak/segment/ test/java/org/apache/jackrabbit/oak/segment/

Author: mduerig
Date: Wed Mar 22 11:21:58 2017
New Revision: 1788095

URL: http://svn.apache.org/viewvc?rev=1788095&view=rev
Log:
OAK-5967: Null instances for SegmentId and RecordId

Added:
    jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/EmptySegmentStoreTest.java
Modified:
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordId.java
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentId.java
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentStore.java

Modified: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordId.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordId.java?rev=1788095&r1=1788094&r2=1788095&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordId.java (original)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/RecordId.java Wed Mar 22 11:21:58 2017
@@ -34,6 +34,11 @@ import javax.annotation.Nonnull;
  */
 public final class RecordId implements Comparable<RecordId> {
 
+    /**
+     * A {@code null} record id not identifying any record.
+     */
+    public static final RecordId NULL = new RecordId(SegmentId.NULL, 0);
+
     private static final Pattern PATTERN = Pattern.compile(
             "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})"
             + "(:(0|[1-9][0-9]*)|\\.([0-9a-f]{8}))");

Modified: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentId.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentId.java?rev=1788095&r1=1788094&r2=1788095&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentId.java (original)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentId.java Wed Mar 22 11:21:58 2017
@@ -19,6 +19,7 @@
 package org.apache.jackrabbit.oak.segment;
 
 import static org.apache.jackrabbit.oak.segment.CacheWeights.OBJECT_HEADER_SIZE;
+import static org.apache.jackrabbit.oak.segment.SegmentStore.EMPTY_STORE;
 
 import java.util.UUID;
 
@@ -36,6 +37,11 @@ import org.slf4j.LoggerFactory;
  */
 public class SegmentId implements Comparable<SegmentId> {
 
+    /**
+     * A {@code null} segment id not representing any segment.
+     */
+    public static final SegmentId NULL = new SegmentId(EMPTY_STORE, 0, 0);
+
     /** Logger instance */
     private static final Logger log = LoggerFactory.getLogger(SegmentId.class);
 

Modified: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentStore.java?rev=1788095&r1=1788094&r2=1788095&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentStore.java (original)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentStore.java Wed Mar 22 11:21:58 2017
@@ -28,6 +28,38 @@ import javax.annotation.Nonnull;
 public interface SegmentStore {
 
     /**
+     * A store that is always empty and that cannot be written to.
+     */
+    SegmentStore EMPTY_STORE = new SegmentStore() {
+
+        /**
+         * @return {@code false}
+         */
+        @Override
+        public boolean containsSegment(SegmentId id) {
+            return false;
+        }
+
+        /**
+         * @throws SegmentNotFoundException always
+         */
+        @Nonnull
+        @Override
+        public Segment readSegment(SegmentId segmentId) {
+            throw new SegmentNotFoundException(segmentId);
+        }
+
+        /**
+         * @throws IOException always
+         */
+        @Override
+        public void writeSegment(SegmentId id, byte[] bytes, int offset, int length)
+        throws IOException {
+            throw new IOException("This store is read only");
+        }
+    };
+
+    /**
      * Checks whether the identified segment exists in this store.
      *
      * @param id segment identifier

Added: jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/EmptySegmentStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/EmptySegmentStoreTest.java?rev=1788095&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/EmptySegmentStoreTest.java (added)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/EmptySegmentStoreTest.java Wed Mar 22 11:21:58 2017
@@ -0,0 +1,43 @@
+/*
+ * 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.
+ *
+ */
+
+package org.apache.jackrabbit.oak.segment;
+
+import static org.apache.jackrabbit.oak.segment.SegmentStore.EMPTY_STORE;
+import static org.junit.Assert.assertFalse;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+public class EmptySegmentStoreTest {
+    @Test
+    public void containsSegment() {
+        assertFalse(EMPTY_STORE.containsSegment(SegmentId.NULL));
+    }
+
+    @Test(expected = SegmentNotFoundException.class)
+    public void readSegment() {
+        EMPTY_STORE.readSegment(SegmentId.NULL);
+    }
+
+    @Test(expected = IOException.class)
+    public void writeSegment() throws IOException {
+        EMPTY_STORE.writeSegment(SegmentId.NULL, new byte[0], 0, 0);
+    }
+}