Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc178d6c

Browse files
committed
Minimal changes required to implement Foreign Function and Memory API
1 parent154df33 commitc178d6c

25 files changed

+2431
-925
lines changed

‎pom.xml‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
<description>Low latency Java API for the ultra-fast, embedded Symas Lightning Database (LMDB)</description>
2727
<properties>
2828
<maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss'Z'</maven.build.timestamp.format>
29-
<maven.compiler.source>1.8</maven.compiler.source>
30-
<maven.compiler.target>1.8</maven.compiler.target>
31-
<maven.enforcer.java>1.8</maven.enforcer.java>
29+
<maven.compiler.source>1.25</maven.compiler.source>
30+
<maven.compiler.target>1.25</maven.compiler.target>
31+
<maven.enforcer.java>1.25</maven.enforcer.java>
3232
<maven.enforcer.mvn>3.5.4</maven.enforcer.mvn>
3333
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3434
<trimStackTrace>false</trimStackTrace>
@@ -39,11 +39,6 @@
3939
<artifactId>jnr-constants</artifactId>
4040
<version>0.10.4</version>
4141
</dependency>
42-
<dependency>
43-
<groupId>com.github.jnr</groupId>
44-
<artifactId>jnr-ffi</artifactId>
45-
<version>2.2.17</version>
46-
</dependency>
4742
<dependency>
4843
<groupId>com.google.guava</groupId>
4944
<artifactId>guava</artifactId>
@@ -89,8 +84,8 @@
8984
</dependency>
9085
<dependency>
9186
<groupId>org.mockito</groupId>
92-
<artifactId>mockito-inline</artifactId>
93-
<version>4.11.0</version>
87+
<artifactId>mockito-core</artifactId>
88+
<version>5.20.0</version>
9489
<scope>test</scope>
9590
</dependency>
9691
</dependencies>
@@ -218,6 +213,7 @@
218213
</manifest>
219214
<manifestEntries>
220215
<Implementation-Build>${buildNumber}</Implementation-Build>
216+
<Enable-Native-Access>true</Enable-Native-Access>
221217
</manifestEntries>
222218
</archive>
223219
</configuration>

‎src/main/java/org/lmdbjava/BufferProxy.java‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
*/
1616
packageorg.lmdbjava;
1717

18+
importorg.lmdbjava.Lmdb.MDB_val;
19+
1820
importstaticjava.lang.Long.BYTES;
1921
importstaticorg.lmdbjava.DbiFlags.MDB_INTEGERKEY;
2022
importstaticorg.lmdbjava.DbiFlags.MDB_UNSIGNEDKEY;
2123
importstaticorg.lmdbjava.MaskedFlag.isSet;
2224
importstaticorg.lmdbjava.MaskedFlag.mask;
2325

26+
importjava.lang.foreign.Arena;
2427
importjava.util.Comparator;
25-
importjnr.ffi.Pointer;
2628

2729
/**
2830
* The strategy for mapping memory address to a given buffer type.
@@ -107,37 +109,33 @@ protected Comparator<T> getComparator(DbiFlags... flags) {
107109
*
108110
* @param buffer the buffer to write to <code>MDB_val</code>
109111
* @param ptr the pointer to the <code>MDB_val</code>
110-
* @param ptrAddr the address of the <code>MDB_val</code> pointer
111112
*/
112-
protectedabstractvoidin(Tbuffer,Pointerptr,longptrAddr);
113+
protectedabstractvoidin(Tbuffer,MDB_valptr);
113114

114115
/**
115116
* Called when the <code>MDB_val</code> should be set to reflect the passed buffer.
116117
*
117118
* @param buffer the buffer to write to <code>MDB_val</code>
118119
* @param size the buffer size to write to <code>MDB_val</code>
119120
* @param ptr the pointer to the <code>MDB_val</code>
120-
* @param ptrAddr the address of the <code>MDB_val</code> pointer
121121
*/
122-
protectedabstractvoidin(Tbuffer,intsize,Pointerptr,longptrAddr);
122+
protectedabstractvoidin(Tbuffer,intsize,MDB_valptr);
123123

124124
/**
125125
* Called when the <code>MDB_val</code> may have changed and the passed buffer should be modified
126126
* to reflect the new <code>MDB_val</code>.
127127
*
128-
* @param buffer the buffer to write to <code>MDB_val</code>
129128
* @param ptr the pointer to the <code>MDB_val</code>
130-
* @param ptrAddr the address of the <code>MDB_val</code> pointer
131129
* @return the buffer for <code>MDB_val</code>
132130
*/
133-
protectedabstractTout(Tbuffer,Pointerptr,longptrAddr);
131+
protectedabstractTout(MDB_valptr);
134132

135133
/**
136134
* Create a new {@link KeyVal} to hold pointers for this buffer proxy.
137135
*
138136
* @return a non-null key value holder
139137
*/
140-
finalKeyVal<T>keyVal() {
141-
returnnewKeyVal<>(this);
138+
finalKeyVal<T>keyVal(finalArenaarena) {
139+
returnnewKeyVal<>(arena,this);
142140
}
143141
}

‎src/main/java/org/lmdbjava/ByteArrayProxy.java‎

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
*/
1616
packageorg.lmdbjava;
1717

18-
importstaticjava.lang.Math.min;
19-
importstaticjava.util.Objects.requireNonNull;
20-
importstaticorg.lmdbjava.Library.RUNTIME;
18+
importorg.lmdbjava.Lmdb.MDB_val;
2119

20+
importjava.lang.foreign.Arena;
21+
importjava.lang.foreign.MemorySegment;
22+
importjava.lang.foreign.ValueLayout;
23+
importjava.nio.ByteBuffer;
2224
importjava.util.Arrays;
2325
importjava.util.Comparator;
24-
importjnr.ffi.Pointer;
25-
importjnr.ffi.provider.MemoryManager;
26+
27+
importstaticjava.lang.Math.min;
28+
importstaticjava.util.Objects.requireNonNull;
2629

2730
/**
2831
* Byte array proxy.
@@ -31,15 +34,14 @@
3134
*/
3235
publicfinalclassByteArrayProxyextendsBufferProxy<byte[]> {
3336

34-
/** The byte array proxy. Guaranteed to never be null. */
35-
publicstaticfinalBufferProxy<byte[]>PROXY_BA =newByteArrayProxy();
36-
37-
privatestaticfinalMemoryManagerMEM_MGR =RUNTIME.getMemoryManager();
37+
privatefinalArenaarena;
3838

3939
privatestaticfinalComparator<byte[]>signedComparator =ByteArrayProxy::compareArraysSigned;
4040
privatestaticfinalComparator<byte[]>unsignedComparator =ByteArrayProxy::compareArrays;
4141

42-
privateByteArrayProxy() {}
42+
publicByteArrayProxy(finalArenaarena) {
43+
this.arena =arena;
44+
}
4345

4446
/**
4547
* Lexicographically compare two byte arrays.
@@ -114,25 +116,22 @@ protected Comparator<byte[]> getUnsignedComparator() {
114116
}
115117

116118
@Override
117-
protectedvoidin(finalbyte[]buffer,finalPointerptr,finallongptrAddr) {
118-
finalPointerpointer =MEM_MGR.allocateDirect(buffer.length);
119-
pointer.put(0,buffer,0,buffer.length);
120-
ptr.putLong(STRUCT_FIELD_OFFSET_SIZE,buffer.length);
121-
ptr.putAddress(STRUCT_FIELD_OFFSET_DATA,pointer.address());
119+
protectedvoidin(finalbyte[]buffer,finalMDB_valptr) {
120+
finalMemorySegmentsegment =arena.allocateFrom(ValueLayout.JAVA_BYTE,buffer);
121+
ptr.mvSize(buffer.length);
122+
ptr.mvData(segment);
122123
}
123124

124125
@Override
125-
protectedvoidin(finalbyte[]buffer,finalintsize,finalPointerptr,finallongptrAddr) {
126+
protectedvoidin(finalbyte[]buffer,finalintsize,finalMDB_valptr) {
126127
// cannot reserve for byte arrays
127128
}
128129

129130
@Override
130-
protectedbyte[]out(finalbyte[]buffer,finalPointerptr,finallongptrAddr) {
131-
finallongaddr =ptr.getAddress(STRUCT_FIELD_OFFSET_DATA);
132-
finalintsize = (int)ptr.getLong(STRUCT_FIELD_OFFSET_SIZE);
133-
finalPointerpointer =MEM_MGR.newPointer(addr,size);
134-
finalbyte[]bytes =newbyte[size];
135-
pointer.get(0,bytes,0,size);
131+
protectedbyte[]out(finalMDB_valptr) {
132+
finalByteBufferbyteBuffer =ptr.mvData().reinterpret(ptr.mvSize()).asByteBuffer();
133+
finalbyte[]bytes =newbyte[byteBuffer.remaining()];
134+
byteBuffer.get(0,bytes,0,byteBuffer.remaining());
136135
returnbytes;
137136
}
138137
}

‎src/main/java/org/lmdbjava/ByteBufProxy.java‎

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,22 @@
1515
*/
1616
packageorg.lmdbjava;
1717

18-
importstaticio.netty.buffer.PooledByteBufAllocator.DEFAULT;
19-
importstaticjava.lang.Class.forName;
20-
importstaticjava.util.Objects.requireNonNull;
21-
importstaticorg.lmdbjava.UnsafeAccess.UNSAFE;
22-
2318
importio.netty.buffer.ByteBuf;
2419
importio.netty.buffer.PooledByteBufAllocator;
25-
importjava.lang.reflect.Field;
20+
importio.netty.buffer.Unpooled;
21+
importorg.lmdbjava.Lmdb.MDB_val;
22+
23+
importjava.lang.foreign.MemorySegment;
24+
importjava.nio.ByteBuffer;
2625
importjava.util.Comparator;
27-
importjnr.ffi.Pointer;
26+
27+
importstaticio.netty.buffer.PooledByteBufAllocator.DEFAULT;
28+
importstaticjava.util.Objects.requireNonNull;
2829

2930
/**
3031
* A buffer proxy backed by Netty's {@link ByteBuf}.
3132
*
32-
* <p>This class requires{@link UnsafeAccess} andnetty-buffer must be in the classpath.
33+
* <p>This class requires netty-buffer in the classpath.
3334
*/
3435
publicfinalclassByteBufProxyextendsBufferProxy<ByteBuf> {
3536

@@ -41,18 +42,14 @@ public final class ByteBufProxy extends BufferProxy<ByteBuf> {
4142
publicstaticfinalBufferProxy<ByteBuf>PROXY_NETTY =newByteBufProxy();
4243

4344
privatestaticfinalintBUFFER_RETRIES =10;
44-
privatestaticfinalStringFIELD_NAME_ADDRESS ="memoryAddress";
45-
privatestaticfinalStringFIELD_NAME_LENGTH ="length";
4645
privatestaticfinalStringNAME ="io.netty.buffer.PooledUnsafeDirectByteBuf";
4746
privatestaticfinalComparator<ByteBuf>comparator =
48-
(o1,o2) -> {
49-
requireNonNull(o1);
50-
requireNonNull(o2);
47+
(o1,o2) -> {
48+
requireNonNull(o1);
49+
requireNonNull(o2);
5150

52-
returno1.compareTo(o2);
53-
};
54-
privatefinallonglengthOffset;
55-
privatefinallongaddressOffset;
51+
returno1.compareTo(o2);
52+
};
5653

5754
privatefinalPooledByteBufAllocatornettyAllocator;
5855

@@ -68,36 +65,6 @@ private ByteBufProxy() {
6865
publicByteBufProxy(finalPooledByteBufAllocatorallocator) {
6966
super();
7067
this.nettyAllocator =allocator;
71-
72-
try {
73-
finalByteBufinitBuf =this.allocate();
74-
initBuf.release();
75-
finalFieldaddress =findField(NAME,FIELD_NAME_ADDRESS);
76-
finalFieldlength =findField(NAME,FIELD_NAME_LENGTH);
77-
addressOffset =UNSAFE.objectFieldOffset(address);
78-
lengthOffset =UNSAFE.objectFieldOffset(length);
79-
}catch (finalSecurityExceptione) {
80-
thrownewLmdbException("Field access error",e);
81-
}
82-
}
83-
84-
staticFieldfindField(finalStringc,finalStringname) {
85-
Class<?>clazz;
86-
try {
87-
clazz =forName(c);
88-
}catch (finalClassNotFoundExceptione) {
89-
thrownewLmdbException(c +" class unavailable",e);
90-
}
91-
do {
92-
try {
93-
finalFieldfield =clazz.getDeclaredField(name);
94-
field.setAccessible(true);
95-
returnfield;
96-
}catch (finalNoSuchFieldExceptione) {
97-
clazz =clazz.getSuperclass();
98-
}
99-
}while (clazz !=null);
100-
thrownewLmdbException(name +" not found");
10168
}
10269

10370
@Override
@@ -136,26 +103,21 @@ protected byte[] getBytes(final ByteBuf buffer) {
136103
}
137104

138105
@Override
139-
protectedvoidin(finalByteBufbuffer,finalPointerptr,finallongptrAddr) {
140-
UNSAFE.putLong(ptrAddr +STRUCT_FIELD_OFFSET_SIZE,buffer.writerIndex() -buffer.readerIndex());
141-
UNSAFE.putLong(
142-
ptrAddr +STRUCT_FIELD_OFFSET_DATA,buffer.memoryAddress() +buffer.readerIndex());
106+
protectedvoidin(finalByteBufbuffer,finalMDB_valptr) {
107+
ptr.mvSize(buffer.writerIndex() -buffer.readerIndex());
108+
ptr.mvData(MemorySegment.ofBuffer(buffer.nioBuffer()));
143109
}
144110

145111
@Override
146-
protectedvoidin(finalByteBufbuffer,finalintsize,finalPointerptr,finallongptrAddr) {
147-
UNSAFE.putLong(ptrAddr +STRUCT_FIELD_OFFSET_SIZE,size);
148-
UNSAFE.putLong(
149-
ptrAddr +STRUCT_FIELD_OFFSET_DATA,buffer.memoryAddress() +buffer.readerIndex());
112+
protectedvoidin(finalByteBufbuffer,finalintsize,finalMDB_valptr) {
113+
ptr.mvSize(size);
114+
ptr.mvData(MemorySegment.ofBuffer(buffer.nioBuffer()));
150115
}
151116

152117
@Override
153-
protectedByteBufout(finalByteBufbuffer,finalPointerptr,finallongptrAddr) {
154-
finallongaddr =UNSAFE.getLong(ptrAddr +STRUCT_FIELD_OFFSET_DATA);
155-
finallongsize =UNSAFE.getLong(ptrAddr +STRUCT_FIELD_OFFSET_SIZE);
156-
UNSAFE.putLong(buffer,addressOffset,addr);
157-
UNSAFE.putInt(buffer,lengthOffset, (int)size);
158-
buffer.clear().writerIndex((int)size);
159-
returnbuffer;
118+
protectedByteBufout(finalMDB_valptr) {
119+
finallongsize =ptr.mvSize();
120+
finalByteBufferbyteBuffer =ptr.mvData().reinterpret(size).asByteBuffer();
121+
returnUnpooled.wrappedBuffer(byteBuffer);
160122
}
161123
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp