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