Avro C++
Reader.hh
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef avro_Reader_hh__
20 #define avro_Reader_hh__
21 
22 #include <stdint.h>
23 #include <vector>
24 #include <array>
25 #include <boost/noncopyable.hpp>
26 
27 #include "Config.hh"
28 #include "Zigzag.hh"
29 #include "Types.hh"
30 #include "Validator.hh"
31 #include "buffer/BufferReader.hh"
32 
33 namespace avro {
34 
39 
40 template<class ValidatorType>
41 class ReaderImpl : private boost::noncopyable
42 {
43 
44  public:
45 
46  explicit ReaderImpl(const InputBuffer &buffer) :
47  reader_(buffer)
48  {}
49 
50  ReaderImpl(const ValidSchema &schema, const InputBuffer &buffer) :
51  validator_(schema),
52  reader_(buffer)
53  {}
54 
55  void readValue(Null &) {
56  validator_.checkTypeExpected(AVRO_NULL);
57  }
58 
59  void readValue(bool &val) {
60  validator_.checkTypeExpected(AVRO_BOOL);
61  uint8_t ival = 0;
62  reader_.read(ival);
63  val = (ival != 0);
64  }
65 
66  void readValue(int32_t &val) {
67  validator_.checkTypeExpected(AVRO_INT);
68  uint32_t encoded = static_cast<uint32_t>(readVarInt());
69  val = decodeZigzag32(encoded);
70  }
71 
72  void readValue(int64_t &val) {
73  validator_.checkTypeExpected(AVRO_LONG);
74  uint64_t encoded = readVarInt();
75  val = decodeZigzag64(encoded);
76  }
77 
78  void readValue(float &val) {
79  validator_.checkTypeExpected(AVRO_FLOAT);
80  union {
81  float f;
82  uint32_t i;
83  } v;
84  reader_.read(v.i);
85  val = v.f;
86  }
87 
88  void readValue(double &val) {
89  validator_.checkTypeExpected(AVRO_DOUBLE);
90  union {
91  double d;
92  uint64_t i;
93  } v;
94  reader_.read(v.i);
95  val = v.d;
96  }
97 
98  void readValue(std::string &val) {
99  validator_.checkTypeExpected(AVRO_STRING);
100  size_t size = static_cast<size_t>(readSize());
101  reader_.read(val, size);
102  }
103 
104  void readBytes(std::vector<uint8_t> &val) {
105  validator_.checkTypeExpected(AVRO_BYTES);
106  size_t size = static_cast<size_t>(readSize());
107  val.resize(size);
108  reader_.read(reinterpret_cast<char *>(val.data()), size);
109  }
110 
111  void readFixed(uint8_t *val, size_t size) {
112  validator_.checkFixedSizeExpected(size);
113  reader_.read(reinterpret_cast<char *>(val), size);
114  }
115 
116  template <size_t N>
117  void readFixed(uint8_t (&val)[N]) {
118  this->readFixed(val, N);
119  }
120 
121  template <size_t N>
122  void readFixed(std::array<uint8_t, N> &val) {
123  this->readFixed(val.data(), N);
124  }
125 
126  void readRecord() {
127  validator_.checkTypeExpected(AVRO_RECORD);
128  validator_.checkTypeExpected(AVRO_LONG);
129  validator_.setCount(1);
130  }
131 
132  void readRecordEnd() {
133  validator_.checkTypeExpected(AVRO_RECORD);
134  validator_.checkTypeExpected(AVRO_LONG);
135  validator_.setCount(0);
136  }
137 
138  int64_t readArrayBlockSize() {
139  validator_.checkTypeExpected(AVRO_ARRAY);
140  return readCount();
141  }
142 
143  int64_t readUnion() {
144  validator_.checkTypeExpected(AVRO_UNION);
145  return readCount();
146  }
147 
148  int64_t readEnum() {
149  validator_.checkTypeExpected(AVRO_ENUM);
150  return readCount();
151  }
152 
153  int64_t readMapBlockSize() {
154  validator_.checkTypeExpected(AVRO_MAP);
155  return readCount();
156  }
157 
158  Type nextType() const {
159  return validator_.nextTypeExpected();
160  }
161 
162  bool currentRecordName(std::string &name) const {
163  return validator_.getCurrentRecordName(name);
164  }
165 
166  bool nextFieldName(std::string &name) const {
167  return validator_.getNextFieldName(name);
168  }
169 
170  private:
171 
172  uint64_t readVarInt() {
173  uint64_t encoded = 0;
174  uint8_t val = 0;
175  int shift = 0;
176  do {
177  reader_.read(val);
178  uint64_t newbits = static_cast<uint64_t>(val & 0x7f) << shift;
179  encoded |= newbits;
180  shift += 7;
181  } while (val & 0x80);
182 
183  return encoded;
184  }
185 
186  int64_t readSize() {
187  uint64_t encoded = readVarInt();
188  int64_t size = decodeZigzag64(encoded);
189  return size;
190  }
191 
192  int64_t readCount() {
193  validator_.checkTypeExpected(AVRO_LONG);
194  int64_t count = readSize();
195  validator_.setCount(count);
196  return count;
197  }
198 
199  ValidatorType validator_;
200  BufferReader reader_;
201 
202 };
203 
206 
207 } // namespace avro
208 
209 #endif
Definition: Types.hh:37
Definition: Types.hh:45
Definition: Types.hh:40
Type
The "type" for the schema.
Definition: Types.hh:31
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
Definition: Types.hh:43
Definition: Types.hh:39
Definition: Types.hh:33
Definition: Types.hh:44
Definition: Types.hh:35
Functions for encoding and decoding integers with zigzag compression.
Definition: Types.hh:34
Definition: Types.hh:38
Parses from an avro encoding to the requested type.
Definition: Reader.hh:41
define a type to identify Null in template functions
Definition: Types.hh:102
A ValidSchema is basically a non-mutable Schema that has passed some minumum of sanity checks...
Definition: ValidSchema.hh:40
Definition: Types.hh:42
Definition: Types.hh:46
Definition: Types.hh:36