Apache Solr open-source search software
LUCENE-6084: add reasonable IndexOutput.toString
Michael McCandlesse989394fb202
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex e266c963d8c..255ee918f94 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -229,6 +229,10 @@ API Changes* LUCENE-6082: Remove abort() from codec apis. (Robert Muir)+* LUCENE-6084: IndexOutput's constructor now requires a String+ resourceDescription so its toString is sane (Robert Muir, Mike+ McCandless)+Bug Fixes* LUCENE-5650: Enforce read-only access to any path outside the temporarydiff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50PostingsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50PostingsWriter.javaindex 25393fe7783..239951df0b8 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50PostingsWriter.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene50/Lucene50PostingsWriter.java@@ -221,7 +221,7 @@ public final class Lucene50PostingsWriter extends PushPostingsWriterBase {final int docDelta = docID - lastDocID;if (docID < 0 || (docCount > 0 && docDelta <= 0)) {- throw new CorruptIndexException("docs out of order (" + docID + " <= " + lastDocID + " )", docOut.toString());+ throw new CorruptIndexException("docs out of order (" + docID + " <= " + lastDocID + " )", docOut);}docDeltaBuffer[docBufferUpto] = docDelta;diff --git a/lucene/core/src/java/org/apache/lucene/index/CorruptIndexException.java b/lucene/core/src/java/org/apache/lucene/index/CorruptIndexException.javaindex 7995634417b..5553a089a9c 100644--- a/lucene/core/src/java/org/apache/lucene/index/CorruptIndexException.java+++ b/lucene/core/src/java/org/apache/lucene/index/CorruptIndexException.java@@ -21,6 +21,7 @@ import java.io.IOException;import java.util.Objects;import org.apache.lucene.store.DataInput;+import org.apache.lucene.store.DataOutput;/*** This exception is thrown when Lucene detects@@ -31,11 +32,21 @@ public class CorruptIndexException extends IOException {public CorruptIndexException(String message, DataInput input) {this(message, input, null);}++ /** Create exception with a message only */+ public CorruptIndexException(String message, DataOutput output) {+ this(message, output, null);+ }/** Create exception with message and root cause. */public CorruptIndexException(String message, DataInput input, Throwable cause) {this(message, Objects.toString(input), cause);}++ /** Create exception with message and root cause. */+ public CorruptIndexException(String message, DataOutput output, Throwable cause) {+ this(message, Objects.toString(output), cause);+ }/** Create exception with a message only */public CorruptIndexException(String message, String resourceDescription) {diff --git a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.javaindex cbb0401d8ed..54fafa19508 100644--- a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java@@ -282,7 +282,7 @@ public abstract class FSDirectory extends BaseDirectory {private final String name;public FSIndexOutput(String name) throws IOException {- super(new FilterOutputStream(Files.newOutputStream(directory.resolve(name))) {+ super("FSIndexOutput(path=\"" + directory.resolve(name) + "\")", new FilterOutputStream(Files.newOutputStream(directory.resolve(name))) {// This implementation ensures, that we never write more than CHUNK_SIZE bytes:@Overridepublic void write(byte[] b, int offset, int length) throws IOException {diff --git a/lucene/core/src/java/org/apache/lucene/store/IndexOutput.java b/lucene/core/src/java/org/apache/lucene/store/IndexOutput.javaindex 618d1356075..3a062d18d24 100644--- a/lucene/core/src/java/org/apache/lucene/store/IndexOutput.java+++ b/lucene/core/src/java/org/apache/lucene/store/IndexOutput.java@@ -31,6 +31,17 @@ import java.io.IOException;*/public abstract class IndexOutput extends DataOutput implements Closeable {+ private final String resourceDescription;++ /** Sole constructor. resourceDescription should be non-null, opaque string+ * describing this resource; it's returned from {@link #toString}. */+ protected IndexOutput(String resourceDescription) {+ if (resourceDescription == null) {+ throw new IllegalArgumentException("resourceDescription must not be null");+ }+ this.resourceDescription = resourceDescription;+ }+/** Closes this stream to further operations. */@Overridepublic abstract void close() throws IOException;@@ -42,4 +53,9 @@ public abstract class IndexOutput extends DataOutput implements Closeable {/** Returns the current checksum of bytes written so far */public abstract long getChecksum() throws IOException;++ @Override+ public String toString() {+ return resourceDescription;+ }}diff --git a/lucene/core/src/java/org/apache/lucene/store/OutputStreamIndexOutput.java b/lucene/core/src/java/org/apache/lucene/store/OutputStreamIndexOutput.javaindex 6fdeb88efdc..0edadbe3469 100644--- a/lucene/core/src/java/org/apache/lucene/store/OutputStreamIndexOutput.java+++ b/lucene/core/src/java/org/apache/lucene/store/OutputStreamIndexOutput.java@@ -36,7 +36,8 @@ public class OutputStreamIndexOutput extends IndexOutput {* @param bufferSize the buffer size in bytes used to buffer writes internally.* @throws IllegalArgumentException if the given buffer size is less or equal to <tt>0</tt>*/- public OutputStreamIndexOutput(OutputStream out, int bufferSize) {+ public OutputStreamIndexOutput(String resourceDescription, OutputStream out, int bufferSize) {+ super(resourceDescription);this.os = new BufferedOutputStream(new CheckedOutputStream(out, crc), bufferSize);}diff --git a/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.java b/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.javaindex 7b4dd1ab414..acc10e2edfd 100644--- a/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.java@@ -173,7 +173,7 @@ public class RAMDirectory extends BaseDirectory implements Accountable {existing.directory = null;}fileMap.put(name, file);- return new RAMOutputStream(file, true);+ return new RAMOutputStream(name, file, true);}/**diff --git a/lucene/core/src/java/org/apache/lucene/store/RAMOutputStream.java b/lucene/core/src/java/org/apache/lucene/store/RAMOutputStream.javaindex c1b02f99072..5c7021cec7f 100644--- a/lucene/core/src/java/org/apache/lucene/store/RAMOutputStream.java+++ b/lucene/core/src/java/org/apache/lucene/store/RAMOutputStream.java@@ -46,10 +46,17 @@ public class RAMOutputStream extends IndexOutput implements Accountable {/** Construct an empty output buffer. */public RAMOutputStream() {- this(new RAMFile(), false);+ this("noname", new RAMFile(), false);}+ /** Creates this, with no name. */public RAMOutputStream(RAMFile f, boolean checksum) {+ this("noname", f, checksum);+ }++ /** Creates this, with specified name. */+ public RAMOutputStream(String name, RAMFile f, boolean checksum) {+ super("RAMOutputStream(name=\"" + name + "\")");file = f;// make sure that we switch to thediff --git a/lucene/core/src/java/org/apache/lucene/store/RateLimitedIndexOutput.java b/lucene/core/src/java/org/apache/lucene/store/RateLimitedIndexOutput.javaindex 92dded8f24b..f8535f22cf6 100644--- a/lucene/core/src/java/org/apache/lucene/store/RateLimitedIndexOutput.java+++ b/lucene/core/src/java/org/apache/lucene/store/RateLimitedIndexOutput.java@@ -37,6 +37,7 @@ final class RateLimitedIndexOutput extends IndexOutput {private long currentMinPauseCheckBytes;RateLimitedIndexOutput(final RateLimiter rateLimiter, final IndexOutput delegate) {+ super("RateLimitedIndexOutput(" + delegate + ")");this.delegate = delegate;this.rateLimiter = rateLimiter;this.currentMinPauseCheckBytes = rateLimiter.getMinPauseCheckBytes();diff --git a/lucene/facet/src/test/org/apache/lucene/facet/SlowRAMDirectory.java b/lucene/facet/src/test/org/apache/lucene/facet/SlowRAMDirectory.javaindex 93687641f1d..ee79262ee79 100644--- a/lucene/facet/src/test/org/apache/lucene/facet/SlowRAMDirectory.java+++ b/lucene/facet/src/test/org/apache/lucene/facet/SlowRAMDirectory.java@@ -143,6 +143,7 @@ public class SlowRAMDirectory extends RAMDirectory {private final Random rand;public SlowIndexOutput(IndexOutput io) {+ super("SlowIndexOutput(" + io + ")");this.io = io;this.rand = forkRandom();}diff --git a/lucene/misc/src/java/org/apache/lucene/store/NativeUnixDirectory.java b/lucene/misc/src/java/org/apache/lucene/store/NativeUnixDirectory.javaindex 668504724e3..32898c333a4 100644--- a/lucene/misc/src/java/org/apache/lucene/store/NativeUnixDirectory.java+++ b/lucene/misc/src/java/org/apache/lucene/store/NativeUnixDirectory.java@@ -166,6 +166,7 @@ public class NativeUnixDirectory extends FSDirectory {private boolean isOpen;public NativeUnixIndexOutput(Path path, int bufferSize) throws IOException {+ super("NativeUnixIndexOutput(path=\"" + path.toString() + "\")");//this.path = path;final FileDescriptor fd = NativePosixUtil.open_direct(path.toString(), false);fos = new FileOutputStream(fd);diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.javaindex 8d896f5bb14..76366e25740 100644--- a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java@@ -1031,5 +1031,14 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {}dir.close();}++ // LUCENE-6084+ public void testIndexOutputToString() throws Throwable {+ Directory dir = getDirectory(createTempDir());+ IndexOutput out = dir.createOutput("camelCase.txt", newIOContext(random()));+ assertTrue(out.toString(), out.toString().contains("camelCase.txt"));+ out.close();+ dir.close();+ }}diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.javaindex 8741c5ea9e2..38e2d4613f8 100644--- a/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java+++ b/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java@@ -38,6 +38,7 @@ public class MockIndexOutputWrapper extends IndexOutput {/** Construct an empty output buffer. */public MockIndexOutputWrapper(MockDirectoryWrapper dir, IndexOutput delegate, String name) {+ super("MockIndexOutputWrapper(" + delegate + ")");this.dir = dir;this.name = name;this.delegate = delegate;diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/ThrottledIndexOutput.java b/lucene/test-framework/src/java/org/apache/lucene/util/ThrottledIndexOutput.javaindex c7a9cbef883..52232061942 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/ThrottledIndexOutput.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/ThrottledIndexOutput.java@@ -59,6 +59,7 @@ public class ThrottledIndexOutput extends IndexOutput {public ThrottledIndexOutput(int bytesPerSecond, long flushDelayMillis,long closeDelayMillis, long seekDelayMillis, long minBytesWritten,IndexOutput delegate) {+ super("ThrottledIndexOutput(" + delegate + ")");assert bytesPerSecond > 0;this.delegate = delegate;this.bytesPerSecond = bytesPerSecond;diff --git a/solr/core/src/java/org/apache/solr/store/blockcache/CachedIndexOutput.java b/solr/core/src/java/org/apache/solr/store/blockcache/CachedIndexOutput.javaindex 2de06f46ee2..d1e0bdcd4f1 100644--- a/solr/core/src/java/org/apache/solr/store/blockcache/CachedIndexOutput.java+++ b/solr/core/src/java/org/apache/solr/store/blockcache/CachedIndexOutput.java@@ -37,7 +37,7 @@ public class CachedIndexOutput extends ReusedBufferedIndexOutput {public CachedIndexOutput(BlockDirectory directory, IndexOutput dest,int blockSize, String name, Cache cache, int bufferSize) {- super(bufferSize);+ super("dest=" + dest + " name=" + name, bufferSize);this.directory = directory;this.dest = dest;this.blockSize = blockSize;diff --git a/solr/core/src/java/org/apache/solr/store/blockcache/ReusedBufferedIndexOutput.java b/solr/core/src/java/org/apache/solr/store/blockcache/ReusedBufferedIndexOutput.javaindex 7f42b62601f..f9e1c423b7a 100644--- a/solr/core/src/java/org/apache/solr/store/blockcache/ReusedBufferedIndexOutput.java+++ b/solr/core/src/java/org/apache/solr/store/blockcache/ReusedBufferedIndexOutput.java@@ -43,11 +43,12 @@ public abstract class ReusedBufferedIndexOutput extends IndexOutput {private final Store store;- public ReusedBufferedIndexOutput() {- this(BUFFER_SIZE);+ public ReusedBufferedIndexOutput(String resourceDescription) {+ this(resourceDescription, BUFFER_SIZE);}- public ReusedBufferedIndexOutput(int bufferSize) {+ public ReusedBufferedIndexOutput(String resourceDescription, int bufferSize) {+ super(resourceDescription);checkBufferSize(bufferSize);this.bufferSize = bufferSize;store = BufferStore.instance(bufferSize);diff --git a/solr/core/src/java/org/apache/solr/store/hdfs/HdfsFileWriter.java b/solr/core/src/java/org/apache/solr/store/hdfs/HdfsFileWriter.javaindex 35f3dd7f4aa..2a467dd054a 100644--- a/solr/core/src/java/org/apache/solr/store/hdfs/HdfsFileWriter.java+++ b/solr/core/src/java/org/apache/solr/store/hdfs/HdfsFileWriter.java@@ -38,7 +38,7 @@ public class HdfsFileWriter extends OutputStreamIndexOutput {public static final int BUFFER_SIZE = 16384;public HdfsFileWriter(FileSystem fileSystem, Path path) throws IOException {- super(getOutputStream(fileSystem, path), BUFFER_SIZE);+ super("fileSystem=" + fileSystem + " path=" + path, getOutputStream(fileSystem, path), BUFFER_SIZE);}private static final OutputStream getOutputStream(FileSystem fileSystem, Path path) throws IOException {