LUCENE-6084: add reasonable IndexOutput.toString

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1642761 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2014-12-01 21:21:38 +00:00
parent 9806b86719
commit e989394fb2
17 changed files with 64 additions and 10 deletions

View File

@ -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 temporary

View File

@ -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;

View File

@ -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) {

View File

@ -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:
@Override
public void write(byte[] b, int offset, int length) throws IOException {

View File

@ -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. */
@Override
public 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;
}
}

View File

@ -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);
}

View File

@ -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);
}
/**

View File

@ -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 the

View File

@ -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();

View File

@ -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();
}

View File

@ -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);

View File

@ -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();
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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 {