作者: JeffDean,SanjayGhemawat 原文: 譯者: phylips@bmy2011-8-16 譯文: 打開一個數(shù)據(jù)庫 一個 LevelDB 數(shù)據(jù)庫有一個文件系統(tǒng)目錄名稱與之關(guān)聯(lián)。該數(shù)據(jù)庫的所有內(nèi)容都存儲在該目錄下。下面的例子展示了如何打開一個數(shù)據(jù)庫,或者如何在必要的時候創(chuàng)建一個
作者:Jeff Dean, Sanjay Ghemawat
原文:
譯者:phylips@bmy 2011-8-16
譯文:
打開一個數(shù)據(jù)庫一個LevelDB數(shù)據(jù)庫有一個文件系統(tǒng)目錄名稱與之關(guān)聯(lián)。該數(shù)據(jù)庫的所有內(nèi)容都存儲在該目錄下。下面的例子展示了如何打開一個數(shù)據(jù)庫,或者如何在必要的時候創(chuàng)建一個:
#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());
...
如果你想在數(shù)據(jù)庫已經(jīng)存在的情況下,讓上面的代碼產(chǎn)生一個錯誤,需要再Open調(diào)用之前加入如下一行:
options.error_if_exists = true;
狀態(tài)(status)leveldb::Status s = ...;
if (!s.ok()) cerr << s.ToString() << endl;
關(guān)閉數(shù)據(jù)庫在完成一個數(shù)據(jù)庫的處理之后,直接刪除該數(shù)據(jù)庫對象即可。
... 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, ...);
并發(fā) 迭代下面的例子用來說明如何打印出數(shù)據(jù)庫中的所有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 { }
};
現(xiàn)在使用定制的比較器,創(chuàng)建一個數(shù)據(jù)庫:
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里的默認(rèn)值來對性能進(jìn)行調(diào)整優(yōu)化。
塊大小 壓縮聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com