Was looking at how some things work and made a few drive-by optimizations

to FindAllQuery.
- We don't need to destructively modify the Set of keys to fetch if we hold
  on to one iterator.
- We can create an ArrayList that won't need to grow if our keys Iterable
  happens to be a Collection.
- And, just generally prefer Lists factories to specific constructors.
This commit is contained in:
Ray Greenwell
2010-09-20 23:21:06 +00:00
parent 8c5f333f73
commit 020d33c8aa
2 changed files with 10 additions and 8 deletions
@@ -20,7 +20,6 @@
package com.samskivert.depot.impl;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
@@ -28,6 +27,8 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.google.common.collect.Lists;
import com.samskivert.jdbc.DatabaseLiaison;
import com.samskivert.depot.DatabaseException;
import com.samskivert.depot.Key;
@@ -71,7 +72,7 @@ public class FindAllKeysQuery<T extends PersistentRecord> extends Query<List<Key
public List<Key<T>> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison)
throws SQLException
{
List<Key<T>> keys = new ArrayList<Key<T>>();
List<Key<T>> keys = Lists.newArrayList();
PreparedStatement stmt = _builder.prepare(conn);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
@@ -25,7 +25,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -228,7 +228,7 @@ public abstract class FindAllQuery<T extends PersistentRecord> extends Query<Lis
public List<T> invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison)
throws SQLException
{
List<T> result = new ArrayList<T>();
List<T> result = Lists.newArrayList();
SQLBuilder builder = ctx.getSQLBuilder(DepotTypes.getDepotTypes(ctx, _select));
builder.newQuery(_select);
ResultSet rs = builder.prepare(conn).executeQuery();
@@ -288,7 +288,9 @@ public abstract class FindAllQuery<T extends PersistentRecord> extends Query<Lis
protected List<T> resolve (Iterable<Key<T>> allKeys, Map<Key<T>, T> entities)
{
List<T> result = new ArrayList<T>();
List<T> result = (allKeys instanceof Collection<?>)
? Lists.<T>newArrayListWithCapacity(((Collection<?>)allKeys).size())
: Lists.<T>newArrayList();
for (Key<T> key : allKeys) {
T value = entities.get(key);
if (value != null) {
@@ -310,12 +312,11 @@ public abstract class FindAllQuery<T extends PersistentRecord> extends Query<Lis
// if we're fetching a huge number of records, we have to do it in multiple queries
if (fetchKeys.size() > In.MAX_KEYS) {
int keyCount = fetchKeys.size();
Iterator<Key<T>> iter = fetchKeys.iterator();
do {
Set<Key<T>> keys = Sets.newHashSet();
Iterator<Key<T>> iter = fetchKeys.iterator();
for (int ii = 0; ii < Math.min(keyCount, In.MAX_KEYS); ii++) {
for (int ii = 0, nn = Math.min(keyCount, In.MAX_KEYS); ii < nn; ii++) {
keys.add(iter.next());
iter.remove();
}
keyCount -= keys.size();
loadRecords(ctx, conn, keys, entities, origStmt);