簡寫readStream的流動模式並完成文章搜索功能

搜索功能實現步驟bash

1.打開文件函數

2.觸發newListener事件並採用flowing模式讀取數據測試

3.對數據進行過濾對符合搜索內容的次數計數ui

  • 可讀流事實上工做在下面兩種模式之一: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。this

  • 定義一個SearchTimes類,並繼承event模塊爲了以後使用事件訂閱發佈,添加傳進來的屬性。spa

  • 當給一個對象添加一個新的監聽函數時候會觸發newListener事件。code

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)
})
複製代碼

進行搜索的對象文件接口

好好今天好好裏的梅林強無敵好王哈也不賴好仍是咕噠子好好最強好好好賽高
複製代碼
相關文章
相關標籤/搜索