Пример #1
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;
        }
            // protected methods
            protected override void SerializeRequest(BsonBinaryWriter bsonWriter, WriteRequest request)
            {
                var insertRequest = (InsertRequest)request;
                var document      = insertRequest.Document;

                if (document == null)
                {
                    throw new ArgumentException("Batch contains one or more null documents.");
                }

                var actualType = document.GetType();

                if (_cachedSerializerType != actualType)
                {
                    _cachedSerializer     = BsonSerializer.LookupSerializer(actualType);
                    _cachedSerializerType = actualType;
                }
                var serializer           = _cachedSerializer;
                var serializationOptions = insertRequest.SerializationOptions ?? DocumentSerializationOptions.SerializeIdFirstInstance;

                var savedCheckElementNames = bsonWriter.CheckElementNames;

                try
                {
                    bsonWriter.PushMaxDocumentSize(_args.MaxDocumentSize);
                    bsonWriter.CheckElementNames = _args.CheckElementNames;
                    serializer.Serialize(bsonWriter, insertRequest.NominalType, document, serializationOptions);
                }
                finally
                {
                    bsonWriter.PopMaxDocumentSize();
                    bsonWriter.CheckElementNames = savedCheckElementNames;
                }
            }
Пример #3
0
        private void AddRequest(BsonBuffer buffer, 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 actualType = document.GetType();
                if (_cachedSerializerType != actualType)
                {
                    _cachedSerializer     = BsonSerializer.LookupSerializer(actualType);
                    _cachedSerializerType = actualType;
                }
                serializer = _cachedSerializer;
            }
            var serializationOptions = request.SerializationOptions ?? DocumentSerializationOptions.SerializeIdFirstInstance;

            _lastDocumentStartPosition = buffer.Position;
            using (var bsonWriter = new BsonBinaryWriter(buffer, false, WriterSettings))
            {
                bsonWriter.PushMaxDocumentSize(_maxDocumentSize);
                bsonWriter.CheckElementNames = _checkElementNames;
                serializer.Serialize(bsonWriter, request.NominalType, document, serializationOptions);
                bsonWriter.PopMaxDocumentSize();
            }

            _batchCount++;
            _batchLength = buffer.Position - _batchStartPosition;
        }
Пример #4
0
            // protected methods
            protected override void SerializeRequest(BsonBinaryWriter bsonWriter, WriteRequest request)
            {
                var deleteRequest = (DeleteRequest)request;

                bsonWriter.PushMaxDocumentSize(MaxDocumentSize);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("q");
                BsonSerializer.Serialize(bsonWriter, deleteRequest.Query ?? new QueryDocument());
                bsonWriter.WriteInt32("limit", deleteRequest.Limit);
                bsonWriter.WriteEndDocument();
                bsonWriter.PopMaxDocumentSize();
            }
Пример #5
0
 // internal methods
 internal override void WriteBodyTo(BsonBuffer buffer)
 {
     using (var bsonWriter = new BsonBinaryWriter(buffer, false, WriterSettings))
     {
         bsonWriter.PushMaxDocumentSize(_maxDocumentSize);
         if (_query == null)
         {
             bsonWriter.WriteStartDocument();
             bsonWriter.WriteEndDocument();
         }
         else
         {
             BsonSerializer.Serialize(bsonWriter, _query.GetType(), _query, DocumentSerializationOptions.SerializeIdFirstInstance);
         }
         bsonWriter.PopMaxDocumentSize();
     }
 }
 // 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();
     }
 }
            // protected methods
            protected override void SerializeRequest(BsonBinaryWriter bsonWriter, WriteRequest request)
            {
                var updateRequest = (UpdateRequest)request;

                bsonWriter.PushMaxDocumentSize(MaxWireDocumentSize);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("q");
                BsonSerializer.Serialize(bsonWriter, updateRequest.Query ?? new QueryDocument());
                bsonWriter.WriteName("u");
                BsonSerializer.Serialize(bsonWriter, updateRequest.Update);
                if (updateRequest.IsMultiUpdate.HasValue)
                {
                    bsonWriter.WriteBoolean("multi", updateRequest.IsMultiUpdate.Value);
                }
                if (updateRequest.IsUpsert.HasValue)
                {
                    bsonWriter.WriteBoolean("upsert", updateRequest.IsUpsert.Value);
                }
                bsonWriter.WriteEndDocument();
                bsonWriter.PopMaxDocumentSize();
            }
            // protected methods
            protected override void SerializeRequest(BsonBinaryWriter bsonWriter, WriteRequest request)
            {
                var insertRequest = (InsertRequest)request;
                var document      = insertRequest.Document;

                if (document == null)
                {
                    throw new ArgumentException("Batch contains one or more null documents.");
                }

                var serializer = insertRequest.Serializer;

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

                var savedCheckElementNames = bsonWriter.CheckElementNames;

                try
                {
                    bsonWriter.PushMaxDocumentSize(MaxDocumentSize);
                    bsonWriter.CheckElementNames = _checkElementNames;
                    var context = BsonSerializationContext.CreateRoot(bsonWriter, insertRequest.NominalType, c => c.SerializeIdFirst = true);
                    serializer.Serialize(context, document);
                }
                finally
                {
                    bsonWriter.PopMaxDocumentSize();
                    bsonWriter.CheckElementNames = savedCheckElementNames;
                }
            }