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.

38 lines
917 B

  1. package mount
  2. type WriterPattern struct {
  3. isSequentialCounter int64
  4. lastWriteStopOffset int64
  5. chunkSize int64
  6. }
  7. const ModeChangeLimit = 3
  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(chunkSize int64) *WriterPattern {
  12. return &WriterPattern{
  13. isSequentialCounter: 0,
  14. lastWriteStopOffset: 0,
  15. chunkSize: chunkSize,
  16. }
  17. }
  18. func (rp *WriterPattern) MonitorWriteAt(offset int64, size int) {
  19. if rp.lastWriteStopOffset == offset {
  20. if rp.isSequentialCounter < ModeChangeLimit {
  21. rp.isSequentialCounter++
  22. }
  23. } else {
  24. if rp.isSequentialCounter > -ModeChangeLimit {
  25. rp.isSequentialCounter--
  26. }
  27. }
  28. rp.lastWriteStopOffset = offset + int64(size)
  29. }
  30. func (rp *WriterPattern) IsSequentialMode() bool {
  31. return rp.isSequentialCounter >= 0
  32. }