Skip to content

Add some more query examples #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.server.test.junit5.InfinispanServerExtension;
import org.infinispan.server.test.junit5.InfinispanServerExtensionBuilder;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand All @@ -13,6 +14,7 @@
/**
* This test is a JUnit 5 test that uses TestContainers under the hood
*/
@Disabled
public class CachingServiceTest {

@RegisterExtension
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.infinispan.tutorial.simple.remote.percache;

import org.infinispan.client.hotrod.DefaultTemplate;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
Expand All @@ -22,11 +21,9 @@
public class InfinispanRemotePerCache {

public static final String MY_CACHE = "my-cache";
public static final String ANOTHER_CACHE = "another-cache";
public static final String URI_CACHE = "uri-cache";
static RemoteCacheManager cacheManager;
static RemoteCache<String, String> cache;
static RemoteCache<String, String> anotherCache;
static RemoteCache<String, String> uriCache;

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

anotherCache = cacheManager.getCache(ANOTHER_CACHE);
/// Store a value
anotherCache.put("hello-another", "world-another");
// Retrieve the value and print it out
System.out.printf("key = %s\n", anotherCache.get("hello-another"));

uriCache = cacheManager.getCache(URI_CACHE);
/// Store a value
uriCache.put("hello-uri", "world-uri");
Expand All @@ -62,11 +53,8 @@ public static void connectToInfinispan() throws Exception {
// Create a configuration for a locally-running server
ConfigurationBuilder builder = TutorialsConnectorHelper.connectionConfig();

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

builder.remoteCache(URI_CACHE).configurationURI(
Expand All @@ -78,7 +66,6 @@ public static void connectToInfinispan() throws Exception {
public static void disconnect(boolean removeCaches) {
if (removeCaches) {
cacheManager.administration().removeCache(MY_CACHE);
cacheManager.administration().removeCache(ANOTHER_CACHE);
cacheManager.administration().removeCache(URI_CACHE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ public static void stop() {
public void testRemotePerCacheConfiguration() {
assertNotNull(InfinispanRemotePerCache.cacheManager);
assertNull(InfinispanRemotePerCache.cache);
assertNull(InfinispanRemotePerCache.anotherCache);
assertNull(InfinispanRemotePerCache.uriCache);

InfinispanRemotePerCache.manipulateCaches();
assertNotNull(InfinispanRemotePerCache.cache);
assertNotNull(InfinispanRemotePerCache.anotherCache);
assertNotNull(InfinispanRemotePerCache.uriCache);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.infinispan.tutorial.simple.connect.TutorialsConnectorHelper;

import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -25,23 +26,27 @@
public class InfinispanRemoteQuery {

public static final String INDEXED_PEOPLE_CACHE = "indexedPeopleCache";
public static final String INDEXED_TEAM_CACHE = "indexedTeamCache";
static RemoteCacheManager client;
static RemoteCache<PersonKey, Person> peopleCache;
static RemoteCache<String, Team> teamCache;

public static void main(String[] args) throws Exception {
connectToInfinispan();

addDataToCache();
queryAll();
addDataToPeopleCache();
queryAllPeople();
queryWithWhereStatementOnValues();
queryByKey();
queryWithProjection();
deleteByQuery();

addDataToTeamCache();
countAllTeam();
queryWithScoreAndFilterOnNestedValues();
disconnect(false);
}

static List<Person> queryAll() {
static List<Person> queryAllPeople() {
// Query all
Query<Person> query = peopleCache.query("FROM tutorial.Person");
List<Person> queryResult = query.execute().list();
Expand All @@ -51,6 +56,19 @@ static List<Person> queryAll() {
return queryResult;
}

static Long countAllTeam() {
// Query all
Query<Object[]> query = teamCache.query("select count(t) FROM tutorial.Team t");
List<Object[]> queryResult = query.execute().list();
// Print the results
if (queryResult.size() > 0) {
System.out.println("COUNT " + queryResult.get(0)[0]);
return (Long) queryResult.get(0)[0];
}
System.out.println("No team found");
return 0L;
}

static List<Person> deleteByQuery() {
Query<Person> query = peopleCache.query("DELETE FROM tutorial.Person p where p.key.pseudo = 'dmalfoy'");
System.out.println("== DELETE count:" + query.execute().count().value());
Expand Down Expand Up @@ -101,6 +119,31 @@ static List<Person> queryWithWhereStatementOnValues() {
return queryResult;
}

static void queryWithScoreAndFilterOnNestedValues() {
System.out.println("== Query and filter on nested values of the team");
Query<Team> query = teamCache.query("FROM tutorial.Team t join t.players p where p.bornIn = :bornIn");
// Set the parameter value
query.setParameter("bornIn", "London");
List<Team> teamsWithPeopleFromLondon = query.execute().list();
System.out.println("There are only 2 teams " + teamsWithPeopleFromLondon.size());
System.out.println(teamsWithPeopleFromLondon);

// Get the score
Query<Object[]> queryWithProjectionAndScore = teamCache.query("select t.teamName, score(t) FROM tutorial.Team t where t.points=88");
List<Object[]> results = queryWithProjectionAndScore.execute().list();
System.out.println("Team with 88 points: " + results.size());
System.out.println("Team name: " + results.get(0)[0]);
System.out.println("Score: " + results.get(0)[1]);

// With filter in nested values
Query<Object[]> queryProjection = teamCache.query("select t.teamName, score(t) FROM tutorial.Team t join t.players p where p.lastName = :lastName");
queryProjection.setParameter("lastName", "Granger");
results = queryProjection.execute().list();
System.out.println("Hermione number of teams: " + results.size());
Comment on lines +139 to +142
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fax4ever this is not working the same in the test suite than the real server. results look the same when I try with the rest api and hotrod.

System.out.println("Hermione team name: " + results.get(0)[0]);
}


static void connectToInfinispan() throws Exception {
ConfigurationBuilder builder = TutorialsConnectorHelper.connectionConfig();

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

// Use indexed cache
URI indexedCacheURI = InfinispanRemoteQuery.class.getClassLoader().getResource("indexedCache.xml").toURI();
URI teamCacheURI = InfinispanRemoteQuery.class.getClassLoader().getResource("teamCache.xml").toURI();
builder.remoteCache(INDEXED_PEOPLE_CACHE).configurationURI(indexedCacheURI);
builder.remoteCache(INDEXED_TEAM_CACHE).configurationURI(teamCacheURI);

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

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

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

static void addDataToTeamCache() {
// Create the persons dataset to be stored in the cache
Map<String, Team> teams = new HashMap<>();

Team team1 = new Team("Heroic team", 900, Arrays.asList(
new Person("Hermione", "Granger", 1990, "London"),
new Person("Neville", "Longbottom", 1990, "Manchester"),
new Person("Luna", "Lovegood", 1991, "London")
));

Team team2 = new Team("Villains team", 88, Arrays.asList(
new Person("Draco", "Malfoy", 1988, "London"),
new Person("Bellatrix", "Lestrange", 1955, "Bristol"),
new Person("Dolores", "Umbridge", 1950, "Bristol")
));

Team team3 = new Team("I don't care team", 1500, Arrays.asList(
new Person("Lavender", "Brown", 1990, "Bristol"),
new Person("Seamus", "Finnigan", 1990, "Manchester")
));

teams.put("1", team1);
teams.put("2", team2);
teams.put("3", team3);

teamCache.putAll(teams);
}

public static void disconnect(boolean removeCaches) {
if (removeCaches) {
client.administration().removeCache(INDEXED_PEOPLE_CACHE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.infinispan.tutorial.simple.remote.query;

import org.infinispan.api.annotations.indexing.Basic;
import org.infinispan.api.annotations.indexing.Embedded;
import org.infinispan.api.annotations.indexing.Indexed;
import org.infinispan.protostream.annotations.Proto;

import java.util.List;

@Proto
@Indexed
public record Team(
@Basic(projectable = true)
String teamName,
@Basic(projectable = true)
Integer points,
@Embedded
List<Person> players){
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import org.infinispan.protostream.GeneratedSchema;
import org.infinispan.protostream.annotations.ProtoSchema;

@ProtoSchema(schemaPackageName = "tutorial", includeClasses = { Person.class, PersonKey.class })
@ProtoSchema(schemaPackageName = "tutorial", includeClasses = { Team.class, Person.class, PersonKey.class })
public interface TutorialSchema extends GeneratedSchema {
}
11 changes: 11 additions & 0 deletions infinispan-remote/query/src/main/resources/teamCache.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<distributed-cache statistics="true">
<encoding>
<key media-type="application/x-protostream"/>
<value media-type="application/x-protostream"/>
</encoding>
<indexing enabled="true" storage="filesystem" startup-mode="AUTO">
<indexed-entities>
<indexed-entity>tutorial.Team</indexed-entity>
</indexed-entities>
</indexing>
</distributed-cache>
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ public static void stop() {
public void testRemoteQuery() {
assertNotNull(InfinispanRemoteQuery.client);
assertNotNull(InfinispanRemoteQuery.peopleCache);
assertNotNull(InfinispanRemoteQuery.teamCache);
InfinispanRemoteQuery.peopleCache.clear();
InfinispanRemoteQuery.teamCache.clear();

List<Person> people = InfinispanRemoteQuery.queryAll();
List<Person> people = InfinispanRemoteQuery.queryAllPeople();
assertEquals(0, people.size());

InfinispanRemoteQuery.addDataToCache();
InfinispanRemoteQuery.addDataToPeopleCache();

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

List<Person> peopleFiltered = InfinispanRemoteQuery.queryWithWhereStatementOnValues();
Expand All @@ -57,5 +60,11 @@ public void testRemoteQuery() {
// Malfoy has been removed
peopleFilteredByKey = InfinispanRemoteQuery.queryByKey();
assertEquals(0, peopleFilteredByKey.size());

// Count teams before adding values
assertEquals(0, InfinispanRemoteQuery.countAllTeam());
InfinispanRemoteQuery.addDataToTeamCache();
assertEquals(3, InfinispanRemoteQuery.countAllTeam());
InfinispanRemoteQuery.queryWithScoreAndFilterOnNestedValues();
}
}
2 changes: 0 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${version.maven-compiler-plugin}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
Expand All @@ -74,7 +73,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${version.enforcer.plugin}</version>
<executions>
<execution>
<id>enforce-java</id>
Expand Down
Loading