8
8
import org .infinispan .tutorial .simple .connect .TutorialsConnectorHelper ;
9
9
10
10
import java .net .URI ;
11
+ import java .util .Arrays ;
11
12
import java .util .HashMap ;
12
13
import java .util .List ;
13
14
import java .util .Map ;
25
26
public class InfinispanRemoteQuery {
26
27
27
28
public static final String INDEXED_PEOPLE_CACHE = "indexedPeopleCache" ;
29
+ public static final String INDEXED_TEAM_CACHE = "indexedTeamCache" ;
28
30
static RemoteCacheManager client ;
29
31
static RemoteCache <PersonKey , Person > peopleCache ;
32
+ static RemoteCache <String , Team > teamCache ;
30
33
31
34
public static void main (String [] args ) throws Exception {
32
35
connectToInfinispan ();
33
-
34
- addDataToCache ();
36
+ addDataToPeopleCache ();
35
37
queryAll ();
36
38
queryWithWhereStatementOnValues ();
37
39
queryByKey ();
38
40
queryWithProjection ();
39
41
deleteByQuery ();
40
42
43
+ addDataToTeamCache ();
44
+ queryWithScoreAndFilterOnNestedValues ();
41
45
disconnect (false );
42
46
}
43
47
@@ -101,6 +105,31 @@ static List<Person> queryWithWhereStatementOnValues() {
101
105
return queryResult ;
102
106
}
103
107
108
+ private static void queryWithScoreAndFilterOnNestedValues () {
109
+ System .out .println ("== Query and filter on nested values of the team" );
110
+ Query <Team > query = teamCache .query ("FROM tutorial.Team t join t.players p where p.bornIn = :bornIn" );
111
+ // Set the parameter value
112
+ query .setParameter ("bornIn" , "London" );
113
+ List <Team > teamsWithPeopleFromLondon = query .execute ().list ();
114
+ System .out .println ("There are only 2 teams " + teamsWithPeopleFromLondon .size ());
115
+ System .out .println (teamsWithPeopleFromLondon );
116
+
117
+ // Get the score
118
+ Query <Object []> queryWithProjectionAndScore = teamCache .query ("select t.teamName, score(t) FROM tutorial.Team t where t.points=88" );
119
+ List <Object []> results = queryWithProjectionAndScore .execute ().list ();
120
+ System .out .println ("Team with 88 points: " + results .size ());
121
+ System .out .println ("Team name: " + results .get (0 )[0 ]);
122
+ System .out .println ("Score: " + results .get (0 )[1 ]);
123
+
124
+ // With filter in nested values
125
+ Query <Object []> queryProjection = teamCache .query ("select t.teamName, score(t) FROM tutorial.Team t join t.players p where p.lastName = :lastName" );
126
+ queryProjection .setParameter ("lastName" , "Granger" );
127
+ results = queryProjection .execute ().list ();
128
+ System .out .println ("Hermione number of teams: " + results .size ());
129
+ System .out .println ("Hermione team name: " + results .get (0 )[0 ]);
130
+ }
131
+
132
+
104
133
static void connectToInfinispan () throws Exception {
105
134
ConfigurationBuilder builder = TutorialsConnectorHelper .connectionConfig ();
106
135
@@ -109,7 +138,9 @@ static void connectToInfinispan() throws Exception {
109
138
110
139
// Use indexed cache
111
140
URI indexedCacheURI = InfinispanRemoteQuery .class .getClassLoader ().getResource ("indexedCache.xml" ).toURI ();
141
+ URI teamCacheURI = InfinispanRemoteQuery .class .getClassLoader ().getResource ("teamCache.xml" ).toURI ();
112
142
builder .remoteCache (INDEXED_PEOPLE_CACHE ).configurationURI (indexedCacheURI );
143
+ builder .remoteCache (INDEXED_TEAM_CACHE ).configurationURI (teamCacheURI );
113
144
114
145
// Connect to the server
115
146
client = TutorialsConnectorHelper .connect (builder );
@@ -119,9 +150,10 @@ static void connectToInfinispan() throws Exception {
119
150
120
151
// Get the people cache, create it if needed with the default configuration
121
152
peopleCache = client .getCache (INDEXED_PEOPLE_CACHE );
153
+ teamCache = client .getCache (INDEXED_TEAM_CACHE );
122
154
}
123
155
124
- static void addDataToCache () {
156
+ static void addDataToPeopleCache () {
125
157
// Create the persons dataset to be stored in the cache
126
158
Map <PersonKey , Person > people = new HashMap <>();
127
159
people .put (new PersonKey ("1" , "hgranger" ),
@@ -137,6 +169,34 @@ static void addDataToCache() {
137
169
peopleCache .putAll (people );
138
170
}
139
171
172
+ static void addDataToTeamCache () {
173
+ // Create the persons dataset to be stored in the cache
174
+ Map <String , Team > teams = new HashMap <>();
175
+
176
+ Team team1 = new Team ("Heroic team" , 900 , Arrays .asList (
177
+ new Person ("Hermione" , "Granger" , 1990 , "London" ),
178
+ new Person ("Neville" , "Longbottom" , 1990 , "Manchester" ),
179
+ new Person ("Luna" , "Lovegood" , 1991 , "London" )
180
+ ));
181
+
182
+ Team team2 = new Team ("Villains team" , 88 , Arrays .asList (
183
+ new Person ("Draco" , "Malfoy" , 1988 , "London" ),
184
+ new Person ("Bellatrix" , "Lestrange" , 1955 , "Bristol" ),
185
+ new Person ("Dolores" , "Umbridge" , 1950 , "Bristol" )
186
+ ));
187
+
188
+ Team team3 = new Team ("I don't care team" , 1500 , Arrays .asList (
189
+ new Person ("Lavender" , "Brown" , 1990 , "Bristol" ),
190
+ new Person ("Seamus" , "Finnigan" , 1990 , "Manchester" )
191
+ ));
192
+
193
+ teams .put ("1" , team1 );
194
+ teams .put ("2" , team2 );
195
+ teams .put ("3" , team3 );
196
+
197
+ teamCache .putAll (teams );
198
+ }
199
+
140
200
public static void disconnect (boolean removeCaches ) {
141
201
if (removeCaches ) {
142
202
client .administration ().removeCache (INDEXED_PEOPLE_CACHE );
0 commit comments