Skip to content

Commit 90955dc

Browse files
committed
Began complete example. Updated documentation. Map deferred loading of
map elements work.
1 parent a1651a6 commit 90955dc

File tree

9 files changed

+630
-2
lines changed

9 files changed

+630
-2
lines changed

src/main/java/com/aerospike/mapper/tools/mappers/MapMapper.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import java.util.TreeMap;
55

66
import com.aerospike.mapper.tools.AeroMapper;
7+
import com.aerospike.mapper.tools.DeferredObjectLoader;
8+
import com.aerospike.mapper.tools.DeferredObjectLoader.DeferredObject;
9+
import com.aerospike.mapper.tools.DeferredObjectLoader.DeferredObjectSetter;
10+
import com.aerospike.mapper.tools.DeferredObjectLoader.DeferredSetter;
711
import com.aerospike.mapper.tools.TypeMapper;
812
import com.aerospike.mapper.tools.TypeUtils;
913
import com.aerospike.mapper.tools.TypeUtils.AnnotatedType;
@@ -63,13 +67,30 @@ public Object fromAerospikeFormat(Object value) {
6367
return value;
6468
}
6569

66-
Map<Object, Object> results = new TreeMap<>();
70+
final Map<Object, Object> results = new TreeMap<>();
6771
for (Object key : map.keySet()) {
6872
Object item = map.get(key);
6973

7074
TypeMapper keyMap = keyMapper != null ? keyMapper : TypeUtils.getMapper(key.getClass(), AnnotatedType.getDefaultAnnotateType(), mapper);
7175
TypeMapper itemMap = itemMapper != null ? itemMapper : TypeUtils.getMapper(item.getClass(), AnnotatedType.getDefaultAnnotateType(), mapper);
72-
results.put(keyMap.fromAerospikeFormat(key), itemMap.fromAerospikeFormat(item));
76+
// results.put(keyMap.fromAerospikeFormat(key), itemMap.fromAerospikeFormat(item));
77+
78+
79+
final Object javaKey = keyMap == null ? null : keyMap.fromAerospikeFormat(key);
80+
final Object javaItem = itemMap == null ? null : itemMap.fromAerospikeFormat(item);
81+
if (javaKey instanceof DeferredObject || javaItem instanceof DeferredObject) {
82+
DeferredSetter setter = new DeferredSetter() {
83+
@Override
84+
public void setValue(Object object) {
85+
results.put(javaKey, object);
86+
}
87+
};
88+
DeferredObjectSetter objectSetter = new DeferredObjectSetter(setter, (DeferredObject)javaItem);
89+
DeferredObjectLoader.add(objectSetter);
90+
}
91+
else {
92+
results.put(javaKey, javaItem);
93+
}
7394
}
7495
return results;
7596
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.aerospike.mapper;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import org.junit.Test;
9+
10+
import com.aerospike.mapper.annotations.AerospikeKey;
11+
import com.aerospike.mapper.annotations.AerospikeRecord;
12+
import com.aerospike.mapper.tools.AeroMapper;
13+
14+
public class MapTest extends AeroMapperBaseTest {
15+
@AerospikeRecord(namespace = "test", set = "A")
16+
public static class A {
17+
public String name;
18+
public int age;
19+
@AerospikeKey
20+
public int id;
21+
22+
A() {}
23+
24+
public A(String name, int age, int id) {
25+
super();
26+
this.name = name;
27+
this.id = id;
28+
}
29+
}
30+
31+
@AerospikeRecord(namespace = "test", set = "B")
32+
public static class B {
33+
@AerospikeKey
34+
public int id;
35+
public Map<Integer, A> batchAs;
36+
}
37+
38+
@Test
39+
public void runTest() {
40+
A a1 = new A("a", 10, 1);
41+
A a2 = new A("b", 20, 2);
42+
A a3 = new A("c", 30, 3);
43+
A a4 = new A("d", 40, 4);
44+
A a5 = new A("e", 50, 5);
45+
A a6 = new A("f", 60, 6);
46+
47+
B b = new B();
48+
b.batchAs = new HashMap<Integer, A>();
49+
b.id = 1;
50+
51+
b.batchAs.put(10, a1);
52+
b.batchAs.put(11, a2);
53+
b.batchAs.put(12, a3);
54+
b.batchAs.put(13, a4);
55+
b.batchAs.put(14, a5);
56+
b.batchAs.put(15, a6);
57+
58+
AeroMapper mapper = new AeroMapper.Builder(client).build();
59+
mapper.save(b);
60+
mapper.save(a1);
61+
mapper.save(a2);
62+
mapper.save(a3);
63+
mapper.save(a4);
64+
mapper.save(a5);
65+
mapper.save(a6);
66+
67+
B b2 = mapper.read(B.class, 1);
68+
69+
assertEquals(b.id, b2.id);
70+
assertEquals(b.batchAs.size(), b2.batchAs.size());
71+
for (int i = 10; i <= 15; i++) {
72+
assertEquals(b.batchAs.get(i).age, b2.batchAs.get(i).age);
73+
assertEquals(b.batchAs.get(i).id, b2.batchAs.get(i).id);
74+
assertEquals(b.batchAs.get(i).name, b2.batchAs.get(i).name);
75+
}
76+
}
77+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.aerospike.mapper.example;
2+
3+
import java.util.Date;
4+
import java.util.concurrent.TimeUnit;
5+
6+
import org.junit.Test;
7+
8+
import com.aerospike.client.DebugAerospikeClient;
9+
import com.aerospike.client.DebugAerospikeClient.Granularity;
10+
import com.aerospike.client.DebugAerospikeClient.Options;
11+
import com.aerospike.client.IAerospikeClient;
12+
import com.aerospike.client.policy.Policy;
13+
import com.aerospike.client.policy.Replica;
14+
import com.aerospike.client.policy.WritePolicy;
15+
import com.aerospike.mapper.example.model.Account;
16+
import com.aerospike.mapper.example.model.Address;
17+
import com.aerospike.mapper.example.model.Branch;
18+
import com.aerospike.mapper.example.model.Checkbook;
19+
import com.aerospike.mapper.example.model.Customer;
20+
import com.aerospike.mapper.example.model.Transaction;
21+
import com.aerospike.mapper.tools.AeroMapper;
22+
23+
public class Application {
24+
25+
@Test
26+
public void run() {
27+
Policy readPolicy = new Policy();
28+
readPolicy.maxRetries = 4;
29+
readPolicy.replica = Replica.SEQUENCE;
30+
readPolicy.sleepBetweenRetries = 50;
31+
readPolicy.socketTimeout = 250;
32+
readPolicy.totalTimeout = 2000;
33+
34+
Policy txnReadPolicy = new Policy();
35+
txnReadPolicy.maxRetries = 3;
36+
txnReadPolicy.replica = Replica.SEQUENCE;
37+
txnReadPolicy.sleepBetweenRetries = 10;
38+
txnReadPolicy.socketTimeout = 50;
39+
txnReadPolicy.totalTimeout = 250;
40+
41+
WritePolicy writePolicy = new WritePolicy();
42+
writePolicy.durableDelete = true;
43+
writePolicy.sendKey = true; // For ease of debugging for now
44+
45+
IAerospikeClient client = new DebugAerospikeClient(null, "127.0.0.1", 3000, new Options(Granularity.EVERY_CALL));
46+
47+
AeroMapper mapper = new AeroMapper.Builder(client)
48+
.withWritePolicy(writePolicy).forAll()
49+
.withReadPolicy(txnReadPolicy).forAll()
50+
.withReadPolicy(txnReadPolicy).forClasses(Transaction.class)
51+
.build();
52+
53+
Address mailingAddress = new Address("773 Elm St", "Apt 2", "Grand Junction", new char[] {'C', 'O'}, "83451");
54+
Address billingAddress = new Address("123 Main St", null, "Denver", new char[] {'C', 'O'}, "80001");
55+
Address alternateAddress = new Address("1 Park Road", null, "Miami", new char[] {'F', 'L'}, "98531");
56+
57+
Customer customer = new Customer("cust1", "Bob", "Smith");
58+
customer.setDateOfBirth(new Date(new Date().getTime() - TimeUnit.MILLISECONDS.convert(30*365, TimeUnit.DAYS)));
59+
customer.setPhone("(555)555-1234");
60+
customer.setMailingAddress(mailingAddress);
61+
62+
Account testAccount = new Account("ACC-1234", customer.getCustomerId(), "Test Account");
63+
testAccount.setBalance(100000);
64+
testAccount.setCard(true);
65+
testAccount.setBillingAddress(billingAddress);
66+
testAccount.getAlternateAddresses().add(mailingAddress);
67+
testAccount.getAlternateAddresses().add(alternateAddress);
68+
testAccount.setRouting("123456789");
69+
testAccount.setPaperless(true);
70+
testAccount.setOverdraftProtection(false);
71+
testAccount.setOnlineUserName("beesmith");
72+
testAccount.setLastLogin(new Date());
73+
74+
Branch issuingBranch = new Branch("Br123", new Address("77 Park Road", null, "Miami", new char[] {'F', 'L'}, "98531"), "Miami Central");
75+
Checkbook checkbook = new Checkbook(testAccount.getAccountId(), 1, 100, new Date());
76+
checkbook.setIssuer(issuingBranch);
77+
checkbook.setRecalled(false);
78+
testAccount.getCheckbooks().put(1, checkbook);
79+
80+
Branch otherIssuingBranch = new Branch("Br567", new Address("129 Bump Drive", null, "New York", new char[] {'N', 'Y'}, "77777"), "New York City Office");
81+
Checkbook checkbook2 = new Checkbook(testAccount.getAccountId(), 101, 600, new Date());
82+
checkbook2.setIssuer(otherIssuingBranch);
83+
checkbook2.setRecalled(false);
84+
testAccount.getCheckbooks().put(2, checkbook2);
85+
86+
customer.getAccounts().add(testAccount);
87+
88+
mapper.save(issuingBranch);
89+
mapper.save(otherIssuingBranch);
90+
mapper.save(testAccount);
91+
mapper.save(customer);
92+
mapper.save(checkbook);
93+
mapper.save(checkbook2);
94+
95+
for (int i = 0; i < 100; i++) {
96+
long now =System.nanoTime();
97+
Customer readCustomer = mapper.read(Customer.class, customer.getCustomerId());
98+
System.out.println((System.nanoTime() - now)/1000);
99+
}
100+
}
101+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package com.aerospike.mapper.example.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.Date;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import com.aerospike.mapper.annotations.AerospikeBin;
10+
import com.aerospike.mapper.annotations.AerospikeEmbed;
11+
import com.aerospike.mapper.annotations.AerospikeKey;
12+
import com.aerospike.mapper.annotations.AerospikeRecord;
13+
import com.aerospike.mapper.annotations.ParamFrom;
14+
15+
@AerospikeRecord(namespace = "test", set = "account")
16+
public class Account {
17+
@AerospikeKey
18+
@AerospikeBin(name = "id")
19+
private final String accountId;
20+
private final String title;
21+
22+
@AerospikeBin(name = "custId")
23+
private final String customerId;
24+
25+
@AerospikeEmbed
26+
@AerospikeBin(name = "bill")
27+
private Address billingAddress;
28+
29+
@AerospikeEmbed
30+
@AerospikeBin(name = "mail")
31+
private Address mailingAddress;
32+
33+
@AerospikeBin(name = "alt")
34+
@AerospikeEmbed
35+
private List<Address> alternateAddresses;
36+
37+
private long balance;
38+
private String routing;
39+
@AerospikeBin(name = "odProt")
40+
private boolean overdraftProtection;
41+
private boolean card;
42+
43+
private boolean paperless;
44+
@AerospikeBin(name = "chkBk")
45+
private Map<Integer, Checkbook> checkbooks;
46+
@AerospikeBin(name = "usr")
47+
private String onlineUserName;
48+
@AerospikeBin(name = "lstLgn")
49+
private Date lastLogin;
50+
51+
public Account(@ParamFrom("id") String accountId, @ParamFrom("custId") String customerId, @ParamFrom("title") String title) {
52+
super();
53+
this.accountId = accountId;
54+
this.title = title;
55+
this.customerId = customerId;
56+
57+
alternateAddresses = new ArrayList<>();
58+
checkbooks = new HashMap<>();
59+
}
60+
61+
public Address getBillingAddress() {
62+
return billingAddress;
63+
}
64+
public void setBillingAddress(Address billingAddress) {
65+
this.billingAddress = billingAddress;
66+
}
67+
public List<Address> getAlternateAddresses() {
68+
return alternateAddresses;
69+
}
70+
public void setAlternateAddresses(List<Address> alternateAddresses) {
71+
this.alternateAddresses = alternateAddresses;
72+
}
73+
public long getBalance() {
74+
return balance;
75+
}
76+
public void setBalance(long balance) {
77+
this.balance = balance;
78+
}
79+
public String getRouting() {
80+
return routing;
81+
}
82+
public void setRouting(String routing) {
83+
this.routing = routing;
84+
}
85+
public boolean isOverdraftProtection() {
86+
return overdraftProtection;
87+
}
88+
public void setOverdraftProtection(boolean overdraftProtection) {
89+
this.overdraftProtection = overdraftProtection;
90+
}
91+
public boolean isCard() {
92+
return card;
93+
}
94+
public void setCard(boolean card) {
95+
this.card = card;
96+
}
97+
public boolean isPaperless() {
98+
return paperless;
99+
}
100+
public void setPaperless(boolean paperless) {
101+
this.paperless = paperless;
102+
}
103+
public Map<Integer, Checkbook> getCheckbooks() {
104+
return checkbooks;
105+
}
106+
public void setCheckbooks(Map<Integer, Checkbook> checkbooks) {
107+
this.checkbooks = checkbooks;
108+
}
109+
public String getAccountId() {
110+
return accountId;
111+
}
112+
public String getTitle() {
113+
return title;
114+
}
115+
116+
public String getOnlineUserName() {
117+
return onlineUserName;
118+
}
119+
120+
public void setOnlineUserName(String onlineUserName) {
121+
this.onlineUserName = onlineUserName;
122+
}
123+
124+
public Date getLastLogin() {
125+
return lastLogin;
126+
}
127+
128+
public void setLastLogin(Date lastLogin) {
129+
this.lastLogin = lastLogin;
130+
}
131+
132+
public Address getMailingAddress() {
133+
return mailingAddress;
134+
}
135+
136+
public void setMailingAddress(Address mailingAddress) {
137+
this.mailingAddress = mailingAddress;
138+
}
139+
140+
141+
}

0 commit comments

Comments
 (0)