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/02/16 09:35:56 UTC
svn commit: r1783185 - in
/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file:
JournalUtil.java ReadOnlyRevisions.java TarRevisions.java
Author: frm
Date: Thu Feb 16 09:35:56 2017
New Revision: 1783185
URL: http://svn.apache.org/viewvc?rev=1783185&view=rev
Log:
OAK-5690 - Remove duplicated code from TarRevisions and ReadOnlyRevisions
Added:
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/JournalUtil.java (with props)
Modified:
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
Added: 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/JournalUtil.java?rev=1783185&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/JournalUtil.java (added)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/JournalUtil.java Thu Feb 16 09:35:56 2017
@@ -0,0 +1,64 @@
+/*
+ * 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.File;
+import java.io.IOException;
+
+import org.apache.jackrabbit.oak.segment.RecordId;
+import org.apache.jackrabbit.oak.segment.SegmentStore;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class JournalUtil {
+
+ private static final Logger log = LoggerFactory.getLogger(JournalUtil.class);
+
+ private JournalUtil() {
+ // Prevent instantiation
+ }
+
+ /**
+ * Traverse the journal until a record ID is found that exists in the
+ * provided segment store.
+ *
+ * @param store An instance of {@link SegmentStore}.
+ * @param journal Path to the journal file.
+ * @return An instance of {@link RecordId}, or {@code null} if none could be
+ * found.
+ * @throws IOException If an I/O error occurs.
+ */
+ static RecordId findPersistedRecordId(SegmentStore store, File journal) throws IOException {
+ try (JournalReader journalReader = new JournalReader(journal)) {
+ while (journalReader.hasNext()) {
+ String entry = journalReader.next();
+ try {
+ RecordId id = RecordId.fromString(store, entry);
+ if (store.containsSegment(id.getSegmentId())) {
+ return id;
+ }
+ log.warn("Unable to access revision {}, rewinding...", id);
+ } catch (IllegalArgumentException ignore) {
+ log.warn("Skipping invalid record id {}", entry);
+ }
+ }
+ }
+ return null;
+ }
+
+}
Propchange: jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/JournalUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
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=1783185&r1=1783184&r2=1783185&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 09:35:56 2017
@@ -20,6 +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 java.io.Closeable;
import java.io.File;
@@ -29,14 +30,13 @@ import java.util.concurrent.atomic.Atomi
import javax.annotation.Nonnull;
+import com.google.common.base.Function;
import org.apache.jackrabbit.oak.segment.RecordId;
import org.apache.jackrabbit.oak.segment.Revisions;
import org.apache.jackrabbit.oak.segment.SegmentStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.base.Function;
-
public class ReadOnlyRevisions implements Revisions, Closeable {
private static final Logger LOG = LoggerFactory
@@ -69,34 +69,14 @@ public class ReadOnlyRevisions implement
* @throws IOException
*/
synchronized void bind(@Nonnull SegmentStore store) throws IOException {
- if (head.get() == null) {
- RecordId persistedId = null;
- try (JournalReader journalReader = new JournalReader(new File(
- directory, JOURNAL_FILE_NAME))) {
- while (persistedId == null && journalReader.hasNext()) {
- String entry = journalReader.next();
- try {
- RecordId id = RecordId.fromString(store, entry);
- if (store.containsSegment(id.getSegmentId())) {
- persistedId = id;
- } else {
- LOG.warn(
- "Unable to access revision {}, rewinding...",
- id);
- }
- } catch (IllegalArgumentException ignore) {
- LOG.warn("Skipping invalid record id {}", entry);
- }
- }
- }
-
- if (persistedId == null) {
- throw new IllegalStateException(
- "Cannot start readonly store from empty journal");
- } else {
- head.set(persistedId);
- }
+ if (head.get() != null) {
+ return;
+ }
+ RecordId persistedId = findPersistedRecordId(store, new File(directory, JOURNAL_FILE_NAME));
+ if (persistedId == null) {
+ throw new IllegalStateException("Cannot start readonly store from empty journal");
}
+ head.set(persistedId);
}
private void checkBound() {
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=1783185&r1=1783184&r2=1783185&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 09:35:56 2017
@@ -25,6 +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 java.io.Closeable;
import java.io.File;
@@ -157,33 +158,16 @@ public class TarRevisions implements Rev
* @param writeInitialNode provider for the initial node in case the journal is empty.
* @throws IOException
*/
- synchronized void bind(@Nonnull SegmentStore store,
- @Nonnull Supplier<RecordId> writeInitialNode)
- throws IOException {
- if (head.get() == null) {
- RecordId persistedId = null;
- try (JournalReader journalReader = new JournalReader(new File(directory, JOURNAL_FILE_NAME))) {
- while (persistedId == null && journalReader.hasNext()) {
- String entry = journalReader.next();
- try {
- RecordId id = RecordId.fromString(store, entry);
- if (store.containsSegment(id.getSegmentId())) {
- persistedId = id;
- } else {
- LOG.warn("Unable to access revision {}, rewinding...", id);
- }
- } catch (IllegalArgumentException ignore) {
- LOG.warn("Skipping invalid record id {}", entry);
- }
- }
- }
-
- if (persistedId == null) {
- head.set(writeInitialNode.get());
- } else {
- persistedHead.set(persistedId);
- head.set(persistedId);
- }
+ synchronized void bind(@Nonnull SegmentStore store, @Nonnull Supplier<RecordId> writeInitialNode) throws IOException {
+ if (head.get() != null) {
+ return;
+ }
+ RecordId persistedId = findPersistedRecordId(store, new File(directory, JOURNAL_FILE_NAME));
+ if (persistedId == null) {
+ head.set(writeInitialNode.get());
+ } else {
+ persistedHead.set(persistedId);
+ head.set(persistedId);
}
}