小组成员: 曹可心-10223903406 朴祉燕-10224602413
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

43 lines
1.2 KiB

#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;
}