2019-02-01 17:03:09 -08:00
|
|
|
package cachekv
|
2017-12-11 23:30:44 -08:00
|
|
|
|
2017-12-12 22:17:20 -08:00
|
|
|
import (
|
2019-05-15 07:42:06 -07:00
|
|
|
"container/list"
|
2017-12-21 20:05:41 -08:00
|
|
|
|
2018-07-02 13:34:06 -07:00
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
2019-08-02 06:20:39 -07:00
|
|
|
dbm "github.com/tendermint/tm-db"
|
2017-12-12 22:17:20 -08:00
|
|
|
)
|
|
|
|
|
2017-12-11 23:30:44 -08:00
|
|
|
// Iterates over iterKVCache items.
|
|
|
|
// if key is nil, means it was deleted.
|
|
|
|
// Implements Iterator.
|
|
|
|
type memIterator struct {
|
|
|
|
start, end []byte
|
2019-05-15 07:42:06 -07:00
|
|
|
items []*cmn.KVPair
|
|
|
|
ascending bool
|
2017-12-11 23:30:44 -08:00
|
|
|
}
|
|
|
|
|
2019-05-15 07:42:06 -07:00
|
|
|
func newMemIterator(start, end []byte, items *list.List, ascending bool) *memIterator {
|
|
|
|
itemsInDomain := make([]*cmn.KVPair, 0)
|
|
|
|
var entered bool
|
|
|
|
for e := items.Front(); e != nil; e = e.Next() {
|
|
|
|
item := e.Value.(*cmn.KVPair)
|
|
|
|
if !dbm.IsKeyInDomain(item.Key, start, end) {
|
|
|
|
if entered {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
2017-12-12 22:17:20 -08:00
|
|
|
}
|
2019-05-15 07:42:06 -07:00
|
|
|
itemsInDomain = append(itemsInDomain, item)
|
|
|
|
entered = true
|
2017-12-12 22:17:20 -08:00
|
|
|
}
|
2019-05-15 07:42:06 -07:00
|
|
|
|
2017-12-11 23:30:44 -08:00
|
|
|
return &memIterator{
|
2019-05-15 07:42:06 -07:00
|
|
|
start: start,
|
|
|
|
end: end,
|
|
|
|
items: itemsInDomain,
|
|
|
|
ascending: ascending,
|
2017-12-11 23:30:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mi *memIterator) Domain() ([]byte, []byte) {
|
|
|
|
return mi.start, mi.end
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mi *memIterator) Valid() bool {
|
|
|
|
return len(mi.items) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mi *memIterator) assertValid() {
|
|
|
|
if !mi.Valid() {
|
|
|
|
panic("memIterator is invalid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mi *memIterator) Next() {
|
|
|
|
mi.assertValid()
|
2019-05-15 07:42:06 -07:00
|
|
|
if mi.ascending {
|
|
|
|
mi.items = mi.items[1:]
|
|
|
|
} else {
|
|
|
|
mi.items = mi.items[:len(mi.items)-1]
|
|
|
|
}
|
2017-12-11 23:30:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mi *memIterator) Key() []byte {
|
|
|
|
mi.assertValid()
|
2019-05-15 07:42:06 -07:00
|
|
|
if mi.ascending {
|
|
|
|
return mi.items[0].Key
|
|
|
|
}
|
|
|
|
return mi.items[len(mi.items)-1].Key
|
2017-12-11 23:30:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mi *memIterator) Value() []byte {
|
|
|
|
mi.assertValid()
|
2019-05-15 07:42:06 -07:00
|
|
|
if mi.ascending {
|
|
|
|
return mi.items[0].Value
|
|
|
|
}
|
|
|
|
return mi.items[len(mi.items)-1].Value
|
2017-12-11 23:30:44 -08:00
|
|
|
}
|
|
|
|
|
2017-12-12 22:17:20 -08:00
|
|
|
func (mi *memIterator) Close() {
|
2017-12-11 23:30:44 -08:00
|
|
|
mi.start = nil
|
|
|
|
mi.end = nil
|
|
|
|
mi.items = nil
|
|
|
|
}
|