cosmos-sdk/store/iavlstore.go

261 lines
6.3 KiB
Go
Raw Normal View History

2017-12-04 00:56:25 -08:00
// XXX Need to s/Committer/CommitStore/g
2017-11-29 03:08:32 -08:00
package store
import (
2017-11-29 04:19:41 -08:00
"path"
"path/filepath"
"strings"
"github.com/pkg/errors"
2017-11-29 03:08:32 -08:00
"github.com/tendermint/iavl"
2017-11-29 04:19:41 -08:00
dbm "github.com/tendermint/tmlibs/db"
2017-11-29 03:08:32 -08:00
)
2017-11-29 04:19:41 -08:00
// NewIAVLLoader returns a CommitterLoader that returns
2017-11-29 05:07:39 -08:00
// an IAVLCommitter
2017-12-03 14:55:44 -08:00
func NewIAVLLoader(dbName string, cacheSize int, nHistoricalVersions uint64) CommitterLoader {
2017-11-29 04:19:41 -08:00
l := iavlLoader{
2017-12-03 14:55:44 -08:00
dbName: dbName,
cacheSize: cacheSize,
nHistoricalVersions: nHistoricalVersions,
2017-11-29 04:19:41 -08:00
}
return CommitterLoader(l.Load)
}
2017-12-03 22:55:15 -08:00
var _ CacheIterKVStore = (*IAVLCommitter)(nil)
2017-12-03 14:55:44 -08:00
var _ Committer = (*IAVLCommitter)(nil)
2017-11-29 05:07:39 -08:00
// IAVLCommitter Implements IterKVStore and Committer
type IAVLCommitter struct {
2017-11-29 04:19:41 -08:00
// we must store the last height here, as it is needed
// for saving the versioned tree
lastHeight uint64
2017-12-03 14:55:44 -08:00
// nHistoricalVersions is how many old versions we hold onto,
2017-11-29 04:19:41 -08:00
// uses a naive "hold last X versions" algorithm
2017-12-03 14:55:44 -08:00
nHistoricalVersions uint64
2017-11-29 04:19:41 -08:00
2017-11-29 05:07:39 -08:00
// this is all historical data and connection to
// the db
2017-11-29 04:19:41 -08:00
tree *iavl.VersionedTree
2017-11-29 05:07:39 -08:00
// this is the current working state to be saved
// on the next commit
IAVLStore
2017-11-29 04:19:41 -08:00
}
2017-11-29 05:07:39 -08:00
// NewIAVLCommitter properly initializes a committer
// that is ready to use as a IterKVStore
func NewIAVLCommitter(tree *iavl.VersionedTree,
2017-12-03 14:55:44 -08:00
lastHeight uint64, nHistoricalVersions uint64) *IAVLCommitter {
2017-12-03 22:55:15 -08:00
ic := &IAVLCommitter{
2017-12-03 14:55:44 -08:00
tree: tree,
lastHeight: lastHeight,
nHistoricalVersions: nHistoricalVersions,
2017-11-29 05:07:39 -08:00
}
2017-12-03 22:55:15 -08:00
ic.updateStore()
return ic
2017-11-29 05:07:39 -08:00
}
// Commit syncs the working state and
// saves another version to the db
func (i *IAVLCommitter) Commit() CommitID {
// TODO: sync working state??
// I think this is done already just by writing to tree.Tree()
2017-11-29 04:19:41 -08:00
// save a new version
2017-12-03 22:55:15 -08:00
ic.lastHeight++
hash, err := ic.tree.SaveVersion(ic.lastHeight)
2017-11-29 04:19:41 -08:00
if err != nil {
// TODO: do we want to extend Commit to
// allow returning errors?
panic(err)
}
2017-11-29 05:07:39 -08:00
// now point working state to the new status
2017-12-03 22:55:15 -08:00
ic.updateStore()
2017-11-29 05:07:39 -08:00
// release an old version of history
2017-12-03 22:55:15 -08:00
if ic.nHistoricalVersions <= ic.lastHeight {
release := ic.lastHeight - ic.nHistoricalVersions
ic.tree.DeleteVersion(release)
2017-11-29 04:19:41 -08:00
}
return CommitID{
2017-12-03 22:55:15 -08:00
Version: ic.lastHeight,
2017-11-29 04:19:41 -08:00
Hash: hash,
}
2017-11-29 03:08:32 -08:00
}
2017-11-29 05:07:39 -08:00
// store returns a wrapper around the current writable state
2017-12-03 22:55:15 -08:00
func (ic *IAVLCommitter) updateStore() {
ic.IAVLStore = IAVLStore{ic.tree.Tree()}
2017-11-29 05:07:39 -08:00
}
// IAVLStore is the writable state (not history) and
// implements the IterKVStore interface.
type IAVLStore struct {
tree *iavl.Tree
}
2017-12-03 22:55:15 -08:00
// CacheWrap implements IterKVStore.
func (is IAVLStore) CacheWrap() CacheWriter {
return is.CacheIterKVStore()
}
// CacheIterKVStore implements IterKVStore.
func (is IAVLStore) CacheIterKVStore() CacheIterKVStore {
// TODO: Add CacheWrap to IAVLTree.
2017-11-29 05:07:39 -08:00
return i
}
2017-12-03 22:55:15 -08:00
// Set implements IterKVStore.
func (is IAVLStore) Set(key, value []byte) (prev []byte) {
_, prev = is.tree.Get(key)
is.tree.Set(key, value)
2017-11-29 05:07:39 -08:00
return prev
}
2017-12-03 22:55:15 -08:00
// Get implements IterKVStore.
func (is IAVLStore) Get(key []byte) (value []byte, exists bool) {
_, v := is.tree.Get(key)
2017-11-29 05:07:39 -08:00
return v, (v != nil)
}
2017-12-03 22:55:15 -08:00
// Has implements IterKVStore.
func (is IAVLStore) Has(key []byte) (exists bool) {
return is.tree.Has(key)
2017-11-29 05:07:39 -08:00
}
2017-12-03 22:55:15 -08:00
// Remove implements IterKVStore.
func (is IAVLStore) Remove(key []byte) (prev []byte, removed bool) {
return is.tree.Remove(key)
2017-11-29 05:07:39 -08:00
}
2017-12-03 22:55:15 -08:00
// Iterator implements IterKVStore.
func (is IAVLStore) Iterator(start, end []byte) Iterator {
// TODO: this needs changes to IAVL tree
return nil
}
2017-12-03 22:55:15 -08:00
// ReverseIterator implements IterKVStore.
func (is IAVLStore) ReverseIterator(start, end []byte) Iterator {
// TODO
return nil
}
2017-12-03 22:55:15 -08:00
// First implements IterKVStore.
func (is IAVLStore) First(start, end []byte) (kv KVPair, ok bool) {
// TODO
return KVPair{}, false
}
2017-12-03 22:55:15 -08:00
// Last implements IterKVStore.
func (is IAVLStore) Last(start, end []byte) (kv KVPair, ok bool) {
// TODO
return KVPair{}, false
}
var _ IterKVStore = IAVLStore{}
type iavlIterator struct {
// TODO
}
var _ Iterator = (*iavlIterator)(nil)
// Domain implements Iterator
//
// The start & end (exclusive) limits to iterate over.
// If end < start, then the Iterator goes in reverse order.
2017-12-03 22:55:15 -08:00
func (ii *iavlIterator) Domain() (start, end []byte) {
// TODO
return nil, nil
}
// Valid implements Iterator
//
// Returns if the current position is valid.
2017-12-03 22:55:15 -08:00
func (ii *iavlIterator) Valid() bool {
// TODO
return false
}
// Next implements Iterator
//
// Next moves the iterator to the next key/value pair.
2017-12-03 22:55:15 -08:00
func (ii *iavlIterator) Next() {
// TODO
}
// 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().
2017-12-03 22:55:15 -08:00
func (ii *iavlIterator) Key() []byte {
// TODO
return nil
}
// 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().
2017-12-03 22:55:15 -08:00
func (ii *iavlIterator) Value() []byte {
// TODO
return nil
}
// Release implements Iterator
//
// Releases any resources and iteration-locks
2017-12-03 22:55:15 -08:00
func (ii *iavlIterator) Release() {
// TODO
}
2017-11-29 04:19:41 -08:00
// iavlLoader contains info on what store we want to load from
type iavlLoader struct {
2017-12-03 14:55:44 -08:00
dbName string
cacheSize int
nHistoricalVersion uint64
2017-12-01 14:08:37 -08:00
}
2017-11-29 04:19:41 -08:00
// Load implements CommitLoader type
2017-12-03 22:55:15 -08:00
func (il iavlLoader) Load(id CommitID) (Committer, error) {
2017-11-29 03:08:32 -08:00
// memory backed case, just for testing
2017-12-03 22:55:15 -08:00
if il.dbName == "" {
2017-11-29 03:08:32 -08:00
tree := iavl.NewVersionedTree(0, dbm.NewMemDB())
2017-12-03 22:55:15 -08:00
store := NewIAVLCommitter(tree, 0, il.nHistoricalVersions)
2017-11-29 04:19:41 -08:00
return store, nil
2017-11-29 03:08:32 -08:00
}
// Expand the path fully
2017-12-03 22:55:15 -08:00
dbPath, err := filepath.Abs(il.dbName)
2017-11-29 03:08:32 -08:00
if err != nil {
2017-11-29 04:19:41 -08:00
return nil, errors.New("Invalid Database Name")
2017-11-29 03:08:32 -08:00
}
// 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)
2017-11-29 04:19:41 -08:00
dir := filepath.Dir(dbPath)
name := filepath.Base(dbPath)
2017-11-29 03:08:32 -08:00
// Open database called "dir/name.db", if it doesn't exist it will be created
db := dbm.NewDB(name, dbm.LevelDBBackendStr, dir)
2017-12-03 22:55:15 -08:00
tree := iavl.NewVersionedTree(il.cacheSize, db)
2017-11-29 03:08:32 -08:00
if err = tree.Load(); err != nil {
2017-11-29 04:19:41 -08:00
return nil, errors.New("Loading tree: " + err.Error())
}
// TODO: load the version stored in id
2017-11-29 05:07:39 -08:00
store := NewIAVLCommitter(tree, tree.LatestVersion(),
2017-12-03 22:55:15 -08:00
il.nHistoricalVersions)
2017-11-29 04:19:41 -08:00
return store, nil
2017-11-29 03:08:32 -08:00
}