Exemplo n.º 1
0
		private IEnumerable<IGrouping<dynamic, object>> ExecuteMaps(IEnumerable<object> items)
		{
			var robustEnumerator = new RobustEnumerator2(_aggregationEngine.CancellationToken, 50)
				{
					OnError = (exception, o) => 
						Log.WarnException("Could not process maps event for aggregator: " + _name + Environment.NewLine + o, exception)
				};

			var results = robustEnumerator
				.RobustEnumeration(items.GetEnumerator(), _generator.MapDefinitions)
			    .ToList();

			var reduced = robustEnumerator.RobustEnumeration(results.GetEnumerator(), _generator.ReduceDefinition);

			var groupedByReduceKey = reduced.GroupBy(x =>
				{
					var reduceKey = _generator.GroupByExtraction(x);
					if (reduceKey == null)
						return "@null";
					var s = reduceKey as string;
					if (s != null)
						return s;
					var ravenJToken = RavenJToken.FromObject(reduceKey);
					if (ravenJToken.Type == JTokenType.String)
						return ravenJToken.Value<string>();
					return ravenJToken.ToString(Formatting.None);
				})
			                                .ToArray();
			return groupedByReduceKey;
		}
Exemplo n.º 2
0
		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));
			}
		}