Avro C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
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 <boost/noncopyable.hpp>
25 
26 #include "Config.hh"
27 #include "Zigzag.hh"
28 #include "Types.hh"
29 #include "Validator.hh"
30 #include "buffer/BufferReader.hh"
31 
32 namespace avro {
33 
38 
39 template<class ValidatorType>
40 class ReaderImpl : private boost::noncopyable
41 {
42 
43  public:
44 
45  explicit ReaderImpl(const InputBuffer &buffer) :
46  reader_(buffer)
47  {}
48 
49  ReaderImpl(const ValidSchema &schema, const InputBuffer &buffer) :
50  validator_(schema),
51  reader_(buffer)
52  {}
53 
54  void readValue(Null &) {
55  validator_.checkTypeExpected(AVRO_NULL);
56  }
57 
58  void readValue(bool &val) {
59  validator_.checkTypeExpected(AVRO_BOOL);
60  uint8_t ival = 0;
61  reader_.read(ival);
62  val = (ival != 0);
63  }
64 
65  void readValue(int32_t &val) {
66  validator_.checkTypeExpected(AVRO_INT);
67  uint32_t encoded = static_cast<uint32_t>(readVarInt());
68  val = decodeZigzag32(encoded);
69  }
70 
71  void readValue(int64_t &val) {
72  validator_.checkTypeExpected(AVRO_LONG);
73  uint64_t encoded = readVarInt();
74  val = decodeZigzag64(encoded);
75  }
76 
77  void readValue(float &val) {
78  validator_.checkTypeExpected(AVRO_FLOAT);
79  union {
80  float f;
81  uint32_t i;
82  } v;
83  reader_.read(v.i);
84  val = v.f;
85  }
86 
87  void readValue(double &val) {
88  validator_.checkTypeExpected(AVRO_DOUBLE);
89  union {
90  double d;
91  uint64_t i;
92  } v;
93  reader_.read(v.i);
94  val = v.d;
95  }
96 
97  void readValue(std::string &val) {
98  validator_.checkTypeExpected(AVRO_STRING);
99  size_t size = static_cast<size_t>(readSize());
100  reader_.read(val, size);
101  }
102 
103  void readBytes(std::vector<uint8_t> &val) {
104  validator_.checkTypeExpected(AVRO_BYTES);
105  size_t size = static_cast<size_t>(readSize());
106  val.resize(size);
107  reader_.read(reinterpret_cast<char *>(&val[0]), size);
108  }
109 
110  void readFixed(uint8_t *val, size_t size) {
111  validator_.checkFixedSizeExpected(size);
112  reader_.read(reinterpret_cast<char *>(val), size);
113  }
114 
115  template <size_t N>
116  void readFixed(uint8_t (&val)[N]) {
117  this->readFixed(val, N);
118  }
119 
120  template <size_t N>
121  void readFixed(boost::array<uint8_t, N> &val) {
122  this->readFixed(val.c_array(), N);
123  }
124 
125  void readRecord() {
126  validator_.checkTypeExpected(AVRO_RECORD);
127  validator_.checkTypeExpected(AVRO_LONG);
128  validator_.setCount(1);
129  }
130 
131  void readRecordEnd() {
132  validator_.checkTypeExpected(AVRO_RECORD);
133  validator_.checkTypeExpected(AVRO_LONG);
134  validator_.setCount(0);
135  }
136 
137  int64_t readArrayBlockSize() {
138  validator_.checkTypeExpected(AVRO_ARRAY);
139  return readCount();
140  }
141 
142  int64_t readUnion() {
143  validator_.checkTypeExpected(AVRO_UNION);
144  return readCount();
145  }
146 
147  int64_t readEnum() {
148  validator_.checkTypeExpected(AVRO_ENUM);
149  return readCount();
150  }
151 
152  int64_t readMapBlockSize() {
153  validator_.checkTypeExpected(AVRO_MAP);
154  return readCount();
155  }
156 
157  Type nextType() const {
158  return validator_.nextTypeExpected();
159  }
160 
161  bool currentRecordName(std::string &name) const {
162  return validator_.getCurrentRecordName(name);
163  }
164 
165  bool nextFieldName(std::string &name) const {
166  return validator_.getNextFieldName(name);
167  }
168 
169  private:
170 
171  uint64_t readVarInt() {
172  uint64_t encoded = 0;
173  uint8_t val = 0;
174  int shift = 0;
175  do {
176  reader_.read(val);
177  uint64_t newbits = static_cast<uint64_t>(val & 0x7f) << shift;
178  encoded |= newbits;
179  shift += 7;
180  } while (val & 0x80);
181 
182  return encoded;
183  }
184 
185  int64_t readSize() {
186  uint64_t encoded = readVarInt();
187  int64_t size = decodeZigzag64(encoded);
188  return size;
189  }
190 
191  int64_t readCount() {
192  validator_.checkTypeExpected(AVRO_LONG);
193  int64_t count = readSize();
194  validator_.setCount(count);
195  return count;
196  }
197 
198  ValidatorType validator_;
199  BufferReader reader_;
200 
201 };
202 
205 
206 } // namespace avro
207 
208 #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:31
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:40
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