private void ExecuteReduce(IEnumerable<IGrouping<dynamic, object>> groupedByReduceKey, WriteBatch writeBatch) { foreach (var grouping in groupedByReduceKey) { string reduceKey = grouping.Key; Slice key = "results/" + _name + "/" + reduceKey; var groupedResults = GetItemsToReduce(reduceKey, key, grouping); var robustEnumerator = new RobustEnumerator2(_aggregationEngine.CancellationToken, 50) { OnError = (exception, o) => Log.WarnException("Could not process reduce for aggregation " + _name + Environment.NewLine + o, exception) }; var reduceResults = robustEnumerator.RobustEnumeration(groupedResults.GetEnumerator(), _generator.ReduceDefinition).ToArray(); RavenJToken finalResult; switch (reduceResults.Length) { case 0: Log.Warn("FLYING PIGS!!! Could not find any results for a reduce on key {0} for aggregator {1}. Should not happen", reduceKey, _name); finalResult = new RavenJObject {{"Error", "Invalid reduce result was generated"}}; break; case 1: finalResult = RavenJObject.FromObject(reduceResults[0]); break; default: finalResult = new RavenJArray(reduceResults.Select(RavenJObject.FromObject)); break; } finalResult.EnsureCannotBeChangeAndEnableSnapshotting(); _cache.Set(reduceKey, finalResult); writeBatch.Put(key, AggregationEngine.RavenJTokenToStream(finalResult)); } }