Search in sources :

Example 81 with RowIterator

use of javax.jcr.query.RowIterator in project jackrabbit-oak by apache.

the class LuceneIndexDescendantSuggestionTest method ambiguousSubtreeIndexWithDescendantConstraint.

@Ignore("OAK-3992")
@Test
public void ambiguousSubtreeIndexWithDescendantConstraint() throws Exception {
    String query = createSuggestQuery(NT_OAK_UNSTRUCTURED, "te", "/content2");
    String explainQuery = "EXPLAIN " + createSuggestQuery(NT_OAK_UNSTRUCTURED, "te", "/content2");
    QueryManager queryManager = session.getWorkspace().getQueryManager();
    QueryResult result = queryManager.createQuery(explainQuery, Query.JCR_SQL2).execute();
    RowIterator rows = result.getRows();
    String explanation = rows.nextRow().toString();
    assertTrue("Subtree index should get picked", explanation.contains("lucene:sugg-idx(/content2/oak:index/sugg-idx)"));
    validateSuggestions(query, newHashSet("test4"));
}
Also used : QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 82 with RowIterator

use of javax.jcr.query.RowIterator in project jackrabbit-oak by apache.

the class LuceneIndexDescendantSuggestionTest method getSuggestions.

private Set<String> getSuggestions(String query) throws Exception {
    QueryManager queryManager = session.getWorkspace().getQueryManager();
    QueryResult result = queryManager.createQuery(query, Query.JCR_SQL2).execute();
    RowIterator rows = result.getRows();
    Set<String> suggestions = newHashSet();
    while (rows.hasNext()) {
        suggestions.add(rows.nextRow().getValue("suggestion").getString());
    }
    return suggestions;
}
Also used : QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager)

Example 83 with RowIterator

use of javax.jcr.query.RowIterator 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 84 with RowIterator

use of javax.jcr.query.RowIterator in project jackrabbit-oak by apache.

the class LuceneIndexSuggestionTest method avoidInfiniteSuggestions.

@Test
public void avoidInfiniteSuggestions() throws Exception {
    final String nodeType = "nt:unstructured";
    final String indexPropName = "description";
    final String higherRankPropValue = "DD DD DD DD";
    final String exceptionThrowingPropValue = "DD~@#$%^&*()_+{}\":?><`1234567890-=[]";
    final String suggestQueryText = "dd";
    createSuggestIndex("lucene-suggest", nodeType, indexPropName);
    root.addNode("higherRankNode", nodeType).setProperty(indexPropName, higherRankPropValue);
    root.addNode("exceptionThrowingNode", nodeType).setProperty(indexPropName, exceptionThrowingPropValue);
    session.save();
    String suggQuery = createSuggestQuery(nodeType, suggestQueryText);
    QueryManager queryManager = session.getWorkspace().getQueryManager();
    QueryResult result = queryManager.createQuery(suggQuery, Query.JCR_SQL2).execute();
    RowIterator rows = result.getRows();
    int count = 0;
    while (count < 3 && rows.hasNext()) {
        count++;
        rows.nextRow();
    }
    assertTrue("There must not be more than 2 suggestions", count <= 2);
}
Also used : QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Test(org.junit.Test)

Example 85 with RowIterator

use of javax.jcr.query.RowIterator in project sling by apache.

the class JcrResourceBundleTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    cleanRepository();
    RepositoryUtil.registerNodeType(getSession(), getClass().getResourceAsStream("/SLING-INF/nodetypes/jcrlanguage.cnd"));
    RepositoryUtil.registerNodeType(getSession(), getClass().getResourceAsStream("/SLING-INF/nodetypes/message.cnd"));
    resolver = new ResourceResolver() {

        @Override
        public Iterator<Resource> findResources(String query, String language) {
            try {
                final Query q = getSession().getWorkspace().getQueryManager().createQuery(query, language);
                final QueryResult result = q.execute();
                final NodeIterator nodes = result.getNodes();
                return new Iterator<Resource>() {

                    @Override
                    public boolean hasNext() {
                        return nodes.hasNext();
                    }

                    @Override
                    public Resource next() {
                        final Node node = nodes.nextNode();
                        return new TestResource(resolver, node);
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException("remove");
                    }
                };
            } catch (NamingException ne) {
                return null;
            } catch (RepositoryException re) {
                return null;
            }
        }

        @Override
        public Resource getResource(Resource base, String path) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Resource getResource(String path) {
            try {
                final Node n = getSession().getNode(path);
                return new TestResource(resolver, n);
            } catch (NamingException ne) {
            //ignore
            } catch (RepositoryException re) {
            //ignore
            }
            return null;
        }

        @Override
        public String[] getSearchPath() {
            return new String[] { "/apps/", "/libs/" };
        }

        @Override
        public Iterator<Resource> listChildren(final Resource parent) {
            try {
                final Node n = getSession().getNode(parent.getPath());
                final NodeIterator nodes = n.getNodes();
                return new Iterator<Resource>() {

                    @Override
                    public boolean hasNext() {
                        return nodes.hasNext();
                    }

                    @Override
                    public Resource next() {
                        final Node node = nodes.nextNode();
                        return new TestResource(resolver, node);
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException("remove");
                    }
                };
            } catch (final RepositoryException re) {
                // ignore
                return null;
            } catch (final NamingException e) {
                // ignore
                return null;
            }
        }

        @Override
        public String map(HttpServletRequest request, String resourcePath) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String map(String resourcePath) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Iterator<Map<String, Object>> queryResources(String query, String language) {
            try {
                final Query q = getSession().getWorkspace().getQueryManager().createQuery(query, language);
                final QueryResult result = q.execute();
                final String[] colNames = result.getColumnNames();
                final RowIterator rows = result.getRows();
                return new Iterator<Map<String, Object>>() {

                    @Override
                    public boolean hasNext() {
                        return rows.hasNext();
                    }

                    @Override
                    public Map<String, Object> next() {
                        Map<String, Object> row = new HashMap<String, Object>();
                        try {
                            Value[] values = rows.nextRow().getValues();
                            for (int i = 0; i < values.length; i++) {
                                Value v = values[i];
                                if (v != null) {
                                    row.put(colNames[i], values[i].getString());
                                }
                            }
                        } catch (RepositoryException re) {
                        // ignore
                        }
                        return row;
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException("remove");
                    }
                };
            } catch (NamingException ne) {
                return null;
            } catch (RepositoryException re) {
                return null;
            }
        }

        @Override
        public Resource resolve(HttpServletRequest request, String absPath) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Resource resolve(HttpServletRequest request) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Resource resolve(String absPath) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void close() {
        // nothing to do
        }

        @Override
        public String getUserID() {
            return null;
        }

        @Override
        public boolean isLive() {
            return true;
        }

        @Override
        public ResourceResolver clone(Map<String, Object> authenticationInfo) {
            return null;
        }

        @Override
        public Iterator<String> getAttributeNames() {
            return null;
        }

        @Override
        public Object getAttribute(String name) {
            return null;
        }

        @Override
        public Iterable<Resource> getChildren(Resource parent) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void delete(Resource resource) throws PersistenceException {
        // TODO Auto-generated method stub
        }

        @Override
        public Resource create(Resource parent, String name, Map<String, Object> properties) throws PersistenceException {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void revert() {
        // TODO Auto-generated method stub
        }

        @Override
        public void commit() throws PersistenceException {
        // TODO Auto-generated method stub
        }

        @Override
        public boolean hasChanges() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public String getParentResourceType(Resource resource) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getParentResourceType(String resourceType) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public boolean isResourceType(final Resource resource, final String resourceType) {
            return resourceType.equals(resource.getResourceType());
        }

        @Override
        public void refresh() {
        // TODO Auto-generated method stub
        }

        @Override
        public Resource getParent(Resource child) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public boolean hasChildren(Resource resource) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public Resource copy(String srcAbsPath, String destAbsPath) throws PersistenceException {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Resource move(String srcAbsPath, String destAbsPath) throws PersistenceException {
            // TODO Auto-generated method stub
            return null;
        }
    };
    createTestContent();
}
Also used : Query(javax.jcr.query.Query) Node(javax.jcr.Node) HttpServletRequest(javax.servlet.http.HttpServletRequest) QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator) Iterator(java.util.Iterator) NodeIterator(javax.jcr.NodeIterator) NamingException(javax.naming.NamingException) NodeIterator(javax.jcr.NodeIterator) Resource(org.apache.sling.api.resource.Resource) AbstractResource(org.apache.sling.api.resource.AbstractResource) RepositoryException(javax.jcr.RepositoryException) RowIterator(javax.jcr.query.RowIterator) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Value(javax.jcr.Value) PersistenceException(org.apache.sling.api.resource.PersistenceException) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

RowIterator (javax.jcr.query.RowIterator)86 Node (javax.jcr.Node)48 QueryResult (javax.jcr.query.QueryResult)45 QueryManager (javax.jcr.query.QueryManager)27 Row (javax.jcr.query.Row)27 Query (javax.jcr.query.Query)25 Test (org.junit.Test)20 Session (javax.jcr.Session)17 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)15 RepositoryException (javax.jcr.RepositoryException)12 Value (javax.jcr.Value)11 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)8 ValueFactory (javax.jcr.ValueFactory)7 ArrayList (java.util.ArrayList)5 NodeIterator (javax.jcr.NodeIterator)5 NoSuchElementException (java.util.NoSuchElementException)4 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)4 RowIteratorAdapter (org.apache.jackrabbit.commons.iterator.RowIteratorAdapter)4 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3