Coverage Report

Created: 2025-12-26 21:43

/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
621
std::string unhex(std::string_view hex_str) {
57
    // clang-format off
58
621
    const static std::unordered_map<char, char> table = {
59
621
            {'0', 0},  {'1', 1},  {'2', 2},  {'3', 3},  {'4', 4}, 
60
621
            {'5', 5},  {'6', 6},  {'7', 7},  {'8', 8},  {'9', 9}, 
61
621
            {'a', 10}, {'b', 11}, {'c', 12}, {'d', 13}, {'e', 14}, {'f', 15},
62
621
            {'A', 10}, {'B', 11}, {'C', 12}, {'D', 13}, {'E', 14}, {'F', 15}};
63
621
    [[maybe_unused]] static int8_t lut[std::max({'9', 'f', 'F'}) + 1];
64
621
    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
621
    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
621
    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
621
    size_t len = hex_str.length();
69
621
    len &= ~0x01UL;
70
621
    std::string buf(len >> 1, '\0');
71
16.7k
    for (size_t i = 0; i < len; ++i) {
72
16.0k
        const auto it = table.find(hex_str[i]);
73
16.0k
        if (it == table.end()) break;
74
16.0k
        buf[i >> 1] |= i & 0x1 ? (it->second & 0x0f) : (it->second & 0x0f) << 4;
75
16.0k
    }
76
621
    return buf;
77
621
}
78
79
static std::string explain_fields(std::string_view text, const std::vector<std::string>& fields,
80
60
                                  const std::vector<int>& pos, bool unicode = false) {
81
60
    if (fields.size() != pos.size() || fields.size() == 0 || pos.size() == 0) {
82
0
        return std::string(text.data(), text.size());
83
0
    }
84
60
    size_t last_hyphen_pos = pos.back() + 1;
85
60
    std::stringstream ss;
86
60
    std::string blank_line(last_hyphen_pos + 1, ' ');
87
88
    // clang-format off
89
60
    static const std::string hyphen("\xe2\x94\x80"); // ─ e2 94 80
90
60
    static const std::string bar   ("\xe2\x94\x82"); // │ e2 94 82
91
60
    static const std::string angle ("\xe2\x94\x8c"); // ┌ e2 94 8c
92
60
    static const std::string arrow ("\xe2\x96\xbc"); // ▼ e2 96 bc
93
    // clang-format on
94
95
    // Each line with hyphens
96
407
    for (size_t i = 0; i < fields.size(); ++i) {
97
347
        std::string line = blank_line;
98
347
        line[pos[i]] = '/';
99
347
        int nbar = i;
100
1.22k
        for (size_t j = 0; j < i; ++j) {
101
881
            line[pos[j]] = '|';
102
881
        }
103
347
        int nhyphen = 0;
104
19.3k
        for (size_t j = pos[i] + 1; j <= last_hyphen_pos; ++j) {
105
18.9k
            line[j] = '-';
106
18.9k
            ++nhyphen;
107
18.9k
        }
108
109
347
        if (unicode) {
110
329
            int i = line.size();
111
329
            line.resize(line.size() + 2 * (1 /*angle*/ + nbar + nhyphen), ' ');
112
329
            int j = line.size();
113
31.9k
            while (--i >= 0) {
114
31.6k
                if (line[i] == '-') {
115
18.0k
                    line[--j] = hyphen[2];
116
18.0k
                    line[--j] = hyphen[1];
117
18.0k
                    line[--j] = hyphen[0];
118
18.0k
                } else if (line[i] == '|') {
119
836
                    line[--j] = bar[2];
120
836
                    line[--j] = bar[1];
121
836
                    line[--j] = bar[0];
122
12.8k
                } else if (line[i] == '/') {
123
329
                    line[--j] = angle[2];
124
329
                    line[--j] = angle[1];
125
329
                    line[--j] = angle[0];
126
12.4k
                } else {
127
12.4k
                    --j;
128
12.4k
                    continue;
129
12.4k
                }
130
19.1k
                line[i] = i != j ? ' ' : line[i]; // Replace if needed
131
19.1k
            }
132
329
        }
133
134
347
        ss << line << " " << i << ". " << fields[i] << "\n";
135
347
    }
136
137
    // Mark position indicator
138
60
    std::string line = blank_line;
139
407
    for (size_t i = 0; i < fields.size(); ++i) {
140
347
        line[pos[i]] = '|';
141
347
    }
142
143
60
    if (unicode) {
144
57
        int i = line.size();
145
57
        line.resize(line.size() + 2 * fields.size(), ' ');
146
57
        int j = line.size();
147
5.19k
        while (--i >= 0) {
148
5.13k
            if (line[i] != '|') {
149
4.80k
                --j;
150
4.80k
                continue;
151
4.80k
            }
152
329
            line[--j] = bar[2];
153
329
            line[--j] = bar[1];
154
329
            line[--j] = bar[0];
155
329
            line[i] = i != j ? ' ' : line[i]; // Replace if needed
156
329
        }
157
57
    }
158
159
60
    ss << line << "\n";
160
161
60
    line = blank_line;
162
407
    for (size_t i = 0; i < fields.size(); ++i) {
163
347
        line[pos[i]] = 'v';
164
347
    }
165
166
60
    if (unicode) {
167
57
        int i = line.size();
168
57
        line.resize(line.size() + 2 * fields.size(), ' ');
169
57
        int j = line.size();
170
5.19k
        while (--i >= 0) {
171
5.13k
            if (line[i] != 'v') {
172
4.80k
                --j;
173
4.80k
                continue;
174
4.80k
            }
175
329
            line[--j] = arrow[2];
176
329
            line[--j] = arrow[1];
177
329
            line[--j] = arrow[0];
178
329
            line[i] = i != j ? ' ' : line[i]; // Replace if needed
179
329
        }
180
57
    }
181
182
60
    ss << line << "\n";
183
184
    // Original text to explain
185
60
    ss << text << "\n";
186
187
60
    return ss.str();
188
60
}
189
190
60
std::string prettify_key(std::string_view key_hex, bool unicode) {
191
    // Decoded result container
192
    //                                    val                  tag  pos
193
    //                     .---------------^----------------.  .^.  .^.
194
60
    std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> fields;
195
60
    std::string unhex_key = unhex(key_hex);
196
60
    int key_space = unhex_key[0];
197
60
    std::string_view key_copy = unhex_key;
198
60
    key_copy.remove_prefix(1); // Remove the first key space byte
199
60
    int ret = decode_key(&key_copy, &fields);
200
60
    if (ret != 0) return "";
201
202
60
    std::vector<std::string> fields_str;
203
60
    std::vector<int> fields_pos;
204
60
    fields_str.reserve(fields.size() + 1);
205
60
    fields_pos.reserve(fields.size() + 1);
206
    // Key space byte
207
60
    fields_str.push_back("key space: " + std::to_string(key_space));
208
60
    fields_pos.push_back(0);
209
210
287
    for (auto& i : fields) {
211
287
        if (std::get<1>(i) == EncodingTag::BYTES_TAG) {
212
191
            fields_str.emplace_back(std::get<std::string>(std::get<0>(i)));
213
191
        } 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
287
        fields_pos.push_back((std::get<2>(i) + 1) * 2);
219
287
    }
220
221
60
    return explain_fields(key_hex, fields_str, fields_pos, unicode);
222
60
}
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