Author: epbernard
Date: 2009-06-12 21:48:33 -0400 (Fri, 12 Jun 2009)
New Revision: 16780
Added:
search/trunk/src/test/java/org/hibernate/search/test/shards/DirectoryProviderForQueryTest.java
search/trunk/src/test/java/org/hibernate/search/test/shards/Email.java
search/trunk/src/test/java/org/hibernate/search/test/shards/SpecificShardingStrategy.java
Modified:
search/trunk/src/main/java/org/hibernate/search/impl/SearchFactoryImpl.java
Log:
HSEARCH-251 add integration test
Modified: search/trunk/src/main/java/org/hibernate/search/impl/SearchFactoryImpl.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/impl/SearchFactoryImpl.java 2009-06-13
01:47:28 UTC (rev 16779)
+++ search/trunk/src/main/java/org/hibernate/search/impl/SearchFactoryImpl.java 2009-06-13
01:48:33 UTC (rev 16780)
@@ -53,6 +53,7 @@
import org.hibernate.search.filter.CachingWrapperFilter;
import org.hibernate.search.filter.FilterCachingStrategy;
import org.hibernate.search.filter.MRUFilterCachingStrategy;
+import org.hibernate.search.filter.ShardSensitiveOnlyFilter;
import org.hibernate.search.reader.ReaderProvider;
import org.hibernate.search.reader.ReaderProviderFactory;
import org.hibernate.search.store.DirectoryProvider;
@@ -240,6 +241,11 @@
}
FilterDef filterDef = new FilterDef( defAnn );
+ if ( filterDef.getImpl().equals( ShardSensitiveOnlyFilter.class ) ) {
+ //this is a placeholder don't process regularly
+ filterDefinitions.put( defAnn.name(), filterDef );
+ return;
+ }
try {
filterDef.getImpl().newInstance();
}
Added:
search/trunk/src/test/java/org/hibernate/search/test/shards/DirectoryProviderForQueryTest.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/shards/DirectoryProviderForQueryTest.java
(rev 0)
+++
search/trunk/src/test/java/org/hibernate/search/test/shards/DirectoryProviderForQueryTest.java 2009-06-13
01:48:33 UTC (rev 16780)
@@ -0,0 +1,107 @@
+// $Id: ShardsTest.java 16210 2009-03-25 10:07:47Z hardy.ferentschik $
+package org.hibernate.search.test.shards;
+
+import java.io.File;
+import java.util.List;
+
+import org.hibernate.search.test.SearchTestCase;
+import org.hibernate.search.util.FileHelper;
+import org.hibernate.search.store.RAMDirectoryProvider;
+import org.hibernate.search.store.FSDirectoryProvider;
+import org.hibernate.search.store.IdHashShardingStrategy;
+import org.hibernate.search.store.DirectoryProvider;
+import org.hibernate.search.Environment;
+import org.hibernate.search.FullTextQuery;
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.Search;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.apache.lucene.analysis.StopAnalyzer;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.TermDocs;
+import org.apache.lucene.index.Term;
+
+/**
+ * @author Chase Seibert
+ */
+public class DirectoryProviderForQueryTest extends SearchTestCase {
+
+
+ protected void configure(Configuration cfg) {
+ super.configure( cfg );
+ cfg.setProperty( "hibernate.search.default.directory_provider",
FSDirectoryProvider.class.getName() );
+ File sub = getBaseIndexDir();
+ cfg.setProperty( "hibernate.search.default.indexBase", sub.getAbsolutePath()
);
+ cfg.setProperty( Environment.ANALYZER_CLASS, StopAnalyzer.class.getName() );
+ // this strategy allows the caller to use a pre-search filter to define which index to
hit
+ cfg.setProperty( "hibernate.search.Email.sharding_strategy",
SpecificShardingStrategy.class.getCanonicalName() );
+ cfg.setProperty( "hibernate.search.Email.sharding_strategy.nbr_of_shards",
"2" );
+ }
+
+ /**
+ * Test that you can filter by shard
+ */
+ public void testDirectoryProviderForQuery() throws Exception {
+
+ Session s = openSession( );
+ Transaction tx = s.beginTransaction();
+
+ Email a = new Email();
+ a.setId( 1 );
+ a.setBody( "corporate message" );
+ s.persist( a );
+
+ a = new Email();
+ a.setId( 2 );
+ a.setBody( "spam message" );
+ s.persist( a );
+
+ tx.commit();
+
+ s.clear();
+
+ tx = s.beginTransaction();
+ FullTextSession fts = Search.getFullTextSession( s );
+ QueryParser parser = new QueryParser("id", new StopAnalyzer() );
+
+ FullTextQuery fullTextQuery = fts.createFullTextQuery( parser.parse(
"body:message" ) );
+ List results = fullTextQuery.list();
+ assertEquals( "Query with no filter should bring back results from both
shards.", 2, results.size() );
+
+ // index is not a field on the entity; the only way to filter on this is by shard
+ fullTextQuery.enableFullTextFilter("shard").setParameter("index",
0);
+ assertEquals( "Query with filter should bring back results from only one
shard.", 1, fullTextQuery.list().size() );
+
+ for (Object o : results) s.delete( o );
+ tx.commit();
+ s.close();
+ }
+
+ protected void setUp() throws Exception {
+ File sub = getBaseIndexDir();
+ sub.mkdir();
+ File[] files = sub.listFiles();
+ for (File file : files) {
+ if ( file.isDirectory() ) {
+ FileHelper.delete( file );
+ }
+ }
+ super.setUp(); //we need a fresh session factory each time for index set up
+ buildSessionFactory( getMappings(), getAnnotatedPackages(), getXmlFiles() );
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ File sub = getBaseIndexDir();
+ FileHelper.delete( sub );
+ }
+
+ @SuppressWarnings("unchecked")
+ protected Class[] getMappings() {
+ return new Class[] {
+ Email.class
+ };
+ }
+}
Added: search/trunk/src/test/java/org/hibernate/search/test/shards/Email.java
===================================================================
--- search/trunk/src/test/java/org/hibernate/search/test/shards/Email.java
(rev 0)
+++ search/trunk/src/test/java/org/hibernate/search/test/shards/Email.java 2009-06-13
01:48:33 UTC (rev 16780)
@@ -0,0 +1,43 @@
+package org.hibernate.search.test.shards;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.FullTextFilterDef;
+import org.hibernate.search.annotations.FullTextFilterDefs;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.filter.ShardSensitiveOnlyFilter;
+
+@Entity
+@Indexed(index="Email")
+@FullTextFilterDefs( {
+ @FullTextFilterDef(name="shard", impl= ShardSensitiveOnlyFilter.class)
+})
+public class Email {
+
+ @Id
+ @DocumentId
+ private Integer id;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ @Field
+ private String body;
+
+ public String getBody() {
+ return body;
+ }
+
+ public void setBody(String body) {
+ this.body = body;
+ }
+
+}
Added:
search/trunk/src/test/java/org/hibernate/search/test/shards/SpecificShardingStrategy.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/shards/SpecificShardingStrategy.java
(rev 0)
+++
search/trunk/src/test/java/org/hibernate/search/test/shards/SpecificShardingStrategy.java 2009-06-13
01:48:33 UTC (rev 16780)
@@ -0,0 +1,33 @@
+package org.hibernate.search.test.shards;
+
+import java.io.Serializable;
+import java.util.Properties;
+
+import org.apache.lucene.document.Document;
+import org.hibernate.search.FullTextFilter;
+import org.hibernate.search.filter.FullTextFilterImplementor;
+import org.hibernate.search.store.DirectoryProvider;
+import org.hibernate.search.store.IdHashShardingStrategy;
+
+public class SpecificShardingStrategy extends IdHashShardingStrategy {
+
+ @Override
+ public DirectoryProvider<?>[]
getDirectoryProvidersForQuery(FullTextFilterImplementor[] filters) {
+
+ FullTextFilter filter = getFilter(filters, "shard");
+ if (filter == null) {
+ return getDirectoryProvidersForAllShards();
+ }
+ else {
+ return new DirectoryProvider[] {
getDirectoryProvidersForAllShards()[Integer.parseInt(filter.getParameter("index").toString())]
};
+ }
+ }
+
+ private FullTextFilter getFilter(FullTextFilterImplementor[] filters, String name) {
+ for (FullTextFilterImplementor filter: filters) {
+ if (filter.getName().equals(name)) return filter;
+ }
+ return null;
+ }
+
+}
Show replies by date