github.com/koko1123/flow-go-1@v0.29.6/module/mempool/stdmap/backdata/mapBackData.go (about)

     1  package backdata
     2  
     3  import (
     4  	"github.com/koko1123/flow-go-1/model/flow"
     5  )
     6  
     7  // MapBackData implements a map-based generic memory BackData backed by a Go map.
     8  type MapBackData struct {
     9  	// NOTE: as a BackData implementation, MapBackData must be non-blocking.
    10  	// Concurrency management is done by overlay Backend.
    11  	entities map[flow.Identifier]flow.Entity
    12  }
    13  
    14  func NewMapBackData() *MapBackData {
    15  	bd := &MapBackData{
    16  		entities: make(map[flow.Identifier]flow.Entity),
    17  	}
    18  	return bd
    19  }
    20  
    21  // Has checks if backdata already contains the entity with the given identifier.
    22  func (b MapBackData) Has(entityID flow.Identifier) bool {
    23  	_, exists := b.entities[entityID]
    24  	return exists
    25  }
    26  
    27  // Add adds the given entity to the backdata.
    28  func (b *MapBackData) Add(entityID flow.Identifier, entity flow.Entity) bool {
    29  	_, exists := b.entities[entityID]
    30  	if exists {
    31  		return false
    32  	}
    33  	b.entities[entityID] = entity
    34  	return true
    35  }
    36  
    37  // Remove removes the entity with the given identifier.
    38  func (b *MapBackData) Remove(entityID flow.Identifier) (flow.Entity, bool) {
    39  	entity, exists := b.entities[entityID]
    40  	if !exists {
    41  		return nil, false
    42  	}
    43  	delete(b.entities, entityID)
    44  	return entity, true
    45  }
    46  
    47  // Adjust adjusts the entity using the given function if the given identifier can be found.
    48  // Returns a bool which indicates whether the entity was updated as well as the updated entity.
    49  func (b *MapBackData) Adjust(entityID flow.Identifier, f func(flow.Entity) flow.Entity) (flow.Entity, bool) {
    50  	entity, ok := b.entities[entityID]
    51  	if !ok {
    52  		return nil, false
    53  	}
    54  	newentity := f(entity)
    55  	newentityID := newentity.ID()
    56  
    57  	delete(b.entities, entityID)
    58  	b.entities[newentityID] = newentity
    59  	return newentity, true
    60  }
    61  
    62  // ByID returns the given entity from the backdata.
    63  func (b MapBackData) ByID(entityID flow.Identifier) (flow.Entity, bool) {
    64  	entity, exists := b.entities[entityID]
    65  	if !exists {
    66  		return nil, false
    67  	}
    68  	return entity, true
    69  }
    70  
    71  // Size returns the size of the backdata, i.e., total number of stored (entityId, entity)
    72  func (b MapBackData) Size() uint {
    73  	return uint(len(b.entities))
    74  }
    75  
    76  // All returns all entities stored in the backdata.
    77  func (b MapBackData) All() map[flow.Identifier]flow.Entity {
    78  	entities := make(map[flow.Identifier]flow.Entity)
    79  	for entityID, entity := range b.entities {
    80  		entities[entityID] = entity
    81  	}
    82  	return entities
    83  }
    84  
    85  // Identifiers returns the list of identifiers of entities stored in the backdata.
    86  func (b MapBackData) Identifiers() flow.IdentifierList {
    87  	ids := make(flow.IdentifierList, len(b.entities))
    88  	i := 0
    89  	for entityID := range b.entities {
    90  		ids[i] = entityID
    91  		i++
    92  	}
    93  	return ids
    94  }
    95  
    96  // Entities returns the list of entities stored in the backdata.
    97  func (b MapBackData) Entities() []flow.Entity {
    98  	entities := make([]flow.Entity, len(b.entities))
    99  	i := 0
   100  	for _, entity := range b.entities {
   101  		entities[i] = entity
   102  		i++
   103  	}
   104  	return entities
   105  }
   106  
   107  // Clear removes all entities from the backdata.
   108  func (b *MapBackData) Clear() {
   109  	b.entities = make(map[flow.Identifier]flow.Entity)
   110  }