|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/util/uuid_util.h" |
| 21 | + |
| 22 | +#include <chrono> |
| 23 | +#include <cstdint> |
| 24 | +#include <cstring> |
| 25 | +#include <random> |
| 26 | +#include <string> |
| 27 | + |
| 28 | +#include "iceberg/result.h" |
| 29 | +#include "iceberg/util/int128.h" |
| 30 | +#include "iceberg/util/macros.h" |
| 31 | + |
| 32 | +namespace iceberg { |
| 33 | + |
| 34 | +std::array<uint8_t, 16> UUIDUtils::GenerateUuidV4() { |
| 35 | + static std::random_device rd; |
| 36 | + static std::mt19937 gen(rd()); |
| 37 | + static std::uniform_int_distribution<uint64_t> distrib( |
| 38 | + std::numeric_limits<uint64_t>::min(), std::numeric_limits<uint64_t>::max()); |
| 39 | + std::array<uint8_t, 16> uuid; |
| 40 | + |
| 41 | + // Generate two random 64-bit integers |
| 42 | + uint64_t high_bits = distrib(gen); |
| 43 | + uint64_t low_bits = distrib(gen); |
| 44 | + |
| 45 | + // Combine them into a uint128_t |
| 46 | + uint128_t random_128_bit_number = (static_cast<uint128_t>(high_bits) << 64) | low_bits; |
| 47 | + |
| 48 | + // Copy the bytes into the uuid array |
| 49 | + std::memcpy(uuid.data(), &random_128_bit_number, 16); |
| 50 | + |
| 51 | + // Set magic numbers for a "version 4" (pseudorandom) UUID and variant, |
| 52 | + // see https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-4 |
| 53 | + uuid[6] = (uuid[6] & 0x0F) | 0x40; |
| 54 | + // Set variant field, top two bits are 1, 0 |
| 55 | + uuid[8] = (uuid[8] & 0x3F) | 0x80; |
| 56 | + |
| 57 | + return uuid; |
| 58 | +} |
| 59 | + |
| 60 | +std::array<uint8_t, 16> UUIDUtils::GenerateUuidV7() { |
| 61 | + // Get the current time in milliseconds since the Unix epoch |
| 62 | + auto now = std::chrono::system_clock::now(); |
| 63 | + auto duration_since_epoch = now.time_since_epoch(); |
| 64 | + auto unix_ts_ms = |
| 65 | + std::chrono::duration_cast<std::chrono::milliseconds>(duration_since_epoch).count(); |
| 66 | + |
| 67 | + return GenerateUuidV7(static_cast<uint64_t>(unix_ts_ms)); |
| 68 | +} |
| 69 | + |
| 70 | +std::array<uint8_t, 16> UUIDUtils::GenerateUuidV7(uint64_t unix_ts_ms) { |
| 71 | + std::array<uint8_t, 16> uuid = {}; |
| 72 | + |
| 73 | + // Set the timestamp (in milliseconds since Unix epoch) |
| 74 | + uuid[0] = (unix_ts_ms >> 40) & 0xFF; |
| 75 | + uuid[1] = (unix_ts_ms >> 32) & 0xFF; |
| 76 | + uuid[2] = (unix_ts_ms >> 24) & 0xFF; |
| 77 | + uuid[3] = (unix_ts_ms >> 16) & 0xFF; |
| 78 | + uuid[4] = (unix_ts_ms >> 8) & 0xFF; |
| 79 | + uuid[5] = unix_ts_ms & 0xFF; |
| 80 | + |
| 81 | + // Generate random bytes for the remaining fields |
| 82 | + static std::random_device rd; |
| 83 | + static std::mt19937 gen(rd()); |
| 84 | + static std::uniform_int_distribution<uint16_t> distrib( |
| 85 | + std::numeric_limits<uint16_t>::min(), std::numeric_limits<uint16_t>::max()); |
| 86 | + |
| 87 | + // Note: uint8_t is invalid for uniform_int_distribution on Windows |
| 88 | + for (size_t i = 6; i < 16; i += 2) { |
| 89 | + auto rand = static_cast<uint16_t>(distrib(gen)); |
| 90 | + uuid[i] = (rand >> 8) & 0xFF; |
| 91 | + uuid[i + 1] = rand & 0xFF; |
| 92 | + } |
| 93 | + |
| 94 | + // Set magic numbers for a "version 7" (pseudorandom) UUID and variant, |
| 95 | + // see https://www.rfc-editor.org/rfc/rfc9562#name-version-field |
| 96 | + uuid[6] = (uuid[6] & 0x0F) | 0x70; |
| 97 | + // set variant field, top two bits are 1, 0 |
| 98 | + uuid[8] = (uuid[8] & 0x3F) | 0x80; |
| 99 | + |
| 100 | + return uuid; |
| 101 | +} |
| 102 | + |
| 103 | +namespace { |
| 104 | + |
| 105 | +constexpr std::array<uint8_t, 256> BuildHexTable() { |
| 106 | + std::array<uint8_t, 256> buf{}; |
| 107 | + for (int i = 0; i < 256; i++) { |
| 108 | + if (i >= '0' && i <= '9') { |
| 109 | + buf[i] = static_cast<uint8_t>(i - '0'); |
| 110 | + } else if (i >= 'a' && i <= 'f') { |
| 111 | + buf[i] = static_cast<uint8_t>(i - 'a' + 10); |
| 112 | + } else if (i >= 'A' && i <= 'F') { |
| 113 | + buf[i] = static_cast<uint8_t>(i - 'A' + 10); |
| 114 | + } else { |
| 115 | + buf[i] = 0xff; |
| 116 | + } |
| 117 | + } |
| 118 | + return buf; |
| 119 | +} |
| 120 | + |
| 121 | +constexpr std::array<uint8_t, 256> BuildShl4Table() { |
| 122 | + std::array<uint8_t, 256> buf{}; |
| 123 | + for (int i = 0; i < 256; i++) { |
| 124 | + buf[i] = static_cast<uint8_t>(i << 4); |
| 125 | + } |
| 126 | + return buf; |
| 127 | +} |
| 128 | + |
| 129 | +constexpr auto HEX_TABLE = BuildHexTable(); |
| 130 | +constexpr auto SHL4_TABLE = BuildShl4Table(); |
| 131 | + |
| 132 | +// Parse a UUID string without dashes, e.g. "67e5504410b1426f9247bb680e5fe0c8" |
| 133 | +inline Result<std::array<uint8_t, 16>> ParseSimple(std::string_view s) { |
| 134 | + ICEBERG_DCHECK(s.size() == 32, "s must be 32 characters long"); |
| 135 | + |
| 136 | + std::array<uint8_t, 16> buf{}; |
| 137 | + for (size_t i = 0; i < 16; i++) { |
| 138 | + uint8_t h1 = HEX_TABLE[static_cast<uint8_t>(s[i * 2])]; |
| 139 | + uint8_t h2 = HEX_TABLE[static_cast<uint8_t>(s[i * 2 + 1])]; |
| 140 | + |
| 141 | + if ((h1 | h2) == 0xff) { |
| 142 | + return InvalidArgument("Invalid UUID string: {}", s); |
| 143 | + } |
| 144 | + |
| 145 | + buf[i] = static_cast<uint8_t>(SHL4_TABLE[h1] | h2); |
| 146 | + } |
| 147 | + return buf; |
| 148 | +} |
| 149 | + |
| 150 | +// Parse a UUID string with dashes, e.g. "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 151 | +inline Result<std::array<uint8_t, 16>> ParseHyphenated(std::string_view s) { |
| 152 | + ICEBERG_DCHECK(s.size() == 36, "s must be 36 characters long"); |
| 153 | + |
| 154 | + // Check that dashes are in the right places |
| 155 | + if (!(s[8] == '-' && s[13] == '-' && s[18] == '-' && s[23] == '-')) { |
| 156 | + return InvalidArgument("Invalid UUID string: {}", s); |
| 157 | + } |
| 158 | + |
| 159 | + constexpr std::array<size_t, 8> positions = {0, 4, 9, 14, 19, 24, 28, 32}; |
| 160 | + std::array<uint8_t, 16> buf{}; |
| 161 | + |
| 162 | + for (size_t j = 0; j < 8; j++) { |
| 163 | + size_t i = positions[j]; |
| 164 | + uint8_t h1 = HEX_TABLE[static_cast<uint8_t>(s[i])]; |
| 165 | + uint8_t h2 = HEX_TABLE[static_cast<uint8_t>(s[i + 1])]; |
| 166 | + uint8_t h3 = HEX_TABLE[static_cast<uint8_t>(s[i + 2])]; |
| 167 | + uint8_t h4 = HEX_TABLE[static_cast<uint8_t>(s[i + 3])]; |
| 168 | + |
| 169 | + if ((h1 | h2 | h3 | h4) == 0xff) { |
| 170 | + return InvalidArgument("Invalid UUID string: {}", s); |
| 171 | + } |
| 172 | + |
| 173 | + buf[j * 2] = static_cast<uint8_t>(SHL4_TABLE[h1] | h2); |
| 174 | + buf[j * 2 + 1] = static_cast<uint8_t>(SHL4_TABLE[h3] | h4); |
| 175 | + } |
| 176 | + |
| 177 | + return buf; |
| 178 | +} |
| 179 | + |
| 180 | +} // namespace |
| 181 | + |
| 182 | +Result<std::array<uint8_t, 16>> UUIDUtils::FromString(std::string_view str) { |
| 183 | + if (str.size() == 32) { |
| 184 | + return ParseSimple(str); |
| 185 | + } else if (str.size() == 36) { |
| 186 | + return ParseHyphenated(str); |
| 187 | + } else { |
| 188 | + return InvalidArgument("Invalid UUID string: {}", str); |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +std::string UUIDUtils::ToString(std::span<uint8_t> uuid) { |
| 193 | + static const char* hex_chars = "0123456789abcdef"; |
| 194 | + ICEBERG_DCHECK(uuid.size() == 16, "uuid must be 16 bytes long"); |
| 195 | + std::string str(36, '-'); |
| 196 | + |
| 197 | + for (size_t i = 0; i < 16; i++) { |
| 198 | + str[i * 2 + (i >= 4 ? 1 : 0) + (i >= 6 ? 1 : 0) + (i >= 8 ? 1 : 0) + |
| 199 | + (i >= 10 ? 1 : 0)] = hex_chars[(uuid[i] >> 4) & 0x0F]; |
| 200 | + str[i * 2 + 1 + (i >= 4 ? 1 : 0) + (i >= 6 ? 1 : 0) + (i >= 8 ? 1 : 0) + |
| 201 | + (i >= 10 ? 1 : 0)] = hex_chars[uuid[i] & 0x0F]; |
| 202 | + } |
| 203 | + |
| 204 | + return str; |
| 205 | +} |
| 206 | + |
| 207 | +} // namespace iceberg |
0 commit comments