|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raven.Client.Document;
using Raven.Client;
using System.Transactions;
using Raven.Client.Indexes;
using Raven.Abstractions.Data;
using Raven.Abstractions.Commands;
namespace DocumentDB.Raven
{
public class RavenBulkUpdateDemo
{
private DocumentStore documentStore;
private Random rand = new Random();
public RavenBulkUpdateDemo(DocumentStore documentStore)
{
this.documentStore = documentStore;
this.documentStore.DatabaseCommands.PutIndex("Users/ByName",
new IndexDefinitionBuilder<User>
{
Map = users => from user in users
select new { user.Name }
});
}
public void Run()
{
InsertTestData(10);
ShowSummary();
DoUpdate(10);
}
private void ShowSummary()
{
using (IDocumentSession session = documentStore.OpenSession())
{
var users = session.Query<User>()
.Customize(x => x.WaitForNonStaleResults(TimeSpan.FromSeconds(10)))
.ToList();
Console.WriteLine(string.Format("There are now {0} Users", users.Count()));
foreach (User user in users)
{
Console.WriteLine(user.ToString());
}
}
}
public void InsertTestData(int numUsers)
{
List<User> newUsers = new List<User>();
using (IDocumentSession session = documentStore.OpenSession())
{
//add Users
for (int i = 0; i < numUsers; i++)
{
User newUser = new User { Name = string.Format("User/{0}", i.ToString()) };
newUsers.Add(newUser);
session.Store(newUser);
}
session.SaveChanges();
}
}
public void DoUpdate(int numUsers)
{
this.documentStore.DatabaseCommands.Batch(
//patch 1st 1/2 of users
Enumerable.Range(0, numUsers / 2).Select(i => new PatchCommandData
{
Key = "users/" + i,
Patches = new[]
{
new PatchRequest
{
Name = "Name",
Value = "Users-" + i
},
}
}
).ToArray());
this.documentStore.DatabaseCommands.Batch(
//patch 1st 1/2 of users
Enumerable.Range(numUsers / 2, numUsers).Select(i => new PatchCommandData
{
Key = "users/" + i,
Patches = new[]
{
new PatchRequest
{
Name = "Name",
Value = "Users-" + i
},
}
}
).ToArray());
using (IDocumentSession session = documentStore.OpenSession())
{
var users = session.Query<User>()
.Customize(x => x.WaitForNonStaleResults(TimeSpan.FromSeconds(10)))
.ToList();
for (int i = 0; i < numUsers; i++)
{
Console.WriteLine("Expecting UserName of '{0}', actual UserName now is '{1}'", "Users-"
+ (i + 1), users[i].Name);
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
Both of these at Sussex University UK.
Award(s)
I am lucky enough to have won a few awards for Zany Crazy code articles over the years
- Microsoft C# MVP 2016
- Codeproject MVP 2016
- Microsoft C# MVP 2015
- Codeproject MVP 2015
- Microsoft C# MVP 2014
- Codeproject MVP 2014
- Microsoft C# MVP 2013
- Codeproject MVP 2013
- Microsoft C# MVP 2012
- Codeproject MVP 2012
- Microsoft C# MVP 2011
- Codeproject MVP 2011
- Microsoft C# MVP 2010
- Codeproject MVP 2010
- Microsoft C# MVP 2009
- Codeproject MVP 2009
- Microsoft C# MVP 2008
- Codeproject MVP 2008
- And numerous codeproject awards which you can see over at my blog