public static BSONBinaryElement GDID_CLRtoBSON(string name, GDID gdid) { //As tested on Feb 27, 2015 //BinData works faster than string 8% and stores 40%-60% less data in index and data segment //Also, SEQUENTIAL keys (big endian) yield 30% smaller indexes (vs non-sequential) //ObjectId() is very similar if not identical to BinData(UserDefined) var bin = new BSONBinary(BSONBinaryType.UserDefined, gdid.Bytes); return(name != null ? new BSONBinaryElement(name, bin) : new BSONBinaryElement(bin)); }
public static BSONBinaryElement ByteBuffer_CLRtoBSON(string name, byte[] buf) { if (buf.Length > MAX_BYTE_BUFFER_SIZE) { throw new BSONException(StringConsts.BUFFER_LONGER_THAN_ALLOWED_ERROR.Args(buf.Length, MAX_BYTE_BUFFER_SIZE)); } var bsonBin = new BSONBinary(BSONBinaryType.GenericBinary, buf); return(name != null ? new BSONBinaryElement(name, bsonBin) : new BSONBinaryElement(bsonBin)); }
public static BSONElement GUID_CLRtoBSON(string name, Guid guid) { if (guid == Guid.Empty) { return(new BSONNullElement(name)); } //As tested on Feb 27, 2015 //BinData works faster than string 8% and stores 40%-60% less data in index and data segment //Also, SEQUENTIAL keys (big endian) yield 30% smaller indexes (vs non-sequential) //ObjectId() is very similar if not identical to BinData(UserDefined) var bin = new BSONBinary(BSONBinaryType.UUID, guid.ToNetworkByteOrder()); return(name != null ? new BSONBinaryElement(name, bin) : new BSONBinaryElement(bin)); }
/// <summary> /// Encodes byte buffer purposed for id, using proper binary subtype /// </summary> public static BSONElement ByteBufferID_CLRtoBSON(string name, byte[] buf) { if (buf == null) { return(new BSONNullElement(name)); } if (buf.Length > MAX_BYTE_BUFFER_SIZE) { throw new BSONException(StringConsts.BUFFER_LONGER_THAN_ALLOWED_ERROR.Args(buf.Length, MAX_BYTE_BUFFER_SIZE)); } var bin = new BSONBinary(BSONBinaryType.UserDefined, buf); return(new BSONBinaryElement(name, bin)); }
public static BSONBinaryElement ByteBuffer_CLRtoBSON(string name, byte[] buf) { if (buf.Length > MAX_BYTE_BUFFER_SIZE) throw new BSONException(StringConsts.BUFFER_LONGER_THAN_ALLOWED_ERROR.Args(buf.Length, MAX_BYTE_BUFFER_SIZE)); var bsonBin = new BSONBinary(BSONBinaryType.GenericBinary, buf); return name != null ? new BSONBinaryElement(name, bsonBin) : new BSONBinaryElement(bsonBin); }
public static BSONBinaryElement GDID_CLRtoBSON(string name, GDID gdid) { //As tested on Feb 27, 2015 //BinData works faster than string 8% and stores 40%-60% less data in index and data segment //Also, SEQUENTIAL keys (big endian) yield 30% smaller indexes (vs non-sequential) //ObjectId() is very similar if not identical to BinData(UserDefined) var bin = new BSONBinary(BSONBinaryType.UserDefined, gdid.Bytes); return name != null ? new BSONBinaryElement(name, bin) : new BSONBinaryElement( bin); }
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 } }
/// <summary> /// Encodes byte buffer purposed for id, using proper binary subtype /// </summary> public static BSONElement ByteBufferID_CLRtoBSON(string name, byte[] buf) { if (buf == null) return new BSONNullElement(name); if (buf.Length > MAX_BYTE_BUFFER_SIZE) throw new BSONException(StringConsts.BUFFER_LONGER_THAN_ALLOWED_ERROR.Args(buf.Length, MAX_BYTE_BUFFER_SIZE)); var bin = new BSONBinary(BSONBinaryType.UserDefined, buf); return new BSONBinaryElement(name, bin); }
/// <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); }