reader_pattern.go 919 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package filer
  2. import (
  3. "sync/atomic"
  4. )
  5. type ReaderPattern struct {
  6. isSequentialCounter int64
  7. lastReadStopOffset int64
  8. }
  9. const ModeChangeLimit = 3
  10. // For streaming read: only cache the first chunk
  11. // For random read: only fetch the requested range, instead of the whole chunk
  12. func NewReaderPattern() *ReaderPattern {
  13. return &ReaderPattern{
  14. isSequentialCounter: 0,
  15. lastReadStopOffset: 0,
  16. }
  17. }
  18. func (rp *ReaderPattern) MonitorReadAt(offset int64, size int) {
  19. lastOffset := atomic.SwapInt64(&rp.lastReadStopOffset, offset+int64(size))
  20. counter := atomic.LoadInt64(&rp.isSequentialCounter)
  21. if lastOffset == offset {
  22. if counter < ModeChangeLimit {
  23. atomic.AddInt64(&rp.isSequentialCounter, 1)
  24. }
  25. } else {
  26. if counter > -ModeChangeLimit {
  27. atomic.AddInt64(&rp.isSequentialCounter, -1)
  28. }
  29. }
  30. }
  31. func (rp *ReaderPattern) IsRandomMode() bool {
  32. return atomic.LoadInt64(&rp.isSequentialCounter) < 0
  33. }