Example usage for org.apache.lucene.util BitSetIterator getFixedBitSetOrNull

List of usage examples for org.apache.lucene.util BitSetIterator getFixedBitSetOrNull

Introduction

In this page you can find the example usage for org.apache.lucene.util BitSetIterator getFixedBitSetOrNull.

Prototype

public static FixedBitSet getFixedBitSetOrNull(DocIdSetIterator iterator) 

Source Link

Document

If the provided iterator wraps a FixedBitSet , returns it, otherwise returns null.

Usage

From source file:de.unihildesheim.iw.lucene.util.DocIdSetUtils.java

License:Open Source License

/**
 * Get the highest document id stored in the {@link DocIdSet}.
 *
 * @param dis DocIdSet// w  w w . j  a v  a  2s  .co m
 * @return Highest document number or {@code -1}, if there's no document
 * @throws IOException Thrown on low-level i/o-errors
 */
public static int maxDoc(@NotNull final DocIdSet dis) throws IOException {
    final int maxDoc;

    @Nullable
    final DocIdSetIterator disi = dis.iterator();
    if (disi == null) {
        maxDoc = 0;
    } else {
        @Nullable
        BitSet bitSet;
        bitSet = BitSetIterator.getFixedBitSetOrNull(disi);
        if (bitSet == null) {
            bitSet = BitSetIterator.getSparseFixedBitSetOrNull(disi);
        }
        if (bitSet == null) {
            bitSet = BitsUtils.bits2BitSet(dis.bits());
        }

        if (bitSet == null) {
            maxDoc = StreamUtils.stream(dis).sorted().max().getAsInt();
        } else {
            if (bitSet.length() == 0) {
                maxDoc = -1;
            } else if (bitSet.length() == 1) {
                maxDoc = bitSet.get(0) ? 0 : -1;
            } else {
                maxDoc = bitSet.prevSetBit(bitSet.length() - 1);
            }
        }
    }
    return maxDoc;
}

From source file:de.unihildesheim.iw.lucene.util.DocIdSetUtils.java

License:Open Source License

/**
 * Get a bits instance from a DocIdSet./*ww  w .j  av a 2  s.  c o m*/
 *
 * @param dis Set whose bits to get
 * @return Bits or null, if no bits are set
 * @throws IOException Thrown on low-level I/O-errors
 */
@Nullable
public static BitSet bits(@NotNull final DocIdSet dis) throws IOException {
    @Nullable
    final DocIdSetIterator disi = dis.iterator();

    if (disi == null) {
        return null;
    } else {
        @Nullable
        BitSet bitSet;

        bitSet = BitSetIterator.getFixedBitSetOrNull(disi);
        if (bitSet == null) {
            bitSet = BitSetIterator.getSparseFixedBitSetOrNull(disi);
        }
        if (bitSet == null) {
            bitSet = BitsUtils.bits2BitSet(dis.bits());
        }

        if (bitSet == null) {
            bitSet = new SparseFixedBitSet(maxDoc(dis) + 1);
            StreamUtils.stream(disi).forEach(bitSet::set);
        }
        return bitSet;
    }
}