forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buffer.h
270 lines (238 loc) · 5.73 KB
/
Buffer.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
/*
* Buffer.h
*
*/
#ifndef TOOLS_BUFFER_H_
#define TOOLS_BUFFER_H_
#include <fstream>
#include <iostream>
#include <assert.h>
using namespace std;
#include "Math/field_types.h"
#include "Tools/time-func.h"
#include "Tools/octetStream.h"
#include "Tools/pprint.h"
#include "Processor/OnlineOptions.h"
#ifndef BUFFER_SIZE
#define BUFFER_SIZE 101
#endif
class BufferBase
{
protected:
static bool rewind;
ifstream* file;
int next;
string data_type;
string field_type;
Timer timer;
int tuple_length;
string filename;
int header_length;
virtual int element_length() = 0;
public:
bool eof;
BufferBase() : file(0), next(BUFFER_SIZE),
tuple_length(-1), header_length(0), eof(false) {}
~BufferBase() {}
virtual ifstream* open() = 0;
void setup(ifstream* f, int length, const string& filename,
const char* type = "", const string& field = {});
void seekg(int pos);
bool is_up() { return file != 0; }
bool is_pipe();
void try_rewind();
void prune();
void purge();
void check_tuple_length(int tuple_length);
};
template <class T, class U>
class Buffer : public BufferBase
{
T buffer[BUFFER_SIZE];
void read(char* read_buffer);
int element_length() { return T::size(); }
public:
virtual ~Buffer();
virtual ifstream* open();
void input(U& a);
void fill_buffer();
};
template<class T>
octetStream file_signature(const typename T::mac_type& mac_key = {})
{
octetStream res(T::type_string());
T::specification(res);
if (T::has_mac)
{
if (mac_key == typename T::mac_type())
T::get_mac_key().pack(res);
else
mac_key.pack(res);
}
return res;
}
template<class T>
octetStream check_file_signature(ifstream& file, const string& filename)
{
octetStream file_spec;
try
{
file_spec.input(file);
}
catch (bad_alloc&)
{
throw signature_mismatch(filename);
}
catch (IO_Error&)
{
throw signature_mismatch(filename);
}
if (file_signature<T>() != file_spec)
{
#ifndef DEBUG_FILE_SIGNATURE
if (OnlineOptions::singleton.has_option("debug_file_signature"))
#endif
{
auto exp = file_signature<T>();
pprint_bytes("found ", file_spec.get_data(), file_spec.get_length());
pprint_bytes("expected", exp.get_data(), exp.get_length());
}
throw signature_mismatch(filename);
}
return file_spec;
}
template<class U, class V, class W = U>
class BufferOwner : public Buffer<U, V>
{
ifstream* file;
public:
BufferOwner() :
file(0)
{
}
BufferOwner(const BufferOwner& other) :
file(0)
{
*this = other;
}
~BufferOwner()
{
close();
}
BufferOwner& operator=(const BufferOwner& other)
{
assert(other.file == 0);
file = 0;
Buffer<U, V>::operator=(other);
return *this;
}
ifstream* open()
{
file = new ifstream(this->filename, ios::in | ios::binary);
BufferBase::file = file;
if (file->good())
{
auto file_spec = check_file_signature<W>(*file, this->filename);
this->header_length = file_spec.get_length()
+ sizeof(file_spec.get_length());
}
return file;
}
void setup(const string& filename, int tuple_length,
const char* data_type = "")
{
Buffer<U, V>::setup(file, tuple_length, filename, data_type,
U::type_string());
}
void setup(const string& filename, int tuple_length,
const string& type_string, const char* data_type = "")
{
Buffer<U, V>::setup(file, tuple_length, filename, data_type,
type_string);
}
void close()
{
if (file)
delete file;
file = 0;
}
};
template<class T, class U>
inline Buffer<T, U>::~Buffer()
{
#ifdef VERBOSE
if (timer.elapsed() && data_type.size())
cerr << T::type_string() << " " << data_type << " reading: "
<< timer.elapsed() << endl;
#endif
}
template<class T, class U>
inline void Buffer<T, U>::fill_buffer()
{
if (T::size() == sizeof(T))
{
// read directly
read((char*)buffer);
}
else
{
char* read_buffer = new char[BUFFER_SIZE * T::size()];
read(read_buffer);
//memset(buffer, 0, sizeof(buffer));
for (int i = 0; i < BUFFER_SIZE; i++)
buffer[i].assign(&read_buffer[i*T::size()]);
delete[] read_buffer;
}
}
template<class T, class U>
ifstream* Buffer<T, U>::open()
{
throw IO_Error(T::type_string() + " buffer not set up");
}
template<class T, class U>
inline void Buffer<T, U>::read(char* read_buffer)
{
int size_in_bytes = T::size() * BUFFER_SIZE;
int n_read = 0;
timer.start();
if (not file)
file = open();
do
{
file->read(read_buffer + n_read, size_in_bytes - n_read);
n_read += file->gcount();
#ifdef DEBUG_BUFFER
fprintf(stderr, "read %d\n", n_read);
#endif
if (file->eof())
{
try_rewind();
}
if (file->fail())
{
stringstream ss;
ss << "IO problem when buffering " << T::type_string();
if (data_type.size())
ss << " " << data_type;
ss << " from " << filename;
throw file_error(ss.str());
}
}
while (n_read < size_in_bytes);
timer.stop();
}
template <class T, class U>
inline void Buffer<T,U>::input(U& a)
{
#ifdef DEBUG_BUFFER
fprintf(stderr, "next is %d\n", next);
#endif
if (next == BUFFER_SIZE)
{
fill_buffer();
next = 0;
}
a = buffer[next];
next++;
}
#endif /* TOOLS_BUFFER_H_ */