Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
RPG-18 committed Oct 20, 2022
2 parents 5b6c1ef + d027de6 commit 21d9e70
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 38 deletions.
1 change: 1 addition & 0 deletions 3rdparty/avro-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(AVRO_SOURCE_FILES
impl/json/JsonIO.cc
impl/json/JsonDom.cc
impl/Resolver.cc impl/Validator.cc
impl/CustomFields.cc
)

set_property(SOURCE ${AVRO_SOURCE_FILES} PROPERTY SKIP_AUTOGEN ON)
Expand Down
55 changes: 55 additions & 0 deletions 3rdparty/avro-cpp/avro/CustomFields.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef avro_CustomFields_hh__
#define avro_CustomFields_hh__

#include <iostream>

#include "../impl/json/JsonDom.hh"

namespace avro {

// CustomFields class stores avro custom attributes.
// Each field is represented by a unique name and value.
// User is supposed to create CustomFields object and then add it to Schema.
class AVRO_DECL CustomFields {
public:
// Retrieves the custom field json entity for that fieldName, returns an
// null Entity if the field doesn't exist.
json::Entity getField(const std::string &fieldName) const;

// Adds a custom field. If the field already exists, throw an exception.
void addField(const std::string &fieldName, const json::Entity &fieldValue);
void addField(const std::string &fieldName, const std::string &fieldValue);

// Provides a way to iterate over the custom fields or check field size.
const std::map<std::string, json::Entity> &fields() const {
return fields_;
}

// Prints the json string for the specific field.
void printJson(std::ostream& os, const std::string &fieldName) const;

private:
std::map<std::string, json::Entity> fields_;
};

} // namespace avro

#endif
7 changes: 7 additions & 0 deletions 3rdparty/avro-cpp/avro/Node.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <memory>
#include <utility>

#include "CustomFields.hh"
#include "Exception.hh"
#include "LogicalType.hh"
#include "SchemaResolution.hh"
Expand Down Expand Up @@ -153,6 +154,11 @@ public:
}
virtual size_t fixedSize() const = 0;

void addCustomAttributesForField(const CustomFields& customAttributes) {
checkLock();
doAddCustomAttribute(customAttributes);
}

virtual bool isValid() const = 0;

virtual SchemaResolution resolve(const Node &reader) const = 0;
Expand Down Expand Up @@ -185,6 +191,7 @@ protected:
virtual void doAddLeaf(const NodePtr &newLeaf) = 0;
virtual void doAddName(const std::string &name) = 0;
virtual void doSetFixedSize(size_t size) = 0;
virtual void doAddCustomAttribute(const CustomFields& customFields) = 0;

private:
const Type type_;
Expand Down
100 changes: 71 additions & 29 deletions 3rdparty/avro-cpp/avro/NodeImpl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "Node.hh"
#include "NodeConcepts.hh"
#include "CustomFields.hh"

namespace avro {

Expand All @@ -42,6 +43,7 @@ template<
class NameConcept,
class LeavesConcept,
class LeafNamesConcept,
class MultiAttributesConcept,
class SizeConcept>
class NodeImpl : public Node {

Expand All @@ -51,17 +53,20 @@ protected:
docAttribute_(),
leafAttributes_(),
leafNameAttributes_(),
customAttributes_(),
sizeAttribute_() {}

NodeImpl(Type type,
const NameConcept &name,
const LeavesConcept &leaves,
const LeafNamesConcept &leafNames,
const MultiAttributesConcept &customAttributes,
const SizeConcept &size) : Node(type),
nameAttribute_(name),
docAttribute_(),
leafAttributes_(leaves),
leafNameAttributes_(leafNames),
customAttributes_(customAttributes),
sizeAttribute_(size) {}

// Ctor with "doc"
Expand All @@ -70,11 +75,13 @@ protected:
const concepts::SingleAttribute<std::string> &doc,
const LeavesConcept &leaves,
const LeafNamesConcept &leafNames,
const MultiAttributesConcept &customAttributes,
const SizeConcept &size) : Node(type),
nameAttribute_(name),
docAttribute_(doc),
leafAttributes_(leaves),
leafNameAttributes_(leafNames),
customAttributes_(customAttributes),
sizeAttribute_(size) {}

void swap(NodeImpl &impl) {
Expand All @@ -83,6 +90,7 @@ protected:
std::swap(leafAttributes_, impl.leafAttributes_);
std::swap(leafNameAttributes_, impl.leafNameAttributes_);
std::swap(sizeAttribute_, impl.sizeAttribute_);
std::swap(customAttributes_, impl.customAttributes_);
std::swap(nameIndex_, impl.nameIndex_);
}

Expand Down Expand Up @@ -152,6 +160,10 @@ protected:

void setLeafToSymbolic(size_t index, const NodePtr &node) override;

void doAddCustomAttribute(const CustomFields &customfields) override {
customAttributes_.add(customfields);
}

SchemaResolution furtherResolution(const Node &reader) const {
SchemaResolution match = RESOLVE_NO_MATCH;

Expand Down Expand Up @@ -195,6 +207,7 @@ protected:

LeavesConcept leafAttributes_;
LeafNamesConcept leafNameAttributes_;
MultiAttributesConcept customAttributes_;
SizeConcept sizeAttribute_;
concepts::NameIndexConcept<LeafNamesConcept> nameIndex_;
};
Expand All @@ -210,19 +223,21 @@ using MultiLeaves = concepts::MultiAttribute<NodePtr>;

using NoLeafNames = concepts::NoAttribute<std::string>;
using LeafNames = concepts::MultiAttribute<std::string>;
using MultiAttributes = concepts::MultiAttribute<CustomFields>;
using NoAttributes = concepts::NoAttribute<CustomFields>;

using NoSize = concepts::NoAttribute<int>;
using HasSize = concepts::SingleAttribute<int>;

using NodeImplPrimitive = NodeImpl<NoName, NoLeaves, NoLeafNames, NoSize>;
using NodeImplSymbolic = NodeImpl<HasName, NoLeaves, NoLeafNames, NoSize>;
using NodeImplPrimitive = NodeImpl<NoName, NoLeaves, NoLeafNames, MultiAttributes, NoSize>;
using NodeImplSymbolic = NodeImpl<HasName, NoLeaves, NoLeafNames, NoAttributes, NoSize>;

using NodeImplRecord = NodeImpl<HasName, MultiLeaves, LeafNames, NoSize>;
using NodeImplEnum = NodeImpl<HasName, NoLeaves, LeafNames, NoSize>;
using NodeImplArray = NodeImpl<NoName, SingleLeaf, NoLeafNames, NoSize>;
using NodeImplMap = NodeImpl<NoName, MultiLeaves, NoLeafNames, NoSize>;
using NodeImplUnion = NodeImpl<NoName, MultiLeaves, NoLeafNames, NoSize>;
using NodeImplFixed = NodeImpl<HasName, NoLeaves, NoLeafNames, HasSize>;
using NodeImplRecord = NodeImpl<HasName, MultiLeaves, LeafNames, MultiAttributes, NoSize>;
using NodeImplEnum = NodeImpl<HasName, NoLeaves, LeafNames, NoAttributes, NoSize>;
using NodeImplArray = NodeImpl<NoName, SingleLeaf, NoLeafNames, NoAttributes, NoSize>;
using NodeImplMap = NodeImpl<NoName, MultiLeaves, NoLeafNames, NoAttributes, NoSize>;
using NodeImplUnion = NodeImpl<NoName, MultiLeaves, NoLeafNames, NoAttributes, NoSize>;
using NodeImplFixed = NodeImpl<HasName, NoLeaves, NoLeafNames, NoAttributes, HasSize>;

class AVRO_DECL NodePrimitive : public NodeImplPrimitive {
public:
Expand All @@ -245,9 +260,9 @@ class AVRO_DECL NodeSymbolic : public NodeImplSymbolic {
public:
NodeSymbolic() : NodeImplSymbolic(AVRO_SYMBOLIC) {}

explicit NodeSymbolic(const HasName &name) : NodeImplSymbolic(AVRO_SYMBOLIC, name, NoLeaves(), NoLeafNames(), NoSize()) {}
explicit NodeSymbolic(const HasName &name) : NodeImplSymbolic(AVRO_SYMBOLIC, name, NoLeaves(), NoLeafNames(), NoAttributes(), NoSize()) {}

NodeSymbolic(const HasName &name, const NodePtr &n) : NodeImplSymbolic(AVRO_SYMBOLIC, name, NoLeaves(), NoLeafNames(), NoSize()), actualNode_(n) {}
NodeSymbolic(const HasName &name, const NodePtr &n) : NodeImplSymbolic(AVRO_SYMBOLIC, name, NoLeaves(), NoLeafNames(), NoAttributes(), NoSize()), actualNode_(n) {}
SchemaResolution resolve(const Node &reader) const override;

void printJson(std::ostream &os, size_t depth) const override;
Expand Down Expand Up @@ -289,15 +304,27 @@ public:

NodeRecord(const HasName &name, const HasDoc &doc, const MultiLeaves &fields,
const LeafNames &fieldsNames,
std::vector<GenericDatum> dv) : NodeImplRecord(AVRO_RECORD, name, doc, fields, fieldsNames, NoSize()),
std::vector<GenericDatum> dv) : NodeImplRecord(AVRO_RECORD, name, doc, fields, fieldsNames, MultiAttributes(), NoSize()),
defaultValues(std::move(dv)) {
for (size_t i = 0; i < leafNameAttributes_.size(); ++i) {
if (!nameIndex_.add(leafNameAttributes_.get(i), i)) {
throw Exception(boost::format(
"Cannot add duplicate field: %1%")
% leafNameAttributes_.get(i));
}
}
leafNameCheck();
}

NodeRecord(const HasName &name, const MultiLeaves &fields,
const LeafNames &fieldsNames,
const std::vector<GenericDatum>& dv,
const MultiAttributes &customAttributes) :
NodeImplRecord(AVRO_RECORD, name, fields, fieldsNames, customAttributes, NoSize()),
defaultValues(dv) {
leafNameCheck();
}

NodeRecord(const HasName &name, const HasDoc &doc, const MultiLeaves &fields,
const LeafNames &fieldsNames,
const std::vector<GenericDatum>& dv,
const MultiAttributes &customAttributes) :
NodeImplRecord(AVRO_RECORD, name, doc, fields, fieldsNames, customAttributes, NoSize()),
defaultValues(dv) {
leafNameCheck();
}

void swap(NodeRecord &r) {
Expand All @@ -310,21 +337,36 @@ public:
void printJson(std::ostream &os, size_t depth) const override;

bool isValid() const override {
return ((nameAttribute_.size() == 1) && (leafAttributes_.size() == leafNameAttributes_.size()));
return ((nameAttribute_.size() == 1) &&
(leafAttributes_.size() == leafNameAttributes_.size()) &&
(customAttributes_.size() == 0 ||
customAttributes_.size() == leafAttributes_.size()));
}

const GenericDatum &defaultValueAt(size_t index) override {
return defaultValues[index];
}

void printDefaultToJson(const GenericDatum &g, std::ostream &os, size_t depth) const override;

private:
// check if leaf name is valid Name and is not duplicate
void leafNameCheck() {
for (size_t i = 0; i < leafNameAttributes_.size(); ++i) {
if (!nameIndex_.add(leafNameAttributes_.get(i), i)) {
throw Exception(boost::format(
"Cannot add duplicate field: %1%")
% leafNameAttributes_.get(i));
}
}
}
};

class AVRO_DECL NodeEnum : public NodeImplEnum {
public:
NodeEnum() : NodeImplEnum(AVRO_ENUM) {}

NodeEnum(const HasName &name, const LeafNames &symbols) : NodeImplEnum(AVRO_ENUM, name, NoLeaves(), symbols, NoSize()) {
NodeEnum(const HasName &name, const LeafNames &symbols) : NodeImplEnum(AVRO_ENUM, name, NoLeaves(), symbols, NoAttributes(), NoSize()) {
for (size_t i = 0; i < leafNameAttributes_.size(); ++i) {
if (!nameIndex_.add(leafNameAttributes_.get(i), i)) {
throw Exception(boost::format("Cannot add duplicate enum: %1%") % leafNameAttributes_.get(i));
Expand All @@ -348,7 +390,7 @@ class AVRO_DECL NodeArray : public NodeImplArray {
public:
NodeArray() : NodeImplArray(AVRO_ARRAY) {}

explicit NodeArray(const SingleLeaf &items) : NodeImplArray(AVRO_ARRAY, NoName(), items, NoLeafNames(), NoSize()) {}
explicit NodeArray(const SingleLeaf &items) : NodeImplArray(AVRO_ARRAY, NoName(), items, NoLeafNames(), NoAttributes(), NoSize()) {}

SchemaResolution resolve(const Node &reader) const override;

Expand All @@ -365,7 +407,7 @@ class AVRO_DECL NodeMap : public NodeImplMap {
public:
NodeMap();

explicit NodeMap(const SingleLeaf &values) : NodeImplMap(AVRO_MAP, NoName(), MultiLeaves(values), NoLeafNames(), NoSize()) {
explicit NodeMap(const SingleLeaf &values) : NodeImplMap(AVRO_MAP, NoName(), MultiLeaves(values), NoLeafNames(), NoAttributes(), NoSize()) {
// need to add the key for the map too
NodePtr key(new NodePrimitive(AVRO_STRING));
doAddLeaf(key);
Expand All @@ -389,7 +431,7 @@ class AVRO_DECL NodeUnion : public NodeImplUnion {
public:
NodeUnion() : NodeImplUnion(AVRO_UNION) {}

explicit NodeUnion(const MultiLeaves &types) : NodeImplUnion(AVRO_UNION, NoName(), types, NoLeafNames(), NoSize()) {}
explicit NodeUnion(const MultiLeaves &types) : NodeImplUnion(AVRO_UNION, NoName(), types, NoLeafNames(), NoAttributes(), NoSize()) {}

SchemaResolution resolve(const Node &reader) const override;

Expand Down Expand Up @@ -458,7 +500,7 @@ class AVRO_DECL NodeFixed : public NodeImplFixed {
public:
NodeFixed() : NodeImplFixed(AVRO_FIXED) {}

NodeFixed(const HasName &name, const HasSize &size) : NodeImplFixed(AVRO_FIXED, name, NoLeaves(), NoLeafNames(), size) {}
NodeFixed(const HasName &name, const HasSize &size) : NodeImplFixed(AVRO_FIXED, name, NoLeaves(), NoLeafNames(), NoAttributes(), size) {}

SchemaResolution resolve(const Node &reader) const override;

Expand All @@ -472,9 +514,9 @@ public:
void printDefaultToJson(const GenericDatum &g, std::ostream &os, size_t depth) const override;
};

template<class A, class B, class C, class D>
template<class A, class B, class C, class D, class E>
inline void
NodeImpl<A, B, C, D>::setLeafToSymbolic(size_t index, const NodePtr &node) {
NodeImpl<A, B, C, D, E>::setLeafToSymbolic(size_t index, const NodePtr &node) {
if (!B::hasAttribute) {
throw Exception("Cannot change leaf node for nonexistent leaf");
}
Expand All @@ -490,15 +532,15 @@ NodeImpl<A, B, C, D>::setLeafToSymbolic(size_t index, const NodePtr &node) {
replaceNode = symbol;
}

template<class A, class B, class C, class D>
template<class A, class B, class C, class D, class E>
inline void
NodeImpl<A, B, C, D>::printBasicInfo(std::ostream &os) const {
NodeImpl<A, B, C, D, E>::printBasicInfo(std::ostream &os) const {
os << type();
if (hasName()) {
os << ' ' << nameAttribute_.get();
}

if (D::hasAttribute) {
if (E::hasAttribute) {
os << " " << sizeAttribute_.get();
}
os << '\n';
Expand Down
4 changes: 4 additions & 0 deletions 3rdparty/avro-cpp/avro/Schema.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "Config.hh"
#include "NodeImpl.hh"
#include "CustomFields.hh"
#include <string>

/// \file
Expand Down Expand Up @@ -100,6 +101,9 @@ class AVRO_DECL RecordSchema : public Schema {
public:
explicit RecordSchema(const std::string &name);
void addField(const std::string &name, const Schema &fieldSchema);
// Add a field with custom attributes
void addField(const std::string &name, const Schema &fieldSchema,
const CustomFields &customFields);

std::string getDoc() const;
void setDoc(const std::string &);
Expand Down
Loading

0 comments on commit 21d9e70

Please sign in to comment.