blockfiledb

package
v3.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2023 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCorrupt is returns when the log is corrupt.
	ErrCorrupt = errors.New("log corrupt")

	// ErrClosed is returned when an operation cannot be completed because
	// the log is closed.
	ErrClosed = errors.New("log closed")

	// ErrNotFound is returned when an entry is not found.
	ErrNotFound = errors.New("not found")

	// ErrOutOfOrder is returned from Write() when the index is not equal to
	// LastIndex()+1. It's required that log monotonically grows by one and has
	// no gaps. Thus, the series 10,11,12,13,14 is valid, but 10,11,13,14 is
	// not because there's a gap between 11 and 13. Also, 10,12,11,13 is not
	// valid because 12 and 11 are out of order.
	ErrOutOfOrder = errors.New("out of order")

	// ErrInvalidateIndex is returned when entry file is not legal
	ErrInvalidateIndex = errors.New("invalidate rfile index")

	// ErrBlockWrite is returned when write error
	ErrBlockWrite = errors.New("write block wfile size invalidate")
)
View Source
var DbType_Mysql = "mysql"

nolint

View Source
var DefaultOptions = &Options{
	NoSync:           false,
	SegmentSize:      67108864,
	SegmentCacheSize: 25,
	NoCopy:           false,
	UseMmap:          true,
}

DefaultOptions for Open().

Functions

func FileIndexToString

func FileIndexToString(fiIndex *storePb.StoreInfo) string

FileIndexToString format store info into a string

@Description:
@param fiIndex
@return string

Types

type Batch

type Batch struct {
	// contains filtered or unexported fields
}

Batch of entries. Used to write multiple entries at once using WriteBatch().

func (*Batch) Write

func (b *Batch) Write(index uint64, data []byte)

Write an entry to the batch

type BlockFile

type BlockFile struct {
	// contains filtered or unexported fields
}

BlockFile represents a block to rfile

func ImportOpen

func ImportOpen(opts *Options, logger protocol.Logger, meta meta.MetaData, height uint64) (*BlockFile, error)

ImportOpen , create block file by snapshot data

@Description:
@param opts
@param logger
@param meta
@param height
@return *BlockFile
@return error

func Open

func Open(path string, opts *Options, logger protocol.Logger, meta meta.MetaData) (*BlockFile, error)

Open a new write ahead log

@Description:
@param height
@param opts
@param logger
@param meta
@return *BlockFile
@return error

func Open(path string, opts *Options, logger protocol.Logger) (*BlockFile, error) {

func (*BlockFile) ClearCache

func (l *BlockFile) ClearCache() error

ClearCache clears the segment cache

func (*BlockFile) Close

func (l *BlockFile) Close() error

Close the log.

func (*BlockFile) LastIndex

func (l *BlockFile) LastIndex() (index uint64, err error)

LastIndex returns the index of the last entry in the log. Returns zero when log has no entries.

func (*BlockFile) ReadFileSection

func (l *BlockFile) ReadFileSection(fiIndex *storePb.StoreInfo) ([]byte, error)

ReadFileSection an entry from the log. Returns a byte slice containing the data entry. 如果文件未归档,直接读取文件数据 如果已归档,调用归档系统接口,读归档数据

func (*BlockFile) ReadLastSegSection

func (l *BlockFile) ReadLastSegSection(index uint64) (data []byte,
	fileName string, offset uint64, byteLen uint64, err error)

ReadLastSegSection read an entry from the log. Returns a byte slice containing the data entry. todo:这里BF支持了多文件系统,以及集成meta,这个函数需要修改,不然有bug todo: 应该根据 index也就是区块块高,查找该区块对应的metaFileInfo,找到这个区块对应的bf的文件名和文件系统 todo: 以及这个区块是否被归档了。 如果要找的区块,正好,不在最近写入的文件系统比如 /tmp/online2 中,在 /tmp/online1中,有bug 如果要找的区块,正好被归档了,那么有bug,这时这个区块,对应的索引,没有写入db todo: 所以归档时,需要保证归档的区块,对应的索引被写入到了db中了,而异步写区块的buffer大小是channel大小10,所以要保证最后10个区块不能被归档 同时,要找到的这个区块,必须在最后写入的文件中,才行。

func (*BlockFile) Sync

func (l *BlockFile) Sync() error

Sync performs an fsync on the log. This is not necessary when the NoSync option is set to false.

func (*BlockFile) TruncateFront

func (l *BlockFile) TruncateFront(index uint64) error

TruncateFront truncate the block from the front

@Description:
@receiver l
@param index
@return error

func (*BlockFile) Write

func (l *BlockFile) Write(index uint64, data []byte) (fileName string, offset, blkLen uint64, err error)

Write an entry to the block rfile db.

func (*BlockFile) WriteBatch

func (l *BlockFile) WriteBatch(b *Batch) (*storePb.StoreInfo, error)

WriteBatch writes the entries in the batch to the log in the order that they were added to the batch. The batch is cleared upon a successful return.

type BlockFileDB

type BlockFileDB struct {
	sync.Mutex
	// contains filtered or unexported fields
}

BlockFileDB provider a implementation of `blockdb.BlockDB` This implementation provides a key-value based data model

func NewBlockFileDB

func NewBlockFileDB(chainId string, dbHandle protocol.DBHandle, logger protocol.Logger,
	storeConfig *conf.StorageConfig, fileStore binlog.BinLogger) *BlockFileDB

NewBlockFileDB construct BlockFileDB

@Description:
@param chainId
@param dbHandle
@param logger
@param storeConfig
@param fileStore
@return *BlockFileDB

func (*BlockFileDB) BlockExists

func (b *BlockFileDB) BlockExists(blockHash []byte) (bool, error)

BlockExists returns true if the block hash exist, or returns false if none exists.

@Description:
@receiver b
@param blockHash
@return bool
@return error

func (*BlockFileDB) Close

func (b *BlockFileDB) Close()

Close is used to close database

@Description:
@receiver b

func (*BlockFileDB) CommitBlock

func (b *BlockFileDB) CommitBlock(blockInfo *serialization.BlockWithSerializedInfo, isCache bool) error

CommitBlock commits the block and the corresponding rwsets in an atomic operation

@Description:
@receiver b
@param blockInfo
@param isCache
@return error

func (*BlockFileDB) CommitCache

func (b *BlockFileDB) CommitCache(blockInfo *serialization.BlockWithSerializedInfo) error

CommitCache 提交数据到cache

@Description:
@receiver b
@param blockInfo
@return error

func (*BlockFileDB) CommitDB

func (b *BlockFileDB) CommitDB(blockInfo *serialization.BlockWithSerializedInfo) error

CommitDB 提交数据到kvdb

@Description:
@receiver b
@param blockInfo
@return error

func (*BlockFileDB) ConfigBlockImport

func (b *BlockFileDB) ConfigBlockImport(block *commonPb.Block) error

ConfigBlockImport import data from snapshot to blockdb

func (*BlockFileDB) GetArchivedPivot

func (b *BlockFileDB) GetArchivedPivot() (uint64, error)

GetArchivedPivot return archived pivot

@Description:
@receiver b
@return uint64
@return error

func (*BlockFileDB) GetBlock

func (b *BlockFileDB) GetBlock(height uint64) (*commonPb.Block, error)

GetBlock returns a block given it's block height, or returns nil if none exists.

@Description:
@receiver b
@param height
@return *commonPb.Block
@return error

func (*BlockFileDB) GetBlockByHash

func (b *BlockFileDB) GetBlockByHash(blockHash []byte) (*commonPb.Block, error)

GetBlockByHash returns a block given it's hash, or returns nil if none exists.

@Description:
@receiver b
@param blockHash
@return *commonPb.Block
@return error

func (*BlockFileDB) GetBlockByTx

func (b *BlockFileDB) GetBlockByTx(txId string) (*commonPb.Block, error)

GetBlockByTx returns a block which contains a tx.

@Description:
@receiver b
@param txId
@return *commonPb.Block
@return error

func (*BlockFileDB) GetBlockHeaderByHeight

func (b *BlockFileDB) GetBlockHeaderByHeight(height uint64) (*commonPb.BlockHeader, error)

GetBlockHeaderByHeight returns a block header by given it's height, or returns nil if none exists.

@Description:
@receiver b
@param height
@return *commonPb.BlockHeader
@return error

func (*BlockFileDB) GetBlockIndex

func (b *BlockFileDB) GetBlockIndex(height uint64) (*storePb.StoreInfo, error)

GetBlockIndex returns the offset of the block in the rfile

@Description:
@receiver b
@param height
@return *storePb.StoreInfo
@return error

func (*BlockFileDB) GetBlockMeta

func (b *BlockFileDB) GetBlockMeta(height uint64) (*storePb.SerializedBlock, error)

GetBlockMeta add next time

@Description:
@receiver b
@param height
@return *storePb.SerializedBlock
@return error

func (*BlockFileDB) GetBlockMetaIndex

func (b *BlockFileDB) GetBlockMetaIndex(height uint64) (*storePb.StoreInfo, error)

GetBlockMetaIndex returns the offset of the block in the rfile

@Description:
@receiver b
@param height
@return *storePb.StoreInfo
@return error

func (*BlockFileDB) GetFilteredBlock

func (b *BlockFileDB) GetFilteredBlock(height uint64) (*storePb.SerializedBlock, error)

GetFilteredBlock returns a filtered block given it's block height, or return nil if none exists.

@Description:
@receiver b
@param height
@return *storePb.SerializedBlock
@return error

func (*BlockFileDB) GetHeightByHash

func (b *BlockFileDB) GetHeightByHash(blockHash []byte) (uint64, error)

GetHeightByHash returns a block height given it's hash, or returns nil if none exists.

@Description:
@receiver b
@param blockHash
@return uint64
@return error

func (*BlockFileDB) GetLastBlock

func (b *BlockFileDB) GetLastBlock() (*commonPb.Block, error)

GetLastBlock returns the last block.

@Description:
@receiver b
@return *commonPb.Block
@return error

func (*BlockFileDB) GetLastConfigBlock

func (b *BlockFileDB) GetLastConfigBlock() (*commonPb.Block, error)

GetLastConfigBlock returns the last config block.

@Description:
@receiver b
@return *commonPb.Block
@return error

func (*BlockFileDB) GetLastConfigBlockHeight

func (b *BlockFileDB) GetLastConfigBlockHeight() (uint64, error)

GetLastConfigBlockHeight returns the last config block height.

@Description:
@receiver b
@return uint64
@return error

func (*BlockFileDB) GetLastSavepoint

func (b *BlockFileDB) GetLastSavepoint() (uint64, error)

GetLastSavepoint returns the last block height

@Description:
@receiver b
@return uint64
@return error

func (*BlockFileDB) GetTx

func (b *BlockFileDB) GetTx(txId string) (*commonPb.Transaction, error)

GetTx retrieves a transaction by txid, or returns nil if none exists.

@Description:
@receiver b
@param txId
@return *commonPb.Transaction
@return error

func (*BlockFileDB) GetTxConfirmedTime

func (b *BlockFileDB) GetTxConfirmedTime(txId string) (int64, error)

GetTxConfirmedTime returns the confirmed time of a given tx

@Description:
@receiver b
@param txId
@return int64
@return error

func (*BlockFileDB) GetTxHeight

func (b *BlockFileDB) GetTxHeight(txId string) (uint64, error)

GetTxHeight retrieves a transaction height by txid, or returns nil if none exists.

@Description:
@receiver b
@param txId
@return uint64
@return error

func (*BlockFileDB) GetTxIndex

func (b *BlockFileDB) GetTxIndex(txId string) (*storePb.StoreInfo, error)

GetTxIndex returns the offset of the transaction in the rfile

@Description:
@receiver b
@param txId
@return *storePb.StoreInfo
@return error

func (*BlockFileDB) GetTxInfoOnly

func (b *BlockFileDB) GetTxInfoOnly(txId string) (*storePb.TransactionStoreInfo, error)

GetTxInfoOnly 获得除Tx之外的其他TxInfo信息

@Description:
@receiver b
@param txId
@return *storePb.TransactionStoreInfo
@return error

func (*BlockFileDB) GetTxWithBlockInfo

func (b *BlockFileDB) GetTxWithBlockInfo(txId string) (*storePb.TransactionStoreInfo, error)

GetTxWithBlockInfo add next time

@Description:
@receiver b
@param txId
@return *storePb.TransactionStoreInfo
@return error

func (*BlockFileDB) GetTxidSet

func (b *BlockFileDB) GetTxidSet() ([]protocol.Iterator, error)

GetTxidSet returns an iterator that contains all the txid

func (*BlockFileDB) ImportSetSavePoint

func (b *BlockFileDB) ImportSetSavePoint(height uint64) error

ImportSetSavePoint set up savePoint when import snapshot

@Description:
@receiver b
@param height
@return error

func (*BlockFileDB) ImportTxID

func (b *BlockFileDB) ImportTxID(key, value []byte) error

ImportTxID import tx id from snapshot data into db

@Description:
@receiver b
@param key
@param value
@return error

func (*BlockFileDB) InitGenesis

func (b *BlockFileDB) InitGenesis(genesisBlock *serialization.BlockWithSerializedInfo) error

InitGenesis init genesis block

@Description:
@receiver b
@param genesisBlock
@return error

func (*BlockFileDB) RestoreBlocks

func (b *BlockFileDB) RestoreBlocks(blockInfos []*serialization.BlockWithSerializedInfo) error

RestoreBlocks restore block data from outside to kvdb: txid--SerializedTx

@Description:
@receiver b
@param blockInfos
@return error

func (*BlockFileDB) ShrinkBlocks

func (b *BlockFileDB) ShrinkBlocks(startHeight uint64, endHeight uint64) (map[uint64][]string, error)

ShrinkBlocks remove ranged txid--SerializedTx from kvdb

@Description:
@receiver b
@param startHeight
@param endHeight
@return map[uint64][]string
@return error

func (*BlockFileDB) TxArchived

func (b *BlockFileDB) TxArchived(txId string) (bool, error)

TxArchived returns true if the tx archived, or returns false.

@Description:
@receiver b
@param txId
@return bool
@return error

func (*BlockFileDB) TxExists

func (b *BlockFileDB) TxExists(txId string) (bool, error)

TxExists returns true if the tx exist, or returns false if none exists.

@Description:
@receiver b
@param txId
@return bool
@return error

type CRC

type CRC uint32

CRC is a CRC-32 checksum computed using Castagnoli's polynomial.

func NewCRC

func NewCRC(b []byte) CRC

NewCRC creates a new crc based on the given bytes.

@Description:
@param b
@return CRC

func (CRC) Update

func (c CRC) Update(b []byte) CRC

Update updates the crc with the given bytes.

@Description:
@receiver c
@param b
@return CRC

func (CRC) Value

func (c CRC) Value() uint32

Value returns a masked crc.

@Description:
@receiver c
@return uint32

type Options

type Options struct {
	// NoSync disables fsync after writes. This is less durable and puts the
	// log at risk of data loss when there's a server crash.
	NoSync bool
	// SegmentSize of each segment. This is just a target value, actual size
	// may differ. Default is 20 MB.
	SegmentSize int
	// SegmentCacheSize is the maximum number of segments that will be held in
	// memory for caching. Increasing this value may enhance performance for
	// concurrent read operations. Default is 1
	SegmentCacheSize int
	// NoCopy allows for the Read() operation to return the raw underlying data
	// slice. This is an optimization to help minimize allocations. When this
	// option is set, do not modify the returned data because it may affect
	// other Read calls. Default false
	NoCopy bool
	// UseMmap It is a method of memory-mapped rfile I/O. It implements demand
	// paging because rfile contents are not read from disk directly and initially
	// do not use physical RAM at all
	UseMmap bool
}

Options for BlockFile

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL