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" "bytes"
"sort" "sort"
"sync" "sync"
cmn "github.com/tendermint/tmlibs/common"
) )
// If value is nil but deleted is false, it means the parent doesn't have the // 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) sort.Strings(keys)
// TODO in tmlibs/db we use Batch to write atomically. // TODO: Consider allowing usage of Batch, which would allow the write to
// Consider allowing usage of Batch. // at least happen atomically.
for _, key := range keys { for _, key := range keys {
cacheValue := ci.cache[key] cacheValue := ci.cache[key]
if cacheValue.deleted { 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. // Constructs a slice of dirty items, to use w/ memIterator.
func (ci *cacheKVStore) dirtyItems(ascending bool) []KVPair { func (ci *cacheKVStore) dirtyItems(ascending bool) []cmn.KVPair {
items := make([]KVPair, 0, len(ci.cache)) items := make([]cmn.KVPair, 0, len(ci.cache))
for key, cacheValue := range ci.cache { for key, cacheValue := range ci.cache {
if !cacheValue.dirty { if !cacheValue.dirty {
continue continue
} }
items = append(items, items = append(items,
KVPair{[]byte(key), cacheValue.value}) cmn.KVPair{[]byte(key), cacheValue.value})
} }
sort.Slice(items, func(i, j int) bool { sort.Slice(items, func(i, j int) bool {
if ascending { if ascending {

View File

@ -1,24 +1,27 @@
package store package store
import "bytes" import (
"bytes"
cmn "github.com/tendermint/tmlibs/common"
)
// Gets the first item. // 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) iter := st.Iterator(start, end)
if !iter.Valid() { if !iter.Valid() {
return kv, false return kv, false
} }
defer iter.Close() 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. // 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) iter := st.ReverseIterator(end, start)
if !iter.Valid() { if !iter.Valid() {
if v := st.Get(start); v != nil { if v := st.Get(start); v != nil {
return KVPair{cp(start), cp(v)}, true return cmn.KVPair{cp(start), cp(v)}, true
} else { } else {
return kv, false 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" "sync"
"github.com/tendermint/iavl" "github.com/tendermint/iavl"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db" dbm "github.com/tendermint/tmlibs/db"
) )
@ -131,7 +132,7 @@ type iavlIterator struct {
ascending bool ascending bool
// Channel to push iteration values. // Channel to push iteration values.
iterCh chan KVPair iterCh chan cmn.KVPair
// Close this to release goroutine. // Close this to release goroutine.
quitCh chan struct{} quitCh chan struct{}
@ -159,7 +160,7 @@ func newIAVLIterator(tree *iavl.Tree, start, end []byte, ascending bool) *iavlIt
start: cp(start), start: cp(start),
end: cp(end), end: cp(end),
ascending: ascending, ascending: ascending,
iterCh: make(chan KVPair, 0), // Set capacity > 0? iterCh: make(chan cmn.KVPair, 0), // Set capacity > 0?
quitCh: make(chan struct{}), quitCh: make(chan struct{}),
initCh: make(chan struct{}), initCh: make(chan struct{}),
} }
@ -176,7 +177,7 @@ func (iter *iavlIterator) iterateRoutine() {
select { select {
case <-iter.quitCh: case <-iter.quitCh:
return true // done with iteration. return true // done with iteration.
case iter.iterCh <- KVPair{key, value}: case iter.iterCh <- cmn.KVPair{key, value}:
return false // yay. return false // yay.
} }
}, },

View File

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

View File

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