Coverage Report

Created: 2025-03-12 11:55

/root/doris/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
61.6k
void to_hex(T val, char* buf) {
40
61.6k
    static const char* digits = "0123456789abcdef";
41
1.04M
    for (int i = 0; i < 2 * sizeof(T); ++i) {
42
987k
        buf[2 * sizeof(T) - 1 - i] = digits[val & 0x0F];
43
987k
        val >>= 4;
44
987k
    }
45
61.6k
}
46
47
template <typename T>
48
34.1k
void from_hex(T* ret, std::string_view buf) {
49
34.1k
    T val = 0;
50
546k
    for (char i : buf) {
51
546k
        int buf_val = 0;
52
546k
        if (i >= '0' && i <= '9') {
53
382k
            buf_val = i - '0';
54
382k
        } else {
55
163k
            buf_val = i - 'a' + 10;
56
163k
        }
57
546k
        val <<= 4;
58
546k
        val = val | buf_val;
59
546k
    }
60
34.1k
    *ret = val;
61
34.1k
}
62
63
struct UniqueId {
64
    int64_t hi = 0;
65
    int64_t lo = 0;
66
67
27.0k
    UniqueId() = default;
68
29.5k
    UniqueId(int64_t hi_, int64_t lo_) : hi(hi_), lo(lo_) {}
69
18.2k
    UniqueId(const UniqueId& uid) : hi(uid.hi), lo(uid.lo) {}
70
202
    UniqueId(const TUniqueId& tuid) : hi(tuid.hi), lo(tuid.lo) {}
71
1.18k
    UniqueId(const PUniqueId& puid) : hi(puid.hi()), lo(puid.lo()) {}
72
6
    UniqueId(const std::string& hi_str, const std::string& lo_str) {
73
6
        from_hex(&hi, hi_str);
74
6
        from_hex(&lo, lo_str);
75
6
    }
76
77
    bool initialized() const { return hi != 0 || lo != 0; }
78
79
    // currently, the implementation is uuid, but it may change in the future
80
26.6k
    static UniqueId gen_uid() {
81
26.6k
        UniqueId uid(0, 0);
82
26.6k
        auto uuid = UUIDGenerator::instance()->next_uuid();
83
26.6k
        memcpy(&uid.hi, uuid.data, sizeof(int64_t));
84
26.6k
        memcpy(&uid.lo, uuid.data + sizeof(int64_t), sizeof(int64_t));
85
26.6k
        return uid;
86
26.6k
    }
87
88
76.2k
    ~UniqueId() noexcept {}
89
90
634
    std::string to_string() const {
91
634
        char buf[33];
92
634
        to_hex(hi, buf);
93
634
        buf[16] = '-';
94
634
        to_hex(lo, buf + 17);
95
634
        return {buf, 33};
96
634
    }
97
98
28.1k
    UniqueId& operator=(const UniqueId uid) {
99
28.1k
        hi = uid.hi;
100
28.1k
        lo = uid.lo;
101
28.1k
        return *this;
102
28.1k
    }
103
104
    UniqueId& operator=(const PUniqueId puid) {
105
        hi = puid.hi();
106
        lo = puid.lo();
107
        return *this;
108
    }
109
110
    UniqueId& operator=(const TUniqueId tuid) {
111
        hi = tuid.hi;
112
        lo = tuid.lo;
113
        return *this;
114
    }
115
    //compare PUniqueId and UniqueId
116
    bool operator==(const PUniqueId& rhs) const { return hi == rhs.hi() && lo == rhs.lo(); }
117
118
    bool operator!=(const PUniqueId& rhs) const { return hi != rhs.hi() || lo != rhs.lo(); }
119
120
    // std::map std::set needs this operator
121
    bool operator<(const UniqueId& right) const {
122
        if (hi != right.hi) {
123
            return hi < right.hi;
124
        } else {
125
            return lo < right.lo;
126
        }
127
    }
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
52
    bool operator==(const UniqueId& rhs) const { return hi == rhs.hi && lo == rhs.lo; }
134
135
290
    bool operator!=(const UniqueId& rhs) const { return hi != rhs.hi || lo != rhs.lo; }
136
137
    TUniqueId to_thrift() const {
138
        TUniqueId tid;
139
        tid.__set_hi(hi);
140
        tid.__set_lo(lo);
141
        return tid;
142
    }
143
144
12.6k
    PUniqueId to_proto() const {
145
12.6k
        PUniqueId pid;
146
12.6k
        pid.set_hi(hi);
147
12.6k
        pid.set_lo(lo);
148
12.6k
        return pid;
149
12.6k
    }
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
inline std::string generate_uuid_string() {
157
    return boost::uuids::to_string(boost::uuids::basic_random_generator<boost::mt19937>()());
158
}
159
160
/// generates a 16 byte UUID
161
43
inline TUniqueId generate_uuid() {
162
43
    auto uuid = boost::uuids::basic_random_generator<boost::mt19937>()();
163
43
    TUniqueId uid;
164
43
    memcpy(&uid.hi, uuid.data, sizeof(int64_t));
165
43
    memcpy(&uid.lo, uuid.data + sizeof(int64_t), sizeof(int64_t));
166
43
    return uid;
167
43
}
168
169
std::ostream& operator<<(std::ostream& os, const UniqueId& uid);
170
171
std::string print_id(const TUniqueId& id);
172
std::string print_id(const PUniqueId& id);
173
174
// Parse 's' into a TUniqueId object.  The format of s needs to be the output format
175
// from PrintId.  (<hi_part>:<low_part>)
176
// Returns true if parse succeeded.
177
bool parse_id(const std::string& s, TUniqueId* id);
178
179
} // namespace doris
180
181
template <>
182
struct std::hash<doris::UniqueId> {
183
132
    size_t operator()(const doris::UniqueId& uid) const { return uid.hash(); }
184
};