14#include "common/lang/span.h"
15#include "common/lang/string.h"
16#include "common/lang/vector.h"
17#include "common/log/log.h"
18#include "common/sys/rc.h"
19#include "common/value.h"
23using byte_t =
unsigned char;
24using bytes = vector<byte_t>;
25using float64_t = double_t;
31 static const byte_t term[];
32 static const byte_t lit00[];
33 static const byte_t litff[];
34 static const byte_t inf[];
35 static const byte_t msb[];
37 static const byte_t increasing = 0x00;
38 static const byte_t decreasing = 0xff;
42 bool operator==(
const infinity &i)
const {
return true; }
44 bool operator==(
infinity &&i) {
return true; }
52 bool operator==(
const decr<T> &o)
const {
return val == o.val; }
54 bool operator==(
decr<T> o) {
return val == o.val; }
66 static void invert(span<byte_t> &s)
68 std::for_each(s.begin(), s.end(), [](byte_t &c) { c ^= 0xff; });
71 static RC append(bytes &s, uint64_t x)
73 vector<byte_t> buf(9);
75 for (; x > 0; x >>= 8) {
76 buf[i--] =
static_cast<byte_t
>(x);
78 buf[i] =
static_cast<byte_t
>(8 - i);
79 s.insert(s.end(), buf.begin() + i, buf.end());
83 static RC append(bytes &s, int64_t x)
85 if (x >= -64 && x < 64) {
86 s.insert(s.end(),
static_cast<byte_t
>(x ^ 0x80));
96 for (; x > 0; x >>= 8) {
97 buf[i--] =
static_cast<byte_t
>(x);
104 if (buf[i + 1] < 1 << (8 - n)) {
113 span<byte_t> sp(&buf[i], buf.size() - i);
116 s.insert(s.end(), buf.begin() + i, buf.end());
120 static RC append(bytes &s, float64_t x)
124 LOG_WARN(
"append: NaN");
125 return RC::INVALID_ARGUMENT;
128 memcpy(&b, &x,
sizeof(x));
131 i = std::numeric_limits<int64_t>::min() - i;
133 if (OB_FAIL(append(s, i))) {
134 LOG_WARN(
"append: append failed, i=%ld, x=%lf", i, x);
140 static RC append(bytes &s,
const string &x)
143 for (
auto c = x.begin(); c < x.end(); c++) {
144 switch (byte_t(*c)) {
146 s.insert(s.end(), l, c);
147 s.insert(s.end(), &lit00[0], &lit00[0] + 2);
151 s.insert(s.end(), l, c);
152 s.insert(s.end(), &litff[0], &litff[0] + 2);
156 s.insert(s.end(), l, x.end());
157 s.insert(s.end(), &term[0], &term[0] + 2);
161 static RC append(bytes &s,
const trailing_string &x)
163 s.insert(s.end(), x.begin(), x.end());
167 static RC append(bytes &s,
const infinity &_)
169 s.insert(s.end(), &inf[0], &inf[0] + 2);
173 static RC append(bytes &s,
const string_or_infinity &x)
178 LOG_WARN(
"orderedcode: string_or_infinity has non-zero string and non-zero infinity");
179 return RC::INVALID_ARGUMENT;
181 if (OB_FAIL(append(s, infinity{}))) {
182 LOG_WARN(
"orderedcode: append infinity failed");
186 if (OB_FAIL(append(s, x.s))) {
187 LOG_WARN(
"orderedcode: append string failed");
194 static RC parse(span<byte_t> &s, byte_t dir, int64_t &dst)
197 LOG_WARN(
"orderedcode: corrupt input");
198 return RC::INVALID_ARGUMENT;
200 byte_t c = s[0] ^ dir;
201 if (c >= 0x40 && c < 0xc0) {
202 dst = int64_t(int8_t(c ^ 0x80));
206 bool neg = (c & 0x80) == 0;
214 LOG_WARN(
"orderedcode: corrupt input");
215 return RC::INVALID_ARGUMENT;
220 LOG_WARN(
"orderedcode: corrupt input");
221 return RC::INVALID_ARGUMENT;
225 for (byte_t mask = 0x80; (c & mask) != 0; mask >>= 1) {
230 LOG_WARN(
"orderedcode: corrupt input");
231 return RC::INVALID_ARGUMENT;
234 for (
size_t i = 1; i < n; i++) {
246 static RC parse(span<byte_t> &s, byte_t dir, uint64_t &dst)
250 LOG_WARN(
"orderedcode: corrupt input");
251 return RC::INVALID_ARGUMENT;
253 byte_t n = s[0] ^ dir;
254 if (n > 8 || (
int)s.size() < (1 + n)) {
255 LOG_WARN(
"orderedcode: corrupt input");
256 return RC::INVALID_ARGUMENT;
259 for (
size_t i = 0; i < n; i++) {
260 x = x << 8 | (s[1 + i] ^ dir);
263 s = s.subspan(1 + n);
267 static RC parse(span<byte_t> &s, byte_t dir, infinity &_)
271 LOG_WARN(
"orderedcode: corrupt input");
272 return RC::INVALID_ARGUMENT;
274 if ((s[0] ^ dir) != inf[0] || (s[1] ^ dir) != inf[1]) {
275 LOG_WARN(
"orderedcode: corrupt input");
276 return RC::INVALID_ARGUMENT;
282 static RC parse(span<byte_t> &s, byte_t dir,
string &dst)
285 for (
auto l = 0, i = 0; i < (int)s.size();) {
286 switch (s[i] ^ dir) {
288 if (i + 1 >= (
int)s.size()) {
289 LOG_WARN(
"orderedcode: corrupt input");
290 return RC::INVALID_ARGUMENT;
292 switch (s[i + 1] ^ dir) {
295 if (l == 0 && dir == increasing) {
296 dst.insert(dst.end(), s.begin(), s.begin() + i);
297 s = s.subspan(i + 2);
300 buf.insert(buf.end(), s.begin() + l, s.begin() + i);
301 if (dir == decreasing) {
302 span<byte_t> sp(buf);
305 dst.insert(dst.end(), buf.begin(), buf.end());
306 s = s.subspan(i + 2);
309 buf.insert(buf.end(), s.begin() + l, s.begin() + i);
310 buf.insert(buf.end(),
static_cast<byte_t
>(0x00 ^ dir));
314 default: LOG_WARN(
"orderedcode: corrupt input");
return RC::INVALID_ARGUMENT;
318 if (i + 1 >= (
int)s.size() || ((s[i + 1] ^ dir) != 0x00)) {
319 LOG_WARN(
"orderedcode: corrupt input");
320 return RC::INVALID_ARGUMENT;
322 buf.insert(buf.end(), s.begin() + l, s.begin() + i);
323 buf.insert(buf.end(),
static_cast<byte_t
>(0xff ^ dir));
330 LOG_WARN(
"orderedcode: corrupt input");
331 return RC::INVALID_ARGUMENT;
334 static RC parse(span<byte_t> &s, byte_t dir, float64_t &dst)
340 i = ((int64_t)-1 << 63) - i;
342 memcpy(&dst, &i,
sizeof(i));
343 if (std::isnan(dst)) {
344 rc = RC::INVALID_ARGUMENT;
349 static RC parse(span<byte_t> &s, byte_t dir, string_or_infinity &dst)
354 rc = parse(s, dir, _);
358 rc = parse(s, dir, dst.s);
363 static RC parse(span<byte_t> &s, byte_t dir, trailing_string &dst)
366 if (dir == increasing) {
367 dst.insert(dst.end(), s.begin(), s.end());
370 dst.insert(dst.end(), s.begin(), s.end());
379 static RC encode_without_rid(int64_t table_id, bytes &encoded_key)
382 if (OB_FAIL(OrderedCode::append(encoded_key, table_prefix))) {
383 LOG_WARN(
"append failed");
384 }
else if (OB_FAIL(OrderedCode::append(encoded_key, table_id))) {
385 LOG_WARN(
"append failed");
389 static RC encode(int64_t table_id, uint64_t rid, bytes &encoded_key)
392 if (OB_FAIL(OrderedCode::append(encoded_key, table_prefix))) {
393 LOG_WARN(
"append failed");
394 }
else if (OB_FAIL(OrderedCode::append(encoded_key, table_id))) {
395 LOG_WARN(
"append failed");
396 }
else if (OB_FAIL(OrderedCode::append(encoded_key, rowkey_prefix))) {
397 LOG_WARN(
"append failed");
398 }
else if (OB_FAIL(OrderedCode::append(encoded_key, rid))) {
399 LOG_WARN(
"append failed");
404 static RC encode_table_prefix(int64_t table_id, bytes &encoded_key)
407 if (OB_FAIL(OrderedCode::append(encoded_key, table_prefix))) {
408 LOG_WARN(
"append failed");
409 }
else if (OB_FAIL(OrderedCode::append(encoded_key, table_id))) {
410 LOG_WARN(
"append failed");
411 }
else if (OB_FAIL(OrderedCode::append(encoded_key, rowkey_prefix))) {
412 LOG_WARN(
"append failed");
417 static RC encode_value(
const Value &val, bytes &dst)
420 switch (val.attr_type()) {
422 if (OB_FAIL(OrderedCode::append(dst, (int64_t)val.
get_int()))) {
423 LOG_WARN(
"append failed");
426 case AttrType::FLOATS:
427 if (OB_FAIL(OrderedCode::append(dst, (
double)val.get_float()))) {
428 LOG_WARN(
"append failed");
431 case AttrType::CHARS:
432 if (OB_FAIL(OrderedCode::append(dst, val.get_string()))) {
433 LOG_WARN(
"append failed");
436 default:
return RC::INVALID_ARGUMENT;
441 static RC encode_int(int64_t val, bytes &dst)
444 if (OB_FAIL(OrderedCode::append(dst, val))) {
445 LOG_WARN(
"append failed");
450 static RC decode(bytes &encoded_key, int64_t &table_id)
453 span<byte_t> sp(encoded_key);
455 if (OB_FAIL(OrderedCode::parse(sp, OrderedCode::increasing, table_prefix))) {
456 LOG_WARN(
"parse failed");
458 }
else if (OB_FAIL(OrderedCode::parse(sp, OrderedCode::increasing, table_id))) {
459 LOG_WARN(
"parse failed");
465 static constexpr const char *table_prefix =
"t";
466 static constexpr const char *rowkey_prefix =
"r";
属性的值
Definition: value.h:30
int get_int() const
Definition: value.cpp:234