2021-12-22 09:28:55 +08:00
|
|
|
package filesys
|
|
|
|
|
2021-12-21 03:53:48 +08:00
|
|
|
type WriterPattern struct {
|
|
|
|
isStreaming bool
|
|
|
|
lastWriteOffset int64
|
2021-12-22 09:28:55 +08:00
|
|
|
chunkSize int64
|
2021-12-21 03:53:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// For streaming write: only cache the first chunk
|
|
|
|
// For random write: fall back to temp file approach
|
2021-12-22 09:28:55 +08:00
|
|
|
// writes can only change from streaming mode to non-streaming mode
|
2021-12-21 03:53:48 +08:00
|
|
|
|
2021-12-25 14:38:22 +08:00
|
|
|
func NewWriterPattern(chunkSize int64) *WriterPattern {
|
2021-12-21 03:53:48 +08:00
|
|
|
return &WriterPattern{
|
|
|
|
isStreaming: true,
|
2021-12-23 09:20:44 +08:00
|
|
|
lastWriteOffset: -1,
|
2021-12-22 09:28:55 +08:00
|
|
|
chunkSize: chunkSize,
|
2021-12-21 03:53:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *WriterPattern) MonitorWriteAt(offset int64, size int) {
|
|
|
|
if rp.lastWriteOffset > offset {
|
|
|
|
rp.isStreaming = false
|
|
|
|
}
|
2021-12-23 09:20:44 +08:00
|
|
|
if rp.lastWriteOffset == -1 {
|
|
|
|
if offset != 0 {
|
|
|
|
rp.isStreaming = false
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 03:53:48 +08:00
|
|
|
rp.lastWriteOffset = offset
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *WriterPattern) IsStreamingMode() bool {
|
|
|
|
return rp.isStreaming
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *WriterPattern) IsRandomMode() bool {
|
|
|
|
return !rp.isStreaming
|
|
|
|
}
|
2021-12-25 14:38:22 +08:00
|
|
|
|
|
|
|
func (rp *WriterPattern) Reset() {
|
|
|
|
rp.isStreaming = true
|
|
|
|
rp.lastWriteOffset = -1
|
|
|
|
}
|