LUCENE-10158: Add a new interface Unwrappable to the utils package to ease migration to new MMAPDirectory and its testing (#369)

This commit is contained in:
Uwe Schindler 2021-10-11 00:25:40 +02:00 committed by GitHub
parent 6f232b6f4b
commit c94aca7e5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 5 deletions

View File

@ -157,6 +157,10 @@ API Changes
* LUCENE-9325: Sort is now final, and the `setSort()` method has been removed (Alan Woodward)
* LUCENE-10158: Add a new interface Unwrappable to the utils package to allow code to
unwrap wrappers/delegators that are added by Lucene's testing framework. This will allow
testing new MMapDirectory implementation based on JDK Project Panama. (Uwe Schindler)
Improvements
* LUCENE-10139: ExternalRefSorter returns a covariant with a subtype of BytesRefIterator

View File

@ -0,0 +1,38 @@
/*
* 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.util;
/**
* An object with this interface is a wrapper around another object (e.g., a filter with a
* delegate). The method {@link #unwrap()} can be called to get the wrapped object
*
* @lucene.internal
*/
public interface Unwrappable<T> {
/** Unwraps this instance */
T unwrap();
/** Unwraps all {@code Unwrappable}s around the given object. */
@SuppressWarnings("unchecked")
public static <T> T unwrapAll(T o) {
while (o instanceof Unwrappable) {
o = ((Unwrappable<T>) o).unwrap();
}
return o;
}
}

View File

@ -29,12 +29,13 @@ import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.Iterator;
import org.apache.lucene.util.SuppressForbidden;
import org.apache.lucene.util.Unwrappable;
/**
* A {@code FilterPath} contains another {@code Path}, which it uses as its basic source of data,
* possibly transforming the data along the way or providing additional functionality.
*/
public class FilterPath implements Path {
public class FilterPath implements Path, Unwrappable<Path> {
/** The underlying {@code Path} instance. */
protected final Path delegate;
@ -63,6 +64,11 @@ public class FilterPath implements Path {
return delegate;
}
@Override
public Path unwrap() {
return delegate;
}
@Override
public FileSystem getFileSystem() {
return fileSystem;
@ -261,10 +267,7 @@ public class FilterPath implements Path {
* @return innermost Path instance
*/
public static Path unwrap(Path path) {
while (path instanceof FilterPath) {
path = ((FilterPath) path).delegate;
}
return path;
return Unwrappable.unwrapAll(path);
}
/** Override this to customize the return wrapped path from various operations */