/
test_kv2.cpp
431 lines (334 loc) · 11.9 KB
/
test_kv2.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*****************************************************************************/
/* Milliways - B+ trees and key-value store C++ library */
/* */
/* Copyright 2016 Marco Pantaleoni and J CUBE Inc. Tokyo, Japan. */
/* */
/* Author: Marco Pantaleoni <marco.pantaleoni@gmail.com> */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/*****************************************************************************/
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
#ifndef MILLIWAYS_DEFAULT_BLOCK_SIZE
#define MILLIWAYS_DEFAULT_BLOCK_SIZE 256
#endif /* MILLIWAYS_DEFAULT_BLOCK_SIZE */
#ifndef MILLIWAYS_DEFAULT_B_FACTOR
#define MILLIWAYS_DEFAULT_B_FACTOR 4
#endif /* MILLIWAYS_DEFAULT_B_FACTOR */
#undef MILLIWAYS_DEFAULT_BLOCK_CACHE_SIZE
#define MILLIWAYS_DEFAULT_BLOCK_CACHE_SIZE 32
#undef MILLIWAYS_DEFAULT_NODE_CACHE_SIZE
#define MILLIWAYS_DEFAULT_NODE_CACHE_SIZE 32
#include "KeyValueStore.h"
static inline int rand_int(int lo, int hi)
{
return rand() % (hi - lo + 1) + lo;
}
static std::string random_string(int length)
{
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
std::string r(length, '\0');
for (int i = 0; i < length; ++i)
r[i] = alphanum[rand_int(0, sizeof(alphanum) - 1)];
return r;
}
TEST_CASE( "KeyValue store II", "[KeyValueStoreII]" ) {
typedef milliways::KeyValueStore kv_t;
typedef XTYPENAME kv_t::block_storage_type kv_blockstorage_t;
SECTION( "alternate get/put" ) {
std::cerr << "T1\n";
const std::string test_pathname("/tmp/test_kv");
std::remove(test_pathname.c_str());
kv_blockstorage_t* bs = new kv_blockstorage_t(test_pathname);
kv_t kv(bs);
kv.open();
REQUIRE(kv.isOpen());
std::string value;
kv.put("foo", "bar");
kv.get("foo", value);
kv.put("Mickey", "Mouse");
kv.put("color", "blue");
kv.get("Mickey", value);
kv.put("z", "534");
kv.put("height", "928m");
kv.get("color", value);
kv.put("aaa", "12");
kv.get("height", value);
kv.put("longish", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
kv.get("longish", value);
kv_t::Search search;
kv.find("longish", search);
REQUIRE(search.found());
REQUIRE(search.valid());
std::string piece;
bool ok = kv.get(search, piece, 6);
REQUIRE(ok);
std::cerr << piece;
REQUIRE(piece == "Lorem ");
ok = kv.get(search, piece, 6);
REQUIRE(ok);
std::cerr << piece;
REQUIRE(piece == "ipsum ");
ok = kv.get(search, piece, 6);
REQUIRE(ok);
std::cerr << piece;
REQUIRE(piece == "dolor ");
ok = kv.get(search, piece, 4);
REQUIRE(ok);
std::cerr << piece;
REQUIRE(piece == "sit ");
ok = kv.get(search, piece);
REQUIRE(ok);
std::cerr << piece << std::endl;
REQUIRE(piece.substr(0,5) == "amet,");
REQUIRE(piece.length() > 8);
REQUIRE(piece.substr(piece.length()-8,8) == "laborum.");
kv.close();
}
SECTION( "straming reads" ) {
std::cerr << "T2\n";
const std::string test_pathname("/tmp/test_kv");
std::remove(test_pathname.c_str());
kv_blockstorage_t* bs = new kv_blockstorage_t(test_pathname);
kv_t kv(bs);
kv.open();
REQUIRE(kv.isOpen());
kv.put("foo", "bar");
kv.put("Mickey", "Mouse");
kv.put("color", "blue");
kv.put("z", "534");
kv.put("height", "928m");
kv.put("aaa", "12");
kv.put("longish", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
kv_t::Search search;
kv.find("longish", search);
REQUIRE(search.found());
REQUIRE(search.valid());
std::string piece;
bool ok = kv.get(search, piece, 6);
REQUIRE(ok);
std::cerr << piece;
REQUIRE(piece == "Lorem ");
ok = kv.get(search, piece, 6);
REQUIRE(ok);
std::cerr << piece;
REQUIRE(piece == "ipsum ");
ok = kv.get(search, piece, 6);
REQUIRE(ok);
std::cerr << piece;
REQUIRE(piece == "dolor ");
ok = kv.get(search, piece, 4);
REQUIRE(ok);
std::cerr << piece;
REQUIRE(piece == "sit ");
ok = kv.get(search, piece);
REQUIRE(ok);
std::cerr << piece << std::endl;
REQUIRE(piece.substr(0,5) == "amet,");
REQUIRE(piece.length() > 8);
REQUIRE(piece.substr(piece.length()-8,8) == "laborum.");
kv.close();
}
SECTION( "largish sets works" ) {
std::cerr << "T3\n";
const std::string test_pathname("/tmp/test_kv");
std::remove(test_pathname.c_str());
typedef std::map<std::string, std::string> kv_set_t;
kv_set_t test_set;
const int test_set_size = 2048;
const int max_key_len = 20;
const int max_value_len = 256;
for (int i = 0; i < test_set_size; ++i)
{
std::string key = random_string(rand_int(1, max_key_len));
std::string value = random_string(rand_int(1, max_value_len));
test_set[key] = value;
}
std::cerr << "CREATE AND WRITE" << std::endl;
{
kv_blockstorage_t* bs = new kv_blockstorage_t(test_pathname);
kv_t kv(bs);
kv.open();
REQUIRE(kv.isOpen());
for (kv_set_t::const_iterator t_it = test_set.begin(); t_it != test_set.end(); ++t_it)
REQUIRE(kv.put(t_it->first, t_it->second));
for (kv_t::iterator it = kv.begin(); it != kv.end(); ++it)
{
std::string key = (*it);
if (test_set.count(key) != 1)
{
std::cerr << "ERROR: test_set.count('" << key << "') == " << test_set.count(key) << std::endl;
}
REQUIRE(test_set.count(key) == 1);
std::string value;
bool ok = kv.get(*it, value);
if (! ok)
{
std::cerr << "FAILED iterating/getting key it:'" << (*it) << "' (key:'" << key << "' value:'" << test_set[key] << "')" << std::endl;
}
REQUIRE(ok);
REQUIRE(value == test_set[key]);
}
for (kv_set_t::const_iterator t_it = test_set.begin(); t_it != test_set.end(); ++t_it)
{
const std::string& key = t_it->first;
const std::string& value = t_it->second;
REQUIRE(kv.has(key));
std::string kv_value;
REQUIRE(kv.get(key, kv_value));
REQUIRE(kv_value == value);
}
kv.close();
}
std::cerr << "OPEN AND LOAD" << std::endl;
{
kv_blockstorage_t* bs = new kv_blockstorage_t(test_pathname);
kv_t kv(bs);
kv.open();
REQUIRE(kv.isOpen());
for (kv_t::iterator it = kv.begin(); it != kv.end(); ++it)
{
std::string key = (*it);
REQUIRE(test_set.count(key) == 1);
std::string value;
REQUIRE(kv.get(*it, value));
REQUIRE(value == test_set[key]);
}
for (kv_set_t::const_iterator t_it = test_set.begin(); t_it != test_set.end(); ++t_it)
{
const std::string& key = t_it->first;
const std::string& value = t_it->second;
REQUIRE(kv.has(key));
std::string kv_value;
REQUIRE(kv.get(key, kv_value));
REQUIRE(kv_value == value);
}
kv.close();
}
std::remove(test_pathname.c_str());
}
SECTION( "largish values works" ) {
std::cerr << "T4\n";
const std::string test_pathname("/tmp/test_kv");
std::remove(test_pathname.c_str());
const std::string large_value1 = random_string(8192);
const std::string large_value2 = random_string(8192);
std::cerr << "CREATE AND WRITE" << std::endl;
{
kv_blockstorage_t* bs = new kv_blockstorage_t(test_pathname);
kv_t kv(bs);
kv.open();
REQUIRE(kv.isOpen());
kv.put("foo", large_value1);
REQUIRE(kv.has("foo"));
kv.put("bar", large_value2);
REQUIRE(kv.has("bar"));
std::string value;
REQUIRE(kv.get("foo", value));
REQUIRE(value == large_value1);
REQUIRE(kv.get("bar", value));
REQUIRE(value == large_value2);
kv.close();
}
std::cerr << "OPEN AND LOAD" << std::endl;
{
kv_blockstorage_t* bs = new kv_blockstorage_t(test_pathname);
kv_t kv(bs);
kv.open();
REQUIRE(kv.isOpen());
REQUIRE(kv.has("foo"));
REQUIRE(kv.has("bar"));
std::string value;
REQUIRE(kv.get("foo", value));
REQUIRE(value == large_value1);
REQUIRE(kv.get("bar", value));
REQUIRE(value == large_value2);
kv.close();
}
std::remove(test_pathname.c_str());
}
SECTION( "sets with largish values works" ) {
const std::string test_pathname("/tmp/test_kv");
std::remove(test_pathname.c_str());
typedef std::map<std::string, std::string> kv_set_t;
kv_set_t test_set;
const int test_set_size = 16;
const int max_key_len = 20;
const int min_value_len = 4096;
const int max_value_len = 16384;
for (int i = 0; i < test_set_size; ++i)
{
std::string key = random_string(rand_int(1, max_key_len));
std::string value = random_string(rand_int(min_value_len, max_value_len));
test_set[key] = value;
}
std::cerr << "CREATE AND WRITE" << std::endl;
{
kv_blockstorage_t* bs = new kv_blockstorage_t(test_pathname);
kv_t kv(bs);
kv.open();
REQUIRE(kv.isOpen());
for (kv_set_t::const_iterator t_it = test_set.begin(); t_it != test_set.end(); ++t_it)
REQUIRE(kv.put(t_it->first, t_it->second));
for (kv_t::iterator it = kv.begin(); it != kv.end(); ++it)
{
std::string key = (*it);
REQUIRE(test_set.count(key) == 1);
std::string value;
REQUIRE(kv.get(*it, value));
REQUIRE(value == test_set[key]);
}
for (kv_set_t::const_iterator t_it = test_set.begin(); t_it != test_set.end(); ++t_it)
{
const std::string& key = t_it->first;
const std::string& value = t_it->second;
REQUIRE(kv.has(key));
std::string kv_value;
REQUIRE(kv.get(key, kv_value));
REQUIRE(kv_value == value);
}
kv.close();
}
std::cerr << "OPEN AND LOAD" << std::endl;
{
kv_blockstorage_t* bs = new kv_blockstorage_t(test_pathname);
kv_t kv(bs);
kv.open();
REQUIRE(kv.isOpen());
for (kv_set_t::const_iterator t_it = test_set.begin(); t_it != test_set.end(); ++t_it)
REQUIRE(kv.put(t_it->first, t_it->second));
for (kv_t::iterator it = kv.begin(); it != kv.end(); ++it)
{
std::string key = (*it);
REQUIRE(test_set.count(key) == 1);
std::string value;
REQUIRE(kv.get(*it, value));
REQUIRE(value == test_set[key]);
}
for (kv_set_t::const_iterator t_it = test_set.begin(); t_it != test_set.end(); ++t_it)
{
const std::string& key = t_it->first;
const std::string& value = t_it->second;
REQUIRE(kv.has(key));
std::string kv_value;
REQUIRE(kv.get(key, kv_value));
REQUIRE(kv_value == value);
}
kv.close();
}
std::remove(test_pathname.c_str());
}
}