All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.azure.cosmos.TransactionalBatch Maven / Gradle / Ivy

Go to download

This Package contains Microsoft Azure Cosmos SDK (with Reactive Extension Reactor support) for Azure Cosmos DB SQL API

There is a newer version: 4.61.1
Show newest version
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.cosmos;

import com.azure.cosmos.implementation.batch.ItemBatchOperation;
import com.azure.cosmos.models.PartitionKey;
import com.azure.cosmos.util.Beta;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.azure.cosmos.implementation.guava25.base.Preconditions.checkNotNull;

/**
 *
 * @deprecated forRemoval = true, since = "4.19"
 * This class is not necessary anymore and will be removed. Please use {@link com.azure.cosmos.models.CosmosBatch}
 *
 * Represents a batch of operations against items with the same {@link PartitionKey} in a container that will be performed
 * in a transactional manner at the Azure Cosmos DB service.
 * 

* Use {@link TransactionalBatch#createTransactionalBatch(PartitionKey)} to create an instance of TransactionalBatch. * Example * This example atomically modifies a set of items as a batch. *

{@code
 * public class ToDoActivity {
 *     public final String type;
 *     public final String id;
 *     public final String status;
 *     public ToDoActivity(String type, String id, String status) {
 *         this.type = type;
 *         this.id = id;
 *         this.status = status;
 *     }
 * }
 *
 * String activityType = "personal";
 *
 * ToDoActivity test1 = new ToDoActivity(activityType, "learning", "ToBeDone");
 * ToDoActivity test2 = new ToDoActivity(activityType, "shopping", "Done");
 * ToDoActivity test3 = new ToDoActivity(activityType, "swimming", "ToBeDone");
 *
 * TransactionalBatch batch = TransactionalBatch.createTransactionalBatch(new Cosmos.PartitionKey(activityType));
 * batch.createItemOperation(test1);
 * batch.replaceItemOperation(test2.id, test2);
 * batch.upsertItemOperation(test3);
 * batch.deleteItemOperation("reading");
 *
 * TransactionalBatchResponse response = container.executeTransactionalBatch(batch);
 *
 * if (!response.isSuccessStatusCode()) {
 *      // Handle and log exception
 *      return;
 * }
 *
 * // Look up interested results - e.g., via typed access on operation results
 *
 * TransactionalBatchOperationResult result = response.get(0);
 * ToDoActivity readActivity = result.getItem(ToDoActivity.class);
 *
 * }
* * Example *

This example atomically reads a set of items as a batch. *

{@code
 * String activityType = "personal";
 *
 * TransactionalBatch batch = TransactionalBatch.createTransactionalBatch(new Cosmos.PartitionKey(activityType));
 * batch.readItemOperation("playing");
 * batch.readItemOperation("walking");
 * batch.readItemOperation("jogging");
 * batch.readItemOperation("running");
 *
 * TransactionalBatchResponse response = container.executeTransactionalBatch(batch);
 * List resultItems = new ArrayList();
 *
 * for (int i = 0; i < response.size(); i++) {
 *     TransactionalBatchOperationResult result = response.get(0);
 *     resultItems.add(result.getItem(ToDoActivity.class));
 * }
 *
 * }
*

* See: * Limits on TransactionalBatch requests. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public final class TransactionalBatch { private final List> operations; private final PartitionKey partitionKey; TransactionalBatch(PartitionKey partitionKey) { checkNotNull(partitionKey, "expected non-null partitionKey"); this.operations = new ArrayList<>(); this.partitionKey = partitionKey; } /** * Initializes a new instance of {@link TransactionalBatch} * that will contain operations to be performed across multiple items in the container with the provided partition * key in a transactional manner * * @param partitionKey the partition key for all items in the batch. * * @return A new instance of {@link TransactionalBatch}. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public static TransactionalBatch createTransactionalBatch(PartitionKey partitionKey) { return new TransactionalBatch(partitionKey); } /** * Adds an operation to create an item into the batch. * * @param item A JSON serializable object that must contain an id property. * @param The type of item to be created. * * @return The transactional batch instance with the operation added. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public CosmosItemOperation createItemOperation(T item) { checkNotNull(item, "expected non-null item"); return this.createItemOperation(item, new TransactionalBatchItemRequestOptions()); } /** * Adds an operation to create an item into the batch. * * @param The type of item to be created. * * @param item A JSON serializable object that must contain an id property. * @param requestOptions The options for the item request. * * @return The transactional batch instance with the operation added. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public CosmosItemOperation createItemOperation(T item, TransactionalBatchItemRequestOptions requestOptions) { checkNotNull(item, "expected non-null item"); if (requestOptions == null) { requestOptions = new TransactionalBatchItemRequestOptions(); } ItemBatchOperation operation = new ItemBatchOperation( com.azure.cosmos.models.CosmosItemOperationType.CREATE, null, this.getPartitionKeyValue(), requestOptions.toRequestOptions(), item ); this.operations.add(operation); return BridgeInternal.toDeprecatedCosmosItemOperation(operation); } /** * Adds an operation to delete an item into the batch. * * @param id The unique id of the item. * * @return The transactional batch instance with the operation added. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public CosmosItemOperation deleteItemOperation(String id) { checkNotNull(id, "expected non-null id"); return this.deleteItemOperation(id, new TransactionalBatchItemRequestOptions()); } /** * Adds an operation to delete an item into the batch. * * @param id The unique id of the item. * @param requestOptions The options for the item request. * * @return The transactional batch instance with the operation added. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public CosmosItemOperation deleteItemOperation(String id, TransactionalBatchItemRequestOptions requestOptions) { checkNotNull(id, "expected non-null id"); if (requestOptions == null) { requestOptions = new TransactionalBatchItemRequestOptions(); } ItemBatchOperation operation = new ItemBatchOperation<>( com.azure.cosmos.models.CosmosItemOperationType.DELETE, id, this.getPartitionKeyValue(), requestOptions.toRequestOptions(), null ); this.operations.add(operation); return BridgeInternal.toDeprecatedCosmosItemOperation(operation); } /** * Adds an operation to read an item into the batch. * * @param id The unique id of the item. * * @return The transactional batch instance with the operation added. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public CosmosItemOperation readItemOperation(String id) { checkNotNull(id, "expected non-null id"); return this.readItemOperation(id, new TransactionalBatchItemRequestOptions()); } /** * Adds an operation to read an item into the batch. * * @param id The unique id of the item. * @param requestOptions The options for the item request. * * @return The transactional batch instance with the operation added. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public CosmosItemOperation readItemOperation(String id, TransactionalBatchItemRequestOptions requestOptions) { checkNotNull(id, "expected non-null id"); if (requestOptions == null) { requestOptions = new TransactionalBatchItemRequestOptions(); } ItemBatchOperation operation = new ItemBatchOperation<>( com.azure.cosmos.models.CosmosItemOperationType.READ, id, this.getPartitionKeyValue(), requestOptions.toRequestOptions(), null ); this.operations.add(operation); return BridgeInternal.toDeprecatedCosmosItemOperation(operation); } /** * Adds an operation to replace an item into the batch. * * @param id The unique id of the item. * @param item A JSON serializable object that must contain an id property. * @param The type of item to be replaced. * * @return The transactional batch instance with the operation added. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public CosmosItemOperation replaceItemOperation(String id, T item) { checkNotNull(id, "expected non-null id"); checkNotNull(item, "expected non-null item"); return this.replaceItemOperation(id, item, new TransactionalBatchItemRequestOptions()); } /** * Adds an operation to replace an item into the batch. * * @param The type of item to be replaced. * * @param id The unique id of the item. * @param item A JSON serializable object that must contain an id property. * @param requestOptions The options for the item request. * * @return The transactional batch instance with the operation added. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public CosmosItemOperation replaceItemOperation( String id, T item, TransactionalBatchItemRequestOptions requestOptions) { checkNotNull(id, "expected non-null id"); checkNotNull(item, "expected non-null item"); if (requestOptions == null) { requestOptions = new TransactionalBatchItemRequestOptions(); } ItemBatchOperation operation = new ItemBatchOperation( com.azure.cosmos.models.CosmosItemOperationType.REPLACE, id, this.getPartitionKeyValue(), requestOptions.toRequestOptions(), item ); this.operations.add(operation); return BridgeInternal.toDeprecatedCosmosItemOperation(operation); } /** * Adds an operation to upsert an item into the batch. * * @param item A JSON serializable object that must contain an id property. * @param The type of item to be upserted. * * @return The transactional batch instance with the operation added. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public CosmosItemOperation upsertItemOperation(T item) { checkNotNull(item, "expected non-null item"); return this.upsertItemOperation(item, new TransactionalBatchItemRequestOptions()); } /** * Adds an operation to upsert an item into the batch. * * @param The type of item to be upserted. * * @param item A JSON serializable object that must contain an id property. * @param requestOptions The options for the item request. * * @return The transactional batch instance with the operation added. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public CosmosItemOperation upsertItemOperation(T item, TransactionalBatchItemRequestOptions requestOptions) { checkNotNull(item, "expected non-null item"); if (requestOptions == null) { requestOptions = new TransactionalBatchItemRequestOptions(); } ItemBatchOperation operation = new ItemBatchOperation( com.azure.cosmos.models.CosmosItemOperationType.UPSERT, null, this.getPartitionKeyValue(), requestOptions.toRequestOptions(), item ); this.operations.add(operation); return BridgeInternal.toDeprecatedCosmosItemOperation(operation); } /** * Adds a patch operations for an item into the batch. * * @param id the item id. * @param cosmosPatchOperations Represents a container having list of operations to be sequentially applied to the referred Cosmos item. * * @return The added operation. */ @Beta(value = Beta.SinceVersion.V4_11_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public CosmosItemOperation patchItemOperation(String id, CosmosPatchOperations cosmosPatchOperations) { checkNotNull(id, "expected non-null id"); checkNotNull(cosmosPatchOperations, "expected non-null cosmosPatchOperations"); return this.patchItemOperation(id, cosmosPatchOperations, new TransactionalBatchPatchItemRequestOptions()); } /** * Adds a patch operations for an item into the batch. * * @param id the item id. * @param cosmosPatchOperations Represents a container having list of operations to be sequentially applied to the referred Cosmos item. * @param requestOptions The options for the item request. * * @return The added operation. */ @Beta(value = Beta.SinceVersion.V4_11_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public CosmosItemOperation patchItemOperation( String id, CosmosPatchOperations cosmosPatchOperations, TransactionalBatchPatchItemRequestOptions requestOptions) { checkNotNull(id, "expected non-null id"); checkNotNull(cosmosPatchOperations, "expected non-null cosmosPatchOperations"); if (requestOptions == null) { requestOptions = new TransactionalBatchPatchItemRequestOptions(); } ItemBatchOperation operation = new ItemBatchOperation<>( com.azure.cosmos.models.CosmosItemOperationType.PATCH, id, this.getPartitionKeyValue(), requestOptions.toRequestOptions(), cosmosPatchOperations ); this.operations.add(operation); return BridgeInternal.toDeprecatedCosmosItemOperation(operation); } /** * Return the list of operation in an unmodifiable instance so no one can change it in the down path. * * @return The list of operations which are to be executed. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public List getOperations() { return Collections.unmodifiableList(operations.stream().map(BridgeInternal::toDeprecatedCosmosItemOperation).collect(Collectors.toList())); } List> getOperationsInternal() { return operations; } /** * Return the partition key for this batch. * * @return The partition key for this batch. */ @Beta(value = Beta.SinceVersion.V4_7_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING) @Deprecated() //forRemoval = true, since = "4.19" public PartitionKey getPartitionKeyValue() { return partitionKey; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy