Skip to content

Commit

Permalink
choose whether to preserve the types of map keys and ids based on con…
Browse files Browse the repository at this point in the history
…figuration, first part
  • Loading branch information
agrgr committed Nov 30, 2023
1 parent 6e0e60e commit 254b17f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,26 @@ WritePolicy ignoreGenerationDeletePolicy() {
}

Key getKey(Object id, AerospikePersistentEntity<?> entity) {
Assert.notNull(id, "Id must not be null!");
String userKey = convertIfNecessary(id, String.class);
return new Key(this.namespace, entity.getSetName(), userKey);
return getKey(id, entity.getSetName());
}

Key getKey(Object id, String setName) {
Assert.notNull(id, "Id must not be null!");
Assert.notNull(setName, "Set name must not be null!");
String userKey = convertIfNecessary(id, String.class);
return new Key(this.namespace, setName, userKey);
Key key;
// choosing whether tp preserve id type based on the configuration
if (converter.getAerospikeDataSettings().isKeepOriginalKeyTypes()) {
if (id instanceof Byte || id instanceof Short || id instanceof Integer || id instanceof Long) {
key = new Key(this.namespace, setName, convertIfNecessary(((Number) id).longValue(), Long.class));
} else if (id instanceof byte[]) {
key = new Key(this.namespace, setName, convertIfNecessary(id, byte[].class));
} else {
key = new Key(this.namespace, setName, convertIfNecessary(id, String.class));
}
return key;
} else {
return new Key(this.namespace, setName, convertIfNecessary(id, String.class));
}
}

GroupedEntities toGroupedEntities(EntitiesKeys entitiesKeys, Record[] records) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ public Filter sIndexFilter(Map<String, Object> qualifierMap) {
@SuppressWarnings("unchecked")
private static Exp processMetadataFieldInOrNot(Map<String, Object> qualifierMap, boolean notIn) {
FilterOperation filterOperation = notIn ? NOTEQ : EQ;
Object value1 = getValue1Object(qualifierMap);
Object value1 = getValue1AsObject(qualifierMap);

Collection<Long> listOfLongs;
try {
Expand Down Expand Up @@ -1325,7 +1325,7 @@ private static Optional<Exp> getMetadataExp(Map<String, Object> qualifierMap) {
return Optional.of(
operationFunction.apply(
mapMetadataExp(metadataField),
Exp.val(getValue1AsLongOrFail(getValue1Object(qualifierMap)))
Exp.val(getValue1AsLongOrFail(getValue1AsObject(qualifierMap)))
)
);
}
Expand Down Expand Up @@ -1464,18 +1464,40 @@ private static Exp getFilterExpMapValOrFail(Map<String, Object> qualifierMap, Bi

private static Exp getMapExp(Map<String, Object> qualifierMap, String[] dotPathArr, Exp.Type expType) {
// VALUE2 contains key (field name)
// currently the only Map keys supported are Strings
Exp mapKeyExp = getMapKeyExp(getValue2AsObject(qualifierMap));
if (dotPathArr.length > 2) {
// TODO: Support Integer, Bytes and Double (also support both modes)
// TODO: read flag (keepOriginalTypes)
return MapExp.getByKey(MapReturnType.VALUE, expType, Exp.val(getValue2(qualifierMap).toString()),
return MapExp.getByKey(MapReturnType.VALUE, expType, mapKeyExp,
Exp.mapBin(getField(qualifierMap)), dotPathToCtxMapKeys(dotPathArr));
} else {
return MapExp.getByKey(MapReturnType.VALUE, expType, Exp.val(getValue2(qualifierMap).toString()),
return MapExp.getByKey(MapReturnType.VALUE, expType, mapKeyExp,
Exp.mapBin(getField(qualifierMap)));
}
}

private static Exp getMapKeyExp(Object mapKey) {
// choosing whether to preserve map key type based on the configuration
if (keepOriginalKeyTypes()) {
Exp res;
if (mapKey instanceof Byte || mapKey instanceof Short || mapKey instanceof Integer || mapKey instanceof Long) {
res = Exp.val(((Number) mapKey).longValue()); // TODO: byte, short
} else if (mapKey instanceof Float || mapKey instanceof Double) {
res = Exp.val(((Number) mapKey).doubleValue()); // TODO: float
} else if (mapKey instanceof byte[]) {
res = Exp.val((byte[]) mapKey);
} else if (Value.get(mapKey) instanceof Value.NullValue) {
res = Exp.nil();
} else {
res = Exp.val(Value.get(mapKey).toString());
}
return res;
}
return Exp.val(Value.get(mapKey).toString());
}

private static boolean keepOriginalKeyTypes() {
return true; // TODO: read flag (keepOriginalKeysTypes)
}

private static Exp getFilterExpMapValEqOrFail(Map<String, Object> qualifierMap, BinaryOperator<Exp> operator) {
return getMapValEqOrFail(qualifierMap, operator, "MAP_VAL_EQ_BY_KEY");
}
Expand Down Expand Up @@ -1610,14 +1632,18 @@ protected static Value getValue1(Map<String, Object> qualifierMap) {
return Value.get(qualifierMap.get(VALUE1));
}

protected static Object getValue1Object(Map<String, Object> qualifierMap) {
protected static Object getValue1AsObject(Map<String, Object> qualifierMap) {
return qualifierMap.get(VALUE1);
}

protected static Value getValue2(Map<String, Object> qualifierMap) {
return Value.get(qualifierMap.get(VALUE2));
}

protected static Object getValue2AsObject(Map<String, Object> qualifierMap) {
return qualifierMap.get(VALUE2);
}

protected static Value getValue3(Map<String, Object> qualifierMap) {
return (Value) qualifierMap.get(VALUE3);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.aerospike.config.BlockingTestConfig;
import org.springframework.data.aerospike.config.CommonTestConfig;
import org.springframework.data.aerospike.convert.MappingAerospikeConverter;
import org.springframework.data.aerospike.core.AerospikeTemplate;
import org.springframework.data.aerospike.query.QueryEngine;
import org.springframework.data.aerospike.query.cache.IndexRefresher;
Expand Down Expand Up @@ -32,6 +33,8 @@ public abstract class BaseBlockingIntegrationTests extends BaseIntegrationTests
protected IndexesCache indexesCache;
@Autowired
protected IndexRefresher indexRefresher;
@Autowired
protected MappingAerospikeConverter converter;

protected <T> void deleteOneByOne(Collection<T> collection) {
collection.forEach(item -> template.delete(item));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,10 +852,29 @@ void findByMapKeyValueLessThanOrEqual() {
List<Person> persons = repository.findByIntMapLessThanEqual("key2", 1);
assertThat(persons).containsExactly(carter);

carter.setLongIntMap(Map.of(10L, 10));
repository.save(carter);
persons = repository.findByLongIntMapLessThanEqual(10L, 10);
assertThat(persons).containsExactly(carter);
if (converter.getAerospikeDataSettings().isKeepOriginalKeyTypes()) {
carter.setLongIntMap(Map.of(10L, 10));
repository.save(carter);
List<Person> persons2 = repository.findByLongIntMapLessThanEqual(10L, 10);
assertThat(persons2).containsExactly(carter);
carter.setLongIntMap(null); // cleanup
repository.save(carter);

carter.setDoubleIntMap(Map.of(0.9D, 10));
repository.save(carter);
List<Person> persons3 = repository.findByDoubleIntMapLessThanEqual(0.9D, 10);
assertThat(persons3).containsExactly(carter);
carter.setDoubleIntMap(null); // cleanup
repository.save(carter);

byte[] byteArray = new byte[]{0, 1, 1, 0};
carter.setByteArrayIntMap(Map.of(byteArray, 10));
repository.save(carter);
List<Person> persons4 = repository.findByByteArrayIntMapLessThanEqual(byteArray, 10);
assertThat(persons4).containsExactly(carter);
carter.setByteArrayIntMap(null); // cleanup
repository.save(carter);
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class Person {
private Map<String, String> stringMap;
private Map<String, Integer> intMap;
private Map<Long, Integer> longIntMap;
private Map<Double, Integer> doubleIntMap;
private Map<byte[], Integer> byteArrayIntMap;
private Map<String, List<Integer>> mapOfIntLists;
private Person friend;
private Person bestFriend;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,6 @@ public interface PersonRepository<P extends Person> extends AerospikeRepository<
*/
List<P> findByStringMapNotContaining(String key, @NotNull String value);

/**
*
* @param key
* @param lessThanOrEqualTo
* @return
*/
List<P> findByLongIntMapLessThanEqual(long key, int lessThanOrEqualTo);

/**
* Find all entities containing the given map element (key or value depending on the given criterion)
*
Expand Down Expand Up @@ -658,6 +650,12 @@ List<P> findByStringMapContaining(String key1, String value1, String key2, Strin
*/
List<P> findByIntMapLessThanEqual(String key, int lessThanOrEqualTo);

List<P> findByLongIntMapLessThanEqual(long key, int lessThanOrEqualTo);

List<P> findByDoubleIntMapLessThanEqual(double key, int lessThanOrEqualTo);

List<P> findByByteArrayIntMapLessThanEqual(byte[] key, int lessThanOrEqualTo);

/**
* Find all entities that satisfy the condition "have the given map key and the value in the range between the given
* integers"
Expand Down

0 comments on commit 254b17f

Please sign in to comment.