Coverage Report

Created: 2026-06-02 10:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/uid_util.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
#pragma once
19
20
#include <gen_cpp/Types_types.h>
21
#include <gen_cpp/types.pb.h>
22
#include <stdint.h>
23
24
#include <boost/random/mersenne_twister.hpp>
25
#include <boost/uuid/random_generator.hpp>
26
#include <boost/uuid/uuid.hpp>
27
#include <boost/uuid/uuid_io.hpp>
28
#include <cstring>
29
#include <ostream>
30
#include <string>
31
#include <string_view>
32
33
#include "util/uuid_generator.h"
34
35
namespace doris {
36
37
// convert int to a hex format string, buf must enough to hold converted hex string
38
template <typename T>
39
51.4M
void to_hex(T val, char* buf) {
40
51.4M
    static const char* digits = "0123456789abcdef";
41
873M
    for (size_t i = 0; i < 2 * sizeof(T); ++i) {
42
822M
        buf[2 * sizeof(T) - 1 - i] = digits[val & 0x0F];
43
822M
        val >>= 4;
44
822M
    }
45
51.4M
}
46
47
template <typename T>
48
3.38M
void from_hex(T* ret, std::string_view buf) {
49
3.38M
    T val = 0;
50
54.2M
    for (char i : buf) {
51
54.2M
        int buf_val = 0;
52
54.2M
        if (i >= '0' && i <= '9') {
53
38.1M
            buf_val = i - '0';
54
38.1M
        } else {
55
16.0M
            buf_val = i - 'a' + 10;
56
16.0M
        }
57
54.2M
        val <<= 4;
58
54.2M
        val = val | buf_val;
59
54.2M
    }
60
3.38M
    *ret = val;
61
3.38M
}
62
63
struct UniqueId {
64
    int64_t hi = 0;
65
    int64_t lo = 0;
66
67
3.45M
    UniqueId() = default;
68
5.20M
    UniqueId(int64_t hi_, int64_t lo_) : hi(hi_), lo(lo_) {}
69
9.87M
    UniqueId(const UniqueId& uid) : hi(uid.hi), lo(uid.lo) {}
70
275k
    UniqueId(const TUniqueId& tuid) : hi(tuid.hi), lo(tuid.lo) {}
71
1.81M
    UniqueId(const PUniqueId& puid) : hi(puid.hi()), lo(puid.lo()) {}
72
72.7k
    UniqueId(const std::string& hi_str, const std::string& lo_str) {
73
72.7k
        from_hex(&hi, hi_str);
74
72.7k
        from_hex(&lo, lo_str);
75
72.7k
    }
76
77
3.26M
    bool initialized() const { return hi != 0 || lo != 0; }
78
79
    // currently, the implementation is uuid, but it may change in the future
80
4.05M
    static UniqueId gen_uid() {
81
4.05M
        UniqueId uid(0, 0);
82
4.05M
        auto uuid = UUIDGenerator::instance()->next_uuid();
83
4.05M
        memcpy(&uid.hi, uuid.data, sizeof(int64_t));
84
4.05M
        memcpy(&uid.lo, uuid.data + sizeof(int64_t), sizeof(int64_t));
85
4.05M
        return uid;
86
4.05M
    }
87
88
19.7M
    ~UniqueId() noexcept {}
89
90
7.98M
    std::string to_string() const {
91
7.98M
        char buf[33];
92
7.98M
        to_hex(hi, buf);
93
7.98M
        buf[16] = '-';
94
7.98M
        to_hex(lo, buf + 17);
95
7.98M
        return {buf, 33};
96
7.98M
    }
97
98
3.76M
    UniqueId& operator=(const UniqueId uid) {
99
3.76M
        hi = uid.hi;
100
3.76M
        lo = uid.lo;
101
3.76M
        return *this;
102
3.76M
    }
103
104
345k
    UniqueId& operator=(const PUniqueId puid) {
105
345k
        hi = puid.hi();
106
345k
        lo = puid.lo();
107
345k
        return *this;
108
345k
    }
109
110
166
    UniqueId& operator=(const TUniqueId tuid) {
111
166
        hi = tuid.hi;
112
166
        lo = tuid.lo;
113
166
        return *this;
114
166
    }
115
    //compare PUniqueId and UniqueId
116
0
    bool operator==(const PUniqueId& rhs) const { return hi == rhs.hi() && lo == rhs.lo(); }
117
118
65.7k
    bool operator!=(const PUniqueId& rhs) const { return hi != rhs.hi() || lo != rhs.lo(); }
119
120
    // std::map std::set needs this operator
121
437k
    bool operator<(const UniqueId& right) const {
122
437k
        if (hi != right.hi) {
123
0
            return hi < right.hi;
124
437k
        } else {
125
437k
            return lo < right.lo;
126
437k
        }
127
437k
    }
128
129
    // std::unordered_map need this api
130
    size_t hash(size_t seed = 0) const;
131
132
    // std::unordered_map need this api
133
225k
    bool operator==(const UniqueId& rhs) const { return hi == rhs.hi && lo == rhs.lo; }
134
135
312k
    bool operator!=(const UniqueId& rhs) const { return hi != rhs.hi || lo != rhs.lo; }
136
137
489k
    TUniqueId to_thrift() const {
138
489k
        TUniqueId tid;
139
489k
        tid.__set_hi(hi);
140
489k
        tid.__set_lo(lo);
141
489k
        return tid;
142
489k
    }
143
144
124k
    PUniqueId to_proto() const {
145
124k
        PUniqueId pid;
146
124k
        pid.set_hi(hi);
147
124k
        pid.set_lo(lo);
148
124k
        return pid;
149
124k
    }
150
};
151
152
// This function must be called 'hash_value' to be picked up by boost.
153
std::size_t hash_value(const doris::TUniqueId& id);
154
155
/// generates a 16 byte UUID
156
15.0k
inline std::string generate_uuid_string() {
157
15.0k
    return boost::uuids::to_string(boost::uuids::basic_random_generator<boost::mt19937>()());
158
15.0k
}
159
160
/// generates a 16 byte UUID
161
194
inline TUniqueId generate_uuid() {
162
194
    auto uuid = boost::uuids::basic_random_generator<boost::mt19937>()();
163
194
    TUniqueId uid;
164
194
    memcpy(&uid.hi, uuid.data, sizeof(int64_t));
165
194
    memcpy(&uid.lo, uuid.data + sizeof(int64_t), sizeof(int64_t));
166
194
    return uid;
167
194
}
168
169
std::ostream& operator<<(std::ostream& os, const UniqueId& uid);
170
171
std::string print_id(const UniqueId& id);
172
std::string print_id(const TUniqueId& id);
173
std::string print_id(const PUniqueId& id);
174
175
} // namespace doris
176
177
template <>
178
struct std::hash<doris::UniqueId> {
179
646k
    size_t operator()(const doris::UniqueId& uid) const { return uid.hash(); }
180
};