HistoricalTimeSeriesProviderGetResult result = new HistoricalTimeSeriesProviderGetResult();
// find in cache
Set<ExternalIdBundle> remainingIds = new HashSet<ExternalIdBundle>();
for (ExternalIdBundle bundle : request.getExternalIdBundles()) {
HistoricalTimeSeriesProviderGetRequest key = createCacheKey(request, bundle, false);
LocalDateDoubleTimeSeries cached = doSingleGetInCache(key);
if (cached != null) {
if (cached == NO_HTS) {
result.getResultMap().put(bundle, null);
} else {
result.getResultMap().put(bundle, cached);
}
} else {
remainingIds.add(bundle);
}
}
// find in underlying
if (remainingIds.size() > 0) {
HistoricalTimeSeriesProviderGetRequest underlyingAllRequest = JodaBeanUtils.clone(request);
underlyingAllRequest.setExternalIdBundles(remainingIds);
underlyingAllRequest.setDateRange(LocalDateRange.ALL);
underlyingAllRequest.setMaxPoints(null);
HistoricalTimeSeriesProviderGetResult underlyingAllResult = _underlying.getHistoricalTimeSeries(underlyingAllRequest);
// cache result for whole time-series
for (ExternalIdBundle bundle : remainingIds) {
LocalDateDoubleTimeSeries underlyingWholeHts = underlyingAllResult.getResultMap().get(bundle);
if (underlyingWholeHts == null) {
underlyingWholeHts = NO_HTS;
}
HistoricalTimeSeriesProviderGetRequest wholeHtsKey = createCacheKey(underlyingAllRequest, bundle, true);
_cache.put(new Element(wholeHtsKey, underlyingWholeHts));
}
// cache result for requested time-series
HistoricalTimeSeriesProviderGetResult fiteredResult = filterResult(underlyingAllResult, request.getDateRange(), request.getMaxPoints());
for (ExternalIdBundle bundle : remainingIds) {
LocalDateDoubleTimeSeries filteredHts = fiteredResult.getResultMap().get(bundle);
result.getResultMap().put(bundle, filteredHts);
if (filteredHts == null) {
filteredHts = NO_HTS;
}
HistoricalTimeSeriesProviderGetRequest key = createCacheKey(request, bundle, false);
_cache.put(new Element(key, filteredHts));
}
}
return result;
}