63 Star 181 Fork 3

Gitee 极速下载 / hyperledger-fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/hyperledger/fabric
克隆/下载
kv_encoding.go 2.68 KB
一键复制 编辑 原始数据 按行查看 历史
Chongxin Luo 提交于 2020-01-27 09:52 . Fix file license header.
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package history
import (
"bytes"
"github.com/hyperledger/fabric/common/ledger/util"
"github.com/pkg/errors"
)
type dataKey []byte
type rangeScan struct {
startKey, endKey []byte
}
var (
compositeKeySep = []byte{0x00} // used as a separator between different components of dataKey
dataKeyPrefix = []byte{'d'} // prefix added to dataKeys
savePointKey = []byte{'s'} // a single key in db for persisting savepoint
emptyValue = []byte{} // used to store as value for keys where only key needs to be stored (e.g., dataKeys)
)
// constructDataKey builds the key of the format namespace~len(key)~key~blocknum~trannum
// using an order preserving encoding so that history query results are ordered by height
// Note: this key format is different than the format in pre-v2.0 releases and requires
// a historydb rebuild when upgrading an older version to v2.0.
func constructDataKey(ns string, key string, blocknum uint64, trannum uint64) dataKey {
k := append([]byte(ns), compositeKeySep...)
k = append(k, util.EncodeOrderPreservingVarUint64(uint64(len(key)))...)
k = append(k, []byte(key)...)
k = append(k, compositeKeySep...)
k = append(k, util.EncodeOrderPreservingVarUint64(blocknum)...)
k = append(k, util.EncodeOrderPreservingVarUint64(trannum)...)
return dataKey(k)
}
// constructRangescanKeys returns start and endKey for performing a range scan
// that covers all the keys for <ns, key>.
// startKey = namespace~len(key)~key~
// endKey = namespace~len(key)~key~0xff
func constructRangeScan(ns string, key string) *rangeScan {
k := append([]byte(ns), compositeKeySep...)
k = append(k, util.EncodeOrderPreservingVarUint64(uint64(len(key)))...)
k = append(k, []byte(key)...)
k = append(k, compositeKeySep...)
return &rangeScan{
startKey: k,
endKey: append(k, 0xff),
}
}
func (r *rangeScan) decodeBlockNumTranNum(dataKey dataKey) (uint64, uint64, error) {
blockNumTranNumBytes := bytes.TrimPrefix(dataKey, r.startKey)
blockNum, blockBytesConsumed, err := util.DecodeOrderPreservingVarUint64(blockNumTranNumBytes)
if err != nil {
return 0, 0, err
}
tranNum, tranBytesConsumed, err := util.DecodeOrderPreservingVarUint64(blockNumTranNumBytes[blockBytesConsumed:])
if err != nil {
return 0, 0, err
}
// The following error should never happen. Keep the check just in case there is some unknown bug.
if blockBytesConsumed+tranBytesConsumed != len(blockNumTranNumBytes) {
return 0, 0, errors.Errorf("number of decoded bytes (%d) is not equal to the length of blockNumTranNumBytes (%d)",
blockBytesConsumed+tranBytesConsumed, len(blockNumTranNumBytes))
}
return blockNum, tranNum, nil
}
Go
1
https://gitee.com/mirrors/hyperledger-fabric.git
git@gitee.com:mirrors/hyperledger-fabric.git
mirrors
hyperledger-fabric
hyperledger-fabric
v2.1.1

搜索帮助