zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    2018年3月至4月小结
    前端面试中,经常看到垂直居中与水平居中,实际排版用的多吗?
    Hbuilder配置识别逍遥安卓模拟器
    php静态变量与方法与phar的使用
    切面反射获取方法
    Spring:源码解读Spring IOC原理
    怎样批量提取JPG照片的文件名
    如何1秒批量提取电脑文件夹中的所有文件、文件夹名字到txt/excel
    用powermock 方法中new对象
    springboot单元测试自动回滚:@Transactional
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3535615.html
Copyright © 2011-2022 走看看