Skip to content

Commit 23ff018

Browse files
committed
OCP-DEMO Add full-text query on body message
1 parent a0c5004 commit 23ff018

File tree

11 files changed

+752
-41
lines changed

11 files changed

+752
-41
lines changed

openshift/message-board/extra/template/cloud-persistent.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,25 @@
312312
<rocksdb-store shared="false" fetch-state="true" passivation="false" preload="true" />
313313
</distributed-cache>
314314
<replicated-cache name="repl" configuration="replicated"/>
315+
<!-- Declare Message Board demo caches in order to use indexes -->
316+
<distributed-cache-configuration name="message-board-with-indexes">
317+
<locking isolation="REPEATABLE_READ" striping="false" acquire-timeout="10000" concurrency-level="50"/>
318+
<transaction mode="NON_DURABLE_XA" locking="PESSIMISTIC"/>
319+
<expiration max-idle="-1"/>
320+
<indexing index="LOCAL" auto-config="true"/>
321+
<state-transfer await-initial-transfer="true" timeout="480000"/>
322+
</distributed-cache-configuration>
323+
<distributed-cache-configuration name="message-board">
324+
<locking isolation="REPEATABLE_READ" striping="false" acquire-timeout="10000" concurrency-level="50"/>
325+
<transaction mode="NON_DURABLE_XA" locking="PESSIMISTIC"/>
326+
<expiration max-idle="-1"/>
327+
<state-transfer await-initial-transfer="true" timeout="480000"/>
328+
</distributed-cache-configuration>
329+
<distributed-cache name="Message_Tag" configuration="message-board"/>
330+
<distributed-cache name="Board" configuration="message-board"/>
331+
<distributed-cache name="Message" configuration="message-board-with-indexes"/>
332+
<distributed-cache name="Board_Message" configuration="message-board"/>
333+
<distributed-cache name="Tag" configuration="message-board"/>
315334
</cache-container>
316335
</subsystem>
317336
<subsystem xmlns="urn:infinispan:server:endpoint:9.2">

openshift/message-board/message-service/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,29 @@
191191
</execution>
192192
</executions>
193193
</plugin>
194+
<plugin>
195+
<groupId>org.apache.maven.plugins</groupId>
196+
<artifactId>maven-resources-plugin</artifactId>
197+
<version>3.1.0</version>
198+
<executions>
199+
<execution>
200+
<id>update-infinispan-config</id>
201+
<phase>process-test-resources</phase>
202+
<goals>
203+
<goal>copy-resources</goal>
204+
</goals>
205+
<configuration>
206+
<outputDirectory>${home.infinispan}/standalone/configuration</outputDirectory>
207+
<overwrite>true</overwrite>
208+
<resources>
209+
<resource>
210+
<directory>${basedir}/src/test/resources/infinispan-config</directory>
211+
</resource>
212+
</resources>
213+
</configuration>
214+
</execution>
215+
</executions>
216+
</plugin>
194217
<plugin>
195218
<artifactId>maven-failsafe-plugin</artifactId>
196219
<version>2.20</version>

openshift/message-board/message-service/src/main/java/org/hibernate/demo/message/post/core/repo/MessageRepo.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import javax.enterprise.context.ApplicationScoped;
1313
import javax.inject.Inject;
1414
import javax.persistence.EntityManager;
15+
import javax.persistence.Query;
1516
import javax.transaction.Transactional;
1617
import javax.validation.Valid;
1718

@@ -88,4 +89,12 @@ public List<Message> findMessageByTime(Date start, Date end) {
8889
.setParameter( "end", end )
8990
.getResultList();
9091
}
92+
93+
public List<Message> findByTerm(String term, int pageNumber, int pageSize) {
94+
Query query = em.createNativeQuery( "from HibernateOGMGenerated.Message m where m.body:'" + term + "'", Message.class )
95+
.setMaxResults( pageSize )
96+
.setFirstResult( pageNumber * pageSize );
97+
98+
return query.getResultList();
99+
}
91100
}

openshift/message-board/message-service/src/main/java/org/hibernate/demo/message/post/core/service/MessageService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ public List<Message> findMessagesByDate(@BeanParam TimeInterval interval) {
9090
return messages.findMessageByTime( interval.getStart(), interval.getEnd() );
9191
}
9292

93+
@GET
94+
@Produces(MediaType.APPLICATION_JSON)
95+
@Path("term/{term}")
96+
public List<Message> findByTerm(@PathParam("term") String term, @QueryParam("page") Integer page, @QueryParam("size") Integer size) {
97+
if ( page == null || size == null ) {
98+
page = 0;
99+
size = Integer.SIZE;
100+
}
101+
102+
return messages.findByTerm( term, page, size );
103+
}
104+
93105
@POST
94106
@Consumes(MediaType.APPLICATION_JSON)
95107
public Integer addMessage(@Valid Message message) {

openshift/message-board/message-service/src/main/resources/META-INF/persistence.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
<properties>
99
<property name="hibernate.ogm.datastore.provider" value="infinispan_remote" />
1010
<property name="hibernate.ogm.infinispan_remote.configuration_resource_name" value="hotrodclient.properties" />
11-
<property name="hibernate.ogm.datastore.create_database" value="true" />
11+
<property name="hibernate.ogm.datastore.create_database" value="false" />
12+
<property name="hibernate.ogm.infinispan_remote.schema_override_resource" value="message.proto" />
1213
</properties>
1314
</persistence-unit>
1415
</persistence>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package HibernateOGMGenerated;
2+
3+
message Message_Tag_id {
4+
required int64 Message_id = 1;
5+
required string tags_name = 2;
6+
}
7+
8+
message Message_Tag {
9+
required int64 Message_id = 1;
10+
required string tags_name = 2;
11+
}
12+
13+
message Board_id {
14+
required string username = 1;
15+
}
16+
17+
message Board {
18+
required string username = 1;
19+
optional int32 next = 2;
20+
}
21+
22+
message Message_id {
23+
required int64 id = 1;
24+
}
25+
26+
/**
27+
* @Indexed
28+
*/
29+
message Message {
30+
31+
/**
32+
* @Field(index = Index.NO, store = Store.NO, analyze = Analyze.NO)
33+
*/
34+
required int64 id = 1;
35+
36+
/**
37+
* @Field(index = Index.YES, store = Store.NO, analyze = Analyze.YES, analyzer = @Analyzer(definition = "standard"))
38+
*/
39+
optional string body = 2;
40+
41+
/**
42+
* @Field(index = Index.YES, store = Store.NO, analyze = Analyze.NO)
43+
*/
44+
optional int64 moment = 3;
45+
46+
/**
47+
* @Field(index = Index.YES, store = Store.NO, analyze = Analyze.NO)
48+
*/
49+
optional string username = 4;
50+
}
51+
52+
message Board_Message_id {
53+
required string Board_username = 1;
54+
required int32 order = 2;
55+
}
56+
57+
message Board_Message {
58+
required string Board_username = 1;
59+
optional int64 messages_id = 2;
60+
required int32 order = 3;
61+
}
62+
63+
message Tag_id {
64+
required string name = 1;
65+
}
66+
67+
message Tag {
68+
required string name = 1;
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package org.hibernate.demo.message.post.it;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.List;
7+
import javax.inject.Inject;
8+
9+
import org.hibernate.demo.message.post.core.entity.Message;
10+
import org.hibernate.demo.message.post.core.repo.MessageRepo;
11+
import org.hibernate.demo.message.post.core.service.MessageService;
12+
import org.hibernate.demo.message.post.core.service.exception.ResourceNotFoundException;
13+
import org.hibernate.demo.message.post.util.DeploymentUtil;
14+
15+
import org.junit.After;
16+
import org.junit.Before;
17+
import org.junit.Test;
18+
import org.junit.runner.RunWith;
19+
20+
import org.jboss.arquillian.container.test.api.Deployment;
21+
import org.jboss.arquillian.container.test.api.TargetsContainer;
22+
import org.jboss.arquillian.junit.Arquillian;
23+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
24+
import org.jboss.shrinkwrap.api.spec.WebArchive;
25+
26+
import org.slf4j.Logger;
27+
28+
/**
29+
* @author Fabio Massimo Ercoli
30+
*/
31+
@RunWith(Arquillian.class)
32+
public class MessageFullTextSearchIT {
33+
34+
private static final Message[] MESSAGES = {
35+
36+
// Ah Sunflower - Poem by William Blake - 1794 MESSAGES[ 0 - 3 ]
37+
new Message( "wblake", "I was angry with my #friend: I told my wrath, my wrath did end. I was angry with my foe: I told it not, my wrath did grow." ),
38+
new Message( "wblake", "And I watered it in fears, Night and morning with my #tears; And I sunned it with smiles, And with soft deceitful wiles." ),
39+
new Message( "wblake", "And it grew both day and night, Till it bore an apple bright. And my foe beheld it shine. And he knew that it was mine," ),
40+
new Message( "wblake", "And into my garden stole When the night had veiled the pole; In the morning glad I seeMy foe outstretched beneath the tree. " ),
41+
42+
// A song - Poem by William Blake - 1789 MESSAGES[ 4 - 7 ]
43+
new Message( "wblake", "Sweet dreams, form a shade O'er my lovely infant's head! Sweet dreams of pleasant streams By happy, silent, moony beams!" ),
44+
new Message( "wblake", "Sweet Sleep, with soft down Weave thy brows an infant crown Sweet Sleep, #angel mild, Hover o'er my happy child!" ),
45+
new Message( "wblake", "Sweet smiles, in the night Hover over my delight! Sweet smiles, mother's smile, All the livelong night beguile." ),
46+
new Message( "wblake", "Sweet moans, dovelike sighs, Chase not slumber from thine eyes! Sweet moan, sweeter smile, All the dovelike moans beguile." ),
47+
// MESSAGES[ 8 - 11 ]
48+
new Message( "wblake", "Sleep, sleep, happy child! All creation slept and smiled. Sleep, sleep, happy sleep, While o'er thee doth mother weep." ),
49+
new Message( "wblake", "Sweet babe, in thy face Holy image I can trace; Sweet babe, once like thee Thy Maker lay, and wept for me:" ),
50+
new Message( "wblake", "Wept for me, for thee, for all, When He was an infant small. Thou His image ever see, Heavenly face that smiles on thee!" ),
51+
new Message( "wblake", "Smiles on thee, on me, on all, Who became an infant small; Infant smiles are his own smiles; Heaven and earth to peace beguiles." )
52+
};
53+
54+
@Deployment
55+
public static WebArchive doDeploy() {
56+
return DeploymentUtil.wildFly();
57+
}
58+
59+
@Deployment(name = "infinispan", testable = false)
60+
@TargetsContainer("infinispan")
61+
public static JavaArchive getInfinispanDeployment() {
62+
return DeploymentUtil.infinispan();
63+
}
64+
65+
@Inject
66+
private MessageService service;
67+
68+
@Inject
69+
private MessageRepo repo;
70+
71+
@Inject
72+
private Logger log;
73+
74+
@Before
75+
public void before() {
76+
for ( Message message : MESSAGES ) {
77+
service.addMessage( message );
78+
}
79+
}
80+
81+
@After
82+
public void after() throws ResourceNotFoundException {
83+
for ( Message message : MESSAGES ) {
84+
service.deleteMessage( message.getId() );
85+
}
86+
}
87+
88+
@Test
89+
public void fullTextSearchOnPoems() {
90+
List occurrences = repo.findByTerm( "Sweet", 0, 10 );
91+
92+
log.info( "Full Text Search result --> {}", occurrences );
93+
assertEquals( 5, occurrences.size() );
94+
assertTrue( occurrences.contains( MESSAGES[4] ) );
95+
assertTrue( occurrences.contains( MESSAGES[5] ) );
96+
assertTrue( occurrences.contains( MESSAGES[6] ) );
97+
assertTrue( occurrences.contains( MESSAGES[7] ) );
98+
assertTrue( occurrences.contains( MESSAGES[9] ) );
99+
}
100+
}

openshift/message-board/message-service/src/test/java/org/hibernate/demo/message/post/util/DeploymentUtil.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
import java.io.File;
44

55
import org.jboss.shrinkwrap.api.ShrinkWrap;
6-
import org.jboss.shrinkwrap.api.asset.StringAsset;
76
import org.jboss.shrinkwrap.api.spec.JavaArchive;
87
import org.jboss.shrinkwrap.api.spec.WebArchive;
9-
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
10-
import org.jboss.shrinkwrap.descriptor.api.persistence21.PersistenceDescriptor;
11-
import org.jboss.shrinkwrap.descriptor.api.persistence21.PersistenceUnitTransactionType;
128
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
139

1410
public class DeploymentUtil {
@@ -21,25 +17,12 @@ public static WebArchive wildFly() {
2117
// only for test
2218
.addPackages( true, "org.hibernate.demo.message.test" )
2319

24-
.addAsResource( new StringAsset( persistenceXml().exportAsString() ), "META-INF/persistence.xml" )
20+
.addAsResource( "META-INF/persistence.xml" )
2521
.addAsResource( "hotrodclient.properties" )
22+
.addAsResource( "message.proto" )
2623
.addAsWebInfResource( new File( "src/main/webapp/WEB-INF/jboss-deployment-structure.xml" ) );
2724
}
2825

29-
private static PersistenceDescriptor persistenceXml() {
30-
return Descriptors.create( PersistenceDescriptor.class )
31-
.version( "2.1" )
32-
.createPersistenceUnit()
33-
.name( "primary" )
34-
.transactionType( PersistenceUnitTransactionType._JTA )
35-
.provider( "org.hibernate.ogm.jpa.HibernateOgmPersistence" )
36-
.getOrCreateProperties()
37-
.createProperty().name( "hibernate.ogm.datastore.provider" ).value( "infinispan_remote" ).up()
38-
.createProperty().name( "hibernate.ogm.infinispan_remote.configuration_resource_name" ).value( "hotrodclient.properties" ).up()
39-
.createProperty().name( "hibernate.ogm.datastore.create_database" ).value( "true" ).up()
40-
.up().up();
41-
}
42-
4326
public static JavaArchive infinispan() {
4427
File file = Maven.resolver()
4528
.resolve( "org.hibernate.demos.messageboard:message-server-task:jar:" + MavenUtils.getProperty( "project.version" ) )

openshift/message-board/message-service/src/test/resources/arquillian.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<property name="jbossHome">${home.infinispan}</property>
3030
<property name="managementPort">10140</property>
3131
<property name="javaVmArguments">-XX:MaxPermSize=512m -Djava.net.preferIPv4Stack=true -XX:+IgnoreUnrecognizedVMOptions -Djboss.socket.binding.port-offset=150</property>
32-
<property name="serverConfig">clustered.xml</property>
32+
<property name="serverConfig">clustered-with-indexes.xml</property>
3333
<property name="waitForPorts">11372</property>
3434
</configuration>
3535
</container>

openshift/message-board/message-service/src/test/resources/hibernate.properties

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)