【LevelDB Cache】
The contents of the database are stored in a set of files in the filesystem and each file stores a sequence of compressed blocks. If options.cache
is non-NULL, it is used to cache frequently used uncompressed block contents.
#include "leveldb/cache.h" leveldb::Options options; options.cache = leveldb::NewLRUCache(100 * 1048576); // 100MB cache leveldb::DB* db; leveldb::DB::Open(options, name, &db); ... use the db ... delete db delete options.cache;
Note that the cache holds uncompressed data, and therefore it should be sized according to application level data sizes, without any reduction from compression. (Caching of compressed blocks is left to the operating system buffer cache, or any custom Env
implementation provided by the client.)
When performing a bulk read, the application may wish to disable caching so that the data processed by the bulk read does not end up displacing most of the cached contents. A per-iterator option can be used to achieve this:
leveldb::ReadOptions options; options.fill_cache = false; leveldb::Iterator* it = db->NewIterator(options); for (it->SeekToFirst(); it->Valid(); it->Next()) { ... }
【Key Layout】
Note that the unit of disk transfer and caching is a block. Adjacent keys (according to the database sort order) will usually be placed in the same block. Therefore the application can improve its performance by placing keys that are accessed together near each other and placing infrequently used keys in a separate region of the key space.
把可能一起访问的key加上相同的前缀,以使他们能在同一block上,以提高性能。
For example, suppose we are implementing a simple file system on top of leveldb
. The types of entries we might wish to store are:
filename -> permission-bits, length, list of file_block_ids file_block_id -> data
We might want to prefix filename
keys with one letter (say '/') and the file_block_id
keys with a different letter (say '0') so that scans over just the metadata do not force us to fetch and cache bulky file contents.
参考:http://htmlpreview.github.io/?https://github.com/google/leveldb/blob/master/doc/index.html