/Users/deen/code/yugabyte-db/src/yb/docdb/subdoc_reader.h
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #ifndef YB_DOCDB_SUBDOC_READER_H_ |
15 | | #define YB_DOCDB_SUBDOC_READER_H_ |
16 | | |
17 | | #include <string> |
18 | | #include <vector> |
19 | | |
20 | | #include "yb/common/doc_hybrid_time.h" |
21 | | #include "yb/common/read_hybrid_time.h" |
22 | | #include "yb/common/transaction.h" |
23 | | |
24 | | #include "yb/docdb/docdb_fwd.h" |
25 | | #include "yb/docdb/expiration.h" |
26 | | #include "yb/docdb/value.h" |
27 | | |
28 | | #include "yb/gutil/macros.h" |
29 | | |
30 | | #include "yb/util/status_fwd.h" |
31 | | #include "yb/util/monotime.h" |
32 | | #include "yb/util/strongly_typed_bool.h" |
33 | | |
34 | | namespace yb { |
35 | | namespace docdb { |
36 | | |
37 | | |
38 | | // This class is responsible for housing state used to determine whether a row is still valid based |
39 | | // on it's write time. It can be constructed in one of two ways: |
40 | | // (1) Using just a write_time_watermark -- this is useful if some parent row was written at a given |
41 | | // time and all children written before that time should be considered overwritten. |
42 | | // (2) Using additionally a read_time and expiration -- this is useful if the data we're reading has |
43 | | // TTL enabled, e.g. CQL data. |
44 | | class ObsolescenceTracker { |
45 | | public: |
46 | 28.5M | ObsolescenceTracker() = default; |
47 | | explicit ObsolescenceTracker(DocHybridTime write_time_watermark); |
48 | | ObsolescenceTracker( |
49 | | const ReadHybridTime& read_time, DocHybridTime write_time_watermark, Expiration expiration); |
50 | | |
51 | | bool IsObsolete(const DocHybridTime& write_time) const; |
52 | | |
53 | | // Create an instance of ObsolescenceTracker derived from this instance. This "child" instance |
54 | | // will incorporate the parents data with the new data, but notably the child's TTL will be |
55 | | // ignored if the parent did not have a TTL. The assumption here is that an ObsolescenceTracker |
56 | | // constructed without an expiration is constructed in the context of a database in which TTL's |
57 | | // are not a valid concept (e.g. in SQL), so the same will be true of any subsequent children. |
58 | | ObsolescenceTracker Child(const DocHybridTime& write_time, const MonoDelta& ttl) const; |
59 | | |
60 | | // A version of the above method which only updates the tracked high write_time_watermark_. |
61 | | ObsolescenceTracker Child(const DocHybridTime& write_time) const; |
62 | | |
63 | | const DocHybridTime& GetHighWriteTime(); |
64 | | |
65 | | // Performs the computation required to set the TTL on a row before constructing a SubDocument |
66 | | // from it. Caller should provide the write time of the row whose TTL is tracked by this instance. |
67 | | // Returns boost::none if this instance is not tracking TTL. |
68 | | boost::optional<uint64_t> GetTtlRemainingSeconds(const HybridTime& ttl_write_time) const; |
69 | | |
70 | | protected: |
71 | | DocHybridTime write_time_watermark_ = DocHybridTime::kMin; |
72 | | boost::optional<ReadHybridTime> read_time_; |
73 | | boost::optional<Expiration> expiration_; |
74 | | }; |
75 | | |
76 | | |
77 | | // This class orchestrates the creation of a SubDocument stored in RocksDB with key |
78 | | // target_subdocument_key, respecting the expiration and high write time passed to it on |
79 | | // construction. |
80 | | class SubDocumentReader { |
81 | | public: |
82 | | SubDocumentReader( |
83 | | const KeyBytes& target_subdocument_key, |
84 | | IntentAwareIterator* iter, |
85 | | DeadlineInfo* deadline_info, |
86 | | const ObsolescenceTracker& ancestor_obsolescence_tracker); |
87 | | |
88 | | // Populate the provided SubDocument* with the data for the provided target_subdocument_key. This |
89 | | // method assumes the provided IntentAwareIterator is pointing to the beginning of the range which |
90 | | // represents target_subdocument_key. If no such data is found at the current position, no data |
91 | | // will be populated on the provided SubDocument*. |
92 | | CHECKED_STATUS Get(SubDocument* result); |
93 | | |
94 | | private: |
95 | | const KeyBytes& target_subdocument_key_; |
96 | | IntentAwareIterator* const iter_; |
97 | | DeadlineInfo* const deadline_info_; |
98 | | // Tracks the combined obsolescence info of not only this SubDocument's direct parent but all |
99 | | // ancestors of the SubDocument. |
100 | | ObsolescenceTracker ancestor_obsolescence_tracker_; |
101 | | }; |
102 | | |
103 | | // This class is responsible for initializing TTL and overwrite metadata based on parent rows, and |
104 | | // then initializing and returning SubDocumentReader's which will produce SubDocument instances. |
105 | | class SubDocumentReaderBuilder { |
106 | | public: |
107 | | SubDocumentReaderBuilder(IntentAwareIterator* iter, DeadlineInfo* deadline_info); |
108 | | |
109 | | // Updates expiration/overwrite data by scanning all parents of this Builder's |
110 | | // target_subdocument_key. |
111 | | CHECKED_STATUS InitObsolescenceInfo( |
112 | | const ObsolescenceTracker& table_obsolescence_tracker, |
113 | | const Slice& root_doc_key, const Slice& target_subdocument_key); |
114 | | |
115 | | // Does NOT seek. This method assumes the caller has seeked iter to the key corresponding to |
116 | | // projection. It will return a SubDocumentReader which will read the key currently pointed to, |
117 | | // assuming the iterator is seeked to the key corresponding to sub_doc_key. Use of this method |
118 | | // without explicit seeking to sub_doc_key by the caller is not supported. |
119 | | Result<std::unique_ptr<SubDocumentReader>> Build(const KeyBytes& sub_doc_key); |
120 | | |
121 | | private: |
122 | | CHECKED_STATUS UpdateWithParentWriteInfo(const Slice& parent_key_without_ht); |
123 | | |
124 | | IntentAwareIterator* iter_; |
125 | | DeadlineInfo* deadline_info_; |
126 | | ObsolescenceTracker parent_obsolescence_tracker_; |
127 | | }; |
128 | | |
129 | | } // namespace docdb |
130 | | } // namespace yb |
131 | | |
132 | | #endif // YB_DOCDB_SUBDOC_READER_H_ |