/Users/deen/code/yugabyte-db/src/yb/util/yb_partition.cc
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 | | #include "yb/util/yb_partition.h" |
15 | | |
16 | | #include "yb/gutil/hash/hash.h" |
17 | | |
18 | | namespace yb { |
19 | | |
20 | 28 | uint16_t YBPartition::CqlToYBHashCode(int64_t cql_hash) { |
21 | 28 | uint16_t hash_code = static_cast<uint16_t>(cql_hash >> 48); |
22 | 28 | hash_code ^= 0x8000; // flip first bit so that negative values are smaller than positives. |
23 | 28 | return hash_code; |
24 | 28 | } |
25 | | |
26 | 98 | int64_t YBPartition::YBToCqlHashCode(uint16_t hash) { |
27 | 98 | uint64 hash_long = hash ^ 0x8000; // undo the flipped bit |
28 | 98 | int64_t cql_hash = static_cast<int64_t>(hash_long << 48); |
29 | 98 | return cql_hash; |
30 | 98 | } |
31 | | |
32 | 165k | std::string YBPartition::CqlTokenSplit(size_t node_count, size_t index) { |
33 | 165k | uint64 hash_code = (UINT16_MAX / node_count * index) << 48; |
34 | 165k | int64_t cql_hash_code = static_cast<int64_t>(hash_code); |
35 | 165k | return std::to_string(cql_hash_code); |
36 | 165k | } |
37 | | |
38 | 6.58M | void YBPartition::AppendBytesToKey(const char *bytes, size_t len, std::string *encoded_key) { |
39 | 6.58M | encoded_key->append(bytes, len); |
40 | 6.58M | } |
41 | | |
42 | 11.1M | uint16_t YBPartition::HashColumnCompoundValue(const std::string &compound) { |
43 | | // In the future, if you wish to change the hashing behavior, you must introduce a new hashing |
44 | | // method for your newly-created tables. Existing tables must continue to use their hashing |
45 | | // methods that was define by their PartitionSchema. |
46 | | |
47 | | // At the moment, Jenkins' hash is the only method we are using. In the future, we'll keep this |
48 | | // as the default hashing behavior. Constant 'kseed" cannot be changed as it'd yield a different |
49 | | // hashing result. |
50 | 11.1M | static const int kseed = 97; |
51 | 11.1M | const uint64_t hash_value = Hash64StringWithSeed(compound, kseed); |
52 | | |
53 | | // Convert the 64-bit hash value to 16 bit integer. |
54 | 11.1M | const uint64_t h1 = hash_value >> 48; |
55 | 11.1M | const uint64_t h2 = 3 * (hash_value >> 32); |
56 | 11.1M | const uint64_t h3 = 5 * (hash_value >> 16); |
57 | 11.1M | const uint64_t h4 = 7 * (hash_value & 0xffff); |
58 | | |
59 | 11.1M | return (h1 ^ h2 ^ h3 ^ h4) & 0xffff; |
60 | 11.1M | } |
61 | | |
62 | | } // namespace yb |