Author: thomasm
Date: Tue Mar 27 08:57:57 2018
New Revision: 1827816

URL: http://svn.apache.org/viewvc?rev=1827816&view=rev
Log:
OAK-6081 - Indexing tooling via oak-run

Modified:
    jackrabbit/oak/trunk/oak-doc/src/site/markdown/query/index-management.md
    
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/JsonIndexCommand.java
    
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/JsonIndexTest.java

Modified: 
jackrabbit/oak/trunk/oak-doc/src/site/markdown/query/index-management.md
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-doc/src/site/markdown/query/index-management.md?rev=1827816&r1=1827815&r2=1827816&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-doc/src/site/markdown/query/index-management.md 
(original)
+++ jackrabbit/oak/trunk/oak-doc/src/site/markdown/query/index-management.md 
Tue Mar 27 08:57:57 2018
@@ -33,7 +33,12 @@ To start the interactive mode,
 with the branch-less commit option, and MongoDB, use:
 
     java -jar oak-run-*.jar json-index --disableBranches --script - \
-      mongodb://localhost:27017/oak
+      mongodb://localhost:27017/oak --user admin --password admin
+
+When using the segment store, use:      
+
+    java -jar oak-run-*.jar json-index --disableBranches --script - \
+      repository/segmentstore --read-write --user admin --password admin
 
 This will open the repository and allow to write and read from the repository.
 An example script creates an index "test", and then re-indexes it:
@@ -144,7 +149,7 @@ The following script created the index e
     ]}
     exit
     
-#### Create Nodex for Testing
+#### Create Nodes for Testing
 
 The tool can also be used for testing queries, and creating nodes.
 The following creates 1000 nodes `/test/n<x>`, saving every 100 nodes:
@@ -171,3 +176,11 @@ The following creates 1000 nodes `/test/
     {"print": "done"}
     exit
 
+#### Estimate the Number of Nodes
+
+The following will estimate the number of nodes in a given subtree.
+
+    {"xpath": "explain measure /jcr:root/content//* option(traversal ok)"}
+    exit
+    
+The field "cost" will contain the number of estimated nodes.

Modified: 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/JsonIndexCommand.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/JsonIndexCommand.java?rev=1827816&r1=1827815&r2=1827816&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/JsonIndexCommand.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/JsonIndexCommand.java
 Tue Mar 27 08:57:57 2018
@@ -90,6 +90,12 @@ public class JsonIndexCommand implements
         OptionSpec<String> scriptOption = parser
                 .accepts("script", "Path to Script").withOptionalArg()
                 .defaultsTo("");
+        OptionSpec<String> userOption = parser
+                .accepts("user", "User name").withOptionalArg()
+                .defaultsTo("admin");
+        OptionSpec<String> passwordOption = parser
+                .accepts("password", "Password").withOptionalArg()
+                .defaultsTo("admin");
 
         Options oakOptions = new Options();
         OptionSet options = oakOptions.parseAndConfigure(parser, args);
@@ -99,9 +105,11 @@ public class JsonIndexCommand implements
 
         NodeStore nodeStore = nodeStoreFixture.getStore();
         String script = scriptOption.value(options);
+        String user = userOption.value(options);
+        String password = passwordOption.value(options);
         LineNumberReader reader = openScriptReader(script);
         try {
-            process(nodeStore, reader);
+            process(nodeStore, reader, user, password);
         } finally {
             nodeStoreFixture.close();
             reader.close();
@@ -120,9 +128,9 @@ public class JsonIndexCommand implements
         return new LineNumberReader(new BufferedReader(reader));
     }
 
-    public void process(NodeStore nodeStore, LineNumberReader reader)
+    public void process(NodeStore nodeStore, LineNumberReader reader, String 
user, String password)
             throws Exception {
-        session = openSession(nodeStore);
+        session = openSession(nodeStore, user, password);
         System.out.println("Nodestore is open");
         if (interactive) {
             System.out.println("Type \"exit\" to quit");
@@ -176,7 +184,7 @@ public class JsonIndexCommand implements
             }
         }
     }
-    
+
     void execute(String command) throws RepositoryException {
         JsopTokenizer t = new JsopTokenizer(command);
         t.read('{');
@@ -471,7 +479,7 @@ public class JsonIndexCommand implements
         builder.endObject();
     }
 
-    public static Session openSession(NodeStore nodeStore) throws 
RepositoryException {
+    public static Session openSession(NodeStore nodeStore, String user, String 
password) throws RepositoryException {
         if (nodeStore == null) {
             return null;
         }
@@ -484,7 +492,7 @@ public class JsonIndexCommand implements
                 .with(createLuceneIndexEditorProvider());
         Jcr jcr = new Jcr(oak);
         Repository repository = jcr.createRepository();
-        return repository.login(new SimpleCredentials("admin", 
"admin".toCharArray()));
+        return repository.login(new SimpleCredentials(user, 
password.toCharArray()));
     }
 
     private static LuceneIndexEditorProvider createLuceneIndexEditorProvider() 
{

Modified: 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/JsonIndexTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/JsonIndexTest.java?rev=1827816&r1=1827815&r2=1827816&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/JsonIndexTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/JsonIndexTest.java
 Tue Mar 27 08:57:57 2018
@@ -38,39 +38,39 @@ public class JsonIndexTest {
     @Test
     public void simple() throws Exception {
         assertCommand(
-                combineLines("hello"), 
+                combineLines("hello"),
                 "{'print':'hello'}");
         assertCommand(
-                combineLines("false"), 
+                combineLines("false"),
                 "{'print':false}");
         assertCommand(
-                combineLines("1", "2", "3"), 
+                combineLines("1", "2", "3"),
                 "{'$x':[1, 2, 3]}",
                 "{'for':'$x', 'do': [{'print': '$x'}]}");
         assertCommand(
-                combineLines("x1", "x2", "x3"), 
-                "{'$myFunction':[{'$y': 'x', '+': '$x'}, {'print':'$y'}]}",    
            
+                combineLines("x1", "x2", "x3"),
+                "{'$myFunction':[{'$y': 'x', '+': '$x'}, {'print':'$y'}]}",
                 "{'$x':[1, 2, 3]}",
                 "{'for':'$x', 'do': '$myFunction'}");
         assertCommand(
-                combineLines("2", "4", "8"), 
+                combineLines("2", "4", "8"),
                 "{'$x':1}",
                 "{'loop':[{'$x': '$x', '+':'$x'}, {'print': '$x'}, {'$break': 
true, 'if': '$x', '=': 8}]}");
         assertCommand(
-                combineLines("b", "d"), 
+                combineLines("b", "d"),
                 "{'$x':1}",
                 "{'print':'a', 'if':'$x', '=':null}",
                 "{'print':'b', 'if':'$x', '=':1}",
                 "{'print':'c', 'if':null, '=':1}",
                 "{'print':'d', 'if':null, '=':null}");
         assertCommand(
-                combineLines("10", "10"), 
+                combineLines("10", "10"),
                 "{'$x':1}",
                 "{'$$x':10}",
                 "{'print':'$1'}",
                 "{'print':'$$x'}");
         assertCommand(
-                combineLines("1", "null", "1", "2", "a1"), 
+                combineLines("1", "null", "1", "2", "a1"),
                 "{'$x':1, '+':null}",
                 "{'print':'$x'}",
                 "{'$x':null, '+':null}",
@@ -82,7 +82,7 @@ public class JsonIndexTest {
                 "{'$x':'a', '+':'1'}",
                 "{'print':'$x'}");
     }
-    
+
     private static NodeStoreFixture memoryFixture() {
         return new NodeStoreFixture() {
             private final Whiteboard whiteboard = new DefaultWhiteboard();
@@ -112,30 +112,30 @@ public class JsonIndexTest {
             }
         };
     }
-    
+
     @Test
     public void readWrite() throws Exception {
         JsonIndexCommand index = new JsonIndexCommand();
         try (NodeStoreFixture fixture = memoryFixture()) {
             NodeStore store = fixture.getStore();
-            index.session = JsonIndexCommand.openSession(store);
-            assertCommand(index, 
+            index.session = JsonIndexCommand.openSession(store, "admin", 
"admin");
+            assertCommand(index,
                     combineLines(""),
-                    "{'addNode':'/foo', 'node':{'jcr:primaryType': 
'nt:unstructured', 'x': 1, 'y':{}}}", 
+                    "{'addNode':'/foo', 'node':{'jcr:primaryType': 
'nt:unstructured', 'x': 1, 'y':{}}}",
                     "{'session': 'save'}");
-            assertCommand(index, 
+            assertCommand(index,
                     combineLines("/foo", "/jcr:system", "/oak:index", 
"/rep:security"),
                     "{'xpath':'/jcr:root/* order by @jcr:path'}");
-            assertCommand(index, 
+            assertCommand(index,
                     combineLines("/oak:index/counter"),
-                    "{'xpath':'/jcr:root//element(*, 
oak:QueryIndexDefinition)[@type=`counter`] " + 
+                    "{'xpath':'/jcr:root//element(*, 
oak:QueryIndexDefinition)[@type=`counter`] " +
                         "order by @jcr:path'}");
-            assertCommand(index, 
-                    combineLines("[nt:unstructured] as [a] /* property test = 
1 " + 
+            assertCommand(index,
+                    combineLines("[nt:unstructured] as [a] /* property test = 
1 " +
                             "where ([a].[x] = 1) and (isdescendantnode([a], 
[/])) */"),
-                    "{'addNode':'/oak:index/test', 'node':{ " + 
-                        "'jcr:primaryType':'oak:QueryIndexDefinition', " + 
-                        "'type':'property', " + 
+                    "{'addNode':'/oak:index/test', 'node':{ " +
+                        "'jcr:primaryType':'oak:QueryIndexDefinition', " +
+                        "'type':'property', " +
                         "'reindex':true, " +
                         "'entryCount': 1, " +
                         "'{Name}declaringNodeTypes': ['nt:unstructured'], " +
@@ -145,15 +145,15 @@ public class JsonIndexTest {
                     "{'xpath':'explain /jcr:root//element(*, 
nt:unstructured)[@x=1]'}",
                     "{'xpath':'/jcr:root//element(*, nt:unstructured)[@x=2]'}"
                     );
-            assertCommand(index, 
+            assertCommand(index,
                     combineLines("50"),
                     "{'addNode':'/foo/test', 'node':{'jcr:primaryType': 
'oak:Unstructured', 'child':{}}}",
                     "{'$x':1}",
-                    "{'loop':[" + 
-                            "{'$p': '/foo/test/child/n', '+': '$x'}, " + 
-                            "{'addNode': '$p', 'node': {'x': '$x', 
'jcr:primaryType': 'nt:unstructured'}}, " + 
+                    "{'loop':[" +
+                            "{'$p': '/foo/test/child/n', '+': '$x'}, " +
+                            "{'addNode': '$p', 'node': {'x': '$x', 
'jcr:primaryType': 'nt:unstructured'}}, " +
                             "{'session':'save'}, " +
-                            "{'$x': '$x', '+':1}, " + 
+                            "{'$x': '$x', '+':1}, " +
                             "{'$break': true, 'if': '$x', '=': 100}]}",
                     "{'session':'save'}",
                     "{'xpath':'/jcr:root//element(*, nt:unstructured)[@x<50]', 
'quiet':true}",
@@ -161,24 +161,24 @@ public class JsonIndexTest {
                     "{'for':'$result', 'do': [{'$y': '$y', '+': 1}]}",
                     "{'print': '$y'}"
                     );
-            assertCommand(index, 
-                    combineLines("[nt:unstructured] as [a] /* nodeType 
Filter(query=" + 
-                            "explain select [jcr:path], [jcr:score], * from 
[nt:unstructured] as a " + 
-                            "where [x] = 1 and isdescendantnode(a, '/') /* 
xpath: " + 
-                            "/jcr:root//element(*, nt:unstructured)[@x=1] */, 
path=//*, " + 
+            assertCommand(index,
+                    combineLines("[nt:unstructured] as [a] /* nodeType 
Filter(query=" +
+                            "explain select [jcr:path], [jcr:score], * from 
[nt:unstructured] as a " +
+                            "where [x] = 1 and isdescendantnode(a, '/') /* 
xpath: " +
+                            "/jcr:root//element(*, nt:unstructured)[@x=1] */, 
path=//*, " +
                             "property=[x=[1]]) where ([a].[x] = 1) and 
(isdescendantnode([a], [/])) */"),
                     "{'setProperty': '/oak:index/test/type', 'value': 
'disabled'}",
                     "{'session':'save'}",
                     "{'xpath':'explain /jcr:root//element(*, 
nt:unstructured)[@x=1]'}"
                     );
-            assertCommand(index, 
-                    combineLines("[nt:unstructured] as [a] /* traverse '*' " + 
+            assertCommand(index,
+                    combineLines("[nt:unstructured] as [a] /* traverse '*' " +
                             "where [a].[x] = 1 */"),
                     "{'removeNode': '/oak:index/nodetype'}",
                     "{'session':'save'}",
                     "{'sql':'explain select * from [nt:unstructured] as [a] 
where [x]=1'}"
                     );
-            assertCommand(index, 
+            assertCommand(index,
                     combineLines("['/foo': {\n" +
                             "  'jcr:primaryType': 'nt:unstructured', 
'{Long}x': '1', 'y': {}, 'test': {}\n" +
                             "}]"),
@@ -187,7 +187,7 @@ public class JsonIndexTest {
             index.session.logout();
         }
     }
-    
+
     void assertCommand(String expected, String... commands) throws Exception {
         assertCommand(new JsonIndexCommand(), expected, commands);
     }
@@ -203,7 +203,7 @@ public class JsonIndexTest {
         got = got.trim().replace('"', '\'');
         assertEquals(expected, got);
     }
-    
+
     static String combineLines(String... lines) {
         StringWriter w = new StringWriter();
         PrintWriter p = new PrintWriter(w);
@@ -212,5 +212,5 @@ public class JsonIndexTest {
         }
         return w.toString().trim();
     }
-    
+
 }


Reply via email to