Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update file coll #889

Merged
merged 6 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public void readLine() throws IOException {
readResult.setFilePathName(file);
readResult.setLineNumber(++lineNumber);
ReadEvent event = new ReadEvent(readResult);
listener.setReadTime();

listener.onEvent(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ default void setPointer(Object obj) {
default void saveProgress() {
}

default void setReadTime() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@ public void setPointer(Object obj) {
}
}
}

@Override
public void setReadTime() {
HeraFile f = monitor.getFileMap().get(logFile.getFileKey());
if (null != f) {
f.getReadTime().set(System.currentTimeMillis());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ public class HeraFile {

@Builder.Default
private AtomicLong utime = new AtomicLong(System.currentTimeMillis());

@Builder.Default
private AtomicLong readTime = new AtomicLong(System.currentTimeMillis());
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class HeraFileMonitor {
@Getter
private ConcurrentHashMap<Object, HeraFile> map = new ConcurrentHashMap<>();

@Getter
private ConcurrentHashMap<String, HeraFile> fileMap = new ConcurrentHashMap<>();

@Setter
Expand All @@ -52,7 +53,7 @@ public HeraFileMonitor(long removeTime) {
List<Pair<String, Object>> remList = Lists.newArrayList();
long now = System.currentTimeMillis();
fileMap.values().forEach(it -> {
if (now - it.getUtime().get() >= removeTime) {
if (now - it.getUtime().get() >= removeTime && now - it.getReadTime().get() >= removeTime) {
remList.add(Pair.of(it.getFileName(), it.getFileKey()));
}
});
Expand Down Expand Up @@ -129,7 +130,7 @@ public void reg(String path, Predicate<String> predicate) throws IOException, In
map.putIfAbsent(k, hf);
fileMap.put(filePath, hf);

listener.onEvent(FileEvent.builder().type(EventType.create).fileName(file.getPath()).build());
listener.onEvent(FileEvent.builder().type(EventType.create).fileKey(k).fileName(file.getPath()).build());
}
}
}
Expand Down Expand Up @@ -167,7 +168,12 @@ private HeraFile initFile(File it) {
log.info("initFile fileName:{},fileKey:{}", name, fileKey);
map.put(hf.getFileKey(), hf);
fileMap.put(hf.getFileName(), hf);
this.listener.onEvent(FileEvent.builder().pointer(pointer).type(EventType.init).fileName(hf.getFileName()).build());
this.listener.onEvent(FileEvent.builder()
.pointer(pointer)
.type(EventType.init)
.fileName(hf.getFileName())
.fileKey(hf.getFileKey())
.build());
return hf;
} catch (Exception e) {
log.error("init file error,fileName:{}", name, e);
Expand Down
16 changes: 14 additions & 2 deletions jcommon/file/src/test/java/com/xiaomi/mone/file/LogFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -74,9 +76,14 @@ public void testLogFileMonitor() {
monitor.setListener(new DefaultMonitorListener(monitor, readEvent -> {
System.out.println(readEvent.getReadResult().getLines());
}));
String fileName = "/home/work/log/file.log.*";
String fileName = "/home/work/log/test/file*.txt";
Pattern pattern = Pattern.compile(fileName);
monitor.reg("/home/work/log", it -> {

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

scheduler.scheduleAtFixedRate(this::test, 1, 2, TimeUnit.SECONDS);

monitor.reg("/home/work/log/test", it -> {
boolean matches = pattern.matcher(it).matches();
log.info("file:{},matches:{}", it, true);
return true;
Expand All @@ -85,6 +92,11 @@ public void testLogFileMonitor() {
System.in.read();
}

private void test() {
log.info("test save progress");
FileInfoCache.ins().shutdown();
}

@Test
public void testLogWS() throws IOException {
LogFile log = new LogFile("D:\\test.log", new ReadListener() {
Expand Down
Loading