-
Notifications
You must be signed in to change notification settings - Fork 179
/
events.go
88 lines (74 loc) · 3.03 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
// (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 batchInsert(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 batchInsert(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)
}
// 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
}
}