forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firstlast.go
40 lines (33 loc) · 821 Bytes
/
firstlast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package store
import (
"bytes"
cmn "github.com/tendermint/tendermint/libs/common"
)
// Gets the first item.
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 cmn.KVPair{iter.Key(), iter.Value()}, true
}
// Gets the last item. `end` is exclusive.
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 cmn.KVPair{cp(start), cp(v)}, true
}
return kv, false
}
defer iter.Close()
if bytes.Equal(iter.Key(), end) {
// Skip this one, end is exclusive.
iter.Next()
if !iter.Valid() {
return kv, false
}
}
return cmn.KVPair{iter.Key(), iter.Value()}, true
}