use of javax.jcr.query.Row in project jackrabbit-oak by apache.
the class QueryTest method skip.
@Test
public void skip() throws RepositoryException {
Session session = getAdminSession();
Node hello1 = session.getRootNode().addNode("hello1");
hello1.setProperty("id", "1");
hello1.setProperty("data", "x");
session.save();
Node hello3 = hello1.addNode("hello3");
hello3.setProperty("id", "3");
hello3.setProperty("data", "z");
session.save();
Node hello2 = hello3.addNode("hello2");
hello2.setProperty("id", "2");
hello2.setProperty("data", "y");
session.save();
ValueFactory vf = session.getValueFactory();
QueryManager qm = session.getWorkspace().getQueryManager();
Query q = qm.createQuery("select id from [nt:base] where data >= $data order by id", Query.JCR_SQL2);
q.bindValue("data", vf.createValue("x"));
for (int i = -1; i < 5; i++) {
QueryResult r = q.execute();
RowIterator it = r.getRows();
assertEquals(3, r.getRows().getSize());
assertEquals(3, r.getNodes().getSize());
Row row;
try {
it.skip(i);
assertTrue(i >= 0 && i <= 3);
} catch (IllegalArgumentException e) {
assertEquals(-1, i);
} catch (NoSuchElementException e) {
assertTrue(i >= 2);
}
if (i <= 0) {
assertTrue(it.hasNext());
row = it.nextRow();
assertEquals("1", row.getValue("id").getString());
}
if (i <= 1) {
assertTrue(it.hasNext());
row = it.nextRow();
assertEquals("2", row.getValue("id").getString());
}
if (i <= 2) {
assertTrue(it.hasNext());
row = it.nextRow();
assertEquals("3", row.getValue("id").getString());
}
assertFalse(it.hasNext());
}
}
use of javax.jcr.query.Row in project jackrabbit-oak by apache.
the class QueryFulltextTest method excerpt.
@Test
public void excerpt() throws Exception {
Session session = getAdminSession();
QueryManager qm = session.getWorkspace().getQueryManager();
Node testRootNode = session.getRootNode().addNode("testroot");
Node n1 = testRootNode.addNode("node1");
n1.setProperty("text", "hello world");
n1.setProperty("desc", "description");
Node n2 = testRootNode.addNode("node2");
n2.setProperty("text", "Hello World");
n2.setProperty("desc", "Description");
session.save();
Query q;
RowIterator it;
Row row;
String s;
String xpath = "//*[jcr:contains(., 'hello')]/rep:excerpt(.) order by @jcr:path";
q = qm.createQuery(xpath, "xpath");
it = q.execute().getRows();
row = it.nextRow();
String path = row.getPath();
s = row.getValue("rep:excerpt(.)").getString();
assertTrue(path + ":" + s + " (1)", s.indexOf("<strong>hello</strong> world") >= 0);
assertTrue(path + ":" + s + " (2)", s.indexOf("description") >= 0);
row = it.nextRow();
path = row.getPath();
s = row.getValue("rep:excerpt(.)").getString();
// TODO is this expected?
assertTrue(path + ":" + s + " (3)", s.indexOf("Hello World") >= 0);
assertTrue(path + ":" + s + " (4)", s.indexOf("Description") >= 0);
xpath = "//*[jcr:contains(., 'hello')]/rep:excerpt(.) order by @jcr:path";
q = qm.createQuery(xpath, "xpath");
it = q.execute().getRows();
row = it.nextRow();
path = row.getPath();
s = row.getValue("rep:excerpt(text)").getString();
assertTrue(path + ":" + s + " (5)", s.indexOf("<strong>hello</strong> world") >= 0);
assertTrue(path + ":" + s + " (6)", s.indexOf("description") < 0);
row = it.nextRow();
path = row.getPath();
s = row.getValue("rep:excerpt(text)").getString();
// TODO is this expected?
assertTrue(path + ":" + s + " (7)", s.indexOf("Hello World") >= 0);
assertTrue(path + ":" + s + " (8)", s.indexOf("Description") < 0);
}
use of javax.jcr.query.Row in project sling by apache.
the class MockQueryManagerTest method testQueryResults_ResultHandler_Rows.
@SuppressWarnings("unchecked")
@Test
public void testQueryResults_ResultHandler_Rows() throws RepositoryException {
final List<String> columnNames = ImmutableList.of("stringProp", "intProp", "optionalStringProp");
MockJcr.addQueryResultHandler(queryManager, new MockQueryResultHandler() {
@Override
public MockQueryResult executeQuery(MockQuery query) {
return new MockQueryResult(sampleNodes, columnNames);
}
});
Query query = queryManager.createQuery("query1", Query.JCR_SQL2);
QueryResult result = query.execute();
assertEquals(sampleNodes, ImmutableList.copyOf(result.getNodes()));
assertEquals(columnNames, ImmutableList.copyOf(result.getColumnNames()));
List<Row> rows = ImmutableList.copyOf(result.getRows());
assertEquals("value1", rows.get(0).getValue("stringProp").getString());
assertEquals(1L, rows.get(0).getValue("intProp").getLong());
assertEquals("optValue1", rows.get(0).getValue("optionalStringProp").getString());
assertEquals("value2", rows.get(1).getValues()[0].getString());
assertEquals(2L, rows.get(1).getValues()[1].getLong());
assertNull(rows.get(1).getValues()[2]);
assertEquals("value3", rows.get(2).getValues()[0].getString());
assertEquals(3L, rows.get(2).getValues()[1].getLong());
assertNull(rows.get(2).getValues()[2]);
}
use of javax.jcr.query.Row in project vorto by eclipse.
the class JcrModelRepository method search.
@Override
public List<ModelInfo> search(final String expression) {
String queryExpression = expression;
if (queryExpression == null || queryExpression.isEmpty()) {
queryExpression = "*";
}
try {
List<ModelInfo> modelResources = new ArrayList<>();
Query query = modelSearchUtil.createQueryFromExpression(session, queryExpression);
logger.debug("Searching repository with expression " + query.getStatement());
QueryResult result = query.execute();
RowIterator rowIterator = result.getRows();
while (rowIterator.hasNext()) {
Row row = rowIterator.nextRow();
Node currentNode = row.getNode();
if (currentNode.hasProperty("vorto:type")) {
try {
modelResources.add(createMinimalModelInfo(currentNode));
} catch (Exception ex) {
// model is corrupt ,ignoring....
}
}
}
return modelResources;
} catch (RepositoryException e) {
throw new RuntimeException("Could not create query manager", e);
}
}
use of javax.jcr.query.Row in project archiva by apache.
the class JcrMetadataRepository method populateStatistics.
@Override
public void populateStatistics(MetadataRepository repository, String repositoryId, RepositoryStatistics repositoryStatistics) throws MetadataRepositoryException {
if (!(repository instanceof JcrMetadataRepository)) {
throw new MetadataRepositoryException("The statistics population is only possible for JcrMetdataRepository implementations");
}
Session session = (Session) repository.obtainAccess(Session.class);
try {
QueryManager queryManager = session.getWorkspace().getQueryManager();
// TODO: Check, if this is still the case - Switched to Jackrabbit OAK with archiva 3.0
// Former statement: JCR-SQL2 query will not complete on a large repo in Jackrabbit 2.2.0 - see JCR-2835
// Using the JCR-SQL2 variants gives
// "org.apache.lucene.search.BooleanQuery$TooManyClauses: maxClauseCount is set to 1024"
// String whereClause = "WHERE ISDESCENDANTNODE([/repositories/" + repositoryId + "/content])";
// Query query = queryManager.createQuery( "SELECT size FROM [archiva:artifact] " + whereClause,
// Query.JCR_SQL2 );
String whereClause = "WHERE ISDESCENDANTNODE([/repositories/" + repositoryId + "/content])";
Query query = queryManager.createQuery("SELECT size FROM [archiva:artifact] " + whereClause, Query.JCR_SQL2);
QueryResult queryResult = query.execute();
Map<String, Integer> totalByType = new HashMap<>();
long totalSize = 0, totalArtifacts = 0;
for (Row row : JcrUtils.getRows(queryResult)) {
Node n = row.getNode();
totalSize += row.getValue("size").getLong();
String type;
if (n.hasNode(MavenArtifactFacet.FACET_ID)) {
Node facetNode = n.getNode(MavenArtifactFacet.FACET_ID);
type = facetNode.getProperty("type").getString();
} else {
type = "Other";
}
Integer prev = totalByType.get(type);
totalByType.put(type, prev != null ? prev + 1 : 1);
totalArtifacts++;
}
repositoryStatistics.setTotalArtifactCount(totalArtifacts);
repositoryStatistics.setTotalArtifactFileSize(totalSize);
for (Map.Entry<String, Integer> entry : totalByType.entrySet()) {
log.info("Setting count for type: {} = {}", entry.getKey(), entry.getValue());
repositoryStatistics.setTotalCountForType(entry.getKey(), entry.getValue());
}
// The query ordering is a trick to ensure that the size is correct, otherwise due to lazy init it will be -1
// query = queryManager.createQuery( "SELECT * FROM [archiva:project] " + whereClause, Query.JCR_SQL2 );
query = queryManager.createQuery("SELECT * FROM [archiva:project] " + whereClause + " ORDER BY [jcr:score]", Query.JCR_SQL2);
repositoryStatistics.setTotalProjectCount(query.execute().getRows().getSize());
// query = queryManager.createQuery(
// "SELECT * FROM [archiva:namespace] " + whereClause + " AND namespace IS NOT NULL", Query.JCR_SQL2 );
query = queryManager.createQuery("SELECT * FROM [archiva:namespace] " + whereClause + " AND namespace IS NOT NULL ORDER BY [jcr:score]", Query.JCR_SQL2);
repositoryStatistics.setTotalGroupCount(query.execute().getRows().getSize());
} catch (RepositoryException e) {
throw new MetadataRepositoryException(e.getMessage(), e);
}
}
Aggregations