Created
October 13, 2011 15:10
-
-
Save dlidstrom/1284459 to your computer and use it in GitHub Desktop.
Raven indexing test (working)
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 RavenIndexTest | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.Composition.Hosting; | |
using System.Linq; | |
using Newtonsoft.Json; | |
using Raven.Client.Embedded; | |
using Raven.Client.Indexes; | |
public class Program | |
{ | |
void Run() | |
{ | |
using (var store = new EmbeddableDocumentStore { RunInMemory = true }.Initialize()) | |
using (var session = store.OpenSession()) | |
{ | |
var typeCatalog = new TypeCatalog(typeof(Matches_PlayerStats)); | |
IndexCreation.CreateIndexes(new CompositionContainer(typeCatalog), store); | |
var match = CreateMatch(); | |
session.Store(match); | |
session.SaveChanges(); | |
var stat = session.Query<Matches_PlayerStats.Results, Matches_PlayerStats>() | |
.Customize(x => x.WaitForNonStaleResults()) | |
.First(); | |
Console.WriteLine("{0,-17}: {1} {2} {3} {4}", stat.Player, stat.TotalPins, stat.TotalPins / stat.Count, stat.Max, stat.Min); | |
} | |
} | |
public static void Main() | |
{ | |
try | |
{ | |
new Program().Run(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex); | |
} | |
} | |
public static Match CreateMatch() | |
{ | |
var series = new List<Serie> | |
{ | |
new Serie(new List<Table> | |
{ | |
new Table | |
{ | |
Score = 1, | |
Game1 = new Game("Mikael Axelsson", 202), | |
Game2 = new Game("Christer Liedholm", 218) | |
}, | |
new Table | |
{ | |
Score = 0, | |
Game1 = new Game("Kjell Persson", 172), | |
Game2 = new Game("Peter Sjöberg", 220), | |
}, | |
new Table | |
{ | |
Score = 0, | |
Game1 = new Game("Kjell Jansson", 166), | |
Game2 = new Game("Hans Norbeck", 194) | |
}, | |
new Table | |
{ | |
Score = 1, | |
Game1 = new Game("Lars Öberg", 222), | |
Game2 = new Game("Torbjörn Jensen", 204) | |
} | |
}), | |
new Serie(new List<Table> | |
{ | |
new Table | |
{ | |
Score = 1, | |
Game1 = new Game("Lars Öberg", 182), | |
Game2 = new Game("Torbjörn Jensen", 211) | |
}, | |
new Table | |
{ | |
Score = 1, | |
Game1 = new Game("Kjell Jansson", 208), | |
Game2 = new Game("Hans Norbeck", 227) | |
}, | |
new Table | |
{ | |
Score = 0, | |
Game1 = new Game("Kjell Persson", 194), | |
Game2 = new Game("Peter Sjöberg", 195) | |
}, | |
new Table | |
{ | |
Score = 0, | |
Game1 = new Game("Mikael Axelsson", 206), | |
Game2 = new Game("Christer Liedholm", 150) | |
} | |
}), | |
new Serie(new List<Table> | |
{ | |
new Table | |
{ | |
Score = 0, | |
Game1 = new Game("Kjell Persson", 174), | |
Game2 = new Game("Peter Sjöberg", 182) | |
}, | |
new Table | |
{ | |
Score = 1, | |
Game1 = new Game("Mikael Axelsson", 214), | |
Game2 = new Game("Christer Liedholm", 176) | |
}, | |
new Table | |
{ | |
Score = 0, | |
Game1 = new Game("Lars Öberg", 168), | |
Game2 = new Game("Torbjörn Jensen", 199) | |
}, | |
new Table | |
{ | |
Score = 0, | |
Game1 = new Game("Kjell Jansson", 180), | |
Game2 = new Game("Hans Norbeck", 212) | |
} | |
}), | |
new Serie(new List<Table> | |
{ | |
new Table | |
{ | |
Score = 0, | |
Game1 = new Game("Kjell Jansson", 189), | |
Game2 = new Game("Hans Norbeck", 181) | |
}, | |
new Table | |
{ | |
Score = 0, | |
Game1 = new Game("Lars Öberg", 227), | |
Game2 = new Game("Torbjörn Jensen", 180) | |
}, | |
new Table | |
{ | |
Score = 1, | |
Game1 = new Game("Mikael Axelsson", 223), | |
Game2 = new Game("Christer Liedholm", 191) | |
}, | |
new Table | |
{ | |
Score = 0, | |
Game1 = new Game("Thomas Gurell", 159), | |
Game2 = new Game("Peter Sjöberg", 190) | |
} | |
}), | |
}; | |
var match = new Match( | |
location: "Sollentuna Bowlinghall", | |
date: new DateTime(2011, 03, 26), | |
bitsMatchId: 3003231, | |
homeTeam: new Team("Sollentuna Bwk", 13), | |
awayTeam: new Team("Fredrikshof IF", 6, series)); | |
return match; | |
} | |
} | |
public class Matches_PlayerStats : AbstractIndexCreationTask<Match, Matches_PlayerStats.Results> | |
{ | |
public Matches_PlayerStats() | |
{ | |
Map = matches => from match in matches | |
from team in match.Teams | |
from serie in team.Series | |
from table in serie.Tables | |
from game in table.Games | |
select new | |
{ | |
Player = game.Player, | |
TotalPins = game.Pins, | |
Count = 1, | |
Max = game.Pins, | |
Min = game.Pins | |
}; | |
Reduce = results => from result in results | |
group result by result.Player into stat | |
select new | |
{ | |
Player = stat.Key, | |
TotalPins = stat.Sum(s => s.TotalPins), | |
Count = stat.Sum(s => s.Count), | |
Max = stat.Max(s => s.Max), | |
Min = stat.Min(s => s.Min) | |
}; | |
} | |
public class Results | |
{ | |
public string Player { get; set; } | |
public int TotalPins { get; set; } | |
public int Count { get; set; } | |
public int Max { get; set; } | |
public int Min { get; set; } | |
} | |
} | |
public class Match | |
{ | |
[JsonProperty(PropertyName = "Teams")] | |
private List<Team> teams; | |
[JsonConstructor] | |
public Match( | |
string location, | |
DateTime date, | |
int bitsMatchId, | |
IEnumerable<Team> teams) | |
{ | |
Location = location; | |
Date = date; | |
BitsMatchId = bitsMatchId; | |
this.teams = new List<Team>(teams); | |
} | |
/// <summary> | |
/// Initializes a new instance of the Match class. | |
/// </summary> | |
/// <param name="location">Match location.</param> | |
/// <param name="date">Match date.</param> | |
/// <param name="bitsMatchId">BITS match id.</param> | |
/// <param name="homeTeam">Home team.</param> | |
/// <param name="awayTeam">Away team.</param> | |
public Match( | |
string location, | |
DateTime date, | |
int bitsMatchId, | |
Team homeTeam, | |
Team awayTeam) | |
{ | |
Location = location; | |
Date = date; | |
BitsMatchId = bitsMatchId; | |
teams = new List<Team> { homeTeam, awayTeam }; | |
} | |
/// <summary> | |
/// Gets or sets the match id. | |
/// </summary> | |
public int Id { get; set; } | |
/// <summary> | |
/// Gets or sets the match location. | |
/// </summary> | |
public string Location { get; set; } | |
/// <summary> | |
/// Gets or sets the match date. | |
/// </summary> | |
public DateTime Date { get; set; } | |
/// <summary> | |
/// Gets or sets the home team. | |
/// </summary> | |
[JsonIgnore] | |
public Team HomeTeam | |
{ | |
get { return teams[0]; } | |
set { teams[0] = value; } | |
} | |
/// <summary> | |
/// Gets or sets the away team. | |
/// </summary> | |
[JsonIgnore] | |
public Team AwayTeam | |
{ | |
get { return teams[1]; } | |
set { teams[1] = value; } | |
} | |
/// <summary> | |
/// Gets or sets the BITS match id. | |
/// </summary> | |
public int BitsMatchId { get; set; } | |
/// <summary> | |
/// Gets the teams. | |
/// </summary> | |
public IEnumerable<Team> Teams | |
{ | |
get { return teams; } | |
} | |
} | |
public class Team | |
{ | |
[JsonProperty(PropertyName = "Series")] | |
private List<Serie> series; | |
/// <summary> | |
/// Initializes a new instance of the Team class. | |
/// </summary> | |
/// <param name="name">Name of the team.</param> | |
/// <param name="score">Total score.</param> | |
public Team(string name, int score) | |
{ | |
Name = name; | |
Score = score; | |
series = new List<Serie>(); | |
} | |
/// <summary> | |
/// Initializes a new instance of the Team class. | |
/// </summary> | |
/// <param name="name">Name of the team.</param> | |
/// <param name="score">Total score.</param> | |
/// <param name="series">Series played by team.</param> | |
[JsonConstructor] | |
public Team(string name, int score, IEnumerable<Serie> series) | |
{ | |
Name = name; | |
Score = score; | |
this.series = new List<Serie>(series); | |
} | |
/// <summary> | |
/// Gets or sets the name. | |
/// </summary> | |
public string Name { get; set; } | |
/// <summary> | |
/// Gets or sets the total score. | |
/// </summary> | |
public int Score { get; set; } | |
/// <summary> | |
/// Gets the series. | |
/// </summary> | |
public IEnumerable<Serie> Series | |
{ | |
get { return series; } | |
} | |
/// <summary> | |
/// Returns the total pins. | |
/// </summary> | |
/// <returns>Total pins.</returns> | |
public int Pins() | |
{ | |
return Series.Sum(s => s.Pins()); | |
} | |
/// <summary> | |
/// Returns the score for a serie. | |
/// </summary> | |
/// <param name="serie">Serie index (1-based).</param> | |
/// <returns></returns> | |
public int ScoreFor(int serie) | |
{ | |
return Series.ElementAt(serie - 1).Score(); | |
} | |
/// <summary> | |
/// Returns the total pins for a serie. | |
/// </summary> | |
/// <param name="serie">Serie number (1-based).</param> | |
/// <returns>Total pins for the specified serie.</returns> | |
public int PinsFor(int serie) | |
{ | |
return Series.ElementAt(serie - 1).Pins(); | |
} | |
/// <summary> | |
/// Returns the total pins for a player. | |
/// </summary> | |
/// <param name="player">Player name.</param> | |
/// <returns>Total pins for player in all series.</returns> | |
public int PinsForPlayer(string player) | |
{ | |
return Series.Sum(s => s.PinsForPlayer(player)); | |
} | |
} | |
public class Serie | |
{ | |
[JsonProperty(PropertyName = "Tables")] | |
private List<Table> tables; | |
/// <summary> | |
/// Initializes a new instance of the Serie class. | |
/// </summary> | |
/// <param name="tables">Tables of the serie.</param> | |
public Serie(IEnumerable<Table> tables) | |
{ | |
this.tables = new List<Table>(tables); | |
} | |
/// <summary> | |
/// Gets the tables. | |
/// </summary> | |
public IEnumerable<Table> Tables | |
{ | |
get { return tables; } | |
} | |
/// <summary> | |
/// Returns the total pins for this serie. | |
/// </summary> | |
/// <returns>Total pins.</returns> | |
public int Pins() | |
{ | |
return Tables.Sum(t => t.Pins()); | |
} | |
/// <summary> | |
/// Gets the total score of this serie. | |
/// </summary> | |
/// <returns></returns> | |
public int Score() | |
{ | |
return Tables.Sum(t => t.Score); | |
} | |
/// <summary> | |
/// Returns the pins for a player. | |
/// </summary> | |
/// <param name="player">Player name.</param> | |
/// <returns>Pins for player.</returns> | |
public int PinsForPlayer(string player) | |
{ | |
return Tables.Sum(t => t.PinsForPlayer(player)); | |
} | |
} | |
public class Table | |
{ | |
[JsonProperty(PropertyName = "Games")] | |
private List<Game> games; | |
/// <summary> | |
/// Initializes a new instance of the Table class. | |
/// </summary> | |
public Table() | |
{ | |
games = new List<Game> | |
{ | |
new Game(string.Empty, 0), | |
new Game(string.Empty, 0) | |
}; | |
} | |
/// <summary> | |
/// Initializes a new instance of the Table class. | |
/// </summary> | |
/// <param name="games">Games in the table.</param> | |
[JsonConstructor] | |
public Table(IEnumerable<Game> games) | |
{ | |
this.games = new List<Game>(games); | |
} | |
/// <summary> | |
/// Gets or sets the score. | |
/// </summary> | |
public int Score { get; set; } | |
/// <summary> | |
/// Gets or sets the first game. | |
/// </summary> | |
[JsonIgnore] | |
public Game Game1 | |
{ | |
get { return games[0]; } | |
set { games[0] = value; } | |
} | |
/// <summary> | |
/// Gets or sets the second game. | |
/// </summary> | |
[JsonIgnore] | |
public Game Game2 | |
{ | |
get { return games[1]; } | |
set { games[1] = value; } | |
} | |
/// <summary> | |
/// Gets the games list. | |
/// </summary> | |
public IEnumerable<Game> Games | |
{ | |
get { return games; } | |
} | |
/// <summary> | |
/// Returns the total pins by both players. | |
/// </summary> | |
/// <returns>Total pins.</returns> | |
public int Pins() | |
{ | |
return Game1.Pins + Game2.Pins; | |
} | |
/// <summary> | |
/// Gets the pins for one of the players. | |
/// </summary> | |
/// <param name="player">Player name.</param> | |
/// <returns>Pins for player.</returns> | |
public int PinsForPlayer(string player) | |
{ | |
if (Game1.Player == player) | |
return Game1.Pins; | |
else if (Game2.Player == player) | |
return Game2.Pins; | |
else | |
return 0; | |
} | |
} | |
public class Game | |
{ | |
/// <summary> | |
/// Initializes a new instance of the Game class. | |
/// </summary> | |
/// <param name="player">Player name.</param> | |
/// <param name="pins">Pins of the game.</param> | |
public Game(string player, int pins) | |
{ | |
Player = player; | |
Pins = pins; | |
} | |
/// <summary> | |
/// Gets the player name. | |
/// </summary> | |
public string Player { get; private set; } | |
/// <summary> | |
/// Gets the pins. | |
/// </summary> | |
public int Pins { get; private set; } | |
/// <summary> | |
/// Gets the strikes. | |
/// </summary> | |
public int Strikes { get; private set; } | |
/// <summary> | |
/// Gets the misses. | |
/// </summary> | |
public int Misses { get; private set; } | |
/// <summary> | |
/// Gets the one-pin misses. | |
/// </summary> | |
public int OnePinMisses { get; private set; } | |
/// <summary> | |
/// Gets the splits. | |
/// </summary> | |
public int Splits { get; private set; } | |
/// <summary> | |
/// Gets a value indicating whether all frames were covered (no misses). | |
/// </summary> | |
public bool CoveredAll { get; private set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment