public static Int32 Write_DELETE(Stream stream, Int32 requestID, Collection collection, DeleteEntry[] deletes) { var body = new BSONDocument(); body.Set(new BSONStringElement("delete", collection.Name)); var writeConcern = getWriteConcern(collection); if (writeConcern != null) { body.Set(new BSONDocumentElement("writeConcern", writeConcern)); } var arr = deletes.Select(one => { var doc = new BSONDocument(); doc.Set(new BSONDocumentElement("q", one.Query)); doc.Set(new BSONInt32Element("limit", one.Limit == DeleteLimit.None ? 0 : 1)); return(new BSONDocumentElement(doc)); } ).ToArray(); body.Set(new BSONArrayElement("deletes", arr)); return(Write_QUERY(stream, requestID, collection.Database, null, QueryFlags.None, 0, -1, body, null)); }
protected override object ExecuteBody() { var crud = Context.Access((tx) => { var cusr = tx.Db[BsonDataModel.GetCollectionName(this.Realm, BsonDataModel.COLLECTION_USER)]; var user = new BSONDocument(); user.Set(new BSONInt64Element(BsonDataModel._ID, Id)); user.Set(new BSONInt32Element(BsonDataModel.FLD_STATUS, (int)Status.Value)); user.Set(new BSONDateTimeElement(BsonDataModel.FLD_CREATEUTC, Context.App.TimeSource.UTCNow)); user.Set(new BSONDateTimeElement(BsonDataModel.FLD_STARTUTC, StartUtc.Value)); user.Set(new BSONDateTimeElement(BsonDataModel.FLD_ENDUTC, EndUtc.Value)); user.Set(new BSONStringElement(BsonDataModel.FLD_ROLE, Role.Default(string.Empty))); user.Set(new BSONStringElement(BsonDataModel.FLD_NAME, Name)); user.Set(new BSONStringElement(BsonDataModel.FLD_DESCRIPTION, Description)); user.Set(new BSONStringElement(BsonDataModel.FLD_NOTE, Note)); user.Set(new BSONStringElement(BsonDataModel.FLD_PROPS, Props)); var cr = cusr.Save(user); Aver.IsNull(cr.WriteErrors, cr.WriteErrors?.FirstOrDefault().Message); return(cr); }); return(crud); }
public void InferSchema() { var doc = new BSONDocument(); doc.Set( new BSONStringElement("FullName", "Alex Bobby") ); doc.Set( new BSONInt32Element("Age", 123) ); doc.Set( new BSONBooleanElement("IsGood", true) ); var c = new RowConverter(); var schema = c.InferSchemaFromBSONDocument(doc); Assert.AreEqual(3, schema.FieldCount); Assert.AreEqual(0, schema["FullName"].Order); Assert.AreEqual(1, schema["Age"].Order); Assert.AreEqual(2, schema["IsGood"].Order); Assert.AreEqual(typeof(object), schema["FullName"].NonNullableType); Assert.AreEqual(typeof(object), schema["Age"].NonNullableType); Assert.AreEqual(typeof(object), schema["IsGood"].NonNullableType); var row = new DynamicRow(schema); c.BSONDocumentToRow(doc, row, null); Assert.AreEqual("Alex Bobby", row[0]); Assert.AreEqual(123, row[1]); Assert.AreEqual(true, row[2]); Assert.AreEqual("Alex Bobby", row["FullName"]); Assert.AreEqual(123, row["Age"]); Assert.AreEqual(true, row["IsGood"]); }
public void InferSchema() { var doc = new BSONDocument(); doc.Set(new BSONStringElement("FullName", "Alex Bobby")); doc.Set(new BSONInt32Element("Age", 123)); doc.Set(new BSONBooleanElement("IsGood", true)); var c = new RowConverter(); var schema = c.InferSchemaFromBSONDocument(doc); Assert.AreEqual(3, schema.FieldCount); Assert.AreEqual(0, schema["FullName"].Order); Assert.AreEqual(1, schema["Age"].Order); Assert.AreEqual(2, schema["IsGood"].Order); Assert.AreEqual(typeof(object), schema["FullName"].NonNullableType); Assert.AreEqual(typeof(object), schema["Age"].NonNullableType); Assert.AreEqual(typeof(object), schema["IsGood"].NonNullableType); var row = new DynamicRow(schema); c.BSONDocumentToRow(doc, row, null); Assert.AreEqual("Alex Bobby", row[0]); Assert.AreEqual(123, row[1]); Assert.AreEqual(true, row[2]); Assert.AreEqual("Alex Bobby", row["FullName"]); Assert.AreEqual(123, row["Age"]); Assert.AreEqual(true, row["IsGood"]); }
/// <summary> /// { name: "Gagarin", birth: 1934 } /// </summary> public void WriteStringAndInt32Pair(Stream stream) { var root = new BSONDocument(); root.Set(new BSONStringElement("name", "Gagarin")); root.Set(new BSONInt32Element("birth", 1934)); root.WriteAsBSON(stream); }
/// <summary> /// { name: "Gagarin", birth: 1934 } /// </summary> public void WriteInt32Pair(Stream stream) { var root = new BSONDocument(); root.Set(new BSONInt32Element("name", 404)); root.Set(new BSONInt32Element("birth", 1934)); root.WriteAsBSON(stream); }
public void WriteBigIntegers(Stream stream) { stream.Position = 0; var root = new BSONDocument(); root.Set(new BSONInt32Element("intMin", int.MinValue)); root.Set(new BSONInt32Element("intMax", int.MaxValue)); root.Set(new BSONInt64Element("longMin", long.MinValue)); root.Set(new BSONInt64Element("longMax", long.MaxValue)); root.WriteAsBSON(stream); }
public void WriteBigIntegers(Stream stream) { stream.Position = 0; var root = new BSONDocument(); root.Set(new BSONInt32Element("intMin", int.MinValue)); root.Set(new BSONInt32Element("intMax", int.MaxValue)); root.Set(new BSONInt64Element("longMin", long.MinValue)); root.Set(new BSONInt64Element("longMax", long.MaxValue)); root.WriteAsBSON(stream); }
public static BSONDocument ToBson(Event evt) { var doc = new BSONDocument(); doc.Set(DataDocConverter.GDID_CLRtoBSON(FLD_GDID, evt.Gdid)); //long is enough to hold positive date (2^63) for 292 million years doc.Set(new BSONInt64Element(FLD_CREATEUTC, (long)evt.CreateUtc)); doc.Set(new BSONInt64Element(FLD_ORIGIN, (long)evt.Origin.ID)); doc.Set(new BSONInt64Element(FLD_CHECKPOINTUTC, (long)evt.CheckpointUtc));//same as above if (evt.Headers.IsNullOrWhiteSpace()) { doc.Set(new BSONNullElement(FLD_HEADERS)); } else { doc.Set(new BSONStringElement(FLD_HEADERS, evt.Headers)); } doc.Set(new BSONInt64Element(FLD_CONTENTTYPE, (long)evt.ContentType.ID)); doc.Set(DataDocConverter.ByteBuffer_CLRtoBSON(FLD_CONTENT, evt.Content)); return(doc); }
protected override object ExecuteBody() { var crud = Context.Access((tx) => { var crole = tx.Db[BsonDataModel.GetCollectionName(this.Realm, BsonDataModel.COLLECTION_ROLE)]; var role = new BSONDocument(); role.Set(new BSONStringElement(BsonDataModel._ID, Id)); role.Set(new BSONStringElement(BsonDataModel.FLD_RIGHTS, Rights.ToLaconicString(CodeAnalysis.Laconfig.LaconfigWritingOptions.Compact))); role.Set(new BSONDateTimeElement(BsonDataModel.FLD_CREATEUTC, Context.App.TimeSource.UTCNow)); var cr = crole.Save(role); Aver.IsNull(cr.WriteErrors, cr.WriteErrors?.FirstOrDefault().Message); return(cr); }); return(crud); }
/// <summary> /// { maxkey: <maxkey> } /// </summary> public void WriteSingleMaxKey(Stream stream) { var root = new BSONDocument(); root.Set(new BSONMaxKeyElement("maxkey")); root.WriteAsBSON(stream); }
private void button4_Click(object sender, EventArgs e) { var toolTip = new ToolTip(); toolTip.SetToolTip(sender as Button, "{ name: \"Gagarin\", birth: 1934 }"); using (var stream = File.Create("test.bson")) { var root = new BSONDocument(); root.Set(new BSONStringElement("name", "Gagarin")); root.Set(new BSONInt32Element("birth", 1934)); root.WriteAsBSON(stream); } Process.Start(AppDomain.CurrentDomain.BaseDirectory); }
protected virtual int DoUpdate(Connector.Database db, Row row, IDataStoreKey key, FieldFilterFunc filter = null) { var doc = convertRowToBSONDocumentWith_ID(row, "update", filter); var _id = doc[Connector.Protocol._ID]; doc.Delete(Connector.Protocol._ID); if (doc.Count == 0) { return(0); // nothing to update } //20160212 spol if (filter != null) { var wrapDoc = new BSONDocument(); wrapDoc.Set(new BSONDocumentElement(Connector.Protocol.SET, doc)); doc = wrapDoc; } var tname = GetCollectionName(row.Schema); var collection = db[tname]; var qry = new Connector.Query(); qry.Set(_id); var upd = new Connector.UpdateEntry(qry, doc, false, false); var result = collection.Update(upd); CheckCRUDResult(result, row.Schema.Name, "update"); return(result.TotalDocumentsAffected); }
/// <summary> /// { greetings: "Hello World!" } /// </summary> public void WriteSingleString(Stream stream) { var root = new BSONDocument(); root.Set(new BSONStringElement("greetings", "Hello World!")); root.WriteAsBSON(stream); }
/// <summary> /// { pi: 3.14159265358979 } /// </summary> public void WriteSingleDouble(Stream stream) { var root = new BSONDocument(); root.Set(new BSONDoubleElement("pi", Math.PI)); root.WriteAsBSON(stream); }
/// <summary> /// { solarSystemDiameter: 10000000000000 } /// </summary> public void WriteSingleInt64(Stream stream) { var root = new BSONDocument(); root.Set(new BSONInt64Element("solarSystemDiameter", 10000000000000)); root.WriteAsBSON(stream); }
/// <summary> /// { lucky: 7 } /// </summary> public void WriteSingleInt32(Stream stream) { var root = new BSONDocument(); root.Set(new BSONInt32Element("lucky", 7)); root.WriteAsBSON(stream); }
/// <summary> /// { booleanFalse: false } /// </summary> public void WriteSingleBooleanFalse(Stream stream) { var root = new BSONDocument(); root.Set(new BSONBooleanElement("booleanFalse", false)); root.WriteAsBSON(stream); }
/// <summary> /// { null: null } /// </summary> public void WriteSingleNull(Stream stream) { var root = new BSONDocument(); root.Set(new BSONNullElement("null")); root.WriteAsBSON(stream); }
/// <summary> /// Array of strings /// { 'fruits': ['apple, 'orange', 'plum'] } --> { 'fruits': { '0': 'apple', '1': 'orange', '2': 'plum' } } /// </summary> public void WriteStringArray(Stream stream) { var root = new BSONDocument(); var array = new[] { new BSONStringElement("apple"), new BSONStringElement("orange"), new BSONStringElement("plum") }; root.Set(new BSONArrayElement("fruits", array)); root.WriteAsBSON(stream); }
/// <summary> /// Array of int32 /// { 'years': [1963, 1984, 2015] } --> { 'years': { '0': 1963, '1': 1984, '2': 2015 } } /// </summary> public void WriteInt32Array(Stream stream) { var root = new BSONDocument(); var array = new[] { new BSONInt32Element(1963), new BSONInt32Element(1984), new BSONInt32Element(2015) }; root.Set(new BSONArrayElement("years", array)); root.WriteAsBSON(stream); }
/// <summary> /// Array of strings /// { 'stuff': ['apple, 3, 2.14] } --> { 'stuff': { '0': 'apple', '1': 3, '2': 2.14 } } /// </summary> public void WriteStringInt32DoubleMixedArray(Stream stream) { var root = new BSONDocument(); var array = new BSONElement[] { new BSONStringElement("apple"), new BSONInt32Element(3), new BSONDoubleElement(2.14D) }; root.Set(new BSONArrayElement("stuff", array)); root.WriteAsBSON(stream); }
public static Int32 Write_PING(Stream stream, Int32 requestID, Database db) { var body = new BSONDocument(); body.Set(new BSONInt32Element("ping", 1)); return(Write_QUERY(stream, requestID, db, null, QueryFlags.None, 0, -1, body, null)); }
/// <summary> /// { now: <DateTime from 635000000000000000 ticks> } /// </summary> public void WriteSingleDateTime(Stream stream) { var now = new DateTime(635000000000000000, DateTimeKind.Utc); var root = new BSONDocument(); root.Set(new BSONDateTimeElement("now", now)); root.WriteAsBSON(stream); }
public static Int32 Write_LIST_COLLECTIONS(Stream stream, Int32 requestID, Database db) { var body = new BSONDocument(); body.Set(new BSONInt32Element("listCollections", 1)); return(Write_QUERY(stream, requestID, db, null, QueryFlags.None, 0, -1, body, null)); }
/// <summary> /// { code: "function(){var x=1;var y='abc';return 1;};" } /// </summary> public void WriteSingleJavaScript(Stream stream) { var root = new BSONDocument(); var code = "function(){var x=1;var y='abc';return 1;};"; root.Set(new BSONJavaScriptElement("code", code)); root.WriteAsBSON(stream); }
public void WriteStringsWithDifferentLengths(Stream stream, int count) { stream.Position = 0; var root = new BSONDocument(); var value = new string('a', count); root.Set(new BSONStringElement("vary", value)); root.WriteAsBSON(stream); }
/// <summary> /// { binary: <bytes from 'This is binary data'> } /// </summary> public void WriteSingleBinaryData(Stream stream) { var data = Encoding.UTF8.GetBytes("This is binary data"); var root = new BSONDocument(); var binary = new BSONBinary(BSONBinaryType.BinaryOld, data); root.Set(new BSONBinaryElement("binary", binary)); root.WriteAsBSON(stream); }
/// <summary> /// { stamp: <seconds since Unix epoch to DateTime from 635000000000000000 ticks with 123 increment> } /// </summary> public void WriteSingleTimestamp(Stream stream) { var root = new BSONDocument(); var now = new DateTime(635000000000000000, DateTimeKind.Utc); var stamp = new BSONTimestamp(now, 123); root.Set(new BSONTimestampElement("stamp", stamp)); root.WriteAsBSON(stream); }
/// <summary> /// { nested: { capital: "Moscow" } } /// </summary> public void WriteNestedDocument(Stream stream) { var root = new BSONDocument(); var nested = new BSONDocument(); nested.Set(new BSONStringElement("capital", "Moscow")); root.Set(new BSONDocumentElement("nested", nested)); root.WriteAsBSON(stream); }
/// <summary> /// { email: <pattern='^[-.\w]+@(?:[a-z\d]{2,}\.)+[a-z]{2,6}$' options=I,M,U> } /// </summary> public void WriteSingleRegularExpression(Stream stream) { var root = new BSONDocument(); var pattern = @"^[-.\w]+@(?:[a-z\d]{2,}\.)+[a-z]{2,6}$"; var options = BSONRegularExpressionOptions.I | BSONRegularExpressionOptions.M | BSONRegularExpressionOptions.U; var regex = new BSONRegularExpression(pattern, options); root.Set(new BSONRegularExpressionElement("email", regex)); root.WriteAsBSON(stream); }
//http://docs.mongodb.org/manual/reference/write-concern/ private static BSONDocument getWriteConcern(Collection collection) { var concern = collection.Server.WriteConcern; if (concern < WriteConcern.Acknowledged) { return(null); //none } var result = new BSONDocument(); result.Set(new BSONInt32Element("w", concern >= WriteConcern.Acknowledged ? 1 : 0)); if (concern == WriteConcern.Journaled) { result.Set(new BSONBooleanElement("j", true)); } return(result); }
public void CollectionDrop() { using(var client= new MongoClient("My Test")) { var collection = client.DefaultLocalServer["db1"]["ToDrop"]; var doc1 = new BSONDocument(); doc1.Set( new BSONStringElement("_id", "id1")) .Set( new BSONStringElement("val", "My value")) .Set( new BSONInt32Element("age", 125)); var r = collection.Insert(doc1); Assert.AreEqual(1, r.TotalDocumentsAffected); collection.Drop(); Assert.IsTrue( collection.Disposed ); } }
public void WriteSingleBooleanTrue() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONBooleanElement("booleanTrue", true)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 19); // ensure document length is 19 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(19)); // content length is 19 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Boolean); // element type is boolean 0x08 CollectionAssert.AreEqual(reader.ReadBytes(11), Encoding.UTF8.GetBytes("booleanTrue")); // element name is 'booleanTrue' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)BSONBoolean.True); // byte content is correct Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 19); // ensure whole document readed } }
public void WriteSingleBinaryData() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var data = Encoding.UTF8.GetBytes("This is binary data"); var root = new BSONDocument(); var binary = new BSONBinary(BSONBinaryType.BinaryOld, data); root.Set(new BSONBinaryElement("binary", binary)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 37); // ensure document length is 37 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(37)); // content length is 37 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Binary); // element type is binary 0x05 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("binary")); // element name is 'binary' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(19)); // byte length is 19 Assert.AreEqual(reader.ReadByte(), (byte)BSONBinaryType.BinaryOld); // binary type is BinaryOld 0x02 CollectionAssert.AreEqual(reader.ReadBytes(19), data); // byte content is correct Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 37); // ensure whole document readed } }
public void WriteSingleDouble() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONDoubleElement("pi", Math.PI)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 17); // ensure document length is 17 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(17)); // ensure content length is 17 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Double); // ensure element type is double 0x01 CollectionAssert.AreEqual(reader.ReadBytes(2), Encoding.UTF8.GetBytes("pi")); // ensure element name is 'pi' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(Math.PI)); // ensure element value is Math.PI Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 17); // ensure whole document readed } }
public void WriteSingleDateTime() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var now = new DateTime(635000000000000000).ToUniversalTime(); var root = new BSONDocument(); root.Set(new BSONDateTimeElement("now", now)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 18); // ensure document length is 18 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(18)); // ensure content length is 18 Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.DateTime); // ensure element type is DateTime 0x09 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("now")); // ensure element name is 'now' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(now.ToMillisecondsSinceUnixEpochStart())); // ensure element value is correct Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 18); // ensure whole document readed } }
public void WriteSingleInt64() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONInt64Element("solarSystemDiameter", 10000000000000)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 34); // ensure document length is 34 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(34)); // ensure content length is 34 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int64); // ensure element type is int64 0x12 CollectionAssert.AreEqual(reader.ReadBytes(19), Encoding.UTF8.GetBytes("solarSystemDiameter")); // ensure element name is 'solarSystemDiameter' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(10000000000000)); // ensure element value is 10000000000000 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 34); // ensure whole document readed } }
public void WriteSingleInt32() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONInt32Element("lucky", 7)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 16); // ensure document length is 16 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(16)); // ensure content length is 16 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // ensure element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("lucky")); // ensure element name is 'lucky' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(7)); // ensure element value is 7 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 16); // ensure whole document readed } }
public void WriteSingleObjectId() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var hex = "507f1f77bcf86cd799439011"; var data = Enumerable.Range(0, hex.Length) .Where(x => x % 2 == 0) .Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) .ToArray(); var root = new BSONDocument(); var objectId = new BSONObjectID(data); root.Set(new BSONObjectIDElement("objectId", objectId)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 27); // ensure document length is 27 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(27)); // content length is 27 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.ObjectID); // element type is objectID 0x07 CollectionAssert.AreEqual(reader.ReadBytes(8), Encoding.UTF8.GetBytes("objectId")); // element name is 'objectId' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(12), data); // byte content is correct Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 27); // ensure whole document readed } }
public void WriteInt32Array() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var array = new[] { new BSONInt32Element(1963), new BSONInt32Element(1984), new BSONInt32Element(2015) }; root.Set(new BSONArrayElement("years", array)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 38); // ensure document length is 38 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(38)); // document's content length is 38 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Array); // element type is array 0x04 CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("years")); // element name is 'years' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(26)); // array's content length is 26 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("0")); // element name is '0' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(1963)); // value is 1963 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("1")); // element name is '1' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(1984)); // value is 1984 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("2")); // element name is '2' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(2015)); // value is 2015 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 38); // ensure whole document readed } }
public void WriteStringArray() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var array = new[] { new BSONStringElement("apple"), new BSONStringElement("orange"), new BSONStringElement("plum") }; root.Set(new BSONArrayElement("fruits", array)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 57); // ensure document length is 57 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(57)); // document's content length is 57 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Array); // element type is array 0x04 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("fruits")); // element name is 'fruits' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(44)); // array's content length is 44 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("0")); // element name is '0' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(6)); // string content length is 6 CollectionAssert.AreEqual(reader.ReadBytes(5),Encoding.UTF8.GetBytes("apple")); // string content length is 'apple' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("1")); // element name is '1' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(7)); // string content length is 7 CollectionAssert.AreEqual(reader.ReadBytes(6),Encoding.UTF8.GetBytes("orange")); // string content length is 'orange' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("2")); // element name is '2' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(5)); // string content length is 5 CollectionAssert.AreEqual(reader.ReadBytes(4),Encoding.UTF8.GetBytes("plum")); // string content length is 'plum' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 57); // ensure whole document readed } }
public void WriteReadSingleDateTime() { using (var stream = new MemoryStream()) { var now = new DateTime(2015, 08, 26, 14, 23, 56); var bson1 = new BSONDocument(); bson1.Set(new BSONDateTimeElement("now", now)); bson1.WriteAsBSON(stream); stream.Position = 0; var bson2 = new BSONDocument(stream); var now1 = ((BSONDateTimeElement)bson1["now"]).Value; var now2 = ((BSONDateTimeElement)bson2["now"]).Value; Console.WriteLine("{0} {1}", now1, now1.Kind); Console.WriteLine("{0} {1}", now2, now2.Kind); Assert.AreEqual(now1.ToUniversalTime(), now2); } }
public static BSONDocumentElement Amount_CLRtoBSON(string name, Amount amount) { var curEl = new BSONStringElement("c", amount.CurrencyISO); var valEl = Decimal_CLRtoBSON("v", amount.Value); var doc = new BSONDocument(); doc.Set(curEl).Set(valEl); return name != null ? new BSONDocumentElement(name, doc) : new BSONDocumentElement(doc); }
/// <summary> /// override to perform the conversion. the data is never null here, and ref cycles a ruled out /// </summary> protected virtual BSONElement DoConvertCLRtoBSON(string name, object data, Type dataType, string targetName) { //1 Primitive/direct types Func<string, object, BSONElement> func; if (m_CLRtoBSON.TryGetValue(dataType, out func)) return func(name, data); //2 Enums if (dataType.IsEnum) return name != null ? new BSONStringElement(name, data.ToString()) : new BSONStringElement(data.ToString()); //3 Complex Types if (data is Row) return this.RowToBSONDocumentElement((Row)data, targetName, name: name); //IDictionary //must be before IEnumerable if (data is IDictionary) { var dict = (IDictionary)data; var result = new BSONDocument(); foreach( var key in dict.Keys) { var fldName = key.ToString(); var el = ConvertCLRtoBSON(fldName, dict[key], targetName); result.Set(el); } return name != null ? new BSONDocumentElement(name, result) : new BSONDocumentElement(result); } //IEnumerable if (data is IEnumerable) { var list = (IEnumerable)data; List<BSONElement> elements = new List<BSONElement>(); foreach( var obj in list) { var el = ConvertCLRtoBSON(null, obj, targetName); elements.Add(el); } var result = name != null ? new BSONArrayElement(name, elements.ToArray()) : new BSONArrayElement(elements.ToArray()); return result; } throw new BSONException(StringConsts.CLR_BSON_CONVERSION_TYPE_NOT_SUPPORTED_ERROR.Args(dataType.FullName)); }
/// <summary> /// Converts row to BSON document suitable for storage in MONGO.DB. /// Pass target name (name of particular store/epoch/implementation) to get targeted field metadata. /// Note: the supplied row MAY NOT CONTAIN REFERENCE CYCLES - either direct or transitive /// </summary> public virtual BSONDocumentElement RowToBSONDocumentElement(Row row, string targetName, bool useAmorphousData = true, string name= null) { if (row==null) return null; var amrow = row as IAmorphousData; if (amrow!=null && useAmorphousData && amrow.AmorphousDataEnabled) { amrow.BeforeSave(targetName); } var result = new BSONDocument(); foreach(var field in row.Schema) { var attr = field[targetName]; if (attr!=null && attr.StoreFlag!=StoreFlag.OnlyStore && attr.StoreFlag!=StoreFlag.LoadAndStore) continue; var el = GetFieldAsBSON(row, field, targetName); result.Set( el ); } if (amrow!=null && useAmorphousData && amrow.AmorphousDataEnabled) foreach(var kvp in amrow.AmorphousData) { result.Set( GetAmorphousFieldAsBSON(kvp, targetName) ); } return name != null ? new BSONDocumentElement(name, result) : new BSONDocumentElement(result); }
public void WriteSingleTimestamp() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var now = new DateTime(635000000000000000).ToUniversalTime(); var stamp = new BSONTimestamp(now, 123); root.Set(new BSONTimestampElement("stamp", stamp)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 20); // ensure document length is 20 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(20)); // content length is 20 Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.TimeStamp); // element type is TimeStamp 0x11 CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("stamp")); // element name is 'now' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(123)); // increment is correct CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes((int)now.ToSecondsSinceUnixEpochStart())); // datetime is correct Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // last byte is terminator 0x00 Assert.AreEqual(stream.Position, 20); // ensure whole document readed } }
public void WriteSingleString() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONStringElement("greetings", "Hello World!")); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 33); // ensure document length is 33 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(33)); // ensure content length is 33 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // ensure element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(9), Encoding.UTF8.GetBytes("greetings")); // ensure element name is 'greetings' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(13)); // ensure string content length is 13 CollectionAssert.AreEqual(reader.ReadBytes(12), Encoding.UTF8.GetBytes("Hello World!")); // ensure element value is 'Hello World!' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 33); // ensure whole document readed } }
public void WriteSingleNull() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONNullElement("null")); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 11); // ensure document length is 11 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(11)); // content length is 11 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Null); // element type is null 0x0a CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("null")); // element name is 'null' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(stream.Position, 11); // ensure whole document readed } }
public void WriteStringInt32DoubleMixedArray() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var array = new BSONElement[] { new BSONStringElement("apple"), new BSONInt32Element(3), new BSONDoubleElement(2.14D) }; root.Set(new BSONArrayElement("stuff", array)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 48); // ensure document length is 48 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(48)); // document's content length is 48 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Array); // element type is array 0x04 CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("stuff")); // element name is 'stuff' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(36)); // array's content length is 36 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("0")); // element name is '0' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(6)); // string content length is 6 CollectionAssert.AreEqual(reader.ReadBytes(5),Encoding.UTF8.GetBytes("apple")); // string content length is 'apple' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("1")); // element name is '1' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(3)); // value is 3 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Double); // element type is double 0x01 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("2")); // element name is '2' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(2.14D)); // value is 2.14 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 48); // ensure whole document readed } }
public void WriteReadStringsWithDifferentLengths() { Parallel.For(0, 10*1024, i => { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { // Write var root = new BSONDocument(); var value = new string('a', i); root.Set(new BSONStringElement("vary", value)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 16 + i); // ensure document length is 16+i bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(16 + i)); // content length is 16+i Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("vary")); // element name is 'vary' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(i + 1)); // string content length is 13 CollectionAssert.AreEqual(reader.ReadBytes(i), Encoding.UTF8.GetBytes(value)); // element value is value Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // last byte is terminator 0x00 Assert.AreEqual(stream.Position, 16 + i); // ensure whole document readed stream.Position = 0; // Read var deser = new BSONDocument(stream); Assert.AreEqual(deser.ByteSize, 16 + i); Assert.AreEqual(deser.Count, 1); var element = deser["vary"] as BSONStringElement; Assert.IsNotNull(element); Assert.AreEqual(element.ElementType, BSONElementType.String); Assert.AreEqual(element.Name, "vary"); Assert.AreEqual(element.Value, value); Assert.AreEqual(stream.Position, 16 + i); // ensure whole document readed stream.Position = 0; } }); }
public void WriteSingleMinKey() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONMinKeyElement("minkey")); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 13); // ensure document length is 13 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(13)); // ensure content length is 13 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.MinKey); // ensure element type is MinKey 0xff CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("minkey")); // ensure element name is 'minkey' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 13); // ensure whole document readed } }
public void WriteNestedDocument() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var nested = new BSONDocument(); nested.Set(new BSONStringElement("capital", "Moscow")); root.Set(new BSONDocumentElement("nested", nested)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 38); // ensure document length is 38 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(38)); // content length is 38 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Document); // element type is document 0x03 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("nested")); // element name is 'nested' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(25)); // nested document length is 25 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("capital")); // element name is 'capital' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(7)); // string length is 7 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("Moscow")); // element value is 'Moscow' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(stream.Position, 38); // ensure whole document readed } }
public void WriteSingleJavaScriptWithScope() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var code = "function(){var x=1;var y='abc';return z;};"; var scope = new BSONDocument(); scope.Set(new BSONInt32Element("z", 23)); var jsWithScope = new BSONCodeWithScope(code, scope); root.Set(new BSONJavaScriptWithScopeElement("codeWithScope", jsWithScope)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 83); // ensure document length is 83 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(83)); // content length is 83 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.JavaScriptWithScope); // element type is JavaScriptWithScope 0x0f CollectionAssert.AreEqual(reader.ReadBytes(13), Encoding.UTF8.GetBytes("codeWithScope")); // element name is 'codeWithScope' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(63)); // full content length is 63 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(43)); // content length is 43 CollectionAssert.AreEqual(reader.ReadBytes(42), Encoding.UTF8.GetBytes(code)); // element value is code Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string value terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(12)); // full scope content length is 12 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int 0x10 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("z")); // element name is 'z' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(23)); // z variable value is 23 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(stream.Position, 83); // ensure whole document readed } }
public void WriteSingleJavaScript() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var code = "function(){var x=1;var y='abc';return 1;};"; root.Set(new BSONJavaScriptElement("code", code)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 58); // ensure document length is 58 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(58)); // content length is 58 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.JavaScript); // element type is JavaScript 0x0d CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("code")); // element name is 'code' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(43)); // js code content length is 43 CollectionAssert.AreEqual(reader.ReadBytes(42), Encoding.UTF8.GetBytes(code)); // element value is code Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(stream.Position, 58); // ensure whole document readed } }
public void WriteSingleRegularExpression() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var pattern = @"^[-.\w]+@(?:[a-z\d]{2,}\.)+[a-z]{2,6}$"; var options = BSONRegularExpressionOptions.I | BSONRegularExpressionOptions.M |BSONRegularExpressionOptions.U; var regex = new BSONRegularExpression(pattern, options); root.Set(new BSONRegularExpressionElement("email", regex)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 55); // ensure document length is 55 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(55)); // ensure content length is 55 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.RegularExpression); // ensure element type is RegularExpression 0x0b CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("email")); // ensure element name is 'email' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(38), Encoding.UTF8.GetBytes(pattern)); // ensure element value is pattern Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string value terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("imu")); // ensure element value is options in BSON format Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 55); // ensure whole document readed } }
public void WriteUnicodeStrings() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONStringElement("eng", "hello")); root.Set(new BSONStringElement("rus", "привет")); root.Set(new BSONStringElement("chi", "你好")); root.Set(new BSONStringElement("jap", "こんにちは")); root.Set(new BSONStringElement("gre", "γεια σας")); root.Set(new BSONStringElement("alb", "përshëndetje")); root.Set(new BSONStringElement("arm", "բարեւ Ձեզ")); root.Set(new BSONStringElement("vie", "xin chào")); root.Set(new BSONStringElement("por", "Olá")); root.Set(new BSONStringElement("ukr", "Привіт")); root.Set(new BSONStringElement("ger", "wünsche")); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 232); // ensure document length is 33 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(232)); // content length is 232 Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("eng")); // element name is 'eng' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(6)); // string content length is 6 CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("hello")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("rus")); // element name is 'rus' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(13)); // string content length is 13 CollectionAssert.AreEqual(reader.ReadBytes(12), Encoding.UTF8.GetBytes("привет")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("chi")); // element name is 'chi' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(7)); // string content length is 7 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("你好")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("jap")); // element name is 'jap' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(16)); // string content length is 16 CollectionAssert.AreEqual(reader.ReadBytes(15), Encoding.UTF8.GetBytes("こんにちは")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("gre")); // element name is 'gre' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(16)); // string content length is 16 CollectionAssert.AreEqual(reader.ReadBytes(15), Encoding.UTF8.GetBytes("γεια σας")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("alb")); // element name is 'alb' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(15)); // string content length is 15 CollectionAssert.AreEqual(reader.ReadBytes(14), Encoding.UTF8.GetBytes("përshëndetje")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("arm")); // element name is 'arm' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(18)); // string content length is 18 CollectionAssert.AreEqual(reader.ReadBytes(17), Encoding.UTF8.GetBytes("բարեւ Ձեզ")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("vie")); // element name is 'vie' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(10)); // string content length is 10 CollectionAssert.AreEqual(reader.ReadBytes(9), Encoding.UTF8.GetBytes("xin chào")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("por")); // element name is 'por' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(5)); // string content length is 5 CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("Olá")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("ukr")); // element name is 'ukr' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(13)); // string content length is 13 CollectionAssert.AreEqual(reader.ReadBytes(12), Encoding.UTF8.GetBytes("Привіт")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("ger")); // element name is 'ger' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(9)); // string content length is 9 CollectionAssert.AreEqual(reader.ReadBytes(8), Encoding.UTF8.GetBytes("wünsche")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 232); // ensure whole document readed } }
public void WriteBigIntegers() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONInt32Element("intMin", int.MinValue)); root.Set(new BSONInt32Element("intMax", int.MaxValue)); root.Set(new BSONInt64Element("longMin", long.MinValue)); root.Set(new BSONInt64Element("longMax", long.MaxValue)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 63); // ensure document length is 63 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(63)); // content length is 63 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("intMin")); // element name is 'intMin' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(int.MinValue)); // element value is int.MinValue Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("intMax")); // element name is 'intMax' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(int.MaxValue)); // element value is int.MaxValue Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int64); // element type is int64 0x12 CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("longMin")); // element name is 'longMin' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(long.MinValue)); // element value is long.MinValue Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int64); // element type is int64 0x12 CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("longMax")); // element name is 'longMax' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(long.MaxValue)); // element value is long.MaxValue Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 63); // ensure whole document readed } }
public void WriteStringAndInt32Pair() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONStringElement("name", "Gagarin")); root.Set(new BSONInt32Element("birth", 1934)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 34); // ensure document length is 38 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(34)); // ensure content length is 34 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // ensure element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("name")); // ensure element name is 'name' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(8)); // ensure string content length is 8 CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("Gagarin")); // ensure element value is 'Gagarin' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // ensure element type is int 0x10 CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("birth")); // ensure element name is 'birth' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(1934)); // ensure element value is int 1934 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 34); // ensure whole document readed } }
private BSONDocument docFromMessage(Message msg) { var doc = new BSONDocument(); var rc = new RowConverter(); doc.Set(new BSONStringElement("Guid", msg.Guid.ToString("N"))); doc.Set(new BSONStringElement("RelatedTo", msg.RelatedTo.ToString("N"))); doc.Set(new BSONStringElement("Type", msg.Type.ToString())); doc.Set(new BSONInt32Element("Source", msg.Source)); doc.Set(new BSONInt64Element("TimeStamp", msg.TimeStamp.Ticks)); doc.Set(new BSONStringElement("Host", msg.Host)); doc.Set(new BSONStringElement("From", msg.From)); doc.Set(new BSONStringElement("Topic", msg.Topic)); doc.Set(new BSONStringElement("Text", msg.Text)); doc.Set(new BSONStringElement("Parameters", msg.Parameters)); doc.Set(new BSONStringElement("Exception", msg.Exception.ToMessageWithType())); doc.Set(new BSONInt32Element("ThreadID", msg.ThreadID)); return doc; }