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