LUCENE-3191: Fix SlowCollatedStringComparator and add tests

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1140713 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2011-06-28 16:30:55 +00:00
parent 8e94c0c216
commit 71d4dc370b
2 changed files with 25 additions and 12 deletions

View File

@ -33,7 +33,7 @@ import org.apache.lucene.util.BytesRef;
* This class will be removed in Lucene 5.0 * This class will be removed in Lucene 5.0
*/ */
@Deprecated @Deprecated
public final class SlowCollatedStringComparator extends FieldComparator<BytesRef> { public final class SlowCollatedStringComparator extends FieldComparator<String> {
private final String[] values; private final String[] values;
private DocTerms currentDocTerms; private DocTerms currentDocTerms;
@ -99,13 +99,12 @@ public final class SlowCollatedStringComparator extends FieldComparator<BytesRef
} }
@Override @Override
public BytesRef value(int slot) { public String value(int slot) {
final String s = values[slot]; return values[slot];
return s == null ? null : new BytesRef(values[slot]);
} }
@Override @Override
public int compareValues(BytesRef first, BytesRef second) { public int compareValues(String first, String second) {
if (first == null) { if (first == null) {
if (second == null) { if (second == null) {
return 0; return 0;

View File

@ -41,6 +41,7 @@ public class TestSlowCollationMethods extends LuceneTestCase {
private static IndexReader reader; private static IndexReader reader;
private static Directory dir; private static Directory dir;
private static int numDocs; private static int numDocs;
private static String splitDoc;
@BeforeClass @BeforeClass
public static void beforeClass() throws Exception { public static void beforeClass() throws Exception {
@ -59,6 +60,7 @@ public class TestSlowCollationMethods extends LuceneTestCase {
doc.add(field); doc.add(field);
iw.addDocument(doc); iw.addDocument(doc);
} }
splitDoc = _TestUtil.randomUnicodeString(random);
reader = iw.getReader(); reader = iw.getReader();
iw.close(); iw.close();
@ -76,6 +78,15 @@ public class TestSlowCollationMethods extends LuceneTestCase {
dir = null; dir = null;
} }
private void doCheckSorting(TopDocs docs) throws Exception {
String prev = "";
for (ScoreDoc doc : docs.scoreDocs) {
String value = reader.document(doc.doc).get("field");
assertTrue(collator.compare(value, prev) >= 0);
prev = value;
}
}
public void testSort() throws Exception { public void testSort() throws Exception {
SortField sf = new SortField("field", new FieldComparatorSource() { SortField sf = new SortField("field", new FieldComparatorSource() {
@Override @Override
@ -83,13 +94,16 @@ public class TestSlowCollationMethods extends LuceneTestCase {
return new SlowCollatedStringComparator(numHits, fieldname, collator); return new SlowCollatedStringComparator(numHits, fieldname, collator);
} }
}); });
TopFieldDocs docs = searcher.search(new MatchAllDocsQuery(), null, numDocs, new Sort(sf)); final Sort sort = new Sort(sf);
String prev = "";
for (ScoreDoc doc : docs.scoreDocs) { final TopDocs docs1 = searcher.search(TermRangeQuery.newStringRange("field", null, splitDoc, true, true), null, numDocs/(1+random.nextInt(4)), sort);
String value = reader.document(doc.doc).get("field"); doCheckSorting(docs1);
assertTrue(collator.compare(value, prev) >= 0);
prev = value; final TopDocs docs2 = searcher.search(TermRangeQuery.newStringRange("field", splitDoc, null, true, true), null, numDocs/(1+random.nextInt(4)), sort);
} doCheckSorting(docs2);
final TopDocs docs = TopDocs.merge(sort, numDocs/(1+random.nextInt(4)), new TopDocs[]{docs1, docs2});
doCheckSorting(docs);
} }
private void doTestRanges(String startPoint, String endPoint, Query query) throws Exception { private void doTestRanges(String startPoint, String endPoint, Query query) throws Exception {