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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef avro_DataFile_hh__
#define avro_DataFile_hh__
#include "Config.hh"
#include "Encoder.hh"
#include "buffer/Buffer.hh"
#include "ValidSchema.hh"
#include "Specific.hh"
#include "Stream.hh"
#include <map>
#include <string>
#include <vector>
#include "array"
#include "boost/utility.hpp"
#include <boost/iostreams/filtering_stream.hpp>
namespace avro {
/** Specify type of compression to use when writing data files. */
enum Codec {
NULL_CODEC,
DEFLATE_CODEC,
#ifdef SNAPPY_CODEC_AVAILABLE
SNAPPY_CODEC
#endif
};
const int SyncSize = 16;
/**
* The sync value.
*/
typedef std::array<uint8_t, SyncSize> DataFileSync;
/**
* Type-independent portion of DataFileWriter.
* At any given point in time, at most one file can be written using
* this object.
*/
class AVRO_DECL DataFileWriterBase : boost::noncopyable {
const std::string filename_;
const ValidSchema schema_;
const EncoderPtr encoderPtr_;
const size_t syncInterval_;
Codec codec_;
std::unique_ptr<OutputStream> stream_;
std::unique_ptr<OutputStream> buffer_;
const DataFileSync sync_;
int64_t objectCount_;
typedef std::map<std::string, std::vector<uint8_t> > Metadata;
Metadata metadata_;
int64_t lastSync_;
static std::unique_ptr<OutputStream> makeStream(const char* filename);
static DataFileSync makeSync();
void writeHeader();
void setMetadata(const std::string& key, const std::string& value);
/**
* Generates a sync marker in the file.
*/
void sync();
/**
* Shared constructor portion since we aren't using C++11
*/
void init(const ValidSchema &schema, size_t syncInterval, const Codec &codec);
public:
/**
* Returns the current encoder for this writer.
*/
Encoder& encoder() const { return *encoderPtr_; }
/**
* Returns true if the buffer has sufficient data for a sync to be
* inserted.
*/
void syncIfNeeded();
/**
* Returns the byte offset (within the current file) of the start of the current block being written.
*/
uint64_t getCurrentBlockStart();
/**
* Increments the object count.
*/
void incr() {
++objectCount_;
}
/**
* Constructs a data file writer with the given sync interval and name.
*/
DataFileWriterBase(const char* filename, const ValidSchema& schema,
size_t syncInterval, Codec codec = NULL_CODEC);
DataFileWriterBase(std::unique_ptr<OutputStream> outputStream,
const ValidSchema& schema, size_t syncInterval, Codec codec);
~DataFileWriterBase();
/**
* Closes the current file. Once closed this datafile object cannot be
* used for writing any more.
*/
void close();
/**
* Returns the schema for this data file.
*/
const ValidSchema& schema() const { return schema_; }
/**
* Flushes any unwritten data into the file.
*/
void flush();
};
/**
* An Avro datafile that can store objects of type T.
*/
template <typename T>
class DataFileWriter : boost::noncopyable {
std::unique_ptr<DataFileWriterBase> base_;
public:
/**
* Constructs a new data file.
*/
DataFileWriter(const char* filename, const ValidSchema& schema,
size_t syncInterval = 16 * 1024, Codec codec = NULL_CODEC) :
base_(new DataFileWriterBase(filename, schema, syncInterval, codec)) { }
DataFileWriter(std::unique_ptr<OutputStream> outputStream, const ValidSchema& schema,
size_t syncInterval = 16 * 1024, Codec codec = NULL_CODEC) :
base_(new DataFileWriterBase(std::move(outputStream), schema, syncInterval, codec)) { }
/**
* Writes the given piece of data into the file.
*/
void write(const T& datum) {
base_->syncIfNeeded();
avro::encode(base_->encoder(), datum);
base_->incr();
}
/**
* Returns the byte offset (within the current file) of the start of the current block being written.
*/
uint64_t getCurrentBlockStart() { return base_->getCurrentBlockStart(); }
/**
* Closes the current file. Once closed this datafile object cannot be
* used for writing any more.
*/
void close() { base_->close(); }
/**
* Returns the schema for this data file.
*/
const ValidSchema& schema() const { return base_->schema(); }
/**
* Flushes any unwritten data into the file.
*/
void flush() { base_->flush(); }
};
/**
* The type independent portion of reader.
*/
class AVRO_DECL DataFileReaderBase : boost::noncopyable {
const std::string filename_;
const std::unique_ptr<InputStream> stream_;
const DecoderPtr decoder_;
int64_t objectCount_;
bool eof_;
Codec codec_;
int64_t blockStart_;
int64_t blockEnd_;
ValidSchema readerSchema_;
ValidSchema dataSchema_;
DecoderPtr dataDecoder_;
std::unique_ptr<InputStream> dataStream_;
typedef std::map<std::string, std::vector<uint8_t> > Metadata;
Metadata metadata_;
DataFileSync sync_;
// for compressed buffer
std::unique_ptr<boost::iostreams::filtering_istream> os_;
std::vector<char> compressed_;
std::string uncompressed;
void readHeader();
void readDataBlock();
void doSeek(int64_t position);
public:
/**
* Returns the current decoder for this reader.
*/
Decoder& decoder() { return *dataDecoder_; }
/**
* Returns true if and only if there is more to read.
*/
bool hasMore();
/**
* Decrements the number of objects yet to read.
*/
void decr() { --objectCount_; }
/**
* Constructs the reader for the given file and the reader is
* expected to use the schema that is used with data.
* This function should be called exactly once after constructing
* the DataFileReaderBase object.
*/
DataFileReaderBase(const char* filename);
DataFileReaderBase(std::unique_ptr<InputStream> inputStream);
/**
* Initializes the reader so that the reader and writer schemas
* are the same.
*/
void init();
/**
* Initializes the reader to read objects according to the given
* schema. This gives an opportunity for the reader to see the schema
* in the data file before deciding the right schema to use for reading.
* This must be called exactly once after constructing the
* DataFileReaderBase object.
*/
void init(const ValidSchema& readerSchema);
/**
* Returns the schema for this object.
*/
const ValidSchema& readerSchema() { return readerSchema_; }
/**
* Returns the schema stored with the data file.
*/
const ValidSchema& dataSchema() { return dataSchema_; }
/**
* Closes the reader. No further operation is possible on this reader.
*/
void close();
/**
* Move to a specific, known synchronization point, for example one returned
* from tell() after sync().
*/
void seek(int64_t position);
/**
* Move to the next synchronization point after a position. To process a
* range of file entries, call this with the starting position, then check
* pastSync() with the end point before each use of decoder().
*/
void sync(int64_t position);
/**
* Return true if past the next synchronization point after a position.
*/
bool pastSync(int64_t position);
/**
* Return the last synchronization point before our current position.
*/
int64_t previousSync();
};
/**
* Reads the contents of data file one after another.
*/
template <typename T>
class DataFileReader : boost::noncopyable {
std::unique_ptr<DataFileReaderBase> base_;
public:
/**
* Constructs the reader for the given file and the reader is
* expected to use the given schema.
*/
DataFileReader(const char* filename, const ValidSchema& readerSchema) :
base_(new DataFileReaderBase(filename)) {
base_->init(readerSchema);
}
DataFileReader(std::unique_ptr<InputStream> inputStream, const ValidSchema& readerSchema) :
base_(new DataFileReaderBase(std::move(inputStream))) {
base_->init(readerSchema);
}
/**
* Constructs the reader for the given file and the reader is
* expected to use the schema that is used with data.
*/
DataFileReader(const char* filename) :
base_(new DataFileReaderBase(filename)) {
base_->init();
}
DataFileReader(std::unique_ptr<InputStream> inputStream) :
base_(new DataFileReaderBase(std::move(inputStream))) {
base_->init();
}
/**
* Constructs a reader using the reader base. This form of constructor
* allows the user to examine the schema of a given file and then
* decide to use the right type of data to be deserialize. Without this
* the user must know the type of data for the template _before_
* he knows the schema within the file.
* The schema present in the data file will be used for reading
* from this reader.
*/
DataFileReader(std::unique_ptr<DataFileReaderBase> base) : base_(std::move(base)) {
base_->init();
}
/**
* Constructs a reader using the reader base. This form of constructor
* allows the user to examine the schema of a given file and then
* decide to use the right type of data to be deserialize. Without this
* the user must know the type of data for the template _before_
* he knows the schema within the file.
* The argument readerSchema will be used for reading
* from this reader.
*/
DataFileReader(std::unique_ptr<DataFileReaderBase> base,
const ValidSchema& readerSchema) : base_(std::move(base)) {
base_->init(readerSchema);
}
/**
* Reads the next entry from the data file.
* \return true if an object has been successfully read into \p datum and
* false if there are no more entries in the file.
*/
bool read(T& datum) {
if (base_->hasMore()) {
base_->decr();
avro::decode(base_->decoder(), datum);
return true;
}
return false;
}
/**
* Returns the schema for this object.
*/
const ValidSchema& readerSchema() { return base_->readerSchema(); }
/**
* Returns the schema stored with the data file.
*/
const ValidSchema& dataSchema() { return base_->dataSchema(); }
/**
* Closes the reader. No further operation is possible on this reader.
*/
void close() { return base_->close(); }
/**
* Move to a specific, known synchronization point, for example one returned
* from previousSync().
*/
void seek(int64_t position) { base_->seek(position); }
/**
* Move to the next synchronization point after a position. To process a
* range of file entries, call this with the starting position, then check
* pastSync() with the end point before each call to read().
*/
void sync(int64_t position) { base_->sync(position); }
/**
* Return true if past the next synchronization point after a position.
*/
bool pastSync(int64_t position) { return base_->pastSync(position); }
/**
* Return the last synchronization point before our current position.
*/
int64_t previousSync() { return base_->previousSync(); }
};
} // namespace avro
#endif
|