uschindler commented on code in PR #14843:
URL: https://github.com/apache/lucene/pull/14843#discussion_r2194867896


##########
lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/faiss/FaissKnnVectorsFormatProvider.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.sandbox.codecs.faiss;
+
+import java.io.IOException;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.Arrays;
+import java.util.stream.Collectors;
+import org.apache.lucene.codecs.KnnVectorsFormat;
+import org.apache.lucene.codecs.KnnVectorsReader;
+import org.apache.lucene.codecs.KnnVectorsWriter;
+import org.apache.lucene.index.SegmentReadState;
+import org.apache.lucene.index.SegmentWriteState;
+
+/**
+ * Provides a Faiss-based vector format, see corresponding classes in {@code 
java21/} for docs!
+ *
+ * @lucene.experimental
+ */
+public class FaissKnnVectorsFormatProvider extends KnnVectorsFormat {
+  private final KnnVectorsFormat delegate;
+
+  public FaissKnnVectorsFormatProvider() {
+    this(lookup());
+  }
+
+  public FaissKnnVectorsFormatProvider(String description, String indexParams) 
{
+    this(lookup(description, indexParams));
+  }
+
+  private FaissKnnVectorsFormatProvider(KnnVectorsFormat delegate) {
+    super(delegate.getName());
+    this.delegate = delegate;
+  }
+
+  private static KnnVectorsFormat lookup(Object... args) {
+    try {
+      MethodHandles.Lookup lookup = MethodHandles.lookup();
+      Class<?> cls =
+          
lookup.findClass("org.apache.lucene.sandbox.codecs.faiss.FaissKnnVectorsFormat");
+
+      MethodType type =
+          MethodType.methodType(
+              void.class,
+              
Arrays.stream(args).map(Object::getClass).collect(Collectors.toUnmodifiableList()));
+      MethodHandle constr = lookup.findConstructor(cls, type);
+
+      return (KnnVectorsFormat) constr.invokeWithArguments(args);
+    } catch (ClassNotFoundException e) {
+      throw new LinkageError("FaissKnnVectorsFormat is missing from JAR file", 
e);
+    } catch (IllegalAccessException | NoSuchMethodException e) {
+      throw new LinkageError("FaissKnnVectorsFormat is missing correctly typed 
constructor", e);
+    } catch (Throwable t) {

Review Comment:
   I the original code we have a separate try/ctach around the invoke call, 
this is unsafe as it may catch too many exceptions from unrelated places.
   
   So please add a try-catch around only the call.
   
   An please use a correctly typed method handle..... This is too lazy as not 
statically typed.



##########
lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/faiss/FaissKnnVectorsFormatProvider.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.sandbox.codecs.faiss;
+
+import java.io.IOException;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.Arrays;
+import java.util.stream.Collectors;
+import org.apache.lucene.codecs.KnnVectorsFormat;
+import org.apache.lucene.codecs.KnnVectorsReader;
+import org.apache.lucene.codecs.KnnVectorsWriter;
+import org.apache.lucene.index.SegmentReadState;
+import org.apache.lucene.index.SegmentWriteState;
+
+/**
+ * Provides a Faiss-based vector format, see corresponding classes in {@code 
java21/} for docs!
+ *
+ * @lucene.experimental
+ */
+public class FaissKnnVectorsFormatProvider extends KnnVectorsFormat {
+  private final KnnVectorsFormat delegate;
+
+  public FaissKnnVectorsFormatProvider() {
+    this(lookup());
+  }
+
+  public FaissKnnVectorsFormatProvider(String description, String indexParams) 
{
+    this(lookup(description, indexParams));
+  }
+
+  private FaissKnnVectorsFormatProvider(KnnVectorsFormat delegate) {
+    super(delegate.getName());
+    this.delegate = delegate;
+  }
+
+  private static KnnVectorsFormat lookup(Object... args) {
+    try {
+      MethodHandles.Lookup lookup = MethodHandles.lookup();
+      Class<?> cls =
+          
lookup.findClass("org.apache.lucene.sandbox.codecs.faiss.FaissKnnVectorsFormat");
+
+      MethodType type =
+          MethodType.methodType(
+              void.class,
+              
Arrays.stream(args).map(Object::getClass).collect(Collectors.toUnmodifiableList()));
+      MethodHandle constr = lookup.findConstructor(cls, type);
+
+      return (KnnVectorsFormat) constr.invokeWithArguments(args);
+    } catch (ClassNotFoundException e) {
+      throw new LinkageError("FaissKnnVectorsFormat is missing from JAR file", 
e);
+    } catch (IllegalAccessException | NoSuchMethodException e) {
+      throw new LinkageError("FaissKnnVectorsFormat is missing correctly typed 
constructor", e);
+    } catch (Throwable t) {

Review Comment:
   In the original code we have a separate try/ctach around the invoke call, 
this is unsafe as it may catch too many exceptions from unrelated places.
   
   So please add a try-catch around only the call.
   
   An please use a correctly typed method handle..... This is too lazy as not 
statically typed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to