reuse incrementBytes() in moss KV store integration
In this commit, I saw that there was a simple incrementBytes() implementation elsewhere in bleve that seemed simpler than using the big int package. Edge case note: if the input bytes would overflow in incrementBytes(), such as with an input of [0xff 0xff 0xff], it returns nil. moss then treats a nil endKeyExclusive iterator param as a logical "higher-than-topmost" key, which produces the prefix iteration behavior that we want for this edge situation.
This commit is contained in:
parent
d026a44230
commit
62e6f1f648
|
@ -15,8 +15,6 @@
|
|||
package moss
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
"github.com/couchbase/moss"
|
||||
|
||||
"github.com/blevesearch/bleve/index/store"
|
||||
|
@ -42,10 +40,8 @@ func (r *Reader) MultiGet(keys [][]byte) ([][]byte, error) {
|
|||
return store.MultiGet(r, keys)
|
||||
}
|
||||
|
||||
var bigOne = big.NewInt(1)
|
||||
|
||||
func (r *Reader) PrefixIterator(k []byte) store.KVIterator {
|
||||
kEnd := big.NewInt(0).Add(big.NewInt(0).SetBytes(k), bigOne).Bytes()
|
||||
kEnd := incrementBytes(k)
|
||||
|
||||
iter, err := r.ss.StartIterator(k, kEnd, moss.IteratorOptions{})
|
||||
if err != nil {
|
||||
|
@ -87,3 +83,15 @@ func (r *Reader) RangeIterator(start, end []byte) store.KVIterator {
|
|||
func (r *Reader) Close() error {
|
||||
return r.ss.Close()
|
||||
}
|
||||
|
||||
func incrementBytes(in []byte) []byte {
|
||||
rv := make([]byte, len(in))
|
||||
copy(rv, in)
|
||||
for i := len(rv) - 1; i >= 0; i-- {
|
||||
rv[i] = rv[i] + 1
|
||||
if rv[i] != 0 {
|
||||
return rv // didn't overflow, so stop
|
||||
}
|
||||
}
|
||||
return nil // overflowed
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user