-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_helpers.hpp
200 lines (163 loc) · 5.45 KB
/
test_helpers.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
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
// Copyright (c) Lorenz Hübschle-Schneider
// Copyright (c) Facebook, Inc. and its affiliates.
// All Rights Reserved. This source code is licensed under the Apache 2.0
// License (found in the LICENSE file in the root directory).
#pragma once
#include "config.hpp"
#include "rocksdb/coding.h"
#include "rocksdb/slice.h"
#include <xxhash.h>
#include <string>
namespace ribbon::test {
struct BasicConfig : ribbon::DefaultConfig<uint32_t, uint8_t, rocksdb::Slice> {
static constexpr ThreshMode kThreshMode = ThreshMode::normal;
static constexpr bool log = false;
};
struct RetrievalConfig : public BasicConfig {
static constexpr bool kIsFilter = false;
};
template <typename CoeffRow, typename ResultRow, typename Key>
struct DefaultRetrievalConfig : public DefaultConfig<CoeffRow, ResultRow, Key> {
static constexpr bool kIsFilter = false;
static constexpr ThreshMode kThreshMode = ThreshMode::normal;
static constexpr bool log = false;
};
// Default config, but specify sizes in bits not types
template <size_t coeff_bits, size_t result_bits>
struct QuietRConfig : public RConfig<coeff_bits, result_bits> {
static constexpr bool log = false;
};
// Generate semi-sequential keys
struct StandardKeyGen {
StandardKeyGen(const std::string& prefix, uint64_t id)
: id_(id), str_(prefix) {
rocksdb::PutFixed64(&str_, /*placeholder*/ 0);
}
// Prefix (only one required)
StandardKeyGen& operator++() {
++id_;
return *this;
}
// Prefix (only one required)
StandardKeyGen operator+(uint64_t i) {
StandardKeyGen copy = *this;
copy += i;
return copy;
}
StandardKeyGen& operator+=(uint64_t i) {
id_ += i;
return *this;
}
const std::string& operator*() {
// Use multiplication to mix things up a little in the key
rocksdb::EncodeFixed64(&str_[str_.size() - 8],
id_ * uint64_t{0x1500000001});
return str_;
}
bool operator==(const StandardKeyGen& other) const {
// Same prefix is assumed
return id_ == other.id_;
}
bool operator!=(const StandardKeyGen& other) const {
// Same prefix is assumed
return id_ != other.id_;
}
ssize_t operator-(const StandardKeyGen& other) const {
return id_ - other.id_;
}
uint64_t id_;
std::string str_;
};
// Generate small sequential keys, that can misbehave with sequential seeds
// as in https://github.com/Cyan4973/xxHash/issues/469.
// These keys are only heuristically unique, but that's OK with 64 bits,
// for testing purposes.
struct SmallKeyGen {
SmallKeyGen(const std::string& prefix, uint64_t id) : id_(id) {
// Hash the prefix for a heuristically unique offset
id_ += XXH3_64bits(prefix.c_str(), prefix.size());
rocksdb::PutFixed64(&str_, id_);
}
// Prefix (only one required)
SmallKeyGen& operator++() {
++id_;
return *this;
}
SmallKeyGen operator+(uint64_t i) {
SmallKeyGen copy = *this;
copy += i;
return copy;
}
SmallKeyGen& operator+=(uint64_t i) {
id_ += i;
return *this;
}
const std::string& operator*() {
rocksdb::EncodeFixed64(&str_[str_.size() - 8], id_);
return str_;
}
bool operator==(const SmallKeyGen& other) const {
return id_ == other.id_;
}
bool operator!=(const SmallKeyGen& other) const {
return id_ != other.id_;
}
uint64_t id_;
std::string str_;
};
struct RetrievalInputGen {
RetrievalInputGen(const std::string& prefix, uint64_t id) : id_(id) {
val_.first = prefix;
rocksdb::PutFixed64(&val_.first, /*placeholder*/ 0);
}
// Prefix (only one required)
RetrievalInputGen& operator++() {
++id_;
return *this;
}
// Prefix (only one required)
RetrievalInputGen operator+(uint64_t i) {
RetrievalInputGen copy = *this;
copy += i;
return copy;
}
RetrievalInputGen& operator+=(uint64_t i) {
id_ += i;
return *this;
}
const std::pair<std::string, uint8_t>& operator*() {
// Use multiplication to mix things up a little in the key
rocksdb::EncodeFixed64(&val_.first[val_.first.size() - 8],
id_ * uint64_t{0x1500000001});
// Occasionally repeat values etc.
val_.second = static_cast<uint8_t>(id_ * 7 / 8);
return val_;
}
const std::pair<std::string, uint8_t>* operator->() {
return &**this;
}
ssize_t operator-(const RetrievalInputGen& other) const {
return id_ - other.id_;
}
bool operator==(const RetrievalInputGen& other) const {
// Same prefix is assumed
return id_ == other.id_;
}
bool operator!=(const RetrievalInputGen& other) const {
// Same prefix is assumed
return id_ != other.id_;
}
uint64_t id_;
std::pair<std::string, uint8_t> val_;
};
// Copied from rocksdb util/ribbon_test.cpp:
// For testing Poisson-distributed (or similar) statistics, get value for
// `stddevs_allowed` standard deviations above expected mean
// `expected_count`.
// (Poisson approximates Binomial only if probability of a trial being
// in the count is low.)
uint64_t PoissonUpperBound(double expected_count, double stddevs_allowed) {
return static_cast<uint64_t>(
expected_count + stddevs_allowed * std::sqrt(expected_count) + 1.0);
}
} // namespace ribbon::test