15
15
16
16
package software .amazon .awssdk .extensions .dynamodb .mappingclient ;
17
17
18
+ import java .util .concurrent .CompletableFuture ;
18
19
import java .util .function .Function ;
19
20
20
21
import software .amazon .awssdk .annotations .SdkPublicApi ;
22
+ import software .amazon .awssdk .services .dynamodb .DynamoDbAsyncClient ;
21
23
import software .amazon .awssdk .services .dynamodb .DynamoDbClient ;
22
24
23
25
/**
24
- * Common interface for a single operation that can be executed against a mapped database table. These operations can be
25
- * made against either the primary index of a table or a secondary index, although some implementations of this
26
- * interface do not support secondary indices and will throw an exception when executed against one. Conceptually an
27
- * operation maps 1:1 with an actual DynamoDb call.
28
- *
26
+ * Common interface for a single operation that can be executed in a synchronous or non-blocking asynchronous fashion
27
+ * against a mapped database table. These operations can be made against either the primary index of a table or a
28
+ * secondary index, although some implementations of this interface do not support secondary indices and will throw
29
+ * an exception when executed against one. Conceptually an operation maps 1:1 with an actual DynamoDb call.
30
+ * <p>
29
31
* This interface is extended by {@link TableOperation} and {@link IndexOperation} which contain implementations of
30
32
* the behavior to actually execute the operation in the context of a table or secondary index and are used by
31
- * {@link MappedTable} and {@link MappedIndex} respectively. By sharing this common interface operations are able to
32
- * re-use code regardless of whether they are executed in the context of a primary or secondary index.
33
+ * {@link MappedTable} or {@link AsyncMappedTable} and {@link MappedIndex} or {@link AsyncMappedIndex} respectively. By
34
+ * sharing this common interface operations are able to re-use code regardless of whether they are executed in the
35
+ * context of a primary or secondary index or whether they are being executed in a synchronous or non-blocking
36
+ * asynchronous fashion.
33
37
*
34
38
* @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}.
39
+ * @param <RequestT> The type of the request object for the DynamoDb call in the low level {@link DynamoDbClient} or
40
+ * {@link DynamoDbAsyncClient}.
41
+ * @param <ResponseT> The type of the response object for the DynamoDb call in the low level {@link DynamoDbClient}
42
+ * or {@link DynamoDbAsyncClient}.
37
43
* @param <ResultT> The type of the mapped result object that will be returned by the execution of this operation.
38
44
*/
39
45
@ SdkPublicApi
@@ -49,12 +55,19 @@ public interface CommonOperation<ItemT, RequestT, ResponseT, ResultT> {
49
55
RequestT generateRequest (TableSchema <ItemT > tableSchema , OperationContext context , MapperExtension mapperExtension );
50
56
51
57
/**
52
- * Provides a function for making the low level SDK call to DynamoDb.
58
+ * Provides a function for making the low level synchronous SDK call to DynamoDb.
53
59
* @param dynamoDbClient A low level {@link DynamoDbClient} to make the call against.
54
60
* @return A function that calls DynamoDb with a provided request object and returns the response object.
55
61
*/
56
62
Function <RequestT , ResponseT > serviceCall (DynamoDbClient dynamoDbClient );
57
63
64
+ /**
65
+ * Provides a function for making the low level non-blocking asynchronous SDK call to DynamoDb.
66
+ * @param dynamoDbAsyncClient A low level {@link DynamoDbAsyncClient} to make the call against.
67
+ * @return A function that calls DynamoDb with a provided request object and returns the response object.
68
+ */
69
+ Function <RequestT , CompletableFuture <ResponseT >> asyncServiceCall (DynamoDbAsyncClient dynamoDbAsyncClient );
70
+
58
71
/**
59
72
* Takes the response object returned by the actual DynamoDb call and maps it into a higher level abstracted
60
73
* result object.
@@ -71,7 +84,8 @@ ResultT transformResponse(ResponseT response,
71
84
MapperExtension mapperExtension );
72
85
73
86
/**
74
- * Default implementation of a complete execution of this operation against either the primary or a secondary index.
87
+ * Default implementation of a complete synchronous execution of this operation against either the primary or a
88
+ * secondary index.
75
89
* It performs three steps:
76
90
* 1) Call generateRequest() to get the request object.
77
91
* 2) Call getServiceCall() and call it using the request object generated in the previous step.
@@ -92,4 +106,30 @@ default ResultT execute(TableSchema<ItemT> tableSchema,
92
106
ResponseT response = serviceCall (dynamoDbClient ).apply (request );
93
107
return transformResponse (response , tableSchema , context , mapperExtension );
94
108
}
109
+
110
+ /**
111
+ * Default implementation of a complete non-blocking asynchronous execution of this operation against either the
112
+ * primary or a secondary index.
113
+ * It performs three steps:
114
+ * 1) Call generateRequest() to get the request object.
115
+ * 2) Call getServiceCall() and call it using the request object generated in the previous step.
116
+ * 3) Wraps the {@link CompletableFuture} returned by the SDK in a new one that calls transformResponse() to
117
+ * convert the response object returned in the previous step to a high level result.
118
+ *
119
+ * @param tableSchema A {@link TableSchema} that maps the table to a modelled object.
120
+ * @param context An object containing the context, or target, of the command execution.
121
+ * @param dynamoDbAsyncClient A {@link DynamoDbAsyncClient} to make the call against.
122
+ * @param mapperExtension A {@link MapperExtension} that may modify the request or result of this operation. A
123
+ * null value here will result in no modifications.
124
+ * @return A {@link CompletableFuture} of the high level result object as specified by the implementation of this
125
+ * operation.
126
+ */
127
+ default CompletableFuture <ResultT > executeAsync (TableSchema <ItemT > tableSchema ,
128
+ OperationContext context ,
129
+ MapperExtension mapperExtension ,
130
+ DynamoDbAsyncClient dynamoDbAsyncClient ) {
131
+ RequestT request = generateRequest (tableSchema , context , mapperExtension );
132
+ CompletableFuture <ResponseT > response = asyncServiceCall (dynamoDbAsyncClient ).apply (request );
133
+ return response .thenApply (r -> transformResponse (r , tableSchema , context , mapperExtension ));
134
+ }
95
135
}
0 commit comments