-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleveldb_test.cpp
More file actions
74 lines (65 loc) · 2.46 KB
/
leveldb_test.cpp
File metadata and controls
74 lines (65 loc) · 2.46 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// sudo dnf install leveldb leveldb-devel -y
//
// g++ -O3 leveldb_test.cpp -o leveldb_test -lleveldb -lpthread
//
// ./leveldb_test
#include "leveldb/db.h"
#include <iostream>
#include <map>
#include <string>
int main() {
leveldb::DB *db;
leveldb::Options options;
options.create_if_missing = true;
// 1. Open the database
leveldb::Status s = leveldb::DB::Open(options, "./leveldb_test.db", &db);
if (!s.ok()) {
std::cerr << "Open failed: " << s.ToString() << std::endl;
return 1;
}
// 2. Prepare the data
std::map<std::string, std::string> biblical_figures = {
{"Othniel",
"The first judge; he delivered Israel from the king of Mesopotamia."},
{"Deborah",
"A prophetess who, with Barak, defeated the Canaanite general Sisera."},
{"Gideon", "Destroyed the altar of Baal and led 300 men to victory over "
"the Midianites."},
{"Jephthah", "The son of a harlot who led Israel against the Ammonites "
"after a rash vow."},
{"Samson", "Known for his immense strength and his struggle against the "
"Philistines."},
{"Sarah", "The wife of Abraham and mother of Isaac in her old age."},
{"Ruth",
"A Moabite woman who showed great loyalty to her mother-in-law Naomi."},
{"Esther",
"A Jewish queen of Persia who saved her people from a massacre."},
{"Hannah",
"The mother of the prophet Samuel, known for her persistent prayer."},
{"Rahab", "A woman of Jericho who assisted the Israelite spies."}};
// 3. Insert data into LevelDB
for (auto const &[name, bio] : biblical_figures) {
s = db->Put(leveldb::WriteOptions(), name, bio);
if (!s.ok())
std::cerr << "Error writing " << name << std::endl;
}
// 4. Look up a specific figure
std::string value;
std::string search_key = "Deborah";
s = db->Get(leveldb::ReadOptions(), search_key, &value);
if (s.ok()) {
std::cout << "--- Single Lookup ---" << std::endl;
std::cout << search_key << ": " << value << "\n" << std::endl;
}
// 5. Iterate through all figures (Sorted Order)
std::cout << "--- All figures (Sorted by Key) ---" << std::endl;
leveldb::Iterator *it = db->NewIterator(leveldb::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next()) {
std::cout << "Figure: " << it->key().ToString() << "\n";
std::cout << "Bio: " << it->value().ToString() << "\n" << std::endl;
}
// 6. Cleanup
delete it;
delete db;
return 0;
}