From 945377df978e7982ca1fac670311b66c29188583 Mon Sep 17 00:00:00 2001 From: agrgr Date: Sun, 26 May 2024 12:44:06 +0300 Subject: [PATCH 1/6] add flag to control writing Maps as TreeMaps --- .../data/aerospike/config/AerospikeDataSettings.java | 1 + .../aerospike/convert/MappingAerospikeWriteConverter.java | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) 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..e2571fb19 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,5 @@ 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; + boolean writeKeyOrderedMaps = 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..c7c565ebd 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,12 @@ private void convertToAerospikeWriteData(Object source, AerospikeWriteData data) private Map convertProperties(TypeInformation type, AerospikePersistentEntity entity, ConvertingPropertyAccessor accessor, boolean isCustomType) { - Map target = new TreeMap<>(); + Map target; + if (!settings.isWriteKeyOrderedMaps()) { + target = new HashMap<>(); + } else { + target = new TreeMap<>(); + } typeMapper.writeType(type, target); entity.doWithProperties((PropertyHandler) property -> { From 681a02df9bf4d332959dc24e1e0d25016a2959b1 Mon Sep 17 00:00:00 2001 From: agrgr Date: Sun, 26 May 2024 16:02:24 +0300 Subject: [PATCH 2/6] add flag to control writing Maps as TreeMaps --- .../aerospike/config/AerospikeDataSettings.java | 3 ++- .../convert/MappingAerospikeWriteConverter.java | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) 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 e2571fb19..0702f8960 100644 --- a/src/main/java/org/springframework/data/aerospike/config/AerospikeDataSettings.java +++ b/src/main/java/org/springframework/data/aerospike/config/AerospikeDataSettings.java @@ -37,5 +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; - boolean writeKeyOrderedMaps = true; + // Define how Maps are written: true - as TreeMaps (default), false - as HashMaps + boolean writeTreeMaps = 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 c7c565ebd..3e995ea26 100644 --- a/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java +++ b/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java @@ -42,6 +42,7 @@ import java.util.Map; import java.util.Optional; import java.util.TreeMap; +import java.util.function.Supplier; import java.util.stream.Collectors; import static com.aerospike.client.ResultCode.OP_NOT_APPLICABLE; @@ -159,7 +160,7 @@ private void convertToAerospikeWriteData(Object source, AerospikeWriteData data) private Map convertProperties(TypeInformation type, AerospikePersistentEntity entity, ConvertingPropertyAccessor accessor, boolean isCustomType) { Map target; - if (!settings.isWriteKeyOrderedMaps()) { + if (!settings.isWriteTreeMaps()) { target = new HashMap<>(); } else { target = new TreeMap<>(); @@ -240,7 +241,14 @@ 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) -> { + Supplier> mapSupplier; + if (!settings.isWriteTreeMaps()) { + mapSupplier = HashMap::new; + } else { + mapSupplier = TreeMap::new; + } + Map map = mapSupplier.get(); + for (Map.Entry e : source.entrySet()) { Object key = e.getKey(); Object value = e.getValue(); if (key == null) { @@ -266,8 +274,9 @@ 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.put(simpleKey, convertedValue); + } + return map; } private Map convertCustomType(Object source, TypeInformation type) { From 0c57e1eaa08bbeb61c3800e506ec3865e672681e Mon Sep 17 00:00:00 2001 From: agrgr Date: Sun, 26 May 2024 18:48:52 +0300 Subject: [PATCH 3/6] code format --- .../convert/MappingAerospikeWriteConverter.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 3e995ea26..a74a4341d 100644 --- a/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java +++ b/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java @@ -160,10 +160,10 @@ private void convertToAerospikeWriteData(Object source, AerospikeWriteData data) private Map convertProperties(TypeInformation type, AerospikePersistentEntity entity, ConvertingPropertyAccessor accessor, boolean isCustomType) { Map target; - if (!settings.isWriteTreeMaps()) { - target = new HashMap<>(); - } else { + if (settings.isWriteTreeMaps()) { target = new TreeMap<>(); + } else { + target = new HashMap<>(); } typeMapper.writeType(type, target); entity.doWithProperties((PropertyHandler) property -> { @@ -242,10 +242,10 @@ protected Map convertMap(final Map source, final Assert.notNull(type, "Given type must not be null!"); Supplier> mapSupplier; - if (!settings.isWriteTreeMaps()) { - mapSupplier = HashMap::new; - } else { + if (settings.isWriteTreeMaps()) { mapSupplier = TreeMap::new; + } else { + mapSupplier = HashMap::new; } Map map = mapSupplier.get(); for (Map.Entry e : source.entrySet()) { From 91875bc619d23bfaea53e5d9da229ee4e13456a0 Mon Sep 17 00:00:00 2001 From: agrgr Date: Mon, 27 May 2024 17:31:58 +0300 Subject: [PATCH 4/6] code format, allow null key when writeTreeMaps == false --- .../convert/MappingAerospikeWriteConverter.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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 a74a4341d..f56b47b5b 100644 --- a/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java +++ b/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java @@ -247,11 +247,10 @@ protected Map convertMap(final Map source, final } else { mapSupplier = HashMap::new; } - Map map = mapSupplier.get(); - for (Map.Entry e : source.entrySet()) { + return source.entrySet().stream().collect(mapSupplier, (m, e) -> { Object key = e.getKey(); Object value = e.getValue(); - if (key == null) { + if (key == null && !settings.isWriteTreeMaps()) { throw new UnsupportedOperationException("Key of a map cannot be null"); } @@ -274,9 +273,8 @@ protected Map convertMap(final Map source, final Object convertedValue = getValueToWrite(value, type.getMapValueType()); if (simpleKey instanceof byte[]) simpleKey = ByteBuffer.wrap((byte[]) simpleKey); - map.put(simpleKey, convertedValue); - } - return map; + m.put(simpleKey, convertedValue); + }, Map::putAll); } private Map convertCustomType(Object source, TypeInformation type) { From d859c6347da6b55b4f34926248e3bb8e44ea29e8 Mon Sep 17 00:00:00 2001 From: agrgr Date: Mon, 27 May 2024 17:33:00 +0300 Subject: [PATCH 5/6] allow null key when writeTreeMaps == false --- .../data/aerospike/convert/MappingAerospikeWriteConverter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 f56b47b5b..f38eb0c1f 100644 --- a/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java +++ b/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java @@ -250,7 +250,7 @@ protected Map convertMap(final Map source, final return source.entrySet().stream().collect(mapSupplier, (m, e) -> { Object key = e.getKey(); Object value = e.getValue(); - if (key == null && !settings.isWriteTreeMaps()) { + if (key == null && settings.isWriteTreeMaps()) { throw new UnsupportedOperationException("Key of a map cannot be null"); } From 605e0a16247cb320150d9320166302612cd8d164 Mon Sep 17 00:00:00 2001 From: agrgr Date: Tue, 28 May 2024 09:08:25 +0300 Subject: [PATCH 6/6] code format --- .../config/AerospikeDataSettings.java | 2 +- .../MappingAerospikeWriteConverter.java | 18 +++--------------- 2 files changed, 4 insertions(+), 16 deletions(-) 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 0702f8960..b13d70b39 100644 --- a/src/main/java/org/springframework/data/aerospike/config/AerospikeDataSettings.java +++ b/src/main/java/org/springframework/data/aerospike/config/AerospikeDataSettings.java @@ -38,5 +38,5 @@ public class AerospikeDataSettings { // true - preserve original type if supported boolean keepOriginalKeyTypes = false; // Define how Maps are written: true - as TreeMaps (default), false - as HashMaps - boolean writeTreeMaps = true; + 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 f38eb0c1f..838ee7a6c 100644 --- a/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java +++ b/src/main/java/org/springframework/data/aerospike/convert/MappingAerospikeWriteConverter.java @@ -42,7 +42,6 @@ import java.util.Map; import java.util.Optional; import java.util.TreeMap; -import java.util.function.Supplier; import java.util.stream.Collectors; import static com.aerospike.client.ResultCode.OP_NOT_APPLICABLE; @@ -159,12 +158,7 @@ private void convertToAerospikeWriteData(Object source, AerospikeWriteData data) private Map convertProperties(TypeInformation type, AerospikePersistentEntity entity, ConvertingPropertyAccessor accessor, boolean isCustomType) { - Map target; - if (settings.isWriteTreeMaps()) { - target = new TreeMap<>(); - } else { - target = new HashMap<>(); - } + Map target = settings.isWriteSortedMaps() ? new TreeMap<>() : new HashMap<>(); typeMapper.writeType(type, target); entity.doWithProperties((PropertyHandler) property -> { @@ -241,16 +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!"); - Supplier> mapSupplier; - if (settings.isWriteTreeMaps()) { - mapSupplier = TreeMap::new; - } else { - mapSupplier = HashMap::new; - } - return source.entrySet().stream().collect(mapSupplier, (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 && settings.isWriteTreeMaps()) { + if (key == null && settings.isWriteSortedMaps()) { throw new UnsupportedOperationException("Key of a map cannot be null"); }