use of org.apache.lucene.search.DocIdSetIterator in project lucene-solr by apache.
the class TestDocsWithFieldSet method testSparse.
public void testSparse() throws IOException {
DocsWithFieldSet set = new DocsWithFieldSet();
int doc = random().nextInt(10000);
set.add(doc);
DocIdSetIterator it = set.iterator();
assertEquals(doc, it.nextDoc());
assertEquals(DocIdSetIterator.NO_MORE_DOCS, it.nextDoc());
int doc2 = doc + TestUtil.nextInt(random(), 1, 100);
set.add(doc2);
it = set.iterator();
assertEquals(doc, it.nextDoc());
assertEquals(doc2, it.nextDoc());
assertEquals(DocIdSetIterator.NO_MORE_DOCS, it.nextDoc());
}
use of org.apache.lucene.search.DocIdSetIterator in project lucene-solr by apache.
the class TestDocsWithFieldSet method testDenseThenSparse.
public void testDenseThenSparse() throws IOException {
int denseCount = random().nextInt(10000);
int nextDoc = denseCount + random().nextInt(10000);
DocsWithFieldSet set = new DocsWithFieldSet();
for (int i = 0; i < denseCount; ++i) {
set.add(i);
}
set.add(nextDoc);
DocIdSetIterator it = set.iterator();
for (int i = 0; i < denseCount; ++i) {
assertEquals(i, it.nextDoc());
}
assertEquals(nextDoc, it.nextDoc());
assertEquals(DocIdSetIterator.NO_MORE_DOCS, it.nextDoc());
}
use of org.apache.lucene.search.DocIdSetIterator in project lucene-solr by apache.
the class NotDocIdSet method iterator.
@Override
public DocIdSetIterator iterator() throws IOException {
final DocIdSetIterator inIterator = in.iterator();
return new DocIdSetIterator() {
int doc = -1;
int nextSkippedDoc = -1;
@Override
public int nextDoc() throws IOException {
return advance(doc + 1);
}
@Override
public int advance(int target) throws IOException {
doc = target;
if (doc > nextSkippedDoc) {
nextSkippedDoc = inIterator.advance(doc);
}
while (true) {
if (doc >= maxDoc) {
return doc = NO_MORE_DOCS;
}
assert doc <= nextSkippedDoc;
if (doc != nextSkippedDoc) {
return doc;
}
doc += 1;
nextSkippedDoc = inIterator.nextDoc();
}
}
@Override
public int docID() {
return doc;
}
@Override
public long cost() {
// costs O(maxDoc) in all cases
return maxDoc;
}
};
}
use of org.apache.lucene.search.DocIdSetIterator in project lucene-solr by apache.
the class TestFilteredDocIdSet method testFilteredDocIdSet.
public void testFilteredDocIdSet() throws Exception {
final int maxdoc = 10;
final DocIdSet innerSet = new DocIdSet() {
@Override
public long ramBytesUsed() {
return 0L;
}
@Override
public DocIdSetIterator iterator() {
return new DocIdSetIterator() {
int docid = -1;
@Override
public int docID() {
return docid;
}
@Override
public int nextDoc() {
docid++;
return docid < maxdoc ? docid : (docid = NO_MORE_DOCS);
}
@Override
public int advance(int target) throws IOException {
return slowAdvance(target);
}
@Override
public long cost() {
return 1;
}
};
}
};
DocIdSet filteredSet = new FilteredDocIdSet(innerSet) {
@Override
protected boolean match(int docid) {
//validate only even docids
return docid % 2 == 0;
}
};
DocIdSetIterator iter = filteredSet.iterator();
ArrayList<Integer> list = new ArrayList<>();
int doc = iter.advance(3);
if (doc != DocIdSetIterator.NO_MORE_DOCS) {
list.add(Integer.valueOf(doc));
while ((doc = iter.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
list.add(Integer.valueOf(doc));
}
}
int[] docs = new int[list.size()];
int c = 0;
Iterator<Integer> intIter = list.iterator();
while (intIter.hasNext()) {
docs[c++] = intIter.next().intValue();
}
int[] answer = new int[] { 4, 6, 8 };
boolean same = Arrays.equals(answer, docs);
if (!same) {
System.out.println("answer: " + Arrays.toString(answer));
System.out.println("gotten: " + Arrays.toString(docs));
fail();
}
}
use of org.apache.lucene.search.DocIdSetIterator in project lucene-solr by apache.
the class TestDocSet method doTestIteratorEqual.
public void doTestIteratorEqual(DocIdSet a, DocIdSet b) throws IOException {
DocIdSetIterator ia = a.iterator();
DocIdSetIterator ib = b.iterator();
// test for next() equivalence
for (; ; ) {
int da = ia.nextDoc();
int db = ib.nextDoc();
assertEquals(da, db);
assertEquals(ia.docID(), ib.docID());
if (da == DocIdSetIterator.NO_MORE_DOCS)
break;
}
for (int i = 0; i < 10; i++) {
// test random skipTo() and next()
ia = a.iterator();
ib = b.iterator();
int doc = -1;
for (; ; ) {
int da, db;
if (rand.nextBoolean()) {
da = ia.nextDoc();
db = ib.nextDoc();
} else {
// keep in mind future edge cases like probing (increase if necessary)
int target = doc + rand.nextInt(10) + 1;
da = ia.advance(target);
db = ib.advance(target);
}
assertEquals(da, db);
assertEquals(ia.docID(), ib.docID());
if (da == DocIdSetIterator.NO_MORE_DOCS)
break;
doc = da;
}
}
}
Aggregations