Conform to new tmlibs Iterator

This commit is contained in:
Jae Kwon 2017-12-17 18:09:39 -08:00
parent 1ec9e2e0ae
commit 6f9a08d921
5 changed files with 25 additions and 23 deletions

View File

@ -4,6 +4,8 @@ import (
"bytes"
"sort"
"sync"
cmn "github.com/tendermint/tmlibs/common"
)
// If value is nil but deleted is false, it means the parent doesn't have the
@ -85,8 +87,8 @@ func (ci *cacheKVStore) Write() {
}
sort.Strings(keys)
// TODO in tmlibs/db we use Batch to write atomically.
// Consider allowing usage of Batch.
// TODO: Consider allowing usage of Batch, which would allow the write to
// at least happen atomically.
for _, key := range keys {
cacheValue := ci.cache[key]
if cacheValue.deleted {
@ -133,14 +135,14 @@ func (ci *cacheKVStore) iterator(start, end []byte, ascending bool) Iterator {
}
// Constructs a slice of dirty items, to use w/ memIterator.
func (ci *cacheKVStore) dirtyItems(ascending bool) []KVPair {
items := make([]KVPair, 0, len(ci.cache))
func (ci *cacheKVStore) dirtyItems(ascending bool) []cmn.KVPair {
items := make([]cmn.KVPair, 0, len(ci.cache))
for key, cacheValue := range ci.cache {
if !cacheValue.dirty {
continue
}
items = append(items,
KVPair{[]byte(key), cacheValue.value})
cmn.KVPair{[]byte(key), cacheValue.value})
}
sort.Slice(items, func(i, j int) bool {
if ascending {

View File

@ -1,24 +1,27 @@
package store
import "bytes"
import (
"bytes"
cmn "github.com/tendermint/tmlibs/common"
)
// Gets the first item.
func First(st KVStore, start, end []byte) (kv KVPair, ok bool) {
func First(st KVStore, start, end []byte) (kv cmn.KVPair, ok bool) {
iter := st.Iterator(start, end)
if !iter.Valid() {
return kv, false
}
defer iter.Close()
return KVPair{iter.Key(), iter.Value()}, true
return cmn.KVPair{iter.Key(), iter.Value()}, true
}
// Gets the last item. `end` is exclusive.
func Last(st KVStore, start, end []byte) (kv KVPair, ok bool) {
func Last(st KVStore, start, end []byte) (kv cmn.KVPair, ok bool) {
iter := st.ReverseIterator(end, start)
if !iter.Valid() {
if v := st.Get(start); v != nil {
return KVPair{cp(start), cp(v)}, true
return cmn.KVPair{cp(start), cp(v)}, true
} else {
return kv, false
}
@ -33,5 +36,5 @@ func Last(st KVStore, start, end []byte) (kv KVPair, ok bool) {
}
}
return KVPair{iter.Key(), iter.Value()}, true
return cmn.KVPair{iter.Key(), iter.Value()}, true
}

View File

@ -4,6 +4,7 @@ import (
"sync"
"github.com/tendermint/iavl"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
)
@ -131,7 +132,7 @@ type iavlIterator struct {
ascending bool
// Channel to push iteration values.
iterCh chan KVPair
iterCh chan cmn.KVPair
// Close this to release goroutine.
quitCh chan struct{}
@ -159,7 +160,7 @@ func newIAVLIterator(tree *iavl.Tree, start, end []byte, ascending bool) *iavlIt
start: cp(start),
end: cp(end),
ascending: ascending,
iterCh: make(chan KVPair, 0), // Set capacity > 0?
iterCh: make(chan cmn.KVPair, 0), // Set capacity > 0?
quitCh: make(chan struct{}),
initCh: make(chan struct{}),
}
@ -176,7 +177,7 @@ func (iter *iavlIterator) iterateRoutine() {
select {
case <-iter.quitCh:
return true // done with iteration.
case iter.iterCh <- KVPair{key, value}:
case iter.iterCh <- cmn.KVPair{key, value}:
return false // yay.
}
},

View File

@ -1,6 +1,7 @@
package store
import (
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
)
@ -9,10 +10,10 @@ import (
// Implements Iterator.
type memIterator struct {
start, end []byte
items []KVPair
items []cmn.KVPair
}
func newMemIterator(start, end []byte, items []KVPair) *memIterator {
func newMemIterator(start, end []byte, items []cmn.KVPair) *memIterator {
itemsInDomain := make([]KVPair, 0)
for _, item := range items {
ascending := keyCompare(start, end) < 0

View File

@ -2,8 +2,8 @@ package store
import (
"bytes"
"fmt"
"github.com/tendermint/go-wire/data"
"github.com/tendermint/tmlibs/db"
)
@ -138,11 +138,6 @@ type CacheWrap interface {
//----------------------------------------
// etc
type KVPair struct {
Key data.Bytes
Value data.Bytes
}
// CommitID contains the tree version number and its merkle root.
type CommitID struct {
Version int64
@ -154,7 +149,7 @@ func (cid CommitID) IsZero() bool {
}
func (cid CommitID) String() string {
return fmt.Spritnf("CommitID{%v:%X}", cid.Hash, cid.Version)
return fmt.Sprintf("CommitID{%v:%X}", cid.Hash, cid.Version)
}
// bytes.Compare but bounded on both sides by nil.