From 2bed1d4d517a0ffeea3c2fef69a10872eb63a2fd Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Tue, 22 Mar 2022 19:40:15 +0100 Subject: [PATCH] Remove unsupported code --- .../disk/LLLocalKeyValueDatabase.java | 6 - .../database/disk/MemorySegmentUtils.java | 129 ------------------ .../it/cavallium/dbengine/DbTestUtils.java | 39 ++---- 3 files changed, 12 insertions(+), 162 deletions(-) delete mode 100644 src/main/java/it/cavallium/dbengine/database/disk/MemorySegmentUtils.java diff --git a/src/main/java/it/cavallium/dbengine/database/disk/LLLocalKeyValueDatabase.java b/src/main/java/it/cavallium/dbengine/database/disk/LLLocalKeyValueDatabase.java index c336d2f..1847946 100644 --- a/src/main/java/it/cavallium/dbengine/database/disk/LLLocalKeyValueDatabase.java +++ b/src/main/java/it/cavallium/dbengine/database/disk/LLLocalKeyValueDatabase.java @@ -133,12 +133,6 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase { PlatformDependent.getUnsafeUnavailabilityCause() ); } - if (!MemorySegmentUtils.isSupported()) { - throw new UnsupportedOperationException("Foreign Memory Access API support is disabled." - + " Please set \"" + MemorySegmentUtils.getSuggestedArgs() + "\"", - MemorySegmentUtils.getUnsupportedCause() - ); - } } OptionsWithCache optionsWithCache = openRocksDb(path, databaseOptions); diff --git a/src/main/java/it/cavallium/dbengine/database/disk/MemorySegmentUtils.java b/src/main/java/it/cavallium/dbengine/database/disk/MemorySegmentUtils.java deleted file mode 100644 index f12e8f7..0000000 --- a/src/main/java/it/cavallium/dbengine/database/disk/MemorySegmentUtils.java +++ /dev/null @@ -1,129 +0,0 @@ -package it.cavallium.dbengine.database.disk; - -import io.netty5.util.internal.PlatformDependent; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodHandles.Lookup; -import java.lang.invoke.MethodType; -import java.nio.ByteBuffer; - -public class MemorySegmentUtils { - - private static final MethodHandle OF_NATIVE_RESTRICTED; - private static final MethodHandle AS_SLICE; - private static final MethodHandle AS_BYTE_BUFFER; - private static Throwable cause; - - private static final Object NATIVE; - - static { - Lookup lookup = MethodHandles.lookup(); - - Object nativeVal = null; - - var ofNativeRestricted = getJava16NativeRestricted(lookup); - if (ofNativeRestricted == null) { - cause = null; - ofNativeRestricted = getJava17NativeRestricted(lookup); - } - if (ofNativeRestricted != null) { - try { - nativeVal = ofNativeRestricted.invoke(); - } catch (Throwable e) { - cause = e; - } - } - OF_NATIVE_RESTRICTED = ofNativeRestricted; - - MethodHandle asSlice; - try { - asSlice = lookup.findVirtual(lookup.findClass("jdk.incubator.foreign.MemorySegment"), - "asSlice", - MethodType.methodType(lookup.findClass("jdk.incubator.foreign.MemorySegment"), long.class, long.class) - ); - } catch (NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) { - asSlice = null; - cause = e; - } - AS_SLICE = asSlice; - - MethodHandle asByteBuffer; - try { - asByteBuffer = lookup.findVirtual(lookup.findClass("jdk.incubator.foreign.MemorySegment"), - "asByteBuffer", MethodType.methodType(ByteBuffer.class)); - } catch (NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) { - asByteBuffer = null; - cause = e; - } - AS_BYTE_BUFFER = asByteBuffer; - - NATIVE = nativeVal; - } - - @SuppressWarnings("JavaLangInvokeHandleSignature") - private static MethodHandle getJava16NativeRestricted(Lookup lookup) { - MethodHandle ofNativeRestricted; - try { - ofNativeRestricted = lookup.findStatic(lookup.findClass("jdk.incubator.foreign.MemorySegment"), - "ofNativeRestricted", - MethodType.methodType(lookup.findClass("jdk.incubator.foreign.MemorySegment")) - ); - } catch (NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) { - ofNativeRestricted = null; - cause = e; - } - return ofNativeRestricted; - } - - @SuppressWarnings("JavaLangInvokeHandleSignature") - private static MethodHandle getJava17NativeRestricted(Lookup lookup) { - MethodHandle ofNativeRestricted; - try { - ofNativeRestricted = lookup.findStatic(lookup.findClass("jdk.incubator.foreign.MemorySegment"), - "globalNativeSegment", - MethodType.methodType(lookup.findClass("jdk.incubator.foreign.MemorySegment")) - ); - } catch (NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) { - ofNativeRestricted = null; - cause = e; - } - return ofNativeRestricted; - } - - public static ByteBuffer directBuffer(long address, long size) { - if (address <= 0) { - throw new IllegalArgumentException("Address is " + address); - } - if (size > Integer.MAX_VALUE || size < 0) { - throw new IllegalArgumentException("size is " + size); - } - try { - if (!isSupported()) { - if (PlatformDependent.hasDirectBufferNoCleanerConstructor()) { - return PlatformDependent.directBuffer(address, (int) size, null); - } - throw new UnsupportedOperationException("Foreign Memory Access API is disabled!" - + " Please set \"" + MemorySegmentUtils.getSuggestedArgs() + "\"", - getUnsupportedCause() - ); - } - var memorySegment = AS_SLICE.invoke(NATIVE, address, size); - return (ByteBuffer) AS_BYTE_BUFFER.invoke(memorySegment); - } catch (Throwable e) { - throw new UnsupportedOperationException("Foreign Memory Access API is disabled!" - + " Please set \"" + MemorySegmentUtils.getSuggestedArgs() + "\"", e); - } - } - - public static boolean isSupported() { - return OF_NATIVE_RESTRICTED != null && AS_SLICE != null && AS_BYTE_BUFFER != null && NATIVE != null; - } - - public static Throwable getUnsupportedCause() { - return cause; - } - - public static String getSuggestedArgs() { - return "--enable-preview --add-modules jdk.incubator.foreign -Dforeign.restricted=permit --enable-native-access=ALL-UNNAMED"; - } -} diff --git a/src/test/java/it/cavallium/dbengine/DbTestUtils.java b/src/test/java/it/cavallium/dbengine/DbTestUtils.java index c4e5d52..6a12df4 100644 --- a/src/test/java/it/cavallium/dbengine/DbTestUtils.java +++ b/src/test/java/it/cavallium/dbengine/DbTestUtils.java @@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import io.netty5.buffer.api.Buffer; import io.netty5.buffer.api.MemoryManager; -import io.netty5.buffer.api.Send; import io.netty5.buffer.api.pool.MetricUtils; import io.netty5.buffer.api.pool.PoolArenaMetric; import io.netty5.buffer.api.pool.PooledBufferAllocator; @@ -23,17 +22,14 @@ import it.cavallium.dbengine.database.collections.DatabaseStageEntry; import it.cavallium.dbengine.database.collections.DatabaseStageMap; import it.cavallium.dbengine.database.collections.SubStageGetterHashMap; import it.cavallium.dbengine.database.collections.SubStageGetterMap; -import it.cavallium.dbengine.database.disk.MemorySegmentUtils; import it.cavallium.dbengine.database.serialization.SerializationException; import it.cavallium.dbengine.database.serialization.Serializer; import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength; import it.unimi.dsi.fastutil.objects.Object2ObjectSortedMap; import java.nio.file.Path; -import java.util.Map; import java.util.Objects; import java.util.function.Function; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -47,7 +43,7 @@ public class DbTestUtils { return "0123456789".repeat(1024); } - public static record TestAllocator(PooledBufferAllocator allocator) {} + public record TestAllocator(PooledBufferAllocator allocator) {} public static TestAllocator newAllocator() { return new TestAllocator(new PooledBufferAllocator(MemoryManager.instance(), true, 1, 8192, 9, 0, 0, true)); @@ -85,18 +81,18 @@ public class DbTestUtils { Function> action) { return Flux.usingWhen( temporaryDbGenerator.openTempDb(alloc), - tempDb -> Flux.from(action.apply(tempDb.db())).doOnDiscard(Object.class, o -> { - System.out.println("Discarded: " + o.getClass().getName() + ", " + o); - }), + tempDb -> Flux + .from(action.apply(tempDb.db())) + .doOnDiscard(Object.class, o -> System.out.println("Discarded: " + o.getClass().getName() + ", " + o)), temporaryDbGenerator::closeTempDb ); } - public static record TempDb(TestAllocator allocator, LLDatabaseConnection connection, LLKeyValueDatabase db, - LLLuceneIndex luceneSingle, - LLLuceneIndex luceneMulti, - SwappableLuceneSearcher swappableLuceneSearcher, - Path path) {} + public record TempDb(TestAllocator allocator, LLDatabaseConnection connection, LLKeyValueDatabase db, + LLLuceneIndex luceneSingle, + LLLuceneIndex luceneMulti, + SwappableLuceneSearcher swappableLuceneSearcher, + Path path) {} static boolean computeCanUseNettyDirect() { boolean canUse = true; @@ -105,16 +101,6 @@ public class DbTestUtils { + " Netty direct buffers will not be used in tests!"); canUse = false; } - if (!MemorySegmentUtils.isSupported()) { - System.err.println("Warning! Foreign Memory Access API is not available!" - + " Netty direct buffers will not be used in tests!" - + " Please set \"" + MemorySegmentUtils.getSuggestedArgs() + "\""); - if (MemorySegmentUtils.getUnsupportedCause() != null) { - System.err.println("\tCause: " + MemorySegmentUtils.getUnsupportedCause().getClass().getName() - + ":" + MemorySegmentUtils.getUnsupportedCause().getLocalizedMessage()); - } - canUse = false; - } return canUse; } @@ -173,14 +159,13 @@ public class DbTestUtils { } @Override - public @NotNull Short deserialize(@NotNull Buffer serialized) throws SerializationException { + public @NotNull Short deserialize(@NotNull Buffer serialized) { Objects.requireNonNull(serialized); - var val = serialized.readShort(); - return val; + return serialized.readShort(); } @Override - public void serialize(@NotNull Short deserialized, Buffer output) throws SerializationException { + public void serialize(@NotNull Short deserialized, Buffer output) { output.writeShort(deserialized); } },