1 Star 0 Fork 0

妥協 / fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
db_helper.go 2.94 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package confighistory
import (
"bytes"
"encoding/binary"
"math"
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
"github.com/pkg/errors"
)
const (
keyPrefix = "s"
separatorByte = byte(0)
)
type compositeKey struct {
ns, key string
blockNum uint64
}
type compositeKV struct {
*compositeKey
value []byte
}
type dbProvider struct {
*leveldbhelper.Provider
}
type db struct {
*leveldbhelper.DBHandle
}
type batch struct {
*leveldbhelper.UpdateBatch
}
func newDBProvider(dbPath string) *dbProvider {
logger.Debugf("Opening db for config history: db path = %s", dbPath)
return &dbProvider{leveldbhelper.NewProvider(&leveldbhelper.Conf{DBPath: dbPath})}
}
func newBatch() *batch {
return &batch{leveldbhelper.NewUpdateBatch()}
}
func (p *dbProvider) getDB(id string) *db {
return &db{p.GetDBHandle(id)}
}
func (b *batch) add(ns, key string, blockNum uint64, value []byte) {
logger.Debugf("add() - {%s, %s, %d}", ns, key, blockNum)
k, v := encodeCompositeKey(ns, key, blockNum), value
b.Put(k, v)
}
func (d *db) writeBatch(batch *batch, sync bool) error {
return d.WriteBatch(batch.UpdateBatch, sync)
}
func (d *db) mostRecentEntryBelow(blockNum uint64, ns, key string) (*compositeKV, error) {
logger.Debugf("mostRecentEntryBelow() - {%s, %s, %d}", ns, key, blockNum)
if blockNum == 0 {
return nil, errors.New("blockNum should be greater than 0")
}
startKey := encodeCompositeKey(ns, key, blockNum-1)
itr := d.GetIterator(startKey, nil)
defer itr.Release()
if !itr.Next() {
logger.Debugf("Key no entry found. Returning nil")
return nil, nil
}
k, v := decodeCompositeKey(itr.Key()), itr.Value()
return &compositeKV{k, v}, nil
}
func (d *db) entryAt(blockNum uint64, ns, key string) (*compositeKV, error) {
logger.Debugf("entryAt() - {%s, %s, %d}", ns, key, blockNum)
keyBytes := encodeCompositeKey(ns, key, blockNum)
valBytes, err := d.Get(keyBytes)
if err != nil {
return nil, err
}
if valBytes == nil {
return nil, nil
}
k, v := decodeCompositeKey(keyBytes), valBytes
return &compositeKV{k, v}, nil
}
func encodeCompositeKey(ns, key string, blockNum uint64) []byte {
b := []byte(keyPrefix + ns)
b = append(b, separatorByte)
b = append(b, []byte(key)...)
return append(b, encodeBlockNum(blockNum)...)
}
func decodeCompositeKey(b []byte) *compositeKey {
blockNumStartIndex := len(b) - 8
nsKeyBytes, blockNumBytes := b[1:blockNumStartIndex], b[blockNumStartIndex:]
separatorIndex := bytes.Index(nsKeyBytes, []byte{separatorByte})
ns, key := nsKeyBytes[0:separatorIndex], nsKeyBytes[separatorIndex+1:]
return &compositeKey{string(ns), string(key), decodeBlockNum(blockNumBytes)}
}
func encodeBlockNum(blockNum uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, math.MaxUint64-blockNum)
return b
}
func decodeBlockNum(blockNumBytes []byte) uint64 {
return math.MaxUint64 - binary.BigEndian.Uint64(blockNumBytes)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liurenhao/fabric.git
git@gitee.com:liurenhao/fabric.git
liurenhao
fabric
fabric
v1.4.7

搜索帮助

344bd9b3 5694891 D2dac590 5694891