page_writer_pattern.go 931 B

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