Search in sources :

Example 6 with SeekStatus

use of org.apache.lucene.index.TermsEnum.SeekStatus in project lucene-solr by apache.

the class LuceneTestCase method assertTermsSeekingEquals.

private void assertTermsSeekingEquals(String info, Terms leftTerms, Terms rightTerms) throws IOException {
    // just an upper bound
    int numTests = atLeast(20);
    Random random = random();
    TermsEnum leftEnum = null;
    // collect this number of terms from the left side
    HashSet<BytesRef> tests = new HashSet<>();
    int numPasses = 0;
    while (numPasses < 10 && tests.size() < numTests) {
        leftEnum = leftTerms.iterator();
        BytesRef term = null;
        while ((term = leftEnum.next()) != null) {
            int code = random.nextInt(10);
            if (code == 0) {
                // the term
                tests.add(BytesRef.deepCopyOf(term));
            } else if (code == 1) {
                // truncated subsequence of term
                term = BytesRef.deepCopyOf(term);
                if (term.length > 0) {
                    // truncate it
                    term.length = random.nextInt(term.length);
                }
            } else if (code == 2) {
                // term, but ensure a non-zero offset
                byte[] newbytes = new byte[term.length + 5];
                System.arraycopy(term.bytes, term.offset, newbytes, 5, term.length);
                tests.add(new BytesRef(newbytes, 5, term.length));
            } else if (code == 3) {
                switch(random().nextInt(3)) {
                    case 0:
                        // before the first term
                        tests.add(new BytesRef());
                        break;
                    case 1:
                        // past the last term
                        tests.add(new BytesRef(new byte[] { (byte) 0xFF, (byte) 0xFF }));
                        break;
                    case 2:
                        // random term
                        tests.add(new BytesRef(TestUtil.randomSimpleString(random())));
                        break;
                    default:
                        throw new AssertionError();
                }
            }
        }
        numPasses++;
    }
    TermsEnum rightEnum = rightTerms.iterator();
    ArrayList<BytesRef> shuffledTests = new ArrayList<>(tests);
    Collections.shuffle(shuffledTests, random);
    for (BytesRef b : shuffledTests) {
        if (rarely()) {
            // make new enums
            leftEnum = leftTerms.iterator();
            rightEnum = rightTerms.iterator();
        }
        final boolean seekExact = random().nextBoolean();
        if (seekExact) {
            assertEquals(info, leftEnum.seekExact(b), rightEnum.seekExact(b));
        } else {
            SeekStatus leftStatus = leftEnum.seekCeil(b);
            SeekStatus rightStatus = rightEnum.seekCeil(b);
            assertEquals(info, leftStatus, rightStatus);
            if (leftStatus != SeekStatus.END) {
                assertEquals(info, leftEnum.term(), rightEnum.term());
                assertTermStatsEquals(info, leftEnum, rightEnum);
            }
        }
    }
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) SeekStatus(org.apache.lucene.index.TermsEnum.SeekStatus) HashSet(java.util.HashSet)

Aggregations

SeekStatus (org.apache.lucene.index.TermsEnum.SeekStatus)6 BytesRef (org.apache.lucene.util.BytesRef)5 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Random (java.util.Random)2 IOException (java.io.IOException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)1 FieldsConsumer (org.apache.lucene.codecs.FieldsConsumer)1 FieldsProducer (org.apache.lucene.codecs.FieldsProducer)1 FilterCodec (org.apache.lucene.codecs.FilterCodec)1 PostingsFormat (org.apache.lucene.codecs.PostingsFormat)1 Document (org.apache.lucene.document.Document)1 TermsEnum (org.apache.lucene.index.TermsEnum)1 Directory (org.apache.lucene.store.Directory)1 LineFileDocs (org.apache.lucene.util.LineFileDocs)1