Example #1
0
        public SingleNodeBatchCommand(DocumentConventions conventions, JsonOperationContext context, IList <ICommandData> commands, BatchOptions options = null, TransactionMode mode = TransactionMode.SingleNode)
        {
            _conventions = conventions ?? throw new ArgumentNullException(nameof(conventions));
            _commands    = commands ?? throw new ArgumentNullException(nameof(commands));
            _options     = options;
            _mode        = mode;

            _commandsAsJson = new BlittableJsonReaderObject[_commands.Count];
            foreach (var command in commands)
            {
                if (command is PutAttachmentCommandData putAttachmentCommandData)
                {
                    if (_attachmentStreams == null)
                    {
                        _attachmentStreams       = new List <Stream>();
                        _uniqueAttachmentStreams = new HashSet <Stream>();
                    }

                    var stream = putAttachmentCommandData.Stream;
                    PutAttachmentCommandHelper.ValidateStream(stream);
                    if (_uniqueAttachmentStreams.Add(stream) == false)
                    {
                        PutAttachmentCommandHelper.ThrowStreamWasAlreadyUsed();
                    }
                    _attachmentStreams.Add(stream);
                }
            }

            Timeout = options?.RequestTimeout;
        }
Example #2
0
        public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url)
        {
            var request = new HttpRequestMessage
            {
                Method  = HttpMethod.Post,
                Content = new BlittableJsonContent(stream =>
                {
                    using (var writer = new BlittableJsonTextWriter(ctx, stream))
                    {
                        writer.WriteStartObject();
                        writer.WriteArray("Commands", _commands);
                        if (_mode == TransactionMode.ClusterWide)
                        {
                            writer.WriteComma();
                            writer.WritePropertyName(nameof(TransactionMode));
                            writer.WriteString(nameof(TransactionMode.ClusterWide));
                        }
                        writer.WriteEndObject();
                    }
                })
            };

            if (_attachmentStreams != null && _attachmentStreams.Count > 0)
            {
                var multipartContent = new MultipartContent {
                    request.Content
                };
                foreach (var stream in _attachmentStreams)
                {
                    PutAttachmentCommandHelper.PrepareStream(stream);
                    var streamContent = new AttachmentStreamContent(stream, CancellationToken);
                    streamContent.Headers.TryAddWithoutValidation("Command-Type", "AttachmentStream");
                    multipartContent.Add(streamContent);
                }
                request.Content = multipartContent;
            }

            var sb = new StringBuilder($"{node.Url}/databases/{node.Database}/bulk_docs");

            AppendOptions(sb);

            url = sb.ToString();

            return(request);
        }
Example #3
0
        public SingleNodeBatchCommand(DocumentConventions conventions, JsonOperationContext context, IList <ICommandData> commands, BatchOptions options = null, TransactionMode mode = TransactionMode.SingleNode)
        {
            if (conventions == null)
            {
                throw new ArgumentNullException(nameof(conventions));
            }
            if (commands == null)
            {
                throw new ArgumentNullException(nameof(commands));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            _commands = new BlittableJsonReaderObject[commands.Count];
            for (var i = 0; i < commands.Count; i++)
            {
                var command = commands[i];
                var json    = command.ToJson(conventions, context);
                _commands[i] = context.ReadObject(json, "command");

                if (command is PutAttachmentCommandData putAttachmentCommandData)
                {
                    if (_attachmentStreams == null)
                    {
                        _attachmentStreams       = new List <Stream>();
                        _uniqueAttachmentStreams = new HashSet <Stream>();
                    }

                    var stream = putAttachmentCommandData.Stream;
                    PutAttachmentCommandHelper.ValidateStream(stream);
                    if (_uniqueAttachmentStreams.Add(stream) == false)
                    {
                        PutAttachmentCommandHelper.ThrowStreamWasAlreadyUsed();
                    }
                    _attachmentStreams.Add(stream);
                }
            }

            _options = options;
            _mode    = mode;

            Timeout = options?.RequestTimeout;
        }
        public PutAttachmentCommandData(string documentId, string name, Stream stream, string contentType, string changeVector)
        {
            if (string.IsNullOrWhiteSpace(documentId))
            {
                throw new ArgumentNullException(nameof(documentId));
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            Id           = documentId;
            Name         = name;
            Stream       = stream;
            ContentType  = contentType;
            ChangeVector = changeVector;

            PutAttachmentCommandHelper.ValidateStream(stream);
        }
Example #5
0
        public BatchCommand(DocumentConventions conventions, JsonOperationContext context, List <ICommandData> commands, BatchOptions options = null)
        {
            if (conventions == null)
            {
                throw new ArgumentNullException(nameof(conventions));
            }
            if (commands == null)
            {
                throw new ArgumentNullException(nameof(commands));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            _commands = new BlittableJsonReaderObject[commands.Count];
            for (var i = 0; i < commands.Count; i++)
            {
                var command = commands[i];
                _commands[i] = context.ReadObject(command.ToJson(conventions, context), "command");

                if (command is PutAttachmentCommandData putAttachmentCommandData)
                {
                    if (_attachmentStreams == null)
                    {
                        _attachmentStreams = new HashSet <Stream>();
                    }

                    var stream = putAttachmentCommandData.Stream;
                    PutAttachmentCommandHelper.ValidateStream(stream);
                    if (_attachmentStreams.Add(stream) == false)
                    {
                        PutAttachmentCommandHelper.ThrowStreamAlready();
                    }
                }
            }

            _options = options;

            Timeout = options?.RequestTimeout;
        }
Example #6
0
        public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url)
        {
            if (_supportsAtomicWrites == null ||
                node.SupportsAtomicClusterWrites != _supportsAtomicWrites)
            {
                _supportsAtomicWrites = node.SupportsAtomicClusterWrites;
                for (var i = 0; i < _commands.Count; i++)
                {
                    var command = _commands[i];

                    var json = command.ToJson(_conventions, ctx);

                    if (node.SupportsAtomicClusterWrites == false)
                    {   // support older clients
                        json.RemoveInMemoryPropertyByName(nameof(PutCommandData.OriginalChangeVector));
                    }
                    _commandsAsJson[i] = ctx.ReadObject(json, "command");
                }
            }

            var request = new HttpRequestMessage
            {
                Method  = HttpMethod.Post,
                Content = new BlittableJsonContent(async stream =>
                {
                    await using (var writer = new AsyncBlittableJsonTextWriter(ctx, stream))
                    {
                        writer.WriteStartObject();
                        writer.WriteArray("Commands", _commandsAsJson);
                        if (_mode == TransactionMode.ClusterWide)
                        {
                            writer.WriteComma();
                            writer.WritePropertyName(nameof(TransactionMode));
                            writer.WriteString(nameof(TransactionMode.ClusterWide));
                        }
                        writer.WriteEndObject();
                    }
                })
            };

            if (_attachmentStreams != null && _attachmentStreams.Count > 0)
            {
                var multipartContent = new MultipartContent {
                    request.Content
                };
                foreach (var stream in _attachmentStreams)
                {
                    PutAttachmentCommandHelper.PrepareStream(stream);
                    var streamContent = new AttachmentStreamContent(stream, CancellationToken);
                    streamContent.Headers.TryAddWithoutValidation("Command-Type", "AttachmentStream");
                    multipartContent.Add(streamContent);
                }
                request.Content = multipartContent;
            }

            var sb = new StringBuilder($"{node.Url}/databases/{node.Database}/bulk_docs?");

            AppendOptions(sb);

            url = sb.ToString();

            return(request);
        }