Skip to content

Commit

Permalink
refactor: update file coll
Browse files Browse the repository at this point in the history
  • Loading branch information
wtt40122 committed Aug 29, 2024
1 parent 7a6ad16 commit ab9f2ac
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
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
18 changes: 15 additions & 3 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,17 +76,27 @@ 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);
log.info("file:{},matches:{}", it, matches);
return true;
});
log.info("reg finish");
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

0 comments on commit ab9f2ac

Please sign in to comment.