Example usage for org.apache.lucene.util Bits Bits

List of usage examples for org.apache.lucene.util Bits Bits

Introduction

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

Prototype

Bits

Source Link

Usage

From source file:cn.hbu.cs.esearch.core.EsearchSegmentReader.java

License:Apache License

@Override
public Bits getLiveDocs() {
    ensureOpen();/*  w w w.  ja va 2s  .com*/
    return new Bits() {
        @Override
        public boolean get(int index) {
            int[] delSet = currentDelDocIds;
            if (delSet != null && Arrays.binarySearch(delSet, index) >= 0) {
                return false;
            }
            Bits liveDocs = in.getLiveDocs();
            if (liveDocs == null) {
                return true;
            }
            return liveDocs.get(index);
        }

        @Override
        public int length() {
            return in.getLiveDocs().length();
        }
    };
}

From source file:lucene.security.index.SecureAtomicReader.java

License:Apache License

@Override
public Bits getLiveDocs() {
    final Bits liveDocs = in.getLiveDocs();
    final int maxDoc = maxDoc();
    return new Bits() {

        @Override//  w w w. j  a  va 2 s. c o  m
        public boolean get(int index) {
            if (liveDocs == null || liveDocs.get(index)) {
                // Need to check access
                try {
                    if (_accessControl.hasAccess(ReadType.LIVEDOCS, index)) {
                        return true;
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return false;
        }

        @Override
        public int length() {
            return maxDoc;
        }

    };
}

From source file:lucene.security.index.SecureAtomicReader.java

License:Apache License

public static Bits getSecureLiveDocs(Bits bits, int maxDoc, final AccessControlReader accessControlReader) {
    final Bits liveDocs;
    if (bits == null) {
        liveDocs = getMatchAll(maxDoc);//from w  w w .java2s  . com
    } else {
        liveDocs = bits;
    }
    final int length = liveDocs.length();
    Bits secureLiveDocs = new Bits() {
        @Override
        public boolean get(int index) {
            if (liveDocs.get(index)) {
                try {
                    if (accessControlReader.hasAccess(ReadType.DOCS_ENUM, index)) {
                        return true;
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return false;
        }

        @Override
        public int length() {
            return length;
        }
    };
    return secureLiveDocs;
}

From source file:lucene.security.index.SecureAtomicReader.java

License:Apache License

public static Bits getMatchAll(final int length) {
    return new Bits() {

        @Override/*from  ww w. j av a  2s.  c  o m*/
        public int length() {
            return length;
        }

        @Override
        public boolean get(int index) {
            return true;
        }
    };
}

From source file:lucene.security.search.DocumentVisibilityFilter.java

License:Apache License

public static DocIdSet getLogicalOr(final List<DocIdSet> list) throws IOException {
    if (list.size() == 0) {
        return DocIdSet.EMPTY_DOCIDSET;
    }// w ww . j  a  v  a2s . c  o m
    if (list.size() == 1) {
        DocIdSet docIdSet = list.get(0);
        Bits bits = docIdSet.bits();
        if (bits == null) {
            throw new IOException("Bits are not allowed to be null for DocIdSet [" + docIdSet + "].");
        }
        return docIdSet;
    }
    int index = 0;
    final Bits[] bitsArray = new Bits[list.size()];
    int length = -1;
    for (DocIdSet docIdSet : list) {
        Bits bits = docIdSet.bits();
        if (bits == null) {
            throw new IOException("Bits are not allowed to be null for DocIdSet [" + docIdSet + "].");
        }
        bitsArray[index] = bits;
        index++;
        if (length < 0) {
            length = bits.length();
        } else if (length != bits.length()) {
            throw new IOException(
                    "Bits length need to be the same [" + length + "] and [" + bits.length() + "]");
        }
    }
    final int len = length;
    return new DocIdSet() {

        @Override
        public Bits bits() throws IOException {
            return new Bits() {

                @Override
                public boolean get(int index) {
                    for (int i = 0; i < bitsArray.length; i++) {
                        if (bitsArray[i].get(index)) {
                            return true;
                        }
                    }
                    return false;
                }

                @Override
                public int length() {
                    return len;
                }

            };
        }

        @Override
        public boolean isCacheable() {
            return true;
        }

        @Override
        public DocIdSetIterator iterator() throws IOException {
            final DocIdSetIterator[] docIdSetIteratorArray = new DocIdSetIterator[list.size()];
            long c = 0;
            int index = 0;
            for (DocIdSet docIdSet : list) {
                DocIdSetIterator iterator = docIdSet.iterator();
                iterator.nextDoc();
                docIdSetIteratorArray[index] = iterator;
                c += iterator.cost();
                index++;
            }
            final long cost = c;
            return new DocIdSetIterator() {

                private int _docId = -1;

                @Override
                public int advance(int target) throws IOException {
                    callAdvanceOnAllThatAreBehind(target);
                    Arrays.sort(docIdSetIteratorArray, COMPARATOR);
                    DocIdSetIterator iterator = docIdSetIteratorArray[0];
                    return _docId = iterator.docID();
                }

                private void callAdvanceOnAllThatAreBehind(int target) throws IOException {
                    for (int i = 0; i < docIdSetIteratorArray.length; i++) {
                        DocIdSetIterator iterator = docIdSetIteratorArray[i];
                        if (iterator.docID() < target) {
                            iterator.advance(target);
                        }
                    }
                }

                @Override
                public int nextDoc() throws IOException {
                    return advance(_docId + 1);
                }

                @Override
                public int docID() {
                    return _docId;
                }

                @Override
                public long cost() {
                    return cost;
                }

            };
        }
    };
}

From source file:org.apache.blur.mapreduce.lib.PrimeDocOverFlowHelper.java

License:Apache License

private static AtomicReader setDocSize(AtomicReader reader, final int count) {
    return new FilterAtomicReader(reader) {
        @Override// w  w w. j  a v  a 2 s. com
        public Bits getLiveDocs() {
            return new Bits() {
                @Override
                public boolean get(int index) {
                    return true;
                }

                @Override
                public int length() {
                    return count;
                }
            };
        }

        @Override
        public int numDocs() {
            return count;
        }

        @Override
        public int maxDoc() {
            return count;
        }

        @Override
        public void document(int docID, StoredFieldVisitor visitor) throws IOException {
            // Do nothing
        }
    };
}

From source file:org.apache.solr.search.BitDocSet.java

License:Apache License

@Override
public Filter getTopFilter() {
    final OpenBitSet bs = bits;
    // TODO: if cardinality isn't cached, do a quick measure of sparseness
    // and return null from bits() if too sparse.

    return new Filter() {
        @Override//from  ww  w . j av  a 2  s.com
        public DocIdSet getDocIdSet(final AtomicReaderContext context, final Bits acceptDocs) {
            AtomicReader reader = context.reader();
            // all Solr DocSets that are used as filters only include live docs
            final Bits acceptDocs2 = acceptDocs == null ? null
                    : (reader.getLiveDocs() == acceptDocs ? null : acceptDocs);

            if (context.isTopLevel) {
                return BitsFilteredDocIdSet.wrap(bs, acceptDocs);
            }

            final int base = context.docBase;
            final int maxDoc = reader.maxDoc();
            final int max = base + maxDoc; // one past the max doc in this segment.

            return BitsFilteredDocIdSet.wrap(new DocIdSet() {
                @Override
                public DocIdSetIterator iterator() {
                    return new DocIdSetIterator() {
                        int pos = base - 1;
                        int adjustedDoc = -1;

                        @Override
                        public int docID() {
                            return adjustedDoc;
                        }

                        @Override
                        public int nextDoc() {
                            pos = bs.nextSetBit(pos + 1);
                            return adjustedDoc = (pos >= 0 && pos < max) ? pos - base : NO_MORE_DOCS;
                        }

                        @Override
                        public int advance(int target) {
                            if (target == NO_MORE_DOCS)
                                return adjustedDoc = NO_MORE_DOCS;
                            pos = bs.nextSetBit(target + base);
                            return adjustedDoc = (pos >= 0 && pos < max) ? pos - base : NO_MORE_DOCS;
                        }

                        @Override
                        public long cost() {
                            // we don't want to actually compute cardinality, but
                            // if its already been computed, we use it
                            if (size != -1) {
                                return size;
                            } else {
                                return bs.capacity();
                            }
                        }
                    };
                }

                @Override
                public boolean isCacheable() {
                    return true;
                }

                @Override
                public Bits bits() {
                    return new Bits() {
                        @Override
                        public boolean get(int index) {
                            return bs.fastGet(index + base);
                        }

                        @Override
                        public int length() {
                            return maxDoc;
                        }
                    };
                }

            }, acceptDocs2);
        }
    };
}

From source file:org.apache.solr.search.FilteredDocIdSet.java

License:Apache License

@Override
public Bits bits() throws IOException {
    final Bits bits = _innerSet.bits();
    return (bits == null) ? null : new Bits() {
        @Override//  w  w w  . jav a2s. c om
        public boolean get(int docid) {
            return bits.get(docid) && FilteredDocIdSet.this.match(docid);
        }

        @Override
        public int length() {
            return bits.length();
        }
    };
}

From source file:org.codelibs.elasticsearch.common.lucene.Lucene.java

License:Apache License

/**
 * Given a {Scorer}, return a {Bits} instance that will match
 * all documents contained in the set. Note that the returned {Bits}
 * instance MUST be consumed in order.//from   w  ww .  j av  a 2  s .  c  o m
 */
public static Bits asSequentialAccessBits(final int maxDoc, @Nullable Scorer scorer) throws IOException {
    if (scorer == null) {
        return new Bits.MatchNoBits(maxDoc);
    }
    final TwoPhaseIterator twoPhase = scorer.twoPhaseIterator();
    final DocIdSetIterator iterator;
    if (twoPhase == null) {
        iterator = scorer.iterator();
    } else {
        iterator = twoPhase.approximation();
    }

    return new Bits() {

        int previous = -1;
        boolean previousMatched = false;

        @Override
        public boolean get(int index) {
            if (index < 0 || index >= maxDoc) {
                throw new IndexOutOfBoundsException(index + " is out of bounds: [" + 0 + "-" + maxDoc + "[");
            }
            if (index < previous) {
                throw new IllegalArgumentException("This Bits instance can only be consumed in order. "
                        + "Got called on [" + index + "] while previously called on [" + previous + "]");
            }
            if (index == previous) {
                // we cache whether it matched because it is illegal to call
                // twoPhase.matches() twice
                return previousMatched;
            }
            previous = index;

            int doc = iterator.docID();
            if (doc < index) {
                try {
                    doc = iterator.advance(index);
                } catch (IOException e) {
                    throw new IllegalStateException("Cannot advance iterator", e);
                }
            }
            if (index == doc) {
                try {
                    return previousMatched = twoPhase == null || twoPhase.matches();
                } catch (IOException e) {
                    throw new IllegalStateException("Cannot validate match", e);
                }
            }
            return previousMatched = false;
        }

        @Override
        public int length() {
            return maxDoc;
        }
    };
}

From source file:org.codelibs.elasticsearch.index.fielddata.FieldData.java

License:Apache License

/**
 * Returns a {Bits} representing all documents from <code>dv</code> that have a value.
 *//*from w w  w.j a v  a 2s . c  o  m*/
public static Bits docsWithValue(final SortedBinaryDocValues dv, final int maxDoc) {
    return new Bits() {
        @Override
        public boolean get(int index) {
            dv.setDocument(index);
            return dv.count() != 0;
        }

        @Override
        public int length() {
            return maxDoc;
        }
    };
}