Leveldb source code analysis-2

Source: Internet
Author: User
Document directory
  • 3.1 decode
  • 3.2 decode
3 int Coding

As mentioned in the previous conventions, leveldb uses many varint-type encodings, such as the various keys that will be involved later. The encoding and decoding functions include varint and fixedint. The int32 and int64 operations are similar.

3.1 decode

The first is the fixedint encoding. The code is simple and clear.

Void encodefixed32 (char * Buf, uint32_t value)

{

If (Port: klittleendian ){

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;

}

}

The following is the varint encoding, int32 and int64 formats. The Code is as follows. The valid bit is 7bit. Therefore, uint32 is separated by 7bit. When the value of unsigned char is assigned, it is automatically truncated if the value exceeds 0xff, therefore, you can directly use * (PTR ++) = v | B. You do not need to use (v | B) and 0xff for & Operations.

Char * encodevarint32 (char * DST, uint32_t V)

{

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 );

}

Char * encodevarint64 (char * DST, uint64_t v ){//
For uint64, direct loop

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 );

Returnreinterpret_cast <char *> (PTR );

}

3.2 decode

Decode, operation, code of fixed INT:

Inline uint32_t decodefixed32 (const char * PTR)

{

If (Port: klittleendian ){

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 <unsignedchar> (PTR [2]) <16)

| (Static_cast <uint32_t> (static_cast <unsigned char> (PTR [3]) <24 ));

}

}

Let's take a look at the decoding of varint. It is very simple to read 1 byte in sequence until the end of the byte with the highest bit of 0, take 7 bit low, and combine (<7) shift operation into Int. Check the Code:

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;

Returnreinterpret_cast <const char *> (P );

}

}

Return NULL;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.