[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]