@@ -55,7 +55,7 @@ type LastSync struct {
5555type BufReader struct {
5656 stopped int32
5757 buf []byte
58- mutiLineCache [] string
58+ mutiLineCache * LineCache
5959 rd reader.FileReader // reader provided by the client
6060 r , w int // buf read and write positions
6161 err error
@@ -136,6 +136,7 @@ func NewReaderSize(rd reader.FileReader, meta *reader.Meta, size int) (*BufReade
136136 r := new (BufReader )
137137 r .stopped = 0
138138 r .reset (make ([]byte , size ), rd )
139+ r .mutiLineCache = NewLineCache ()
139140
140141 r .Meta = meta
141142 encodingWay := r .Meta .GetEncodingWay ()
@@ -163,7 +164,7 @@ func NewReaderSize(rd reader.FileReader, meta *reader.Meta, size int) (*BufReade
163164 }
164165 }
165166 if len (linesbytes ) > 0 {
166- r .mutiLineCache = append ( r . mutiLineCache , string (linesbytes ))
167+ r .mutiLineCache . Append ( string (linesbytes ))
167168 }
168169 return r , nil
169170}
@@ -183,7 +184,7 @@ func (b *BufReader) reset(buf []byte, r reader.FileReader) {
183184 rd : r ,
184185 lastByte : - 1 ,
185186 lastRuneSize : - 1 ,
186- mutiLineCache : make ([] string , 0 , 16 ),
187+ mutiLineCache : NewLineCache ( ),
187188 lastSync : LastSync {},
188189 mux : sync.Mutex {},
189190 statsLock : sync.RWMutex {},
@@ -242,18 +243,24 @@ func (b *BufReader) fill() {
242243 panic (errNegativeRead )
243244 }
244245 if b .latestSource != b .rd .Source () {
245- //这个情况表示文件的数据源出现了变化,在buf中已经出现了2个数据源的数据 ,要定位是哪个位置的数据出现的分隔
246- if rc , ok := b .rd .(seqfile. NewLineBytesRecorder ); ok {
247- SIdx := rc .NewLineBytesIndex ()
246+ //这个情况表示文件的数据源出现了变化,在buf中已经出现了至少2个数据源的数据 ,要定位是哪个位置的数据出现的分隔
247+ if rc , ok := b .rd .(reader. NewSourceRecorder ); ok {
248+ SIdx := rc .NewSourceIndex ()
248249 for _ , v := range SIdx {
249- // 从 NewLineBytesIndex 函数中返回的index值就是本次读取的批次中上一个DataSource的数据量,加上b.w就是上个DataSource的整体数据
250+ // 从 NewSourceIndex 函数中返回的index值就是本次读取的批次中上一个DataSource的数据量,加上b.w就是上个DataSource的整体数据
250251 b .lastRdSource = append (b .lastRdSource , reader.SourceIndex {
251252 Source : v .Source ,
252253 Index : b .w + v .Index ,
253254 })
254255 }
255- b .latestSource = b .rd .Source ()
256+ } else {
257+ //如果没实现这个接口,那么就认为到上次读到的为止都是前一次source的文件
258+ b .lastRdSource = append (b .lastRdSource , reader.SourceIndex {
259+ Source : b .latestSource ,
260+ Index : b .w ,
261+ })
256262 }
263+ b .latestSource = b .rd .Source ()
257264 }
258265
259266 b .w += n
@@ -404,24 +411,24 @@ func (b *BufReader) ReadPattern() (string, error) {
404411 line , err := b .ReadString ('\n' )
405412 //读取到line的情况
406413 if len (line ) > 0 {
407- if len ( b .mutiLineCache ) <= 0 {
408- b .mutiLineCache = []string {line }
414+ if b .mutiLineCache . Size ( ) <= 0 {
415+ b .mutiLineCache . Set ( []string {line })
409416 continue
410417 }
411418 //匹配行首,成功则返回之前的cache,否则加入到cache,返回空串
412419 if b .multiLineRegexp .Match ([]byte (line )) {
413420 tmp := line
414- line = string (b .FormMutiLine ())
415- b .mutiLineCache = make ([]string , 0 , 16 )
416- b .mutiLineCache = append ( b . mutiLineCache , tmp )
421+ line = string (b .mutiLineCache . Combine ())
422+ b .mutiLineCache . Set ( make ([]string , 0 , 16 ) )
423+ b .mutiLineCache . Append ( tmp )
417424 return line , err
418425 }
419- b .mutiLineCache = append ( b . mutiLineCache , line )
426+ b .mutiLineCache . Append ( line )
420427 maxTimes = 0
421428 } else { //读取不到日志
422429 if err != nil {
423- line = string (b .FormMutiLine ())
424- b .mutiLineCache = make ([]string , 0 , 16 )
430+ line = string (b .mutiLineCache . Combine ())
431+ b .mutiLineCache . Set ( make ([]string , 0 , 16 ) )
425432 return line , err
426433 }
427434 maxTimes ++
@@ -432,36 +439,16 @@ func (b *BufReader) ReadPattern() (string, error) {
432439 }
433440 }
434441 //对于读取到了Cache的情况,继续循环,直到超过最大限制
435- if b .calcMutiLineCache () > MaxHeadPatternBufferSize {
436- line = string (b .FormMutiLine ())
437- b .mutiLineCache = make ([]string , 0 , 16 )
442+ if b .mutiLineCache . TotalLen () > MaxHeadPatternBufferSize {
443+ line = string (b .mutiLineCache . Combine ())
444+ b .mutiLineCache . Set ( make ([]string , 0 , 16 ) )
438445 return line , err
439446 }
440447 }
441448}
442449
443450func (b * BufReader ) FormMutiLine () []byte {
444- if len (b .mutiLineCache ) <= 0 {
445- return make ([]byte , 0 )
446- }
447- n := 0
448- for i := 0 ; i < len (b .mutiLineCache ); i ++ {
449- n += len (b .mutiLineCache [i ])
450- }
451-
452- xb := make ([]byte , n )
453- bp := copy (xb , b .mutiLineCache [0 ])
454- for _ , s := range b .mutiLineCache [1 :] {
455- bp += copy (xb [bp :], s )
456- }
457- return xb
458- }
459-
460- func (b * BufReader ) calcMutiLineCache () (ret int ) {
461- for _ , v := range b .mutiLineCache {
462- ret += len (v )
463- }
464- return
451+ return b .mutiLineCache .Combine ()
465452}
466453
467454//ReadLine returns a string line as a normal Reader
@@ -561,7 +548,7 @@ func (b *BufReader) ReadDone() bool {
561548func (b * BufReader ) SyncMeta () {
562549 b .mux .Lock ()
563550 defer b .mux .Unlock ()
564- linecache := string (b .FormMutiLine ())
551+ linecache := string (b .mutiLineCache . Combine ())
565552 //把linecache也缓存
566553 if b .lastSync .cache != linecache || b .lastSync .buf != string (b .buf ) || b .r != b .lastSync .r || b .w != b .lastSync .w {
567554 log .Debugf ("Runner[%v] %v sync meta started, linecache [%v] buf [%v] (%v %v)" , b .Meta .RunnerName , b .Name (), linecache , string (b .buf ), b .r , b .w )
0 commit comments