Coverage Report

Created: 2026-08-12 15:25

/root/doris/cloud/src/common/util.cpp
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
// clang-format off
19
#include "util.h"
20
21
#include <bthread/butex.h>
22
#include <butil/iobuf.h>
23
#include <google/protobuf/util/json_util.h>
24
#include <google/protobuf/util/field_mask_util.h>
25
26
// FIXME: we should not rely other modules that may rely on this common module
27
#include "common/logging.h"
28
#include "common/config.h"
29
#include "meta-store/keys.h"
30
#include "meta-store/codec.h"
31
#include "meta-store/txn_kv.h"
32
#include "meta-store/txn_kv_error.h"
33
34
#include <iomanip>
35
#include <sstream>
36
#include <unordered_map>
37
#include <variant>
38
// clang-format on
39
40
namespace doris::cloud {
41
42
/**
43
 * This is a naïve implementation of hex, DONOT use it on retical path.
44
 */
45
917k
std::string hex(std::string_view str) {
46
917k
    std::stringstream ss;
47
56.9M
    for (auto& i : str) {
48
56.9M
        ss << std::hex << std::setw(2) << std::setfill('0') << ((int16_t)i & 0xff);
49
56.9M
    }
50
917k
    return ss.str();
51
917k
}
52
53
/**
54
 * This is a naïve implementation of unhex.
55
 */
56
626
std::string unhex(std::string_view hex_str) {
57
    // clang-format off
58
626
    const static std::unordered_map<char, char> table = {
59
626
            {'0', 0},  {'1', 1},  {'2', 2},  {'3', 3},  {'4', 4}, 
60
626
            {'5', 5},  {'6', 6},  {'7', 7},  {'8', 8},  {'9', 9}, 
61
626
            {'a', 10}, {'b', 11}, {'c', 12}, {'d', 13}, {'e', 14}, {'f', 15},
62
626
            {'A', 10}, {'B', 11}, {'C', 12}, {'D', 13}, {'E', 14}, {'F', 15}};
63
626
    [[maybe_unused]] static int8_t lut[std::max({'9', 'f', 'F'}) + 1];
64
626
    lut[(int)'0'] = 0; lut[(int)'1'] = 1; lut[(int)'2'] = 2; lut[(int)'3'] = 3; lut[(int)'4'] = 4; lut[(int)'5'] = 5; lut[(int)'6'] = 6; lut[(int)'7'] = 7; lut[(int)'8'] = 8; lut[(int)'9'] = 9;
65
626
    lut[(int)'a'] = 10; lut[(int)'b'] = 11; lut[(int)'c'] = 12; lut[(int)'d'] = 13; lut[(int)'e'] = 14; lut[(int)'f'] = 15;
66
626
    lut[(int)'A'] = 10; lut[(int)'B'] = 11; lut[(int)'C'] = 12; lut[(int)'D'] = 13; lut[(int)'E'] = 14; lut[(int)'F'] = 15;
67
    // clang-format on
68
626
    size_t len = hex_str.length();
69
626
    len &= ~0x01UL;
70
626
    std::string buf(len >> 1, '\0');
71
17.4k
    for (size_t i = 0; i < len; ++i) {
72
16.8k
        const auto it = table.find(hex_str[i]);
73
16.8k
        if (it == table.end()) break;
74
16.8k
        buf[i >> 1] |= i & 0x1 ? (it->second & 0x0f) : (it->second & 0x0f) << 4;
75
16.8k
    }
76
626
    return buf;
77
626
}
78
79
static std::string explain_fields(std::string_view text, const std::vector<std::string>& fields,
80
62
                                  const std::vector<int>& pos, bool unicode = false) {
81
62
    if (fields.size() != pos.size() || fields.size() == 0 || pos.size() == 0) {
82
0
        return std::string(text.data(), text.size());
83
0
    }
84
62
    size_t last_hyphen_pos = pos.back() + 1;
85
62
    std::stringstream ss;
86
62
    std::string blank_line(last_hyphen_pos + 1, ' ');
87
88
    // clang-format off
89
62
    static const std::string hyphen("\xe2\x94\x80"); // ─ e2 94 80
90
62
    static const std::string bar   ("\xe2\x94\x82"); // │ e2 94 82
91
62
    static const std::string angle ("\xe2\x94\x8c"); // ┌ e2 94 8c
92
62
    static const std::string arrow ("\xe2\x96\xbc"); // ▼ e2 96 bc
93
    // clang-format on
94
95
    // Each line with hyphens
96
427
    for (size_t i = 0; i < fields.size(); ++i) {
97
365
        std::string line = blank_line;
98
365
        line[pos[i]] = '/';
99
365
        int nbar = i;
100
1.31k
        for (size_t j = 0; j < i; ++j) {
101
953
            line[pos[j]] = '|';
102
953
        }
103
365
        int nhyphen = 0;
104
20.9k
        for (size_t j = pos[i] + 1; j <= last_hyphen_pos; ++j) {
105
20.5k
            line[j] = '-';
106
20.5k
            ++nhyphen;
107
20.5k
        }
108
109
365
        if (unicode) {
110
347
            int i = line.size();
111
347
            line.resize(line.size() + 2 * (1 /*angle*/ + nbar + nhyphen), ' ');
112
347
            int j = line.size();
113
35.0k
            while (--i >= 0) {
114
34.6k
                if (line[i] == '-') {
115
19.5k
                    line[--j] = hyphen[2];
116
19.5k
                    line[--j] = hyphen[1];
117
19.5k
                    line[--j] = hyphen[0];
118
19.5k
                } else if (line[i] == '|') {
119
908
                    line[--j] = bar[2];
120
908
                    line[--j] = bar[1];
121
908
                    line[--j] = bar[0];
122
14.2k
                } else if (line[i] == '/') {
123
347
                    line[--j] = angle[2];
124
347
                    line[--j] = angle[1];
125
347
                    line[--j] = angle[0];
126
13.8k
                } else {
127
13.8k
                    --j;
128
13.8k
                    continue;
129
13.8k
                }
130
20.8k
                line[i] = i != j ? ' ' : line[i]; // Replace if needed
131
20.8k
            }
132
347
        }
133
134
365
        ss << line << " " << i << ". " << fields[i] << "\n";
135
365
    }
136
137
    // Mark position indicator
138
62
    std::string line = blank_line;
139
427
    for (size_t i = 0; i < fields.size(); ++i) {
140
365
        line[pos[i]] = '|';
141
365
    }
142
143
62
    if (unicode) {
144
59
        int i = line.size();
145
59
        line.resize(line.size() + 2 * fields.size(), ' ');
146
59
        int j = line.size();
147
5.53k
        while (--i >= 0) {
148
5.47k
            if (line[i] != '|') {
149
5.12k
                --j;
150
5.12k
                continue;
151
5.12k
            }
152
347
            line[--j] = bar[2];
153
347
            line[--j] = bar[1];
154
347
            line[--j] = bar[0];
155
347
            line[i] = i != j ? ' ' : line[i]; // Replace if needed
156
347
        }
157
59
    }
158
159
62
    ss << line << "\n";
160
161
62
    line = blank_line;
162
427
    for (size_t i = 0; i < fields.size(); ++i) {
163
365
        line[pos[i]] = 'v';
164
365
    }
165
166
62
    if (unicode) {
167
59
        int i = line.size();
168
59
        line.resize(line.size() + 2 * fields.size(), ' ');
169
59
        int j = line.size();
170
5.53k
        while (--i >= 0) {
171
5.47k
            if (line[i] != 'v') {
172
5.12k
                --j;
173
5.12k
                continue;
174
5.12k
            }
175
347
            line[--j] = arrow[2];
176
347
            line[--j] = arrow[1];
177
347
            line[--j] = arrow[0];
178
347
            line[i] = i != j ? ' ' : line[i]; // Replace if needed
179
347
        }
180
59
    }
181
182
62
    ss << line << "\n";
183
184
    // Original text to explain
185
62
    ss << text << "\n";
186
187
62
    return ss.str();
188
62
}
189
190
62
std::string prettify_key(std::string_view key_hex, bool unicode) {
191
    // Decoded result container
192
    //                                    val                  tag  pos
193
    //                     .---------------^----------------.  .^.  .^.
194
62
    std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> fields;
195
62
    std::string unhex_key = unhex(key_hex);
196
62
    int key_space = unhex_key[0];
197
62
    std::string_view key_copy = unhex_key;
198
62
    key_copy.remove_prefix(1); // Remove the first key space byte
199
62
    int ret = decode_key(&key_copy, &fields);
200
62
    if (ret != 0) return "";
201
202
62
    std::vector<std::string> fields_str;
203
62
    std::vector<int> fields_pos;
204
62
    fields_str.reserve(fields.size() + 1);
205
62
    fields_pos.reserve(fields.size() + 1);
206
    // Key space byte
207
62
    fields_str.push_back("key space: " + std::to_string(key_space));
208
62
    fields_pos.push_back(0);
209
210
303
    for (auto& i : fields) {
211
303
        if (std::get<1>(i) == EncodingTag::BYTES_TAG) {
212
197
            fields_str.emplace_back(std::get<std::string>(std::get<0>(i)));
213
197
        } else if (std::get<1>(i) == EncodingTag::VERSIONSTAMP_TAG) {
214
2
            fields_str.emplace_back("versionstamp: " + std::get<std::string>(std::get<0>(i)));
215
104
        } else {
216
104
            fields_str.emplace_back(std::to_string(std::get<int64_t>(std::get<0>(i))));
217
104
        }
218
303
        fields_pos.push_back((std::get<2>(i) + 1) * 2);
219
303
    }
220
221
62
    return explain_fields(key_hex, fields_str, fields_pos, unicode);
222
62
}
223
224
2.47k
std::string proto_to_json(const ::google::protobuf::Message& msg, bool add_whitespace) {
225
2.47k
    std::string json;
226
2.47k
    google::protobuf::util::JsonPrintOptions opts;
227
2.47k
    opts.add_whitespace = add_whitespace;
228
2.47k
    opts.preserve_proto_field_names = true;
229
2.47k
    google::protobuf::util::MessageToJsonString(msg, &json, opts);
230
2.47k
    return json;
231
2.47k
}
232
233
139k
TxnErrorCode key_exists(Transaction* txn, std::string_view key, bool snapshot) {
234
139k
    std::string end_key {key};
235
139k
    encode_int64(INT64_MAX, &end_key);
236
139k
    std::unique_ptr<RangeGetIterator> it;
237
139k
    TxnErrorCode err = txn->get(key, end_key, &it, snapshot, 1);
238
139k
    if (err != TxnErrorCode::TXN_OK) {
239
120
        return err;
240
120
    }
241
139k
    return it->has_next() ? TxnErrorCode::TXN_OK : TxnErrorCode::TXN_KEY_NOT_FOUND;
242
139k
}
243
244
} // namespace doris::cloud