2525import org .apache .lucene .search .Filter ;
2626import org .apache .lucene .search .FilteredDocIdSetIterator ;
2727import org .apache .lucene .util .Bits ;
28+ import org .apache .lucene .util .FixedBitSet ;
2829import org .elasticsearch .common .lucene .docset .DocIdSets ;
2930
3031import 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