-
Notifications
You must be signed in to change notification settings - Fork 1
/
binx.go
97 lines (78 loc) · 1.72 KB
/
binx.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package binx
import (
"encoding"
bolt "github.com/coreos/bbolt"
)
type (
Bucket interface {
BucketKey() []byte
}
Index interface {
Bucket
Key() []byte
}
UniqueIndex interface {
Bucket
UniqueKey() []byte
}
Queryable interface {
Bucket
AppendBinary(data []byte) (bool, error)
}
Indexable interface {
UniqueIndex
encoding.BinaryMarshaler
MasterIndexBucketKey() []byte
Indexes() []Index
}
Bound interface {
Index
Upper() bool
Lower() bool
}
)
func UpperBound(idx Index) Bound { return upperBound{idx} }
func LowerBound(idx Index) Bound { return lowerBound{idx} }
func Where(idx Index) Bound { return where{idx} }
func By(idx Index) Bound { return by{idx} }
func Page(q Queryable, skip, limit int) Queryable { return &page{q, skip, limit} }
type Tx struct {
*bolt.Tx
}
type (
upperBound struct{ Index }
lowerBound struct{ Index }
where struct{ Index }
by struct{ Index }
)
func (b upperBound) Upper() bool { return true }
func (b upperBound) Lower() bool { return false }
func (b lowerBound) Upper() bool { return false }
func (b lowerBound) Lower() bool { return true }
func (b where) Upper() bool { return true }
func (b where) Lower() bool { return true }
func (b by) Upper() bool { return false }
func (b by) Lower() bool { return false }
type page struct {
Queryable
Skip int
Limit int
}
func (e *page) AppendBinary(data []byte) (bool, error) {
if e.Limit > 0 {
e.Limit--
}
if e.Skip > 0 {
e.Skip--
return true, nil
}
_, err := e.Queryable.AppendBinary(data)
return e.Limit > 0, err
}
type Count struct {
Total int
}
func (c *Count) AppendBinary([]byte) error {
c.Total++
return nil
}