/** * @return log segment files sorted based on their index. */ public List<LogPathAndIndex> getLogSegmentFiles() throws IOException { List<LogPathAndIndex> list = new ArrayList<>(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(getCurrentDir().toPath())) { for (Path path : stream) { for (Pattern pattern : Arrays.asList(CLOSED_SEGMENT_REGEX, OPEN_SEGMENT_REGEX)) { Matcher matcher = pattern.matcher(path.getFileName().toString()); if (matcher.matches()) { if (pattern == OPEN_SEGMENT_REGEX && Files.size(path) == 0L) { Files.delete(path); LOG.info("Delete zero size file " + path); break; } final long startIndex = Long.parseLong(matcher.group(1)); final long endIndex = matcher.groupCount() == 2 ? Long.parseLong(matcher.group(2)) : INVALID_LOG_INDEX; list.add(new LogPathAndIndex(path, startIndex, endIndex)); break; } } } } Collections.sort(list, (o1, o2) -> o1.startIndex == o2.startIndex ? 0 : (o1.startIndex < o2.startIndex ? -1 : 1)); return list; }
FileUtils.delete(path.getPath());
/** * @return log segment files sorted based on their index. */ public List<LogPathAndIndex> getLogSegmentFiles() throws IOException { List<LogPathAndIndex> list = new ArrayList<>(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(getCurrentDir().toPath())) { for (Path path : stream) { for (Pattern pattern : Arrays.asList(CLOSED_SEGMENT_REGEX, OPEN_SEGMENT_REGEX)) { Matcher matcher = pattern.matcher(path.getFileName().toString()); if (matcher.matches()) { if (pattern == OPEN_SEGMENT_REGEX && Files.size(path) == 0L) { Files.delete(path); LOG.info("Delete zero size file " + path); break; } final long startIndex = Long.parseLong(matcher.group(1)); final long endIndex = matcher.groupCount() == 2 ? Long.parseLong(matcher.group(2)) : INVALID_LOG_INDEX; list.add(new LogPathAndIndex(path, startIndex, endIndex)); break; } } } } list.sort(Comparator.comparingLong(o -> o.startIndex)); return list; }
void loadSegment(LogPathAndIndex pi, boolean keepEntryInCache, Consumer<LogEntryProto> logConsumer) throws IOException { LogSegment logSegment = LogSegment.loadSegment(storage, pi.getPath().toFile(), pi.startIndex, pi.endIndex, pi.isOpen(), keepEntryInCache, logConsumer); if (logSegment != null) { addSegment(logSegment); } }