[hibernate-commits] Hibernate SVN: r15195 - in search/trunk/src: test/org/hibernate/search/test/shards and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Sun Sep 14 05:00:06 EDT 2008


Author: sannegrinovero
Date: 2008-09-14 05:00:05 -0400 (Sun, 14 Sep 2008)
New Revision: 15195

Added:
   search/trunk/src/test/org/hibernate/search/test/shards/IdShardingStrategyTest.java
Modified:
   search/trunk/src/java/org/hibernate/search/store/IdHashShardingStrategy.java
Log:
HSEARCH-237 IdHashShardingStrategy fails on IDs having negative hashcode

Modified: search/trunk/src/java/org/hibernate/search/store/IdHashShardingStrategy.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/store/IdHashShardingStrategy.java	2008-09-12 07:14:40 UTC (rev 15194)
+++ search/trunk/src/java/org/hibernate/search/store/IdHashShardingStrategy.java	2008-09-14 09:00:05 UTC (rev 15195)
@@ -12,6 +12,7 @@
  * @author Emmanuel Bernard
  */
 public class IdHashShardingStrategy implements IndexShardingStrategy {
+	
 	private DirectoryProvider<?>[] providers;
 	public void initialize(Properties properties, DirectoryProvider<?>[] providers) {
 		this.providers = providers;
@@ -27,17 +28,17 @@
 
 	public DirectoryProvider<?>[] getDirectoryProvidersForDeletion(Class<?> entity, Serializable id, String idInString) {
 		if ( idInString == null ) return providers;
-		return new DirectoryProvider[] { providers[ hashKey(idInString) ] };
+		return new DirectoryProvider[] { providers[hashKey( idInString )] };
 	}
 
 	private int hashKey(String key) {
-		//reproduce the hashCode implementaiton of String as documented in the javadoc
+		// reproduce the hashCode implementation of String as documented in the javadoc
 		// to be safe cross Java version (in case it changes some day)
 		int hash = 0;
 		int length = key.length();
-		for (int index = 0 ; index < length ; index++) {
-			hash = 31*hash + key.charAt( index );
+		for ( int index = 0; index < length; index++ ) {
+			hash = 31 * hash + key.charAt( index );
 		}
-		return hash % providers.length;
+		return Math.abs( hash % providers.length );
 	}
 }

Added: search/trunk/src/test/org/hibernate/search/test/shards/IdShardingStrategyTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/shards/IdShardingStrategyTest.java	                        (rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/shards/IdShardingStrategyTest.java	2008-09-14 09:00:05 UTC (rev 15195)
@@ -0,0 +1,40 @@
+// $Id$
+package org.hibernate.search.test.shards;
+
+import org.hibernate.search.store.DirectoryProvider;
+import org.hibernate.search.store.IdHashShardingStrategy;
+import org.hibernate.search.store.RAMDirectoryProvider;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Sanne Grinovero
+ */
+public class IdShardingStrategyTest extends TestCase {
+
+	private IdHashShardingStrategy shardStrategy;
+
+	protected void setUp() throws Exception {
+		shardStrategy = new IdHashShardingStrategy();
+		shardStrategy.initialize( null, new DirectoryProvider[] {
+				new RAMDirectoryProvider(), new RAMDirectoryProvider() } );
+	}
+
+	public void testHashOverflow() {
+		String key = String.valueOf( Integer.MAX_VALUE - 1 );
+		// any key will do as long as it's hash is negative
+		assertTrue( key.hashCode() < 0 );
+		assertAcceptableId( key );
+	}
+
+	private void assertAcceptableId(String id) {
+		try {
+			shardStrategy.getDirectoryProviderForAddition( null, id, id, null );
+			shardStrategy.getDirectoryProvidersForDeletion( null, id, id );
+		}
+		catch ( Exception e ) {
+			fail( "Couldn't get directory for id " + id );
+		}
+	}
+
+}
\ No newline at end of file


Property changes on: search/trunk/src/test/org/hibernate/search/test/shards/IdShardingStrategyTest.java
___________________________________________________________________
Name: svn:keywords
   + Id




More information about the hibernate-commits mailing list