Author: hardy.ferentschik
Date: 2008-11-10 15:22:44 -0500 (Mon, 10 Nov 2008)
New Revision: 15542
Modified:
search/trunk/src/java/org/hibernate/search/FullTextSession.java
search/trunk/src/java/org/hibernate/search/backend/Work.java
search/trunk/src/java/org/hibernate/search/backend/impl/BatchedQueueingProcessor.java
search/trunk/src/java/org/hibernate/search/event/FullTextIndexEventListener.java
Log:
HSEARCH-281 - made Work to Work<T>. Also changed the FullTextSession interface. Not
sure if this is a good idea. Needs to be discussed.
Modified: search/trunk/src/java/org/hibernate/search/FullTextSession.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/FullTextSession.java 2008-11-10 20:14:05
UTC (rev 15541)
+++ search/trunk/src/java/org/hibernate/search/FullTextSession.java 2008-11-10 20:22:44
UTC (rev 15542)
@@ -6,15 +6,20 @@
import org.hibernate.classic.Session;
/**
- * Extends the Hibernate {@link Session} with Full text search and indexing capabilities
+ * Extends the Hibernate {@link Session} with fulltext search and indexing capabilities.
*
* @author Emmanuel Bernard
*/
public interface FullTextSession extends Session {
/**
- * Create a Query on top of a native Lucene Query returning the matching objects
+ * Create a fulltext query on top of a native Lucene query returning the matching
objects
* of type <code>entities</code> and their respective subclasses.
- * If no entity is provided, no type filtering is done.
+ *
+ * @param luceneQuery The native Lucene query to be rn against the Lucene index.
+ * @param entities List of classes for type filtering. The query result will only return
entities of
+ * the specified types and their respective subtype. If no class is specified no type
filtering will take place.
+ *
+ * @return A <code>FullTextQuery</code> wrapping around the native Lucene
wuery.
*/
FullTextQuery createFullTextQuery(org.apache.lucene.search.Query luceneQuery,
Class<?>... entities);
@@ -24,12 +29,13 @@
* will not affect the index at least until commit.
*
* @param entity The entity to index - must not be <code>null</code>.
+ *
* @throws IllegalArgumentException if entity is null or not an @Indexed entity
*/
- void index(Object entity);
+ <T> void index(T entity);
/**
- * return the SearchFactory
+ * @return the <code>SearchFactory</code> instance.
*/
SearchFactory getSearchFactory();
@@ -43,15 +49,16 @@
*
* @throws IllegalArgumentException if entityType is <code>null</codE> or
not an @Indexed entity type.
*/
- public void purge(Class<?> entityType, Serializable id);
+ public <T> void purge(Class<T> entityType, Serializable id);
/**
* Remove all entities from of particular class and all its subclasses from the index.
*
* @param entityType The class of the entities to remove.
+ *
* @throws IllegalArgumentException if entityType is <code>null</code> or
not an @Indexed entity type.
*/
- public void purgeAll(Class<?> entityType);
+ public <T> void purgeAll(Class<T> entityType);
/**
* flush full text changes to the index
Modified: search/trunk/src/java/org/hibernate/search/backend/Work.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/backend/Work.java 2008-11-10 20:14:05 UTC
(rev 15541)
+++ search/trunk/src/java/org/hibernate/search/backend/Work.java 2008-11-10 20:22:44 UTC
(rev 15542)
@@ -6,31 +6,31 @@
import org.hibernate.annotations.common.reflection.XMember;
/**
- * work unit. Only make sense inside the same session since it uses the scope principle
+ * A unit of work. Only make sense inside the same session since it uses the scope
principle.
*
* @author Emmanuel Bernard
*/
-public class Work {
- private final Object entity;
- private final Class entityClass;
+public class Work<T> {
+ private final T entity;
+ private final Class<T> entityClass;
private final Serializable id;
private final XMember idGetter;
private final WorkType type;
- public Work(Object entity, Serializable id, WorkType type) {
+ public Work(T entity, Serializable id, WorkType type) {
this( entity, null, id, null, type );
}
- public Work(Class entityType, Serializable id, WorkType type) {
+ public Work(Class<T> entityType, Serializable id, WorkType type) {
this( null, entityType, id, null, type );
}
- public Work(Object entity, XMember idGetter, WorkType type) {
+ public Work(T entity, XMember idGetter, WorkType type) {
this( entity, null, null, idGetter, type );
}
-
- private Work(Object entity, Class entityClass, Serializable id,
- XMember idGetter, WorkType type) {
+
+ private Work(T entity, Class<T> entityClass, Serializable id,
+ XMember idGetter, WorkType type) {
this.entity = entity;
this.entityClass = entityClass;
this.id = id;
@@ -38,11 +38,11 @@
this.type = type;
}
- public Class getEntityClass() {
+ public Class<T> getEntityClass() {
return entityClass;
}
- public Object getEntity() {
+ public T getEntity() {
return entity;
}
Modified:
search/trunk/src/java/org/hibernate/search/backend/impl/BatchedQueueingProcessor.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/backend/impl/BatchedQueueingProcessor.java 2008-11-10
20:14:05 UTC (rev 15541)
+++
search/trunk/src/java/org/hibernate/search/backend/impl/BatchedQueueingProcessor.java 2008-11-10
20:22:44 UTC (rev 15542)
@@ -53,7 +53,9 @@
//default to a simple asynchronous operation
int min = ConfigurationParseHelper.getIntValue( properties,
Environment.WORKER_THREADPOOL_SIZE, 1 );
//no queue limit
- int queueSize = ConfigurationParseHelper.getIntValue( properties,
Environment.WORKER_WORKQUEUE_SIZE, Integer.MAX_VALUE );
+ int queueSize = ConfigurationParseHelper.getIntValue(
+ properties, Environment.WORKER_WORKQUEUE_SIZE, Integer.MAX_VALUE
+ );
batchSize = ConfigurationParseHelper.getIntValue( properties,
Environment.WORKER_BATCHSIZE, 0 );
@@ -67,7 +69,7 @@
*/
executorService = new ThreadPoolExecutor(
min, min, 60, TimeUnit.SECONDS,
- new LinkedBlockingQueue<Runnable>(queueSize),
+ new LinkedBlockingQueue<Runnable>( queueSize ),
new ThreadPoolExecutor.CallerRunsPolicy()
);
}
@@ -84,15 +86,15 @@
else {
try {
Class processorFactoryClass = ReflectHelper.classForName( backend,
BatchedQueueingProcessor.class );
- backendQueueProcessorFactory = (BackendQueueProcessorFactory)
processorFactoryClass.newInstance();
+ backendQueueProcessorFactory = ( BackendQueueProcessorFactory )
processorFactoryClass.newInstance();
}
- catch (ClassNotFoundException e) {
+ catch ( ClassNotFoundException e ) {
throw new SearchException( "Unable to find processor class: " + backend, e
);
}
- catch (IllegalAccessException e) {
+ catch ( IllegalAccessException e ) {
throw new SearchException( "Unable to instanciate processor class: " +
backend, e );
}
- catch (InstantiationException e) {
+ catch ( InstantiationException e ) {
throw new SearchException( "Unable to instanciate processor class: " +
backend, e );
}
}
@@ -129,9 +131,9 @@
}
private void processWorkByLayer(List<Work> queue, int initialSize,
List<LuceneWork> luceneQueue, Layer layer) {
- for ( int i = 0 ; i < initialSize ; i++ ) {
+ for ( int i = 0; i < initialSize; i++ ) {
Work work = queue.get( i );
- if ( work != null) {
+ if ( work != null ) {
if ( layer.isRightLayer( work.getType() ) ) {
queue.set( i, null ); // help GC and avoid 2 loaded queues in memory
addWorkToBuilderQueue( luceneQueue, work );
@@ -140,19 +142,22 @@
}
}
- private <T> void addWorkToBuilderQueue(List<LuceneWork> luceneQueue, Work
work) {
- @SuppressWarnings( "unchecked" )
+ private <T> void addWorkToBuilderQueue(List<LuceneWork> luceneQueue,
Work<T> work) {
+ @SuppressWarnings("unchecked")
Class<T> entityClass = work.getEntityClass() != null ?
- work.getEntityClass() :
- Hibernate.getClass( work.getEntity() );
+ work.getEntityClass() :
+ Hibernate.getClass( work.getEntity() );
DocumentBuilder<T> builder = searchFactoryImplementor.getDocumentBuilder(
entityClass );
if ( builder == null ) {
//might be a entity contained in
builder = searchFactoryImplementor.getContainedInOnlyBuilder( entityClass );
}
- if ( builder == null ) return;
- //TODO remove casting when Work is Work<T>
- builder.addWorkToQueue(entityClass, (T) work.getEntity(), work.getId(), work.getType(),
luceneQueue, searchFactoryImplementor );
+ if ( builder == null ) {
+ return;
+ }
+ builder.addWorkToQueue(
+ entityClass, work.getEntity(), work.getId(), work.getType(), luceneQueue,
searchFactoryImplementor
+ );
}
//TODO implements parallel batchWorkers (one per Directory)
@@ -177,20 +182,22 @@
try {
executorService.awaitTermination( Long.MAX_VALUE, TimeUnit.SECONDS );
}
- catch (InterruptedException e) {
+ catch ( InterruptedException e ) {
log.error( "Unable to properly shut down asynchronous indexing work", e );
}
}
}
private static enum Layer {
- FIRST,
+ FIRST,
SECOND;
public boolean isRightLayer(WorkType type) {
- if (this == FIRST && type != WorkType.COLLECTION) return true;
+ if ( this == FIRST && type != WorkType.COLLECTION ) {
+ return true;
+ }
return this == SECOND && type == WorkType.COLLECTION;
- }
+ }
}
}
Modified:
search/trunk/src/java/org/hibernate/search/event/FullTextIndexEventListener.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/event/FullTextIndexEventListener.java 2008-11-10
20:14:05 UTC (rev 15541)
+++
search/trunk/src/java/org/hibernate/search/event/FullTextIndexEventListener.java 2008-11-10
20:22:44 UTC (rev 15542)
@@ -26,7 +26,6 @@
import org.hibernate.search.backend.Work;
import org.hibernate.search.backend.WorkType;
import org.hibernate.search.backend.impl.EventSourceTransactionContext;
-import org.hibernate.search.engine.DocumentBuilder;
import org.hibernate.search.engine.SearchFactoryImplementor;
import org.hibernate.search.util.LoggerFactory;
@@ -103,8 +102,8 @@
}
}
- protected void processWork(Object entity, Serializable id, WorkType workType,
AbstractEvent event) {
- Work work = new Work( entity, id, workType );
+ protected <T> void processWork(T entity, Serializable id, WorkType workType,
AbstractEvent event) {
+ Work<T> work = new Work<T>( entity, id, workType );
final EventSourceTransactionContext transactionContext = new
EventSourceTransactionContext( event.getSession() );
searchFactoryImplementor.getWorker().performWork( work, transactionContext );
}