Skip to content

Commit 8e15113

Browse files
committed
Add some more query examples
1 parent bc5f22a commit 8e15113

File tree

5 files changed

+95
-5
lines changed

5 files changed

+95
-5
lines changed

infinispan-remote/query/src/main/java/org/infinispan/tutorial/simple/remote/query/InfinispanRemoteQuery.java

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.infinispan.tutorial.simple.connect.TutorialsConnectorHelper;
99

1010
import java.net.URI;
11+
import java.util.Arrays;
1112
import java.util.HashMap;
1213
import java.util.List;
1314
import java.util.Map;
@@ -25,19 +26,22 @@
2526
public class InfinispanRemoteQuery {
2627

2728
public static final String INDEXED_PEOPLE_CACHE = "indexedPeopleCache";
29+
public static final String INDEXED_TEAM_CACHE = "indexedTeamCache";
2830
static RemoteCacheManager client;
2931
static RemoteCache<PersonKey, Person> peopleCache;
32+
static RemoteCache<String, Team> teamCache;
3033

3134
public static void main(String[] args) throws Exception {
3235
connectToInfinispan();
33-
34-
addDataToCache();
36+
addDataToPeopleCache();
3537
queryAll();
3638
queryWithWhereStatementOnValues();
3739
queryByKey();
3840
queryWithProjection();
3941
deleteByQuery();
4042

43+
addDataToTeamCache();
44+
queryWithScoreAndFilterOnNestedValues();
4145
disconnect(false);
4246
}
4347

@@ -101,6 +105,31 @@ static List<Person> queryWithWhereStatementOnValues() {
101105
return queryResult;
102106
}
103107

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+
104133
static void connectToInfinispan() throws Exception {
105134
ConfigurationBuilder builder = TutorialsConnectorHelper.connectionConfig();
106135

@@ -109,7 +138,9 @@ static void connectToInfinispan() throws Exception {
109138

110139
// Use indexed cache
111140
URI indexedCacheURI = InfinispanRemoteQuery.class.getClassLoader().getResource("indexedCache.xml").toURI();
141+
URI teamCacheURI = InfinispanRemoteQuery.class.getClassLoader().getResource("teamCache.xml").toURI();
112142
builder.remoteCache(INDEXED_PEOPLE_CACHE).configurationURI(indexedCacheURI);
143+
builder.remoteCache(INDEXED_TEAM_CACHE).configurationURI(teamCacheURI);
113144

114145
// Connect to the server
115146
client = TutorialsConnectorHelper.connect(builder);
@@ -119,9 +150,10 @@ static void connectToInfinispan() throws Exception {
119150

120151
// Get the people cache, create it if needed with the default configuration
121152
peopleCache = client.getCache(INDEXED_PEOPLE_CACHE);
153+
teamCache = client.getCache(INDEXED_TEAM_CACHE);
122154
}
123155

124-
static void addDataToCache() {
156+
static void addDataToPeopleCache() {
125157
// Create the persons dataset to be stored in the cache
126158
Map<PersonKey, Person> people = new HashMap<>();
127159
people.put(new PersonKey("1", "hgranger"),
@@ -137,6 +169,34 @@ static void addDataToCache() {
137169
peopleCache.putAll(people);
138170
}
139171

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+
140200
public static void disconnect(boolean removeCaches) {
141201
if (removeCaches) {
142202
client.administration().removeCache(INDEXED_PEOPLE_CACHE);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.infinispan.tutorial.simple.remote.query;
2+
3+
import org.infinispan.api.annotations.indexing.Basic;
4+
import org.infinispan.api.annotations.indexing.Embedded;
5+
import org.infinispan.api.annotations.indexing.Indexed;
6+
import org.infinispan.protostream.annotations.Proto;
7+
8+
import java.util.List;
9+
10+
@Proto
11+
@Indexed
12+
public record Team(
13+
@Basic(projectable = true)
14+
String teamName,
15+
@Basic(projectable = true)
16+
Integer points,
17+
@Embedded
18+
List<Person> players){
19+
}

infinispan-remote/query/src/main/java/org/infinispan/tutorial/simple/remote/query/TutorialSchema.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
import org.infinispan.protostream.GeneratedSchema;
44
import org.infinispan.protostream.annotations.ProtoSchema;
55

6-
@ProtoSchema(schemaPackageName = "tutorial", includeClasses = { Person.class, PersonKey.class })
6+
@ProtoSchema(schemaPackageName = "tutorial", includeClasses = { Team.class, Person.class, PersonKey.class })
77
public interface TutorialSchema extends GeneratedSchema {
88
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<distributed-cache statistics="true">
2+
<encoding>
3+
<key media-type="application/x-protostream"/>
4+
<value media-type="application/x-protostream"/>
5+
</encoding>
6+
<indexing enabled="true" storage="filesystem" startup-mode="AUTO">
7+
<indexed-entities>
8+
<indexed-entity>tutorial.Team</indexed-entity>
9+
</indexed-entities>
10+
</indexing>
11+
</distributed-cache>

infinispan-remote/query/src/test/java/org/infinispan/tutorial/simple/remote/query/InfinispanRemoteQueryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void testRemoteQuery() {
3232
List<Person> people = InfinispanRemoteQuery.queryAll();
3333
assertEquals(0, people.size());
3434

35-
InfinispanRemoteQuery.addDataToCache();
35+
InfinispanRemoteQuery.addDataToPeopleCache();
3636

3737
people = InfinispanRemoteQuery.queryAll();
3838
assertEquals(4, people.size());

0 commit comments

Comments
 (0)