protected void HandleReduceForIndex(IndexToWorkOn indexToWorkOn)
{
List<MappedResultInfo> reduceKeyAndEtags = null;
try
{
transactionalStorage.Batch(actions =>
{
reduceKeyAndEtags = actions.MappedResults.GetMappedResultsReduceKeysAfter
(
indexToWorkOn.IndexName,
indexToWorkOn.LastIndexedEtag
).ToList();
if(log.IsDebugEnabled)
{
if (reduceKeyAndEtags.Count > 0)
log.Debug(() => string.Format("Found {0} mapped results for keys [{1}] for index {2}", reduceKeyAndEtags.Count, string.Join(", ", reduceKeyAndEtags.Select(x => x.ReduceKey).Distinct()), indexToWorkOn.IndexName));
else
log.Debug("No reduce keys found for {0}", indexToWorkOn.IndexName);
}
new ReduceTask
{
Index = indexToWorkOn.IndexName,
ReduceKeys = reduceKeyAndEtags.Select(x => x.ReduceKey).Distinct().ToArray(),
}.Execute(context);
});
}
finally
{
if (reduceKeyAndEtags != null && reduceKeyAndEtags.Count > 0)
{
var lastByEtag = GetLastByEtag(reduceKeyAndEtags);
var lastEtag = lastByEtag.Etag;
var lastIndexedEtag = new ComparableByteArray(lastEtag.ToByteArray());
// whatever we succeeded in indexing or not, we have to update this
// because otherwise we keep trying to re-index failed mapped results
transactionalStorage.Batch(actions =>
{
if (new ComparableByteArray(indexToWorkOn.LastIndexedEtag.ToByteArray()).CompareTo(lastIndexedEtag) <= 0)
{
actions.Indexing.UpdateLastReduced(indexToWorkOn.IndexName, lastByEtag.Etag, lastByEtag.Timestamp);
}
});
}
}
}