private List<ClusterSummary> getSummaries(PreparedStatement statement) throws SQLException { ResultSet rs = statement.executeQuery(); try { List<ClusterSummary> summaries = Lists.newArrayList(); while (rs.next()) { Cluster cluster = dbQueryExecutor.deserializeBlob(rs.getBlob(1), Cluster.class); ClusterJob clusterJob = dbQueryExecutor.deserializeBlob(rs.getBlob(2), ClusterJob.class); summaries.add(new ClusterSummary(cluster, clusterJob)); } return ImmutableList.copyOf(summaries); } finally { rs.close(); } }
/** * Queries the store for a single item, deserializing the item and returning it or null if the item does not exist. * Statement passed in must be closed by the caller. * * @param statement PreparedStatement of the query, ready for execution. * @param clazz Class of the item being queried. * @param <T> Type of the item being queried. * @return Item queried for, or null if none exists. * @throws java.sql.SQLException */ public <T> T getQueryItem(PreparedStatement statement, Class<T> clazz) throws SQLException { ResultSet rs = statement.executeQuery(); try { if (rs.next()) { Blob blob = rs.getBlob(1); return deserializeBlob(blob, clazz); } else { return null; } } finally { rs.close(); } }
/** * Queries the store for a set of items, deserializing the items and returning an immutable set of them. If no items * exist, the set will be empty. Statement passed in must be closed by the caller. * * @param statement PreparedStatement of the query, ready for execution. * @param clazz Class of the items being queried. * @param <T> Type of the items being queried. * @return Immutable set of items queried for. * @throws java.sql.SQLException */ public <T> ImmutableSet<T> getQuerySet(PreparedStatement statement, Class<T> clazz) throws SQLException { ResultSet rs = statement.executeQuery(); try { Set<T> results = Sets.newHashSet(); while (rs.next()) { Blob blob = rs.getBlob(1); results.add(deserializeBlob(blob, clazz)); } return ImmutableSet.copyOf(results); } finally { rs.close(); } }
/** * Queries the store for a list of at most limit items, deserializing the items and returning an immutable * list of them. If no items exist, the list will be empty. Statement passed in must be closed by the caller. * * @param statement PreparedStatement of the query, ready for execution. * @param clazz Class of the items being queried. * @param <T> Type of the items being queried. * @param limit Max number of items to get. * @return Immutable list of objects that were queried for. * @throws java.sql.SQLException */ public <T> ImmutableList<T> getQueryList(PreparedStatement statement, Class<T> clazz, int limit) throws SQLException { ResultSet rs = statement.executeQuery(); try { List<T> results = Lists.newArrayList(); int numResults = 0; int actualLimit = limit < 0 ? Integer.MAX_VALUE : limit; while (rs.next() && numResults < actualLimit) { Blob blob = rs.getBlob(1); results.add(deserializeBlob(blob, clazz)); numResults++; } return ImmutableList.copyOf(results); } finally { rs.close(); } }
while (rs.next() && numResults < actualLimit) { Blob blob = rs.getBlob(1); results.add(transform.apply(deserializeBlob(blob, clazz))); numResults++;