|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.enhanced.dynamodb.internal.client;
|
17 | 17 |
|
18 |
| -import static java.util.Collections.emptyList; |
19 | 18 | import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.createKeyFromItem;
|
20 | 19 |
|
21 |
| -import java.util.Collection; |
22 |
| -import java.util.List; |
23 |
| -import java.util.Map; |
| 20 | +import java.util.ArrayList; |
24 | 21 | import java.util.function.Consumer;
|
25 |
| -import java.util.stream.Collectors; |
26 | 22 | import software.amazon.awssdk.annotations.SdkInternalApi;
|
27 | 23 | import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension;
|
28 | 24 | import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
|
29 |
| -import software.amazon.awssdk.enhanced.dynamodb.IndexMetadata; |
30 | 25 | import software.amazon.awssdk.enhanced.dynamodb.Key;
|
31 |
| -import software.amazon.awssdk.enhanced.dynamodb.KeyAttributeMetadata; |
32 | 26 | import software.amazon.awssdk.enhanced.dynamodb.TableMetadata;
|
33 | 27 | import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
|
| 28 | +import software.amazon.awssdk.enhanced.dynamodb.internal.TableIndices; |
34 | 29 | import software.amazon.awssdk.enhanced.dynamodb.internal.operations.CreateTableOperation;
|
35 | 30 | import software.amazon.awssdk.enhanced.dynamodb.internal.operations.DeleteItemOperation;
|
36 | 31 | import software.amazon.awssdk.enhanced.dynamodb.internal.operations.DeleteTableOperation;
|
|
46 | 41 | import software.amazon.awssdk.enhanced.dynamodb.model.DeleteItemEnhancedRequest;
|
47 | 42 | import software.amazon.awssdk.enhanced.dynamodb.model.DeleteItemEnhancedResponse;
|
48 | 43 | import software.amazon.awssdk.enhanced.dynamodb.model.DescribeTableEnhancedResponse;
|
49 |
| -import software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex; |
50 |
| -import software.amazon.awssdk.enhanced.dynamodb.model.EnhancedLocalSecondaryIndex; |
51 | 44 | import software.amazon.awssdk.enhanced.dynamodb.model.GetItemEnhancedRequest;
|
52 | 45 | import software.amazon.awssdk.enhanced.dynamodb.model.GetItemEnhancedResponse;
|
53 | 46 | import software.amazon.awssdk.enhanced.dynamodb.model.PageIterable;
|
|
61 | 54 | import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
|
62 | 55 | import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest;
|
63 | 56 | import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse;
|
64 |
| -import software.amazon.awssdk.services.dynamodb.model.ProjectionType; |
65 | 57 |
|
66 | 58 | @SdkInternalApi
|
67 | 59 | public class DefaultDynamoDbTable<T> implements DynamoDbTable<T> {
|
@@ -126,52 +118,14 @@ public void createTable(Consumer<CreateTableEnhancedRequest.Builder> requestCons
|
126 | 118 |
|
127 | 119 | @Override
|
128 | 120 | public void createTable() {
|
129 |
| - Map<IndexType, List<IndexMetadata>> indexGroups = splitSecondaryIndicesToLocalAndGlobalOnes(); |
| 121 | + TableIndices indices = new TableIndices(new ArrayList<>(tableSchema.tableMetadata().indices())); |
| 122 | + |
130 | 123 | createTable(CreateTableEnhancedRequest.builder()
|
131 |
| - .localSecondaryIndices(extractLocalSecondaryIndices(indexGroups)) |
132 |
| - .globalSecondaryIndices(extractGlobalSecondaryIndices(indexGroups)) |
| 124 | + .localSecondaryIndices(indices.localSecondaryIndices()) |
| 125 | + .globalSecondaryIndices(indices.globalSecondaryIndices()) |
133 | 126 | .build());
|
134 | 127 | }
|
135 | 128 |
|
136 |
| - private Map<IndexType, List<IndexMetadata>> splitSecondaryIndicesToLocalAndGlobalOnes() { |
137 |
| - Collection<IndexMetadata> indices = tableSchema.tableMetadata().indices(); |
138 |
| - return indices.stream() |
139 |
| - .filter(index -> !TableMetadata.primaryIndexName().equals(index.name())) |
140 |
| - .collect(Collectors.groupingBy(metadata -> { |
141 |
| - String partitionKeyName = metadata.partitionKey().map(KeyAttributeMetadata::name).orElse(null); |
142 |
| - if (partitionKeyName == null) { |
143 |
| - return IndexType.LSI; |
144 |
| - } |
145 |
| - return IndexType.GSI; |
146 |
| - })); |
147 |
| - } |
148 |
| - |
149 |
| - private List<EnhancedLocalSecondaryIndex> extractLocalSecondaryIndices(Map<IndexType, List<IndexMetadata>> indicesGroups) { |
150 |
| - return indicesGroups.getOrDefault(IndexType.LSI, emptyList()).stream() |
151 |
| - .map(this::mapIndexMetadataToEnhancedLocalSecondaryIndex) |
152 |
| - .collect(Collectors.toList()); |
153 |
| - } |
154 |
| - |
155 |
| - private EnhancedLocalSecondaryIndex mapIndexMetadataToEnhancedLocalSecondaryIndex(IndexMetadata indexMetadata) { |
156 |
| - return EnhancedLocalSecondaryIndex.builder() |
157 |
| - .indexName(indexMetadata.name()) |
158 |
| - .projection(pb -> pb.projectionType(ProjectionType.ALL)) |
159 |
| - .build(); |
160 |
| - } |
161 |
| - |
162 |
| - private List<EnhancedGlobalSecondaryIndex> extractGlobalSecondaryIndices(Map<IndexType, List<IndexMetadata>> indicesGroups) { |
163 |
| - return indicesGroups.getOrDefault(IndexType.GSI, emptyList()).stream() |
164 |
| - .map(this::mapIndexMetadataToEnhancedGlobalSecondaryIndex) |
165 |
| - .collect(Collectors.toList()); |
166 |
| - } |
167 |
| - |
168 |
| - private EnhancedGlobalSecondaryIndex mapIndexMetadataToEnhancedGlobalSecondaryIndex(IndexMetadata indexMetadata) { |
169 |
| - return EnhancedGlobalSecondaryIndex.builder() |
170 |
| - .indexName(indexMetadata.name()) |
171 |
| - .projection(pb -> pb.projectionType(ProjectionType.ALL)) |
172 |
| - .build(); |
173 |
| - } |
174 |
| - |
175 | 129 | @Override
|
176 | 130 | public T deleteItem(DeleteItemEnhancedRequest request) {
|
177 | 131 | TableOperation<T, ?, ?, DeleteItemEnhancedResponse<T>> operation = DeleteItemOperation.create(request);
|
|
0 commit comments