Last active
December 11, 2015 06:08
-
-
Save khalidabuhakmeh/4557101 to your computer and use it in GitHub Desktop.
Loading documents from RavenDB Sharding without knowing what shard it is currently in.
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 Raven.Client; | |
using Raven.Client.Document; | |
using Raven.Client.Shard; | |
namespace ShardingExample | |
{ | |
class Program | |
{ | |
static readonly IDictionary<string, IDocumentStore> Shards = new Dictionary<string, IDocumentStore> { | |
{Company.Regions.Asia, new DocumentStore {Url = "http://localhost:8080", DefaultDatabase = "Sharding" }}, | |
{Company.Regions.MiddleEast, new DocumentStore {Url = "http://localhost:8081", DefaultDatabase = "Sharding"}}, | |
{Company.Regions.USA, new DocumentStore { Url = "http://localhost:8081", DefaultDatabase = "Sharding"}} | |
}; | |
private static readonly ShardStrategy ShardStrategy = new ShardStrategy(Shards) | |
.ShardingOn<Company>(company => company.Region); | |
private static readonly IDocumentStore Store = new ShardedDocumentStore(ShardStrategy).Initialize(); | |
static void Main(string[] args) | |
{ | |
using (var session = Store.OpenSession()) | |
{ | |
var company = new Company { Name = "FedEx", Region = Company.Regions.Asia }; | |
var id = company.Id; | |
session.Store(company); | |
session.Store(new Company { Name = "UPS", Region = Company.Regions.MiddleEast }); | |
session.Store(new Company { Name = "Nike", Region = Company.Regions.USA }); | |
session.SaveChanges(); | |
var reloaded = session.LoadAny<Company>(id); | |
if (reloaded == null) | |
{ | |
Console.WriteLine("Woops!"); | |
} | |
} | |
Console.ReadLine(); | |
} | |
public class Company | |
{ | |
public Company() | |
{ | |
Id = Guid.NewGuid().ToString(); | |
} | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public string Region { get; set; } | |
public static class Regions | |
{ | |
public const string Asia = "Asia"; | |
public const string MiddleEast = "Middle East"; | |
public const string USA = "USA"; | |
} | |
} | |
} | |
public static class IDocumentSessionExtensions | |
{ | |
public static T LoadAny<T>(this IDocumentSession session, string id) | |
where T: class | |
{ | |
if (session == null) | |
return default(T); | |
var sharded = session as ShardedDocumentSession; | |
if (sharded == null) | |
return session.Load<T>(id); | |
var store = ((ShardedDocumentStore)sharded.DocumentStore); | |
var keys = store.ShardStrategy.Shards.Keys; | |
var seperator = store.Conventions.IdentityPartsSeparator; | |
foreach (var key in keys) | |
{ | |
// we could parallelize these loads, since they are inexpesive | |
var result = session.Load<T>(string.Join(seperator, key, id)); | |
if (result != null) | |
return result; | |
} | |
return default(T); // not found | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment