-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathrekey.cpp
More file actions
138 lines (127 loc) · 4.02 KB
/
Copy pathrekey.cpp
File metadata and controls
138 lines (127 loc) · 4.02 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
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
//
// Copyright (c) 2015-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Test that header file is self-contained
#include <nudb/rekey.hpp>
#include "suite.hpp"
#include <nudb/_experimental/test/fail_file.hpp>
#include <nudb/_experimental/test/test_store.hpp>
#include <nudb/progress.hpp>
#include <nudb/verify.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
namespace nudb {
namespace test {
// Simple test to check that rekey works, and
// also to exercise all its failure paths.
//
class rekey_test : public boost::beast::unit_test::suite
{
public:
void
do_recover(
std::size_t N, nsize_t blockSize, float loadFactor)
{
using key_type = std::uint32_t;
auto const keys = static_cast<std::size_t>(
loadFactor * detail::bucket_capacity(blockSize));
std::size_t const bufferSize =
(blockSize * (1 + ((N + keys - 1) / keys)))
/ 2;
error_code ec;
test_store ts{sizeof(key_type), blockSize, loadFactor};
ts.create(ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
ts.open(ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
// Insert
for(std::size_t i = 0; i < N; ++i)
{
auto const item = ts[i];
ts.db.insert(item.key, item.data, item.size, ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
}
ts.close(ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
// Verify
verify_info info;
verify<xxhasher>(
info, ts.dp, ts.kp, bufferSize, no_progress{}, ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
if(! BEAST_EXPECT(info.value_count == N))
return;
if(! BEAST_EXPECT(info.spill_count > 0))
return;
// Rekey
auto const kp2 = ts.kp + "2";
for(std::size_t n = 1;; ++n)
{
fail_counter fc{n};
rekey<xxhasher, fail_file<native_file>>(
ts.dp, kp2, ts.lp, blockSize, loadFactor,
N, bufferSize, ec, no_progress{}, fc);
if(! ec)
break;
if(! BEAST_EXPECTS(ec ==
test::test_error::failure, ec.message()))
return;
ec = {};
recover<xxhasher, native_file>(
ts.dp, kp2, ts.lp, ec);
if(ec == error::no_key_file ||
ec == errc::no_such_file_or_directory)
{
ec = {};
continue;
}
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
native_file::erase(kp2, ec);
if(ec == errc::no_such_file_or_directory)
ec = {};
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
// Verify
verify<xxhasher>(info, ts.dp, ts.kp,
bufferSize, no_progress{}, ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
if(! BEAST_EXPECT(info.value_count == N))
return;
}
// Verify
verify<xxhasher>(info, ts.dp, ts.kp,
bufferSize, no_progress{}, ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
if(! BEAST_EXPECT(info.value_count == N))
return;
verify<xxhasher>(info, ts.dp, kp2,
bufferSize, no_progress{}, ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
if(! BEAST_EXPECT(info.value_count == N))
return;
}
void
run() override
{
enum
{
N = 50000,
blockSize = 256
};
float const loadFactor = 0.95f;
do_recover(N, blockSize, loadFactor);
}
};
DEFINE_TESTSUITE(nudb,test,rekey);
} // test
} // nudb