Skip to content

Commit

Permalink
refactoring qualifier builders, order of qualifier builder setters is…
Browse files Browse the repository at this point in the history
… now irrelevant
  • Loading branch information
agrgr committed Oct 16, 2023
1 parent 19f393a commit 92b1d48
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1233,8 +1233,8 @@ private static Exp processMetadataFieldInOrNot(Map<String, Object> qualifierMap,
new Qualifier(
new MetadataQualifierBuilder()
.setMetadataField(getMetadataField(qualifierMap))
.setValue1AsObj(item)
.setFilterOperation(filterOperation)
.setValue1AsObj(item)
).toFilterExp()
).toArray(Exp[]::new);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,33 @@
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static org.springframework.data.aerospike.query.Qualifier.FIELD;
import static org.springframework.data.aerospike.query.Qualifier.METADATA_FIELD;
import static org.springframework.data.aerospike.query.Qualifier.OPERATION;
import static org.springframework.data.aerospike.query.Qualifier.QUALIFIERS;
import static org.springframework.data.aerospike.query.Qualifier.VALUE1;
import static org.springframework.data.aerospike.query.Qualifier.VALUE2;

public class MetadataQualifierBuilder extends QualifierBuilder {
public class MetadataQualifierBuilder implements QualifierMapBuilder {

private final Map<String, Object> map = new HashMap<>();

public MetadataQualifierBuilder setFilterOperation(FilterOperation filterOperation) {
this.map.put(OPERATION, filterOperation);
return this;
}

public FilterOperation getFilterOperation() {
return (FilterOperation) this.map.get(OPERATION);
}

public MetadataQualifierBuilder setQualifiers(Qualifier... qualifiers) {
this.map.put(QUALIFIERS, qualifiers);
return this;
}

public MetadataQualifierBuilder setMetadataField(CriteriaDefinition.AerospikeMetadata metadataField) {
this.map.put(METADATA_FIELD, metadataField);
Expand All @@ -21,6 +41,10 @@ public CriteriaDefinition.AerospikeMetadata getMetadataField() {
return (CriteriaDefinition.AerospikeMetadata) map.get(METADATA_FIELD);
}

public String getField() {
return (String) this.map.get(FIELD);
}

public MetadataQualifierBuilder setValue1AsObj(Object object) {
this.map.put(VALUE1, object);
return this;
Expand All @@ -39,12 +63,16 @@ public Object getValue2AsObj() {
return this.map.get(VALUE2);
}

public Qualifier build() {
validate();
return new Qualifier(this);
}

public Map<String, Object> buildMap() {
return this.map;
}

@SuppressWarnings("unchecked")
@Override
protected void validate() {
// metadata query
if (this.getMetadataField() != null) {
Expand All @@ -71,5 +99,4 @@ protected void validate() {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class Qualifier implements Map<String, Object>, Serializable {
private static final long serialVersionUID = -2689196529952712849L;
protected final Map<String, Object> internalMap;

public Qualifier(QualifierBuilder builder) {
public Qualifier(QualifierMapBuilder builder) {
internalMap = new HashMap<>();

if (!builder.buildMap().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package org.springframework.data.aerospike.query;

import com.aerospike.client.Value;
import lombok.Data;
import org.springframework.data.aerospike.convert.MappingAerospikeConverter;

import java.util.HashMap;
import java.util.Map;

import static org.springframework.data.aerospike.query.Qualifier.*;

@Data
public class QualifierBuilder {
public class QualifierBuilder implements QualifierMapBuilder {

final Map<String, Object> map = new HashMap<>();

public QualifierBuilder() {
}
private final Map<String, Object> map = new HashMap<>();

public QualifierBuilder setField(String field) {
this.map.put(FIELD, field);
Expand Down Expand Up @@ -76,31 +71,26 @@ public QualifierBuilder setConverter(MappingAerospikeConverter converter) {
}

public boolean hasValue1() {
return this.map.containsKey(VALUE1) && this.map.get(VALUE1) != null;
return this.map.get(VALUE1) != null;
}

public boolean hasValue2() {
return this.map.containsKey(VALUE2) && this.map.get(VALUE2) != null;
return this.map.get(VALUE2) != null;
}

public boolean hasValue3() {
return this.map.containsKey(VALUE3) && this.map.get(VALUE3) != null;
return this.map.get(VALUE3) != null;
}

public boolean hasDotPath() {
return this.map.containsKey(DOT_PATH) && this.map.get(DOT_PATH) != null;
return this.map.get(DOT_PATH) != null;
}

public Qualifier build() {
validate();
return new Qualifier(this);
}

public Map<String, Object> buildMap() {
return this.map;
}

// is overridden in MetadataQualifierBuilder
protected void validate() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.springframework.data.aerospike.query;

import java.util.Map;

public interface QualifierMapBuilder {

Map<String, Object> buildMap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ public void numericGTQualifier() {
public void metadataSinceUpdateEQQualifier() {
Qualifier qualifier = new Qualifier(new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue1AsObj(1L)
.setFilterOperation(FilterOperation.GT)
.setValue1AsObj(1L)
);
KeyRecordIterator iterator = queryEngine.select(namespace, SET_NAME, null, qualifier);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,18 @@ public void findPersonsByMetadata() {
// creating a condition "since_update_time metadata value is less than 50 seconds"
Qualifier sinceUpdateTimeLt10Seconds = new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue1AsObj(50000L)
.setFilterOperation(FilterOperation.LT)
.setValue1AsObj(50000L)
.build();
assertThat(reactiveRepository.findByQualifiers(sinceUpdateTimeLt10Seconds).collectList().block())
.containsAll(allIndexedPersons);

// creating a condition "since_update_time metadata value is between 1 millisecond and 50 seconds"
Qualifier sinceUpdateTimeBetween1And50000 = new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setFilterOperation(FilterOperation.BETWEEN)
.setValue1AsObj(1L)
.setValue2AsObj(50000L)
.setFilterOperation(FilterOperation.BETWEEN)
.build();
assertThat(reactiveRepository.findByQualifiers(sinceUpdateTimeBetween1And50000).collectList().block())
.containsAll(reactiveRepository.findByQualifiers(sinceUpdateTimeLt10Seconds).collectList().block());
Expand All @@ -323,25 +323,25 @@ public void findPersonsByQualifiers() {
// creating a condition "since_update_time metadata value is greater than 1 millisecond"
Qualifier sinceUpdateTimeGt1 = new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue1AsObj(1L)
.setFilterOperation(FilterOperation.GT)
.setValue1AsObj(1L)
.build();

// creating a condition "since_update_time metadata value is less than 50 seconds"
Qualifier sinceUpdateTimeLt50Seconds = new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue1AsObj(50000L)
.setFilterOperation(FilterOperation.LT)
.setValue1AsObj(50000L)
.build();
assertThat(reactiveRepository.findByQualifiers(sinceUpdateTimeLt50Seconds).collectList().block())
.containsAll(allIndexedPersons);

// creating a condition "since_update_time metadata value is between 1 and 50 seconds"
Qualifier sinceUpdateTimeBetween1And50000 = new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setFilterOperation(FilterOperation.BETWEEN)
.setValue1AsObj(1L)
.setValue2AsObj(50000L)
.setFilterOperation(FilterOperation.BETWEEN)
.build();

// creating a condition "firsName is equal to Petra"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1222,17 +1222,17 @@ public void findPersonsByMetadata() {
// creating a condition "since_update_time metadata value is less than 50 seconds"
Qualifier sinceUpdateTimeLt10Seconds = new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue1AsObj(50000L)
.setFilterOperation(FilterOperation.LT)
.setValue1AsObj(50000L)
.build();
assertThat(repository.findByQualifiers(sinceUpdateTimeLt10Seconds)).containsAll(allPersons);

// creating a condition "since_update_time metadata value is between 1 millisecond and 50 seconds"
Qualifier sinceUpdateTimeBetween1And50000 = new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setFilterOperation(FilterOperation.BETWEEN)
.setValue1AsObj(1L)
.setValue2AsObj(50000L)
.setFilterOperation(FilterOperation.BETWEEN)
.build();
assertThat(repository.findByQualifiers(sinceUpdateTimeBetween1And50000))
.containsAll(repository.findByQualifiers(sinceUpdateTimeLt10Seconds));
Expand All @@ -1245,24 +1245,24 @@ public void findPersonsByQualifiers() {
// creating a condition "since_update_time metadata value is greater than 1 millisecond"
Qualifier sinceUpdateTimeGt1 = new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue1AsObj(1L)
.setFilterOperation(FilterOperation.GT)
.setValue1AsObj(1L)
.build();

// creating a condition "since_update_time metadata value is less than 50 seconds"
Qualifier sinceUpdateTimeLt50Seconds = new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue1AsObj(50000L)
.setFilterOperation(FilterOperation.LT)
.setValue1AsObj(50000L)
.build();
assertThat(repository.findByQualifiers(sinceUpdateTimeLt50Seconds)).containsAll(allPersons);

// creating a condition "since_update_time metadata value is between 1 millisecond and 50 seconds"
Qualifier sinceUpdateTimeBetween1And50000 = new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setFilterOperation(FilterOperation.BETWEEN)
.setValue1AsObj(1L)
.setValue2AsObj(50000L)
.setFilterOperation(FilterOperation.BETWEEN)
.build();

// creating a condition "firsName is equal to Carter"
Expand Down Expand Up @@ -1345,89 +1345,48 @@ public void findPersonsByQualifiers() {
public void findPersonsByQualifiersMustBeValid() {
assertThatThrownBy(() -> repository.findByQualifiers(new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue1AsObj(1L)
.setFilterOperation(FilterOperation.BETWEEN)
.setValue1AsObj(1L)
.build()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("BETWEEN: value2 is expected to be set as Long");

assertThatThrownBy(() -> repository.findByQualifiers(new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue2AsObj(1L)
.setFilterOperation(FilterOperation.BETWEEN)
.setValue2AsObj(1L)
.build()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("BETWEEN: value1 is expected to be set as Long");

assertThatThrownBy(() -> repository.findByQualifiers(new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue1AsObj(1)
.setFilterOperation(FilterOperation.GT)
.setValue1AsObj(1)
.build()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("GT: value1 is expected to be set as Long");

assertThatThrownBy(() -> repository.findByQualifiers(new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setFilterOperation(FilterOperation.GTEQ)
.setValue1(Value.get(1))
.build()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("GTEQ: value1 is expected to be set as Long");

assertThatThrownBy(() -> repository.findByQualifiers(new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setFilterOperation(FilterOperation.NOT_IN)
.setValue1(Value.get(1))
.build()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("NOT_IN: value1 is expected to be a non-empty Collection<Long>");

assertThatThrownBy(() -> repository.findByQualifiers(new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setFilterOperation(FilterOperation.IN)
.setValue1(Value.get(1))
.build()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("IN: value1 is expected to be a non-empty Collection<Long>");

assertThatThrownBy(() -> repository.findByQualifiers(new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue1AsObj(1)
.setFilterOperation(FilterOperation.LT)
.setValue1AsObj(1)
.build()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("LT: value1 is expected to be set as Long");

assertThatThrownBy(() -> repository.findByQualifiers(new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue1AsObj(Value.get(1))
.setFilterOperation(FilterOperation.LTEQ)
.build()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("LTEQ: value1 is expected to be set as Long");

assertThatThrownBy(() -> repository.findByQualifiers(new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setFilterOperation(FilterOperation.LTEQ)
.setValue1(Value.get(1))
.setValue1AsObj(Value.get(1))
.build()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("LTEQ: value1 is expected to be set as Long");

assertThatThrownBy(() -> repository.findByQualifiers(new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue1AsObj(1L)
.setField("firstName")
.setFilterOperation(FilterOperation.LTEQ)
.build()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Either a field or a metadataField must be set, not both");

assertThatThrownBy(() -> repository.findByQualifiers(new MetadataQualifierBuilder()
.setMetadataField(SINCE_UPDATE_TIME)
.setValue1AsObj(1L)
.setFilterOperation(FilterOperation.STARTS_WITH)
.setValue1AsObj(1L)
.build()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Operation STARTS_WITH cannot be applied to metadataField");
Expand Down

0 comments on commit 92b1d48

Please sign in to comment.