2021-12-20 14:43:14 +08:00
|
|
|
package filer
|
|
|
|
|
2022-08-27 07:41:37 +08:00
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
2021-12-20 14:43:14 +08:00
|
|
|
type ReaderPattern struct {
|
2022-07-13 17:20:03 +08:00
|
|
|
isSequentialCounter int64
|
|
|
|
lastReadStopOffset int64
|
2021-12-20 14:43:14 +08:00
|
|
|
}
|
|
|
|
|
2022-08-08 01:14:01 +08:00
|
|
|
const ModeChangeLimit = 3
|
|
|
|
|
2021-12-20 14:43:14 +08:00
|
|
|
// 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 17:20:03 +08:00
|
|
|
isSequentialCounter: 0,
|
|
|
|
lastReadStopOffset: 0,
|
2021-12-20 14:43:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ReaderPattern) MonitorReadAt(offset int64, size int) {
|
2022-08-27 07:41:37 +08:00
|
|
|
lastOffset := atomic.SwapInt64(&rp.lastReadStopOffset, offset+int64(size))
|
|
|
|
counter := atomic.LoadInt64(&rp.isSequentialCounter)
|
|
|
|
|
|
|
|
if lastOffset == offset {
|
|
|
|
if counter < ModeChangeLimit {
|
|
|
|
atomic.AddInt64(&rp.isSequentialCounter, 1)
|
2022-08-08 01:14:01 +08:00
|
|
|
}
|
2022-07-13 17:20:03 +08:00
|
|
|
} else {
|
2022-08-27 07:41:37 +08:00
|
|
|
if counter > -ModeChangeLimit {
|
|
|
|
atomic.AddInt64(&rp.isSequentialCounter, -1)
|
2022-08-08 01:14:01 +08:00
|
|
|
}
|
2021-12-20 14:43:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *ReaderPattern) IsRandomMode() bool {
|
2022-08-27 07:41:37 +08:00
|
|
|
return atomic.LoadInt64(&rp.isSequentialCounter) < 0
|
2021-12-20 14:43:14 +08:00
|
|
|
}
|