|
18 | 18 | import com.azure.cosmos.models.FeedResponse; |
19 | 19 | import com.azure.cosmos.models.PartitionKey; |
20 | 20 | import com.azure.cosmos.models.SqlQuerySpec; |
| 21 | +import com.azure.cosmos.models.CosmosBulkOperationResponse; |
| 22 | +import com.azure.cosmos.models.CosmosItemOperation; |
| 23 | +import com.azure.cosmos.models.CosmosBulkExecutionOptions; |
| 24 | +import com.azure.cosmos.models.CosmosPatchOperations; |
| 25 | +import com.azure.cosmos.models.CosmosPatchItemRequestOptions; |
21 | 26 | import com.azure.cosmos.util.CosmosPagedFlux; |
22 | 27 | import com.azure.cosmos.util.CosmosPagedIterable; |
23 | 28 | import reactor.core.Exceptions; |
| 29 | +import reactor.core.publisher.Flux; |
24 | 30 | import reactor.core.publisher.Mono; |
25 | 31 |
|
| 32 | +import java.util.List; |
| 33 | + |
26 | 34 | import static com.azure.cosmos.implementation.guava25.base.Preconditions.checkNotNull; |
27 | 35 |
|
28 | 36 | /** |
@@ -359,6 +367,82 @@ public CosmosBatchResponse executeCosmosBatch( |
359 | 367 | return this.blockBatchResponse(this.cosmosEncryptionAsyncContainer.executeCosmosBatch(cosmosBatch, requestOptions)); |
360 | 368 | } |
361 | 369 |
|
| 370 | + /** |
| 371 | + * Run patch operations on an Item. |
| 372 | + * |
| 373 | + * @param <T> the type parameter. |
| 374 | + * @param itemId the item id. |
| 375 | + * @param partitionKey the partition key. |
| 376 | + * @param cosmosPatchOperations Represents a container having list of operations to be sequentially applied to the referred Cosmos item. |
| 377 | + * @param options the request options. |
| 378 | + * @param itemType the item type. |
| 379 | + * |
| 380 | + * @return the Cosmos item resource response with the patched item or an exception. |
| 381 | + */ |
| 382 | + public <T> CosmosItemResponse<T> patchItem( |
| 383 | + String itemId, |
| 384 | + PartitionKey partitionKey, |
| 385 | + CosmosPatchOperations cosmosPatchOperations, |
| 386 | + CosmosPatchItemRequestOptions options, |
| 387 | + Class<T> itemType) { |
| 388 | + |
| 389 | + return this.blockItemResponse(this.cosmosEncryptionAsyncContainer.patchItem(itemId, partitionKey, cosmosPatchOperations, options, itemType)); |
| 390 | + } |
| 391 | + |
| 392 | + /** |
| 393 | + * Executes list of operations in Bulk. |
| 394 | + * |
| 395 | + * @param <TContext> The context for the bulk processing. |
| 396 | + * @param operations list of operation which will be executed by this container. |
| 397 | + * |
| 398 | + * @return An Iterable of {@link CosmosBulkOperationResponse} which contains operation and it's response or exception. |
| 399 | + * <p> |
| 400 | + * To create a operation which can be executed here, use {@link com.azure.cosmos.models.CosmosBulkOperations}. For eg. |
| 401 | + * for a upsert operation use {@link com.azure.cosmos.models.CosmosBulkOperations#getUpsertItemOperation(Object, PartitionKey)} |
| 402 | + * </p> |
| 403 | + * <p> |
| 404 | + * We can get the corresponding operation using {@link CosmosBulkOperationResponse#getOperation()} and |
| 405 | + * it's response using {@link CosmosBulkOperationResponse#getResponse()}. If the operation was executed |
| 406 | + * successfully, the value returned by {@link com.azure.cosmos.models.CosmosBulkItemResponse#isSuccessStatusCode()} will be true. To get |
| 407 | + * actual status use {@link com.azure.cosmos.models.CosmosBulkItemResponse#getStatusCode()}. |
| 408 | + * </p> |
| 409 | + * To check if the operation had any exception, use {@link CosmosBulkOperationResponse#getException()} to |
| 410 | + * get the exception. |
| 411 | + */ |
| 412 | + public <TContext> Iterable<CosmosBulkOperationResponse<TContext>> executeBulkOperations( |
| 413 | + Iterable<CosmosItemOperation> operations) { |
| 414 | + return this.blockBulkResponse(this.cosmosEncryptionAsyncContainer.executeBulkOperations(Flux.fromIterable(operations))); |
| 415 | + } |
| 416 | + |
| 417 | + /** |
| 418 | + * Executes list of operations in Bulk. |
| 419 | + * |
| 420 | + * @param <TContext> The context for the bulk processing. |
| 421 | + * |
| 422 | + * @param operations list of operation which will be executed by this container. |
| 423 | + * @param bulkOptions Options that apply for this Bulk request which specifies options regarding execution like |
| 424 | + * concurrency, batching size, interval and context. |
| 425 | + * |
| 426 | + * @return An Iterable of {@link CosmosBulkOperationResponse} which contains operation and it's response or exception. |
| 427 | + * <p> |
| 428 | + * To create a operation which can be executed here, use {@link com.azure.cosmos.models.CosmosBulkOperations}. For eg. |
| 429 | + * for a upsert operation use {@link com.azure.cosmos.models.CosmosBulkOperations#getUpsertItemOperation(Object, PartitionKey)} |
| 430 | + * </p> |
| 431 | + * <p> |
| 432 | + * We can get the corresponding operation using {@link CosmosBulkOperationResponse#getOperation()} and |
| 433 | + * it's response using {@link CosmosBulkOperationResponse#getResponse()}. If the operation was executed |
| 434 | + * successfully, the value returned by {@link com.azure.cosmos.models.CosmosBulkItemResponse#isSuccessStatusCode()} will be true. To get |
| 435 | + * actual status use {@link com.azure.cosmos.models.CosmosBulkItemResponse#getStatusCode()}. |
| 436 | + * </p> |
| 437 | + * To check if the operation had any exception, use {@link CosmosBulkOperationResponse#getException()} to |
| 438 | + * get the exception. |
| 439 | + */ |
| 440 | + public <TContext> Iterable<CosmosBulkOperationResponse<TContext>> executeBulkOperations( |
| 441 | + Iterable<CosmosItemOperation> operations, |
| 442 | + CosmosBulkExecutionOptions bulkOptions) { |
| 443 | + return this.blockBulkResponse(this.cosmosEncryptionAsyncContainer.executeBulkOperations(Flux.fromIterable(operations), bulkOptions)); |
| 444 | + } |
| 445 | + |
362 | 446 | /** |
363 | 447 | * Gets the CosmosContainer |
364 | 448 | * |
@@ -408,4 +492,19 @@ private CosmosBatchResponse blockBatchResponse(Mono<CosmosBatchResponse> batchRe |
408 | 492 | } |
409 | 493 | } |
410 | 494 | } |
| 495 | + |
| 496 | + private <TContext> List<CosmosBulkOperationResponse<TContext>> blockBulkResponse( |
| 497 | + Flux<CosmosBulkOperationResponse<TContext>> bulkResponse) { |
| 498 | + |
| 499 | + try { |
| 500 | + return bulkResponse.collectList().block(); |
| 501 | + } catch (Exception ex) { |
| 502 | + final Throwable throwable = Exceptions.unwrap(ex); |
| 503 | + if (throwable instanceof CosmosException) { |
| 504 | + throw (CosmosException) throwable; |
| 505 | + } else { |
| 506 | + throw ex; |
| 507 | + } |
| 508 | + } |
| 509 | + } |
411 | 510 | } |
0 commit comments