-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcap.go
More file actions
68 lines (57 loc) · 1.25 KB
/
cap.go
File metadata and controls
68 lines (57 loc) · 1.25 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package batch
type capped struct {
current, limit int64
}
func (c capped) reachedLimit() bool { return c.limit > 0 && c.current >= c.limit }
func (c capped) remaining() int64 {
if c.limit > 0 {
return c.limit - c.current
}
return -1
}
func (c capped) remainingPerN(n int64) int64 {
if c.limit > 0 {
return (c.limit - c.current) / n
}
return -1
}
func (c capped) cap() int64 {
if c.limit > 0 {
return c.limit
}
return -1
}
func (c capped) capPerN(n int64) int64 {
if c.limit > 0 {
return c.limit / n
}
return -1
}
type Cap struct {
bytes, rows capped
}
func (c *Cap) ReachedLimit() bool { return c.bytes.reachedLimit() || c.rows.reachedLimit() }
func (c *Cap) Rows() int64 { return c.rows.current }
func (c *Cap) AddRows(rows int64) { c.rows.current += rows }
func (c *Cap) AddSlice(record *SlicedRecord) {
c.rows.current += record.NumRows()
c.bytes.current += record.Bytes
}
func (c *Cap) Reset() {
c.bytes.current = 0
c.rows.current = 0
}
func (c *Cap) add(bytes, rows int64) {
c.bytes.current += bytes
c.rows.current += rows
}
func (c *Cap) set(bytes, rows int64) {
c.bytes.current = bytes
c.rows.current = rows
}
func CappedAt(bytes, rows int64) *Cap {
return &Cap{
bytes: capped{limit: bytes},
rows: capped{limit: rows},
}
}