-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_demo.cpp
108 lines (88 loc) · 2.25 KB
/
simple_demo.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdlib.h>
#include <leveldb/db.h>
#include "util.h"
leveldb::DB *g_db;
std::string g_data_dir = "./data/simple_demo";
int init() {
leveldb::Options init_options;
init_options.create_if_missing = true;
init_options.write_buffer_size = 1;
leveldb::Status s = leveldb::DB::Open(init_options, g_data_dir.c_str(), &g_db);
if (!s.ok()) {
warning("db open failed:%s\n", s.ToString().c_str());
return -1;
}
return 0;
}
void write_random() {
std::string key = "box";
string_random_append(&key, 1, 5);
leveldb::Slice value = leveldb::Slice(key.data(), key.size());
leveldb::Status s = g_db->Put(leveldb::WriteOptions(),
leveldb::Slice(key.data(), key.size()), value);
if (!s.ok()) {
warning("put failed: %s\n", s.ToString().c_str());
} else {
notice("put ok. key:%s, key_len:%lu, key_len:%lu\n",
key.c_str(), key.size(), key.size());
}
}
void multi_write(int n) {
for (int i = 0; i < n; ++i) {
write_random();
}
}
void write(std::string key) {
leveldb::Slice value = leveldb::Slice(key.data(), key.size());
leveldb::Status s = g_db->Put(leveldb::WriteOptions(),
leveldb::Slice(key.data(), key.size()), value);
if (!s.ok()) {
warning("put failed: %s\n", s.ToString().c_str());
} else {
notice("put ok. key:%s\n", key.c_str());
}
}
void read(std::string key) {
std::string value;
leveldb::Status s = g_db->Get(leveldb::ReadOptions(),
leveldb::Slice(key.data(), key.size()), &value);
if (!s.ok()) {
warning("get failed: %s\n", s.ToString().c_str());
} else {
notice("get ok. key:%s, value:%s\n", key.data(), value.data());
}
}
void test1() {
read("save");
printf("test 1\n");
write("box");
read("box");
sleep(1);
read("box");
printf("test 2\n");
write("box");
read("box");
printf("test 3\n");
write("box");
read("box");
write("save");
}
void test2() {
multi_write(1);
write("box");
}
void test3() {
read("box");
write("box");
read("box");
}
int main() {
srand(time(NULL));
if (init()) {
warning("init failed");
return -1;
}
test3();
delete g_db;
return 0;
}