Author: epbernard
Date: 2010-06-30 09:45:30 -0400 (Wed, 30 Jun 2010)
New Revision: 19872
Added:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/spi/SearchFactoryIntegrator.java
Removed:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/IncrementalSearchFactory.java
Modified:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/SearchFactoryImplementor.java
search/trunk/hibernate-search/src/main/java/org/hibernate/search/impl/ImmutableSearchFactory.java
search/trunk/hibernate-search/src/main/java/org/hibernate/search/impl/MutableSearchFactory.java
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/configuration/mutablefactory/MutableFactoryTest.java
Log:
HSEARCH-397 Commit a simplified contract to add new classes to a SearchFactory
Introduce the notion of SearchFactoryIntegrator containing contracts like:
- addClasses
- getWorker (for indexing)
- close
Simplify test based on new SearchFactoryIntegrator contract
Deleted:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/IncrementalSearchFactory.java
===================================================================
---
search/trunk/hibernate-search/src/main/java/org/hibernate/search/IncrementalSearchFactory.java 2010-06-30
13:44:30 UTC (rev 19871)
+++
search/trunk/hibernate-search/src/main/java/org/hibernate/search/IncrementalSearchFactory.java 2010-06-30
13:45:30 UTC (rev 19872)
@@ -1,19 +0,0 @@
-package org.hibernate.search;
-
-/**
- * This contract is considered experimental
- * Allow modifications of the SearchFactory internals
- *
- * As of today allow addition of new indexed classes.
- *
- * @author Emmanuel Bernard
- */
-public interface IncrementalSearchFactory extends SearchFactory {
- /**
- * Add the following classes to the SearchFactory
- *
- */
- void addClasses(Class<?>... classes);
-
- //TODO consider accepting SearchConfiguration or SearchMapping
-}
Modified:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/SearchFactoryImplementor.java
===================================================================
---
search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/SearchFactoryImplementor.java 2010-06-30
13:44:30 UTC (rev 19871)
+++
search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/SearchFactoryImplementor.java 2010-06-30
13:45:30 UTC (rev 19872)
@@ -36,6 +36,7 @@
import org.hibernate.search.backend.impl.batchlucene.BatchBackend;
import org.hibernate.search.batchindexing.MassIndexerProgressMonitor;
import org.hibernate.search.filter.FilterCachingStrategy;
+import org.hibernate.search.spi.SearchFactoryIntegrator;
import org.hibernate.search.store.DirectoryProvider;
import org.hibernate.search.store.optimization.OptimizerStrategy;
import org.hibernate.search.exception.ErrorHandler;
@@ -46,7 +47,7 @@
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
-public interface SearchFactoryImplementor extends SearchFactory {
+public interface SearchFactoryImplementor extends SearchFactoryIntegrator {
BackendQueueProcessorFactory getBackendQueueProcessorFactory();
Map<Class<?>, DocumentBuilderIndexedEntity<?>>
getDocumentBuildersIndexedEntities();
@@ -55,8 +56,6 @@
<T> DocumentBuilderContainedEntity<T>
getDocumentBuilderContainedEntity(Class<T> entityType);
- Worker getWorker();
-
OptimizerStrategy getOptimizerStrategy(DirectoryProvider<?> provider);
FilterCachingStrategy getFilterCachingStrategy();
@@ -67,8 +66,6 @@
String getIndexingStrategy();
- void close();
-
Set<Class<?>> getClassesInDirectoryProvider(DirectoryProvider<?>
directoryProvider);
Set<DirectoryProvider<?>> getDirectoryProviders();
Modified:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/impl/ImmutableSearchFactory.java
===================================================================
---
search/trunk/hibernate-search/src/main/java/org/hibernate/search/impl/ImmutableSearchFactory.java 2010-06-30
13:44:30 UTC (rev 19871)
+++
search/trunk/hibernate-search/src/main/java/org/hibernate/search/impl/ImmutableSearchFactory.java 2010-06-30
13:45:30 UTC (rev 19872)
@@ -70,6 +70,8 @@
import org.hibernate.util.StringHelper;
/**
+ * This implementation is never directly exposed to the user, it is always wrapped into a
{@link org.hibernate.search.impl.MutableSearchFactory}
+ *
* @author Emmanuel Bernard
*/
public class ImmutableSearchFactory implements StateSearchFactoryImplementor,
WorkerBuildContext {
@@ -191,6 +193,10 @@
return this.dirProviderData.keySet();
}
+ public void addClasses(Class<?>... classes) {
+ throw new AssertionFailure( "Cannot add classes to an " +
ImmutableSearchFactory.class.getName() );
+ }
+
public Worker getWorker() {
return worker;
}
Modified:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/impl/MutableSearchFactory.java
===================================================================
---
search/trunk/hibernate-search/src/main/java/org/hibernate/search/impl/MutableSearchFactory.java 2010-06-30
13:44:30 UTC (rev 19871)
+++
search/trunk/hibernate-search/src/main/java/org/hibernate/search/impl/MutableSearchFactory.java 2010-06-30
13:45:30 UTC (rev 19872)
@@ -9,7 +9,7 @@
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.Similarity;
-import org.hibernate.search.IncrementalSearchFactory;
+import org.hibernate.search.spi.SearchFactoryIntegrator;
import org.hibernate.search.backend.BackendQueueProcessorFactory;
import org.hibernate.search.backend.LuceneIndexingParameters;
import org.hibernate.search.backend.Worker;
@@ -35,8 +35,12 @@
*
* @author Emmanuel Bernard
*/
-public class MutableSearchFactory implements StateSearchFactoryImplementor,
IncrementalSearchFactory {
+public class MutableSearchFactory implements StateSearchFactoryImplementor,
SearchFactoryIntegrator {
+ //a reference to the same instance of this class is help by clients and various HSearch
services
+ //when changing the SearchFactory internals, only the underlying delegate should be
changed.
+ //the volatile ensure that the state is replicated upong underlying factory switch.
private volatile StateSearchFactoryImplementor delegate;
+
//lock to be acquired every time the underlying searchFactory is rebuilt
private final Lock mutating = new ReentrantLock( );
Added:
search/trunk/hibernate-search/src/main/java/org/hibernate/search/spi/SearchFactoryIntegrator.java
===================================================================
---
search/trunk/hibernate-search/src/main/java/org/hibernate/search/spi/SearchFactoryIntegrator.java
(rev 0)
+++
search/trunk/hibernate-search/src/main/java/org/hibernate/search/spi/SearchFactoryIntegrator.java 2010-06-30
13:45:30 UTC (rev 19872)
@@ -0,0 +1,31 @@
+package org.hibernate.search.spi;
+
+import org.hibernate.search.SearchFactory;
+import org.hibernate.search.backend.Worker;
+
+/**
+ * This contract is considered experimental.
+ *
+ * This contract gives access to lower level APIs of Hibernate Search for
+ * frameworks integrating with it. The piece of code creating the SearchFactory should
+ * use this contract. It should however pass the higherlevel {@link }SearchFactory}
contract to
+ * its clients.
+ *
+ * It also allows modification of some of the search factory internals:
+ * - today allow addition of new indexed classes.
+ *
+ * @author Emmanuel Bernard
+ */
+public interface SearchFactoryIntegrator extends SearchFactory {
+ /**
+ * Add the following classes to the SearchFactory
+ *
+ */
+ void addClasses(Class<?>... classes);
+
+ //TODO consider accepting SearchConfiguration or SearchMapping
+
+ Worker getWorker();
+
+ void close();
+}
Modified:
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/configuration/mutablefactory/MutableFactoryTest.java
===================================================================
---
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/configuration/mutablefactory/MutableFactoryTest.java 2010-06-30
13:44:30 UTC (rev 19871)
+++
search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/configuration/mutablefactory/MutableFactoryTest.java 2010-06-30
13:45:30 UTC (rev 19872)
@@ -6,7 +6,7 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
-import org.hibernate.search.IncrementalSearchFactory;
+import org.hibernate.search.spi.SearchFactoryIntegrator;
import org.hibernate.search.backend.Work;
import org.hibernate.search.backend.WorkType;
import org.hibernate.search.engine.SearchFactoryImplementor;
@@ -83,14 +83,14 @@
public void testAddingClassSimpleAPI() throws Exception {
ManualConfiguration configuration = new ManualConfiguration()
.addProperty( "hibernate.search.default.directory_provider",
RAMDirectoryProvider.class.getName() );
- //FIXME downcasting of MSF. create a getDelegate() ?
- IncrementalSearchFactory sf = (IncrementalSearchFactory) new
SearchFactoryBuilder().configuration( configuration ).buildSearchFactory();
- SearchFactoryImplementor sfi = (SearchFactoryImplementor) sf;
+
+ SearchFactoryIntegrator sf = new SearchFactoryBuilder().configuration( configuration
).buildSearchFactory();
+
sf.addClasses( A.class );
ManualTransactionContext tc = new ManualTransactionContext();
- doIndexWork( new A(1, "Emmanuel"), 1, sfi, tc );
+ doIndexWork( new A(1, "Emmanuel"), 1, sf, tc );
tc.end();
@@ -98,7 +98,7 @@
Query luceneQuery = parser.parse( "Emmanuel" );
//we know there is only one DP
- DirectoryProvider provider = sfi
+ DirectoryProvider provider = sf
.getDirectoryProviders( A.class )[0];
IndexSearcher searcher = new IndexSearcher( provider.getDirectory(), true );
TopDocs hits = searcher.search( luceneQuery, 1000 );
@@ -110,32 +110,32 @@
tc = new ManualTransactionContext();
- doIndexWork( new B(1, "Noel"), 1, sfi, tc );
- doIndexWork( new C(1, "Vincent"), 1, sfi, tc );
+ doIndexWork( new B(1, "Noel"), 1, sf, tc );
+ doIndexWork( new C(1, "Vincent"), 1, sf, tc );
tc.end();
luceneQuery = parser.parse( "Noel" );
//we know there is only one DP
- provider = sfi.getDirectoryProviders( B.class )[0];
+ provider = sf.getDirectoryProviders( B.class )[0];
searcher = new IndexSearcher( provider.getDirectory(), true );
hits = searcher.search( luceneQuery, 1000 );
assertEquals( 1, hits.totalHits );
luceneQuery = parser.parse( "Vincent" );
- provider = sfi.getDirectoryProviders( C.class )[0];
+ provider = sf.getDirectoryProviders( C.class )[0];
searcher = new IndexSearcher( provider.getDirectory(), true );
hits = searcher.search( luceneQuery, 1000 );
assertEquals( 1, hits.totalHits );
searcher.close();
- sfi.close();
+ sf.close();
}
- private void doIndexWork(Object entity, Integer id, SearchFactoryImplementor sfi,
ManualTransactionContext tc) {
+ private void doIndexWork(Object entity, Integer id, SearchFactoryIntegrator sfi,
ManualTransactionContext tc) {
Work<?> work = new Work<Object>( entity, id, WorkType.INDEX );
sfi.getWorker().performWork( work, tc );
}