Coverage Report

Created: 2026-01-09 19:15

/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
868k
std::string hex(std::string_view str) {
46
868k
    std::stringstream ss;
47
54.3M
    for (auto& i : str) {
48
54.3M
        ss << std::hex << std::setw(2) << std::setfill('0') << ((int16_t)i & 0xff);
49
54.3M
    }
50
868k
    return ss.str();
51
868k
}
52
53
/**
54
 * This is a naïve implementation of unhex.
55
 */
56
618
std::string unhex(std::string_view hex_str) {
57
    // clang-format off
58
618
    const static std::unordered_map<char, char> table = {
59
618
            {'0', 0},  {'1', 1},  {'2', 2},  {'3', 3},  {'4', 4}, 
60
618
            {'5', 5},  {'6', 6},  {'7', 7},  {'8', 8},  {'9', 9}, 
61
618
            {'a', 10}, {'b', 11}, {'c', 12}, {'d', 13}, {'e', 14}, {'f', 15},
62
618
            {'A', 10}, {'B', 11}, {'C', 12}, {'D', 13}, {'E', 14}, {'F', 15}};
63
618
    [[maybe_unused]] static int8_t lut[std::max({'9', 'f', 'F'}) + 1];
64
618
    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
618
    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
618
    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
618
    size_t len = hex_str.length();
69
618
    len &= ~0x01UL;
70
618
    std::string buf(len >> 1, '\0');
71
16.3k
    for (size_t i = 0; i < len; ++i) {
72
15.7k
        const auto it = table.find(hex_str[i]);
73
15.7k
        if (it == table.end()) break;
74
15.7k
        buf[i >> 1] |= i & 0x1 ? (it->second & 0x0f) : (it->second & 0x0f) << 4;
75
15.7k
    }
76
618
    return buf;
77
618
}
78
79
static std::string explain_fields(std::string_view text, const std::vector<std::string>& fields,
80
59
                                  const std::vector<int>& pos, bool unicode = false) {
81
59
    if (fields.size() != pos.size() || fields.size() == 0 || pos.size() == 0) {
82
0
        return std::string(text.data(), text.size());
83
0
    }
84
59
    size_t last_hyphen_pos = pos.back() + 1;
85
59
    std::stringstream ss;
86
59
    std::string blank_line(last_hyphen_pos + 1, ' ');
87
88
    // clang-format off
89
59
    static const std::string hyphen("\xe2\x94\x80"); // ─ e2 94 80
90
59
    static const std::string bar   ("\xe2\x94\x82"); // │ e2 94 82
91
59
    static const std::string angle ("\xe2\x94\x8c"); // ┌ e2 94 8c
92
59
    static const std::string arrow ("\xe2\x96\xbc"); // ▼ e2 96 bc
93
    // clang-format on
94
95
    // Each line with hyphens
96
401
    for (size_t i = 0; i < fields.size(); ++i) {
97
342
        std::string line = blank_line;
98
342
        line[pos[i]] = '/';
99
342
        int nbar = i;
100
1.21k
        for (size_t j = 0; j < i; ++j) {
101
871
            line[pos[j]] = '|';
102
871
        }
103
342
        int nhyphen = 0;
104
19.0k
        for (size_t j = pos[i] + 1; j <= last_hyphen_pos; ++j) {
105
18.7k
            line[j] = '-';
106
18.7k
            ++nhyphen;
107
18.7k
        }
108
109
342
        if (unicode) {
110
324
            int i = line.size();
111
324
            line.resize(line.size() + 2 * (1 /*angle*/ + nbar + nhyphen), ' ');
112
324
            int j = line.size();
113
31.5k
            while (--i >= 0) {
114
31.2k
                if (line[i] == '-') {
115
17.7k
                    line[--j] = hyphen[2];
116
17.7k
                    line[--j] = hyphen[1];
117
17.7k
                    line[--j] = hyphen[0];
118
17.7k
                } else if (line[i] == '|') {
119
826
                    line[--j] = bar[2];
120
826
                    line[--j] = bar[1];
121
826
                    line[--j] = bar[0];
122
12.6k
                } else if (line[i] == '/') {
123
324
                    line[--j] = angle[2];
124
324
                    line[--j] = angle[1];
125
324
                    line[--j] = angle[0];
126
12.3k
                } else {
127
12.3k
                    --j;
128
12.3k
                    continue;
129
12.3k
                }
130
18.9k
                line[i] = i != j ? ' ' : line[i]; // Replace if needed
131
18.9k
            }
132
324
        }
133
134
342
        ss << line << " " << i << ". " << fields[i] << "\n";
135
342
    }
136
137
    // Mark position indicator
138
59
    std::string line = blank_line;
139
401
    for (size_t i = 0; i < fields.size(); ++i) {
140
342
        line[pos[i]] = '|';
141
342
    }
142
143
59
    if (unicode) {
144
56
        int i = line.size();
145
56
        line.resize(line.size() + 2 * fields.size(), ' ');
146
56
        int j = line.size();
147
5.11k
        while (--i >= 0) {
148
5.05k
            if (line[i] != '|') {
149
4.73k
                --j;
150
4.73k
                continue;
151
4.73k
            }
152
324
            line[--j] = bar[2];
153
324
            line[--j] = bar[1];
154
324
            line[--j] = bar[0];
155
324
            line[i] = i != j ? ' ' : line[i]; // Replace if needed
156
324
        }
157
56
    }
158
159
59
    ss << line << "\n";
160
161
59
    line = blank_line;
162
401
    for (size_t i = 0; i < fields.size(); ++i) {
163
342
        line[pos[i]] = 'v';
164
342
    }
165
166
59
    if (unicode) {
167
56
        int i = line.size();
168
56
        line.resize(line.size() + 2 * fields.size(), ' ');
169
56
        int j = line.size();
170
5.11k
        while (--i >= 0) {
171
5.05k
            if (line[i] != 'v') {
172
4.73k
                --j;
173
4.73k
                continue;
174
4.73k
            }
175
324
            line[--j] = arrow[2];
176
324
            line[--j] = arrow[1];
177
324
            line[--j] = arrow[0];
178
324
            line[i] = i != j ? ' ' : line[i]; // Replace if needed
179
324
        }
180
56
    }
181
182
59
    ss << line << "\n";
183
184
    // Original text to explain
185
59
    ss << text << "\n";
186
187
59
    return ss.str();
188
59
}
189
190
59
std::string prettify_key(std::string_view key_hex, bool unicode) {
191
    // Decoded result container
192
    //                                    val                  tag  pos
193
    //                     .---------------^----------------.  .^.  .^.
194
59
    std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> fields;
195
59
    std::string unhex_key = unhex(key_hex);
196
59
    int key_space = unhex_key[0];
197
59
    std::string_view key_copy = unhex_key;
198
59
    key_copy.remove_prefix(1); // Remove the first key space byte
199
59
    int ret = decode_key(&key_copy, &fields);
200
59
    if (ret != 0) return "";
201
202
59
    std::vector<std::string> fields_str;
203
59
    std::vector<int> fields_pos;
204
59
    fields_str.reserve(fields.size() + 1);
205
59
    fields_pos.reserve(fields.size() + 1);
206
    // Key space byte
207
59
    fields_str.push_back("key space: " + std::to_string(key_space));
208
59
    fields_pos.push_back(0);
209
210
283
    for (auto& i : fields) {
211
283
        if (std::get<1>(i) == EncodingTag::BYTES_TAG) {
212
187
            fields_str.emplace_back(std::get<std::string>(std::get<0>(i)));
213
187
        } 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
94
        } else {
216
94
            fields_str.emplace_back(std::to_string(std::get<int64_t>(std::get<0>(i))));
217
94
        }
218
283
        fields_pos.push_back((std::get<2>(i) + 1) * 2);
219
283
    }
220
221
59
    return explain_fields(key_hex, fields_str, fields_pos, unicode);
222
59
}
223
224
2.07k
std::string proto_to_json(const ::google::protobuf::Message& msg, bool add_whitespace) {
225
2.07k
    std::string json;
226
2.07k
    google::protobuf::util::JsonPrintOptions opts;
227
2.07k
    opts.add_whitespace = add_whitespace;
228
2.07k
    opts.preserve_proto_field_names = true;
229
2.07k
    google::protobuf::util::MessageToJsonString(msg, &json, opts);
230
2.07k
    return json;
231
2.07k
}
232
233
147k
TxnErrorCode key_exists(Transaction* txn, std::string_view key, bool snapshot) {
234
147k
    std::string end_key {key};
235
147k
    encode_int64(INT64_MAX, &end_key);
236
147k
    std::unique_ptr<RangeGetIterator> it;
237
147k
    TxnErrorCode err = txn->get(key, end_key, &it, snapshot, 1);
238
147k
    if (err != TxnErrorCode::TXN_OK) {
239
120
        return err;
240
120
    }
241
147k
    return it->has_next() ? TxnErrorCode::TXN_OK : TxnErrorCode::TXN_KEY_NOT_FOUND;
242
147k
}
243
244
} // namespace doris::cloud