Example usage for org.apache.lucene.util FixedBitSet getAndSet

List of usage examples for org.apache.lucene.util FixedBitSet getAndSet

Introduction

In this page you can find the example usage for org.apache.lucene.util FixedBitSet getAndSet.

Prototype

public boolean getAndSet(int index) 

Source Link

Usage

From source file:de.unihildesheim.iw.lucene.document.FeedbackQuery.java

License:Open Source License

/**
 * Tries to get the minimum number of document without {@link
 * RelaxableQuery#relax() relaxing} the query. If the minimum number of
 * documents is not reached without relaxing at most the maximum number of
 * documents is returned while relaxing the query.
 *
 * @param searcher Searcher to issue queries
 * @param query Relaxable query to get matching documents
 * @param minDocs Minimum number of documents to get. Must be greater than
 * zero./*from   w w  w. j a v  a  2s. c om*/
 * @param maxDocCount Maximum number of documents to get. {@code -1} for
 * unlimited or greater than zero.
 * @return List of documents matching the (relaxed) query. Ranking order is
 * not preserved!
 * @throws IOException Thrown on low-level I/O errors
 */
public static DocIdSet getMinMax(@NotNull final IndexSearcher searcher, @NotNull final RelaxableQuery query,
        final int minDocs, final int maxDocCount) throws IOException {
    final int maxDocs;

    if (maxDocCount == -1) {
        maxDocs = Integer.MAX_VALUE;
    } else if (maxDocCount < 0) {
        throw new IllegalArgumentException(
                "Maximum number of documents must " + "be -1 (unlimited) or greater than zero.");
    } else if (maxDocCount < minDocs) {
        throw new IllegalArgumentException(
                "Maximum number of documents must " + "be greater than minimum value.");
    } else {
        maxDocs = maxDocCount;
    }
    if (minDocs <= 0) {
        throw new IllegalArgumentException("Minimum number of documents must be" + " greater than zero.");
    }

    final int maxRetDocs = getMaxDocs(searcher.getIndexReader(), maxDocs);
    final FixedBitSet bits = new FixedBitSet(searcher.getIndexReader().maxDoc());
    bits.or(BitsUtils.arrayToBits(getDocs(searcher, query.getQueryObj(), maxRetDocs)));

    // build a log-info string
    final String logInfo = "Got {} matching feedback documents. " + "Relaxing query to "
            + (maxDocCount > 0 ? "get additional" : "reach the minimum of") + " {} feedback documents...";

    int docsToGet;
    int bitsCount;
    while ((bitsCount = bits.cardinality()) < minDocs && query.relax()) {
        docsToGet = maxRetDocs - bitsCount;
        LOG.info(logInfo, bitsCount, docsToGet);

        final int[] docs = getDocs(searcher, query.getQueryObj(), maxRetDocs);
        int maxAdd = maxDocs - bitsCount;

        for (int i = docs.length - 1; i >= 0 && maxAdd > 0; i--) {
            if (!bits.getAndSet(docs[i])) {
                maxAdd--;
            }
        }
    }

    LOG.info("Returning {} documents.", bits.cardinality());
    return new BitDocIdSet(bits);
}

From source file:org.apache.solr.legacy.TestLegacyNumericUtils.java

License:Apache License

/** Note: The neededBounds Iterable must be unsigned (easier understanding what's happening) */
private void assertIntRangeSplit(final int lower, final int upper, int precisionStep, final boolean useBitSet,
        final Iterable<Integer> expectedBounds, final Iterable<Integer> expectedShifts) {
    final FixedBitSet bits = useBitSet ? new FixedBitSet(upper - lower + 1) : null;
    final Iterator<Integer> neededBounds = (expectedBounds == null) ? null : expectedBounds.iterator();
    final Iterator<Integer> neededShifts = (expectedShifts == null) ? null : expectedShifts.iterator();

    LegacyNumericUtils.splitIntRange(new LegacyNumericUtils.IntRangeBuilder() {
        @Override//  w ww  . j  a v  a 2s  . c o  m
        public void addRange(int min, int max, int shift) {
            assertTrue("min, max should be inside bounds",
                    min >= lower && min <= upper && max >= lower && max <= upper);
            if (useBitSet)
                for (int i = min; i <= max; i++) {
                    assertFalse("ranges should not overlap", bits.getAndSet(i - lower));
                    // extra exit condition to prevent overflow on MAX_VALUE
                    if (i == max)
                        break;
                }
            if (neededBounds == null)
                return;
            // make unsigned ints for easier display and understanding
            min ^= 0x80000000;
            max ^= 0x80000000;
            //System.out.println("0x"+Integer.toHexString(min>>>shift)+",0x"+Integer.toHexString(max>>>shift)+")/*shift="+shift+"*/,");
            assertEquals("shift", neededShifts.next().intValue(), shift);
            assertEquals("inner min bound", neededBounds.next().intValue(), min >>> shift);
            assertEquals("inner max bound", neededBounds.next().intValue(), max >>> shift);
        }
    }, precisionStep, lower, upper);

    if (useBitSet) {
        // after flipping all bits in the range, the cardinality should be zero
        bits.flip(0, upper - lower + 1);
        assertEquals("The sub-range concenated should match the whole range", 0, bits.cardinality());
    }
}