2015-02-16 07:28:33 -06:00
|
|
|
// Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package iterator provides interface and implementation to traverse over
|
|
|
|
// contents of a database.
|
|
|
|
package iterator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
|
|
|
)
|
|
|
|
|
2015-04-28 04:18:01 -05:00
|
|
|
var (
|
|
|
|
ErrIterReleased = errors.New("leveldb/iterator: iterator released")
|
|
|
|
)
|
|
|
|
|
2015-02-16 07:28:33 -06:00
|
|
|
// IteratorSeeker is the interface that wraps the 'seeks method'.
|
|
|
|
type IteratorSeeker interface {
|
|
|
|
// First moves the iterator to the first key/value pair. If the iterator
|
2016-10-20 07:56:34 -05:00
|
|
|
// only contains one key/value pair then First and Last would moves
|
2015-02-16 07:28:33 -06:00
|
|
|
// to the same key/value pair.
|
|
|
|
// It returns whether such pair exist.
|
|
|
|
First() bool
|
|
|
|
|
|
|
|
// Last moves the iterator to the last key/value pair. If the iterator
|
2016-10-20 07:56:34 -05:00
|
|
|
// only contains one key/value pair then First and Last would moves
|
2015-02-16 07:28:33 -06:00
|
|
|
// to the same key/value pair.
|
|
|
|
// It returns whether such pair exist.
|
|
|
|
Last() bool
|
|
|
|
|
|
|
|
// Seek moves the iterator to the first key/value pair whose key is greater
|
|
|
|
// than or equal to the given key.
|
|
|
|
// It returns whether such pair exist.
|
|
|
|
//
|
|
|
|
// It is safe to modify the contents of the argument after Seek returns.
|
|
|
|
Seek(key []byte) bool
|
|
|
|
|
|
|
|
// Next moves the iterator to the next key/value pair.
|
2018-11-28 13:53:02 -06:00
|
|
|
// It returns false if the iterator is exhausted.
|
2015-02-16 07:28:33 -06:00
|
|
|
Next() bool
|
|
|
|
|
|
|
|
// Prev moves the iterator to the previous key/value pair.
|
2018-11-28 13:53:02 -06:00
|
|
|
// It returns false if the iterator is exhausted.
|
2015-02-16 07:28:33 -06:00
|
|
|
Prev() bool
|
|
|
|
}
|
|
|
|
|
2016-10-20 07:56:34 -05:00
|
|
|
// CommonIterator is the interface that wraps common iterator methods.
|
2015-02-16 07:28:33 -06:00
|
|
|
type CommonIterator interface {
|
|
|
|
IteratorSeeker
|
|
|
|
|
|
|
|
// util.Releaser is the interface that wraps basic Release method.
|
|
|
|
// When called Release will releases any resources associated with the
|
|
|
|
// iterator.
|
|
|
|
util.Releaser
|
|
|
|
|
|
|
|
// util.ReleaseSetter is the interface that wraps the basic SetReleaser
|
|
|
|
// method.
|
|
|
|
util.ReleaseSetter
|
|
|
|
|
|
|
|
// TODO: Remove this when ready.
|
|
|
|
Valid() bool
|
|
|
|
|
|
|
|
// Error returns any accumulated error. Exhausting all the key/value pairs
|
|
|
|
// is not considered to be an error.
|
|
|
|
Error() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterator iterates over a DB's key/value pairs in key order.
|
|
|
|
//
|
2016-10-20 07:56:34 -05:00
|
|
|
// When encounter an error any 'seeks method' will return false and will
|
2015-02-16 07:28:33 -06:00
|
|
|
// yield no key/value pairs. The error can be queried by calling the Error
|
|
|
|
// method. Calling Release is still necessary.
|
|
|
|
//
|
|
|
|
// An iterator must be released after use, but it is not necessary to read
|
|
|
|
// an iterator until exhaustion.
|
2016-10-20 07:56:34 -05:00
|
|
|
// Also, an iterator is not necessarily safe for concurrent use, but it is
|
|
|
|
// safe to use multiple iterators concurrently, with each in a dedicated
|
|
|
|
// goroutine.
|
2015-02-16 07:28:33 -06:00
|
|
|
type Iterator interface {
|
|
|
|
CommonIterator
|
|
|
|
|
|
|
|
// Key returns the key of the current key/value pair, or nil if done.
|
|
|
|
// The caller should not modify the contents of the returned slice, and
|
|
|
|
// its contents may change on the next call to any 'seeks method'.
|
|
|
|
Key() []byte
|
|
|
|
|
2018-03-08 06:59:00 -06:00
|
|
|
// Value returns the value of the current key/value pair, or nil if done.
|
2015-02-16 07:28:33 -06:00
|
|
|
// The caller should not modify the contents of the returned slice, and
|
|
|
|
// its contents may change on the next call to any 'seeks method'.
|
|
|
|
Value() []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorCallbackSetter is the interface that wraps basic SetErrorCallback
|
|
|
|
// method.
|
|
|
|
//
|
|
|
|
// ErrorCallbackSetter implemented by indexed and merged iterator.
|
|
|
|
type ErrorCallbackSetter interface {
|
2016-10-20 07:56:34 -05:00
|
|
|
// SetErrorCallback allows set an error callback of the corresponding
|
2015-02-16 07:28:33 -06:00
|
|
|
// iterator. Use nil to clear the callback.
|
|
|
|
SetErrorCallback(f func(err error))
|
|
|
|
}
|
|
|
|
|
|
|
|
type emptyIterator struct {
|
2015-04-28 04:18:01 -05:00
|
|
|
util.BasicReleaser
|
|
|
|
err error
|
2015-02-16 07:28:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *emptyIterator) rErr() {
|
2015-04-28 04:18:01 -05:00
|
|
|
if i.err == nil && i.Released() {
|
|
|
|
i.err = ErrIterReleased
|
2015-02-16 07:28:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*emptyIterator) Valid() bool { return false }
|
|
|
|
func (i *emptyIterator) First() bool { i.rErr(); return false }
|
|
|
|
func (i *emptyIterator) Last() bool { i.rErr(); return false }
|
|
|
|
func (i *emptyIterator) Seek(key []byte) bool { i.rErr(); return false }
|
|
|
|
func (i *emptyIterator) Next() bool { i.rErr(); return false }
|
|
|
|
func (i *emptyIterator) Prev() bool { i.rErr(); return false }
|
|
|
|
func (*emptyIterator) Key() []byte { return nil }
|
|
|
|
func (*emptyIterator) Value() []byte { return nil }
|
|
|
|
func (i *emptyIterator) Error() error { return i.err }
|
|
|
|
|
|
|
|
// NewEmptyIterator creates an empty iterator. The err parameter can be
|
|
|
|
// nil, but if not nil the given err will be returned by Error method.
|
|
|
|
func NewEmptyIterator(err error) Iterator {
|
|
|
|
return &emptyIterator{err: err}
|
|
|
|
}
|