/Users/deen/code/yugabyte-db/src/yb/docdb/doc_path.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_PATH_H_ |
15 | | #define YB_DOCDB_DOC_PATH_H_ |
16 | | |
17 | | #include <stdint.h> |
18 | | |
19 | | #include <cstring> |
20 | | #include <functional> |
21 | | #include <ostream> |
22 | | #include <string> |
23 | | #include <type_traits> |
24 | | #include <unordered_set> |
25 | | #include <utility> |
26 | | #include <vector> |
27 | | |
28 | | #include "yb/docdb/docdb_fwd.h" |
29 | | #include "yb/docdb/key_bytes.h" |
30 | | |
31 | | #include "yb/gutil/integral_types.h" |
32 | | #include "yb/gutil/macros.h" |
33 | | #include "yb/gutil/strings/substitute.h" |
34 | | |
35 | | #include "yb/util/string_util.h" |
36 | | |
37 | | namespace yb { |
38 | | namespace docdb { |
39 | | |
40 | | // Identifies a particular subdocument inside the logical representation of the document database. |
41 | | // By "logical representation" we mean that we are not concerned with the exact keys used in the |
42 | | // underlying key-value store. This is very similar to a SubDocKey without a hybrid time, and can |
43 | | // probably be merged with it. |
44 | | |
45 | | class DocPath { |
46 | | public: |
47 | | template<class... T> |
48 | 126k | DocPath(const KeyBytes& encoded_doc_key, T... subkeys) { |
49 | 126k | encoded_doc_key_ = encoded_doc_key; |
50 | 126k | AppendPrimitiveValues(&subkeys_, subkeys...); |
51 | 126k | } yb::docdb::DocPath::DocPath<>(yb::docdb::KeyBytes const&) Line | Count | Source | 48 | 125k | DocPath(const KeyBytes& encoded_doc_key, T... subkeys) { | 49 | 125k | encoded_doc_key_ = encoded_doc_key; | 50 | 125k | AppendPrimitiveValues(&subkeys_, subkeys...); | 51 | 125k | } |
yb::docdb::DocPath::DocPath<yb::docdb::PrimitiveValue>(yb::docdb::KeyBytes const&, yb::docdb::PrimitiveValue) Line | Count | Source | 48 | 586 | DocPath(const KeyBytes& encoded_doc_key, T... subkeys) { | 49 | 586 | encoded_doc_key_ = encoded_doc_key; | 50 | 586 | AppendPrimitiveValues(&subkeys_, subkeys...); | 51 | 586 | } |
|
52 | | |
53 | | template<class... T> |
54 | | DocPath(const Slice& encoded_doc_key, T... subkeys) |
55 | 70.7M | : encoded_doc_key_(encoded_doc_key) { |
56 | 70.7M | AppendPrimitiveValues(&subkeys_, subkeys...); |
57 | 70.7M | } yb::docdb::DocPath::DocPath<yb::docdb::PrimitiveValue>(yb::Slice const&, yb::docdb::PrimitiveValue) Line | Count | Source | 55 | 69.8M | : encoded_doc_key_(encoded_doc_key) { | 56 | 69.8M | AppendPrimitiveValues(&subkeys_, subkeys...); | 57 | 69.8M | } |
yb::docdb::DocPath::DocPath<>(yb::Slice const&) Line | Count | Source | 55 | 913k | : encoded_doc_key_(encoded_doc_key) { | 56 | 913k | AppendPrimitiveValues(&subkeys_, subkeys...); | 57 | 913k | } |
|
58 | | |
59 | | DocPath(const KeyBytes& encoded_doc_key, const vector<PrimitiveValue>& subkeys) |
60 | | : encoded_doc_key_(encoded_doc_key), |
61 | 628k | subkeys_(subkeys) { |
62 | 628k | } |
63 | | |
64 | 72.1M | const KeyBytes& encoded_doc_key() const { return encoded_doc_key_; } |
65 | | |
66 | 143M | size_t num_subkeys() const { return subkeys_.size(); } |
67 | | |
68 | 71.2M | const PrimitiveValue& subkey(size_t i) const { |
69 | 71.2M | assert(i < num_subkeys()); |
70 | 0 | return subkeys_[i]; |
71 | 71.2M | } |
72 | | |
73 | | std::string ToString() const; |
74 | | |
75 | | void AddSubKey(const PrimitiveValue& subkey); |
76 | | |
77 | | void AddSubKey(PrimitiveValue&& subkey); |
78 | | |
79 | 185k | const PrimitiveValue& last_subkey() const { |
80 | 185k | assert(!subkeys_.empty()); |
81 | 0 | return subkeys_.back(); |
82 | 185k | } |
83 | | |
84 | | // Note: the hash is supposed to be uint16_t, but protobuf only supports uint32. |
85 | | // So this function takes in uint32_t. |
86 | | // TODO (akashnil): Add uint16 data type in docdb. |
87 | | static DocPath DocPathFromRedisKey(uint16_t hash, const string& key, const string& subkey = ""); |
88 | | |
89 | 27 | const std::vector<PrimitiveValue>& subkeys() const { |
90 | 27 | return subkeys_; |
91 | 27 | } |
92 | | |
93 | | private: |
94 | | // Encoded key identifying the document. This key can itself contain multiple components |
95 | | // (hash bucket, hashed components, range components). |
96 | | // TODO(mikhail): should this really be encoded? |
97 | | KeyBytes encoded_doc_key_; |
98 | | |
99 | | std::vector<PrimitiveValue> subkeys_; |
100 | | }; |
101 | | |
102 | 0 | inline std::ostream& operator << (std::ostream& out, const DocPath& doc_path) { |
103 | 0 | return out << doc_path.ToString(); |
104 | 0 | } |
105 | | |
106 | | } // namespace docdb |
107 | | } // namespace yb |
108 | | |
109 | | #endif // YB_DOCDB_DOC_PATH_H_ |