Skip to content

Commit

Permalink
base variabls / logic, need tests
Browse files Browse the repository at this point in the history
  • Loading branch information
UnitylChaos committed Jul 2, 2018
1 parent f48881d commit 60a8a63
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
31 changes: 17 additions & 14 deletions store/iavlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (

const (
defaultIAVLCacheSize = 10000
defaultIAVLNumHistory = 1<<53 - 1 // DEPRECATED
defaultIAVLNumRecent = 1000
defaultIAVLStoreEvery = 10000
)

// load the iavl store
Expand All @@ -25,7 +26,7 @@ func LoadIAVLStore(db dbm.DB, id CommitID) (CommitStore, error) {
if err != nil {
return nil, err
}
store := newIAVLStore(tree, defaultIAVLNumHistory)
store := newIAVLStore(tree, defaultIAVLNumRecent, defaultIAVLStoreEvery)
return store, nil
}

Expand All @@ -43,16 +44,17 @@ type iavlStore struct {

// How many old versions we hold onto.
// A value of 0 means keep all history.
numHistory int64
numRecent int64

storeEvery int64
}

// CONTRACT: tree should be fully loaded.
// TODO: use more numHistory's, so the below nolint can be removed
// nolint: unparam
func newIAVLStore(tree *iavl.VersionedTree, numHistory int64) *iavlStore {
func newIAVLStore(tree *iavl.VersionedTree, numRecent int64, storeEvery int64) *iavlStore {
st := &iavlStore{
tree: tree,
numHistory: numHistory,
numRecent: numRecent,
storeEvery: storeEvery,
}
return st
}
Expand All @@ -67,13 +69,14 @@ func (st *iavlStore) Commit() CommitID {
panic(err)
}

// Release an old version of history
if st.numHistory > 0 && (st.numHistory < st.tree.Version64()) {
toRelease := version - st.numHistory
err := st.tree.DeleteVersion(toRelease)
if err != nil {
// TODO: Handle with #870
panic(err)
// Release an old version of history, if not a sync waypoint
if st.numRecent < st.tree.Version64() {
toRelease := version - st.numRecent
if toRelease%st.storeEvery != 0 {
err := st.tree.DeleteVersion(toRelease)
if err != nil {
panic(err)
}
}
}

Expand Down
13 changes: 7 additions & 6 deletions store/iavlstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (

var (
cacheSize = 100
numHistory int64 = 5
numRecent int64 = 5
storeEvery int64 = 3
)

var (
Expand Down Expand Up @@ -45,7 +46,7 @@ func newTree(t *testing.T, db dbm.DB) (*iavl.VersionedTree, CommitID) {
func TestIAVLStoreGetSetHasDelete(t *testing.T) {
db := dbm.NewMemDB()
tree, _ := newTree(t, db)
iavlStore := newIAVLStore(tree, numHistory)
iavlStore := newIAVLStore(tree, numRecent, storeEvery)

key := "hello"

Expand All @@ -70,7 +71,7 @@ func TestIAVLStoreGetSetHasDelete(t *testing.T) {
func TestIAVLIterator(t *testing.T) {
db := dbm.NewMemDB()
tree, _ := newTree(t, db)
iavlStore := newIAVLStore(tree, numHistory)
iavlStore := newIAVLStore(tree, numRecent, storeEvery)
iter := iavlStore.Iterator([]byte("aloha"), []byte("hellz"))
expected := []string{"aloha", "hello"}
var i int
Expand Down Expand Up @@ -143,7 +144,7 @@ func TestIAVLIterator(t *testing.T) {
func TestIAVLSubspaceIterator(t *testing.T) {
db := dbm.NewMemDB()
tree, _ := newTree(t, db)
iavlStore := newIAVLStore(tree, numHistory)
iavlStore := newIAVLStore(tree, numRecent, storeEvery)

iavlStore.Set([]byte("test1"), []byte("test1"))
iavlStore.Set([]byte("test2"), []byte("test2"))
Expand Down Expand Up @@ -202,7 +203,7 @@ func TestIAVLSubspaceIterator(t *testing.T) {
func TestIAVLReverseSubspaceIterator(t *testing.T) {
db := dbm.NewMemDB()
tree, _ := newTree(t, db)
iavlStore := newIAVLStore(tree, numHistory)
iavlStore := newIAVLStore(tree, numRecent, storeEvery)

iavlStore.Set([]byte("test1"), []byte("test1"))
iavlStore.Set([]byte("test2"), []byte("test2"))
Expand Down Expand Up @@ -261,7 +262,7 @@ func TestIAVLReverseSubspaceIterator(t *testing.T) {
func TestIAVLStoreQuery(t *testing.T) {
db := dbm.NewMemDB()
tree := iavl.NewVersionedTree(db, cacheSize)
iavlStore := newIAVLStore(tree, numHistory)
iavlStore := newIAVLStore(tree, numRecent, storeEvery)

k1, v1 := []byte("key1"), []byte("val1")
k2, v2 := []byte("key2"), []byte("val2")
Expand Down
2 changes: 1 addition & 1 deletion store/prefixstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func testPrefixStore(t *testing.T, baseStore KVStore, prefix []byte) {
func TestIAVLStorePrefix(t *testing.T) {
db := dbm.NewMemDB()
tree := iavl.NewVersionedTree(db, cacheSize)
iavlStore := newIAVLStore(tree, numHistory)
iavlStore := newIAVLStore(tree, numRecent, storeEvery)

testPrefixStore(t, iavlStore, []byte("test"))
}
Expand Down

0 comments on commit 60a8a63

Please sign in to comment.