diff --git a/src/main/java/org/springframework/data/aerospike/config/AerospikeDataSettings.java b/src/main/java/org/springframework/data/aerospike/config/AerospikeDataSettings.java index 7246a964e..b13d70b39 100644 --- a/src/main/java/org/springframework/data/aerospike/config/AerospikeDataSettings.java +++ b/src/main/java/org/springframework/data/aerospike/config/AerospikeDataSettings.java @@ -37,4 +37,6 @@ public class AerospikeDataSettings { // Define how @Id fields (primary keys) and Map keys are stored: false - always as String, // true - preserve original type if supported boolean keepOriginalKeyTypes = false; + // Define how Maps are written: true - as TreeMaps (default), false - as HashMaps + boolean writeSortedMaps = true; } diff --git a/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java b/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java index f9c6bed61..838ee7a6c 100644 --- a/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java +++ b/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java @@ -37,6 +37,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -157,7 +158,7 @@ private void convertToAerospikeWriteData(Object source, AerospikeWriteData data) private Map convertProperties(TypeInformation type, AerospikePersistentEntity entity, ConvertingPropertyAccessor accessor, boolean isCustomType) { - Map target = new TreeMap<>(); + Map target = settings.isWriteSortedMaps() ? new TreeMap<>() : new HashMap<>(); typeMapper.writeType(type, target); entity.doWithProperties((PropertyHandler) property -> { @@ -234,10 +235,10 @@ protected Map convertMap(final Map source, final Assert.notNull(source, "Given map must not be null!"); Assert.notNull(type, "Given type must not be null!"); - return source.entrySet().stream().collect(TreeMap::new, (m, e) -> { + return source.entrySet().stream().collect(settings.isWriteSortedMaps() ? TreeMap::new : HashMap::new, (m, e) -> { Object key = e.getKey(); Object value = e.getValue(); - if (key == null) { + if (key == null && settings.isWriteSortedMaps()) { throw new UnsupportedOperationException("Key of a map cannot be null"); } @@ -261,7 +262,7 @@ protected Map convertMap(final Map source, final Object convertedValue = getValueToWrite(value, type.getMapValueType()); if (simpleKey instanceof byte[]) simpleKey = ByteBuffer.wrap((byte[]) simpleKey); m.put(simpleKey, convertedValue); - }, TreeMap::putAll); + }, Map::putAll); } private Map convertCustomType(Object source, TypeInformation type) {