77
88import org .apache .lucene .store .BufferedIndexInput ;
99import org .apache .lucene .store .IndexInput ;
10+ import org .elasticsearch .common .blobstore .BlobContainer ;
1011import org .elasticsearch .index .snapshots .blobstore .BlobStoreIndexShardSnapshot .FileInfo ;
1112
1213import java .io .EOFException ;
1314import java .io .IOException ;
15+ import java .io .InputStream ;
1416import java .util .Objects ;
1517
1618/**
3335 */
3436public class SearchableSnapshotIndexInput extends BufferedIndexInput {
3537
36- private final BlobBytesReader reader ;
38+ private final BlobContainer blobContainer ;
3739 private final FileInfo fileInfo ;
40+ private final long offset ;
41+ private final long length ;
3842
3943 private long position ;
4044 private boolean closed ;
4145
42- public SearchableSnapshotIndexInput (final BlobBytesReader reader , final FileInfo fileInfo ) {
43- this ("SearchableSnapshotIndexInput(" + fileInfo + ")" , reader , fileInfo , 0L );
46+ public SearchableSnapshotIndexInput (final BlobContainer blobContainer , final FileInfo fileInfo ) {
47+ this ("SearchableSnapshotIndexInput(" + fileInfo . physicalName () + ")" , blobContainer , fileInfo , 0L , 0L , fileInfo . length () );
4448 }
4549
46- private SearchableSnapshotIndexInput (final String resourceDesc , final BlobBytesReader reader ,
47- final FileInfo fileInfo , final long position ) {
50+ private SearchableSnapshotIndexInput (final String resourceDesc , final BlobContainer blobContainer ,
51+ final FileInfo fileInfo , final long position , final long offset , final long length ) {
4852 super (resourceDesc );
49- this .reader = Objects .requireNonNull (reader );
53+ this .blobContainer = Objects .requireNonNull (blobContainer );
5054 this .fileInfo = Objects .requireNonNull (fileInfo );
55+ this .offset = offset ;
56+ this .length = length ;
5157 this .position = position ;
5258 this .closed = false ;
5359 }
5460
5561 @ Override
5662 public long length () {
57- return fileInfo . length () ;
63+ return length ;
5864 }
5965
6066 private void ensureOpen () throws IOException {
@@ -88,29 +94,33 @@ protected void readInternal(byte[] b, int offset, int length) throws IOException
8894 }
8995
9096 private void readInternalBytes (final long part , final long pos , byte [] b , int offset , int length ) throws IOException {
91- reader .readBlobBytes (fileInfo .partName (part ), pos , length , b , offset );
92- position += length ;
97+ try (InputStream inputStream = blobContainer .readBlob (fileInfo .partName (part ), pos , length )) {
98+ int read = inputStream .read (b , offset , length );
99+ assert read == length ;
100+ position += read ;
101+ }
93102 }
94103
95104 @ Override
96105 protected void seekInternal (long pos ) throws IOException {
97- if (pos > fileInfo . length () ) {
98- throw new EOFException ("Reading past end of file [position=" + pos + ", length=" + fileInfo . length () + "] for " + toString ());
106+ if (pos > length ) {
107+ throw new EOFException ("Reading past end of file [position=" + pos + ", length=" + length + "] for " + toString ());
99108 } else if (pos < 0L ) {
100109 throw new IOException ("Seeking to negative position [" + pos + "] for " + toString ());
101110 }
102- this .position = pos ;
111+ this .position = offset + pos ;
103112 }
104113
105114 @ Override
106115 public BufferedIndexInput clone () {
107- return new SearchableSnapshotIndexInput ("clone(" + this + ")" , reader , fileInfo , position );
116+ return new SearchableSnapshotIndexInput ("clone(" + this + ")" , blobContainer , fileInfo , position , offset , length );
108117 }
109118
110119 @ Override
111120 public IndexInput slice (String sliceDescription , long offset , long length ) throws IOException {
112121 if ((offset >= 0L ) && (length >= 0L ) && (offset + length <= length ())) {
113- final Slice slice = new Slice (sliceDescription , offset , length , this );
122+ final SearchableSnapshotIndexInput slice =
123+ new SearchableSnapshotIndexInput (sliceDescription , blobContainer , fileInfo , position , this .offset + offset , length );
114124 slice .seek (0L );
115125 return slice ;
116126 } else {
@@ -126,57 +136,12 @@ public void close() throws IOException {
126136
127137 @ Override
128138 public String toString () {
129- return getClass ().getSimpleName ()
130- + "(resourceDesc=" + super .toString ()
131- + ", name=" + fileInfo .physicalName ()
132- + ", length=" + fileInfo .length ()
133- + ", sizeOfParts=" + fileInfo .partSize ()
134- + ", numberOfParts=" + fileInfo .numberOfParts () + ")" ;
135- }
136-
137- /**
138- * A slice created from a {@link SearchableSnapshotIndexInput}.
139- *
140- * The slice overrides the {@link #length()} and {@link #seekInternal(long)}
141- * methods so that it adjust the values according to initial offset position
142- * from which the slice was created.
143- */
144- private static class Slice extends SearchableSnapshotIndexInput {
145-
146- private final long offset ;
147- private final long length ;
148-
149- Slice (String sliceDescription , long offset , long length , SearchableSnapshotIndexInput base ) {
150- super (base .toString () + " [slice=" + sliceDescription + "]" , base .reader , base .fileInfo , base .position );
151- this .offset = offset ;
152- this .length = length ;
153- }
154-
155- @ Override
156- public long length () {
157- return length ;
158- }
159-
160- @ Override
161- protected void seekInternal (long pos ) throws IOException {
162- super .seekInternal (offset + pos );
163- }
164-
165- @ Override
166- public BufferedIndexInput clone () {
167- return new Slice ("clone(" + this + ")" , offset , length , this );
168- }
169-
170- @ Override
171- public IndexInput slice (String sliceDescription , long offset , long length ) throws IOException {
172- if ((offset >= 0L ) && (length >= 0L ) && (offset + length <= length ())) {
173- final Slice slice = new Slice (sliceDescription , offset + this .offset , length , this );
174- slice .seek (0L );
175- return slice ;
176- } else {
177- throw new IllegalArgumentException ("slice() " + sliceDescription + " out of bounds: offset=" + offset
178- + ",length=" + length + ",fileLength=" + length () + ": " + this );
179- }
180- }
139+ return "SearchableSnapshotIndexInput{" +
140+ "resourceDesc=" + super .toString () +
141+ ", fileInfo=" + fileInfo +
142+ ", offset=" + offset +
143+ ", length=" + length +
144+ ", position=" + position +
145+ '}' ;
181146 }
182147}
0 commit comments