@@ -102,38 +102,20 @@ public DocIdAndVersion lookupVersion(BytesRef id, boolean loadSeqNo, LeafReaderC
102102 throws IOException {
103103 assert context .reader ().getCoreCacheHelper ().getKey ().equals (readerKey ) :
104104 "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 );
106106
107107 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- }
115108 final long seqNo ;
116109 final long term ;
117110 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 );
132113 } else {
133114 seqNo = UNASSIGNED_SEQ_NO ;
134115 term = UNASSIGNED_PRIMARY_TERM ;
135116 }
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 );
137119 } else {
138120 return null ;
139121 }
@@ -143,9 +125,10 @@ public DocIdAndVersion lookupVersion(BytesRef id, boolean loadSeqNo, LeafReaderC
143125 * returns the internal lucene doc id for the given id bytes.
144126 * {@link DocIdSetIterator#NO_MORE_DOCS} is returned if not found
145127 * */
146- private int getDocID (BytesRef id , Bits liveDocs ) throws IOException {
128+ private int getDocID (BytesRef id , LeafReaderContext context ) throws IOException {
147129 // termsEnum can possibly be null here if this leaf contains only no-ops.
148130 if (termsEnum != null && termsEnum .seekExact (id )) {
131+ final Bits liveDocs = context .reader ().getLiveDocs ();
149132 int docID = DocIdSetIterator .NO_MORE_DOCS ;
150133 // there may be more than one matching docID, in the case of nested docs, so we want the last one:
151134 docsEnum = termsEnum .postings (docsEnum , 0 );
@@ -161,41 +144,23 @@ private int getDocID(BytesRef id, Bits liveDocs) throws IOException {
161144 }
162145 }
163146
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+
164156 /** Return null if id is not found. */
165157 DocIdAndSeqNo lookupSeqNo (BytesRef id , LeafReaderContext context ) throws IOException {
166158 assert context .reader ().getCoreCacheHelper ().getKey ().equals (readerKey ) :
167159 "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 );
199164 } else {
200165 return null ;
201166 }
0 commit comments