public void TestCreateMultipleWithNominalTypeAndValues() { var wrappers = BsonDocumentWrapper.CreateMultiple(typeof(C), new C[] { _c, null }); var expected = "[{ \"X\" : 1 }, null]"; Assert.AreEqual(expected, wrappers.ToJson()); }
public void TestCreateMultipleGenericWithValues() { var wrappers = BsonDocumentWrapper.CreateMultiple <C>(new C[] { _c, null }); var expected = "[{ \"X\" : 1 }, null]"; Assert.AreEqual(expected, wrappers.ToJson()); }
public void TestCreateMultiple() { var c = new C { X = 1 }; var wrappers = BsonDocumentWrapper.CreateMultiple(typeof(C), new[] { c, null }).ToArray(); Assert.Equal(2, wrappers.Length); var wrapper1 = wrappers[0]; Assert.Same(BsonSerializer.LookupSerializer(typeof(C)), wrapper1.Serializer); Assert.Same(c, wrapper1.Wrapped); Assert.Equal(false, wrapper1.IsMaterialized); var wrapper2 = wrappers[1]; Assert.Same(BsonSerializer.LookupSerializer(typeof(C)), wrapper2.Serializer); Assert.Same(null, wrapper2.Wrapped); Assert.Equal(false, wrapper2.IsMaterialized); Assert.Throws <ArgumentNullException>(() => BsonDocumentWrapper.CreateMultiple(null, new[] { c, null })); Assert.Throws <ArgumentNullException>(() => BsonDocumentWrapper.CreateMultiple(typeof(C), null)); }
public void TestCreateMultipleGeneric() { var c = new C { X = 1 }; var wrappers = BsonDocumentWrapper.CreateMultiple <C>(new[] { c, null }).ToArray(); Assert.AreEqual(2, wrappers.Length); var wrapper1 = wrappers[0]; Assert.AreEqual(false, wrapper1.IsUpdateDocument); Assert.AreEqual(null, wrapper1.SerializationOptions); Assert.AreSame(BsonSerializer.LookupSerializer(typeof(C)), wrapper1.Serializer); Assert.AreSame(typeof(C), wrapper1.WrappedNominalType); Assert.AreSame(c, wrapper1.WrappedObject); Assert.AreEqual(false, wrapper1.IsMaterialized); var wrapper2 = wrappers[1]; Assert.AreEqual(false, wrapper2.IsUpdateDocument); Assert.AreEqual(null, wrapper2.SerializationOptions); Assert.AreSame(BsonSerializer.LookupSerializer(typeof(C)), wrapper2.Serializer); Assert.AreSame(typeof(C), wrapper2.WrappedNominalType); Assert.AreSame(null, wrapper2.WrappedObject); Assert.AreEqual(false, wrapper2.IsMaterialized); Assert.Throws <ArgumentNullException>(() => BsonDocumentWrapper.CreateMultiple <C>(null)); }
/// <summary> /// Adds an $addToSet/$each update modifier. /// </summary> /// <param name="name">The name of the array element.</param> /// <param name="values">A list of wrapped values to add to the set.</param> /// <returns>The builder (so method calls can be chained).</returns> public UpdateBuilder AddToSetEachWrapped <T>( string name, IEnumerable <T> values ) { var wrappedValues = BsonDocumentWrapper.CreateMultiple(values).Cast <BsonValue>(); // the cast to BsonValue is required return(AddToSetEach(name, wrappedValues)); }
/// <summary> /// Adds a list of wrapped values to the end of the named array element (see $pushAll). /// </summary> /// <typeparam name="T">The type of wrapped values.</typeparam> /// <param name="name">The name of the array element.</param> /// <param name="values">The wrapped values to add to the end of the array.</param> /// <returns>The builder (so method calls can be chained).</returns> public UpdateBuilder PushAllWrapped <T>(string name, IEnumerable <T> values) { var wrappedValues = new BsonArray(BsonDocumentWrapper.CreateMultiple(values).Cast <BsonValue>()); // the cast to BsonValue is required BsonElement element; if (document.TryGetElement("$pushAll", out element)) { element.Value.AsBsonDocument.Add(name, wrappedValues); } else { document.Add("$pushAll", new BsonDocument(name, wrappedValues)); } return(this); }
public void Flush() { long tobson = 0; int index = 0; Task[] tasks = new Task[_inMemoryDatabase.Collections.Keys.Count]; foreach (var pair in _inMemoryDatabase.Collections) { var mongoSettings = new MongoCollectionSettings { AssignIdOnInsert = false }; var mongoCollection = Database.GetCollection(typeof(BsonDocument), pair.Key.CollectionName, mongoSettings); var inMemoryCollection = (IInMemoryCollection)pair.Value; var stopwatch = Stopwatch.StartNew(); var docs = BsonDocumentWrapper.CreateMultiple(inMemoryCollection.Documents.Values); stopwatch.Stop(); //Console.WriteLine("Collection {0} serialized to bson in {1:n0} ms", pair.Key, stopwatch.ElapsedMilliseconds); tobson += stopwatch.ElapsedMilliseconds; stopwatch.Start(); tasks[index] = Task.Factory.StartNew(() => { var mongoInsertOptions = new MongoInsertOptions(); mongoInsertOptions.WriteConcern = WriteConcern.Acknowledged; mongoCollection.InsertBatch(docs); }, TaskCreationOptions.LongRunning); stopwatch.Stop(); //Console.WriteLine("Collection {0} inserted to MongoDB in {1:n0} ms", pair.Key, stopwatch.ElapsedMilliseconds); index++; } Task.WaitAll(tasks); //Console.WriteLine("Total time for serialization: {0:n0} ms", tobson); }
public void TestCreateMultipleWithNominalTypeAndNullValues() { Assert.Throws <ArgumentNullException>(() => { var wrappers = BsonDocumentWrapper.CreateMultiple(typeof(C), null); }); }
public void TestCreateMultipleWithNullNominalTypeAndValues() { Assert.Throws <ArgumentNullException>(() => { var wrappers = BsonDocumentWrapper.CreateMultiple(null, new C[] { _c, null }); }); }
public void TestCreateMultipleGenericWithNullValues() { Assert.Throws <ArgumentNullException>(() => { var wrappers = BsonDocumentWrapper.CreateMultiple <C>(null); }); }
public async Task <Card> CardFindAndModify(Card card) { if (card == null) { throw new ArgumentNullException("card"); } var collection = this.mDatabase.GetCollection <Card>(cCardsCollectionName); // Query for this card var cardQuery = Builders <Card> .Filter.And( Builders <Card> .Filter.Eq("name", card.Name), Builders <Card> .Filter.Eq("setId", card.SetId), Builders <Card> .Filter.Eq("multiverseId", card.MultiverseId) ); var cardSoryBy = Builders <Card> .Sort.Descending("name"); // Create BsonDocumentWrapper for the card's Colors array var colorsDocument = BsonDocumentWrapper.CreateMultiple(card.Colors); var colorsArray = new BsonArray(colorsDocument); // Create BsonDocumentWrapper for the card's Rulings array var rulingsDocument = BsonDocumentWrapper.CreateMultiple(card.Rulings); var rulingArray = new BsonArray(rulingsDocument); var colorIdentityDocument = BsonDocumentWrapper.CreateMultiple(card.ColorIdentity); var colorIdentityArray = new BsonArray(colorIdentityDocument); var variationsDocument = BsonDocumentWrapper.CreateMultiple(card.Variations); var variationsArray = new BsonArray(variationsDocument); var typesDocument = BsonDocumentWrapper.CreateMultiple(card.Types); var typesArray = new BsonArray(typesDocument); // Update document var cardUpdate = Builders <Card> .Update .Set("relatedCardId", card.RelatedCardId) .Set("name", card.Name) .Set("searchName", card.SearchName) .Set("desc", card.Desc ?? "") .Set("flavor", card.Flavor ?? "") .Set("colors", colorsArray) // Use BsonArray that contains a BsonDocumentWrapper of the card's colors .Set("cost", card.Cost ?? "") .Set("cmc", card.Cmc) .Set("setName", card.SetName) .Set("setSearchName", card.SetSearchName) .Set("type", card.Type ?? "") .Set("subType", card.SubType ?? "") .Set("power", card.Power ?? "") .Set("toughness", card.Toughness ?? "") .Set("loyalty", card.Loyalty ?? "") .Set("rarity", card.Rarity ?? "") .Set("artist", card.Artist ?? "") .Set("setId", card.SetId) .Set("token", card.Token) .Set("rulings", rulingArray) // Use BsonArray that contains a BsonDocumentWrapper of the card's rulings .Set("img", card.Img ?? "") .Set("imgHires", card.ImgHires ?? "") .Set("multiverseId", card.MultiverseId) .Set("number", card.Number ?? "") .Set("mciNumber", card.McINumber ?? "") .Set("colorIdentity", colorIdentityArray) .Set("variations", variationsArray) .Set("types", typesArray); var options = new FindOneAndUpdateOptions <Card>(); options.Sort = cardSoryBy; options.IsUpsert = true; options.ReturnDocument = ReturnDocument.After; var cardResult = collection.FindOneAndUpdate(cardQuery, cardUpdate, options); return(cardResult); }
public async Task <Card> CardFindAndModify(Card card) { if (card == null) { throw new ArgumentNullException("card"); } var collection = this.mDatabase.GetCollection <Card>(cCardsCollectionName); // Query for this card var cardQuery = Query.And( Query.EQ("name", card.Name), Query.EQ("setId", card.SetId), Query.EQ("multiverseId", card.MultiverseId) ); var cardSoryBy = SortBy.Descending("name"); // Create BsonDocumentWrapper for the card's Colors array var colorsDocument = BsonDocumentWrapper.CreateMultiple(card.Colors); var colorsArray = new BsonArray(colorsDocument); // Create BsonDocumentWrapper for the card's Rulings array var rulingsDocument = BsonDocumentWrapper.CreateMultiple(card.Rulings); var rulingArray = new BsonArray(rulingsDocument); var colorIdentityDocument = BsonDocumentWrapper.CreateMultiple(card.ColorIdentity); var colorIdentityArray = new BsonArray(colorIdentityDocument); var variationsDocument = BsonDocumentWrapper.CreateMultiple(card.Variations); var variationsArray = new BsonArray(variationsDocument); var typesDocument = BsonDocumentWrapper.CreateMultiple(card.Types); var typesArray = new BsonArray(typesDocument); // Update document var cardUpdate = Update .Set("relatedCardId", card.RelatedCardId) .Set("name", card.Name) .Set("searchName", card.SearchName) .Set("desc", card.Desc ?? "") .Set("flavor", card.Flavor ?? "") .Set("colors", colorsArray) // Use BsonArray that contains a BsonDocumentWrapper of the card's colors .Set("cost", card.Cost ?? "") .Set("cmc", card.Cmc) .Set("setName", card.SetName) .Set("setSearchName", card.SetSearchName) .Set("type", card.Type ?? "") .Set("subType", card.SubType ?? "") .Set("power", card.Power ?? "") .Set("toughness", card.Toughness ?? "") .Set("loyalty", card.Loyalty ?? "") .Set("rarity", card.Rarity ?? "") .Set("artist", card.Artist ?? "") .Set("setId", card.SetId) .Set("token", card.Token) .Set("rulings", rulingArray) // Use BsonArray that contains a BsonDocumentWrapper of the card's rulings .Set("img", card.Img ?? "") .Set("imgHires", card.ImgHires ?? "") .Set("multiverseId", card.MultiverseId) .Set("number", card.Number ?? "") .Set("mciNumber", card.McINumber ?? "") .Set("colorIdentity", colorIdentityArray) .Set("variations", variationsArray) .Set("types", typesArray); // Find and modify document. If document doesnt exist, insert it FindAndModifyArgs findModifyArgs = new FindAndModifyArgs(); findModifyArgs.SortBy = cardSoryBy; findModifyArgs.Query = cardQuery; findModifyArgs.Upsert = true; findModifyArgs.Update = cardUpdate; findModifyArgs.VersionReturned = FindAndModifyDocumentVersion.Modified; var cardResult = collection.FindAndModify(findModifyArgs); var cardModified = cardResult.GetModifiedDocumentAs <Card>(); return(cardModified); }