iterator.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package ethdb
  17. // Iterator iterates over a database's key/value pairs in ascending key order.
  18. //
  19. // When it encounters an error any seek will return false and will yield no key/
  20. // value pairs. The error can be queried by calling the Error method. Calling
  21. // Release is still necessary.
  22. //
  23. // An iterator must be released after use, but it is not necessary to read an
  24. // iterator until exhaustion. An iterator is not safe for concurrent use, but it
  25. // is safe to use multiple iterators concurrently.
  26. type Iterator interface {
  27. // Next moves the iterator to the next key/value pair. It returns whether the
  28. // iterator is exhausted.
  29. Next() bool
  30. // Error returns any accumulated error. Exhausting all the key/value pairs
  31. // is not considered to be an error.
  32. Error() error
  33. // Key returns the key of the current key/value pair, or nil if done. The caller
  34. // should not modify the contents of the returned slice, and its contents may
  35. // change on the next call to Next.
  36. Key() []byte
  37. // Value returns the value of the current key/value pair, or nil if done. The
  38. // caller should not modify the contents of the returned slice, and its contents
  39. // may change on the next call to Next.
  40. Value() []byte
  41. // Release releases associated resources. Release should always succeed and can
  42. // be called multiple times without causing error.
  43. Release()
  44. }
  45. // Iteratee wraps the NewIterator methods of a backing data store.
  46. type Iteratee interface {
  47. // NewIterator creates a binary-alphabetical iterator over a subset
  48. // of database content with a particular key prefix, starting at a particular
  49. // initial key (or after, if it does not exist).
  50. //
  51. // Note: This method assumes that the prefix is NOT part of the start, so there's
  52. // no need for the caller to prepend the prefix to the start
  53. NewIterator(prefix []byte, start []byte) Iterator
  54. }