|
@@ -142,8 +142,8 @@ async function readLastNLines(dirPath, filePathList, n) {
|
|
|
// let lines = content.trim().split("\n");
|
|
|
|
|
|
//新版本:流式读取和逐行处理
|
|
|
- const lines = await readLinesFromEnd(filepath, n - count);
|
|
|
- // logger.info("日:" + lines)
|
|
|
+ const lines = await readLinesFromEnd(filepath,n-count);
|
|
|
+ logger.info("日:" + lines)
|
|
|
fileList.push({
|
|
|
filePath: item,
|
|
|
lines: lines,
|
|
@@ -173,39 +173,81 @@ async function readLinesFromEnd(filePath, maxLines) {
|
|
|
const fileSize = fs.statSync(filePath).size;
|
|
|
const chunkSize = 1024; // 每次读取的块大小
|
|
|
let buffer = '';
|
|
|
- let position = fileSize - chunkSize;
|
|
|
|
|
|
- const fileStream = fs.createReadStream(filePath, {
|
|
|
- start: Math.max(position, 0),
|
|
|
- end: fileSize
|
|
|
- });
|
|
|
|
|
|
- for await (const chunk of fileStream) {
|
|
|
- buffer = chunk + buffer;
|
|
|
- let lineEndIndex = buffer.length;
|
|
|
+ let forNum = (fileSize/chunkSize)+1;
|
|
|
+ for (var i = 1 ;i <= forNum;i++){
|
|
|
+ let position = fileSize - (chunkSize * i);
|
|
|
+ if(position < 0){
|
|
|
+ position = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ const fileStream = fs.createReadStream(filePath, {
|
|
|
+ start: Math.max(position, 0),
|
|
|
+ end: fileSize
|
|
|
+ });
|
|
|
+
|
|
|
+ for await (const chunk of fileStream) {
|
|
|
+ buffer = chunk + buffer;
|
|
|
+ let lineEndIndex = buffer.length;
|
|
|
+
|
|
|
+ while (lineEndIndex >= 0 && lines.length < maxLines) {
|
|
|
+ const lineStartIndex = buffer.lastIndexOf('\n', lineEndIndex - 1);
|
|
|
+ if (lineStartIndex === -1) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ const line = buffer.substring(lineStartIndex + 1, lineEndIndex).trim();
|
|
|
+ if (line) {
|
|
|
+ lines.push(line);
|
|
|
+ }
|
|
|
+ lineEndIndex = lineStartIndex;
|
|
|
+ }
|
|
|
|
|
|
- while (lineEndIndex >= 0 && lines.length < maxLines) {
|
|
|
- const lineStartIndex = buffer.lastIndexOf('\n', lineEndIndex - 1);
|
|
|
- if (lineStartIndex === -1) {
|
|
|
+ if (lines.length >= maxLines) {
|
|
|
break;
|
|
|
}
|
|
|
- const line = buffer.substring(lineStartIndex + 1, lineEndIndex).trim();
|
|
|
- if (line) {
|
|
|
- lines.push(line);
|
|
|
+
|
|
|
+ position -= chunkSize;
|
|
|
+ if (position < 0) {
|
|
|
+ position = 0;
|
|
|
}
|
|
|
- lineEndIndex = lineStartIndex;
|
|
|
}
|
|
|
-
|
|
|
if (lines.length >= maxLines) {
|
|
|
break;
|
|
|
}
|
|
|
-
|
|
|
- position -= chunkSize;
|
|
|
- if (position < 0) {
|
|
|
- position = 0;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
+ // const fileStream = fs.createReadStream(filePath, {
|
|
|
+ // start: Math.max(position, 0),
|
|
|
+ // end: fileSize
|
|
|
+ // });
|
|
|
+
|
|
|
+ // for await (const chunk of fileStream) {
|
|
|
+ // buffer = chunk + buffer;
|
|
|
+ // let lineEndIndex = buffer.length;
|
|
|
+ //
|
|
|
+ // while (lineEndIndex >= 0 && lines.length < maxLines) {
|
|
|
+ // const lineStartIndex = buffer.lastIndexOf('\n', lineEndIndex - 1);
|
|
|
+ // if (lineStartIndex === -1) {
|
|
|
+ // break;
|
|
|
+ // }
|
|
|
+ // const line = buffer.substring(lineStartIndex + 1, lineEndIndex).trim();
|
|
|
+ // if (line) {
|
|
|
+ // lines.push(line);
|
|
|
+ // }
|
|
|
+ // lineEndIndex = lineStartIndex;
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // if (lines.length >= maxLines) {
|
|
|
+ // break;
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // position -= chunkSize;
|
|
|
+ // if (position < 0) {
|
|
|
+ // position = 0;
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
return lines;
|
|
|
}
|
|
|
async function readLines(filePath, maxLines) {
|