File size: 9,933 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package net.minecraft.util.eventlog;

import com.mojang.logging.LogUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.annotation.Nullable;
import org.slf4j.Logger;

public class EventLogDirectory {
    static final Logger LOGGER = LogUtils.getLogger();
    private static final int COMPRESS_BUFFER_SIZE = 4096;
    private static final String COMPRESSED_EXTENSION = ".gz";
    private final Path root;
    private final String extension;

    private EventLogDirectory(Path p_261546_, String p_261467_) {
        this.root = p_261546_;
        this.extension = p_261467_;
    }

    public static EventLogDirectory open(Path p_261743_, String p_261659_) throws IOException {
        Files.createDirectories(p_261743_);
        return new EventLogDirectory(p_261743_, p_261659_);
    }

    public EventLogDirectory.FileList listFiles() throws IOException {
        EventLogDirectory.FileList eventlogdirectory$filelist;
        try (Stream<Path> stream = Files.list(this.root)) {
            eventlogdirectory$filelist = new EventLogDirectory.FileList(
                stream.filter(p_262170_ -> Files.isRegularFile(p_262170_)).map(this::parseFile).filter(Objects::nonNull).toList()
            );
        }

        return eventlogdirectory$filelist;
    }

    @Nullable
    private EventLogDirectory.File parseFile(Path p_261985_) {
        String s = p_261985_.getFileName().toString();
        int i = s.indexOf(46);
        if (i == -1) {
            return null;
        } else {
            EventLogDirectory.FileId eventlogdirectory$fileid = EventLogDirectory.FileId.parse(s.substring(0, i));
            if (eventlogdirectory$fileid != null) {
                String s1 = s.substring(i);
                if (s1.equals(this.extension)) {
                    return new EventLogDirectory.RawFile(p_261985_, eventlogdirectory$fileid);
                }

                if (s1.equals(this.extension + ".gz")) {
                    return new EventLogDirectory.CompressedFile(p_261985_, eventlogdirectory$fileid);
                }
            }

            return null;
        }
    }

    static void tryCompress(Path p_261741_, Path p_262101_) throws IOException {
        if (Files.exists(p_262101_)) {
            throw new IOException("Compressed target file already exists: " + p_262101_);
        } else {
            try (FileChannel filechannel = FileChannel.open(p_261741_, StandardOpenOption.WRITE, StandardOpenOption.READ)) {
                FileLock filelock = filechannel.tryLock();
                if (filelock == null) {
                    throw new IOException("Raw log file is already locked, cannot compress: " + p_261741_);
                }

                writeCompressed(filechannel, p_262101_);
                filechannel.truncate(0L);
            }

            Files.delete(p_261741_);
        }
    }

    private static void writeCompressed(ReadableByteChannel p_262066_, Path p_262054_) throws IOException {
        try (OutputStream outputstream = new GZIPOutputStream(Files.newOutputStream(p_262054_))) {
            byte[] abyte = new byte[4096];
            ByteBuffer bytebuffer = ByteBuffer.wrap(abyte);

            while (p_262066_.read(bytebuffer) >= 0) {
                bytebuffer.flip();
                outputstream.write(abyte, 0, bytebuffer.limit());
                bytebuffer.clear();
            }
        }
    }

    public EventLogDirectory.RawFile createNewFile(LocalDate p_261865_) throws IOException {
        int i = 1;
        Set<EventLogDirectory.FileId> set = this.listFiles().ids();

        EventLogDirectory.FileId eventlogdirectory$fileid;
        do {
            eventlogdirectory$fileid = new EventLogDirectory.FileId(p_261865_, i++);
        } while (set.contains(eventlogdirectory$fileid));

        EventLogDirectory.RawFile eventlogdirectory$rawfile = new EventLogDirectory.RawFile(
            this.root.resolve(eventlogdirectory$fileid.toFileName(this.extension)), eventlogdirectory$fileid
        );
        Files.createFile(eventlogdirectory$rawfile.path());
        return eventlogdirectory$rawfile;
    }

    public static record CompressedFile(Path path, EventLogDirectory.FileId id) implements EventLogDirectory.File {
        @Nullable
        @Override
        public Reader openReader() throws IOException {
            return !Files.exists(this.path) ? null : new BufferedReader(new InputStreamReader(new GZIPInputStream(Files.newInputStream(this.path))));
        }

        @Override
        public EventLogDirectory.CompressedFile compress() {
            return this;
        }

        @Override
        public Path path() {
            return this.path;
        }

        @Override
        public EventLogDirectory.FileId id() {
            return this.id;
        }
    }

    public interface File {
        Path path();

        EventLogDirectory.FileId id();

        @Nullable
        Reader openReader() throws IOException;

        EventLogDirectory.CompressedFile compress() throws IOException;
    }

    public static record FileId(LocalDate date, int index) {
        private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.BASIC_ISO_DATE;

        @Nullable
        public static EventLogDirectory.FileId parse(String p_261762_) {
            int i = p_261762_.indexOf("-");
            if (i == -1) {
                return null;
            } else {
                String s = p_261762_.substring(0, i);
                String s1 = p_261762_.substring(i + 1);

                try {
                    return new EventLogDirectory.FileId(LocalDate.parse(s, DATE_FORMATTER), Integer.parseInt(s1));
                } catch (DateTimeParseException | NumberFormatException numberformatexception) {
                    return null;
                }
            }
        }

        @Override
        public String toString() {
            return DATE_FORMATTER.format(this.date) + "-" + this.index;
        }

        public String toFileName(String p_261982_) {
            return this + p_261982_;
        }
    }

    public static class FileList implements Iterable<EventLogDirectory.File> {
        private final List<EventLogDirectory.File> files;

        FileList(List<EventLogDirectory.File> p_261941_) {
            this.files = new ArrayList<>(p_261941_);
        }

        public EventLogDirectory.FileList prune(LocalDate p_261825_, int p_261918_) {
            this.files.removeIf(p_261494_ -> {
                EventLogDirectory.FileId eventlogdirectory$fileid = p_261494_.id();
                LocalDate localdate = eventlogdirectory$fileid.date().plusDays((long)p_261918_);
                if (!p_261825_.isBefore(localdate)) {
                    try {
                        Files.delete(p_261494_.path());
                        return true;
                    } catch (IOException ioexception) {
                        EventLogDirectory.LOGGER.warn("Failed to delete expired event log file: {}", p_261494_.path(), ioexception);
                    }
                }

                return false;
            });
            return this;
        }

        public EventLogDirectory.FileList compressAll() {
            ListIterator<EventLogDirectory.File> listiterator = this.files.listIterator();

            while (listiterator.hasNext()) {
                EventLogDirectory.File eventlogdirectory$file = listiterator.next();

                try {
                    listiterator.set(eventlogdirectory$file.compress());
                } catch (IOException ioexception) {
                    EventLogDirectory.LOGGER.warn("Failed to compress event log file: {}", eventlogdirectory$file.path(), ioexception);
                }
            }

            return this;
        }

        @Override
        public Iterator<EventLogDirectory.File> iterator() {
            return this.files.iterator();
        }

        public Stream<EventLogDirectory.File> stream() {
            return this.files.stream();
        }

        public Set<EventLogDirectory.FileId> ids() {
            return this.files.stream().map(EventLogDirectory.File::id).collect(Collectors.toSet());
        }
    }

    public static record RawFile(Path path, EventLogDirectory.FileId id) implements EventLogDirectory.File {
        public FileChannel openChannel() throws IOException {
            return FileChannel.open(this.path, StandardOpenOption.WRITE, StandardOpenOption.READ);
        }

        @Nullable
        @Override
        public Reader openReader() throws IOException {
            return Files.exists(this.path) ? Files.newBufferedReader(this.path) : null;
        }

        @Override
        public EventLogDirectory.CompressedFile compress() throws IOException {
            Path path = this.path.resolveSibling(this.path.getFileName().toString() + ".gz");
            EventLogDirectory.tryCompress(this.path, path);
            return new EventLogDirectory.CompressedFile(path, this.id);
        }

        @Override
        public Path path() {
            return this.path;
        }

        @Override
        public EventLogDirectory.FileId id() {
            return this.id;
        }
    }
}