query-lines-reader
Query multi lines or pagination by reading file. Support big file for nodejs
Install
$ npm install query-lines-reader
中文文档
Usage
General usage
let queryLinesReader = filePath options; queryLinesReader; queryLinesReader;
You can use the api set a file size.
The ‘fileSize‘ for different decisions to improve efficiency
We use ‘readline’ module to read the file when less than then the size
We use system command to get file info when greater than the size
default 1.5 * 1024 K
let queryLinesReader = filePath options; queryLinesReader
Options
filePath
-
File absolute path, example:
path.resolve(__dirname, './test.txt') -
Stream, the stream path is must be absolute, example:
fs.createReadStream(path.resolve(__dirname, './test.txt')) -
Buffer, example:
Buffer.from(path.resolve(__dirname, './test.txt')) -
URL, example:
new URL('file:///tmp/hello')
options
start && end:
- Type: Number
- start: you need first line number (Default 0)
- end: you need last line number (Default 10)
the line include start
exclude end
. [start, end) !!!!
pageSize && currentPage
- Type: Number
- pageSize: number of pages per page (Default 10)
- currentPage: current page (Default 0)
needTotal
- Type: Boolean
- If true will return total lines or not
reverse
- Type: Boolean, read direction
- Tf false, from top to bottom
- If true, from bottom to top
include
- Type: String or RegExp, string or regular expression
- You can use it to search file
lineRes
- lineList: lines result
- total: if options.needTotal is true, this will return
totalRes
- Type: Number, file's total line
Example
// From top to bottomconst path = ;const QueryLinesReader = ; let queryLinesReader = path; queryLinesReader
// From bottom to topconst path = ;const QueryLinesReader = ; let queryLinesReader = path reverse: true // include: /xxxx/g; queryLinesReader
// Paginationconst path = ;const QueryLinesReader = ; let queryLinesReader = path pageSize: 10; // first pagequeryLinesReader; // third pagequeryLinesReader;
Others
This api is global api, set process‘s maximum of one cpu. If the maximum number is exceeded, a another strategy of reader will be used
QueryLinesReader;
query-lines-reader
高效分页、按行读取文件,支持大文件
安装
$ npm install query-lines-reader
使用
一般用法
let queryLinesReader = filePath options; queryLinesReader; queryLinesReader;
setMinSizeOfCommand 这个 API 是设置文件大小的一个值,用不同策略来提高效率
小于这个值的时候,我们用 ‘readline’ 这个模块来读取文件 大于这个值的时候,我们使用 系统内部命令 读取文件 默认是 1.5 * 1024 k
let queryLinesReader = filePath options; queryLinesReader
参数
filePath
-
文件的绝对路径,例如:
path.resolve(__dirname, './test.txt') -
stream 流, 流的路径也必须是绝对路径, 例如:
fs.createReadStream(path.resolve(__dirname, './test.txt')) -
Buffer, 例如:
Buffer.from(path.resolve(__dirname, './test.txt')) -
URL, 例如:
new URL('file:///tmp/hello')
options
start && end:
- 类型: Number
- start: 读取的第一行的行数 (Default 0)
- end: 读取的最后一行 (Default 10)
读取结果 包含 start
不包含 end
. [start, end) !!!!
pageSize && currentPage
- 类型: Number
- pageSize: 每页的数量 (Default 10)
- currentPage: 当前页 (Default 0)
needTotal
- 类型: Boolean
- 如果你设置它为 true 会返回总数
reverse
- 类型: Boolean, 文件读取方向
- 如果是 false, 从上往下读
- 如果是 true, 从下往上读
include
- 类型: String or RegExp, 包含的字符串或者正则表达式
- 可以使用这个值去搜索文件
lineRes
- lineList: 读取文件的结果
- total: 如果 options.needTotal 是 true, 将会返回这个值
totalRes
- 类型: Number, 文件的总行数
例子
// 从上往下读const path = ;const QueryLinesReader = ; let queryLinesReader = path; queryLinesReader
// 从下往上读const path = ;const QueryLinesReader = ; let queryLinesReader = path reverse: true // include: /xxxx/g; queryLinesReader
// 分页const path = ;const QueryLinesReader = ; let queryLinesReader = path pageSize: 10; // 第一页queryLinesReader; // 第三页queryLinesReader;
Others
这个是全局 api, 并发较大时一个 cpu 可以开两个子进程。当进程数都占满后 会通过 ‘readline’ 模块去读文件。
QueryLinesReader;