public void TestInsertMany() { var collection = GetTestCollection <BsonDocument>(); Dictionary <long, Role> Roles = new Dictionary <long, Role>(); for (int i = 0; i < 100; i++) { var role = new Role { RoleID = i, RoleName = i.ToString() }; Roles.Add(i, role); } BulkWriteContext context = new BulkWriteContext(); foreach (var pair in Roles) { context.Insert((BsonDocument)pair.Value); } context.Execute(collection); }
public void TestUpdateVsReplace() { var collection = GetTestCollection <BsonDocument>(); BulkWriteContext context = new BulkWriteContext(); Dictionary <long, Role> Roles = new Dictionary <long, Role>(); for (int i = 0; i < 100; i++) { var role = new Role { RoleID = i, RoleName = i.ToString() }; for (int itemIndex = 0; itemIndex < 50; itemIndex++) { role.Items.Add(itemIndex, new Item { ItemID = itemIndex, ItemUID = itemIndex }); } role.ClearDirty(); Roles.Add(i, role); context.Insert((BsonDocument)role); } context.Execute(collection); context.Clear(); Stopwatch watch = new Stopwatch(); List <ReplaceOneModel <BsonDocument> > replaces = new List <ReplaceOneModel <BsonDocument> >(); watch.Start(); foreach (var pair in Roles) { replaces.Add(new ReplaceOneModel <BsonDocument>(new BsonDocument { { "_id", pair.Key } }, (BsonDocument)pair.Value)); } collection.BulkWrite(replaces); watch.Stop(); var time1 = watch.ElapsedMilliseconds; watch.Reset(); for (int i = 0; i < 100; i++) { Roles[i].RoleName += i; for (int itemIndex = 0; itemIndex < 10; itemIndex++) { Roles[i].Items[itemIndex].ItemID += 1; } Roles[i].Items.Add(51, new Item { ItemID = 51, ItemUID = 51 }); } UpdateContext update = new UpdateContext(); watch.Start(); foreach (var pair in Roles) { update.Clear(); pair.Value.Update(update); context.Update(new BsonDocument { { "_id", pair.Key } }, update.Build()); } context.Execute(collection); context.Clear(); watch.Stop(); var time2 = watch.ElapsedMilliseconds; Assert.IsTrue(time1 > time2); }
public void TestBulkWriteRoles() { var collection = GetTestCollection <BsonDocument>(); Dictionary <long, Role> Roles = new Dictionary <long, Role>(); for (int i = 0; i < 100; i++) { var role = new Role { RoleID = i, RoleName = i.ToString(), Inserted = true }; for (int itemIndex = 0; itemIndex < 50; itemIndex++) { role.Items.Add(itemIndex, new Item { ItemID = itemIndex, ItemUID = itemIndex }); } Roles.Add(i, role); } UpdateContext update = new UpdateContext(); BulkWriteContext context = new BulkWriteContext(); foreach (var pair in Roles) { if (pair.Value.Inserted) { context.Insert((BsonDocument)pair.Value); } else { update.Clear(); pair.Value.Update(update); context.Update(new BsonDocument { { "_id", pair.Key } }, update.Build()); } pair.Value.ClearDirty(); pair.Value.Inserted = false; } context.Execute(collection); update.Clear(); context.Clear(); for (int i = 0; i < 100; i++) { Roles[i].RoleName += i; Roles[i].Items.Add(51, new Item { ItemID = 51, ItemUID = 51 }); } Roles.Add(100, new Role { RoleID = 100, Inserted = true }); foreach (var pair in Roles) { if (pair.Value.Inserted) { context.Insert((BsonDocument)pair.Value); } else { update.Clear(); pair.Value.Update(update); context.Update(new BsonDocument { { "_id", pair.Key } }, update.Build()); } pair.Value.ClearDirty(); pair.Value.Inserted = false; } context.Execute(collection); update.Clear(); context.Clear(); }