Skip to content

Commit 7c9ee26

Browse files
committed
added a second level object and did some cleanup
1 parent e232c94 commit 7c9ee26

File tree

3 files changed

+96
-17
lines changed

3 files changed

+96
-17
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.sharrissf.sample;
2+
3+
public class Address {
4+
private String street;
5+
private String state;
6+
private String city;
7+
8+
public Address(String street, String city, String state) {
9+
this.setStreet(street);
10+
this.setState(state);
11+
this.setCity(city);
12+
}
13+
14+
public void setCity(String city) {
15+
this.city = city;
16+
}
17+
18+
public String getCity() {
19+
return city;
20+
}
21+
22+
public void setState(String state) {
23+
this.state = state;
24+
}
25+
26+
public String getState() {
27+
return state;
28+
}
29+
30+
public void setStreet(String street) {
31+
this.street = street;
32+
}
33+
34+
public String getStreet() {
35+
return street;
36+
}
37+
38+
}

src/main/java/org/sharrissf/sample/EhcacheSearchPlaying.java

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.io.IOException;
44

5-
import net.sf.ehcache.Cache;
65
import net.sf.ehcache.CacheManager;
6+
import net.sf.ehcache.Ehcache;
77
import net.sf.ehcache.Element;
88
import net.sf.ehcache.config.CacheConfiguration;
99
import net.sf.ehcache.config.Configuration;
@@ -12,7 +12,9 @@
1212
import net.sf.ehcache.search.Query;
1313
import net.sf.ehcache.search.Results;
1414
import net.sf.ehcache.search.aggregator.Average;
15+
import net.sf.ehcache.search.aggregator.Count;
1516
import net.sf.ehcache.search.attribute.AttributeExtractor;
17+
import net.sf.ehcache.search.expression.And;
1618

1719
import org.sharrissf.sample.Person.Gender;
1820

@@ -25,7 +27,7 @@
2527
*/
2628
public class EhcacheSearchPlaying {
2729
private CacheManager cacheManager;
28-
private Cache cache;
30+
private Ehcache cache;
2931

3032
public EhcacheSearchPlaying() {
3133
initializeCache();
@@ -48,7 +50,6 @@ private void initializeCache() {
4850

4951
sa = new SearchAttribute();
5052
sa.className("org.sharrissf.sample.EhcacheSearchPlaying$NameAttributeExtractor");
51-
5253
sa.setName("name");
5354
cacheConfig.addSearchAttribute(sa);
5455

@@ -57,46 +58,72 @@ private void initializeCache() {
5758
sa.setName("gender");
5859
cacheConfig.addSearchAttribute(sa);
5960

61+
sa = new SearchAttribute();
62+
sa.setExpression("value.getAddress().getState()");
63+
sa.setName("state");
64+
cacheConfig.addSearchAttribute(sa);
65+
6066
cacheManagerConfig.addCache(cacheConfig);
6167

6268
cacheManager = new CacheManager(cacheManagerConfig);
63-
cache = cacheManager.getCache("test");
69+
cache = cacheManager.getEhcache("test");
6470
}
6571

6672
public void runTests() throws IOException {
67-
cache.put(new Element(1, new Person("Tim Eck", 35, Gender.MALE)));
68-
cache.put(new Element(2, new Person("Pamela Jones", 23, Gender.FEMALE)));
69-
cache.put(new Element(3, new Person("Ari Zilka", 25, Gender.MALE)));
70-
cache.put(new Element(4, new Person("Ari gold", 45, Gender.MALE)));
71-
cache.put(new Element(5, new Person("Nabib El-Rahman", 30, Gender.MALE)));
72-
for (int i = 5; i < 1000; i++) {
73-
cache.put(new Element(i, new Person("Nabib El-Rahman" + i, 30, Gender.MALE)));
74-
}
73+
loadCache();
7574

7675
Attribute<Integer> age = cache.getSearchAttribute("age");
7776
Attribute<Gender> gender = cache.getSearchAttribute("gender");
7877
Attribute<String> name = cache.getSearchAttribute("name");
78+
Attribute<String> state = cache.getSearchAttribute("state");
7979

8080
Query query = cache.createQuery();
8181
query.includeKeys();
82-
query.add(name.like("Ari*"));
82+
query.add(new And(name.like("Ari*"), gender.eq(Gender.MALE)));
8383

8484
long t = System.currentTimeMillis();
85+
System.out.println("Searching for all Person's who's name start with Ari and are Male:");
86+
8587
Results results = query.execute();
8688
System.out.println("Took: " + (System.currentTimeMillis() - t) + " Size: " + results.size());
8789

8890
read();
8991

90-
cache.put(new Element(1, new Person("Tim Eck", 36, Gender.MALE)));
92+
System.out.println("Adding another Ari");
9193

92-
t = System.currentTimeMillis();
94+
cache.put(new Element(1, new Person("Ari Eck", 36, Gender.MALE, "eck street", "San Mateo", "CA")));
9395

96+
t = System.currentTimeMillis();
97+
System.out.println("Again Searching for all Person's who's name start with Ari and are Male:");
9498
results = query.execute();
9599
System.out.println("Took: " + (System.currentTimeMillis() - t) + " Size: " + results.size());
96100

101+
read();
102+
103+
System.out.println("Find the average age of all the entries in the cache");
104+
97105
Query averageAgeQuery = cache.createQuery();
98106
averageAgeQuery.includeAggregator(new Average(), age);
99107
System.out.println("Average age: " + averageAgeQuery.execute().aggregateResult());
108+
109+
read();
110+
111+
System.out.println("Find the count of people from NJ");
112+
113+
Query newJerseyCountQuery = cache.createQuery().add(state.eq("NJ"));
114+
newJerseyCountQuery.includeAggregator(new Count(), state);
115+
System.out.println("Count of people from NJ: " + newJerseyCountQuery.execute().aggregateResult());
116+
}
117+
118+
private void loadCache() {
119+
cache.put(new Element(1, new Person("Tim Eck", 35, Gender.MALE, "eck street", "San Mateo", "CA")));
120+
cache.put(new Element(2, new Person("Pamela Jones", 23, Gender.FEMALE, "berry st", "Parsippany", "LA")));
121+
cache.put(new Element(3, new Person("Ari Zilka", 25, Gender.MALE, "big wig", "Beverly Hills", "NJ")));
122+
cache.put(new Element(4, new Person("Ari gold", 45, Gender.MALE, "cool agent", "Madison", "WI")));
123+
cache.put(new Element(5, new Person("Nabib El-Rahman", 30, Gender.MALE, "dah man", "Bangladesh", "MN")));
124+
for (int i = 5; i < 1000; i++) {
125+
cache.put(new Element(i, new Person("Nabib El-Rahman" + i, 30, Gender.MALE, "dah man", "Bangladesh", "NJ")));
126+
}
100127
}
101128

102129
private static void read() throws IOException {
@@ -108,9 +135,13 @@ public static void main(String[] args) throws IOException {
108135
new EhcacheSearchPlaying().runTests();
109136
}
110137

111-
@SuppressWarnings( { "unused", "serial" })
112138
public static class NameAttributeExtractor implements AttributeExtractor {
113139

140+
/**
141+
*
142+
*/
143+
private static final long serialVersionUID = 1L;
144+
114145
@Override
115146
public Object attributeFor(Element element) {
116147
return ((Person) element.getValue()).getName();

src/main/java/org/sharrissf/sample/Person.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44

55
public class Person implements Serializable {
66

7+
/**
8+
* Simple Person class that can be searched on by the new Ehcache Search API
9+
*/
10+
private static final long serialVersionUID = 1L;
711
private final int age;
812
private final String name;
913
private final Gender gender;
14+
private final Address address;
1015

11-
public Person(String name, int age, Gender gender) {
16+
public Person(String name, int age, Gender gender, String street, String state, String zip) {
1217
this.name = name;
1318
this.age = age;
1419
this.gender = gender;
20+
this.address = new Address(street, state, zip);
1521
}
1622

1723
public int getAge() {
@@ -35,4 +41,8 @@ public String toString() {
3541
return getClass().getSimpleName() + "(name:" + name + ", age:" + age + ", sex:" + gender.name().toLowerCase() + ")";
3642
}
3743

44+
public Address getAddress() {
45+
return address;
46+
}
47+
3848
}

0 commit comments

Comments
 (0)