use of org.apache.lucene.util.BitSetIterator in project lucene-solr by apache.
the class TestDocSet method getIntDocSet.
public DocSet getIntDocSet(FixedBitSet bs) {
int[] docs = new int[bs.cardinality()];
BitSetIterator iter = new BitSetIterator(bs, 0);
for (int i = 0; i < docs.length; i++) {
docs[i] = iter.nextDoc();
}
return new SortedIntDocSet(docs);
}
use of org.apache.lucene.util.BitSetIterator in project lucene-solr by apache.
the class BitSetPerf method main.
public static void main(String[] args) {
if (args.length < 5) {
System.out.println("BitSetTest <bitSetSize> <numSets> <numBitsSet> <testName> <iter> <impl>");
System.out.println(" impl => open for FixedBitSet");
}
int bitSetSize = Integer.parseInt(args[0]);
int numSets = Integer.parseInt(args[1]);
int numBitsSet = Integer.parseInt(args[2]);
String test = args[3];
int iter = Integer.parseInt(args[4]);
String impl = args.length > 5 ? args[5].intern() : "bit";
BitSet[] sets = new BitSet[numSets];
FixedBitSet[] osets = new FixedBitSet[numSets];
for (int i = 0; i < numSets; i++) {
sets[i] = new BitSet(bitSetSize);
osets[i] = new FixedBitSet(bitSetSize);
randomSets(bitSetSize, numBitsSet, sets[i], osets[i]);
}
BitSet bs = new BitSet(bitSetSize);
FixedBitSet obs = new FixedBitSet(bitSetSize);
randomSets(bitSetSize, numBitsSet, bs, obs);
int ret = 0;
final RTimer timer = new RTimer();
if ("union".equals(test)) {
for (int it = 0; it < iter; it++) {
for (int i = 0; i < numSets; i++) {
if (impl == "open") {
FixedBitSet other = osets[i];
obs.or(other);
} else {
BitSet other = sets[i];
bs.or(other);
}
}
}
}
if ("cardinality".equals(test)) {
for (int it = 0; it < iter; it++) {
for (int i = 0; i < numSets; i++) {
if (impl == "open") {
ret += osets[i].cardinality();
} else {
ret += sets[i].cardinality();
}
}
}
}
if ("get".equals(test)) {
for (int it = 0; it < iter; it++) {
for (int i = 0; i < numSets; i++) {
if (impl == "open") {
FixedBitSet oset = osets[i];
for (int k = 0; k < bitSetSize; k++) if (oset.get(k))
ret++;
} else {
BitSet bset = sets[i];
for (int k = 0; k < bitSetSize; k++) if (bset.get(k))
ret++;
}
}
}
}
if ("icount".equals(test)) {
for (int it = 0; it < iter; it++) {
for (int i = 0; i < numSets - 1; i++) {
if (impl == "open") {
FixedBitSet a = osets[i];
FixedBitSet b = osets[i + 1];
ret += FixedBitSet.intersectionCount(a, b);
} else {
BitSet a = sets[i];
BitSet b = sets[i + 1];
BitSet newset = (BitSet) a.clone();
newset.and(b);
ret += newset.cardinality();
}
}
}
}
if ("clone".equals(test)) {
for (int it = 0; it < iter; it++) {
for (int i = 0; i < numSets; i++) {
if (impl == "open") {
osets[i] = osets[i].clone();
} else {
sets[i] = (BitSet) sets[i].clone();
}
}
}
}
if ("nextSetBit".equals(test)) {
for (int it = 0; it < iter; it++) {
for (int i = 0; i < numSets; i++) {
if (impl == "open") {
final FixedBitSet set = osets[i];
for (int next = set.nextSetBit(0); next != DocIdSetIterator.NO_MORE_DOCS; next = set.nextSetBit(next + 1)) {
ret += next;
}
} else {
final BitSet set = sets[i];
for (int next = set.nextSetBit(0); next >= 0; next = set.nextSetBit(next + 1)) {
ret += next;
}
}
}
}
}
if ("iterator".equals(test)) {
for (int it = 0; it < iter; it++) {
for (int i = 0; i < numSets; i++) {
if (impl == "open") {
final FixedBitSet set = osets[i];
final BitSetIterator iterator = new BitSetIterator(set, 0);
for (int next = iterator.nextDoc(); next >= 0; next = iterator.nextDoc()) {
ret += next;
}
} else {
final BitSet set = sets[i];
for (int next = set.nextSetBit(0); next >= 0; next = set.nextSetBit(next + 1)) {
ret += next;
}
}
}
}
}
System.out.println("ret=" + ret);
System.out.println("TIME=" + timer.getTime());
}
use of org.apache.lucene.util.BitSetIterator in project lucene-solr by apache.
the class CheckJoinIndex method check.
/**
* Check that the given index is good to use for block joins.
* @throws IllegalStateException if the index does not have an appropriate structure
*/
public static void check(IndexReader reader, BitSetProducer parentsFilter) throws IOException {
for (LeafReaderContext context : reader.leaves()) {
if (context.reader().maxDoc() == 0) {
continue;
}
final BitSet parents = parentsFilter.getBitSet(context);
if (parents == null || parents.cardinality() == 0) {
throw new IllegalStateException("Every segment should have at least one parent, but " + context.reader() + " does not have any");
}
if (parents.get(context.reader().maxDoc() - 1) == false) {
throw new IllegalStateException("The last document of a segment must always be a parent, but " + context.reader() + " has a child as a last doc");
}
final Bits liveDocs = context.reader().getLiveDocs();
if (liveDocs != null) {
int prevParentDoc = -1;
DocIdSetIterator it = new BitSetIterator(parents, 0L);
for (int parentDoc = it.nextDoc(); parentDoc != DocIdSetIterator.NO_MORE_DOCS; parentDoc = it.nextDoc()) {
final boolean parentIsLive = liveDocs.get(parentDoc);
for (int child = prevParentDoc + 1; child != parentDoc; child++) {
final boolean childIsLive = liveDocs.get(child);
if (parentIsLive != childIsLive) {
if (childIsLive) {
throw new IllegalStateException("Parent doc " + parentDoc + " of segment " + context.reader() + " is live but has a deleted child document " + child);
} else {
throw new IllegalStateException("Parent doc " + parentDoc + " of segment " + context.reader() + " is deleted but has a live child document " + child);
}
}
}
prevParentDoc = parentDoc;
}
}
}
}
use of org.apache.lucene.util.BitSetIterator in project lucene-solr by apache.
the class PointInSetIncludingScoreQuery method createWeight.
@Override
public final Weight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException {
return new Weight(this) {
@Override
public void extractTerms(Set<Term> terms) {
}
@Override
public Explanation explain(LeafReaderContext context, int doc) throws IOException {
Scorer scorer = scorer(context);
if (scorer != null) {
int target = scorer.iterator().advance(doc);
if (doc == target) {
return Explanation.match(scorer.score(), "A match");
}
}
return Explanation.noMatch("Not a match");
}
@Override
public Scorer scorer(LeafReaderContext context) throws IOException {
LeafReader reader = context.reader();
FieldInfo fieldInfo = reader.getFieldInfos().fieldInfo(field);
if (fieldInfo == null) {
return null;
}
if (fieldInfo.getPointDimensionCount() != 1) {
throw new IllegalArgumentException("field=\"" + field + "\" was indexed with numDims=" + fieldInfo.getPointDimensionCount() + " but this query has numDims=1");
}
if (fieldInfo.getPointNumBytes() != bytesPerDim) {
throw new IllegalArgumentException("field=\"" + field + "\" was indexed with bytesPerDim=" + fieldInfo.getPointNumBytes() + " but this query has bytesPerDim=" + bytesPerDim);
}
PointValues values = reader.getPointValues(field);
if (values == null) {
return null;
}
FixedBitSet result = new FixedBitSet(reader.maxDoc());
float[] scores = new float[reader.maxDoc()];
values.intersect(new MergePointVisitor(sortedPackedPoints, result, scores));
return new Scorer(this) {
DocIdSetIterator disi = new BitSetIterator(result, 10L);
@Override
public float score() throws IOException {
return scores[docID()];
}
@Override
public int freq() throws IOException {
return 1;
}
@Override
public int docID() {
return disi.docID();
}
@Override
public DocIdSetIterator iterator() {
return disi;
}
};
}
};
}
use of org.apache.lucene.util.BitSetIterator in project lucene-solr by apache.
the class IndexedDISI method flush.
private static void flush(int block, FixedBitSet buffer, int cardinality, IndexOutput out) throws IOException {
assert block >= 0 && block < 65536;
out.writeShort((short) block);
assert cardinality > 0 && cardinality <= 65536;
out.writeShort((short) (cardinality - 1));
if (cardinality > MAX_ARRAY_LENGTH) {
if (cardinality != 65536) {
// all docs are set
for (long word : buffer.getBits()) {
out.writeLong(word);
}
}
} else {
BitSetIterator it = new BitSetIterator(buffer, cardinality);
for (int doc = it.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = it.nextDoc()) {
out.writeShort((short) doc);
}
}
}
Aggregations