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.

41 lines
915 B

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