简写readStream的流动模式并完成文章搜索功能

搜索功能实现步骤

1.打开文件

2.触发newListener事件并采用flowing模式读取数据

3.对数据进行过滤对符合搜索内容的次数计数

  • 可读流事实上工作在下面两种模式之一:flowing 和 paused
  • 在 flowing 模式下, 可读流自动从系统底层读取数据,并通过 EventEmitter 接口的事件尽快将数据提供给应用。

调用方法

let SearchTime = require('./SearchTimes')
let reader = new SearchTime('./1.txt',{
    text:'好好',//搜索内容
    highWaterMark:30//最高水位线
})

reader.on('searchTimes',(data)=>{//监听searchTimes 返回文件内容
    console.log(data)
})
reader.on('end',data=>{//监听send 返回文件满足条件个数
    console.log('count',data)
})

reader.on('error',(error)=>{
    console.log('error',error)
})

createReadStream类的flowing模式实例化

  • 引入events,fs。

  • 定义一个SearchTimes类,并继承event模块为了之后使用事件订阅发布,添加传进来的属性。

  • 当给一个对象添加一个新的监听函数时候会触发newListener事件。

this.on('newListener',(type)=>{
            if(type == 'searchTimes'){//如果添加了searchTimes和监听,就开始以flowing模式读取文件内容
                this.read();
            }
        })

实现read方法

  • 判断文件是否打开,为否就触发一次文件打开事件。
  • 根据水位线位置和余下内容的大小判断一次读几个字节。
        let howMuchToRead = this.end?Math.min(this.end-this.pos+1,this.highWaterMark):this.highWaterMark
  • fs.read读取文件。
  • 读取成功,发射searchTimes事件,返回读取到的内容。
  • 并将读取到的内容累积记录。
  • 没有读取到或者传入的end已经大于文件位移说明读取完成,执行下一步。
  • 关闭文件,并用正则过滤问价,返回匹配个数。

测试文件及代码

SearchTimes类

let EventEmitter = require('events')
let fs = require('fs')

class SearchTimes extends EventEmitter {
    constructor(path, options) {
        super(path, options);
        this.path = path;
        this.text = options.text || '';
        this.highWaterMark = options.highWaterMark || 64 * 1024;
        this.buffer = Buffer.alloc(this.highWaterMark);
        this.flags = options.flags || 'r';
        this.encoding = options.encoding || 'utf-8';
        this.mode = options.mode || 0o666;
        this.start = options.start || 0;
        this.end = options.end;
        this.pos = this.start;
        this.autoClose = options.autoClose || true;
        this.buffers = '';
        this.on('newListener',(type)=>{
            if(type == 'searchTimes'){
                this.read();
            }
        })
        this.open();
    }
    read(){
        if(typeof this.fd != 'number'){
            return this.once('open',()=>this.read())
        }
        let howMuchToRead = this.end?Math.min(this.end-this.pos+1,this.highWaterMark):this.highWaterMark
        fs.read(this.fd,this.buffer,0,howMuchToRead,this.pos,(err,bytes)=>{
            if(err){
                if(this.autoClose)
                    this.destroy()
                return this.emit('err',err)
            }
            if(bytes){
                let data = this.buffer.slice(0,bytes)
                this.pos += bytes
                data = this.encoding?data.toString(this.encoding):data
                this.emit('searchTimes',data)
                this.buffers += data
                if(this.end && this.pos > this.end){
                    return this.endFn();
                }else{
                    this.read();
                }
            }else{
                return this.endFn();
            }
       })
    }
    getPlaceholderCount(strSource,text) {
        var thisCount = 0;
        strSource.replace(new RegExp(this.unicode(text),'g'), function (m, i) {
            thisCount++;
        });
        return thisCount;
    }
    unicode(str){
        var value='';
        for (var i = 0; i < str.length; i++) {
            value += '\\u' + this.left_zero_4(parseInt(str.charCodeAt(i)).toString(16));
        }
        return value;
    }
    left_zero_4(str) {
        if (str != null && str != '' && str != 'undefined') {
            if (str.length == 2) {
                return '00' + str;
            }
        }
        return str;
    }
    endFn(){
        const count = this.getPlaceholderCount(this.buffers,this.text)
        this.emit('end',count);
        this.destroy();
    }
    open(){
        fs.open(this.path,this.flags,this.mode,(err,fd)=>{
            if(err){
                if(this.autoClose){
                    this.destroy()
                    return this.emit('err',err)
                }
            }
            this.fd = fd
            this.emit('open')
        })
    }
    destroy(){
        fs.close(this.fd,(err)=>{
            this.emit('close')
        })
    }
}

module.exports = SearchTimes

调用文件

let SearchTime = require('./SearchTimes')
let reader = new SearchTime('./1.txt',{
    text:'好好',//搜索内容
    highWaterMark:30//最高水位线
})

reader.on('searchTimes',(data)=>{//监听searchTimes 返回文件内容
    console.log(data)
})
reader.on('end',data=>{//监听send 返回文件满足条件个数
    console.log('count',data)
})

reader.on('error',(error)=>{
    console.log('error',error)
})

进行搜索的对象文件

好好今天好好里的梅林强无敌好王哈也不赖好还是咕哒子好好最强好好好赛高

猜你喜欢

转载自blog.csdn.net/weixin_33933118/article/details/87641453