Writer.hh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_Writer_hh__
  19. #define avro_Writer_hh__
  20. #include <array>
  21. #include <boost/noncopyable.hpp>
  22. #include "Config.hh"
  23. #include "Types.hh"
  24. #include "Validator.hh"
  25. #include "Zigzag.hh"
  26. #include "buffer/Buffer.hh"
  27. namespace avro {
  28. /// Class for writing avro data to a stream.
  29. template<class ValidatorType>
  30. class WriterImpl : private boost::noncopyable {
  31. public:
  32. WriterImpl() = default;
  33. explicit WriterImpl(const ValidSchema &schema) : validator_(schema) {}
  34. void writeValue(const Null &) {
  35. validator_.checkTypeExpected(AVRO_NULL);
  36. }
  37. void writeValue(bool val) {
  38. validator_.checkTypeExpected(AVRO_BOOL);
  39. int8_t byte = (val != 0);
  40. buffer_.writeTo(byte);
  41. }
  42. void writeValue(int32_t val) {
  43. validator_.checkTypeExpected(AVRO_INT);
  44. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
  45. std::array<uint8_t, 5> bytes;
  46. size_t size = encodeInt32(val, bytes);
  47. buffer_.writeTo(reinterpret_cast<const char *>(bytes.data()), size);
  48. }
  49. void writeValue(int64_t val) {
  50. validator_.checkTypeExpected(AVRO_LONG);
  51. putLong(val);
  52. }
  53. void writeValue(float val) {
  54. validator_.checkTypeExpected(AVRO_FLOAT);
  55. union {
  56. float f;
  57. int32_t i;
  58. } v;
  59. v.f = val;
  60. buffer_.writeTo(v.i);
  61. }
  62. void writeValue(double val) {
  63. validator_.checkTypeExpected(AVRO_DOUBLE);
  64. union {
  65. double d;
  66. int64_t i;
  67. } v;
  68. v.d = val;
  69. buffer_.writeTo(v.i);
  70. }
  71. void writeValue(const std::string &val) {
  72. validator_.checkTypeExpected(AVRO_STRING);
  73. putBytes(val.c_str(), val.size());
  74. }
  75. void writeBytes(const void *val, size_t size) {
  76. validator_.checkTypeExpected(AVRO_BYTES);
  77. putBytes(val, size);
  78. }
  79. template<size_t N>
  80. void writeFixed(const uint8_t (&val)[N]) {
  81. validator_.checkFixedSizeExpected(N);
  82. buffer_.writeTo(reinterpret_cast<const char *>(val), N);
  83. }
  84. template<size_t N>
  85. void writeFixed(const std::array<uint8_t, N> &val) {
  86. validator_.checkFixedSizeExpected(val.size());
  87. buffer_.writeTo(reinterpret_cast<const char *>(val.data()), val.size());
  88. }
  89. void writeRecord() {
  90. validator_.checkTypeExpected(AVRO_RECORD);
  91. validator_.checkTypeExpected(AVRO_LONG);
  92. validator_.setCount(1);
  93. }
  94. void writeRecordEnd() {
  95. validator_.checkTypeExpected(AVRO_RECORD);
  96. validator_.checkTypeExpected(AVRO_LONG);
  97. validator_.setCount(0);
  98. }
  99. void writeArrayBlock(int64_t size) {
  100. validator_.checkTypeExpected(AVRO_ARRAY);
  101. writeCount(size);
  102. }
  103. void writeArrayEnd() {
  104. writeArrayBlock(0);
  105. }
  106. void writeMapBlock(int64_t size) {
  107. validator_.checkTypeExpected(AVRO_MAP);
  108. writeCount(size);
  109. }
  110. void writeMapEnd() {
  111. writeMapBlock(0);
  112. }
  113. void writeUnion(int64_t choice) {
  114. validator_.checkTypeExpected(AVRO_UNION);
  115. writeCount(choice);
  116. }
  117. void writeEnum(int64_t choice) {
  118. validator_.checkTypeExpected(AVRO_ENUM);
  119. writeCount(choice);
  120. }
  121. InputBuffer buffer() const {
  122. return buffer_;
  123. }
  124. private:
  125. void putLong(int64_t val) {
  126. // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
  127. std::array<uint8_t, 10> bytes;
  128. size_t size = encodeInt64(val, bytes);
  129. buffer_.writeTo(reinterpret_cast<const char *>(bytes.data()), size);
  130. }
  131. void putBytes(const void *val, size_t size) {
  132. putLong(size);
  133. buffer_.writeTo(reinterpret_cast<const char *>(val), size);
  134. }
  135. void writeCount(int64_t count) {
  136. validator_.checkTypeExpected(AVRO_LONG);
  137. validator_.setCount(count);
  138. putLong(count);
  139. }
  140. ValidatorType validator_;
  141. OutputBuffer buffer_;
  142. };
  143. using Writer = WriterImpl<NullValidator>;
  144. using ValidatingWriter = WriterImpl<Validator>;
  145. } // namespace avro
  146. #endif