本文整理汇总了C#中Raven.Client.Shard.ShardedDocumentStore.OpenSession方法的典型用法代码示例。如果您正苦于以下问题:C# ShardedDocumentStore.OpenSession方法的具体用法?C# ShardedDocumentStore.OpenSession怎么用?C# ShardedDocumentStore.OpenSession使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Raven.Client.Shard.ShardedDocumentStore
的用法示例。
在下文中一共展示了ShardedDocumentStore.OpenSession方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Index
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();
}
示例2: Main
static void Main(string[] args)
{
var shards = new Dictionary<string, IDocumentStore>
{
{ "one", new DocumentStore { Url = "http://localhost:8079", } },
{ "two", new DocumentStore { Url = "http://localhost:8078", } },
};
var shardStrategy = new ShardStrategy(shards)
.ShardingOn<User>()
.ShardingOn<Story>(x => x.UserId);
using (var store = new ShardedDocumentStore(shardStrategy).Initialize())
{
//using (var session = store.OpenSession())
//{
// var user = new User { Name = "Ayende" };
// session.Store(user);
// session.Store(new Story { UserId = user.Id });
// session.SaveChanges();
//}
using (var session = store.OpenSession())
{
var load = session.Query<Story>()
.Where(x => x.UserId == "two/users/1")
.ToList();
Console.WriteLine(load[0].UserId);
}
}
}
示例3: CanIgnoreParallel
public void CanIgnoreParallel()
{
using (GetNewServer())
{
var shardingStrategy = new ShardStrategy(new Dictionary<string, IDocumentStore>
{
{"one", new DocumentStore {Url = "http://localhost:8079"}},
{"two", new DocumentStore {Url = "http://localhost:8078"}},
})
{
ShardAccessStrategy = new ParallelShardAccessStrategy()
};
shardingStrategy.ShardAccessStrategy.OnError += (commands, request, exception) => request.Query != null;
using (var docStore = new ShardedDocumentStore(shardingStrategy).Initialize())
{
using (var session = docStore.OpenSession())
{
session.Query<AccurateCount.User>()
.ToList();
}
}
}
}
示例4: CanDisableQueryResultsTrackingForShardedDocumentQuery
public void CanDisableQueryResultsTrackingForShardedDocumentQuery()
{
using (GetNewServer(8079))
using (GetNewServer(8078))
using (var store = new ShardedDocumentStore(new ShardStrategy(new Dictionary<string, IDocumentStore>
{
{"1", CreateDocumentStore(8079)},
{"2", CreateDocumentStore(8078)},
})))
{
store.Initialize();
using (var session = store.OpenSession())
{
session.Store(new Item());
session.Store(new Item());
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var items = session.Query<Item>().Customize(x => x.NoTracking().WaitForNonStaleResults()).ToList();
Assert.Equal(2, items.Count);
Assert.Equal(0, ((InMemoryDocumentSessionOperations) session).NumberOfEntitiesInUnitOfWork);
}
}
}
示例5: Main
private static void Main()
{
var shards = new Dictionary<string, IDocumentStore>
{
{"_", new DocumentStore {Url = "http://localhost:8080", DefaultDatabase = "Shop"}}, //existing data
{"ME", new DocumentStore {Url = "http://localhost:8080", DefaultDatabase = "Shop_ME"}},
{"US", new DocumentStore {Url = "http://localhost:8080", DefaultDatabase = "Shop_US"}},
};
var shardStrategy = new ShardStrategy(shards)
.ShardingOn<Customer>(c => c.Region)
.ShardingOn<Invoice>(i => i.Customer);
var x = new ShardedDocumentStore(shardStrategy).Initialize();
using (var s = x.OpenSession())
{
var customer = new Customer
{
Region = "US"
};
s.Store(customer);
s.Store(new Invoice
{
Customer = customer.Id
});
s.SaveChanges();
}
}
示例6: Sharding
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
}
示例7: Main
static void Main()
{
var shards = new Dictionary<string, IDocumentStore>
{
{"one", new DocumentStore {Url = "http://localhost:8079"}},
{"two", new DocumentStore {Url = "http://localhost:8078"}},
{"three", new DocumentStore {Url = "http://localhost:8077"}},
};
var shardStrategy = new ShardStrategy(shards)
.ShardingOn<Company>()
.ShardingOn<Invoice>(x => x.CompanyId);
using (var documentStore = new ShardedDocumentStore(shardStrategy).Initialize())
{
new InvoicesAmountByDate().Execute(documentStore);
using (var session = documentStore.OpenSession())
{
var asian = new Company { Name = "Company 1" };
session.Store(asian);
var middleEastern = new Company { Name = "Company 2" };
session.Store(middleEastern);
var american = new Company { Name = "Company 3" };
session.Store(american);
session.Store(new Invoice { CompanyId = american.Id, Amount = 3, IssuedAt = DateTime.Today.AddDays(-1) });
session.Store(new Invoice { CompanyId = asian.Id, Amount = 5, IssuedAt = DateTime.Today.AddDays(-1) });
session.Store(new Invoice { CompanyId = middleEastern.Id, Amount = 12, IssuedAt = DateTime.Today });
session.SaveChanges();
}
using (var session = documentStore.OpenSession())
{
var reduceResults = session.Query<InvoicesAmountByDate.ReduceResult, InvoicesAmountByDate>()
.ToList();
foreach (var reduceResult in reduceResults)
{
string dateStr = reduceResult.IssuedAt.ToString("MMM dd, yyyy", CultureInfo.InvariantCulture);
Console.WriteLine("{0}: {1}", dateStr, reduceResult.Amount);
}
Console.WriteLine();
}
}
}
示例8: get_metadata_for_sharded
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);
}
}
}
示例9: OverwritingExistingDocumentGeneratesWrongIdWithShardedDocumentStore
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);
}
}
}
}
}
示例10: Can_insert_into_two_sharded_servers
public void Can_insert_into_two_sharded_servers()
{
using (var documentStore = new ShardedDocumentStore(shardStrategy, shards))
{
documentStore.Initialize();
using (var session = documentStore.OpenSession())
{
session.Store(company1);
session.Store(company2);
session.SaveChanges();
}
}
}
示例11: OverwritingExistingDocumentGeneratesWrongIdWithShardedDocumentStore
public void OverwritingExistingDocumentGeneratesWrongIdWithShardedDocumentStore()
{
var server1 = GetNewServer(8079);
var server2 = GetNewServer(8078);
var shards = new Dictionary<string, IDocumentStore>
{
{"Shard1", new DocumentStore{Url = server1.Configuration.ServerUrl}},
{"Shard2", new DocumentStore{Url = server2.Configuration.ServerUrl}},
};
var shardStrategy = new ShardStrategy(shards);
shardStrategy.ShardingOn<Profile>(x => x.Location);
using (var shardedDocumentStore = new ShardedDocumentStore(shardStrategy))
{
shardedDocumentStore.Initialize();
var profile = new Profile {Name = "Test", Location = "Shard1"};
using (var documentSession = shardedDocumentStore.OpenSession())
{
documentSession.Store(profile, profile.Id);
documentSession.SaveChanges();
}
using (var documentSession = shardedDocumentStore.OpenSession())
{
var correctId = profile.Id;
documentSession.Store(profile, profile.Id);
Assert.Equal(correctId, profile.Id);
}
}
}
示例12: Main
static void Main()
{
var shards = new Shards
{
new DocumentStore {Url = "http://localhost:8080", Identifier = "Posts"},
new DocumentStore
{
Url = "http://localhost:8081",
Identifier = "Users",
Conventions = {DocumentKeyGenerator = user => "users/" + ((User) user).Name}
}
};
var shardStrategy = new ShardStrategy
{
ShardAccessStrategy = new ParallelShardAccessStrategy(),
ShardSelectionStrategy = new BlogShardSelectionStrategy(),
ShardResolutionStrategy = new BlogShardResolutionStrategy()
};
using (var documentStore = new ShardedDocumentStore(shardStrategy, shards).Initialize())
{
using (var session = documentStore.OpenSession())
{
var user = new User { Name = "PastyGeek" };
session.Store(user);
session.SaveChanges();
var post = new Post
{
AuthorId = user.Id,
Name = user.Name,
BlogId = "blogs/1",
Title = "More CodeMash Gloating!",
Body = "You wouldn't believe how much more fun I'm having than you!",
PostDate = DateTime.Now,
Tags = new List<string> { "codemash", "gloating" }
};
session.Store(post);
session.SaveChanges();
}
}
}
示例13: get_metadata_for_async_sharded
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);
}
}
}
示例14: Can_insert_into_two_sharded_servers
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]);
}
示例15: CanOverrideTheShardIdGeneration
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);
}
}
}