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.
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.
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.
15 #ifndef STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
16 #define STORAGE_LEVELDB_INCLUDE_ITERATOR_H_
18 #include "leveldb/slice.h"
19 #include "leveldb/status.h"
28 // An iterator is either positioned at a key/value pair, or
29 // not valid. This method returns true iff the iterator is valid.
30 virtual bool Valid() const = 0;
32 // Position at the first key in the source. The iterator is Valid()
33 // after this call iff the source is not empty.
34 virtual void SeekToFirst() = 0;
36 // Position at the last key in the source. The iterator is
37 // Valid() after this call iff the source is not empty.
38 virtual void SeekToLast() = 0;
40 // Position at the first key in the source that at or past target
41 // The iterator is Valid() after this call iff the source contains
42 // an entry that comes at or past target.
43 virtual void Seek(const Slice& target) = 0;
45 // Moves to the next entry in the source. After this call, Valid() is
46 // true iff the iterator was not positioned at the last entry in the source.
48 virtual void Next() = 0;
50 // Moves to the previous entry in the source. After this call, Valid() is
51 // true iff the iterator was not positioned at the first entry in source.
53 virtual void Prev() = 0;
55 // Return the key for the current entry. The underlying storage for
56 // the returned slice is valid only until the next modification of
59 virtual Slice key() const = 0;
61 // Return the value for the current entry. The underlying storage for
62 // the returned slice is valid only until the next modification of
65 virtual Slice value() const = 0;
67 // If an error has occurred, return it. Else return an ok status.
68 virtual Status status() const = 0;
70 // Clients are allowed to register function/arg1/arg2 triples that
71 // will be invoked when this iterator is destroyed.
73 // Note that unlike all of the preceding methods, this method is
74 // not abstract and therefore clients should not override it.
75 typedef void (*CleanupFunction)(void* arg1, void* arg2);
76 void RegisterCleanup(CleanupFunction function, void* arg1, void* arg2);
80 CleanupFunction function;
88 Iterator(const Iterator&);
89 void operator=(const Iterator&);
92 // Return an empty iterator (yields nothing).
93 extern Iterator* NewEmptyIterator();
95 // Return an empty iterator with the specified status.
96 extern Iterator* NewErrorIterator(const Status& status);
98 } // namespace leveldb
100 #endif // STORAGE_LEVELDB_INCLUDE_ITERATOR_H_