Author: frm
Date: Thu Feb 16 10:33:38 2017
New Revision: 1783195

URL: http://svn.apache.org/viewvc?rev=1783195&view=rev
Log:
OAK-5691 - Remove duplicated code from FileStore and ReadOnlyFileStore

Added:
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStoreUtil.java
      - copied, changed from r1783194, 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/JournalUtil.java
Removed:
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/JournalUtil.java
Modified:
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStore.java
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyFileStore.java
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyRevisions.java
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRevisions.java

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStore.java?rev=1783195&r1=1783194&r2=1783195&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStore.java
 Thu Feb 16 10:33:38 2017
@@ -501,22 +501,14 @@ public class FileStore extends AbstractF
 
     @Override
     public boolean containsSegment(SegmentId id) {
-        long msb = id.getMostSignificantBits();
-        long lsb = id.getLeastSignificantBits();
-        return containsSegment(msb, lsb);
-    }
-
-    private boolean containsSegment(long msb, long lsb) {
-        for (TarReader reader : readers) {
-            if (reader.containsEntry(msb, lsb)) {
-                return true;
-            }
+        if (FileStoreUtil.containSegment(readers, id)) {
+            return true;
         }
 
         if (tarWriter != null) {
             fileStoreLock.readLock().lock();
             try {
-                if (tarWriter.containsEntry(msb, lsb)) {
+                if (tarWriter.containsEntry(id.getMostSignificantBits(), 
id.getLeastSignificantBits())) {
                     return true;
                 }
             } finally {
@@ -526,13 +518,7 @@ public class FileStore extends AbstractF
 
         // the writer might have switched to a new file,
         // so we need to re-check the readers
-        for (TarReader reader : readers) {
-            if (reader.containsEntry(msb, lsb)) {
-                return true;
-            }
-        }
-
-        return false;
+        return FileStoreUtil.containSegment(readers, id);
     }
 
     @Override
@@ -542,32 +528,16 @@ public class FileStore extends AbstractF
             return segmentCache.getSegment(id, new Callable<Segment>() {
                 @Override
                 public Segment call() throws Exception {
-                    long msb = id.getMostSignificantBits();
-                    long lsb = id.getLeastSignificantBits();
-
-                    for (TarReader reader : readers) {
-                        try {
-                            if (reader.isClosed()) {
-                                // Cleanup might already have closed the file.
-                                // The segment should be available from 
another file.
-                                log.debug("Skipping closed tar file {}", 
reader);
-                                continue;
-                            }
-
-                            ByteBuffer buffer = reader.readEntry(msb, lsb);
-                            if (buffer != null) {
-                                return new Segment(FileStore.this, 
segmentReader, id, buffer);
-                            }
-                        } catch (IOException e) {
-                            log.warn("Failed to read from tar file {}", 
reader, e);
-                        }
+                    ByteBuffer buffer = FileStoreUtil.readEntry(readers, id);
+                    if (buffer != null) {
+                        return new Segment(FileStore.this, segmentReader, id, 
buffer);
                     }
 
                     if (tarWriter != null) {
                         fileStoreLock.readLock().lock();
                         try {
                             try {
-                                ByteBuffer buffer = tarWriter.readEntry(msb, 
lsb);
+                                buffer = 
tarWriter.readEntry(id.getMostSignificantBits(), id.getLeastSignificantBits());
                                 if (buffer != null) {
                                     return new Segment(FileStore.this, 
segmentReader, id, buffer);
                                 }
@@ -579,39 +549,33 @@ public class FileStore extends AbstractF
                         }
                     }
 
-                    // the writer might have switched to a new file,
-                    // so we need to re-check the readers
-                    for (TarReader reader : readers) {
-                        try {
-                            if (reader.isClosed()) {
-                                // Cleanup might already have closed the file.
-                                // The segment should be available from 
another file.
-                                log.info("Skipping closed tar file {}", 
reader);
-                                continue;
-                            }
-
-                            ByteBuffer buffer = reader.readEntry(msb, lsb);
-                            if (buffer != null) {
-                                return new Segment(FileStore.this, 
segmentReader, id, buffer);
-                            }
-                        } catch (IOException e) {
-                            log.warn("Failed to read from tar file {}", 
reader, e);
-                        }
+                    // The TarWriter might have become a TarReader in the
+                    // meantime. Moreover, the TarWriter that became a 
TarReader
+                    // might have additional entries. Because of this, we need
+                    // to check the list of TarReaders once more.
+
+                    buffer = FileStoreUtil.readEntry(readers, id);
+                    if (buffer != null) {
+                        return new Segment(FileStore.this, segmentReader, id, 
buffer);
                     }
 
                     throw new SegmentNotFoundException(id);
                 }
             });
         } catch (ExecutionException e) {
-            SegmentNotFoundException snfe = e.getCause() instanceof 
SegmentNotFoundException
-                    ? (SegmentNotFoundException) e.getCause() : new 
SegmentNotFoundException(id, e);
-
+            SegmentNotFoundException snfe = asSegmentNotFoundException(e, id);
             snfeListener.notify(id, snfe);
-            
             throw snfe;
         }
     }
 
+    private SegmentNotFoundException 
asSegmentNotFoundException(ExecutionException e, SegmentId id) {
+        if (e.getCause() instanceof SegmentNotFoundException) {
+            return (SegmentNotFoundException) e.getCause();
+        }
+        return new SegmentNotFoundException(id, e);
+    }
+
     @Override
     public void writeSegment(SegmentId id, byte[] buffer, int offset, int 
length) throws IOException {
         Segment segment = null;

Copied: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStoreUtil.java
 (from r1783194, 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/JournalUtil.java)
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStoreUtil.java?p2=jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStoreUtil.java&p1=jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/JournalUtil.java&r1=1783194&r2=1783195&rev=1783195&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/JournalUtil.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/FileStoreUtil.java
 Thu Feb 16 10:33:38 2017
@@ -19,17 +19,20 @@ package org.apache.jackrabbit.oak.segmen
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.List;
 
 import org.apache.jackrabbit.oak.segment.RecordId;
+import org.apache.jackrabbit.oak.segment.SegmentId;
 import org.apache.jackrabbit.oak.segment.SegmentStore;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-class JournalUtil {
+class FileStoreUtil {
 
-    private static final Logger log = 
LoggerFactory.getLogger(JournalUtil.class);
+    private static final Logger log = 
LoggerFactory.getLogger(FileStoreUtil.class);
 
-    private JournalUtil() {
+    private FileStoreUtil() {
         // Prevent instantiation
     }
 
@@ -59,6 +62,50 @@ class JournalUtil {
             }
         }
         return null;
+    }
+
+    /**
+     * Check if a segment is contained in one of the provided TAR files.
+     *
+     * @param readers A list of {@link TarReader} instances.
+     * @param id      An instance of {@link SegmentId}.
+     * @return {@code true} if the segment is contained in at least one of the
+     * provided TAR files, {@code false} otherwise.
+     */
+    static boolean containSegment(List<TarReader> readers, SegmentId id) {
+        for (TarReader reader : readers) {
+            if (reader.containsEntry(id.getMostSignificantBits(), 
id.getLeastSignificantBits())) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Read the entry corresponding to a segment from one of the provided TAR
+     * files.
+     *
+     * @param readers A list of {@link TarReader} instances.
+     * @param id      An instance of {@link SegmentId}.
+     * @return An instance of {@link ByteBuffer} if the entry for the segment
+     * could be found, {@code null} otherwise.
+     */
+    static ByteBuffer readEntry(List<TarReader> readers, SegmentId id) {
+        for (TarReader reader : readers) {
+            if (reader.isClosed()) {
+                log.debug("Skipping closed tar file {}", reader);
+                continue;
+            }
+            try {
+                ByteBuffer buffer = 
reader.readEntry(id.getMostSignificantBits(), id.getLeastSignificantBits());
+                if (buffer != null) {
+                    return buffer;
+                }
+            } catch (IOException e) {
+                log.warn("Failed to read from tar file {}", reader, e);
+            }
+        }
+        return null;
     }
 
 }

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyFileStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyFileStore.java?rev=1783195&r1=1783194&r2=1783195&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyFileStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyFileStore.java
 Thu Feb 16 10:33:38 2017
@@ -159,14 +159,7 @@ public class ReadOnlyFileStore extends A
 
     @Override
     public boolean containsSegment(SegmentId id) {
-        long msb = id.getMostSignificantBits();
-        long lsb = id.getLeastSignificantBits();
-        for (TarReader reader : readers) {
-            if (reader.containsEntry(msb, lsb)) {
-                return true;
-            }
-        }
-        return false;
+        return FileStoreUtil.containSegment(readers, id);
     }
 
     @Override
@@ -176,20 +169,11 @@ public class ReadOnlyFileStore extends A
             return segmentCache.getSegment(id, new Callable<Segment>() {
                 @Override
                 public Segment call() throws Exception {
-                    long msb = id.getMostSignificantBits();
-                    long lsb = id.getLeastSignificantBits();
-
-                    for (TarReader reader : readers) {
-                        try {
-                            ByteBuffer buffer = reader.readEntry(msb, lsb);
-                            if (buffer != null) {
-                                return new Segment(ReadOnlyFileStore.this, 
segmentReader, id, buffer);
-                            }
-                        } catch (IOException e) {
-                            log.warn("Failed to read from tar file {}", 
reader, e);
-                        }
+                    ByteBuffer buffer = FileStoreUtil.readEntry(readers, id);
+                    if (buffer == null) {
+                        throw new SegmentNotFoundException(id);
                     }
-                    throw new SegmentNotFoundException(id);
+                    return new Segment(ReadOnlyFileStore.this, segmentReader, 
id, buffer);
                 }
             });
         } catch (ExecutionException e) {

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyRevisions.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyRevisions.java?rev=1783195&r1=1783194&r2=1783195&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyRevisions.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/ReadOnlyRevisions.java
 Thu Feb 16 10:33:38 2017
@@ -20,7 +20,7 @@ package org.apache.jackrabbit.oak.segmen
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
-import static 
org.apache.jackrabbit.oak.segment.file.JournalUtil.findPersistedRecordId;
+import static 
org.apache.jackrabbit.oak.segment.file.FileStoreUtil.findPersistedRecordId;
 
 import java.io.Closeable;
 import java.io.File;

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRevisions.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRevisions.java?rev=1783195&r1=1783194&r2=1783195&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRevisions.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/TarRevisions.java
 Thu Feb 16 10:33:38 2017
@@ -25,7 +25,7 @@ import static com.google.common.base.Thr
 import static com.google.common.base.Throwables.propagateIfInstanceOf;
 import static java.lang.Long.MAX_VALUE;
 import static java.util.concurrent.TimeUnit.DAYS;
-import static 
org.apache.jackrabbit.oak.segment.file.JournalUtil.findPersistedRecordId;
+import static 
org.apache.jackrabbit.oak.segment.file.FileStoreUtil.findPersistedRecordId;
 
 import java.io.Closeable;
 import java.io.File;


Reply via email to