19 #ifndef avro_NodeImpl_hh__ 20 #define avro_NodeImpl_hh__ 23 #include "GenericDatum.hh" 33 #include "NodeConcepts.hh" 44 class LeafNamesConcept,
57 leafNameAttributes_(),
62 const NameConcept &name,
63 const LeavesConcept &leaves,
64 const LeafNamesConcept &leafNames,
65 const SizeConcept &size) :
70 leafNameAttributes_(leafNames),
76 const NameConcept &name,
78 const LeavesConcept &leaves,
79 const LeafNamesConcept &leafNames,
80 const SizeConcept &size) :
85 leafNameAttributes_(leafNames),
90 std::swap(nameAttribute_, impl.nameAttribute_);
91 std::swap(docAttribute_, impl.docAttribute_);
93 std::swap(leafNameAttributes_, impl.leafNameAttributes_);
94 std::swap(sizeAttribute_, impl.sizeAttribute_);
95 std::swap(nameIndex_, impl.nameIndex_);
98 bool hasName()
const {
100 return NameConcept::hasAttribute;
103 void doSetName(
const Name &name) {
104 nameAttribute_.add(name);
107 const Name &name()
const {
108 return nameAttribute_.get();
111 void doSetDoc(
const std::string &doc) {
112 docAttribute_.add(doc);
115 const std::string &getDoc()
const {
116 return docAttribute_.get();
119 void doAddLeaf(
const NodePtr &newLeaf) {
123 size_t leaves()
const {
127 const NodePtr &leafAt(
int index)
const {
131 void doAddName(
const std::string &name) {
132 if (! nameIndex_.add(name, leafNameAttributes_.size())) {
133 throw Exception(boost::format(
"Cannot add duplicate name: %1%") % name);
135 leafNameAttributes_.add(name);
138 size_t names()
const {
139 return leafNameAttributes_.size();
142 const std::string &nameAt(
int index)
const {
143 return leafNameAttributes_.get(index);
146 bool nameIndex(
const std::string &name,
size_t &index)
const {
147 return nameIndex_.lookup(name, index);
150 void doSetFixedSize(
int size) {
151 sizeAttribute_.add(size);
154 int fixedSize()
const {
155 return sizeAttribute_.get();
158 virtual bool isValid()
const = 0;
160 void printBasicInfo(std::ostream &os)
const;
162 void setLeafToSymbolic(
int index,
const NodePtr &node);
170 const NodePtr &node = reader.leafAt(0);
171 match = resolve(*node);
179 for(
size_t i= 0; i < reader.leaves(); ++i) {
181 const NodePtr &node = reader.leafAt(i);
201 NameConcept nameAttribute_;
207 LeafNamesConcept leafNameAttributes_;
208 SizeConcept sizeAttribute_;
242 NodeImplPrimitive(type)
247 void printJson(std::ostream &os,
int depth)
const;
249 bool isValid()
const {
253 void printDefaultToJson(
const GenericDatum& g, std::ostream &os,
int depth)
const;
258 typedef std::weak_ptr<Node> NodeWeakPtr;
267 NodeImplSymbolic(
AVRO_SYMBOLIC, name, NoLeaves(), NoLeafNames(), NoSize())
271 NodeImplSymbolic(
AVRO_SYMBOLIC, name, NoLeaves(), NoLeafNames(), NoSize()), actualNode_(n)
275 void printJson(std::ostream &os,
int depth)
const;
277 bool isValid()
const {
278 return (nameAttribute_.size() == 1);
281 void printDefaultToJson(
const GenericDatum& g, std::ostream &os,
int depth)
const;
284 return (actualNode_.lock() != 0);
287 NodePtr getNode()
const {
288 NodePtr node = actualNode_.lock();
290 throw Exception(boost::format(
"Could not follow symbol %1%") % name());
295 void setNode(
const NodePtr &node) {
301 NodeWeakPtr actualNode_;
306 std::vector<GenericDatum> defaultValues;
309 NodeRecord(
const HasName &name,
const MultiLeaves &fields,
310 const LeafNames &fieldsNames,
311 const std::vector<GenericDatum>& dv) :
312 NodeImplRecord(
AVRO_RECORD, name, fields, fieldsNames, NoSize()),
314 for (
size_t i = 0; i < leafNameAttributes_.size(); ++i) {
315 if (!nameIndex_.add(leafNameAttributes_.get(i), i)) {
317 "Cannot add duplicate field: %1%") %
318 leafNameAttributes_.get(i));
323 NodeRecord(
const HasName &name,
const HasDoc &doc,
const MultiLeaves &fields,
324 const LeafNames &fieldsNames,
325 const std::vector<GenericDatum> &dv) :
326 NodeImplRecord(
AVRO_RECORD, name, doc, fields, fieldsNames, NoSize()),
328 for (
size_t i = 0; i < leafNameAttributes_.size(); ++i) {
329 if (!nameIndex_.add(leafNameAttributes_.get(i), i)) {
331 "Cannot add duplicate field: %1%") %
332 leafNameAttributes_.get(i));
337 void swap(NodeRecord& r) {
338 NodeImplRecord::swap(r);
339 defaultValues.swap(r.defaultValues);
344 void printJson(std::ostream &os,
int depth)
const;
346 bool isValid()
const {
347 return ((nameAttribute_.size() == 1) &&
352 return defaultValues[index];
355 void printDefaultToJson(
const GenericDatum& g, std::ostream &os,
int depth)
const;
366 NodeEnum(
const HasName &name,
const LeafNames &symbols) :
367 NodeImplEnum(
AVRO_ENUM, name, NoLeaves(), symbols, NoSize())
369 for(
size_t i=0; i < leafNameAttributes_.size(); ++i) {
370 if(!nameIndex_.add(leafNameAttributes_.get(i), i)) {
371 throw Exception(boost::format(
"Cannot add duplicate enum: %1%") % leafNameAttributes_.get(i));
378 void printJson(std::ostream &os,
int depth)
const;
380 bool isValid()
const {
382 (nameAttribute_.size() == 1) &&
383 (leafNameAttributes_.size() > 0)
387 void printDefaultToJson(
const GenericDatum& g, std::ostream &os,
int depth)
const;
398 explicit NodeArray(
const SingleLeaf &items) :
399 NodeImplArray(
AVRO_ARRAY, NoName(), items, NoLeafNames(), NoSize())
404 void printJson(std::ostream &os,
int depth)
const;
406 bool isValid()
const {
410 void printDefaultToJson(
const GenericDatum& g, std::ostream &os,
int depth)
const;
424 explicit NodeMap(
const SingleLeaf &values) :
425 NodeImplMap(
AVRO_MAP, NoName(), values, NoLeafNames(), NoSize())
437 void printJson(std::ostream &os,
int depth)
const;
439 bool isValid()
const {
443 void printDefaultToJson(
const GenericDatum& g, std::ostream &os,
int depth)
const;
454 explicit NodeUnion(
const MultiLeaves &types) :
455 NodeImplUnion(
AVRO_UNION, NoName(), types, NoLeafNames(), NoSize())
460 void printJson(std::ostream &os,
int depth)
const;
462 bool isValid()
const {
463 std::set<std::string> seen;
504 name = n->name().fullname();
509 if (seen.find(name) != seen.end()) {
519 void printDefaultToJson(
const GenericDatum& g, std::ostream &os,
int depth)
const;
530 NodeFixed(
const HasName &name,
const HasSize &size) :
531 NodeImplFixed(
AVRO_FIXED, name, NoLeaves(), NoLeafNames(), size)
536 void printJson(std::ostream &os,
int depth)
const;
538 bool isValid()
const {
540 (nameAttribute_.size() == 1) &&
541 (sizeAttribute_.size() == 1)
545 void printDefaultToJson(
const GenericDatum& g, std::ostream &os,
int depth)
const;
548 template <
class A,
class B,
class C,
class D >
552 if(!B::hasAttribute) {
553 throw Exception(
"Cannot change leaf node for nonexistent leaf");
556 NodePtr &replaceNode =
const_cast<NodePtr &
>(
leafAttributes_.get(index));
557 if(replaceNode->name() != node->name()) {
558 throw Exception(
"Symbolic name does not match the name of the schema it references");
564 ptr->setName(node->name());
566 replaceNode.swap(symbol);
569 template <
class A,
class B,
class C,
class D >
575 os <<
' ' << nameAttribute_.get();
578 if(D::hasAttribute) {
579 os <<
" " << sizeAttribute_.get();
582 int count = leaves();
583 count = count ? count : names();
584 for(
int i= 0; i < count; ++i) {
585 if( C::hasAttribute ) {
586 os <<
"name " << nameAt(i) <<
'\n';
589 leafAt(i)->printBasicInfo(os);
593 os <<
"end " << type() <<
'\n';
598 inline NodePtr resolveSymbol(
const NodePtr &node)
601 throw Exception(
"Only symbolic nodes may be resolved");
603 std::shared_ptr<NodeSymbolic> symNode = std::static_pointer_cast<
NodeSymbolic>(node);
604 return symNode->getNode();
607 template<
typename T >
608 inline std::string intToHex(T i)
610 std::stringstream stream;
612 << std::setfill(
'0') << std::setw(
sizeof(T))
Node is the building block for parse trees.
Definition: Node.hh:89
Definition: NodeImpl.hh:358
The schemas match at a cursory level.
Definition: SchemaResolution.hh:38
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:30
Definition: NodeImpl.hh:256
Definition: NodeImpl.hh:390
SchemaResolution
Definition: SchemaResolution.hh:27
Definition: NodeImpl.hh:237
Definition: NodeImpl.hh:413
Definition: NodeConcepts.hh:132
LeavesConcept leafAttributes_
Doc used to compare schemas.
Definition: NodeImpl.hh:206
The schemas definitely do not match.
Definition: SchemaResolution.hh:31
Wrapper for std::runtime_error that provides convenience constructor for boost::format objects...
Definition: Exception.hh:31
Definition: NodeConcepts.hh:51
Definition: NodeImpl.hh:305
Implementation details for Node.
Definition: NodeImpl.hh:47
Definition: NodeImpl.hh:446
bool isCompound(Type t)
Returns true if and only if the given type is a non primitive valid type.
Definition: Types.hh:72
Generic datum which can hold any Avro type.
Definition: GenericDatum.hh:61
Definition: NodeImpl.hh:522