private async Task <TableResult> ExecuteAsync(
            TableOperation operation,
            CloudTableClient client,
            CloudTable table,
            TableRequestOptions requestOptions,
            OperationContext operationContext)
        {
            TableResult result = null;

            try
            {
                switch (operation.OperationType)
                {
                case TableOperationType.Insert:
                    result = await this.HandleInsertAsync(operation, client, table, requestOptions, operationContext);

                    break;

                case TableOperationType.InsertOrMerge:
                    result = await this.HandleInsertOrMergeAsync(operation, client, table, requestOptions, operationContext);

                    break;

                case TableOperationType.Merge:
                    result = await this.HandleMergeAsync(operation, client, table, requestOptions, operationContext);

                    break;

                case TableOperationType.Delete:
                    result = await this.HandleDeleteAsync(operation, client, table, requestOptions, operationContext);

                    break;

                case TableOperationType.InsertOrReplace:
                    result = await this.HandleUpsertAsync(operation, client, table, requestOptions, operationContext);

                    break;

                case TableOperationType.Replace:
                    result = await this.HandleReplaceAsync(operation, client, table, requestOptions, operationContext);

                    break;

                case TableOperationType.Retrieve:
                    result = await this.HandleReadAsync(operation, client, table, requestOptions, operationContext);

                    break;

                default:
                    throw new NotSupportedException();
                }
            }
            catch (Exception ex)
            {
                StorageException storageException = EntityHelpers.GetTableResultFromException(ex, operation);
                operationContext.RequestResults.Add(storageException.RequestInformation);

                throw storageException;
            }

            operationContext.FireRequestCompleted(new RequestEventArgs(operationContext.LastResult));
            return(result);
        }
예제 #2
0
        private async Task <IList <TableResult> > ExecuteAsync(
            TableBatchOperation batch,
            CloudTableClient client,
            CloudTable table,
            TableRequestOptions requestOptions,
            OperationContext operationContext)
        {
            if (batch.Count == 0)
            {
                throw new InvalidOperationException(SR.EmptyBatchOperation);
            }

            if (batch.Count > 100)
            {
                throw new InvalidOperationException(SR.BatchExceededMaximumNumberOfOperations);
            }

            try
            {
                string          script = StellarBatchExecutor.MakeCreateDocumentsScript();
                StoredProcedure sproc  = await this.GetOrCreateStoredProcedureAsync(table, StellarBatchExecutor.BulkInsertOrMergeOrUpdate, script);

                List <Document>           javaScriptParams1 = new List <Document>();
                List <TableOperationType> javaScriptParams2 = new List <TableOperationType>();
                List <string>             javaScriptParams3 = new List <string>();
                string partitionKey = batch.FirstOrDefault().PartitionKey;
                foreach (TableOperation operation in batch)
                {
                    Document document = null;
                    if (operation.OperationType == TableOperationType.Retrieve)
                    {
                        document = this.GetDocumentWithPartitionAndRowKey(operation.RetrievePartitionKey, operation.RetrieveRowKey);
                    }
                    else
                    {
                        document = EntityHelpers.GetDocumentFromEntity(operation.Entity, operationContext, requestOptions);
                    }

                    javaScriptParams1.Add(document);
                    javaScriptParams2.Add(operation.OperationType);
                    javaScriptParams3.Add(operation.Entity == null ? "" : operation.Entity.ETag);
                }

                RequestOptions docdbRequestOptions = new RequestOptions {
                    PartitionKey = new PartitionKey(partitionKey)
                };
                StoredProcedureResponse <string> response =
                    await table.ServiceClient.DocumentClient.ExecuteStoredProcedureAsync <string>(sproc.SelfLink, docdbRequestOptions,
                                                                                                  javaScriptParams1.ToArray(), javaScriptParams2.ToArray(), javaScriptParams3.ToArray());

                JArray jArray = JArray.Parse(response.Response);

                List <TableResult> tableResults = new List <TableResult>();

                for (int i = 0; i < jArray.Count; i++)
                {
                    tableResults.Add(GetTableResultFromDocument(batch[i], jArray[i].ToObject <Document>(), operationContext));
                }

                return(tableResults);
            }
            catch (Exception ex)
            {
                throw EntityHelpers.GetTableResultFromException(ex);
            }
        }