-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathatomic_sequence.go
More file actions
281 lines (238 loc) · 9.21 KB
/
Copy pathatomic_sequence.go
File metadata and controls
281 lines (238 loc) · 9.21 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package sequence
import (
"encoding/json"
"errors"
"fmt"
"sync/atomic"
)
// NewAtomic creates a NewSequence that implements the Incrementer interface.
// It is safe for concurrent use.
func NewAtomic(params ...uint64) (*AtomicSequence, error) {
seq := new(AtomicSequence)
err := seq.Init(params...)
return seq, err
}
// AtomicSequence is a basic Sequence that uses atomic instructions in Sequence methods.
// Although implementation is very close, it is safe for concurrent use.
type AtomicSequence Sequence
// Init a sequence with reasonable defaults based on the number and order of
// the numeric parameters passed into this method. By default, if no arguments
// are passed into Init, then the Sequence will be initialized as a
// monotonically increasing counter in the positive space as follows:
//
// seq.Init() // count by 1 from 1 to MaximumBound
//
// If only a single argument is passed in, then it is interpreted as the
// maximum bound as follows:
//
// seq.Init(100) // count by 1 from 1 until 100.
//
// If two arguments are passed in, then it is interpreted as a discrete range.
//
// seq.Init(10, 100) // count by 1 from 10 until 100.
//
// If three arguments are passed in, then the third is the step.
//
// seq.Init(2, 100, 2) // even numbers from 2 until 100.
//
// Both endpoints of these ranges are inclusive.
//
// Init can return a variety of errors. The most common error is if Init is
// called twice - that is that an already initialized sequence is attempted to
// be modified in a way that doesn't reset it. This is part of the safety
// features that Sequence provides. Other errors include mismatched or
// non-sensical arguments that won't initialize the Sequence properly.
// It is done in an atomic way and is safe for concurrent use.
func (s *AtomicSequence) Init(params ...uint64) error {
if s.initialized {
return errors.New("cannot re-initialize a sequence object")
}
// If no parameters, create the default sequence.
if len(params) == 0 {
atomic.AddUint64(&s.increment, 1)
atomic.AddUint64(&s.minvalue, MinimumBound)
atomic.AddUint64(&s.maxvalue, MaximumBound)
}
// If a single parameter create a maximal bounding.
if len(params) == 1 {
// Ensure that the parameter is greater than the minimum value.
if params[0] < MinimumBound {
return errors.New("must specify a maximal value greater than 0")
}
atomic.AddUint64(&s.increment, 1)
atomic.AddUint64(&s.minvalue, MinimumBound)
atomic.AddUint64(&s.maxvalue, params[0])
}
// If two parameters create a positive range.
if len(params) == 2 {
if params[1] < params[0] {
return errors.New("for a positive increment, the maximum value must be greater than or equal to the minimum value")
}
if params[0] < MinimumBound || params[1] > MaximumBound {
return errors.New("part of the range is out of bounds for positive increment")
}
atomic.AddUint64(&s.increment, 1)
atomic.AddUint64(&s.minvalue, params[0])
atomic.AddUint64(&s.maxvalue, params[1])
}
// If three parameters create a range with a new step.
if len(params) == 3 {
// The step cannot be zero
if params[2] == 0 {
return errors.New("must have a non-zero step to increment by")
}
if params[2] < 0 {
// If the step is negative
// TODO: This is not yet implemented since uints have to be positive.
if params[0] < params[1] {
return errors.New("for a negative increment, the first value must be greater than or equal to the second value")
}
if params[1] < MinimumBound || params[0] > MaximumBound {
return errors.New("part of the range is out of bounds for negative increment")
}
} else {
// If the step is positive
if params[1] < params[0] {
return errors.New("for a positive increment, the second value must be greater than or equal to the first value")
}
if params[0] < MinimumBound || params[1] > MaximumBound {
return errors.New("part of the range is out of bounds for positive increment")
}
}
atomic.AddUint64(&s.increment, params[2])
atomic.AddUint64(&s.minvalue, params[0])
atomic.AddUint64(&s.maxvalue, params[1])
}
// If more than three parameters then return an error.
if len(params) > 3 {
return errors.New("too many arguments specified")
}
// Ensure unsigned subtraction won't lead to a problem.
if int(s.minvalue)-int(s.increment) < 0 {
return errors.New("the minimum value must be less than or equal to the step")
}
atomic.SwapUint64(&s.current, atomic.LoadUint64(&s.minvalue)-atomic.LoadUint64(&s.increment))
s.initialized = true
return nil
}
// Next updates the state of the Sequence and return the next item in the
// sequence. It will return an error if either the minimum or the maximal
// value has been reached.
// It is done in an atomic way.
func (s *AtomicSequence) Next() (uint64, error) {
atomic.AddUint64(&s.current, atomic.LoadUint64(&s.increment))
// Check for missed minimum condition
if atomic.LoadUint64(&s.current) < atomic.LoadUint64(&s.minvalue) {
return 0, errors.New("reached minimum bound of the sequence")
}
// Check for reached maximum condition
if atomic.LoadUint64(&s.current) > atomic.LoadUint64(&s.maxvalue) {
return 0, errors.New("reached maximum bound of sequence")
}
return atomic.LoadUint64(&s.current), nil
}
// Restart the sequence by resetting the current value. This is the only
// method that allows direct manipulation of the sequence state which violates
// the monotonically increasing or decreasing rule. Use with care and as a
// fail safe if required.
// It is done in an atomic way.
func (s *AtomicSequence) Restart() error {
// Ensure that the sequence has been initialized.
if !s.initialized {
return errors.New("sequence has not been initialized")
}
// Ensure unsigned subtraction won't lead to a problem.
if int(atomic.LoadUint64(&s.minvalue))-int(atomic.LoadUint64(&s.increment)) < 0 {
return errors.New("the minimum value must be less than or equal to the step")
}
// Set current based on the minvalue and the increment.
atomic.SwapUint64(&s.current, atomic.LoadUint64(&s.minvalue)-atomic.LoadUint64(&s.increment))
return nil
}
// Update the sequence to the current value. If the update value violates the
// monotonically increasing or decreasing rule, an error is returned.
// It is done in an atomic way.
func (s *AtomicSequence) Update(val uint64) error {
// monotonically increasing error
if atomic.LoadUint64(&s.increment) > 0 && val < atomic.LoadUint64(&s.current) {
return errors.New("cannot decrease monotonically increasing sequence")
}
// monotonically decreasing error
if atomic.LoadUint64(&s.increment) < 0 && val > atomic.LoadUint64(&s.current) {
return errors.New("cannot increase monotonically decreasing sequence")
}
// Update the sequence.
atomic.SwapUint64(&s.current, val)
return nil
}
// Current gives the current value of this sequence atomically.
func (s *AtomicSequence) Current() (uint64, error) {
if !s.initialized {
return 0, errors.New("sequence has not been initialized")
}
if !s.IsStarted() {
return 0, errors.New("sequence has not been started")
}
return atomic.LoadUint64(&s.current), nil
}
// IsStarted does atomic checks to see if this sequence has already started.
func (s *AtomicSequence) IsStarted() bool {
if !s.initialized {
return false
}
return !(atomic.LoadUint64(&s.current) < atomic.LoadUint64(&s.minvalue)) &&
atomic.LoadUint64(&s.current) < atomic.LoadUint64(&s.maxvalue)
}
// String returns a human readable representation of this sequence.
func (s *AtomicSequence) String() string {
d := fmt.Sprintf("incremented by %d between %d and %d", atomic.LoadUint64(&s.increment),
atomic.LoadUint64(&s.minvalue), atomic.LoadUint64(&s.maxvalue))
if !s.IsStarted() {
return fmt.Sprintf("Unstarted Sequence %s", d)
}
return fmt.Sprintf("Sequence at %d, %s", atomic.LoadUint64(&s.current), d)
}
// Dump uses atomic Loads to Marshal current data from a AtomicSequence into a JSON object
func (s *AtomicSequence) Dump() ([]byte, error) {
if !s.IsStarted() {
return nil, errors.New("cannot dump an uninitialized or unstarted sequence")
}
data := make(map[string]uint64)
data["current"] = atomic.LoadUint64(&s.current)
data["increment"] = atomic.LoadUint64(&s.increment)
data["minvalue"] = atomic.LoadUint64(&s.minvalue)
data["maxvalue"] = atomic.LoadUint64(&s.maxvalue)
return json.Marshal(data)
}
// Load loads data from Dump. If the input is not the same as the output from Dump() then it will return a error.
func (s *AtomicSequence) Load(data []byte) error {
if s.initialized {
return errors.New("cannot load into an initialized sequence")
}
vals := make(map[string]uint64)
if err := json.Unmarshal(data, &vals); err != nil {
return err
}
if val, ok := vals["current"]; !ok {
return errors.New("improperly formatted data or sequence version")
} else {
atomic.SwapUint64(&s.current, val)
}
if val, ok := vals["increment"]; !ok {
return errors.New("improperly formatted data or sequence version")
} else {
atomic.SwapUint64(&s.increment, val)
}
if val, ok := vals["minvalue"]; !ok {
return errors.New("improperly formatted data or sequence version")
} else {
atomic.SwapUint64(&s.minvalue, val)
}
if val, ok := vals["maxvalue"]; !ok {
return errors.New("improperly formatted data or sequence version")
} else {
atomic.SwapUint64(&s.maxvalue, val)
}
s.initialized = true
return nil
}