Author: frm
Date: Thu Sep  1 13:59:07 2016
New Revision: 1758775

URL: http://svn.apache.org/viewvc?rev=1758775&view=rev
Log:
OAK-4740 - Parameterize the strategy for the recovery of a segment

Added:
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRecovery.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarReader.java

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarReader.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarReader.java?rev=1758775&r1=1758774&r2=1758775&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarReader.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarReader.java
 Thu Sep  1 13:59:07 2016
@@ -71,6 +71,18 @@ class TarReader implements Closeable {
 
     private static final Logger GC_LOG = 
LoggerFactory.getLogger(TarReader.class.getName() + "-GC");
 
+    private static final TarRecovery DEFAULT_TAR_RECOVERY = new TarRecovery() {
+
+        @Override
+        public void recoverEntry(UUID uuid, byte[] data, TarWriter writer) 
throws IOException {
+            int generation = getGcGeneration(wrap(data), uuid);
+            long msb = uuid.getMostSignificantBits();
+            long lsb = uuid.getLeastSignificantBits();
+            writer.writeEntry(msb, lsb, data, 0, data.length, generation);
+        }
+
+    };
+
     /** Magic byte sequence at the end of the index block. */
     private static final int INDEX_MAGIC = TarWriter.INDEX_MAGIC;
 
@@ -136,7 +148,7 @@ class TarReader implements Closeable {
 
         // regenerate the first generation based on the recovered data
         File file = sorted.values().iterator().next();
-        generateTarFile(entries, file);
+        generateTarFile(entries, file, DEFAULT_TAR_RECOVERY);
 
         reader = openFirstFileWithValidIndex(singletonList(file), 
memoryMapping);
         if (reader != null) {
@@ -166,7 +178,7 @@ class TarReader implements Closeable {
             LinkedHashMap<UUID, byte[]> entries = newLinkedHashMap();
             collectFileEntries(file, entries, false);
             file = findAvailGen(file, ".ro.bak");
-            generateTarFile(entries, file);
+            generateTarFile(entries, file, DEFAULT_TAR_RECOVERY);
             reader = openFirstFileWithValidIndex(singletonList(file),
                     memoryMapping);
             if (reader != null) {
@@ -213,20 +225,18 @@ class TarReader implements Closeable {
      * @param file
      * @throws IOException
      */
-    private static void generateTarFile(LinkedHashMap<UUID, byte[]> entries,
-            File file) throws IOException {
+    private static void generateTarFile(LinkedHashMap<UUID, byte[]> entries, 
File file, TarRecovery recovery) throws IOException {
         log.info("Regenerating tar file {}", file);
-        TarWriter writer = new TarWriter(file);
-        for (Map.Entry<UUID, byte[]> entry : entries.entrySet()) {
-            UUID uuid = entry.getKey();
-            byte[] data = entry.getValue();
-            int generation = getGcGeneration(wrap(data), uuid);
-            writer.writeEntry(
-                    uuid.getMostSignificantBits(),
-                    uuid.getLeastSignificantBits(),
-                    data, 0, data.length, generation);
+
+        try (TarWriter writer = new TarWriter(file)) {
+            for (Entry<UUID, byte[]> entry : entries.entrySet()) {
+                try {
+                    recovery.recoverEntry(entry.getKey(), entry.getValue(), 
writer);
+                } catch (IOException e) {
+                    throw new IOException(String.format("Unable to recover 
entry %s for file %s", entry.getKey(), file), e);
+                }
+            }
         }
-        writer.close();
     }
 
     /**

Added: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRecovery.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRecovery.java?rev=1758775&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRecovery.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRecovery.java
 Thu Sep  1 13:59:07 2016
@@ -0,0 +1,42 @@
+/*
+ * 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;
+
+import java.io.IOException;
+import java.util.UUID;
+
+/**
+ * A strategy for the recovery of segments.
+ */
+interface TarRecovery {
+
+    /**
+     * Recover the data and meta-data of the given segment. The implementor of
+     * this method might want to parse the content of the segment and generate
+     * any metadata as needed. The result of the recovery process has to be
+     * saved in the provided {@link TarWriter}.
+     *
+     * @param uuid   the identifier of the segment.
+     * @param data   the raw data of the segment.
+     * @param writer the destination of the recovered data.
+     * @throws IOException if an I/O error occurs while recovering the data of
+     *                     the segment.
+     */
+    void recoverEntry(UUID uuid, byte[] data, TarWriter writer) throws 
IOException;
+
+}

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


Reply via email to