Last active
September 9, 2016 07:07
-
-
Save lynxz/3272b8b409f1703dc792e68789917819 to your computer and use it in GitHub Desktop.
Bulkinsert on RavenDB 2.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var store = new EmbeddableDocumentStore(); | |
store.RunInMemory = true; | |
using (store.Initialize()) | |
{ | |
var index = new DocumentIndex(); | |
index.Execute(store); | |
var docs = Enumerable.Range(0, 100000).Select(i => new SomeClass { Value = "Doc: " + i}).ToList(); | |
var counter = 0; | |
using (var blk = store.BulkInsert()) | |
{ | |
blk.Report += (s) => | |
{ | |
counter++; | |
Console.WriteLine(counter.ToString("000") + ": " + s); | |
}; | |
docs.ForEach(d => blk.Store(d)); | |
} | |
SpinWait.SpinUntil(() => store.DatabaseCommands.GetStatistics().StaleIndexes.Length == 0, TimeSpan.FromSeconds(20)); | |
using (var session = store.OpenSession()) | |
{ | |
var documentsByCollection = session.Query<DocumentIndex.Result, DocumentIndex>().ToArray(); | |
foreach (var dbc in documentsByCollection) | |
{ | |
Console.WriteLine($"{dbc.Tag}:\t{dbc.Sum}"); | |
} | |
} | |
} | |
} | |
public class SomeClass | |
{ | |
public string Id { get; set; } | |
public string Value { get; set; } | |
} | |
public class DocumentIndex : AbstractIndexCreationTask<SomeClass, DocumentIndex.Result> | |
{ | |
public class Result | |
{ | |
public string Tag { get; set;} | |
public int Sum { get; set;} | |
} | |
public DocumentIndex() | |
{ | |
Map = docs => from doc in docs | |
let metadata = MetadataFor(doc) | |
select new Result { Tag = metadata["Raven-Entity-Name"].ToString(), Sum = 1}; | |
Reduce = results => from result in results | |
group result by result.Tag into g | |
select new Result { Tag = g.Key, Sum = g.Sum(x => x.Sum)}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment