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
|
/*
* include/query/irs.h
*
* Copyright (C) 2023 Douglas B. Rumbaugh <drumbaugh@psu.edu>
*
* Distributed under the Modified BSD License.
*
* A query class for independent range sampling. This query requires
* that the shard support get_lower_bound(key), get_upper_bound(key),
* and get_record_at(index).
*/
#pragma once
#include "framework/QueryRequirements.h"
#include "psu-ds/Alias.h"
namespace de {
namespace irs {
template <ShardInterface S, bool REJECTION = true> class Query {
typedef typename S::RECORD R;
public:
struct Parameters {
decltype(R::key) lower_bound;
decltype(R::key) upper_bound;
size_t sample_size;
gsl_rng *rng;
};
struct LocalQuery {
size_t lower_idx;
size_t upper_idx;
size_t total_weight;
size_t sample_size;
Parameters global_parms;
};
struct LocalQueryBuffer {
BufferView<R> *buffer;
size_t cutoff;
std::vector<Wrapped<R>> records;
std::unique_ptr<psudb::Alias> alias;
size_t sample_size;
Parameters global_parms;
};
typedef Wrapped<R> LocalResultType;
typedef R ResultType;
constexpr static bool EARLY_ABORT = false;
constexpr static bool SKIP_DELETE_FILTER = false;
static LocalQuery *local_preproc(S *shard, Parameters *parms) {
auto query = new LocalQuery();
query->global_parms = *parms;
query->lower_idx = shard->get_lower_bound(query->global_parms.lower_bound);
query->upper_idx = shard->get_upper_bound(query->global_parms.upper_bound);
if (query->lower_idx == shard->get_record_count()) {
query->total_weight = 0;
} else {
query->total_weight = query->upper_idx - query->lower_idx;
}
query->sample_size = 0;
return query;
}
static LocalQueryBuffer *local_preproc_buffer(BufferView<R> *buffer,
Parameters *parms) {
auto query = new LocalQueryBuffer();
query->buffer = buffer;
query->cutoff = query->buffer->get_record_count();
query->sample_size = 0;
query->alias = nullptr;
query->global_parms = *parms;
if constexpr (REJECTION) {
return query;
}
for (size_t i = 0; i < query->cutoff; i++) {
if ((query->buffer->get(i)->rec.key >= query->global_parms.lower_bound) &&
(buffer->get(i)->rec.key <= query->global_parms.upper_bound)) {
query->records.emplace_back(*(query->buffer->get(i)));
}
}
return query;
}
static void distribute_query(Parameters *parms,
std::vector<LocalQuery *> const &local_queries,
LocalQueryBuffer *buffer_query) {
std::vector<size_t> shard_sample_sizes(local_queries.size() + 1, 0);
size_t buffer_sz = 0;
/* for simplicity of static structure testing */
if (!buffer_query) {
assert(local_queries.size() == 1);
local_queries[0]->sample_size =
local_queries[0]->global_parms.sample_size;
return;
}
/* we only need to build the shard alias on the first call */
if (buffer_query->alias == nullptr) {
std::vector<size_t> weights;
if constexpr (REJECTION) {
weights.push_back(buffer_query->cutoff);
} else {
weights.push_back(buffer_query->records.size());
}
size_t total_weight = weights[0];
for (auto &q : local_queries) {
total_weight += q->total_weight;
weights.push_back(q->total_weight);
}
/*
* if no valid records fall within the query range,
* set all of the sample sizes to 0 and bail out.
*/
if (total_weight == 0) {
for (auto q : local_queries) {
q->sample_size = 0;
}
return;
}
std::vector<double> normalized_weights;
for (auto w : weights) {
normalized_weights.push_back((double)w / (double)total_weight);
}
buffer_query->alias = std::make_unique<psudb::Alias>(normalized_weights);
}
for (size_t i = 0; i < parms->sample_size; i++) {
auto idx = buffer_query->alias->get(parms->rng);
if (idx == 0) {
buffer_sz++;
} else {
shard_sample_sizes[idx - 1]++;
}
}
if (buffer_query) {
buffer_query->sample_size = buffer_sz;
}
for (size_t i = 0; i < local_queries.size(); i++) {
local_queries[i]->sample_size = shard_sample_sizes[i];
}
}
static std::vector<LocalResultType> local_query(S *shard, LocalQuery *query) {
auto sample_sz = query->sample_size;
std::vector<LocalResultType> result_set;
if (sample_sz == 0 || query->lower_idx == shard->get_record_count()) {
return result_set;
}
size_t attempts = 0;
size_t range_length = query->upper_idx - query->lower_idx;
do {
attempts++;
size_t idx =
(range_length > 0)
? gsl_rng_uniform_int(query->global_parms.rng, range_length)
: 0;
result_set.emplace_back(*shard->get_record_at(query->lower_idx + idx));
} while (attempts < sample_sz);
return result_set;
}
static std::vector<LocalResultType>
local_query_buffer(LocalQueryBuffer *query) {
std::vector<LocalResultType> result;
result.reserve(query->sample_size);
if constexpr (REJECTION) {
for (size_t i = 0; i < query->sample_size; i++) {
auto idx = gsl_rng_uniform_int(query->global_parms.rng, query->cutoff);
auto rec = query->buffer->get(idx);
if (rec->rec.key >= query->global_parms.lower_bound &&
rec->rec.key <= query->global_parms.upper_bound) {
result.emplace_back(*rec);
}
}
return result;
}
for (size_t i = 0; i < query->sample_size; i++) {
auto idx =
gsl_rng_uniform_int(query->global_parms.rng, query->records.size());
result.emplace_back(query->records[idx]);
}
return result;
}
static void
combine(std::vector<std::vector<LocalResultType>> const &local_results,
Parameters *parms, std::vector<ResultType> &output) {
for (size_t i = 0; i < local_results.size(); i++) {
for (size_t j = 0; j < local_results[i].size(); j++) {
output.emplace_back(local_results[i][j].rec);
}
}
}
static bool repeat(Parameters *parms, std::vector<ResultType> &output,
std::vector<LocalQuery *> const &local_queries,
LocalQueryBuffer *buffer_query) {
if (output.size() < parms->sample_size) {
parms->sample_size -= output.size();
distribute_query(parms, local_queries, buffer_query);
return true;
}
return false;
}
};
} // namespace irs
} // namespace de
|