Skip to content

Create secondary indices based on table bean annotations (#3923) #4004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "Amazon DynamoDB Enhanced",
"contributor": "breader124",
"type": "bugfix",
"description": "Thanks to this bugfix it'll be possible to create DynamoDB table containing\nsecondary indices when using no arugments `createTable` method from `DefaultDynamoDbTable`\nclass. Information about their presence might be expressed using annotations, but it was ignored\nand created tables didn't contain specified indices. Plase note that it is still not possible\nto specify projections for indices using annotations. By default, all fields will be projected."
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@

package software.amazon.awssdk.enhanced.dynamodb.internal.client;

import static java.util.Collections.emptyList;
import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.createKeyFromItem;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.IndexMetadata;
import software.amazon.awssdk.enhanced.dynamodb.Key;
import software.amazon.awssdk.enhanced.dynamodb.KeyAttributeMetadata;
import software.amazon.awssdk.enhanced.dynamodb.TableMetadata;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.enhanced.dynamodb.internal.operations.CreateTableOperation;
Expand All @@ -39,6 +46,8 @@
import software.amazon.awssdk.enhanced.dynamodb.model.DeleteItemEnhancedRequest;
import software.amazon.awssdk.enhanced.dynamodb.model.DeleteItemEnhancedResponse;
import software.amazon.awssdk.enhanced.dynamodb.model.DescribeTableEnhancedResponse;
import software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex;
import software.amazon.awssdk.enhanced.dynamodb.model.EnhancedLocalSecondaryIndex;
import software.amazon.awssdk.enhanced.dynamodb.model.GetItemEnhancedRequest;
import software.amazon.awssdk.enhanced.dynamodb.model.PageIterable;
import software.amazon.awssdk.enhanced.dynamodb.model.PutItemEnhancedRequest;
Expand All @@ -51,6 +60,7 @@
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest;
import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse;
import software.amazon.awssdk.services.dynamodb.model.ProjectionType;

@SdkInternalApi
public class DefaultDynamoDbTable<T> implements DynamoDbTable<T> {
Expand Down Expand Up @@ -115,7 +125,52 @@ public void createTable(Consumer<CreateTableEnhancedRequest.Builder> requestCons

@Override
public void createTable() {
createTable(CreateTableEnhancedRequest.builder().build());
Map<IndexType, List<IndexMetadata>> indexGroups = splitSecondaryIndicesToLocalAndGlobalOnes();
createTable(CreateTableEnhancedRequest.builder()
.localSecondaryIndices(extractLocalSecondaryIndices(indexGroups))
.globalSecondaryIndices(extractGlobalSecondaryIndices(indexGroups))
.build());
}

private Map<IndexType, List<IndexMetadata>> splitSecondaryIndicesToLocalAndGlobalOnes() {
String primaryPartitionKeyName = tableSchema.tableMetadata().primaryPartitionKey();
Collection<IndexMetadata> indices = tableSchema.tableMetadata().indices();
return indices.stream()
.filter(index -> !TableMetadata.primaryIndexName().equals(index.name()))
.collect(Collectors.groupingBy(metadata -> {
String partitionKeyName = metadata.partitionKey().map(KeyAttributeMetadata::name).orElse(null);
if (partitionKeyName == null || primaryPartitionKeyName.equals(partitionKeyName)) {
return IndexType.LSI;
}
return IndexType.GSI;
}));
}

private List<EnhancedLocalSecondaryIndex> extractLocalSecondaryIndices(Map<IndexType, List<IndexMetadata>> indicesGroups) {
return indicesGroups.getOrDefault(IndexType.LSI, emptyList()).stream()
.map(this::mapIndexMetadataToEnhancedLocalSecondaryIndex)
.collect(Collectors.toList());
}

private EnhancedLocalSecondaryIndex mapIndexMetadataToEnhancedLocalSecondaryIndex(IndexMetadata indexMetadata) {
return EnhancedLocalSecondaryIndex.builder()
.indexName(indexMetadata.name())
.projection(pb -> pb.projectionType(ProjectionType.ALL))
.build();
}

private List<EnhancedGlobalSecondaryIndex> extractGlobalSecondaryIndices(Map<IndexType, List<IndexMetadata>> indicesGroups) {
return indicesGroups.getOrDefault(IndexType.GSI, emptyList()).stream()
.map(this::mapIndexMetadataToEnhancedGlobalSecondaryIndex)
.collect(Collectors.toList());
}

private EnhancedGlobalSecondaryIndex mapIndexMetadataToEnhancedGlobalSecondaryIndex(IndexMetadata indexMetadata) {
return EnhancedGlobalSecondaryIndex.builder()
.indexName(indexMetadata.name())
.projection(pb -> pb.projectionType(ProjectionType.ALL))
.provisionedThroughput(ptb -> ptb.readCapacityUnits(20L).writeCapacityUnits(20L))
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.enhanced.dynamodb.internal.client;

import software.amazon.awssdk.annotations.SdkInternalApi;

/**
* Enum collecting types of secondary indexes
*/
@SdkInternalApi
public enum IndexType {
LSI,
GSI
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,30 @@
package software.amazon.awssdk.enhanced.dynamodb.internal.client;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
import static org.mockito.Mockito.verify;
import static software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues.stringValue;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension;
import software.amazon.awssdk.enhanced.dynamodb.Key;
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem;
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithIndices;
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithSort;
import software.amazon.awssdk.enhanced.dynamodb.model.CreateTableEnhancedRequest;
import software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex;
import software.amazon.awssdk.enhanced.dynamodb.model.EnhancedLocalSecondaryIndex;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -113,4 +123,50 @@ public void keyFrom_primaryIndex_partitionAndNullSort() {
assertThat(key.partitionKeyValue(), is(stringValue(item.getId())));
assertThat(key.sortKeyValue(), is(Optional.empty()));
}

@Test
public void createTable_doesNotTreatPrimaryIndexAsAnyOfSecondaryIndexes() {
DefaultDynamoDbTable<FakeItem> dynamoDbMappedIndex =
Mockito.spy(new DefaultDynamoDbTable<>(mockDynamoDbClient,
mockDynamoDbEnhancedClientExtension,
FakeItem.getTableSchema(),
"test_table"));

dynamoDbMappedIndex.createTable();

CreateTableEnhancedRequest request = captureCreateTableRequest(dynamoDbMappedIndex);

assertThat(request.localSecondaryIndices().size(), is(0));
assertThat(request.globalSecondaryIndices().size(), is(0));
}

@Test
public void createTable_groupsSecondaryIndexesExistingInTableSchema() {
DefaultDynamoDbTable<FakeItemWithIndices> dynamoDbMappedIndex =
Mockito.spy(new DefaultDynamoDbTable<>(mockDynamoDbClient,
mockDynamoDbEnhancedClientExtension,
FakeItemWithIndices.getTableSchema(),
"test_table"));

dynamoDbMappedIndex.createTable();

CreateTableEnhancedRequest request = captureCreateTableRequest(dynamoDbMappedIndex);

assertThat(request.localSecondaryIndices().size(), is(1));
Iterator<EnhancedLocalSecondaryIndex> lsiIterator = request.localSecondaryIndices().iterator();
assertThat(lsiIterator.next().indexName(), is("lsi_1"));

assertThat(request.globalSecondaryIndices().size(), is(2));
List<String> globalIndicesNames = request.globalSecondaryIndices().stream()
.map(EnhancedGlobalSecondaryIndex::indexName)
.collect(Collectors.toList());
assertThat(globalIndicesNames, containsInAnyOrder("gsi_1", "gsi_2"));
}

private static <T> CreateTableEnhancedRequest captureCreateTableRequest(DefaultDynamoDbTable<T> index) {
ArgumentCaptor<CreateTableEnhancedRequest> createTableOperationCaptor =
ArgumentCaptor.forClass(CreateTableEnhancedRequest.class);
verify(index).createTable(createTableOperationCaptor.capture());
return createTableOperationCaptor.getValue();
}
}