Node.hh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_Node_hh__
  19. #define avro_Node_hh__
  20. #include "Config.hh"
  21. #include <boost/noncopyable.hpp>
  22. #include <cassert>
  23. #include <memory>
  24. #include <utility>
  25. #include "CustomAttributes.hh"
  26. #include "Exception.hh"
  27. #include "LogicalType.hh"
  28. #include "SchemaResolution.hh"
  29. #include "Types.hh"
  30. namespace avro {
  31. class Node;
  32. class GenericDatum;
  33. using NodePtr = std::shared_ptr<Node>;
  34. class AVRO_DECL Name {
  35. std::string ns_;
  36. std::string simpleName_;
  37. public:
  38. Name() = default;
  39. explicit Name(const std::string &fullname);
  40. Name(std::string simpleName, std::string ns) : ns_(std::move(ns)), simpleName_(std::move(simpleName)) { check(); }
  41. std::string fullname() const;
  42. const std::string &ns() const { return ns_; }
  43. const std::string &simpleName() const { return simpleName_; }
  44. void ns(std::string n) { ns_ = std::move(n); }
  45. void simpleName(std::string n) { simpleName_ = std::move(n); }
  46. void fullname(const std::string &n);
  47. bool operator<(const Name &n) const;
  48. void check() const;
  49. bool operator==(const Name &n) const;
  50. bool operator!=(const Name &n) const { return !((*this) == n); }
  51. void clear() {
  52. ns_.clear();
  53. simpleName_.clear();
  54. }
  55. explicit operator std::string() const {
  56. return fullname();
  57. }
  58. };
  59. inline std::ostream &operator<<(std::ostream &os, const Name &n) {
  60. return os << n.fullname();
  61. }
  62. /// Node is the building block for parse trees. Each node represents an avro
  63. /// type. Compound types have leaf nodes that represent the types they are
  64. /// composed of.
  65. ///
  66. /// The user does not use the Node object directly, they interface with Schema
  67. /// objects.
  68. ///
  69. /// The Node object uses reference-counted pointers. This is so that schemas
  70. /// may be reused in other schemas, without needing to worry about memory
  71. /// deallocation for nodes that are added to multiple schema parse trees.
  72. ///
  73. /// Node has minimal implementation, serving as an abstract base class for
  74. /// different node types.
  75. ///
  76. class AVRO_DECL Node : private boost::noncopyable {
  77. public:
  78. explicit Node(Type type) : type_(type),
  79. logicalType_(LogicalType::NONE),
  80. locked_(false) {}
  81. virtual ~Node();
  82. Type type() const {
  83. return type_;
  84. }
  85. LogicalType logicalType() const {
  86. return logicalType_;
  87. }
  88. void setLogicalType(LogicalType logicalType);
  89. void lock() {
  90. locked_ = true;
  91. }
  92. bool locked() const {
  93. return locked_;
  94. }
  95. virtual bool hasName() const = 0;
  96. void setName(const Name &name) {
  97. checkLock();
  98. checkName(name);
  99. doSetName(name);
  100. }
  101. virtual const Name &name() const = 0;
  102. virtual const std::string &getDoc() const = 0;
  103. void setDoc(const std::string &doc) {
  104. checkLock();
  105. doSetDoc(doc);
  106. }
  107. void addLeaf(const NodePtr &newLeaf) {
  108. checkLock();
  109. doAddLeaf(newLeaf);
  110. }
  111. virtual size_t leaves() const = 0;
  112. virtual const NodePtr &leafAt(size_t index) const = 0;
  113. virtual const GenericDatum &defaultValueAt(size_t index) {
  114. throw Exception(boost::format("No default value at: %1%") % index);
  115. }
  116. void addName(const std::string &name) {
  117. checkLock();
  118. checkName(Name(name));
  119. doAddName(name);
  120. }
  121. virtual size_t names() const = 0;
  122. virtual const std::string &nameAt(size_t index) const = 0;
  123. virtual bool nameIndex(const std::string &name, size_t &index) const = 0;
  124. void setFixedSize(size_t size) {
  125. checkLock();
  126. doSetFixedSize(size);
  127. }
  128. virtual size_t fixedSize() const = 0;
  129. void addCustomAttributesForField(const CustomAttributes& customAttributes) {
  130. checkLock();
  131. doAddCustomAttribute(customAttributes);
  132. }
  133. virtual bool isValid() const = 0;
  134. virtual SchemaResolution resolve(const Node &reader) const = 0;
  135. virtual void printJson(std::ostream &os, size_t depth) const = 0;
  136. virtual void printBasicInfo(std::ostream &os) const = 0;
  137. virtual void setLeafToSymbolic(size_t index, const NodePtr &node) = 0;
  138. // Serialize the default value GenericDatum g for the node contained
  139. // in a record node.
  140. virtual void printDefaultToJson(const GenericDatum &g, std::ostream &os,
  141. size_t depth) const = 0;
  142. protected:
  143. void checkLock() const {
  144. if (locked()) {
  145. throw Exception("Cannot modify locked schema");
  146. }
  147. }
  148. virtual void checkName(const Name &name) const {
  149. name.check();
  150. }
  151. virtual void doSetName(const Name &name) = 0;
  152. virtual void doSetDoc(const std::string &name) = 0;
  153. virtual void doAddLeaf(const NodePtr &newLeaf) = 0;
  154. virtual void doAddName(const std::string &name) = 0;
  155. virtual void doSetFixedSize(size_t size) = 0;
  156. virtual void doAddCustomAttribute(const CustomAttributes& customAttributes) = 0;
  157. private:
  158. const Type type_;
  159. LogicalType logicalType_;
  160. bool locked_;
  161. };
  162. } // namespace avro
  163. namespace std {
  164. inline std::ostream &operator<<(std::ostream &os, const avro::Node &n) {
  165. n.printJson(os, 0);
  166. return os;
  167. }
  168. } // namespace std
  169. #endif