Skip to content

Commit

Permalink
Test both converter cases: convert keys (record and maps) to String o…
Browse files Browse the repository at this point in the history
…r store as it is if the type is natively supported
  • Loading branch information
roimenashe committed Nov 22, 2023
1 parent 2807034 commit a305dcc
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.data.aerospike.convert;

import org.springframework.core.convert.ConversionService;
import org.springframework.data.aerospike.config.AerospikeDataSettings;
import org.springframework.data.convert.EntityConverter;

/**
Expand All @@ -31,4 +32,11 @@ public interface AerospikeConverter extends AerospikeReader<Object>, AerospikeWr
* @return the underlying {@link ConversionService} used by the converter
*/
ConversionService getConversionService();

/**
* Access Aerospike-specific data settings.
*
* @return the underlying {@link AerospikeDataSettings} used by the converter
*/
AerospikeDataSettings getAerospikeDataSettings();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package org.springframework.data.aerospike.convert;

import lombok.Getter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.aerospike.config.AerospikeDataSettings;
Expand All @@ -40,7 +40,10 @@
public class MappingAerospikeConverter implements InitializingBean, AerospikeConverter {

private final CustomConversions conversions;
@Getter
private final GenericConversionService conversionService;
@Getter
private final AerospikeDataSettings aerospikeDataSettings;
private final MappingAerospikeReadConverter readConverter;
private final MappingAerospikeWriteConverter writeConverter;

Expand All @@ -52,6 +55,7 @@ public MappingAerospikeConverter(AerospikeMappingContext mappingContext, CustomC
AerospikeDataSettings aerospikeDataSettings) {
this.conversions = conversions;
this.conversionService = new DefaultConversionService();
this.aerospikeDataSettings = aerospikeDataSettings;

EntityInstantiators entityInstantiators = new EntityInstantiators();
TypeMapper<Map<String, Object>> typeMapper = new DefaultTypeMapper<>(aerospikeTypeAliasAccessor,
Expand All @@ -69,11 +73,6 @@ public void afterPropertiesSet() {
conversions.registerConvertersIn(conversionService);
}

@Override
public ConversionService getConversionService() {
return conversionService;
}

@Override
public <R> R read(Class<R> type, final AerospikeReadData data) {
return readConverter.read(type, data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.aerospike.client.AerospikeException;
import com.aerospike.client.Key;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.aerospike.config.AerospikeDataSettings;
import org.springframework.data.aerospike.mapping.AerospikeMappingContext;
Expand Down Expand Up @@ -44,6 +45,7 @@
import static com.aerospike.client.ResultCode.OP_NOT_APPLICABLE;
import static org.springframework.data.aerospike.utility.TimeUtils.unixTimeToOffsetInSeconds;

@Slf4j
public class MappingAerospikeWriteConverter implements EntityWriter<Object, AerospikeWriteData> {

private final TypeMapper<Map<String, Object>> typeMapper;
Expand Down Expand Up @@ -128,6 +130,7 @@ public Optional<Key> getNewKey(AerospikeWriteData data,
// Store record key as it is (if Aerospike supports it natively and configured)
if (aerospikeDataSettings.isKeepOriginalKeyTypes() &&
isValidAerospikeRecordKeyType(idProperty.getType())) {
log.debug("Attempt to construct record key with original key type");
Object nativeTypeId = accessor.getProperty(idProperty, idProperty.getType());
Assert.notNull(nativeTypeId, "Id must not be null!");
Key aerospikeRecordKey = constructAerospikeRecordKey(data.getNamespace(), setName, nativeTypeId);
Expand All @@ -138,6 +141,7 @@ public Optional<Key> getNewKey(AerospikeWriteData data,
// Store record key as a String (Used for unsupported Aerospike key types and older versions)
String stringId = accessor.getProperty(idProperty, String.class);
Assert.notNull(stringId, "Id must not be null!");
log.debug("Attempt to construct record key as String");
return Optional.of(new Key(data.getNamespace(), setName, stringId));
} else {
// id is mandatory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ public abstract class BaseMappingAerospikeConverterTest {

protected static final String NAMESPACE = "namespace";
public final AerospikeDataSettings aerospikeDataSettings = AerospikeDataSettings.builder().build();
public final AerospikeDataSettings aerospikeDataSettingsReversedKeyTypesOption = AerospikeDataSettings.builder()
.keepOriginalKeyTypes(!aerospikeDataSettings.isKeepOriginalKeyTypes()).build();

protected final MappingAerospikeConverter converter = getMappingAerospikeConverter(
aerospikeDataSettings,
new SampleClasses.ComplexIdToStringConverter(),
new SampleClasses.StringToComplexIdConverter());

protected final MappingAerospikeConverter converterReversedKeyTypes = getMappingAerospikeConverter(
aerospikeDataSettingsReversedKeyTypesOption,
new SampleClasses.ComplexIdToStringConverter(),
new SampleClasses.StringToComplexIdConverter());

Expand All @@ -38,12 +46,21 @@ protected static Record aeroRecord(Map<String, Object> bins) {
return new Record(bins, 0, 0);
}

protected MappingAerospikeConverter getMappingAerospikeConverter(Converter<?, ?>... customConverters) {
return getMappingAerospikeConverter(new AerospikeTypeAliasAccessor(), customConverters);
protected MappingAerospikeConverter getAerospikeMappingConverterByOption(int converterOption) {
if (converterOption == 0) {
return converter;
}
return converterReversedKeyTypes;
}

protected MappingAerospikeConverter getMappingAerospikeConverter(AerospikeTypeAliasAccessor typeAliasAccessor,
protected MappingAerospikeConverter getMappingAerospikeConverter(AerospikeDataSettings aerospikeDataSettings,
Converter<?, ?>... customConverters) {
return getMappingAerospikeConverter(aerospikeDataSettings, new AerospikeTypeAliasAccessor(), customConverters);
}

protected MappingAerospikeConverter getMappingAerospikeConverter(AerospikeDataSettings aerospikeDataSettings,
AerospikeTypeAliasAccessor typeAliasAccessor,
Converter<?, ?>... customConverters) {
AerospikeMappingContext mappingContext = new AerospikeMappingContext();
mappingContext.setApplicationContext(getApplicationContext());
CustomConversions customConversions = new AerospikeCustomConversions(asList(customConverters));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void usesDocumentsStoredTypeIfSubtypeOfRequest() {
@Test
public void shouldWriteAndReadUsingCustomConverter() {
MappingAerospikeConverter converter =
getMappingAerospikeConverter(new UserToAerospikeWriteDataConverter(),
getMappingAerospikeConverter(aerospikeDataSettings, new UserToAerospikeWriteDataConverter(),
new AerospikeReadDataToUserConverter());

AerospikeWriteData forWrite = AerospikeWriteData.forWrite(NAMESPACE);
Expand All @@ -137,7 +137,7 @@ class TestName {
}

MappingAerospikeConverter converter =
getMappingAerospikeConverter(new UserToAerospikeWriteDataConverter(),
getMappingAerospikeConverter(aerospikeDataSettings, new UserToAerospikeWriteDataConverter(),
new AerospikeReadDataToUserConverter());

AerospikeWriteData forWrite = AerospikeWriteData.forWrite(NAMESPACE);
Expand All @@ -150,7 +150,7 @@ class TestName {
@Test
public void shouldWriteAndReadUsingCustomConverterOnNestedMapKeyObject() {
MappingAerospikeConverter converter =
getMappingAerospikeConverter(new SampleClasses.SomeIdToStringConverter(),
getMappingAerospikeConverter(aerospikeDataSettings, new SampleClasses.SomeIdToStringConverter(),
new SampleClasses.StringToSomeIdConverter());

AerospikeWriteData forWrite = AerospikeWriteData.forWrite(NAMESPACE);
Expand Down Expand Up @@ -194,7 +194,7 @@ public void shouldWriteAndReadUsingCustomConverterOnNestedMapKeyObject() {
@Test
public void shouldWriteAndReadIfTypeKeyIsNull() {
MappingAerospikeConverter converter =
getMappingAerospikeConverter(new AerospikeTypeAliasAccessor(null));
getMappingAerospikeConverter(aerospikeDataSettings, new AerospikeTypeAliasAccessor(null));

AerospikeWriteData forWrite = AerospikeWriteData.forWrite(NAMESPACE);
User user = new User(678L, null, null);
Expand Down Expand Up @@ -369,7 +369,7 @@ public void shouldReadObjectWithByteArrayFieldWithOneValueInData() {
@Test
public void getConversionService() {
MappingAerospikeConverter mappingAerospikeConverter =
getMappingAerospikeConverter(new AerospikeTypeAliasAccessor());
getMappingAerospikeConverter(aerospikeDataSettings, new AerospikeTypeAliasAccessor());
assertThat(mappingAerospikeConverter.getConversionService()).isNotNull()
.isInstanceOf(DefaultConversionService.class);
}
Expand Down
Loading

0 comments on commit a305dcc

Please sign in to comment.