com.googlecode.objectify.impl.SessionCachingQueryImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of objectify Show documentation
Show all versions of objectify Show documentation
*** THIS VERSION UPLOADED FOR USE WITH CEDAR-COMMON, TO AVOID DEPENDENCIES ON GOOGLE CODE-BASED MAVEN REPOSITORIES. *** The simplest convenient interface to the Google App Engine datastore
The newest version!
package com.googlecode.objectify.impl;
import java.util.Map;
import com.google.appengine.api.datastore.QueryResultIterator;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.util.QueryResultIteratorWrapper;
/**
* Extends the QueryImpl to add a session cache. Note that it only needs
* to override the iterator() method.
*
* @author Jeff Schnitzer
*/
public class SessionCachingQueryImpl extends QueryImpl
{
/** The cache is a simple hashmap, obtained from the SessionCachingObjectifyImpl */
final protected Map, Object> cache;
/** */
public SessionCachingQueryImpl(ObjectifyFactory fact, Objectify ofy, Map, Object> cache)
{
super(fact, ofy);
this.cache = cache;
}
/** */
public SessionCachingQueryImpl(ObjectifyFactory fact, Objectify ofy, Map, Object> cache, Class clazz)
{
super(fact, ofy, clazz);
this.cache = cache;
}
@Override
public QueryResultIterator iterator()
{
return new SessionCachingQueryResultIterator(super.iterator());
}
/**
* Simple iterator passes through and merges with the cache.
*/
protected class SessionCachingQueryResultIterator extends QueryResultIteratorWrapper
{
public SessionCachingQueryResultIterator(QueryResultIterator base)
{
super(base);
}
@Override
@SuppressWarnings("unchecked")
public T next()
{
T t = super.next();
Key key = factory.getKey(t);
T cached = (T)cache.get(key);
if (cached == null || cached == SessionCachingAsyncObjectifyImpl.NEGATIVE_RESULT)
{
cache.put(key, t);
cached = t;
}
return cached;
}
}
}