/Users/deen/code/yugabyte-db/src/yb/common/partial_row.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | #ifndef YB_COMMON_PARTIAL_ROW_H |
33 | | #define YB_COMMON_PARTIAL_ROW_H |
34 | | |
35 | | #include <stdint.h> |
36 | | #include <string> |
37 | | #include <vector> |
38 | | |
39 | | #include "yb/gutil/macros.h" |
40 | | #include "yb/gutil/port.h" |
41 | | |
42 | | #include "yb/util/slice.h" |
43 | | #include "yb/util/status_fwd.h" |
44 | | |
45 | | namespace yb { |
46 | | class ColumnSchema; |
47 | | namespace client { |
48 | | template<typename KeyTypeWrapper> struct SliceKeysTestSetup; |
49 | | template<typename KeyTypeWrapper> struct IntKeysTestSetup; |
50 | | } // namespace client |
51 | | |
52 | | class Schema; |
53 | | |
54 | | // A row which may only contain values for a subset of the columns. |
55 | | // This type contains a normal contiguous row, plus a bitfield indicating |
56 | | // which columns have been set. Additionally, this type may optionally own |
57 | | // copies of indirect data for variable length columns. |
58 | | class YBPartialRow { |
59 | | public: |
60 | | // The given Schema object must remain valid for the lifetime of this |
61 | | // row. |
62 | | explicit YBPartialRow(const Schema* schema); |
63 | | virtual ~YBPartialRow(); |
64 | | |
65 | | YBPartialRow(const YBPartialRow& other); |
66 | | |
67 | | YBPartialRow& operator=(YBPartialRow other); |
68 | | |
69 | | //------------------------------------------------------------ |
70 | | // Setters |
71 | | //------------------------------------------------------------ |
72 | | |
73 | | CHECKED_STATUS SetBool(const Slice& col_name, bool val) WARN_UNUSED_RESULT; |
74 | | |
75 | | CHECKED_STATUS SetInt8(const Slice& col_name, int8_t val) WARN_UNUSED_RESULT; |
76 | | CHECKED_STATUS SetInt16(const Slice& col_name, int16_t val) WARN_UNUSED_RESULT; |
77 | | CHECKED_STATUS SetInt32(const Slice& col_name, int32_t val) WARN_UNUSED_RESULT; |
78 | | CHECKED_STATUS SetInt64(const Slice& col_name, int64_t val) WARN_UNUSED_RESULT; |
79 | | CHECKED_STATUS SetTimestamp(const Slice& col_name, |
80 | | int64_t micros_since_utc_epoch) WARN_UNUSED_RESULT; |
81 | | CHECKED_STATUS SetFloat(const Slice& col_name, float val) WARN_UNUSED_RESULT; |
82 | | CHECKED_STATUS SetDouble(const Slice& col_name, double val) WARN_UNUSED_RESULT; |
83 | | |
84 | | // Same as above setters, but with numeric column indexes. |
85 | | // These are faster since they avoid a hashmap lookup, so should |
86 | | // be preferred in performance-sensitive code (eg bulk loaders). |
87 | | CHECKED_STATUS SetBool(size_t col_idx, bool val) WARN_UNUSED_RESULT; |
88 | | |
89 | | CHECKED_STATUS SetInt8(size_t col_idx, int8_t val) WARN_UNUSED_RESULT; |
90 | | CHECKED_STATUS SetInt16(size_t col_idx, int16_t val) WARN_UNUSED_RESULT; |
91 | | CHECKED_STATUS SetInt32(size_t col_idx, int32_t val) WARN_UNUSED_RESULT; |
92 | | CHECKED_STATUS SetInt64(size_t col_idx, int64_t val) WARN_UNUSED_RESULT; |
93 | | CHECKED_STATUS SetTimestamp(size_t col_idx, int64_t micros_since_utc_epoch) WARN_UNUSED_RESULT; |
94 | | |
95 | | CHECKED_STATUS SetFloat(size_t col_idx, float val) WARN_UNUSED_RESULT; |
96 | | CHECKED_STATUS SetDouble(size_t col_idx, double val) WARN_UNUSED_RESULT; |
97 | | |
98 | | // Sets the string/binary value but does not copy the value. The slice |
99 | | // must remain valid until the call to AppendToPB(). |
100 | | CHECKED_STATUS SetString(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
101 | | CHECKED_STATUS SetString(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
102 | | CHECKED_STATUS SetBinary(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
103 | | CHECKED_STATUS SetBinary(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
104 | | CHECKED_STATUS SetInet(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
105 | | CHECKED_STATUS SetInet(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
106 | | CHECKED_STATUS SetJsonb(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
107 | | CHECKED_STATUS SetJsonb(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
108 | | CHECKED_STATUS SetDecimal(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
109 | | CHECKED_STATUS SetDecimal(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
110 | | CHECKED_STATUS SetUuid(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
111 | | CHECKED_STATUS SetUuid(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
112 | | CHECKED_STATUS SetTimeUuid(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
113 | | CHECKED_STATUS SetTimeUuid(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
114 | | CHECKED_STATUS SetFrozen(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
115 | | CHECKED_STATUS SetFrozen(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
116 | | |
117 | | // Copies 'val' immediately. |
118 | | CHECKED_STATUS SetStringCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
119 | | CHECKED_STATUS SetStringCopy(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
120 | | CHECKED_STATUS SetBinaryCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
121 | | CHECKED_STATUS SetBinaryCopy(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
122 | | CHECKED_STATUS SetUuidCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
123 | | CHECKED_STATUS SetUuidCopy(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
124 | | CHECKED_STATUS SetTimeUuidCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
125 | | CHECKED_STATUS SetTimeUuidCopy(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
126 | | CHECKED_STATUS SetDecimalCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
127 | | CHECKED_STATUS SetDecimalCopy(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
128 | | CHECKED_STATUS SetFrozenCopy(const Slice& col_name, const Slice& val) WARN_UNUSED_RESULT; |
129 | | CHECKED_STATUS SetFrozenCopy(size_t col_idx, const Slice& val) WARN_UNUSED_RESULT; |
130 | | |
131 | | // Set the given column to NULL. This will only succeed on nullable |
132 | | // columns. Use Unset(...) to restore a column to its default. |
133 | | CHECKED_STATUS SetNull(const Slice& col_name) WARN_UNUSED_RESULT; |
134 | | CHECKED_STATUS SetNull(size_t col_idx) WARN_UNUSED_RESULT; |
135 | | |
136 | | // Unsets the given column. Note that this is different from setting |
137 | | // it to NULL. |
138 | | CHECKED_STATUS Unset(const Slice& col_name) WARN_UNUSED_RESULT; |
139 | | CHECKED_STATUS Unset(size_t col_idx) WARN_UNUSED_RESULT; |
140 | | |
141 | | //------------------------------------------------------------ |
142 | | // Getters |
143 | | //------------------------------------------------------------ |
144 | | // These getters return a bad Status if the type does not match, |
145 | | // the value is unset, or the value is NULL. Otherwise they return |
146 | | // the current set value in *val. |
147 | | |
148 | | // Return true if the given column has been specified. |
149 | | bool IsColumnSet(const Slice& col_name) const; |
150 | | bool IsColumnSet(size_t col_idx) const; |
151 | | |
152 | | bool IsNull(const Slice& col_name) const; |
153 | | bool IsNull(size_t col_idx) const; |
154 | | |
155 | | CHECKED_STATUS GetBool(const Slice& col_name, bool* val) const WARN_UNUSED_RESULT; |
156 | | |
157 | | CHECKED_STATUS GetInt8(const Slice& col_name, int8_t* val) const WARN_UNUSED_RESULT; |
158 | | CHECKED_STATUS GetInt16(const Slice& col_name, int16_t* val) const WARN_UNUSED_RESULT; |
159 | | CHECKED_STATUS GetInt32(const Slice& col_name, int32_t* val) const WARN_UNUSED_RESULT; |
160 | | CHECKED_STATUS GetInt64(const Slice& col_name, int64_t* val) const WARN_UNUSED_RESULT; |
161 | | CHECKED_STATUS GetTimestamp(const Slice& col_name, |
162 | | int64_t* micros_since_utc_epoch) const WARN_UNUSED_RESULT; |
163 | | |
164 | | CHECKED_STATUS GetFloat(const Slice& col_name, float* val) const WARN_UNUSED_RESULT; |
165 | | CHECKED_STATUS GetDouble(const Slice& col_name, double* val) const WARN_UNUSED_RESULT; |
166 | | |
167 | | // Same as above getters, but with numeric column indexes. |
168 | | // These are faster since they avoid a hashmap lookup, so should |
169 | | // be preferred in performance-sensitive code. |
170 | | CHECKED_STATUS GetBool(size_t col_idx, bool* val) const WARN_UNUSED_RESULT; |
171 | | |
172 | | CHECKED_STATUS GetInt8(size_t col_idx, int8_t* val) const WARN_UNUSED_RESULT; |
173 | | CHECKED_STATUS GetInt16(size_t col_idx, int16_t* val) const WARN_UNUSED_RESULT; |
174 | | CHECKED_STATUS GetInt32(size_t col_idx, int32_t* val) const WARN_UNUSED_RESULT; |
175 | | CHECKED_STATUS GetInt64(size_t col_idx, int64_t* val) const WARN_UNUSED_RESULT; |
176 | | CHECKED_STATUS GetTimestamp(size_t col_idx, |
177 | | int64_t* micros_since_utc_epoch) const WARN_UNUSED_RESULT; |
178 | | |
179 | | CHECKED_STATUS GetFloat(size_t col_idx, float* val) const WARN_UNUSED_RESULT; |
180 | | CHECKED_STATUS GetDouble(size_t col_idx, double* val) const WARN_UNUSED_RESULT; |
181 | | |
182 | | // Gets the string/binary value but does not copy the value. Callers should |
183 | | // copy the resulting Slice if necessary. |
184 | | CHECKED_STATUS GetString(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT; |
185 | | CHECKED_STATUS GetString(size_t col_idx, Slice* val) const WARN_UNUSED_RESULT; |
186 | | CHECKED_STATUS GetBinary(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT; |
187 | | CHECKED_STATUS GetBinary(size_t col_idx, Slice* val) const WARN_UNUSED_RESULT; |
188 | | CHECKED_STATUS GetInet(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT; |
189 | | CHECKED_STATUS GetInet(size_t col_idx, Slice* val) const WARN_UNUSED_RESULT; |
190 | | CHECKED_STATUS GetJsonb(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT; |
191 | | CHECKED_STATUS GetJsonb(size_t col_idx, Slice* val) const WARN_UNUSED_RESULT; |
192 | | CHECKED_STATUS GetDecimal(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT; |
193 | | CHECKED_STATUS GetDecimal(size_t col_idx, Slice* val) const WARN_UNUSED_RESULT; |
194 | | CHECKED_STATUS GetUuid(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT; |
195 | | CHECKED_STATUS GetUuid(size_t col_idx, Slice* val) const WARN_UNUSED_RESULT; |
196 | | CHECKED_STATUS GetTimeUuid(const Slice& col_name, Slice* val) const WARN_UNUSED_RESULT; |
197 | | CHECKED_STATUS GetTimeUuid(size_t col_idx, Slice* val) const WARN_UNUSED_RESULT; |
198 | | |
199 | | //------------------------------------------------------------ |
200 | | // Utility code |
201 | | //------------------------------------------------------------ |
202 | | |
203 | | // Returns true if there are hash key columns and values for all of them have been specified. |
204 | | bool IsHashKeySet() const; |
205 | | |
206 | | // Returns true if there are primary key (hash plus range) columns and values for all of them |
207 | | // have been specified. |
208 | | bool IsKeySet() const; |
209 | | |
210 | | // Returns true if either hash key or primary key exist and are set. |
211 | | // In practice this means that either: |
212 | | // - hash key columns exist and are set |
213 | | // - no hash key column exist but primary key columns exist and are set (i.e. range portion only) |
214 | | bool IsHashOrPrimaryKeySet() const; |
215 | | |
216 | | // Return true if values for all columns have been specified. |
217 | | bool AllColumnsSet() const; |
218 | | |
219 | | std::string ToString() const; |
220 | | |
221 | 282 | const Schema* schema() const { return schema_; } |
222 | | |
223 | | private: |
224 | | friend class RowKeyUtilTest; |
225 | | friend class PartitionSchema; |
226 | | template<typename KeyTypeWrapper> friend struct client::SliceKeysTestSetup; |
227 | | template<typename KeyTypeWrapper> friend struct client::IntKeysTestSetup; |
228 | | |
229 | | template<typename T> |
230 | | CHECKED_STATUS Set(const Slice& col_name, const typename T::cpp_type& val, |
231 | | bool owned = false); |
232 | | |
233 | | template<typename T> |
234 | | CHECKED_STATUS Set(size_t col_idx, const typename T::cpp_type& val, bool owned = false); |
235 | | |
236 | | // Runtime version of the generic setter. |
237 | | CHECKED_STATUS Set(size_t column_idx, const uint8_t* val); |
238 | | |
239 | | template<typename T> |
240 | | CHECKED_STATUS Get(const Slice& col_name, typename T::cpp_type* val) const; |
241 | | |
242 | | template<typename T> |
243 | | CHECKED_STATUS Get(size_t col_idx, typename T::cpp_type* val) const; |
244 | | |
245 | | template<typename T> |
246 | | CHECKED_STATUS SetSliceCopy(const Slice& col_name, const Slice& val); |
247 | | |
248 | | template<typename T> |
249 | | CHECKED_STATUS SetSliceCopy(size_t col_idx, const Slice& val); |
250 | | |
251 | | // If the given column is a variable length column whose memory is owned by this instance, |
252 | | // deallocates the value. |
253 | | // NOTE: Does not mutate the isset bitmap. |
254 | | // REQUIRES: col_idx must be a variable length column. |
255 | | void DeallocateStringIfSet(size_t col_idx, const ColumnSchema& col); |
256 | | |
257 | | // Deallocate any string/binary values whose memory is managed by this object. |
258 | | void DeallocateOwnedStrings(); |
259 | | |
260 | | const Schema* schema_; |
261 | | |
262 | | // 1-bit set for any field which has been explicitly set. This is distinct |
263 | | // from NULL -- an "unset" field will take the server-side default on insert, |
264 | | // whereas a field explicitly set to NULL will override the default. |
265 | | uint8_t* isset_bitmap_; |
266 | | |
267 | | // 1-bit set for any variable length columns whose memory is managed by this instance. |
268 | | // These strings need to be deallocated whenever the value is reset, |
269 | | // or when the instance is destructed. |
270 | | uint8_t* owned_strings_bitmap_; |
271 | | |
272 | | // The normal "contiguous row" format row data. Any column whose data is unset |
273 | | // or NULL can have undefined bytes. |
274 | | uint8_t* row_data_; |
275 | | }; |
276 | | |
277 | | } // namespace yb |
278 | | #endif /* YB_COMMON_PARTIAL_ROW_H */ |