Skip to content

Add support for ByteBufferAttributeConverter #1911

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
merged 1 commit into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -35,6 +35,7 @@
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.BooleanAttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.ByteArrayAttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.ByteAttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.ByteBufferAttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.CharSequenceAttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.CharacterArrayAttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.CharacterAttributeConverter;
Expand Down Expand Up @@ -204,6 +205,7 @@ private static Builder getDefaultBuilder() {
.addConverter(BigIntegerAttributeConverter.create())
.addConverter(BooleanAttributeConverter.create())
.addConverter(ByteArrayAttributeConverter.create())
.addConverter(ByteBufferAttributeConverter.create())
.addConverter(ByteAttributeConverter.create())
.addConverter(CharacterArrayAttributeConverter.create())
.addConverter(CharacterAttributeConverter.create())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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.converter.attribute;

import java.nio.ByteBuffer;
import software.amazon.awssdk.annotations.Immutable;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.annotations.ThreadSafe;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.AttributeValueType;
import software.amazon.awssdk.enhanced.dynamodb.EnhancedType;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.TypeConvertingVisitor;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;


/**
* A converter between {@link ByteBuffer} and {@link AttributeValue}.
*
* <p>
* This stores values in DynamoDB as a binary blob.
*
* <p>
* This supports reading every byte value supported by DynamoDB, making it fully compatible with custom converters as
* well as internal converters (e.g. {@link SdkBytesAttributeConverter}).
*
* <p>
* This can be created via {@link #create()}.
*/
@SdkInternalApi
@ThreadSafe
@Immutable
public final class ByteBufferAttributeConverter implements AttributeConverter<ByteBuffer> {
private static final Visitor VISITOR = new Visitor();

private ByteBufferAttributeConverter() {
}

public static ByteBufferAttributeConverter create() {
return new ByteBufferAttributeConverter();
}

@Override
public EnhancedType<ByteBuffer> type() {
return EnhancedType.of(ByteBuffer.class);
}

@Override
public AttributeValueType attributeValueType() {
return AttributeValueType.B;
}

@Override
public AttributeValue transformFrom(ByteBuffer input) {
return AttributeValue.builder().b(SdkBytes.fromByteBuffer(input)).build();
}

@Override
public ByteBuffer transformTo(AttributeValue input) {
if (input.b() != null) {
return EnhancedAttributeValue.fromBytes(input.b()).convert(VISITOR);
}

return EnhancedAttributeValue.fromAttributeValue(input).convert(VISITOR);
}

private static final class Visitor extends TypeConvertingVisitor<ByteBuffer> {
private Visitor() {
super(ByteBuffer.class, ByteBufferAttributeConverter.class);
}

@Override
public ByteBuffer convertBytes(SdkBytes value) {
return value.asByteBuffer();
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static software.amazon.awssdk.enhanced.dynamodb.converters.attribute.ConverterTestUtils.transformFrom;
import static software.amazon.awssdk.enhanced.dynamodb.converters.attribute.ConverterTestUtils.transformTo;

import java.nio.ByteBuffer;
import java.util.Set;
import org.junit.Test;
import software.amazon.awssdk.core.SdkBytes;
Expand All @@ -27,6 +28,7 @@
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.EnhancedAttributeValue;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.SdkBytesAttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.SetAttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.ByteBufferAttributeConverter;

public class BinaryAttributeConvertersTest {
@Test
Expand Down Expand Up @@ -56,4 +58,15 @@ public void sdkBytesSetAttributeConverter_ReturnsBSType() {
SetAttributeConverter<Set<SdkBytes>> bytesSet = SetAttributeConverter.setConverter(SdkBytesAttributeConverter.create());
assertThat(bytesSet.attributeValueType()).isEqualTo(AttributeValueType.BS);
}

@Test
public void byteBufferAttributeConverterBehaves() {
ByteBufferAttributeConverter converter = ByteBufferAttributeConverter.create();
assertThat(converter.attributeValueType()).isEqualTo(AttributeValueType.B);
String foo = "foo";
ByteBuffer byteBuffer = ByteBuffer.wrap(foo.getBytes());
SdkBytes sdkBytes = SdkBytes.fromUtf8String(foo);
assertThat(transformFrom(converter, byteBuffer).b()).isEqualTo(sdkBytes);
assertThat(transformTo(converter, EnhancedAttributeValue.fromBytes(sdkBytes).toAttributeValue())).isEqualTo(byteBuffer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.functionaltests.models;

import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema;

import java.nio.ByteBuffer;
import java.util.Objects;

import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primaryPartitionKey;

public class FakeItemWithByteBufferKey {
private static final StaticTableSchema<FakeItemWithByteBufferKey> FAKE_ITEM_WITH_BINARY_KEY_SCHEMA =
StaticTableSchema.builder(FakeItemWithByteBufferKey.class)
.newItemSupplier(FakeItemWithByteBufferKey::new)
.addAttribute(SdkBytes.class, a -> a.name("id")
.getter(FakeItemWithByteBufferKey::getIdAsSdkBytes)
.setter(FakeItemWithByteBufferKey::setIdAsSdkBytes)
.tags(primaryPartitionKey()))
.build();

private ByteBuffer id;

public static StaticTableSchema<FakeItemWithByteBufferKey> getTableSchema() {
return FAKE_ITEM_WITH_BINARY_KEY_SCHEMA;
}

public ByteBuffer getId() {
return id;
}

public void setId(ByteBuffer id) {
this.id = id;
}


public SdkBytes getIdAsSdkBytes() {
return SdkBytes.fromByteBuffer(id);
}

public void setIdAsSdkBytes(SdkBytes id) {
this.id = id.asByteBuffer();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FakeItemWithByteBufferKey that = (FakeItemWithByteBufferKey) o;
return Objects.equals(id, that.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import software.amazon.awssdk.enhanced.dynamodb.TableMetadata;
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem;
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithBinaryKey;
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithByteBufferKey;
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithIndices;
import software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithNumericSort;
import software.amazon.awssdk.enhanced.dynamodb.model.CreateTableEnhancedRequest;
Expand Down Expand Up @@ -387,6 +388,31 @@ public void generateRequest_withBinaryKey() {
.build()));
}

@Test
public void generateRequest_withByteBufferKey() {
CreateTableOperation<FakeItemWithByteBufferKey> operation = CreateTableOperation.create(CreateTableEnhancedRequest.builder()
.build());

CreateTableRequest request = operation.generateRequest(FakeItemWithByteBufferKey.getTableSchema(),
PRIMARY_CONTEXT,
null);

assertThat(request.tableName(), is(TABLE_NAME));
assertThat(request.keySchema(), containsInAnyOrder(KeySchemaElement.builder()
.attributeName("id")
.keyType(HASH)
.build()));

assertThat(request.globalSecondaryIndexes(), is(empty()));
assertThat(request.localSecondaryIndexes(), is(empty()));

assertThat(request.attributeDefinitions(), containsInAnyOrder(
AttributeDefinition.builder()
.attributeName("id")
.attributeType(ScalarAttributeType.B)
.build()));
}

@Test(expected = IllegalArgumentException.class)
public void generateRequest_doesNotWorkForIndex() {
CreateTableOperation<FakeItemWithIndices> operation = CreateTableOperation.create(CreateTableEnhancedRequest.builder()
Expand Down