예제 #1
0
 public ItemBatchOperation(
     OperationType operationType,
     int operationIndex,
     string id             = null,
     Stream resourceStream = null,
     BatchItemRequestOptions requestOptions = null)
 {
     this.OperationType  = operationType;
     this.OperationIndex = operationIndex;
     this.Id             = id;
     this.ResourceStream = resourceStream;
     this.RequestOptions = requestOptions;
 }
        /// <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="BatchItemRequestOptions"/>.</param>
        /// <returns>The <see cref="Batch"/> instance with the operation added.</returns>
        public virtual Batch PatchItemStream(
            string id,
            Stream patchStream,
            BatchItemRequestOptions requestOptions = null)
        {
            this.operations.Add(new ItemBatchOperation(
                                    operationType: OperationType.Patch,
                                    operationIndex: this.operations.Count,
                                    id: id,
                                    resourceStream: patchStream,
                                    requestOptions: requestOptions));

            return(this);
        }
        public override Batch UpsertItem <T>(
            T item,
            BatchItemRequestOptions 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);
        }
        public override Batch ReadItem(
            string id,
            BatchItemRequestOptions 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);
        }
        public override Batch CreateItemStream(
            Stream streamPayload,
            BatchItemRequestOptions 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);
        }
        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;
            BatchItemRequestOptions batchItemRequestOptions = BatchItemRequestOptions.FromItemRequestOptions(itemRequestOptions);
            ItemBatchOperation      itemBatchOperation      = new ItemBatchOperation(operationType, /* index */ 0, partitionKey, itemId, streamPayload, batchItemRequestOptions);
            BatchOperationResult    batchOperationResult    = await cosmosContainerCore.BatchExecutor.AddAsync(itemBatchOperation, itemRequestOptions, cancellationToken);

            return(batchOperationResult.ToResponseMessage());
        }
        public override Batch ReplaceItem <T>(
            string id,
            T item,
            BatchItemRequestOptions 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);
        }
 /// <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="BatchItemRequestOptions"/>.</param>
 /// <returns>The <see cref="Batch"/> instance with the operation added.</returns>
 /// <typeparam name="T">The type of item to be created.</typeparam>
 public abstract Batch ReplaceItem <T>(
     string id,
     T item,
     BatchItemRequestOptions requestOptions = null);
 /// <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="BatchItemRequestOptions"/>.</param>
 /// <returns>The <see cref="Batch"/> instance with the operation added.</returns>
 public abstract Batch UpsertItemStream(
     Stream streamPayload,
     BatchItemRequestOptions requestOptions = null);
예제 #10
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="BatchItemRequestOptions"/>.</param>
 /// <returns>The <see cref="Batch"/> instance with the operation added.</returns>
 /// <typeparam name="T">The type of item to be created.</typeparam>
 public abstract Batch UpsertItem <T>(
     T item,
     BatchItemRequestOptions requestOptions = null);
예제 #11
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="BatchItemRequestOptions"/>.</param>
 /// <returns>The <see cref="Batch"/> instance with the operation added.</returns>
 public abstract Batch ReadItem(
     string id,
     BatchItemRequestOptions requestOptions = null);
예제 #12
0
 /// <summary>
 /// Adds an operation to create 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="BatchItemRequestOptions"/>.</param>
 /// <returns>The <see cref="Batch"/> instance with the operation added.</returns>
 /// <typeparam name="T">The type of item to be created.</typeparam>
 public abstract Batch CreateItem <T>(
     T item,
     BatchItemRequestOptions requestOptions = null);
예제 #13
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="BatchItemRequestOptions"/>.</param>
 /// <returns>The <see cref="Batch"/> instance with the operation added.</returns>
 public abstract Batch ReplaceItemStream(
     string id,
     Stream streamPayload,
     BatchItemRequestOptions requestOptions = null);
예제 #14
0
        internal static Result WriteOperation(ref RowWriter writer, TypeArgument typeArg, ItemBatchOperation operation)
        {
            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);
                }
            }

            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)
            {
                BatchItemRequestOptions 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 (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);
        }