12
12
import net .sf .ehcache .search .Query ;
13
13
import net .sf .ehcache .search .Results ;
14
14
import net .sf .ehcache .search .aggregator .Average ;
15
+ import net .sf .ehcache .search .attribute .AttributeExtractor ;
15
16
16
17
import org .sharrissf .sample .Person .Gender ;
17
18
19
+ /**
20
+ * 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
21
+ * it can become outdated at any time
22
+ *
23
+ * @author steve
24
+ *
25
+ */
18
26
public class EhcacheSearchPlaying {
27
+ private CacheManager cacheManager ;
28
+ private Cache cache ;
19
29
20
- public static void main ( String [] args ) throws IOException {
21
-
22
- Configuration cacheManagerConfig = new Configuration ();
30
+ public EhcacheSearchPlaying () {
31
+ initializeCache ();
32
+ }
23
33
24
- // Add default cache
25
- cacheManagerConfig .addDefaultCache (new CacheConfiguration ());
34
+ private void initializeCache () {
26
35
27
36
// Create Cache
28
-
37
+ Configuration cacheManagerConfig = new Configuration ();
38
+ cacheManagerConfig .addDefaultCache (new CacheConfiguration ());
29
39
CacheConfiguration cacheConfig = new CacheConfiguration ("test" , -1 ).eternal (true );
30
40
41
+ // Create attributes on the stuff we want to be able to search on.
42
+
43
+ // You can use an expression for getting at the value to be indexed on a cache or you can code your own
31
44
SearchAttribute sa = new SearchAttribute ();
32
45
sa .setExpression ("value.getAge()" );
33
46
sa .setName ("age" );
34
47
cacheConfig .addSearchAttribute (sa );
35
48
36
49
sa = new SearchAttribute ();
37
- sa .setExpression ("value.getName()" );
50
+ sa .className ("org.sharrissf.sample.EhcacheSearchPlaying$NameAttributeExtractor" );
51
+
38
52
sa .setName ("name" );
39
53
cacheConfig .addSearchAttribute (sa );
40
54
@@ -45,10 +59,11 @@ public static void main(String[] args) throws IOException {
45
59
46
60
cacheManagerConfig .addCache (cacheConfig );
47
61
48
- CacheManager cacheManager = new CacheManager (cacheManagerConfig );
49
-
50
- Cache cache = cacheManager . getCache ( "test" );
62
+ cacheManager = new CacheManager (cacheManagerConfig );
63
+ cache = cacheManager . getCache ( "test" );
64
+ }
51
65
66
+ public void runTests () throws IOException {
52
67
cache .put (new Element (1 , new Person ("Tim Eck" , 35 , Gender .MALE )));
53
68
cache .put (new Element (2 , new Person ("Pamela Jones" , 23 , Gender .FEMALE )));
54
69
cache .put (new Element (3 , new Person ("Ari Zilka" , 25 , Gender .MALE )));
@@ -82,12 +97,24 @@ public static void main(String[] args) throws IOException {
82
97
Query averageAgeQuery = cache .createQuery ();
83
98
averageAgeQuery .includeAggregator (new Average (), age );
84
99
System .out .println ("Average age: " + averageAgeQuery .execute ().aggregateResult ());
85
-
86
100
}
87
101
88
102
private static void read () throws IOException {
89
103
System .err .println ("\n hit enter to continue" );
90
104
System .in .read ();
91
105
}
92
106
107
+ public static void main (String [] args ) throws IOException {
108
+ new EhcacheSearchPlaying ().runTests ();
109
+ }
110
+
111
+ @ SuppressWarnings ( { "unused" , "serial" })
112
+ public static class NameAttributeExtractor implements AttributeExtractor {
113
+
114
+ @ Override
115
+ public Object attributeFor (Element element ) {
116
+ return ((Person ) element .getValue ()).getName ();
117
+ }
118
+
119
+ }
93
120
}
0 commit comments