-
-
Save fitzchak/4335881 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using Raven.Abstractions.Indexing; | |
using Raven.Client.Indexes; | |
using Xunit; | |
namespace Raven.Tests | |
{ | |
public class DataSetIndexTests : RavenTest | |
{ | |
[Fact] | |
public void can_execute_query_default() | |
{ | |
using (var store = NewRemoteDocumentStore()) | |
{ | |
new DataSetIndex().Execute(store); | |
using (var session = store.OpenSession()) | |
{ | |
session.Store(CreateDataSet("stations/rtl", "T1")); | |
session.Store(CreateDataSet("stations/rtl", "T2")); | |
session.Store(CreateDataSet("stations/energy", "EX")); | |
session.SaveChanges(); | |
} | |
using (var session = store.OpenSession()) | |
{ | |
var result = session.Advanced.LuceneQuery<DataSetIndex.Result, DataSetIndex>() | |
.WaitForNonStaleResults() | |
.AddOrder("Split_N1_Range", true, typeof (double)) | |
.SelectFields<dynamic>("SongId", "Title", "Interpret", "Year", "Attributes", "SID", "SetId") | |
.ToList(); | |
Assert.Equal("songs/50", result.First().SongId); //GREEN | |
} | |
} | |
} | |
[Fact] | |
public void can_execute_query_lazily() | |
{ | |
using (var store = NewRemoteDocumentStore()) | |
{ | |
new DataSetIndex().Execute(store); | |
using (var session = store.OpenSession()) | |
{ | |
session.Store(CreateDataSet("stations/rtl", "T1")); | |
session.Store(CreateDataSet("stations/rtl", "T2")); | |
session.Store(CreateDataSet("stations/energy", "EX")); | |
session.SaveChanges(); | |
} | |
using (var session = store.OpenSession()) | |
{ | |
var query = session.Advanced.LuceneQuery<DataSetIndex.Result, DataSetIndex>() | |
.WaitForNonStaleResults() | |
.AddOrder("Split_N1_Range", true, typeof (double)) | |
.SelectFields<dynamic>("SongId", "Title", "Interpret", "Year", "Attributes", "SID", "SetId") | |
.Lazily(); | |
var result = query.Value.ToList(); | |
Assert.Equal("songs/50", result.First().SongId); //RED! (: | |
} | |
} | |
} | |
public class DataSet | |
{ | |
public string Id { get; set; } | |
public List<Item> Items { get; set; } | |
public string StationId { get; set; } | |
public DateTime Date { get; set; } | |
} | |
public class Item | |
{ | |
public List<Attribute> Attributes { get; set; } | |
public string SongId { get; set; } | |
} | |
public class Attribute | |
{ | |
public Attribute() | |
{ | |
} | |
public Attribute(string name, object value) | |
{ | |
Name = name; | |
Value = value; | |
} | |
public string Name { get; set; } | |
public object Value { get; set; } | |
} | |
private static DataSet CreateDataSet(string stationId, string datasetKey) | |
{ | |
return new DataSet | |
{ | |
Id = stationId + "/test/" + datasetKey, | |
StationId = stationId, | |
Date = DateTime.UtcNow, | |
Items = Enumerable.Range(1, 50).Select(x => new Item | |
{ | |
SongId = "songs/" + x, | |
Attributes = new List<Attribute> | |
{ | |
new Attribute("Split_N1", x*0.99d), | |
new Attribute("Split_N4", x*0.01d), | |
new Attribute("SoundCode", "Rock"), | |
new Attribute("Kat", "T" + x) | |
} | |
}).ToList() | |
}; | |
} | |
public class DataSetIndex : AbstractIndexCreationTask<DataSet, DataSetIndex.Result> | |
{ | |
public class Result | |
{ | |
public string SetId { get; set; } | |
public string SongId { get; set; } | |
public string StationId { get; set; } | |
public Attribute[] Attributes { get; set; } | |
public DateTime Date { get; set; } | |
} | |
public DataSetIndex() | |
{ | |
Map = sets => | |
from set in sets | |
from item in set.Items | |
select new | |
{ | |
SongId = item.SongId, | |
SetId = set.Id, | |
StationId = set.StationId, | |
Date = set.Date, | |
item.Attributes, | |
_ = "ignore" | |
}; | |
Reduce = results => | |
from result in results | |
group result by new {result.SongId, result.StationId} | |
into g | |
select new | |
{ | |
SongId = g.Key.SongId, | |
StationId = g.Key.StationId, | |
Date = g.OrderByDescending(x => x.Date).Select(x => x.Date).FirstOrDefault(), | |
SetId = g.OrderByDescending(x => x.Date).Select(x => x.SetId).FirstOrDefault(), | |
Attributes = g.OrderByDescending(x => x.Date).First().Attributes, | |
_ = g.OrderByDescending(x => x.Date).First().Attributes.Select(x => CreateField(x.Name, x.Value)) | |
}; | |
Sort(x => x.Date, SortOptions.String); | |
Stores = new Dictionary<Expression<Func<Result, object>>, FieldStorage>() | |
{ | |
{e => e.SongId, FieldStorage.Yes}, | |
{e => e.SetId, FieldStorage.Yes}, | |
{e => e.Attributes, FieldStorage.Yes}, | |
{e => e.StationId, FieldStorage.Yes} | |
}; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment