Leveldb Advanced
[Slice]
The return value of
the it->key()
and it->value()
is
a simple structure that contains a length and a pointer to an external byte
array. Returning a Slice
is a cheaper
alternative to returning a std::string
since
we do not need to copy potentially large keys and values.
C++ strings and null-terminated C-style strings can be easily converted to a Slice:
A Slice can be easily converted back to a C++ string:
Be careful when using Slices since it is up to the caller to ensure that the external byte array into which the Slice points remains live while the Slice is in use. For example, the following is buggy:
[Comparators]
The default ordering function for key, which orders bytes
lexicographically. You can however supply a custom comparator when opening a
database. For example, suppose each database key consists of two numbers and we
should sort by the first number, breaking ties by the second number. First,
define a proper subclass
ofleveldb::Comparator
that expresses these
rules:
Now create a database using this custom comparator:
[Filters]
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。