Author: alexparvulescu Date: Mon Feb 3 08:49:46 2014 New Revision: 1563802
URL: http://svn.apache.org/r1563802 Log: OAK-1380 Restore from backup utility class Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreRestore.java (with props) Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackupTest.java Added: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreRestore.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreRestore.java?rev=1563802&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreRestore.java (added) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreRestore.java Mon Feb 3 08:49:46 2014 @@ -0,0 +1,86 @@ +/* + * 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.plugins.backup; + +import java.io.File; +import java.io.IOException; + +import org.apache.jackrabbit.oak.api.CommitFailedException; +import org.apache.jackrabbit.oak.plugins.segment.Journal; +import org.apache.jackrabbit.oak.plugins.segment.SegmentNodeState; +import org.apache.jackrabbit.oak.plugins.segment.file.FileStore; +import org.apache.jackrabbit.oak.spi.commit.EmptyHook; +import org.apache.jackrabbit.oak.spi.state.ApplyDiff; +import org.apache.jackrabbit.oak.spi.state.NodeBuilder; +import org.apache.jackrabbit.oak.spi.state.NodeState; +import org.apache.jackrabbit.oak.spi.state.NodeStore; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FileStoreRestore { + + private static final Logger log = LoggerFactory + .getLogger(FileStoreRestore.class); + + static int MAX_FILE_SIZE = 256; + + private static final String JOURNAL_FILE_NAME = "journal.log"; + + public static void restore(File source, NodeStore store) + throws IOException, CommitFailedException { + // 1. verify that this is an actual filestore + if (!validFileStore(source)) { + throw new IOException("Folder " + source + + " is not a valid FileStore directory"); + } + + // 2. init filestore + FileStore restore = new FileStore(source, MAX_FILE_SIZE, false); + try { + Journal journal = restore.getJournal("root"); + SegmentNodeState state = new SegmentNodeState(restore.getWriter() + .getDummySegment(), journal.getHead()); + restore(state.getChildNode("root"), store); + } finally { + restore.close(); + } + } + + public static void restore(NodeState source, NodeStore store) + throws CommitFailedException { + long s = System.currentTimeMillis(); + NodeState after = store.getRoot(); + NodeBuilder builder = after.builder(); + source.compareAgainstBaseState(after, new ApplyDiff(builder)); + store.merge(builder, EmptyHook.INSTANCE, null); + log.debug("Restore finished in {} ms.", System.currentTimeMillis() - s); + } + + private static boolean validFileStore(File source) { + if (source == null || !source.isDirectory()) { + return false; + } + for (String f : source.list()) { + if (JOURNAL_FILE_NAME.equals(f)) { + return true; + } + } + return false; + } +} Propchange: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreRestore.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackupTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackupTest.java?rev=1563802&r1=1563801&r2=1563802&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackupTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/backup/FileStoreBackupTest.java Mon Feb 3 08:49:46 2014 @@ -81,6 +81,25 @@ public class FileStoreBackupTest { source.close(); } + @Test + public void testRestore() throws Exception { + FileStore source = new FileStore(src, 256, false); + + NodeStore store = new SegmentNodeStore(source); + init(store); + + // initial content + FileStoreBackup.backup(store, destination); + + addTestContent(store); + + FileStoreRestore.restore(destination, store); + + compare(store, destination); + + source.close(); + } + private static void addTestContent(NodeStore store) throws CommitFailedException { NodeBuilder builder = store.getRoot().builder();
