作者: JeffDean,SanjayGhemawat 原文: 译者: phylips@bmy2011-8-16 译文: 打开一个数据库 一个 LevelDB 数据库有一个文件系统目录名称与之关联。该数据库的所有内容都存储在该目录下。下面的例子展示了如何打开一个数据库,或者如何在必要的时候创建一个
作者:Jeff Dean, Sanjay Ghemawat
原文:
译者:phylips@bmy 2011-8-16
译文:
打开一个数据库一个LevelDB数据库有一个文件系统目录名称与之关联。该数据库的所有内容都存储在该目录下。下面的例子展示了如何打开一个数据库,或者如何在必要的时候创建一个:
#include
#include "leveldb/db.h"
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
assert(status.ok());
...
如果你想在数据库已经存在的情况下,让上面的代码产生一个错误,需要再Open调用之前加入如下一行:
options.error_if_exists = true;
状态(status)leveldb::Status s = ...;
if (!s.ok()) cerr
关闭数据库在完成一个数据库的处理之后,直接删除该数据库对象即可。
... open the db as described above ...
... do something with db ...
delete db;
读操作与写操作std::string value;
leveldb::Status s = db->Get(leveldb::ReadOptions(), key1, &value);
if (s.ok()) s = db->Put(leveldb::WriteOptions(), key2, value);
if (s.ok()) s = db->Delete(leveldb::WriteOptions(), key1);
原子性更新#include "leveldb/write_batch.h"
...
std::string value;
leveldb::Status s = db->Get(leveldb::ReadOptions(), key1, &value);
if (s.ok()) {
leveldb::WriteBatch batch;
batch.Delete(key1);
batch.Put(key2, value);
s = db->Write(leveldb::WriteOptions(), &batch);
}
WriteBatch 同步性的写操作leveldb::WriteOptions write_options;
write_options.sync = true;
db->Put(write_options, ...);
并发 迭代下面的例子用来说明如何打印出数据库中的所有key value对。
leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next()) {
cout key().ToString() value().ToString()
}
assert(it->status().ok());
// Check for any errors found during the scan
delete it;
for (it->Seek(start);
it->Valid() && it->key().ToString()
it->Next()) {
...
}
for (it->SeekToLast(); it->Valid(); it->Prev()) {
...
}
Snapshotsleveldb::ReadOptions options;
options.snapshot = db->GetSnapshot();
... apply some updates to db ...
leveldb::Iterator* iter = db->NewIterator(options);
... read using iter to view the state when the snapshot was created ...
delete iter;
db->ReleaseSnapshot(options.snapshot);
leveldb::Snapshot* snapshot;
leveldb::WriteOptions write_options;
write_options.post_write_snapshot = &snapshot;
leveldb::Status status = db->Write(write_options, ...);
... perform other mutations to db ...
leveldb::ReadOptions read_options;
read_options.snapshot = snapshot;
leveldb::Iterator* iter = db->NewIterator(read_options);
... read as of the state just after the Write call returned ...
delete iter;
db->ReleaseSnapshot(snapshot);
Sliceleveldb::Slice s1 = "hello";
std::string str("world");
leveldb::Slice s2 = str;
std::string str = s1.ToString();
assert(str == std::string("hello"));
。比如下面代码的就是有问题的:
leveldb::Slice slice;
if (...) {
std::string str = ...;
slice = str;
}
Use(slice);
比较器class TwoPartComparator : public leveldb::Comparator {
public:
// Three-way comparison function:
// if a
// if a > b: positive result
// else: zero result
int Compare(const leveldb::Slice& a, const leveldb::Slice& b) const {
int a1, a2, b1, b2;
ParseKey(a, &a1, &a2);
ParseKey(b, &b1, &b2);
if (a1
if (a1 > b1) return +1;
if (a2
if (a2 > b2) return +1;
return 0;
}
// Ignore the following methods for now:
const char* Name() const { return "TwoPartComparator"; }
void FindShortestSeparator(std::string*, const leveldb::Slice&) const { }
void FindShortSuccessor(std::string*) const { }
};
现在使用定制的比较器,创建一个数据库:
TwoPartComparator cmp;
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
options.comparator = &cmp;
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
向后兼容(Backwards compatibility)
性能
可以通过改变include/leveldb/options.h里的默认值来对性能进行调整优化。
块大小 压缩