Skip to content

Commit 7ff9b09

Browse files
authored
Add convinient query methods in Aeromapper to return a List of result (#76)
The current query methods needed to use processor to be able to convert a result set into an array list. The code looked too verbose and needed repetition. The newly added convenient query methods abstract away that implementation and returns the list of result. Added by: https://github.com/sujeet100.
1 parent 38e1409 commit 7ff9b09

File tree

3 files changed

+119
-20
lines changed

3 files changed

+119
-20
lines changed

src/main/java/com/aerospike/mapper/tools/AeroMapper.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,22 @@ public <T> void query(QueryPolicy policy, @NotNull Class<T> clazz, @NotNull Proc
520520
}
521521
}
522522

523+
@Override
524+
public <T> List<T> query(Class<T> clazz, Filter filter) {
525+
return query(null, clazz, filter);
526+
}
527+
528+
@Override
529+
public <T> List<T> query(QueryPolicy policy, Class<T> clazz, Filter filter) {
530+
List<T> result = new ArrayList<>();
531+
Processor<T> resultProcessor = record -> {
532+
result.add(record);
533+
return true;
534+
};
535+
query(policy, clazz, resultProcessor, filter);
536+
return result;
537+
}
538+
523539
@Override
524540
public <T> VirtualList<T> asBackedList(@NotNull Object object, @NotNull String binName, Class<T> elementClazz) {
525541
return new VirtualList<>(this, object, binName, elementClazz);

src/main/java/com/aerospike/mapper/tools/IAeroMapper.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.aerospike.mapper.tools;
22

3+
import java.util.List;
34
import java.util.function.Function;
45

56
import javax.validation.constraints.NotNull;
@@ -277,6 +278,31 @@ public interface IAeroMapper extends IBaseAeroMapper {
277278
*/
278279
<T> void query(QueryPolicy policy, @NotNull Class<T> clazz, @NotNull Processor<T> processor, Filter filter);
279280

281+
/**
282+
* Perform a secondary index query with the specified query policy
283+
* and returns the list of records converted to the appropriate class.
284+
* <p/>
285+
* The query policy used will be the one associated with the passed classtype.
286+
*
287+
* @param clazz - the class used to determine which set to scan and to convert the returned records to.
288+
* @param filter - the filter used to determine which secondary index to use. If this filter is null, every record in the set
289+
* associated with the passed classtype will be scanned, effectively turning the query into a scan
290+
* @return List of records converted to the appropriate class
291+
*/
292+
<T> List<T> query(@NotNull Class<T> clazz, Filter filter);
293+
294+
/**
295+
* Perform a secondary index query with the specified query policy
296+
* and returns the list of records converted to the appropriate class.
297+
*
298+
* @param policy - The query policy to use. If this parameter is not passed, the query policy associated with the passed classtype will be used
299+
* @param clazz - the class used to determine which set to scan and to convert the returned records to.
300+
* @param filter - the filter used to determine which secondary index to use. If this filter is null, every record in the set
301+
* associated with the passed classtype will be scanned, effectively turning the query into a scan
302+
* @return List of records converted to the appropriate class
303+
*/
304+
<T> List<T> query(QueryPolicy policy, @NotNull Class<T> clazz, Filter filter);
305+
280306
/**
281307
* Create a virtual list against an attribute on a class. The list does all operations to the database and does not affect the underlying
282308
* class, and is useful for situation when operations are needed to affect the database without having to return all the elements on the

src/test/java/com/aerospike/mapper/QueryTest.java

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.aerospike.mapper;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static java.util.Comparator.comparing;
5+
import static java.util.stream.Collectors.toList;
46

7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Objects;
510
import java.util.concurrent.atomic.AtomicInteger;
611

712
import org.junit.jupiter.api.Test;
813

914
import com.aerospike.client.AerospikeException;
15+
import com.aerospike.client.exp.Exp;
1016
import com.aerospike.client.policy.QueryPolicy;
1117
import com.aerospike.client.query.Filter;
1218
import com.aerospike.client.query.IndexType;
@@ -46,31 +52,52 @@ public int getAge() {
4652
public String toString() {
4753
return String.format("id:%d, name:%s, age:%d", id, name, age);
4854
}
55+
56+
@Override
57+
public boolean equals(Object o) {
58+
if (this == o) {
59+
return true;
60+
}
61+
if (o == null || getClass() != o.getClass()) {
62+
return false;
63+
}
64+
A a = (A) o;
65+
return id == a.id && age == a.age && Objects.equals(name, a.name);
66+
}
67+
68+
@Override
69+
public int hashCode() {
70+
return Objects.hash(id, name, age);
71+
}
4972
}
5073

74+
private List<A> data = new ArrayList<A>() {{
75+
add(new A(1, "Tim", 312));
76+
add(new A(2, "Bob", 44));
77+
add(new A(3, "Sue", 56));
78+
add(new A(4, "Rob", 23));
79+
add(new A(5, "Jim", 32));
80+
add(new A(6, "Bob", 78));
81+
add(new A(7, "Fred", 23));
82+
add(new A(8, "Wilma", 11));
83+
add(new A(9, "Barney", 54));
84+
add(new A(10, "Steve", 72));
85+
add(new A(11, "Bam Bam", 19));
86+
add(new A(12, "Betty", 34));
87+
add(new A(13, "Del", 7));
88+
add(new A(14, "Khon", 98));
89+
add(new A(15, "Dave", 21));
90+
add(new A(16, "Mike", 32));
91+
add(new A(17, "Darren", 14));
92+
add(new A(18, "Lucy", 45));
93+
add(new A(19, "Gertrude", 36));
94+
add(new A(20, "Lucinda", 63));
95+
}};
96+
5197
private AeroMapper populate() {
5298
client.truncate(null, "test", "testScan", null);
5399
AeroMapper mapper = new AeroMapper.Builder(client).build();
54-
mapper.save(new A(1, "Tim", 312),
55-
new A(2, "Bob", 44),
56-
new A(3, "Sue", 56),
57-
new A(4, "Rob", 23),
58-
new A(5, "Jim", 32),
59-
new A(6, "Bob", 78),
60-
new A(7, "Fred", 23),
61-
new A(8, "Wilma", 11),
62-
new A(9, "Barney", 54),
63-
new A(10, "Steve", 72),
64-
new A(11, "Bam Bam", 19),
65-
new A(12, "Betty", 34),
66-
new A(13, "Del", 7),
67-
new A(14, "Khon", 98),
68-
new A(15, "Dave", 21),
69-
new A(16, "Mike", 32),
70-
new A(17, "Darren", 14),
71-
new A(18, "Lucy", 45),
72-
new A(19, "Gertrude", 36),
73-
new A(20, "Lucinda", 63));
100+
mapper.save(data.toArray());
74101

75102
try {
76103
client.createIndex(null, "test", "testScan", "age_idx", "age", IndexType.NUMERIC).waitTillComplete();
@@ -104,4 +131,34 @@ public void queryTestWithAbort() {
104131
}, Filter.range("age", 30, 54));
105132
assertEquals(1, counter.get());
106133
}
134+
135+
@Test
136+
public void queryTestReturnsList() {
137+
AeroMapper mapper = populate();
138+
139+
List<A> result = mapper.query(A.class, Filter.range("age", 30, 54));
140+
141+
List<A> expected = data.stream()
142+
.filter(d -> d.age >= 30 && d.age <= 54)
143+
.sorted(comparing(A::getId))
144+
.collect(toList());
145+
assertEquals(7, result.size());
146+
assertEquals(expected, result.stream().sorted(comparing(A::getId)).collect(toList()));
147+
}
148+
149+
@Test
150+
public void queryWithQueryPolicyTestReturnsList() {
151+
AeroMapper mapper = populate();
152+
QueryPolicy queryPolicy = new QueryPolicy();
153+
queryPolicy.filterExp = Exp.build(Exp.eq(Exp.stringBin("name"), Exp.val("Bob")));
154+
155+
List<A> result = mapper.query(queryPolicy, A.class, Filter.range("age", 44, 78));
156+
157+
List<A> expected = data.stream()
158+
.filter(d -> d.age >= 44 && d.age <= 78 && d.name.equals("Bob"))
159+
.sorted(comparing(A::getId))
160+
.collect(toList());
161+
assertEquals(2, result.size());
162+
assertEquals(expected, result.stream().sorted(comparing(A::getId)).collect(toList()));
163+
}
107164
}

0 commit comments

Comments
 (0)