Author: sannegrinovero
Date: 2008-09-29 11:35:13 -0400 (Mon, 29 Sep 2008)
New Revision: 15224
Added:
search/trunk/src/java/org/hibernate/search/backend/WorkVisitor.java
Modified:
search/trunk/src/java/org/hibernate/search/backend/AddLuceneWork.java
search/trunk/src/java/org/hibernate/search/backend/DeleteLuceneWork.java
search/trunk/src/java/org/hibernate/search/backend/LuceneWork.java
search/trunk/src/java/org/hibernate/search/backend/OptimizeLuceneWork.java
search/trunk/src/java/org/hibernate/search/backend/PurgeAllLuceneWork.java
search/trunk/src/java/org/hibernate/search/engine/DocumentBuilder.java
search/trunk/src/java/org/hibernate/search/engine/SearchFactoryImplementor.java
search/trunk/src/java/org/hibernate/search/impl/SearchFactoryImpl.java
Log:
backend refactoring, step1: make all LuceneWork threadsafe(was needed anyway) and
"visitable".
Modified: search/trunk/src/java/org/hibernate/search/backend/AddLuceneWork.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/backend/AddLuceneWork.java 2008-09-26
18:56:55 UTC (rev 15223)
+++ search/trunk/src/java/org/hibernate/search/backend/AddLuceneWork.java 2008-09-29
15:35:13 UTC (rev 15224)
@@ -13,6 +13,16 @@
private static final long serialVersionUID = -2450349312813297371L;
public AddLuceneWork(Serializable id, String idInString, Class entity, Document
document) {
- super( id, idInString, entity, document );
+ super( id, idInString, entity, document, false );
}
+
+ public AddLuceneWork(Serializable id, String idInString, Class entity, Document
document, boolean batch) {
+ super( id, idInString, entity, document, batch );
+ }
+
+ @Override
+ public <T> T getWorkDelegate(final WorkVisitor<T> visitor) {
+ return visitor.getDelegate( this );
+ }
+
}
Modified: search/trunk/src/java/org/hibernate/search/backend/DeleteLuceneWork.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/backend/DeleteLuceneWork.java 2008-09-26
18:56:55 UTC (rev 15223)
+++ search/trunk/src/java/org/hibernate/search/backend/DeleteLuceneWork.java 2008-09-29
15:35:13 UTC (rev 15224)
@@ -7,9 +7,16 @@
* @author Emmanuel Bernard
*/
public class DeleteLuceneWork extends LuceneWork implements Serializable {
+
private static final long serialVersionUID = -854604138119230246L;
public DeleteLuceneWork(Serializable id, String idInString, Class entity) {
super( id, idInString, entity );
}
+
+ @Override
+ public <T> T getWorkDelegate(final WorkVisitor<T> visitor) {
+ return visitor.getDelegate( this );
+ }
+
}
Modified: search/trunk/src/java/org/hibernate/search/backend/LuceneWork.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/backend/LuceneWork.java 2008-09-26 18:56:55
UTC (rev 15223)
+++ search/trunk/src/java/org/hibernate/search/backend/LuceneWork.java 2008-09-29 15:35:13
UTC (rev 15224)
@@ -17,6 +17,7 @@
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
+ * @author Sanne Grinovero
*/
public abstract class LuceneWork implements Serializable {
@@ -27,7 +28,7 @@
/**
* Flag indicating if this lucene work has to be indexed in batch mode.
*/
- private boolean batch = false;
+ private final boolean batch;
private final String idInString;
public LuceneWork(Serializable id, String idInString, Class entity) {
@@ -35,21 +36,21 @@
}
public LuceneWork(Serializable id, String idInString, Class entity, Document document)
{
+ this( id, idInString, entity, document, false );
+ }
+
+ public LuceneWork(Serializable id, String idInString, Class entity, Document document,
boolean batch) {
this.id = id;
this.idInString = idInString;
this.entityClass = entity;
this.document = document;
+ this.batch = batch;
}
public boolean isBatch() {
return batch;
}
- //TODO move to final field, or enable synchronization?
- public void setBatch(boolean batch) {
- this.batch = batch;
- }
-
public Document getDocument() {
return document;
}
@@ -65,4 +66,7 @@
public String getIdInString() {
return idInString;
}
+
+ public abstract <T> T getWorkDelegate(WorkVisitor<T> visitor);
+
}
Modified: search/trunk/src/java/org/hibernate/search/backend/OptimizeLuceneWork.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/backend/OptimizeLuceneWork.java 2008-09-26
18:56:55 UTC (rev 15223)
+++ search/trunk/src/java/org/hibernate/search/backend/OptimizeLuceneWork.java 2008-09-29
15:35:13 UTC (rev 15224)
@@ -4,15 +4,22 @@
import java.io.Serializable;
/**
- * A unit of work triggering an optimize operation
+ * A unit of work triggering an optimize operation.
* This work does not propagate to a cluster: it should be filtered before being sent to
- * the network
+ * the network.
*
* @author Andrew Hahn
* @author Emmanuel Bernard
*/
public class OptimizeLuceneWork extends LuceneWork implements Serializable {
+
public OptimizeLuceneWork(Class entity) {
super( null, null, entity );
}
+
+ @Override
+ public <T> T getWorkDelegate(final WorkVisitor<T> visitor) {
+ return visitor.getDelegate( this );
+ }
+
}
Modified: search/trunk/src/java/org/hibernate/search/backend/PurgeAllLuceneWork.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/backend/PurgeAllLuceneWork.java 2008-09-26
18:56:55 UTC (rev 15223)
+++ search/trunk/src/java/org/hibernate/search/backend/PurgeAllLuceneWork.java 2008-09-29
15:35:13 UTC (rev 15224)
@@ -9,9 +9,16 @@
* @author John Griffin
*/
public class PurgeAllLuceneWork extends LuceneWork implements Serializable {
+
private static final long serialVersionUID = 8124091288284011715L;
public PurgeAllLuceneWork(Class entity) {
super( null, null, entity, null );
}
+
+ @Override
+ public <T> T getWorkDelegate(final WorkVisitor<T> visitor) {
+ return visitor.getDelegate( this );
+ }
+
}
Added: search/trunk/src/java/org/hibernate/search/backend/WorkVisitor.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/backend/WorkVisitor.java
(rev 0)
+++ search/trunk/src/java/org/hibernate/search/backend/WorkVisitor.java 2008-09-29
15:35:13 UTC (rev 15224)
@@ -0,0 +1,22 @@
+package org.hibernate.search.backend;
+
+/**
+ * A visitor delegate to manipulate a LuceneWork
+ * needs to implement this interface.
+ * This pattern enables any implementation to virtually add delegate
+ * methods to the base LuceneWork without having to change them.
+ * This contract however breaks if more subclasses of LuceneWork
+ * are created, as a visitor must support all existing types.
+ *
+ * @author Sanne Grinovero
+ *
+ * @param <T> used to force a return type of choice.
+ */
+public interface WorkVisitor<T> {
+
+ T getDelegate(AddLuceneWork addLuceneWork);
+ T getDelegate(DeleteLuceneWork deleteLuceneWork);
+ T getDelegate(OptimizeLuceneWork optimizeLuceneWork);
+ T getDelegate(PurgeAllLuceneWork purgeAllLuceneWork);
+
+}
Modified: search/trunk/src/java/org/hibernate/search/engine/DocumentBuilder.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/engine/DocumentBuilder.java 2008-09-26
18:56:55 UTC (rev 15223)
+++ search/trunk/src/java/org/hibernate/search/engine/DocumentBuilder.java 2008-09-29
15:35:13 UTC (rev 15224)
@@ -544,12 +544,9 @@
else if ( workType == WorkType.INDEX ) {
Document doc = getDocument( entity, id );
queue.add( new DeleteLuceneWork( id, idInString, entityClass ) );
- LuceneWork work = new AddLuceneWork( id, idInString, entityClass, doc );
- work.setBatch( true );
- queue.add( work );
+ queue.add( new AddLuceneWork( id, idInString, entityClass, doc, true ) );
searchForContainers = true;
}
-
else {
throw new AssertionFailure( "Unknown WorkType: " + workType );
}
Modified: search/trunk/src/java/org/hibernate/search/engine/SearchFactoryImplementor.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/engine/SearchFactoryImplementor.java 2008-09-26
18:56:55 UTC (rev 15223)
+++
search/trunk/src/java/org/hibernate/search/engine/SearchFactoryImplementor.java 2008-09-29
15:35:13 UTC (rev 15224)
@@ -3,7 +3,7 @@
import java.util.Map;
import java.util.Set;
-import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
import org.hibernate.search.SearchFactory;
import org.hibernate.search.backend.BackendQueueProcessorFactory;
@@ -51,7 +51,7 @@
Set<DirectoryProvider> getDirectoryProviders();
- Lock getDirectoryProviderLock(DirectoryProvider dp);
+ ReentrantLock getDirectoryProviderLock(DirectoryProvider dp);
void addDirectoryProvider(DirectoryProvider<?> provider);
Modified: search/trunk/src/java/org/hibernate/search/impl/SearchFactoryImpl.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/impl/SearchFactoryImpl.java 2008-09-26
18:56:55 UTC (rev 15223)
+++ search/trunk/src/java/org/hibernate/search/impl/SearchFactoryImpl.java 2008-09-29
15:35:13 UTC (rev 15224)
@@ -14,7 +14,6 @@
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.lucene.analysis.Analyzer;
@@ -392,12 +391,12 @@
}
private static class DirectoryProviderData {
- public final Lock dirLock = new ReentrantLock();
+ public final ReentrantLock dirLock = new ReentrantLock();
public OptimizerStrategy optimizerStrategy;
public Set<Class> classes = new HashSet<Class>(2);
}
- public Lock getDirectoryProviderLock(DirectoryProvider dp) {
+ public ReentrantLock getDirectoryProviderLock(DirectoryProvider dp) {
if (barrier != 0) {} //read barrier
return this.dirProviderData.get( dp ).dirLock;
}