github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/procedure/index.go (about)

     1  package procedure
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/dgraph-io/badger/v2"
     7  
     8  	"github.com/onflow/flow-go/model/flow"
     9  	"github.com/onflow/flow-go/storage/badger/operation"
    10  )
    11  
    12  func InsertIndex(blockID flow.Identifier, index *flow.Index) func(tx *badger.Txn) error {
    13  	return func(tx *badger.Txn) error {
    14  		err := operation.IndexPayloadGuarantees(blockID, index.CollectionIDs)(tx)
    15  		if err != nil {
    16  			return fmt.Errorf("could not store guarantee index: %w", err)
    17  		}
    18  		err = operation.IndexPayloadSeals(blockID, index.SealIDs)(tx)
    19  		if err != nil {
    20  			return fmt.Errorf("could not store seal index: %w", err)
    21  		}
    22  		err = operation.IndexPayloadReceipts(blockID, index.ReceiptIDs)(tx)
    23  		if err != nil {
    24  			return fmt.Errorf("could not store receipts index: %w", err)
    25  		}
    26  		err = operation.IndexPayloadResults(blockID, index.ResultIDs)(tx)
    27  		if err != nil {
    28  			return fmt.Errorf("could not store results index: %w", err)
    29  		}
    30  		err = operation.IndexPayloadProtocolStateID(blockID, index.ProtocolStateID)(tx)
    31  		if err != nil {
    32  			return fmt.Errorf("could not store protocol state id: %w", err)
    33  		}
    34  		return nil
    35  	}
    36  }
    37  
    38  func RetrieveIndex(blockID flow.Identifier, index *flow.Index) func(tx *badger.Txn) error {
    39  	return func(tx *badger.Txn) error {
    40  		var collIDs []flow.Identifier
    41  		err := operation.LookupPayloadGuarantees(blockID, &collIDs)(tx)
    42  		if err != nil {
    43  			return fmt.Errorf("could not retrieve guarantee index: %w", err)
    44  		}
    45  		var sealIDs []flow.Identifier
    46  		err = operation.LookupPayloadSeals(blockID, &sealIDs)(tx)
    47  		if err != nil {
    48  			return fmt.Errorf("could not retrieve seal index: %w", err)
    49  		}
    50  		var receiptIDs []flow.Identifier
    51  		err = operation.LookupPayloadReceipts(blockID, &receiptIDs)(tx)
    52  		if err != nil {
    53  			return fmt.Errorf("could not retrieve receipts index: %w", err)
    54  		}
    55  		var resultsIDs []flow.Identifier
    56  		err = operation.LookupPayloadResults(blockID, &resultsIDs)(tx)
    57  		if err != nil {
    58  			return fmt.Errorf("could not retrieve results index: %w", err)
    59  		}
    60  		var stateID flow.Identifier
    61  		err = operation.LookupPayloadProtocolStateID(blockID, &stateID)(tx)
    62  		if err != nil {
    63  			return fmt.Errorf("could not retrieve protocol state id: %w", err)
    64  		}
    65  
    66  		*index = flow.Index{
    67  			CollectionIDs:   collIDs,
    68  			SealIDs:         sealIDs,
    69  			ReceiptIDs:      receiptIDs,
    70  			ResultIDs:       resultsIDs,
    71  			ProtocolStateID: stateID,
    72  		}
    73  		return nil
    74  	}
    75  }