2022-03-14 01:17:35 +00:00
|
|
|
package mount
|
|
|
|
|
|
|
|
type WriterPattern struct {
|
2022-07-13 09:30:44 +00:00
|
|
|
isSequentialCounter int64
|
|
|
|
lastWriteStopOffset int64
|
|
|
|
chunkSize int64
|
2022-03-14 01:17:35 +00:00
|
|
|
}
|
|
|
|
|
2022-08-07 17:14:01 +00:00
|
|
|
const ModeChangeLimit = 3
|
|
|
|
|
2022-03-14 01:17:35 +00:00
|
|
|
// For streaming write: only cache the first chunk
|
|
|
|
// For random write: fall back to temp file approach
|
|
|
|
|
|
|
|
func NewWriterPattern(chunkSize int64) *WriterPattern {
|
|
|
|
return &WriterPattern{
|
2022-07-13 09:30:44 +00:00
|
|
|
isSequentialCounter: 0,
|
|
|
|
lastWriteStopOffset: 0,
|
|
|
|
chunkSize: chunkSize,
|
2022-03-14 01:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *WriterPattern) MonitorWriteAt(offset int64, size int) {
|
2022-07-13 09:30:44 +00:00
|
|
|
if rp.lastWriteStopOffset == offset {
|
2022-08-07 17:14:01 +00:00
|
|
|
if rp.isSequentialCounter < ModeChangeLimit {
|
|
|
|
rp.isSequentialCounter++
|
|
|
|
}
|
2022-07-13 09:30:44 +00:00
|
|
|
} else {
|
2022-08-07 17:14:01 +00:00
|
|
|
if rp.isSequentialCounter > -ModeChangeLimit {
|
|
|
|
rp.isSequentialCounter--
|
|
|
|
}
|
2022-03-14 01:17:35 +00:00
|
|
|
}
|
2022-07-13 09:30:44 +00:00
|
|
|
rp.lastWriteStopOffset = offset + int64(size)
|
2022-03-14 01:17:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 09:30:44 +00:00
|
|
|
func (rp *WriterPattern) IsSequentialMode() bool {
|
|
|
|
return rp.isSequentialCounter >= 0
|
2022-03-14 01:17:35 +00:00
|
|
|
}
|