RDBDocumentStoreJDBC { public long getLong(Connection connection, RDBTableMetaData tmd, String aggregate, String field, String minId, String maxId, List<String> excludeKeyPatterns, List<QueryCondition> conditions) throws SQLException { PreparedStatement stmt = null; ResultSet rs = null; long start = System.currentTimeMillis(); long result = -1; String selector = aggregate + "(" + ("*".equals(field) ? "*" : INDEXED_PROP_MAPPING.get(field)) + ")"; try { stmt = prepareQuery(connection, tmd, selector, minId, maxId, excludeKeyPatterns, conditions, Integer.MAX_VALUE, null); rs = stmt.executeQuery(); result = rs.next() ? rs.getLong(1) : -1; return result; } finally { closeStatement(stmt); closeResultSet(rs); if (LOG.isDebugEnabled()) { long elapsed = System.currentTimeMillis() - start; String params = String.format("params minid '%s' maxid '%s' excludeKeyPatterns %s conditions %s.", minId, maxId, excludeKeyPatterns, conditions); LOG.debug("Aggregate query " + selector + " on " + tmd.getName() + " with " + params + " -> " + result + ", took " + elapsed + "ms"); } } } RDBDocumentStoreJDBC(RDBDocumentStoreDB dbInfo, RDBDocumentSerializer ser, int queryHitsLimit, int queryTimeLimit); boolean appendingUpdate(Connection connection, RDBTableMetaData tmd, String id, Long modified,
            boolean setModifiedConditionally, Number hasBinary, Boolean deletedOnce, Long modcount, Long cmodcount,
            Long oldmodcount, String appendData); int delete(Connection connection, RDBTableMetaData tmd, List<String> allIds); int delete(Connection connection, RDBTableMetaData tmd, Map<String, Long> toDelete); int deleteWithCondition(Connection connection, RDBTableMetaData tmd, List<QueryCondition> conditions); long determineServerTimeDifferenceMillis(Connection connection); Set<String> insert(Connection connection, RDBTableMetaData tmd, List<T> documents); Set<String> update(Connection connection, RDBTableMetaData tmd, List<T> documents, boolean upsert); @NotNull List<RDBRow> query(Connection connection, RDBTableMetaData tmd, String minId, String maxId,
            List<String> excludeKeyPatterns, List<QueryCondition> conditions, int limit); long getLong(Connection connection, RDBTableMetaData tmd, String aggregate, String field, String minId, String maxId,
            List<String> excludeKeyPatterns, List<QueryCondition> conditions); @NotNull Iterator<RDBRow> queryAsIterator(RDBConnectionHandler ch, RDBTableMetaData tmd, String minId, String maxId,
            List<String> excludeKeyPatterns, List<QueryCondition> conditions, int limit, String sortBy); List<RDBRow> read(Connection connection, RDBTableMetaData tmd, Collection<String> allKeys); @Nullable RDBRow read(Connection connection, RDBTableMetaData tmd, String id, long lastmodcount, long lastmodified); boolean update(Connection connection, RDBTableMetaData tmd, String id, Long modified, Number hasBinary,
            Boolean deletedOnce, Long modcount, Long cmodcount, Long oldmodcount, String data);  }
@Test public void queryCountTest() throws SQLException { insertTestResource(this.getClass().getName() + "." + name.getMethodName()); Connection con = super.rdbDataSource.getConnection(); try { con.setReadOnly(true); RDBTableMetaData tmd = ((RDBDocumentStore) super.ds).getTable(Collection.NODES); List<QueryCondition> conditions = Collections.emptyList(); long cnt = jdbc.getLong(con, tmd, "count", "*", null, null, RDBDocumentStore.EMPTY_KEY_PATTERN, conditions); assertTrue(cnt > 0); } finally { con.close(); } }
@Test public void queryMinLastModifiedTest() throws SQLException { String baseName = this.getClass().getName() + "." + name.getMethodName(); long magicValue = (long)(Math.random() * 100000); String baseNameNullModified = baseName + "-1"; super.ds.remove(Collection.NODES, baseNameNullModified); UpdateOp op = new UpdateOp(baseNameNullModified, true); op.set(RDBDocumentStore.COLLISIONSMODCOUNT, magicValue); op.set(NodeDocument.DELETED_ONCE, true); assertTrue(super.ds.create(Collection.NODES, Collections.singletonList(op))); removeMe.add(baseNameNullModified); String baseName10Modified = baseName + "-2"; super.ds.remove(Collection.NODES, baseName10Modified); op = new UpdateOp(baseName10Modified, true); op.set(RDBDocumentStore.COLLISIONSMODCOUNT, magicValue); op.set(NodeDocument.MODIFIED_IN_SECS, 10); op.set(NodeDocument.DELETED_ONCE, true); assertTrue(super.ds.create(Collection.NODES, Collections.singletonList(op))); removeMe.add(baseName10Modified); String baseName20Modified = baseName + "-3"; super.ds.remove(Collection.NODES, baseName20Modified); op = new UpdateOp(baseName20Modified, true); op.set(RDBDocumentStore.COLLISIONSMODCOUNT, magicValue); op.set(NodeDocument.MODIFIED_IN_SECS, 20); op.set(NodeDocument.DELETED_ONCE, true); assertTrue(super.ds.create(Collection.NODES, Collections.singletonList(op))); removeMe.add(baseName20Modified); String baseName5ModifiedNoDeletedOnce = baseName + "-4"; super.ds.remove(Collection.NODES, baseName5ModifiedNoDeletedOnce); op = new UpdateOp(baseName5ModifiedNoDeletedOnce, true); op.set(RDBDocumentStore.COLLISIONSMODCOUNT, magicValue); op.set(NodeDocument.MODIFIED_IN_SECS, 5); assertTrue(super.ds.create(Collection.NODES, Collections.singletonList(op))); removeMe.add(baseName5ModifiedNoDeletedOnce); LogCustomizer customLogs = LogCustomizer.forLogger(RDBDocumentStoreJDBC.class.getName()).enable(Level.DEBUG) .contains("Aggregate query").contains("min(MODIFIED)").create(); customLogs.starting(); Connection con = super.rdbDataSource.getConnection(); try { con.setReadOnly(true); RDBTableMetaData tmd = ((RDBDocumentStore) super.ds).getTable(Collection.NODES); List<QueryCondition> conditions = new ArrayList<QueryCondition>(); conditions.add(new QueryCondition(RDBDocumentStore.COLLISIONSMODCOUNT, "=", magicValue)); long min = jdbc.getLong(con, tmd, "min", "_modified", null, null, RDBDocumentStore.EMPTY_KEY_PATTERN, conditions); assertEquals(5, min); con.commit(); } finally { con.close(); assertEquals("should have a DEBUG level log entry", 1, customLogs.getLogs().size()); customLogs.finished(); customLogs = null; } con = super.rdbDataSource.getConnection(); try { con.setReadOnly(true); RDBTableMetaData tmd = ((RDBDocumentStore) super.ds).getTable(Collection.NODES); List<QueryCondition> conditions = new ArrayList<QueryCondition>(); conditions.add(new QueryCondition(RDBDocumentStore.COLLISIONSMODCOUNT, "=", magicValue)); conditions.add(new QueryCondition(NodeDocument.DELETED_ONCE, "=", 1)); long min = jdbc.getLong(con, tmd, "min", "_modified", null, null, RDBDocumentStore.EMPTY_KEY_PATTERN, conditions); assertEquals(10, min); con.commit(); } finally { con.close(); } }