-
Notifications
You must be signed in to change notification settings - Fork 178
/
events.go
185 lines (158 loc) · 4.82 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
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
package badger
import (
"fmt"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/storage/badger/operation"
)
type Events struct {
db *badger.DB
cache *Cache
}
func NewEvents(collector module.CacheMetrics, db *badger.DB) *Events {
retrieve := func(key interface{}) func(tx *badger.Txn) (interface{}, error) {
blockID := key.(flow.Identifier)
var events []flow.Event
return func(tx *badger.Txn) (interface{}, error) {
err := operation.LookupEventsByBlockID(blockID, &events)(tx)
return events, handleError(err, flow.Event{})
}
}
return &Events{
db: db,
cache: newCache(collector, metrics.ResourceEvents,
withStore(noopStore),
withRetrieve(retrieve)),
}
}
func (e *Events) BatchStore(blockID flow.Identifier, blockEvents []flow.EventsList, batch storage.BatchStorage) error {
writeBatch := batch.GetWriter()
// pre-allocating and indexing slice is faster than appending
sliceSize := 0
for _, b := range blockEvents {
sliceSize += len(b)
}
combinedEvents := make([]flow.Event, sliceSize)
eventIndex := 0
for _, events := range blockEvents {
for _, event := range events {
err := operation.BatchInsertEvent(blockID, event)(writeBatch)
if err != nil {
return fmt.Errorf("cannot batch insert event: %w", err)
}
combinedEvents[eventIndex] = event
eventIndex++
}
}
callback := func() {
e.cache.Insert(blockID, combinedEvents)
}
batch.OnSucceed(callback)
return nil
}
// ByBlockID returns the events for the given block ID
func (e *Events) ByBlockID(blockID flow.Identifier) ([]flow.Event, error) {
tx := e.db.NewTransaction(false)
defer tx.Discard()
val, err := e.cache.Get(blockID)(tx)
if err != nil {
return nil, err
}
return val.([]flow.Event), nil
}
// ByBlockIDTransactionID returns the events for the given block ID and transaction ID
func (e *Events) ByBlockIDTransactionID(blockID flow.Identifier, txID flow.Identifier) ([]flow.Event, error) {
events, err := e.ByBlockID(blockID)
if err != nil {
return nil, handleError(err, flow.Event{})
}
var matched []flow.Event
for _, event := range events {
if event.TransactionID == txID {
matched = append(matched, event)
}
}
return matched, nil
}
func (e *Events) ByBlockIDTransactionIndex(blockID flow.Identifier, txIndex uint32) ([]flow.Event, error) {
events, err := e.ByBlockID(blockID)
if err != nil {
return nil, handleError(err, flow.Event{})
}
var matched []flow.Event
for _, event := range events {
if event.TransactionIndex == txIndex {
matched = append(matched, event)
}
}
return matched, nil
}
// ByBlockIDEventType returns the events for the given block ID and event type
func (e *Events) ByBlockIDEventType(blockID flow.Identifier, eventType flow.EventType) ([]flow.Event, error) {
events, err := e.ByBlockID(blockID)
if err != nil {
return nil, handleError(err, flow.Event{})
}
var matched []flow.Event
for _, event := range events {
if event.Type == eventType {
matched = append(matched, event)
}
}
return matched, nil
}
// RemoveByBlockID removes events by block ID
func (e *Events) RemoveByBlockID(blockID flow.Identifier) error {
return e.db.Update(operation.RemoveEventsByBlockID(blockID))
}
type ServiceEvents struct {
db *badger.DB
cache *Cache
}
func NewServiceEvents(collector module.CacheMetrics, db *badger.DB) *ServiceEvents {
retrieve := func(key interface{}) func(tx *badger.Txn) (interface{}, error) {
blockID := key.(flow.Identifier)
var events []flow.Event
return func(tx *badger.Txn) (interface{}, error) {
err := operation.LookupServiceEventsByBlockID(blockID, &events)(tx)
return events, handleError(err, flow.Event{})
}
}
return &ServiceEvents{
db: db,
cache: newCache(collector, metrics.ResourceEvents,
withStore(noopStore),
withRetrieve(retrieve)),
}
}
func (e *ServiceEvents) BatchStore(blockID flow.Identifier, events []flow.Event, batch storage.BatchStorage) error {
writeBatch := batch.GetWriter()
for _, event := range events {
err := operation.BatchInsertServiceEvent(blockID, event)(writeBatch)
if err != nil {
return fmt.Errorf("cannot batch insert service event: %w", err)
}
}
callback := func() {
e.cache.Insert(blockID, events)
}
batch.OnSucceed(callback)
return nil
}
// ByBlockID returns the events for the given block ID
func (e *ServiceEvents) ByBlockID(blockID flow.Identifier) ([]flow.Event, error) {
tx := e.db.NewTransaction(false)
defer tx.Discard()
val, err := e.cache.Get(blockID)(tx)
if err != nil {
return nil, err
}
return val.([]flow.Event), nil
}
// RemoveByBlockID removes service events by block ID
func (e *ServiceEvents) RemoveByBlockID(blockID flow.Identifier) error {
return e.db.Update(operation.RemoveServiceEventsByBlockID(blockID))
}