detect sequential mode with a counter

This commit is contained in:
chrislu 2022-07-13 02:20:03 -07:00
parent 578c8aea0e
commit 6e90f7bdd0

View file

@ -1,8 +1,8 @@
package filer package filer
type ReaderPattern struct { type ReaderPattern struct {
isStreaming bool isSequentialCounter int64
lastReadOffset int64 lastReadStopOffset int64
} }
// For streaming read: only cache the first chunk // For streaming read: only cache the first chunk
@ -10,29 +10,20 @@ type ReaderPattern struct {
func NewReaderPattern() *ReaderPattern { func NewReaderPattern() *ReaderPattern {
return &ReaderPattern{ return &ReaderPattern{
isStreaming: true, isSequentialCounter: 0,
lastReadOffset: -1, lastReadStopOffset: 0,
} }
} }
func (rp *ReaderPattern) MonitorReadAt(offset int64, size int) { func (rp *ReaderPattern) MonitorReadAt(offset int64, size int) {
isStreaming := true if rp.lastReadStopOffset == offset {
if rp.lastReadOffset > offset { rp.isSequentialCounter++
isStreaming = false } else {
rp.isSequentialCounter--
} }
if rp.lastReadOffset == -1 { rp.lastReadStopOffset = offset + int64(size)
if offset != 0 {
isStreaming = false
}
}
rp.lastReadOffset = offset
rp.isStreaming = isStreaming
}
func (rp *ReaderPattern) IsStreamingMode() bool {
return rp.isStreaming
} }
func (rp *ReaderPattern) IsRandomMode() bool { func (rp *ReaderPattern) IsRandomMode() bool {
return !rp.isStreaming return rp.isSequentialCounter >= 0
} }