Last active
October 12, 2015 21:39
-
-
Save afifmohammed/4091197 to your computer and use it in GitHub Desktop.
multipmap reduce on locations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace RavenIndexSandbox | |
{ | |
using System; | |
using System.Linq; | |
using NUnit.Framework; | |
using Raven.Client.Embedded; | |
using Raven.Client.Indexes; | |
[TestFixture] | |
public class WhenGroupinByLocation | |
{ | |
[Serializable] | |
public class Sale | |
{ | |
public string Id { get; set; } | |
public string Title { get; set; } | |
public Schedule[] Schedules { get; set; } | |
public Location[] Locations { get; set; } | |
public int Quantity { get; set; } | |
public class Location | |
{ | |
public double Lat { get; set; } | |
public double Lng { get; set; } | |
} | |
public class Schedule | |
{ | |
public DateTime From { get; set; } | |
public DateTime Till { get; set; } | |
} | |
} | |
[Serializable] | |
public class Order | |
{ | |
public string Id { get; set; } | |
public string SaleId { get; set; } | |
} | |
[Serializable] | |
public class SiteSale | |
{ | |
public string SaleId { get; set; } | |
public DateTime From { get; set; } | |
public DateTime Till { get; set; } | |
public double[][] Locations { get; set; } | |
public int TotalSold { get; set; } | |
} | |
public class Sales_ByLocation : AbstractMultiMapIndexCreationTask<SiteSale> | |
{ | |
public Sales_ByLocation() | |
{ | |
AddMap<Sale>(sales => from sale in sales | |
from sch in sale.Schedules | |
select new | |
{ | |
SaleId = sale.Id, | |
sch.From, | |
sch.Till, | |
Locations = sale.Locations.Select(l => new [] { l.Lat, l.Lng}), | |
TotalSold = 0 | |
}); | |
AddMap<Order>(orders => from order in orders | |
select new | |
{ | |
order.SaleId, | |
From = DateTime.MinValue, | |
Till = DateTime.MinValue, | |
Locations = new double[][] {}, | |
TotalSold = 1 | |
}); | |
Reduce = sitesales => from sitesale in sitesales | |
group sitesale by sitesale.SaleId | |
into sales | |
let locations = sales.SelectMany(x => x.Locations) | |
from sale in sales | |
select new | |
{ | |
sale.SaleId, | |
sale.From, | |
sale.Till, | |
Locations = locations, | |
TotalSold = sales.Sum(x => x.TotalSold) | |
}; | |
} | |
} | |
[Test] | |
public void CanFindSale() | |
{ | |
var now = DateTime.Now; | |
using(var d = new EmbeddableDocumentStore { RunInMemory = true}.Initialize()) // build 1.0.960 | |
using(var s = d.OpenSession()) | |
{ | |
d.ExecuteIndex(new Sales_ByLocation()); | |
var sale = new Sale | |
{ | |
Schedules = new [] { new Sale.Schedule { From = now.AddDays(-1), Till = now.AddDays(1)}, new Sale.Schedule { From = now.AddDays(2), Till = now.AddDays(3)} }, | |
Locations = new [] { new Sale.Location { Lat = 37.780, Lng = 144.960}, new Sale.Location { Lat = 37.790, Lng = 144.960}, }, | |
Quantity = 10, | |
Title = "raven master class" | |
}; | |
s.Store(sale); | |
s.Store(new Order { SaleId = sale.Id}); | |
s.Store(new Order { SaleId = sale.Id}); | |
s.SaveChanges(); | |
var sitesales = s.Query<SiteSale, Sales_ByLocation>().Customize(x => x.WaitForNonStaleResults()) | |
.Where(x => x.From < now && x.Till > now).ToList(); | |
Assert.That(sitesales.Count, Is.EqualTo(1)); | |
Assert.That(sitesales.First().TotalSold, Is.EqualTo(2)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment