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

     1  // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
     2  
     3  package stdmap
     4  
     5  import (
     6  	"fmt"
     7  	"time"
     8  
     9  	"github.com/koko1123/flow-go-1/model/flow"
    10  )
    11  
    12  // Times implements the times memory pool used to store time.Times for an idetifier to track transaction metrics in
    13  // access nodes
    14  type Times struct {
    15  	*Backend
    16  }
    17  
    18  // NewTimes creates a new memory pool for times
    19  func NewTimes(limit uint) (*Times, error) {
    20  	t := &Times{
    21  		Backend: NewBackend(WithLimit(limit)),
    22  	}
    23  
    24  	return t, nil
    25  }
    26  
    27  type Time struct {
    28  	id flow.Identifier
    29  	ti time.Time
    30  }
    31  
    32  func (t *Time) ID() flow.Identifier {
    33  	return t.id
    34  }
    35  
    36  func (t *Time) Checksum() flow.Identifier {
    37  	return t.id
    38  }
    39  
    40  // Add adds a time to the mempool.
    41  func (t *Times) Add(id flow.Identifier, ti time.Time) bool {
    42  	return t.Backend.Add(&Time{id, ti})
    43  }
    44  
    45  // ByID returns the time with the given ID from the mempool.
    46  func (t *Times) ByID(id flow.Identifier) (time.Time, bool) {
    47  	entity, exists := t.Backend.ByID(id)
    48  	if !exists {
    49  		return time.Time{}, false
    50  	}
    51  	tt, ok := entity.(*Time)
    52  	if !ok {
    53  		panic(fmt.Sprintf("invalid entity in times pool (%T)", entity))
    54  	}
    55  	return tt.ti, true
    56  }
    57  
    58  // Remove removes the time with the given ID.
    59  func (t *Times) Remove(id flow.Identifier) bool {
    60  	return t.Backend.Remove(id)
    61  }