Fabric 1.0源代码分析(21)Ledger #historydb(历史数据库)

# Fabric 1.0源代码笔记 之 Ledger #historydb(历史数据库)

## 1、historydb概述

historydb,用于存储所有块读写集中写集的内容。
代码分布在core/ledger/kvledger/history/historydb目录下,目录结构如下:

* historydb.go,定义核心接口HistoryDBProvider和HistoryDB。
* histmgr_helper.go,historydb工具函数。
* historyleveldb目录,historydb基于leveldb的实现。
     * historyleveldb.go,HistoryDBProvider和HistoryDB接口实现,即historyleveldb.HistoryDBProvider和historyleveldb.historyDB结构体及方法。
     * historyleveldb_query_executer.go,定义LevelHistoryDBQueryExecutor和historyScanner结构体及方法。

## 2、核心接口定义

HistoryDBProvider接口定义:

```go
type HistoryDBProvider interface {
    GetDBHandle(id string) (HistoryDB, error) //获取HistoryDB
    Close() //关闭所有HistoryDB
}
//代码在core/ledger/kvledger/history/historydb/historydb.go
```

HistoryDB接口定义:

```go
type HistoryDB interface {
     //构造 LevelHistoryDBQueryExecutor
    NewHistoryQueryExecutor(blockStore blkstorage.BlockStore) (ledger.HistoryQueryExecutor, error)
     //提交Block入historyDB
    Commit(block *common.Block) error
     //获取savePointKey,即version.Height
    GetLastSavepoint() (*version.Height, error)
     //是否应该恢复,比较lastAvailableBlock和Savepoint
    ShouldRecover(lastAvailableBlock uint64) ( bool, uint64, error)
     //提交丢失的块
    CommitLostBlock(block *common.Block) error
}
//代码在core/ledger/kvledger/history/historydb/historydb.go
```

补充ledger.HistoryQueryExecutor接口定义:执行历史记录查询。

```go
type HistoryQueryExecutor interface {
    GetHistoryForKey(namespace string, key string) (commonledger.ResultsIterator, error) //按key查历史记录
}
//代码在core/ledger/ledger_interface.go
```

## 3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值