// protected methods protected override void WriteBody(BsonBuffer buffer) { buffer.WriteInt32((int)_flags); buffer.WriteCString(new UTF8Encoding(false, true), _collectionFullName); _firstDocumentStartPosition = buffer.Position; // documents to be added later by calling AddDocument }
public static BsonWriter Create( BsonBuffer buffer, BsonBinaryWriterSettings settings ) { return new BsonBinaryWriter(null, buffer, settings); }
internal void ResetBatch(BsonBuffer buffer, byte[] lastDocument) { buffer.Position = _firstDocumentStartPosition; buffer.Length = _firstDocumentStartPosition; buffer.WriteBytes(lastDocument); BackpatchMessageLength(buffer); }
internal virtual void WriteHeaderTo(BsonBuffer buffer) { buffer.WriteInt32(0); // messageLength will be backpatched later buffer.WriteInt32(_requestId); buffer.WriteInt32(0); // responseTo not used in requests sent by client buffer.WriteInt32((int)_opcode); }
// public methods public WriteConcernResult Execute(MongoConnection connection) { var serverInstance = connection.ServerInstance; if (serverInstance.Supports(FeatureId.WriteCommands) && _args.WriteConcern.Enabled) { var emulator = new UpdateOpcodeOperationEmulator(_args); return emulator.Execute(connection); } SendMessageWithWriteConcernResult sendMessageResult; using (var buffer = new BsonBuffer(new MultiChunkBuffer(BsonChunkPool.Default), true)) { var requests = _args.Requests.ToList(); if (requests.Count != 1) { throw new NotSupportedException("Update opcode only supports a single update request."); } var updateRequest = (UpdateRequest)requests[0]; var flags = UpdateFlags.None; if (updateRequest.IsMultiUpdate ?? false) { flags |= UpdateFlags.Multi; } if (updateRequest.IsUpsert ?? false) { flags |= UpdateFlags.Upsert; } var maxDocumentSize = connection.ServerInstance.MaxDocumentSize; var query = updateRequest.Query ?? new QueryDocument(); var message = new MongoUpdateMessage(WriterSettings, CollectionFullName, _args.CheckElementNames, flags, maxDocumentSize, query, updateRequest.Update); message.WriteTo(buffer); sendMessageResult = SendMessageWithWriteConcern(connection, buffer, message.RequestId, ReaderSettings, WriterSettings, WriteConcern); } return WriteConcern.Enabled ? ReadWriteConcernResult(connection, sendMessageResult) : null; }
// protected methods protected override void WriteBody(BsonBuffer buffer) { buffer.WriteInt32(0); // reserved buffer.WriteCString(new UTF8Encoding(false, true), _collectionFullName); buffer.WriteInt32(_numberToReturn); buffer.WriteInt64(_cursorId); }
public static BsonReader Create( BsonBuffer buffer, BsonBinaryReaderSettings settings ) { return new BsonBinaryReader(buffer, settings); }
public void Test20KDocument() { // manufacture an approximately 20K document using 200 strings each 100 characters long // it's enough to cause the document to straddle a chunk boundary var document = new BsonDocument(); var value = new string('x', 100); for (int i = 0; i < 200; i++) { var name = i.ToString(); document.Add(name, value); } // round trip tests var bson = document.ToBson(); var rehydrated = BsonSerializer.Deserialize<BsonDocument>(bson); Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); // test failure mode when 20 bytes are truncated from the buffer using (var buffer = new BsonBuffer()) { buffer.LoadFrom(new MemoryStream(bson)); buffer.Length -= 20; using (var bsonReader = BsonReader.Create(buffer)) { Assert.Throws<EndOfStreamException>(() => BsonSerializer.Deserialize<BsonDocument>(bsonReader)); } } }
// internal methods internal override void WriteBodyTo(BsonBuffer buffer) { var processedRequests = new List<InsertRequest>(); var continuationBatch = _batch as ContinuationBatch<InsertRequest, byte[]>; if (continuationBatch != null) { AddOverflow(buffer, continuationBatch.PendingState); processedRequests.Add(continuationBatch.PendingItem); continuationBatch.ClearPending(); // so pending objects can be garbage collected sooner } // always go one document too far so that we can set IsDone as early as possible var enumerator = _batch.Enumerator; while (enumerator.MoveNext()) { var request = enumerator.Current; AddRequest(buffer, request); if ((_batchCount > _maxBatchCount || _batchLength > _maxBatchLength) && _batchCount > 1) { var serializedDocument = RemoveLastDocument(buffer); var nextBatch = new ContinuationBatch<InsertRequest, byte[]>(enumerator, request, serializedDocument); _batchProgress = new BatchProgress<InsertRequest>(_batchCount, _batchLength, processedRequests, nextBatch); return; } processedRequests.Add(request); } _batchProgress = new BatchProgress<InsertRequest>(_batchCount, _batchLength, processedRequests, null); }
internal override void WriteHeaderTo(BsonBuffer buffer) { base.WriteHeaderTo(buffer); buffer.WriteInt32(0); // reserved buffer.WriteCString(new UTF8Encoding(false, true), _collectionFullName); buffer.WriteInt32(_numberToReturn); }
protected void WriteMessageHeaderTo( BsonBuffer buffer ) { buffer.WriteInt32(0); // messageLength will be backpatched later buffer.WriteInt32(requestId); buffer.WriteInt32(0); // responseTo not used in requests sent by client buffer.WriteInt32((int) opcode); }
// internal methods internal override void WriteBodyTo(BsonBuffer buffer) { buffer.WriteInt32(_cursorIds.Length); foreach (long cursorId in _cursorIds) { buffer.WriteInt64(cursorId); } }
public void Dispose() { if (!disposed) { if (disposeBuffer) { buffer.Dispose(); } buffer = null; disposed = true; } }
internal byte[] RemoveLastDocument(BsonBuffer buffer) { var lastDocumentLength = buffer.Position - _lastDocumentStartPosition; buffer.Position = _lastDocumentStartPosition; var lastDocument = buffer.ReadBytes(lastDocumentLength); buffer.Length = _lastDocumentStartPosition; BackpatchMessageLength(buffer); return lastDocument; }
public BsonBinaryReader( BsonBuffer buffer, BsonBinaryReaderSettings settings ) { this.buffer = buffer ?? new BsonBuffer(); this.disposeBuffer = buffer == null; // only call Dispose if we allocated the buffer this.settings = settings; context = null; }
// protected methods protected override void WriteBody(BsonBuffer buffer) { buffer.WriteInt32(0); // reserved buffer.WriteInt32(_cursorIds.Length); foreach (long cursorId in _cursorIds) { buffer.WriteInt64(cursorId); } }
public override void Dispose() { if (!disposed) { Close(); if (disposeBuffer) { buffer.Dispose(); buffer = null; } disposed = true; } }
// protected methods protected void ReadMessageHeaderFrom(BsonBuffer buffer) { messageLength = buffer.ReadInt32(); requestId = buffer.ReadInt32(); responseTo = buffer.ReadInt32(); if ((MessageOpcode)buffer.ReadInt32() != opcode) { throw new FileFormatException("Message header opcode is not the expected one."); } }
// internal methods internal virtual void ReadHeaderFrom(BsonBuffer buffer) { _messageLength = buffer.ReadInt32(); _requestId = buffer.ReadInt32(); _responseTo = buffer.ReadInt32(); if ((MessageOpcode)buffer.ReadInt32() != _opcode) { throw new FileFormatException("Message header opcode is not the expected one."); } }
internal MongoQueryMessage(BsonBuffer buffer, BsonBinaryWriterSettings writerSettings, string collectionFullName, QueryFlags flags, int numberToSkip, int numberToReturn, IMongoQuery query, IMongoFields fields) : base(MessageOpcode.Query, buffer, writerSettings) { this.collectionFullName = collectionFullName; this.flags = flags; this.numberToSkip = numberToSkip; this.numberToReturn = numberToReturn; this.query = query; this.fields = fields; }
// internal methods internal void AddDocument(BsonBuffer buffer, Type nominalType, object document) { _lastDocumentStartPosition = buffer.Position; using (var bsonWriter = new BsonBinaryWriter(buffer, false, WriterSettings)) { bsonWriter.CheckElementNames = _checkElementNames; BsonSerializer.Serialize(bsonWriter, nominalType, document, DocumentSerializationOptions.SerializeIdFirstInstance); } BackpatchMessageLength(buffer); }
public BsonBinaryReader( BsonBuffer buffer, BsonBinaryReaderSettings settings ) { this.buffer = buffer ?? new BsonBuffer(); this.disposeBuffer = buffer == null; // only call Dispose if we allocated the buffer this.settings = settings; context = null; state = BsonReadState.Initial; currentBsonType = BsonType.Document; }
protected MongoRequestMessage( MongoServer server, MessageOpcode opcode, BsonBuffer buffer // not null if piggybacking this message onto an existing buffer ) : base(server, opcode) { this.buffer = buffer ?? new BsonBuffer(); this.disposeBuffer = buffer == null; // only call Dispose if we allocated the buffer this.requestId = Interlocked.Increment(ref lastRequestId); }
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); }
public WriteConcernResult Execute(MongoConnection connection) { using (var buffer = new BsonBuffer(new MultiChunkBuffer(BsonChunkPool.Default), true)) { var readerSettings = GetNodeAdjustedReaderSettings(connection.ServerInstance); var writerSettings = GetNodeAdjustedWriterSettings(connection.ServerInstance); var message = new MongoDeleteMessage(writerSettings, CollectionFullName, _flags, _query); message.WriteToBuffer(buffer); return SendMessageWithWriteConcern(connection, buffer, message.RequestId, readerSettings, writerSettings, WriteConcern); } }
protected WriteConcernResult SendMessageWithWriteConcern( MongoConnection connection, BsonBuffer buffer, int requestId, BsonBinaryReaderSettings readerSettings, BsonBinaryWriterSettings writerSettings, WriteConcern writeConcern) { CommandDocument getLastErrorCommand = null; if (writeConcern.Enabled) { var fsync = (writeConcern.FSync == null) ? null : (BsonValue)writeConcern.FSync; var journal = (writeConcern.Journal == null) ? null : (BsonValue)writeConcern.Journal; var w = (writeConcern.W == null) ? null : writeConcern.W.ToGetLastErrorWValue(); var wTimeout = (writeConcern.WTimeout == null) ? null : (BsonValue)(int)writeConcern.WTimeout.Value.TotalMilliseconds; getLastErrorCommand = new CommandDocument { { "getlasterror", 1 }, // use all lowercase for backward compatibility { "fsync", fsync, fsync != null }, { "j", journal, journal != null }, { "w", w, w != null }, { "wtimeout", wTimeout, wTimeout != null } }; // piggy back on network transmission for message var getLastErrorMessage = new MongoQueryMessage(writerSettings, DatabaseName + ".$cmd", QueryFlags.None, 0, 1, getLastErrorCommand, null); getLastErrorMessage.WriteToBuffer(buffer); } connection.SendMessage(buffer, requestId); WriteConcernResult writeConcernResult = null; if (writeConcern.Enabled) { var writeConcernResultSerializer = BsonSerializer.LookupSerializer(typeof(WriteConcernResult)); var replyMessage = connection.ReceiveMessage<WriteConcernResult>(readerSettings, writeConcernResultSerializer, null); if (replyMessage.NumberReturned == 0) { throw new MongoCommandException("Command 'getLastError' failed. No response returned"); } writeConcernResult = replyMessage.Documents[0]; writeConcernResult.Command = getLastErrorCommand; var mappedException = ExceptionMapper.Map(writeConcernResult); if (mappedException != null) { throw mappedException; } } return writeConcernResult; }
// public methods public void Dispose() { if (!_disposed) { if (_disposeBuffer) { _buffer.Dispose(); } _buffer = null; _disposed = true; } }
// internal methods internal void WriteToBuffer(BsonBuffer buffer) { // normally this method is only called once (from MongoConnection.SendMessage) // but in the case of InsertBatch it is called before SendMessage is called to initialize the message so that AddDocument can be called // therefore we need the if statement to ignore subsequent calls from SendMessage if (_messageStartPosition == -1) { _messageStartPosition = buffer.Position; WriteMessageHeaderTo(buffer); WriteBody(buffer); BackpatchMessageLength(buffer); } }
internal override void WriteHeaderTo(BsonBuffer buffer) { if ((_flags & QueryFlags.Exhaust) != 0) { throw new NotSupportedException("The Exhaust QueryFlag is not yet supported."); } base.WriteHeaderTo(buffer); buffer.WriteInt32((int)_flags); buffer.WriteCString(new UTF8Encoding(false, true), _collectionFullName); buffer.WriteInt32(_numberToSkip); buffer.WriteInt32(_numberToReturn); }
/// <summary> /// Initializes a new instance of the BsonBinaryReader class. /// <param name="buffer">A BsonBuffer.</param> /// <param name="settings">A BsonBinaryReaderSettings.</param> /// </summary> public BsonBinaryReader( BsonBuffer buffer, BsonBinaryReaderSettings settings ) { if (buffer == null) { this.buffer = new BsonBuffer(); this.disposeBuffer = true; // only call Dispose if we allocated the buffer } else { this.buffer = buffer; this.disposeBuffer = false; } this.settings = settings.Freeze(); context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0); }
// constructors /// <summary> /// Initializes a new instance of the BsonBinaryReader class. /// </summary> /// <param name="buffer">A BsonBuffer.</param> /// <param name="settings">A BsonBinaryReaderSettings.</param> public BsonBinaryReader(BsonBuffer buffer, BsonBinaryReaderSettings settings) : this(buffer ?? new BsonBuffer(), buffer == null, settings) { }
// public static methods /// <summary> /// Creates a BsonReader for a BsonBuffer. /// </summary> /// <param name="buffer">The BsonBuffer.</param> /// <returns>A BsonReader.</returns> public static BsonReader Create(BsonBuffer buffer) { return(Create(buffer, BsonBinaryReaderSettings.Defaults)); }
/// <summary> /// Creates a BsonReader for a BsonBuffer. /// </summary> /// <param name="buffer">The BsonBuffer.</param> /// <param name="settings">Optional reader settings.</param> /// <returns>A BsonReader.</returns> public static BsonReader Create(BsonBuffer buffer, BsonBinaryReaderSettings settings) { return(new BsonBinaryReader(buffer, false, settings)); }
// constructors /// <summary> /// Initializes a new instance of the BsonBinaryWriter class. /// </summary> /// <param name="stream">A stream.</param> /// <param name="buffer">A BsonBuffer.</param> /// <param name="settings">Optional BsonBinaryWriter settings.</param> public BsonBinaryWriter(Stream stream, BsonBuffer buffer, BsonBinaryWriterSettings settings) : this(buffer ?? new BsonBuffer(), buffer == null, settings) { _stream = stream; }
/// <summary> /// Creates a BsonWriter to a BsonBuffer. /// </summary> /// <param name="buffer">A BsonBuffer.</param> /// <returns>A BsonWriter.</returns> public static BsonWriter Create( BsonBuffer buffer ) { return(new BsonBinaryWriter(null, buffer, BsonBinaryWriterSettings.Defaults)); }