Author: frm
Date: Mon Aug  7 07:48:37 2017
New Revision: 1804292

URL: http://svn.apache.org/viewvc?rev=1804292&view=rev
Log:
OAK-6518 - Add tests for loading transparently different index formats

Added:
    
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderTest.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/TarReader.java
    
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/ReaderAtEnd.java

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/TarReader.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/TarReader.java?rev=1804292&r1=1804291&r2=1804292&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/TarReader.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/TarReader.java
 Mon Aug  7 07:48:37 2017
@@ -65,6 +65,7 @@ import org.apache.jackrabbit.oak.segment
 import org.apache.jackrabbit.oak.segment.file.tar.index.IndexEntry;
 import org.apache.jackrabbit.oak.segment.file.tar.index.IndexLoader;
 import org.apache.jackrabbit.oak.segment.file.tar.index.InvalidIndexException;
+import org.apache.jackrabbit.oak.segment.file.tar.index.ReaderAtEnd;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -335,11 +336,34 @@ class TarReader implements Closeable {
      * instead.
      */
     private static Index loadAndValidateIndex(RandomAccessFile file, String 
name) throws IOException {
+        long length = file.length();
+
+        if (length % BLOCK_SIZE != 0) {
+            log.warn("Unable to load index of file {}: Invalid alignment", 
name);
+            return null;
+        }
+        if (length < 6 * BLOCK_SIZE) {
+            log.warn("Unable to load index of file {}: File too short", name);
+            return null;
+        }
+        if (length > Integer.MAX_VALUE) {
+            log.warn("Unable to load index of file {}: File too long", name);
+            return null;
+        }
+
+        ReaderAtEnd r = (whence, size) -> {
+            ByteBuffer buffer = ByteBuffer.allocate(size);
+            file.seek(length - 2 * BLOCK_SIZE - whence);
+            file.readFully(buffer.array());
+            return buffer;
+        };
+
         try {
-            return indexLoader.loadIndex(file);
+            return indexLoader.loadIndex(r);
         } catch (InvalidIndexException e) {
             log.warn("Unable to load index of file {}: {}", name, 
e.getMessage());
         }
+
         return null;
     }
 

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=1804292&r1=1804291&r2=1804292&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
 Mon Aug  7 07:48:37 2017
@@ -46,7 +46,7 @@ public class IndexLoader {
         return reader.readAtEnd(Integer.BYTES, Integer.BYTES).getInt();
     }
 
-    private Index loadIndex(ReaderAtEnd reader) throws IOException, 
InvalidIndexException {
+    public Index loadIndex(ReaderAtEnd reader) throws IOException, 
InvalidIndexException {
         switch (readMagic(reader)) {
             case IndexLoaderV1.MAGIC:
                 return v1.loadIndex(reader);
@@ -57,19 +57,4 @@ public class IndexLoader {
         }
     }
 
-    public Index loadIndex(RandomAccessFile file) throws IOException, 
InvalidIndexException {
-        long length = file.length();
-
-        if (length % blockSize != 0 || length < 6 * blockSize || length > 
Integer.MAX_VALUE) {
-            throw new InvalidIndexException(String.format("Unexpected size 
%d", length));
-        }
-
-        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/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=1804292&r1=1804291&r2=1804292&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/ReaderAtEnd.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/index/ReaderAtEnd.java
 Mon Aug  7 07:48:37 2017
@@ -20,7 +20,7 @@ package org.apache.jackrabbit.oak.segmen
 import java.io.IOException;
 import java.nio.ByteBuffer;
 
-interface ReaderAtEnd {
+public interface ReaderAtEnd {
 
     ByteBuffer readAtEnd(int whence, int amount) throws IOException;
 

Added: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderTest.java?rev=1804292&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/index/IndexLoaderTest.java
 Mon Aug  7 07:48:37 2017
@@ -0,0 +1,95 @@
+/*
+ * 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 static 
org.apache.jackrabbit.oak.segment.file.tar.index.IndexLoader.newIndexLoader;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.nio.ByteBuffer;
+
+import org.junit.Test;
+
+public class IndexLoaderTest {
+
+    private static Index loadIndex(ByteBuffer buffer) throws Exception {
+        return newIndexLoader(1).loadIndex((whence, length) -> {
+            ByteBuffer slice = buffer.duplicate();
+            slice.position(slice.limit() - whence);
+            slice.limit(slice.position() + length);
+            return slice.slice();
+        });
+    }
+
+    private static void assertEntry(IndexEntry entry, long msb, long lsb, int 
position, int length, int full, int tail, boolean isTail) {
+        assertEquals(msb, entry.getMsb());
+        assertEquals(lsb, entry.getLsb());
+        assertEquals(position, entry.getPosition());
+        assertEquals(length, entry.getLength());
+        assertEquals(full, entry.getFullGeneration());
+        assertEquals(tail, entry.getTailGeneration());
+        assertEquals(isTail, entry.isTail());
+    }
+
+    @Test(expected = InvalidIndexException.class)
+    public void testUnrecognizedMagicNumber() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE);
+        buffer.duplicate().putInt(0xDEADBEEF);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidIndexException e) {
+            assertEquals("Unrecognized magic number", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test
+    public void testLoadIndexV1() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV1.SIZE + 
IndexV1.FOOTER_SIZE);
+        buffer.duplicate()
+                .putLong(1).putLong(2).putInt(3).putInt(4).putInt(5)
+                .putLong(6).putLong(7).putInt(8).putInt(9).putInt(10)
+                .putInt(0x3F5F40E9)
+                .putInt(2)
+                .putInt(2 * IndexEntryV1.SIZE + IndexV1.FOOTER_SIZE)
+                .putInt(IndexLoaderV1.MAGIC);
+        Index index = loadIndex(buffer);
+        assertNotNull(index);
+        assertEquals(2, index.count());
+        assertEntry(index.entry(0), 1, 2, 3, 4, 5, 0, false);
+        assertEntry(index.entry(1), 6, 7, 8, 9, 10, 0, false);
+    }
+
+    @Test
+    public void testLoadIndexV2() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(2 * IndexEntryV2.SIZE + 
IndexV2.FOOTER_SIZE);
+        buffer.duplicate()
+                
.putLong(1).putLong(2).putInt(3).putInt(4).putInt(5).putInt(6).put((byte) 0)
+                
.putLong(7).putLong(8).putInt(9).putInt(10).putInt(11).putInt(12).put((byte) 1)
+                .putInt(0xE2138EB4)
+                .putInt(2)
+                .putInt(2 * IndexEntryV2.SIZE + IndexV2.FOOTER_SIZE)
+                .putInt(IndexLoaderV2.MAGIC);
+        Index index = loadIndex(buffer);
+        assertNotNull(index);
+        assertEquals(2, index.count());
+        assertEntry(index.entry(0), 1, 2, 3, 4, 5, 6, false);
+        assertEntry(index.entry(1), 7, 8, 9, 10, 11, 12, true);
+    }
+
+}

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


Reply via email to