mirror of https://github.com/apache/lucene.git
LUCENE-6099: add FilterDirectory.unwrap and FilterDirectoryReader.unwrap
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1643902 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0c28951031
commit
a5b8a645c6
|
@ -299,6 +299,9 @@ API Changes
|
||||||
IndexWriter.abortMerges and waitForMerges methods. (Robert Muir,
|
IndexWriter.abortMerges and waitForMerges methods. (Robert Muir,
|
||||||
Mike McCandless)
|
Mike McCandless)
|
||||||
|
|
||||||
|
* LUCENE-6099: Add FilterDirectory.unwrap and
|
||||||
|
FilterDirectoryReader.unwrap (Simon Willnauer, Mike McCandless)
|
||||||
|
|
||||||
Bug Fixes
|
Bug Fixes
|
||||||
|
|
||||||
* LUCENE-5650: Enforce read-only access to any path outside the temporary
|
* LUCENE-5650: Enforce read-only access to any path outside the temporary
|
||||||
|
|
|
@ -33,6 +33,15 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public abstract class FilterDirectoryReader extends DirectoryReader {
|
public abstract class FilterDirectoryReader extends DirectoryReader {
|
||||||
|
|
||||||
|
/** Get the wrapped instance by <code>reader</code> as long as this reader is
|
||||||
|
* an instance of {@link FilterDirectoryReader}. */
|
||||||
|
public static DirectoryReader unwrap(DirectoryReader reader) {
|
||||||
|
while (reader instanceof FilterDirectoryReader) {
|
||||||
|
reader = ((FilterDirectoryReader) reader).in;
|
||||||
|
}
|
||||||
|
return reader;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory class passed to FilterDirectoryReader constructor that allows
|
* Factory class passed to FilterDirectoryReader constructor that allows
|
||||||
* subclasses to wrap the filtered DirectoryReader's subreaders. You
|
* subclasses to wrap the filtered DirectoryReader's subreaders. You
|
||||||
|
@ -125,4 +134,8 @@ public abstract class FilterDirectoryReader extends DirectoryReader {
|
||||||
in.doClose();
|
in.doClose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns the wrapped {@link DirectoryReader}. */
|
||||||
|
public DirectoryReader getDelegate() {
|
||||||
|
return in;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,7 @@ import org.apache.lucene.util.BytesRef;
|
||||||
public class FilterLeafReader extends LeafReader {
|
public class FilterLeafReader extends LeafReader {
|
||||||
|
|
||||||
/** Get the wrapped instance by <code>reader</code> as long as this reader is
|
/** Get the wrapped instance by <code>reader</code> as long as this reader is
|
||||||
* an intance of {@link FilterLeafReader}. */
|
* an instance of {@link FilterLeafReader}. */
|
||||||
public static LeafReader unwrap(LeafReader reader) {
|
public static LeafReader unwrap(LeafReader reader) {
|
||||||
while (reader instanceof FilterLeafReader) {
|
while (reader instanceof FilterLeafReader) {
|
||||||
reader = ((FilterLeafReader) reader).in;
|
reader = ((FilterLeafReader) reader).in;
|
||||||
|
@ -468,4 +468,9 @@ public class FilterLeafReader extends LeafReader {
|
||||||
ensureOpen();
|
ensureOpen();
|
||||||
in.checkIntegrity();
|
in.checkIntegrity();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns the wrapped {@link LeafReader}. */
|
||||||
|
public LeafReader getDelegate() {
|
||||||
|
return in;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,15 @@ import java.util.Collection;
|
||||||
* @lucene.internal */
|
* @lucene.internal */
|
||||||
public class FilterDirectory extends Directory {
|
public class FilterDirectory extends Directory {
|
||||||
|
|
||||||
|
/** Get the wrapped instance by <code>dir</code> as long as this reader is
|
||||||
|
* an instance of {@link FilterDirectory}. */
|
||||||
|
public static Directory unwrap(Directory dir) {
|
||||||
|
while (dir instanceof FilterDirectory) {
|
||||||
|
dir = ((FilterDirectory) dir).in;
|
||||||
|
}
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
|
||||||
protected final Directory in;
|
protected final Directory in;
|
||||||
|
|
||||||
/** Sole constructor, typically called from sub-classes. */
|
/** Sole constructor, typically called from sub-classes. */
|
||||||
|
|
|
@ -192,4 +192,17 @@ public class TestFilterLeafReader extends LuceneTestCase {
|
||||||
checkOverrideMethods(FilterLeafReader.FilterDocsAndPositionsEnum.class);
|
checkOverrideMethods(FilterLeafReader.FilterDocsAndPositionsEnum.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testUnwrap() throws IOException {
|
||||||
|
Directory dir = newDirectory();
|
||||||
|
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
|
||||||
|
w.addDocument(new Document());
|
||||||
|
DirectoryReader dr = w.getReader();
|
||||||
|
LeafReader r = dr.leaves().get(0).reader();
|
||||||
|
FilterLeafReader r2 = new FilterLeafReader(r);
|
||||||
|
assertEquals(r, r2.getDelegate());
|
||||||
|
assertEquals(r, FilterLeafReader.unwrap(r2));
|
||||||
|
w.close();
|
||||||
|
dr.close();
|
||||||
|
dir.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -462,7 +462,9 @@ public class TestSearcherManager extends ThreadedIndexingAndSearchingTestCase {
|
||||||
new FilterDirectoryReader.SubReaderWrapper() {
|
new FilterDirectoryReader.SubReaderWrapper() {
|
||||||
@Override
|
@Override
|
||||||
public LeafReader wrap(LeafReader reader) {
|
public LeafReader wrap(LeafReader reader) {
|
||||||
return new MyFilterLeafReader(reader);
|
FilterLeafReader wrapped = new MyFilterLeafReader(reader);
|
||||||
|
assertEquals(reader, wrapped.getDelegate());
|
||||||
|
return wrapped;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -477,7 +479,12 @@ public class TestSearcherManager extends ThreadedIndexingAndSearchingTestCase {
|
||||||
public void testCustomDirectoryReader() throws Exception {
|
public void testCustomDirectoryReader() throws Exception {
|
||||||
Directory dir = newDirectory();
|
Directory dir = newDirectory();
|
||||||
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
|
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
|
||||||
DirectoryReader reader = new MyFilterDirectoryReader(w.getReader());
|
DirectoryReader nrtReader = w.getReader();
|
||||||
|
|
||||||
|
FilterDirectoryReader reader = new MyFilterDirectoryReader(nrtReader);
|
||||||
|
assertEquals(nrtReader, reader.getDelegate());
|
||||||
|
assertEquals(nrtReader, FilterDirectoryReader.unwrap(reader));
|
||||||
|
|
||||||
SearcherManager mgr = new SearcherManager(reader, null);
|
SearcherManager mgr = new SearcherManager(reader, null);
|
||||||
for(int i=0;i<10;i++) {
|
for(int i=0;i<10;i++) {
|
||||||
w.addDocument(new Document());
|
w.addDocument(new Document());
|
||||||
|
|
|
@ -17,6 +17,7 @@ package org.apache.lucene.store;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -40,4 +41,11 @@ public class TestFilterDirectory extends LuceneTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testUnwrap() throws IOException {
|
||||||
|
Directory dir = FSDirectory.open(createTempDir());
|
||||||
|
FilterDirectory dir2 = new FilterDirectory(dir);
|
||||||
|
assertEquals(dir, dir2.getDelegate());
|
||||||
|
assertEquals(dir, FilterDirectory.unwrap(dir2));
|
||||||
|
dir2.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue