1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.enhanced.dynamodb.internal.operations;
17 
18 import java.util.concurrent.CompletableFuture;
19 import software.amazon.awssdk.annotations.SdkInternalApi;
20 import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension;
21 import software.amazon.awssdk.enhanced.dynamodb.OperationContext;
22 import software.amazon.awssdk.enhanced.dynamodb.TableMetadata;
23 import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
24 import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
25 import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
26 
27 /**
28  * Interface for a single operation that can be executed against a mapped database table. These operations will be
29  * executed against the primary index of the table. Conceptually an operation maps 1:1 with an actual DynamoDb call.
30  * <p>
31  * A concrete implementation of this interface should also implement {@link IndexOperation} with the same types if
32  * the operation supports being executed against both the primary index and secondary indices.
33  *
34  * @param <ItemT> The modelled object that this table maps records to.
35  * @param <RequestT>  The type of the request object for the DynamoDb call in the low level {@link DynamoDbClient}.
36  * @param <ResponseT> The type of the response object for the DynamoDb call in the low level {@link DynamoDbClient}.
37  * @param <ResultT> The type of the mapped result object that will be returned by the execution of this operation.
38  */
39 @SdkInternalApi
40 public interface TableOperation<ItemT, RequestT, ResponseT, ResultT>
41     extends CommonOperation<ItemT, RequestT, ResponseT, ResultT> {
42     /**
43      * Default implementation of a complete synchronous execution of this operation against the primary index. It will
44      * construct a context based on the given table name and then call execute() on the {@link CommonOperation} interface to
45      * perform the operation.
46      *
47      * @param tableSchema A {@link TableSchema} that maps the table to a modelled object.
48      * @param tableName The physical name of the table to execute the operation against.
49      * @param dynamoDbClient A {@link DynamoDbClient} to make the call against.
50      * @param extension A {@link DynamoDbEnhancedClientExtension} that may modify the request or result of this
51      *                  operation. A null value here will result in no modifications.
52      * @return A high level result object as specified by the implementation of this operation.
53      */
executeOnPrimaryIndex(TableSchema<ItemT> tableSchema, String tableName, DynamoDbEnhancedClientExtension extension, DynamoDbClient dynamoDbClient)54     default ResultT executeOnPrimaryIndex(TableSchema<ItemT> tableSchema,
55                                           String tableName,
56                                           DynamoDbEnhancedClientExtension extension,
57                                           DynamoDbClient dynamoDbClient) {
58         OperationContext context = DefaultOperationContext.create(tableName, TableMetadata.primaryIndexName());
59         return execute(tableSchema, context, extension, dynamoDbClient);
60     }
61 
62     /**
63      * Default implementation of a complete non-blocking asynchronous execution of this operation against the primary
64      * index. It will construct a context based on the given table name and then call executeAsync() on the
65      * {@link CommonOperation} interface to perform the operation.
66      *
67      * @param tableSchema A {@link TableSchema} that maps the table to a modelled object.
68      * @param tableName The physical name of the table to execute the operation against.
69      * @param dynamoDbAsyncClient A {@link DynamoDbAsyncClient} to make the call against.
70      * @param extension A {@link DynamoDbEnhancedClientExtension} that may modify the request or result of this
71      *                  operation. A null value here will result in no modifications.
72      * @return A {@link CompletableFuture} of the high level result object as specified by the implementation of this
73      * operation.
74      */
executeOnPrimaryIndexAsync(TableSchema<ItemT> tableSchema, String tableName, DynamoDbEnhancedClientExtension extension, DynamoDbAsyncClient dynamoDbAsyncClient)75     default CompletableFuture<ResultT> executeOnPrimaryIndexAsync(TableSchema<ItemT> tableSchema,
76                                                                   String tableName,
77                                                                   DynamoDbEnhancedClientExtension extension,
78                                                                   DynamoDbAsyncClient dynamoDbAsyncClient) {
79 
80         OperationContext context = DefaultOperationContext.create(tableName, TableMetadata.primaryIndexName());
81         return executeAsync(tableSchema, context, extension, dynamoDbAsyncClient);
82     }
83 }
84