Encoder.hh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_Encoder_hh__
  19. #define avro_Encoder_hh__
  20. #include "Config.hh"
  21. #include <cstdint>
  22. #include <memory>
  23. #include <string>
  24. #include <vector>
  25. #include "Stream.hh"
  26. #include "ValidSchema.hh"
  27. /// \file
  28. ///
  29. /// Low level support for encoding avro values.
  30. /// This class has two types of functions. One type of functions support
  31. /// the writing of leaf values (for example, encodeLong and
  32. /// encodeString). These functions have analogs in Decoder.
  33. ///
  34. /// The other type of functions support the writing of maps and arrays.
  35. /// These functions are arrayStart, startItem, and arrayEnd
  36. /// (and similar functions for maps).
  37. /// Some implementations of Encoder handle the
  38. /// buffering required to break large maps and arrays into blocks,
  39. /// which is necessary for applications that want to do streaming.
  40. namespace avro {
  41. /**
  42. * The abstract base class for all Avro encoders. The implementations
  43. * differ in the method of encoding (binary versus JSON) or in capabilities
  44. * such as ability to verify the order of invocation of different functions.
  45. */
  46. class AVRO_DECL Encoder {
  47. public:
  48. virtual ~Encoder() = default;
  49. /// All future encodings will go to os, which should be valid until
  50. /// it is reset with another call to init() or the encoder is
  51. /// destructed.
  52. virtual void init(OutputStream &os) = 0;
  53. /// Flushes any data in internal buffers.
  54. virtual void flush() = 0;
  55. /// Returns the number of bytes produced so far.
  56. /// For a meaningful value, do a flush() before invoking this function.
  57. virtual int64_t byteCount() const = 0;
  58. /// Encodes a null to the current stream.
  59. virtual void encodeNull() = 0;
  60. /// Encodes a bool to the current stream
  61. virtual void encodeBool(bool b) = 0;
  62. /// Encodes a 32-bit int to the current stream.
  63. virtual void encodeInt(int32_t i) = 0;
  64. /// Encodes a 64-bit signed int to the current stream.
  65. virtual void encodeLong(int64_t l) = 0;
  66. /// Encodes a single-precision floating point number to the current stream.
  67. virtual void encodeFloat(float f) = 0;
  68. /// Encodes a double-precision floating point number to the current stream.
  69. virtual void encodeDouble(double d) = 0;
  70. /// Encodes a UTF-8 string to the current stream.
  71. virtual void encodeString(const std::string &s) = 0;
  72. /**
  73. * Encodes arbitrary binary data into the current stream as Avro "bytes"
  74. * data type.
  75. * \param bytes Where the data is
  76. * \param len Number of bytes at \p bytes.
  77. */
  78. virtual void encodeBytes(const uint8_t *bytes, size_t len) = 0;
  79. /**
  80. * Encodes arbitrary binary data into the current stream as Avro "bytes"
  81. * data type.
  82. * \param bytes The data.
  83. */
  84. void encodeBytes(const std::vector<uint8_t> &bytes) {
  85. uint8_t b = 0;
  86. encodeBytes(bytes.empty() ? &b : bytes.data(), bytes.size());
  87. }
  88. /// Encodes fixed length binary to the current stream.
  89. virtual void encodeFixed(const uint8_t *bytes, size_t len) = 0;
  90. /**
  91. * Encodes an Avro data type Fixed.
  92. * \param bytes The fixed, the length of which is taken as the size
  93. * of fixed.
  94. */
  95. void encodeFixed(const std::vector<uint8_t> &bytes) {
  96. encodeFixed(bytes.data(), bytes.size());
  97. }
  98. /// Encodes enum to the current stream.
  99. virtual void encodeEnum(size_t e) = 0;
  100. /// Indicates that an array of items is being encoded.
  101. virtual void arrayStart() = 0;
  102. /// Indicates that the current array of items have ended.
  103. virtual void arrayEnd() = 0;
  104. /// Indicates that a map of items is being encoded.
  105. virtual void mapStart() = 0;
  106. /// Indicates that the current map of items have ended.
  107. virtual void mapEnd() = 0;
  108. /// Indicates that count number of items are to follow in the current array
  109. /// or map.
  110. virtual void setItemCount(size_t count) = 0;
  111. /// Marks a beginning of an item in the current array or map.
  112. virtual void startItem() = 0;
  113. /// Encodes a branch of a union. The actual value is to follow.
  114. virtual void encodeUnionIndex(size_t e) = 0;
  115. };
  116. /**
  117. * Shared pointer to Encoder.
  118. */
  119. using EncoderPtr = std::shared_ptr<Encoder>;
  120. /**
  121. * Returns an encoder that can encode binary Avro standard.
  122. */
  123. AVRO_DECL EncoderPtr binaryEncoder();
  124. /**
  125. * Returns an encoder that validates sequence of calls to an underlying
  126. * Encoder against the given schema.
  127. */
  128. AVRO_DECL EncoderPtr validatingEncoder(const ValidSchema &schema,
  129. const EncoderPtr &base);
  130. /**
  131. * Returns an encoder that encodes Avro standard for JSON.
  132. */
  133. AVRO_DECL EncoderPtr jsonEncoder(const ValidSchema &schema);
  134. /**
  135. * Returns an encoder that encodes Avro standard for pretty printed JSON.
  136. */
  137. AVRO_DECL EncoderPtr jsonPrettyEncoder(const ValidSchema &schema);
  138. } // namespace avro
  139. #endif