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"
22using byte_t =
unsigned char;
23using bytes = vector<byte_t>;
24using float64_t = double_t;
30 static const byte_t term[];
31 static const byte_t lit00[];
32 static const byte_t litff[];
33 static const byte_t inf[];
34 static const byte_t msb[];
36 static const byte_t increasing = 0x00;
37 static const byte_t decreasing = 0xff;
41 bool operator==(
const infinity &i)
const {
return true; }
43 bool operator==(
infinity &&i) {
return true; }
51 bool operator==(
const decr<T> &o)
const {
return val == o.val; }
53 bool operator==(
decr<T> o) {
return val == o.val; }
65 static void invert(span<byte_t> &s)
67 std::for_each(s.begin(), s.end(), [](byte_t &c) { c ^= 0xff; });
70 static RC append(bytes &s, uint64_t x)
72 vector<byte_t> buf(9);
74 for (; x > 0; x >>= 8) {
75 buf[i--] =
static_cast<byte_t
>(x);
77 buf[i] =
static_cast<byte_t
>(8 - i);
78 s.insert(s.end(), buf.begin() + i, buf.end());
82 static RC append(bytes &s, int64_t x)
84 if (x >= -64 && x < 64) {
85 s.insert(s.end(),
static_cast<byte_t
>(x ^ 0x80));
95 for (; x > 0; x >>= 8) {
96 buf[i--] =
static_cast<byte_t
>(x);
103 if (buf[i + 1] < 1 << (8 - n)) {
112 span<byte_t> sp(&buf[i], buf.size() - i);
115 s.insert(s.end(), buf.begin() + i, buf.end());
119 static RC append(bytes &s, float64_t x)
123 LOG_WARN(
"append: NaN");
124 return RC::INVALID_ARGUMENT;
127 memcpy(&b, &x,
sizeof(x));
130 i = std::numeric_limits<int64_t>::min() - i;
132 if (OB_FAIL(append(s, i))) {
133 LOG_WARN(
"append: append failed, i=%ld, x=%lf", i, x);
139 static RC append(bytes &s,
const string &x)
142 for (
auto c = x.begin(); c < x.end(); c++) {
143 switch (byte_t(*c)) {
145 s.insert(s.end(), l, c);
146 s.insert(s.end(), &lit00[0], &lit00[0] + 2);
150 s.insert(s.end(), l, c);
151 s.insert(s.end(), &litff[0], &litff[0] + 2);
155 s.insert(s.end(), l, x.end());
156 s.insert(s.end(), &term[0], &term[0] + 2);
160 static RC append(bytes &s,
const trailing_string &x)
162 s.insert(s.end(), x.begin(), x.end());
166 static RC append(bytes &s,
const infinity &_)
168 s.insert(s.end(), &inf[0], &inf[0] + 2);
172 static RC append(bytes &s,
const string_or_infinity &x)
177 LOG_WARN(
"orderedcode: string_or_infinity has non-zero string and non-zero infinity");
178 return RC::INVALID_ARGUMENT;
180 if (OB_FAIL(append(s, infinity{}))) {
181 LOG_WARN(
"orderedcode: append infinity failed");
185 if (OB_FAIL(append(s, x.s))) {
186 LOG_WARN(
"orderedcode: append string failed");
193 static RC parse(span<byte_t> &s, byte_t dir, int64_t &dst)
196 LOG_WARN(
"orderedcode: corrupt input");
197 return RC::INVALID_ARGUMENT;
199 byte_t c = s[0] ^ dir;
200 if (c >= 0x40 && c < 0xc0) {
201 dst = int64_t(int8_t(c ^ 0x80));
205 bool neg = (c & 0x80) == 0;
213 LOG_WARN(
"orderedcode: corrupt input");
214 return RC::INVALID_ARGUMENT;
219 LOG_WARN(
"orderedcode: corrupt input");
220 return RC::INVALID_ARGUMENT;
224 for (byte_t mask = 0x80; (c & mask) != 0; mask >>= 1) {
229 LOG_WARN(
"orderedcode: corrupt input");
230 return RC::INVALID_ARGUMENT;
233 for (
size_t i = 1; i < n; i++) {
245 static RC parse(span<byte_t> &s, byte_t dir, uint64_t &dst)
249 LOG_WARN(
"orderedcode: corrupt input");
250 return RC::INVALID_ARGUMENT;
252 byte_t n = s[0] ^ dir;
253 if (n > 8 || (
int)s.size() < (1 + n)) {
254 LOG_WARN(
"orderedcode: corrupt input");
255 return RC::INVALID_ARGUMENT;
258 for (
size_t i = 0; i < n; i++) {
259 x = x << 8 | (s[1 + i] ^ dir);
262 s = s.subspan(1 + n);
266 static RC parse(span<byte_t> &s, byte_t dir, infinity &_)
270 LOG_WARN(
"orderedcode: corrupt input");
271 return RC::INVALID_ARGUMENT;
273 if ((s[0] ^ dir) != inf[0] || (s[1] ^ dir) != inf[1]) {
274 LOG_WARN(
"orderedcode: corrupt input");
275 return RC::INVALID_ARGUMENT;
281 static RC parse(span<byte_t> &s, byte_t dir,
string &dst)
284 for (
auto l = 0, i = 0; i < (int)s.size();) {
285 switch (s[i] ^ dir) {
287 if (i + 1 >= (
int)s.size()) {
288 LOG_WARN(
"orderedcode: corrupt input");
289 return RC::INVALID_ARGUMENT;
291 switch (s[i + 1] ^ dir) {
294 if (l == 0 && dir == increasing) {
295 dst.insert(dst.end(), s.begin(), s.begin() + i);
296 s = s.subspan(i + 2);
299 buf.insert(buf.end(), s.begin() + l, s.begin() + i);
300 if (dir == decreasing) {
301 span<byte_t> sp(buf);
304 dst.insert(dst.end(), buf.begin(), buf.end());
305 s = s.subspan(i + 2);
308 buf.insert(buf.end(), s.begin() + l, s.begin() + i);
309 buf.insert(buf.end(),
static_cast<byte_t
>(0x00 ^ dir));
313 default: LOG_WARN(
"orderedcode: corrupt input");
return RC::INVALID_ARGUMENT;
317 if (i + 1 >= (
int)s.size() || ((s[i + 1] ^ dir) != 0x00)) {
318 LOG_WARN(
"orderedcode: corrupt input");
319 return RC::INVALID_ARGUMENT;
321 buf.insert(buf.end(), s.begin() + l, s.begin() + i);
322 buf.insert(buf.end(),
static_cast<byte_t
>(0xff ^ dir));
329 LOG_WARN(
"orderedcode: corrupt input");
330 return RC::INVALID_ARGUMENT;
333 static RC parse(span<byte_t> &s, byte_t dir, float64_t &dst)
339 i = ((int64_t)-1 << 63) - i;
341 memcpy(&dst, &i,
sizeof(i));
342 if (std::isnan(dst)) {
343 rc = RC::INVALID_ARGUMENT;
348 static RC parse(span<byte_t> &s, byte_t dir, string_or_infinity &dst)
353 rc = parse(s, dir, _);
357 rc = parse(s, dir, dst.s);
362 static RC parse(span<byte_t> &s, byte_t dir, trailing_string &dst)
365 if (dir == increasing) {
366 dst.insert(dst.end(), s.begin(), s.end());
369 dst.insert(dst.end(), s.begin(), s.end());
378 static RC encode_without_rid(int64_t table_id, bytes &encoded_key)
381 if (OB_FAIL(OrderedCode::append(encoded_key, table_prefix))) {
382 LOG_WARN(
"append failed");
383 }
else if (OB_FAIL(OrderedCode::append(encoded_key, table_id))) {
384 LOG_WARN(
"append failed");
388 static RC encode(int64_t table_id, uint64_t rid, bytes &encoded_key)
391 if (OB_FAIL(OrderedCode::append(encoded_key, table_prefix))) {
392 LOG_WARN(
"append failed");
393 }
else if (OB_FAIL(OrderedCode::append(encoded_key, table_id))) {
394 LOG_WARN(
"append failed");
395 }
else if (OB_FAIL(OrderedCode::append(encoded_key, rowkey_prefix))) {
396 LOG_WARN(
"append failed");
397 }
else if (OB_FAIL(OrderedCode::append(encoded_key, rid))) {
398 LOG_WARN(
"append failed");
403 static RC encode_table_prefix(int64_t table_id, bytes &encoded_key)
406 if (OB_FAIL(OrderedCode::append(encoded_key, table_prefix))) {
407 LOG_WARN(
"append failed");
408 }
else if (OB_FAIL(OrderedCode::append(encoded_key, table_id))) {
409 LOG_WARN(
"append failed");
410 }
else if (OB_FAIL(OrderedCode::append(encoded_key, rowkey_prefix))) {
411 LOG_WARN(
"append failed");
416 static RC encode_value(
const Value &val, bytes &dst)
419 switch (val.attr_type()) {
421 if (OB_FAIL(OrderedCode::append(dst, (int64_t)val.
get_int()))) {
422 LOG_WARN(
"append failed");
425 case AttrType::FLOATS:
426 if (OB_FAIL(OrderedCode::append(dst, (
double)val.get_float()))) {
427 LOG_WARN(
"append failed");
430 case AttrType::CHARS:
431 if (OB_FAIL(OrderedCode::append(dst, val.get_string()))) {
432 LOG_WARN(
"append failed");
435 default:
return RC::INVALID_ARGUMENT;
440 static RC encode_int(int64_t val, bytes &dst)
443 if (OB_FAIL(OrderedCode::append(dst, val))) {
444 LOG_WARN(
"append failed");
449 static RC decode(bytes &encoded_key, int64_t &table_id)
452 span<byte_t> sp(encoded_key);
454 if (OB_FAIL(OrderedCode::parse(sp, OrderedCode::increasing, table_prefix))) {
455 LOG_WARN(
"parse failed");
457 }
else if (OB_FAIL(OrderedCode::parse(sp, OrderedCode::increasing, table_id))) {
458 LOG_WARN(
"parse failed");
464 static constexpr const char *table_prefix =
"t";
465 static constexpr const char *rowkey_prefix =
"r";
属性的值
Definition: value.h:30
int get_int() const
Definition: value.cpp:234