-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_shuffle.h
291 lines (255 loc) · 9.48 KB
/
random_shuffle.h
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
/*
Copyright 2017 InitialDLab
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <string>
#include <fstream>
#include <vector>
#include <memory>
//#include <unistd.h>
#include <cstdio>
#include <boost/geometry/geometry.hpp>
//#include <tpie/tpie.h>
//#include <tpie/file_stream.h>
//#include <tpie/sort.h>
//#include <tpie/progress_indicator_null.h>
template<typename Value>
struct RandomShuffle
{
// should be consistent with rtree/io_layers.h for experiments
struct Parameters {
double fill_ratio = 0.7;
size_t block_size = 8192;
};
using converter_t = std::function<Value(std::string const&)>;
using value_with_random_id_t = std::pair<Value, int>;
//using file_stream_t = tpie::file_stream<value_with_random_id_t>;
using cache_t = std::vector<Value>;
static void
build(
std::string const& in_filename,
std::string const& out_filename,
converter_t ReadConverter,
Parameters const& parameters,
size_t tpie_allowed_memory
);
RandomShuffle(std::string const& filename, size_t cache_size = 0)
: filename(filename)
{
cache.reserve(cache_size / sizeof(Value) + 1);
std::ifstream fin(filename, std::ifstream::binary);
fin.read(reinterpret_cast<char*>(&entry_count), sizeof(entry_count));
fin.read(reinterpret_cast<char*>(&block_size), sizeof(block_size));
int64_t entry_read = 0;
size_t cache_used = 0;
value_with_random_id_t v;
entry_read = 0;
block_read_for_cache = 0 ;
while(entry_read < entry_count)
{
auto vec = read_block(fin, block_read_for_cache);
if(cache_used + sizeof(Value) * vec.size() > cache_size)
break;
++block_read_for_cache;
cache.insert(cache.end(), vec.begin(), vec.end());
cache_used += sizeof(Value) * vec.size();
entry_read += vec.size();
}
}
std::vector<Value>
read_block(std::ifstream & in, size_t block_id) const
{
std::vector<Value> v;
size_t offset = sizeof(int64_t) + sizeof(size_t) + block_size * block_id;
in.seekg(offset);
int64_t count;
in.read(reinterpret_cast<char*>(&count), sizeof(count));
value_with_random_id_t e;
for(int64_t i = 0; i < count; ++i)
{
in.read(reinterpret_cast<char*>(&e), sizeof(e));
v.push_back(e.first);
}
return v;
}
template<typename Geometry>
struct sample_query_cursor {
sample_query_cursor(Geometry const& query, RandomShuffle const& rs)
: query(query)
, rs(rs)
, cache(rs.cache)
, pfs(new std::ifstream(rs.filename, std::ifstream::binary))
, entry_read(rs.cache.size())
, block_read(rs.block_read_for_cache)
, entry_count(rs.entry_count)
, block_size(rs.block_size)
{ }
struct Stats {
size_t cached_entries_read = 0;
size_t disk_entries_read = 0;
size_t blocks_read = 0;
size_t accepted_results = 0;
size_t rejected_results = 0;
};
Stats get_stats() const { return stats; }
template<typename OutIter>
void
get_samples(size_t sample_size, OutIter out_iter) {
while((sample_size > 0) && (cache_used < cache.size()))
{
Value const& v = cache[cache_used];
++cache_used;
++stats.cached_entries_read;
if(boost::geometry::covered_by(v.get_point(), query)) {
*out_iter = v;
++out_iter;
--sample_size;
stats.accepted_results++;
}
else {
stats.rejected_results++;
}
}
while(sample_size > 0)
{
if(buffer.empty())
{
if(entry_read >= entry_count) break;
buffer = rs.read_block(*pfs, block_read);
++stats.blocks_read;
++block_read;
stats.disk_entries_read += buffer.size();
entry_read += buffer.size();
}
Value v = buffer.back();
buffer.pop_back();
if(boost::geometry::covered_by(v.get_point(), query)) {
*out_iter = v;
++out_iter;
--sample_size;
stats.accepted_results++;
}
else {
stats.rejected_results++;
}
}
}
Geometry query;
RandomShuffle const& rs;
cache_t const& cache;
std::unique_ptr<std::ifstream> pfs;
int64_t entry_count;
size_t block_size;
size_t cache_used = 0;
size_t entry_read;
size_t block_read; // blocks read for cache
Stats stats;
std::vector<Value> buffer;
};
template<typename Geometry>
sample_query_cursor<Geometry>
sample_query(Geometry const& query) {
return sample_query_cursor<Geometry>(query, *this);
}
std::string filename;
int64_t entry_count;
size_t block_size;
size_t block_read_for_cache;
cache_t cache;
};
template<typename Value>
void RandomShuffle<Value>::build(
std::string const& in_filename,
std::string const& out_filename,
converter_t ReadConverter,
Parameters const& parameters,
size_t tpie_allowed_memory
)
{
throw "convert to using newer template class";
//tpie::tpie_init();
//tpie::get_memory_manager().set_limit(tpie_allowed_memory);
const char *tmp_filename = "./staging";
int64_t entry_count = 0;
// read plain text input file
// assign random id and write to binary output file
// see also IOLayers::convert_data_file
{
std::ifstream fin(in_filename);
//file_stream_t tmp_fout;
remove(tmp_filename); // make sure it is not there before trying to open the temp file.
//tmp_fout.open(tmp_filename);
std::default_random_engine rng(std::random_device{}());
std::uniform_int_distribution<int> random_id(std::numeric_limits<int>::min(), std::numeric_limits<int>::max());
std::string line;
while(fin)
{
std::getline(fin, line);
if(line.size() == 0) continue;
//tmp_fout.write(std::make_pair(
// (Value)std::move(ReadConverter(line)),
// random_id(rng)
// ));
++entry_count;
}
fin.close();
// sort based on random id
throw "sort by random id using new interface";
//tpie::progress_indicator_null pi;
//tpie::sort(
// tmp_fout,
// [](value_with_random_id_t const& p1, value_with_random_id_t const& p2)->bool{
// return p1.second < p2.second;
// },
// pi
// );
}
// dump to our own format
throw "update dumping of formating information";
//{
// std::ofstream fout(out_filename, std::ofstream::binary);
// fout.write(reinterpret_cast<char*>(&entry_count), sizeof(entry_count));
// size_t block_size = parameters.block_size;
// fout.write(reinterpret_cast<char*>(&block_size), sizeof(block_size));
// size_t block_capacity = (block_size - sizeof(int64_t)) / sizeof(value_with_random_id_t) * parameters.fill_ratio;
// file_stream_t tmp_fin;
// tmp_fin.open(tmp_filename);
// size_t entries_in_buffer = 0;
// char * buffer = new char[block_size];
// while(tmp_fin.can_read())
// {
// if(entries_in_buffer == block_capacity)
// {
// *reinterpret_cast<int64_t*>(buffer) = (int64_t) entries_in_buffer;
// fout.write(buffer, block_size);
// entries_in_buffer = 0;
// }
// value_with_random_id_t const& v = tmp_fin.read();
// memcpy(buffer + sizeof(int64_t) + sizeof(value_with_random_id_t) * entries_in_buffer, &v, sizeof(v));
// ++entries_in_buffer;
// }
// if(entries_in_buffer > 0)
// {
// *reinterpret_cast<int64_t*>(buffer) = (int64_t) entries_in_buffer;
// fout.write(buffer, block_size);
// entries_in_buffer = 0;
// }
// delete [] buffer;
// ::unlink(tmp_filename);
//}
}