Rollback the tmp storage of BytesRefHash to -1 after sort (#13014)

This commit is contained in:
gf2121 2024-01-25 10:48:11 +08:00 committed by GitHub
parent 3a205feb27
commit d0098f8489
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 7 deletions

View File

@ -214,6 +214,7 @@ public final class BytesRefHash implements Accountable {
pool.fillBytesRef(result, bytesStart[compact[i]]); pool.fillBytesRef(result, bytesStart[compact[i]]);
} }
}.sort(0, count); }.sort(0, count);
Arrays.fill(compact, tmpOffset, compact.length, -1);
return compact; return compact;
} }

View File

@ -176,13 +176,17 @@ public class TestBytesRefHash extends LuceneTestCase {
hash.add(ref.get()); hash.add(ref.get());
strings.add(str); strings.add(str);
} }
int[] sort = hash.sort(); for (int iter = 0; iter < 3; iter++) {
assertTrue(strings.size() < sort.length); // Test duplicate sort on a BytesRefHash instance work well. This makes no sense but some
int i = 0; // users need that.
BytesRef scratch = new BytesRef(); int[] sort = hash.sort();
for (String string : strings) { assertTrue(strings.size() < sort.length);
ref.copyChars(string); int i = 0;
assertEquals(ref.get(), hash.get(sort[i++], scratch)); BytesRef scratch = new BytesRef();
for (String string : strings) {
ref.copyChars(string);
assertEquals(ref.get(), hash.get(sort[i++], scratch));
}
} }
hash.clear(); hash.clear();
assertEquals(0, hash.size()); assertEquals(0, hash.size());