From 62e6f1f6489a329376d263c10ca969316715e47b Mon Sep 17 00:00:00 2001 From: Steve Yen Date: Tue, 11 Oct 2016 09:12:37 -0700 Subject: [PATCH] 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. --- index/store/moss/reader.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/index/store/moss/reader.go b/index/store/moss/reader.go index fe3a154d..6aca999e 100644 --- a/index/store/moss/reader.go +++ b/index/store/moss/reader.go @@ -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 +}