/
art_key.cpp
115 lines (105 loc) · 3.3 KB
/
art_key.cpp
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
#include "execution/index/art/art_key.hpp"
//! these are optimized and assume a particular byte order
#define BSWAP8(x) ((uint8_t)((((uint8_t)(x)&0xf0) >> 4) | (((uint8_t)(x)&0x0f) << 4)))
#define BSWAP16(x) ((uint16_t)((((uint16_t)(x)&0xff00) >> 8) | (((uint16_t)(x)&0x00ff) << 8)))
#define BSWAP32(x) \
((uint32_t)((((uint32_t)(x)&0xff000000) >> 24) | (((uint32_t)(x)&0x00ff0000) >> 8) | \
(((uint32_t)(x)&0x0000ff00) << 8) | (((uint32_t)(x)&0x000000ff) << 24)))
#define BSWAP64(x) \
((uint64_t)((((uint64_t)(x)&0xff00000000000000ull) >> 56) | (((uint64_t)(x)&0x00ff000000000000ull) >> 40) | \
(((uint64_t)(x)&0x0000ff0000000000ull) >> 24) | (((uint64_t)(x)&0x000000ff00000000ull) >> 8) | \
(((uint64_t)(x)&0x00000000ff000000ull) << 8) | (((uint64_t)(x)&0x0000000000ff0000ull) << 24) | \
(((uint64_t)(x)&0x000000000000ff00ull) << 40) | (((uint64_t)(x)&0x00000000000000ffull) << 56)))
KeyLen Key::getKeyLen() const {
return len;
}
Key::Key(Key &&key) {
len = key.len;
// if (len > stackLen) {
data = move(key.data);
// } else {
// memcpy(stackKey.get(), key.stackKey.get(), key.len);
// data = move(stackKey);
// }
}
void Key::set(const char bytes[], const uint64_t length) {
// if (length <= stackLen) {
// memcpy(stackKey.get(), bytes, length);
// data = move(stackKey);
// } else {
data = unique_ptr<uint8_t[]>(new uint8_t[length]);
memcpy(data.get(), bytes, length);
// }
len = length;
}
void Key::operator=(const char key[]) {
len = strlen(key);
// if (len <= stackLen) {
// memcpy(stackKey.get(), key, len);
// data = move(stackKey);
// } else {
data = unique_ptr<uint8_t[]>(new uint8_t[len]);
memcpy(data.get(), key, len);
// }
}
void Key::setKeyLen(KeyLen newLen) {
if (len == newLen)
return;
len = newLen;
// if (len > stackLen) {
data = unique_ptr<uint8_t[]>(new uint8_t[len]);
// } else {
// data = move(stackKey);
// }
}
void Key::convert_to_binary_comparable(bool isLittleEndian, TypeId type, uintptr_t tid) {
// data = move(stackKey);
switch (type) {
case TypeId::BOOLEAN:
len = 1;
if (isLittleEndian) {
data[0] = BSWAP8(tid);
} else {
data[0] = tid;
}
break;
case TypeId::TINYINT:
len = 1;
if (isLittleEndian) {
data[0] = BSWAP8(tid);
} else {
data[0] = tid;
}
data[0] = flipSign(data[0]);
break;
case TypeId::SMALLINT:
len = 2;
if (isLittleEndian) {
reinterpret_cast<uint16_t *>(data.get())[0] = BSWAP16(tid);
} else {
reinterpret_cast<uint16_t *>(data.get())[0] = tid;
}
data[0] = flipSign(data[0]);
break;
case TypeId::INTEGER:
len = 4;
if (isLittleEndian) {
reinterpret_cast<uint32_t *>(data.get())[0] = BSWAP32(tid);
} else {
reinterpret_cast<uint32_t *>(data.get())[0] = tid;
}
data[0] = flipSign(data[0]);
break;
case TypeId::BIGINT:
len = 8;
if (isLittleEndian) {
reinterpret_cast<uint64_t *>(data.get())[0] = BSWAP64(tid);
} else {
reinterpret_cast<uint64_t *>(data.get())[0] = tid;
}
data[0] = flipSign(data[0]);
break;
default:
throw NotImplementedException("Unimplemented type for ART index");
}
}