|
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;
|
| 19 | +import static software.amazon.awssdk.enhanced.dynamodb.internal.IndexUtils.extractGlobalSecondaryIndices; |
| 20 | +import static software.amazon.awssdk.enhanced.dynamodb.internal.IndexUtils.extractLocalSecondaryIndices; |
| 21 | +import static software.amazon.awssdk.enhanced.dynamodb.internal.IndexUtils.splitSecondaryIndicesToLocalAndGlobalOnes; |
20 | 22 |
|
21 | 23 | import java.util.Collection;
|
22 | 24 | import java.util.List;
|
23 | 25 | import java.util.Map;
|
24 | 26 | import java.util.function.Consumer;
|
25 |
| -import java.util.stream.Collectors; |
26 | 27 | import software.amazon.awssdk.annotations.SdkInternalApi;
|
27 | 28 | import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension;
|
28 | 29 | import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
|
29 | 30 | import software.amazon.awssdk.enhanced.dynamodb.IndexMetadata;
|
30 | 31 | import software.amazon.awssdk.enhanced.dynamodb.Key;
|
31 |
| -import software.amazon.awssdk.enhanced.dynamodb.KeyAttributeMetadata; |
32 | 32 | import software.amazon.awssdk.enhanced.dynamodb.TableMetadata;
|
33 | 33 | import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
|
34 | 34 | import software.amazon.awssdk.enhanced.dynamodb.internal.operations.CreateTableOperation;
|
|
46 | 46 | import software.amazon.awssdk.enhanced.dynamodb.model.DeleteItemEnhancedRequest;
|
47 | 47 | import software.amazon.awssdk.enhanced.dynamodb.model.DeleteItemEnhancedResponse;
|
48 | 48 | 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 | 49 | import software.amazon.awssdk.enhanced.dynamodb.model.GetItemEnhancedRequest;
|
52 | 50 | import software.amazon.awssdk.enhanced.dynamodb.model.GetItemEnhancedResponse;
|
53 | 51 | import software.amazon.awssdk.enhanced.dynamodb.model.PageIterable;
|
|
61 | 59 | import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
|
62 | 60 | import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest;
|
63 | 61 | import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse;
|
64 |
| -import software.amazon.awssdk.services.dynamodb.model.ProjectionType; |
65 | 62 |
|
66 | 63 | @SdkInternalApi
|
67 | 64 | public class DefaultDynamoDbTable<T> implements DynamoDbTable<T> {
|
@@ -126,52 +123,14 @@ public void createTable(Consumer<CreateTableEnhancedRequest.Builder> requestCons
|
126 | 123 |
|
127 | 124 | @Override
|
128 | 125 | public void createTable() {
|
129 |
| - Map<IndexType, List<IndexMetadata>> indexGroups = splitSecondaryIndicesToLocalAndGlobalOnes(); |
| 126 | + Collection<IndexMetadata> indices = tableSchema.tableMetadata().indices(); |
| 127 | + Map<IndexType, List<IndexMetadata>> indexGroups = splitSecondaryIndicesToLocalAndGlobalOnes(indices); |
130 | 128 | createTable(CreateTableEnhancedRequest.builder()
|
131 | 129 | .localSecondaryIndices(extractLocalSecondaryIndices(indexGroups))
|
132 | 130 | .globalSecondaryIndices(extractGlobalSecondaryIndices(indexGroups))
|
133 | 131 | .build());
|
134 | 132 | }
|
135 | 133 |
|
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 | 134 | @Override
|
176 | 135 | public T deleteItem(DeleteItemEnhancedRequest request) {
|
177 | 136 | TableOperation<T, ?, ?, DeleteItemEnhancedResponse<T>> operation = DeleteItemOperation.create(request);
|
|
0 commit comments