]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | // This source code is licensed under both the GPLv2 (found in the | |
3 | // COPYING file in the root directory) and Apache 2.0 License | |
4 | // (found in the LICENSE.Apache file in the root directory). | |
5 | #pragma once | |
6 | ||
7 | #include <string> | |
8 | #include <vector> | |
9 | ||
10 | #include "db/dbformat.h" | |
11 | #include "rocksdb/iterator.h" | |
12 | #include "rocksdb/slice.h" | |
13 | #include "table/internal_iterator.h" | |
14 | ||
15 | namespace ROCKSDB_NAMESPACE { | |
16 | ||
17 | class HistoryTrimmingIterator : public InternalIterator { | |
18 | public: | |
19 | explicit HistoryTrimmingIterator(InternalIterator* input, | |
20 | const Comparator* cmp, const std::string& ts) | |
21 | : input_(input), filter_ts_(ts), cmp_(cmp) { | |
22 | assert(cmp_->timestamp_size() > 0 && !ts.empty()); | |
23 | } | |
24 | ||
25 | bool filter() const { | |
26 | if (!input_->Valid()) { | |
27 | return true; | |
28 | } | |
29 | Slice current_ts = ExtractTimestampFromKey(key(), cmp_->timestamp_size()); | |
30 | return cmp_->CompareTimestamp(current_ts, Slice(filter_ts_)) <= 0; | |
31 | } | |
32 | ||
33 | bool Valid() const override { return input_->Valid(); } | |
34 | ||
35 | void SeekToFirst() override { | |
36 | input_->SeekToFirst(); | |
37 | while (!filter()) { | |
38 | input_->Next(); | |
39 | } | |
40 | } | |
41 | ||
42 | void SeekToLast() override { | |
43 | input_->SeekToLast(); | |
44 | while (!filter()) { | |
45 | input_->Prev(); | |
46 | } | |
47 | } | |
48 | ||
49 | void Seek(const Slice& target) override { | |
50 | input_->Seek(target); | |
51 | while (!filter()) { | |
52 | input_->Next(); | |
53 | } | |
54 | } | |
55 | ||
56 | void SeekForPrev(const Slice& target) override { | |
57 | input_->SeekForPrev(target); | |
58 | while (!filter()) { | |
59 | input_->Prev(); | |
60 | } | |
61 | } | |
62 | ||
63 | void Next() override { | |
64 | do { | |
65 | input_->Next(); | |
66 | } while (!filter()); | |
67 | } | |
68 | ||
69 | void Prev() override { | |
70 | do { | |
71 | input_->Prev(); | |
72 | } while (!filter()); | |
73 | } | |
74 | ||
75 | Slice key() const override { return input_->key(); } | |
76 | ||
77 | Slice value() const override { return input_->value(); } | |
78 | ||
79 | Status status() const override { return input_->status(); } | |
80 | ||
81 | bool IsKeyPinned() const override { return input_->IsKeyPinned(); } | |
82 | ||
83 | bool IsValuePinned() const override { return input_->IsValuePinned(); } | |
84 | ||
85 | private: | |
86 | InternalIterator* input_; | |
87 | const std::string filter_ts_; | |
88 | const Comparator* const cmp_; | |
89 | }; | |
90 | ||
91 | } // namespace ROCKSDB_NAMESPACE |