/Users/deen/code/yugabyte-db/src/yb/common/row_key-util.cc
Line | Count | Source (jump to first uncovered line) |
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 | | |
33 | | #include "yb/common/row_key-util.h" |
34 | | |
35 | | |
36 | | #include "yb/gutil/mathlimits.h" |
37 | | |
38 | | #include "yb/common/common.pb.h" |
39 | | #include "yb/common/row.h" |
40 | | #include "yb/common/schema.h" |
41 | | #include "yb/common/types.h" |
42 | | |
43 | | #include "yb/util/memory/arena.h" |
44 | | |
45 | | namespace yb { |
46 | | namespace row_key_util { |
47 | | |
48 | | namespace { |
49 | | |
50 | | template<DataType type> |
51 | 9 | bool IncrementIntCell(void* cell_ptr) { |
52 | 9 | typedef DataTypeTraits<type> traits; |
53 | 9 | typedef typename traits::cpp_type cpp_type; |
54 | | |
55 | 9 | cpp_type orig; |
56 | 9 | memcpy(&orig, cell_ptr, sizeof(cpp_type)); |
57 | | |
58 | 9 | cpp_type inc; |
59 | 9 | if (boost::is_unsigned<cpp_type>::value) { |
60 | 0 | inc = orig + 1; |
61 | 9 | } else { |
62 | | // Signed overflow is undefined in C. So, we'll use a branch here |
63 | | // instead of counting on undefined behavior. |
64 | 9 | if (orig == MathLimits<cpp_type>::kMax) { |
65 | 5 | inc = MathLimits<cpp_type>::kMin; |
66 | 4 | } else { |
67 | 4 | inc = orig + 1; |
68 | 4 | } |
69 | 9 | } |
70 | 9 | memcpy(cell_ptr, &inc, sizeof(cpp_type)); |
71 | 9 | return inc > orig; |
72 | 9 | } Unexecuted instantiation: row_key-util.cc:_ZN2yb12row_key_util12_GLOBAL__N_116IncrementIntCellILNS_8DataTypeE100EEEbPv Unexecuted instantiation: row_key-util.cc:_ZN2yb12row_key_util12_GLOBAL__N_116IncrementIntCellILNS_8DataTypeE101EEEbPv Unexecuted instantiation: row_key-util.cc:_ZN2yb12row_key_util12_GLOBAL__N_116IncrementIntCellILNS_8DataTypeE102EEEbPv Unexecuted instantiation: row_key-util.cc:_ZN2yb12row_key_util12_GLOBAL__N_116IncrementIntCellILNS_8DataTypeE103EEEbPv Unexecuted instantiation: row_key-util.cc:_ZN2yb12row_key_util12_GLOBAL__N_116IncrementIntCellILNS_8DataTypeE1EEEbPv Unexecuted instantiation: row_key-util.cc:_ZN2yb12row_key_util12_GLOBAL__N_116IncrementIntCellILNS_8DataTypeE2EEEbPv row_key-util.cc:_ZN2yb12row_key_util12_GLOBAL__N_116IncrementIntCellILNS_8DataTypeE3EEEbPv Line | Count | Source | 51 | 9 | bool IncrementIntCell(void* cell_ptr) { | 52 | 9 | typedef DataTypeTraits<type> traits; | 53 | 9 | typedef typename traits::cpp_type cpp_type; | 54 | | | 55 | 9 | cpp_type orig; | 56 | 9 | memcpy(&orig, cell_ptr, sizeof(cpp_type)); | 57 | | | 58 | 9 | cpp_type inc; | 59 | 9 | if (boost::is_unsigned<cpp_type>::value) { | 60 | 0 | inc = orig + 1; | 61 | 9 | } else { | 62 | | // Signed overflow is undefined in C. So, we'll use a branch here | 63 | | // instead of counting on undefined behavior. | 64 | 9 | if (orig == MathLimits<cpp_type>::kMax) { | 65 | 5 | inc = MathLimits<cpp_type>::kMin; | 66 | 4 | } else { | 67 | 4 | inc = orig + 1; | 68 | 4 | } | 69 | 9 | } | 70 | 9 | memcpy(cell_ptr, &inc, sizeof(cpp_type)); | 71 | 9 | return inc > orig; | 72 | 9 | } |
Unexecuted instantiation: row_key-util.cc:_ZN2yb12row_key_util12_GLOBAL__N_116IncrementIntCellILNS_8DataTypeE10EEEbPv Unexecuted instantiation: row_key-util.cc:_ZN2yb12row_key_util12_GLOBAL__N_116IncrementIntCellILNS_8DataTypeE4EEEbPv |
73 | | |
74 | 3 | bool IncrementStringCell(void* cell_ptr, Arena* arena) { |
75 | 3 | Slice orig; |
76 | 3 | memcpy(&orig, cell_ptr, sizeof(orig)); |
77 | 3 | uint8_t* new_buf = CHECK_NOTNULL( |
78 | 3 | static_cast<uint8_t*>(arena->AllocateBytes(orig.size() + 1))); |
79 | 3 | memcpy(new_buf, orig.data(), orig.size()); |
80 | 3 | new_buf[orig.size()] = '\0'; |
81 | | |
82 | 3 | Slice inc(new_buf, orig.size() + 1); |
83 | 3 | memcpy(cell_ptr, &inc, sizeof(inc)); |
84 | 3 | return true; |
85 | 3 | } |
86 | | |
87 | 12 | bool IncrementCell(const ColumnSchema& col, void* cell_ptr, Arena* arena) { |
88 | 12 | DataType type = col.type_info()->physical_type(); |
89 | 12 | switch (type) { |
90 | 9 | #define HANDLE_TYPE(t) case t: return IncrementIntCell<t>(cell_ptr); |
91 | 0 | HANDLE_TYPE(UINT8); |
92 | 0 | HANDLE_TYPE(UINT16); |
93 | 0 | HANDLE_TYPE(UINT32); |
94 | 0 | HANDLE_TYPE(UINT64); |
95 | 0 | HANDLE_TYPE(INT8); |
96 | 0 | HANDLE_TYPE(INT16); |
97 | 9 | HANDLE_TYPE(INT32); |
98 | 0 | HANDLE_TYPE(TIMESTAMP); |
99 | 0 | HANDLE_TYPE(INT64); |
100 | 0 | case UNKNOWN_DATA: |
101 | 0 | case BOOL: |
102 | 0 | case FLOAT: |
103 | 0 | case DOUBLE: |
104 | 0 | LOG(FATAL) << "Unable to handle type " << type << " in row keys"; |
105 | 0 | case STRING: |
106 | 3 | case BINARY: |
107 | 3 | return IncrementStringCell(cell_ptr, arena); |
108 | 0 | default: CHECK(false) << "Unknown data type: " << type; |
109 | 12 | } |
110 | 0 | return false; // unreachable |
111 | 12 | #undef HANDLE_TYPE |
112 | 12 | } |
113 | | |
114 | | } // anonymous namespace |
115 | | |
116 | 0 | void SetKeyToMinValues(ContiguousRow* row) { |
117 | 0 | for (size_t i = 0; i < row->schema()->num_key_columns(); i++) { |
118 | 0 | const ColumnSchema& col = row->schema()->column(i); |
119 | 0 | col.type_info()->CopyMinValue(row->mutable_cell_ptr(i)); |
120 | 0 | } |
121 | 0 | } |
122 | | |
123 | 9 | bool IncrementKey(ContiguousRow* row, Arena* arena) { |
124 | 9 | return IncrementKeyPrefix(row, row->schema()->num_key_columns(), arena); |
125 | 9 | } |
126 | | |
127 | 9 | bool IncrementKeyPrefix(ContiguousRow* row, size_t prefix_len, Arena* arena) { |
128 | 14 | for (size_t i = prefix_len; i > 0;) { |
129 | 12 | --i; |
130 | 12 | if (IncrementCell(row->schema()->column(i), row->mutable_cell_ptr(i), arena)) { |
131 | 7 | return true; |
132 | 7 | } |
133 | 12 | } |
134 | 2 | return false; |
135 | 9 | } |
136 | | |
137 | | } // namespace row_key_util |
138 | | } // namespace yb |