@@ -183,16 +183,20 @@ void readDocsForNextPage() throws IOException {
183183 for (LeafIterator leaf : oneTsidQueue ) {
184184 leaf .reinitializeIfNeeded (executingThread );
185185 }
186- do {
187- PriorityQueue <LeafIterator > sub = subQueueForNextTsid ();
188- if (sub .size () == 0 ) {
189- break ;
190- }
191- tsHashesBuilder .appendNewTsid (sub .top ().timeSeriesHash );
192- if (readValuesForOneTsid (sub )) {
193- break ;
194- }
195- } while (mainQueue .size () > 0 );
186+ if (mainQueue .size () + oneTsidQueue .size () == 1 ) {
187+ readValuesFromSingleRemainingLeaf ();
188+ } else {
189+ do {
190+ PriorityQueue <LeafIterator > sub = subQueueForNextTsid ();
191+ if (sub .size () == 0 ) {
192+ break ;
193+ }
194+ tsHashesBuilder .appendNewTsid (sub .top ().timeSeriesHash );
195+ if (readValuesForOneTsid (sub )) {
196+ break ;
197+ }
198+ } while (mainQueue .size () > 0 );
199+ }
196200 }
197201
198202 private boolean readValuesForOneTsid (PriorityQueue <LeafIterator > sub ) throws IOException {
@@ -236,6 +240,38 @@ private PriorityQueue<LeafIterator> subQueueForNextTsid() {
236240 return oneTsidQueue ;
237241 }
238242
243+ private void readValuesFromSingleRemainingLeaf () throws IOException {
244+ if (oneTsidQueue .size () == 0 ) {
245+ oneTsidQueue .add (getMainQueue ().pop ());
246+ tsidsLoaded ++;
247+ }
248+ final LeafIterator sub = oneTsidQueue .top ();
249+ int lastTsid = -1 ;
250+ do {
251+ currentPagePos ++;
252+ remainingDocs --;
253+ docCollector .collect (sub .segmentOrd , sub .docID );
254+ if (lastTsid != sub .lastTsidOrd ) {
255+ tsHashesBuilder .appendNewTsid (sub .timeSeriesHash );
256+ lastTsid = sub .lastTsidOrd ;
257+ }
258+ tsHashesBuilder .appendOrdinal ();
259+ timestampsBuilder .appendLong (sub .timestamp );
260+ if (sub .nextDoc () == false ) {
261+ if (sub .docID == DocIdSetIterator .NO_MORE_DOCS ) {
262+ oneTsidQueue .clear ();
263+ return ;
264+ } else {
265+ ++tsidsLoaded ;
266+ }
267+ }
268+ } while (remainingDocs > 0 && currentPagePos < maxPageSize );
269+ }
270+
271+ private PriorityQueue <LeafIterator > getMainQueue () {
272+ return mainQueue ;
273+ }
274+
239275 boolean completed () {
240276 return mainQueue .size () == 0 && oneTsidQueue .size () == 0 ;
241277 }
0 commit comments