private void fetchForFeeds(Date sdate, Set<String> feedids, PersistenceManager pm,
boolean fetchEntries, boolean prior, int range) {
try {
List<String> fetchlist = new ArrayList<String>();
StreamItem entry = null;
Query dq = null, q2 = null;
String qstring = null;
if (sdate == null) {
qstring = " where :f1.contains(feedUrl)";
}
else if (prior) {
qstring = " where date < :d1 && :f1.contains(feedUrl)";
}
else {
qstring = " where date >= :d1 && :f1.contains(feedUrl)";
}
// break feedids list into sublists so don't reach Datastore query max
List<List<String>> partition = ListPartition.partition(new ArrayList<String>(feedids),
MAXFLIST);
// for each sublist of feed ids
for (List<String> fsublist : partition) {
dq = pm.newQuery("select id from " + StreamItem.class.getName() + qstring);
dq.setOrdering("date desc");
if (prior) {
dq.setRange(0, range);
}
dq.addExtension("datanucleus.appengine.datastoreReadConsistency", "EVENTUAL");
List<String> entryids;
if (sdate != null) {
entryids = (List<String>) dq.execute(sdate, fsublist);
}
else {
entryids = (List<String>) dq.execute(fsublist);
}
// for each id, check for cached object(s) as appropriate;
// if not available, add to list of streamitem ids for which the objects
// need to be fetched
// if available, add to list which will eventually need to be sorted.
Object o = null, o2 = null;
for (String eid : entryids) {
if (fetchEntries) {
o = CacheSupport.cacheGet(StreamItem.class.getName(), eid);
if (o != null && o instanceof StreamItem) {
entry = (StreamItem) o;
entries.add(entry);
// add summary to summaries list
o2 = CacheSupport.cacheGet(StreamItemSummaryDTO.class.getName(), eid);
if (o2 != null && o2 instanceof StreamItemSummaryDTO) {
// cache hit
summaries.add((StreamItemSummaryDTO) o2);
}
else {
// this case should not come up unless item removed from cache by system
summaries.add(entry.addSummaryToCache());
}
}
else {
fetchlist.add(eid);
}
}
else {
// fetch just summaries
o2 = CacheSupport.cacheGet(StreamItemSummaryDTO.class.getName(), eid);
if (o2 != null && o2 instanceof StreamItemSummaryDTO) {
// cache hit
summaries.add((StreamItemSummaryDTO) o2);
}
else {
// is the stream item itself cached?
o = CacheSupport.cacheGet(StreamItem.class.getName(), eid);
if (o != null && o instanceof StreamItem) {
// logger.info("got cache hit on stream item " + StreamItem.class.getName() + ", \n" + eid);
entry = (StreamItem) o;
// add summary to summaries list
summaries.add(entry.addSummaryToCache());
}
else {
fetchlist.add(eid);
}
}