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
|
#pragma once
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <gsl/gsl_rng.h>
#include <cstring>
#include <vector>
#include <memory>
#include "psu-util/progress.h"
template <typename QP>
static std::vector<QP> read_lookup_queries(std::string fname, double selectivity) {
std::vector<QP> queries;
FILE *qf = fopen(fname.c_str(), "r");
if (!qf) {
fprintf(stderr, "ERROR: Failed to open file %s\n", fname.c_str());
exit(EXIT_FAILURE);
}
size_t start, stop;
double sel;
while (fscanf(qf, "%zu%zu%lf\n", &start, &stop, &sel) != EOF) {
if (start < stop && std::abs(sel - selectivity) < 0.1) {
QP q;
q.target_key = start;
queries.push_back(q);
}
}
fclose(qf);
return queries;
}
template <typename QP>
static std::vector<QP> generate_string_lookup_queries(std::vector<std::unique_ptr<char[]>> &strings, size_t cnt, gsl_rng *rng) {
std::vector<QP> queries;
for (size_t i=0; i<cnt; i++) {
auto idx = gsl_rng_uniform_int(rng, strings.size());
QP q;
q.search_key = strings[idx].get();
queries.push_back(q);
}
return queries;
}
template <typename QP>
static std::vector<QP> read_range_queries(std::string &fname, double selectivity) {
std::vector<QP> queries;
FILE *qf = fopen(fname.c_str(), "r");
if (!qf) {
fprintf(stderr, "ERROR: Failed to open file %s\n", fname.c_str());
exit(EXIT_FAILURE);
}
size_t start, stop;
double sel;
while (fscanf(qf, "%zu%zu%lf\n", &start, &stop, &sel) != EOF) {
if (start < stop && std::abs(sel - selectivity) < 0.00001) {
QP q;
q.lower_bound = start;
q.upper_bound = stop;
queries.push_back(q);
}
}
fclose(qf);
return queries;
}
template <typename QP>
static std::vector<QP> read_binary_knn_queries(std::string fname, size_t k, size_t n) {
std::vector<QP> queries;
queries.reserve(n);
std::fstream file;
file.open(fname, std::ios::in | std::ios::binary);
if (!file.is_open()) {
fprintf(stderr, "ERROR: Failed to open file %s\n", fname.c_str());
exit(EXIT_FAILURE);
}
int32_t dim;
int32_t cnt;
file.read((char*) &(cnt), sizeof(cnt));
file.read((char*) &(dim), sizeof(dim));
if (n > cnt) {
n = cnt;
}
for (size_t i=0; i<n; i++) {
QP query;
for (size_t j=0; j<dim; j++) {
uint64_t val;
file.read((char*) &(val), sizeof(uint64_t));
query.point.data[j] = val;
}
query.k = k;
queries.push_back(query);
}
return queries;
}
template <typename QP>
static std::vector<QP> read_knn_queries(std::string fname, size_t k) {
std::vector<QP> queries;
FILE *qf = fopen(fname.c_str(), "r");
char *line = NULL;
size_t len = 0;
if (!qf) {
fprintf(stderr, "ERROR: Failed to open file %s\n", fname.c_str());
exit(EXIT_FAILURE);
}
while (getline(&line, &len, qf) > 0) {
char *token;
QP query;
size_t idx = 0;
token = strtok(line, " ");
do {
query.point.data[idx++] = atof(token);
} while ((token = strtok(NULL, " ")));
query.k = k;
queries.emplace_back(query);
}
free(line);
fclose(qf);
return queries;
}
template<typename R>
static std::vector<R> read_sosd_file(std::string &fname, size_t n) {
std::fstream file;
file.open(fname, std::ios::in | std::ios::binary);
if (!file.is_open()) {
fprintf(stderr, "ERROR: Failed to open file %s\n", fname.c_str());
exit(EXIT_FAILURE);
}
std::vector<R> records(n);
for (size_t i=0; i<n; i++) {
decltype(R::key) k;
file.read((char*) &(k), sizeof(R::key));
records[i].key = k;
records[i].value = i;
}
return records;
}
template<typename K, typename V>
static std::vector<std::pair<K, V>> read_sosd_file_pair(std::string &fname, size_t n) {
std::fstream file;
file.open(fname, std::ios::in | std::ios::binary);
if (!file.is_open()) {
fprintf(stderr, "ERROR: Failed to open file %s\n", fname.c_str());
exit(EXIT_FAILURE);
}
std::vector<std::pair<K,V>> records(n);
for (size_t i=0; i<n; i++) {
K k;
file.read((char*) &(k), sizeof(K));
records[i].first = k;
records[i].second = i;
}
return records;
}
/*
* This function expects a plaintext file with each vector on its own line.
* There should be D dimensions (or more) for each record, separated by
* whitespace. The function will generate a vector of n records, each
* record built from the first D dimensions of the data in the file.
*/
template <typename R, size_t D>
static std::vector<R> read_vector_file(std::string &fname, size_t n) {
std::fstream file;
file.open(fname, std::ios::in);
if (!file.is_open()) {
fprintf(stderr, "ERROR: Failed to open file %s\n", fname.c_str());
exit(EXIT_FAILURE);
}
std::vector<R> records;
records.reserve(n);
for (size_t i=0; i<n; i++) {
std::string line;
if (!std::getline(file, line, '\n')) break;
std::stringstream line_stream(line);
R rec;
for (size_t j=0; j<D; j++) {
std::string dim;
if (!std::getline(line_stream, dim, ' ')) break;
rec.data[j] = atof(dim.c_str());
}
records.emplace_back(rec);
}
return records;
}
template <typename R>
static std::vector<R> read_binary_vector_file(std::string &fname, size_t n) {
std::fstream file;
file.open(fname, std::ios::in | std::ios::binary);
if (!file.is_open()) {
fprintf(stderr, "ERROR: Failed to open file %s\n", fname.c_str());
exit(EXIT_FAILURE);
}
std::vector<R> records;
records.reserve(n);
int32_t dim;
int32_t cnt;
file.read((char*) &(cnt), sizeof(cnt));
file.read((char*) &(dim), sizeof(dim));
if (n > cnt) {
n = cnt;
}
R rec;
for (size_t i=0; i<n; i++) {
for (size_t j=0; j<dim; j++) {
uint64_t val;
file.read((char*) &(val), sizeof(uint64_t));
rec.data[j] = val;
}
records.emplace_back(rec);
}
return records;
}
[[maybe_unused]] static std::vector<std::unique_ptr<char[]>>read_string_file(std::string fname, size_t n=10000000) {
std::fstream file;
file.open(fname, std::ios::in);
if (!file.is_open()) {
fprintf(stderr, "ERROR: Failed to open file %s\n", fname.c_str());
exit(EXIT_FAILURE);
}
std::vector<std::unique_ptr<char[]>> strings;
strings.reserve(n);
size_t i=0;
std::string line;
while (i < n && std::getline(file, line, '\n')) {
strings.emplace_back(std::unique_ptr<char[]>(strdup(line.c_str())));
i++;
psudb::progress_update((double) i / (double) n, "Reading file:");
}
return strings;
}
|