2021-12-22 09:28:55 +08:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import "fmt"
|
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
|
|
|
|
fileName string
|
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-22 09:28:55 +08:00
|
|
|
func NewWriterPattern(fileName string, chunkSize int64) *WriterPattern {
|
2021-12-21 03:53:48 +08:00
|
|
|
return &WriterPattern{
|
|
|
|
isStreaming: true,
|
|
|
|
lastWriteOffset: 0,
|
2021-12-22 09:28:55 +08:00
|
|
|
chunkSize: chunkSize,
|
|
|
|
fileName: fileName,
|
2021-12-21 03:53:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *WriterPattern) MonitorWriteAt(offset int64, size int) {
|
2021-12-22 09:28:55 +08:00
|
|
|
if rp.lastWriteOffset == 0 {
|
|
|
|
}
|
2021-12-21 03:53:48 +08:00
|
|
|
if rp.lastWriteOffset > offset {
|
2021-12-22 09:28:55 +08:00
|
|
|
if rp.isStreaming {
|
|
|
|
fmt.Printf("file %s ==> non streaming at [%d,%d)\n", rp.fileName, offset, offset+int64(size))
|
|
|
|
}
|
|
|
|
fmt.Printf("write %s [%d,%d)\n", rp.fileName, offset, offset+int64(size))
|
2021-12-21 03:53:48 +08:00
|
|
|
rp.isStreaming = false
|
|
|
|
}
|
|
|
|
rp.lastWriteOffset = offset
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *WriterPattern) IsStreamingMode() bool {
|
|
|
|
return rp.isStreaming
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *WriterPattern) IsRandomMode() bool {
|
|
|
|
return !rp.isStreaming
|
|
|
|
}
|