memorysegments per-file scope

This commit is contained in:
Andrea Cavalli 2023-10-20 15:23:03 +02:00
parent fe33243258
commit 67402c3ef6
3 changed files with 12 additions and 11 deletions

View File

@ -4,7 +4,7 @@
<groupId>it.cavallium</groupId>
<artifactId>filequeue</artifactId>
<name>file queue project</name>
<version>3.1.8</version>
<version>3.1.9</version>
<packaging>jar</packaging>
<description>Light weight, high performance, simple, reliable and persistent queue</description>
<modelVersion>4.0.0</modelVersion>

View File

@ -24,7 +24,6 @@ public class SimpleQueueMemorySegment<T> implements SimpleQueue<T>, Closeable {
private final String name;
private final Serializer<T> serializer;
private final Deserializer<T> deserializer;
private final Arena arena;
private final long segmentSize;
public SimpleQueueMemorySegment(Path directory,
@ -32,7 +31,6 @@ public class SimpleQueueMemorySegment<T> implements SimpleQueue<T>, Closeable {
Serializer<T> serializer,
Deserializer<T> deserializer,
long segmentSize) throws IOException {
this.arena = Arena.ofShared();
this.name = name;
this.directory = directory;
this.serializer = serializer;
@ -74,7 +72,7 @@ public class SimpleQueueMemorySegment<T> implements SimpleQueue<T>, Closeable {
private SimpleQueueMemorySegmentFixedSize expandQueueSegmentForWrite() throws IOException {
synchronized (queueSegments) {
var queueMemorySegment = new SimpleQueueMemorySegmentFixedSize(arena, segmentSize, generateQueuePath());
var queueMemorySegment = new SimpleQueueMemorySegmentFixedSize(segmentSize, generateQueuePath());
// Hibernate the middle segments, if there will be 3 or more segments after the expansion
if (queueSegments.size() >= 2) {
queueSegments.getLast().hibernate();
@ -157,7 +155,6 @@ public class SimpleQueueMemorySegment<T> implements SimpleQueue<T>, Closeable {
e.printStackTrace();
}
});
this.arena.close();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {

View File

@ -18,26 +18,30 @@ public class SimpleQueueMemorySegmentFixedSize implements Closeable {
private final Path filePath;
private final FileChannel ch;
private final MemorySegment writer;
private final Arena arena;
private final Arena mmapArena;
private final long fixedSize;
private long readPosition = 0;
private long writePosition = 0;
private final AtomicInteger size = new AtomicInteger();
public SimpleQueueMemorySegmentFixedSize(Arena arena, long fixedSize, Path filePath) throws IOException {
this.arena = arena;
public SimpleQueueMemorySegmentFixedSize(long fixedSize, Path filePath) throws IOException {
this.mmapArena = Arena.ofShared();
this.filePath = filePath;
this.fixedSize = fixedSize;
Files.deleteIfExists(filePath);
this.ch = FileChannel.open(filePath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE, StandardOpenOption.READ);
ch.truncate(fixedSize);
this.writer = ch.map(MapMode.READ_WRITE, 0L, fixedSize, arena);
this.writer = ch.map(MapMode.READ_WRITE, 0L, fixedSize, mmapArena);
}
@Override
public void close() throws IOException {
ch.close();
Files.deleteIfExists(filePath);
try {
ch.close();
Files.deleteIfExists(filePath);
} finally {
mmapArena.close();
}
}
public void add(MemorySegment element) {