// internal methods
        internal override void WriteBodyTo(BsonStreamWriter streamWriter)
        {
            var processedRequests = new List<InsertRequest>();

            var continuationBatch = _batch as ContinuationBatch<InsertRequest, byte[]>;
            if (continuationBatch != null)
            {
                AddOverflow(streamWriter, 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(streamWriter, request);

                if ((_batchCount > _maxBatchCount || _batchLength > _maxBatchLength) && _batchCount > 1)
                {
                    var serializedDocument = RemoveLastDocument(streamWriter.BaseStream);
                    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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Writes a raw BSON array.
        /// </summary>
        /// <param name="slice">The byte buffer containing the raw BSON array.</param>
        public virtual void WriteRawBsonArray(IByteBuffer slice)
        {
            // overridden in BsonBinaryWriter to write the raw bytes to the stream
            // for all other streams, deserialize the raw bytes and serialize the resulting array instead

            var documentLength = slice.Length + 8;

            using (var memoryStream = new MemoryStream(documentLength))
            {
                // wrap the array in a fake document so we can deserialize it
                var streamWriter = new BsonStreamWriter(memoryStream, Utf8Helper.StrictUtf8Encoding);
                streamWriter.WriteInt32(documentLength);
                streamWriter.WriteBsonType(BsonType.Array);
                streamWriter.WriteByte((byte)'x');
                streamWriter.WriteByte(0);
                slice.WriteTo(streamWriter.BaseStream);
                streamWriter.WriteByte(0);

                memoryStream.Position = 0;
                using (var bsonReader = new BsonBinaryReader(memoryStream, BsonBinaryReaderSettings.Defaults))
                {
                    var deserializationContext = BsonDeserializationContext.CreateRoot <BsonDocument>(bsonReader);
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadName("x");
                    var array = deserializationContext.DeserializeWithChildContext(BsonArraySerializer.Instance);
                    bsonReader.ReadEndDocument();

                    var serializationContext = BsonSerializationContext.CreateRoot <BsonArray>(this);
                    BsonArraySerializer.Instance.Serialize(serializationContext, array);
                }
            }
        }
 internal void WriteTo(Stream stream)
 {
     var streamWriter = new BsonStreamWriter(stream, WriterSettings.Encoding);
     WriteHeaderTo(streamWriter);
     WriteBodyTo(streamWriter);
     BackpatchMessageLength(streamWriter);
 }
Exemplo n.º 4
0
 internal virtual void WriteHeaderTo(BsonStreamWriter streamWriter)
 {
     streamWriter.WriteInt32(0); // messageLength will be backpatched later
     streamWriter.WriteInt32(_requestId);
     streamWriter.WriteInt32(0); // responseTo not used in requests sent by client
     streamWriter.WriteInt32((int)_opcode);
 }
 internal override void WriteHeaderTo(BsonStreamWriter streamWriter)
 {
     base.WriteHeaderTo(streamWriter);
     streamWriter.WriteInt32(0); // reserved
     streamWriter.WriteCString(_collectionFullName);
     streamWriter.WriteInt32((int)_flags);
 }
 // private methods
 private void Backpatch(Stream stream, int position, int value)
 {
     var streamWriter = new BsonStreamWriter(stream, Utf8Helper.StrictUtf8Encoding);
     var currentPosition = stream.Position;
     stream.Position = position;
     streamWriter.WriteInt32(value);
     stream.Position = currentPosition;
 }
 // internal methods
 internal override void WriteBodyTo(BsonStreamWriter streamWriter)
 {
     streamWriter.WriteInt32(_cursorIds.Length);
     foreach (long cursorId in _cursorIds)
     {
         streamWriter.WriteInt64(cursorId);
     }
 }
        internal override void WriteHeaderTo(BsonStreamWriter 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(_collectionFullName);
            buffer.WriteInt32(_numberToSkip);
            buffer.WriteInt32(_numberToReturn);
        }
 // internal methods
 internal override void WriteBodyTo(BsonStreamWriter streamWriter)
 {
     using (var bsonWriter = new BsonBinaryWriter(streamWriter.BaseStream, WriterSettings))
     {
         bsonWriter.PushMaxDocumentSize(_maxDocumentSize);
         if (_query == null)
         {
             bsonWriter.WriteStartDocument();
             bsonWriter.WriteEndDocument();
         }
         else
         {
             BsonSerializer.Serialize(bsonWriter, _query.GetType(), _query, b => b.SerializeIdFirst = true);
         }
         bsonWriter.PopMaxDocumentSize();
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// Initializes a new instance of the BsonBinaryWriter class.
        /// </summary>
        /// <param name="stream">A stream. The BsonBinaryWriter does not own the stream and will not Dispose it.</param>
        /// <param name="settings">The BsonBinaryWriter settings.</param>
        public BsonBinaryWriter(Stream stream, BsonBinaryWriterSettings settings)
            : base(settings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("The stream must be capable of seeking.", "stream");
            }

            _stream       = stream;
            _streamWriter = new BsonStreamWriter(stream, settings.Encoding);
            _settings     = settings; // already frozen by base class
            _maxDocumentSizeStack.Push(_settings.MaxDocumentSize);

            _context = null;
            State    = BsonWriterState.Initial;
        }
Exemplo n.º 11
0
        /// <summary>
        /// Initializes a new instance of the BsonBinaryWriter class.
        /// </summary>
        /// <param name="stream">A stream. The BsonBinaryWriter does not own the stream and will not Dispose it.</param>
        /// <param name="settings">The BsonBinaryWriter settings.</param>
        public BsonBinaryWriter(Stream stream, BsonBinaryWriterSettings settings)
            : base(settings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("The stream must be capable of seeking.", "stream");
            }

            _stream = stream;
            _streamWriter = new BsonStreamWriter(stream, settings.Encoding);
            _settings = settings; // already frozen by base class
            _maxDocumentSizeStack.Push(_settings.MaxDocumentSize);

            _context = null;
            State = BsonWriterState.Initial;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Writes a raw BSON array.
        /// </summary>
        /// <param name="slice">The byte buffer containing the raw BSON array.</param>
        public virtual void WriteRawBsonArray(IByteBuffer slice)
        {
            // overridden in BsonBinaryWriter to write the raw bytes to the stream
            // for all other streams, deserialize the raw bytes and serialize the resulting array instead

            var documentLength = slice.Length + 8;
            using (var memoryStream = new MemoryStream(documentLength))
            {
                // wrap the array in a fake document so we can deserialize it
                var streamWriter = new BsonStreamWriter(memoryStream, Utf8Helper.StrictUtf8Encoding);
                streamWriter.WriteInt32(documentLength);
                streamWriter.WriteBsonType(BsonType.Array);
                streamWriter.WriteByte((byte)'x');
                streamWriter.WriteByte(0);
                slice.WriteTo(streamWriter.BaseStream);
                streamWriter.WriteByte(0);

                memoryStream.Position = 0;
                using (var bsonReader = new BsonBinaryReader(memoryStream, BsonBinaryReaderSettings.Defaults))
                {
                    var deserializationContext = BsonDeserializationContext.CreateRoot<BsonDocument>(bsonReader);
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadName("x");
                    var array = deserializationContext.DeserializeWithChildContext(BsonArraySerializer.Instance);
                    bsonReader.ReadEndDocument();

                    var serializationContext = BsonSerializationContext.CreateRoot<BsonArray>(this);
                    BsonArraySerializer.Instance.Serialize(serializationContext, array);
                }
            }
        }
 internal override void WriteHeaderTo(BsonStreamWriter streamWriter)
 {
     base.WriteHeaderTo(streamWriter);
     streamWriter.WriteInt32(0); // reserved
 }
 internal abstract void WriteBodyTo(BsonStreamWriter streamWriter);
Exemplo n.º 15
0
        private void AddRequest(BsonStreamWriter streamWriter, InsertRequest request)
        {
            var document = request.Document;
            if (document == null)
            {
                throw new ArgumentException("Batch contains one or more null documents.");
            }

            var serializer = request.Serializer;
            if (serializer == null)
            {
                var nominalType = request.NominalType;
                if (_cachedSerializerType != nominalType)
                {
                    _cachedSerializer = BsonSerializer.LookupSerializer(nominalType);
                    _cachedSerializerType = nominalType;
                }
                serializer = _cachedSerializer;
            }

            _lastDocumentStartPosition = (int)streamWriter.Position;
            using (var bsonWriter = new BsonBinaryWriter(streamWriter.BaseStream, WriterSettings))
            {
                bsonWriter.PushMaxDocumentSize(_maxDocumentSize);
                bsonWriter.CheckElementNames = _checkElementNames;
                var context = BsonSerializationContext.CreateRoot(bsonWriter, request.NominalType, c => c.SerializeIdFirst = true);
                serializer.Serialize(context, document);
                bsonWriter.PopMaxDocumentSize();
            }

            _batchCount++;
            _batchLength = (int)streamWriter.Position - _batchStartPosition;
        }
Exemplo n.º 16
0
        // private methods
        private void AddOverflow(BsonStreamWriter streamWriter, byte[] serializedDocument)
        {
            streamWriter.WriteBytes(serializedDocument);

            _batchCount++;
            _batchLength = (int)streamWriter.Position - _batchStartPosition;
        }
Exemplo n.º 17
0
 internal override void WriteHeaderTo(BsonStreamWriter streamWriter)
 {
     _batchStartPosition = (int)streamWriter.Position;
     base.WriteHeaderTo(streamWriter);
     streamWriter.WriteInt32((int)_flags);
     streamWriter.WriteCString(_collectionFullName);
 }
 // internal methods
 internal override void WriteBodyTo(BsonStreamWriter streamWriter)
 {
     streamWriter.WriteInt64(_cursorId);
 }
 // internal methods
 internal void BackpatchMessageLength(BsonStreamWriter streamWriter)
 {
     MessageLength = (int)streamWriter.Position - _messageStartPosition;
     Backpatch(streamWriter.BaseStream, _messageStartPosition, MessageLength);
 }
 internal override void WriteHeaderTo(BsonStreamWriter streamWriter)
 {
     _messageStartPosition = (int)streamWriter.Position;
     base.WriteHeaderTo(streamWriter);
 }