Skip to content

Commit c36638d

Browse files
committed
not delete filter improvements
- don't check no null for liveDocs, since we know they are not null with the check for hasDeletion - improve iteration over liveDocs vs. innerSet, prefer to iterate over the faster one
1 parent 6cfd938 commit c36638d

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

src/main/java/org/elasticsearch/common/lucene/search/DeletionAwareConstantScoreQuery.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,8 @@
2323
import org.apache.lucene.search.Filter;
2424

2525
/**
26-
*
26+
* We still need sometimes to exclude deletes, because we don't remove them always with acceptDocs on filters
2727
*/
28-
// LUCENE MONITOR: Against ConstantScoreQuery, basically added logic in the doc iterator to take deletions into account
29-
// So it can basically be cached safely even with a reader that changes deletions but remain with teh same cache key
30-
// See more: https://issues.apache.org/jira/browse/LUCENE-2468
31-
// TODO Lucene 4.0 won't need this, since live docs are "and'ed" while scoring
32-
// LUCENE 4 UPGRADE: we probably don't need this anymore, because of acceptDocs
3328
public class DeletionAwareConstantScoreQuery extends ConstantScoreQuery {
3429

3530
private final Filter actualFilter;

src/main/java/org/elasticsearch/common/lucene/search/NotDeletedFilter.java

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.lucene.search.Filter;
2626
import org.apache.lucene.search.FilteredDocIdSetIterator;
2727
import org.apache.lucene.util.Bits;
28+
import org.apache.lucene.util.FixedBitSet;
2829
import org.elasticsearch.common.lucene.docset.DocIdSets;
2930

3031
import java.io.IOException;
@@ -64,16 +65,37 @@ public String toString() {
6465
static class NotDeletedDocIdSet extends DocIdSet {
6566

6667
private final DocIdSet innerSet;
67-
6868
private final Bits liveDocs;
6969

7070
NotDeletedDocIdSet(DocIdSet innerSet, Bits liveDocs) {
7171
this.innerSet = innerSet;
7272
this.liveDocs = liveDocs;
7373
}
7474

75+
@Override
76+
public boolean isCacheable() {
77+
return innerSet.isCacheable();
78+
}
79+
80+
@Override
81+
public Bits bits() throws IOException {
82+
Bits bits = innerSet.bits();
83+
if (bits == null) {
84+
return null;
85+
}
86+
return new NotDeleteBits(bits, liveDocs);
87+
}
88+
7589
@Override
7690
public DocIdSetIterator iterator() throws IOException {
91+
if (!DocIdSets.isFastIterator(innerSet) && liveDocs instanceof FixedBitSet) {
92+
// might as well iterate over the live docs..., since the iterator is not fast enough
93+
// but we can only do that if we have Bits..., in short, we reverse the order...
94+
Bits bits = innerSet.bits();
95+
if (bits != null) {
96+
return new NotDeletedDocIdSetIterator(((FixedBitSet) liveDocs).iterator(), bits);
97+
}
98+
}
7799
DocIdSetIterator iterator = innerSet.iterator();
78100
if (iterator == null) {
79101
return null;
@@ -82,18 +104,39 @@ public DocIdSetIterator iterator() throws IOException {
82104
}
83105
}
84106

85-
static class NotDeletedDocIdSetIterator extends FilteredDocIdSetIterator {
107+
static class NotDeleteBits implements Bits {
86108

109+
private final Bits bits;
87110
private final Bits liveDocs;
88111

89-
NotDeletedDocIdSetIterator(DocIdSetIterator innerIter, Bits liveDocs) {
90-
super(innerIter);
112+
NotDeleteBits(Bits bits, Bits liveDocs) {
113+
this.bits = bits;
91114
this.liveDocs = liveDocs;
92115
}
93116

117+
@Override
118+
public boolean get(int index) {
119+
return liveDocs.get(index) && bits.get(index);
120+
}
121+
122+
@Override
123+
public int length() {
124+
return bits.length();
125+
}
126+
}
127+
128+
static class NotDeletedDocIdSetIterator extends FilteredDocIdSetIterator {
129+
130+
private final Bits match;
131+
132+
NotDeletedDocIdSetIterator(DocIdSetIterator innerIter, Bits match) {
133+
super(innerIter);
134+
this.match = match;
135+
}
136+
94137
@Override
95138
protected boolean match(int doc) {
96-
return liveDocs == null || liveDocs.get(doc);
139+
return match.get(doc);
97140
}
98141
}
99142
}

0 commit comments

Comments
 (0)