Skip to content

Apache Lucene open-source search software

GITHUB-11868: Add FilterIndexInput and FilterIndexOutput wrapper classes (#11958)

Marc D'Mellof9cb6a3b42f3
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 046130e69b2..1094842a970 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -124,7 +124,9 @@ Bug Fixes
Other
---------------------
-(No changes)
+
+* GITHUB#11868: Add a FilterIndexInput and FilterIndexOutput class to more easily and safely create delegate
+ IndexInput and IndexOutput classes (Marc D'Mello)
======================== Lucene 9.5.0 =======================
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/store/EndiannessReverserIndexInput.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/store/EndiannessReverserIndexInput.java
index 4a1c0566ffc..b0a2b3ad57e 100644
--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/store/EndiannessReverserIndexInput.java
+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/store/EndiannessReverserIndexInput.java
@@ -17,34 +17,15 @@
package org.apache.lucene.backward_codecs.store;
import java.io.IOException;
-import java.util.Map;
-import java.util.Set;
+import org.apache.lucene.store.FilterIndexInput;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.RandomAccessInput;
/** A {@link IndexInput} wrapper that changes the endianness of the provided index input. */
-final class EndiannessReverserIndexInput extends IndexInput {
-
- final IndexInput in;
+final class EndiannessReverserIndexInput extends FilterIndexInput {
EndiannessReverserIndexInput(IndexInput in) {
- super("Endianness reverser Index Input wrapper");
- this.in = in;
- }
-
- @Override
- public byte readByte() throws IOException {
- return in.readByte();
- }
-
- @Override
- public void readBytes(byte[] b, int offset, int len) throws IOException {
- in.readBytes(b, offset, len);
- }
-
- @Override
- public void readBytes(byte[] b, int offset, int len, boolean useBuffer) throws IOException {
- in.readBytes(b, offset, len, useBuffer);
+ super("Endianness reverser Index Input wrapper", in);
}
@Override
@@ -57,16 +38,6 @@ final class EndiannessReverserIndexInput extends IndexInput {
return Integer.reverseBytes(in.readInt());
}
- @Override
- public int readVInt() throws IOException {
- return in.readVInt();
- }
-
- @Override
- public int readZInt() throws IOException {
- return in.readZInt();
- }
-
@Override
public long readLong() throws IOException {
return Long.reverseBytes(in.readLong());
@@ -97,21 +68,6 @@ final class EndiannessReverserIndexInput extends IndexInput {
}
}
- @Override
- public long readVLong() throws IOException {
- return in.readVLong();
- }
-
- @Override
- public long readZLong() throws IOException {
- return in.readZLong();
- }
-
- @Override
- public String readString() throws IOException {
- return in.readString();
- }
-
@Override
public IndexInput clone() {
return new EndiannessReverserIndexInput(in.clone());
@@ -122,46 +78,6 @@ final class EndiannessReverserIndexInput extends IndexInput {
return new EndiannessReverserIndexInput(in.slice(sliceDescription, offset, length));
}
- @Override
- public Map<String, String> readMapOfStrings() throws IOException {
- return in.readMapOfStrings();
- }
-
- @Override
- public Set<String> readSetOfStrings() throws IOException {
- return in.readSetOfStrings();
- }
-
- @Override
- public void skipBytes(final long numBytes) throws IOException {
- in.skipBytes(numBytes);
- }
-
- @Override
- public void close() throws IOException {
- in.close();
- }
-
- @Override
- public long getFilePointer() {
- return in.getFilePointer();
- }
-
- @Override
- public void seek(long pos) throws IOException {
- in.seek(pos);
- }
-
- @Override
- public long length() {
- return in.length();
- }
-
- @Override
- public String toString() {
- return in.toString();
- }
-
@Override
public RandomAccessInput randomAccessSlice(long offset, long length) throws IOException {
return new EndiannessReverserRandomAccessInput(in.randomAccessSlice(offset, length));
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/store/EndiannessReverserIndexOutput.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/store/EndiannessReverserIndexOutput.java
index 7da9909da70..f1d94808bc0 100644
--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/store/EndiannessReverserIndexOutput.java
+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/store/EndiannessReverserIndexOutput.java
@@ -17,40 +17,14 @@
package org.apache.lucene.backward_codecs.store;
import java.io.IOException;
-import java.util.Map;
-import java.util.Set;
-import org.apache.lucene.store.DataInput;
+import org.apache.lucene.store.FilterIndexOutput;
import org.apache.lucene.store.IndexOutput;
/** A {@link IndexOutput} wrapper that changes the endianness of the provided index output. */
-final class EndiannessReverserIndexOutput extends IndexOutput {
-
- final IndexOutput out;
+final class EndiannessReverserIndexOutput extends FilterIndexOutput {
EndiannessReverserIndexOutput(IndexOutput out) {
- super("Endianness reverser Index Output wrapper", out.getName());
- this.out = out;
- }
-
- @Override
- public String getName() {
- return out.getName();
- }
-
- /** Closes this stream to further operations. */
- @Override
- public void close() throws IOException {
- out.close();
- }
-
- @Override
- public long getFilePointer() {
- return out.getFilePointer();
- }
-
- @Override
- public long getChecksum() throws IOException {
- return out.getChecksum();
+ super("Endianness reverser Index Output wrapper", out.getName(), out);
}
@Override
@@ -58,21 +32,6 @@ final class EndiannessReverserIndexOutput extends IndexOutput {
return out.getName();
}
- @Override
- public void writeByte(byte b) throws IOException {
- out.writeByte(b);
- }
-
- @Override
- public void writeBytes(byte[] b, int length) throws IOException {
- writeBytes(b, 0, length);
- }
-
- @Override
- public void writeBytes(byte[] b, int offset, int length) throws IOException {
- out.writeBytes(b, offset, length);
- }
-
@Override
public void writeInt(int i) throws IOException {
out.writeInt(Integer.reverseBytes(i));
@@ -87,24 +46,4 @@ final class EndiannessReverserIndexOutput extends IndexOutput {
public void writeLong(long i) throws IOException {
out.writeLong(Long.reverseBytes(i));
}
-
- @Override
- public void writeString(String s) throws IOException {
- out.writeString(s);
- }
-
- @Override
- public void copyBytes(DataInput input, long numBytes) throws IOException {
- out.copyBytes(input, numBytes);
- }
-
- @Override
- public void writeMapOfStrings(Map<String, String> map) throws IOException {
- out.writeMapOfStrings(map);
- }
-
- @Override
- public void writeSetOfStrings(Set<String> set) throws IOException {
- out.writeSetOfStrings(set);
- }
}
diff --git a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/store/EndiannessReverserUtil.java b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/store/EndiannessReverserUtil.java
index b18595d54ca..44fde79229b 100644
--- a/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/store/EndiannessReverserUtil.java
+++ b/lucene/backward-codecs/src/java/org/apache/lucene/backward_codecs/store/EndiannessReverserUtil.java
@@ -66,7 +66,7 @@ public final class EndiannessReverserUtil {
return ((EndiannessReverserDataOutput) dataOutput).out;
}
if (dataOutput instanceof EndiannessReverserIndexOutput) {
- return ((EndiannessReverserIndexOutput) dataOutput).out;
+ return ((EndiannessReverserIndexOutput) dataOutput).getDelegate();
}
return new EndiannessReverserDataOutput(dataOutput);
}
@@ -77,7 +77,7 @@ public final class EndiannessReverserUtil {
return ((EndiannessReverserDataInput) dataInput).in;
}
if (dataInput instanceof EndiannessReverserIndexInput) {
- return ((EndiannessReverserIndexInput) dataInput).in;
+ return ((EndiannessReverserIndexInput) dataInput).getDelegate();
}
return new EndiannessReverserDataInput(dataInput);
}
diff --git a/lucene/core/src/java/org/apache/lucene/store/ByteBuffersIndexOutput.java b/lucene/core/src/java/org/apache/lucene/store/ByteBuffersIndexOutput.java
index 0a7ed683edb..fdbe8e1dbea 100644
--- a/lucene/core/src/java/org/apache/lucene/store/ByteBuffersIndexOutput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/ByteBuffersIndexOutput.java
@@ -159,4 +159,8 @@ public final class ByteBuffersIndexOutput extends IndexOutput {
throw new AlreadyClosedException("Already closed.");
}
}
+
+ public byte[] toArrayCopy() {
+ return delegate.toArrayCopy();
+ }
}
diff --git a/lucene/core/src/java/org/apache/lucene/store/FilterIndexInput.java b/lucene/core/src/java/org/apache/lucene/store/FilterIndexInput.java
new file mode 100644
index 00000000000..5b4a5c506ee
--- /dev/null
+++ b/lucene/core/src/java/org/apache/lucene/store/FilterIndexInput.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.store;
+
+import java.io.IOException;
+
+/**
+ * IndexInput implementation that delegates calls to another directory. This class can be used to
+ * add limitations on top of an existing {@link IndexInput} implementation or to add additional
+ * sanity checks for tests. However, if you plan to write your own {@link IndexInput}
+ * implementation, you should consider extending directly {@link IndexInput} or {@link DataInput}
+ * rather than try to reuse functionality of existing {@link IndexInput}s by extending this class.
+ *
+ * @lucene.internal
+ */
+public class FilterIndexInput extends IndexInput {
+
+ /**
+ * Unwraps all FilterIndexInputs until the first non-FilterIndexInput IndexInput instance and
+ * returns it
+ */
+ public static IndexInput unwrap(IndexInput in) {
+ while (in instanceof FilterIndexInput) {
+ in = ((FilterIndexInput) in).in;
+ }
+ return in;
+ }
+
+ protected final IndexInput in;
+
+ /** Creates a FilterIndexInput with a resource description and wrapped delegate IndexInput */
+ public FilterIndexInput(String resourceDescription, IndexInput in) {
+ super(resourceDescription);
+ this.in = in;
+ }
+
+ /** Gets the delegate that was passed in on creation */
+ public IndexInput getDelegate() {
+ return in;
+ }
+
+ @Override
+ public void close() throws IOException {
+ in.close();
+ }
+
+ @Override
+ public long getFilePointer() {
+ return in.getFilePointer();
+ }
+
+ @Override
+ public void seek(long pos) throws IOException {
+ in.seek(pos);
+ }
+
+ @Override
+ public long length() {
+ return in.length();
+ }
+
+ @Override
+ public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
+ return in.slice(sliceDescription, offset, length);
+ }
+
+ @Override
+ public byte readByte() throws IOException {
+ return in.readByte();
+ }
+
+ @Override
+ public void readBytes(byte[] b, int offset, int len) throws IOException {
+ in.readBytes(b, offset, len);
+ }
+}
diff --git a/lucene/core/src/java/org/apache/lucene/store/FilterIndexOutput.java b/lucene/core/src/java/org/apache/lucene/store/FilterIndexOutput.java
new file mode 100644
index 00000000000..1faec626d08
--- /dev/null
+++ b/lucene/core/src/java/org/apache/lucene/store/FilterIndexOutput.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.store;
+
+import java.io.IOException;
+
+/**
+ * IndexOutput implementation that delegates calls to another directory. This class can be used to
+ * add limitations on top of an existing {@link IndexOutput} implementation such as {@link
+ * ByteBuffersIndexOutput} or to add additional sanity checks for tests. However, if you plan to
+ * write your own {@link IndexOutput} implementation, you should consider extending directly {@link
+ * IndexOutput} or {@link DataOutput} rather than try to reuse functionality of existing {@link
+ * IndexOutput}s by extending this class.
+ *
+ * @lucene.internal
+ */
+public class FilterIndexOutput extends IndexOutput {
+
+ /**
+ * Unwraps all FilterIndexOutputs until the first non-FilterIndexOutput IndexOutput instance and
+ * returns it
+ */
+ public static IndexOutput unwrap(IndexOutput out) {
+ while (out instanceof FilterIndexOutput) {
+ out = ((FilterIndexOutput) out).out;
+ }
+ return out;
+ }
+
+ protected final IndexOutput out;
+
+ /**
+ * Creates a FilterIndexOutput with a resource description, name, and wrapped delegate IndexOutput
+ */
+ protected FilterIndexOutput(String resourceDescription, String name, IndexOutput out) {
+ super(resourceDescription, name);
+ this.out = out;
+ }
+
+ /** Gets the delegate that was passed in on creation */
+ public final IndexOutput getDelegate() {
+ return out;
+ }
+
+ @Override
+ public void close() throws IOException {
+ out.close();
+ }
+
+ @Override
+ public long getFilePointer() {
+ return out.getFilePointer();
+ }
+
+ @Override
+ public long getChecksum() throws IOException {
+ return out.getChecksum();
+ }
+
+ @Override
+ public void writeByte(byte b) throws IOException {
+ out.writeByte(b);
+ }
+
+ @Override
+ public void writeBytes(byte[] b, int offset, int length) throws IOException {
+ out.writeBytes(b, offset, length);
+ }
+}
diff --git a/lucene/core/src/java/org/apache/lucene/store/RateLimitedIndexOutput.java b/lucene/core/src/java/org/apache/lucene/store/RateLimitedIndexOutput.java
index d5d5709bca4..bfac505b8b0 100644
--- a/lucene/core/src/java/org/apache/lucene/store/RateLimitedIndexOutput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/RateLimitedIndexOutput.java
@@ -23,9 +23,8 @@ import java.io.IOException;
*
* @lucene.internal
*/
-public final class RateLimitedIndexOutput extends IndexOutput {
+public final class RateLimitedIndexOutput extends FilterIndexOutput {
- private final IndexOutput delegate;
private final RateLimiter rateLimiter;
/** How many bytes we've written since we last called rateLimiter.pause. */
@@ -37,33 +36,17 @@ public final class RateLimitedIndexOutput extends IndexOutput {
*/
private long currentMinPauseCheckBytes;
- public RateLimitedIndexOutput(final RateLimiter rateLimiter, final IndexOutput delegate) {
- super("RateLimitedIndexOutput(" + delegate + ")", delegate.getName());
- this.delegate = delegate;
+ public RateLimitedIndexOutput(final RateLimiter rateLimiter, final IndexOutput out) {
+ super("RateLimitedIndexOutput(" + out + ")", out.getName(), out);
this.rateLimiter = rateLimiter;
this.currentMinPauseCheckBytes = rateLimiter.getMinPauseCheckBytes();
}
- @Override
- public void close() throws IOException {
- delegate.close();
- }
-
- @Override
- public long getFilePointer() {
- return delegate.getFilePointer();
- }
-
- @Override
- public long getChecksum() throws IOException {
- return delegate.getChecksum();
- }
-
@Override
public void writeByte(byte b) throws IOException {
bytesSinceLastPause++;
checkRate();
- delegate.writeByte(b);
+ out.writeByte(b);
}
@Override
@@ -74,28 +57,28 @@ public final class RateLimitedIndexOutput extends IndexOutput {
// This can cause instant write rate to breach rate limit if there have
// been no writes for enough time to keep the average write rate within limit.
// See https://issues.apache.org/jira/browse/LUCENE-10448
- delegate.writeBytes(b, offset, length);
+ out.writeBytes(b, offset, length);
}
@Override
public void writeInt(int i) throws IOException {
bytesSinceLastPause += Integer.BYTES;
checkRate();
- delegate.writeInt(i);
+ out.writeInt(i);
}
@Override
public void writeShort(short i) throws IOException {
bytesSinceLastPause += Short.BYTES;
checkRate();
- delegate.writeShort(i);
+ out.writeShort(i);
}
@Override
public void writeLong(long i) throws IOException {
bytesSinceLastPause += Long.BYTES;
checkRate();
- delegate.writeLong(i);
+ out.writeLong(i);
}
private void checkRate() throws IOException {
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFilterIndexInput.java b/lucene/core/src/test/org/apache/lucene/index/TestFilterIndexInput.java
new file mode 100644
index 00000000000..d43b580f601
--- /dev/null
+++ b/lucene/core/src/test/org/apache/lucene/index/TestFilterIndexInput.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.index;
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.Random;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+import org.apache.lucene.store.FilterIndexInput;
+import org.apache.lucene.store.IOContext;
+import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.store.IndexOutput;
+import org.junit.Test;
+
+public class TestFilterIndexInput extends TestIndexInput {
+
+ @Override
+ public IndexInput getIndexInput(long len) {
+ return new FilterIndexInput("wrapped foo", new InterceptingIndexInput("foo", len));
+ }
+
+ public void testRawFilterIndexInputRead() throws IOException {
+ for (int i = 0; i < 10; i++) {
+ Random random = random();
+ final Directory dir = newDirectory();
+ IndexOutput os = dir.createOutput("foo", newIOContext(random));
+ os.writeBytes(READ_TEST_BYTES, READ_TEST_BYTES.length);
+ os.close();
+ IndexInput is =
+ new FilterIndexInput("wrapped foo", dir.openInput("foo", newIOContext(random)));
+ checkReads(is, IOException.class);
+ checkSeeksAndSkips(is, random);
+ is.close();
+
+ os = dir.createOutput("bar", newIOContext(random));
+ os.writeBytes(RANDOM_TEST_BYTES, RANDOM_TEST_BYTES.length);
+ os.close();
+ is = new FilterIndexInput("wrapped bar", dir.openInput("bar", newIOContext(random)));
+ checkRandomReads(is);
+ checkSeeksAndSkips(is, random);
+ is.close();
+ dir.close();
+ }
+ }
+
+ @Test
+ public void testOverrides() {
+ for (Method m : FilterIndexInput.class.getMethods()) {
+ if (m.getName().contains("clone")) {
+ // special case
+ continue;
+ }
+ if (m.getDeclaringClass() == FilterIndexInput.class) {
+ // verify that only abstract methods are overridden
+ Method indexInputMethod;
+ try {
+ indexInputMethod = IndexInput.class.getMethod(m.getName(), m.getParameterTypes());
+ assertTrue(
+ "Non-abstract method " + m.getName() + " is overridden",
+ Modifier.isAbstract(indexInputMethod.getModifiers()));
+ } catch (Exception e) {
+ assertTrue(e instanceof NoSuchMethodException);
+ }
+ }
+ }
+ }
+
+ public void testUnwrap() throws IOException {
+ Directory dir = FSDirectory.open(createTempDir());
+ IndexOutput ignored = dir.createOutput("test", IOContext.DEFAULT);
+ IndexInput indexInput = dir.openInput("test", IOContext.DEFAULT);
+ FilterIndexInput filterIndexInput = new FilterIndexInput("wrapper of test", indexInput);
+ assertEquals(indexInput, filterIndexInput.getDelegate());
+ assertEquals(indexInput, FilterIndexInput.unwrap(filterIndexInput));
+ ignored.close();
+ filterIndexInput.close();
+ dir.close();
+ }
+}
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java
index 70ca5fe6d00..a5a33582c08 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java
@@ -193,7 +193,7 @@ public class TestIndexInput extends LuceneTestCase {
RANDOM_TEST_BYTES = null;
}
- private void checkReads(DataInput is, Class<? extends Exception> expectedEx) throws IOException {
+ void checkReads(DataInput is, Class<? extends Exception> expectedEx) throws IOException {
assertEquals(128, is.readVInt());
assertEquals(16383, is.readVInt());
assertEquals(16384, is.readVInt());
@@ -218,7 +218,7 @@ public class TestIndexInput extends LuceneTestCase {
assertEquals("Lu\u0000ce\u0000ne", is.readString());
}
- private void checkRandomReads(DataInput is) throws IOException {
+ void checkRandomReads(DataInput is) throws IOException {
for (int i = 0; i < COUNT; i++) {
assertEquals(INTS[i], is.readVInt());
assertEquals(INTS[i], is.readInt());
@@ -227,7 +227,7 @@ public class TestIndexInput extends LuceneTestCase {
}
}
- private void checkSeeksAndSkips(IndexInput is, Random random) throws IOException {
+ void checkSeeksAndSkips(IndexInput is, Random random) throws IOException {
long len = is.length();
int iterations = LuceneTestCase.TEST_NIGHTLY ? 1_000 : 10;
@@ -296,21 +296,25 @@ public class TestIndexInput extends LuceneTestCase {
public void testNoReadOnSkipBytes() throws IOException {
long len = LuceneTestCase.TEST_NIGHTLY ? Long.MAX_VALUE : 1_000_000;
long maxSeekPos = len - 1;
- InterceptingIndexInput is = new InterceptingIndexInput("foo", len);
+ IndexInput is = getIndexInput(len);
- while (is.pos < maxSeekPos) {
- long seekPos = TestUtil.nextLong(random(), is.pos, maxSeekPos);
- long skipDelta = seekPos - is.pos;
+ while (is.getFilePointer() < maxSeekPos) {
+ long seekPos = TestUtil.nextLong(random(), is.getFilePointer(), maxSeekPos);
+ long skipDelta = seekPos - is.getFilePointer();
is.skipBytes(skipDelta);
- assertEquals(seekPos, is.pos);
+ assertEquals(seekPos, is.getFilePointer());
}
}
+ public IndexInput getIndexInput(long len) {
+ return new InterceptingIndexInput("foo", len);
+ }
+
/**
* Mock IndexInput that just tracks a position (which responds to seek/skip) and throws if
* #readByte or #readBytes are called, ensuring seek/skip doesn't invoke reads.
*/
- private static final class InterceptingIndexInput extends IndexInput {
+ protected static final class InterceptingIndexInput extends IndexInput {
long pos = 0;
final long len;
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestLazyProxSkipping.java b/lucene/core/src/test/org/apache/lucene/index/TestLazyProxSkipping.java
index d6c84895152..310d8524be9 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestLazyProxSkipping.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestLazyProxSkipping.java
@@ -25,6 +25,7 @@ import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.ByteBuffersDirectory;
import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FilterIndexInput;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.tests.analysis.MockAnalyzer;
@@ -174,53 +175,21 @@ public class TestLazyProxSkipping extends LuceneTestCase {
// Simply extends IndexInput in a way that we are able to count the number
// of invocations of seek()
- class SeeksCountingStream extends IndexInput {
- private IndexInput input;
+ class SeeksCountingStream extends FilterIndexInput {
SeeksCountingStream(IndexInput input) {
- super("SeekCountingStream(" + input + ")");
- this.input = input;
- }
-
- @Override
- public byte readByte() throws IOException {
- return this.input.readByte();
- }
-
- @Override
- public void readBytes(byte[] b, int offset, int len) throws IOException {
- this.input.readBytes(b, offset, len);
- }
-
- @Override
- public void close() throws IOException {
- this.input.close();
- }
-
- @Override
- public long getFilePointer() {
- return this.input.getFilePointer();
+ super("SeekCountingStream(" + input + ")", input);
}
@Override
public void seek(long pos) throws IOException {
TestLazyProxSkipping.this.seeksCounter++;
- this.input.seek(pos);
- }
-
- @Override
- public long length() {
- return this.input.length();
+ in.seek(pos);
}
@Override
public SeeksCountingStream clone() {
- return new SeeksCountingStream(this.input.clone());
- }
-
- @Override
- public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
- return new SeeksCountingStream(this.input.slice(sliceDescription, offset, length));
+ return new SeeksCountingStream(in.clone());
}
}
}
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestMultiLevelSkipList.java b/lucene/core/src/test/org/apache/lucene/index/TestMultiLevelSkipList.java
index a0d14f50705..0b0e6c8759c 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestMultiLevelSkipList.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestMultiLevelSkipList.java
@@ -27,6 +27,7 @@ import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.store.ByteBuffersDirectory;
import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FilterIndexInput;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.tests.analysis.MockTokenizer;
@@ -150,54 +151,32 @@ public class TestMultiLevelSkipList extends LuceneTestCase {
// Simply extends IndexInput in a way that we are able to count the number
// of bytes read
- class CountingStream extends IndexInput {
- private IndexInput input;
+ class CountingStream extends FilterIndexInput {
CountingStream(IndexInput input) {
- super("CountingStream(" + input + ")");
- this.input = input;
+ super("CountingStream(" + input + ")", input);
}
@Override
public byte readByte() throws IOException {
TestMultiLevelSkipList.this.counter++;
- return this.input.readByte();
+ return in.readByte();
}
@Override
public void readBytes(byte[] b, int offset, int len) throws IOException {
TestMultiLevelSkipList.this.counter += len;
- this.input.readBytes(b, offset, len);
- }
-
- @Override
- public void close() throws IOException {
- this.input.close();
- }
-
- @Override
- public long getFilePointer() {
- return this.input.getFilePointer();
- }
-
- @Override
- public void seek(long pos) throws IOException {
- this.input.seek(pos);
- }
-
- @Override
- public long length() {
- return this.input.length();
+ in.readBytes(b, offset, len);
}
@Override
public CountingStream clone() {
- return new CountingStream(this.input.clone());
+ return new CountingStream(in.clone());
}
@Override
public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
- return new CountingStream(this.input.slice(sliceDescription, offset, length));
+ return new CountingStream(in.slice(sliceDescription, offset, length));
}
}
}
diff --git a/lucene/core/src/test/org/apache/lucene/store/TestFilterIndexOutput.java b/lucene/core/src/test/org/apache/lucene/store/TestFilterIndexOutput.java
new file mode 100644
index 00000000000..2abdf754d59
--- /dev/null
+++ b/lucene/core/src/test/org/apache/lucene/store/TestFilterIndexOutput.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.store;
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import org.junit.Test;
+
+public class TestFilterIndexOutput extends BaseDataOutputTestCase<FilterIndexOutput> {
+
+ @Override
+ protected FilterIndexOutput newInstance() {
+ return new FilterIndexOutput(
+ "test",
+ "test",
+ new ByteBuffersIndexOutput(ByteBuffersDataOutput.newResettableInstance(), "test", "test"));
+ }
+
+ @Override
+ protected byte[] toBytes(FilterIndexOutput instance) {
+ return ((ByteBuffersIndexOutput) instance.out).toArrayCopy();
+ }
+
+ @Test
+ public void testOverrides() {
+ for (Method m : FilterIndexOutput.class.getMethods()) {
+ if (m.getDeclaringClass() == FilterIndexOutput.class) {
+ // verify that only abstract methods are overridden
+ Method indexOutputMethod;
+ try {
+ indexOutputMethod = IndexOutput.class.getMethod(m.getName(), m.getParameterTypes());
+ assertTrue(
+ "Non-abstract method " + m.getName() + " is overridden",
+ Modifier.isAbstract(indexOutputMethod.getModifiers()));
+ } catch (Exception e) {
+ assertTrue(e instanceof NoSuchMethodException);
+ }
+ }
+ }
+ }
+
+ public void testUnwrap() throws IOException {
+ Directory dir = FSDirectory.open(createTempDir());
+ IndexOutput output = dir.createOutput("test", IOContext.DEFAULT);
+ FilterIndexOutput filterIndexOutput =
+ new FilterIndexOutput("wrapper of test", "FilterDirectory{test}", output);
+ assertEquals(output, filterIndexOutput.getDelegate());
+ assertEquals(output, FilterIndexOutput.unwrap(filterIndexOutput));
+ filterIndexOutput.close();
+ dir.close();
+ }
+}
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/SlowDirectory.java b/lucene/facet/src/test/org/apache/lucene/facet/SlowDirectory.java
index a809ea1e711..b0c17742c2f 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/SlowDirectory.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/SlowDirectory.java
@@ -20,6 +20,8 @@ import java.io.IOException;
import java.util.Random;
import org.apache.lucene.store.ByteBuffersDirectory;
import org.apache.lucene.store.FilterDirectory;
+import org.apache.lucene.store.FilterIndexInput;
+import org.apache.lucene.store.FilterIndexOutput;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
@@ -82,15 +84,13 @@ public class SlowDirectory extends FilterDirectory {
}
/** Delegate class to wrap an IndexInput and delay reading bytes by some specified time. */
- private class SlowIndexInput extends IndexInput {
- private IndexInput ii;
+ private class SlowIndexInput extends FilterIndexInput {
private int numRead = 0;
private Random rand;
public SlowIndexInput(IndexInput ii) {
- super("SlowIndexInput(" + ii + ")");
+ super("SlowIndexInput(" + ii + ")", ii);
this.rand = forkRandom();
- this.ii = ii;
}
@Override
@@ -100,7 +100,7 @@ public class SlowDirectory extends FilterDirectory {
numRead = 0;
}
++numRead;
- return ii.readByte();
+ return in.readByte();
}
@Override
@@ -110,61 +110,28 @@ public class SlowDirectory extends FilterDirectory {
numRead = 0;
}
numRead += len;
- ii.readBytes(b, offset, len);
- }
-
- // TODO: is it intentional that clone doesnt wrap?
- @Override
- public IndexInput clone() {
- return ii.clone();
- }
-
- @Override
- public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
- return ii.slice(sliceDescription, offset, length);
- }
-
- @Override
- public void close() throws IOException {
- ii.close();
+ in.readBytes(b, offset, len);
}
@Override
public boolean equals(Object o) {
- return ii.equals(o);
- }
-
- @Override
- public long getFilePointer() {
- return ii.getFilePointer();
+ return in.equals(o);
}
@Override
public int hashCode() {
- return ii.hashCode();
- }
-
- @Override
- public long length() {
- return ii.length();
- }
-
- @Override
- public void seek(long pos) throws IOException {
- ii.seek(pos);
+ return in.hashCode();
}
}
/** Delegate class to wrap an IndexOutput and delay writing bytes by some specified time. */
- private class SlowIndexOutput extends IndexOutput {
+ private class SlowIndexOutput extends FilterIndexOutput {
- private IndexOutput io;
private int numWrote;
private final Random rand;
- public SlowIndexOutput(IndexOutput io) {
- super("SlowIndexOutput(" + io + ")", io.getName());
- this.io = io;
+ public SlowIndexOutput(IndexOutput out) {
+ super("SlowIndexOutput(" + out + ")", out.getName(), out);
this.rand = forkRandom();
}
@@ -175,7 +142,7 @@ public class SlowDirectory extends FilterDirectory {
numWrote = 0;
}
++numWrote;
- io.writeByte(b);
+ out.writeByte(b);
}
@Override
@@ -185,22 +152,7 @@ public class SlowDirectory extends FilterDirectory {
numWrote = 0;
}
numWrote += length;
- io.writeBytes(b, offset, length);
- }
-
- @Override
- public void close() throws IOException {
- io.close();
- }
-
- @Override
- public long getFilePointer() {
- return io.getFilePointer();
- }
-
- @Override
- public long getChecksum() throws IOException {
- return io.getChecksum();
+ out.writeBytes(b, offset, length);
}
}
}
diff --git a/lucene/misc/src/java/org/apache/lucene/misc/store/ByteTrackingIndexOutput.java b/lucene/misc/src/java/org/apache/lucene/misc/store/ByteTrackingIndexOutput.java
index 4adb1938b4a..f55c555b368 100644
--- a/lucene/misc/src/java/org/apache/lucene/misc/store/ByteTrackingIndexOutput.java
+++ b/lucene/misc/src/java/org/apache/lucene/misc/store/ByteTrackingIndexOutput.java
@@ -18,74 +18,31 @@ package org.apache.lucene.misc.store;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;
+import org.apache.lucene.store.FilterIndexOutput;
import org.apache.lucene.store.IndexOutput;
/** An {@link IndexOutput} that wraps another instance and tracks the number of bytes written */
-public class ByteTrackingIndexOutput extends IndexOutput {
+public class ByteTrackingIndexOutput extends FilterIndexOutput {
- private final IndexOutput output;
private final AtomicLong byteTracker;
private boolean closed = false;
- protected ByteTrackingIndexOutput(IndexOutput output, AtomicLong byteTracker) {
+ protected ByteTrackingIndexOutput(IndexOutput out, AtomicLong byteTracker) {
super(
- "Byte tracking wrapper for: " + output.getName(),
- "ByteTrackingIndexOutput{" + output.getName() + "}");
- this.output = output;
+ "Byte tracking wrapper for: " + out.getName(),
+ "ByteTrackingIndexOutput{" + out.getName() + "}",
+ out);
this.byteTracker = byteTracker;
}
- @Override
- public void writeByte(byte b) throws IOException {
- output.writeByte(b);
- }
-
- @Override
- public void writeBytes(byte[] b, int offset, int length) throws IOException {
- output.writeBytes(b, offset, length);
- }
-
- @Override
- public void writeShort(short i) throws IOException {
- output.writeShort(i);
- }
-
- @Override
- public void writeInt(int i) throws IOException {
- output.writeInt(i);
- }
-
- @Override
- public void writeLong(long i) throws IOException {
- output.writeLong(i);
- }
-
@Override
public void close() throws IOException {
if (closed) {
- output.close();
+ out.close();
return;
}
- byteTracker.addAndGet(output.getFilePointer());
+ byteTracker.addAndGet(out.getFilePointer());
closed = true;
- output.close();
- }
-
- @Override
- public long getFilePointer() {
- return output.getFilePointer();
- }
-
- @Override
- public long getChecksum() throws IOException {
- return output.getChecksum();
- }
-
- public String getWrappedName() {
- return output.getName();
- }
-
- public String getWrappedToString() {
- return output.toString();
+ out.close();
}
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseIndexFileFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseIndexFileFormatTestCase.java
index 8d4faa58271..3d879520f65 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseIndexFileFormatTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/index/BaseIndexFileFormatTestCase.java
@@ -78,6 +78,7 @@ import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.ChecksumIndexInput;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FilterDirectory;
+import org.apache.lucene.store.FilterIndexInput;
import org.apache.lucene.store.FlushInfo;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
@@ -780,14 +781,12 @@ abstract class BaseIndexFileFormatTestCase extends LuceneTestCase {
}
}
- private static class ReadBytesIndexInputWrapper extends IndexInput {
+ private static class ReadBytesIndexInputWrapper extends FilterIndexInput {
- private final IndexInput in;
private final IntConsumer readByte;
ReadBytesIndexInputWrapper(IndexInput in, IntConsumer readByte) {
- super(in.toString());
- this.in = in;
+ super(in.toString(), in);
this.readByte = readByte;
}
@@ -796,26 +795,6 @@ abstract class BaseIndexFileFormatTestCase extends LuceneTestCase {
return new ReadBytesIndexInputWrapper(in.clone(), readByte);
}
- @Override
- public void close() throws IOException {
- in.close();
- }
-
- @Override
- public long getFilePointer() {
- return in.getFilePointer();
- }
-
- @Override
- public void seek(long pos) throws IOException {
- in.seek(pos);
- }
-
- @Override
- public long length() {
- return in.length();
- }
-
@Override
public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
IndexInput slice = in.slice(sliceDescription, offset, length);
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/CorruptingIndexOutput.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/CorruptingIndexOutput.java
index c0d65b31001..c3c3ebf4313 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/CorruptingIndexOutput.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/CorruptingIndexOutput.java
@@ -19,22 +19,21 @@ package org.apache.lucene.tests.store;
import java.io.IOException;
import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FilterIndexOutput;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
/** Corrupts on bit of a file after close */
-public class CorruptingIndexOutput extends IndexOutput {
- protected final IndexOutput out;
+public class CorruptingIndexOutput extends FilterIndexOutput {
final Directory dir;
final long byteToCorrupt;
private boolean closed;
public CorruptingIndexOutput(Directory dir, long byteToCorrupt, IndexOutput out) {
- super("CorruptingIndexOutput(" + out + ")", out.getName());
+ super("CorruptingIndexOutput(" + out + ")", out.getName(), out);
this.dir = dir;
this.byteToCorrupt = byteToCorrupt;
- this.out = out;
}
@Override
@@ -83,26 +82,11 @@ public class CorruptingIndexOutput extends IndexOutput {
dir.deleteFile(newTempName);
}
- @Override
- public long getFilePointer() {
- return out.getFilePointer();
- }
-
@Override
public long getChecksum() throws IOException {
return out.getChecksum() ^ 1;
}
- @Override
- public String toString() {
- return "CorruptingIndexOutput(" + out + ")";
- }
-
- @Override
- public void writeByte(byte b) throws IOException {
- out.writeByte(b);
- }
-
@Override
public void writeBytes(byte[] b, int offset, int length) throws IOException {
for (int i = 0; i < length; i++) {
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java
index d1b034a1237..39c41d46825 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexInputWrapper.java
@@ -20,15 +20,15 @@ import java.io.Closeable;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
+import org.apache.lucene.store.FilterIndexInput;
import org.apache.lucene.store.IndexInput;
/**
* Used by MockDirectoryWrapper to create an input stream that keeps track of when it's been closed.
*/
-public class MockIndexInputWrapper extends IndexInput {
+public class MockIndexInputWrapper extends FilterIndexInput {
private MockDirectoryWrapper dir;
final String name;
- private IndexInput delegate;
private volatile boolean closed;
// Which MockIndexInputWrapper we were cloned from, or null if we are not a clone:
@@ -37,7 +37,7 @@ public class MockIndexInputWrapper extends IndexInput {
/** Sole constructor */
public MockIndexInputWrapper(
MockDirectoryWrapper dir, String name, IndexInput delegate, MockIndexInputWrapper parent) {
- super("MockIndexInputWrapper(name=" + name + " delegate=" + delegate + ")");
+ super("MockIndexInputWrapper(name=" + name + " delegate=" + delegate + ")", delegate);
// If we are a clone then our parent better not be a clone!
assert parent == null || parent.parent == null;
@@ -45,18 +45,17 @@ public class MockIndexInputWrapper extends IndexInput {
this.parent = parent;
this.name = name;
this.dir = dir;
- this.delegate = delegate;
}
@Override
public void close() throws IOException {
if (closed) {
- delegate.close(); // don't mask double-close bugs
+ in.close(); // don't mask double-close bugs
return;
}
closed = true;
- try (Closeable delegate = this.delegate) {
+ try (Closeable delegate = in) {
// Pending resolution on LUCENE-686 we may want to
// remove the conditional check so we also track that
// all clones get closed:
@@ -86,7 +85,7 @@ public class MockIndexInputWrapper extends IndexInput {
new Exception("clone: " + this).printStackTrace(System.out);
}
dir.inputCloneCount.incrementAndGet();
- IndexInput iiclone = delegate.clone();
+ IndexInput iiclone = in.clone();
MockIndexInputWrapper clone =
new MockIndexInputWrapper(dir, name, iiclone, parent != null ? parent : this);
// Pending resolution on LUCENE-686 we may want to
@@ -113,7 +112,7 @@ public class MockIndexInputWrapper extends IndexInput {
new Exception("slice: " + this).printStackTrace(System.out);
}
dir.inputCloneCount.incrementAndGet();
- IndexInput slice = delegate.slice(sliceDescription, offset, length);
+ IndexInput slice = in.slice(sliceDescription, offset, length);
MockIndexInputWrapper clone =
new MockIndexInputWrapper(dir, sliceDescription, slice, parent != null ? parent : this);
return clone;
@@ -122,91 +121,91 @@ public class MockIndexInputWrapper extends IndexInput {
@Override
public long getFilePointer() {
ensureOpen();
- return delegate.getFilePointer();
+ return in.getFilePointer();
}
@Override
public void seek(long pos) throws IOException {
ensureOpen();
- delegate.seek(pos);
+ in.seek(pos);
}
@Override
public long length() {
ensureOpen();
- return delegate.length();
+ return in.length();
}
@Override
public byte readByte() throws IOException {
ensureOpen();
- return delegate.readByte();
+ return in.readByte();
}
@Override
public void readBytes(byte[] b, int offset, int len) throws IOException {
ensureOpen();
- delegate.readBytes(b, offset, len);
+ in.readBytes(b, offset, len);
}
@Override
public void readBytes(byte[] b, int offset, int len, boolean useBuffer) throws IOException {
ensureOpen();
- delegate.readBytes(b, offset, len, useBuffer);
+ in.readBytes(b, offset, len, useBuffer);
}
@Override
public void readFloats(float[] floats, int offset, int len) throws IOException {
ensureOpen();
- delegate.readFloats(floats, offset, len);
+ in.readFloats(floats, offset, len);
}
@Override
public short readShort() throws IOException {
ensureOpen();
- return delegate.readShort();
+ return in.readShort();
}
@Override
public int readInt() throws IOException {
ensureOpen();
- return delegate.readInt();
+ return in.readInt();
}
@Override
public long readLong() throws IOException {
ensureOpen();
- return delegate.readLong();
+ return in.readLong();
}
@Override
public String readString() throws IOException {
ensureOpen();
- return delegate.readString();
+ return in.readString();
}
@Override
public int readVInt() throws IOException {
ensureOpen();
- return delegate.readVInt();
+ return in.readVInt();
}
@Override
public long readVLong() throws IOException {
ensureOpen();
- return delegate.readVLong();
+ return in.readVLong();
}
@Override
public int readZInt() throws IOException {
ensureOpen();
- return delegate.readZInt();
+ return in.readZInt();
}
@Override
public long readZLong() throws IOException {
ensureOpen();
- return delegate.readZLong();
+ return in.readZLong();
}
@Override
@@ -218,17 +217,17 @@ public class MockIndexInputWrapper extends IndexInput {
@Override
public Map<String, String> readMapOfStrings() throws IOException {
ensureOpen();
- return delegate.readMapOfStrings();
+ return in.readMapOfStrings();
}
@Override
public Set<String> readSetOfStrings() throws IOException {
ensureOpen();
- return delegate.readSetOfStrings();
+ return in.readSetOfStrings();
}
@Override
public String toString() {
- return "MockIndexInputWrapper(" + delegate + ")";
+ return "MockIndexInputWrapper(" + in + ")";
}
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexOutputWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexOutputWrapper.java
index 005542a2e90..0e76492f093 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexOutputWrapper.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/MockIndexOutputWrapper.java
@@ -20,6 +20,7 @@ import java.io.Closeable;
import java.io.IOException;
import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.DataInput;
+import org.apache.lucene.store.FilterIndexOutput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.tests.util.LuceneTestCase;
@@ -27,20 +28,18 @@ import org.apache.lucene.tests.util.LuceneTestCase;
* Used to create an output stream that will throw an IOException on fake disk full, track max disk
* space actually used, and maybe throw random IOExceptions.
*/
-public class MockIndexOutputWrapper extends IndexOutput {
+public class MockIndexOutputWrapper extends FilterIndexOutput {
private MockDirectoryWrapper dir;
- private final IndexOutput delegate;
private boolean first = true;
final String name;
byte[] singleByte = new byte[1];
/** Construct an empty output buffer. */
- public MockIndexOutputWrapper(MockDirectoryWrapper dir, IndexOutput delegate, String name) {
- super("MockIndexOutputWrapper(" + delegate + ")", delegate.getName());
+ public MockIndexOutputWrapper(MockDirectoryWrapper dir, IndexOutput out, String name) {
+ super("MockIndexOutputWrapper(" + out + ")", out.getName(), out);
this.dir = dir;
this.name = name;
- this.delegate = delegate;
}
private void checkCrashed() throws IOException {
@@ -67,9 +66,9 @@ public class MockIndexOutputWrapper extends IndexOutput {
if (freeSpace > 0) {
realUsage += freeSpace;
if (b != null) {
- delegate.writeBytes(b, offset, (int) freeSpace);
+ out.writeBytes(b, offset, (int) freeSpace);
} else {
- delegate.copyBytes(in, (int) freeSpace);
+ out.copyBytes(in, (int) freeSpace);
}
}
if (realUsage > dir.maxUsedSize) {
@@ -81,7 +80,7 @@ public class MockIndexOutputWrapper extends IndexOutput {
+ " bytes when writing "
+ name
+ " (file length="
- + delegate.getFilePointer();
+ + out.getFilePointer();
if (freeSpace > 0) {
message += "; wrote " + freeSpace + " of " + len + " bytes";
}
@@ -99,13 +98,13 @@ public class MockIndexOutputWrapper extends IndexOutput {
@Override
public void close() throws IOException {
if (closed) {
- delegate.close(); // don't mask double-close bugs
+ out.close(); // don't mask double-close bugs
return;
}
closed = true;
- try (Closeable delegate = this.delegate) {
- assert delegate != null;
+ try (Closeable out = this.out) {
+ assert out != null;
dir.maybeThrowDeterministicException();
} finally {
dir.removeIndexOutput(this, name);
@@ -140,11 +139,11 @@ public class MockIndexOutputWrapper extends IndexOutput {
if (dir.randomState.nextInt(200) == 0) {
final int half = len / 2;
- delegate.writeBytes(b, offset, half);
+ out.writeBytes(b, offset, half);
Thread.yield();
- delegate.writeBytes(b, offset + half, len - half);
+ out.writeBytes(b, offset + half, len - half);
} else {
- delegate.writeBytes(b, offset, len);
+ out.writeBytes(b, offset, len);
}
dir.maybeThrowDeterministicException();
@@ -158,8 +157,8 @@ public class MockIndexOutputWrapper extends IndexOutput {
}
@Override
- public long getFilePointer() {
- return delegate.getFilePointer();
+ public void writeBytes(byte[] b, int len) throws IOException {
+ writeBytes(b, 0, len);
}
@Override
@@ -168,17 +167,7 @@ public class MockIndexOutputWrapper extends IndexOutput {
checkCrashed();
checkDiskFull(null, 0, input, numBytes);
- delegate.copyBytes(input, numBytes);
+ out.copyBytes(input, numBytes);
dir.maybeThrowDeterministicException();
}
-
- @Override
- public long getChecksum() throws IOException {
- return delegate.getChecksum();
- }
-
- @Override
- public String toString() {
- return "MockIndexOutputWrapper(" + delegate + ")";
- }
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/util/ThrottledIndexOutput.java b/lucene/test-framework/src/java/org/apache/lucene/tests/util/ThrottledIndexOutput.java
index 013bdc21d3d..6e29e79ad69 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/util/ThrottledIndexOutput.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/util/ThrottledIndexOutput.java
@@ -18,15 +18,14 @@ package org.apache.lucene.tests.util;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
-import org.apache.lucene.store.DataInput;
+import org.apache.lucene.store.FilterIndexOutput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.ThreadInterruptedException;
/** Intentionally slow IndexOutput for testing. */
-public class ThrottledIndexOutput extends IndexOutput {
+public class ThrottledIndexOutput extends FilterIndexOutput {
public static final int DEFAULT_MIN_WRITTEN_BYTES = 1024;
private final int bytesPerSecond;
- private IndexOutput delegate;
private long flushDelayMillis;
private long closeDelayMillis;
private long seekDelayMillis;
@@ -35,29 +34,24 @@ public class ThrottledIndexOutput extends IndexOutput {
private long timeElapsed;
private final byte[] bytes = new byte[1];
- public ThrottledIndexOutput newFromDelegate(IndexOutput output) {
+ public ThrottledIndexOutput newFromDelegate(IndexOutput out) {
return new ThrottledIndexOutput(
- bytesPerSecond,
- flushDelayMillis,
- closeDelayMillis,
- seekDelayMillis,
- minBytesWritten,
- output);
+ bytesPerSecond, flushDelayMillis, closeDelayMillis, seekDelayMillis, minBytesWritten, out);
}
- public ThrottledIndexOutput(int bytesPerSecond, long delayInMillis, IndexOutput delegate) {
+ public ThrottledIndexOutput(int bytesPerSecond, long delayInMillis, IndexOutput out) {
this(
bytesPerSecond,
delayInMillis,
delayInMillis,
delayInMillis,
DEFAULT_MIN_WRITTEN_BYTES,
- delegate);
+ out);
}
public ThrottledIndexOutput(
- int bytesPerSecond, long delays, int minBytesWritten, IndexOutput delegate) {
- this(bytesPerSecond, delays, delays, delays, minBytesWritten, delegate);
+ int bytesPerSecond, long delays, int minBytesWritten, IndexOutput out) {
+ this(bytesPerSecond, delays, delays, delays, minBytesWritten, out);
}
public static final int mBitsToBytes(int mbits) {
@@ -70,10 +64,9 @@ public class ThrottledIndexOutput extends IndexOutput {
long closeDelayMillis,
long seekDelayMillis,
long minBytesWritten,
- IndexOutput delegate) {
- super("ThrottledIndexOutput(" + delegate + ")", delegate == null ? "n/a" : delegate.getName());
+ IndexOutput out) {
+ super("ThrottledIndexOutput(" + out + ")", out == null ? "n/a" : out.getName(), out);
assert bytesPerSecond > 0;
- this.delegate = delegate;
this.bytesPerSecond = bytesPerSecond;
this.flushDelayMillis = flushDelayMillis;
this.closeDelayMillis = closeDelayMillis;
@@ -86,15 +79,10 @@ public class ThrottledIndexOutput extends IndexOutput {
try {
sleep(closeDelayMillis + getDelay(true));
} finally {
- delegate.close();
+ out.close();
}
}
- @Override
- public long getFilePointer() {
- return delegate.getFilePointer();
- }
-
@Override
public void writeByte(byte b) throws IOException {
bytes[0] = b;
@@ -107,7 +95,7 @@ public class ThrottledIndexOutput extends IndexOutput {
// TODO: sometimes, write only half the bytes, then
// sleep, then 2nd half, then sleep, so we sometimes
// interrupt having only written not all bytes
- delegate.writeBytes(b, offset, length);
+ out.writeBytes(b, offset, length);
timeElapsed += System.nanoTime() - before;
pendingBytes += length;
sleep(getDelay(false));
@@ -127,7 +115,7 @@ public class ThrottledIndexOutput extends IndexOutput {
return 0;
}
- private static final void sleep(long ms) {
+ private static void sleep(long ms) {
if (ms <= 0) {
return;
}
@@ -137,14 +125,4 @@ public class ThrottledIndexOutput extends IndexOutput {
throw new ThreadInterruptedException(e);
}
}
-
- @Override
- public void copyBytes(DataInput input, long numBytes) throws IOException {
- delegate.copyBytes(input, numBytes);
- }
-
- @Override
- public long getChecksum() throws IOException {
- return delegate.getChecksum();
- }
}