github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/operation/events.go (about) 1 package operation 2 3 import ( 4 "github.com/dgraph-io/badger/v2" 5 6 "github.com/onflow/flow-go/model/flow" 7 ) 8 9 func eventPrefix(prefix byte, blockID flow.Identifier, event flow.Event) []byte { 10 return makePrefix(prefix, blockID, event.TransactionID, event.TransactionIndex, event.EventIndex) 11 } 12 13 func InsertEvent(blockID flow.Identifier, event flow.Event) func(*badger.Txn) error { 14 return insert(eventPrefix(codeEvent, blockID, event), event) 15 } 16 17 func BatchInsertEvent(blockID flow.Identifier, event flow.Event) func(batch *badger.WriteBatch) error { 18 return batchWrite(eventPrefix(codeEvent, blockID, event), event) 19 } 20 21 func InsertServiceEvent(blockID flow.Identifier, event flow.Event) func(*badger.Txn) error { 22 return insert(eventPrefix(codeServiceEvent, blockID, event), event) 23 } 24 25 func BatchInsertServiceEvent(blockID flow.Identifier, event flow.Event) func(batch *badger.WriteBatch) error { 26 return batchWrite(eventPrefix(codeServiceEvent, blockID, event), event) 27 } 28 29 func RetrieveEvents(blockID flow.Identifier, transactionID flow.Identifier, events *[]flow.Event) func(*badger.Txn) error { 30 iterationFunc := eventIterationFunc(events) 31 return traverse(makePrefix(codeEvent, blockID, transactionID), iterationFunc) 32 } 33 34 func LookupEventsByBlockID(blockID flow.Identifier, events *[]flow.Event) func(*badger.Txn) error { 35 iterationFunc := eventIterationFunc(events) 36 return traverse(makePrefix(codeEvent, blockID), iterationFunc) 37 } 38 39 func LookupServiceEventsByBlockID(blockID flow.Identifier, events *[]flow.Event) func(*badger.Txn) error { 40 iterationFunc := eventIterationFunc(events) 41 return traverse(makePrefix(codeServiceEvent, blockID), iterationFunc) 42 } 43 44 func LookupEventsByBlockIDEventType(blockID flow.Identifier, eventType flow.EventType, events *[]flow.Event) func(*badger.Txn) error { 45 iterationFunc := eventFilterIterationFunc(events, eventType) 46 return traverse(makePrefix(codeEvent, blockID), iterationFunc) 47 } 48 49 func RemoveServiceEventsByBlockID(blockID flow.Identifier) func(*badger.Txn) error { 50 return removeByPrefix(makePrefix(codeServiceEvent, blockID)) 51 } 52 53 // BatchRemoveServiceEventsByBlockID removes all service events for the given blockID. 54 // No errors are expected during normal operation, even if no entries are matched. 55 // If Badger unexpectedly fails to process the request, the error is wrapped in a generic error and returned. 56 func BatchRemoveServiceEventsByBlockID(blockID flow.Identifier, batch *badger.WriteBatch) func(*badger.Txn) error { 57 return func(txn *badger.Txn) error { 58 return batchRemoveByPrefix(makePrefix(codeServiceEvent, blockID))(txn, batch) 59 } 60 } 61 62 func RemoveEventsByBlockID(blockID flow.Identifier) func(*badger.Txn) error { 63 return removeByPrefix(makePrefix(codeEvent, blockID)) 64 } 65 66 // BatchRemoveEventsByBlockID removes all events for the given blockID. 67 // No errors are expected during normal operation, even if no entries are matched. 68 // If Badger unexpectedly fails to process the request, the error is wrapped in a generic error and returned. 69 func BatchRemoveEventsByBlockID(blockID flow.Identifier, batch *badger.WriteBatch) func(*badger.Txn) error { 70 return func(txn *badger.Txn) error { 71 return batchRemoveByPrefix(makePrefix(codeEvent, blockID))(txn, batch) 72 } 73 74 } 75 76 // eventIterationFunc returns an in iteration function which returns all events found during traversal or iteration 77 func eventIterationFunc(events *[]flow.Event) func() (checkFunc, createFunc, handleFunc) { 78 return func() (checkFunc, createFunc, handleFunc) { 79 check := func(key []byte) bool { 80 return true 81 } 82 var val flow.Event 83 create := func() interface{} { 84 return &val 85 } 86 handle := func() error { 87 *events = append(*events, val) 88 return nil 89 } 90 return check, create, handle 91 } 92 } 93 94 // eventFilterIterationFunc returns an iteration function which filters the result by the given event type in the handleFunc 95 func eventFilterIterationFunc(events *[]flow.Event, eventType flow.EventType) func() (checkFunc, createFunc, handleFunc) { 96 return func() (checkFunc, createFunc, handleFunc) { 97 check := func(key []byte) bool { 98 return true 99 } 100 var val flow.Event 101 create := func() interface{} { 102 return &val 103 } 104 handle := func() error { 105 // filter out all events not of type eventType 106 if val.Type == eventType { 107 *events = append(*events, val) 108 } 109 return nil 110 } 111 return check, create, handle 112 } 113 }