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-348 Cleanup custom queries #727

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -56,7 +56,8 @@ public AerospikeTemplate aerospikeTemplate(IAerospikeClient aerospikeClient,
public QueryEngine queryEngine(IAerospikeClient aerospikeClient,
StatementBuilder statementBuilder,
FilterExpressionsBuilder filterExpressionsBuilder, AerospikeSettings settings) {
QueryEngine queryEngine = new QueryEngine(aerospikeClient, statementBuilder, filterExpressionsBuilder);
QueryEngine queryEngine = new QueryEngine(aerospikeClient, statementBuilder, filterExpressionsBuilder,
settings.getDataSettings());
boolean scansEnabled = settings.getDataSettings().isScansEnabled();
log.debug("AerospikeDataSettings.scansEnabled: {}", scansEnabled);
queryEngine.setScansEnabled(scansEnabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public ReactorQueryEngine reactorQueryEngine(IAerospikeReactorClient aerospikeRe
FilterExpressionsBuilder filterExpressionsBuilder,
AerospikeSettings settings) {
ReactorQueryEngine queryEngine = new ReactorQueryEngine(aerospikeReactorClient, statementBuilder,
filterExpressionsBuilder);
filterExpressionsBuilder, settings.getDataSettings());
boolean scansEnabled = settings.getDataSettings().isScansEnabled();
queryEngine.setScansEnabled(scansEnabled);
log.debug("AerospikeDataSettings.scansEnabled: {}", scansEnabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ public interface AerospikeConverter extends AerospikeReader<Object>, AerospikeWr
*
* @return the underlying {@link AerospikeDataSettings} used by the converter
*/
AerospikeDataSettings getAerospikeSettings();
AerospikeDataSettings getAerospikeDataSettings();
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class MappingAerospikeConverter implements InitializingBean, AerospikeCon
@Getter
private final GenericConversionService conversionService;
@Getter
private final AerospikeDataSettings aerospikeSettings;
private final AerospikeDataSettings aerospikeDataSettings;
private final MappingAerospikeReadConverter readConverter;
private final MappingAerospikeWriteConverter writeConverter;

Expand All @@ -55,7 +55,7 @@ public MappingAerospikeConverter(AerospikeMappingContext mappingContext, CustomC
AerospikeDataSettings settings) {
this.conversions = conversions;
this.conversionService = new DefaultConversionService();
this.aerospikeSettings = settings;
this.aerospikeDataSettings = settings;

EntityInstantiators entityInstantiators = new EntityInstantiators();
TypeMapper<Map<String, Object>> typeMapper = new DefaultTypeMapper<>(aerospikeTypeAliasAccessor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public <T> void saveAll(Iterable<T> documents, String setName) {
}

private <T> void applyBufferedBatchWrite(Iterable<T> documents, String setName, OperationType operationType) {
int batchSize = converter.getAerospikeSettings().getBatchWriteSize();
int batchSize = converter.getAerospikeDataSettings().getBatchWriteSize();
List<T> docsList = new ArrayList<>();

for (T doc : documents) {
Expand Down Expand Up @@ -456,7 +456,7 @@ public boolean deleteById(Object id, String setName) {
public <T> void deleteAll(Iterable<T> documents) {
String setName = getSetName(documents.iterator().next());

int batchSize = converter.getAerospikeSettings().getBatchWriteSize();
int batchSize = converter.getAerospikeDataSettings().getBatchWriteSize();
List<Object> documentsList = new ArrayList<>();
for (Object document : documents) {
if (batchWriteSizeMatch(batchSize, documentsList.size())) {
Expand Down Expand Up @@ -490,7 +490,7 @@ public void deleteByIds(Iterable<?> ids, String setName) {
Assert.notNull(setName, "Set name must not be null!");
validateForBatchWrite(ids, "IDs");

int batchSize = converter.getAerospikeSettings().getBatchWriteSize();
int batchSize = converter.getAerospikeDataSettings().getBatchWriteSize();
List<Object> idsList = new ArrayList<>();
for (Object id : ids) {
if (batchWriteSizeMatch(batchSize, idsList.size())) {
Expand Down Expand Up @@ -884,7 +884,7 @@ public <T, S> List<S> findByIds(Iterable<?> ids, Class<T> entityClass, Class<S>
Assert.notNull(entityClass, "Entity class must not be null!");
Assert.notNull(setName, "Set name must not be null!");

int batchSize = converter.getAerospikeSettings().getBatchWriteSize();
int batchSize = converter.getAerospikeDataSettings().getBatchWriteSize();
List<Object> idsList = new ArrayList<>();
List<S> result = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ Key getKey(Object id, String setName) {
Assert.notNull(setName, "Set name must not be null!");
Key key;
// choosing whether tp preserve id type based on the configuration
if (converter.getAerospikeSettings().isKeepOriginalKeyTypes()) {
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 Character) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public <T> Flux<T> saveAll(Iterable<T> documents, String setName) {

private <T> Flux<T> applyBufferedBatchWrite(Iterable<? extends T> documents, String setName,
OperationType operationType) {
int batchSize = converter.getAerospikeSettings().getBatchWriteSize();
int batchSize = converter.getAerospikeDataSettings().getBatchWriteSize();
List<T> docsList = new ArrayList<>();
Flux<T> result = Flux.empty();

Expand Down Expand Up @@ -477,7 +477,7 @@ public Mono<Void> deleteByIds(Iterable<?> ids, String setName) {
Assert.notNull(setName, "Set name must not be null!");
validateForBatchWrite(ids, "IDs");

int batchSize = converter.getAerospikeSettings().getBatchWriteSize();
int batchSize = converter.getAerospikeDataSettings().getBatchWriteSize();
List<Object> idsList = new ArrayList<>();
Flux<Void> result = Flux.empty();
for (Object id : ids) {
Expand Down Expand Up @@ -786,7 +786,7 @@ public <T> Flux<T> findByIds(Iterable<?> ids, Class<T> targetClass, String setNa
Assert.notNull(targetClass, "Class must not be null!");
Assert.notNull(setName, "Set name must not be null!");

int batchSize = converter.getAerospikeSettings().getBatchWriteSize();
int batchSize = converter.getAerospikeDataSettings().getBatchWriteSize();
List<Object> idsList = new ArrayList<>();
Flux<T> result = Flux.empty();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.aerospike.client.query.Filter;
import com.aerospike.client.query.IndexCollectionType;
import com.aerospike.client.query.RegexFlag;
import org.springframework.data.aerospike.convert.MappingAerospikeConverter;
import org.springframework.data.aerospike.config.AerospikeDataSettings;
import org.springframework.data.aerospike.query.qualifier.Qualifier;
import org.springframework.data.aerospike.query.qualifier.QualifierKey;
import org.springframework.data.aerospike.repository.query.CriteriaDefinition;
Expand Down Expand Up @@ -56,7 +56,6 @@
import static org.springframework.data.aerospike.util.FilterOperationRegexpBuilder.getStringEquals;

public enum FilterOperation {

AND {
@Override
public Exp filterExp(Map<QualifierKey, Object> qualifierMap) {
Expand Down Expand Up @@ -462,9 +461,8 @@ public Exp filterExp(Map<QualifierKey, Object> qualifierMap) {
*/
@Override
public Filter sIndexFilter(Map<QualifierKey, Object> qualifierMap) {
String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap),
"MAP_VAL_EQ_BY_KEY secondary index filter: dotPath has not been set");
final boolean useCtx = dotPathArr.length > 2;
String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap));
final boolean useCtx = dotPathArr != null && dotPathArr.length > 2;

return switch (getValue(qualifierMap).getType()) {
case STRING -> {
Expand Down Expand Up @@ -518,9 +516,8 @@ public Filter sIndexFilter(Map<QualifierKey, Object> qualifierMap) {
return null;
}

String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap),
"MAP_VAL_GT_BY_KEY secondary index filter: dotPath has not been set");
if (dotPathArr.length > 2) {
String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap));
if (dotPathArr != null && dotPathArr.length > 2) {
return null; // currently not supported
} else {
return Filter.range(getField(qualifierMap), IndexCollectionType.MAPVALUES,
Expand All @@ -544,9 +541,8 @@ public Filter sIndexFilter(Map<QualifierKey, Object> qualifierMap) {
return null;
}

String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap),
"MAP_VAL_GTEQ_BY_KEY secondary index filter: dotPath has not been set");
if (dotPathArr.length > 2) {
String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap));
if (dotPathArr != null && dotPathArr.length > 2) {
return null; // currently not supported
} else {
return Filter.range(getField(qualifierMap), IndexCollectionType.MAPVALUES,
Expand All @@ -571,9 +567,8 @@ public Filter sIndexFilter(Map<QualifierKey, Object> qualifierMap) {
return null;
}

String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap),
"MAP_VAL_LT_BY_KEY secondary index filter: dotPath has not been set");
if (dotPathArr.length > 2) {
String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap));
if (dotPathArr != null && dotPathArr.length > 2) {
return null; // currently not supported
} else {
return Filter.range(getField(qualifierMap), IndexCollectionType.MAPVALUES, Long.MIN_VALUE,
Expand All @@ -596,9 +591,8 @@ public Filter sIndexFilter(Map<QualifierKey, Object> qualifierMap) {
return null;
}

String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap),
"MAP_VAL_LTEQ_BY_KEY secondary index filter: dotPath has not been set");
if (dotPathArr.length > 2) {
String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap));
if (dotPathArr != null && dotPathArr.length > 2) {
return null; // currently not supported
} else {
return Filter.range(getField(qualifierMap), IndexCollectionType.MAPVALUES, Long.MIN_VALUE,
Expand All @@ -609,8 +603,7 @@ public Filter sIndexFilter(Map<QualifierKey, Object> qualifierMap) {
MAP_VAL_BETWEEN_BY_KEY {
@Override
public Exp filterExp(Map<QualifierKey, Object> qualifierMap) {
String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap),
"MAP_VAL_BETWEEN_BY_KEY filter expression: dotPath has not been set");
String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap));
Exp lowerLimit;
Exp upperLimit;
Exp.Type type;
Expand Down Expand Up @@ -648,7 +641,7 @@ private static Exp mapValBetweenByKey(Map<QualifierKey, Object> qualifierMap, St
Exp lowerLimit,
Exp upperLimit) {
Exp mapExp;
if (dotPathArr.length > 2) {
if (dotPathArr != null && dotPathArr.length > 2) {
mapExp = MapExp.getByKey(MapReturnType.VALUE, type, Exp.val(getKey(qualifierMap).toString()),
Exp.mapBin(getField(qualifierMap)), dotPathToCtxMapKeys(dotPathArr));
} else {
Expand All @@ -671,9 +664,8 @@ public Filter sIndexFilter(Map<QualifierKey, Object> qualifierMap) {
return null;
}

String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap),
"MAP_VAL_BETWEEN_BY_KEY secondary index filter: dotPath has not been set");
if (dotPathArr.length > 2) {
String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap));
if (dotPathArr != null && dotPathArr.length > 2) {
return null; // currently not supported
} else {
return Filter.range(getField(qualifierMap), IndexCollectionType.MAPVALUES,
Expand Down Expand Up @@ -792,9 +784,8 @@ public Filter sIndexFilter(Map<QualifierKey, Object> qualifierMap) {
MAP_VAL_IS_NOT_NULL_BY_KEY {
@Override
public Exp filterExp(Map<QualifierKey, Object> qualifierMap) {
String[] dotPathArray = getDotPathArray(getDotPath(qualifierMap),
"MAP_VAL_IS_NULL_BY_KEY: dotPath was not set");
if (dotPathArray.length > 1) {
String[] dotPathArray = getDotPathArray(getDotPath(qualifierMap));
if (dotPathArray != null && dotPathArray.length > 1) {
// in case it is a field of an object set to null the key does not get added to a Map,
// so it is enough to look for Maps with the given key
return mapKeysContain(qualifierMap);
Expand All @@ -813,9 +804,8 @@ public Filter sIndexFilter(Map<QualifierKey, Object> qualifierMap) {
MAP_VAL_IS_NULL_BY_KEY {
@Override
public Exp filterExp(Map<QualifierKey, Object> qualifierMap) {
String[] dotPathArray = getDotPathArray(getDotPath(qualifierMap),
"MAP_VAL_IS_NULL_BY_KEY: dotPath was not set");
if (dotPathArray.length > 1) {
String[] dotPathArray = getDotPathArray(getDotPath(qualifierMap));
if (dotPathArray != null && dotPathArray.length > 1) {
// in case it is a field of an object set to null the key does not get added to a Map,
// so it is enough to look for Maps without the given key
return mapKeysNotContain(qualifierMap);
Expand Down Expand Up @@ -1413,8 +1403,7 @@ private static Exp mapValuesCountComparedToZero(Map<QualifierKey, Object> qualif

private static Exp getFilterExpMapValOrFail(Map<QualifierKey, Object> qualifierMap, BinaryOperator<Exp> operator,
String opName) {
String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap),
opName + " filter expression: dotPath has not been set");
String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap));

return switch (getValue(qualifierMap).getType()) {
case INTEGER -> operator.apply(getMapExp(qualifierMap, dotPathArr, Exp.Type.INT),
Expand All @@ -1432,7 +1421,7 @@ private static Exp getFilterExpMapValOrFail(Map<QualifierKey, Object> qualifierM

private static Exp getMapExp(Map<QualifierKey, Object> qualifierMap, String[] dotPathArr, Exp.Type expType) {
Exp mapKeyExp = getMapKeyExp(getKey(qualifierMap).getObject(), keepOriginalKeyTypes(qualifierMap));
if (dotPathArr.length > 2) {
if (dotPathArr != null && dotPathArr.length > 2) {
return MapExp.getByKey(MapReturnType.VALUE, expType, mapKeyExp,
Exp.mapBin(getField(qualifierMap)), dotPathToCtxMapKeys(dotPathArr));
} else {
Expand Down Expand Up @@ -1463,8 +1452,10 @@ private static Exp getMapKeyExp(Object mapKey, boolean keepOriginalKeyTypes) {
}

private static boolean keepOriginalKeyTypes(Map<QualifierKey, Object> qualifierMap) {
return ((MappingAerospikeConverter) qualifierMap.get(CONVERTER))
.getAerospikeSettings().isKeepOriginalKeyTypes();
Object dataSettings = qualifierMap.get(DATA_SETTINGS);
if (dataSettings == null) throw new IllegalStateException("Expecting AerospikeDataSettings in qualifier map " +
"with the key " + DATA_SETTINGS);
return ((AerospikeDataSettings) dataSettings).isKeepOriginalKeyTypes();
}

private static Exp getFilterExpMapValEqOrFail(Map<QualifierKey, Object> qualifierMap,
Expand All @@ -1479,9 +1470,8 @@ private static Exp getFilterExpMapValNotEqOrFail(Map<QualifierKey, Object> quali

private static Exp getMapValEqOrFail(Map<QualifierKey, Object> qualifierMap, BinaryOperator<Exp> operator,
String opName) {
String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap),
opName + " filter expression: dotPath has not been set");
final boolean useCtx = dotPathArr.length > 2;
String[] dotPathArr = getDotPathArray(getDotPath(qualifierMap));
final boolean useCtx = dotPathArr != null && dotPathArr.length > 2;

// boolean values are read as BoolIntValue (INTEGER ParticleType) if Value.UseBoolBin == false
// so converting to BooleanValue to process correctly
Expand Down Expand Up @@ -1536,17 +1526,16 @@ private static Exp getFilterExp(Exp exp, String field,
return operator.apply(binExp.apply(field), exp);
}

private static String[] getDotPathArray(List<String> dotPathList, String errMsg) {
private static String[] getDotPathArray(List<String> dotPathList) {
if (dotPathList != null && !dotPathList.isEmpty()) {
// the first element of dotPath is part.getProperty().toDotPath()
// the second element of dotPath, if present, is a value
Stream<String> valueStream = dotPathList.size() == 1 || dotPathList.get(1) == null ? Stream.empty()
: Stream.of(dotPathList.get(1));
return Stream.concat(Arrays.stream(dotPathList.get(0).split("\\.")), valueStream)
.toArray(String[]::new);
} else {
throw new IllegalStateException(errMsg);
}
return null;
}

private static CTX[] dotPathToCtxMapKeys(String[] dotPathArray) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.aerospike.client.query.Statement;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.aerospike.config.AerospikeDataSettings;
import org.springframework.data.aerospike.query.qualifier.Qualifier;
import org.springframework.data.aerospike.repository.query.Query;
import org.springframework.lang.Nullable;
Expand All @@ -48,6 +49,7 @@ public class QueryEngine {
private final StatementBuilder statementBuilder;
@Getter
private final FilterExpressionsBuilder filterExpressionsBuilder;
private final AerospikeDataSettings dataSettings;
/**
* Scans can potentially slow down Aerospike server, so we are disabling them by default. If you still need to use
* scans, set this property to true.
Expand All @@ -59,10 +61,11 @@ public class QueryEngine {
private long queryMaxRecords;

public QueryEngine(IAerospikeClient client, StatementBuilder statementBuilder,
FilterExpressionsBuilder filterExpressionsBuilder) {
FilterExpressionsBuilder filterExpressionsBuilder, AerospikeDataSettings dataSettings) {
this.client = client;
this.statementBuilder = statementBuilder;
this.filterExpressionsBuilder = filterExpressionsBuilder;
this.dataSettings = dataSettings;
}

/**
Expand Down Expand Up @@ -106,6 +109,10 @@ public KeyRecordIterator select(String namespace, String set, String[] binNames,
/*
* query with filters
*/
if (query != null) {
// dataSettings provided to be used in FilterOperation
query.getCriteriaObject().setDataSettings(dataSettings);
}
Statement statement = statementBuilder.build(namespace, set, query, binNames);
statement.setMaxRecords(queryMaxRecords);
QueryPolicy localQueryPolicy = getQueryPolicy(query, true);
Expand Down
Loading
Loading