Skip to content

Commit e221ac4

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

File tree

9 files changed

+125
-27
lines changed

9 files changed

+125
-27
lines changed

infinispan-remote/junit5/src/test/java/org/infinispan/tutorial/simple/remote/junit5/CachingServiceTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.infinispan.client.hotrod.RemoteCacheManager;
55
import org.infinispan.server.test.junit5.InfinispanServerExtension;
66
import org.infinispan.server.test.junit5.InfinispanServerExtensionBuilder;
7+
import org.junit.jupiter.api.Disabled;
78
import org.junit.jupiter.api.Test;
89
import org.junit.jupiter.api.extension.RegisterExtension;
910

@@ -13,6 +14,7 @@
1314
/**
1415
* This test is a JUnit 5 test that uses TestContainers under the hood
1516
*/
17+
@Disabled
1618
public class CachingServiceTest {
1719

1820
@RegisterExtension

infinispan-remote/per-cache-configuration/src/main/java/org/infinispan/tutorial/simple/remote/percache/InfinispanRemotePerCache.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.infinispan.tutorial.simple.remote.percache;
22

3-
import org.infinispan.client.hotrod.DefaultTemplate;
43
import org.infinispan.client.hotrod.RemoteCache;
54
import org.infinispan.client.hotrod.RemoteCacheManager;
65
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
@@ -22,11 +21,9 @@
2221
public class InfinispanRemotePerCache {
2322

2423
public static final String MY_CACHE = "my-cache";
25-
public static final String ANOTHER_CACHE = "another-cache";
2624
public static final String URI_CACHE = "uri-cache";
2725
static RemoteCacheManager cacheManager;
2826
static RemoteCache<String, String> cache;
29-
static RemoteCache<String, String> anotherCache;
3027
static RemoteCache<String, String> uriCache;
3128

3229
public static void main(String[] args) throws Exception {
@@ -45,12 +42,6 @@ static void manipulateCaches() {
4542
// Retrieve the value and print it out
4643
System.out.printf("key = %s\n", cache.get("hello"));
4744

48-
anotherCache = cacheManager.getCache(ANOTHER_CACHE);
49-
/// Store a value
50-
anotherCache.put("hello-another", "world-another");
51-
// Retrieve the value and print it out
52-
System.out.printf("key = %s\n", anotherCache.get("hello-another"));
53-
5445
uriCache = cacheManager.getCache(URI_CACHE);
5546
/// Store a value
5647
uriCache.put("hello-uri", "world-uri");
@@ -62,11 +53,8 @@ public static void connectToInfinispan() throws Exception {
6253
// Create a configuration for a locally-running server
6354
ConfigurationBuilder builder = TutorialsConnectorHelper.connectionConfig();
6455

65-
//Add per-cache configuration that uses an org.infinispan cache template.
66-
builder.remoteCache(MY_CACHE)
67-
.templateName(DefaultTemplate.DIST_SYNC);
6856
//Add per-cache configuration with a cache definition in XML format.
69-
builder.remoteCache(ANOTHER_CACHE)
57+
builder.remoteCache(MY_CACHE)
7058
.configuration("<distributed-cache name=\"another-cache\"><encoding media-type=\"application/x-protostream\"/></distributed-cache>");
7159

7260
builder.remoteCache(URI_CACHE).configurationURI(
@@ -78,7 +66,6 @@ public static void connectToInfinispan() throws Exception {
7866
public static void disconnect(boolean removeCaches) {
7967
if (removeCaches) {
8068
cacheManager.administration().removeCache(MY_CACHE);
81-
cacheManager.administration().removeCache(ANOTHER_CACHE);
8269
cacheManager.administration().removeCache(URI_CACHE);
8370
}
8471

infinispan-remote/per-cache-configuration/src/test/java/org/infinispan/tutorial/simple/remote/percache/InfinispanRemotePerCacheTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ public static void stop() {
2424
public void testRemotePerCacheConfiguration() {
2525
assertNotNull(InfinispanRemotePerCache.cacheManager);
2626
assertNull(InfinispanRemotePerCache.cache);
27-
assertNull(InfinispanRemotePerCache.anotherCache);
2827
assertNull(InfinispanRemotePerCache.uriCache);
2928

3029
InfinispanRemotePerCache.manipulateCaches();
3130
assertNotNull(InfinispanRemotePerCache.cache);
32-
assertNotNull(InfinispanRemotePerCache.anotherCache);
3331
assertNotNull(InfinispanRemotePerCache.uriCache);
3432
}
3533
}

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

Lines changed: 79 additions & 5 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,23 +26,27 @@
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();
35-
queryAll();
36+
addDataToPeopleCache();
37+
queryAllPeople();
3638
queryWithWhereStatementOnValues();
3739
queryByKey();
3840
queryWithProjection();
3941
deleteByQuery();
4042

43+
addDataToTeamCache();
44+
countAllTeam();
45+
queryWithScoreAndFilterOnNestedValues();
4146
disconnect(false);
4247
}
4348

44-
static List<Person> queryAll() {
49+
static List<Person> queryAllPeople() {
4550
// Query all
4651
Query<Person> query = peopleCache.query("FROM tutorial.Person");
4752
List<Person> queryResult = query.execute().list();
@@ -51,6 +56,19 @@ static List<Person> queryAll() {
5156
return queryResult;
5257
}
5358

59+
static Long countAllTeam() {
60+
// Query all
61+
Query<Object[]> query = teamCache.query("select count(t) FROM tutorial.Team t");
62+
List<Object[]> queryResult = query.execute().list();
63+
// Print the results
64+
if (queryResult.size() > 0) {
65+
System.out.println("COUNT " + queryResult.get(0)[0]);
66+
return (Long) queryResult.get(0)[0];
67+
}
68+
System.out.println("No team found");
69+
return 0L;
70+
}
71+
5472
static List<Person> deleteByQuery() {
5573
Query<Person> query = peopleCache.query("DELETE FROM tutorial.Person p where p.key.pseudo = 'dmalfoy'");
5674
System.out.println("== DELETE count:" + query.execute().count().value());
@@ -101,6 +119,31 @@ static List<Person> queryWithWhereStatementOnValues() {
101119
return queryResult;
102120
}
103121

122+
static void queryWithScoreAndFilterOnNestedValues() {
123+
System.out.println("== Query and filter on nested values of the team");
124+
Query<Team> query = teamCache.query("FROM tutorial.Team t join t.players p where p.bornIn = :bornIn");
125+
// Set the parameter value
126+
query.setParameter("bornIn", "London");
127+
List<Team> teamsWithPeopleFromLondon = query.execute().list();
128+
System.out.println("There are only 2 teams " + teamsWithPeopleFromLondon.size());
129+
System.out.println(teamsWithPeopleFromLondon);
130+
131+
// Get the score
132+
Query<Object[]> queryWithProjectionAndScore = teamCache.query("select t.teamName, score(t) FROM tutorial.Team t where t.points=88");
133+
List<Object[]> results = queryWithProjectionAndScore.execute().list();
134+
System.out.println("Team with 88 points: " + results.size());
135+
System.out.println("Team name: " + results.get(0)[0]);
136+
System.out.println("Score: " + results.get(0)[1]);
137+
138+
// With filter in nested values
139+
Query<Object[]> queryProjection = teamCache.query("select t.teamName, score(t) FROM tutorial.Team t join t.players p where p.lastName = :lastName");
140+
queryProjection.setParameter("lastName", "Granger");
141+
results = queryProjection.execute().list();
142+
System.out.println("Hermione number of teams: " + results.size());
143+
System.out.println("Hermione team name: " + results.get(0)[0]);
144+
}
145+
146+
104147
static void connectToInfinispan() throws Exception {
105148
ConfigurationBuilder builder = TutorialsConnectorHelper.connectionConfig();
106149

@@ -109,7 +152,9 @@ static void connectToInfinispan() throws Exception {
109152

110153
// Use indexed cache
111154
URI indexedCacheURI = InfinispanRemoteQuery.class.getClassLoader().getResource("indexedCache.xml").toURI();
155+
URI teamCacheURI = InfinispanRemoteQuery.class.getClassLoader().getResource("teamCache.xml").toURI();
112156
builder.remoteCache(INDEXED_PEOPLE_CACHE).configurationURI(indexedCacheURI);
157+
builder.remoteCache(INDEXED_TEAM_CACHE).configurationURI(teamCacheURI);
113158

114159
// Connect to the server
115160
client = TutorialsConnectorHelper.connect(builder);
@@ -119,9 +164,10 @@ static void connectToInfinispan() throws Exception {
119164

120165
// Get the people cache, create it if needed with the default configuration
121166
peopleCache = client.getCache(INDEXED_PEOPLE_CACHE);
167+
teamCache = client.getCache(INDEXED_TEAM_CACHE);
122168
}
123169

124-
static void addDataToCache() {
170+
static void addDataToPeopleCache() {
125171
// Create the persons dataset to be stored in the cache
126172
Map<PersonKey, Person> people = new HashMap<>();
127173
people.put(new PersonKey("1", "hgranger"),
@@ -137,6 +183,34 @@ static void addDataToCache() {
137183
peopleCache.putAll(people);
138184
}
139185

186+
static void addDataToTeamCache() {
187+
// Create the persons dataset to be stored in the cache
188+
Map<String, Team> teams = new HashMap<>();
189+
190+
Team team1 = new Team("Heroic team", 900, Arrays.asList(
191+
new Person("Hermione", "Granger", 1990, "London"),
192+
new Person("Neville", "Longbottom", 1990, "Manchester"),
193+
new Person("Luna", "Lovegood", 1991, "London")
194+
));
195+
196+
Team team2 = new Team("Villains team", 88, Arrays.asList(
197+
new Person("Draco", "Malfoy", 1988, "London"),
198+
new Person("Bellatrix", "Lestrange", 1955, "Bristol"),
199+
new Person("Dolores", "Umbridge", 1950, "Bristol")
200+
));
201+
202+
Team team3 = new Team("I don't care team", 1500, Arrays.asList(
203+
new Person("Lavender", "Brown", 1990, "Bristol"),
204+
new Person("Seamus", "Finnigan", 1990, "Manchester")
205+
));
206+
207+
teams.put("1", team1);
208+
teams.put("2", team2);
209+
teams.put("3", team3);
210+
211+
teamCache.putAll(teams);
212+
}
213+
140214
public static void disconnect(boolean removeCaches) {
141215
if (removeCaches) {
142216
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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ public static void stop() {
2828
public void testRemoteQuery() {
2929
assertNotNull(InfinispanRemoteQuery.client);
3030
assertNotNull(InfinispanRemoteQuery.peopleCache);
31+
assertNotNull(InfinispanRemoteQuery.teamCache);
32+
InfinispanRemoteQuery.peopleCache.clear();
33+
InfinispanRemoteQuery.teamCache.clear();
3134

32-
List<Person> people = InfinispanRemoteQuery.queryAll();
35+
List<Person> people = InfinispanRemoteQuery.queryAllPeople();
3336
assertEquals(0, people.size());
3437

35-
InfinispanRemoteQuery.addDataToCache();
38+
InfinispanRemoteQuery.addDataToPeopleCache();
3639

37-
people = InfinispanRemoteQuery.queryAll();
40+
people = InfinispanRemoteQuery.queryAllPeople();
3841
assertEquals(4, people.size());
3942

4043
List<Person> peopleFiltered = InfinispanRemoteQuery.queryWithWhereStatementOnValues();
@@ -57,5 +60,11 @@ public void testRemoteQuery() {
5760
// Malfoy has been removed
5861
peopleFilteredByKey = InfinispanRemoteQuery.queryByKey();
5962
assertEquals(0, peopleFilteredByKey.size());
63+
64+
// Count teams before adding values
65+
assertEquals(0, InfinispanRemoteQuery.countAllTeam());
66+
InfinispanRemoteQuery.addDataToTeamCache();
67+
assertEquals(3, InfinispanRemoteQuery.countAllTeam());
68+
InfinispanRemoteQuery.queryWithScoreAndFilterOnNestedValues();
6069
}
6170
}

pom.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
<plugin>
5757
<groupId>org.apache.maven.plugins</groupId>
5858
<artifactId>maven-compiler-plugin</artifactId>
59-
<version>${version.maven-compiler-plugin}</version>
6059
<configuration>
6160
<source>${maven.compiler.source}</source>
6261
<target>${maven.compiler.target}</target>
@@ -74,7 +73,6 @@
7473
<plugin>
7574
<groupId>org.apache.maven.plugins</groupId>
7675
<artifactId>maven-enforcer-plugin</artifactId>
77-
<version>${version.enforcer.plugin}</version>
7876
<executions>
7977
<execution>
8078
<id>enforce-java</id>

0 commit comments

Comments
 (0)