Skip to content

Commit

Permalink
FMWK-443 Add configuration flag writeSortedMaps (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr authored May 28, 2024
1 parent 9b09dde commit bce7b11
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -157,7 +158,7 @@ private void convertToAerospikeWriteData(Object source, AerospikeWriteData data)

private Map<String, Object> convertProperties(TypeInformation<?> type, AerospikePersistentEntity<?> entity,
ConvertingPropertyAccessor<?> accessor, boolean isCustomType) {
Map<String, Object> target = new TreeMap<>();
Map<String, Object> target = settings.isWriteSortedMaps() ? new TreeMap<>() : new HashMap<>();
typeMapper.writeType(type, target);
entity.doWithProperties((PropertyHandler<AerospikePersistentProperty>) property -> {

Expand Down Expand Up @@ -234,10 +235,10 @@ protected Map<Object, Object> convertMap(final Map<Object, Object> 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");
}

Expand All @@ -261,7 +262,7 @@ protected Map<Object, Object> convertMap(final Map<Object, Object> 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<String, Object> convertCustomType(Object source, TypeInformation<?> type) {
Expand Down

0 comments on commit bce7b11

Please sign in to comment.