| 1 | package net.minecraft.util.eventlog;
|
| 2 |
|
| 3 | import com.google.gson.Gson;
|
| 4 | import com.google.gson.JsonElement;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.JsonOps;
|
| 7 | import java.io.Closeable;
|
| 8 | import java.io.IOException;
|
| 9 | import java.io.Writer;
|
| 10 | import java.nio.channels.Channels;
|
| 11 | import java.nio.channels.FileChannel;
|
| 12 | import java.nio.charset.StandardCharsets;
|
| 13 | import java.nio.file.Path;
|
| 14 | import java.nio.file.StandardOpenOption;
|
| 15 | import java.util.concurrent.atomic.AtomicInteger;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | public class JsonEventLog<T> implements Closeable {
|
| 19 | private static final Gson GSON = new Gson();
|
| 20 | private final Codec<T> codec;
|
| 21 | private final FileChannel channel;
|
| 22 | private final AtomicInteger referenceCount = new AtomicInteger(1);
|
| 23 |
|
| 24 | public JsonEventLog(final Codec<T> codec, final FileChannel channel) {
|
| 25 | this.codec = codec;
|
| 26 | this.channel = channel;
|
| 27 | }
|
| 28 |
|
| 29 | public static <T> JsonEventLog<T> open(final Codec<T> codec, final Path path) throws IOException {
|
| 30 | FileChannel channel = FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);
|
| 31 | return new JsonEventLog<>(codec, channel);
|
| 32 | }
|
| 33 |
|
| 34 | public void write(final T event) throws IOException {
|
| 35 | JsonElement json = this.codec.encodeStart(JsonOps.INSTANCE, event).getOrThrow(IOException::new);
|
| 36 | this.channel.position(this.channel.size());
|
| 37 | Writer writer = Channels.newWriter(this.channel, StandardCharsets.UTF_8);
|
| 38 | GSON.toJson(json, GSON.newJsonWriter(writer));
|
| 39 | writer.write(10);
|
| 40 | writer.flush();
|
| 41 | }
|
| 42 |
|
| 43 | public JsonEventLogReader<T> openReader() throws IOException {
|
| 44 | if (this.referenceCount.get() <= 0) {
|
| 45 | throw new IOException("Event log has already been closed");
|
| 46 | }
|
| 47 |
|
| 48 | this.referenceCount.incrementAndGet();
|
| 49 | final JsonEventLogReader<T> reader = JsonEventLogReader.create(this.codec, Channels.newReader(this.channel, StandardCharsets.UTF_8));
|
| 50 | return new JsonEventLogReader<T>() {
|
| 51 | private volatile long position;
|
| 52 |
|
| 53 | @Override
|
| 54 | public @Nullable T next() throws IOException {
|
| 55 | try {
|
| 56 | JsonEventLog.this.channel.position(this.position);
|
| 57 | return reader.next();
|
| 58 | } finally {
|
| 59 | this.position = JsonEventLog.this.channel.position();
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | public void close() throws IOException {
|
| 65 | JsonEventLog.this.releaseReference();
|
| 66 | }
|
| 67 | };
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public void close() throws IOException {
|
| 72 | this.releaseReference();
|
| 73 | }
|
| 74 |
|
| 75 | private void releaseReference() throws IOException {
|
| 76 | if (this.referenceCount.decrementAndGet() <= 0) {
|
| 77 | this.channel.close();
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 |
|