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