-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
60 lines (52 loc) · 1.65 KB
/
Copy pathclient.cpp
File metadata and controls
60 lines (52 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
#include "leveldb/db.h"
using namespace leveldb;
int main() {
DB *db;
Options options;
Status status = DB::Open(options, "./client.db", &db);
if (!status.ok()) {
std::cerr << "Unable to open/create test database './client.db'" << std::endl;
std::cerr << status.ToString() << std::endl;
return -1;
}
std::string key;
std::string value;
std::string cmd;
while (true) {
std::cout << "leveldb> ";
std::cin >> cmd;
if (cmd == "set") {
std::cin >> key >> value;
status = db->Put(WriteOptions(), key, value);
if (status.ok()) {
std::cout << "OK" << std::endl;
} else {
std::cout << "Error setting value: " << status.ToString() << std::endl;
}
} else if (cmd == "get") {
std::cin >> key;
status = db->Get(ReadOptions(), key, &value);
if (status.ok()) {
std::cout << value << std::endl;
} else {
std::cout << "Not found" << std::endl;
}
} else if (cmd == "del") {
std::cin >> key;
status = db->Delete(WriteOptions(), key);
if (status.ok()) {
std::cout << "OK" << std::endl;
} else {
std::cout << "Error deleting key: " << status.ToString() << std::endl;
}
} else if (cmd == "exit") {
break;
} else {
std::cout << "Unknown command. Supported commands are: set, get, del, exit" << std::endl;
}
}
delete db;
return 0;
}