/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/compaction_iterator.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Use of this source code is governed by a BSD-style license that can be |
2 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
3 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
4 | | // This source code is licensed under the BSD-style license found in the |
5 | | // LICENSE file in the root directory of this source tree. An additional grant |
6 | | // of patent rights can be found in the PATENTS file in the same directory. |
7 | | // |
8 | | // The following only applies to changes made to this file as part of YugaByte development. |
9 | | // |
10 | | // Portions Copyright (c) YugaByte, Inc. |
11 | | // |
12 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
13 | | // in compliance with the License. You may obtain a copy of the License at |
14 | | // |
15 | | // http://www.apache.org/licenses/LICENSE-2.0 |
16 | | // |
17 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
18 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
19 | | // or implied. See the License for the specific language governing permissions and limitations |
20 | | // under the License. |
21 | | // |
22 | | #ifndef YB_ROCKSDB_DB_COMPACTION_ITERATOR_H |
23 | | #define YB_ROCKSDB_DB_COMPACTION_ITERATOR_H |
24 | | |
25 | | #pragma once |
26 | | |
27 | | #include <algorithm> |
28 | | #include <deque> |
29 | | #include <string> |
30 | | #include <vector> |
31 | | |
32 | | #include "yb/rocksdb/db/compaction.h" |
33 | | #include "yb/rocksdb/db/merge_helper.h" |
34 | | #include "yb/rocksdb/compaction_filter.h" |
35 | | #include "yb/rocksdb/util/log_buffer.h" |
36 | | |
37 | | namespace rocksdb { |
38 | | |
39 | | struct CompactionIteratorStats { |
40 | | // Compaction statistics |
41 | | int64_t num_record_drop_user = 0; |
42 | | int64_t num_record_drop_hidden = 0; |
43 | | int64_t num_record_drop_obsolete = 0; |
44 | | |
45 | | // Input statistics |
46 | | // TODO(noetzli): The stats are incomplete. They are lacking everything |
47 | | // consumed by MergeHelper. |
48 | | uint64_t num_input_records = 0; |
49 | | uint64_t num_input_deletion_records = 0; |
50 | | uint64_t num_input_corrupt_records = 0; |
51 | | uint64_t total_input_raw_key_bytes = 0; |
52 | | uint64_t total_input_raw_value_bytes = 0; |
53 | | }; |
54 | | |
55 | | class CompactionIterator { |
56 | | public: |
57 | | CompactionIterator(InternalIterator* input, const Comparator* cmp, |
58 | | MergeHelper* merge_helper, SequenceNumber last_sequence, |
59 | | std::vector<SequenceNumber>* snapshots, |
60 | | SequenceNumber earliest_write_conflict_snapshot, |
61 | | bool expect_valid_internal_key, |
62 | | Compaction* compaction = nullptr, |
63 | | CompactionFilter* compaction_filter = nullptr, |
64 | | LogBuffer* log_buffer = nullptr); |
65 | | |
66 | | void ResetRecordCounts(); |
67 | | |
68 | | // Seek to the beginning of the compaction iterator output. |
69 | | // |
70 | | // REQUIRED: Call only once. |
71 | | void SeekToFirst(); |
72 | | |
73 | | // Produces the next record in the compaction. |
74 | | // |
75 | | // REQUIRED: SeekToFirst() has been called. |
76 | | void Next(); |
77 | | |
78 | | // Getters |
79 | 158M | const Slice& key() const { return key_; } |
80 | 158M | const Slice& value() const { return value_; } |
81 | 31.1k | const Status& status() const { return status_; } |
82 | 0 | const ParsedInternalKey& ikey() const { return ikey_; } |
83 | 158M | bool Valid() const { return valid_; } |
84 | 157k | const Slice& user_key() const { return current_user_key_; } |
85 | 11.4k | const CompactionIteratorStats& iter_stats() const { return iter_stats_; } |
86 | | |
87 | | private: |
88 | | // Processes the input stream to find the next output |
89 | | void NextFromInput(); |
90 | | |
91 | | // Do last preparations before presenting the output to the callee. At this |
92 | | // point this only zeroes out the sequence number if possible for better |
93 | | // compression. |
94 | | void PrepareOutput(); |
95 | | |
96 | | // Given a sequence number, return the sequence number of the |
97 | | // earliest snapshot that this sequence number is visible in. |
98 | | // The snapshots themselves are arranged in ascending order of |
99 | | // sequence numbers. |
100 | | // Employ a sequential search because the total number of |
101 | | // snapshots are typically small. |
102 | | inline SequenceNumber FindEarliestVisibleSnapshot( |
103 | | SequenceNumber in, SequenceNumber* prev_snapshot); |
104 | | |
105 | | InternalIterator* input_; |
106 | | const Comparator* cmp_; |
107 | | MergeHelper* merge_helper_; |
108 | | const std::vector<SequenceNumber>* snapshots_; |
109 | | const SequenceNumber earliest_write_conflict_snapshot_; |
110 | | bool expect_valid_internal_key_; |
111 | | Compaction* compaction_; |
112 | | CompactionFilter* compaction_filter_; |
113 | | LogBuffer* log_buffer_; |
114 | | bool bottommost_level_; |
115 | | bool valid_ = false; |
116 | | SequenceNumber visible_at_tip_; |
117 | | SequenceNumber earliest_snapshot_; |
118 | | SequenceNumber latest_snapshot_; |
119 | | bool ignore_snapshots_; |
120 | | |
121 | | // State |
122 | | // |
123 | | // Points to a copy of the current compaction iterator output (current_key_) |
124 | | // if valid_. |
125 | | Slice key_; |
126 | | // Points to the value in the underlying iterator that corresponds to the |
127 | | // current output. |
128 | | Slice value_; |
129 | | // The status is OK unless compaction iterator encounters a merge operand |
130 | | // while not having a merge operator defined. |
131 | | Status status_; |
132 | | // Stores the user key, sequence number and type of the current compaction |
133 | | // iterator output (or current key in the underlying iterator during |
134 | | // NextFromInput()). |
135 | | ParsedInternalKey ikey_; |
136 | | // Stores whether ikey_.user_key is valid. If set to false, the user key is |
137 | | // not compared against the current key in the underlying iterator. |
138 | | bool has_current_user_key_ = false; |
139 | | bool at_next_ = false; // If false, the iterator |
140 | | // Holds a copy of the current compaction iterator output (or current key in |
141 | | // the underlying iterator during NextFromInput()). |
142 | | IterKey current_key_; |
143 | | Slice current_user_key_; |
144 | | SequenceNumber current_user_key_sequence_; |
145 | | SequenceNumber current_user_key_snapshot_; |
146 | | |
147 | | // True if the iterator has already returned a record for the current key. |
148 | | bool has_outputted_key_ = false; |
149 | | |
150 | | // truncated the value of the next key and output it without applying any |
151 | | // compaction rules. This is used for outputting a put after a single delete. |
152 | | bool clear_and_output_next_key_ = false; |
153 | | |
154 | | MergeOutputIterator merge_out_iter_; |
155 | | std::string compaction_filter_value_; |
156 | | // "level_ptrs" holds indices that remember which file of an associated |
157 | | // level we were last checking during the last call to compaction-> |
158 | | // KeyNotExistsBeyondOutputLevel(). This allows future calls to the function |
159 | | // to pick off where it left off since each subcompaction's key range is |
160 | | // increasing so a later call to the function must be looking for a key that |
161 | | // is in or beyond the last file checked during the previous call |
162 | | std::vector<size_t> level_ptrs_; |
163 | | CompactionIteratorStats iter_stats_; |
164 | | |
165 | | // Stores the disjoint live ranges of this tablet in user keyspace. Ranges at the back are |
166 | | // lexicographically first. Ranges are popped off the back of the stack as our iteration passes |
167 | | // them. |
168 | | std::vector<std::pair<Slice, Slice>> live_key_ranges_stack_; |
169 | | }; |
170 | | } // namespace rocksdb |
171 | | |
172 | | #endif // YB_ROCKSDB_DB_COMPACTION_ITERATOR_H |