1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
14 #include "db/column_family.h"
15 #include "rocksdb/db.h"
16 #include "rocksdb/iterator.h"
17 #include "rocksdb/options.h"
18 #include "util/arena.h"
24 class ColumnFamilyData
;
27 * ManagedIterator is a special type of iterator that supports freeing the
28 * underlying iterator and still being able to access the current key/value
29 * pair. This is done by copying the key/value pair so that clients can
30 * continue to access the data without getting a SIGSEGV.
31 * The underlying iterator can be freed manually through the call to
32 * ReleaseIter or automatically (as needed on space pressure or age.)
33 * The iterator is recreated using the saved original arguments.
35 class ManagedIterator
: public Iterator
{
37 ManagedIterator(DBImpl
* db
, const ReadOptions
& read_options
,
38 ColumnFamilyData
* cfd
);
39 virtual ~ManagedIterator();
41 virtual void SeekToLast() override
;
42 virtual void Prev() override
;
43 virtual bool Valid() const override
;
44 void SeekToFirst() override
;
45 virtual void Seek(const Slice
& target
) override
;
46 virtual void SeekForPrev(const Slice
& target
) override
;
47 virtual void Next() override
;
48 virtual Slice
key() const override
;
49 virtual Slice
value() const override
;
50 virtual Status
status() const override
;
51 void ReleaseIter(bool only_old
);
52 void SetDropOld(bool only_old
) {
53 only_drop_old_
= read_options_
.tailing
|| only_old
;
57 void RebuildIterator();
59 void SeekInternal(const Slice
& user_key
, bool seek_to_first
);
65 ReadOptions read_options_
;
66 ColumnFamilyData
* const cfd_
;
67 ColumnFamilyHandleInternal cfh_
;
70 std::unique_ptr
<Iterator
> mutable_iter_
;
71 // internal iterator status
76 IterKey cached_value_
;
78 bool only_drop_old_
= true;
79 bool snapshot_created_
;
80 bool release_supported_
;
81 std::mutex in_use_
; // is managed iterator in use
84 } // namespace rocksdb
85 #endif // !ROCKSDB_LITE