/
argument.go
161 lines (139 loc) · 2.79 KB
/
argument.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package boltutil
import (
"bytes"
)
type Filter struct {
min, max []byte
prefix []byte
filters []func(k, v []byte) (skip bool, stop bool)
storableFilters []func(obj Storable) (skip bool, stop bool)
}
func NewFilter() *Filter {
return &Filter{}
}
func (c *Filter) SetRange(min, max []byte) *Filter {
c.min = min
c.max = max
return c
}
func (c *Filter) SetPrefix(prefix []byte) *Filter {
c.min = prefix
c.max = make([]byte, len(prefix))
copy(c.max, c.max)
for i := len(c.max) - 1; i >= 0; i-- {
if c.max[i] < 0xff {
c.max[i]++
break
}
}
return c
}
func (c *Filter) AddCondition(f func(k, v []byte) (skip bool, stop bool)) *Filter {
c.filters = append(c.filters, f)
return c
}
func (c *Filter) AddStorableCondition(f func(obj Storable) (skip bool, stop bool)) *Filter {
c.storableFilters = append(c.storableFilters, f)
return c
}
func (c *Filter) seek() []byte {
if c == nil {
return nil
}
if c.prefix != nil && bytes.Compare(c.prefix, c.min) > 0 {
return c.prefix
}
return c.min
}
func (c *Filter) goon(k []byte) bool {
if k == nil {
return false
}
if c == nil {
return true
}
if len(c.max) > 0 && bytes.Compare(k, c.max) > 0 {
return false
}
if len(c.prefix) > 0 && !bytes.HasPrefix(k, c.prefix) {
return false
}
return true
}
func (c *Filter) getConditions() []func(k, v []byte) (skip bool, stop bool) {
if c == nil {
return nil
}
return c.filters
}
func (c *Filter) getStorableConditions() []func(obj Storable) (skip bool, stop bool) {
if c == nil {
return nil
}
return c.storableFilters
}
type Condition struct {
ignoreIfExist bool // for Put
failIfExist bool // for Put
ignoreIfNotExist bool // for Get
failIfNotExist bool // for Put, Delete
}
func NewCondition() *Condition {
return &Condition{}
}
func (c *Condition) IgnoreIfExist(v ...bool) *Condition {
if len(v) == 0 {
c.ignoreIfExist = true
} else {
c.ignoreIfExist = v[0]
}
return c
}
func (c *Condition) FailIfExist(v ...bool) *Condition {
if len(v) == 0 {
c.failIfExist = true
} else {
c.failIfExist = v[0]
}
return c
}
func (c *Condition) IgnoreIfNotExist(v ...bool) *Condition {
if len(v) == 0 {
c.ignoreIfNotExist = true
} else {
c.ignoreIfNotExist = v[0]
}
return c
}
func (c *Condition) FailIfNotExist(v ...bool) *Condition {
if len(v) == 0 {
c.failIfNotExist = true
} else {
c.failIfNotExist = v[0]
}
return c
}
func (c *Condition) getIgnoreIfExist() bool {
if c == nil {
return false
}
return c.ignoreIfExist
}
func (c *Condition) getFailIfExist() bool {
if c == nil {
return false
}
return c.failIfExist
}
func (c *Condition) getIgnoreIfNotExist() bool {
if c == nil {
return false
}
return c.ignoreIfNotExist
}
func (c *Condition) getFailIfNotExist() bool {
if c == nil {
return false
}
return c.failIfNotExist
}