-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlsm_tree_fuzzer.hpp
104 lines (94 loc) · 2.95 KB
/
lsm_tree_fuzzer.hpp
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
/**
* Copyright 2024 KUMAZAKI Hiroki
*
* 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.
*/
#ifndef TINYLAMB_LSM_TREE_FUZZER_HPP
#define TINYLAMB_LSM_TREE_FUZZER_HPP
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <map>
#include <random>
#include <string>
#include "common/constants.hpp"
#include "common/log_message.hpp"
#include "common/random_string.hpp"
#include "lsm_detail/lsm_view.hpp"
#include "lsm_tree.hpp"
namespace tinylamb {
inline uint64_t Generate(size_t offset) { return offset * 19937 + 2147483647; }
inline void Try(const uint64_t seed, bool verbose) {
RandomStringInitialize();
std::filesystem::path base_path = "lsm_tree_fuzzer-" + RandomString(20, true);
if (std::filesystem::exists(base_path)) {
std::filesystem::remove_all(base_path);
}
std::mt19937 rand(seed);
std::map<std::string, std::string> expected;
LSMTree tree(base_path);
const size_t kTestSize = rand() % 1000 + 10;
auto Scan = [&]() {
for (const auto& it : expected) {
if (!tree.Contains(it.first)) {
LOG(FATAL) << it.first << "not found";
exit(1);
}
}
};
for (size_t i = 0; i < kTestSize; ++i) {
std::string key = RandomString(rand() % 4 + 2, false);
std::string value = RandomString(rand() % 16 + 8, false);
if (verbose) {
LOG(TRACE) << "Insert: (" << (i + 1) << "/" << kTestSize << "): " << key
<< " => " << value;
}
tree.Write(key, value, false);
expected[key] = value;
Scan();
}
tree.Sync();
Scan();
LSMView v = tree.GetView();
if (verbose) {
LOG(TRACE) << v;
LOG(WARN) << v.Begin();
}
auto expected_it = expected.begin();
for (auto actual_it = v.Begin(); actual_it.IsValid();
++actual_it, ++expected_it) {
if (actual_it.Key() != expected_it->first) {
LOG(ERROR) << actual_it.Key() << " != " << expected_it->first;
exit(1);
}
if (actual_it.Value() != expected_it->second) {
LOG(ERROR) << actual_it.Key() << " -- " << actual_it.Value()
<< " != " << expected_it->second;
exit(1);
}
if (!tree.Contains(actual_it.Key())) {
LOG(ERROR) << actual_it.Key() << " not found";
exit(1);
}
}
if (expected_it != expected.end()) {
LOG(ERROR) << expected_it->first << " not finished";
}
std::filesystem::remove_all(base_path);
if (verbose) {
LOG(INFO) << "Successfully finished.";
}
}
} // namespace tinylamb
#endif // TINYLAMB_LSM_TREE_FUZZER_HPP