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.

37 lines
849 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. func NewWriterPattern(chunkSize int64) *WriterPattern {
  11. return &WriterPattern{
  12. isSequentialCounter: 0,
  13. lastWriteStopOffset: 0,
  14. chunkSize: chunkSize,
  15. }
  16. }
  17. func (rp *WriterPattern) MonitorWriteAt(offset int64, size int) {
  18. if rp.lastWriteStopOffset == offset {
  19. if rp.isSequentialCounter < ModeChangeLimit {
  20. rp.isSequentialCounter++
  21. }
  22. } else {
  23. if rp.isSequentialCounter > -ModeChangeLimit {
  24. rp.isSequentialCounter--
  25. }
  26. }
  27. rp.lastWriteStopOffset = offset + int64(size)
  28. }
  29. func (rp *WriterPattern) IsSequentialMode() bool {
  30. return rp.isSequentialCounter >= 0
  31. }