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.

95 lines
3.2 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. //
  5. // An iterator yields a sequence of key/value pairs from a source.
  6. // The following class defines the interface. Multiple implementations
  7. // are provided by this library. In particular, iterators are provided
  8. // to access the contents of a Table or a DB.
  9. #ifndef STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
  10. #define STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
  11. #include "leveldb/slice.h"
  12. #include "leveldb/status.h"
  13. namespace leveldb {
  14. class Iterator {
  15. public:
  16. Iterator();
  17. virtual ~Iterator();
  18. // An iterator is either positioned at a key/value pair, or
  19. // not valid. This method returns true iff the iterator is valid.
  20. virtual bool Valid() const = 0;
  21. // Position at the first key in the source. The iterator is Valid()
  22. // after this call iff the source is not empty.
  23. virtual void SeekToFirst() = 0;
  24. // Position at the last key in the source. The iterator is
  25. // Valid() after this call iff the source is not empty.
  26. virtual void SeekToLast() = 0;
  27. // Position at the first key in the source that at or past target
  28. // The iterator is Valid() after this call iff the source contains
  29. // an entry that comes at or past target.
  30. virtual void Seek(const Slice& target) = 0;
  31. // Moves to the next entry in the source. After this call, Valid() is
  32. // true iff the iterator was not positioned at the last entry in the source.
  33. // REQUIRES: Valid()
  34. virtual void Next() = 0;
  35. // Moves to the previous entry in the source. After this call, Valid() is
  36. // true iff the iterator was not positioned at the first entry in source.
  37. // REQUIRES: Valid()
  38. virtual void Prev() = 0;
  39. // Return the key for the current entry. The underlying storage for
  40. // the returned slice is valid only until the next modification of
  41. // the iterator.
  42. // REQUIRES: Valid()
  43. virtual Slice key() const = 0;
  44. // Return the value for the current entry. The underlying storage for
  45. // the returned slice is valid only until the next modification of
  46. // the iterator.
  47. // REQUIRES: !AtEnd() && !AtStart()
  48. virtual Slice value() const = 0;
  49. // If an error has occurred, return it. Else return an ok status.
  50. virtual Status status() const = 0;
  51. // Clients are allowed to register function/arg1/arg2 triples that
  52. // will be invoked when this iterator is destroyed.
  53. //
  54. // Note that unlike all of the preceding methods, this method is
  55. // not abstract and therefore clients should not override it.
  56. typedef void (*CleanupFunction)(void* arg1, void* arg2);
  57. void RegisterCleanup(CleanupFunction function, void* arg1, void* arg2);
  58. private:
  59. struct Cleanup {
  60. CleanupFunction function;
  61. void* arg1;
  62. void* arg2;
  63. Cleanup* next;
  64. };
  65. Cleanup cleanup_;
  66. // No copying allowed
  67. Iterator(const Iterator&);
  68. void operator=(const Iterator&);
  69. };
  70. // Return an empty iterator (yields nothing).
  71. extern Iterator* NewEmptyIterator();
  72. // Return an empty iterator with the specified status.
  73. extern Iterator* NewErrorIterator(const Status& status);
  74. }
  75. #endif // STORAGE_LEVELDB_INCLUDE_ITERATOR_H_