You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2021/10/10 22:25:47 UTC

[lucene] branch main updated: LUCENE-10158: Add a new interface Unwrappable to the utils package to ease migration to new MMAPDirectory and its testing (#369)

This is an automated email from the ASF dual-hosted git repository.

uschindler pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new c94aca7  LUCENE-10158: Add a new interface Unwrappable to the utils package to ease migration to new MMAPDirectory and its testing (#369)
c94aca7 is described below

commit c94aca7e5dd63a2430b9245478a15baa60c80aa9
Author: Uwe Schindler <us...@apache.org>
AuthorDate: Mon Oct 11 00:25:40 2021 +0200

    LUCENE-10158: Add a new interface Unwrappable to the utils package to ease migration to new MMAPDirectory and its testing (#369)
---
 lucene/CHANGES.txt                                 |  4 +++
 .../java/org/apache/lucene/util/Unwrappable.java   | 38 ++++++++++++++++++++++
 .../org/apache/lucene/mockfile/FilterPath.java     | 13 +++++---
 3 files changed, 50 insertions(+), 5 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 1ab534e..6ade60b 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -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
diff --git a/lucene/core/src/java/org/apache/lucene/util/Unwrappable.java b/lucene/core/src/java/org/apache/lucene/util/Unwrappable.java
new file mode 100644
index 0000000..4c91e38
--- /dev/null
+++ b/lucene/core/src/java/org/apache/lucene/util/Unwrappable.java
@@ -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;
+  }
+}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/mockfile/FilterPath.java b/lucene/test-framework/src/java/org/apache/lucene/mockfile/FilterPath.java
index 9323536..73f496f 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/mockfile/FilterPath.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/mockfile/FilterPath.java
@@ -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;
@@ -64,6 +65,11 @@ public class FilterPath implements Path {
   }
 
   @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 */