-
Notifications
You must be signed in to change notification settings - Fork 1
/
expired_time_demo.cpp
121 lines (101 loc) · 2.88 KB
/
expired_time_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
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdlib.h>
#include "util.h"
#include "custom_leveldb.h"
using namespace store;
leveldb::DB *g_db;
CustomComparator g_comparator;
CustomDeletePolicy g_delete_policy;
std::string g_data_dir = "./data/expired_time_demo";
int init() {
leveldb::Options init_options;
init_options.comparator = &g_comparator;
init_options.delete_policy = &g_delete_policy;
init_options.create_if_missing = true;
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 user_key = "box";
string_random_append(&user_key, 1, 5);
CustomKey key(user_key, time(NULL) + 3);
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, user_key_len:%lu, key_len:%lu\n",
user_key.c_str(), user_key.size(), key.size());
}
}
void multi_write(int n) {
for (int i = 0; i < n; ++i) {
write_random();
}
}
void write(std::string key_str, int expired_time_s) {
CustomKey key(key_str, expired_time_s);
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_str.c_str());
}
}
void read(std::string key_str) {
CustomKey key(key_str, 0);
std::string value;
std::string rkey_raw;
leveldb::Status s = g_db->Get(leveldb::ReadOptions(),
leveldb::Slice(key.data(), key.size()), &value, &rkey_raw);
if (!s.ok()) {
warning("get failed: %s\n", s.ToString().c_str());
} else {
CustomKey rkey;
rkey.DecodeFrom(rkey_raw);
notice("get ok. key:%s, rkey:%s, expired_time_s:%d, value:%s\n",
key_str.data(), rkey.user_key().data(), rkey.expired_time_s(), value.data());
}
}
void test1() {
read("save");
printf("test 1\n");
write("box", time(NULL) + 1);
read("box");
sleep(1);
read("box");
printf("test 2\n");
write("box", 0);
read("box");
printf("test 3\n");
write("box", time(NULL) - 1);
read("box");
write("save", time(NULL) + 3);
}
void test2() {
multi_write(100);
write("box", 0);
write("box", 0);
write("box", 0);
write("box", 0);
write("box", 0);
write("box", 0);
write("box", 0);
write("box", 0);
}
int main() {
srand(time(NULL));
if (init()) {
warning("init failed");
return -1;
}
test1();
delete g_db;
return 0;
}