internal void ResetBatch(BsonBuffer buffer, byte[] lastDocument) { buffer.Position = _firstDocumentStartPosition; buffer.Length = _firstDocumentStartPosition; buffer.WriteBytes(lastDocument); BackpatchMessageLength(buffer); }
public void TestBenchmarks() { int iterations; DateTime start; DateTime end; TimeSpan duration; iterations = 1; start = DateTime.UtcNow; for (int i = 0; i < iterations; i++) { // about 2.06 on my machine //var doc = new BsonDocument { // { "a", 1 }, // { "b", 2.0 }, // { "c", "hello" }, // { "d", DateTime.UtcNow }, // { "e", true } // }; byte[] value = { 1, 2, 3, 4 }; MemoryStream stream = new MemoryStream(); for (int n = 0; n < 100000; n++) { stream.Write(value, 0, 4); } } end = DateTime.UtcNow; duration = end - start; System.Diagnostics.Debug.WriteLine(duration); start = DateTime.UtcNow; for (int i = 0; i < iterations; i++) { // about 2.22 on my machine //var doc = new BsonDocument { // { "a", BsonValue.Create((object) 1) }, // { "b", BsonValue.Create((object) 2.0) }, // { "c", BsonValue.Create((object) "hello") }, // { "d", BsonValue.Create((object) DateTime.UtcNow) }, // { "e", BsonValue.Create((object) true) } //}; byte[] value = { 1, 2, 3, 4 }; using (var buffer = new BsonBuffer()) { for (int n = 0; n < 100000; n++) { buffer.WriteBytes(value); } } } end = DateTime.UtcNow; duration = end - start; System.Diagnostics.Debug.WriteLine(duration); }
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The representation for Guids.</param> public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation // ignored since BSON doesn't have a place to store this ) { if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (state != BsonWriterState.Value) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value); } buffer.WriteByte((byte)BsonType.Binary); WriteNameHelper(); if (subType == BsonBinarySubType.OldBinary && settings.FixOldBinarySubTypeOnOutput) { subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type } if (subType == BsonBinarySubType.OldBinary) { // sub type OldBinary has two sizes (for historical reasons) buffer.WriteInt32(bytes.Length + 4); buffer.WriteByte((byte)subType); buffer.WriteInt32(bytes.Length); } else { buffer.WriteInt32(bytes.Length); buffer.WriteByte((byte)subType); } buffer.WriteBytes(bytes); state = GetNextState(); }
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType ) { if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (state != BsonWriterState.Value) { var message = string.Format("WriteBinaryData cannot be called when State is: {0}", state); throw new InvalidOperationException(message); } buffer.WriteByte((byte)BsonType.Binary); WriteNameHelper(); if (subType == BsonBinarySubType.OldBinary && settings.FixOldBinarySubTypeOnOutput) { subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type } if (subType == BsonBinarySubType.OldBinary) { // sub type OldBinary has two sizes (for historical reasons) buffer.WriteInt32(bytes.Length + 4); buffer.WriteByte((byte)subType); buffer.WriteInt32(bytes.Length); } else { buffer.WriteInt32(bytes.Length); buffer.WriteByte((byte)subType); } buffer.WriteBytes(bytes); state = GetNextState(); }
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Writes BSON binary data to the writer. /// </summary> /// <param name="bytes">The binary data.</param> /// <param name="subType">The binary data subtype.</param> /// <param name="guidRepresentation">The representation for Guids.</param> public override void WriteBinaryData( byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation ) { if (disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); } if (state != BsonWriterState.Value) { ThrowInvalidState("WriteBinaryData", BsonWriterState.Value); } switch (subType) { case BsonBinarySubType.OldBinary: if (settings.FixOldBinarySubTypeOnOutput) { subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type } break; case BsonBinarySubType.UuidLegacy: case BsonBinarySubType.UuidStandard: if (settings.GuidRepresentation != GuidRepresentation.Unspecified) { var expectedSubType = (settings.GuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy; if (subType != expectedSubType) { var message = string.Format("The GuidRepresentation for the writer is {0}, which requires the subType argument to be {1}, not {2}.", settings.GuidRepresentation, expectedSubType, subType); throw new BsonSerializationException(message); } if (guidRepresentation != settings.GuidRepresentation) { var message = string.Format("The GuidRepresentation for the writer is {0}, which requires the the guidRepresentation argument to also be {0}, not {1}.", settings.GuidRepresentation, guidRepresentation); throw new BsonSerializationException(message); } } break; } buffer.WriteByte((byte)BsonType.Binary); WriteNameHelper(); if (subType == BsonBinarySubType.OldBinary) { // sub type OldBinary has two sizes (for historical reasons) buffer.WriteInt32(bytes.Length + 4); buffer.WriteByte((byte)subType); buffer.WriteInt32(bytes.Length); } else { buffer.WriteInt32(bytes.Length); buffer.WriteByte((byte)subType); } buffer.WriteBytes(bytes); state = GetNextState(); }
// private methods private void AddOverflow(BsonBuffer buffer, byte[] serializedDocument) { buffer.WriteBytes(serializedDocument); _batchCount++; _batchLength = buffer.Position - _batchStartPosition; }