/
thresholdevent.go
295 lines (231 loc) · 10.9 KB
/
thresholdevent.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package events
import (
"fmt"
"sync"
"golang.org/x/xerrors"
"github.com/iotaledger/hive.go/v2/cerrors"
"github.com/iotaledger/hive.go/v2/datastructure/thresholdmap"
"github.com/iotaledger/hive.go/v2/marshalutil"
)
// region ThresholdEvent ///////////////////////////////////////////////////////////////////////////////////////////////
// ThresholdEvent is a data structure that acts like a normal event but only triggers when the value that was reported
// using the Set method causes the corresponding identifier to reach a new threshold. It is stateful which means that it
// tracks the current level for all identifiers individually to not trigger the same event multiple times.
type ThresholdEvent struct {
thresholds *thresholdmap.ThresholdMap
currentLevels map[interface{}]int
currentLevelsMutex sync.Mutex
configuration *ThresholdEventConfiguration
*Event
}
// NewThresholdEvent is the constructor for the ThresholdEvent.
func NewThresholdEvent(options ...ThresholdEventOption) (thresholdEvent *ThresholdEvent) {
thresholdEvent = &ThresholdEvent{
currentLevels: make(map[interface{}]int),
thresholds: thresholdmap.New(thresholdmap.LowerThresholdMode),
configuration: NewThresholdEventConfiguration(options...),
}
if thresholdEvent.configuration.Thresholds == nil {
panic("missing WithThresholds option when calling NewThresholdEvent")
}
for i, threshold := range thresholdEvent.configuration.Thresholds {
thresholdEvent.registerThreshold(threshold, i+1)
}
thresholdEvent.Event = NewEvent(func(handler interface{}, params ...interface{}) {
thresholdEvent.configuration.CallbackTypecaster(handler, params[0].(interface{}), params[1].(int), params[2].(ThresholdEventTransition))
})
return
}
// ThresholdEventFromBytes unmarshals a collection of BranchIDs from a sequence of bytes.
func ThresholdEventFromBytes(bytes []byte, options ...ThresholdEventOption) (thresholdEvent *ThresholdEvent, consumedBytes int, err error) {
marshalUtil := marshalutil.New(bytes)
if thresholdEvent, err = ThresholdEventFromMarshalUtil(marshalUtil, options...); err != nil {
err = xerrors.Errorf("failed to parse ThresholdEvent from MarshalUtil: %w", err)
return
}
consumedBytes = marshalUtil.ReadOffset()
return
}
// ThresholdEventFromMarshalUtil unmarshals a ThresholdEvent using a MarshalUtil (for easier unmarshaling).
func ThresholdEventFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil, options ...ThresholdEventOption) (thresholdEvent *ThresholdEvent, err error) {
thresholdEvent = &ThresholdEvent{
currentLevels: make(map[interface{}]int),
thresholds: thresholdmap.New(thresholdmap.LowerThresholdMode),
configuration: NewThresholdEventConfiguration(options...),
}
if thresholdEvent.configuration.Thresholds == nil {
panic("missing WithThresholds option when calling ThresholdEventFromMarshalUtil")
}
if thresholdEvent.configuration.IdentifierParser == nil {
panic("missing WithIdentifierParser option when calling ThresholdEventFromMarshalUtil")
}
for i, threshold := range thresholdEvent.configuration.Thresholds {
thresholdEvent.registerThreshold(threshold, i+1)
}
thresholdEvent.Event = NewEvent(func(handler interface{}, params ...interface{}) {
thresholdEvent.configuration.CallbackTypecaster(handler, params[0].(interface{}), params[1].(int), params[2].(ThresholdEventTransition))
})
levelCount, err := marshalUtil.ReadUint64()
if err != nil {
err = xerrors.Errorf("failed to read level count (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
for i := uint64(0); i < levelCount; i++ {
value, valueErr := marshalUtil.ReadInt64()
if valueErr != nil {
err = xerrors.Errorf("failed to read level (%v): %w", valueErr, cerrors.ErrParseBytesFailed)
return
}
identifier, identifierErr := thresholdEvent.configuration.IdentifierParser(marshalUtil)
if identifierErr != nil {
err = xerrors.Errorf("failed to read identifier (%v): %w", identifierErr, cerrors.ErrParseBytesFailed)
return
}
thresholdEvent.currentLevels[identifier] = int(value)
}
return
}
// Set updates the value associated with the given identifier and triggers the Event if necessary.
func (t *ThresholdEvent) Set(identifier ThresholdEventIdentifier, newValue float64) (newLevel int, transition ThresholdEventTransition) {
t.currentLevelsMutex.Lock()
defer t.currentLevelsMutex.Unlock()
newLevel, levelReached := t.level(newValue)
if !levelReached {
if currentLevel, exists := t.currentLevels[identifier]; exists {
delete(t.currentLevels, identifier)
transition = t.trigger(identifier, currentLevel, newLevel)
}
return
}
currentLevel := t.currentLevels[identifier]
if currentLevel == newLevel {
return
}
t.currentLevels[identifier] = newLevel
transition = t.trigger(identifier, currentLevel, newLevel)
return
}
// Level returns the current level of the reached threshold for the given identity.
func (t *ThresholdEvent) Level(identifier ThresholdEventIdentifier) (level int) {
t.currentLevelsMutex.Lock()
defer t.currentLevelsMutex.Unlock()
return t.currentLevels[identifier]
}
// Bytes returns a marshaled version of the ThresholdEvent.
func (t *ThresholdEvent) Bytes() []byte {
t.currentLevelsMutex.Lock()
defer t.currentLevelsMutex.Unlock()
marshalUtil := marshalutil.New()
marshalUtil.WriteUint64(uint64(len(t.currentLevels)))
for key, value := range t.currentLevels {
marshalUtil.WriteInt64(int64(value))
marshalUtil.WriteBytes(key.(marshalutil.SimpleBinaryMarshaler).Bytes())
}
return marshalUtil.Bytes()
}
// level returns the level of the threshold that the given value represents (and a boolean flag indicating if no
// threshold was reached).
func (t *ThresholdEvent) level(value float64) (level int, levelReached bool) {
untypedLevel, exists := t.thresholds.Get(value)
if !exists {
return 0, false
}
return untypedLevel.(int), true
}
// trigger triggers the embedded Event with the correct parameters.
func (t *ThresholdEvent) trigger(branchID interface{}, oldLevel, newLevel int) (transition ThresholdEventTransition) {
if newLevel >= oldLevel {
transition = ThresholdLevelIncreased
for i := oldLevel + 1; i <= newLevel; i++ {
t.Event.Trigger(branchID, i, transition)
}
} else {
transition = ThresholdLevelDecreased
for i := oldLevel - 1; i >= newLevel; i-- {
t.Event.Trigger(branchID, i, transition)
}
}
return
}
// registerThreshold create a new threshold in the internal ThresholdMap.
func (t *ThresholdEvent) registerThreshold(threshold float64, level int) {
t.thresholds.Set(threshold, level)
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region ThresholdEventConfiguration //////////////////////////////////////////////////////////////////////////////////
// ThresholdEventConfiguration represents a collection of optional parameters that are used by the ThresholdEvent.
type ThresholdEventConfiguration struct {
Thresholds []float64
CallbackTypecaster ThresholdEventCallbackTypecaster
IdentifierParser ThresholdEventIdentifierParser
}
// NewThresholdEventConfiguration creates a ThresholdEventConfiguration from the given Options.
func NewThresholdEventConfiguration(options ...ThresholdEventOption) (configuration *ThresholdEventConfiguration) {
configuration = &ThresholdEventConfiguration{
Thresholds: make([]float64, 0),
CallbackTypecaster: func(handler interface{}, identifier interface{}, newLevel int, transition ThresholdEventTransition) {
handler.(func(identifier interface{}, newLevel int, transition ThresholdEventTransition))(identifier, newLevel, transition)
},
}
for _, option := range options {
option(configuration)
}
return configuration
}
// ThresholdEventOption is the type of the optional parameters of the ThresholdEvent.
type ThresholdEventOption func(*ThresholdEventConfiguration)
// WithThresholds sets the thresholds that are supposed to be used for the Triggers.
func WithThresholds(thresholds ...float64) ThresholdEventOption {
return func(options *ThresholdEventConfiguration) {
options.Thresholds = thresholds
}
}
// WithIdentifierParser sets the parser for the ThresholdEventIdentifier that is used to identify different entities.
func WithIdentifierParser(identifierParser ThresholdEventIdentifierParser) ThresholdEventOption {
return func(configuration *ThresholdEventConfiguration) {
configuration.IdentifierParser = identifierParser
}
}
// WithCallbackTypeCaster sets the method that is used to type cast the called callbacks to their correct types.
func WithCallbackTypeCaster(callbackTypeCaster ThresholdEventCallbackTypecaster) ThresholdEventOption {
return func(configuration *ThresholdEventConfiguration) {
configuration.CallbackTypecaster = callbackTypeCaster
}
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region ThresholdEventTransition /////////////////////////////////////////////////////////////////////////////////////
// ThresholdEventTransition is the type of the values that are used to indicate in which direction a threshold was
// passed.
type ThresholdEventTransition int
const (
// ThresholdLevelMaintained indicates that the reached threshold did not change.
ThresholdLevelMaintained ThresholdEventTransition = 0
// ThresholdLevelIncreased indicates that the new value is larger than the passed threshold.
ThresholdLevelIncreased ThresholdEventTransition = 1
// ThresholdLevelDecreased indicates that the new value is smaller than the passed threshold.
ThresholdLevelDecreased ThresholdEventTransition = -1
)
// String returns a human readable version of the ThresholdEventTransition.
func (t ThresholdEventTransition) String() string {
switch t {
case ThresholdLevelMaintained:
return "ThresholdLevelMaintained"
case ThresholdLevelIncreased:
return "ThresholdLevelIncreased"
case ThresholdLevelDecreased:
return "ThresholdLevelDecreased"
default:
panic(fmt.Sprintf("invalid ThresholdEventTransition (%d)", int(t)))
}
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region Types & Interfaces ///////////////////////////////////////////////////////////////////////////////////////////
// ThresholdEventIdentifier is the type that is used to address the identifiers of the entities whose values we are
// tracking.
type ThresholdEventIdentifier marshalutil.SimpleBinaryMarshaler
// ThresholdEventCallbackTypecaster defines the signature of the function that is used to convert the parameters to the
// types expected by the callbacks.
type ThresholdEventCallbackTypecaster func(handler interface{}, identifier interface{}, newLevel int, transition ThresholdEventTransition)
// ThresholdEventIdentifierParser defines the signature of the function that is used to parse the Identifiers.
type ThresholdEventIdentifierParser func(marshalUtil *marshalutil.MarshalUtil) (identifier interface{}, err error)
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////