Author: chetanm
Date: Thu May 18 06:27:50 2017
New Revision: 1795478
URL: http://svn.apache.org/viewvc?rev=1795478&view=rev
Log:
OAK-6232 - Utility class to dump lucene index content
Added:
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/LuceneIndexDumper.java
(with props)
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/IndexDumperTest.java
(with props)
Added:
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/LuceneIndexDumper.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/LuceneIndexDumper.java?rev=1795478&view=auto
==============================================================================
---
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/LuceneIndexDumper.java
(added)
+++
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/LuceneIndexDumper.java
Thu May 18 06:27:50 2017
@@ -0,0 +1,126 @@
+/*
+ * 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.index.lucene.directory;
+
+import java.io.File;
+import java.io.IOException;
+
+import com.google.common.io.Closer;
+import org.apache.commons.io.FileUtils;
+import org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition;
+import org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.oak.spi.state.NodeStateUtils;
+import org.apache.jackrabbit.oak.spi.state.ReadOnlyBuilder;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+import org.apache.lucene.store.IOContext;
+
+import static
org.apache.jackrabbit.oak.plugins.index.lucene.directory.IndexRootDirectory.INDEX_METADATA_FILE_NAME;
+import static
org.apache.jackrabbit.oak.plugins.index.lucene.writer.MultiplexersLucene.isIndexDirName;
+import static
org.apache.jackrabbit.oak.plugins.index.lucene.writer.MultiplexersLucene.isSuggestIndexDirName;
+
+public class LuceneIndexDumper {
+ private final NodeState rootState;
+ private final String indexPath;
+ private final File baseDir;
+ private long size;
+ private File indexDir;
+
+ /**
+ * Constructs the dumper for copying Lucene index contents
+ *
+ * @param rootState rootState of repository
+ * @param indexPath path of index
+ * @param baseDir directory under which index contents would be copied to.
Dumper
+ * would create a sub directory based on index path and
then copy
+ * the index content under that directory
+ */
+ public LuceneIndexDumper(NodeState rootState, String indexPath, File
baseDir) {
+ this.rootState = rootState;
+ this.indexPath = indexPath;
+ this.baseDir = baseDir;
+ }
+
+ public void dump() throws IOException {
+ try (Closer closer = Closer.create()) {
+ NodeState idx = NodeStateUtils.getNode(rootState, indexPath);
+ IndexDefinition defn = IndexDefinition.newBuilder(rootState, idx,
indexPath).build();
+ indexDir = createIndexDir();
+ writeMeta();
+ for (String dirName : idx.getChildNodeNames()) {
+ if (NodeStateUtils.isHidden(dirName) &&
+ (isIndexDirName(dirName) ||
isSuggestIndexDirName(dirName))) {
+ copyContent(idx, defn, indexDir, dirName, closer);
+ }
+ }
+ }
+ }
+
+ public long getSize() {
+ return size;
+ }
+
+ public File getIndexDir() {
+ return indexDir;
+ }
+
+ private void writeMeta() throws IOException {
+ File readMe = new File(indexDir, INDEX_METADATA_FILE_NAME);
+ IndexMeta meta = new IndexMeta(indexPath, System.currentTimeMillis());
+ meta.writeTo(readMe);
+ }
+
+ private File createIndexDir() throws IOException {
+ String subDirPath =
IndexRootDirectory.getIndexFolderBaseName(indexPath);
+ File indexDir = new File(baseDir, subDirPath);
+ int count = 0;
+ while (true) {
+ if (indexDir.exists()) {
+ indexDir = new File(baseDir, subDirPath + "_" + count++);
+ } else {
+ break;
+ }
+ }
+ FileUtils.forceMkdir(indexDir);
+ return indexDir;
+ }
+
+ private void copyContent(NodeState idx, IndexDefinition defn, File dir,
String dirName, Closer closer) throws IOException {
+ File idxDir = createWorkDir(dir, dirName);
+ Directory sourceDir = new OakDirectory(new ReadOnlyBuilder(idx),
dirName, defn, true);
+ Directory targetDir = FSDirectory.open(idxDir);
+
+ closer.register(sourceDir);
+ closer.register(targetDir);
+
+ for (String file : sourceDir.listAll()) {
+ sourceDir.copy(targetDir, file, file, IOContext.DEFAULT);
+ size += sourceDir.fileLength(file);
+ }
+ }
+
+ private static File createWorkDir(File parent, String name) throws
IOException {
+ String fsSafeName = IndexRootDirectory.getFSSafeName(name);
+ File dir = new File(parent, fsSafeName);
+ FileUtils.forceMkdir(dir);
+ return dir;
+ }
+}
Propchange:
jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/LuceneIndexDumper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/IndexDumperTest.java
URL:
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/IndexDumperTest.java?rev=1795478&view=auto
==============================================================================
---
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/IndexDumperTest.java
(added)
+++
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/IndexDumperTest.java
Thu May 18 06:27:50 2017
@@ -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.index.lucene.directory;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.jackrabbit.oak.InitialContent;
+import org.apache.jackrabbit.oak.plugins.index.lucene.IndexDefinition;
+import org.apache.jackrabbit.oak.plugins.index.lucene.OakDirectory;
+import
org.apache.jackrabbit.oak.plugins.index.lucene.util.IndexDefinitionBuilder;
+import
org.apache.jackrabbit.oak.plugins.index.lucene.writer.MultiplexersLucene;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.IOContext;
+import org.apache.lucene.store.IndexOutput;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class IndexDumperTest {
+ private NodeState rootState = InitialContent.INITIAL_CONTENT;
+ private NodeBuilder idx = new IndexDefinitionBuilder().build().builder();
+
+ @Rule
+ public final TemporaryFolder temporaryFolder = new TemporaryFolder(new
File("target"));
+
+ @Test
+ public void directoryDump() throws Exception{
+ IndexDefinition defn = IndexDefinition.newBuilder(rootState,
idx.getNodeState(), "/fooIndex").build();
+
+ long size = 0;
+
+ Directory dir = new OakDirectory(idx, ":data", defn, false);
+ createFile(dir, "foo.txt", "Test content");
+ size += DirectoryUtils.dirSize(dir);
+
+ Directory dir2 = new OakDirectory(idx, ":data2"+
MultiplexersLucene.INDEX_DIR_SUFFIX, defn, false);
+ createFile(dir2, "foo.txt", "Test content");
+ size += DirectoryUtils.dirSize(dir2);
+
+ NodeBuilder builder = rootState.builder();
+ builder.setChildNode("fooIndex", idx.getNodeState());
+ NodeState indexState = builder.getNodeState();
+
+ File out = temporaryFolder.newFolder();
+ LuceneIndexDumper dumper = new LuceneIndexDumper(indexState,
"/fooIndex", out);
+ dumper.dump();
+
+ File indexDir = dumper.getIndexDir();
+ assertNotNull(indexDir);
+
+ assertEquals(3, indexDir.listFiles().length); // 2 dir + 1 meta
+ assertEquals(dumper.getSize(), size);
+ }
+
+ private int createFile(Directory dir, String fileName, String content)
throws IOException {
+ byte[] data = content.getBytes();
+ IndexOutput o = dir.createOutput(fileName, IOContext.DEFAULT);
+ o.writeBytes(data, data.length);
+ o.close();
+ return data.length;
+ }
+}
\ No newline at end of file
Propchange:
jackrabbit/oak/trunk/oak-lucene/src/test/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/IndexDumperTest.java
------------------------------------------------------------------------------
svn:eol-style = native