github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/txmgmt/privacyenabledstate/optimization.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package privacyenabledstate
     8  
     9  import (
    10  	"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
    11  )
    12  
    13  type metadataHint struct {
    14  	cache      map[string]bool
    15  	bookkeeper *leveldbhelper.DBHandle
    16  }
    17  
    18  func newMetadataHint(bookkeeper *leveldbhelper.DBHandle) *metadataHint {
    19  	cache := map[string]bool{}
    20  	itr := bookkeeper.GetIterator(nil, nil)
    21  	defer itr.Release()
    22  	for itr.Next() {
    23  		namespace := string(itr.Key())
    24  		cache[namespace] = true
    25  	}
    26  	return &metadataHint{cache, bookkeeper}
    27  }
    28  
    29  func (h *metadataHint) metadataEverUsedFor(namespace string) bool {
    30  	return h.cache[namespace]
    31  }
    32  
    33  func (h *metadataHint) setMetadataUsedFlag(updates *UpdateBatch) {
    34  	batch := leveldbhelper.NewUpdateBatch()
    35  	for ns := range filterNamespacesThatHasMetadata(updates) {
    36  		if h.cache[ns] {
    37  			continue
    38  		}
    39  		h.cache[ns] = true
    40  		batch.Put([]byte(ns), []byte{})
    41  	}
    42  	h.bookkeeper.WriteBatch(batch, true)
    43  }
    44  
    45  func filterNamespacesThatHasMetadata(updates *UpdateBatch) map[string]bool {
    46  	namespaces := map[string]bool{}
    47  	pubUpdates, hashUpdates := updates.PubUpdates, updates.HashUpdates
    48  	// add ns for public data
    49  	for _, ns := range pubUpdates.GetUpdatedNamespaces() {
    50  		for _, vv := range updates.PubUpdates.GetUpdates(ns) {
    51  			if vv.Metadata == nil {
    52  				continue
    53  			}
    54  			namespaces[ns] = true
    55  		}
    56  	}
    57  	// add ns for private hashes
    58  	for ns, nsBatch := range hashUpdates.UpdateMap {
    59  		for _, coll := range nsBatch.GetCollectionNames() {
    60  			for _, vv := range nsBatch.GetUpdates(coll) {
    61  				if vv.Metadata == nil {
    62  					continue
    63  				}
    64  				namespaces[ns] = true
    65  			}
    66  		}
    67  	}
    68  	return namespaces
    69  }