Skip to content

Commit 8d24eb9

Browse files
committed
more examples, unit tests. Added a generic comparison method to the test
base class. Added more methods to VirtualLists
1 parent 915578e commit 8d24eb9

File tree

10 files changed

+282
-14
lines changed

10 files changed

+282
-14
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,25 @@ public <T> T translateFromAerospike(@NotNull Object obj, @NotNull Class<T> expec
249249
}
250250

251251

252+
/**
253+
* Save each object in the database. This method will perform a REPLACE on the existing record so any existing
254+
* data will be overwritten by the data in the passed object. This is a convenience method for
255+
* <pre>
256+
* save(A);
257+
* save(B);
258+
* save(C);
259+
* </pre>
260+
* Not that no transactionality is implied by this method -- if any of the save methods fail, the exception will be
261+
* thrown without trying the other objects, nor attempting to roll back previously saved objects
262+
* @param object
263+
* @throws AerospikeException
264+
*/
265+
public void save(@NotNull Object ... objects) throws AerospikeException {
266+
for (Object thisObject : objects) {
267+
this.save(thisObject);
268+
}
269+
}
270+
252271
/**
253272
* Save an object in the database. This method will perform a REPLACE on the existing record so any existing
254273
* data will be overwritten by the data in the passed object

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ public MultiOperation<E> append(E item) {
129129
this.interactions.add(new Interactor(virtualList.getAppendOperation(aerospikeItem)));
130130
return this;
131131
}
132+
public MultiOperation<E> removeByKey(Object key) {
133+
this.interactions.add(getRemoveKeyInteractor(key));
134+
return this;
135+
}
132136
public MultiOperation<E> removeByKeyRange(Object startKey, Object endKey) {
133137
this.interactions.add(getRemoveKeyRangeInteractor(startKey, endKey));
134138
return this;
@@ -473,7 +477,42 @@ public boolean isGetOperation() {
473477
};
474478
return new Interactor(deferred);
475479
}
480+
481+
private Interactor getRemoveKeyInteractor(Object key) {
482+
DeferredOperation deferred = new DeferredOperation() {
483+
484+
@Override
485+
public ResultsUnpacker[] getUnpackers(OperationParameters operationParams) {
486+
switch (operationParams.getNeedsResultOfType()) {
487+
case DEFAULT:
488+
case ELEMENTS:
489+
return new ResultsUnpacker[] { new ArrayUnpacker(instanceMapper) };
490+
default:
491+
return new ResultsUnpacker[0];
492+
}
493+
}
494+
495+
@Override
496+
public Operation getOperation(OperationParameters operationParams) {
497+
if (listType == EmbedType.LIST) {
498+
return ListOperation.removeByValue(binName, getValue(key, true),
499+
returnTypeToListReturnType(operationParams.getNeedsResultOfType()));
500+
}
501+
else {
502+
return MapOperation.removeByKey(binName, getValue(key, true),
503+
returnTypeToMapReturnType(operationParams.getNeedsResultOfType()));
504+
}
505+
}
506+
507+
@Override
508+
public boolean isGetOperation() {
509+
return false;
510+
}
511+
};
512+
return new Interactor(deferred);
513+
}
476514

515+
477516
private Interactor getRemoveValueRangeInteractor(Object startValue, Object endValue) {
478517
DeferredOperation deferred = new DeferredOperation() {
479518

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

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

3+
import static org.junit.Assert.assertEquals;
4+
35
import org.junit.AfterClass;
46
import org.junit.Before;
57
import org.junit.BeforeClass;
@@ -9,6 +11,9 @@
911
import com.aerospike.client.DebugAerospikeClient.Options;
1012
import com.aerospike.client.IAerospikeClient;
1113
import com.aerospike.mapper.tools.ClassCache;
14+
import com.fasterxml.jackson.core.JsonProcessingException;
15+
import com.fasterxml.jackson.databind.ObjectMapper;
16+
import com.fasterxml.jackson.databind.ObjectWriter;
1217

1318
public abstract class AeroMapperBaseTest {
1419

@@ -33,4 +38,17 @@ public void clearCache() {
3338
ClassCache.getInstance().clear();
3439

3540
}
41+
42+
public void compare(Object original, Object read) {
43+
try {
44+
ObjectWriter objectWriter = new ObjectMapper().writerWithDefaultPrettyPrinter();
45+
String readString = objectWriter.writeValueAsString(read);
46+
System.out.println(readString);
47+
String originalObject = objectWriter.writeValueAsString(original);
48+
assertEquals(originalObject, readString);
49+
} catch (JsonProcessingException jpe) {
50+
throw new RuntimeException(jpe);
51+
}
52+
53+
}
3654
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.aerospike.mapper;
2+
3+
import org.junit.Test;
4+
5+
import com.aerospike.mapper.annotations.AerospikeEmbed;
6+
import com.aerospike.mapper.annotations.AerospikeKey;
7+
import com.aerospike.mapper.annotations.AerospikeRecord;
8+
import com.aerospike.mapper.tools.AeroMapper;
9+
10+
public class InheritenceTest extends AeroMapperBaseTest {
11+
@AerospikeRecord(namespace = "test", set = "A")
12+
public static class A {
13+
@AerospikeKey
14+
public int id;
15+
public String name;
16+
@AerospikeEmbed
17+
public B topB;
18+
@AerospikeEmbed
19+
public B cAsAb;
20+
@AerospikeEmbed
21+
public B dAsAb;
22+
}
23+
24+
@AerospikeRecord(namespace = "test", set = "B")
25+
public static class B {
26+
@AerospikeKey
27+
public int id;
28+
public String name;
29+
}
30+
31+
// Inherit namespace and set from B
32+
@AerospikeRecord
33+
public static class C extends B {
34+
public String otherName;
35+
}
36+
37+
// Map into a separate table
38+
@AerospikeRecord(namespace = "test", set = "D")
39+
public static class D extends B {
40+
public int age;
41+
}
42+
43+
@Test
44+
public void run() {
45+
AeroMapper mapper = new AeroMapper.Builder(client).build();
46+
A a = new A();
47+
a.id = 1;
48+
a.name = "test";
49+
50+
B b = new B();
51+
b.id = 2;
52+
b.name = "b";
53+
54+
C c = new C();
55+
c.id = 3;
56+
c.name = "c";
57+
c.otherName = "maquerading as a B";
58+
59+
D d = new D();
60+
d.id = 4;
61+
d.name = "d";
62+
d.age = 312;
63+
a.topB = b;
64+
a.cAsAb = c;
65+
a.dAsAb = d;
66+
67+
mapper.save(a,b,c,d);
68+
A a2 = mapper.read(A.class, a.id);
69+
compare(a, a2);
70+
}
71+
72+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import org.junit.Test;
1010

11+
import com.aerospike.mapper.annotations.AerospikeEmbed;
12+
import com.aerospike.mapper.annotations.AerospikeEmbed.EmbedType;
1113
import com.aerospike.mapper.annotations.AerospikeKey;
1214
import com.aerospike.mapper.annotations.AerospikeRecord;
1315
import com.aerospike.mapper.annotations.AerospikeReference;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.aerospike.mapper;
2+
3+
import org.junit.Test;
4+
5+
import com.aerospike.client.AerospikeClient;
6+
import com.aerospike.client.IAerospikeClient;
7+
import com.aerospike.mapper.annotations.AerospikeEmbed;
8+
import com.aerospike.mapper.annotations.AerospikeKey;
9+
import com.aerospike.mapper.annotations.AerospikeRecord;
10+
import com.aerospike.mapper.annotations.AerospikeEmbed.EmbedType;
11+
import com.aerospike.mapper.tools.AeroMapper;
12+
13+
public class ObjectReferencesTest {
14+
@AerospikeRecord(namespace = "test", set = "parent")
15+
public static class Parent {
16+
@AerospikeKey
17+
private int id;
18+
@AerospikeEmbed(type = EmbedType.LIST)
19+
private Child child;
20+
}
21+
22+
@AerospikeRecord(namespace = "test", set = "child")
23+
public static class Child {
24+
@AerospikeKey
25+
private String key;
26+
private int age;
27+
private String name;
28+
}
29+
30+
@Test
31+
public void test() {
32+
Child child = new Child();
33+
child.key = "child1";
34+
child.age = 17;
35+
child.name = "bob";
36+
37+
Parent parent = new Parent();
38+
parent.id = 1;
39+
parent.child = child;
40+
41+
IAerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
42+
AeroMapper mapper = new AeroMapper.Builder(client).build();
43+
mapper.save(parent);
44+
mapper.save(child);
45+
}
46+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public void runner() {
4141
AeroMapper mapper = new AeroMapper.Builder(client).build();
4242
B b = new B();
4343
b.id = 2;
44-
b.name = "test";
44+
b.name = "a B";
4545
mapper.save(b);
4646

4747
B b1 = new B();
48-
b1.id = 2;
49-
b1.name = "test";
48+
b1.id = 3;
49+
b1.name = "another B";
5050
mapper.save(b1);
5151

5252
A a = new A();
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.aerospike.mapper;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.Date;
7+
import java.util.List;
8+
9+
import org.junit.Test;
10+
11+
import com.aerospike.mapper.annotations.AerospikeEmbed;
12+
import com.aerospike.mapper.annotations.AerospikeEmbed.EmbedType;
13+
import com.aerospike.mapper.annotations.AerospikeKey;
14+
import com.aerospike.mapper.annotations.AerospikeRecord;
15+
import com.aerospike.mapper.tools.AeroMapper;
16+
import com.aerospike.mapper.tools.ReturnType;
17+
import com.aerospike.mapper.tools.VirtualList;
18+
19+
public class VirtualListExample extends AeroMapperBaseTest {
20+
21+
@AerospikeRecord(namespace = "test", set = "item")
22+
public static class Item {
23+
@AerospikeKey
24+
private int id;
25+
private Date due;
26+
private String desc;
27+
public Item(int id, Date due, String desc) {
28+
super();
29+
this.id = id;
30+
this.due = due;
31+
this.desc = desc;
32+
}
33+
34+
public Item() {
35+
}
36+
}
37+
38+
@AerospikeRecord(namespace = "test", set = "container")
39+
public static class Container {
40+
@AerospikeKey
41+
private int id;
42+
private String name;
43+
@AerospikeEmbed(type = EmbedType.MAP, elementType = EmbedType.LIST)
44+
private List<Item> items;
45+
46+
public Container() {
47+
this.items = new ArrayList<>();
48+
}
49+
}
50+
51+
@Test
52+
public void testListOfReferences() {
53+
Container container = new Container();
54+
container.id = 1;
55+
container.name = "container";
56+
57+
container.items.add(new Item(100, new Date(), "Item 1"));
58+
container.items.add(new Item(200, new Date(), "Item 2"));
59+
container.items.add(new Item(300, new Date(), "Item 3"));
60+
container.items.add(new Item(400, new Date(), "Item 4"));
61+
62+
AeroMapper mapper = new AeroMapper.Builder(client).build();
63+
mapper.save(container);
64+
65+
VirtualList<Item> list = mapper.asBackedList(container, "items", Item.class);
66+
// perform a single operation. NOTE: This does NOT change the backed item, just the database
67+
list.append(new Item(500, new Date(), "Item5"));
68+
69+
List<Item> results = (List<Item>) list.beginMultiOperation()
70+
.append(new Item(600, new Date(), "Item6"))
71+
.removeByKey(200)
72+
.getByKeyRange(100, 450)
73+
.end();
74+
75+
System.out.println(results.size());
76+
}
77+
78+
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,7 @@ public void test() {
158158

159159
AeroMapper mapper = new AeroMapper.Builder(client).build();
160160
mapper.save(collection);
161-
mapper.save(a);
162-
mapper.save(b);
163-
mapper.save(c);
164-
mapper.save(d);
165-
mapper.save(e);
166-
mapper.save(f);
167-
mapper.save(g);
168-
mapper.save(h);
169-
mapper.save(i);
170-
mapper.save(j);
161+
mapper.save(a,b,c,d,e,f,g,h,i,j);
171162

172163
VirtualList<B> list = mapper.asBackedList(collection, "elements", B.class);
173164
// list.append(new CollectionElement(103, "tom", 45678));

src/test/java/com/aerospike/mapper/example/ApplicationBase.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ protected Property createAndPopulateProperty1() {
9696

9797
protected Property createAndPopulateProperty2() {
9898
Property property = new Property(11567724, new Address("333 Bob Jones Street", null, "Seattle", "WA", "23152"), 1955, "Brick");
99+
property
100+
.addValuation(new Valuation(new Date(), 550000, 4000, "Don's Valuers", 56367, new Address("67 Dartmouth Cl", null, "Chicago", "IL", "43252")))
101+
.addValuation(new Valuation(new Date(), 563000, 3000, "Tim's Valuers", 34245, null));
99102
return property;
100103
}
101104

@@ -107,7 +110,7 @@ protected Property createAndPopulateProperty3() {
107110
protected Property createAndPopulateProperty4() {
108111
Property property = new Property(8898, new Address("203 S Aneky St", null, "Denver", "CO", "82222-4321"), 1965, "Brick");
109112
property.addValuation(new Valuation(new Date(), 240000, 5000, "Steve's Valuers", 5647328, null))
110-
.addValuation(new Valuation(new Date(), 220000, 7000, "John's Valuers", 65462, new Address("67 Dartmouth Cl", null, "Smithfield", "IL", "43252")))
113+
.addValuation(new Valuation(new Date(), 220000, 7000, "John's Valuers", 65462, new Address("88 Dartmouth Cl", null, "Smithfield", "IL", "43252")))
111114
.addValuation(new Valuation(new Date(), 255000, 3000, "Wilma's Valuers", 254665, null));
112115

113116
return property;

0 commit comments

Comments
 (0)