Skip to content

Instantly share code, notes, and snippets.

@jesuslpm
Created October 5, 2012 11:18
Show Gist options
  • Save jesuslpm/3839301 to your computer and use it in GitHub Desktop.
Save jesuslpm/3839301 to your computer and use it in GitHub Desktop.
Metadata filtering in multimap reduce index
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 MetadataFilteringTest
{
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_Stats1 : AbstractMultiMapIndexCreationTask<UsesStats>
{
public Uses_Stats1()
{
AddMap<Use>(uses => from use in uses
where MetadataFor(use)["Raven-Clr-Type"].ToString().StartsWith("MetadataFilteringTest.UseA,")
select new
{
UsageCount = 1,
LastUsed = use.LastUsedTicks,
UsageType = "a"
});
AddMap<Use>(uses => from use in uses
where this.MetadataFor(use)["Raven-Clr-Type"].ToString().StartsWith("MetadataFilteringTest.UseB,")
select new
{
UsageCount = 1,
LastUsed = use.LastUsedTicks,
UsageType = "b"
});
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 MetadataFiltering : IDisposable
{
DocumentStore Store;
public MetadataFiltering()
{
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 MetadataFilteringWorks()
{
new Uses_Stats1().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_Stats1>()
.Customize(x => x.WaitForNonStaleResultsAsOfNow())
.ToList();
stats = session.Query<UsesStats, Uses_Stats1>()
.Customize(x => x.WaitForNonStaleResultsAsOfNow())
.OrderBy( x => x.UsageType)
.ToList();
Assert.Equal(2, stats.Count);
Assert.Equal(2, stats[0].UsageCount);
Assert.Equal("a", stats[0].UsageType);
Assert.Equal(2, stats[1].UsageCount);
Assert.Equal("b", stats[1].UsageType);
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