Search in sources :

Example 96 with FixedBitSet

use of org.apache.lucene.util.FixedBitSet in project lucene-solr by apache.

the class TestLegacyNumericUtils method assertIntRangeSplit.

/** 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
        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());
    }
}
Also used : LegacyNumericUtils(org.apache.solr.legacy.LegacyNumericUtils) FixedBitSet(org.apache.lucene.util.FixedBitSet)

Example 97 with FixedBitSet

use of org.apache.lucene.util.FixedBitSet in project lucene-solr by apache.

the class TestSort method randSet.

public DocIdSet randSet(int sz) {
    FixedBitSet obs = new FixedBitSet(sz);
    int n = r.nextInt(sz);
    for (int i = 0; i < n; i++) {
        obs.set(r.nextInt(sz));
    }
    return new BitDocIdSet(obs);
}
Also used : BitDocIdSet(org.apache.lucene.util.BitDocIdSet) FixedBitSet(org.apache.lucene.util.FixedBitSet)

Example 98 with FixedBitSet

use of org.apache.lucene.util.FixedBitSet in project lucene-solr by apache.

the class DocSetPerf method generate.

static void generate(int maxSize, int bitsToSet) {
    bs = new FixedBitSet(maxSize);
    ids = new int[bitsToSet];
    int count = 0;
    if (maxSize > 0) {
        for (int i = 0; i < bitsToSet; i++) {
            int id = rand.nextInt(maxSize);
            if (!bs.get(id)) {
                bs.set(id);
                ids[count++] = id;
            }
        }
    }
    bds = new BitDocSet(bs, bitsToSet);
    hds = new HashDocSet(ids, 0, count);
}
Also used : FixedBitSet(org.apache.lucene.util.FixedBitSet)

Example 99 with FixedBitSet

use of org.apache.lucene.util.FixedBitSet in project lucene-solr by apache.

the class TestFiltering method makeRandomQuery.

String makeRandomQuery(Model model, boolean mainQuery, boolean facetQuery) {
    boolean cache = random().nextBoolean();
    int cost = cache ? 0 : random().nextBoolean() ? random().nextInt(200) : 0;
    boolean positive = random().nextBoolean();
    // can't exclude a facet query from faceting
    boolean exclude = facetQuery ? false : random().nextBoolean();
    FixedBitSet[] sets = facetQuery ? new FixedBitSet[] { model.facetQuery } : (exclude ? new FixedBitSet[] { model.answer, model.facetQuery } : new FixedBitSet[] { model.answer, model.multiSelect, model.facetQuery });
    if (random().nextInt(100) < 60) {
        // frange
        int l = 0;
        int u = 0;
        if (positive) {
            // positive frange, make it big by taking the max of 4 tries
            int n = -1;
            for (int i = 0; i < 4; i++) {
                int ll = random().nextInt(model.indexSize);
                int uu = ll + ((ll == model.indexSize - 1) ? 0 : random().nextInt(model.indexSize - l));
                if (uu - ll + 1 > n) {
                    n = uu - ll + 1;
                    u = uu;
                    l = ll;
                }
            }
            for (FixedBitSet set : sets) {
                set.clear(0, l);
                if (u + 1 < model.indexSize) {
                    set.clear(u + 1, model.indexSize);
                }
            }
        } else {
            // negative frange.. make it relatively small
            l = random().nextInt(model.indexSize);
            u = Math.max(model.indexSize - 1, l + random().nextInt(Math.max(model.indexSize / 10, 2)));
            for (FixedBitSet set : sets) {
                int end = Math.min(u + 1, set.length());
                set.clear(l, end);
            }
        }
        String whichField = random().nextBoolean() ? f : f_s;
        return random().nextBoolean() ? // todo: frange doesn't work on the string field?
        frangeStr(f, !positive, l, u, cache, cost, exclude) : rangeStr(whichField, !positive, l, u, cache, cost, exclude);
    } else {
        // term or boolean query
        int numWords = FixedBitSet.bits2words(model.indexSize);
        long[] psetBits = new long[numWords];
        for (int i = 0; i < psetBits.length; i++) {
            // set 50% of the bits on average
            psetBits[i] = random().nextLong();
        }
        // Make sure no 'ghost' bits are set beyond model.indexSize (see FixedBitSet.verifyGhostBitsClear)
        if ((model.indexSize & 0x3f) != 0) {
            // & 0x3f is implicit
            long mask = -1L << model.indexSize;
            psetBits[numWords - 1] &= ~mask;
        }
        FixedBitSet pset = new FixedBitSet(psetBits, model.indexSize);
        if (positive) {
            for (FixedBitSet set : sets) {
                set.and(pset);
            }
        } else {
            for (FixedBitSet set : sets) {
                set.andNot(pset);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int doc = -1; ; ) {
            if (doc + 1 >= model.indexSize)
                break;
            doc = pset.nextSetBit(doc + 1);
            if (doc == DocIdSetIterator.NO_MORE_DOCS)
                break;
            sb.append((positive ? " " : " -") + f + ":" + doc);
        }
        String ret = sb.toString();
        if (ret.length() == 0)
            ret = (positive ? "" : "-") + "id:99999999";
        if (!cache || exclude || random().nextBoolean()) {
            ret = "{!cache=" + cache + ((cost != 0) ? " cost=" + cost : "") + ((exclude) ? " tag=t" : "") + "}" + ret;
        }
        return ret;
    }
}
Also used : FixedBitSet(org.apache.lucene.util.FixedBitSet)

Example 100 with FixedBitSet

use of org.apache.lucene.util.FixedBitSet in project lucene-solr by apache.

the class TestDocSet method doSingle.

protected void doSingle(int maxSize) {
    int sz = rand.nextInt(maxSize + 1);
    int sz2 = rand.nextInt(maxSize);
    FixedBitSet bs1 = getRandomSet(sz, rand.nextInt(sz + 1));
    FixedBitSet bs2 = getRandomSet(sz, rand.nextInt(sz2 + 1));
    DocSet a1 = new BitDocSet(bs1);
    DocSet a2 = new BitDocSet(bs2);
    DocSet b1 = getDocSet(bs1);
    DocSet b2 = getDocSet(bs2);
    checkEqual(bs1, b1);
    checkEqual(bs2, b2);
    iter(a1, b1);
    iter(a2, b2);
    collect(a1, maxSize);
    collect(a2, maxSize);
    FixedBitSet a_and = bs1.clone();
    a_and.and(bs2);
    FixedBitSet a_or = bs1.clone();
    a_or.or(bs2);
    // FixedBitSet a_xor = bs1.clone(); a_xor.xor(bs2);
    FixedBitSet a_andn = bs1.clone();
    a_andn.andNot(bs2);
    checkEqual(a_and, b1.intersection(b2));
    checkEqual(a_or, b1.union(b2));
    checkEqual(a_andn, b1.andNot(b2));
    assertEquals(a_and.cardinality(), b1.intersectionSize(b2));
    assertEquals(a_or.cardinality(), b1.unionSize(b2));
    assertEquals(a_andn.cardinality(), b1.andNotSize(b2));
}
Also used : FixedBitSet(org.apache.lucene.util.FixedBitSet)

Aggregations

FixedBitSet (org.apache.lucene.util.FixedBitSet)162 Term (org.apache.lucene.index.Term)27 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)26 Directory (org.apache.lucene.store.Directory)25 BytesRef (org.apache.lucene.util.BytesRef)22 IOException (java.io.IOException)19 Document (org.apache.lucene.document.Document)17 ArrayList (java.util.ArrayList)15 Query (org.apache.lucene.search.Query)15 NumericDocValues (org.apache.lucene.index.NumericDocValues)14 BitDocIdSet (org.apache.lucene.util.BitDocIdSet)13 Bits (org.apache.lucene.util.Bits)13 LeafReader (org.apache.lucene.index.LeafReader)12 IndexSearcher (org.apache.lucene.search.IndexSearcher)12 TermQuery (org.apache.lucene.search.TermQuery)12 IndexReader (org.apache.lucene.index.IndexReader)11 HashSet (java.util.HashSet)10 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)10 DocIterator (org.apache.solr.search.DocIterator)10 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)9