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.

35 lines
790 B

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