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 ca...@apache.org on 2016/04/13 18:17:05 UTC

svn commit: r1738966 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/ oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/ oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/commands/

Author: catholicon
Date: Wed Apr 13 16:17:05 2016
New Revision: 1738966

URL: http://svn.apache.org/viewvc?rev=1738966&view=rev
Log:
OAK-4198: Add oak run command to export document and its dependencies

Also, updated mongo deserialization a bit to assume Long for Number coming in for _modCount and _modified

Added:
    jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/commands/ExportRelevantDocumentsCommand.groovy
    jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/commands/ExportRelevantDocumentsCommand.properties   (with props)
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
    jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/GroovyConsole.groovy

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java?rev=1738966&r1=1738965&r2=1738966&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java Wed Apr 13 16:17:05 2016
@@ -1294,6 +1294,9 @@ public class MongoDocumentStore implemen
                 Object o = n.get(key);
                 if (o instanceof String) {
                     copy.put(key, o);
+                } else if (o instanceof Number &&
+                        (NodeDocument.MODIFIED_IN_SECS.equals(key) || Document.MOD_COUNT.equals(key))) {
+                    copy.put(key, Utils.asLong((Number) o));
                 } else if (o instanceof Long) {
                     copy.put(key, o);
                 } else if (o instanceof Integer) {

Modified: jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/GroovyConsole.groovy
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/GroovyConsole.groovy?rev=1738966&r1=1738965&r2=1738966&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/GroovyConsole.groovy (original)
+++ jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/GroovyConsole.groovy Wed Apr 13 16:17:05 2016
@@ -120,7 +120,8 @@ class GroovyConsole {
                 new PnCommand(shell),
                 new RefreshCommand(shell),
                 new RetrieveCommand(shell),
-                new LuceneCommand(shell)
+                new LuceneCommand(shell),
+                new ExportRelevantDocumentsCommand(shell)
         ])
 
         if(session.store instanceof DocumentNodeStore){

Added: jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/commands/ExportRelevantDocumentsCommand.groovy
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/commands/ExportRelevantDocumentsCommand.groovy?rev=1738966&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/commands/ExportRelevantDocumentsCommand.groovy (added)
+++ jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/commands/ExportRelevantDocumentsCommand.groovy Wed Apr 13 16:17:05 2016
@@ -0,0 +1,129 @@
+/*
+ * 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.console.commands
+
+import org.apache.jackrabbit.oak.commons.PathUtils
+import org.apache.jackrabbit.oak.console.ConsoleSession
+import org.apache.jackrabbit.oak.plugins.document.Collection
+import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore
+import org.apache.jackrabbit.oak.plugins.document.DocumentStore
+import org.apache.jackrabbit.oak.plugins.document.NodeDocument
+import org.apache.jackrabbit.oak.plugins.document.util.Utils
+import org.codehaus.groovy.tools.shell.CommandSupport
+import org.codehaus.groovy.tools.shell.Groovysh
+
+class ExportRelevantDocumentsCommand extends CommandSupport {
+    public static final String COMMAND_NAME = 'export-docs'
+
+    private DocumentNodeStore store;
+    private DocumentStore docStore;
+
+    private String path;
+    private String out = 'all-required-nodes.json'
+
+    public ExportRelevantDocumentsCommand(Groovysh shell) {
+        super(shell, COMMAND_NAME, 'export')
+    }
+
+    @Override
+    Object execute(List<String> args) {
+        if (parseOpts(args)) {
+            initStores()
+            exportDocs()
+        }
+    }
+
+    private boolean parseOpts(List<String> args) {
+        def cli = new CliBuilder(usage: getName() + ' ' + getUsage(), header: getHelp())
+
+        cli.h(longOpt: 'help', 'Print usage')
+        cli.o(longOpt: 'out', args: 1, argName: 'out',
+                'File name to export documents in (default: ' + out + ')')
+        cli.p(longOpt: 'path', args: 1, argName: 'path', 'Repository path to export (default: current node)')
+
+        def options = cli.parse(args)
+
+        if (!options) {
+            return false
+        }
+
+        if (options.h) {
+            cli.usage()
+            return false
+        }
+
+        path = options.path ?: session.getWorkingPath()
+        if (!PathUtils.isAbsolute(path)) {
+            path = PathUtils.concat(session.getWorkingPath(), path);
+        }
+
+        out = options.out ?: out
+
+        return true
+    }
+
+    private void initStores() {
+        store = (DocumentNodeStore)getSession().getStore()
+        docStore = store.getDocumentStore()
+    }
+
+    private void exportDocs() {
+        File file = new File(out)
+        file.delete()
+
+        file = new File(out)
+
+        io.println("Exporting " + path + " to " + file.absolutePath)
+
+        writeOutDocAndAncestors(file)
+    }
+
+    private void writeOutDocAndAncestors(File file) {
+        String currPath = path
+
+        writeOutDocAndSplits(currPath, file)
+        while (!PathUtils.denotesRoot(currPath)) {
+            currPath = PathUtils.getParentPath(currPath)
+            writeOutDocAndSplits(currPath, file)
+        }
+    }
+
+    private void writeOutDocAndSplits(String currPath, File file) {
+        NodeDocument doc = docStore.find(Collection.NODES, Utils.getIdFromPath(currPath))
+
+        if (doc != null) {
+            writeOutDoc(doc, file)
+
+            for (NodeDocument splitDoc : doc.getAllPreviousDocs()) {
+                writeOutDoc(splitDoc, file)
+            }
+        }
+    }
+
+    private void writeOutDoc(NodeDocument doc, File file) {
+        file << '{'
+        file << doc.asString()
+        file << '}'
+        file << System.getProperty("line.separator")
+    }
+
+    ConsoleSession getSession(){
+        return (ConsoleSession)variables.session
+    }
+}

Added: jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/commands/ExportRelevantDocumentsCommand.properties
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/commands/ExportRelevantDocumentsCommand.properties?rev=1738966&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/commands/ExportRelevantDocumentsCommand.properties (added)
+++ jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/commands/ExportRelevantDocumentsCommand.properties Wed Apr 13 16:17:05 2016
@@ -0,0 +1,21 @@
+#
+# 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.
+#
+command.description=Export documents and its ancestors along with relevant split documents.
+command.usage=[-h] [-p <repo_path_to_export>] [-o <file_name>]
+command.help=Export documents and its ancestors along with relevant split documents.

Propchange: jackrabbit/oak/trunk/oak-run/src/main/groovy/org/apache/jackrabbit/oak/console/commands/ExportRelevantDocumentsCommand.properties
------------------------------------------------------------------------------
    svn:eol-style = native