Skip to content

Commit

Permalink
OffheapMap API enhancements; compatibility with newer JDKs
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Jul 25, 2021
1 parent c0afc97 commit af1cf93
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
21 changes: 16 additions & 5 deletions src/one/nio/mem/MappedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,17 @@ public static long map(RandomAccessFile f, int mode, long start, long size) thro
}

try {
Method map0 = Class.forName("sun.nio.ch.FileChannelImpl").getDeclaredMethod("map0", int.class, long.class, long.class);
map0.setAccessible(true);
return (Long) map0.invoke(f.getChannel(), mode, start, size);
Class<?> cls = Class.forName("sun.nio.ch.FileChannelImpl");
Method map0 = JavaInternals.getMethod(cls, "map0", int.class, long.class, long.class);
if (map0 != null) {
return (long) map0.invoke(f.getChannel(), mode, start, size);
}
// Newer JDK has an extra 'sync' argument
map0 = JavaInternals.getMethod(cls, "map0", int.class, long.class, long.class, boolean.class);
if (map0 != null) {
return (long) map0.invoke(f.getChannel(), mode, start, size, false);
}
throw new IllegalStateException("map0 method not found");
} catch (InvocationTargetException e) {
Throwable target = e.getTargetException();
throw (target instanceof IOException) ? (IOException) target : new IOException(target);
Expand All @@ -179,8 +187,11 @@ public static void unmap(long start, long size) {
}

try {
Method unmap0 = Class.forName("sun.nio.ch.FileChannelImpl").getDeclaredMethod("unmap0", long.class, long.class);
unmap0.setAccessible(true);
Class<?> cls = Class.forName("sun.nio.ch.FileChannelImpl");
Method unmap0 = JavaInternals.getMethod(cls, "unmap0", long.class, long.class);
if (unmap0 == null) {
throw new IllegalStateException("unmap0 method not found");
}
unmap0.invoke(null, start, size);
} catch (ReflectiveOperationException e) {
throw new IllegalStateException(e);
Expand Down
28 changes: 18 additions & 10 deletions src/one/nio/mem/OffheapMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,20 +431,24 @@ public void clear() {
}

public void iterate(Visitor<K, V> visitor) {
iterate(visitor, 0, 1);
iterate(visitor, 0, capacity, 1);
}

public void iterate(final Visitor<K, V> visitor, final int workers) {
AsyncExecutor.fork(workers, new ParallelTask() {
@Override
public void execute(int taskNum, int taskCount) {
iterate(visitor, taskNum, taskCount);
iterate(visitor, taskNum, capacity, taskCount);
}
});
}

public void iterate(Visitor<K, V> visitor, int taskNum, int taskCount) {
for (int i = taskNum; i < capacity; i += taskCount) {
public void iterate(Visitor<K, V> visitor, int start, int end, int step) {
if (start < 0 || end > capacity) {
throw new IndexOutOfBoundsException();
}

for (int i = start; i < end; i += step) {
long currentPtr = mapBase + (long) i * 8;
RWLock lock = locks[i & (CONCURRENCY_LEVEL - 1)].lockRead();
try {
Expand All @@ -458,20 +462,24 @@ public void iterate(Visitor<K, V> visitor, int taskNum, int taskCount) {
}

public void iterate(WritableVisitor<K, V> visitor) {
iterate(visitor, 0, 1);
iterate(visitor, 0, capacity, 1);
}

public void iterate(final WritableVisitor<K, V> visitor, final int workers) {
AsyncExecutor.fork(workers, new ParallelTask() {
@Override
public void execute(int taskNum, int taskCount) {
iterate(visitor, taskNum, taskCount);
iterate(visitor, taskNum, capacity, taskCount);
}
});
}

public void iterate(WritableVisitor<K, V> visitor, int taskNum, int taskCount) {
for (int i = taskNum; i < capacity; i += taskCount) {
public void iterate(WritableVisitor<K, V> visitor, int start, int end, int step) {
if (start < 0 || end > capacity) {
throw new IndexOutOfBoundsException();
}

for (int i = start; i < end; i += step) {
long currentPtr = mapBase + (long) i * 8;
RWLock lock = locks[i & (CONCURRENCY_LEVEL - 1)].lockWrite();
try {
Expand Down Expand Up @@ -543,7 +551,7 @@ public static class Record<K, V> {
protected final RWLock lock;
protected long entry;

protected Record(OffheapMap<K, V> map, RWLock lock, long entry) {
public Record(OffheapMap<K, V> map, RWLock lock, long entry) {
this.map = map;
this.lock = lock;
this.entry = entry;
Expand Down Expand Up @@ -591,7 +599,7 @@ public static class WritableRecord<K, V> extends Record<K, V> {
protected K key;
protected long currentPtr;

protected WritableRecord(OffheapMap<K, V> map, RWLock lock, long entry, K key, long currentPtr) {
public WritableRecord(OffheapMap<K, V> map, RWLock lock, long entry, K key, long currentPtr) {
super(map, lock, entry);
this.key = key;
this.currentPtr = currentPtr;
Expand Down
2 changes: 1 addition & 1 deletion src/one/nio/mem/SharedMemoryFixedMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected SharedMemoryFixedMap(String fileName, long fileSize, int valueSize, lo
this.allocator = createFixedSizeAllocator(getEntrySize(valueSize));
}

private static int getEntrySize(int valueSize) {
protected static int getEntrySize(int valueSize) {
return (valueSize + (HEADER_SIZE + 7)) & ~7; // align to 8-byte boundary
}

Expand Down

0 comments on commit af1cf93

Please sign in to comment.