Skip to content

Commit

Permalink
FMWK-280 Support byte array equality queries (#812)
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr authored Dec 26, 2024
1 parent a7d9543 commit d6b9317
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.function.BinaryOperator;
import java.util.function.Function;

import static com.aerospike.client.command.ParticleType.BLOB;
import static com.aerospike.client.command.ParticleType.BOOL;
import static com.aerospike.client.command.ParticleType.INTEGER;
import static com.aerospike.client.command.ParticleType.LIST;
Expand Down Expand Up @@ -192,6 +193,7 @@ public Exp filterExp(Map<QualifierKey, Object> qualifierMap) {
Exp::mapBin);
case LIST -> getFilterExp(Exp.val((List<?>) value.getObject()), getBinName(qualifierMap), Exp::eq,
Exp::listBin);
case BLOB -> Exp.eq(Exp.blobBin(getBinName(qualifierMap)), Exp.val((byte[]) value.getObject()));
default -> throw new IllegalArgumentException("EQ FilterExpression unsupported particle type: " +
value.getClass().getSimpleName());
};
Expand All @@ -211,6 +213,12 @@ public Filter sIndexFilter(Map<QualifierKey, Object> qualifierMap) {
}
yield Filter.equal(getBinName(qualifierMap), value.toString());
}
case BLOB -> {
if (!FilterOperation.getServerVersionSupport(qualifierMap).isServerVersionGtOrEq7()) {
yield null;
}
yield Filter.equal(getBinName(qualifierMap), (byte[]) value.getObject());
}
default -> null;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ protected CriteriaDefinition create(Part part, Iterator<Object> iterator) {
private CriteriaDefinition create(Part part, AerospikePersistentProperty property, Iterator<?> parameters) {
FilterOperation filterOperation = getFilterOperation(part.getType());
List<Object> queryParameters = getQueryParameters(parameters, filterOperation);
// In case of byte[] it does not get converted to an ArrayList, so queryParameters contain byte array
IAerospikeQueryCreator queryCreator = getQueryCreator(part, property, queryParameters, filterOperation);

queryCreator.validate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ private void validateCollectionQueryComparison(List<Object> queryParameters, Str
throw new IllegalArgumentException(queryPartDescription + ": invalid number of arguments, expecting one");
}

// In case of byte[] it does not get converted to an ArrayList, so queryParameters contain byte array
if (queryParameters.get(0) instanceof byte[]) {
return;
}

if (queryParameters.get(0) instanceof Collection) {
validateTypes(converter, Collection.class, queryParameters, this.filterOperation, queryPartDescription);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public class AerospikeTemplateFindByQueryTests extends BaseBlockingIntegrationTe
.strings(Collections.singletonList("str1")).friend(new Person("id21", "TestPerson21", 50)).build();
final Person ashley = Person.builder().id(nextId()).firstName("Ashley").lastName("Matthews")
.ints(Collections.singletonList(22))
.strings(Collections.singletonList("str2")).age(22).friend(new Person("id22", "TestPerson22", 50)).build();
.byteArray(new byte[]{1, 0, 1, 1, 0, 0, 0, 1})
.strings(Collections.singletonList("str2")).age(22).friend(new Person("id22", "TestPerson22", 50))
.build();
final Person beatrice = Person.builder().id(nextId()).firstName("Beatrice").lastName("Matthews").age(23)
.ints(Collections.singletonList(23))
.friend(new Person("id23", "TestPerson23", 42)).build();
Expand Down Expand Up @@ -86,6 +88,8 @@ public void beforeAllSetUp() {

additionalAerospikeTestOperations.createIndex(Person.class, "person_first_name_index", "firstName",
IndexType.STRING);
additionalAerospikeTestOperations.createIndex(Person.class, "person_byte_array_index", "byteArray",
IndexType.BLOB);
}

@Override
Expand All @@ -102,12 +106,22 @@ public void afterAll() {
}

@Test
public void findWithFilterEqual() {
public void findWithFilterEqual_String() {
Query query = QueryUtils.createQueryForMethodWithArgs(serverVersionSupport, "findByFirstName", "Dave");
Stream<Person> result = template.find(query, Person.class);
assertThat(result).containsOnly(dave);
}

@Test
public void findWithFilterEqual_ByteArray() {
if (serverVersionSupport.isServerVersionGtOrEq7()) {
byte[] byteArray = new byte[]{1, 0, 1, 1, 0, 0, 0, 1};
Query query = QueryUtils.createQueryForMethodWithArgs(serverVersionSupport, "findByByteArray", new Object[]{byteArray});
Stream<Person> result = template.find(query, Person.class);
assertThat(result).containsOnly(ashley);
}
}

@Test
public void findWithFilterEqualWithSetName() {
Query query = QueryUtils.createQueryForMethodWithArgs(serverVersionSupport, "findByFirstName", "Dave");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class PersonRepositoryQueryTests extends BaseBlockingIntegrationTests {
.firstName("Stefan")
.lastName("Lessard")
.age(34)
.byteArray(new byte[]{1, 0, 1, 1, 0, 0, 0, 1})
.build();
protected static final Person leroi = Person.builder()
.id(nextId())
Expand All @@ -89,7 +90,7 @@ public class PersonRepositoryQueryTests extends BaseBlockingIntegrationTests {
.firstName("Matias")
.lastName("Craft")
.age(24)
.intArray(new int[]{1, 2, 3, 4, 5})
.intArray(new int[]{0, 1, 2, 3, 4, 5})
.build();
protected static final Person douglas = Person.builder()
.id(nextId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,12 @@ void findByCollectionEquals() {
// another way to call the method
List<Person> persons2 = repository.findByStrings(listToCompareWith);
assertThat(persons2).contains(dave);

List<Person> persons3 = repository.findByIntArray(new int[]{0, 1, 2, 3, 4, 5});
assertThat(persons3).containsOnly(matias);

List<Person> persons4 = repository.findByByteArray(new byte[]{1, 0, 1, 1, 0, 0, 0, 1});
assertThat(persons4).containsOnly(stefan);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class Person {
private List<String> strings;
private List<Integer> ints;
private int[] intArray;
private byte[] byteArray;
private List<List<Integer>> listOfIntLists;
private List<Map<String, Integer>> listOfIntMaps;
private Set<Integer> intSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,10 @@ List<P> findByFriendStringMapNotContaining(AerospikeQueryCriterion criterion,

List<P> findByGenderIn(List<Person.Gender> list);

List<P> findByByteArray(byte[] byteArray);

List<P> findByIntArray(int[] intArray);

List<P> findByFirstNameIs(String name);

boolean existsByFirstNameIs(String name);
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/bootstrap.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
embedded.aerospike.dockerImage=aerospike/aerospike-server:6.4.0.10
embedded.aerospike.dockerImage=aerospike/aerospike-server:7.1.0.7
embedded.aerospike.enabled=true

0 comments on commit d6b9317

Please sign in to comment.