@@ -572,30 +572,22 @@ BytesRef decode(int docNumber, int numBlocks) throws IOException {
572572 return uncompressedBytesRef ;
573573 }
574574
575- int computeMultipleBlockBufferSize (int count , int firstDoc , long firstBlockId ) throws IOException {
576- long lastBlockId = firstBlockId ;
577-
578- int remaining = count ;
579- int nextDoc = firstDoc ;
580- for (long blockId = firstBlockId ; remaining > 0 ; blockId ++, lastBlockId ++) {
581- long blockStart = docOffsets .get (blockId );
582- long blockLimit = docOffsets .get (blockId + 1 );
583- int numDocsInBlock = (int ) (blockLimit - blockStart );
584- int idxFirstDocInBlock = (int ) (nextDoc - blockStart );
585- int countInBlock = Math .min (numDocsInBlock - idxFirstDocInBlock , remaining );
586- remaining -= countInBlock ;
587- nextDoc += countInBlock ;
588- }
589-
590- // We could use compressedData directly, but making a clone seems less error-prone.
575+ int computeMultipleBlockBufferSize (int count , int firstDoc , long firstBlockId , long numBlocks ) throws IOException {
591576 IndexInput readAhead = compressedData .clone ();
577+ int lastDoc = firstDoc + count - 1 ;
592578 int requiredBufferSize = 0 ;
593- for (long blockId = firstBlockId ; blockId <= lastBlockId ; blockId ++) {
579+
580+ for (long blockId = firstBlockId ; blockId < numBlocks ; blockId ++) {
594581 long blockStartOffset = addresses .get (blockId );
595582 readAhead .seek (blockStartOffset );
596583 readAhead .readByte (); // skip BlockHeader
597584 int uncompressedBlockLength = readAhead .readVInt ();
598585 requiredBufferSize += uncompressedBlockLength ;
586+
587+ long blockLimit = docOffsets .get (blockId + 1 );
588+ if (lastDoc < blockLimit ) {
589+ break ;
590+ }
599591 }
600592 return requiredBufferSize ;
601593 }
@@ -610,7 +602,7 @@ void decodeBulk(int numBlocks, int firstDoc, int count, BlockLoader.SingletonByt
610602 // This block contains at least one value for range.
611603 long firstBlockId = findAndUpdateBlock (nextDoc , numBlocks );
612604 long [] offsets = new long [count + 1 ];
613- int bufferSize = computeMultipleBlockBufferSize (count , firstDoc , firstBlockId );
605+ int bufferSize = computeMultipleBlockBufferSize (count , firstDoc , firstBlockId , numBlocks );
614606 byte [] bytes = new byte [bufferSize ];
615607
616608 while (remainingCount > 0 ) {
0 commit comments