You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.1 KiB

  1. package filesys
  2. import "fmt"
  3. type WriterPattern struct {
  4. isStreaming bool
  5. lastWriteOffset int64
  6. chunkSize int64
  7. fileName string
  8. }
  9. // For streaming write: only cache the first chunk
  10. // For random write: fall back to temp file approach
  11. // writes can only change from streaming mode to non-streaming mode
  12. func NewWriterPattern(fileName string, chunkSize int64) *WriterPattern {
  13. return &WriterPattern{
  14. isStreaming: true,
  15. lastWriteOffset: 0,
  16. chunkSize: chunkSize,
  17. fileName: fileName,
  18. }
  19. }
  20. func (rp *WriterPattern) MonitorWriteAt(offset int64, size int) {
  21. if rp.lastWriteOffset == 0 {
  22. }
  23. if rp.lastWriteOffset > offset {
  24. if rp.isStreaming {
  25. fmt.Printf("file %s ==> non streaming at [%d,%d)\n", rp.fileName, offset, offset+int64(size))
  26. }
  27. fmt.Printf("write %s [%d,%d)\n", rp.fileName, offset, offset+int64(size))
  28. rp.isStreaming = false
  29. }
  30. rp.lastWriteOffset = offset
  31. }
  32. func (rp *WriterPattern) IsStreamingMode() bool {
  33. return rp.isStreaming
  34. }
  35. func (rp *WriterPattern) IsRandomMode() bool {
  36. return !rp.isStreaming
  37. }