|
21 | 21 | import org.sharrissf.sample.Person.Gender;
|
22 | 22 |
|
23 | 23 | /**
|
24 |
| - * Sample app briefly showing some of the api's one can use to search in Ehcache. This has been written against the latest snapshot build so |
25 |
| - * it can become outdated at any time |
| 24 | + * Sample app briefly showing some of the api's one can use to search in |
| 25 | + * Ehcache. This has been written against the latest snapshot build so it can |
| 26 | + * become outdated at any time |
26 | 27 | *
|
27 | 28 | * @author steve
|
28 | 29 | *
|
29 | 30 | */
|
30 | 31 | public class EhcacheSearchPlaying {
|
31 |
| - private CacheManager cacheManager; |
32 |
| - private Ehcache cache; |
33 |
| - |
34 |
| - public EhcacheSearchPlaying() { |
35 |
| - initializeCache(); |
36 |
| - } |
37 |
| - |
38 |
| - private void initializeCache() { |
39 |
| - |
40 |
| - // Create Cache |
41 |
| - Configuration cacheManagerConfig = new Configuration(); |
42 |
| - cacheManagerConfig.addDefaultCache(new CacheConfiguration()); |
43 |
| - CacheConfiguration cacheConfig = new CacheConfiguration("test", -1).eternal(true); |
44 |
| - Searchable searchable = new Searchable(); |
45 |
| - cacheConfig.addSearchable(searchable); |
46 |
| - |
47 |
| - // Create attributes on the stuff we want to be able to search on. |
48 |
| - |
49 |
| - // You can use an expression for getting at the value to be indexed on a cache or you can code your own |
50 |
| - |
51 |
| - // Expressions |
52 |
| - searchable.addSearchAttribute(new SearchAttribute().name("age").expression("value.getAge()")); |
53 |
| - searchable.addSearchAttribute(new SearchAttribute().name("gender").expression("value.getGender()")); |
54 |
| - searchable.addSearchAttribute(new SearchAttribute().name("state").expression("value.getAddress().getState()")); |
55 |
| - |
56 |
| - // Coding your own |
57 |
| - searchable.addSearchAttribute(new SearchAttribute().name("name").className( |
58 |
| - "org.sharrissf.sample.EhcacheSearchPlaying$NameAttributeExtractor")); |
59 |
| - |
60 |
| - /* |
61 |
| - * If you want to initialize it via ehcache.xml it would look like this: |
62 |
| - * <cache name="test" maxElementsInMemory="0" eternal="true" overflowToDisk="false"> |
63 |
| - * <searchable> |
64 |
| - * <searchAttribute name="age" expression="value.getAge()"/> |
65 |
| - * <searchAttribute name="name" class="org.sharrissf.sample.EhcacheSearchPlaying$NameAttributeExtractor"/> |
66 |
| - * <searchAttribute name="gender" expression="value.getGender()"/> |
67 |
| - * <searchAttribute name="state" expression="value.getState()"/> |
68 |
| - * </searchable> |
69 |
| - * </cache> |
70 |
| - */ |
71 |
| - |
72 |
| - cacheManagerConfig.addCache(cacheConfig); |
73 |
| - |
74 |
| - cacheManager = new CacheManager(cacheManagerConfig); |
75 |
| - cache = cacheManager.getEhcache("test"); |
76 |
| - } |
77 |
| - |
78 |
| - public void runTests() throws IOException { |
79 |
| - loadCache(); |
80 |
| - |
81 |
| - Attribute<Integer> age = cache.getSearchAttribute("age"); |
82 |
| - Attribute<Gender> gender = cache.getSearchAttribute("gender"); |
83 |
| - Attribute<String> name = cache.getSearchAttribute("name"); |
84 |
| - Attribute<String> state = cache.getSearchAttribute("state"); |
85 |
| - |
86 |
| - Query query = cache.createQuery(); |
87 |
| - query.includeKeys(); |
88 |
| - query.add(new And(name.like("Ari*"), gender.eq(Gender.MALE))).addOrder(age, Direction.ASCENDING).maxResults(10); |
89 |
| - |
90 |
| - long t = System.currentTimeMillis(); |
91 |
| - System.out.println("Searching for all Person's who's name start with Ari and are Male:"); |
92 |
| - |
93 |
| - Results results = query.execute(); |
94 |
| - System.out.println("Took: " + (System.currentTimeMillis() - t) + " Size: " + results.size()); |
95 |
| - System.out.println("----Results-----"); |
96 |
| - for (Result result : results.all()) { |
97 |
| - System.out.println("Got: Key[" + result.getKey() + "] Value class [" + cache.get(result.getKey()).getValue() + "] Value [" |
98 |
| - + cache.get(result.getKey()).getValue() + "]"); |
99 |
| - } |
100 |
| - |
101 |
| - read(); |
102 |
| - |
103 |
| - System.out.println("Adding another Ari"); |
104 |
| - |
105 |
| - cache.put(new Element(1, new Person("Ari Eck", 36, Gender.MALE, "eck street", "San Mateo", "CA"))); |
106 |
| - |
107 |
| - t = System.currentTimeMillis(); |
108 |
| - System.out.println("Again Searching for all Person's who's name start with Ari and are Male:"); |
109 |
| - results = query.execute(); |
110 |
| - System.out.println("Took: " + (System.currentTimeMillis() - t) + " Size: " + results.size()); |
111 |
| - |
112 |
| - read(); |
113 |
| - |
114 |
| - System.out.println("Find the average age of all the entries in the cache"); |
115 |
| - |
116 |
| - Query averageAgeQuery = cache.createQuery(); |
117 |
| - averageAgeQuery.includeAggregator(Aggregators.average(age)); |
118 |
| - System.out.println("Average age: " + averageAgeQuery.execute().getAggregatorResults()); |
119 |
| - |
120 |
| - read(); |
121 |
| - |
122 |
| - System.out.println("Find the average age of all people between 30 and 40"); |
123 |
| - |
124 |
| - Query agesBetween = cache.createQuery(); |
125 |
| - agesBetween.add(age.between(30, 40)); |
126 |
| - agesBetween.includeAggregator(Aggregators.average(age)); |
127 |
| - System.out.println("Average age between 30 and 40: " + agesBetween.execute().getAggregatorResults()); |
128 |
| - |
129 |
| - read(); |
130 |
| - |
131 |
| - System.out.println("Find the count of people from NJ"); |
132 |
| - |
133 |
| - Query newJerseyCountQuery = cache.createQuery().add(state.eq("NJ")); |
134 |
| - newJerseyCountQuery.includeAggregator(Aggregators.count()); |
135 |
| - System.out.println("Count of people from NJ: " + newJerseyCountQuery.execute().getAggregatorResults()); |
136 |
| - } |
137 |
| - |
138 |
| - private void loadCache() { |
139 |
| - cache.put(new Element(1, new Person("Tim Eck", 35, Gender.MALE, "eck street", "San Mateo", "CA"))); |
140 |
| - cache.put(new Element(2, new Person("Pamela Jones", 23, Gender.FEMALE, "berry st", "Parsippany", "LA"))); |
141 |
| - cache.put(new Element(3, new Person("Ari Zilka", 25, Gender.MALE, "big wig", "Beverly Hills", "NJ"))); |
142 |
| - cache.put(new Element(4, new Person("Ari gold", 45, Gender.MALE, "cool agent", "Madison", "WI"))); |
143 |
| - cache.put(new Element(5, new Person("Nabib El-Rahman", 30, Gender.MALE, "dah man", "Bangladesh", "MN"))); |
144 |
| - for (int i = 5; i < 1000; i++) { |
145 |
| - cache.put(new Element(i, new Person("Nabib El-Rahman" + i, 30, Gender.MALE, "dah man", "Bangladesh", "NJ"))); |
146 |
| - } |
147 |
| - } |
148 |
| - |
149 |
| - private static void read() throws IOException { |
150 |
| - System.err.println("\nhit enter to continue"); |
151 |
| - System.in.read(); |
152 |
| - } |
153 |
| - |
154 |
| - public static void main(String[] args) throws IOException { |
155 |
| - new EhcacheSearchPlaying().runTests(); |
156 |
| - } |
157 |
| - |
158 |
| - public static class NameAttributeExtractor implements AttributeExtractor { |
159 |
| - |
160 |
| - /** |
161 |
| - * Implementing the AttributeExtractor Interface and passing it in allows you to create very efficient and specific attribute |
162 |
| - * extraction for performance sensitive code |
163 |
| - */ |
164 |
| - |
165 |
| - public Object attributeFor(Element element) { |
166 |
| - return ((Person) element.getValue()).getName(); |
167 |
| - } |
168 |
| - |
169 |
| - } |
| 32 | + private CacheManager cacheManager; |
| 33 | + private Ehcache cache; |
| 34 | + |
| 35 | + public EhcacheSearchPlaying() { |
| 36 | + initializeCache(); |
| 37 | + } |
| 38 | + |
| 39 | + private void initializeCache() { |
| 40 | + |
| 41 | + // Create Cache |
| 42 | + Configuration cacheManagerConfig = new Configuration(); |
| 43 | + cacheManagerConfig.addDefaultCache(new CacheConfiguration()); |
| 44 | + CacheConfiguration cacheConfig = new CacheConfiguration("test", -1) |
| 45 | + .eternal(true); |
| 46 | + Searchable searchable = new Searchable(); |
| 47 | + cacheConfig.addSearchable(searchable); |
| 48 | + |
| 49 | + // Create attributes on the stuff we want to be able to search on. |
| 50 | + |
| 51 | + // You can use an expression for getting at the value to be indexed on a |
| 52 | + // cache or you can code your own |
| 53 | + |
| 54 | + // Expressions |
| 55 | + // By attribute |
| 56 | + searchable.addSearchAttribute(new SearchAttribute().name("age")); |
| 57 | + // By Expression |
| 58 | + searchable.addSearchAttribute(new SearchAttribute().name("gender") |
| 59 | + .expression("value.getGender()")); |
| 60 | + searchable.addSearchAttribute(new SearchAttribute().name("state") |
| 61 | + .expression("value.getAddress().getState()")); |
| 62 | + |
| 63 | + // Coding your own extracter |
| 64 | + searchable |
| 65 | + .addSearchAttribute(new SearchAttribute() |
| 66 | + .name("name") |
| 67 | + .className( |
| 68 | + "org.sharrissf.sample.EhcacheSearchPlaying$NameAttributeExtractor")); |
| 69 | + |
| 70 | + /* |
| 71 | + * If you want to initialize it via ehcache.xml it would look like this: |
| 72 | + * <cache name="test" maxElementsInMemory="0" eternal="true" |
| 73 | + * overflowToDisk="false"> <searchable> <searchAttribute name="age"/> |
| 74 | + * <searchAttribute name="name" |
| 75 | + * class="org.sharrissf.sample.EhcacheSearchPlaying$NameAttributeExtractor" |
| 76 | + * /> <searchAttribute name="gender" expression="value.getGender()"/> |
| 77 | + * <searchAttribute name="state" expression="value.getState()"/> |
| 78 | + * </searchable> </cache> |
| 79 | + */ |
| 80 | + |
| 81 | + cacheManagerConfig.addCache(cacheConfig); |
| 82 | + |
| 83 | + cacheManager = new CacheManager(cacheManagerConfig); |
| 84 | + cache = cacheManager.getEhcache("test"); |
| 85 | + } |
| 86 | + |
| 87 | + public void runTests() throws IOException { |
| 88 | + loadCache(); |
| 89 | + |
| 90 | + Attribute<Integer> age = cache.getSearchAttribute("age"); |
| 91 | + Attribute<Gender> gender = cache.getSearchAttribute("gender"); |
| 92 | + Attribute<String> name = cache.getSearchAttribute("name"); |
| 93 | + Attribute<String> state = cache.getSearchAttribute("state"); |
| 94 | + |
| 95 | + Query query = cache.createQuery(); |
| 96 | + query.includeKeys(); |
| 97 | + query.add(new And(name.like("Ari*"), gender.eq(Gender.MALE))) |
| 98 | + .addOrder(age, Direction.ASCENDING).maxResults(10); |
| 99 | + |
| 100 | + long t = System.currentTimeMillis(); |
| 101 | + System.out |
| 102 | + .println("Searching for all Person's who's name start with Ari and are Male:"); |
| 103 | + |
| 104 | + Results results = query.execute(); |
| 105 | + System.out.println("Took: " + (System.currentTimeMillis() - t) |
| 106 | + + " Size: " + results.size()); |
| 107 | + System.out.println("----Results-----\n"); |
| 108 | + for (Result result : results.all()) { |
| 109 | + System.out |
| 110 | + .println("Got: Key[" + result.getKey() + "] Value class [" |
| 111 | + + cache.get(result.getKey()).getValue() |
| 112 | + + "] Value [" |
| 113 | + + cache.get(result.getKey()).getValue() + "]"); |
| 114 | + } |
| 115 | + |
| 116 | + read(); |
| 117 | + |
| 118 | + System.out.println("Adding another Ari"); |
| 119 | + |
| 120 | + cache.put(new Element(1, new Person("Ari Eck", 36, Gender.MALE, |
| 121 | + "eck street", "San Mateo", "CA"))); |
| 122 | + |
| 123 | + t = System.currentTimeMillis(); |
| 124 | + System.out |
| 125 | + .println("Again Searching for all Person's who's name start with Ari and are Male:"); |
| 126 | + results = query.execute(); |
| 127 | + System.out.println("Took: " + (System.currentTimeMillis() - t) |
| 128 | + + " Size: " + results.size()); |
| 129 | + |
| 130 | + read(); |
| 131 | + |
| 132 | + System.out |
| 133 | + .println("Find the average age of all the entries in the cache"); |
| 134 | + |
| 135 | + Query averageAgeQuery = cache.createQuery(); |
| 136 | + averageAgeQuery.includeAggregator(Aggregators.average(age)); |
| 137 | + System.out.println("Average age: " |
| 138 | + + averageAgeQuery.execute().getAggregatorResults()); |
| 139 | + |
| 140 | + read(); |
| 141 | + |
| 142 | + System.out |
| 143 | + .println("Find the average age of all people between 30 and 40"); |
| 144 | + |
| 145 | + Query agesBetween = cache.createQuery(); |
| 146 | + agesBetween.add(age.between(30, 40)); |
| 147 | + agesBetween.includeAggregator(Aggregators.average(age)); |
| 148 | + System.out.println("Average age between 30 and 40: " |
| 149 | + + agesBetween.execute().getAggregatorResults()); |
| 150 | + |
| 151 | + read(); |
| 152 | + |
| 153 | + System.out.println("Find the count of people from NJ"); |
| 154 | + |
| 155 | + Query newJerseyCountQuery = cache.createQuery().add(state.eq("NJ")); |
| 156 | + newJerseyCountQuery.includeAggregator(Aggregators.count()); |
| 157 | + System.out.println("Count of people from NJ: " |
| 158 | + + newJerseyCountQuery.execute().getAggregatorResults()); |
| 159 | + } |
| 160 | + |
| 161 | + private void loadCache() { |
| 162 | + cache.put(new Element(1, new Person("Tim Eck", 35, Gender.MALE, |
| 163 | + "eck street", "San Mateo", "CA"))); |
| 164 | + cache.put(new Element(2, new Person("Pamela Jones", 23, Gender.FEMALE, |
| 165 | + "berry st", "Parsippany", "LA"))); |
| 166 | + cache.put(new Element(3, new Person("Ari Zilka", 25, Gender.MALE, |
| 167 | + "big wig", "Beverly Hills", "NJ"))); |
| 168 | + cache.put(new Element(4, new Person("Ari gold", 45, Gender.MALE, |
| 169 | + "cool agent", "Madison", "WI"))); |
| 170 | + cache.put(new Element(5, new Person("Nabib El-Rahman", 30, Gender.MALE, |
| 171 | + "dah man", "Bangladesh", "MN"))); |
| 172 | + for (int i = 5; i < 1000; i++) { |
| 173 | + cache.put(new Element(i, new Person("Nabib El-Rahman" + i, 30, |
| 174 | + Gender.MALE, "dah man", "Bangladesh", "NJ"))); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private static void read() throws IOException { |
| 179 | + System.err.println("\nhit enter to continue"); |
| 180 | + System.in.read(); |
| 181 | + } |
| 182 | + |
| 183 | + public static void main(String[] args) throws IOException { |
| 184 | + new EhcacheSearchPlaying().runTests(); |
| 185 | + } |
| 186 | + |
| 187 | + public static class NameAttributeExtractor implements AttributeExtractor { |
| 188 | + |
| 189 | + /** |
| 190 | + * Implementing the AttributeExtractor Interface and passing it in |
| 191 | + * allows you to create very efficient and specific attribute extraction |
| 192 | + * for performance sensitive code |
| 193 | + */ |
| 194 | + |
| 195 | + public Object attributeFor(Element element) { |
| 196 | + return ((Person) element.getValue()).getName(); |
| 197 | + } |
| 198 | + |
| 199 | + } |
170 | 200 | }
|
0 commit comments