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
931 B

  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. }