Skip to content

Instantly share code, notes, and snippets.

@jesuslpm
Created October 5, 2012 11:20
Show Gist options
  • Save jesuslpm/3839305 to your computer and use it in GitHub Desktop.
Save jesuslpm/3839305 to your computer and use it in GitHub Desktop.
Group by metadata in RavenDb
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raven.Database;
using Raven.Client.Document;
using Raven.Client.Embedded;
using Xunit;
using Raven.Client.Indexes;
namespace Raven.Tests
{
public class Use
{
public string Id { get; set; }
public long LastUsedTicks { get; set; }
public string UsageType { get; set; }
}
public class UseA : Use
{
}
public class UseB : Use
{
}
public class UsesStats
{
public long UsageCount { get; set; }
public long LastUsed { get; set; }
public string UsageType { get; set; }
}
public class Uses_Stats : AbstractIndexCreationTask<Use, UsesStats>
{
public Uses_Stats()
{
Map = uses => from use in uses
select new
{
UsageCount = 1,
LastUsed = use.LastUsedTicks,
UsageType = MetadataFor(use)["Raven-Clr-Type"]
};
Reduce = results => from result in results
group result by result.UsageType
into g
select new
{
UsageCount = (int)g.Sum(x => x.UsageCount),
LastUsed = g.Max(x => x.LastUsed),
UsageType = g.Key
};
}
}
public class StatsByMetadata : IDisposable
{
DocumentStore Store;
public StatsByMetadata()
{
Store = new EmbeddableDocumentStore { RunInMemory = true };
var defaultBehavior = Store.Conventions.FindTypeTagName;
Store.Conventions.FindTypeTagName = type =>
{
if (typeof(Use).IsAssignableFrom(type))
{
return defaultBehavior(typeof(Use));
}
return defaultBehavior(type);
};
Store.Initialize();
}
[Fact]
public void StatsByMetadataWorks()
{
new Uses_Stats().Execute(this.Store);
using (var session = Store.OpenSession())
{
var stats1 = new UseA { Id = "Stats/1", LastUsedTicks = DateTime.UtcNow.Ticks };
var stats2 = new UseB { Id = "Stats/2", LastUsedTicks = DateTime.UtcNow.Ticks };
var stats3 = new UseA { Id = "Stats/3", LastUsedTicks = DateTime.UtcNow.Ticks };
var stats4 = new UseB { Id = "Stats/4", LastUsedTicks = DateTime.UtcNow.Ticks };
session.Store(stats1);
session.Store(stats2);
session.Store(stats3);
session.Store(stats4);
session.SaveChanges();
var metadata = session.Advanced.GetMetadataFor(stats1);
}
using (var session = Store.OpenSession())
{
var stats = session.Query<UsesStats, Uses_Stats>()
.Customize(x => x.WaitForNonStaleResultsAsOfNow())
.ToList();
stats = session.Query<UsesStats, Uses_Stats>()
.Customize(x => x.WaitForNonStaleResultsAsOfNow())
.OrderBy(x => x.UsageType)
.ToList();
Assert.Equal(2, stats.Count);
Assert.Equal(2, stats[0].UsageCount);
Assert.True( stats[0].UsageType.StartsWith("Raven.Tests.UseA,"));
Assert.Equal(2, stats[1].UsageCount);
Assert.True(stats[1].UsageType.StartsWith("Raven.Tests.UseB,"));
var statistics = session.Advanced.DocumentStore.DatabaseCommands.GetStatistics();
}
}
public void Dispose()
{
if (Store != null) Store.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment