3232import java .nio .file .Path ;
3333import java .nio .file .StandardOpenOption ;
3434
35+ import org .slf4j .Logger ;
36+ import org .slf4j .LoggerFactory ;
37+
3538/**
3639 * {@link MemorySegment} based implementation of RandomAccessReader. This is the recommended
3740 * RandomAccessReader implementation included with JVector.
4043 * of {@link SimpleMappedReader}.
4144 */
4245public class MemorySegmentReader implements RandomAccessReader {
46+ private static final Logger logger = LoggerFactory .getLogger (MemorySegmentReader .class );
4347
4448 private static final int MADV_RANDOM = 1 ; // Value for Linux
4549 private static final OfInt intLayout = ValueLayout .JAVA_INT_UNALIGNED .withOrder (ByteOrder .BIG_ENDIAN );
@@ -57,15 +61,24 @@ public MemorySegmentReader(Path path) throws IOException {
5761
5862 // Apply MADV_RANDOM advice
5963 var linker = Linker .nativeLinker ();
60- var madvise = linker .downcallHandle (linker .defaultLookup ().find ("posix_madvise" ).get (),
61- FunctionDescriptor .of (ValueLayout .JAVA_INT , ValueLayout .ADDRESS , ValueLayout .JAVA_LONG , ValueLayout .JAVA_INT ));
62- try {
63- int result = (int ) madvise .invokeExact (memory , memory .byteSize (), MADV_RANDOM );
64- if (result != 0 ) {
65- throw new IOException ("posix_madvise failed with error code: " + result );
64+ var maybeMadvise = linker .defaultLookup ().find ("posix_madvise" );
65+ if (maybeMadvise .isPresent ()) {
66+ var madvise = linker .downcallHandle (maybeMadvise .get (),
67+ FunctionDescriptor .of (ValueLayout .JAVA_INT , ValueLayout .ADDRESS , ValueLayout .JAVA_LONG , ValueLayout .JAVA_INT ));
68+ try
69+ {
70+ int result = (int ) madvise .invokeExact (memory , memory .byteSize (), MADV_RANDOM );
71+ if (result != 0 )
72+ {
73+ throw new IOException ("posix_madvise failed with error code: " + result );
74+ }
75+ }
76+ catch (Throwable t )
77+ {
78+ throw new RuntimeException (t );
6679 }
67- } catch ( Throwable t ) {
68- throw new RuntimeException ( t );
80+ } else {
81+ logger . warn ( "posix_madvise not found, MADV_RANDOM advice not applied" );
6982 }
7083 } catch (Exception e ) {
7184 arena .close ();
0 commit comments