Implementing iavlStore...
This commit is contained in:
parent
a47da2c8e1
commit
f4484ab3b5
|
@ -131,12 +131,12 @@ imports:
|
||||||
- keys/wordlist
|
- keys/wordlist
|
||||||
- nano
|
- nano
|
||||||
- name: github.com/tendermint/go-wire
|
- name: github.com/tendermint/go-wire
|
||||||
version: 99d2169a1e39c65983eacaa1da867d6f3218e1c9
|
version: 078a95fc9117752ced2590cb4632efdf48e41c84
|
||||||
subpackages:
|
subpackages:
|
||||||
- data
|
- data
|
||||||
- data/base58
|
- data/base58
|
||||||
- name: github.com/tendermint/iavl
|
- name: github.com/tendermint/iavl
|
||||||
version: 7198bc18ac0cf4ac0f57802d8022695640b79ec7
|
version: 03e6c011329bce607eed69d60737c899519f1f70
|
||||||
- name: github.com/tendermint/light-client
|
- name: github.com/tendermint/light-client
|
||||||
version: 76313d625e662ed7b284d066d68ff71edd7a9fac
|
version: 76313d625e662ed7b284d066d68ff71edd7a9fac
|
||||||
subpackages:
|
subpackages:
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
// XXX Need to s/Committer/CommitStore/g
|
|
||||||
|
|
||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -13,152 +11,119 @@ import (
|
||||||
dbm "github.com/tendermint/tmlibs/db"
|
dbm "github.com/tendermint/tmlibs/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewIAVLLoader returns a CommitterLoader that returns
|
// NewIAVLStoreLoader returns a CommitStoreLoader that returns an iavlStore
|
||||||
// an IAVLCommitter
|
func NewIAVLStoreLoader(dbName string, cacheSize int, numHistory int64) CommitStoreLoader {
|
||||||
func NewIAVLLoader(dbName string, cacheSize int, nHistoricalVersions uint64) CommitterLoader {
|
l := iavlStoreLoader{
|
||||||
l := iavlLoader{
|
|
||||||
dbName: dbName,
|
dbName: dbName,
|
||||||
cacheSize: cacheSize,
|
cacheSize: cacheSize,
|
||||||
nHistoricalVersions: nHistoricalVersions,
|
numHistory: numHistory,
|
||||||
}
|
}
|
||||||
return CommitterLoader(l.Load)
|
return l.Load
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ CacheIterKVStore = (*IAVLCommitter)(nil)
|
var _ IterKVStore = (*iavlStore)(nil)
|
||||||
var _ Committer = (*IAVLCommitter)(nil)
|
var _ CommitStore = (*iavlStore)(nil)
|
||||||
|
|
||||||
// IAVLCommitter Implements IterKVStore and Committer
|
// iavlStore Implements IterKVStore and CommitStore.
|
||||||
type IAVLCommitter struct {
|
type iavlStore struct {
|
||||||
// we must store the last height here, as it is needed
|
|
||||||
// for saving the versioned tree
|
|
||||||
lastHeight uint64
|
|
||||||
|
|
||||||
// nHistoricalVersions is how many old versions we hold onto,
|
// The underlying tree.
|
||||||
// uses a naive "hold last X versions" algorithm
|
|
||||||
nHistoricalVersions uint64
|
|
||||||
|
|
||||||
// this is all historical data and connection to
|
|
||||||
// the db
|
|
||||||
tree *iavl.VersionedTree
|
tree *iavl.VersionedTree
|
||||||
|
|
||||||
// this is the current working state to be saved
|
// How many old versions we hold onto.
|
||||||
// on the next commit
|
numHistory int64
|
||||||
IAVLStore
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewIAVLCommitter properly initializes a committer
|
// CONTRACT: tree should be fully loaded.
|
||||||
// that is ready to use as a IterKVStore
|
func newIAVLStore(tree *iavl.VersionedTree, numHistory int64) *iavlStore {
|
||||||
func NewIAVLCommitter(tree *iavl.VersionedTree,
|
st := &iavlStore{
|
||||||
lastHeight uint64, nHistoricalVersions uint64) *IAVLCommitter {
|
|
||||||
ic := &IAVLCommitter{
|
|
||||||
tree: tree,
|
tree: tree,
|
||||||
lastHeight: lastHeight,
|
numHistory: numHistory,
|
||||||
nHistoricalVersions: nHistoricalVersions,
|
|
||||||
}
|
}
|
||||||
ic.updateStore()
|
return st
|
||||||
return ic
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit syncs the working state and
|
// Commit persists the store.
|
||||||
// saves another version to the db
|
func (st *iavlStore) Commit() CommitID {
|
||||||
func (i *IAVLCommitter) Commit() CommitID {
|
|
||||||
// TODO: sync working state??
|
|
||||||
// I think this is done already just by writing to tree.Tree()
|
|
||||||
|
|
||||||
// save a new version
|
// Save a new version.
|
||||||
ic.lastHeight++
|
hash, version, err := st.tree.SaveVersion()
|
||||||
hash, err := ic.tree.SaveVersion(ic.lastHeight)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: do we want to extend Commit to
|
// TODO: Do we want to extend Commit to allow returning errors?
|
||||||
// allow returning errors?
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// now point working state to the new status
|
// Release an old version of history
|
||||||
ic.updateStore()
|
if st.numHistory < st.tree.Version() {
|
||||||
|
toRelease := version - st.numHistory
|
||||||
// release an old version of history
|
st.tree.DeleteVersion(toRelease)
|
||||||
if ic.nHistoricalVersions <= ic.lastHeight {
|
|
||||||
release := ic.lastHeight - ic.nHistoricalVersions
|
|
||||||
ic.tree.DeleteVersion(release)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return CommitID{
|
return CommitID{
|
||||||
Version: ic.lastHeight,
|
Version: version,
|
||||||
Hash: hash,
|
Hash: hash,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// store returns a wrapper around the current writable state
|
|
||||||
func (ic *IAVLCommitter) updateStore() {
|
|
||||||
ic.IAVLStore = IAVLStore{ic.tree.Tree()}
|
|
||||||
}
|
|
||||||
|
|
||||||
// IAVLStore is the writable state (not history) and
|
|
||||||
// implements the IterKVStore interface.
|
|
||||||
type IAVLStore struct {
|
|
||||||
tree *iavl.Tree
|
|
||||||
}
|
|
||||||
|
|
||||||
// CacheWrap implements IterKVStore.
|
// CacheWrap implements IterKVStore.
|
||||||
func (is IAVLStore) CacheWrap() CacheWriter {
|
func (st *iavlStore) CacheWrap() CacheWriter {
|
||||||
return is.CacheIterKVStore()
|
return st.CacheIterKVStore()
|
||||||
}
|
}
|
||||||
|
|
||||||
// CacheIterKVStore implements IterKVStore.
|
// CacheIterKVStore implements IterKVStore.
|
||||||
func (is IAVLStore) CacheIterKVStore() CacheIterKVStore {
|
func (st *iavlStore) CacheIterKVStore() CacheIterKVStore {
|
||||||
// TODO: Add CacheWrap to IAVLTree.
|
// XXX Create generic IterKVStore wrapper.
|
||||||
return i
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set implements IterKVStore.
|
// Set implements IterKVStore.
|
||||||
func (is IAVLStore) Set(key, value []byte) (prev []byte) {
|
func (st *iavlStore) Set(key, value []byte) (prev []byte) {
|
||||||
_, prev = is.tree.Get(key)
|
_, prev = st.tree.Get(key)
|
||||||
is.tree.Set(key, value)
|
st.tree.Set(key, value)
|
||||||
return prev
|
return prev
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get implements IterKVStore.
|
// Get implements IterKVStore.
|
||||||
func (is IAVLStore) Get(key []byte) (value []byte, exists bool) {
|
func (st *iavlStore) Get(key []byte) (value []byte, exists bool) {
|
||||||
_, v := is.tree.Get(key)
|
_, v := st.tree.Get(key)
|
||||||
return v, (v != nil)
|
return v, (v != nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Has implements IterKVStore.
|
// Has implements IterKVStore.
|
||||||
func (is IAVLStore) Has(key []byte) (exists bool) {
|
func (st *iavlStore) Has(key []byte) (exists bool) {
|
||||||
return is.tree.Has(key)
|
return st.tree.Has(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove implements IterKVStore.
|
// Remove implements IterKVStore.
|
||||||
func (is IAVLStore) Remove(key []byte) (prev []byte, removed bool) {
|
func (st *iavlStore) Remove(key []byte) (prev []byte, removed bool) {
|
||||||
return is.tree.Remove(key)
|
return st.tree.Remove(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterator implements IterKVStore.
|
// Iterator implements IterKVStore.
|
||||||
func (is IAVLStore) Iterator(start, end []byte) Iterator {
|
func (st *iavlStore) Iterator(start, end []byte) Iterator {
|
||||||
// TODO: this needs changes to IAVL tree
|
// XXX Create iavlIterator (without modifying tendermint/iavl)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReverseIterator implements IterKVStore.
|
// ReverseIterator implements IterKVStore.
|
||||||
func (is IAVLStore) ReverseIterator(start, end []byte) Iterator {
|
func (st *iavlStore) ReverseIterator(start, end []byte) Iterator {
|
||||||
// TODO
|
// XXX Create iavlIterator (without modifying tendermint/iavl)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// First implements IterKVStore.
|
// First implements IterKVStore.
|
||||||
func (is IAVLStore) First(start, end []byte) (kv KVPair, ok bool) {
|
func (is IAVLStore) First(start, end []byte) (kv KVPair, ok bool) {
|
||||||
// TODO
|
// XXX
|
||||||
return KVPair{}, false
|
return KVPair{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Last implements IterKVStore.
|
// Last implements IterKVStore.
|
||||||
func (is IAVLStore) Last(start, end []byte) (kv KVPair, ok bool) {
|
func (is IAVLStore) Last(start, end []byte) (kv KVPair, ok bool) {
|
||||||
// TODO
|
// XXX
|
||||||
return KVPair{}, false
|
return KVPair{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ IterKVStore = IAVLStore{}
|
//----------------------------------------
|
||||||
|
|
||||||
type iavlIterator struct {
|
type iavlIterator struct {
|
||||||
// TODO
|
// TODO
|
||||||
|
@ -167,94 +132,55 @@ type iavlIterator struct {
|
||||||
var _ Iterator = (*iavlIterator)(nil)
|
var _ Iterator = (*iavlIterator)(nil)
|
||||||
|
|
||||||
// Domain implements Iterator
|
// Domain implements Iterator
|
||||||
//
|
|
||||||
// The start & end (exclusive) limits to iterate over.
|
|
||||||
// If end < start, then the Iterator goes in reverse order.
|
|
||||||
func (ii *iavlIterator) Domain() (start, end []byte) {
|
func (ii *iavlIterator) Domain() (start, end []byte) {
|
||||||
// TODO
|
// TODO
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Valid implements Iterator
|
// Valid implements Iterator
|
||||||
//
|
|
||||||
// Returns if the current position is valid.
|
|
||||||
func (ii *iavlIterator) Valid() bool {
|
func (ii *iavlIterator) Valid() bool {
|
||||||
// TODO
|
// TODO
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next implements Iterator
|
// Next implements Iterator
|
||||||
//
|
|
||||||
// Next moves the iterator to the next key/value pair.
|
|
||||||
func (ii *iavlIterator) Next() {
|
func (ii *iavlIterator) Next() {
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
// Key implements Iterator
|
// Key implements Iterator
|
||||||
//
|
|
||||||
// Key returns the key of the current key/value pair, or nil if done.
|
|
||||||
// The caller should not modify the contents of the returned slice, and
|
|
||||||
// its contents may change after calling Next().
|
|
||||||
func (ii *iavlIterator) Key() []byte {
|
func (ii *iavlIterator) Key() []byte {
|
||||||
// TODO
|
// TODO
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value implements Iterator
|
// Value implements Iterator
|
||||||
//
|
|
||||||
// Value returns the key of the current key/value pair, or nil if done.
|
|
||||||
// The caller should not modify the contents of the returned slice, and
|
|
||||||
// its contents may change after calling Next().
|
|
||||||
func (ii *iavlIterator) Value() []byte {
|
func (ii *iavlIterator) Value() []byte {
|
||||||
// TODO
|
// TODO
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release implements Iterator
|
// Release implements Iterator
|
||||||
//
|
|
||||||
// Releases any resources and iteration-locks
|
|
||||||
func (ii *iavlIterator) Release() {
|
func (ii *iavlIterator) Release() {
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
// iavlLoader contains info on what store we want to load from
|
//----------------------------------------
|
||||||
type iavlLoader struct {
|
|
||||||
dbName string
|
// iavlStoreLoader contains info on what store we want to load from
|
||||||
|
type iavlStoreLoader struct {
|
||||||
|
db dbm.DB
|
||||||
cacheSize int
|
cacheSize int
|
||||||
nHistoricalVersion uint64
|
numHistory int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load implements CommitLoader type
|
// Load implements CommitLoader.
|
||||||
func (il iavlLoader) Load(id CommitID) (Committer, error) {
|
func (isl iavlLoader) Load(id CommitID) (CommitStore, error) {
|
||||||
// memory backed case, just for testing
|
tree := iavl.NewVersionedTree(isl.db, isl.cacheSize)
|
||||||
if il.dbName == "" {
|
err := tree.Load()
|
||||||
tree := iavl.NewVersionedTree(0, dbm.NewMemDB())
|
|
||||||
store := NewIAVLCommitter(tree, 0, il.nHistoricalVersions)
|
|
||||||
return store, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Expand the path fully
|
|
||||||
dbPath, err := filepath.Abs(il.dbName)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("Invalid Database Name")
|
return nil, err
|
||||||
}
|
}
|
||||||
|
store := newIAVLStore(tree, isl.numHistory)
|
||||||
// Some external calls accidently add a ".db", which is now removed
|
|
||||||
dbPath = strings.TrimSuffix(dbPath, path.Ext(dbPath))
|
|
||||||
|
|
||||||
// Split the database name into it's components (dir, name)
|
|
||||||
dir := filepath.Dir(dbPath)
|
|
||||||
name := filepath.Base(dbPath)
|
|
||||||
|
|
||||||
// Open database called "dir/name.db", if it doesn't exist it will be created
|
|
||||||
db := dbm.NewDB(name, dbm.LevelDBBackendStr, dir)
|
|
||||||
tree := iavl.NewVersionedTree(il.cacheSize, db)
|
|
||||||
if err = tree.Load(); err != nil {
|
|
||||||
return nil, errors.New("Loading tree: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: load the version stored in id
|
|
||||||
store := NewIAVLCommitter(tree, tree.LatestVersion(),
|
|
||||||
il.nHistoricalVersions)
|
|
||||||
return store, nil
|
return store, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,7 @@ func (rs *rootMultiStore) SetCommitStoreLoader(name string, loader CommitStoreLo
|
||||||
// Call once after all calls to SetCommitStoreLoader are complete.
|
// Call once after all calls to SetCommitStoreLoader are complete.
|
||||||
func (rs *rootMultiStore) LoadLatestVersion() error {
|
func (rs *rootMultiStore) LoadLatestVersion() error {
|
||||||
ver := getLatestVersion(rs.db)
|
ver := getLatestVersion(rs.db)
|
||||||
rs.LoadVersion(ver)
|
return rs.LoadVersion(ver)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: Returns 0 unless LoadVersion() or LoadLatestVersion() is called.
|
// NOTE: Returns 0 unless LoadVersion() or LoadLatestVersion() is called.
|
||||||
|
@ -71,7 +71,10 @@ func (rs *rootMultiStore) LoadVersion(ver int64) error {
|
||||||
// Otherwise, version is 1 or greater
|
// Otherwise, version is 1 or greater
|
||||||
|
|
||||||
// Load commitState
|
// Load commitState
|
||||||
var state commitState = loadCommitState(rs.db, ver)
|
state, err := loadCommitState(rs.db, ver)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Load each Substore
|
// Load each Substore
|
||||||
var newSubstores = make(map[string]CommitStore)
|
var newSubstores = make(map[string]CommitStore)
|
||||||
|
@ -112,12 +115,10 @@ func (rs *rootMultiStore) doCommit() commitState {
|
||||||
commitID := store.Commit()
|
commitID := store.Commit()
|
||||||
|
|
||||||
// Record CommitID
|
// Record CommitID
|
||||||
substores = append(substores,
|
substore := substore{}
|
||||||
substore{
|
substore.Name = name
|
||||||
Name: name,
|
substore.CommitID = commitID
|
||||||
CommitID: commitID,
|
substores = append(substores, substore)
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Incr curVersion
|
// Incr curVersion
|
||||||
|
@ -134,7 +135,7 @@ func (rs *rootMultiStore) doCommit() commitState {
|
||||||
// Implements CommitStore
|
// Implements CommitStore
|
||||||
func (rs *rootMultiStore) Commit() CommitID {
|
func (rs *rootMultiStore) Commit() CommitID {
|
||||||
|
|
||||||
version := rs.version
|
version := rs.curVersion
|
||||||
|
|
||||||
// Needs to be transactional
|
// Needs to be transactional
|
||||||
batch := rs.db.NewBatch()
|
batch := rs.db.NewBatch()
|
||||||
|
@ -145,15 +146,15 @@ func (rs *rootMultiStore) Commit() CommitID {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
commitStateKey := fmt.Sprintf(commitStateKeyFmt, rs.version)
|
commitStateKey := fmt.Sprintf(commitStateKeyFmt, rs.curVersion)
|
||||||
batch.Set(commitStateKey, stateBytes)
|
batch.Set([]byte(commitStateKey), stateBytes)
|
||||||
|
|
||||||
// Save the latest version
|
// Save the latest version
|
||||||
latestBytes, _ := wire.Marshal(rs.version) // Does not error
|
latestBytes, _ := wire.Marshal(rs.curVersion) // Does not error
|
||||||
batch.Set(latestVersionKey, latestBytes)
|
batch.Set([]byte(latestVersionKey), latestBytes)
|
||||||
|
|
||||||
batch.Write()
|
batch.Write()
|
||||||
rs.version += 1
|
rs.curVersion += 1
|
||||||
commitID := CommitID{
|
commitID := CommitID{
|
||||||
Version: version,
|
Version: version,
|
||||||
Hash: state.Hash(),
|
Hash: state.Hash(),
|
||||||
|
@ -179,17 +180,17 @@ func (rs *rootMultiStore) CacheMultiStore() CacheMultiStore {
|
||||||
|
|
||||||
// Implements MultiStore
|
// Implements MultiStore
|
||||||
func (rs *rootMultiStore) GetCommitStore(name string) CommitStore {
|
func (rs *rootMultiStore) GetCommitStore(name string) CommitStore {
|
||||||
return rs.store[name]
|
return rs.substores[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements MultiStore
|
// Implements MultiStore
|
||||||
func (rs *rootMultiStore) GetKVStore(name string) KVStore {
|
func (rs *rootMultiStore) GetKVStore(name string) KVStore {
|
||||||
return rs.store[name].(KVStore)
|
return rs.substores[name].(KVStore)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements MultiStore
|
// Implements MultiStore
|
||||||
func (rs *rootMultiStore) GetIterKVStore(name string) IterKVStore {
|
func (rs *rootMultiStore) GetIterKVStore(name string) IterKVStore {
|
||||||
return rs.store[name].(IterKVStore)
|
return rs.substores[name].(IterKVStore)
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------
|
//----------------------------------------
|
||||||
|
@ -210,8 +211,8 @@ func loadCommitState(db dbm.DB, ver int64) (commitState, error) {
|
||||||
|
|
||||||
// Load from DB.
|
// Load from DB.
|
||||||
commitStateKey := fmt.Sprintf(commitStateKeyFmt, ver)
|
commitStateKey := fmt.Sprintf(commitStateKeyFmt, ver)
|
||||||
stateBytes := db.Get(commitStateKey, ver)
|
stateBytes := db.Get([]byte(commitStateKey))
|
||||||
if bz == nil {
|
if stateBytes == nil {
|
||||||
return commitState{}, fmt.Errorf("Failed to load rootMultiStore: no data")
|
return commitState{}, fmt.Errorf("Failed to load rootMultiStore: no data")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -264,7 +265,7 @@ func (sc substoreCore) Hash() []byte {
|
||||||
|
|
||||||
func getLatestVersion(db dbm.DB) int64 {
|
func getLatestVersion(db dbm.DB) int64 {
|
||||||
var latest int64
|
var latest int64
|
||||||
latestBytes := db.Get(latestVersionKey)
|
latestBytes := db.Get([]byte(latestVersionKey))
|
||||||
if latestBytes == nil {
|
if latestBytes == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,7 +88,7 @@ type IterKVStore interface {
|
||||||
CacheIterKVStore() CacheIterKVStore
|
CacheIterKVStore() CacheIterKVStore
|
||||||
|
|
||||||
// CacheWrap() returns a CacheIterKVStore.
|
// CacheWrap() returns a CacheIterKVStore.
|
||||||
CacheWrap() CacheWrap
|
// CacheWrap() defined in KVStore
|
||||||
}
|
}
|
||||||
|
|
||||||
type CacheIterKVStore interface {
|
type CacheIterKVStore interface {
|
||||||
|
|
Loading…
Reference in New Issue