Skip to content

Apache Lucene open-source search software

LUCENE-6311: make sure IndexInput.toString confesses when it's within a compound file

Michael McCandlessf4011cfc51eb
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index e1e8df17b6b..9d600dbfd03 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -70,7 +70,11 @@ Bug Fixes
* LUCENE-6001: DrillSideways hits NullPointerException for certain
BooleanQuery searches. (Dragan Jotannovic, jane chang via Mike
McCandless)
-
+
+* LUCENE-6311: Fix NIOFSDirectory and SimpleFSDirectory so that the
+ toString method of IndexInputs confess when they are from a compound
+ file. (Robert Muir, Mike McCandless)
+
Optimizations
* LUCENE-6183, LUCENE-5647: Avoid recompressing stored fields
diff --git a/lucene/core/src/java/org/apache/lucene/store/ByteBufferIndexInput.java b/lucene/core/src/java/org/apache/lucene/store/ByteBufferIndexInput.java
index 709f9bf0259..dac15932010 100644
--- a/lucene/core/src/java/org/apache/lucene/store/ByteBufferIndexInput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/ByteBufferIndexInput.java
@@ -281,10 +281,9 @@ abstract class ByteBufferIndexInput extends IndexInput implements RandomAccessIn
}
final ByteBuffer newBuffers[] = buildSlice(buffers, offset, length);
- final String newResourceDescription = (sliceDescription == null) ? toString() : (toString() + " [slice=" + sliceDescription + "]");
final int ofs = (int) (offset & chunkSizeMask);
- final ByteBufferIndexInput clone = newCloneInstance(newResourceDescription, newBuffers, ofs, length);
+ final ByteBufferIndexInput clone = newCloneInstance(getFullSliceDescription(sliceDescription), newBuffers, ofs, length);
clone.isClone = true;
// register the new clone in our clone list to clean it up on closing:
diff --git a/lucene/core/src/java/org/apache/lucene/store/IndexInput.java b/lucene/core/src/java/org/apache/lucene/store/IndexInput.java
index 36e6b28eaab..013bbc4cff1 100644
--- a/lucene/core/src/java/org/apache/lucene/store/IndexInput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/IndexInput.java
@@ -88,7 +88,17 @@ public abstract class IndexInput extends DataInput implements Cloneable,Closeabl
* The slice is seeked to the beginning.
*/
public abstract IndexInput slice(String sliceDescription, long offset, long length) throws IOException;
-
+
+ /** Subclasses call this to get the String for resourceDescription of a slice of this {@code IndexInput}. */
+ protected String getFullSliceDescription(String sliceDescription) {
+ if (sliceDescription == null) {
+ // Clones pass null sliceDescription:
+ return toString();
+ } else {
+ return toString() + " [slice=" + sliceDescription + "]";
+ }
+ }
+
/**
* Creates a random-access slice of this index input, with the given offset and length.
* <p>
diff --git a/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
index 95f4477368a..84f1a7ff899 100644
--- a/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
@@ -137,7 +137,7 @@ public class NIOFSDirectory extends FSDirectory {
if (offset < 0 || length < 0 || offset + length > this.length()) {
throw new IllegalArgumentException("slice() " + sliceDescription + " out of bounds: " + this);
}
- return new NIOFSIndexInput(sliceDescription, channel, off + offset, length, getBufferSize());
+ return new NIOFSIndexInput(getFullSliceDescription(sliceDescription), channel, off + offset, length, getBufferSize());
}
@Override
diff --git a/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java b/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java
index c26224790ed..2aee222e915 100644
--- a/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java
+++ b/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java
@@ -128,8 +128,7 @@ public class RAMInputStream extends IndexInput implements Cloneable {
if (offset < 0 || length < 0 || offset + length > this.length) {
throw new IllegalArgumentException("slice() " + sliceDescription + " out of bounds: " + this);
}
- final String newResourceDescription = (sliceDescription == null) ? toString() : (toString() + " [slice=" + sliceDescription + "]");
- return new RAMInputStream(newResourceDescription, file, offset + length) {
+ return new RAMInputStream(getFullSliceDescription(sliceDescription), file, offset + length) {
{
seek(0L);
}
diff --git a/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java
index 69e3d31d823..f30b383c65b 100644
--- a/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/SimpleFSDirectory.java
@@ -131,7 +131,7 @@ public class SimpleFSDirectory extends FSDirectory {
if (offset < 0 || length < 0 || offset + length > this.length()) {
throw new IllegalArgumentException("slice() " + sliceDescription + " out of bounds: " + this);
}
- return new SimpleFSIndexInput(sliceDescription, channel, off + offset, length, getBufferSize());
+ return new SimpleFSIndexInput(getFullSliceDescription(sliceDescription), channel, off + offset, length, getBufferSize());
}
@Override
diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java
index 4331eab278b..6b437bccc9b 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java
@@ -765,4 +765,21 @@ public abstract class BaseCompoundFormatTestCase extends BaseIndexFileFormatTest
public void testMergeStability() throws Exception {
assumeTrue("test does not work with CFS", true);
}
+
+ // LUCENE-6311: make sure the resource name inside a compound file confesses that it's inside a compound file
+ public void testResourceNameInsideCompoundFile() throws Exception {
+ Directory dir = newDirectory();
+ String subFile = "_123.xyz";
+ createSequenceFile(dir, subFile, (byte) 0, 10);
+
+ SegmentInfo si = newSegmentInfo(dir, "_123");
+ si.setFiles(Collections.singletonList(subFile));
+ si.getCodec().compoundFormat().write(dir, si, IOContext.DEFAULT);
+ Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si, IOContext.DEFAULT);
+ IndexInput in = cfs.openInput(subFile, IOContext.DEFAULT);
+ String desc = in.toString();
+ assertTrue("resource description hides that it's inside a compound file: " + desc, desc.contains("[slice=" + subFile + "]"));
+ cfs.close();
+ dir.close();
+ }
}