#include <cassert>
|
|
#include <iostream>
|
|
#include "leveldb/db.h"
|
|
#include "db/db_impl.h"
|
|
|
|
int main() {
|
|
leveldb::DB* db;
|
|
leveldb::Options options;
|
|
options.create_if_missing = true;
|
|
options.kvSepType = leveldb::kVSepBeforeMem;
|
|
|
|
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
|
|
if (!status.ok()) {
|
|
std::cerr << "Failed to open DB: " << status.ToString() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
// 使用 dynamic_cast 将基类指针转换为 DBImpl
|
|
auto* dbimpl = static_cast<leveldb::DBImpl*>(db);
|
|
if (dbimpl == nullptr) {
|
|
std::cerr << "Failed to cast to DBImpl\n";
|
|
delete db;
|
|
return 1;
|
|
}
|
|
|
|
status = dbimpl->Put(leveldb::WriteOptions(), "key1", "val1");
|
|
if (status.ok()) {
|
|
std::string val;
|
|
status = dbimpl->Get(leveldb::ReadOptions(), "key1", &val);
|
|
std::cout << "Find value of 'key1' From db: " << val << "\n";
|
|
}
|
|
|
|
if (status.ok()) {
|
|
std::string val;
|
|
dbimpl->Delete(leveldb::WriteOptions(), "key1");
|
|
status = dbimpl->Get(leveldb::ReadOptions(), "key1", &val);
|
|
// Not found.
|
|
std::cout << status.ToString() << '\n';
|
|
}
|
|
|
|
delete db;
|
|
return 0;
|
|
}
|