// constructors /// <summary> /// Initializes a new instance of the <see cref="QueryMessage"/> class. /// </summary> /// <param name="requestId">The request identifier.</param> /// <param name="collectionNamespace">The collection namespace.</param> /// <param name="query">The query.</param> /// <param name="fields">The fields.</param> /// <param name="queryValidator">The query validator.</param> /// <param name="skip">The number of documents to skip.</param> /// <param name="batchSize">The size of a batch.</param> /// <param name="slaveOk">if set to <c>true</c> it is OK if the server is not the primary.</param> /// <param name="partialOk">if set to <c>true</c> the server is allowed to return partial results if any shards are unavailable.</param> /// <param name="noCursorTimeout">if set to <c>true</c> the server should not timeout the cursor.</param> /// <param name="oplogReplay">if set to <c>true</c> the OplogReplay bit will be set.</param> /// <param name="tailableCursor">if set to <c>true</c> the query should return a tailable cursor.</param> /// <param name="awaitData">if set to <c>true</c> the server should await data (used with tailable cursors).</param> /// <param name="shouldBeSent">A delegate that determines whether this message should be sent.</param> public QueryMessage( int requestId, CollectionNamespace collectionNamespace, BsonDocument query, BsonDocument fields, IElementNameValidator queryValidator, int skip, int batchSize, bool slaveOk, bool partialOk, bool noCursorTimeout, bool oplogReplay, bool tailableCursor, bool awaitData, Func<bool> shouldBeSent = null) : base(requestId, shouldBeSent) { _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace)); _query = Ensure.IsNotNull(query, nameof(query)); _fields = fields; // can be null _queryValidator = Ensure.IsNotNull(queryValidator, nameof(queryValidator)); _skip = Ensure.IsGreaterThanOrEqualToZero(skip, nameof(skip)); _batchSize = batchSize; // can be negative _slaveOk = slaveOk; _partialOk = partialOk; _noCursorTimeout = noCursorTimeout; _oplogReplay = oplogReplay; _tailableCursor = tailableCursor; _awaitData = awaitData; }
/// <inheritdoc/> public bool IsValidElementName(string elementName) { // the first elementName we see determines whether we are validating an update or a replacement document if (_chosenValidator == null) { if (elementName.Length > 0 && elementName[0] == '$') { _chosenValidator = UpdateElementNameValidator.Instance; ; } else { _chosenValidator = CollectionElementNameValidator.Instance; } } return _chosenValidator.IsValidElementName(elementName); }
// constructors /// <summary> /// Initializes a new instance of the <see cref="UpdateMessage"/> class. /// </summary> /// <param name="requestId">The request identifier.</param> /// <param name="collectionNamespace">The collection namespace.</param> /// <param name="query">The query.</param> /// <param name="update">The update.</param> /// <param name="updateValidator">The update validator.</param> /// <param name="isMulti">if set to <c>true</c> all matching documents should be updated.</param> /// <param name="isUpsert">if set to <c>true</c> a document should be inserted if no matching document is found.</param> public UpdateMessage( int requestId, CollectionNamespace collectionNamespace, BsonDocument query, BsonDocument update, IElementNameValidator updateValidator, bool isMulti, bool isUpsert) : base(requestId) { _collectionNamespace = Ensure.IsNotNull(collectionNamespace, nameof(collectionNamespace)); _query = Ensure.IsNotNull(query, nameof(query)); _update = Ensure.IsNotNull(update, nameof(update)); _updateValidator = Ensure.IsNotNull(updateValidator, nameof(updateValidator)); _isMulti = isMulti; _isUpsert = isUpsert; }
// constructors public UpdateWireProtocol( CollectionNamespace collectionNamespace, MessageEncoderSettings messageEncoderSettings, WriteConcern writeConcern, BsonDocument query, BsonDocument update, IElementNameValidator updateValidator, bool isMulti, bool isUpsert) : base(collectionNamespace, messageEncoderSettings, writeConcern) { _updateValidator = Ensure.IsNotNull(updateValidator, nameof(updateValidator)); _query = Ensure.IsNotNull(query, nameof(query)); _update = Ensure.IsNotNull(update, nameof(update)); _isMulti = isMulti; _isUpsert = isUpsert; }
// constructors public InsertBatchSerializer(int maxBatchCount, int maxBatchLength, int maxDocumentSize, int maxWireDocumentSize, IElementNameValidator elementNameValidator) : base(maxBatchCount, maxBatchLength, maxDocumentSize, maxWireDocumentSize) { _elementNameValidator = elementNameValidator; }
// constructors public InsertBatchSerializer(ConnectionDescription connectionDescription, int maxBatchCount, int maxBatchLength, IElementNameValidator elementNameValidator) : base(connectionDescription, maxBatchCount, maxBatchLength) { _elementNameValidator = elementNameValidator; }
private Mock <IElementNameValidator> CreatePrefixValidatorMock(string prefix, IElementNameValidator childValidator = null) { var mockValidator = new Mock <IElementNameValidator>(); mockValidator .Setup(m => m.GetValidatorForChildContent(It.IsAny <string>())) .Returns(childValidator ?? NoOpElementNameValidator.Instance); mockValidator .Setup(m => m.IsValidElementName(It.IsAny <string>())) .Returns <string>((name) => name.StartsWith(prefix)); return(mockValidator); }
/// <summary> /// Pops the element name validator. /// </summary> /// <returns>The popped element validator.</returns> public void PopElementNameValidator() { _elementNameValidator = _elementNameValidatorStack.Pop(); _childElementNameValidatorFactory = () => _elementNameValidator; }
/// <summary> /// Pushes the element name validator. /// </summary> /// <param name="validator">The validator.</param> public void PushElementNameValidator(IElementNameValidator validator) { if (validator == null) { throw new ArgumentNullException("validator"); } _elementNameValidatorStack.Push(_elementNameValidator); _elementNameValidator = validator; _childElementNameValidatorFactory = () => _elementNameValidator; }
/// <summary> /// Pops the element name validator. /// </summary> /// <returns>The popped element validator.</returns> public void PopElementNameValidator() { _elementNameValidator = _elementNameValidatorStack.Pop(); _childElementNameValidatorFactory = () => _elementNameValidator; }
/// <summary> /// Pops the element name validator. /// </summary> /// <returns>The popped element validator.</returns> public void PopElementNameValidator() { _elementNameValidator = _elementNameValidatorStack.Pop(); _useChildValidator = false; }
// constructors /// <summary> /// Initializes a new instance of the <see cref="FixedCountBatchableSourceSerializer{TITem}" /> class. /// </summary> /// <param name="itemSerializer">The item serializer.</param> /// <param name="itemElementNameValidator">The item element name validator.</param> /// <param name="count">The count.</param> public FixedCountBatchableSourceSerializer(IBsonSerializer <TItem> itemSerializer, IElementNameValidator itemElementNameValidator, int count) { _itemSerializer = Ensure.IsNotNull(itemSerializer, nameof(itemSerializer)); _itemElementNameValidator = Ensure.IsNotNull(itemElementNameValidator, nameof(itemElementNameValidator)); _count = Ensure.IsBetween(count, 0, int.MaxValue, nameof(count)); }
// constructors public InsertBatchSerializer(int maxBatchCount, int maxBatchLength, int maxDocumentSize, int maxWireDocumentSize, IElementNameValidator elementNameValidator) : base(maxBatchCount, maxBatchLength, maxDocumentSize, maxWireDocumentSize) { _elementNameValidator = elementNameValidator; }
private void WriteQuery(BsonBinaryWriter binaryWriter, BsonDocument query, IElementNameValidator queryValidator) { binaryWriter.PushElementNameValidator(queryValidator); try { var context = BsonSerializationContext.CreateRoot(binaryWriter); BsonDocumentSerializer.Instance.Serialize(context, query ?? new BsonDocument()); } finally { binaryWriter.PopElementNameValidator(); } }
/// <inheritdoc /> public virtual void PushElementNameValidator(IElementNameValidator validator) { ThrowIfDisposed(); _wrapped.PushElementNameValidator(validator); }
private void WriteUpdate(BsonBinaryWriter binaryWriter, BsonDocument update, IElementNameValidator updateValidator) { binaryWriter.PushElementNameValidator(updateValidator); try { var position = binaryWriter.BaseStream.Position; var context = BsonSerializationContext.CreateRoot(binaryWriter); BsonDocumentSerializer.Instance.Serialize(context, update); if (updateValidator is UpdateElementNameValidator && binaryWriter.BaseStream.Position == position + 5) { throw new BsonSerializationException("Update documents cannot be empty."); } } finally { binaryWriter.PopElementNameValidator(); } }
// constructors public InsertBatchSerializer(ConnectionDescription connectionDescription, int maxBatchCount, int maxBatchLength, IElementNameValidator elementNameValidator) : base(connectionDescription, maxBatchCount, maxBatchLength) { _elementNameValidator = elementNameValidator; }