From 890a5c8bd3b60ac35825d5d38379054dd11fe5fb Mon Sep 17 00:00:00 2001 From: Ryan French Date: Wed, 22 Mar 2023 16:30:59 +0000 Subject: [PATCH] DRY up creation of the CreateTable Request builder --- .../dynamodb/DynamoDBLeaseRefresher.java | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/amazon-kinesis-client/src/main/java/software/amazon/kinesis/leases/dynamodb/DynamoDBLeaseRefresher.java b/amazon-kinesis-client/src/main/java/software/amazon/kinesis/leases/dynamodb/DynamoDBLeaseRefresher.java index aeaa4fd8..c133b6fe 100644 --- a/amazon-kinesis-client/src/main/java/software/amazon/kinesis/leases/dynamodb/DynamoDBLeaseRefresher.java +++ b/amazon-kinesis-client/src/main/java/software/amazon/kinesis/leases/dynamodb/DynamoDBLeaseRefresher.java @@ -186,23 +186,13 @@ public class DynamoDBLeaseRefresher implements LeaseRefresher { @Override public boolean createLeaseTableIfNotExists(@NonNull final Long readCapacity, @NonNull final Long writeCapacity) throws ProvisionedThroughputException, DependencyException { - ProvisionedThroughput throughput = ProvisionedThroughput.builder().readCapacityUnits(readCapacity) + final CreateTableRequest.Builder builder = createTableRequestBuilder(); + if(BillingMode.PROVISIONED.equals(billingMode)) { + ProvisionedThroughput throughput = ProvisionedThroughput.builder().readCapacityUnits(readCapacity) .writeCapacityUnits(writeCapacity).build(); - final CreateTableRequest request; - if(BillingMode.PAY_PER_REQUEST.equals(billingMode)){ - request = CreateTableRequest.builder().tableName(table).keySchema(serializer.getKeySchema()) - .attributeDefinitions(serializer.getAttributeDefinitions()) - .billingMode(billingMode) - .tags(tags) - .build(); - } else { - request = CreateTableRequest.builder().tableName(table).keySchema(serializer.getKeySchema()) - .attributeDefinitions(serializer.getAttributeDefinitions()).provisionedThroughput(throughput) - .tags(tags) - .build(); + builder.provisionedThroughput(throughput); } - - return createTableIfNotExists(request); + return createTableIfNotExists(builder.build()); } /** @@ -211,11 +201,7 @@ public class DynamoDBLeaseRefresher implements LeaseRefresher { @Override public boolean createLeaseTableIfNotExists() throws ProvisionedThroughputException, DependencyException { - final CreateTableRequest request = CreateTableRequest.builder().tableName(table).keySchema(serializer.getKeySchema()) - .attributeDefinitions(serializer.getAttributeDefinitions()) - .billingMode(billingMode) - .tags(tags) - .build(); + final CreateTableRequest request = createTableRequestBuilder().build(); return createTableIfNotExists(request); } @@ -816,6 +802,16 @@ public class DynamoDBLeaseRefresher implements LeaseRefresher { } } + private CreateTableRequest.Builder createTableRequestBuilder() { + final CreateTableRequest.Builder builder = CreateTableRequest.builder().tableName(table).keySchema(serializer.getKeySchema()) + .attributeDefinitions(serializer.getAttributeDefinitions()) + .tags(tags); + if (BillingMode.PAY_PER_REQUEST.equals(billingMode)) { + builder.billingMode(billingMode); + } + return builder; +} + private AWSExceptionManager createExceptionManager() { final AWSExceptionManager exceptionManager = new AWSExceptionManager(); exceptionManager.add(DynamoDbException.class, t -> t);