LevelDB:一个快速轻量级的key-value存储库(译)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了LevelDB:一个快速轻量级的key-value存储库(译),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4143字,纯文字阅读大概需要6分钟。
内容图文
![LevelDB:一个快速轻量级的key-value存储库(译)](/upload/InfoBanner/zyjiaocheng/546/a81218745380432ba5b2c4e04fd20b66.jpg)
作者: 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 << s.ToString() << endl;
关闭数据库在完成一个数据库的处理之后,直接删除该数据库对象即可。
... 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 << it->key().ToString() << ": " << it->value().ToString() << endl;
}
assert(it->status().ok());
// Check for any errors found during the scan
delete it;
for (it->Seek(start);
it->Valid() && it->key().ToString() < limit;
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 < b: negative result
// 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 < b1) return -1;
if (a1 > b1) return +1;
if (a2 < b2) return -1;
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里的默认值来对性能进行调整优化。
块大小 压缩内容总结
以上是互联网集市为您收集整理的LevelDB:一个快速轻量级的key-value存储库(译)全部内容,希望文章能够帮你解决LevelDB:一个快速轻量级的key-value存储库(译)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。