Search in sources :

Example 1 with QueryCondition

use of org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition in project jackrabbit-oak by apache.

the class RDBVersionGCSupport method getPossiblyDeletedDocs.

@Override
public Iterable<NodeDocument> getPossiblyDeletedDocs(final long fromModified, final long toModified) {
    List<QueryCondition> conditions = new ArrayList<QueryCondition>();
    conditions.add(new QueryCondition(NodeDocument.DELETED_ONCE, "=", 1));
    conditions.add(new QueryCondition(NodeDocument.MODIFIED_IN_SECS, "<", NodeDocument.getModifiedInSecs(toModified)));
    conditions.add(new QueryCondition(NodeDocument.MODIFIED_IN_SECS, ">=", NodeDocument.getModifiedInSecs(fromModified)));
    return store.queryAsIterable(Collection.NODES, null, null, RDBDocumentStore.EMPTY_KEY_PATTERN, conditions, Integer.MAX_VALUE, null);
}
Also used : ArrayList(java.util.ArrayList) QueryCondition(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition)

Example 2 with QueryCondition

use of org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition in project jackrabbit-oak by apache.

the class RDBDocumentStoreJDBC method prepareQuery.

@Nonnull
private PreparedStatement prepareQuery(Connection connection, RDBTableMetaData tmd, String columns, String minId, String maxId, List<String> excludeKeyPatterns, List<QueryCondition> conditions, int limit, String sortBy) throws SQLException {
    StringBuilder selectClause = new StringBuilder();
    if (limit != Integer.MAX_VALUE && this.dbInfo.getFetchFirstSyntax() == FETCHFIRSTSYNTAX.TOP) {
        selectClause.append("TOP " + limit + " ");
    }
    selectClause.append(columns + " from " + tmd.getName());
    String whereClause = buildWhereClause(minId, maxId, excludeKeyPatterns, conditions);
    StringBuilder query = new StringBuilder();
    query.append("select ").append(selectClause);
    if (whereClause.length() != 0) {
        query.append(" where ").append(whereClause);
    }
    if (sortBy != null) {
        query.append(" order by ID");
    }
    if (limit != Integer.MAX_VALUE) {
        switch(this.dbInfo.getFetchFirstSyntax()) {
            case LIMIT:
                query.append(" LIMIT " + limit);
                break;
            case FETCHFIRST:
                query.append(" FETCH FIRST " + limit + " ROWS ONLY");
                break;
            default:
                break;
        }
    }
    PreparedStatement stmt = connection.prepareStatement(query.toString());
    int si = 1;
    if (minId != null) {
        setIdInStatement(tmd, stmt, si++, minId);
    }
    if (maxId != null) {
        setIdInStatement(tmd, stmt, si++, maxId);
    }
    for (String keyPattern : excludeKeyPatterns) {
        setIdInStatement(tmd, stmt, si++, keyPattern);
    }
    for (QueryCondition cond : conditions) {
        stmt.setLong(si++, cond.getValue());
    }
    if (limit != Integer.MAX_VALUE) {
        stmt.setFetchSize(limit);
    }
    return stmt;
}
Also used : PreparedStatement(java.sql.PreparedStatement) QueryCondition(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition) Nonnull(javax.annotation.Nonnull)

Example 3 with QueryCondition

use of org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition in project jackrabbit-oak by apache.

the class RDBDocumentStoreJDBC method deleteWithCondition.

public int deleteWithCondition(Connection connection, RDBTableMetaData tmd, List<QueryCondition> conditions) throws SQLException, DocumentStoreException {
    StringBuilder query = new StringBuilder("delete from " + tmd.getName());
    String whereClause = buildWhereClause(null, null, null, conditions);
    if (whereClause.length() != 0) {
        query.append(" where ").append(whereClause);
    }
    PreparedStatement stmt = connection.prepareStatement(query.toString());
    try {
        int si = 1;
        for (QueryCondition cond : conditions) {
            stmt.setLong(si++, cond.getValue());
        }
        return stmt.executeUpdate();
    } finally {
        stmt.close();
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) QueryCondition(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition)

Example 4 with QueryCondition

use of org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition in project jackrabbit-oak by apache.

the class RDBDocumentStoreTest method testRDBQueryConditions.

@Test
public void testRDBQueryConditions() {
    if (ds instanceof RDBDocumentStore) {
        RDBDocumentStore rds = (RDBDocumentStore) ds;
        // create ten documents
        long now = System.currentTimeMillis();
        String base = this.getClass().getName() + ".testRDBQuery-";
        for (int i = 0; i < 10; i++) {
            String id = base + i;
            UpdateOp up = new UpdateOp(id, true);
            up.set(NodeDocument.DELETED_ONCE, i % 2 == 1);
            up.set(NodeDocument.MODIFIED_IN_SECS, now++);
            boolean success = super.ds.create(Collection.NODES, Collections.singletonList(up));
            assertTrue("document with " + id + " not created", success);
            removeMe.add(id);
        }
        List<QueryCondition> conditions = new ArrayList<QueryCondition>();
        // matches every second
        conditions.add(new QueryCondition(NodeDocument.DELETED_ONCE, "=", 0));
        // matches first eight
        conditions.add(new QueryCondition(NodeDocument.MODIFIED_IN_SECS, "<", now - 2));
        List<NodeDocument> result = rds.query(Collection.NODES, base, base + "A", RDBDocumentStore.EMPTY_KEY_PATTERN, conditions, 10);
        assertEquals(4, result.size());
    }
}
Also used : UpdateOp(org.apache.jackrabbit.oak.plugins.document.UpdateOp) ArrayList(java.util.ArrayList) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) QueryCondition(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition) Test(org.junit.Test) AbstractDocumentStoreTest(org.apache.jackrabbit.oak.plugins.document.AbstractDocumentStoreTest)

Example 5 with QueryCondition

use of org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition in project jackrabbit-oak by apache.

the class RDBDocumentStoreTest method testRDBQueryKeyPatterns.

@Test
public void testRDBQueryKeyPatterns() {
    if (ds instanceof RDBDocumentStore) {
        int cnt = 10;
        RDBDocumentStore rds = (RDBDocumentStore) ds;
        // create ten documents
        String base = this.getClass().getName() + ".testRDBQuery-";
        for (int i = 0; i < cnt; i++) {
            // every second is a "regular" path
            String id = "1:" + (i % 2 == 1 ? "p" : "") + "/" + base + i;
            UpdateOp up = new UpdateOp(id, true);
            up.set("_test", base);
            boolean success = super.ds.create(Collection.NODES, Collections.singletonList(up));
            assertTrue("document with " + id + " not created", success);
            removeMe.add(id);
        }
        List<QueryCondition> conditions = new ArrayList<QueryCondition>();
        List<NodeDocument> result = rds.query(Collection.NODES, NodeDocument.MIN_ID_VALUE, NodeDocument.MAX_ID_VALUE, Arrays.asList("_:/%", "__:/%", "___:/%"), conditions, 10000);
        for (NodeDocument d : result) {
            if (base.equals(d.get("_test"))) {
                assertTrue(d.getId().startsWith("1:p"));
            }
        }
        Iterable<NodeDocument> it = rds.queryAsIterable(Collection.NODES, NodeDocument.MIN_ID_VALUE, NodeDocument.MAX_ID_VALUE, Arrays.asList("_:/%", "__:/%", "___:/%"), conditions, Integer.MAX_VALUE, null);
        assertTrue(it instanceof Closeable);
        int c1 = 0, c2 = 0;
        for (NodeDocument d : it) {
            if (base.equals(d.get("_test"))) {
                assertTrue(d.getId().startsWith("1:p"));
                c1 += 1;
            }
        }
        // check that getting the iterator twice works
        for (NodeDocument d : it) {
            if (base.equals(d.get("_test"))) {
                assertTrue(d.getId().startsWith("1:p"));
                c2 += 1;
            }
        }
        assertEquals(cnt / 2, c1);
        assertEquals(cnt / 2, c2);
        Utils.closeIfCloseable(it);
    }
}
Also used : UpdateOp(org.apache.jackrabbit.oak.plugins.document.UpdateOp) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) QueryCondition(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition) Test(org.junit.Test) AbstractDocumentStoreTest(org.apache.jackrabbit.oak.plugins.document.AbstractDocumentStoreTest)

Aggregations

QueryCondition (org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition)12 AbstractDocumentStoreTest (org.apache.jackrabbit.oak.plugins.document.AbstractDocumentStoreTest)7 Test (org.junit.Test)7 RDBTableMetaData (org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.RDBTableMetaData)5 Closeable (java.io.Closeable)4 ArrayList (java.util.ArrayList)4 LogCustomizer (org.apache.jackrabbit.oak.commons.junit.LogCustomizer)3 UpdateOp (org.apache.jackrabbit.oak.plugins.document.UpdateOp)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)2 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)2 Method (java.lang.reflect.Method)1 Nonnull (javax.annotation.Nonnull)1