seaweedfs/weed/filer/reader_pattern.go

36 lines
790 B
Go
Raw Normal View History

package filer
type ReaderPattern struct {
2022-07-13 09:20:03 +00:00
isSequentialCounter int64
lastReadStopOffset int64
}
2022-08-07 17:14:01 +00:00
const ModeChangeLimit = 3
// For streaming read: only cache the first chunk
// For random read: only fetch the requested range, instead of the whole chunk
func NewReaderPattern() *ReaderPattern {
return &ReaderPattern{
2022-07-13 09:20:03 +00:00
isSequentialCounter: 0,
lastReadStopOffset: 0,
}
}
func (rp *ReaderPattern) MonitorReadAt(offset int64, size int) {
2022-07-13 09:20:03 +00:00
if rp.lastReadStopOffset == offset {
2022-08-07 17:14:01 +00:00
if rp.isSequentialCounter < ModeChangeLimit {
rp.isSequentialCounter++
}
2022-07-13 09:20:03 +00:00
} else {
2022-08-07 17:14:01 +00:00
if rp.isSequentialCounter > -ModeChangeLimit {
rp.isSequentialCounter--
}
}
2022-07-13 09:20:03 +00:00
rp.lastReadStopOffset = offset + int64(size)
}
func (rp *ReaderPattern) IsRandomMode() bool {
return rp.isSequentialCounter < 0
}