Reader.hh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. * https://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. #ifndef avro_Reader_hh__
  19. #define avro_Reader_hh__
  20. #include <array>
  21. #include <boost/noncopyable.hpp>
  22. #include <cstdint>
  23. #include <vector>
  24. #include "Config.hh"
  25. #include "Types.hh"
  26. #include "Validator.hh"
  27. #include "Zigzag.hh"
  28. #include "buffer/BufferReader.hh"
  29. namespace avro {
  30. ///
  31. /// Parses from an avro encoding to the requested type. Assumes the next item
  32. /// in the avro binary data is the expected type.
  33. ///
  34. template<class ValidatorType>
  35. class ReaderImpl : private boost::noncopyable {
  36. public:
  37. explicit ReaderImpl(const InputBuffer &buffer) : reader_(buffer) {}
  38. ReaderImpl(const ValidSchema &schema, const InputBuffer &buffer) : validator_(schema),
  39. reader_(buffer) {}
  40. void readValue(Null &) {
  41. validator_.checkTypeExpected(AVRO_NULL);
  42. }
  43. void readValue(bool &val) {
  44. validator_.checkTypeExpected(AVRO_BOOL);
  45. uint8_t intVal = 0;
  46. reader_.read(intVal);
  47. val = (intVal != 0);
  48. }
  49. void readValue(int32_t &val) {
  50. validator_.checkTypeExpected(AVRO_INT);
  51. auto encoded = static_cast<uint32_t>(readVarInt());
  52. val = decodeZigzag32(encoded);
  53. }
  54. void readValue(int64_t &val) {
  55. validator_.checkTypeExpected(AVRO_LONG);
  56. uint64_t encoded = readVarInt();
  57. val = decodeZigzag64(encoded);
  58. }
  59. void readValue(float &val) {
  60. validator_.checkTypeExpected(AVRO_FLOAT);
  61. union {
  62. float f;
  63. uint32_t i;
  64. } v;
  65. reader_.read(v.i);
  66. val = v.f;
  67. }
  68. void readValue(double &val) {
  69. validator_.checkTypeExpected(AVRO_DOUBLE);
  70. union {
  71. double d;
  72. uint64_t i;
  73. } v = { 0 };
  74. reader_.read(v.i);
  75. val = v.d;
  76. }
  77. void readValue(std::string &val) {
  78. validator_.checkTypeExpected(AVRO_STRING);
  79. auto size = static_cast<size_t>(readSize());
  80. reader_.read(val, size);
  81. }
  82. void readBytes(std::vector<uint8_t> &val) {
  83. validator_.checkTypeExpected(AVRO_BYTES);
  84. auto size = static_cast<size_t>(readSize());
  85. val.resize(size);
  86. reader_.read(reinterpret_cast<char *>(val.data()), size);
  87. }
  88. void readFixed(uint8_t *val, size_t size) {
  89. validator_.checkFixedSizeExpected(size);
  90. reader_.read(reinterpret_cast<char *>(val), size);
  91. }
  92. template<size_t N>
  93. void readFixed(uint8_t (&val)[N]) {
  94. this->readFixed(val, N);
  95. }
  96. template<size_t N>
  97. void readFixed(std::array<uint8_t, N> &val) {
  98. this->readFixed(val.data(), N);
  99. }
  100. void readRecord() {
  101. validator_.checkTypeExpected(AVRO_RECORD);
  102. validator_.checkTypeExpected(AVRO_LONG);
  103. validator_.setCount(1);
  104. }
  105. void readRecordEnd() {
  106. validator_.checkTypeExpected(AVRO_RECORD);
  107. validator_.checkTypeExpected(AVRO_LONG);
  108. validator_.setCount(0);
  109. }
  110. int64_t readArrayBlockSize() {
  111. validator_.checkTypeExpected(AVRO_ARRAY);
  112. return readCount();
  113. }
  114. int64_t readUnion() {
  115. validator_.checkTypeExpected(AVRO_UNION);
  116. return readCount();
  117. }
  118. int64_t readEnum() {
  119. validator_.checkTypeExpected(AVRO_ENUM);
  120. return readCount();
  121. }
  122. int64_t readMapBlockSize() {
  123. validator_.checkTypeExpected(AVRO_MAP);
  124. return readCount();
  125. }
  126. Type nextType() const {
  127. return validator_.nextTypeExpected();
  128. }
  129. bool currentRecordName(std::string &name) const {
  130. return validator_.getCurrentRecordName(name);
  131. }
  132. bool nextFieldName(std::string &name) const {
  133. return validator_.getNextFieldName(name);
  134. }
  135. private:
  136. uint64_t readVarInt() {
  137. uint64_t encoded = 0;
  138. uint8_t val = 0;
  139. int shift = 0;
  140. do {
  141. reader_.read(val);
  142. uint64_t newBits = static_cast<uint64_t>(val & 0x7f) << shift;
  143. encoded |= newBits;
  144. shift += 7;
  145. } while (val & 0x80);
  146. return encoded;
  147. }
  148. int64_t readSize() {
  149. uint64_t encoded = readVarInt();
  150. int64_t size = decodeZigzag64(encoded);
  151. return size;
  152. }
  153. int64_t readCount() {
  154. validator_.checkTypeExpected(AVRO_LONG);
  155. int64_t count = readSize();
  156. validator_.setCount(count);
  157. return count;
  158. }
  159. ValidatorType validator_;
  160. BufferReader reader_;
  161. };
  162. using Reader = ReaderImpl<NullValidator>;
  163. using ValidatingReader = ReaderImpl<Validator>;
  164. } // namespace avro
  165. #endif