/Users/deen/code/yugabyte-db/src/yb/docdb/doc_write_batch_cache.h
Line | Count | Source (jump to first uncovered line) |
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_DOC_WRITE_BATCH_CACHE_H_ |
15 | | #define YB_DOCDB_DOC_WRITE_BATCH_CACHE_H_ |
16 | | |
17 | | #include <unordered_map> |
18 | | #include <string> |
19 | | |
20 | | #include <boost/optional.hpp> |
21 | | |
22 | | #include "yb/common/hybrid_time.h" |
23 | | #include "yb/docdb/key_bytes.h" |
24 | | #include "yb/docdb/value_type.h" |
25 | | #include "yb/docdb/value.h" |
26 | | |
27 | | namespace yb { |
28 | | namespace docdb { |
29 | | |
30 | | // A utility used by DocWriteBatch. Caches generation hybrid_times (hybrid_times of full overwrite |
31 | | // or deletion) for key prefixes that were read from RocksDB or created by previous operations |
32 | | // performed on the DocWriteBatch. |
33 | | // |
34 | | // This class is not thread-safe. |
35 | | class DocWriteBatchCache { |
36 | | public: |
37 | | struct Entry { |
38 | | DocHybridTime doc_hybrid_time; |
39 | | ValueType value_type = ValueType::kInvalid; |
40 | | UserTimeMicros user_timestamp = Value::kInvalidUserTimestamp; |
41 | | // We found a key which matched the exact key_prefix_ we were searching for (excluding the |
42 | | // hybrid time). Since we search for a key prefix, we could search for a.b.c, but end up |
43 | | // finding a key like a.b.c.d.e. This field indicates that we searched for something like a.b.c |
44 | | // and found a key for a.b.c. |
45 | | bool found_exact_key_prefix = false; |
46 | | |
47 | 0 | bool operator<(const Entry& other) const { |
48 | 0 | return (doc_hybrid_time < other.doc_hybrid_time); |
49 | 0 | } |
50 | | }; |
51 | | |
52 | | // Records the generation hybrid_time corresponding to the given encoded key prefix, which is |
53 | | // assumed not to include the hybrid_time at the end. |
54 | | void Put(const KeyBytes& key_bytes, const Entry& entry); |
55 | | |
56 | | // Same thing, but doesn't use an already created entry. |
57 | | void Put(const KeyBytes& key_bytes, |
58 | | DocHybridTime gen_ht, |
59 | | ValueType value_type, |
60 | | UserTimeMicros user_timestamp = Value::kInvalidUserTimestamp, |
61 | 21.3M | bool found_exact_key_prefix = true) { |
62 | 21.3M | Put(key_bytes, {gen_ht, value_type, user_timestamp, found_exact_key_prefix}); |
63 | 21.3M | } |
64 | | |
65 | | // Returns the latest generation hybrid_time for the document/subdocument identified by the given |
66 | | // encoded key prefix. |
67 | | // TODO: switch to taking a slice as an input to avoid making a copy on lookup. |
68 | | boost::optional<Entry> Get(const KeyBytes& encoded_key_prefix); |
69 | | |
70 | | std::string ToDebugString(); |
71 | | |
72 | | static std::string EntryToStr(const Entry& entry); |
73 | | |
74 | | void Clear(); |
75 | | |
76 | | private: |
77 | | std::unordered_map<KeyBuffer, Entry, ByteBufferHash> prefix_to_gen_ht_; |
78 | | }; |
79 | | |
80 | | |
81 | | } // namespace docdb |
82 | | } // namespace yb |
83 | | |
84 | | #endif // YB_DOCDB_DOC_WRITE_BATCH_CACHE_H_ |