aboutsummaryrefslogtreecommitdiff
path: root/weed/filer/reader_pattern.go
blob: 38a51d788d37b8ecca50052e869cb38f7eb1acdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package filer

type ReaderPattern struct {
	isSequentialCounter int64
	lastReadStopOffset  int64
}

// For streaming read: only cache the first chunk
// For random read: only fetch the requested range, instead of the whole chunk

func NewReaderPattern() *ReaderPattern {
	return &ReaderPattern{
		isSequentialCounter: 0,
		lastReadStopOffset:  0,
	}
}

func (rp *ReaderPattern) MonitorReadAt(offset int64, size int) {
	if rp.lastReadStopOffset == offset {
		rp.isSequentialCounter++
	} else {
		rp.isSequentialCounter--
	}
	rp.lastReadStopOffset = offset + int64(size)
}

func (rp *ReaderPattern) IsRandomMode() bool {
	return rp.isSequentialCounter < 0
}