The data type of C ++ leveldb slice is not necessarily a string. It can also be binary.

Source: Internet
Author: User

First, list the references:

See this translation: http://duanple.blog.163.com/blog/static/70971767201171705113636/

Leveldb source code analysis 4 server: http://www.kuqin.com/database/20110919/265041.html

Slice. H source: http://www.oschina.net/code/explore/leveldb/include/leveldb/slice.h

 
1Compared with the returned string, the overhead of the returned slice is much smaller (there is no copy, there is no actual data in the slice, there is only a pointer to the data, and the overhead is low ).2Leveldb allows key and value inclusion'\ 0', Cannot return a string of the C style ending with null.

For the above two points, the second point can include \ 0 ?? This is strange. The string is determined based on \ 0. If binary is used, there will be too many \ 0 values.

So we can see the source code analysis in the slice constructor.

   //  Create an empty slice. Slice (): Data _( "" ), Size _( 0  ){}  //  Create a slice that refers to data [0, n-1]. Slice ( Const   Char * Data, size_t N): Data _ (data), size _ (n ){}  //  Create a slice that refers to the contents of "S" Slice ( Const STD :: String & S): Data _ (S. Data (), size _ (S. Size ()){} //  Create a slice that refers to s [0, strlen (S)-1] Slice ( Const   Char * S): Data _ (s), size _ (strlen (s )){}

The second one can specify an N and a char *.

Our binary is generally char * type. As long as we can specify N, we can actually put the binary into slice and then put it into leveldb.

Maybe I should write an example as soon as possible to verify everything ......

 

 

Todo: leveldb uses slice to save binary data.

 

 

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.