Author: chetanm
Date: Fri May 12 05:20:40 2017
New Revision: 1794923

URL: http://svn.apache.org/viewvc?rev=1794923&view=rev
Log:
OAK-6080 - Index report service

Printer to dump index definitions

Added:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexDefinitionPrinter.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/NodeStateJsonUtils.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexDefinitionPrinterTest.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/NodeStateJsonUtilsTest.java
   (with props)

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexDefinitionPrinter.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexDefinitionPrinter.java?rev=1794923&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexDefinitionPrinter.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexDefinitionPrinter.java
 Fri May 12 05:20:40 2017
@@ -0,0 +1,76 @@
+/*
+ * 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.inventory;
+
+import java.io.PrintWriter;
+
+import org.apache.felix.inventory.Format;
+import org.apache.felix.inventory.InventoryPrinter;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
+import org.apache.jackrabbit.oak.commons.json.JsopWriter;
+import org.apache.jackrabbit.oak.plugins.index.IndexPathService;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.oak.spi.state.NodeStateUtils;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+
+@Component
+@Service
+@Properties({
+        @Property(name = "felix.inventory.printer.name", value = 
"oak-index-defn"),
+        @Property(name = "felix.inventory.printer.title", value = "Oak Index 
Definitions"),
+        @Property(name = "felix.inventory.printer.format", value = {"JSON"})
+})
+public class IndexDefinitionPrinter implements InventoryPrinter {
+
+    @Reference
+    private IndexPathService indexPathService;
+
+    @Reference
+    private NodeStore nodeStore;
+
+    public IndexDefinitionPrinter() {
+    }
+
+    public IndexDefinitionPrinter(NodeStore nodeStore, IndexPathService 
indexPathService) {
+        this.indexPathService = indexPathService;
+        this.nodeStore = nodeStore;
+    }
+
+    @Override
+    public void print(PrintWriter printWriter, Format format, boolean isZip) {
+        if (format == Format.JSON) {
+            NodeState root = nodeStore.getRoot();
+            JsopWriter json = new JsopBuilder();
+            json.object();
+            for (String indexPath : indexPathService.getIndexPaths()) {
+                json.key(indexPath);
+                NodeState idxState = NodeStateUtils.getNode(root, indexPath);
+                NodeStateJsonUtils.copyAsJson(json, idxState, false);
+            }
+            json.endObject();
+            printWriter.print(JsopBuilder.prettyPrint(json.toString()));
+        }
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexDefinitionPrinter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/NodeStateJsonUtils.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/NodeStateJsonUtils.java?rev=1794923&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/NodeStateJsonUtils.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/NodeStateJsonUtils.java
 Fri May 12 05:20:40 2017
@@ -0,0 +1,104 @@
+/*
+ * 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.inventory;
+
+import javax.jcr.PropertyType;
+
+import org.apache.jackrabbit.oak.api.Blob;
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
+import org.apache.jackrabbit.oak.commons.json.JsopWriter;
+import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.oak.spi.state.NodeStateUtils;
+
+final class NodeStateJsonUtils {
+
+    public static String toJson(NodeState state, boolean includeHiddenContent) 
{
+        JsopWriter json = new JsopBuilder();
+        copyAsJson(json, state, includeHiddenContent);
+        return json.toString();
+    }
+
+    public static void copyAsJson(JsopWriter json, NodeState state, boolean 
includeHiddenContent) {
+        json.object();
+        copyNode(state, json, includeHiddenContent);
+        json.endObject();
+    }
+
+    private static void copyNode(NodeState state, JsopWriter json, boolean 
includeHiddenContent) {
+        copyProperties(state, json, includeHiddenContent);
+        for (ChildNodeEntry cne : state.getChildNodeEntries()) {
+            if (!includeHiddenContent && 
NodeStateUtils.isHidden(cne.getName())) {
+                continue;
+            }
+            json.key(cne.getName());
+            json.object();
+            copyNode(cne.getNodeState(), json, includeHiddenContent);
+            json.endObject();
+        }
+    }
+
+    private static void copyProperties(NodeState state, JsopWriter json, 
boolean includeHiddenContent) {
+        for (PropertyState ps : state.getProperties()) {
+            String name = ps.getName();
+            if (!includeHiddenContent && NodeStateUtils.isHidden(name)) {
+                continue;
+            }
+            if (ps.isArray()) {
+                json.key(name).array();
+                for (int i = 0; i < ps.count(); i++) {
+                    copyProperty(ps, i, json);
+                }
+                json.endArray();
+            } else {
+                json.key(name);
+                copyProperty(ps, 0, json);
+            }
+        }
+    }
+
+    private static void copyProperty(PropertyState ps, int i, JsopWriter json) 
{
+        switch (ps.getType().tag()) {
+            case PropertyType.LONG:
+                long longVal = ps.isArray() ? ps.getValue(Type.LONG, i) : 
ps.getValue(Type.LONG);
+                json.value(longVal);
+                break;
+            case PropertyType.BOOLEAN:
+                boolean boolVal = ps.isArray() ? ps.getValue(Type.BOOLEAN, i) 
: ps.getValue(Type.BOOLEAN);
+                json.value(boolVal);
+                break;
+            case PropertyType.BINARY:
+                Blob b = ps.isArray() ? ps.getValue(Type.BINARY, i) : 
ps.getValue(Type.BINARY);
+                String binVal = toString(b);
+                json.value(binVal);
+                break;
+            default:
+                String strVal = ps.isArray() ? ps.getValue(Type.STRING, i) : 
ps.getValue(Type.STRING);
+                json.value(strVal);
+        }
+    }
+
+    private static String toString(Blob b) {
+        String id = b.getContentIdentity();
+        return id == null ? "<binary>#" + b.length() : b.getContentIdentity();
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/NodeStateJsonUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexDefinitionPrinterTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexDefinitionPrinterTest.java?rev=1794923&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexDefinitionPrinterTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexDefinitionPrinterTest.java
 Fri May 12 05:20:40 2017
@@ -0,0 +1,67 @@
+/*
+ * 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.inventory;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import com.google.common.collect.Lists;
+import org.apache.felix.inventory.Format;
+import org.apache.jackrabbit.oak.plugins.index.IndexPathService;
+import 
org.apache.jackrabbit.oak.plugins.index.inventory.IndexDefinitionPrinter;
+import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore;
+import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
+import org.apache.jackrabbit.oak.spi.commit.EmptyHook;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+import org.json.simple.JSONValue;
+import org.junit.Test;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class IndexDefinitionPrinterTest {
+    private NodeStore store = new MemoryNodeStore();
+    private IndexPathService pathService = mock(IndexPathService.class);
+    private IndexDefinitionPrinter printer = new IndexDefinitionPrinter(store, 
pathService);
+
+    @Test
+    public void printer() throws Exception{
+        NodeBuilder builder = store.getRoot().builder();
+        builder.child("a").setProperty("foo", "bar");
+        builder.child("b").child("c").setProperty("foo", "bar");
+
+        store.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
+
+        when(pathService.getIndexPaths()).thenReturn(Lists.newArrayList("/a", 
"/b"));
+
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        printer.print(pw, Format.JSON, false);
+
+        pw.flush();
+
+        String json = sw.toString();
+
+        //If there is any error in rendered json
+        //exception would fail the test
+        JSONValue.parseWithException(json);
+    }
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexDefinitionPrinterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/NodeStateJsonUtilsTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/NodeStateJsonUtilsTest.java?rev=1794923&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/NodeStateJsonUtilsTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/NodeStateJsonUtilsTest.java
 Fri May 12 05:20:40 2017
@@ -0,0 +1,90 @@
+/*
+ * 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.inventory;
+
+import com.google.common.collect.Lists;
+import org.apache.jackrabbit.oak.api.Type;
+import org.apache.jackrabbit.oak.plugins.index.inventory.NodeStateJsonUtils;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+import org.junit.Test;
+
+import static 
org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class NodeStateJsonUtilsTest {
+
+    @Test
+    public void toJson() throws Exception{
+        NodeBuilder builder = EMPTY_NODE.builder();
+        builder.child("a").setProperty("foo", 10);
+        builder.child("a").setProperty("foo2", "bar");
+        builder.child("a").setProperty("foo3", true);
+        builder.child("a").setProperty("foo4", new 
ArrayBasedBlob("foo".getBytes()));
+        builder.child("a").child("b").setProperty("foo", 
Lists.newArrayList(1L,2L,3L), Type.LONGS);
+        builder.child("a").child("b").setProperty("foo2", 
Lists.newArrayList("x", "y", "z"), Type.STRINGS);
+        builder.child("a").child("b").setProperty("foo3", 
Lists.newArrayList(true, false), Type.BOOLEANS);
+        builder.child("a").child(":c").setProperty("foo", "bar");
+
+        String jsop = NodeStateJsonUtils.toJson(builder.getNodeState(), false);
+        JSONObject json = (JSONObject) JSONValue.parseWithException(jsop);
+
+        assertTrue(json.containsKey("a"));
+        JSONObject a = (JSONObject) json.get("a");
+        JSONObject b = (JSONObject) a.get("b");
+        assertEquals(Lists.newArrayList(1L,2L,3L), b.get("foo"));
+        assertEquals(Lists.newArrayList("x", "y", "z"), b.get("foo2"));
+        assertEquals(Lists.newArrayList(true, false), b.get("foo3"));
+    }
+
+    @Test
+    public void hiddenContent() throws Exception{
+        NodeBuilder builder = EMPTY_NODE.builder();
+        builder.child("a").setProperty("foo", "bar");
+        builder.child("a").setProperty(":foo", "bar");
+        builder.child("a");
+        builder.child(":b");
+        NodeState state = builder.getNodeState();
+
+        String jsop = NodeStateJsonUtils.toJson(state, false);
+        JSONObject json = (JSONObject) JSONValue.parseWithException(jsop);
+        assertFalse(json.containsKey(":b")); //Hidden content should not be 
found
+
+        JSONObject a = (JSONObject) json.get("a");
+        assertTrue(a.containsKey("foo"));
+        assertFalse(a.containsKey(":foo"));
+
+        String jsop2 = NodeStateJsonUtils.toJson(builder.getNodeState(), true);
+        JSONObject json2 = (JSONObject) JSONValue.parseWithException(jsop2);
+        assertTrue(json2.containsKey(":b"));
+
+        JSONObject a2 = (JSONObject) json2.get("a");
+        assertTrue(a2.containsKey("foo"));
+        assertTrue(a2.containsKey(":foo"));
+    }
+
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/NodeStateJsonUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to