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.