Skip to content

Commit 915578e

Browse files
committed
Example application changes and documentation enhancements
1 parent dbabfea commit 915578e

File tree

15 files changed

+628
-49
lines changed

15 files changed

+628
-49
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1742,4 +1742,4 @@ When mapping a Java object to Aerospike the most common operations to do are to
17421742
- Ensure batch loading option exists in AerospikeReference Configuration
17431743
- handle object graph circularities (A->B->C). Be careful of: A->B(Lazy), A->C->B: B should end up fully hydrated in both instances, not lazy in both instances
17441744
- Consider the items on virtual list which return a list to be able to return a map as well (ELEMENT_LIST, ELEMENT_MAP)
1745-
- Test if map supports lazy loading of referenced objects.
1745+
- Test a constructor which requires a sub-object. For example, Account has a Property, Property has an Address. All 3 a referenced objects. Constructor for Property requires Address

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,12 @@ private void findConstructor() {
366366
this.constructorParamBins = new String[params.length];
367367
this.constructorParamDefaults = new Object[params.length];
368368

369+
Map<String, ValueType> allValues = new HashMap<>();
370+
ClassCacheEntry<?> current = this;
371+
while (current != null) {
372+
allValues.putAll(current.values);
373+
current = current.superClazz;
374+
}
369375
int count = 0;
370376
for (Parameter thisParam : params) {
371377
count++;
@@ -377,13 +383,13 @@ private void findConstructor() {
377383
String binName = parameterDetails.value();
378384

379385
// Validate that we have such a value
380-
if (!values.containsKey(binName)) {
386+
if (!allValues.containsKey(binName)) {
381387
String valueList = String.join(",", values.keySet());
382388
throw new AerospikeException("Class " + clazz.getSimpleName() + " has a preferred constructor of " + desiredConstructor.toString()+ ". However, parameter " + count +
383389
" is mapped to bin \"" + binName + "\" which is not one of the values on the class, which are: " + valueList);
384390
}
385391
Class<?> type = thisParam.getType();
386-
if (!type.isAssignableFrom(values.get(binName).getType())) {
392+
if (!type.isAssignableFrom(allValues.get(binName).getType())) {
387393
throw new AerospikeException("Class " + clazz.getSimpleName() + " has a preferred constructor of " + desiredConstructor.toString()+ ". However, parameter " + count +
388394
" is of type " + type + " but assigned from bin \"" + binName + "\" of type " +values.get(binName).getType()+ ". These types are incompatible.");
389395
}
Lines changed: 76 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.aerospike.mapper.example;
22

3-
import java.util.Date;
4-
import java.util.concurrent.TimeUnit;
3+
import static org.junit.Assert.assertEquals;
54

65
import org.junit.Test;
76

@@ -12,18 +11,24 @@
1211
import com.aerospike.client.policy.Policy;
1312
import com.aerospike.client.policy.Replica;
1413
import com.aerospike.client.policy.WritePolicy;
15-
import com.aerospike.mapper.example.model.Account;
1614
import com.aerospike.mapper.example.model.Address;
1715
import com.aerospike.mapper.example.model.Branch;
1816
import com.aerospike.mapper.example.model.Checkbook;
1917
import com.aerospike.mapper.example.model.Customer;
18+
import com.aerospike.mapper.example.model.Property;
2019
import com.aerospike.mapper.example.model.Transaction;
20+
import com.aerospike.mapper.example.model.accounts.Account;
21+
import com.aerospike.mapper.example.model.accounts.LoanAccount;
22+
import com.aerospike.mapper.example.model.accounts.PortfolioAccount;
2123
import com.aerospike.mapper.tools.AeroMapper;
24+
import com.fasterxml.jackson.core.JsonProcessingException;
25+
import com.fasterxml.jackson.databind.ObjectMapper;
26+
import com.fasterxml.jackson.databind.ObjectWriter;
2227

23-
public class Application {
28+
public class Application extends ApplicationBase {
2429

2530
@Test
26-
public void run() {
31+
public void run() throws JsonProcessingException {
2732
Policy readPolicy = new Policy();
2833
readPolicy.maxRetries = 4;
2934
readPolicy.replica = Replica.SEQUENCE;
@@ -46,56 +51,89 @@ public void run() {
4651

4752
AeroMapper mapper = new AeroMapper.Builder(client)
4853
.withWritePolicy(writePolicy).forAll()
49-
.withReadPolicy(txnReadPolicy).forAll()
54+
.withReadPolicy(readPolicy).forAll()
5055
.withReadPolicy(txnReadPolicy).forClasses(Transaction.class)
5156
.build();
5257

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");
58+
Address mailingAddress = new Address("773 Elm St", "Apt 2", "Grand Junction", "CO", "83451");
59+
Address billingAddress = new Address("123 Main St", null, "Denver", "CO", "80001");
60+
Address alternateAddress = new Address("1 Park Road", null, "Miami", "FL", "98531");
5661

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");
62+
Customer customer = createAndPopulateCustomer();
6063
customer.setMailingAddress(mailingAddress);
6164

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());
65+
/* Create a checking account for this customer */
66+
Account checkingAccount = createAndPopulateChecking(customer.getCustomerId());
67+
checkingAccount.setBillingAddress(billingAddress);
68+
checkingAccount.getAlternateAddresses().add(mailingAddress);
69+
checkingAccount.getAlternateAddresses().add(alternateAddress);
7370

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);
71+
Branch issuingBranch = new Branch("Br123", new Address("77 Park Road", null, "Miami", "FL", "98531"), "Miami Central");
72+
Checkbook checkbook = createAndPopulateCheckbook1(checkingAccount.getAccountId(), issuingBranch);
73+
checkingAccount.getCheckbooks().put(1, checkbook);
7974

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);
75+
Branch otherIssuingBranch = new Branch("Br567", new Address("129 Bump Drive", null, "New York", "NY", "77777"), "New York City Office");
76+
Checkbook checkbook2 = createAndPopulateCheckbook2(checkingAccount.getAccountId(), otherIssuingBranch);
77+
checkingAccount.getCheckbooks().put(2, checkbook2);
8578

86-
customer.getAccounts().add(testAccount);
79+
mapper.save(checkingAccount);
80+
customer.getAccounts().add(checkingAccount);
81+
82+
/* Create a savings account for this customer */
83+
Account savingsAccount = createAndPopulateSavingsAccount(customer.getCustomerId());
84+
savingsAccount.setBillingAddress(billingAddress);
85+
savingsAccount.getAlternateAddresses().add(mailingAddress);
86+
87+
mapper.save(savingsAccount);
88+
customer.getAccounts().add(savingsAccount);
89+
90+
/* Create a porfolio account for this customer */
91+
Property property1 = createAndPopulateProperty1();
92+
Property property2 = createAndPopulateProperty2();
93+
Property property3 = createAndPopulateProperty3();
94+
mapper.save(property1);
95+
mapper.save(property2);
96+
mapper.save(property3);
97+
98+
PortfolioAccount portfolioAccount = createAndPopulatePortfolioAccount(customer.getCustomerId());
99+
portfolioAccount.setBillingAddress(billingAddress);
100+
portfolioAccount.getAlternateAddresses().add(mailingAddress);
101+
portfolioAccount.addPropertyToPortfolio(property1, 0.0239f);
102+
portfolioAccount.addPropertyToPortfolio(property2, 0.0299f);
103+
portfolioAccount.addPropertyToPortfolio(property3, 0.0319f);
104+
105+
mapper.save(portfolioAccount);
106+
customer.getAccounts().add(portfolioAccount);
107+
108+
/* Create a loan account for this customer */
87109

110+
LoanAccount loanAccount = createAndPopulateLoanAccount(customer.getCustomerId());
111+
loanAccount.setBillingAddress(billingAddress);
112+
loanAccount.getAlternateAddresses().add(mailingAddress);
113+
customer.getAccounts().add(loanAccount);
114+
115+
Property securityProperty = createAndPopulateProperty4();
116+
loanAccount.setSecurityProperty(securityProperty);
117+
118+
mapper.save(securityProperty);
119+
mapper.save(loanAccount);
120+
88121
mapper.save(issuingBranch);
89122
mapper.save(otherIssuingBranch);
90-
mapper.save(testAccount);
91123
mapper.save(customer);
92124
mapper.save(checkbook);
93125
mapper.save(checkbook2);
94126

127+
Customer readCustomer = null;
95128
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);
129+
long now = System.nanoTime();
130+
readCustomer = mapper.read(Customer.class, customer.getCustomerId());
131+
System.out.println(String.format("Customer graph read time: %.3fms", (System.nanoTime() - now)/1000000f));
99132
}
133+
ObjectWriter objectWriter = new ObjectMapper().writerWithDefaultPrettyPrinter();
134+
String readString = objectWriter.writeValueAsString(readCustomer);
135+
System.out.println(readString);
136+
String originalObject = objectWriter.writeValueAsString(customer);
137+
assertEquals(originalObject, readString);
100138
}
101139
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.aerospike.mapper.example;
2+
3+
import java.util.Date;
4+
import java.util.concurrent.TimeUnit;
5+
6+
import com.aerospike.mapper.example.model.Address;
7+
import com.aerospike.mapper.example.model.Branch;
8+
import com.aerospike.mapper.example.model.Checkbook;
9+
import com.aerospike.mapper.example.model.Customer;
10+
import com.aerospike.mapper.example.model.InterestType;
11+
import com.aerospike.mapper.example.model.Property;
12+
import com.aerospike.mapper.example.model.Valuation;
13+
import com.aerospike.mapper.example.model.accounts.Account;
14+
import com.aerospike.mapper.example.model.accounts.AccountType;
15+
import com.aerospike.mapper.example.model.accounts.LoanAccount;
16+
import com.aerospike.mapper.example.model.accounts.PortfolioAccount;
17+
18+
public class ApplicationBase {
19+
protected Customer createAndPopulateCustomer() {
20+
Customer customer = new Customer("cust1", "Robert", "Smith");
21+
customer.setDateOfBirth(new Date(new Date().getTime() - TimeUnit.MILLISECONDS.convert(30*365, TimeUnit.DAYS)));
22+
customer.setPhone("(555)555-1234");
23+
customer.setPreferredSaluation("Bobby");
24+
customer.setJoinedBank(new Date());
25+
customer.setVip(true);
26+
return customer;
27+
}
28+
29+
protected Account createAndPopulateChecking(String customerId) {
30+
Account checkingAccount = new Account("ACC-1234", customerId, "Checking Account", AccountType.CHECKING);
31+
checkingAccount.setBalance(100000);
32+
checkingAccount.setCard(true);
33+
checkingAccount.setRouting("123456789");
34+
checkingAccount.setPaperless(true);
35+
checkingAccount.setOverdraftProtection(false);
36+
checkingAccount.setOnlineUserName("beesmith");
37+
checkingAccount.setLastLogin(new Date());
38+
return checkingAccount;
39+
}
40+
41+
protected Checkbook createAndPopulateCheckbook1(String accountId, Branch issuingBranch) {
42+
Checkbook checkbook = new Checkbook(accountId, 1, 100, new Date());
43+
checkbook.setIssuer(issuingBranch);
44+
checkbook.setRecalled(false);
45+
return checkbook;
46+
}
47+
48+
protected Checkbook createAndPopulateCheckbook2(String accountId, Branch issuingBranch) {
49+
Checkbook checkbook = new Checkbook(accountId, 101, 600, new Date());
50+
checkbook.setIssuer(issuingBranch);
51+
checkbook.setRecalled(false);
52+
return checkbook;
53+
}
54+
55+
protected Account createAndPopulateSavingsAccount(String customerId) {
56+
Account savingsAccount = new Account("SVG-999", customerId, "Savings Account", AccountType.SAVINGS);
57+
savingsAccount.setBalance(31415);
58+
savingsAccount.setCard(false);
59+
savingsAccount.setRouting("123456789");
60+
savingsAccount.setPaperless(false);
61+
savingsAccount.setOverdraftProtection(false);
62+
savingsAccount.setLastLogin(null);
63+
return savingsAccount;
64+
}
65+
66+
protected PortfolioAccount createAndPopulatePortfolioAccount(String customerId) {
67+
PortfolioAccount portfolioAccount = new PortfolioAccount("PFOLIO-12312", customerId, "Portfolio Account", AccountType.PORTFOLIO);
68+
portfolioAccount.setBalance(314992);
69+
portfolioAccount.setCard(false);
70+
portfolioAccount.setRouting("134756700");
71+
portfolioAccount.setPaperless(false);
72+
portfolioAccount.setOverdraftProtection(false);
73+
portfolioAccount.setLastLogin(null);
74+
portfolioAccount.setContratClausesExcluded(37,108,312,333);
75+
return portfolioAccount;
76+
}
77+
78+
protected LoanAccount createAndPopulateLoanAccount(String customerId) {
79+
Date originationDate = new Date(new Date().getTime() - TimeUnit.MILLISECONDS.convert(5*365, TimeUnit.DAYS));
80+
Date expirationDate = new Date(originationDate.getTime() + TimeUnit.MILLISECONDS.convert(30*365, TimeUnit.DAYS));
81+
LoanAccount loanAccount = new LoanAccount("LOAN-34672", customerId, "Loan Account", AccountType.LOAN, originationDate, expirationDate, 0.0399f);
82+
loanAccount.setBalance(31415);
83+
loanAccount.setCard(false);
84+
loanAccount.setRouting("123456789");
85+
loanAccount.setPaperless(false);
86+
loanAccount.setOverdraftProtection(false);
87+
loanAccount.setLastLogin(null);
88+
loanAccount.setInterestType(InterestType.PRINCIPAL_INTEREST);
89+
return loanAccount;
90+
}
91+
92+
protected Property createAndPopulateProperty1() {
93+
Property property = new Property(11567723, new Address("888 Yam Road", null, "Chicago", "IL", "55555"), 1977, "Stucco");
94+
return property;
95+
}
96+
97+
protected Property createAndPopulateProperty2() {
98+
Property property = new Property(11567724, new Address("333 Bob Jones Street", null, "Seattle", "WA", "23152"), 1955, "Brick");
99+
return property;
100+
}
101+
102+
protected Property createAndPopulateProperty3() {
103+
Property property = new Property(11567725, new Address("14003 Des Moines Blvd", null, "Austin", "TX", "78123"), 2004, "Wood");
104+
return property;
105+
}
106+
107+
protected Property createAndPopulateProperty4() {
108+
Property property = new Property(8898, new Address("203 S Aneky St", null, "Denver", "CO", "82222-4321"), 1965, "Brick");
109+
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")))
111+
.addValuation(new Valuation(new Date(), 255000, 3000, "Wilma's Valuers", 254665, null));
112+
113+
return property;
114+
}
115+
116+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.aerospike.mapper.example;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import com.aerospike.client.DebugAerospikeClient;
8+
import com.aerospike.client.DebugAerospikeClient.Granularity;
9+
import com.aerospike.client.DebugAerospikeClient.Options;
10+
import com.aerospike.client.IAerospikeClient;
11+
import com.aerospike.mapper.example.model.Customer;
12+
import com.aerospike.mapper.tools.AeroMapper;
13+
import com.fasterxml.jackson.core.JsonProcessingException;
14+
import com.fasterxml.jackson.databind.ObjectMapper;
15+
import com.fasterxml.jackson.databind.ObjectWriter;
16+
17+
public class JavaMapperApplication extends ApplicationBase {
18+
@Test
19+
public void run() throws JsonProcessingException {
20+
IAerospikeClient client = new DebugAerospikeClient(null, "127.0.0.1", 3000, new Options(Granularity.EVERY_CALL));
21+
22+
AeroMapper mapper = new AeroMapper.Builder(client).build();
23+
Customer customer = createAndPopulateCustomer();
24+
25+
mapper.save(customer);
26+
Customer customer2 = null;
27+
for (int i = 0; i < 100; i++) {
28+
long now = System.nanoTime();
29+
customer2 = mapper.read(Customer.class, customer.getCustomerId());
30+
System.out.println(String.format("Customer graph read time: %.3fms", (System.nanoTime() - now)/1000000f));
31+
}
32+
33+
ObjectWriter objectWriter = new ObjectMapper().writerWithDefaultPrettyPrinter();
34+
String readString = objectWriter.writeValueAsString(customer2);
35+
System.out.println(readString);
36+
String originalObject = objectWriter.writeValueAsString(customer);
37+
assertEquals(originalObject, readString);
38+
39+
client.close();
40+
}
41+
42+
}

0 commit comments

Comments
 (0)