diff --git a/src/java/com/samskivert/depot/FindAllQuery.java b/src/java/com/samskivert/depot/FindAllQuery.java index de6f9fa..16ea025 100644 --- a/src/java/com/samskivert/depot/FindAllQuery.java +++ b/src/java/com/samskivert/depot/FindAllQuery.java @@ -79,6 +79,13 @@ public abstract class FindAllQuery extends Query getCachedResult (PersistenceContext ctx) + { + return null; // TODO + } + + @Override // from Query public List invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) throws SQLException { @@ -86,6 +93,7 @@ public abstract class FindAllQuery extends Query> allKeys = Lists.newArrayList(); Set> fetchKeys = Sets.newHashSet(); + // first load up the primary keys on which we're operating PreparedStatement stmt = _builder.prepare(conn); String stmtString = stmt.toString(); // for debugging try { @@ -93,21 +101,13 @@ public abstract class FindAllQuery extends Query key = _marsh.makePrimaryKey(rs); allKeys.add(key); - - T value = ctx.cacheLookup(key); - if (value != null) { - @SuppressWarnings("unchecked") T newValue = (T) value.clone(); - entities.put(key, newValue); - continue; - } - - fetchKeys.add(key); } - } finally { JDBCUtil.close(stmt); } + // now fetch any records we can from the cache + loadFromCache(ctx, allKeys, entities, fetchKeys); _cachedRecords = entities.size(); _uncachedRecords = fetchKeys.size(); @@ -116,10 +116,13 @@ public abstract class FindAllQuery extends Query extends Query getCachedResult (PersistenceContext ctx) + { + // look up what we can from the cache + loadFromCache(ctx, _keys, _entities, _fetchKeys); + _cachedRecords = _entities.size(); + _uncachedRecords = _fetchKeys.size(); + + // if we found everything, we can just return our result straight away, yay! + return _fetchKeys.isEmpty() ? resolve(_keys, _entities) : null; + } + + @Override // from Query public List invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) throws SQLException { - Map, T> entities = Maps.newHashMap(); - Set> fetchKeys = Sets.newHashSet(); - for (Key key : _keys) { - T value = ctx.cacheLookup(key); - if (value != null) { - @SuppressWarnings("unchecked") T newValue = (T) value.clone(); - entities.put(key, newValue); - continue; - } - fetchKeys.add(key); - } - - _cachedRecords = entities.size(); - _uncachedRecords = fetchKeys.size(); - - return loadAndResolve(ctx, conn, _keys, fetchKeys, entities, null); + return loadAndResolve(ctx, conn, _keys, _fetchKeys, _entities, null); } - public void updateStats (Stats stats) { + @Override // from Query + public void updateStats (Stats stats) + { stats.noteQuery(0, 0, _cachedRecords, _uncachedRecords); } protected Collection> _keys; + protected Map, T> _entities = Maps.newHashMap(); + protected Set> _fetchKeys = Sets.newHashSet(); protected int _cachedRecords, _uncachedRecords; } @@ -185,6 +190,14 @@ public abstract class FindAllQuery extends Query getCachedResult (PersistenceContext ctx) + { + return null; // TODO: we could cache all the records as one giant list but that would + // not play nicely when records were evicted from the cache by primary key + } + + @Override // from Query public List invoke (PersistenceContext ctx, Connection conn, DatabaseLiaison liaison) throws SQLException { @@ -207,25 +220,15 @@ public abstract class FindAllQuery extends Query getCachedResult (PersistenceContext ctx) - { - return null; // TODO - } - - @Override // from Operation - public void updateStats (Stats stats) - { - // TODO - } - protected FindAllQuery (PersistenceContext ctx, Class type) throws DatabaseException { @@ -233,6 +236,32 @@ public abstract class FindAllQuery extends Query> allKeys, + Map, T> entities, Set> fetchKeys) + { + for (Key key : allKeys) { + T value = ctx.cacheLookup(key); + if (value != null) { + @SuppressWarnings("unchecked") T newValue = (T) value.clone(); + entities.put(key, newValue); + continue; + } + fetchKeys.add(key); + } + } + + protected List resolve (Collection> allKeys, Map, T> entities) + { + List result = Lists.newArrayList(); + for (Key key : allKeys) { + T value = entities.get(key); + if (value != null) { + result.add(value); + } + } + return result; + } + protected List loadAndResolve (PersistenceContext ctx, Connection conn, Collection> allKeys, Set> fetchKeys, Map, T> entities, String origStmt) @@ -260,14 +289,7 @@ public abstract class FindAllQuery extends Query result = Lists.newArrayList(); - for (Key key : allKeys) { - T value = entities.get(key); - if (value != null) { - result.add(value); - } - } - return result; + return resolve(allKeys, entities); } protected void loadRecords (PersistenceContext ctx, Connection conn, Set> keys,