下了leveldb的源码,感觉十分给力,代码质量都非常高,非常值得学习。
首先把它用起来先,没什么好说的,直接上代码吧。
#include <leveldb/db.h> #include <string> #include <iostream> using namespace std; void PrintStatus(leveldb::Status& status) { if (status.ok()) cout << "OK!" << endl; else cout << "NO!" << endl; } int main() { leveldb::DB* db; leveldb::Options options; options.create_if_missing = true; leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db); assert(status.ok()); string cmd, key, value; while (cin >> cmd) { if (cmd == "get") { cin >> key; status = db->Get(leveldb::ReadOptions(), key, &value); PrintStatus(status); if (status.ok()) cout << "value = " << value << endl; } else if (cmd == "put") { cin >> key >> value; status = db->Put(leveldb::WriteOptions(), key, value); PrintStatus(status); } else if (cmd == "del") { cin >> key; status = db->Delete(leveldb::WriteOptions(), key); PrintStatus(status); } else { cout << "No defined operation!" << endl; } cout << "=========================================" << endl; } return 0; }