rmuir commented on code in PR #13196:
URL: https://github.com/apache/lucene/pull/13196#discussion_r1535488782


##########
lucene/core/src/java21/org/apache/lucene/store/PosixNativeAccess.java:
##########
@@ -0,0 +1,123 @@
+/*
+ * 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.foreign.FunctionDescriptor;
+import java.lang.foreign.Linker;
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.SymbolLookup;
+import java.lang.foreign.ValueLayout;
+import java.lang.invoke.MethodHandle;
+import java.util.Locale;
+import java.util.logging.Logger;
+import org.apache.lucene.store.IOContext.Context;
+
+@SuppressWarnings("preview")
+final class PosixNativeAccess extends NativeAccess {
+
+  private static final Logger LOG = 
Logger.getLogger(PosixNativeAccess.class.getName());
+
+  // these constants were extracted from glibc and macos header files - 
luckily they are the same:
+
+  /** No further special treatment. */
+  public static final int POSIX_MADV_NORMAL = 0;
+
+  /** Expect random page references. */
+  public static final int POSIX_MADV_RANDOM = 1;
+
+  /** Expect sequential page references. */
+  public static final int POSIX_MADV_SEQUENTIAL = 2;
+
+  /** Will need these pages. */
+  public static final int POSIX_MADV_WILLNEED = 3;
+
+  /** Don't need these pages. */
+  public static final int POSIX_MADV_DONTNEED = 4;
+
+  private final MethodHandle mh$posix_madvise;
+
+  public PosixNativeAccess() {
+    final Linker linker = Linker.nativeLinker();
+    final SymbolLookup stdlib = linker.defaultLookup();
+    this.mh$posix_madvise =
+        findFunction(
+            linker,
+            stdlib,
+            "posix_madvise",
+            FunctionDescriptor.of(
+                ValueLayout.JAVA_INT,
+                ValueLayout.ADDRESS,
+                ValueLayout.JAVA_LONG,
+                ValueLayout.JAVA_INT));
+    LOG.info("posix_madvise() available on this platform");
+  }
+
+  private static MethodHandle findFunction(
+      Linker linker, SymbolLookup lookup, String name, FunctionDescriptor 
desc) {
+    final MemorySegment symbol =
+        lookup
+            .find(name)
+            .orElseThrow(
+                () ->
+                    new UnsupportedOperationException(
+                        "Platform has no symbol for '" + name + "' in libc."));
+    return linker.downcallHandle(symbol, desc);
+  }
+
+  @Override
+  public void madvise(MemorySegment segment, IOContext context) throws 
IOException {

Review Comment:
   i think this is really strange, that the "native access" takes IOContext and 
mixes in the "business" logic of mapping IOContext to native integer value. why 
not make the native access more transparent/straightforward?



-- 
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