22
22
import org .apache .lucene .index .CorruptIndexException ;
23
23
import org .apache .lucene .index .IndexFormatTooNewException ;
24
24
import org .apache .lucene .index .IndexFormatTooOldException ;
25
+ import org .apache .lucene .store .ByteBuffersDataInput ;
26
+ import org .apache .lucene .store .ByteBuffersIndexInput ;
27
+ import org .apache .lucene .store .IndexInput ;
25
28
import org .apache .lucene .store .OutputStreamIndexOutput ;
29
+ import org .apache .lucene .util .BytesRef ;
26
30
import org .elasticsearch .cluster .metadata .Metadata ;
27
31
import org .elasticsearch .common .CheckedConsumer ;
28
32
import org .elasticsearch .common .CheckedFunction ;
29
33
import org .elasticsearch .common .blobstore .BlobContainer ;
30
- import org .elasticsearch .common .bytes .BytesArray ;
31
34
import org .elasticsearch .common .bytes .BytesReference ;
32
35
import org .elasticsearch .common .compress .CompressorFactory ;
33
36
import org .elasticsearch .common .io .Streams ;
46
49
import org .elasticsearch .gateway .CorruptStateException ;
47
50
import org .elasticsearch .snapshots .SnapshotInfo ;
48
51
49
- import java .io .ByteArrayOutputStream ;
50
52
import java .io .IOException ;
51
53
import java .io .InputStream ;
52
54
import java .io .OutputStream ;
55
+ import java .util .Arrays ;
53
56
import java .util .HashMap ;
54
57
import java .util .Locale ;
55
58
import java .util .Map ;
@@ -127,8 +130,10 @@ public String blobName(String name) {
127
130
public T readBlob (BlobContainer blobContainer , String blobName ) throws IOException {
128
131
final BytesReference bytes = Streams .readFully (blobContainer .readBlob (blobName ));
129
132
final String resourceDesc = "ChecksumBlobStoreFormat.readBlob(blob=\" " + blobName + "\" )" ;
130
- try (ByteArrayIndexInput indexInput =
131
- new ByteArrayIndexInput (resourceDesc , BytesReference .toBytes (bytes ))) {
133
+ try {
134
+ final IndexInput indexInput = bytes .length () > 0 ? new ByteBuffersIndexInput (
135
+ new ByteBuffersDataInput (Arrays .asList (BytesReference .toByteBuffers (bytes ))), resourceDesc )
136
+ : new ByteArrayIndexInput (resourceDesc , BytesRef .EMPTY_BYTES );
132
137
CodecUtil .checksumEntireFile (indexInput );
133
138
CodecUtil .checkHeader (indexInput , codec , VERSION , VERSION );
134
139
long filePointer = indexInput .getFilePointer ();
@@ -182,19 +187,9 @@ public void write(T obj, BlobContainer blobContainer, String name, boolean failI
182
187
});
183
188
}
184
189
185
- private void writeTo (final T obj , final String blobName , final CheckedConsumer <BytesArray , IOException > consumer ) throws IOException {
186
- final BytesReference bytes ;
187
- try (BytesStreamOutput bytesStreamOutput = new BytesStreamOutput ()) {
188
- if (compress ) {
189
- try (StreamOutput compressedStreamOutput = CompressorFactory .COMPRESSOR .streamOutput (bytesStreamOutput )) {
190
- write (obj , compressedStreamOutput );
191
- }
192
- } else {
193
- write (obj , bytesStreamOutput );
194
- }
195
- bytes = bytesStreamOutput .bytes ();
196
- }
197
- try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
190
+ private void writeTo (final T obj , final String blobName ,
191
+ final CheckedConsumer <BytesReference , IOException > consumer ) throws IOException {
192
+ try (BytesStreamOutput outputStream = new BytesStreamOutput ()) {
198
193
final String resourceDesc = "ChecksumBlobStoreFormat.writeBlob(blob=\" " + blobName + "\" )" ;
199
194
try (OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput (resourceDesc , blobName , outputStream , BUFFER_SIZE )) {
200
195
CodecUtil .writeHeader (indexOutput , codec , VERSION );
@@ -205,15 +200,21 @@ public void close() throws IOException {
205
200
// in order to write the footer we need to prevent closing the actual index input.
206
201
}
207
202
}) {
208
- bytes .writeTo (indexOutputOutputStream );
203
+ if (compress ) {
204
+ try (StreamOutput compressedStreamOutput = CompressorFactory .COMPRESSOR .streamOutput (indexOutputOutputStream )) {
205
+ write (obj , compressedStreamOutput );
206
+ }
207
+ } else {
208
+ write (obj , indexOutputOutputStream );
209
+ }
209
210
}
210
211
CodecUtil .writeFooter (indexOutput );
211
212
}
212
- consumer .accept (new BytesArray ( outputStream .toByteArray () ));
213
+ consumer .accept (outputStream .bytes ( ));
213
214
}
214
215
}
215
216
216
- private void write (T obj , StreamOutput streamOutput ) throws IOException {
217
+ private void write (T obj , OutputStream streamOutput ) throws IOException {
217
218
try (XContentBuilder builder = XContentFactory .contentBuilder (XContentType .SMILE , streamOutput )) {
218
219
builder .startObject ();
219
220
obj .toXContent (builder , SNAPSHOT_ONLY_FORMAT_PARAMS );
0 commit comments