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 fr...@apache.org on 2017/08/04 12:31:22 UTC

svn commit: r1804110 - in /jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index: IndexLoader.java IndexLoaderV1.java IndexLoaderV2.java ReaderAtEnd.java

Author: frm
Date: Fri Aug  4 12:31:22 2017
New Revision: 1804110

URL: http://svn.apache.org/viewvc?rev=1804110&view=rev
Log:
OAK-6518 - Abstract the medium the index is read from

Added:
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/ReaderAtEnd.java   (with props)
Modified:
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoader.java
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1.java
    jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2.java

Modified: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoader.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoader.java?rev=1804110&r1=1804109&r2=1804110&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoader.java (original)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoader.java Fri Aug  4 12:31:22 2017
@@ -18,12 +18,10 @@
 package org.apache.jackrabbit.oak.segment.file.tar.index;
 
 import static com.google.common.base.Preconditions.checkArgument;
-import static java.nio.ByteBuffer.wrap;
 
 import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
-import java.util.zip.CRC32;
 
 public class IndexLoader {
 
@@ -44,6 +42,21 @@ public class IndexLoader {
         this.v2 = new IndexLoaderV2(blockSize);
     }
 
+    private int readMagic(ReaderAtEnd reader) throws IOException {
+        return reader.readAtEnd(Integer.BYTES, Integer.BYTES).getInt();
+    }
+
+    private Index loadIndex(ReaderAtEnd reader) throws IOException, InvalidIndexException {
+        switch (readMagic(reader)) {
+            case IndexLoaderV1.MAGIC:
+                return v1.loadIndex(reader);
+            case IndexLoaderV2.MAGIC:
+                return v2.loadIndex(reader);
+            default:
+                throw new InvalidIndexException("Unrecognized magic number");
+        }
+    }
+
     public Index loadIndex(RandomAccessFile file) throws IOException, InvalidIndexException {
         long length = file.length();
 
@@ -51,19 +64,12 @@ public class IndexLoader {
             throw new InvalidIndexException(String.format("Unexpected size %d", length));
         }
 
-        ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
-        file.seek(length - 2 * blockSize - Integer.BYTES);
-        file.readFully(buffer.array());
-        int magic = buffer.getInt();
-
-        if (magic == IndexLoaderV1.MAGIC) {
-            return v1.loadIndex(file);
-        }
-        if (magic == IndexLoaderV2.MAGIC) {
-            return v2.loadIndex(file);
-        }
-
-        throw new InvalidIndexException("Unrecognized magic number");
+        return loadIndex((whence, size) -> {
+            ByteBuffer buffer = ByteBuffer.allocate(size);
+            file.seek(length - 2 * blockSize - whence);
+            file.readFully(buffer.array());
+            return buffer;
+        });
     }
 
 }

Modified: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1.java?rev=1804110&r1=1804109&r2=1804110&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1.java (original)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV1.java Fri Aug  4 12:31:22 2017
@@ -20,7 +20,6 @@ package org.apache.jackrabbit.oak.segmen
 import static java.nio.ByteBuffer.wrap;
 
 import java.io.IOException;
-import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
 import java.util.zip.CRC32;
 
@@ -34,12 +33,8 @@ class IndexLoaderV1 {
         this.blockSize = blockSize;
     }
 
-    Index loadIndex(RandomAccessFile file) throws InvalidIndexException, IOException {
-        long length = file.length();
-        // read the index metadata just before the two final zero blocks
-        ByteBuffer meta = ByteBuffer.allocate(IndexV1.FOOTER_SIZE);
-        file.seek(length - 2 * blockSize - IndexV1.FOOTER_SIZE);
-        file.readFully(meta.array());
+    Index loadIndex(ReaderAtEnd reader) throws InvalidIndexException, IOException {
+        ByteBuffer meta = reader.readAtEnd(IndexV1.FOOTER_SIZE, IndexV1.FOOTER_SIZE);
         int crc32 = meta.getInt();
         int count = meta.getInt();
         int bytes = meta.getInt();
@@ -53,11 +48,7 @@ class IndexLoaderV1 {
             throw new InvalidIndexException("Invalid metadata");
         }
 
-        // this involves seeking backwards in the file, which might not
-        // perform well, but that's OK since we only do this once per file
-        ByteBuffer index = ByteBuffer.allocate(count * IndexEntryV1.SIZE);
-        file.seek(length - 2 * blockSize - IndexV1.FOOTER_SIZE - count * IndexEntryV1.SIZE);
-        file.readFully(index.array());
+        ByteBuffer index = reader.readAtEnd(IndexV1.FOOTER_SIZE + count * IndexEntryV1.SIZE, count * IndexEntryV1.SIZE);
         index.mark();
 
         CRC32 checksum = new CRC32();
@@ -66,7 +57,6 @@ class IndexLoaderV1 {
             throw new InvalidIndexException("Invalid checksum");
         }
 
-        long limit = length - 2 * blockSize - bytes - blockSize;
         long lastMsb = Long.MIN_VALUE;
         long lastLsb = Long.MIN_VALUE;
         byte[] entry = new byte[IndexEntryV1.SIZE];
@@ -88,7 +78,7 @@ class IndexLoaderV1 {
             if (offset < 0 || offset % blockSize != 0) {
                 throw new InvalidIndexException("Invalid entry offset");
             }
-            if (size < 1 || offset + size > limit) {
+            if (size < 1) {
                 throw new InvalidIndexException("Invalid entry size");
             }
 

Modified: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2.java?rev=1804110&r1=1804109&r2=1804110&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2.java (original)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderV2.java Fri Aug  4 12:31:22 2017
@@ -20,7 +20,6 @@ package org.apache.jackrabbit.oak.segmen
 import static java.nio.ByteBuffer.wrap;
 
 import java.io.IOException;
-import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
 import java.util.zip.CRC32;
 
@@ -34,12 +33,8 @@ class IndexLoaderV2 {
         this.blockSize = blockSize;
     }
 
-    Index loadIndex(RandomAccessFile file) throws InvalidIndexException, IOException {
-        long length = file.length();
-        // read the index metadata just before the two final zero blocks
-        ByteBuffer meta = ByteBuffer.allocate(IndexV2.FOOTER_SIZE);
-        file.seek(length - 2 * blockSize - IndexV2.FOOTER_SIZE);
-        file.readFully(meta.array());
+    Index loadIndex(ReaderAtEnd reader) throws InvalidIndexException, IOException {
+        ByteBuffer meta = reader.readAtEnd(IndexV2.FOOTER_SIZE, IndexV2.FOOTER_SIZE);
         int crc32 = meta.getInt();
         int count = meta.getInt();
         int bytes = meta.getInt();
@@ -53,11 +48,7 @@ class IndexLoaderV2 {
             throw new InvalidIndexException("Invalid metadata");
         }
 
-        // this involves seeking backwards in the file, which might not
-        // perform well, but that's OK since we only do this once per file
-        ByteBuffer index = ByteBuffer.allocate(count * IndexEntryV2.SIZE);
-        file.seek(length - 2 * blockSize - IndexV2.FOOTER_SIZE - count * IndexEntryV2.SIZE);
-        file.readFully(index.array());
+        ByteBuffer index = reader.readAtEnd(IndexV2.FOOTER_SIZE + count * IndexEntryV2.SIZE, count * IndexEntryV2.SIZE);
         index.mark();
 
         CRC32 checksum = new CRC32();
@@ -66,7 +57,6 @@ class IndexLoaderV2 {
             throw new InvalidIndexException("Invalid checksum");
         }
 
-        long limit = length - 2 * blockSize - bytes - blockSize;
         long lastMsb = Long.MIN_VALUE;
         long lastLsb = Long.MIN_VALUE;
         byte[] entry = new byte[IndexEntryV2.SIZE];
@@ -88,7 +78,7 @@ class IndexLoaderV2 {
             if (offset < 0 || offset % blockSize != 0) {
                 throw new InvalidIndexException("Invalid entry offset");
             }
-            if (size < 1 || offset + size > limit) {
+            if (size < 1) {
                 throw new InvalidIndexException("Invalid entry size");
             }
 

Added: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/ReaderAtEnd.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/ReaderAtEnd.java?rev=1804110&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/ReaderAtEnd.java (added)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/ReaderAtEnd.java Fri Aug  4 12:31:22 2017
@@ -0,0 +1,27 @@
+/*
+ * 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.file.tar.index;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+interface ReaderAtEnd {
+
+    ByteBuffer readAtEnd(int whence, int amount) throws IOException;
+
+}

Propchange: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/ReaderAtEnd.java
------------------------------------------------------------------------------
    svn:eol-style = native