Exemplo n.º 1
0
 /// <summary>
 /// Materializes the operation's resource into a Memory{byte} wrapping a byte array.
 /// </summary>
 /// <param name="serializerCore">Serializer to serialize user provided objects to JSON.</param>
 /// <param name="cancellationToken"><see cref="CancellationToken"/> for cancellation.</param>
 internal virtual async Task MaterializeResourceAsync(CosmosSerializerCore serializerCore, CancellationToken cancellationToken)
 {
     if (this.body.IsEmpty && this.ResourceStream != null)
     {
         this.body = await BatchExecUtils.StreamToMemoryAsync(this.ResourceStream, cancellationToken);
     }
 }
        public async Task <TransactionalBatchResponse> ExecuteAsync(CancellationToken cancellationToken)
        {
            using (this.diagnosticsContext.CreateOverallScope("BatchExecuteAsync"))
            {
                BatchExecUtils.EnsureValid(this.inputOperations, this.batchOptions);

                PartitionKey?serverRequestPartitionKey = this.partitionKey;
                if (this.batchOptions != null && this.batchOptions.IsEffectivePartitionKeyRouting)
                {
                    serverRequestPartitionKey = null;
                }

                SinglePartitionKeyServerBatchRequest serverRequest;
                using (this.diagnosticsContext.CreateScope("CreateBatchRequest"))
                {
                    serverRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                        serverRequestPartitionKey,
                        new ArraySegment <ItemBatchOperation>(this.inputOperations.ToArray()),
                        this.clientContext.SerializerCore,
                        cancellationToken);
                }

                return(await this.ExecuteServerRequestAsync(serverRequest, cancellationToken));
            }
        }
Exemplo n.º 3
0
        public async Task <BatchResponse> ExecuteAsync(CancellationToken cancellationToken)
        {
            BatchExecUtils.EnsureValid(this.inputOperations, this.batchOptions, this.maxServerRequestOperationCount);

            PartitionKey?serverRequestPartitionKey = this.partitionKey;

            if (this.batchOptions != null && this.batchOptions.IsEffectivePartitionKeyRouting)
            {
                serverRequestPartitionKey = null;
            }

            SinglePartitionKeyServerBatchRequest serverRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                serverRequestPartitionKey,
                new ArraySegment <ItemBatchOperation>(this.inputOperations.ToArray()),
                this.maxServerRequestBodyLength,
                this.maxServerRequestOperationCount,
                serializer : this.clientContext.CosmosSerializer,
                cancellationToken : cancellationToken);

            if (serverRequest.Operations.Count != this.inputOperations.Count)
            {
                throw new CosmosException(HttpStatusCode.RequestEntityTooLarge, ClientResources.BatchTooLarge);
            }

            return(await this.ExecuteServerRequestAsync(serverRequest, cancellationToken));
        }
        public async Task <TransactionalBatchResponse> ExecuteAsync(ITrace trace, CancellationToken cancellationToken)
        {
            using (ITrace executeNextBatchTrace = trace.StartChild("Execute Next Batch", TraceComponent.Batch, Tracing.TraceLevel.Info))
            {
                BatchExecUtils.EnsureValid(this.inputOperations, this.batchOptions);

                PartitionKey?serverRequestPartitionKey = this.partitionKey;
                if (this.batchOptions != null && this.batchOptions.IsEffectivePartitionKeyRouting)
                {
                    serverRequestPartitionKey = null;
                }

                SinglePartitionKeyServerBatchRequest serverRequest;
                serverRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                    serverRequestPartitionKey,
                    new ArraySegment <ItemBatchOperation>(this.inputOperations.ToArray()),
                    this.clientContext.SerializerCore,
                    executeNextBatchTrace,
                    cancellationToken);

                return(await this.ExecuteServerRequestAsync(
                           serverRequest,
                           executeNextBatchTrace,
                           cancellationToken));
            }
        }
        public static void EnsureValid(
            IReadOnlyList <ItemBatchOperation> operations,
            RequestOptions batchOptions)
        {
            string errorMessage = BatchExecUtils.IsValid(operations, batchOptions);

            if (errorMessage != null)
            {
                throw new ArgumentException(errorMessage);
            }
        }
        public static void EnsureValid(
            IReadOnlyList <ItemBatchOperation> operations,
            RequestOptions batchOptions,
            int?maxOperationCount = null)
        {
            string errorMessage = BatchExecUtils.EnsureValidInternal(operations, batchOptions, maxOperationCount);

            if (errorMessage != null)
            {
                throw new ArgumentException(errorMessage);
            }
        }
        public static ResponseMessage EnsureValidStream(
            IReadOnlyList <ItemBatchOperation> operations,
            RequestOptions batchOptions,
            int?maxOperationCount = null)
        {
            string errorMessage = BatchExecUtils.EnsureValidInternal(operations, batchOptions, maxOperationCount);

            if (errorMessage != null)
            {
                return(new ResponseMessage(HttpStatusCode.BadRequest, errorMessage: errorMessage));
            }

            return(new ResponseMessage(HttpStatusCode.OK));
        }
        private async Task <string> ResolvePartitionKeyRangeIdAsync(
            ItemBatchOperation operation,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            PartitionKeyDefinition partitionKeyDefinition = await this.cosmosContainer.GetPartitionKeyDefinitionAsync(cancellationToken);

            CollectionRoutingMap collectionRoutingMap = await this.cosmosContainer.GetRoutingMapAsync(cancellationToken);

            Debug.Assert(operation.RequestOptions?.Properties?.TryGetValue(WFConstants.BackendHeaders.EffectivePartitionKeyString, out object epkObj) == null, "EPK is not supported");
            await this.FillOperationPropertiesAsync(operation, cancellationToken);

            return(BatchExecUtils.GetPartitionKeyRangeId(operation.PartitionKey.Value, partitionKeyDefinition, collectionRoutingMap));
        }
        /// <summary>
        /// Encrypts (if encryption options are set) and materializes the operation's resource into a Memory{byte} wrapping a byte array.
        /// </summary>
        /// <param name="serializerCore">Serializer to serialize user provided objects to JSON.</param>
        /// <param name="cancellationToken"><see cref="CancellationToken"/> for cancellation.</param>
        internal virtual async Task EncryptAndMaterializeResourceAsync(CosmosSerializerCore serializerCore, CancellationToken cancellationToken)
        {
            if (this.body.IsEmpty && this.ResourceStream != null)
            {
                Stream stream = this.ResourceStream;
                if (this.ContainerCore != null && this.RequestOptions?.EncryptionOptions != null)
                {
                    stream = await this.ContainerCore.ClientContext.EncryptItemAsync(
                        stream,
                        this.RequestOptions.EncryptionOptions,
                        (DatabaseCore)this.ContainerCore.Database,
                        this.DiagnosticsContext,
                        cancellationToken);
                }

                this.body = await BatchExecUtils.StreamToMemoryAsync(stream, cancellationToken);
            }
        }
Exemplo n.º 10
0
        public async Task <BatchResponse> ExecuteAsync(CancellationToken cancellationToken)
        {
            BatchExecUtils.EnsureValid(this.inputOperations, this.batchOptions);

            PartitionKey?serverRequestPartitionKey = this.partitionKey;

            if (this.batchOptions != null && this.batchOptions.IsEffectivePartitionKeyRouting)
            {
                serverRequestPartitionKey = null;
            }

            SinglePartitionKeyServerBatchRequest serverRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
                serverRequestPartitionKey,
                new ArraySegment <ItemBatchOperation>(this.inputOperations.ToArray()),
                this.clientContext.CosmosSerializer,
                cancellationToken);

            return(await this.ExecuteServerRequestAsync(serverRequest, cancellationToken));
        }