1 Star 0 Fork 0

妥協/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
redolog.go 1.96 KB
一键复制 编辑 原始数据 按行查看 历史
manish 提交于 2019-08-20 19:56 . Check HistoryDB format
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package statecouchdb
import (
"bytes"
"encoding/gob"
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
)
var redologKeyPrefix = []byte{byte(0)}
type redoLoggerProvider struct {
leveldbProvider *leveldbhelper.Provider
}
type redoLogger struct {
dbName string
dbHandle *leveldbhelper.DBHandle
}
type redoRecord struct {
UpdateBatch *statedb.UpdateBatch
Version *version.Height
}
func newRedoLoggerProvider(dirPath string) (*redoLoggerProvider, error) {
provider, err := leveldbhelper.NewProvider(&leveldbhelper.Conf{DBPath: dirPath})
if err != nil {
return nil, err
}
return &redoLoggerProvider{leveldbProvider: provider}, nil
}
func (p *redoLoggerProvider) newRedoLogger(dbName string) *redoLogger {
return &redoLogger{
dbHandle: p.leveldbProvider.GetDBHandle(dbName),
}
}
func (p *redoLoggerProvider) close() {
p.leveldbProvider.Close()
}
func (l *redoLogger) persist(r *redoRecord) error {
k := encodeRedologKey(l.dbName)
v, err := encodeRedologVal(r)
if err != nil {
return err
}
return l.dbHandle.Put(k, v, true)
}
func (l *redoLogger) load() (*redoRecord, error) {
k := encodeRedologKey(l.dbName)
v, err := l.dbHandle.Get(k)
if err != nil || v == nil {
return nil, err
}
return decodeRedologVal(v)
}
func encodeRedologKey(dbName string) []byte {
return append(redologKeyPrefix, []byte(dbName)...)
}
func encodeRedologVal(r *redoRecord) ([]byte, error) {
buf := bytes.NewBuffer(nil)
encoder := gob.NewEncoder(buf)
if err := encoder.Encode(r); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func decodeRedologVal(b []byte) (*redoRecord, error) {
decoder := gob.NewDecoder(bytes.NewBuffer(b))
var r *redoRecord
if err := decoder.Decode(&r); err != nil {
return nil, err
}
return r, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liurenhao/fabric.git
git@gitee.com:liurenhao/fabric.git
liurenhao
fabric
fabric
v2.0.0-beta

搜索帮助