LevelDB存储编码
LevelDB 存储编码
flyfish 2015-10-10
对于字节存储分⼤端⼩端字节序还是⼩端⼩端字节序
LevelDB使⽤的是⼩端字节序存储,低位字节排放在内存的低地址端,⾼位字节排放在内存的⾼地址端。 编码分为变长的VarInt和固定⼤⼩FixedInt两种,每种分32位和64位。
固定⼤⼩的FixedInt32和FixedInt64的编码 ⽂件
void EncodeFixed32(char* buf, uint32_t value) {
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(buf, &value, sizeof(value));
#else
buf[0] = value & 0xff;
buf[1] = (value >> 8) & 0xff;
buf[2] = (value >> 16) & 0xff;
buf[3] = (value >> 24) & 0xff;
#endif
}
void EncodeFixed64(char* buf, uint64_t value) {
#if __BYTE_ORDER == __LITTLE_ENDIAN
memcpy(buf, &value, sizeof(value));
#else
buf[0] = value & 0xff;
buf[1] = (value >> 8) & 0xff;
buf[2] = (value >> 16) & 0xff;
buf[3] = (value >> 24) & 0xff;
buf[4] = (value >> 32) & 0xff;
buf[5] = (value >> 40) & 0xff;
buf[6] = (value >> 48) & 0xff;
buf[7] = (value >> 56) & 0xff;
#endif
}
FixedInt解码 ⽂件coding.h
inline uint32_t DecodeFixed32(const char* ptr) {
if (port::kLittleEndian) {
/
/ Load the raw bytes
uint32_t result;
memcpy(&result, ptr, sizeof(result));  // gcc optimizes this to a plain load
return result;
} else {
return ((static_cast<uint32_t>(static_cast<unsigned char>(ptr[0])))
| (static_cast<uint32_t>(static_cast<unsigned char>(ptr[1])) << 8)
| (static_cast<uint32_t>(static_cast<unsigned char>(ptr[2])) << 16)
| (static_cast<uint32_t>(static_cast<unsigned char>(ptr[3])) << 24));
}
}
inline uint64_t DecodeFixed64(const char* ptr) {
if (port::kLittleEndian) {leveldb使用
// Load the raw bytes
uint64_t result;
memcpy(&result, ptr, sizeof(result));  // gcc optimizes this to a plain load
return result;
} else {
uint64_t lo = DecodeFixed32(ptr);
uint64_t hi = DecodeFixed32(ptr + 4);
return (hi << 32) | lo;
}
}
不定⼤⼩的VarInt编码
int32类型的数字,⼀般需要4个byte来表⽰,int64类型的数字,⼀般需要8个byte来表⽰;
Varint是⼀种紧凑的表⽰数字的⽅法。它⽤⼀个或多个字节来表⽰⼀个数字,值越⼩的数字使⽤越少的字节数。数值⼩的⽤⼀个字节就可以存储。
Varint 中的每个 byte 的最⾼位 bit 是标识位有特殊的含义。 如果该位为 1,表⽰后续的 byte 也是该数字的⼀部分,
如果该位为 0,则结束。 其他的 7 个 bit 都⽤来表⽰数字。
VarInt32编码
看如下代码处理1到5个字节的情况,如果数特别⼤,可能需要5个字节才能存放。
char* EncodeVarint32(char* dst, uint32_t v) {
// Operate on characters as unsigneds
unsigned char* ptr = reinterpret_cast<unsigned char*>(dst);
static const int B = 128;
if (v < (1<<7)) {
*(ptr++) = v;
} else if (v < (1<<14)) {
*(ptr++) = v | B;
*(ptr++) = v>>7;
} else if (v < (1<<21)) {
*(ptr++) = v | B;
*(ptr++) = (v>>7) | B;
*(ptr++) = v>>14;
} else if (v < (1<<28)) {
*(ptr++) = v | B;
*(ptr++) = (v>>7) | B;
*(ptr++) = (v>>14) | B;
*(ptr++) = v>>21;
} else {
*(ptr++) = v | B;
*(ptr++) = (v>>7) | B;
*(ptr++) = (v>>14) | B;
*(ptr++) = (v>>21) | B;
*(ptr++) = v>>28;
}
return reinterpret_cast<char*>(ptr);
}
正好⽤EncodeVarint32代码的5个分⽀解释下⾯EncodeVarint64的while,该while能转换成10个分⽀。
char* EncodeVarint64(char* dst, uint64_t v) {
static const int B = 128;
unsigned char* ptr = reinterpret_cast<unsigned char*>(dst);
while (v >= B) {
*(ptr++) = (v & (B-1)) | B;
v >>= 7;
}
*(ptr++) = static_cast<unsigned char>(v);
return reinterpret_cast<char*>(ptr);
}
**varInt32的解码** ⽂件coding.h
inline const char* GetVarint32Ptr(const char* p,
const char* limit,
uint32_t* value) {
if (p < limit) {
uint32_t result = *(reinterpret_cast<const unsigned char*>(p));
if ((result & 128) == 0) {
*value = result;
return p + 1;
}
}
return GetVarint32PtrFallback(p, limit, value);
}
<⽂件
const char* GetVarint32PtrFallback(const char* p,
const char* limit,
uint32_t* value) {
uint32_t result = 0;
for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
uint32_t byte = *(reinterpret_cast<const unsigned char*>(p));
p++;
if (byte & 128) {
// More bytes are present
result |= ((byte & 127) << shift);
} else {
result |= (byte << shift);
*value = result;
return reinterpret_cast<const char*>(p);
}
}
return NULL;
}
varInt64的解码
const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
uint64_t result = 0;
for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) {
uint64_t byte = *(reinterpret_cast<const unsigned char*>(p));
p++;
if (byte & 128) {
// More bytes are present
result |= ((byte & 127) << shift);
} else {
result |= (byte << shift);
*value = result;
return reinterpret_cast<const char*>(p);
}
}
return NULL;
}
举例说明 Varint编码和解码过程
1000 为例
编码过程
1000 ⼆进制为 1111101000
取右边7bit,1101000
剩下的是111
⼩端存储要转换位置
字节1:01101000
字节2: 00000111
每个字节位为标识1,还有后续。所以字节1最⾼是1,字节2最⾼位为0
编码结果是: 11101000 00000111
解码过程
11101000 00000111
⾼位1有后续,与下⼀个字节⼀同表⽰⼀个数值
1101000 00000111
字节交换位置结果是: 111 1101000