OpenSession() public method

Opens the session.
public OpenSession ( ) : IDocumentSession
return IDocumentSession
示例#1
0
		public Index()
		{
			#region store
			var shards = new Dictionary<string, IDocumentStore>
			             	{
			             		{"Asia", new DocumentStore {Url = "http://localhost:8080"}},
			             		{"Middle East", new DocumentStore {Url = "http://localhost:8081"}},
			             		{"America", new DocumentStore {Url = "http://localhost:8082"}},
			             	};

			var shardStrategy = new ShardStrategy(shards)
				.ShardingOn<Company>(company => company.Region)
				.ShardingOn<Invoice>(x => x.CompanyId);

			var documentStore = new ShardedDocumentStore(shardStrategy).Initialize();

			#endregion

			#region SaveEntities
			using (var session = documentStore.OpenSession())
			{
				var asian = new Company { Name = "Company 1", Region = "Asia" };
				session.Store(asian);
				var middleEastern = new Company { Name = "Company 2", Region = "Middle-East" };
				session.Store(middleEastern);
				var american = new Company { Name = "Company 3", Region = "America" };
				session.Store(american);

				session.Store(new Invoice { CompanyId = american.Id, Amount = 3, IssuedAt = DateTime.Today.AddDays(-1) });
				session.Store(new Invoice { CompanyId = asian.Id, Amount = 5, IssuedAt = DateTime.Today.AddDays(-1) });
				session.Store(new Invoice { CompanyId = middleEastern.Id, Amount = 12, IssuedAt = DateTime.Today });
				session.SaveChanges();
			}

			#endregion

			#region Query
			using (var session = documentStore.OpenSession())
			{
				//get all, should automagically retrieve from each shard
				var allCompanies = session.Query<Company>()
					.Customize(x => x.WaitForNonStaleResultsAsOfNow())
					.Where(company => company.Region == "Asia")
					.ToArray();

				foreach (var company in allCompanies)
					Console.WriteLine(company.Name);
			}

			#endregion

			documentStore.Dispose();
		}
        static void Main(string[] args)
        {
            var shards = new Dictionary<string, IDocumentStore>
                {
                    { "one", new DocumentStore { Url = "http://localhost:8079", } },
                    { "two", new DocumentStore { Url = "http://localhost:8078", } },
                };

            var shardStrategy = new ShardStrategy(shards)
                .ShardingOn<User>()
                .ShardingOn<Story>(x => x.UserId);

            using (var store = new ShardedDocumentStore(shardStrategy).Initialize())
            {
                //using (var session = store.OpenSession())
                //{
                //	var user = new User { Name = "Ayende" };
                //	session.Store(user);
                //	session.Store(new Story { UserId = user.Id });
                //	session.SaveChanges();
                //}

                using (var session = store.OpenSession())
                {
                    var load = session.Query<Story>()
                        .Where(x => x.UserId == "two/users/1")
                        .ToList();

                    Console.WriteLine(load[0].UserId);
                }
            }
        }
示例#3
0
		public void CanIgnoreParallel()
		{
			using (GetNewServer())
			{
				var shardingStrategy = new ShardStrategy(new Dictionary<string, IDocumentStore>
				{
					{"one", new DocumentStore {Url = "http://localhost:8079"}},
					{"two", new DocumentStore {Url = "http://localhost:8078"}},
				})
				{
					ShardAccessStrategy = new ParallelShardAccessStrategy()
				};
				shardingStrategy.ShardAccessStrategy.OnError += (commands, request, exception) => request.Query != null;

				using (var docStore = new ShardedDocumentStore(shardingStrategy).Initialize())
				{
					using (var session = docStore.OpenSession())
					{
						session.Query<AccurateCount.User>()
							.ToList();
					}
				}

			}
		}
示例#4
0
		public void CanDisableQueryResultsTrackingForShardedDocumentQuery()
		{

			using (GetNewServer(8079))
			using (GetNewServer(8078))
			using (var store = new ShardedDocumentStore(new ShardStrategy(new Dictionary<string, IDocumentStore>
			{
				{"1", CreateDocumentStore(8079)},
				{"2", CreateDocumentStore(8078)},
			})))
			{
				store.Initialize();

				using (var session = store.OpenSession())
				{
					session.Store(new Item());
					session.Store(new Item());

					session.SaveChanges();
				}

				using (var session = store.OpenSession())
				{
					var items = session.Query<Item>().Customize(x => x.NoTracking().WaitForNonStaleResults()).ToList();

					Assert.Equal(2, items.Count);
					Assert.Equal(0, ((InMemoryDocumentSessionOperations) session).NumberOfEntitiesInUnitOfWork);
				}
			}
		}
示例#5
0
        private static void Main()
        {
            var shards = new Dictionary<string, IDocumentStore>
            {
                {"_", new DocumentStore {Url = "http://localhost:8080", DefaultDatabase = "Shop"}}, //existing data
                {"ME", new DocumentStore {Url = "http://localhost:8080", DefaultDatabase = "Shop_ME"}},
                {"US", new DocumentStore {Url = "http://localhost:8080", DefaultDatabase = "Shop_US"}},
            };

            var shardStrategy = new ShardStrategy(shards)
                .ShardingOn<Customer>(c => c.Region)
                .ShardingOn<Invoice>(i => i.Customer);

            var x = new ShardedDocumentStore(shardStrategy).Initialize();
            using (var s = x.OpenSession())
            {
                var customer = new Customer
                {
                    Region = "US"
                };
                s.Store(customer);
                s.Store(new Invoice
                {
                    Customer = customer.Id
                });
                s.SaveChanges();
            }
        }
示例#6
0
文件: Sharding.cs 项目: kprog/docs
        public Sharding()
        {
            #region intro
            var shards = new Dictionary<string, IDocumentStore>
                         	{
                         		{"Asia", new DocumentStore {Url = "http://localhost:8080"}},
                         		{"Middle East", new DocumentStore {Url = "http://localhost:8081"}},
                         		{"America", new DocumentStore {Url = "http://localhost:8082"}},
                         	};

            var shardStrategy = new ShardStrategy
                                    {
                                        ShardAccessStrategy = new ParallelShardAccessStrategy(),
                                        ShardResolutionStrategy = new ShardResolutionByRegion(),
                                    };

            using (var documentStore = new ShardedDocumentStore(shardStrategy, shards).Initialize())
            using (var session = documentStore.OpenSession())
            {
                //store 3 items in the 3 shards
                session.Store(new Company {Name = "Company 1", Region = "Asia"});
                session.Store(new Company {Name = "Company 2", Region = "Middle East"});
                session.Store(new Company {Name = "Company 3", Region = "America"});
                session.SaveChanges();

                //get all, should automagically retrieve from each shard
                var allCompanies = session.Query<Company>()
                    .Customize(x => x.WaitForNonStaleResultsAsOfNow()).ToArray();

                foreach (var company in allCompanies)
                    Console.WriteLine(company.Name);
            }
            #endregion
        }
		static void Main()
		{
			var shards = new Dictionary<string, IDocumentStore>
			             	{
			             		{"one", new DocumentStore {Url = "http://localhost:8079"}},
			             		{"two", new DocumentStore {Url = "http://localhost:8078"}},
			             		{"three", new DocumentStore {Url = "http://localhost:8077"}},
			             	};

			var shardStrategy = new ShardStrategy(shards)
				.ShardingOn<Company>()
				.ShardingOn<Invoice>(x => x.CompanyId);

			using (var documentStore = new ShardedDocumentStore(shardStrategy).Initialize())
			{
				new InvoicesAmountByDate().Execute(documentStore);

				using (var session = documentStore.OpenSession())
				{
					var asian = new Company { Name = "Company 1" };
					session.Store(asian);
					var middleEastern = new Company { Name = "Company 2" };
					session.Store(middleEastern);
					var american = new Company { Name = "Company 3" };
					session.Store(american);

					session.Store(new Invoice { CompanyId = american.Id, Amount = 3, IssuedAt = DateTime.Today.AddDays(-1) });
					session.Store(new Invoice { CompanyId = asian.Id, Amount = 5, IssuedAt = DateTime.Today.AddDays(-1) });
					session.Store(new Invoice { CompanyId = middleEastern.Id, Amount = 12, IssuedAt = DateTime.Today });
					session.SaveChanges();
				}


				using (var session = documentStore.OpenSession())
				{
					var reduceResults = session.Query<InvoicesAmountByDate.ReduceResult, InvoicesAmountByDate>()
						.ToList();

					foreach (var reduceResult in reduceResults)
					{
						string dateStr = reduceResult.IssuedAt.ToString("MMM dd, yyyy", CultureInfo.InvariantCulture);
						Console.WriteLine("{0}: {1}", dateStr, reduceResult.Amount);
					}
					Console.WriteLine();
				}
			}
		}
示例#8
0
		public void get_metadata_for_sharded()
		{
			var server1 = GetNewServer(8079);
			var server2 = GetNewServer(8078);
			var shards = new List<IDocumentStore>
			{
				new DocumentStore {Identifier = "Shard1", Url = server1.Configuration.ServerUrl},
				new DocumentStore {Identifier = "Shard2", Url = server2.Configuration.ServerUrl}
			}
				.ToDictionary(x => x.Identifier, x => x);

			var shardStrategy = new ShardStrategy(shards);
			shardStrategy.ShardingOn<Profile>(x => x.Location);

			using (var shardedDocumentStore = new ShardedDocumentStore(shardStrategy))
			{
				shardedDocumentStore.Initialize();

				var profile = new Profile {Name = "Test", Location = "Shard1"};
				var profile2 = new Profile {Name = "Test2", Location = "Shard2"};

				using (var documentSession = shardedDocumentStore.OpenSession())
				{
					documentSession.Store(profile, profile.Id);
					documentSession.Store(profile2, profile2.Id);
					documentSession.SaveChanges();
				}
				using (var documentSession = shardedDocumentStore.OpenSession())
				{
					var correctId = profile.Id;
					var correctId2 = profile2.Id;

					documentSession.Store(profile, profile.Id);
					var metaData = documentSession.Advanced.GetMetadataFor(profile);
					var metaData2 = documentSession.Advanced.GetMetadataFor(profile2);

					Assert.NotNull(metaData);
					Assert.NotNull(metaData2);
					Assert.Equal(correctId, profile.Id);
				}
			}
		}
        public void OverwritingExistingDocumentGeneratesWrongIdWithShardedDocumentStore()
        {
            using (var store1 = NewRemoteDocumentStoreWithUrl(8079, ravenDbServer: GetNewServer(8079)))
            {
                using (var store2 = NewRemoteDocumentStoreWithUrl(8078, ravenDbServer: GetNewServer(8078)))
                {
                    var shards = new List<IDocumentStore> { 
                        new DocumentStore { Identifier="Shard1", Url = store1.Url}, 
                        new DocumentStore { Identifier="Shard2", Url = store2.Url} }
                            .ToDictionary(x => x.Identifier, x => x);

                    var shardStrategy = new ShardStrategy(shards);
                    shardStrategy.ShardingOn<Profile>(x => x.Location);

                    using (var shardedDocumentStore = new ShardedDocumentStore(shardStrategy))
                    {
                        shardedDocumentStore.Initialize();

                        var profile = new Profile { Name = "Test", Location = "Shard1" };

                        using (var documentSession = shardedDocumentStore.OpenSession())
                        {
                            documentSession.Store(profile, profile.Id);
                            documentSession.SaveChanges();
                        }

                        using (var documentSession = shardedDocumentStore.OpenSession())
                        {
                            var correctId = profile.Id;

                            documentSession.Store(profile, profile.Id);

                            Assert.Equal(correctId, profile.Id);
                        }
                    }
                }
            }
        }
        public void Can_insert_into_two_sharded_servers()
        {
            using (var documentStore = new ShardedDocumentStore(shardStrategy, shards))
            {
                documentStore.Initialize();

                using (var session = documentStore.OpenSession())
                {
                	session.Store(company1);
					session.Store(company2);
					session.SaveChanges();
                }
            }
        }
示例#11
0
        public void OverwritingExistingDocumentGeneratesWrongIdWithShardedDocumentStore()
        {
			var server1 = GetNewServer(8079);
			var server2 = GetNewServer(8078);
			var shards = new Dictionary<string, IDocumentStore>
			{
				{"Shard1", new DocumentStore{Url = server1.Configuration.ServerUrl}},
				{"Shard2", new DocumentStore{Url = server2.Configuration.ServerUrl}},
			};



	        var shardStrategy = new ShardStrategy(shards);
	        shardStrategy.ShardingOn<Profile>(x => x.Location);

	        using (var shardedDocumentStore = new ShardedDocumentStore(shardStrategy))
	        {
		        shardedDocumentStore.Initialize();

		        var profile = new Profile {Name = "Test", Location = "Shard1"};

		        using (var documentSession = shardedDocumentStore.OpenSession())
		        {
			        documentSession.Store(profile, profile.Id);
			        documentSession.SaveChanges();
		        }

		        using (var documentSession = shardedDocumentStore.OpenSession())
		        {
			        var correctId = profile.Id;

			        documentSession.Store(profile, profile.Id);

			        Assert.Equal(correctId, profile.Id);
		        }
	        }
        }
示例#12
0
		static void Main()
		{
			var shards = new Shards
			             	{
			             		new DocumentStore {Url = "http://localhost:8080", Identifier = "Posts"},
			             		new DocumentStore
			             			{
			             				Url = "http://localhost:8081",
			             				Identifier = "Users",
			             				Conventions = {DocumentKeyGenerator = user => "users/" + ((User) user).Name}
			             			}
			             	};

			var shardStrategy = new ShardStrategy
			{
				ShardAccessStrategy = new ParallelShardAccessStrategy(),
				ShardSelectionStrategy = new BlogShardSelectionStrategy(),
				ShardResolutionStrategy = new BlogShardResolutionStrategy()
			};

			using (var documentStore = new ShardedDocumentStore(shardStrategy, shards).Initialize())
			{
				using (var session = documentStore.OpenSession())
				{
					var user = new User { Name = "PastyGeek" };
					session.Store(user);
					session.SaveChanges();

					var post = new Post
					{
						AuthorId = user.Id,
						Name = user.Name,
						BlogId = "blogs/1",
						Title = "More CodeMash Gloating!",
						Body = "You wouldn't believe how much more fun I'm having than you!",
						PostDate = DateTime.Now,
						Tags = new List<string> { "codemash", "gloating" }
					};
					session.Store(post);
					session.SaveChanges();
				}
			}
		}
示例#13
0
		public async Task get_metadata_for_async_sharded()
		{
			var server1 = GetNewServer(8079);
			var server2 = GetNewServer(8078);
			var shards = new Dictionary<string, IDocumentStore>
			{
				{"Shard1", new DocumentStore{Url = server1.Configuration.ServerUrl}},
				{"Shard2", new DocumentStore{Url = server2.Configuration.ServerUrl}},
			};

			var shardStrategy = new ShardStrategy(shards);
			shardStrategy.ShardingOn<Profile>(x => x.Location);

			using (var shardedDocumentStore = new ShardedDocumentStore(shardStrategy))
			{
				shardedDocumentStore.Initialize();

				var profile = new Profile {Name = "Test", Location = "Shard1"};
				var profile2 = new Profile {Name = "Test2", Location = "Shard2"};

				using (var documentSession = shardedDocumentStore.OpenSession())
				{
					documentSession.Store(profile, profile.Id);
					documentSession.Store(profile2, profile2.Id);
					documentSession.SaveChanges();
				}

				using (var documentSession = shardedDocumentStore.OpenSession())
				{
					var metaData = documentSession.Advanced.GetMetadataFor(profile);
				}
				using (var documentSession = shardedDocumentStore.OpenAsyncSession())
				{
					//var data = await documentSession.LoadAsync<Profile>(profile.Id);
					var metaData = await documentSession.Advanced.GetMetadataForAsync(profile);
					var metaData2 = await documentSession.Advanced.GetMetadataForAsync(profile2);

					Assert.NotNull(metaData);
					Assert.NotNull(metaData2);
				}
			}
		}
        public void Can_insert_into_two_sharded_servers()
        {
            var serverPortsStoredUpon = new List<string>();

            using (var documentStore = new ShardedDocumentStore(shardStrategy, shards))
            {
                documentStore.Stored += (storeServer, storeEntity) => serverPortsStoredUpon.Add(storeServer);
                documentStore.Initialize();

                using (var session = documentStore.OpenSession())
                {
                	session.Store(company1);
					session.Store(company2);
					session.SaveChanges();
                }
            }

			Assert.Contains("Shard1", serverPortsStoredUpon[0]);
			Assert.Contains("Shard2", serverPortsStoredUpon[1]);
        }
		public void CanOverrideTheShardIdGeneration()
		{
			using (var documentStore = new ShardedDocumentStore(shardStrategy))
			{
				documentStore.Initialize();

				foreach (var shard in shards)
				{
					shard.Value.Conventions.DocumentKeyGenerator = c => ((Company)c).Name;
				}

				using (var session = documentStore.OpenSession())
				{
					session.Store(company1);
					session.Store(company2);

					Assert.Equal("Shard1/companies/1", company1.Id);
					Assert.Equal("Shard2/companies/2", company2.Id);
				}
			}
		}
示例#16
0
        static void Main(string[] args)
        {
            var shards = new Shards {
                new DocumentStore("localhost", 8080) { Identifier="Shard1" },
                new DocumentStore("localhost", 8081) { Identifier="Shard2" }
            };

            using (var documentStore = new ShardedDocumentStore(new ShardStrategy(), shards).Initialise())
            using (var session = documentStore.OpenSession())
            {
                //store 2 items in the 2 shards
                session.Store(new Company { Name = "Company 1", Region = "A" });
                session.Store(new Company { Name = "Company 2", Region = "B" });
                session.SaveChanges();

                //get all, should automagically retrieve from each shard
                var allCompanies = session.Query<Company>().WaitForNonStaleResults().ToArray();

                foreach (var company in allCompanies)
                    Console.WriteLine(company.Name);
            }
        }
示例#17
0
		public void CanIgnore_Lazy()
		{
			using (GetNewServer())
			{
				var shardingStrategy = new ShardStrategy(new Dictionary<string, IDocumentStore>
				{
					{"one", new DocumentStore {Url = "http://localhost:8079"}},
					{"two", new DocumentStore {Url = "http://localhost:8078"}},
				});
				shardingStrategy.ShardAccessStrategy.OnError += (commands, request, exception) => true;

				using (var docStore = new ShardedDocumentStore(shardingStrategy).Initialize())
				{
					using (var session = docStore.OpenSession())
					{
						var lazily = session.Query<AccurateCount.User>().Lazily();
						GC.KeepAlive(lazily.Value);
					}
				}

			}
		}
示例#18
0
        static void Main()
        {
            var shards = new Shards
            {
                CreateShard("Asia", "http://localhost:8080"),
                CreateShard("Middle-East", "http://localhost:8081"),
            };

            using (var documentStore = new ShardedDocumentStore(new ShardStrategy(), shards).Initialize())
            using (var session = documentStore.OpenSession())
            {
                //store 2 items in the 2 shards
                session.Store(new Company { Name = "Company 1", Region = "Asia" });
                session.Store(new Company { Name = "Company 2", Region = "Middle East" });
                session.SaveChanges();

                //get all, should automagically retrieve from each shard
                var allCompanies = session.LuceneQuery<Company>().WaitForNonStaleResults().ToArray();

                foreach (var company in allCompanies)
                    Console.WriteLine(company.Name);
            }
        }
示例#19
0
        public void CanGetAllShardedEntities()
        {
            //get them in simple single threaded sequence for this test
            shardStrategy.ShardAccessStrategy = new SequentialShardAccessStrategy();

            using (var documentStore = new ShardedDocumentStore(shardStrategy).Initialize())
            using (var session = documentStore.OpenSession())
            {
                //store 2 items in 2 shards
                session.Store(company1);
                session.Store(company2);

                session.SaveChanges();

                //get all, should automagically retrieve from each shard
                var allCompanies = session.Advanced.DocumentQuery<Company>()
                    .WaitForNonStaleResults()
                    .ToArray();

                Assert.NotNull(allCompanies);
                Assert.Equal(company1.Name, allCompanies[0].Name);
                Assert.Equal(company2.Name, allCompanies[1].Name);
            }
        }
		public void CanInsertIntoTwoShardedServers()
		{
			using (var documentStore = new ShardedDocumentStore(shardStrategy))
			{
				documentStore.Initialize();

				using (var session = documentStore.OpenSession())
				{
					session.Store(company1);
					session.Store(company2);
					session.SaveChanges();
				}
			}
		}
示例#21
0
		static void Main()
		{
			var consoleAppender = new ConsoleAppender
			{
				Layout = new SimpleLayout(),
			};
			consoleAppender.AddFilter(new LoggerMatchFilter
			{
				AcceptOnMatch = true,
				LoggerToMatch = "Raven.Client"
			});
			consoleAppender.AddFilter(new DenyAllFilter());
			BasicConfigurator.Configure(consoleAppender);

			// start 5 instances of Raven's servers
			Console.WriteLine("Starting...");
			DeleteDirectories("Users", "Blogs", "Posts.1", "Posts.2", "Posts.3");
			var ravenDbServers = StartServers();
			Console.WriteLine("All servers started...");

			var shards = new Shards
			{
				new DocumentStore
				{
					Identifier = "Users",
					Url = "http://*****:*****@ Rahien" };

				session.Store(user);
				session.Store(blog);

				// we have to save to Raven to get the generated id for the blog instance
				session.SaveChanges();
				var posts = new List<Post>();
				for (var i = 0; i < 6; i++)
				{
					var post = new Post
					{
						BlogId = blog.Id,
						UserId = user.Id,
						Content = "Just a post",
						Title = "Post #" + (i + 1)
					};
					posts.Add(post);
					session.Store(post);
				}

				session.SaveChanges();
			}

			// queries
			using (var session = documentStore.OpenSession())
			{
				session.LuceneQuery<User>().WaitForNonStaleResults().ToArray();
				session.LuceneQuery<Blog>().WaitForNonStaleResults().ToArray();
				session.LuceneQuery<Post>().WaitForNonStaleResults().ToArray();
			}

			// loading
			using (var session = documentStore.OpenSession())
			{
				session.Load<User>("users/ayende");
				session.Load<Blog>("blogs/1");
				session.Load<Post>("posts/1/2");
				session.Load<Post>("posts/2/2");
			}

			documentStore.Dispose();

			foreach (var server in ravenDbServers)
			{
				server.Dispose();
			}
		}
示例#22
0
        public void ToFacetsDoesntWorkWithShardedDocumentSession()
        {
            using (var store1 = NewRemoteDocumentStoreWithUrl(8079, ravenDbServer: GetNewServer(8079)))
            {
                using (var store2 = NewRemoteDocumentStoreWithUrl(8078, ravenDbServer: GetNewServer(8078)))
                {
                    var shards = new List<IDocumentStore> { 
                        new DocumentStore { Identifier="Shard1", Url = store1.Url}, 
                        new DocumentStore { Identifier="Shard2", Url = store2.Url} }
                            .ToDictionary(x => x.Identifier, x => x);

                    var shardStrategy = new ShardStrategy(shards);
                    shardStrategy.ShardResolutionStrategy = new HybridShardingResolutionStrategy(shards.Keys, shardStrategy, new Type[0], "Shard1");
                    shardStrategy.ShardingOn<Profile>(x => x.Location);

                    using (var shardedDocumentStore = new ShardedDocumentStore(shardStrategy))
                    {
                        shardedDocumentStore.Initialize();
                        new ProfileIndex().Execute(shardedDocumentStore);
                        /*var facetSetup = new FacetSetup
                        {
                            Id = "facets/ProfileFacet",
                            Facets = new List<Facet>
                            {
                                new Facet {Name = "Name", Mode = FacetMode.Default}
                            }
                        };*/
                        var facets = new List<Facet>
                        {
                            new Facet {Name = "Name", Mode = FacetMode.Default}
                        };
                        var profile = new Profile { Name = "Test", Location = "Shard1" };

                        using (var documentSession = shardedDocumentStore.OpenSession())
                        {
                            documentSession.Store(profile, profile.Id);
                            //documentSession.Store(facetSetup);
                            documentSession.SaveChanges();
                        }
                        using (var documentSession = shardedDocumentStore.OpenSession())
                        {
                            var query = documentSession.Query<Profile>("ProfileIndex").Where(x => x.Name == "Test");
                            var res = query.ToFacets(facets);
                            Assert.Equal(1,res.Results.Count);
                        }
                    }
                }
            }
        }
示例#23
0
        public void CanUseShardedDocumentStore()
        {
            const string shard1Name = "Asia";
            const string shard2Name = "Middle East";
            const string shard3Name = "America";

            using (var shard1 = GetDocumentStore())
            using (var shard2 = GetDocumentStore())
            using (var shard3 = GetDocumentStore())
            {
                var shards = new Dictionary<string, IDocumentStore>
                {
                    {shard1Name, shard1},
                    {shard2Name, shard2},
                    {shard3Name, shard3}
                };

                var shardStrategy = new ShardStrategy(shards)
                    .ShardingOn<Company>(company => company.Name, result =>
                    {
                        if (ReferenceEquals(result, null))
                            throw new InvalidOperationException("Should not be null.");

                        char firstCharacter = result.ToString().First();
                        if (char.ToLower(firstCharacter) == 'a')
                        {
                            return shard1Name;
                        }
                        else if (char.ToLower(firstCharacter) == 'b')
                        {
                            return shard2Name;
                        }
                        else
                        {
                            return shard3Name;
                        }
                    });

                using (var documentStore = new ShardedDocumentStore(shardStrategy).Initialize())
                {
                    using (var session = documentStore.OpenSession())
                    {
                        session.Store(new Company { Name = "A corporation" });
                        session.Store(new Company { Name = "alimited" });
                        session.Store(new Company { Name = "B corporation" });
                        session.Store(new Company { Name = "blimited" });
                        session.Store(new Company { Name = "Z corporation" });
                        session.Store(new Company { Name = "rlimited" });
                        session.SaveChanges();

                        var companies = session.Query<Company>()
                            .ToArray();

                        Assert.Equal(6, companies.Length);
                        Assert.Equal(shard1Name + "/companies/1", companies[0].Id);
                        Assert.Equal(shard1Name + "/companies/2", companies[1].Id);
                        Assert.Equal(shard2Name + "/companies/3", companies[2].Id);
                        Assert.Equal(shard2Name + "/companies/4", companies[3].Id);
                        Assert.Equal(shard3Name + "/companies/5", companies[4].Id);
                        Assert.Equal(shard3Name + "/companies/6", companies[5].Id);

                        Assert.Throws<InvalidOperationException>(() =>
                            {
                                session.Store(new Company { });
                                session.SaveChanges();
                            });
                    }
                }
            }
        }
		public void Can_override_the_shard_id_generation()
		{
			using (var documentStore = new ShardedDocumentStore(shardStrategy, shards))
			{
				documentStore.Initialize();

				foreach (var shard in shards)
				{
					var s = shard;
					shard.Conventions.DocumentKeyGenerator = c => ((Company) c).Name;
				}

				using (var session = documentStore.OpenSession())
				{
					session.Store(company1);
					session.Store(company2);

					Assert.Equal("Company1", company1.Id);
					Assert.Equal("Company2", company2.Id);
				}
			}
		}
        public void Can_insert_into_two_sharded_servers()
        {
            var serverPortsStoredUpon = new List<int>();

            using (var documentStore = new ShardedDocumentStore(shardStrategy, shards))
            {
                documentStore.Stored += (storeServer, storePort, storeEntity) => serverPortsStoredUpon.Add(storePort);
                documentStore.Initialise();

                using (var session = documentStore.OpenSession())
                {
                    session.Store(company1);
                    session.Store(company2);
                    session.SaveChanges();
                }
            }

            Assert.Equal(port1, serverPortsStoredUpon[0]);
            Assert.Equal(port2, serverPortsStoredUpon[1]);
        }
		public void CanGetSingleEntityFromCorrectShardedServer()
		{
			using (var documentStore = new ShardedDocumentStore(shardStrategy).Initialize())
			using (var session = documentStore.OpenSession())
			{
				//store item that goes in 2nd shard
				session.Store(company2);
				session.SaveChanges();

				//get it, should automagically retrieve from 2nd shard
				shardResolution.Stub(x => x.PotentialShardsFor(null)).IgnoreArguments().Return(new[] { "Shard2" });
				var loadedCompany = session.Load<Company>(company2.Id);

				Assert.NotNull(loadedCompany);
				Assert.Equal(company2.Name, loadedCompany.Name);
			}
		}
		public void CanGetSingleEntityFromCorrectShardedServerWhenLocationIsUnknown()
		{
			shardStrategy.ShardAccessStrategy = new SequentialShardAccessStrategy();

			using (var documentStore = new ShardedDocumentStore(shardStrategy).Initialize())
			using (var session = documentStore.OpenSession())
			{
				//store item that goes in 2nd shard
				session.Store(company2);

				session.SaveChanges();

				//get it, should try all shards and find it
				shardResolution.Stub(x => x.PotentialShardsFor(null)).IgnoreArguments().Return(null);
				var loadedCompany = session.Load<Company>(company2.Id);

				Assert.NotNull(loadedCompany);
				Assert.Equal(company2.Name, loadedCompany.Name);
			}
		}
		public void Can_query_using_int()
		{
			shardStrategy.Stub(x => x.ShardAccessStrategy).Return(new SequentialShardAccessStrategy());
			using (var documentStore = new ShardedDocumentStore(shardStrategy, shards))
			{
				documentStore.Initialize();

				using (var session = documentStore.OpenSession())
				{
					session.Load<Company>(1);
				}
			}
		}
		public void CanQueryUsingInt()
		{
			shardStrategy.ShardAccessStrategy = new SequentialShardAccessStrategy();
			using (var documentStore = new ShardedDocumentStore(shardStrategy))
			{
				documentStore.Initialize();

				using (var session = documentStore.OpenSession())
				{
					session.Load<Company>(1);
				}
			}
		}
		public void Can_get_single_entity_from_correct_sharded_server_when_location_is_unknown()
		{
			shardStrategy.Stub(x => x.ShardAccessStrategy).Return(new SequentialShardAccessStrategy());
			
			using (var documentStore = new ShardedDocumentStore(shardStrategy, shards).Initialize())
			using (var session = documentStore.OpenSession())
			{
				//store item that goes in 2nd shard
				session.Store(company2);

				session.SaveChanges();

				//get it, should try all shards and find it
				shardResolution.Stub(x => x.SelectShardIds(null)).IgnoreArguments().Return(null);
				var loadedCompany = session.Load<Company>(company2.Id);

				Assert.NotNull(loadedCompany);
				Assert.Equal(company2.Name, loadedCompany.Name);
			}
		}