aboutsummaryrefslogtreecommitdiff
path: root/weed/filesys/page_writer_pattern.go
blob: 44b69cda7c0482d752994aea6139f21f82ba5064 (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
30
31
32
33
34
35
36
37
38
39
40
41
package filesys

type WriterPattern struct {
	isStreaming     bool
	lastWriteOffset int64
	chunkSize       int64
	fileName        string
}

// For streaming write: only cache the first chunk
// For random write: fall back to temp file approach
// writes can only change from streaming mode to non-streaming mode

func NewWriterPattern(fileName string, chunkSize int64) *WriterPattern {
	return &WriterPattern{
		isStreaming:     true,
		lastWriteOffset: -1,
		chunkSize:       chunkSize,
		fileName:        fileName,
	}
}

func (rp *WriterPattern) MonitorWriteAt(offset int64, size int) {
	if rp.lastWriteOffset > offset {
		rp.isStreaming = false
	}
	if rp.lastWriteOffset == -1 {
		if offset != 0 {
			rp.isStreaming = false
		}
	}
	rp.lastWriteOffset = offset
}

func (rp *WriterPattern) IsStreamingMode() bool {
	return rp.isStreaming
}

func (rp *WriterPattern) IsRandomMode() bool {
	return !rp.isStreaming
}