Skip to content
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

FMWK-443 Add configuration flag writeSortedMaps #745

Merged
merged 8 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -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 writeTreeMaps = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming to useSortedMap.

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
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;
import java.util.TreeMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static com.aerospike.client.ResultCode.OP_NOT_APPLICABLE;
Expand Down Expand Up @@ -157,7 +159,12 @@ 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;
if (!settings.isWriteTreeMaps()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simpler to check isWriteTreeMaps and set target as TreeMap, otherwise use HashMap (follow the condition, no need for !

target = new HashMap<>();
} else {
target = new TreeMap<>();
}
typeMapper.writeType(type, target);
entity.doWithProperties((PropertyHandler<AerospikePersistentProperty>) property -> {

Expand Down Expand Up @@ -234,7 +241,14 @@ 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) -> {
Supplier<Map<Object, Object>> mapSupplier;
if (!settings.isWriteTreeMaps()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here:

simpler to check isWriteTreeMaps and set target as TreeMap, otherwise use HashMap (follow the condition, no need for !

mapSupplier = HashMap::new;
} else {
mapSupplier = TreeMap::new;
}
Map<Object, Object> map = mapSupplier.get();
for (Map.Entry<Object, Object> e : source.entrySet()) {
Object key = e.getKey();
Object value = e.getValue();
if (key == null) {
Expand All @@ -260,8 +274,9 @@ 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.put(simpleKey, convertedValue);
}
return map;
}

private Map<String, Object> convertCustomType(Object source, TypeInformation<?> type) {
Expand Down
Loading