# 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