-
Notifications
You must be signed in to change notification settings - Fork 38
/
page.go
49 lines (42 loc) · 868 Bytes
/
page.go
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
package option
import (
"sync/atomic"
)
//Page represents a page
type Page struct {
counter uint32
limit int
offset int
}
//ShallSkip returns true if item needs to be skipped
func (p *Page) ShallSkip() bool {
if p.limit == 0 {
return false
}
return int(atomic.LoadUint32(&p.counter)) < p.offset
}
//MaxResult returns max results or zero
func (p *Page) MaxResult() int64 {
if p.offset > 0 {
return 0
}
return int64(p.limit)
}
//HasReachedLimit returns true if limit has been reaced
func (p *Page) HasReachedLimit() bool {
if p.limit == 0 {
return false
}
return int(atomic.LoadUint32(&p.counter)) >= p.limit
}
//Increment increment counter
func (p *Page) Increment() int {
return int(atomic.AddUint32(&p.counter, 1))
}
//NewPage returns a page
func NewPage(offset, limit int) *Page {
return &Page{
offset: offset,
limit: limit,
}
}