Search in sources :

Example 6 with JsopBuilder

use of org.apache.jackrabbit.oak.commons.json.JsopBuilder in project jackrabbit-oak by apache.

the class SimpleTest method nodeAndPropertyNames.

@Test
public void nodeAndPropertyNames() {
    DocumentMK mk = createMK();
    String rev;
    String nodes;
    for (String s : new String[] { "_", "$", "__", "_id", "$x", ".", ".\\", "x\\", "\\x", "first.name" }) {
        String x2 = Utils.escapePropertyName(s);
        String s2 = Utils.unescapePropertyName(x2);
        if (!s.equals(s2)) {
            assertEquals(s, s2);
        }
        JsopBuilder jsop = new JsopBuilder();
        jsop.tag('+').key(s).object().key(s).value("x").endObject();
        rev = mk.commit("/", jsop.toString(), null, null);
        nodes = mk.getNodes("/" + s, rev, 0, 0, 10, null);
        jsop = new JsopBuilder();
        jsop.object().key(s).value("x").key(":childNodeCount").value(0).endObject();
        String n = jsop.toString();
        assertEquals(n, nodes);
        nodes = mk.getNodes("/", rev, 0, 0, 10, null);
        jsop = new JsopBuilder();
        jsop.object().key(s).object().endObject().key(":childNodeCount").value(1).endObject();
        n = jsop.toString();
        assertEquals(n, nodes);
        jsop = new JsopBuilder();
        jsop.tag('-').value(s);
        rev = mk.commit("/", jsop.toString(), rev, null);
    }
}
Also used : JsopBuilder(org.apache.jackrabbit.oak.commons.json.JsopBuilder) Test(org.junit.Test)

Example 7 with JsopBuilder

use of org.apache.jackrabbit.oak.commons.json.JsopBuilder in project jackrabbit-oak by apache.

the class ClusterView method asJson.

/**
     * Converts the provided parameters into the clusterview json that will be
     * provided via JMX
     **/
private String asJson(final long viewSeqNum, final boolean viewFinal, final String clusterId, final int localId, final Set<Integer> activeIds, final Set<Integer> deactivatingIds, final Set<Integer> inactiveIds) {
    JsopBuilder builder = new JsopBuilder();
    builder.object();
    builder.key("seq").value(viewSeqNum);
    builder.key("final").value(viewFinal);
    builder.key("id").value(clusterId);
    builder.key("me").value(localId);
    builder.key("active").array();
    for (Iterator<Integer> it = activeIds.iterator(); it.hasNext(); ) {
        Integer anInstance = it.next();
        builder.value(anInstance);
    }
    builder.endArray();
    builder.key("deactivating").array();
    for (Iterator<Integer> it = deactivatingIds.iterator(); it.hasNext(); ) {
        Integer anInstance = it.next();
        builder.value(anInstance);
    }
    builder.endArray();
    builder.key("inactive").array();
    for (Iterator<Integer> it = inactiveIds.iterator(); it.hasNext(); ) {
        Integer anInstance = it.next();
        builder.value(anInstance);
    }
    builder.endArray();
    builder.endObject();
    return builder.toString();
}
Also used : JsopBuilder(org.apache.jackrabbit.oak.commons.json.JsopBuilder)

Example 8 with JsopBuilder

use of org.apache.jackrabbit.oak.commons.json.JsopBuilder in project jackrabbit-oak by apache.

the class AbstractBlobStoreTest method addFiles.

public static String addFiles(BlobStore store, String dir) throws Exception {
    ArrayList<String> list = new ArrayList<String>();
    String root = new File(dir).getAbsolutePath();
    String parent = new File(dir).getParentFile().getAbsolutePath();
    addFiles(list, new File(root));
    JsopBuilder listing = new JsopBuilder();
    listing.object();
    for (String f : list) {
        FileInputStream in = new FileInputStream(f);
        String id = store.writeBlob(in);
        in.close();
        String name = f.substring(parent.length());
        listing.key(name).value(id);
        listing.newline();
    }
    listing.endObject();
    String l = listing.toString();
    String id = store.writeBlob(new ByteArrayInputStream(l.getBytes("UTF-8")));
    return id;
}
Also used : JsopBuilder(org.apache.jackrabbit.oak.commons.json.JsopBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 9 with JsopBuilder

use of org.apache.jackrabbit.oak.commons.json.JsopBuilder in project jackrabbit-oak by apache.

the class JsonIndexCommand method runQuery.

private void runQuery(String query, String language, String columnName, boolean quiet, int depth) throws RepositoryException {
    ArrayList<String> list = new ArrayList<String>();
    columnName = query.startsWith("explain") ? "plan" : columnName;
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery(query, language);
    for (String b : q.getBindVariableNames()) {
        ValueFactory vf = session.getValueFactory();
        q.bindValue(b, vf.createValue(data.get("$" + b).toString()));
    }
    QueryResult result = q.execute();
    if (depth != 0) {
        NodeIterator ni = result.getNodes();
        JsopBuilder builder = new JsopBuilder().array();
        while (ni.hasNext()) {
            Node n = ni.nextNode();
            builder.key(n.getPath());
            appendNode(builder, n, depth - 1);
        }
        output.println(JsopBuilder.prettyPrint(builder.endArray().toString()));
        return;
    }
    RowIterator ri = result.getRows();
    while (ri.hasNext()) {
        Row r = ri.nextRow();
        if (columnName != null) {
            String x = r.getValue(columnName).getString();
            list.add(x);
            if (!quiet) {
                output.println(x);
            }
        } else {
            String[] columnNames = result.getColumnNames();
            for (String cn : columnNames) {
                Value v = r.getValue(cn);
                String x = v == null ? null : v.getString();
                if (columnNames.length == 1) {
                    list.add(x);
                    if (!quiet) {
                        output.println(x);
                    }
                } else {
                    list.add(x);
                    if (!quiet) {
                        output.println(cn + ": " + x);
                    }
                }
            }
        }
    }
    data.put("$resultSize", (long) list.size());
    data.put("$result", list.toArray(new String[0]));
}
Also used : NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) ValueFactory(javax.jcr.ValueFactory) QueryResult(javax.jcr.query.QueryResult) JsopBuilder(org.apache.jackrabbit.oak.commons.json.JsopBuilder) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Value(javax.jcr.Value) Row(javax.jcr.query.Row)

Example 10 with JsopBuilder

use of org.apache.jackrabbit.oak.commons.json.JsopBuilder in project jackrabbit-oak by apache.

the class LuceneJournalPropertyBuilder method buildAsString.

@Override
public String buildAsString() {
    JsopWriter json = new JsopBuilder();
    json.object();
    for (Map.Entry<String, Collection<String>> e : indexedNodes.asMap().entrySet()) {
        json.key(e.getKey()).array();
        for (String v : e.getValue()) {
            json.value(v);
        }
        json.endArray();
    }
    json.endObject();
    return json.toString();
}
Also used : JsopBuilder(org.apache.jackrabbit.oak.commons.json.JsopBuilder) Collection(java.util.Collection) Map(java.util.Map) JsopWriter(org.apache.jackrabbit.oak.commons.json.JsopWriter)

Aggregations

JsopBuilder (org.apache.jackrabbit.oak.commons.json.JsopBuilder)18 JsopWriter (org.apache.jackrabbit.oak.commons.json.JsopWriter)9 Test (org.junit.Test)5 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 AbstractDocumentNodeState (org.apache.jackrabbit.oak.plugins.document.AbstractDocumentNodeState)2 DocumentNodeState (org.apache.jackrabbit.oak.plugins.document.DocumentNodeState)2 DelegatingDocumentNodeState (org.apache.jackrabbit.oak.plugins.document.secondary.DelegatingDocumentNodeState)2 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 Node (javax.jcr.Node)1 NodeIterator (javax.jcr.NodeIterator)1 Value (javax.jcr.Value)1