2017-12-11 23:30:44 -08:00
|
|
|
package store
|
|
|
|
|
2017-12-17 18:09:39 -08:00
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-18 21:49:24 -07:00
|
|
|
|
2020-01-16 13:46:51 -08:00
|
|
|
tmkv "github.com/tendermint/tendermint/libs/kv"
|
2019-02-01 17:03:09 -08:00
|
|
|
|
2020-03-01 16:27:03 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2017-12-17 18:09:39 -08:00
|
|
|
)
|
2017-12-11 23:30:44 -08:00
|
|
|
|
2017-12-12 20:13:51 -08:00
|
|
|
// Gets the first item.
|
2020-01-16 13:46:51 -08:00
|
|
|
func First(st KVStore, start, end []byte) (kv tmkv.Pair, ok bool) {
|
2017-12-11 23:30:44 -08:00
|
|
|
iter := st.Iterator(start, end)
|
|
|
|
if !iter.Valid() {
|
|
|
|
return kv, false
|
|
|
|
}
|
2017-12-12 22:17:20 -08:00
|
|
|
defer iter.Close()
|
2017-12-12 20:13:51 -08:00
|
|
|
|
2020-01-16 13:46:51 -08:00
|
|
|
return tmkv.Pair{Key: iter.Key(), Value: iter.Value()}, true
|
2017-12-11 23:30:44 -08:00
|
|
|
}
|
|
|
|
|
2017-12-12 20:13:51 -08:00
|
|
|
// Gets the last item. `end` is exclusive.
|
2020-01-16 13:46:51 -08:00
|
|
|
func Last(st KVStore, start, end []byte) (kv tmkv.Pair, ok bool) {
|
2017-12-11 23:30:44 -08:00
|
|
|
iter := st.ReverseIterator(end, start)
|
|
|
|
if !iter.Valid() {
|
|
|
|
if v := st.Get(start); v != nil {
|
2020-03-01 16:27:03 -08:00
|
|
|
return tmkv.Pair{Key: sdk.CopyBytes(start), Value: sdk.CopyBytes(v)}, true
|
2017-12-11 23:30:44 -08:00
|
|
|
}
|
2018-04-18 21:49:24 -07:00
|
|
|
return kv, false
|
2017-12-11 23:30:44 -08:00
|
|
|
}
|
2017-12-12 22:17:20 -08:00
|
|
|
defer iter.Close()
|
2017-12-11 23:30:44 -08:00
|
|
|
|
|
|
|
if bytes.Equal(iter.Key(), end) {
|
|
|
|
// Skip this one, end is exclusive.
|
|
|
|
iter.Next()
|
|
|
|
if !iter.Valid() {
|
|
|
|
return kv, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 13:46:51 -08:00
|
|
|
return tmkv.Pair{Key: iter.Key(), Value: iter.Value()}, true
|
2017-12-11 23:30:44 -08:00
|
|
|
}
|