github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/pvtdatastorage/persistent_msgs_helper.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package pvtdatastorage 8 9 func newExpiryData() *ExpiryData { 10 return &ExpiryData{Map: make(map[string]*NamespaceExpiryData)} 11 } 12 13 func (e *ExpiryData) getOrCreateCollections(ns string) *NamespaceExpiryData { 14 nsExpiryData, ok := e.Map[ns] 15 if !ok { 16 nsExpiryData = &NamespaceExpiryData{ 17 PresentData: make(map[string]*TxNums), 18 MissingData: make(map[string]bool), 19 BootKVHashes: make(map[string]*TxNums), 20 } 21 e.Map[ns] = nsExpiryData 22 } else { 23 // due to protobuf encoding/decoding, the previously 24 // initialized map could be a nil now due to 0 length. 25 // Hence, we need to reinitialize the map. 26 if nsExpiryData.PresentData == nil { 27 nsExpiryData.PresentData = make(map[string]*TxNums) 28 } 29 if nsExpiryData.MissingData == nil { 30 nsExpiryData.MissingData = make(map[string]bool) 31 } 32 if nsExpiryData.BootKVHashes == nil { 33 nsExpiryData.BootKVHashes = make(map[string]*TxNums) 34 } 35 } 36 return nsExpiryData 37 } 38 39 func (e *ExpiryData) addPresentData(ns, coll string, txNum uint64) { 40 nsExpiryData := e.getOrCreateCollections(ns) 41 42 txNums, ok := nsExpiryData.PresentData[coll] 43 if !ok { 44 txNums = &TxNums{} 45 nsExpiryData.PresentData[coll] = txNums 46 } 47 txNums.List = append(txNums.List, txNum) 48 } 49 50 func (e *ExpiryData) addMissingData(ns, coll string) { 51 nsExpiryData := e.getOrCreateCollections(ns) 52 nsExpiryData.MissingData[coll] = true 53 } 54 55 func (e *ExpiryData) addBootKVHash(ns, coll string, txNum uint64) { 56 nsExpiryData := e.getOrCreateCollections(ns) 57 58 txNums, ok := nsExpiryData.BootKVHashes[coll] 59 if !ok { 60 txNums = &TxNums{} 61 nsExpiryData.BootKVHashes[coll] = txNums 62 } 63 txNums.List = append(txNums.List, txNum) 64 } 65 66 func (h *BootKVHashes) toMap() map[string][]byte { 67 m := make(map[string][]byte, len(h.List)) 68 for _, kv := range h.List { 69 m[string(kv.KeyHash)] = kv.ValueHash 70 } 71 return m 72 } 73 74 func newCollElgInfo(nsCollMap map[string][]string) *CollElgInfo { 75 m := &CollElgInfo{NsCollMap: map[string]*CollNames{}} 76 for ns, colls := range nsCollMap { 77 collNames, ok := m.NsCollMap[ns] 78 if !ok { 79 collNames = &CollNames{} 80 m.NsCollMap[ns] = collNames 81 } 82 collNames.Entries = colls 83 } 84 return m 85 }