@@ -102,38 +102,20 @@ public DocIdAndVersion lookupVersion(BytesRef id, boolean loadSeqNo, LeafReaderC
102
102
throws IOException {
103
103
assert context .reader ().getCoreCacheHelper ().getKey ().equals (readerKey ) :
104
104
"context's reader is not the same as the reader class was initialized on." ;
105
- int docID = getDocID (id , context . reader (). getLiveDocs () );
105
+ int docID = getDocID (id , context );
106
106
107
107
if (docID != DocIdSetIterator .NO_MORE_DOCS ) {
108
- final NumericDocValues versions = context .reader ().getNumericDocValues (VersionFieldMapper .NAME );
109
- if (versions == null ) {
110
- throw new IllegalArgumentException ("reader misses the [" + VersionFieldMapper .NAME + "] field" );
111
- }
112
- if (versions .advanceExact (docID ) == false ) {
113
- throw new IllegalArgumentException ("Document [" + docID + "] misses the [" + VersionFieldMapper .NAME + "] field" );
114
- }
115
108
final long seqNo ;
116
109
final long term ;
117
110
if (loadSeqNo ) {
118
- NumericDocValues seqNos = context .reader ().getNumericDocValues (SeqNoFieldMapper .NAME );
119
- // remove the null check in 7.0 once we can't read indices with no seq#
120
- if (seqNos != null && seqNos .advanceExact (docID )) {
121
- seqNo = seqNos .longValue ();
122
- } else {
123
- seqNo = UNASSIGNED_SEQ_NO ;
124
- }
125
- NumericDocValues terms = context .reader ().getNumericDocValues (SeqNoFieldMapper .PRIMARY_TERM_NAME );
126
- if (terms != null && terms .advanceExact (docID )) {
127
- term = terms .longValue ();
128
- } else {
129
- term = UNASSIGNED_PRIMARY_TERM ;
130
- }
131
-
111
+ seqNo = readNumericDocValues (context .reader (), SeqNoFieldMapper .NAME , docID );
112
+ term = readNumericDocValues (context .reader (), SeqNoFieldMapper .PRIMARY_TERM_NAME , docID );
132
113
} else {
133
114
seqNo = UNASSIGNED_SEQ_NO ;
134
115
term = UNASSIGNED_PRIMARY_TERM ;
135
116
}
136
- return new DocIdAndVersion (docID , versions .longValue (), seqNo , term , context .reader (), context .docBase );
117
+ final long version = readNumericDocValues (context .reader (), VersionFieldMapper .NAME , docID );
118
+ return new DocIdAndVersion (docID , version , seqNo , term , context .reader (), context .docBase );
137
119
} else {
138
120
return null ;
139
121
}
@@ -143,9 +125,10 @@ public DocIdAndVersion lookupVersion(BytesRef id, boolean loadSeqNo, LeafReaderC
143
125
* returns the internal lucene doc id for the given id bytes.
144
126
* {@link DocIdSetIterator#NO_MORE_DOCS} is returned if not found
145
127
* */
146
- private int getDocID (BytesRef id , Bits liveDocs ) throws IOException {
128
+ private int getDocID (BytesRef id , LeafReaderContext context ) throws IOException {
147
129
// termsEnum can possibly be null here if this leaf contains only no-ops.
148
130
if (termsEnum != null && termsEnum .seekExact (id )) {
131
+ final Bits liveDocs = context .reader ().getLiveDocs ();
149
132
int docID = DocIdSetIterator .NO_MORE_DOCS ;
150
133
// there may be more than one matching docID, in the case of nested docs, so we want the last one:
151
134
docsEnum = termsEnum .postings (docsEnum , 0 );
@@ -161,41 +144,23 @@ private int getDocID(BytesRef id, Bits liveDocs) throws IOException {
161
144
}
162
145
}
163
146
147
+ private static long readNumericDocValues (LeafReader reader , String field , int docId ) throws IOException {
148
+ final NumericDocValues dv = reader .getNumericDocValues (field );
149
+ if (dv == null || dv .advanceExact (docId ) == false ) {
150
+ assert false : "document [" + docId + "] does not have docValues for [" + field + "]" ;
151
+ throw new IllegalStateException ("document [" + docId + "] does not have docValues for [" + field + "]" );
152
+ }
153
+ return dv .longValue ();
154
+ }
155
+
164
156
/** Return null if id is not found. */
165
157
DocIdAndSeqNo lookupSeqNo (BytesRef id , LeafReaderContext context ) throws IOException {
166
158
assert context .reader ().getCoreCacheHelper ().getKey ().equals (readerKey ) :
167
159
"context's reader is not the same as the reader class was initialized on." ;
168
- // termsEnum can possibly be null here if this leaf contains only no-ops.
169
- if (termsEnum != null && termsEnum .seekExact (id )) {
170
- docsEnum = termsEnum .postings (docsEnum , 0 );
171
- final Bits liveDocs = context .reader ().getLiveDocs ();
172
- DocIdAndSeqNo result = null ;
173
- int docID = docsEnum .nextDoc ();
174
- if (docID != DocIdSetIterator .NO_MORE_DOCS ) {
175
- final NumericDocValues seqNoDV = context .reader ().getNumericDocValues (SeqNoFieldMapper .NAME );
176
- for (; docID != DocIdSetIterator .NO_MORE_DOCS ; docID = docsEnum .nextDoc ()) {
177
- final long seqNo ;
178
- // remove the null check in 7.0 once we can't read indices with no seq#
179
- if (seqNoDV != null && seqNoDV .advanceExact (docID )) {
180
- seqNo = seqNoDV .longValue ();
181
- } else {
182
- seqNo = UNASSIGNED_SEQ_NO ;
183
- }
184
- final boolean isLive = (liveDocs == null || liveDocs .get (docID ));
185
- if (isLive ) {
186
- // The live document must always be the latest copy, thus we can early terminate here.
187
- // If a nested docs is live, we return the first doc which doesn't have term (only the last doc has term).
188
- // This should not be an issue since we no longer use primary term as tier breaker when comparing operations.
189
- assert result == null || result .seqNo <= seqNo :
190
- "the live doc does not have the highest seq_no; live_seq_no=" + seqNo + " < deleted_seq_no=" + result .seqNo ;
191
- return new DocIdAndSeqNo (docID , seqNo , context , isLive );
192
- }
193
- if (result == null || result .seqNo < seqNo ) {
194
- result = new DocIdAndSeqNo (docID , seqNo , context , isLive );
195
- }
196
- }
197
- }
198
- return result ;
160
+ final int docID = getDocID (id , context );
161
+ if (docID != DocIdSetIterator .NO_MORE_DOCS ) {
162
+ final long seqNo = readNumericDocValues (context .reader (), SeqNoFieldMapper .NAME , docID );
163
+ return new DocIdAndSeqNo (docID , seqNo , context );
199
164
} else {
200
165
return null ;
201
166
}
0 commit comments