-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsample.cpp
More file actions
57 lines (43 loc) · 1.41 KB
/
sample.cpp
File metadata and controls
57 lines (43 loc) · 1.41 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
#include <iostream>
#include <sstream>
#include <string>
#include "leveldb/db.h"
using namespace std;
int main(int argc, char** argv)
{
// Set up database connection information and open database
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "./testdb", &db);
if (false == status.ok())
{
cerr << "Unable to open/create test database './testdb'" << endl;
cerr << status.ToString() << endl;
return -1;
}
// Add 256 values to the database
leveldb::WriteOptions writeOptions;
for (unsigned int i = 0; i < 256; ++i)
{
ostringstream keyStream;
keyStream << "Key" << i;
ostringstream valueStream;
valueStream << "Test data value: " << i;
db->Put(writeOptions, keyStream.str(), valueStream.str());
}
// Iterate over each item in the database and print them
leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next())
{
cout << it->key().ToString() << " : " << it->value().ToString() << endl;
}
if (false == it->status().ok())
{
cerr << "An error was found during the scan" << endl;
cerr << it->status().ToString() << endl;
}
delete it;
// Close the database
delete db;
}