1//  Copyright (c) 2015 Couchbase, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// 		http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Package gtreap provides an in-memory implementation of the
16// KVStore interfaces using the gtreap balanced-binary treap,
17// copy-on-write data structure.
18package gtreap
19
20import (
21	"github.com/blevesearch/bleve/index/store"
22
23	"github.com/steveyen/gtreap"
24)
25
26type Reader struct {
27	t *gtreap.Treap
28}
29
30func (w *Reader) Get(k []byte) (v []byte, err error) {
31	var rv []byte
32	itm := w.t.Get(&Item{k: k})
33	if itm != nil {
34		rv = make([]byte, len(itm.(*Item).v))
35		copy(rv, itm.(*Item).v)
36		return rv, nil
37	}
38	return nil, nil
39}
40
41func (r *Reader) MultiGet(keys [][]byte) ([][]byte, error) {
42	return store.MultiGet(r, keys)
43}
44
45func (w *Reader) PrefixIterator(k []byte) store.KVIterator {
46	rv := Iterator{
47		t:      w.t,
48		prefix: k,
49	}
50	rv.restart(&Item{k: k})
51	return &rv
52}
53
54func (w *Reader) RangeIterator(start, end []byte) store.KVIterator {
55	rv := Iterator{
56		t:     w.t,
57		start: start,
58		end:   end,
59	}
60	rv.restart(&Item{k: start})
61	return &rv
62}
63
64func (w *Reader) Close() error {
65	return nil
66}
67