Skip to content

Commit f82f104

Browse files
authored
Extract files stored within metadata hash into ByteArrayIndexInputs (#54072)
We can now detect blobs that are stored within the metadata hash field and open them directly as ByteArrayIndexInput. Relates #53860
1 parent e20f16e commit f82f104

File tree

3 files changed

+11
-35
lines changed

3 files changed

+11
-35
lines changed

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/BaseSearchableSnapshotIndexInput.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77

88
import org.apache.lucene.store.BufferedIndexInput;
99
import org.apache.lucene.store.IOContext;
10-
import org.apache.lucene.util.BytesRef;
1110
import org.elasticsearch.common.blobstore.BlobContainer;
1211
import org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot.FileInfo;
1312
import org.elasticsearch.index.snapshots.blobstore.SlicedInputStream;
1413

15-
import java.io.ByteArrayInputStream;
1614
import java.io.IOException;
1715
import java.io.InputStream;
1816
import java.util.Objects;
@@ -28,6 +26,8 @@ public BaseSearchableSnapshotIndexInput(String resourceDesc, BlobContainer blobC
2826
this.blobContainer = Objects.requireNonNull(blobContainer);
2927
this.fileInfo = Objects.requireNonNull(fileInfo);
3028
this.context = Objects.requireNonNull(context);
29+
assert fileInfo.metadata().hashEqualsContents() == false
30+
: "this method should only be used with blobs that are NOT stored in metadata's hash field (fileInfo: " + fileInfo + ')';
3131
}
3232

3333
public BaseSearchableSnapshotIndexInput(
@@ -42,18 +42,6 @@ public BaseSearchableSnapshotIndexInput(
4242
}
4343

4444
protected InputStream openInputStream(final long position, final long length) throws IOException {
45-
// TODO move this at the Directory level
46-
if (fileInfo.metadata().hashEqualsContents()) {
47-
// extract blob content from metadata hash
48-
final BytesRef data = fileInfo.metadata().hash();
49-
if ((position < 0L) || (length < 0L) || (position + length > data.bytes.length)) {
50-
throw new IllegalArgumentException(
51-
"Invalid arguments (pos=" + position + ", length=" + length + ") for hash content (length=" + data.bytes.length + ')'
52-
);
53-
}
54-
return new ByteArrayInputStream(data.bytes, Math.toIntExact(position), Math.toIntExact(length));
55-
}
56-
5745
final long startPart = getPartNumberForPosition(position);
5846
final long endPart = getPartNumberForPosition(position + length);
5947
if ((startPart == endPart) || fileInfo.numberOfParts() == 1L) {

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/SearchableSnapshotDirectory.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
import org.apache.lucene.store.IndexInput;
1414
import org.apache.lucene.store.IndexOutput;
1515
import org.apache.lucene.store.SingleInstanceLockFactory;
16+
import org.apache.lucene.util.BytesRef;
1617
import org.elasticsearch.common.Nullable;
1718
import org.elasticsearch.common.blobstore.BlobContainer;
19+
import org.elasticsearch.common.lucene.store.ByteArrayIndexInput;
1820
import org.elasticsearch.common.settings.Settings;
1921
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
2022
import org.elasticsearch.index.IndexSettings;
@@ -231,7 +233,13 @@ public CacheFile getCacheFile(CacheKey cacheKey, long fileLength) throws Excepti
231233
@Override
232234
public IndexInput openInput(final String name, final IOContext context) throws IOException {
233235
ensureOpen();
236+
234237
final BlobStoreIndexShardSnapshot.FileInfo fileInfo = fileInfo(name);
238+
if (fileInfo.metadata().hashEqualsContents()) {
239+
final BytesRef content = fileInfo.metadata().hash();
240+
return new ByteArrayIndexInput("ByteArrayIndexInput(" + name + ')', content.bytes, content.offset, content.length);
241+
}
242+
235243
final IndexInputStats inputStats = stats.computeIfAbsent(name, n -> createIndexInputStats(fileInfo.length()));
236244
if (useCache && isExcludedFromCache(name) == false) {
237245
return new CacheBufferedIndexInput(this, fileInfo, context, inputStats);

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/index/store/SearchableSnapshotIndexInput.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
import org.apache.lucene.store.BufferedIndexInput;
99
import org.apache.lucene.store.IOContext;
1010
import org.apache.lucene.store.IndexInput;
11-
import org.apache.lucene.util.BytesRef;
1211
import org.elasticsearch.common.CheckedRunnable;
1312
import org.elasticsearch.common.Nullable;
1413
import org.elasticsearch.common.blobstore.BlobContainer;
1514
import org.elasticsearch.core.internal.io.IOUtils;
1615
import org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot.FileInfo;
1716

18-
import java.io.ByteArrayInputStream;
1917
import java.io.Closeable;
2018
import java.io.EOFException;
2119
import java.io.IOException;
@@ -259,25 +257,7 @@ public String toString() {
259257
}
260258

261259
private InputStream openBlobStream(int part, long pos, long length) throws IOException {
262-
final InputStream stream;
263-
if (fileInfo.metadata().hashEqualsContents() == false) {
264-
stream = blobContainer.readBlob(fileInfo.partName(part), pos, length);
265-
} else {
266-
// extract blob content from metadata hash
267-
final BytesRef data = fileInfo.metadata().hash();
268-
if (part > 0) {
269-
assert fileInfo.numberOfParts() >= part;
270-
for (int i = 0; i < part; i++) {
271-
pos += fileInfo.partBytes(i);
272-
}
273-
}
274-
if ((pos < 0L) || (length < 0L) || (pos + length > data.bytes.length)) {
275-
throw new IllegalArgumentException("Invalid arguments (pos=" + pos + ", length=" + length
276-
+ ") for hash content (length=" + data.bytes.length + ')');
277-
}
278-
stream = new ByteArrayInputStream(data.bytes, Math.toIntExact(pos), Math.toIntExact(length));
279-
}
280-
return stream;
260+
return blobContainer.readBlob(fileInfo.partName(part), pos, length);
281261
}
282262

283263
/**

0 commit comments

Comments
 (0)