github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/txmgmt/storageutil/metadata.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package storageutil 8 9 import ( 10 "github.com/golang/protobuf/proto" 11 "github.com/hyperledger/fabric-protos-go/ledger/rwset/kvrwset" 12 "github.com/hyperledger/fabric/core/ledger/util" 13 ) 14 15 // SerializeMetadata serializes metadata entries for storing in statedb 16 func SerializeMetadata(metadataEntries []*kvrwset.KVMetadataEntry) ([]byte, error) { 17 metadata := &kvrwset.KVMetadataWrite{Entries: metadataEntries} 18 return proto.Marshal(metadata) 19 } 20 21 // SerializeMetadataByMap takes the metadata entries in the form of a map and serializes the metadata for storing in statedb 22 func SerializeMetadataByMap(metadataMap map[string][]byte) ([]byte, error) { 23 if metadataMap == nil { 24 return nil, nil 25 } 26 names := util.GetSortedKeys(metadataMap) 27 metadataEntries := []*kvrwset.KVMetadataEntry{} 28 for _, k := range names { 29 metadataEntries = append(metadataEntries, &kvrwset.KVMetadataEntry{Name: k, Value: metadataMap[k]}) 30 } 31 return SerializeMetadata(metadataEntries) 32 } 33 34 // DeserializeMetadata deserializes metadata bytes from statedb 35 func DeserializeMetadata(metadataBytes []byte) (map[string][]byte, error) { 36 if metadataBytes == nil { 37 return nil, nil 38 } 39 metadata := &kvrwset.KVMetadataWrite{} 40 if err := proto.Unmarshal(metadataBytes, metadata); err != nil { 41 return nil, err 42 } 43 m := make(map[string][]byte, len(metadata.Entries)) 44 for _, metadataEntry := range metadata.Entries { 45 m[metadataEntry.Name] = metadataEntry.Value 46 } 47 return m, nil 48 }