store

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2018 License: LGPL-3.0 Imports: 15 Imported by: 2

Documentation

Index

Constants

View Source
const IdealBatchSize = 100 * 1024

Code using batches should try to add this much data to the batch. The value was determined empirically.

Variables

View Source
var (
	ErrArgInvalid        = errors.New("invalid argument")
	ErrExist             = errors.New("item already exists")
	ErrNotExist          = errors.New("item does not exist")
	ErrAncestorsNotExist = errors.New("the block's ancestors does not exist")
	ErrEOF               = errors.New("file EOF")
	ErrRlpEncode         = errors.New("rlp encode err")
	ErrOutOfMemory       = errors.New("out of memory")
	ErrUnKnown           = errors.New("")
)
View Source
var M10 = int64(1024 * 1024 * 10)
View Source
var MbTable = []uint16{}/* 256 elements not displayed */
View Source
var OpenFileLimit = 64

Functions

func Byte2Uint32

func Byte2Uint32(val []byte) uint32

func CheckSum

func CheckSum(data []byte) uint16

func ClearData

func ClearData()

func CompareFile

func CompareFile(index int, dataMFile *MFile, hintMFile *MFile, after int64) error

func CreateBlock

func CreateBlock(hash common.Hash, parent common.Hash, height uint32) *types.Block

func CreateBufWithNumber

func CreateBufWithNumber(size int) ([]byte, error)

func CreateBufWithNumberBatch

func CreateBufWithNumberBatch(cnt int, template []byte) ([][]byte, error)

func CreateSign

func CreateSign(cnt int) ([]types.SignData, error)

func GetAccount

func GetAccount(address string, balance int64, version uint32) *types.AccountData

func GetAccounts

func GetAccounts() []*types.AccountData

func GetBlock0

func GetBlock0() *types.Block

func GetBlock1

func GetBlock1() *types.Block

func GetBlock2

func GetBlock2() *types.Block

func GetBucketIndex

func GetBucketIndex(pos uint32) uint32

func GetItemIndex

func GetItemIndex(pos uint32) uint32

func GetPos

func GetPos(bucketIndex uint32, itemIndex uint32) uint32

func GetStorePath

func GetStorePath() string

func IsExist

func IsExist(path string) (bool, error)

func NewKey1

func NewKey1() ([]byte, uint32)

func NewKey2

func NewKey2() ([]byte, uint32)

func NewKey3

func NewKey3() ([]byte, uint32)

func ScanDataFile

func ScanDataFile(index int, dataPath string, hintPath string) error

func SetItems

func SetItems(pool *CachePool, items []*TItem) (uint32, error)

func SetNodes

func SetNodes(pool *CachePool, nodes []*Node) (uint32, error)

func UpdateItems

func UpdateItems(pool *CachePool, start uint32, items []*TItem) error

func UpdateNodes

func UpdateNodes(pool *CachePool, header uint32, nodes []*Node) error

Types

type Batch

type Batch interface {
	Putter
	ValueSize() int // amount of data in the batch
	Write() error
	// Reset resets the batch for reuse
	Reset()
}

Batch is a write-only database that commits changes to its host database when Write is called. Batch cannot be used concurrently.

type BatchItem

type BatchItem struct {
	Key []byte
	Val []byte
}

type CacheBucket

type CacheBucket struct {
	Index   uint32
	MaxCnt  uint32
	UsedCnt uint32
	ItemLen uint8
	UnitCnt uint8
	Buf     []byte
}

func NewCacheBucket

func NewCacheBucket(index uint32, maxCnt uint32, itemLen uint8) *CacheBucket

func (*CacheBucket) GetUnit

func (bucket *CacheBucket) GetUnit(start uint32) ([]byte, error)

func (*CacheBucket) Remind

func (bucket *CacheBucket) Remind() uint32

func (*CacheBucket) SetUnit

func (bucket *CacheBucket) SetUnit(buf []byte) (uint32, error)

func (*CacheBucket) UpdateUint

func (bucket *CacheBucket) UpdateUint(start uint32, buf []byte) error

type CacheChain

type CacheChain struct {
	ConfirmNum int64
	Blocks     map[common.Hash]*types.Block
	Accounts   map[common.Hash]map[common.Address]*types.AccountData
	LmDataBase *LmDataBase
}

func NewCacheChain

func NewCacheChain(path string) (*CacheChain, error)

func (*CacheChain) AppendConfirmInfo

func (chain *CacheChain) AppendConfirmInfo(hash common.Hash, signData types.SignData) error

func (*CacheChain) AppendConfirms

func (chain *CacheChain) AppendConfirms(hash common.Hash, pack []types.SignData) error

func (*CacheChain) Close

func (chain *CacheChain) Close() error

func (*CacheChain) DelAccount

func (chain *CacheChain) DelAccount(address common.Address) error

func (*CacheChain) GetAccount

func (chain *CacheChain) GetAccount(blockHash common.Hash, address common.Address) (*types.AccountData, error)

GetAccount loads account from cache or db

func (*CacheChain) GetBlock

func (chain *CacheChain) GetBlock(hash common.Hash, height uint32) (*types.Block, error)

获取区块 优先根据hash与height同时获取,若hash为空则根据Height获取 获取不到返回:nil,原因

func (*CacheChain) GetBlockByHash

func (chain *CacheChain) GetBlockByHash(hash common.Hash) (*types.Block, error)

func (*CacheChain) GetBlockByHeight

func (chain *CacheChain) GetBlockByHeight(height uint32) (*types.Block, error)

func (*CacheChain) GetCanonicalAccount

func (chain *CacheChain) GetCanonicalAccount(address common.Address) (*types.AccountData, error)

func (*CacheChain) GetConfirms

func (chain *CacheChain) GetConfirms(hash common.Hash) ([]types.SignData, error)

获取区块的确认包 获取不到返回:nil,原因

func (*CacheChain) GetContractCode

func (chain *CacheChain) GetContractCode(codeHash common.Hash) (types.Code, error)

GetContractCode loads contract's code from db.

func (*CacheChain) GetTrieDatabase

func (chain *CacheChain) GetTrieDatabase() *TrieDatabase

OpenStorageTrie opens the storage trie of an account.

func (*CacheChain) IsExistByHash

func (chain *CacheChain) IsExistByHash(hash common.Hash) (bool, error)

func (*CacheChain) LoadLatestBlock

func (chain *CacheChain) LoadLatestBlock() (*types.Block, error)

func (*CacheChain) SetAccounts

func (chain *CacheChain) SetAccounts(blockHash common.Hash, accounts []*types.AccountData) error

SetAccounts saves dirty accounts generated by a block

func (*CacheChain) SetBlock

func (chain *CacheChain) SetBlock(hash common.Hash, block *types.Block) error

设置区块

func (*CacheChain) SetConfirmInfo

func (chain *CacheChain) SetConfirmInfo(hash common.Hash, signData types.SignData) error

设置区块的确认信息 每次收到一个

func (*CacheChain) SetConfirms

func (chain *CacheChain) SetConfirms(hash common.Hash, pack []types.SignData) error

func (*CacheChain) SetContractCode

func (chain *CacheChain) SetContractCode(codeHash common.Hash, code types.Code) error

SetContractCode saves contract's code

func (*CacheChain) SetStableBlock

func (chain *CacheChain) SetStableBlock(hash common.Hash) error

区块得到共识

type CachePool

type CachePool struct {
	Current uint32
	MaxCnt  uint32
	ItemLen uint8
	Buckets []*CacheBucket
	UintBuf *bytes.Buffer
}

func NewCachePool

func NewCachePool(bucketMaxCnt uint32, bucketItemLen uint8) *CachePool

func (*CachePool) Get

func (pool *CachePool) Get(header uint32, index uint32) ([]byte, error)

func (*CachePool) GetUnit

func (pool *CachePool) GetUnit(header uint32) ([]byte, error)

func (*CachePool) Malloc

func (pool *CachePool) Malloc(size uint32) *bytes.Buffer

func (*CachePool) SetUnit

func (pool *CachePool) SetUnit(buf []byte) (uint32, error)

func (*CachePool) UpdateUnit

func (pool *CachePool) UpdateUnit(start uint32, buf []byte) error

type CachedNode

type CachedNode struct {
	Blob     []byte              // Cached data block of the trie node
	Parents  int                 // Number of live nodes referencing this one
	Children map[common.Hash]int // Children referenced by this nodes
}

CachedNode is all the information we know about a single cached node in the memory database write layer.

type ContextFileHeader

type ContextFileHeader struct {
	Magic     uint8
	TCnt      uint8
	TLen      uint32
	TimeStamp uint64
	Crc       uint16
}

type Database

type Database interface {
	Putter
	Get(key []byte) ([]byte, error)
	Has(key []byte) (bool, error)
	Delete(key []byte) error
	Close()
	NewBatch() Batch
}

Database wraps all database operations. All methods are safe for concurrent use.

type DatabaseReader

type DatabaseReader interface {
	// Get retrieves the value associated with key form the database.
	Get(key []byte) (value []byte, err error)

	// Has retrieves whether a key is present in the database.
	Has(key []byte) (bool, error)
}

DatabaseReader wraps the Get and Has method of a backing store for the trie.

type HItem

type HItem struct {
	Flg uint8
	Num uint32
	Pos uint32 // 文件位置
}

type HintItem

type HintItem struct {
	Num uint32
	Pos uint32
}

type Index

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

func (*Index) Bak

func (index *Index) Bak() int

func (*Index) Cur

func (index *Index) Cur() int

func (*Index) Init

func (index *Index) Init()

func (*Index) Swap

func (index *Index) Swap()

type LDBBatch

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

func (*LDBBatch) Put

func (b *LDBBatch) Put(key, value []byte) error

func (*LDBBatch) Reset

func (b *LDBBatch) Reset()

func (*LDBBatch) ValueSize

func (b *LDBBatch) ValueSize() int

func (*LDBBatch) Write

func (b *LDBBatch) Write() error

type LDBDatabase

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

func NewLDBDatabase

func NewLDBDatabase(database *LmDataBase, cache int, handles int) *LDBDatabase

NewLDBDatabase returns a LevelDB wrapped object.

func (*LDBDatabase) Close

func (db *LDBDatabase) Close()

func (*LDBDatabase) Delete

func (db *LDBDatabase) Delete(key []byte) error

Delete deletes the key from the queue and database

func (*LDBDatabase) Get

func (db *LDBDatabase) Get(key []byte) ([]byte, error)

Get returns the given key if it's present.

func (*LDBDatabase) Has

func (db *LDBDatabase) Has(key []byte) (bool, error)

func (*LDBDatabase) LDB

func (db *LDBDatabase) LDB() *LmDataBase

func (*LDBDatabase) NewBatch

func (db *LDBDatabase) NewBatch() Batch

func (*LDBDatabase) Put

func (db *LDBDatabase) Put(key []byte, value []byte) error

Put puts the given key / value to the queue

type LmBuffer

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

func NewLmBuffer

func NewLmBuffer(buf []byte) *LmBuffer

func (*LmBuffer) Read

func (buf *LmBuffer) Read(p []byte) (n int, err error)

func (*LmBuffer) Write

func (buf *LmBuffer) Write(p []byte) (n int, err error)

type LmDataBase

type LmDataBase struct {
	HomePath  string
	CurIndex  int
	CurOffset int64
	Tree      *Tree
	Buf       *bytes.Buffer
	Context   [][]byte
	// contains filtered or unexported fields
}

func NewLmDataBase

func NewLmDataBase(homePath string) (*LmDataBase, error)

func (*LmDataBase) Close

func (database *LmDataBase) Close() error

func (*LmDataBase) Commit

func (database *LmDataBase) Commit(items []*BatchItem) error

func (*LmDataBase) CurrentBlock

func (database *LmDataBase) CurrentBlock() []byte

func (*LmDataBase) Delete

func (database *LmDataBase) Delete(key []byte) error

func (*LmDataBase) Get

func (database *LmDataBase) Get(key []byte) ([]byte, error)

func (*LmDataBase) Has

func (database *LmDataBase) Has(key []byte) (bool, error)

func (*LmDataBase) Put

func (database *LmDataBase) Put(key []byte, val []byte) error

func (*LmDataBase) ScanDataFiles

func (database *LmDataBase) ScanDataFiles() error

func (*LmDataBase) SetCurrentBlock

func (database *LmDataBase) SetCurrentBlock(block []byte) error

type MFile

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

func OpenMFileForRead

func OpenMFileForRead(path string) (*MFile, error)

func OpenMFileForWrite

func OpenMFileForWrite(path string) (*MFile, error)

func (*MFile) Flush

func (mFile *MFile) Flush() error

func (*MFile) Read

func (mFile *MFile) Read(offset int64, size int64) ([]byte, error)

func (*MFile) Size

func (mFile *MFile) Size() int64

func (*MFile) Write

func (mFile *MFile) Write(data []byte) error

type MemDatabase

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

* This is a test memory database. Do not use for any production it does not get persisted

func NewMemDatabase

func NewMemDatabase() (*MemDatabase, error)

func NewMemDatabaseWithCap

func NewMemDatabaseWithCap(size int) (*MemDatabase, error)

func (*MemDatabase) Close

func (db *MemDatabase) Close()

func (*MemDatabase) Delete

func (db *MemDatabase) Delete(key []byte) error

func (*MemDatabase) Get

func (db *MemDatabase) Get(key []byte) ([]byte, error)

func (*MemDatabase) Has

func (db *MemDatabase) Has(key []byte) (bool, error)

func (*MemDatabase) Keys

func (db *MemDatabase) Keys() [][]byte

func (*MemDatabase) Len

func (db *MemDatabase) Len() int

func (*MemDatabase) NewBatch

func (db *MemDatabase) NewBatch() Batch

func (*MemDatabase) Put

func (db *MemDatabase) Put(key []byte, value []byte) error

type Node

type Node struct {
	IsNode  bool
	Depth   uint32
	MaxCnt  uint8
	UsedCnt uint8
	Header  uint32
}

func GetNodes

func GetNodes(pool *CachePool, header uint32) ([]*Node, error)

type Putter

type Putter interface {
	Put(key []byte, value []byte) error
}

Putter wraps the database write operation supported by both batches and regular databases.

type RecordHeader

type RecordHeader struct {
	Flg       uint8
	Num       uint32
	KLen      uint8
	VLen      uint32
	TimeStamp uint64
	Crc       uint16
}

type TItem

type TItem struct {
	Flg uint8
	Num uint32
	Pos uint32 // 文件位置
	Key []byte
}

func GetItems

func GetItems(pool *CachePool, header uint32) ([]*TItem, error)

type Tree

type Tree struct {
	Root    *Node
	DataBuf *CachePool
	NodeBuf *CachePool
}

func NewTree

func NewTree() (*Tree, error)

func (*Tree) Add

func (tree *Tree) Add(key []byte, pos uint32) error

func (*Tree) Get

func (tree *Tree) Get(key []byte) (*HItem, error)

type TrieDatabase

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

Database is an intermediate write layer between the trie data structures and the disk database. The aim is to accumulate trie writes in-memory and only periodically flush a couple tries to disk, garbage collecting the remainder.

func NewTrieDatabase

func NewTrieDatabase(diskdb Database) *TrieDatabase

NewTrieDatabase creates a new trie database to store ephemeral trie content before its written out to disk or garbage collected.

func (*TrieDatabase) Commit

func (db *TrieDatabase) Commit(node common.Hash, report bool) error

Commit iterates over all the Children of a particular node, writes them out to disk, forcefully tearing down all references in both directions.

As a side effect, all pre-images accumulated up to this point are also written.

func (*TrieDatabase) Dereference

func (db *TrieDatabase) Dereference(child common.Hash, parent common.Hash)

Dereference removes an existing reference from a parent node to a child node.

func (*TrieDatabase) DiskDB

func (db *TrieDatabase) DiskDB() DatabaseReader

DiskDB retrieves the persistent storage backing the trie database.

func (*TrieDatabase) DiskDB4Test

func (db *TrieDatabase) DiskDB4Test() Database

func (*TrieDatabase) Insert

func (db *TrieDatabase) Insert(hash common.Hash, blob []byte)

Insert writes a new trie node to the memory database if it's yet unknown. The method will make a copy of the slice.

func (*TrieDatabase) InsertPreimage

func (db *TrieDatabase) InsertPreimage(hash common.Hash, preimage []byte)

insertPreimage writes a new trie node pre-image to the memory database if it's yet unknown. The method will make a copy of the slice.

Note, this method assumes that the database's lock is held!

func (*TrieDatabase) Lock

func (db *TrieDatabase) Lock()

func (*TrieDatabase) Node

func (db *TrieDatabase) Node(hash common.Hash) ([]byte, error)

Node retrieves a cached trie node from memory. If it cannot be found cached, the method queries the persistent database for the content.

func (*TrieDatabase) Nodes

func (db *TrieDatabase) Nodes() []common.Hash

Nodes retrieves the hashes of all the nodes cached within the memory database. This method is extremely expensive and should only be used to validate internal states in test code.

func (*TrieDatabase) Nodes4Test

func (db *TrieDatabase) Nodes4Test() map[common.Hash]*CachedNode

func (*TrieDatabase) Preimage

func (db *TrieDatabase) Preimage(hash common.Hash) ([]byte, error)

preimage retrieves a cached trie node pre-image from memory. If it cannot be found cached, the method queries the persistent database for the content.

func (*TrieDatabase) Reference

func (db *TrieDatabase) Reference(child common.Hash, parent common.Hash)

Reference adds a new reference from a parent node to a child node.

func (*TrieDatabase) Size

func (db *TrieDatabase) Size() common.StorageSize

Size returns the current storage size of the memory cache in front of the persistent database layer.

func (*TrieDatabase) UnLock

func (db *TrieDatabase) UnLock()

Directories

Path Synopsis
Package trie implements Merkle Patricia Tries.
Package trie implements Merkle Patricia Tries.

Jump to

Keyboard shortcuts

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