1 // Copyright (c) 2014, Suryandaru Triandana <syndtr@gmail.com>
2 // All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
10 "github.com/syndtr/goleveldb/leveldb/util"
13 // BasicArray is the interface that wraps basic Len and Search method.
14 type BasicArray interface {
15 // Len returns length of the array.
18 // Search finds smallest index that point to a key that is greater
19 // than or equal to the given key.
20 Search(key []byte) int
23 // Array is the interface that wraps BasicArray and basic Index method.
24 type Array interface {
27 // Index returns key/value pair with index of i.
28 Index(i int) (key, value []byte)
31 // Array is the interface that wraps BasicArray and basic Get method.
32 type ArrayIndexer interface {
35 // Get returns a new data iterator with index of i.
39 type basicArrayIterator struct {
46 func (i *basicArrayIterator) Valid() bool {
47 return i.pos >= 0 && i.pos < i.array.Len() && !i.Released()
50 func (i *basicArrayIterator) First() bool {
52 i.err = ErrIterReleased
56 if i.array.Len() == 0 {
64 func (i *basicArrayIterator) Last() bool {
66 i.err = ErrIterReleased
79 func (i *basicArrayIterator) Seek(key []byte) bool {
81 i.err = ErrIterReleased
90 i.pos = i.array.Search(key)
97 func (i *basicArrayIterator) Next() bool {
99 i.err = ErrIterReleased
104 if n := i.array.Len(); i.pos >= n {
111 func (i *basicArrayIterator) Prev() bool {
113 i.err = ErrIterReleased
125 func (i *basicArrayIterator) Error() error { return i.err }
127 type arrayIterator struct {
134 func (i *arrayIterator) updateKV() {
135 if i.pos == i.basicArrayIterator.pos {
138 i.pos = i.basicArrayIterator.pos
140 i.key, i.value = i.array.Index(i.pos)
147 func (i *arrayIterator) Key() []byte {
152 func (i *arrayIterator) Value() []byte {
157 type arrayIteratorIndexer struct {
162 func (i *arrayIteratorIndexer) Get() Iterator {
164 return i.array.Get(i.basicArrayIterator.pos)
169 // NewArrayIterator returns an iterator from the given array.
170 func NewArrayIterator(array Array) Iterator {
171 return &arrayIterator{
172 basicArrayIterator: basicArrayIterator{array: array, pos: -1},
178 // NewArrayIndexer returns an index iterator from the given array.
179 func NewArrayIndexer(array ArrayIndexer) IteratorIndexer {
180 return &arrayIteratorIndexer{
181 basicArrayIterator: basicArrayIterator{array: array, pos: -1},