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(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); } } }
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(); } } } }
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(); }
public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy) { var result = new FacetResults(); foreach (var response in responses.Select(response => (RavenJObject)response.Result)) { var facet = response.JsonDeserialization<FacetResults>(); foreach (var facetResult in facet.Results) { if (!result.Results.ContainsKey(facetResult.Key)) result.Results[facetResult.Key] = new FacetResult(); var newFacetResult = result.Results[facetResult.Key]; foreach (var facetValue in facetResult.Value.Values) { var existingFacetValueRange = newFacetResult.Values.Find((x) => x.Range == facetValue.Range); if (existingFacetValueRange != null) existingFacetValueRange.Hits += facetValue.Hits; else newFacetResult.Values.Add(new FacetValue() { Hits = facetValue.Hits, Range = facetValue.Range }); } foreach (var facetTerm in facetResult.Value.RemainingTerms) { if (!newFacetResult.RemainingTerms.Contains(facetTerm)) newFacetResult.RemainingTerms.Add(facetTerm); } } } Result = result; }
public HybridShardingResolutionStrategy(IEnumerable<string> shardIds, ShardStrategy shardStrategy, IEnumerable<Type> sharedTypes, string defaultShard) : base(shardIds, shardStrategy) { this.sharedTypes = new HashSet<Type>(sharedTypes); this.defaultShard = defaultShard; }
/// <summary> /// Initializes a new instance of the <see cref="ShardedDocumentStore"/> class. /// </summary> /// <param name="shardStrategy">The shard strategy.</param> public ShardedDocumentStore(ShardStrategy shardStrategy) { if (shardStrategy == null) throw new ArgumentException("Must have shard strategy", "shardStrategy"); this.ShardStrategy = shardStrategy; }
public WhenUsingShardedServers() { const string server = "localhost"; const int port1 = 8079; const int port2 = 8081; company1 = new Company { Name = "Company1" }; company2 = new Company { Name = "Company2" }; server1 = GetNewServer(port1); server2 = GetNewServer(port2); 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(Arg.Is(company1), Arg<ITransactionalDocumentSession>.Is.Anything)).Return("Shard1"); shardResolution.Stub(x => x.GenerateShardIdFor(Arg.Is(company2), Arg<ITransactionalDocumentSession>.Is.Anything)).Return("Shard2"); shardResolution.Stub(x => x.MetadataShardIdFor(company1)).Return("Shard1"); shardResolution.Stub(x => x.MetadataShardIdFor(company2)).Return("Shard1"); shardStrategy = new ShardStrategy(shards) { ShardResolutionStrategy = shardResolution }; }
public WhenUsingShardedServers() { const string server = "localhost"; const int port1 = 8079; const int port2 = 8081; path1 = GetPath("TestShardedDb1"); path2 = GetPath("TestShardedDb2"); NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(port1); NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(port2); company1 = new Company { Name = "Company1" }; company2 = new Company { Name = "Company2" }; server1 = GetNewServer(port1, path1); server2 = GetNewServer(port2, path2); 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"); shardResolution.Stub(x => x.GenerateShardIdFor(company2)).Return("Shard2"); shardResolution.Stub(x => x.MetadataShardIdFor(company1)).Return("Shard1"); shardResolution.Stub(x => x.MetadataShardIdFor(company2)).Return("Shard1"); shardStrategy = new ShardStrategy(shards) { ShardResolutionStrategy = shardResolution }; }
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(); } }
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); } }
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); } } }
static void Main(string[] args) { var shard1 = new DocumentStore { Identifier = "Shard1", Url = "http://localhost:8080" }.Initialize(); var shard2 = new DocumentStore { Identifier = "Shard2", Url = "http://localhost:8081" }.Initialize(); var shards = new Shards { shard1, shard2 }; var shardStrategy = new ShardStrategy { ShardAccessStrategy = new SequentialShardAccessStrategy(), ShardResolutionStrategy = new MyShardsResolutionStrategy(shards), ShardSelectionStrategy = new MyShardSelectionStrategy(shards) }; var documentStore = new ShardedDocumentStore(shardStrategy, shards).Initialize(); for (int i = 0; i < 3; i++) { using (var s = documentStore.OpenSession()) { var user = new User { Name = "Ayende" }; s.Store(user); s.Store(new ReadingList { UserId = user.Id, Books = new List <ReadingList.ReadBook>() }); s.SaveChanges(); } } using (var session = documentStore.OpenSession()) { var load = session.Load <User>("users/Shard2/33"); } using (var session = documentStore.OpenSession()) { var load = session.Advanced.LuceneQuery <ReadingList>().ToList(); } }
/// <summary> /// Initializes a new instance of the <see cref="ShardedDocumentQuery{T}"/> class. /// </summary> public AsyncShardedDocumentQuery(InMemoryDocumentSessionOperations session, Func <ShardRequestData, IList <Tuple <string, IAsyncDatabaseCommands> > > getShardsToOperateOn, ShardStrategy shardStrategy, string indexName, string[] fieldsToFetch, string[] projectionFields, IDocumentQueryListener[] queryListeners) : base(session #if !SILVERLIGHT , null #endif , null, indexName, fieldsToFetch, projectionFields, queryListeners) { this.getShardsToOperateOn = getShardsToOperateOn; this.shardStrategy = shardStrategy; }
public async Task Foo() { #region sharding_1 var shards = new Dictionary <string, IAsyncFilesCommands> { { "europe", new AsyncFilesServerClient("http://localhost:8080", "NorthwindFS") }, { "asia", new AsyncFilesServerClient("http://localhost:8081", "NorthwindFS") }, }; var shardStrategy = new ShardStrategy(shards) { /* * ShardAccessStrategy = ... * ShardResolutionStrategy = ... * ModifyFileName = ... */ }; var shardedCommands = new AsyncShardedFilesServerClient(shardStrategy); #endregion #region file_operations string fileName = await shardedCommands.UploadAsync("test.bin", new RavenJObject() { { "Owner", "Admin" } }, new MemoryStream()); // will return either /europe/test.bin or /asia/test.bin name // you need to pass the returned file name here to let the client know on which shard the file exists using (var content = await shardedCommands.DownloadAsync(fileName)) { } string renamed = await shardedCommands.RenameAsync(fileName, "new.bin"); await shardedCommands.DeleteAsync(renamed); #endregion #region search_browse_operations FileHeader[] fileHeaders = await shardedCommands.BrowseAsync(); SearchResults searchResults = await shardedCommands.SearchAsync("__fileName:test*"); #endregion #region custom_shard_res_strategy_2 var strategy = new ShardStrategy(shards); strategy.ShardResolutionStrategy = new RegionMetadataBasedResolutionStrategy(shards.Keys.ToList(), strategy.ModifyFileName, strategy.Conventions); var client = new AsyncShardedFilesServerClient(strategy); #endregion }
public async Task Foo() { #region sharding_1 var shards = new Dictionary<string, IAsyncFilesCommands> { {"europe", new AsyncFilesServerClient("http://localhost:8080", "NorthwindFS")}, {"asia", new AsyncFilesServerClient("http://localhost:8081", "NorthwindFS")}, }; var shardStrategy = new ShardStrategy(shards) { /* ShardAccessStrategy = ... ShardResolutionStrategy = ... ModifyFileName = ... */ }; var shardedCommands = new AsyncShardedFilesServerClient(shardStrategy); #endregion #region file_operations string fileName = await shardedCommands.UploadAsync("test.bin", new RavenJObject() { { "Owner", "Admin" } }, new MemoryStream()); // will return either /europe/test.bin or /asia/test.bin name // you need to pass the returned file name here to let the client know on which shard the file exists using (var content = await shardedCommands.DownloadAsync(fileName)) { } string renamed = await shardedCommands.RenameAsync(fileName, "new.bin"); await shardedCommands.DeleteAsync(renamed); #endregion #region search_browse_operations FileHeader[] fileHeaders = await shardedCommands.BrowseAsync(); SearchResults searchResults = await shardedCommands.SearchAsync("__fileName:test*"); #endregion #region custom_shard_res_strategy_2 var strategy = new ShardStrategy(shards); strategy.ShardResolutionStrategy = new RegionMetadataBasedResolutionStrategy(shards.Keys.ToList(), strategy.ModifyFileName, strategy.Conventions); var client = new AsyncShardedFilesServerClient(strategy); #endregion }
public async Task Test() { 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 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.SaveChanges(); } using (var documentSession = shardedDocumentStore.OpenAsyncSession()) { var query = documentSession.Query <Profile>("ProfileIndex").Where(x => x.Name == "Test"); var res = await query.ToFacetsAsync(facets); Assert.Equal(1, res.Results.Count); } } } } }
public ShardedBulkInsertOperation(string database, ShardedDocumentStore shardedDocumentStore) { this.database = database; this.shardedDocumentStore = shardedDocumentStore; 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; }
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; }
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 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 HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy) { var result = new SuggestionQueryResult { Suggestions = responses .Select(item => RavenJObject.Parse(item.Result)) .SelectMany(data => ((RavenJArray)data["Suggestions"]).Select(x => x.Value <string>())) .Distinct() .ToArray() }; Result = result; }
/// <summary> /// Initializes a new instance of the <see cref="ShardedDocumentQuery{T}"/> class. /// </summary> public ShardedDocumentQuery(InMemoryDocumentSessionOperations session, Func <ShardRequestData, IList <Tuple <string, IDatabaseCommands> > > getShardsToOperateOn, ShardStrategy shardStrategy, string indexName, string[] fieldsToFetch, string[] projectionFields, IDocumentQueryListener[] queryListeners, bool isMapReduce) : base(session , null , null , indexName, fieldsToFetch, projectionFields, queryListeners, isMapReduce) { this.getShardsToOperateOn = getShardsToOperateOn; this.shardStrategy = shardStrategy; }
public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy) { var result = new SuggestionQueryResult { Suggestions = responses .Select(item => RavenJObject.Parse(item.Result)) .SelectMany(data => ((RavenJArray) data["Suggestions"]).Select(x => x.Value<string>())) .Distinct() .ToArray() }; Result = result; }
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 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)); } } }
public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy) { var result = new SuggestionQueryResult { Suggestions = (from item in responses let data = (RavenJObject)item.Result from suggestion in (RavenJArray)data["Suggestions"] select suggestion.Value <string>()) .Distinct() .ToArray() }; Result = result; }
public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy) { var result = new SuggestionQueryResult { Suggestions = (from item in responses let data = (RavenJObject)item.Result from suggestion in (RavenJArray)data["Suggestions"] select suggestion.Value<string>()) .Distinct() .ToArray() }; Result = result; }
public async Task CanUseAsyncTransformer() { 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(); new Transformer().Execute(shardedDocumentStore); using (var session = shardedDocumentStore.OpenAsyncSession()) { await session.StoreAsync(new Profile { Name = "Oren", Location = "Shard1" }); await session.SaveChangesAsync(); } using (var session = shardedDocumentStore.OpenAsyncSession()) { var results = await session.Query <Profile>() .Customize(x => x.WaitForNonStaleResults()) .Where(x => x.Name == "Oren") .TransformWith <Transformer, Result>() .ToListAsync(); Assert.Equal("Oren", results[0].Name); } } }
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(); } } }
public static void CreateStore() { var shards = new Dictionary <string, IAsyncFilesCommands> { { "Italia", new AsyncFilesServerClient("http://localhost:8080", "fsItalia") }, { "Francia", new AsyncFilesServerClient("http://localhost:8080", "fsFrancia") }, { "Polonia", new AsyncFilesServerClient("http://localhost:8080", "fsPolonia") } }; FileStore.ShardStrategy = new ShardStrategy(shards); FileStore.ShardStrategy.ModifyFileName = (convention, shardId, filename) => shardId + convention.IdentityPartsSeparator + filename; FileStore.ShardStrategy.ShardResolutionStrategy = new CountryResolutionStrategy(shards.Keys.ToList(), FileStore.ShardStrategy.ModifyFileName, FileStore.ShardStrategy.Conventions); }
public void Sample() { Dictionary<string, IDocumentStore> 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"}}, }; #region sharding_1 ShardStrategy shardStrategy = new ShardStrategy(shards) .ShardingOn<Company>(company => company.Region) .ShardingOn<Invoice>(x => x.CompanyId); #endregion }
public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy) { var result = new FacetResults(); foreach (var response in responses.Select(response => (RavenJObject)response.Result)) { var facet = response.JsonDeserialization <FacetResults>(); foreach (var facetResult in facet.Results) { if (!result.Results.ContainsKey(facetResult.Key)) { result.Results[facetResult.Key] = new FacetResult(); } var newFacetResult = result.Results[facetResult.Key]; foreach (var facetValue in facetResult.Value.Values) { var existingFacetValueRange = newFacetResult.Values.Find((x) => x.Range == facetValue.Range); if (existingFacetValueRange != null) { existingFacetValueRange.Hits += facetValue.Hits; } else { newFacetResult.Values.Add(new FacetValue() { Hits = facetValue.Hits, Range = facetValue.Range }); } } foreach (var facetTerm in facetResult.Value.RemainingTerms) { if (!newFacetResult.RemainingTerms.Contains(facetTerm)) { newFacetResult.RemainingTerms.Add(facetTerm); } } } } Result = result; }
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); } } }
public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy) { throw new NotImplementedException(); /*var result = new SuggestionQueryResult * { * Suggestions = (from item in responses * let data = (RavenJObject)item.Result * from suggestion in (RavenJArray)data["Suggestions"] * select suggestion.Value<string>()) * .Distinct() * .ToArray() * }; * * Result = result;*/ }
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 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); } } }
public WhenUsingShardedServers() { const string server = "localhost"; const int port1 = 8079; const int port2 = 8081; path1 = GetPath("TestShardedDb1"); path2 = GetPath("TestShardedDb2"); NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(port1); NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(port2); company1 = new Company { Name = "Company1" }; company2 = new Company { Name = "Company2" }; server1 = GetNewServer(port1, path1); server2 = GetNewServer(port2, path2); 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(Arg.Is(company1), Arg <ITransactionalDocumentSession> .Is.Anything)).Return("Shard1"); shardResolution.Stub(x => x.GenerateShardIdFor(Arg.Is(company2), Arg <ITransactionalDocumentSession> .Is.Anything)).Return("Shard2"); shardResolution.Stub(x => x.MetadataShardIdFor(company1)).Return("Shard1"); shardResolution.Stub(x => x.MetadataShardIdFor(company2)).Return("Shard1"); shardStrategy = new ShardStrategy(shards) { ShardResolutionStrategy = shardResolution }; }
public void ShouldWork() { 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 SomeShardingResolutionStrategy(shards.Keys, shardStrategy); shardStrategy.ShardingOn <Profile>(x => x.Location); using (var shardedDocumentStore = new ShardedDocumentStore(shardStrategy)) { shardedDocumentStore.Initialize(); new ProfileIndex().Execute(shardedDocumentStore); server1.Options.RequestManager.ResetNumberOfRequests(); server2.Options.RequestManager.ResetNumberOfRequests(); Assert.Equal(0, server1.Options.RequestManager.NumberOfRequests); Assert.Equal(0, server2.Options.RequestManager.NumberOfRequests); using (var session = shardedDocumentStore.OpenSession()) { var query = session .Query <Profile>("ProfileIndex") .Where(x => x.Location == "Shard1" || x.Location == "Shard2") .ToList(); Assert.Equal(0, server1.Options.RequestManager.NumberOfRequests); Assert.Equal(1, server2.Options.RequestManager.NumberOfRequests); } } }
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 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); } } }
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 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)); } } }
public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy) { var result = new Dictionary <string, IEnumerable <FacetValue> >(); IEnumerable <IGrouping <string, KeyValuePair <string, IEnumerable <FacetValue> > > > list = responses.Select(response => RavenJObject.Parse(response.Result)) .SelectMany(jsonResult => jsonResult.JsonDeserialization <IDictionary <string, IEnumerable <FacetValue> > >()) .GroupBy(x => x.Key); foreach (var facet in list) { var individualFacet = facet.SelectMany(x => x.Value).GroupBy(x => x.Range) .Select(g => new FacetValue { Count = g.Sum(y => y.Count), Range = g.Key }); result[facet.Key] = individualFacet.ToList(); } Result = result; }
public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy) { var result = new Dictionary<string, IEnumerable<FacetValue>>(); IEnumerable<IGrouping<string, KeyValuePair<string, IEnumerable<FacetValue>>>> list = responses.Select(response => RavenJObject.Parse(response.Result)) .SelectMany(jsonResult => jsonResult.JsonDeserialization<IDictionary<string, IEnumerable<FacetValue>>>()) .GroupBy(x => x.Key); foreach (var facet in list) { var individualFacet = facet.SelectMany(x=>x.Value).GroupBy(x=>x.Range) .Select(g=>new FacetValue { Count = g.Sum(y=>y.Count), Range = g.Key }); result[facet.Key] = individualFacet.ToList(); } Result = result; }
public void Sample() { Dictionary <string, IDocumentStore> 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" } }, }; #region sharding_1 ShardStrategy shardStrategy = new ShardStrategy(shards) .ShardingOn <Company>(company => company.Region) .ShardingOn <Invoice>(x => x.CompanyId); #endregion }
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 RavenDbRegistry() { For<DocumentStores>() .Singleton() .Use(x => { var shards = new Dictionary<string, IDocumentStore> { {"01", new DocumentStore{Url = "http://localhost:8079", DefaultDatabase = "Questions"}}, {"02", new DocumentStore{Url = "http://localhost:8078", DefaultDatabase = "Questions"}}, {"03", new DocumentStore{Url = "http://localhost:8077", DefaultDatabase = "Questions"}}, {"04", new DocumentStore{Url = "http://localhost:8076", DefaultDatabase = "Questions"}}, }; var shardStrategy = new ShardStrategy(shards); shardStrategy.ShardAccessStrategy.OnError += (commands, request, exception) => { if(HttpContext.Current.Items["raven-query-error"] == null) HttpContext.Current.Items["raven-query-error"] = new HashSet<string>(); var msgs = ((HashSet<string>)HttpContext.Current.Items["raven-query-error"]); msgs.Add(exception.Message + " " + ((ServerClient) commands).Url); return request.Query != null; }; var documentStores = new DocumentStores { Users = new DocumentStore { Url = "http://localhost:8079", DefaultDatabase = "Users" }.Initialize(), Questions = new ShardedDocumentStore(shardStrategy).Initialize() }; return documentStores; } ) .Named("RavenDB Document Store."); }
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); } } } }
private static ShardStrategy ShardStrategySetup() { var shards = new Dictionary <string, IDocumentStore> { { "vet1", new DocumentStore { Url = "http://localhost:8080", DefaultDatabase = "Vet1" } }, { "vet2", new DocumentStore { Url = "http://localhost:8080", DefaultDatabase = "Vet2" } }, { "vet3", new DocumentStore { Url = "http://localhost:8080", DefaultDatabase = "Vet3" } } }; var shardStrategy = new ShardStrategy(shards) .ShardingOn <User>() .ShardingOn <Dog>(x => x.OwnerId) .ShardingOn <Cat>(x => x.OwnerId); return(shardStrategy); }
public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy) { var list = new List <MultiLoadResult>( from response in responses let result = response.Result select new MultiLoadResult { Includes = result.Value <RavenJArray>("Includes").Cast <RavenJObject>().ToList(), Results = result.Value <RavenJArray>("Results").Cast <RavenJObject>().ToList() }); var capacity = list.Max(x => x.Results.Count); var finalResult = new MultiLoadResult { Includes = new List <RavenJObject>(), Results = new List <RavenJObject>(Enumerable.Range(0, capacity).Select(x => (RavenJObject)null)) }; foreach (var multiLoadResult in list) { finalResult.Includes.AddRange(multiLoadResult.Includes); for (int i = 0; i < multiLoadResult.Results.Count; i++) { if (finalResult.Results[i] == null) { finalResult.Results[i] = multiLoadResult.Results[i]; } } } RequiresRetry = multiLoadOperation.SetResult(finalResult); if (RequiresRetry == false) { Result = multiLoadOperation.Complete <T>(); } }
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 HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy) { throw new NotImplementedException(); /* var count = responses.Count(x => x.Status == 404); * if (count != 0) * { * throw new InvalidOperationException("There is no index named: " + queryOperation.IndexName + " in " + count + " shards"); * } * * var list = responses * .Select(response => SerializationHelper.ToQueryResult((RavenJObject)response.Result, response.GetEtagHeader(), response.Headers[Constants.Headers.RequestTime], -1)) * .ToList(); * * var queryResult = shardStrategy.MergeQueryResults(queryOperation.IndexQuery, list); * * queryOperation.EnsureIsAcceptable(queryResult); * * if (afterQueryExecuted != null) * afterQueryExecuted(queryResult); * Result = queryOperation.Complete<T>(); * QueryResult = queryResult;*/ }
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); } } }
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); } } }
public void HandleResponses(GetResponse[] responses, ShardStrategy shardStrategy) { throw new NotImplementedException(); /*var list = new List<LoadResult>( * from response in responses * let result = response.Result * select new LoadResult * { * Includes = result.Value<RavenJArray>("Includes").Cast<RavenJObject>().ToList(), * Results = result.Value<RavenJArray>("Results").Select(x => x as RavenJObject).ToList() * }); * * var capacity = list.Max(x => x.Results.Count); * * var finalResult = new LoadResult * { * Includes = new List<RavenJObject>(), * Results = new List<RavenJObject>(Enumerable.Range(0, capacity).Select(x => (RavenJObject)null)) * }; * * * foreach (var multiLoadResult in list) * { * finalResult.Includes.AddRange(multiLoadResult.Includes); * * for (int i = 0; i < multiLoadResult.Results.Count; i++) * { * if (finalResult.Results[i] == null) * finalResult.Results[i] = multiLoadResult.Results[i]; * } * } * RequiresRetry = loadOperation.SetResult(finalResult); * if (RequiresRetry == false) * Result = loadOperation.Complete<T>(); */ }
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); } } }
public ShardedTransformed() { servers = new[] { GetNewServer(8079, requestedStorage: "esent"), GetNewServer(8078, requestedStorage: "esent") }; Dictionary <string, IDocumentStore> shards = new Dictionary <string, IDocumentStore> { { "shard1", new DocumentStore { Url = "http://localhost:8079" } }, { "shard2", new DocumentStore { Url = "http://localhost:8078" } } }; var shardStrategy = new ShardStrategy(shards); store = new ShardedDocumentStore(shardStrategy).Initialize(); new Articles_Search().Execute(store); new ArticlesFullTransformer().Execute(store); }
public RavenDbConnection(IRavenDbConnectionManager connectionManager) { var servers = connectionManager.GetConnectionStrings(); _nrShards = servers.Count; _shards = new Dictionary<string, IDocumentStore>(); foreach (string regionName in servers) { _shards.Add(regionName, new DocumentStore { ConnectionStringName = regionName }); } var shardStrategy = new ShardStrategy(_shards) .ShardingOn<Client>(client => client.Region); _clientsDocumentStore = new ShardedDocumentStore(shardStrategy).Initialize(); servers = connectionManager.GetReplicationStrings(); foreach (var replicationString in servers) { _replicationStores.Add(new DocumentStore() { ConnectionStringName = replicationString }.Initialize()); } foreach (var replicationStore in _replicationStores) { replicationStore.Conventions.FailoverBehavior = FailoverBehavior.AllowReadsFromSecondariesAndWritesToSecondaries; } }
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(); }); } } } }