cosmos-sdk/store/iavlstore.go

174 lines
4.1 KiB
Go
Raw Normal View History

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-11-29 04:19:41 -08:00
func NewIAVLLoader(dbName string, cacheSize int, history uint64) CommitterLoader {
l := iavlLoader{
dbName: dbName,
cacheSize: cacheSize,
history: history,
}
return CommitterLoader(l.Load)
}
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
// history is how many old versions we hold onto,
// uses a naive "hold last X versions" algorithm
history uint64
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,
lastHeight uint64, history uint64) *IAVLCommitter {
i := &IAVLCommitter{
tree: tree,
lastHeight: lastHeight,
history: history,
}
i.updateStore()
return i
}
// 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
i.lastHeight++
hash, err := i.tree.SaveVersion(i.lastHeight)
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
i.updateStore()
// release an old version of history
2017-11-29 04:19:41 -08:00
if i.history <= i.lastHeight {
release := i.lastHeight - i.history
i.tree.DeleteVersion(release)
}
return CommitID{
Version: i.lastHeight,
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
func (i *IAVLCommitter) updateStore() {
i.IAVLStore = IAVLStore{i.tree.Tree()}
}
var _ CacheWrappable = (*IAVLCommitter)(nil)
var _ Committer = (*IAVLCommitter)(nil)
// IAVLStore is the writable state (not history) and
// implements the IterKVStore interface.
type IAVLStore struct {
tree *iavl.Tree
}
// CacheWrap returns a wrapper around the current writable state
func (i IAVLStore) CacheWrap() interface{} {
// TODO: something here for sure
return i
}
// Set implements KVStore
func (i IAVLStore) Set(key, value []byte) (prev []byte) {
_, prev = i.tree.Get(key)
i.tree.Set(key, value)
return prev
}
// Get implements KVStore
func (i IAVLStore) Get(key []byte) (value []byte, exists bool) {
_, v := i.tree.Get(key)
return v, (v != nil)
}
// Has implements KVStore
func (i IAVLStore) Has(key []byte) (exists bool) {
return i.tree.Has(key)
}
// Remove implements KVStore
func (i IAVLStore) Remove(key []byte) (prev []byte, removed bool) {
return i.tree.Remove(key)
}
// var _ IterKVStore = IAVLStore{}
var _ KVStore = IAVLStore{}
2017-11-29 04:19:41 -08:00
// iavlLoader contains info on what store we want to load from
type iavlLoader struct {
dbName string
cacheSize int
history uint64
2017-12-01 14:08:37 -08:00
}
2017-11-29 04:19:41 -08:00
// Load implements CommitLoader type
func (l iavlLoader) Load(id CommitID) (Committer, error) {
2017-11-29 03:08:32 -08:00
// memory backed case, just for testing
2017-11-29 04:19:41 -08:00
if l.dbName == "" {
2017-11-29 03:08:32 -08:00
tree := iavl.NewVersionedTree(0, dbm.NewMemDB())
2017-11-29 05:07:39 -08:00
store := NewIAVLCommitter(tree, 0, l.history)
2017-11-29 04:19:41 -08:00
return store, nil
2017-11-29 03:08:32 -08:00
}
// Expand the path fully
2017-11-29 04:19:41 -08:00
dbPath, err := filepath.Abs(l.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-11-29 04:19:41 -08:00
tree := iavl.NewVersionedTree(l.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(),
l.history)
2017-11-29 04:19:41 -08:00
return store, nil
2017-11-29 03:08:32 -08:00
}