Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

112 rader
3.9 KiB

3 veckor sedan
  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. //
  10. // Multiple threads can invoke const methods on an Iterator without
  11. // external synchronization, but if any of the threads may call a
  12. // non-const method, all threads accessing the same Iterator must use
  13. // external synchronization.
  14. #ifndef STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
  15. #define STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
  16. #include "leveldb/export.h"
  17. #include "leveldb/slice.h"
  18. #include "leveldb/status.h"
  19. namespace leveldb {
  20. class LEVELDB_EXPORT Iterator {
  21. public:
  22. Iterator();
  23. Iterator(const Iterator&) = delete;
  24. Iterator& operator=(const Iterator&) = delete;
  25. virtual ~Iterator();
  26. // An iterator is either positioned at a key/value pair, or
  27. // not valid. This method returns true iff the iterator is valid.
  28. virtual bool Valid() const = 0;
  29. // Position at the first key in the source. The iterator is Valid()
  30. // after this call iff the source is not empty.
  31. virtual void SeekToFirst() = 0;
  32. // Position at the last key in the source. The iterator is
  33. // Valid() after this call iff the source is not empty.
  34. virtual void SeekToLast() = 0;
  35. // Position at the first key in the source that is at or past target.
  36. // The iterator is Valid() after this call iff the source contains
  37. // an entry that comes at or past target.
  38. virtual void Seek(const Slice& target) = 0;
  39. // Moves to the next entry in the source. After this call, Valid() is
  40. // true iff the iterator was not positioned at the last entry in the source.
  41. // REQUIRES: Valid()
  42. virtual void Next() = 0;
  43. // Moves to the previous entry in the source. After this call, Valid() is
  44. // true iff the iterator was not positioned at the first entry in source.
  45. // REQUIRES: Valid()
  46. virtual void Prev() = 0;
  47. // Return the key for the current entry. The underlying storage for
  48. // the returned slice is valid only until the next modification of
  49. // the iterator.
  50. // REQUIRES: Valid()
  51. virtual Slice key() const = 0;
  52. // Return the value for the current entry. The underlying storage for
  53. // the returned slice is valid only until the next modification of
  54. // the iterator.
  55. // REQUIRES: Valid()
  56. virtual Slice value() const = 0;
  57. // If an error has occurred, return it. Else return an ok status.
  58. virtual Status status() const = 0;
  59. // Clients are allowed to register function/arg1/arg2 triples that
  60. // will be invoked when this iterator is destroyed.
  61. //
  62. // Note that unlike all of the preceding methods, this method is
  63. // not abstract and therefore clients should not override it.
  64. using CleanupFunction = void (*)(void* arg1, void* arg2);
  65. void RegisterCleanup(CleanupFunction function, void* arg1, void* arg2);
  66. private:
  67. // Cleanup functions are stored in a single-linked list.
  68. // The list's head node is inlined in the iterator.
  69. struct CleanupNode {
  70. // True if the node is not used. Only head nodes might be unused.
  71. bool IsEmpty() const { return function == nullptr; }
  72. // Invokes the cleanup function.
  73. void Run() {
  74. assert(function != nullptr);
  75. (*function)(arg1, arg2);
  76. }
  77. // The head node is used if the function pointer is not null.
  78. CleanupFunction function;
  79. void* arg1;
  80. void* arg2;
  81. CleanupNode* next;
  82. };
  83. CleanupNode cleanup_head_;
  84. };
  85. // Return an empty iterator (yields nothing).
  86. LEVELDB_EXPORT Iterator* NewEmptyIterator();
  87. // Return an empty iterator with the specified status.
  88. LEVELDB_EXPORT Iterator* NewErrorIterator(const Status& status);
  89. } // namespace leveldb
  90. #endif // STORAGE_LEVELDB_INCLUDE_ITERATOR_H_