public async Task CanAdd()
		{
			using (var agg = new AggregationEngine())
			{
				await agg.InitAsync();
				await agg.CreateAggregationAsync(new IndexDefinition
				{
					Name = "Test",
					Map = "from doc in docs select new { Count = 1}",
					Reduce = "from result in results group result by 1 into g select new { Count = g.Sum(x=>x.Count) }"
				});

				Etag last = null;
				for (int j = 0; j < 3; j++)
				{
					for (int i = 0; i < 15; i++)
					{
						last = await agg.AppendAsync("test", new RavenJObject { { "Item", i } });
					}

					var aggregation = agg.GetAggregation("test");

					await aggregation.WaitForEtagAsync(last);


					var eventDatas = agg.Events(Etag.Empty).ToList();
					Assert.Equal(15 * (j + 1), eventDatas.Count);
					
					var result = aggregation.AggregationResultFor("1");
					Assert.Equal(eventDatas.Count, result.Value<int>("Count"));
				}
				await agg.DisposeAsync();
			}
		}
		public async Task CanAddAndRememberAfterRestart()
		{
			FileSystem fileSystem;
			using (var agg = new AggregationEngine())
			{
				await agg.InitAsync();
				await agg.CreateAggregationAsync(new IndexDefinition
				{
					Name = "Test",
					Map = "from doc in docs select new { Count = 1}",
					Reduce = "from result in results group result by 1 into g select new { Count = g.Sum(x=>x.Count) }"
				});
				Assert.NotNull(agg.GetAggregation("Test"));

				fileSystem = agg.Storage.StorageState.FileSystem;

			    await agg.DisposeAsync();
			}

			using (var agg = new AggregationEngine())
			{
				agg.Storage.StorageState.FileSystem = fileSystem;
				await agg.InitAsync();
				Assert.NotNull(agg.GetAggregation("Test"));
			}
		}
		public async Task WillRememberAfterRestart()
		{
			FileSystem fs;
			Etag last;
			using (var agg = new AggregationEngine())
			{
				await agg.InitAsync();
				fs = agg.Storage.StorageState.FileSystem;

				await agg.CreateAggregationAsync(new IndexDefinition
				{
					Name = "Test",
					Map = "from doc in docs select new { Count = 1}",
					Reduce = "from result in results group result by 1 into g select new { Count = g.Sum(x=>x.Count) }"
				});

				var aggregation = agg.GetAggregation("test");
				for (int j = 0; j < 3; j++)
				{
					Etag lastWrite = null;
					for (int i = 0; i < 15; i++)
					{
						lastWrite = await agg.AppendAsync("test", new RavenJObject { { "Item", i } });
					}


					await aggregation.WaitForEtagAsync(lastWrite);

					var result = aggregation.AggregationResultFor("1");

					Assert.Equal(15 * (j + 1), result.Value<int>("Count"));
				}

				last = aggregation.LastAggregatedEtag;
				await agg.DisposeAsync();
			}

			using (var agg = new AggregationEngine())
			{
				agg.Storage.StorageState.FileSystem = fs;
				await agg.InitAsync();

				var aggregation = agg.GetAggregation("test");
				var result = aggregation.AggregationResultFor("1");

				Assert.Equal(last, aggregation.LastAggregatedEtag);
				Assert.Equal(45, result.Value<int>("Count"));

				await agg.DisposeAsync();
			}

		}
		public async Task UsingMultiMap()
		{
			using (var agg = new AggregationEngine())
			{
				await agg.InitAsync();
				
				await agg.CreateAggregationAsync(new IndexDefinition
					{
						Name = "TotalMonthlyBill",
						Maps =
							{
								"from sms in docs.Sms select new " +
								"{ Calls = 0, Sms = 1,  Key = sms.From + '/' + sms.At.Year  +'/' + sms.At.Month}",
								"from call in docs.Calls select new " +
								"{ Calls = call.Duration.TotalSeconds, Sms = 0, Key = call.From + '/' + call.At.Year  +'/' + call.At.Month }"
							},
						Reduce = "from result in results group result by result.Key into g " +
						         "select new {Calls = g.Sum(x=>x.Calls), Sms = g.Sum(x=>x.Sms), g.Key }"
					});


				for (int i = 0; i < 50; i++)
				{
					await agg.AppendAsync("Sms", new RavenJObject
						{
							{"From", "1234"},
							{"At", new DateTime(2013, 6, 19).AddDays(i).ToString("o")}
						});
				}

				for (int i = 0; i < 70; i++)
				{
					await agg.AppendAsync("Calls", new RavenJObject
						{
							{"From", "1234"},
							{"At", new DateTime(2013, 6, 19).AddDays(i).ToString("o")},
							{"Duration", TimeSpan.FromSeconds(i+4).ToString()}
						});
				}

				var aggregation = agg.GetAggregation("TotalMonthlyBill");
				await aggregation.WaitForEtagAsync(agg.LastGeneratedEtag);

				var datas = aggregation.AggregationResults().ToArray();

				Assert.Equal(3, datas.Length);

				await agg.DisposeAsync();
			}
		}
		public async Task CanAdd()
		{
			using (var agg = new AggregationEngine())
			{
				await agg.InitAsync();
				await agg.CreateAggregationAsync(new IndexDefinition
					{
						Name = "Test",
						Map = "from doc in docs select new { Count = 1}",
						Reduce = "from result in results group result by 1 into g select new { Count = g.Sum(x=>x.Count) }"
					});
				Assert.NotNull(agg.GetAggregation("Test"));
			}
		}
		public async Task CanUpdate()
		{
			using (var agg = new AggregationEngine())
			{
				await agg.InitAsync();

				await agg.CreateAggregationAsync(new IndexDefinition
				{
					Name = "Test",
					Map = "from doc in docs select new { Count = 1}",
					Reduce = "from result in results group result by 1 into g select new { Count = g.Sum(x=>x.Count) }"
				});

				var indexDefinition = new IndexDefinition
					{
						Name = "Test",
						Map = "from doc in docs select new { Sum = 1}",
						Reduce = "from result in results group result by 1 into g select new { Sum = g.Sum(x=>x.Sum) }"
					};
				await agg.CreateAggregationAsync(indexDefinition);

				Assert.Contains(indexDefinition.Reduce, agg.GetAggregation("Test").Generator.ViewText);
			}
		}