Debian Codegraph @

src/dmlc-core/include/dmlc/recordio.h

raw symbols SCIP
1 /*!
2 * Copyright (c) 2015 by Contributors
3 * \file recordio.h
4 * \brief recordio that is able to pack binary data into a splittable
5 * format, useful to exchange data in binary serialization,
6 * such as binary raw data or protobuf
7 */
8 #ifndef DMLC_RECORDIO_H_
9 #define DMLC_RECORDIO_H_
10 #include <cstring>
11 #include <string>
12
13 #include "./io.h"
14 #include "./logging.h"
15
16 namespace dmlc {
17 /*!
18 * \brief writer of binary recordio
19 * binary format for recordio
20 * recordio format: magic lrecord data pad
21 *
22 * - magic is magic number
23 * - pad is simply a padding space to make record align to 4 bytes
24 * - lrecord encodes length and continue bit
25 * - data.length() = (lrecord & (1U<<29U - 1));
26 * - cflag == (lrecord >> 29U) & 7;
27 *
28 * cflag was used to handle (rare) special case when magic number
29 * occured in the data sequence.
30 *
31 * In such case, the data is splitted into multiple records by
32 * the cells of magic number
33 *
34 * (1) cflag == 0: this is a complete record;
35 * (2) cflag == 1: start of a multiple-rec;
36 * cflag == 2: middle of multiple-rec;
37 * cflag == 3: end of multiple-rec
38 */
39 class RecordIOWriter {
40 public:
41 /*!
42 * \brief magic number of recordio
43 * note: (kMagic >> 29U) & 7 > 3
44 * this ensures lrec will not be kMagic
45 */
46 static const uint32_t kMagic = 0xced7230a;
47 /*!
48 * \brief encode the lrecord
49 * \param cflag cflag part of the lrecord
50 * \param length length part of lrecord
51 * \return the encoded data
52 */
53 inline static uint32_t EncodeLRec(uint32_t cflag, uint32_t length) {
54 return (cflag << 29U) | length;
55 }
56 /*!
57 * \brief decode the flag part of lrecord
58 * \param rec the lrecord
59 * \return the flag
60 */
61 inline static uint32_t DecodeFlag(uint32_t rec) {
62 return (rec >> 29U) & 7U;
63 }
64 /*!
65 * \brief decode the length part of lrecord
66 * \param rec the lrecord
67 * \return the length
68 */
69 inline static uint32_t DecodeLength(uint32_t rec) {
70 return rec & ((1U << 29U) - 1U);
71 }
72 /*!
73 * \brief constructor
74 * \param stream the stream to be constructed
75 */
76 explicit RecordIOWriter(Stream *stream)
77 : stream_(stream), seek_stream_(dynamic_cast<SeekStream *>(stream)), except_counter_(0) {
78 CHECK(sizeof(uint32_t) == 4) << "uint32_t needs to be 4 bytes";
79 }
80 /*!
81 * \brief write record to the stream
82 * \param buf the buffer of memory region
83 * \param size the size of record to write out
84 */
85 void WriteRecord(const void *buf, size_t size);
86 /*!
87 * \brief write record to the stream
88 * \param data the data to write out
89 */
90 inline void WriteRecord(const std::string &data) {
91 this->WriteRecord(data.c_str(), data.length());
92 }
93 /*!
94 * \return number of exceptions(occurance of magic number)
95 * during the writing process
96 */
97 inline size_t except_counter(void) const {
98 return except_counter_;
99 }
100
101 /*! \brief tell the current position of the input stream */
102 inline size_t Tell(void) {
103 CHECK(seek_stream_ != NULL) << "The input stream is not seekable";
104 return seek_stream_->Tell();
105 }
106
107 private:
108 /*! \brief output stream */
109 Stream *stream_;
110 /*! \brief seekable stream */
111 SeekStream *seek_stream_;
112 /*! \brief counts the number of exceptions */
113 size_t except_counter_;
114 };
115 /*!
116 * \brief reader of binary recordio to reads in record from stream
117 * \sa RecordIOWriter
118 */
119 class RecordIOReader {
120 public:
121 /*!
122 * \brief constructor
123 * \param stream the stream to be constructed
124 */
125 explicit RecordIOReader(Stream *stream)
126 : stream_(stream), seek_stream_(dynamic_cast<SeekStream *>(stream)), end_of_stream_(false) {
127 CHECK(sizeof(uint32_t) == 4) << "uint32_t needs to be 4 bytes";
128 }
129 /*!
130 * \brief read next complete record from stream
131 * \param out_rec used to store output record in string
132 * \return true of read was successful, false if end of stream was reached
133 */
134 bool NextRecord(std::string *out_rec);
135
136 /*! \brief seek to certain position of the input stream */
137 inline void Seek(size_t pos) {
138 CHECK(seek_stream_ != NULL) << "The input stream is not seekable";
139 seek_stream_->Seek(pos);
140 }
141
142 /*! \brief tell the current position of the input stream */
143 inline size_t Tell(void) {
144 CHECK(seek_stream_ != NULL) << "The input stream is not seekable";
145 return seek_stream_->Tell();
146 }
147
148 private:
149 /*! \brief output stream */
150 Stream *stream_;
151 SeekStream *seek_stream_;
152 /*! \brief whether we are at end of stream */
153 bool end_of_stream_;
154 };
155
156 /*!
157 * \brief reader of binary recordio from Blob returned by InputSplit
158 * This class divides the blob into several independent parts specified by caller,
159 * and read from one segment.
160 * The part reading can be used together with InputSplit::NextChunk for
161 * multi-threaded parsing(each thread take a RecordIOChunkReader)
162 *
163 * \sa RecordIOWriter, InputSplit
164 */
165 class RecordIOChunkReader {
166 public:
167 /*!
168 * \brief constructor
169 * \param chunk source data returned by InputSplit
170 * \param part_index which part we want to reado
171 * \param num_parts number of total segments
172 */
173 explicit RecordIOChunkReader(
174 InputSplit::Blob chunk, unsigned part_index = 0, unsigned num_parts = 1);
175 /*!
176 * \brief read next complete record from stream
177 * the blob contains the memory content
178 * NOTE: this function is not threadsafe, use one
179 * RecordIOChunkReader per thread
180 * \param out_rec used to store output blob, the header is already
181 * removed and out_rec only contains the memory content
182 * \return true of read was successful, false if end was reached
183 */
184 bool NextRecord(InputSplit::Blob *out_rec);
185
186 private:
187 /*! \brief internal temporal data */
188 std::string temp_;
189 /*! \brief internal data pointer */
190 char *pbegin_, *pend_;
191 };
192
193 } // namespace dmlc
194 #endif // DMLC_RECORDIO_H_