github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/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/osdi23p228/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, error) { 19 cache := map[string]bool{} 20 itr, err := bookkeeper.GetIterator(nil, nil) 21 if err != nil { 22 return nil, err 23 } 24 defer itr.Release() 25 for itr.Next() { 26 namespace := string(itr.Key()) 27 cache[namespace] = true 28 } 29 return &metadataHint{cache, bookkeeper}, nil 30 } 31 32 func (h *metadataHint) metadataEverUsedFor(namespace string) bool { 33 return h.cache[namespace] 34 } 35 36 func (h *metadataHint) setMetadataUsedFlag(updates *UpdateBatch) { 37 batch := h.bookkeeper.NewUpdateBatch() 38 for ns := range filterNamespacesThatHasMetadata(updates) { 39 if h.cache[ns] { 40 continue 41 } 42 h.cache[ns] = true 43 batch.Put([]byte(ns), []byte{}) 44 } 45 h.bookkeeper.WriteBatch(batch, true) 46 } 47 48 func filterNamespacesThatHasMetadata(updates *UpdateBatch) map[string]bool { 49 namespaces := map[string]bool{} 50 pubUpdates, hashUpdates := updates.PubUpdates, updates.HashUpdates 51 // add ns for public data 52 for _, ns := range pubUpdates.GetUpdatedNamespaces() { 53 for _, vv := range updates.PubUpdates.GetUpdates(ns) { 54 if vv.Metadata == nil { 55 continue 56 } 57 namespaces[ns] = true 58 } 59 } 60 // add ns for private hashes 61 for ns, nsBatch := range hashUpdates.UpdateMap { 62 for _, coll := range nsBatch.GetCollectionNames() { 63 for _, vv := range nsBatch.GetUpdates(coll) { 64 if vv.Metadata == nil { 65 continue 66 } 67 namespaces[ns] = true 68 } 69 } 70 } 71 return namespaces 72 }