2016-02-10 02:29:58 +01:00
|
|
|
// Copyright (c) 2016 Couchbase, Inc.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the
|
|
|
|
// License. You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing,
|
|
|
|
// software distributed under the License is distributed on an "AS
|
|
|
|
// IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
|
|
|
// express or implied. See the License for the specific language
|
|
|
|
// governing permissions and limitations under the License.
|
|
|
|
|
|
|
|
package moss
|
|
|
|
|
|
|
|
import (
|
2016-07-22 03:31:07 +02:00
|
|
|
"math/big"
|
|
|
|
|
2016-02-10 02:29:58 +01:00
|
|
|
"github.com/couchbase/moss"
|
|
|
|
|
|
|
|
"github.com/blevesearch/bleve/index/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Reader struct {
|
2016-02-17 22:03:54 +01:00
|
|
|
store *Store
|
|
|
|
ss moss.Snapshot
|
2016-02-10 02:29:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) Get(k []byte) (v []byte, err error) {
|
|
|
|
v, err = r.ss.Get(k, moss.ReadOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if v != nil {
|
2016-07-15 23:41:27 +02:00
|
|
|
return append(make([]byte, 0, len(v)), v...), nil
|
2016-02-10 02:29:58 +01:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) MultiGet(keys [][]byte) ([][]byte, error) {
|
|
|
|
return store.MultiGet(r, keys)
|
|
|
|
}
|
|
|
|
|
2016-07-22 03:31:07 +02:00
|
|
|
var bigOne = big.NewInt(1)
|
|
|
|
|
2016-02-10 02:29:58 +01:00
|
|
|
func (r *Reader) PrefixIterator(k []byte) store.KVIterator {
|
2016-07-22 03:31:07 +02:00
|
|
|
kEnd := big.NewInt(0).Add(big.NewInt(0).SetBytes(k), bigOne).Bytes()
|
|
|
|
|
|
|
|
iter, err := r.ss.StartIterator(k, kEnd, moss.IteratorOptions{})
|
2016-02-10 02:29:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rv := &Iterator{
|
2016-07-22 03:31:07 +02:00
|
|
|
store: r.store,
|
|
|
|
ss: r.ss,
|
|
|
|
iter: iter,
|
|
|
|
start: k,
|
|
|
|
end: kEnd,
|
2016-02-10 02:29:58 +01:00
|
|
|
}
|
|
|
|
|
2016-08-19 01:37:21 +02:00
|
|
|
rv.current()
|
2016-02-10 02:29:58 +01:00
|
|
|
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) RangeIterator(start, end []byte) store.KVIterator {
|
|
|
|
iter, err := r.ss.StartIterator(start, end, moss.IteratorOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rv := &Iterator{
|
2016-07-22 03:31:07 +02:00
|
|
|
store: r.store,
|
|
|
|
ss: r.ss,
|
|
|
|
iter: iter,
|
|
|
|
start: start,
|
|
|
|
end: end,
|
2016-02-10 02:29:58 +01:00
|
|
|
}
|
|
|
|
|
2016-08-19 01:37:21 +02:00
|
|
|
rv.current()
|
2016-02-10 02:29:58 +01:00
|
|
|
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) Close() error {
|
|
|
|
return r.ss.Close()
|
|
|
|
}
|