github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/mempool/backData.go (about) 1 package mempool 2 3 import ( 4 "github.com/onflow/flow-go/model/flow" 5 ) 6 7 // BackData represents the underlying data structure that is utilized by mempool.Backend, as the 8 // core structure of maintaining data on memory-pools. 9 // NOTE: BackData by default is not expected to provide concurrency-safe operations. As it is just the 10 // model layer of the mempool, the safety against concurrent operations are guaranteed by the Backend that 11 // is the control layer. 12 type BackData interface { 13 // Has checks if backdata already contains the entity with the given identifier. 14 Has(entityID flow.Identifier) bool 15 16 // Add adds the given entity to the backdata. 17 Add(entityID flow.Identifier, entity flow.Entity) bool 18 19 // Remove removes the entity with the given identifier. 20 Remove(entityID flow.Identifier) (flow.Entity, bool) 21 22 // Adjust adjusts the entity using the given function if the given identifier can be found. 23 // Returns a bool which indicates whether the entity was updated as well as the updated entity. 24 Adjust(entityID flow.Identifier, f func(flow.Entity) flow.Entity) (flow.Entity, bool) 25 26 // AdjustWithInit adjusts the entity using the given function if the given identifier can be found. When the 27 // entity is not found, it initializes the entity using the given init function and then applies the adjust function. 28 // Args: 29 // - entityID: the identifier of the entity to adjust. 30 // - adjust: the function that adjusts the entity. 31 // - init: the function that initializes the entity when it is not found. 32 // Returns: 33 // - the adjusted entity. 34 // 35 // - a bool which indicates whether the entity was adjusted. 36 AdjustWithInit(entityID flow.Identifier, adjust func(flow.Entity) flow.Entity, init func() flow.Entity) (flow.Entity, bool) 37 38 // GetWithInit returns the given entity from the backdata. If the entity does not exist, it creates a new entity 39 // using the factory function and stores it in the backdata. 40 // Args: 41 // - entityID: the identifier of the entity to get. 42 // - init: the function that initializes the entity when it is not found. 43 // Returns: 44 // - the entity. 45 // - a bool which indicates whether the entity was found (or created). 46 GetWithInit(entityID flow.Identifier, init func() flow.Entity) (flow.Entity, bool) 47 48 // ByID returns the given entity from the backdata. 49 ByID(entityID flow.Identifier) (flow.Entity, bool) 50 51 // Size returns the size of the backdata, i.e., total number of stored (entityId, entity) pairs. 52 Size() uint 53 54 // All returns all entities stored in the backdata. 55 All() map[flow.Identifier]flow.Entity 56 57 // Identifiers returns the list of identifiers of entities stored in the backdata. 58 Identifiers() flow.IdentifierList 59 60 // Entities returns the list of entities stored in the backdata. 61 Entities() []flow.Entity 62 63 // Clear removes all entities from the backdata. 64 Clear() 65 }