19 #ifndef avro_DataFile_hh__ 20 #define avro_DataFile_hh__ 24 #include "buffer/Buffer.hh" 25 #include "ValidSchema.hh" 26 #include "Specific.hh" 34 #include "boost/utility.hpp" 35 #include <boost/iostreams/filtering_stream.hpp> 44 #ifdef SNAPPY_CODEC_AVAILABLE 50 const int SyncSize = 16;
62 const std::string filename_;
65 const size_t syncInterval_;
68 std::unique_ptr<OutputStream> stream_;
69 std::unique_ptr<OutputStream> buffer_;
70 const DataFileSync sync_;
73 typedef std::map<std::string, std::vector<uint8_t> > Metadata;
77 static std::unique_ptr<OutputStream> makeStream(
const char* filename);
78 static DataFileSync makeSync();
81 void setMetadata(
const std::string& key,
const std::string& value);
91 void init(
const ValidSchema &schema,
size_t syncInterval,
const Codec &codec);
115 size_t syncInterval,
Codec codec = NULL_CODEC);
140 template <
typename T>
142 std::unique_ptr<DataFileWriterBase> base_;
148 size_t syncInterval = 16 * 1024,
Codec codec = NULL_CODEC) :
152 size_t syncInterval = 16 * 1024,
Codec codec = NULL_CODEC) :
153 base_(
new DataFileWriterBase(std::move(outputStream), schema, syncInterval, codec)) { }
159 base_->syncIfNeeded();
185 const std::string filename_;
186 const std::unique_ptr<InputStream> stream_;
188 int64_t objectCount_;
197 std::unique_ptr<InputStream> dataStream_;
198 typedef std::map<std::string, std::vector<uint8_t> > Metadata;
204 std::unique_ptr<boost::iostreams::filtering_istream> os_;
205 std::vector<char> compressed_;
206 std::string uncompressed;
209 void readDataBlock();
210 void doSeek(int64_t position);
225 void decr() { --objectCount_; }
271 void seek(int64_t position);
278 void sync(int64_t position);
283 bool pastSync(int64_t position);
288 int64_t previousSync();
294 template <
typename T>
296 std::unique_ptr<DataFileReaderBase> base_;
304 base_->init(readerSchema);
309 base_->init(readerSchema);
350 base_->init(readerSchema);
359 if (base_->hasMore()) {
380 void close() {
return base_->close(); }
386 void seek(int64_t position) { base_->seek(position); }
393 void sync(int64_t position) { base_->sync(position); }
398 bool pastSync(int64_t position) {
return base_->pastSync(position); }
Low level support for encoding avro values.
Type-independent portion of DataFileWriter.
Definition: DataFile.hh:61
void decode(Decoder &d, T &t)
Generic decoder function that makes use of the codec_traits.
Definition: Specific.hh:339
const ValidSchema & schema() const
Returns the schema for this data file.
Definition: DataFile.hh:129
bool read(T &datum)
Reads the next entry from the data file.
Definition: DataFile.hh:358
const ValidSchema & readerSchema()
Returns the schema for this object.
Definition: DataFile.hh:255
const ValidSchema & schema() const
Returns the schema for this data file.
Definition: DataFile.hh:173
std::array< uint8_t, SyncSize > DataFileSync
The sync value.
Definition: DataFile.hh:54
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
void decr()
Decrements the number of objects yet to read.
Definition: DataFile.hh:225
void close()
Closes the current file.
Definition: DataFile.hh:168
DataFileReader(const char *filename, const ValidSchema &readerSchema)
Constructs the reader for the given file and the reader is expected to use the given schema...
Definition: DataFile.hh:302
int64_t previousSync()
Return the last synchronization point before our current position.
Definition: DataFile.hh:403
An Avro datafile that can store objects of type T.
Definition: DataFile.hh:141
void incr()
Increments the object count.
Definition: DataFile.hh:108
void flush()
Flushes any unwritten data into the file.
Definition: DataFile.hh:178
DataFileReader(const char *filename)
Constructs the reader for the given file and the reader is expected to use the schema that is used wi...
Definition: DataFile.hh:316
const ValidSchema & dataSchema()
Returns the schema stored with the data file.
Definition: DataFile.hh:375
std::shared_ptr< Encoder > EncoderPtr
Shared pointer to Encoder.
Definition: Encoder.hh:147
Decoder & decoder()
Returns the current decoder for this reader.
Definition: DataFile.hh:215
void write(const T &datum)
Writes the given piece of data into the file.
Definition: DataFile.hh:158
std::shared_ptr< Decoder > DecoderPtr
Shared pointer to Decoder.
Definition: Decoder.hh:177
Codec
Specify type of compression to use when writing data files.
Definition: DataFile.hh:40
void encode(Encoder &e, const T &t)
Generic encoder function that makes use of the codec_traits.
Definition: Specific.hh:331
const ValidSchema & readerSchema()
Returns the schema for this object.
Definition: DataFile.hh:370
The type independent portion of rader.
Definition: DataFile.hh:184
A ValidSchema is basically a non-mutable Schema that has passed some minumum of sanity checks...
Definition: ValidSchema.hh:40
void seek(int64_t position)
Move to a specific, known synchronization point, for example one returned from previousSync().
Definition: DataFile.hh:386
Reads the contents of data file one after another.
Definition: DataFile.hh:295
DataFileReader(std::unique_ptr< DataFileReaderBase > base, const ValidSchema &readerSchema)
Constructs a reader using the reader base.
Definition: DataFile.hh:348
const ValidSchema & dataSchema()
Returns the schema stored with the data file.
Definition: DataFile.hh:260
DataFileWriter(const char *filename, const ValidSchema &schema, size_t syncInterval=16 *1024, Codec codec=NULL_CODEC)
Constructs a new data file.
Definition: DataFile.hh:147
DataFileReader(std::unique_ptr< DataFileReaderBase > base)
Constructs a reader using the reader base.
Definition: DataFile.hh:335
bool pastSync(int64_t position)
Return true if past the next synchronization point after a position.
Definition: DataFile.hh:398
The abstract base class for all Avro encoders.
Definition: Encoder.hh:52
Encoder & encoder() const
Returns the current encoder for this writer.
Definition: DataFile.hh:97
void close()
Closes the reader.
Definition: DataFile.hh:380
void sync(int64_t position)
Move to the next synchronization point after a position.
Definition: DataFile.hh:393
Decoder is an interface implemented by every decoder capable of decoding Avro data.
Definition: Decoder.hh:48