YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/slice.cc
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
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#include "yb/util/slice.h"
33
34
#include "yb/util/status.h"
35
#include "yb/util/status_format.h"
36
37
DEFINE_int32(non_graph_characters_percentage_to_use_hexadecimal_rendering, 10,
38
             "Non graph charaters percentage to use hexadecimal rendering");
39
40
namespace yb {
41
42
18.3k
Status Slice::check_size(size_t expected_size) const {
43
18.3k
  if (PREDICT_FALSE(size() != expected_size)) {
44
0
    return STATUS(Corruption, StringPrintf("Unexpected Slice size. "
45
0
        "Expected %zu but got %zu.", expected_size, size()), ToDebugString(100));
46
0
  }
47
18.3k
  return Status::OK();
48
18.3k
}
49
50
3.98M
void Slice::CopyToBuffer(std::string* buffer) const {
51
3.98M
  buffer->assign(cdata(), size());
52
3.98M
}
53
54
// Return a string that contains the copy of the referenced data.
55
225M
std::string Slice::ToBuffer() const {
56
225M
  return std::string(cdata(), size());
57
225M
}
58
59
9.91k
std::string Slice::ToString(bool hex) const {
60
8.27k
  return hex ? ToDebugHexString() : ToString();
61
9.91k
}
62
63
39.8k
std::string Slice::ToDebugHexString() const {
64
39.8k
  std::string result;
65
39.8k
  char buf[10];
66
776k
  for (auto i = begin_; i != end_; ++i) {
67
736k
    snprintf(buf, sizeof(buf), "%02X", *i);
68
736k
    result += buf;
69
736k
  }
70
39.8k
  return result;
71
39.8k
}
72
73
16.6k
std::string Slice::ToDebugString(size_t max_len) const {
74
16.6k
  size_t bytes_to_print = size();
75
16.6k
  bool abbreviated = false;
76
16.6k
  if (max_len != 0 && bytes_to_print > max_len) {
77
1
    bytes_to_print = max_len;
78
1
    abbreviated = true;
79
1
  }
80
81
16.6k
  size_t num_not_graph = 0;
82
34.2k
  for (size_t i = 0; i < bytes_to_print; i++) {
83
17.6k
    if (!isgraph(begin_[i])) {
84
15.7k
      ++num_not_graph;
85
15.7k
    }
86
17.6k
  }
87
88
16.6k
  if (num_not_graph * 100 >
89
7.89k
      bytes_to_print * FLAGS_non_graph_characters_percentage_to_use_hexadecimal_rendering) {
90
7.89k
    return ToDebugHexString();
91
7.89k
  }
92
8.77k
  size_t size = bytes_to_print + 3 * num_not_graph + (abbreviated ? 20 : 0);
93
94
8.77k
  std::string ret;
95
8.77k
  ret.reserve(size);
96
10.5k
  for (size_t i = 0; i < bytes_to_print; i++) {
97
1.76k
    auto ch = begin_[i];
98
1.76k
    if (!isgraph(ch)) {
99
0
      if (ch == '\r') {
100
0
        ret += "\\r";
101
0
      } else if (ch == '\n') {
102
0
        ret += "\\n";
103
0
      } else if (ch == ' ') {
104
0
        ret += ' ';
105
0
      } else {
106
0
        StringAppendF(&ret, "\\x%02x", ch & 0xff);
107
0
      }
108
1.76k
    } else {
109
1.76k
      ret.push_back(ch);
110
1.76k
    }
111
1.76k
  }
112
8.77k
  if (abbreviated) {
113
1
    StringAppendF(&ret, "...<%zd bytes total>", this->size());
114
1
  }
115
8.77k
  return ret;
116
8.77k
}
117
118
627
Slice::Slice(const SliceParts& parts, std::string* buf) {
119
627
  size_t length = 0;
120
1.26k
  for (int i = 0; i < parts.num_parts; ++i) {
121
635
    length += parts.parts[i].size();
122
635
  }
123
627
  buf->reserve(length);
124
125
1.26k
  for (int i = 0; i < parts.num_parts; ++i) {
126
635
    buf->append(parts.parts[i].cdata(), parts.parts[i].size());
127
635
  }
128
627
  *this = Slice(*buf);
129
627
}
130
131
40.0M
Status Slice::consume_byte(char c) {
132
40.0M
  char consumed = consume_byte();
133
40.0M
  if (consumed != c) {
134
0
    return STATUS_FORMAT(Corruption, "Wrong first byte, expected $0 but found $1",
135
0
                         static_cast<int>(c), static_cast<int>(consumed));
136
0
  }
137
138
40.0M
  return Status::OK();
139
40.0M
}
140
141
1.73G
uint8_t Slice::operator[](size_t n) const {
142
1.73G
  DCHECK_LT(n, size());
143
1.73G
  return begin_[n];
144
1.73G
}
145
146
5.19G
void Slice::remove_prefix(size_t n) {
147
5.19G
  DCHECK_LE(n, size());
148
5.19G
  begin_ += n;
149
5.19G
}
150
151
122M
Slice Slice::Prefix(size_t n) const {
152
122M
  DCHECK_LE(n, size());
153
122M
  return Slice(begin_, n);
154
122M
}
155
156
11.7M
Slice Slice::WithoutPrefix(size_t n) const {
157
11.7M
  DCHECK_LE(n, size());
158
11.7M
  return Slice(begin_ + n, end_);
159
11.7M
}
160
161
758M
void Slice::remove_suffix(size_t n) {
162
758M
  DCHECK_LE(n, size());
163
758M
  end_ -= n;
164
758M
}
165
166
0
Slice Slice::Suffix(size_t n) const {
167
0
  DCHECK_LE(n, size());
168
0
  return Slice(end_ - n, end_);
169
0
}
170
171
0
Slice Slice::WithoutSuffix(size_t n) const {
172
0
  DCHECK_LE(n, size());
173
0
  return Slice(begin_, end_ - n);
174
0
}
175
176
0
void Slice::truncate(size_t n) {
177
0
  DCHECK_LE(n, size());
178
0
  end_ = begin_ + n;
179
0
}
180
181
1.82G
char Slice::consume_byte() {
182
1.82G
  DCHECK_GT(end_, begin_);
183
1.82G
  return *begin_++;
184
1.82G
}
185
186
0
std::string SliceParts::ToDebugHexString() const {
187
0
  std::string result;
188
0
  for (int i = 0; i != num_parts; ++i) {
189
0
    result += parts[i].ToDebugHexString();
190
0
  }
191
0
  return result;
192
0
}
193
194
301M
size_t SliceParts::SumSizes() const {
195
301M
  size_t result = 0;
196
972M
  for (int i = 0; i != num_parts; ++i) {
197
670M
    result += parts[i].size();
198
670M
  }
199
301M
  return result;
200
301M
}
201
202
301M
char* SliceParts::CopyAllTo(char* out) const {
203
971M
  for (int i = 0; i != num_parts; ++i) {
204
670M
    if (!parts[i].size()) {
205
43.7M
      continue;
206
43.7M
    }
207
626M
    memcpy(out, parts[i].data(), parts[i].size());
208
626M
    out += parts[i].size();
209
626M
  }
210
301M
  return out;
211
301M
}
212
213
758k
Slice SliceParts::TheOnlyPart() const {
214
758k
  CHECK_EQ(num_parts, 1);
215
758k
  return parts[0];
216
758k
}
217
218
}  // namespace yb