Inheritance: BsonReaderSettings
 // constructors
 protected BulkWriteOperationArgs(
     string collectionName,
     string databaseName,
     int maxBatchCount,
     int maxBatchLength,
     int maxDocumentSize,
     int maxWireDocumentSize,
     bool isOrdered,
     BsonBinaryReaderSettings readerSettings,
     IEnumerable<WriteRequest> requests,
     WriteConcern writeConcern,
     BsonBinaryWriterSettings writerSettings)
 {
     _collectionName = collectionName;
     _databaseName = databaseName;
     _maxBatchCount = maxBatchCount;
     _maxBatchLength = maxBatchLength;
     _maxDocumentSize = maxDocumentSize;
     _maxWireDocumentSize = maxWireDocumentSize;
     _isOrdered = isOrdered;
     _readerSettings = readerSettings;
     _requests = requests;
     _writeConcern = writeConcern;
     _writerSettings = writerSettings;
 }
Beispiel #2
0
        /// <summary>
        /// Creates a BsonReader for a BSON Stream.
        /// </summary>
        /// <param name="stream">The BSON Stream.</param>
        /// <param name="settings">Optional reader settings.</param>
        /// <returns>A BsonReader.</returns>
        public static BsonReader Create(Stream stream, BsonBinaryReaderSettings settings)
        {
            var reader = new BsonBinaryReader(null, settings);

            reader.Buffer.LoadFrom(stream);
            return(reader);
        }
Beispiel #3
0
        /// <summary>
        /// Creates a BsonReader for a BSON Stream.
        /// </summary>
        /// <param name="stream">The BSON Stream.</param>
        /// <param name="settings">Optional reader settings.</param>
        /// <returns>A BsonReader.</returns>
        public static BsonReader Create(Stream stream, BsonBinaryReaderSettings settings)
        {
            var byteBuffer = ByteBufferFactory.LoadFrom(stream);

            byteBuffer.MakeReadOnly();
            return(new BsonBinaryReader(new BsonBuffer(byteBuffer, true), true, settings));
        }
Beispiel #4
0
 /// <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, settings));
 }
 // constructors
 public BulkMixedWriteOperation(
     Action<InsertRequest> assignId,
     bool checkElementNames,
     string collectionName,
     string databaseName,
     int maxBatchCount,
     int maxBatchLength,
     int maxDocumentSize,
     int maxWireDocumentSize,
     bool isOrdered,
     BsonBinaryReaderSettings readerSettings,
     IEnumerable<WriteRequest> requests,
     WriteConcern writeConcern,
     BsonBinaryWriterSettings writerSettings)
 {
     _assignId = assignId;
     _checkElementNames = checkElementNames;
     _collectionName = collectionName;
     _databaseName = databaseName;
     _maxBatchCount = maxBatchCount;
     _maxBatchLength = maxBatchLength;
     _maxDocumentSize = maxDocumentSize;
     _maxWireDocumentSize = maxWireDocumentSize;
     _isOrdered = isOrdered;
     _readerSettings = readerSettings;
     _requests = requests;
     _writeConcern = writeConcern;
     _writerSettings = writerSettings;
 }
 // constructors
 public BulkDeleteOperationArgs(
     string collectionName,
     string databaseName,
     int maxBatchCount,
     int maxBatchLength,
     int maxDocumentSize,
     int maxWireDocumentSize,
     bool isOrdered,
     BsonBinaryReaderSettings readerSettings,
     IEnumerable<DeleteRequest> requests,
     WriteConcern writeConcern,
     BsonBinaryWriterSettings writerSettings)
     : base(
         collectionName,
         databaseName,
         maxBatchCount,
         maxBatchLength,
         maxDocumentSize,
         maxWireDocumentSize,
         isOrdered,
         readerSettings,
         requests.Cast<WriteRequest>(),
         writeConcern,
         writerSettings)
 {
 }
 public static BsonReader Create(
     BsonBuffer buffer,
     BsonBinaryReaderSettings settings
 )
 {
     return new BsonBinaryReader(buffer, settings);
 }
 // constructors
 public BulkInsertOperationArgs(
     Action<InsertRequest> assignId,
     bool checkElementNames,
     string collectionName,
     string databaseName,
     int maxBatchCount,
     int maxBatchLength,
     int maxDocumentSize,
     int maxWireDocumentSize,
     bool isOrdered,
     BsonBinaryReaderSettings readerSettings,
     IEnumerable<InsertRequest> requests,
     WriteConcern writeConcern,
     BsonBinaryWriterSettings writerSettings)
     : base(
         collectionName,
         databaseName,
         maxBatchCount,
         maxBatchLength,
         maxDocumentSize,
         maxWireDocumentSize,
         isOrdered,
         readerSettings,
         requests.Cast<WriteRequest>(),
         writeConcern,
         writerSettings)
 {
     _assignId = assignId;
     _checkElementNames = checkElementNames;
 }
 protected ReadOperationBase(
     string databaseName,
     string collectionName,
     BsonBinaryReaderSettings readerSettings,
     BsonBinaryWriterSettings writerSettings)
     : base(databaseName, collectionName, readerSettings, writerSettings)
 {
 }
 public static BsonReader Create(
     Stream stream,
     BsonBinaryReaderSettings settings
 )
 {
     BsonBuffer buffer = new BsonBuffer();
     buffer.LoadFrom(stream);
     return new BsonBinaryReader(buffer, settings);
 }
 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 WriteOpcodeOperationBase(
     string databaseName,
     string collectionName,
     BsonBinaryReaderSettings readerSettings,
     BsonBinaryWriterSettings writerSettings,
     WriteConcern writeConcern)
     : base(databaseName, collectionName, readerSettings, writerSettings)
 {
     _writeConcern = writeConcern;
 }
 protected DatabaseOperation(
     string databaseName,
     string collectionName,
     BsonBinaryReaderSettings readerSettings,
     BsonBinaryWriterSettings writerSettings)
 {
     _databaseName = databaseName;
     _collectionName = collectionName;
     _readerSettings = (BsonBinaryReaderSettings)readerSettings.FrozenCopy();
     _writerSettings = (BsonBinaryWriterSettings)writerSettings.FrozenCopy();
 }
 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 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;
        }
 // methods
 /// <summary>
 /// Creates a binary reader for this encoder.
 /// </summary>
 /// <returns>A binary reader.</returns>
 public BsonBinaryReader CreateBinaryReader()
 {
     var readerSettings = new BsonBinaryReaderSettings();
     if (_encoderSettings != null)
     {
         readerSettings.Encoding = _encoderSettings.GetOrDefault(MessageEncoderSettingsName.ReadEncoding, readerSettings.Encoding);
         readerSettings.FixOldBinarySubTypeOnInput = _encoderSettings.GetOrDefault(MessageEncoderSettingsName.FixOldBinarySubTypeOnInput, readerSettings.FixOldBinarySubTypeOnInput);
         readerSettings.FixOldDateTimeMaxValueOnInput = _encoderSettings.GetOrDefault(MessageEncoderSettingsName.FixOldBinarySubTypeOnOutput, readerSettings.FixOldDateTimeMaxValueOnInput);
         readerSettings.GuidRepresentation = _encoderSettings.GetOrDefault(MessageEncoderSettingsName.GuidRepresentation, readerSettings.GuidRepresentation);
         readerSettings.MaxDocumentSize = _encoderSettings.GetOrDefault(MessageEncoderSettingsName.MaxDocumentSize, readerSettings.MaxDocumentSize);
     }
     return new BsonBinaryReader(_stream, readerSettings);
 }
Beispiel #17
0
        // protected methods
        /// <summary>
        /// Creates a clone of the settings.
        /// </summary>
        /// <returns>A clone of the settings.</returns>
        protected override BsonReaderSettings CloneImplementation()
        {
            var clone = new BsonBinaryReaderSettings
            {
                Encoding = _encoding,
                FixOldBinarySubTypeOnInput    = _fixOldBinarySubTypeOnInput,
                FixOldDateTimeMaxValueOnInput = _fixOldDateTimeMaxValueOnInput,
                GuidRepresentation            = GuidRepresentation,
                MaxDocumentSize = _maxDocumentSize
            };

            return(clone);
        }
 public RemoveOperation(
     string databaseName,
     string collectionName,
     BsonBinaryReaderSettings readerSettings,
     BsonBinaryWriterSettings writerSettings,
     WriteConcern writeConcern,
     IMongoQuery query,
     RemoveFlags flags)
     : base(databaseName, collectionName, readerSettings, writerSettings, writeConcern)
 {
     _query = query;
     _flags = flags;
 }
 /// <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);
 }
Beispiel #20
0
        /// <summary>
        /// Initializes a new instance of the BsonBinaryReader class.
        /// </summary>
        /// <param name="buffer">A BsonBuffer.</param>
        /// <param name="disposeBuffer">if set to <c>true</c> this BsonBinaryReader will own the buffer and when Dispose is called the buffer will be Disposed also.</param>
        /// <param name="settings">A BsonBinaryReaderSettings.</param>
        /// <exception cref="System.ArgumentNullException">
        /// buffer
        /// or
        /// settings
        /// </exception>
        public BsonBinaryReader(BsonBuffer buffer, bool disposeBuffer, BsonBinaryReaderSettings settings)
            : base(settings)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            _buffer               = buffer;
            _disposeBuffer        = disposeBuffer;
            _binaryReaderSettings = settings; // already frozen by base class

            _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)
     : base(settings)
 {
     if (buffer == null)
     {
         _buffer = new BsonBuffer();
         _disposeBuffer = true; // only call Dispose if we allocated the buffer
     }
     else
     {
         _buffer = buffer;
         _disposeBuffer = false;
     }
     _binaryReaderSettings = settings; // already frozen by base class
     _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)
     : base(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; // already frozen by base class
     context       = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
 }
        /// <summary>
        /// Initializes a new instance of the BsonBinaryReader class.
        /// </summary>
        /// <param name="stream">A stream (BsonBinary does not own the stream and will not Dispose it).</param>
        /// <param name="settings">A BsonBinaryReaderSettings.</param>
        public BsonBinaryReader(Stream stream, BsonBinaryReaderSettings settings)
            : base(settings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("The stream must be capable of seeking.", "stream");
            }

            _streamReader = new BsonStreamReader(stream, settings.Encoding);
            _settings = settings; // already frozen by base class

            _context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
        }
Beispiel #24
0
        /// <summary>
        /// Initializes a new instance of the BsonBinaryReader class.
        /// </summary>
        /// <param name="stream">A stream (BsonBinary does not own the stream and will not Dispose it).</param>
        /// <param name="settings">A BsonBinaryReaderSettings.</param>
        public BsonBinaryReader(Stream stream, BsonBinaryReaderSettings settings)
            : base(settings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("The stream must be capable of seeking.", "stream");
            }

            _baseStream = stream;
            _bsonStream = (stream as BsonStream) ?? new BsonStreamAdapter(stream);

            _context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
        }
        /// <summary>
        /// Initializes a new instance of the BsonBinaryReader class.
        /// </summary>
        /// <param name="stream">A stream (BsonBinary does not own the stream and will not Dispose it).</param>
        /// <param name="settings">A BsonBinaryReaderSettings.</param>
        public BsonBinaryReader(Stream stream, BsonBinaryReaderSettings settings)
            : base(settings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("The stream must be capable of seeking.", "stream");
            }

            _streamReader = new BsonStreamReader(stream, settings.Encoding);
            _settings     = settings; // already frozen by base class

            _context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
        }
 public UpdateOperation(
     string databaseName,
     string collectionName,
     BsonBinaryReaderSettings readerSettings,
     BsonBinaryWriterSettings writerSettings,
     WriteConcern writeConcern,
     IMongoQuery query,
     IMongoUpdate update,
     UpdateFlags flags,
     bool checkElementNames)
     : base(databaseName, collectionName, readerSettings, writerSettings, writeConcern)
 {
     _query = query;
     _update = update;
     _flags = flags;
     _checkElementNames = checkElementNames;
 }
        // private methods
        private CommandResult RunCommand(MongoConnection connection, string databaseName, IMongoCommand command)
        {
            var readerSettings = new BsonBinaryReaderSettings();
            var writerSettings = new BsonBinaryWriterSettings();
            var resultSerializer = BsonSerializer.LookupSerializer<CommandResult>();

            var commandOperation = new CommandOperation<CommandResult>(
                databaseName,
                readerSettings,
                writerSettings,
                command,
                QueryFlags.None,
                null, // options
                null, // readPreference
                resultSerializer);

            return commandOperation.Execute(connection);
        }
 public InsertOperation(
     string databaseName,
     string collectionName,
     BsonBinaryReaderSettings readerSettings,
     BsonBinaryWriterSettings writerSettings,
     WriteConcern writeConcern,
     bool assignIdOnInsert,
     bool checkElementNames,
     Type documentType,
     IEnumerable documents,
     InsertFlags flags)
     : base(databaseName, collectionName, readerSettings, writerSettings, writeConcern)
 {
     _assignIdOnInsert = assignIdOnInsert;
     _checkElementNames = checkElementNames;
     _documentType = documentType;
     _documents = documents;
     _flags = flags;
 }
        // protected methods
        /// <summary>
        /// Creates a clone of the settings.
        /// </summary>
        /// <returns>A clone of the settings.</returns>
        protected override BsonReaderSettings CloneImplementation()
        {
            var clone = new BsonBinaryReaderSettings
            {
                Encoding = _encoding,
                FixOldBinarySubTypeOnInput    = _fixOldBinarySubTypeOnInput,
                FixOldDateTimeMaxValueOnInput = _fixOldDateTimeMaxValueOnInput,
                MaxDocumentSize = _maxDocumentSize
            };

#pragma warning disable 618
            if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
            {
                clone.GuidRepresentation = GuidRepresentation;
            }
#pragma warning restore 618

            return(clone);
        }
        protected SendMessageWithWriteConcernResult SendMessageWithWriteConcern(
            MongoConnection connection,
            Stream stream,
            int requestId,
            BsonBinaryReaderSettings readerSettings,
            BsonBinaryWriterSettings writerSettings,
            WriteConcern writeConcern)
        {
            var result = new SendMessageWithWriteConcernResult();

            if (writeConcern.Enabled)
            {
                var maxDocumentSize = connection.ServerInstance.MaxDocumentSize;

                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;

                var 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, maxDocumentSize, 0, 1, getLastErrorCommand, null);
                getLastErrorMessage.WriteTo(stream);

                result.GetLastErrorCommand = getLastErrorCommand;
                result.GetLastErrorRequestId = getLastErrorMessage.RequestId;
            }

            connection.SendMessage(stream, requestId);

            return result;
        }
 // constructors
 public BulkUpdateOperationArgs(
     bool checkElementNames,
     string collectionName,
     string databaseName,
     int maxBatchCount,
     int maxBatchLength,
     bool isOrdered,
     BsonBinaryReaderSettings readerSettings,
     IEnumerable<UpdateRequest> requests,
     WriteConcern writeConcern,
     BsonBinaryWriterSettings writerSettings)
     : base(
        collectionName,
        databaseName,
        maxBatchCount,
        maxBatchLength,
        isOrdered,
        readerSettings,
        requests.Cast<WriteRequest>(),
        writeConcern,
        writerSettings)
 {
     _checkElementNames = checkElementNames;
 }
 // protected methods
 /// <summary>
 /// Creates a clone of the settings.
 /// </summary>
 /// <returns>A clone of the settings.</returns>
 protected override BsonReaderSettings CloneImplementation()
 {
     var clone = new BsonBinaryReaderSettings
     {
         Encoding = _encoding,
         FixOldBinarySubTypeOnInput = _fixOldBinarySubTypeOnInput,
         FixOldDateTimeMaxValueOnInput = _fixOldDateTimeMaxValueOnInput,
         GuidRepresentation = GuidRepresentation,
         MaxDocumentSize = _maxDocumentSize
     };
     return clone;
 }
 /// <summary>
 /// Creates a BsonReader for a BSON Stream.
 /// </summary>
 /// <param name="stream">The BSON Stream.</param>
 /// <param name="settings">Optional reader settings.</param>
 /// <returns>A BsonReader.</returns>
 public static BsonReader Create(Stream stream, BsonBinaryReaderSettings settings)
 {
     var reader = new BsonBinaryReader(null, settings);
     reader.Buffer.LoadFrom(stream);
     return reader;
 }
 /// <summary>
 /// Materializes the RawBsonDocument into a regular BsonDocument.
 /// </summary>
 /// <param name="binaryReaderSettings">The binary reader settings.</param>
 /// <returns>A BsonDocument.</returns>
 public BsonDocument Materialize(BsonBinaryReaderSettings binaryReaderSettings)
 {
     ThrowIfDisposed();
     using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
     using (var reader = new BsonBinaryReader(stream, binaryReaderSettings))
     {
         var context = BsonDeserializationContext.CreateRoot(reader);
         return BsonDocumentSerializer.Instance.Deserialize(context);
     }
 }
Beispiel #35
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)
 {
 }