Permalink
Browse files

Do not use the store identifier for the shardId, have a dictionary ke…

…y for that instead
  • Loading branch information...
1 parent 834eae9 commit 032ac57218666bc65d5b5a2839d17cdf5f43659a @fitzchak fitzchak committed Mar 5, 2012
@@ -122,7 +122,6 @@ public IDocumentStore DocumentStore
/// <summary>
/// Gets the store identifier for this session.
/// The store identifier is the identifier for the particular RavenDB instance.
- /// This is mostly useful when using sharding.
/// </summary>
/// <value>The store identifier.</value>
public string StoreIdentifier
@@ -31,7 +31,6 @@ public interface IAdvancedDocumentSessionOperations
/// <summary>
/// Gets the store identifier for this session.
/// The store identifier is the identifier for the particular RavenDB instance.
- /// This is mostly useful when using sharding.
/// </summary>
/// <value>The store identifier.</value>
string StoreIdentifier { get; }
@@ -26,7 +26,7 @@ namespace Raven.Client.Shard
/// </summary>
public class ShardedDocumentStore : DocumentStoreBase
{
- private readonly List<IDocumentStore> shards;
+ private readonly IDictionary<string, IDocumentStore> shards;
#if !SILVERLIGHT
/// <summary>
@@ -56,15 +56,15 @@ public override HttpJsonRequestFactory JsonRequestFactory
/// </summary>
/// <param name="shardStrategy">The shard strategy.</param>
/// <param name="shards">The shards.</param>
- public ShardedDocumentStore(ShardStrategy shardStrategy, List<IDocumentStore> shards)
+ public ShardedDocumentStore(ShardStrategy shardStrategy, IDictionary<string, IDocumentStore> shards)
{
if (shards == null || shards.Count == 0)
throw new ArgumentException("Must have one or more shards", "shards");
if (shardStrategy == null)
throw new ArgumentException("Must have shard strategy", "shardStrategy");
this.ShardStrategy = shardStrategy;
- this.shards = shards;
+ this.shards = new Dictionary<string, IDocumentStore>(shards, StringComparer.InvariantCultureIgnoreCase);
}
/// <summary>
@@ -85,15 +85,13 @@ public ShardedDocumentStore(ShardStrategy shardStrategy, List<IDocumentStore> sh
/// </summary>
public override void Dispose()
{
- foreach (var shard in shards)
- shard.Dispose();
+ shards.ForEach(shard => shard.Value.Dispose());
WasDisposed = true;
var afterDispose = AfterDispose;
if (afterDispose != null)
afterDispose(this, EventArgs.Empty);
-
}
#endregion
@@ -141,7 +139,7 @@ public override IAsyncDocumentSession OpenAsyncSession(string databaseName)
/// </remarks>
public override IDisposable AggressivelyCacheFor(TimeSpan cacheDuration)
{
- var disposables = shards.Select(shard => shard.AggressivelyCacheFor(cacheDuration)).ToList();
+ var disposables = shards.Select(shard => shard.Value.AggressivelyCacheFor(cacheDuration)).ToList();
return new DisposableAction(() =>
{
@@ -162,7 +160,7 @@ public override IDisposable AggressivelyCacheFor(TimeSpan cacheDuration)
/// </remarks>
public override IDisposable DisableAggressiveCaching()
{
- var disposables = shards.Select(shard => shard.DisableAggressiveCaching()).ToList();
+ var disposables = shards.Select(shard => shard.Value.DisableAggressiveCaching()).ToList();
return new DisposableAction(() =>
{
@@ -181,23 +179,23 @@ public override IDisposable DisableAggressiveCaching()
/// <returns></returns>
public override IDocumentSession OpenSession()
{
- return OpenSessionInternal(shards.ToDictionary(x => x.Identifier, x => x.DatabaseCommands));
+ return OpenSessionInternal(shards.ToDictionary(x => x.Key, x => x.Value.DatabaseCommands));
}
/// <summary>
/// Opens the session for a particular database
/// </summary>
public override IDocumentSession OpenSession(string database)
{
- return OpenSessionInternal(shards.ToDictionary(x => x.Identifier, x => x.DatabaseCommands.ForDatabase(database)));
+ return OpenSessionInternal(shards.ToDictionary(x => x.Key, x => x.Value.DatabaseCommands.ForDatabase(database)));
}
/// <summary>
/// Opens the session for a particular database with the specified credentials
/// </summary>
public override IDocumentSession OpenSession(string database, ICredentials credentialsForSession)
{
- return OpenSessionInternal(shards.ToDictionary(x => x.Identifier, x => x.DatabaseCommands.ForDatabase(database).With(credentialsForSession)));
+ return OpenSessionInternal(shards.ToDictionary(x => x.Key, x => x.Value.DatabaseCommands.ForDatabase(database).With(credentialsForSession)));
}
/// <summary>
@@ -206,7 +204,7 @@ public override IDocumentSession OpenSession(string database, ICredentials crede
/// <param name="credentialsForSession">The credentials for session.</param>
public override IDocumentSession OpenSession(ICredentials credentialsForSession)
{
- return OpenSessionInternal(shards.ToDictionary(x => x.Identifier, x => x.DatabaseCommands.With(credentialsForSession)));
+ return OpenSessionInternal(shards.ToDictionary(x => x.Key, x => x.Value.DatabaseCommands.With(credentialsForSession)));
}
private IDocumentSession OpenSessionInternal(Dictionary<string, IDatabaseCommands> shardDbCommands)
@@ -256,7 +254,7 @@ public override IDocumentStore Initialize()
{
try
{
- shards.ForEach(shard => shard.Initialize());
+ shards.ForEach(shard => shard.Value.Initialize());
if (Conventions.DocumentKeyGenerator == null)// don't overwrite what the user is doing
{
var generator = new ShardedHiloKeyGenerator(this, 32);
@@ -274,8 +272,8 @@ public override IDocumentStore Initialize()
public IDatabaseCommands DatabaseCommandsFor(string shardId)
{
- var store = shards.FirstOrDefault(x => x.Identifier == shardId);
- if(store == null)
+ IDocumentStore store;
+ if (shards.TryGetValue(shardId, out store) == false)
throw new InvalidOperationException("Could not find a shard named: " + shardId);
return store.DatabaseCommands;
@@ -13,7 +13,7 @@ public class ShardedDocumentStoreTest
public void WillThrowIsThereIsNoShards()
{
Assert.Throws<ArgumentException>(() => new ShardedDocumentStore(new ShardStrategy(), null));
- Assert.Throws<ArgumentException>(() => new ShardedDocumentStore(new ShardStrategy(), new List<IDocumentStore>()));
+ Assert.Throws<ArgumentException>(() => new ShardedDocumentStore(new ShardStrategy(), new Dictionary<string, IDocumentStore>()));
}
[Fact]
@@ -29,11 +29,10 @@ public void AssertInitialized()
private static ShardedDocumentStore GetDocumentStore()
{
- var shards = new List<IDocumentStore>
- {
- new DocumentStore {Url = "http://localhost:8079/"}
- };
- return new ShardedDocumentStore(new ShardStrategy(), shards);
+ return new ShardedDocumentStore(new ShardStrategy(), new Dictionary<string, IDocumentStore>
+ {
+ {"default", new DocumentStore {Url = "http://localhost:8079/"}}
+ });
}
[Fact]
@@ -29,7 +29,7 @@ public class WhenUsingShardedServers : RemoteClientTest, IDisposable
readonly RavenDbServer server2;
readonly Company company1;
readonly Company company2;
- readonly List<IDocumentStore> shards;
+ readonly IDictionary<string, IDocumentStore> shards;
readonly IShardResolutionStrategy shardResolution;
readonly ShardStrategy shardStrategy;
@@ -55,7 +55,7 @@ public WhenUsingShardedServers()
shards = new List<IDocumentStore> {
new DocumentStore { Identifier="Shard1", Url = "http://" + server +":"+port1},
new DocumentStore { Identifier="Shard2", Url = "http://" + server +":"+port2}
- };
+ }.ToDictionary(x => x.Identifier, x => x);
shardResolution = MockRepository.GenerateStub<IShardResolutionStrategy>();
shardResolution.Stub(x => x.GenerateShardIdFor(company1)).Return("Shard1");
@@ -64,7 +64,7 @@ public WhenUsingShardedServers()
shardResolution.Stub(x => x.MetadataShardIdFor(company1)).Return("Shard1");
shardResolution.Stub(x => x.MetadataShardIdFor(company2)).Return("Shard1");
- shardStrategy = new ShardStrategy {ShardResolutionStrategy = shardResolution};
+ shardStrategy = new ShardStrategy { ShardResolutionStrategy = shardResolution };
}
[Fact]
@@ -76,8 +76,7 @@ public void CanOverrideTheShardIdGeneration()
foreach (var shard in shards)
{
- var s = shard;
- shard.Conventions.DocumentKeyGenerator = c => ((Company) c).Name;
+ shard.Value.Conventions.DocumentKeyGenerator = c => ((Company)c).Name;
}
using (var session = documentStore.OpenSession())
@@ -145,7 +144,7 @@ public void CanGetSingleEntityFromCorrectShardedServer()
public void CanGetSingleEntityFromCorrectShardedServerWhenLocationIsUnknown()
{
shardStrategy.ShardAccessStrategy = new SequentialShardAccessStrategy();
-
+
using (var documentStore = new ShardedDocumentStore(shardStrategy, shards).Initialize())
using (var session = documentStore.OpenSession())
{
@@ -178,7 +177,7 @@ public void CanGetAllShardedEntities()
session.SaveChanges();
-
+
//get all, should automagically retrieve from each shard
var allCompanies = session.Advanced.LuceneQuery<Company>()
.WaitForNonStaleResults()

0 comments on commit 032ac57

Please sign in to comment.