-
Notifications
You must be signed in to change notification settings - Fork 178
/
events.go
115 lines (96 loc) · 4.29 KB
/
events.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
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package operation
import (
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/flow"
)
func eventPrefix(prefix byte, blockID flow.Identifier, event flow.Event) []byte {
return makePrefix(prefix, blockID, event.TransactionID, event.TransactionIndex, event.EventIndex)
}
func InsertEvent(blockID flow.Identifier, event flow.Event) func(*badger.Txn) error {
return insert(eventPrefix(codeEvent, blockID, event), event)
}
func BatchInsertEvent(blockID flow.Identifier, event flow.Event) func(batch *badger.WriteBatch) error {
return batchWrite(eventPrefix(codeEvent, blockID, event), event)
}
func InsertServiceEvent(blockID flow.Identifier, event flow.Event) func(*badger.Txn) error {
return insert(eventPrefix(codeServiceEvent, blockID, event), event)
}
func BatchInsertServiceEvent(blockID flow.Identifier, event flow.Event) func(batch *badger.WriteBatch) error {
return batchWrite(eventPrefix(codeServiceEvent, blockID, event), event)
}
func RetrieveEvents(blockID flow.Identifier, transactionID flow.Identifier, events *[]flow.Event) func(*badger.Txn) error {
iterationFunc := eventIterationFunc(events)
return traverse(makePrefix(codeEvent, blockID, transactionID), iterationFunc)
}
func LookupEventsByBlockID(blockID flow.Identifier, events *[]flow.Event) func(*badger.Txn) error {
iterationFunc := eventIterationFunc(events)
return traverse(makePrefix(codeEvent, blockID), iterationFunc)
}
func LookupServiceEventsByBlockID(blockID flow.Identifier, events *[]flow.Event) func(*badger.Txn) error {
iterationFunc := eventIterationFunc(events)
return traverse(makePrefix(codeServiceEvent, blockID), iterationFunc)
}
func LookupEventsByBlockIDEventType(blockID flow.Identifier, eventType flow.EventType, events *[]flow.Event) func(*badger.Txn) error {
iterationFunc := eventFilterIterationFunc(events, eventType)
return traverse(makePrefix(codeEvent, blockID), iterationFunc)
}
func RemoveServiceEventsByBlockID(blockID flow.Identifier) func(*badger.Txn) error {
return removeByPrefix(makePrefix(codeServiceEvent, blockID))
}
// BatchRemoveServiceEventsByBlockID removes all service events for the given blockID.
// No errors are expected during normal operation, even if no entries are matched.
// If Badger unexpectedly fails to process the request, the error is wrapped in a generic error and returned.
func BatchRemoveServiceEventsByBlockID(blockID flow.Identifier, batch *badger.WriteBatch) func(*badger.Txn) error {
return func(txn *badger.Txn) error {
return batchRemoveByPrefix(makePrefix(codeServiceEvent, blockID))(txn, batch)
}
}
func RemoveEventsByBlockID(blockID flow.Identifier) func(*badger.Txn) error {
return removeByPrefix(makePrefix(codeEvent, blockID))
}
// BatchRemoveEventsByBlockID removes all events for the given blockID.
// No errors are expected during normal operation, even if no entries are matched.
// If Badger unexpectedly fails to process the request, the error is wrapped in a generic error and returned.
func BatchRemoveEventsByBlockID(blockID flow.Identifier, batch *badger.WriteBatch) func(*badger.Txn) error {
return func(txn *badger.Txn) error {
return batchRemoveByPrefix(makePrefix(codeEvent, blockID))(txn, batch)
}
}
// eventIterationFunc returns an in iteration function which returns all events found during traversal or iteration
func eventIterationFunc(events *[]flow.Event) func() (checkFunc, createFunc, handleFunc) {
return func() (checkFunc, createFunc, handleFunc) {
check := func(key []byte) bool {
return true
}
var val flow.Event
create := func() interface{} {
return &val
}
handle := func() error {
*events = append(*events, val)
return nil
}
return check, create, handle
}
}
// eventFilterIterationFunc returns an iteration function which filters the result by the given event type in the handleFunc
func eventFilterIterationFunc(events *[]flow.Event, eventType flow.EventType) func() (checkFunc, createFunc, handleFunc) {
return func() (checkFunc, createFunc, handleFunc) {
check := func(key []byte) bool {
return true
}
var val flow.Event
create := func() interface{} {
return &val
}
handle := func() error {
// filter out all events not of type eventType
if val.Type == eventType {
*events = append(*events, val)
}
return nil
}
return check, create, handle
}
}