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:

  

Backwards compatibility

  The result of the comparator's Name method is attached to the database when it is created, and is checked on every subsequent database open. If the name changes, theleveldb::DB::Open call will fail.   

  Therefore, change the name if and only if the new key format and comparison function are incompatible with existing databases, and it is ok to discard the contents of all existing databases.

  You can however still gradually evolve your key format over time with a little bit of pre-planning. For example, you could store a version number at the end of each key (one byte should suffice for most uses). When you wish to switch to a new key format (e.g., adding an optional third part to the keys processed by TwoPartComparator), (a) keep the same comparator name (b) increment the version number for new keys (c) change the comparator function so it uses the version numbers found in the keys to decide how to interpret them.

  Name变了,则无法打开老的数据库。  

链接: http://leveldb.googlecode.com/svn/trunk/doc/index.html

原文地址:https://www.cnblogs.com/tekkaman/p/3535615.html