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

     1  // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
     2  
     3  package stdmap
     4  
     5  import (
     6  	"github.com/koko1123/flow-go-1/model/flow"
     7  	"github.com/koko1123/flow-go-1/module/mempool"
     8  	"github.com/koko1123/flow-go-1/module/mempool/entity"
     9  	_ "github.com/koko1123/flow-go-1/utils/binstat"
    10  )
    11  
    12  // Hold all the missing collections.
    13  // Each entry is a missing collection, and all the blocks that contain
    14  // this collection
    15  type BlockByCollections struct {
    16  	*Backend
    17  }
    18  
    19  // BlockByCollectionBackdata contains all the collections is being requested,
    20  // for each collection it stores the blocks that contains the collection.
    21  // the Backdata is essentially map<collectionID>map<blockID>*ExecutableBlock
    22  type BlockByCollectionBackdata struct {
    23  	mempool.BackData
    24  }
    25  
    26  func NewBlockByCollections() *BlockByCollections {
    27  	return &BlockByCollections{NewBackend(WithEject(EjectPanic))}
    28  }
    29  
    30  func (b *BlockByCollections) Add(block *entity.BlocksByCollection) bool {
    31  	return b.Backend.Add(block)
    32  }
    33  
    34  func (b *BlockByCollections) Get(collID flow.Identifier) (*entity.BlocksByCollection, bool) {
    35  	backdata := &BlockByCollectionBackdata{b.backData}
    36  	return backdata.ByID(collID)
    37  }
    38  
    39  func (b *BlockByCollections) Run(f func(backdata *BlockByCollectionBackdata) error) error {
    40  	//bs1 := binstat.EnterTime(binstat.BinStdmap + ".w_lock.(BlockByCollections).Run")
    41  	b.Lock()
    42  	//binstat.Leave(bs1)
    43  
    44  	//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(BlockByCollections).Run)")
    45  	defer b.Unlock()
    46  	err := f(&BlockByCollectionBackdata{b.backData})
    47  	//binstat.Leave(bs2)
    48  
    49  	if err != nil {
    50  		return err
    51  	}
    52  	return nil
    53  }
    54  
    55  func (b *BlockByCollectionBackdata) ByID(id flow.Identifier) (*entity.BlocksByCollection, bool) {
    56  	e, exists := b.BackData.ByID(id)
    57  	if !exists {
    58  		return nil, false
    59  	}
    60  	block := e.(*entity.BlocksByCollection)
    61  	return block, true
    62  }