reader_pattern.go 724 B

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