Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <limits>
#include <string>
#include <string_view>
#include <type_traits>
 
namespace onnxruntime {
 
inline constexpr size_t kMaxTelemetryStringLength = 1024;
 
namespace telemetry_detail {
 
inline constexpr size_t kMaxTelemetryCollectionEntries = 128;
inline constexpr size_t kMaxTelemetryProbeBytes = 16 * 1024;
inline constexpr size_t kMaxTelemetryPathBytes = 4096;
 
inline std::string_view TelemetryStringView(std::string_view value,
                                            size_t max_bytes = kMaxTelemetryStringLength) {
  if (value.size() <= max_bytes) {
    return value;
  }
  size_t end = max_bytes;
  while (end > 0 && (static_cast<unsigned char>(value[end]) & 0xC0) == 0x80) {
    --end;
  }
  return value.substr(0, end);
}
 
inline std::string_view TelemetryCStringView(const char* value, size_t max_bytes = kMaxTelemetryStringLength) {
  if (value == nullptr) {
    return {};
  }
  size_t length = 0;
  while (length <= max_bytes && value[length] != '\0') {
    ++length;
  }
  return std::string_view(value, length);
}
 
inline std::string_view TelemetryStringView(const char* value,
                                            size_t max_bytes = kMaxTelemetryStringLength) {
  return TelemetryStringView(TelemetryCStringView(value, max_bytes), max_bytes);
}
 
// Invalid UTF-8 bytes become '?'; a valid codepoint that cannot fit is omitted in full.
inline bool AppendTelemetryString(std::string& output, std::string_view value,
                                  size_t max_bytes = kMaxTelemetryStringLength) {
  if (output.size() > max_bytes) {
    output.resize(TelemetryStringView(std::string_view(output), max_bytes).size());
  }
  size_t offset = 0;
  while (offset < value.size() && output.size() < max_bytes) {
    const auto lead = static_cast<unsigned char>(value[offset]);
    const size_t width = lead < 0x80 ? 1 : lead >= 0xC2 && lead <= 0xDF ? 2
                                       : lead >= 0xE0 && lead <= 0xEF   ? 3
                                       : lead >= 0xF0 && lead <= 0xF4   ? 4
                                                                        : 0;
    bool valid = width != 0 && width <= value.size() - offset;
    for (size_t i = 1; valid && i < width; ++i) {
      const auto byte = static_cast<unsigned char>(value[offset + i]);
      valid = (byte & 0xC0) == 0x80;
      if (i == 1) {
        valid = valid && !(lead == 0xE0 && byte < 0xA0) &&
                !(lead == 0xED && byte >= 0xA0) &&
                !(lead == 0xF0 && byte < 0x90) &&
                !(lead == 0xF4 && byte >= 0x90);
      }
    }
    if (!valid) {
      output += '?';
      ++offset;
    } else {
      if (width > max_bytes - output.size()) break;
      output.append(value.data() + offset, width);
      offset += width;
    }
  }
  return offset == value.size();
}
 
// Commit a complete comma-separated row or leave every summary unchanged.
template <size_t Columns>
bool AppendTelemetryRow(const std::array<std::string*, Columns>& summaries,
                        const std::array<std::string_view, Columns>& values, bool first) {
  std::array<std::string, Columns> staged;
  for (size_t i = 0; i < Columns; ++i) {
    staged[i] = *summaries[i];
    if ((!first && !AppendTelemetryString(staged[i], ",")) ||
        !AppendTelemetryString(staged[i], values[i])) {
      return false;
    }
  }
  for (size_t i = 0; i < Columns; ++i) {
    summaries[i]->swap(staged[i]);
  }
  return true;
}
 
inline std::string BoundedTelemetryString(std::string_view value,
                                          size_t max_bytes = kMaxTelemetryStringLength) {
  std::string result;
  result.reserve((std::min)(value.size(), max_bytes));
  AppendTelemetryString(result, value, max_bytes);
  return result;
}
 
inline std::string BoundedTelemetryString(const char* value,
                                          size_t max_bytes = kMaxTelemetryStringLength) {
  if (max_bytes == 0) return {};
  // Look past the output boundary far enough to distinguish a full codepoint from malformed input.
  const size_t probe_bytes = max_bytes + (std::min)(size_t{3}, (std::numeric_limits<size_t>::max)() - max_bytes);
  return BoundedTelemetryString(TelemetryCStringView(value, probe_bytes), max_bytes);
}
 
// JNI encodes supplementary characters as two three-byte sequences, and NUL as two bytes.
template <typename Char>
size_t TelemetryUnicodePrefixLength(
    const Char* value, size_t length, size_t max_bytes = kMaxTelemetryStringLength, bool modified_utf8 = false) {
  size_t bytes = 0;
  size_t end = 0;
  while (end < length) {
    uint32_t codepoint = static_cast<uint32_t>(value[end]);
    size_t width = 1;
    if constexpr (sizeof(Char) == 2) {
      if (codepoint >= 0xD800 && codepoint <= 0xDBFF && end + 1 < length) {
        const uint32_t low = static_cast<uint32_t>(value[end + 1]);
        if (low >= 0xDC00 && low <= 0xDFFF) {
          codepoint = 0x10000 + ((codepoint - 0xD800) << 10) + low - 0xDC00;
          width = 2;
        }
      }
    }
    const size_t encoded_bytes = modified_utf8 && width == 2       ? 6
                                 : modified_utf8 && codepoint == 0 ? 2
                                 : codepoint <= 0x7F               ? 1
                                 : codepoint <= 0x7FF              ? 2
                                 : codepoint <= 0xFFFF             ? 3
                                                                   : 4;
    if (encoded_bytes > max_bytes - bytes) {
      break;
    }
    bytes += encoded_bytes;
    end += width;
  }
  return end;
}
 
template <typename Char>
std::basic_string_view<Char> TelemetryUnicodeStringView(
    std::basic_string_view<Char> value, size_t max_bytes = kMaxTelemetryStringLength, bool modified_utf8 = false) {
  return value.substr(0, TelemetryUnicodePrefixLength(value.data(), value.size(), max_bytes, modified_utf8));
}
 
inline std::wstring_view TelemetryWideStringView(std::wstring_view value,
                                                 size_t max_bytes = kMaxTelemetryStringLength) {
  return TelemetryUnicodeStringView(value, max_bytes);
}
 
inline std::wstring BoundedTelemetryWideString(std::wstring_view value) {
  return std::wstring(TelemetryWideStringView(value));
}
 
inline std::wstring_view TelemetryWideStringView(const wchar_t* value) {
  if (value == nullptr) {
    return {};
  }
  size_t length = 0;
  while (length <= kMaxTelemetryStringLength && value[length] != L'\0') {
    ++length;
  }
  return TelemetryWideStringView(std::wstring_view(value, length));
}
 
inline std::string_view TelemetryStringValue(std::string_view value) {
  // Preserve the suffix so the aggregate append can report truncation to path redaction.
  return value;
}
 
template <typename T, std::enable_if_t<std::is_arithmetic_v<T>, int> = 0>
std::string TelemetryStringValue(T value) {
  return std::to_string(value);
}
 
template <typename Range>
std::string JoinTelemetryStrings(const Range& values) {
  std::string output;
  size_t count = 0;
  for (const auto& value : values) {
    if (count++ == kMaxTelemetryCollectionEntries ||
        (count > 1 && !AppendTelemetryString(output, ",")) ||
        !AppendTelemetryString(output, value) || output.size() == kMaxTelemetryStringLength) {
      break;
    }
  }
  return output;
}
 
template <typename Map>
std::string FormatTelemetryMap(const Map& values, std::string_view separator = ",",
                               std::string_view key_separator = "=", bool* truncated = nullptr) {
  if (truncated != nullptr) *truncated = false;
  std::array<const typename Map::value_type*, kMaxTelemetryCollectionEntries> entries{};
  size_t count = 0;
  for (const auto& entry : values) {
    if (count == entries.size()) {
      if (truncated != nullptr) *truncated = true;
      break;
    }
    entries[count++] = &entry;
  }
  std::sort(entries.begin(), entries.begin() + count, [](const auto* lhs, const auto* rhs) {
    if constexpr (std::is_arithmetic_v<typename Map::key_type>) {
      return lhs->first < rhs->first;
    } else {
      return TelemetryStringView(std::string_view(lhs->first)) < TelemetryStringView(std::string_view(rhs->first));
    }
  });
  std::string output;
  for (size_t i = 0; i < count; ++i) {
    if ((i != 0 && !AppendTelemetryString(output, separator)) ||
        !AppendTelemetryString(output, TelemetryStringValue(entries[i]->first)) ||
        !AppendTelemetryString(output, key_separator) ||
        !AppendTelemetryString(output, TelemetryStringValue(entries[i]->second)) ||
        output.size() == kMaxTelemetryStringLength) {
      if (truncated != nullptr) *truncated = true;
      break;
    }
  }
  return output;
}
 
// TraceLogging macros retain pointers across statements; temporaries cannot own these values.
class TelemetryStrings {
 public:
  const char* Utf8(std::string_view value) {
    strings_.push_back(BoundedTelemetryString(value));
    return strings_.back().c_str();
  }
 
  const char* Utf8(const char* value) {
    strings_.push_back(BoundedTelemetryString(value));
    return strings_.back().c_str();
  }
 
  const wchar_t* Wide(std::wstring_view value) {
    wide_strings_.push_back(BoundedTelemetryWideString(value));
    return wide_strings_.back().c_str();
  }
 
  const wchar_t* Wide(const wchar_t* value) {
    return Wide(TelemetryWideStringView(value));
  }
 
  TelemetryStrings() = default;
  TelemetryStrings(const TelemetryStrings&) = delete;
  TelemetryStrings& operator=(const TelemetryStrings&) = delete;
  TelemetryStrings(TelemetryStrings&&) = delete;
  TelemetryStrings& operator=(TelemetryStrings&&) = delete;
 
 private:
  std::deque<std::string> strings_;
  std::deque<std::wstring> wide_strings_;
};
 
}  // namespace telemetry_detail
}  // namespace onnxruntime