github.com/koko1123/flow-go-1@v0.29.6/storage/badger/procedure/index.go (about)

     1  package procedure
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/dgraph-io/badger/v3"
     7  
     8  	"github.com/koko1123/flow-go-1/model/flow"
     9  	"github.com/koko1123/flow-go-1/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  		return nil
    31  	}
    32  }
    33  
    34  func RetrieveIndex(blockID flow.Identifier, index *flow.Index) func(tx *badger.Txn) error {
    35  	return func(tx *badger.Txn) error {
    36  		var collIDs []flow.Identifier
    37  		err := operation.LookupPayloadGuarantees(blockID, &collIDs)(tx)
    38  		if err != nil {
    39  			return fmt.Errorf("could not retrieve guarantee index: %w", err)
    40  		}
    41  		var sealIDs []flow.Identifier
    42  		err = operation.LookupPayloadSeals(blockID, &sealIDs)(tx)
    43  		if err != nil {
    44  			return fmt.Errorf("could not retrieve seal index: %w", err)
    45  		}
    46  		var receiptIDs []flow.Identifier
    47  		err = operation.LookupPayloadReceipts(blockID, &receiptIDs)(tx)
    48  		if err != nil {
    49  			return fmt.Errorf("could not retrieve receipts index: %w", err)
    50  		}
    51  		var resultsIDs []flow.Identifier
    52  		err = operation.LookupPayloadResults(blockID, &resultsIDs)(tx)
    53  		if err != nil {
    54  			return fmt.Errorf("could not retrieve receipts index: %w", err)
    55  		}
    56  
    57  		*index = flow.Index{
    58  			CollectionIDs: collIDs,
    59  			SealIDs:       sealIDs,
    60  			ReceiptIDs:    receiptIDs,
    61  			ResultIDs:     resultsIDs,
    62  		}
    63  		return nil
    64  	}
    65  }