コード例 #1
0
        private async Task <ResponseMessage> ProcessResourceOperationAsBulkStreamAsync(
            Uri resourceUri,
            ResourceType resourceType,
            OperationType operationType,
            RequestOptions requestOptions,
            ContainerCore cosmosContainerCore,
            PartitionKey partitionKey,
            string itemId,
            Stream streamPayload,
            CosmosDiagnosticsContext diagnosticsContext,
            CancellationToken cancellationToken)
        {
            ItemRequestOptions itemRequestOptions = requestOptions as ItemRequestOptions;
            TransactionalBatchItemRequestOptions batchItemRequestOptions = TransactionalBatchItemRequestOptions.FromItemRequestOptions(itemRequestOptions);
            ItemBatchOperation itemBatchOperation = new ItemBatchOperation(
                operationType: operationType,
                operationIndex: 0,
                partitionKey: partitionKey,
                id: itemId,
                resourceStream: streamPayload,
                requestOptions: batchItemRequestOptions,
                diagnosticsContext: diagnosticsContext);

            TransactionalBatchOperationResult batchOperationResult = await cosmosContainerCore.BatchExecutor.AddAsync(itemBatchOperation, itemRequestOptions, cancellationToken);

            return(batchOperationResult.ToResponseMessage());
        }
コード例 #2
0
        TransactionalBatch PatchItem(
            string id,
            IReadOnlyList <PatchOperation> patchOperations,
            TransactionalBatchItemRequestOptions requestOptions = null)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException(nameof(id));
            }

            if (patchOperations == null ||
                !patchOperations.Any())
            {
                throw new ArgumentNullException(nameof(patchOperations));
            }

            this.operations.Add(new ItemBatchOperation <IReadOnlyList <PatchOperation> >(
                                    operationType: OperationType.Patch,
                                    operationIndex: this.operations.Count,
                                    id: id,
                                    resource: patchOperations,
                                    requestOptions: requestOptions,
                                    containerCore: this.container));

            return(this);
        }
コード例 #3
0
        private async Task <ResponseMessage> ProcessResourceOperationAsBulkStreamAsync(
            OperationType operationType,
            RequestOptions requestOptions,
            ContainerInternal cosmosContainerCore,
            PartitionKey partitionKey,
            string itemId,
            Stream streamPayload,
            CancellationToken cancellationToken)
        {
            this.ThrowIfDisposed();
            ItemRequestOptions itemRequestOptions = requestOptions as ItemRequestOptions;
            TransactionalBatchItemRequestOptions batchItemRequestOptions = TransactionalBatchItemRequestOptions.FromItemRequestOptions(itemRequestOptions);
            ItemBatchOperation itemBatchOperation = new ItemBatchOperation(
                operationType: operationType,
                operationIndex: 0,
                partitionKey: partitionKey,
                id: itemId,
                resourceStream: streamPayload,
                requestOptions: batchItemRequestOptions,
                cosmosClientContext: this);

            TransactionalBatchOperationResult batchOperationResult = await cosmosContainerCore.BatchExecutor.AddAsync(
                itemBatchOperation,
                itemRequestOptions,
                cancellationToken);

            return(batchOperationResult.ToResponseMessage());
        }
コード例 #4
0
        public override TransactionalBatch ReplaceItemStream(
            string id,
            Stream streamPayload,
            TransactionalBatchItemRequestOptions requestOptions = null)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            if (streamPayload == null)
            {
                throw new ArgumentNullException(nameof(streamPayload));
            }

            this.operations.Add(new ItemBatchOperation(
                                    operationType: OperationType.Replace,
                                    operationIndex: this.operations.Count,
                                    id: id,
                                    resourceStream: streamPayload,
                                    requestOptions: requestOptions,
                                    containerCore: this.container));

            return(this);
        }
コード例 #5
0
 public ItemBatchOperation(
     OperationType operationType,
     int operationIndex,
     string id             = null,
     Stream resourceStream = null,
     TransactionalBatchItemRequestOptions requestOptions = null)
 {
     this.OperationType  = operationType;
     this.OperationIndex = operationIndex;
     this.Id             = id;
     this.ResourceStream = resourceStream;
     this.RequestOptions = requestOptions;
 }
コード例 #6
0
        /// <summary>
        /// Adds an operation to patch an item into the batch.
        /// </summary>
        /// <param name="id">The cosmos item id.</param>
        /// <param name="patchStream">A <see cref="Stream"/> containing the patch specification.</param>
        /// <param name="requestOptions">(Optional) The options for the item request. <see cref="TransactionalBatchItemRequestOptions"/>.</param>
        /// <returns>The <see cref="TransactionalBatch"/> instance with the operation added.</returns>
        public virtual TransactionalBatch PatchItemStream(
            string id,
            Stream patchStream,
            TransactionalBatchItemRequestOptions requestOptions = null)
        {
            this.operations.Add(new ItemBatchOperation(
                                    operationType: OperationType.Patch,
                                    operationIndex: this.operations.Count,
                                    id: id,
                                    resourceStream: patchStream,
                                    requestOptions: requestOptions));

            return(this);
        }
コード例 #7
0
 public ItemBatchOperation(
     OperationType operationType,
     int operationIndex,
     ContainerInternal containerCore,
     string id             = null,
     Stream resourceStream = null,
     TransactionalBatchItemRequestOptions requestOptions = null)
 {
     this.OperationType     = operationType;
     this.OperationIndex    = operationIndex;
     this.ContainerInternal = containerCore;
     this.Id                 = id;
     this.ResourceStream     = resourceStream;
     this.RequestOptions     = requestOptions;
     this.DiagnosticsContext = null;
 }
コード例 #8
0
        public override TransactionalBatch UpsertItem <T>(
            T item,
            TransactionalBatchItemRequestOptions requestOptions = null)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            this.operations.Add(new ItemBatchOperation <T>(
                                    operationType: OperationType.Upsert,
                                    operationIndex: this.operations.Count,
                                    resource: item,
                                    requestOptions: requestOptions));

            return(this);
        }
コード例 #9
0
        public override TransactionalBatch ReadItem(
            string id,
            TransactionalBatchItemRequestOptions requestOptions = null)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            this.operations.Add(new ItemBatchOperation(
                                    operationType: OperationType.Read,
                                    operationIndex: this.operations.Count,
                                    id: id,
                                    requestOptions: requestOptions));

            return(this);
        }
コード例 #10
0
        public override TransactionalBatch CreateItemStream(
            Stream streamPayload,
            TransactionalBatchItemRequestOptions requestOptions = null)
        {
            if (streamPayload == null)
            {
                throw new ArgumentNullException(nameof(streamPayload));
            }

            this.operations.Add(new ItemBatchOperation(
                                    operationType: OperationType.Create,
                                    operationIndex: this.operations.Count,
                                    resourceStream: streamPayload,
                                    requestOptions: requestOptions));

            return(this);
        }
コード例 #11
0
 public ItemBatchOperation(
     OperationType operationType,
     int operationIndex,
     PartitionKey partitionKey,
     string id             = null,
     Stream resourceStream = null,
     TransactionalBatchItemRequestOptions requestOptions = null,
     CosmosDiagnosticsContext diagnosticsContext         = null)
 {
     this.OperationType      = operationType;
     this.OperationIndex     = operationIndex;
     this.PartitionKey       = partitionKey;
     this.Id                 = id;
     this.ResourceStream     = resourceStream;
     this.RequestOptions     = requestOptions;
     this.DiagnosticsContext = diagnosticsContext;
 }
コード例 #12
0
        private async Task <ResponseMessage> ProcessResourceOperationAsBulkStreamAsync(
            Uri resourceUri,
            ResourceType resourceType,
            OperationType operationType,
            RequestOptions requestOptions,
            ContainerCore cosmosContainerCore,
            PartitionKey partitionKey,
            string itemId,
            Stream streamPayload,
            Action <RequestMessage> requestEnricher,
            CancellationToken cancellationToken)
        {
            ItemRequestOptions itemRequestOptions = requestOptions as ItemRequestOptions;
            TransactionalBatchItemRequestOptions batchItemRequestOptions = TransactionalBatchItemRequestOptions.FromItemRequestOptions(itemRequestOptions);
            ItemBatchOperation itemBatchOperation = new ItemBatchOperation(operationType, /* index */ 0, partitionKey, itemId, streamPayload, batchItemRequestOptions);
            TransactionalBatchOperationResult batchOperationResult = await cosmosContainerCore.BatchExecutor.AddAsync(itemBatchOperation, itemRequestOptions, cancellationToken);

            return(batchOperationResult.ToResponseMessage());
        }
コード例 #13
0
        internal static TransactionalBatchItemRequestOptions FromItemRequestOptions(ItemRequestOptions itemRequestOptions)
        {
            if (itemRequestOptions == null)
            {
                return(null);
            }

            TransactionalBatchItemRequestOptions batchItemRequestOptions = new TransactionalBatchItemRequestOptions
            {
                IndexingDirective              = itemRequestOptions.IndexingDirective,
                IfMatchEtag                    = itemRequestOptions.IfMatchEtag,
                IfNoneMatchEtag                = itemRequestOptions.IfNoneMatchEtag,
                Properties                     = itemRequestOptions.Properties,
                EnableContentResponseOnWrite   = itemRequestOptions.EnableContentResponseOnWrite,
                IsEffectivePartitionKeyRouting = itemRequestOptions.IsEffectivePartitionKeyRouting
            };

            return(batchItemRequestOptions);
        }
コード例 #14
0
        public override TransactionalBatch ReplaceItem <T>(
            string id,
            T item,
            TransactionalBatchItemRequestOptions requestOptions = null)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            this.operations.Add(new ItemBatchOperation <T>(
                                    operationType: OperationType.Replace,
                                    operationIndex: this.operations.Count,
                                    id: id,
                                    resource: item,
                                    requestOptions: requestOptions));

            return(this);
        }
コード例 #15
0
        internal static Result WriteOperation(ref RowWriter writer, TypeArgument typeArg, ItemBatchOperation operation)
        {
            bool   pkWritten = false;
            Result r         = writer.WriteInt32("operationType", (int)operation.OperationType);

            if (r != Result.Success)
            {
                return(r);
            }

            r = writer.WriteInt32("resourceType", (int)ResourceType.Document);
            if (r != Result.Success)
            {
                return(r);
            }

            if (operation.PartitionKeyJson != null)
            {
                r = writer.WriteString("partitionKey", operation.PartitionKeyJson);
                if (r != Result.Success)
                {
                    return(r);
                }

                pkWritten = true;
            }

            if (operation.Id != null)
            {
                r = writer.WriteString("id", operation.Id);
                if (r != Result.Success)
                {
                    return(r);
                }
            }

            if (!operation.ResourceBody.IsEmpty)
            {
                r = writer.WriteBinary("resourceBody", operation.ResourceBody.Span);
                if (r != Result.Success)
                {
                    return(r);
                }
            }

            if (operation.RequestOptions != null)
            {
                TransactionalBatchItemRequestOptions options = operation.RequestOptions;
                if (options.IndexingDirective.HasValue)
                {
                    string indexingDirectiveString = IndexingDirectiveStrings.FromIndexingDirective(options.IndexingDirective.Value);
                    r = writer.WriteString("indexingDirective", indexingDirectiveString);
                    if (r != Result.Success)
                    {
                        return(r);
                    }
                }

                if (options.IfMatchEtag != null)
                {
                    r = writer.WriteString("ifMatch", options.IfMatchEtag);
                    if (r != Result.Success)
                    {
                        return(r);
                    }
                }
                else if (options.IfNoneMatchEtag != null)
                {
                    r = writer.WriteString("ifNoneMatch", options.IfNoneMatchEtag);
                    if (r != Result.Success)
                    {
                        return(r);
                    }
                }

                if (options.Properties != null)
                {
                    if (options.Properties.TryGetValue(WFConstants.BackendHeaders.BinaryId, out object binaryIdObj))
                    {
                        byte[] binaryId = binaryIdObj as byte[];
                        if (binaryId != null)
                        {
                            r = writer.WriteBinary("binaryId", binaryId);
                            if (r != Result.Success)
                            {
                                return(r);
                            }
                        }
                    }

                    if (options.Properties.TryGetValue(WFConstants.BackendHeaders.EffectivePartitionKey, out object epkObj))
                    {
                        byte[] epk = epkObj as byte[];
                        if (epk != null)
                        {
                            r = writer.WriteBinary("effectivePartitionKey", epk);
                            if (r != Result.Success)
                            {
                                return(r);
                            }
                        }
                    }

                    if (!pkWritten && options.Properties.TryGetValue(
                            HttpConstants.HttpHeaders.PartitionKey,
                            out object pkStrObj))
                    {
                        string pkString = pkStrObj as string;
                        if (pkString != null)
                        {
                            r = writer.WriteString("partitionKey", pkString);
                            if (r != Result.Success)
                            {
                                return(r);
                            }
                        }
                    }

                    if (options.Properties.TryGetValue(WFConstants.BackendHeaders.TimeToLiveInSeconds, out object ttlObj))
                    {
                        string ttlStr = ttlObj as string;
                        if (ttlStr != null && int.TryParse(ttlStr, out int ttl))
                        {
                            r = writer.WriteInt32("timeToLiveInSeconds", ttl);
                            if (r != Result.Success)
                            {
                                return(r);
                            }
                        }
                    }
                }
            }

            return(Result.Success);
        }
コード例 #16
0
 /// <summary>
 /// Adds an operation to replace an item into the batch.
 /// </summary>
 /// <param name="id">The cosmos item id.</param>
 /// <param name="streamPayload">
 /// A <see cref="Stream"/> containing the payload of the item.
 /// The stream must have a UTF-8 encoded JSON object which contains an id property.
 /// </param>
 /// <param name="requestOptions">(Optional) The options for the item request. <see cref="TransactionalBatchItemRequestOptions"/>.</param>
 /// <returns>The <see cref="TransactionalBatch"/> instance with the operation added.</returns>
 public abstract TransactionalBatch ReplaceItemStream(
     string id,
     Stream streamPayload,
     TransactionalBatchItemRequestOptions requestOptions = null);
コード例 #17
0
 /// <summary>
 /// Adds an operation to replace an item into the batch.
 /// </summary>
 /// <param name="id">The cosmos item id.</param>
 /// <param name="item">A JSON serializable object that must contain an id property. <see cref="CosmosSerializer"/> to implement a custom serializer.</param>
 /// <param name="requestOptions">(Optional) The options for the item request. <see cref="TransactionalBatchItemRequestOptions"/>.</param>
 /// <returns>The <see cref="TransactionalBatch"/> instance with the operation added.</returns>
 /// <typeparam name="T">The type of item to be created.</typeparam>
 public abstract TransactionalBatch ReplaceItem <T>(
     string id,
     T item,
     TransactionalBatchItemRequestOptions requestOptions = null);
コード例 #18
0
 /// <summary>
 /// Adds an operation to upsert an item into the batch.
 /// </summary>
 /// <param name="streamPayload">
 /// A <see cref="Stream"/> containing the payload of the item.
 /// The stream must have a UTF-8 encoded JSON object which contains an id property.
 /// </param>
 /// <param name="requestOptions">(Optional) The options for the item request. <see cref="TransactionalBatchItemRequestOptions"/>.</param>
 /// <returns>The <see cref="TransactionalBatch"/> instance with the operation added.</returns>
 public abstract TransactionalBatch UpsertItemStream(
     Stream streamPayload,
     TransactionalBatchItemRequestOptions requestOptions = null);
コード例 #19
0
 /// <summary>
 /// Adds an operation to upsert an item into the batch.
 /// </summary>
 /// <param name="item">A JSON serializable object that must contain an id property. <see cref="CosmosSerializer"/> to implement a custom serializer.</param>
 /// <param name="requestOptions">(Optional) The options for the item request. <see cref="TransactionalBatchItemRequestOptions"/>.</param>
 /// <returns>The <see cref="TransactionalBatch"/> instance with the operation added.</returns>
 /// <typeparam name="T">The type of item to be created.</typeparam>
 public abstract TransactionalBatch UpsertItem <T>(
     T item,
     TransactionalBatchItemRequestOptions requestOptions = null);
コード例 #20
0
 /// <summary>
 /// Adds an operation to read an item into the batch.
 /// </summary>
 /// <param name="id">The cosmos item id.</param>
 /// <param name="requestOptions">(Optional) The options for the item request. <see cref="TransactionalBatchItemRequestOptions"/>.</param>
 /// <returns>The <see cref="TransactionalBatch"/> instance with the operation added.</returns>
 public abstract TransactionalBatch ReadItem(
     string id,
     TransactionalBatchItemRequestOptions requestOptions = null);
コード例 #21
0
 /// <summary>
 /// Adds an operation to patch an item into the batch.
 /// </summary>
 /// <param name="id">The unique id of the item.</param>
 /// <param name="patchOperations">Represents a list of operations to be sequentially applied to the referred Cosmos item.</param>
 /// <param name="requestOptions">(Optional) The options for the item request.</param>
 /// <returns>The transactional batch instance with the operation added.</returns>
 public abstract TransactionalBatch PatchItem(
     string id,
     System.Collections.Generic.IReadOnlyList <PatchOperation> patchOperations,
     TransactionalBatchItemRequestOptions requestOptions = null);
コード例 #22
0
 /// <summary>
 /// Adds an operation to patch an item into the batch.
 /// </summary>
 /// <param name="id">The unique id of the item.</param>
 /// <param name="patchOperations">Represents a list of operations to be sequentially applied to the referred Cosmos item.</param>
 /// <param name="requestOptions">(Optional) The options for the item request.</param>
 /// <returns>The transactional batch instance with the operation added.</returns>
 public abstract TransactionalBatch PatchItem(
     string id,
     IReadOnlyList <PatchOperation> patchOperations,
     TransactionalBatchItemRequestOptions requestOptions = null);