Inheritance: IDocumentStore
 public AsyncShardedDocumentSession(string dbName, ShardedDocumentStore documentStore, DocumentSessionListeners listeners, Guid id,
                                    ShardStrategy shardStrategy, IDictionary <string, IAsyncDatabaseCommands> shardDbCommands)
     : base(dbName, documentStore, listeners, id, shardStrategy, shardDbCommands)
 {
     GenerateDocumentKeysOnStore = false;
     asyncDocumentKeyGeneration  = new AsyncDocumentKeyGeneration(this, entitiesAndMetadata.TryGetValue, ModifyObjectId);
 }
Example #2
0
        public void CanIgnoreMultiShard()
        {
            using (var server1 = GetNewServer(8079))
            using (var server2 = GetNewServer(8078))
            using (var server3 = GetNewServer(8077))
            using (var server4 = server3)
            {
                Dictionary<string, IDocumentStore> shards = new Dictionary<string, IDocumentStore>
                {
                    {"Eastern", server1.DocumentStore},
                    {"Western", server2.DocumentStore},
                    {"Northern", server3.DocumentStore},
                    {"Southern", server4.DocumentStore},
                };

                ShardStrategy shardStrategy = new ShardStrategy(shards)
                    .ShardingOn<Region2>(r => r.Name)//, name => (name == "Northern" || name == "Southern") ? "NorthSouth" : name)
                    .ShardingOn<TerritoryOf>(x => x.RegionId);

                IDocumentStore store = new ShardedDocumentStore(shardStrategy);
                NotSupportedException notSuppotedEx = null;
                try
                {
                    store.Initialize();
                }
                catch (Exception ex)
                {

                    notSuppotedEx = ex as NotSupportedException;
                }

                Assert.NotNull(notSuppotedEx);
                Assert.Contains("Multiple keys in shard dictionary for", notSuppotedEx.Message);
            }
        }
Example #3
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();
            }
        }
 protected BaseShardedDocumentSession(string dbName, ShardedDocumentStore documentStore, DocumentSessionListeners listeners, Guid id,
                                      ShardStrategy shardStrategy, IDictionary <string, TDatabaseCommands> shardDbCommands)
     : base(dbName, documentStore, listeners, id)
 {
     this.shardStrategy   = shardStrategy;
     this.shardDbCommands = shardDbCommands;
 }
Example #5
0
		public RoundRobinSharding()
		{
			servers = new Dictionary<string, RavenDbServer>
			{
				{"one",GetNewServer(8078)},
				{"two", GetNewServer(8077)},
				{"tri", GetNewServer(8076)}
			};

			var documentStores = new Dictionary<string, IDocumentStore>
			                            {
				                            {"one", new DocumentStore{Url = "http://localhost:8078"}},
				                            {"two", new DocumentStore{Url = "http://localhost:8077"}},
				                            {"tri", new DocumentStore{Url = "http://localhost:8076"}},
			                            };

			foreach (var documentStore in documentStores)
			{
				documentStore.Value.Conventions.FailoverBehavior = FailoverBehavior.FailImmediately;
			}


			var shardStrategy = new ShardStrategy(documentStores)
				.ShardingOn<Post>()
				.ShardingOn<PostComments>(x => x.PostId);

			store = new ShardedDocumentStore(shardStrategy);
			store.Initialize();
		}
Example #6
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();
					}
				}

			}
		}
Example #7
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())
			{
				var posts = shards[0].DatabaseCommands.StartsWith("post", 0, 50);
				foreach (var post in posts) shards[0].DatabaseCommands.Delete(post.Key, post.Etag);

				var users = shards[1].DatabaseCommands.StartsWith("user", 0, 50);
				foreach (var user in users) shards[1].DatabaseCommands.Delete(user.Key, user.Etag);
			}
		}
Example #8
0
        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
        }
Example #9
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);
				}
			}
		}
        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);
                }
            }
        }
Example #11
0
        private static void Main()
        {
            //set up
            const int createUserCount = 30;
            Shards shards = ShardsConfiguration.SetupShards();
            IEnumerable<User> users = TestDataGenerator.GenerateUsers(createUserCount);
            var shardStore = new ShardedDocumentStore(new UserShardStrategy(), shards);

            //main logic
            using (IDocumentStore store = shardStore.Initialize())
            {
                //Based on user id, save users into the three shards
                var repo = new UserRepository(store);
                repo.SaveAll(users);

                //load all from database
                //this will query all three shards
                IEnumerable<User> persistedUsers = repo.FindAll();
                ConsoleOutput.PrintUsersInfo(persistedUsers);
                Console.WriteLine("===================================================");

                //pick a random user
                int id = rnd.Next(1, createUserCount);
                //query all shards for specific user
                //user location in a shard is resolved automatically
                User user = repo.FindUserById(id);
                ConsoleOutput.PrintTweetsPostedBy(user);
                Console.WriteLine("===================================================");
            }
            Console.ReadLine();
        }
Example #12
0
		protected ShardingScenario()
		{
			RavenDbServer users = null;
			RavenDbServer blogs = null;
			RavenDbServer posts1 = null;
			RavenDbServer posts2 = null;
			RavenDbServer posts3 = null;
			try
			{
				users = GetNewServer(8079, "shard1");
				blogs = GetNewServer(8078, "shard2");
				posts1 = GetNewServer(8077, "shard3");
				posts2 = GetNewServer(8076, "shard4");
				posts3 = GetNewServer(8075, "shard5");
			}
			catch (Exception)
			{
				if (users != null)
					users.Dispose();
				if (blogs != null)
					blogs.Dispose();
				if (posts1 != null)
					posts1.Dispose();
				if (posts2 != null)
					posts2.Dispose();
				if (posts3 != null)
					posts3.Dispose();
				throw;
			}

			Servers = new Dictionary<string, RavenDbServer>
			{
				{"Users", users},
				{"Blogs", blogs},
				{"Posts01", posts1},
				{"Posts02", posts2},
				{"Posts03", posts3}
			};

			var shards = new List<IDocumentStore>
			             	{
			             		new DocumentStore {Identifier = "Users", Url = "http://localhost:8079"},
			             		new DocumentStore {Identifier = "Blogs", Url = "http://localhost:8078"},
			             		new DocumentStore {Identifier = "Posts01", Url = "http://localhost:8077"},
			             		new DocumentStore {Identifier = "Posts02", Url = "http://localhost:8076"},
			             		new DocumentStore {Identifier = "Posts03", Url = "http://localhost:8075"}
			             	}.ToDictionary(x => x.Identifier, x => x);

			foreach (var shard in shards)
			{
				shard.Value.Conventions.FailoverBehavior = FailoverBehavior.FailImmediately;
			}

			ShardedDocumentStore = new ShardedDocumentStore(new ShardStrategy(shards)
															{
																ShardAccessStrategy = new SequentialShardAccessStrategy(),
																ShardResolutionStrategy = new BlogShardResolutionStrategy(3),
															});
			ShardedDocumentStore = (ShardedDocumentStore) ShardedDocumentStore.Initialize();
		}
        /// <summary>
        /// Initializes a new instance of the <see cref="ShardedDocumentSession"/> class.
        /// </summary>
        /// <param name="shardStrategy">The shard strategy.</param>
        /// <param name="shardDbCommands">The shard IDatabaseCommands.</param>
        /// <param name="id"></param>
        /// <param name="documentStore"></param>
        /// <param name="listeners"></param>
        public ShardedDocumentSession(ShardedDocumentStore documentStore, DocumentSessionListeners listeners, Guid id,
                                      ShardStrategy shardStrategy, IDictionary <string, IDatabaseCommands> shardDbCommands

                                      )
            : base(documentStore, listeners, id)
        {
            this.shardStrategy   = shardStrategy;
            this.shardDbCommands = shardDbCommands;
        }
Example #14
0
        public void bulk_insert_sharded(string databaseName1, string databaseName2)
        {
            var server1 = GetNewServer(8079);
            var server2 = GetNewServer(8078);
            var shards = new Dictionary<string, IDocumentStore>
			{
				{"Shard1", new DocumentStore {Url = server1.Configuration.ServerUrl, DefaultDatabase = databaseName1}},
				{"Shard2", new DocumentStore {Url = server2.Configuration.ServerUrl, DefaultDatabase = databaseName2}}
			};
            var shardStrategy = new ShardStrategy(shards);
            shardStrategy.ShardingOn<Profile>(x => x.Location);

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

                var entity1 = new Profile {Id = "bulk1", Name = "Hila", Location = "Shard1"};
                var entity2 = new Profile {Name = "Jay", Location = "Shard2"};
                var entity3 = new Profile {Name = "Jay", Location = "Shard1"};
                using (var bulkInsert = shardedDocumentStore.ShardedBulkInsert())
                {
                    bulkInsert.Store(entity1);
                    bulkInsert.Store(entity2);
                    bulkInsert.Store(entity3);
                }
            }

            using (var store1 = new DocumentStore { Url = server1.SystemDatabase.Configuration.ServerUrl, DefaultDatabase = databaseName1 }.Initialize())
            {
                using (var session = store1.OpenSession())
                {
                    var docs = session.Load<Profile>("Shard1/bulk1");
                    Assert.Equal("Shard1", docs.Location);
                    var docs2 = session.Load<Profile>("Shard1/profiles/2");
                    Assert.Equal("Shard1", docs2.Location);

                    var totalDocs = session.Query<Profile>()
                        .Customize(x => x.WaitForNonStaleResults())
                        .ToList();
                    Assert.Equal(2, totalDocs.Count);
                }
            }
            using (var store2 = new DocumentStore { Url = server2.SystemDatabase.Configuration.ServerUrl, DefaultDatabase = databaseName2 }.Initialize())
            {
                using (var session = store2.OpenSession())
                {
                    var docs = session.Load<Profile>("Shard2/profiles/1");
                    Assert.Equal("Shard2", docs.Location);

                    var totalDocs = session.Query<Profile>()
                        .Customize(x => x.WaitForNonStaleResults())
                        .ToList();
                    Assert.Equal(1, totalDocs.Count);
                }
            }
        }
 public ShardedBulkInsertOperation(string database, ShardedDocumentStore shardedDocumentStore, BulkInsertOptions options)
 {
     this.database = database;
     this.shardedDocumentStore = shardedDocumentStore;
     this.options = options;
     shards = shardedDocumentStore.ShardStrategy.Shards;
     Bulks = new Dictionary<string, BulkInsertOperation>();
     generateEntityIdOnTheClient = new GenerateEntityIdOnTheClient(shardedDocumentStore.Conventions,
         entity => AsyncHelpers.RunSync(() => shardedDocumentStore.Conventions.GenerateDocumentKeyAsync(database, DatabaseCommands, entity)));
     shardResolutionStrategy = shardedDocumentStore.ShardStrategy.ShardResolutionStrategy;
     shardStrategy = this.shardedDocumentStore.ShardStrategy;
 }
Example #16
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();
		}
		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_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();
                }
            }
        }
Example #19
0
        public void ToFacetsDoesntWorkWithShardedDocumentSession()
        {
            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.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);
                }
            }
        }
Example #20
0
        public ShardedSessionInclude()
        {
            servers = new[]
            {
                GetNewServer(8079,requestedStorage: "esent"),
                GetNewServer(8080, requestedStorage: "esent")
            };

            documentStore = CreateDocumentStore(8080);
            shardedDocumentStore = new ShardedDocumentStore(new ShardStrategy(new Dictionary<string, IDocumentStore>
            {
                {"1", CreateDocumentStore(8079)}
            }));
            shardedDocumentStore.Initialize();
            documentStore.Initialize();
        }
		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();
				}
			}
		}
Example #22
0
		public SimpleSharding()
		{
			servers = new[]
			{
				GetNewServer(8079),
				GetNewServer(8078),
				GetNewServer(8077),
			};

			documentStore = new ShardedDocumentStore(new ShardStrategy(new Dictionary<string, IDocumentStore>
			{
				{"1", CreateDocumentStore(8079)},
				{"2", CreateDocumentStore(8078)},
				{"3", CreateDocumentStore(8077)}
			}));
			documentStore.Initialize();
		}
Example #23
0
		public RavenDB_579()
		{
			servers = new[]
			{
				GetNewServer(8079),
				GetNewServer(8078),
				GetNewServer(8077),
			};

			documentStore = new ShardedDocumentStore(new ShardStrategy(new Dictionary<string, IDocumentStore>
			{
				{shardNames[0], CreateDocumentStore(8079)},
				{shardNames[1], CreateDocumentStore(8078)},
				{shardNames[2], CreateDocumentStore(8077)}
			}));
			documentStore.Initialize();
		}
Example #24
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();
				}
			}
		}
Example #25
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);
				}
			}
		}
Example #26
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 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]);
        }
Example #28
0
        public async Task DeleteItemOnAsyncShardedDocumentSession()
        {
            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 documentSessionAsync = shardedDocumentStore.OpenAsyncSession())
                {
                    await documentSessionAsync.StoreAsync(profile, profile.Id);
                    await documentSessionAsync.StoreAsync(profile2, profile2.Id);
                    await documentSessionAsync.SaveChangesAsync();

                    documentSessionAsync.Delete(profile);
                    await documentSessionAsync.SaveChangesAsync();

                    var doc = documentSessionAsync.LoadAsync<Profile>(profile.Id);
                    Assert.Null(await doc);

                }

                using (var documentSessionAsync = shardedDocumentStore.OpenAsyncSession())
                {
                    Assert.Null(await documentSessionAsync.LoadAsync<Profile>(profile.Id));

                }
            }
        }
        static IDocumentStore CreateStore()
        {
            var s1 = new DocumentStore()
            {
                Url = "http://localhost:8080",
                DefaultDatabase = "S1"
            }.Initialize();

            var s2 = new DocumentStore()
            {
                Url = "http://localhost:8081",
                DefaultDatabase = "S2"
            }.Initialize();

            var strategy = new ShardStrategy( new Dictionary<String, IDocumentStore>()
            {
                {"S1", s1},
                {"S2", s2}
            } );

            strategy.ShardingOn<Order>( o => o.Country, c =>
            {
                if ( c.Equals( "italy", StringComparison.OrdinalIgnoreCase ) )
                {
                    return "S1";
                }

                return "S2";
            } );

            strategy.ShardingOn<Person>();

            var store = new ShardedDocumentStore( strategy )
            {

            }.Initialize();

            IndexCreation.CreateIndexes( Assembly.GetExecutingAssembly(), store );

            return store;
        }
		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);
				}
			}
		}
Example #31
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);
            }
        }
		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);
				}
			}
		}
Example #33
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);
					}
				}

			}
		}
Example #34
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShardedDocumentSession"/> class.
 /// </summary>
 /// <param name="shardStrategy">The shard strategy.</param>
 /// <param name="shardDbCommands">The shard IDatabaseCommands.</param>
 /// <param name="id"></param>
 /// <param name="documentStore"></param>
 /// <param name="listeners"></param>
 public ShardedDocumentSession(string dbName, ShardedDocumentStore documentStore, DocumentSessionListeners listeners, Guid id,
                               ShardStrategy shardStrategy, IDictionary <string, IDatabaseCommands> shardDbCommands)
     : base(dbName, documentStore, listeners, id, shardStrategy, shardDbCommands)
 {
 }
Example #35
0
 public ShardedBulkInsertOperation ShardedBulkInsert(string database = null, ShardedDocumentStore store = null)
 {
     return(new ShardedBulkInsertOperation(database, this));
 }
Example #36
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ShardedDocumentSession"/> class.
 /// </summary>
 /// <param name="shardStrategy">The shard strategy.</param>
 /// <param name="shardSessions">The shard sessions.</param>
 /// <param name="documentStore"></param>
 public ShardedDocumentSession(IShardStrategy shardStrategy, IDocumentSession[] shardSessions, ShardedDocumentStore documentStore)
 {
     this.shardStrategy = shardStrategy;
     this.shardSessions = shardSessions;
     this.documentStore = documentStore;
 }
Example #37
0
 public ShardedHiloKeyGenerator(ShardedDocumentStore shardedDocumentStore, int capacity)
 {
     this.shardedDocumentStore = shardedDocumentStore;
     this.capacity             = capacity;
 }
Example #38
0
 public ShardedBulkInsertOperation ShardedBulkInsert(string database = null, ShardedDocumentStore store = null, BulkInsertOptions options = null)
 {
     return(new ShardedBulkInsertOperation(database, this, options ?? new BulkInsertOptions()));
 }