2929import org .apache .lucene .search .PrefixQuery ;
3030import org .apache .lucene .search .Query ;
3131import org .apache .lucene .search .TermQuery ;
32- import org .apache .lucene .store .ByteArrayDataInput ;
3332import org .apache .lucene .util .BytesRef ;
3433import org .apache .lucene .util .IOFunction ;
3534import org .elasticsearch .common .CheckedIntFunction ;
35+ import org .elasticsearch .common .io .stream .ByteArrayStreamInput ;
3636import org .elasticsearch .common .lucene .Lucene ;
3737import org .elasticsearch .common .text .UTF8DecodingReader ;
3838import org .elasticsearch .common .unit .Fuzziness ;
@@ -786,31 +786,24 @@ protected void writeValue(Object value, XContentBuilder b) throws IOException {
786786 return fieldLoader ;
787787 }
788788
789-
790789 /**
791790 * A wrapper around {@link BinaryDocValues} that exposes some quality of life functions.
792791 */
793792 private static class CustomBinaryDocValues extends AbstractBinaryDocValues {
794793
795794 private final BinaryDocValues binaryDocValues ;
795+ private final ByteArrayStreamInput stream ;
796796
797- private ByteArrayDataInput data ;
798797 private int docValueCount = 0 ;
799798
800799 CustomBinaryDocValues (BinaryDocValues binaryDocValues ) {
801800 this .binaryDocValues = binaryDocValues ;
801+ this .stream = new ByteArrayStreamInput ();
802802 }
803803
804- public BytesRef nextValue () {
805- // get the length of the value
806- int length = data .readVInt ();
807-
808- // read that many bytes from the underlying bytes array
809- // the read will automatically move the offset to the next value
810- byte [] valueBytes = new byte [length ];
811- data .readBytes (valueBytes , 0 , length );
812-
813- return new BytesRef (valueBytes );
804+ public BytesRef nextValue () throws IOException {
805+ // this function already knows how to decode the underlying bytes array, so no need to explicitly call VInt()
806+ return stream .readBytesRef ();
814807 }
815808
816809 @ Override
@@ -823,8 +816,8 @@ public boolean advanceExact(int docId) throws IOException {
823816 // if document has a value, read underlying bytes
824817 if (binaryDocValues .advanceExact (docId )) {
825818 BytesRef docValuesBytes = binaryDocValues .binaryValue ();
826- data = new ByteArrayDataInput (docValuesBytes .bytes , docValuesBytes .offset , docValuesBytes .length );
827- docValueCount = data .readVInt ();
819+ stream . reset (docValuesBytes .bytes , docValuesBytes .offset , docValuesBytes .length );
820+ docValueCount = stream .readVInt ();
828821 return true ;
829822 }
830823
0 commit comments