iterator.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package graph
  11. import (
  12. "devt.de/krotik/eliasdb/graph/util"
  13. "devt.de/krotik/eliasdb/hash"
  14. )
  15. /*
  16. NodeKeyIterator can be used to iterate node keys of a certain node kind.
  17. */
  18. type NodeKeyIterator struct {
  19. gm *Manager // GraphManager which created the iterator
  20. it *hash.HTreeIterator // Internal HTree iterator
  21. LastError error // Last encountered error
  22. }
  23. /*
  24. Next returns the next node key. Sets the LastError attribute if an error occurs.
  25. */
  26. func (it *NodeKeyIterator) Next() string {
  27. // Take reader lock
  28. it.gm.mutex.RLock()
  29. defer it.gm.mutex.RUnlock()
  30. k, _ := it.it.Next()
  31. if it.it.LastError != nil {
  32. it.LastError = &util.GraphError{Type: util.ErrReading, Detail: it.it.LastError.Error()}
  33. return ""
  34. } else if len(k) == 0 {
  35. return ""
  36. }
  37. return string(k[len(PrefixNSAttrs):])
  38. }
  39. /*
  40. HasNext returns if there is a next node key.
  41. */
  42. func (it *NodeKeyIterator) HasNext() bool {
  43. return it.it.HasNext()
  44. }
  45. /*
  46. Error returns the last encountered error.
  47. */
  48. func (it *NodeKeyIterator) Error() error {
  49. return it.LastError
  50. }