You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.4 KiB

  1. // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
  2. // All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style license that can be
  5. // found in the LICENSE file.
  6. // Package iterator provides interface and implementation to traverse over
  7. // contents of a database.
  8. package iterator
  9. import (
  10. "errors"
  11. "github.com/syndtr/goleveldb/leveldb/util"
  12. )
  13. var (
  14. ErrIterReleased = errors.New("leveldb/iterator: iterator released")
  15. )
  16. // IteratorSeeker is the interface that wraps the 'seeks method'.
  17. type IteratorSeeker interface {
  18. // First moves the iterator to the first key/value pair. If the iterator
  19. // only contains one key/value pair then First and Last would moves
  20. // to the same key/value pair.
  21. // It returns whether such pair exist.
  22. First() bool
  23. // Last moves the iterator to the last key/value pair. If the iterator
  24. // only contains one key/value pair then First and Last would moves
  25. // to the same key/value pair.
  26. // It returns whether such pair exist.
  27. Last() bool
  28. // Seek moves the iterator to the first key/value pair whose key is greater
  29. // than or equal to the given key.
  30. // It returns whether such pair exist.
  31. //
  32. // It is safe to modify the contents of the argument after Seek returns.
  33. Seek(key []byte) bool
  34. // Next moves the iterator to the next key/value pair.
  35. // It returns whether the iterator is exhausted.
  36. Next() bool
  37. // Prev moves the iterator to the previous key/value pair.
  38. // It returns whether the iterator is exhausted.
  39. Prev() bool
  40. }
  41. // CommonIterator is the interface that wraps common iterator methods.
  42. type CommonIterator interface {
  43. IteratorSeeker
  44. // util.Releaser is the interface that wraps basic Release method.
  45. // When called Release will releases any resources associated with the
  46. // iterator.
  47. util.Releaser
  48. // util.ReleaseSetter is the interface that wraps the basic SetReleaser
  49. // method.
  50. util.ReleaseSetter
  51. // TODO: Remove this when ready.
  52. Valid() bool
  53. // Error returns any accumulated error. Exhausting all the key/value pairs
  54. // is not considered to be an error.
  55. Error() error
  56. }
  57. // Iterator iterates over a DB's key/value pairs in key order.
  58. //
  59. // When encounter an error any 'seeks method' will return false and will
  60. // yield no key/value pairs. The error can be queried by calling the Error
  61. // method. Calling Release is still necessary.
  62. //
  63. // An iterator must be released after use, but it is not necessary to read
  64. // an iterator until exhaustion.
  65. // Also, an iterator is not necessarily safe for concurrent use, but it is
  66. // safe to use multiple iterators concurrently, with each in a dedicated
  67. // goroutine.
  68. type Iterator interface {
  69. CommonIterator
  70. // Key returns the key of the current key/value pair, or nil if done.
  71. // The caller should not modify the contents of the returned slice, and
  72. // its contents may change on the next call to any 'seeks method'.
  73. Key() []byte
  74. // Value returns the value of the current key/value pair, or nil if done.
  75. // The caller should not modify the contents of the returned slice, and
  76. // its contents may change on the next call to any 'seeks method'.
  77. Value() []byte
  78. }
  79. // ErrorCallbackSetter is the interface that wraps basic SetErrorCallback
  80. // method.
  81. //
  82. // ErrorCallbackSetter implemented by indexed and merged iterator.
  83. type ErrorCallbackSetter interface {
  84. // SetErrorCallback allows set an error callback of the corresponding
  85. // iterator. Use nil to clear the callback.
  86. SetErrorCallback(f func(err error))
  87. }
  88. type emptyIterator struct {
  89. util.BasicReleaser
  90. err error
  91. }
  92. func (i *emptyIterator) rErr() {
  93. if i.err == nil && i.Released() {
  94. i.err = ErrIterReleased
  95. }
  96. }
  97. func (*emptyIterator) Valid() bool { return false }
  98. func (i *emptyIterator) First() bool { i.rErr(); return false }
  99. func (i *emptyIterator) Last() bool { i.rErr(); return false }
  100. func (i *emptyIterator) Seek(key []byte) bool { i.rErr(); return false }
  101. func (i *emptyIterator) Next() bool { i.rErr(); return false }
  102. func (i *emptyIterator) Prev() bool { i.rErr(); return false }
  103. func (*emptyIterator) Key() []byte { return nil }
  104. func (*emptyIterator) Value() []byte { return nil }
  105. func (i *emptyIterator) Error() error { return i.err }
  106. // NewEmptyIterator creates an empty iterator. The err parameter can be
  107. // nil, but if not nil the given err will be returned by Error method.
  108. func NewEmptyIterator(err error) Iterator {
  109. return &emptyIterator{err: err}
  110. }