indexed_iter.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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
  7. import (
  8. "github.com/syndtr/goleveldb/leveldb/errors"
  9. "github.com/syndtr/goleveldb/leveldb/util"
  10. )
  11. // IteratorIndexer is the interface that wraps CommonIterator and basic Get
  12. // method. IteratorIndexer provides index for indexed iterator.
  13. type IteratorIndexer interface {
  14. CommonIterator
  15. // Get returns a new data iterator for the current position, or nil if
  16. // done.
  17. Get() Iterator
  18. }
  19. type indexedIterator struct {
  20. util.BasicReleaser
  21. index IteratorIndexer
  22. strict bool
  23. data Iterator
  24. err error
  25. errf func(err error)
  26. closed bool
  27. }
  28. func (i *indexedIterator) setData() {
  29. if i.data != nil {
  30. i.data.Release()
  31. }
  32. i.data = i.index.Get()
  33. }
  34. func (i *indexedIterator) clearData() {
  35. if i.data != nil {
  36. i.data.Release()
  37. }
  38. i.data = nil
  39. }
  40. func (i *indexedIterator) indexErr() {
  41. if err := i.index.Error(); err != nil {
  42. if i.errf != nil {
  43. i.errf(err)
  44. }
  45. i.err = err
  46. }
  47. }
  48. func (i *indexedIterator) dataErr() bool {
  49. if err := i.data.Error(); err != nil {
  50. if i.errf != nil {
  51. i.errf(err)
  52. }
  53. if i.strict || !errors.IsCorrupted(err) {
  54. i.err = err
  55. return true
  56. }
  57. }
  58. return false
  59. }
  60. func (i *indexedIterator) Valid() bool {
  61. return i.data != nil && i.data.Valid()
  62. }
  63. func (i *indexedIterator) First() bool {
  64. if i.err != nil {
  65. return false
  66. } else if i.Released() {
  67. i.err = ErrIterReleased
  68. return false
  69. }
  70. if !i.index.First() {
  71. i.indexErr()
  72. i.clearData()
  73. return false
  74. }
  75. i.setData()
  76. return i.Next()
  77. }
  78. func (i *indexedIterator) Last() bool {
  79. if i.err != nil {
  80. return false
  81. } else if i.Released() {
  82. i.err = ErrIterReleased
  83. return false
  84. }
  85. if !i.index.Last() {
  86. i.indexErr()
  87. i.clearData()
  88. return false
  89. }
  90. i.setData()
  91. if !i.data.Last() {
  92. if i.dataErr() {
  93. return false
  94. }
  95. i.clearData()
  96. return i.Prev()
  97. }
  98. return true
  99. }
  100. func (i *indexedIterator) Seek(key []byte) bool {
  101. if i.err != nil {
  102. return false
  103. } else if i.Released() {
  104. i.err = ErrIterReleased
  105. return false
  106. }
  107. if !i.index.Seek(key) {
  108. i.indexErr()
  109. i.clearData()
  110. return false
  111. }
  112. i.setData()
  113. if !i.data.Seek(key) {
  114. if i.dataErr() {
  115. return false
  116. }
  117. i.clearData()
  118. return i.Next()
  119. }
  120. return true
  121. }
  122. func (i *indexedIterator) Next() bool {
  123. if i.err != nil {
  124. return false
  125. } else if i.Released() {
  126. i.err = ErrIterReleased
  127. return false
  128. }
  129. switch {
  130. case i.data != nil && !i.data.Next():
  131. if i.dataErr() {
  132. return false
  133. }
  134. i.clearData()
  135. fallthrough
  136. case i.data == nil:
  137. if !i.index.Next() {
  138. i.indexErr()
  139. return false
  140. }
  141. i.setData()
  142. return i.Next()
  143. }
  144. return true
  145. }
  146. func (i *indexedIterator) Prev() bool {
  147. if i.err != nil {
  148. return false
  149. } else if i.Released() {
  150. i.err = ErrIterReleased
  151. return false
  152. }
  153. switch {
  154. case i.data != nil && !i.data.Prev():
  155. if i.dataErr() {
  156. return false
  157. }
  158. i.clearData()
  159. fallthrough
  160. case i.data == nil:
  161. if !i.index.Prev() {
  162. i.indexErr()
  163. return false
  164. }
  165. i.setData()
  166. if !i.data.Last() {
  167. if i.dataErr() {
  168. return false
  169. }
  170. i.clearData()
  171. return i.Prev()
  172. }
  173. }
  174. return true
  175. }
  176. func (i *indexedIterator) Key() []byte {
  177. if i.data == nil {
  178. return nil
  179. }
  180. return i.data.Key()
  181. }
  182. func (i *indexedIterator) Value() []byte {
  183. if i.data == nil {
  184. return nil
  185. }
  186. return i.data.Value()
  187. }
  188. func (i *indexedIterator) Release() {
  189. i.clearData()
  190. i.index.Release()
  191. i.BasicReleaser.Release()
  192. }
  193. func (i *indexedIterator) Error() error {
  194. if i.err != nil {
  195. return i.err
  196. }
  197. if err := i.index.Error(); err != nil {
  198. return err
  199. }
  200. return nil
  201. }
  202. func (i *indexedIterator) SetErrorCallback(f func(err error)) {
  203. i.errf = f
  204. }
  205. // NewIndexedIterator returns an 'indexed iterator'. An index is iterator
  206. // that returns another iterator, a 'data iterator'. A 'data iterator' is the
  207. // iterator that contains actual key/value pairs.
  208. //
  209. // If strict is true the any 'corruption errors' (i.e errors.IsCorrupted(err) == true)
  210. // won't be ignored and will halt 'indexed iterator', otherwise the iterator will
  211. // continue to the next 'data iterator'. Corruption on 'index iterator' will not be
  212. // ignored and will halt the iterator.
  213. func NewIndexedIterator(index IteratorIndexer, strict bool) Iterator {
  214. return &indexedIterator{index: index, strict: strict}
  215. }