-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmemtable_test.cc
228 lines (190 loc) · 6.41 KB
/
memtable_test.cc
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "db/memtable.h"
#include <algorithm>
#include <vector>
#include "gtest/gtest.h"
#include "treeline/status.h"
namespace {
using namespace tl;
using namespace tl::format;
TEST(MemTableTest, SimpleReadWrite) {
MemTableOptions moptions;
MemTable table(moptions);
std::string key = "hello";
std::string value = "world";
Status s = table.Put(key, value);
ASSERT_TRUE(s.ok());
std::string value_out;
WriteType write_type_out;
s = table.Get(key, &write_type_out, &value_out);
ASSERT_TRUE(s.ok());
ASSERT_EQ(write_type_out, WriteType::kWrite);
ASSERT_EQ(value, value_out);
}
TEST(MemTableTest, NegativeLookup) {
MemTableOptions moptions;
MemTable table(moptions);
std::string key = "hello";
std::string value_out;
WriteType write_type_out;
Status s = table.Get(key, &write_type_out, &value_out);
ASSERT_TRUE(s.IsNotFound());
s = table.Delete(key);
ASSERT_TRUE(s.ok());
s = table.Get(key, &write_type_out, &value_out);
ASSERT_TRUE(s.ok());
ASSERT_EQ(write_type_out, WriteType::kDelete);
}
TEST(MemTableTest, WriteThenDelete) {
MemTableOptions moptions;
MemTable table(moptions);
std::string key = "hello";
std::string value = "world";
Status s = table.Put(key, value);
ASSERT_TRUE(s.ok());
std::string value_out;
WriteType write_type_out;
s = table.Get(key, &write_type_out, &value_out);
ASSERT_TRUE(s.ok());
ASSERT_EQ(write_type_out, WriteType::kWrite);
ASSERT_EQ(value, value_out);
s = table.Delete(key);
ASSERT_TRUE(s.ok());
s = table.Get(key, &write_type_out, &value_out);
ASSERT_TRUE(s.ok());
ASSERT_EQ(write_type_out, WriteType::kDelete);
}
TEST(MemTableTest, DeleteThenWrite) {
MemTableOptions moptions;
MemTable table(moptions);
std::string key = "hello";
std::string value = "world";
Status s = table.Delete(key);
ASSERT_TRUE(s.ok());
std::string value_out;
WriteType write_type_out;
s = table.Get(key, &write_type_out, &value_out);
ASSERT_TRUE(s.ok());
ASSERT_EQ(write_type_out, WriteType::kDelete);
s = table.Put(key, value);
ASSERT_TRUE(s.ok());
s = table.Get(key, &write_type_out, &value_out);
ASSERT_TRUE(s.ok());
ASSERT_EQ(write_type_out, WriteType::kWrite);
ASSERT_EQ(value, value_out);
}
TEST(MemTableTest, InOrderIterate) {
std::vector<std::string> strs = {"hello", "world", "ordering", "alphabet"};
std::vector<std::string> ordered(strs);
std::sort(ordered.begin(), ordered.end());
std::string value = "test string";
MemTableOptions moptions;
MemTable table(moptions);
for (const auto& s : strs) {
ASSERT_TRUE(table.Put(s, value).ok());
}
size_t idx = 0;
MemTable::Iterator it = table.GetIterator();
it.SeekToFirst();
ASSERT_TRUE(it.Valid());
for (; it.Valid(); it.Next(), ++idx) {
ASSERT_TRUE(idx < ordered.size());
ASSERT_TRUE(it.key().compare(ordered.at(idx)) == 0);
ASSERT_TRUE(it.value().compare(value) == 0);
ASSERT_EQ(it.type(), WriteType::kWrite);
}
// All strings should have been found
ASSERT_EQ(idx, ordered.size());
}
TEST(MemTableTest, MultiWriteIterate) {
MemTableOptions moptions;
MemTable table(moptions);
const std::string final_value = "test string";
ASSERT_TRUE(table.Put("abc", "hello").ok());
ASSERT_TRUE(table.Put("xyz123", "world").ok());
ASSERT_TRUE(table.Put("xyz123abc", "test").ok());
ASSERT_TRUE(table.Put("test key", final_value).ok());
ASSERT_TRUE(table.Put("abcde", "another test string").ok());
// Make sure our iterator behaves properly on "duplicate" key writes.
ASSERT_TRUE(table.Put("xyz123", "replaced1").ok());
ASSERT_TRUE(table.Put("xyz123", "replaced2").ok());
ASSERT_TRUE(table.Put("xyz123", "replaced3").ok());
ASSERT_TRUE(table.Put("xyz123", "replaced4").ok());
ASSERT_TRUE(table.Put("xyz123", "replaced5").ok());
ASSERT_TRUE(table.Put("xyz123", final_value).ok());
ASSERT_TRUE(table.Delete("abc").ok());
ASSERT_TRUE(table.Put("abcde", final_value).ok());
ASSERT_TRUE(table.Put("abcd", final_value).ok());
ASSERT_TRUE(table.Put("xyz123abc", final_value).ok());
MemTable::Iterator it = table.GetIterator();
// Delete: abc
it.SeekToFirst();
ASSERT_TRUE(it.Valid());
ASSERT_TRUE(it.key().compare("abc") == 0);
ASSERT_EQ(it.type(), WriteType::kDelete);
// Write: (abcd, test string)
it.Next();
ASSERT_TRUE(it.Valid());
ASSERT_TRUE(it.key().compare("abcd") == 0);
ASSERT_EQ(it.type(), WriteType::kWrite);
ASSERT_TRUE(it.value().compare(final_value) == 0);
// Write: (abcde, test string)
it.Next();
ASSERT_TRUE(it.Valid());
ASSERT_TRUE(it.key().compare("abcde") == 0);
ASSERT_EQ(it.type(), WriteType::kWrite);
ASSERT_TRUE(it.value().compare(final_value) == 0);
// Write: (test key, test string)
it.Next();
ASSERT_TRUE(it.Valid());
ASSERT_TRUE(it.key().compare("test key") == 0);
ASSERT_EQ(it.type(), WriteType::kWrite);
ASSERT_TRUE(it.value().compare(final_value) == 0);
// Write: (xyz123, test string)
it.Next();
ASSERT_TRUE(it.Valid());
ASSERT_TRUE(it.key().compare("xyz123") == 0);
ASSERT_EQ(it.type(), WriteType::kWrite);
ASSERT_TRUE(it.value().compare(final_value) == 0);
// Write: (xyz123abc, test string)
it.Next();
ASSERT_TRUE(it.Valid());
ASSERT_TRUE(it.key().compare("xyz123abc") == 0);
ASSERT_EQ(it.type(), WriteType::kWrite);
ASSERT_TRUE(it.value().compare(final_value) == 0);
// End of table
it.Next();
ASSERT_FALSE(it.Valid());
}
TEST(MemTableTest, SimilarGet) {
MemTableOptions moptions;
MemTable table(moptions);
std::string value_out;
WriteType write_type_out;
ASSERT_TRUE(table.Put("abcd", "hello").ok());
ASSERT_TRUE(table.Get("abc", &write_type_out, &value_out).IsNotFound());
}
TEST(MemTableTest, AddFromDeferral) {
MemTableOptions moptions;
MemTable table(moptions);
ASSERT_TRUE(table.Put("abcd", "hello1").ok());
ASSERT_TRUE(table
.Add("abcd", "hello2", WriteType::kWrite,
/*from_deferral = */ true,
/*injected_sequence_number = */ 0)
.ok());
// Check that you don't read it out
std::string value_out;
WriteType write_type_out;
Status s = table.Get("abcd", &write_type_out, &value_out);
ASSERT_EQ(value_out, "hello1");
// Check that it's skipped by the iterator
ASSERT_TRUE(table.Put("abcc", "hello1").ok());
ASSERT_TRUE(table.Put("abce", "hello1").ok());
MemTable::Iterator it = table.GetIterator();
it.SeekToFirst();
ASSERT_TRUE(it.Valid());
for (; it.Valid(); it.Next()) {
ASSERT_EQ(it.value(), "hello1");
}
}
} // namespace