You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by si...@apache.org on 2010/10/14 12:11:47 UTC

svn commit: r1022453 - in /lucene/dev/trunk/lucene/src: java/org/apache/lucene/util/BytesRefHash.java test/org/apache/lucene/util/TestBytesRefHash.java

Author: simonw
Date: Thu Oct 14 10:11:46 2010
New Revision: 1022453

URL: http://svn.apache.org/viewvc?rev=1022453&view=rev
Log:
LUCENE-2707: BytesRefHash#get() should expect a BytesRef instances for consistency

Modified:
    lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/BytesRefHash.java
    lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/TestBytesRefHash.java

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/BytesRefHash.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/BytesRefHash.java?rev=1022453&r1=1022452&r2=1022453&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/BytesRefHash.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/BytesRefHash.java Thu Oct 14 10:11:46 2010
@@ -89,21 +89,21 @@ public final class BytesRefHash {
   }
 
   /**
-   * Returns the {@link BytesRef} value for the given ord.
+   * Populates and returns a {@link BytesRef} with the bytes for the given ord.
    * <p>
    * Note: the given ord must be a positive integer less that the current size (
    * {@link #size()})
    * </p>
+   *
+   * @param ord the ord
+   * @param ref the {@link BytesRef} to populate
    * 
-   * @param ord
-   *          the ord
-   * 
-   * @return a BytesRef instance for the given ord
+   * @return the given BytesRef instance populated with the bytes for the given ord
    */
-  public BytesRef get(int ord) {
+  public BytesRef get(int ord, BytesRef ref) {
     assert bytesStart != null : "bytesStart is null - not initialized";
     assert ord < bytesStart.length: "ord exceeeds byteStart len: " + bytesStart.length;
-    return pool.setBytesRef(scratch1, bytesStart[ord]);
+    return pool.setBytesRef(ref, bytesStart[ord]);
   }
 
   /**

Modified: lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/TestBytesRefHash.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/TestBytesRefHash.java?rev=1022453&r1=1022452&r2=1022453&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/TestBytesRefHash.java (original)
+++ lucene/dev/trunk/lucene/src/test/org/apache/lucene/util/TestBytesRefHash.java Thu Oct 14 10:11:46 2010
@@ -95,6 +95,7 @@ public class TestBytesRefHash extends Lu
   @Test
   public void testGet() {
     BytesRef ref = new BytesRef();
+    BytesRef scratch = new BytesRef();
     for (int j = 0; j < 2 * RANDOM_MULTIPLIER; j++) {
       Map<String, Integer> strings = new HashMap<String, Integer>();
       for (int i = 0; i < 797; i++) {
@@ -116,7 +117,7 @@ public class TestBytesRefHash extends Lu
       }
       for (Entry<String, Integer> entry : strings.entrySet()) {
         ref.copy(entry.getKey());
-        assertEquals(ref, hash.get(entry.getValue().intValue()));
+        assertEquals(ref, hash.get(entry.getValue().intValue(), scratch));
       }
       hash.clear();
       assertEquals(0, hash.size());
@@ -176,9 +177,10 @@ public class TestBytesRefHash extends Lu
       int[] sort = hash.sort(BytesRef.getUTF8SortedAsUTF16Comparator());
       assertTrue(strings.size() < sort.length);
       int i = 0;
+      BytesRef scratch = new BytesRef();
       for (String string : strings) {
         ref.copy(string);
-        assertEquals(ref, hash.get(sort[i++]));
+        assertEquals(ref, hash.get(sort[i++], scratch));
       }
       hash.clear();
       assertEquals(0, hash.size());
@@ -195,6 +197,7 @@ public class TestBytesRefHash extends Lu
   @Test
   public void testAdd() {
     BytesRef ref = new BytesRef();
+    BytesRef scratch = new BytesRef();
     for (int j = 0; j < 2 * RANDOM_MULTIPLIER; j++) {
       Set<String> strings = new HashSet<String>();
       for (int i = 0; i < 797; i++) {
@@ -213,7 +216,7 @@ public class TestBytesRefHash extends Lu
         } else {
           assertFalse(strings.add(str));
           assertTrue((-key)-1 < count);
-          assertEquals(str, hash.get((-key)-1).utf8ToString());
+          assertEquals(str, hash.get((-key)-1, scratch).utf8ToString());
           assertEquals(count, hash.size());
         }
       }
@@ -253,6 +256,7 @@ public class TestBytesRefHash extends Lu
   @Test
   public void testAddByPoolOffset() {
     BytesRef ref = new BytesRef();
+    BytesRef scratch = new BytesRef();
     BytesRefHash offsetHash = newHash(pool);
     for (int j = 0; j < 2 * RANDOM_MULTIPLIER; j++) {
       Set<String> strings = new HashSet<String>();
@@ -275,11 +279,11 @@ public class TestBytesRefHash extends Lu
         } else {
           assertFalse(strings.add(str));
           assertTrue((-key)-1 < count);
-          assertEquals(str, hash.get((-key)-1).utf8ToString());
+          assertEquals(str, hash.get((-key)-1, scratch).utf8ToString());
           assertEquals(count, hash.size());
           int offsetKey = offsetHash.addByPoolOffset(hash.byteStart((-key)-1));
           assertTrue((-offsetKey)-1 < count);
-          assertEquals(str, hash.get((-offsetKey)-1).utf8ToString());
+          assertEquals(str, hash.get((-offsetKey)-1, scratch).utf8ToString());
           assertEquals(count, hash.size());
         }
       }
@@ -288,7 +292,7 @@ public class TestBytesRefHash extends Lu
       for (String string : strings) {
         ref.copy(string);
         int key = hash.add(ref);
-        BytesRef bytesRef = offsetHash.get((-key)-1);
+        BytesRef bytesRef = offsetHash.get((-key)-1, scratch);
         assertEquals(ref, bytesRef);
       }
 
@@ -303,11 +307,12 @@ public class TestBytesRefHash extends Lu
   
   private void assertAllIn(Set<String> strings, BytesRefHash hash) {
     BytesRef ref = new BytesRef();
+    BytesRef scratch = new BytesRef();
     int count = hash.size();
     for (String string : strings) {
       ref.copy(string);
       int key  =  hash.add(ref); // add again to check duplicates
-      assertEquals(string, hash.get((-key)-1).utf8ToString());
+      assertEquals(string, hash.get((-key)-1, scratch).utf8ToString());
       assertEquals(count, hash.size());
       assertTrue("key: " + key + " count: " + count + " string: " + string,
           key < count);