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.

100 rader
3.4 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. //
  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/slice.h"
  17. #include "leveldb/status.h"
  18. namespace leveldb {
  19. class Iterator {
  20. public:
  21. Iterator();
  22. virtual ~Iterator();
  23. // An iterator is either positioned at a key/value pair, or
  24. // not valid. This method returns true iff the iterator is valid.
  25. virtual bool Valid() const = 0;
  26. // Position at the first key in the source. The iterator is Valid()
  27. // after this call iff the source is not empty.
  28. virtual void SeekToFirst() = 0;
  29. // Position at the last key in the source. The iterator is
  30. // Valid() after this call iff the source is not empty.
  31. virtual void SeekToLast() = 0;
  32. // Position at the first key in the source that at or past target
  33. // The iterator is Valid() after this call iff the source contains
  34. // an entry that comes at or past target.
  35. virtual void Seek(const Slice& target) = 0;
  36. // Moves to the next entry in the source. After this call, Valid() is
  37. // true iff the iterator was not positioned at the last entry in the source.
  38. // REQUIRES: Valid()
  39. virtual void Next() = 0;
  40. // Moves to the previous entry in the source. After this call, Valid() is
  41. // true iff the iterator was not positioned at the first entry in source.
  42. // REQUIRES: Valid()
  43. virtual void Prev() = 0;
  44. // Return the key 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: Valid()
  48. virtual Slice key() const = 0;
  49. // Return the value for the current entry. The underlying storage for
  50. // the returned slice is valid only until the next modification of
  51. // the iterator.
  52. // REQUIRES: !AtEnd() && !AtStart()
  53. virtual Slice value() const = 0;
  54. // If an error has occurred, return it. Else return an ok status.
  55. virtual Status status() const = 0;
  56. // Clients are allowed to register function/arg1/arg2 triples that
  57. // will be invoked when this iterator is destroyed.
  58. //
  59. // Note that unlike all of the preceding methods, this method is
  60. // not abstract and therefore clients should not override it.
  61. typedef void (*CleanupFunction)(void* arg1, void* arg2);
  62. void RegisterCleanup(CleanupFunction function, void* arg1, void* arg2);
  63. private:
  64. struct Cleanup {
  65. CleanupFunction function;
  66. void* arg1;
  67. void* arg2;
  68. Cleanup* next;
  69. };
  70. Cleanup cleanup_;
  71. // No copying allowed
  72. Iterator(const Iterator&);
  73. void operator=(const Iterator&);
  74. };
  75. // Return an empty iterator (yields nothing).
  76. extern Iterator* NewEmptyIterator();
  77. // Return an empty iterator with the specified status.
  78. extern Iterator* NewErrorIterator(const Status& status);
  79. } // namespace leveldb
  80. #endif // STORAGE_LEVELDB_INCLUDE_ITERATOR_H_