Author: epbernard
Date: 2008-06-21 09:47:31 -0400 (Sat, 21 Jun 2008)
New Revision: 14794
Added:
search/trunk/src/test/org/hibernate/search/test/session/Domain.java
search/trunk/src/test/org/hibernate/search/test/session/MassIndexUsingManualFlushTest.java
Modified:
search/trunk/src/java/org/hibernate/search/FullTextSession.java
search/trunk/src/java/org/hibernate/search/backend/Worker.java
search/trunk/src/java/org/hibernate/search/backend/impl/PostTransactionWorkQueueSynchronization.java
search/trunk/src/java/org/hibernate/search/backend/impl/TransactionalWorker.java
search/trunk/src/java/org/hibernate/search/impl/FullTextSessionImpl.java
search/trunk/src/java/org/hibernate/search/jpa/FullTextEntityManager.java
search/trunk/src/java/org/hibernate/search/jpa/impl/FullTextEntityManagerImpl.java
search/trunk/src/test/org/hibernate/search/test/session/Email.java
search/trunk/src/test/org/hibernate/search/test/session/MassIndexTest.java
search/trunk/src/test/org/hibernate/search/test/session/OptimizeTest.java
search/trunk/src/test/org/hibernate/search/test/session/SessionTest.java
Log:
HSEARCH-220 introduce session.flushToIndexes
Modified: search/trunk/src/java/org/hibernate/search/FullTextSession.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/FullTextSession.java 2008-06-20 22:27:08
UTC (rev 14793)
+++ search/trunk/src/java/org/hibernate/search/FullTextSession.java 2008-06-21 13:47:31
UTC (rev 14794)
@@ -50,4 +50,10 @@
* @throws IllegalArgumentException if entityType is null or not an @Indexed entity
type
*/
public void purgeAll(Class entityType);
+
+ /**
+ * flush full text changes to the index
+ * Force Hibernate Search to apply all changes to the index no waiting for the batch
limit
+ */
+ public void flushToIndexes();
}
Modified: search/trunk/src/java/org/hibernate/search/backend/Worker.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/backend/Worker.java 2008-06-20 22:27:08 UTC
(rev 14793)
+++ search/trunk/src/java/org/hibernate/search/backend/Worker.java 2008-06-21 13:47:31 UTC
(rev 14794)
@@ -22,4 +22,9 @@
* This method can return exceptions
*/
void close();
+
+ /**
+ * flush any work queue
+ */
+ void flushWorks(EventSource session);
}
Modified:
search/trunk/src/java/org/hibernate/search/backend/impl/PostTransactionWorkQueueSynchronization.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/backend/impl/PostTransactionWorkQueueSynchronization.java 2008-06-20
22:27:08 UTC (rev 14793)
+++
search/trunk/src/java/org/hibernate/search/backend/impl/PostTransactionWorkQueueSynchronization.java 2008-06-21
13:47:31 UTC (rev 14794)
@@ -56,4 +56,10 @@
if (queuePerTransaction != null) queuePerTransaction.removeValue( this );
}
}
+
+ public void flushWorks() {
+ WorkQueue subQueue = queue.splitQueue();
+ queueingProcessor.prepareWorks( subQueue );
+ queueingProcessor.performWorks( subQueue );
+ }
}
Modified:
search/trunk/src/java/org/hibernate/search/backend/impl/TransactionalWorker.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/backend/impl/TransactionalWorker.java 2008-06-20
22:27:08 UTC (rev 14793)
+++
search/trunk/src/java/org/hibernate/search/backend/impl/TransactionalWorker.java 2008-06-21
13:47:31 UTC (rev 14794)
@@ -54,4 +54,15 @@
queueingProcessor.close();
}
+ public void flushWorks(EventSource session) {
+ if ( session.isTransactionInProgress() ) {
+ Transaction transaction = session.getTransaction();
+ PostTransactionWorkQueueSynchronization txSync =
(PostTransactionWorkQueueSynchronization)
+ synchronizationPerTransaction.get( transaction );
+ if ( txSync != null && ! txSync.isConsumed() ) {
+ txSync.flushWorks();
+ }
+ }
+ }
+
}
Modified: search/trunk/src/java/org/hibernate/search/impl/FullTextSessionImpl.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/impl/FullTextSessionImpl.java 2008-06-20
22:27:08 UTC (rev 14793)
+++ search/trunk/src/java/org/hibernate/search/impl/FullTextSessionImpl.java 2008-06-21
13:47:31 UTC (rev 14794)
@@ -92,6 +92,11 @@
purge( entityType, null );
}
+ public void flushToIndexes() {
+ SearchFactoryImplementor searchFactoryImplementor = getSearchFactoryImplementor();
+ searchFactoryImplementor.getWorker().flushWorks(eventSource);
+ }
+
/**
* Remove a particular entity from a particular class of an index.
*
Modified: search/trunk/src/java/org/hibernate/search/jpa/FullTextEntityManager.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/jpa/FullTextEntityManager.java 2008-06-20
22:27:08 UTC (rev 14793)
+++ search/trunk/src/java/org/hibernate/search/jpa/FullTextEntityManager.java 2008-06-21
13:47:31 UTC (rev 14794)
@@ -50,4 +50,10 @@
*/
public void purgeAll(Class entityType);
+ /**
+ * flush index change
+ * Force Hibernate Search to apply all changes to the index no waiting for the batch
limit
+ */
+ public void flushToIndexes();
+
}
Modified:
search/trunk/src/java/org/hibernate/search/jpa/impl/FullTextEntityManagerImpl.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/jpa/impl/FullTextEntityManagerImpl.java 2008-06-20
22:27:08 UTC (rev 14793)
+++
search/trunk/src/java/org/hibernate/search/jpa/impl/FullTextEntityManagerImpl.java 2008-06-21
13:47:31 UTC (rev 14794)
@@ -77,7 +77,11 @@
getFullTextSession().purgeAll( entityType );
}
+ public void flushToIndexes() {
+ getFullTextSession().flushToIndexes();
+ }
+
public void persist(Object entity) {
em.persist( entity );
}
Added: search/trunk/src/test/org/hibernate/search/test/session/Domain.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/session/Domain.java
(rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/session/Domain.java 2008-06-21
13:47:31 UTC (rev 14794)
@@ -0,0 +1,37 @@
+package org.hibernate.search.test.session;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+@Indexed
+public class Domain {
+ @Id
+ @DocumentId
+ private Integer id;
+ @Field
+ private String name;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Modified: search/trunk/src/test/org/hibernate/search/test/session/Email.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/session/Email.java 2008-06-20 22:27:08
UTC (rev 14793)
+++ search/trunk/src/test/org/hibernate/search/test/session/Email.java 2008-06-21 13:47:31
UTC (rev 14794)
@@ -4,11 +4,14 @@
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
+import javax.persistence.ManyToOne;
+import javax.persistence.FetchType;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.IndexedEmbedded;
/**
* @author Emmanuel Bernard
@@ -28,7 +31,17 @@
private String header;
+ @IndexedEmbedded @ManyToOne(fetch = FetchType.LAZY)
+ private Domain domain;
+ public Domain getDomain() {
+ return domain;
+ }
+
+ public void setDomain(Domain domain) {
+ this.domain = domain;
+ }
+
public Long getId() {
return id;
}
Modified: search/trunk/src/test/org/hibernate/search/test/session/MassIndexTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/session/MassIndexTest.java 2008-06-20
22:27:08 UTC (rev 14793)
+++ search/trunk/src/test/org/hibernate/search/test/session/MassIndexTest.java 2008-06-21
13:47:31 UTC (rev 14794)
@@ -31,8 +31,10 @@
int loop = 14;
for (int i = 0; i < loop; i++) {
Statement statmt = s.connection().createStatement();
- statmt.executeUpdate( "insert into Email(id, title, body, header) values( +
"
- + ( i + 1 ) + ", 'Bob Sponge', 'Meet the guys who create the
software', 'nope')" );
+ statmt.executeUpdate( "insert into Domain(id, name) values( + "
+ + ( i + 1 ) + ", 'sponge" + i + "')" );
+ statmt.executeUpdate( "insert into Email(id, title, body, header, domain_id)
values( + "
+ + ( i + 1 ) + ", 'Bob Sponge', 'Meet the guys who create the
software', 'nope', " + ( i + 1 ) +")" );
statmt.close();
}
tx.commit();
@@ -61,6 +63,7 @@
s.close();
}
+
public void testTransactional() throws Exception {
FullTextSession s = Search.createFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
@@ -192,7 +195,8 @@
return new Class[] {
Email.class,
Entite.class,
- Categorie.class
+ Categorie.class,
+ Domain.class
};
}
}
Added:
search/trunk/src/test/org/hibernate/search/test/session/MassIndexUsingManualFlushTest.java
===================================================================
---
search/trunk/src/test/org/hibernate/search/test/session/MassIndexUsingManualFlushTest.java
(rev 0)
+++
search/trunk/src/test/org/hibernate/search/test/session/MassIndexUsingManualFlushTest.java 2008-06-21
13:47:31 UTC (rev 14794)
@@ -0,0 +1,74 @@
+package org.hibernate.search.test.session;
+
+import java.sql.Statement;
+import java.util.List;
+
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.Search;
+import org.hibernate.search.Environment;
+import org.hibernate.search.test.SearchTestCase;
+import org.hibernate.search.impl.FullTextSessionImpl;
+import org.hibernate.Transaction;
+import org.hibernate.ScrollableResults;
+import org.hibernate.ScrollMode;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.analysis.StopAnalyzer;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class MassIndexUsingManualFlushTest extends SearchTestCase {
+ public void testManualIndexFlush() throws Exception {
+ FullTextSession s = Search.createFullTextSession( openSession() );
+ Transaction tx = s.beginTransaction();
+ int loop = 14;
+ for (int i = 0; i < loop; i++) {
+ Statement statmt = s.connection().createStatement();
+ statmt.executeUpdate( "insert into Domain(id, name) values( + "
+ + ( i + 1 ) + ", 'sponge" + i + "')" );
+ statmt.executeUpdate( "insert into Email(id, title, body, header, domain_id)
values( + "
+ + ( i + 1 ) + ", 'Bob Sponge', 'Meet the guys who create the
software', 'nope', " + ( i + 1 ) +")" );
+ statmt.close();
+ }
+ tx.commit();
+ s.close();
+
+ //check non created object does get found!!1
+ s = new FullTextSessionImpl( openSession() );
+ tx = s.beginTransaction();
+ ScrollableResults results = s.createCriteria( Email.class ).scroll(
ScrollMode.FORWARD_ONLY );
+ int index = 0;
+ while ( results.next() ) {
+ index++;
+ final Email o = (Email) results.get( 0 );
+ s.index( o );
+ if ( index % 5 == 0 ) {
+ s.flushToIndexes();
+ s.clear();
+ }
+ }
+ tx.commit();
+ s.clear();
+ tx = s.beginTransaction();
+ QueryParser parser = new QueryParser( "id", new StopAnalyzer() );
+ List result = s.createFullTextQuery( parser.parse( "body:create" ) ).list();
+ assertEquals( 14, result.size() );
+ for (Object object : result) {
+ s.delete( object );
+ }
+ tx.commit();
+ s.close();
+ }
+
+ protected void configure(org.hibernate.cfg.Configuration cfg) {
+ super.configure( cfg );
+ cfg.setProperty( Environment.ANALYZER_CLASS, StopAnalyzer.class.getName() );
+ }
+
+ protected Class[] getMappings() {
+ return new Class[] {
+ Email.class,
+ Domain.class
+ };
+ }
+}
Modified: search/trunk/src/test/org/hibernate/search/test/session/OptimizeTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/session/OptimizeTest.java 2008-06-20
22:27:08 UTC (rev 14793)
+++ search/trunk/src/test/org/hibernate/search/test/session/OptimizeTest.java 2008-06-21
13:47:31 UTC (rev 14794)
@@ -84,7 +84,8 @@
protected Class[] getMappings() {
return new Class[] {
- Email.class
+ Email.class,
+ Domain.class
};
}
}
Modified: search/trunk/src/test/org/hibernate/search/test/session/SessionTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/session/SessionTest.java 2008-06-20
22:27:08 UTC (rev 14793)
+++ search/trunk/src/test/org/hibernate/search/test/session/SessionTest.java 2008-06-21
13:47:31 UTC (rev 14794)
@@ -32,7 +32,7 @@
wrapper
);
try {
- FullTextSession fts = Search.createFullTextSession( wrapped );
+ Search.createFullTextSession( wrapped );
}
catch( ClassCastException e ) {
e.printStackTrace( );
@@ -57,7 +57,8 @@
protected Class[] getMappings() {
return new Class[] {
- Email.class
+ Email.class,
+ Domain.class
};
}
}