[hibernate-commits] Hibernate SVN: r20676 - in search/trunk/hibernate-search/src: main/java/org/hibernate/search/engine and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Sep 21 10:12:43 EDT 2010


Author: hardy.ferentschik
Date: 2010-09-21 10:12:42 -0400 (Tue, 21 Sep 2010)
New Revision: 20676

Modified:
   search/trunk/hibernate-search/src/main/java/org/hibernate/search/bridge/LuceneOptions.java
   search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/LuceneOptionsImpl.java
   search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/compression/CompressionTest.java
   search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/compression/HTMLBoldFieldBridge.java
Log:
HSEARCH-596 Updated CompressionTest

Modified: search/trunk/hibernate-search/src/main/java/org/hibernate/search/bridge/LuceneOptions.java
===================================================================
--- search/trunk/hibernate-search/src/main/java/org/hibernate/search/bridge/LuceneOptions.java	2010-09-21 12:26:03 UTC (rev 20675)
+++ search/trunk/hibernate-search/src/main/java/org/hibernate/search/bridge/LuceneOptions.java	2010-09-21 14:12:42 UTC (rev 20676)
@@ -35,8 +35,19 @@
  */
 public interface LuceneOptions {
 
-	void addFieldToDocument(String name, String indexedString, Document document);
+	/**
+	 * Add a new field with the name {@code fieldName} to the Lucene Document {@code document} using the value
+	 * {@code indexedString}.
+	 *
+	 * @param fieldName The field name
+	 * @param indexedString The value to index
+	 * @param document the document to which to add the the new field
+	 */
+	void addFieldToDocument(String fieldName, String indexedString, Document document);
 
+	/**
+	 * @return {@code true} if the field value is compressed, {@code false} otherwise.
+	 */
 	boolean isCompressed();
 
 	/**

Modified: search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/LuceneOptionsImpl.java
===================================================================
--- search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/LuceneOptionsImpl.java	2010-09-21 12:26:03 UTC (rev 20675)
+++ search/trunk/hibernate-search/src/main/java/org/hibernate/search/engine/LuceneOptionsImpl.java	2010-09-21 14:12:42 UTC (rev 20676)
@@ -59,7 +59,6 @@
 
 	public void addFieldToDocument(String name, String indexedString, Document document) {
 		//Do not add fields on empty strings, seems a sensible default in most situations
-		//TODO if Store, probably also save empty ones
 		if ( StringHelper.isNotEmpty( indexedString ) ) {
 			if ( !( indexMode.equals( Index.NO ) && storeCompressed ) ) {
 				standardFieldAdd( name, indexedString, document );

Modified: search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/compression/CompressionTest.java
===================================================================
--- search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/compression/CompressionTest.java	2010-09-21 12:26:03 UTC (rev 20675)
+++ search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/compression/CompressionTest.java	2010-09-21 14:12:42 UTC (rev 20676)
@@ -47,10 +47,16 @@
 import org.hibernate.search.store.DirectoryProvider;
 import org.hibernate.search.test.SearchTestCase;
 
+/**
+ * @author Sanne Grinovero
+ * @author Hardy Ferentschik
+ */
 public class CompressionTest extends SearchTestCase {
 
 	/**
-	 * verifies the fields are really stored in compressed format
+	 * Verifies the fields are really stored in compressed format
+	 *
+	 * @throws Exception in case the test fails
 	 */
 	public void testFieldWasCompressed() throws Exception {
 		DirectoryProvider[] directoryProviders = getSearchFactory().getDirectoryProviders( LargeDocument.class );
@@ -64,30 +70,29 @@
 			Document document = indexReader.document( doc.doc );
 			{
 				Field[] fields = document.getFields( "title" );
-				Assert.assertEquals( 1, fields.length );
-//				Assert.assertFalse( fields[0].isCompressed() );
-				Assert.assertTrue( fields[0].isIndexed() );
-				Assert.assertTrue( fields[0].isStored() );
-				Assert.assertEquals(
+				assertEquals( 1, fields.length );
+				assertTrue( fields[0].isIndexed() );
+				assertTrue( fields[0].isStored() );
+				assertFalse( isCompressed( fields[0] ) );
+				assertEquals(
 						"Hibernate in Action, third edition",
 						fields[0].stringValue()
 				);
 			}
 			{
 				Field[] fields = document.getFields( "abstract" );
-				Assert.assertEquals( 1, fields.length );
-//				Assert.assertTrue( isCompressed( fields[0] ) );
-//				Assert.assertTrue( fields[0].isCompressed() );
-				Assert.assertEquals(
+				assertEquals( 1, fields.length );
+				assertTrue( isCompressed( fields[0] ) );
+				assertEquals(
 						"<b>JPA2 with Hibernate</b>",
 						restoreValue( fields[0] )
 				);
 			}
 			{
 				Field[] fields = document.getFields( "text" );
-				Assert.assertEquals( 1, fields.length );
-				Assert.assertTrue( isCompressed( fields[0] ) );
-				Assert.assertEquals(
+				assertEquals( 1, fields.length );
+				assertTrue( isCompressed( fields[0] ) );
+				assertEquals(
 						"This is a placeholder for the new text that you should write",
 						restoreValue( fields[0] )
 				);
@@ -99,25 +104,28 @@
 	}
 
 	/**
-	 * Verifies the compressed fields are also searchable
+	 * Verifies the compressed fields are also searchable.
+	 *
+	 * @throws Exception in case the test fails
 	 */
-//	public void testCompressedFieldSearch() throws ParseException {
-//		assertFindsN( 1, "title:third" );
-//		assertFindsN( 1, "abstract:jpa2" );
-//		assertFindsN( 1, "text:write" );
-//		assertFindsN( 0, "text:jpa2" );
-//	}
+	public void testCompressedFieldSearch() throws Exception {
+		assertFindsN( 1, "title:third" );
+		assertFindsN( 1, "abstract:jpa2" );
+		assertFindsN( 1, "text:write" );
+		assertFindsN( 0, "text:jpa2" );
+	}
 
 	private void assertFindsN(int expectedToFind, String queryString) throws ParseException {
 		openSession().beginTransaction();
 		try {
 			FullTextSession fullTextSession = Search.getFullTextSession( session );
-			QueryParser qparser = new QueryParser( getTargetLuceneVersion(), "", new SimpleAnalyzer() );
-			Query query = qparser.parse( queryString );
+			QueryParser queryParser = new QueryParser( getTargetLuceneVersion(), "", new SimpleAnalyzer() );
+			Query query = queryParser.parse( queryString );
 			FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(
 					query,
 					LargeDocument.class
 			);
+			@SuppressWarnings("unchecked")
 			List<LargeDocument> list = fullTextQuery.list();
 			Assert.assertEquals( expectedToFind, list.size() );
 			if ( expectedToFind == 1 ) {
@@ -133,29 +141,27 @@
 	/**
 	 * Verify that projection is able to inflate stored data
 	 */
-//	public void testProjectionOnCompressedFields() {
-//		openSession().beginTransaction();
-//		try {
-//			FullTextSession fullTextSession = Search.getFullTextSession( session );
-//			FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(
-//					new MatchAllDocsQuery(),
-//					LargeDocument.class
-//			);
-//			List list = fullTextQuery.setProjection( "title", "abstract", "text" ).list();
-//			Assert.assertEquals( 1, list.size() );
-//			Object[] results = ( Object[] ) list.get( 0 );
-//			Assert.assertEquals( "Hibernate in Action, third edition", results[0] );
-//			Assert.assertEquals( "JPA2 with Hibernate", results[1] );
-//			Assert.assertEquals( "This is a placeholder for the new text that you should write", results[2] );
-//		}
-//		finally {
-//			session.getTransaction().commit();
-//			session.close();
-//		}
-//	}
+	public void testProjectionOnCompressedFields() {
+		openSession().beginTransaction();
+		try {
+			FullTextSession fullTextSession = Search.getFullTextSession( session );
+			FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(
+					new MatchAllDocsQuery(),
+					LargeDocument.class
+			);
+			List list = fullTextQuery.setProjection( "title", "abstract", "text" ).list();
+			Assert.assertEquals( 1, list.size() );
+			Object[] results = ( Object[] ) list.get( 0 );
+			Assert.assertEquals( "Hibernate in Action, third edition", results[0] );
+			Assert.assertEquals( "JPA2 with Hibernate", results[1] );
+			Assert.assertEquals( "This is a placeholder for the new text that you should write", results[2] );
+		}
+		finally {
+			session.getTransaction().commit();
+			session.close();
+		}
+	}
 
-	// test helpers:
-
 	private String restoreValue(Field field) throws DataFormatException {
 		if ( field.isBinary() ) {
 			Assert.assertNull( "we rely on this in the Projection implementation", field.stringValue() );
@@ -167,11 +173,20 @@
 	}
 
 	private boolean isCompressed(Field field) {
-		return field.isBinary(); // ( field.isBinary() || field.isCompressed() );
+		if ( !field.isBinary() ) {
+			return false;
+		}
+		else {
+			try {
+				CompressionTools.decompressString( field.getBinaryValue() );
+				return true;
+			}
+			catch ( DataFormatException e ) {
+				return false;
+			}
+		}
 	}
 
-	// test setup:
-
 	protected Class<?>[] getAnnotatedClasses() {
 		return new Class[] {
 				LargeDocument.class

Modified: search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/compression/HTMLBoldFieldBridge.java
===================================================================
--- search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/compression/HTMLBoldFieldBridge.java	2010-09-21 12:26:03 UTC (rev 20675)
+++ search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/compression/HTMLBoldFieldBridge.java	2010-09-21 14:12:42 UTC (rev 20676)
@@ -24,41 +24,50 @@
  */
 package org.hibernate.search.test.compression;
 
+import java.util.zip.DataFormatException;
+
+import org.apache.lucene.document.CompressionTools;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Field;
+
+import org.hibernate.search.SearchException;
 import org.hibernate.search.bridge.FieldBridge;
 import org.hibernate.search.bridge.LuceneOptions;
 import org.hibernate.search.bridge.TwoWayFieldBridge;
 
 /**
- * This FieldBridge is storing strings in the index wrapping the
- * entity value in html bold tags.
- * It's using deprecated method "getStore" to verify backwards compatibility
- * Almost useless, needed for CompressionTest
+ * This FieldBridge is storing strings in the index wrapping the entity value in html bold tags.
+ *
+ * @author Sanne Grinovero
  * @see LuceneOptions
- * @author Sanne Grinovero
  */
 public class HTMLBoldFieldBridge implements FieldBridge, TwoWayFieldBridge {
 
 	public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
 		String fieldValue = objectToString( value );
-		Field field = new Field( name, fieldValue, luceneOptions.getStore(),
-				luceneOptions.getIndex(), luceneOptions.getTermVector() );
-		field.setBoost( luceneOptions.getBoost() );
-		document.add( field );
+		luceneOptions.addFieldToDocument( name, fieldValue, document );
 	}
 
 	public Object get(String name, Document document) {
 		Field field = document.getField( name );
-		String stringValue = field.stringValue();
-		return stringValue.substring( 3, stringValue.length()-4 );
+			String stringValue;
+			if ( field.isBinary() ) {
+				try {
+					stringValue = CompressionTools.decompressString( field.getBinaryValue() );
+				}
+				catch ( DataFormatException e) {
+					throw new SearchException( "Field " + name + " looks like binary but couldn't be decompressed" );
+				}
+			}
+			else {
+				stringValue = field.stringValue();
+			}
+			return stringValue.substring( 3, stringValue.length() - 4 );
 	}
 
 	public String objectToString(Object value) {
 		String originalValue = value.toString();
-		String fieldValue = "<b>" + originalValue + "</b>";
-		return fieldValue;
+		return  "<b>" + originalValue + "</b>";
 	}
-
 }
 



More information about the hibernate-commits mailing list