leveldb

LevelDB

  LevelDB is a fast key-value storage library that provides an ordered mapping from string keys to string values.

  • Keys and values are arbitrary byte arrays.
  • Data is stored sorted by key.
  • Callers can provide a custom comparison function to override the sort order. 自定义Comparator。
  • The basic operations are Put(key,value)Get(key)Delete(key).
  • Multiple changes can be made in one atomic batch. 批处理,原子化,高效。
  • Users can create a transient snapshot to get a consistent view of data.
  • Forward and backward iteration is supported over the data.
  • Data is automatically compressed using the Snappy compression library.
  • External activity (file system operations etc.) is relayed through a virtual interface so users can customize the operating system interactions.

性能

  LevelDB性能非常突出,官方网站报道其随机写性能达到40万条记录每秒,而随机读性能达到6万条记录每秒。总体来说,LevelDb的写操作要大大快于读操作,而顺序读写操作则大大快于随机读写操作。

Limitations

  • This is not a SQL database. It does not have a relational data model, it does not support SQL queries, and it has no support for indexes. 不支持结SQL查询。
  • Only a single process (possibly multi-threaded) can access a particular database at a time. 一个数据库只能被单进程访问。
  • There is no client-server support builtin to the library. An application that needs such support will have to wrap their own server around the library.  无网络模型。

[Synchronous Writes]

  By default, each write to leveldb is asynchronous: it returns after pushing the write from the process into the operating system.

默认所有到leveldb的写操作都是异步, 在把数据从进程交给操作系统后该写操作就返回.

  The transfer from operating system memory to the underlying persistent storage happens asynchronously.

  从操作系统到下层的持久存储是一个异步的过程.

  The sync flag can be turned on for a particular write to make the write operation not return until the data being written has been pushed all the way to persistent storage.

  sync标致可打开, 以使得写操作到直至把数据写入到持久存储才返回.

  leveldb

  The downside of asynchronous writes is that a crash of the machine may cause the last few updates to be lost.

  异步写操作不利的一面是, 机器的崩溃会导致最后几个操作丢失.

  Note that a crash of just the writing process will not cause any loss since even when sync is false, an update is pushed from the process memory into the operating system before it is considered done.

  当sync为false时,写进程的崩溃不会导致异步写操作丢失, 国为数据已经被写到了操作系统.

  Asynchronous writes can often be used safely. For example, when loading a large amount of data into the database you can handle lost updates by restarting the bulk load after a crash. A hybrid scheme is also possible where every Nth write is synchronous, and in the event of a crash, the bulk load is restarted just after the last synchronous write finished by the previous run.

[Concurrency]

  A database may only be opened by one process at a time. The leveldb implementation acquires a lock from the operating system to prevent misuse. Within a single process, the same leveldb::DB object may be safely shared by multiple concurrent threads. I.e., different threads may write into or fetch iterators or call Get on the same database without any external synchronization (the leveldb implementation will automatically do the required synchronization). However other objects (like Iterator and WriteBatch) may require external synchronization. If two threads share such an object, they must protect access to it using their own locking protocol.

[Iteration]

  The following example demonstrates how to print all key,value pairs in a database.

  leveldb

   The following variation shows how to process just the keys in the range [start,limit):

   leveldb

   You can also process entries in reverse order. (Caveat: reverse iteration may be somewhat slower than forward iteration.)
  leveldb
[Snapshots]
  Snapshots provide consistent read-only views over the entire state of the key-value store. ReadOptions::snapshot may be non-NULL to indicate that a read should operate on a particular version of the DB state. IReadOptions::snapshot is NULL, the read will operate on an implicit snapshot of the current state.
  如果ReadOptions的snapshot属性非空, 意味着操作会在该版本上发生, 如果为NULL, 操作会在一个隐含的版本上发生. 所以无论怎样, 使用iterator都会得到一个一致的版本, 不用担心在iteration过程中会插入或是删除数据.

  Snapshots are created by the DB::GetSnapshot() method:

  leveldb

  Note that when a snapshot is no longer needed, it should be released using the DB::ReleaseSnapshot interface. This allows the implementation to get rid of state that was being maintained just to support reading as of that snapshot.

上一篇:Java基础知识强化之集合框架笔记39:Set集合之HashSet存储字符串并遍历


下一篇:Mysql数据库登录问题:Your password has expired.