iter.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package context
  2. import (
  3. "bytes"
  4. "github.com/syndtr/goleveldb/leveldb/iterator"
  5. )
  6. // Iter allows iteration of the named routes in the store.
  7. type Iter struct {
  8. it iterator.Iterator
  9. name string
  10. rt *Route
  11. err error
  12. }
  13. func (i *Iter) decode() error {
  14. rt := &Route{}
  15. if err := rt.read(bytes.NewBuffer(i.it.Value())); err != nil {
  16. return err
  17. }
  18. i.name = string(i.it.Key())
  19. i.rt = rt
  20. return nil
  21. }
  22. // Valid indicates whether the current values of the iterator are valid.
  23. func (i *Iter) Valid() bool {
  24. return i.it.Valid() && i.err == nil
  25. }
  26. // Next advances the iterator to the next value.
  27. func (i *Iter) Next() bool {
  28. i.name = ""
  29. i.rt = nil
  30. if !i.it.Next() {
  31. return false
  32. }
  33. if err := i.decode(); err != nil {
  34. i.err = err
  35. return false
  36. }
  37. return true
  38. }
  39. // Seek ...
  40. func (i *Iter) Seek(cur []byte) bool {
  41. i.name = ""
  42. i.rt = nil
  43. v := i.it.Seek(cur)
  44. if !i.it.Valid() {
  45. return v
  46. }
  47. if err := i.decode(); err != nil {
  48. i.err = err
  49. }
  50. return v
  51. }
  52. // Error returns any active error that has stopped the iterator.
  53. func (i *Iter) Error() error {
  54. if err := i.it.Error(); err != nil {
  55. return err
  56. }
  57. return i.err
  58. }
  59. // Name is the name of the current route.
  60. func (i *Iter) Name() string {
  61. return i.name
  62. }
  63. // Route is the current route.
  64. func (i *Iter) Route() *Route {
  65. return i.rt
  66. }
  67. // Release disposes of the resources in the iterator.
  68. func (i *Iter) Release() {
  69. i.it.Release()
  70. }