123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744 |
- /**
- * 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
- *
- * https://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.
- */
- #include "Resolver.hh"
- #include "AvroTraits.hh"
- #include "Layout.hh"
- #include "NodeImpl.hh"
- #include "Reader.hh"
- #include "ValidSchema.hh"
- #include <memory>
- namespace avro {
- using std::unique_ptr;
- class ResolverFactory;
- typedef std::shared_ptr<Resolver> ResolverPtr;
- typedef std::vector<std::unique_ptr<Resolver>> ResolverPtrVector;
- // #define DEBUG_VERBOSE
- #ifdef DEBUG_VERBOSE
- #define DEBUG_OUT(str) std::cout << str << '\n'
- #else
- class NoOp {};
- template<typename T>
- NoOp &operator<<(NoOp &noOp, const T &) {
- return noOp;
- }
- NoOp noop;
- #define DEBUG_OUT(str) noop << str
- #endif
- template<typename T>
- class PrimitiveSkipper : public Resolver {
- public:
- PrimitiveSkipper() : Resolver() {}
- void parse(Reader &reader, uint8_t *address) const final {
- T val;
- reader.readValue(val);
- DEBUG_OUT("Skipping " << val);
- }
- };
- template<typename T>
- class PrimitiveParser : public Resolver {
- public:
- explicit PrimitiveParser(const PrimitiveLayout &offset) : Resolver(),
- offset_(offset.offset()) {}
- void parse(Reader &reader, uint8_t *address) const final {
- T *location = reinterpret_cast<T *>(address + offset_);
- reader.readValue(*location);
- DEBUG_OUT("Reading " << *location);
- }
- private:
- size_t offset_;
- };
- template<typename WT, typename RT>
- class PrimitivePromoter : public Resolver {
- public:
- explicit PrimitivePromoter(const PrimitiveLayout &offset) : Resolver(),
- offset_(offset.offset()) {}
- void parse(Reader &reader, uint8_t *address) const final {
- parseIt<WT>(reader, address);
- }
- private:
- void parseIt(Reader &reader, uint8_t *address, const std::true_type &) const {
- WT val;
- reader.readValue(val);
- RT *location = reinterpret_cast<RT *>(address + offset_);
- *location = static_cast<RT>(val);
- DEBUG_OUT("Promoting " << val);
- }
- void parseIt(Reader &reader, uint8_t *, const std::false_type &) const {}
- template<typename T>
- void parseIt(Reader &reader, uint8_t *address) const {
- parseIt(reader, address, is_promotable<T>());
- }
- size_t offset_;
- };
- template<>
- class PrimitiveSkipper<std::vector<uint8_t>> : public Resolver {
- public:
- PrimitiveSkipper() : Resolver() {}
- void parse(Reader &reader, uint8_t *address) const final {
- std::vector<uint8_t> val;
- reader.readBytes(val);
- DEBUG_OUT("Skipping bytes");
- }
- };
- template<>
- class PrimitiveParser<std::vector<uint8_t>> : public Resolver {
- public:
- explicit PrimitiveParser(const PrimitiveLayout &offset) : Resolver(),
- offset_(offset.offset()) {}
- void parse(Reader &reader, uint8_t *address) const final {
- auto *location = reinterpret_cast<std::vector<uint8_t> *>(address + offset_);
- reader.readBytes(*location);
- DEBUG_OUT("Reading bytes");
- }
- private:
- size_t offset_;
- };
- class RecordSkipper : public Resolver {
- public:
- RecordSkipper(ResolverFactory &factory, const NodePtr &writer);
- void parse(Reader &reader, uint8_t *address) const final {
- DEBUG_OUT("Skipping record");
- reader.readRecord();
- size_t steps = resolvers_.size();
- for (size_t i = 0; i < steps; ++i) {
- resolvers_[i]->parse(reader, address);
- }
- }
- protected:
- ResolverPtrVector resolvers_;
- };
- class RecordParser : public Resolver {
- public:
- void parse(Reader &reader, uint8_t *address) const final {
- DEBUG_OUT("Reading record");
- reader.readRecord();
- size_t steps = resolvers_.size();
- for (size_t i = 0; i < steps; ++i) {
- resolvers_[i]->parse(reader, address);
- }
- }
- RecordParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets);
- protected:
- ResolverPtrVector resolvers_;
- };
- class MapSkipper : public Resolver {
- public:
- MapSkipper(ResolverFactory &factory, const NodePtr &writer);
- void parse(Reader &reader, uint8_t *address) const final {
- DEBUG_OUT("Skipping map");
- std::string key;
- int64_t size;
- do {
- size = reader.readMapBlockSize();
- for (auto i = 0; i < size; ++i) {
- reader.readValue(key);
- resolver_->parse(reader, address);
- }
- } while (size != 0);
- }
- protected:
- ResolverPtr resolver_;
- };
- class MapParser : public Resolver {
- public:
- typedef uint8_t *(*GenericMapSetter)(uint8_t *map, const std::string &key);
- MapParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets);
- void parse(Reader &reader, uint8_t *address) const final {
- DEBUG_OUT("Reading map");
- uint8_t *mapAddress = address + offset_;
- std::string key;
- auto *setter = reinterpret_cast<GenericMapSetter *>(address + setFuncOffset_);
- int64_t size;
- do {
- size = reader.readMapBlockSize();
- for (auto i = 0; i < size; ++i) {
- reader.readValue(key);
- // create a new map entry and get the address
- uint8_t *location = (*setter)(mapAddress, key);
- resolver_->parse(reader, location);
- }
- } while (size != 0);
- }
- protected:
- ResolverPtr resolver_;
- size_t offset_;
- size_t setFuncOffset_;
- };
- class ArraySkipper : public Resolver {
- public:
- ArraySkipper(ResolverFactory &factory, const NodePtr &writer);
- void parse(Reader &reader, uint8_t *address) const final {
- DEBUG_OUT("Skipping array");
- int64_t size;
- do {
- size = reader.readArrayBlockSize();
- for (auto i = 0; i < size; ++i) {
- resolver_->parse(reader, address);
- }
- } while (size != 0);
- }
- protected:
- ResolverPtr resolver_;
- };
- typedef uint8_t *(*GenericArraySetter)(uint8_t *array);
- class ArrayParser : public Resolver {
- public:
- ArrayParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets);
- void parse(Reader &reader, uint8_t *address) const final {
- DEBUG_OUT("Reading array");
- uint8_t *arrayAddress = address + offset_;
- auto *setter = reinterpret_cast<GenericArraySetter *>(address + setFuncOffset_);
- int64_t size;
- do {
- size = reader.readArrayBlockSize();
- for (auto i = 0; i < size; ++i) {
- // create a new map entry and get the address
- uint8_t *location = (*setter)(arrayAddress);
- resolver_->parse(reader, location);
- }
- } while (size != 0);
- }
- protected:
- ArrayParser() : Resolver(), offset_(0), setFuncOffset_(0) {}
- ResolverPtr resolver_;
- size_t offset_;
- size_t setFuncOffset_;
- };
- class EnumSkipper : public Resolver {
- public:
- EnumSkipper(ResolverFactory &factory, const NodePtr &writer) : Resolver() {}
- void parse(Reader &reader, uint8_t *address) const final {
- int64_t val = reader.readEnum();
- DEBUG_OUT("Skipping enum" << val);
- }
- };
- class EnumParser : public Resolver {
- public:
- enum EnumRepresentation {
- VAL
- };
- EnumParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets) : Resolver(),
- offset_(offsets.at(0).offset()),
- readerSize_(reader->names()) {
- const size_t writerSize = writer->names();
- mapping_.reserve(writerSize);
- for (size_t i = 0; i < writerSize; ++i) {
- const std::string &name = writer->nameAt(i);
- size_t readerIndex = readerSize_;
- reader->nameIndex(name, readerIndex);
- mapping_.push_back(readerIndex);
- }
- }
- void parse(Reader &reader, uint8_t *address) const final {
- auto val = static_cast<size_t>(reader.readEnum());
- assert(static_cast<size_t>(val) < mapping_.size());
- if (mapping_[val] < readerSize_) {
- auto *location = reinterpret_cast<EnumRepresentation *>(address + offset_);
- *location = static_cast<EnumRepresentation>(mapping_[val]);
- DEBUG_OUT("Setting enum" << *location);
- }
- }
- protected:
- size_t offset_;
- size_t readerSize_;
- std::vector<size_t> mapping_;
- };
- class UnionSkipper : public Resolver {
- public:
- UnionSkipper(ResolverFactory &factory, const NodePtr &writer);
- void parse(Reader &reader, uint8_t *address) const final {
- DEBUG_OUT("Skipping union");
- auto choice = static_cast<size_t>(reader.readUnion());
- resolvers_[choice]->parse(reader, address);
- }
- protected:
- ResolverPtrVector resolvers_;
- };
- class UnionParser : public Resolver {
- public:
- typedef uint8_t *(*GenericUnionSetter)(uint8_t *, int64_t);
- UnionParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets);
- void parse(Reader &reader, uint8_t *address) const final {
- DEBUG_OUT("Reading union");
- auto writerChoice = static_cast<size_t>(reader.readUnion());
- auto *readerChoice = reinterpret_cast<int64_t *>(address + choiceOffset_);
- *readerChoice = choiceMapping_[writerChoice];
- auto *setter = reinterpret_cast<GenericUnionSetter *>(address + setFuncOffset_);
- auto *value = reinterpret_cast<uint8_t *>(address + offset_);
- uint8_t *location = (*setter)(value, *readerChoice);
- resolvers_[writerChoice]->parse(reader, location);
- }
- protected:
- ResolverPtrVector resolvers_;
- std::vector<int64_t> choiceMapping_;
- size_t offset_;
- size_t choiceOffset_;
- size_t setFuncOffset_;
- };
- class UnionToNonUnionParser : public Resolver {
- public:
- typedef uint8_t *(*GenericUnionSetter)(uint8_t *, int64_t);
- UnionToNonUnionParser(ResolverFactory &factory,
- const NodePtr &writer,
- const NodePtr &reader,
- const Layout &offsets);
- void parse(Reader &reader, uint8_t *address) const final {
- DEBUG_OUT("Reading union to non-union");
- auto choice = static_cast<size_t>(reader.readUnion());
- resolvers_[choice]->parse(reader, address);
- }
- protected:
- ResolverPtrVector resolvers_;
- };
- class NonUnionToUnionParser : public Resolver {
- public:
- typedef uint8_t *(*GenericUnionSetter)(uint8_t *, int64_t);
- NonUnionToUnionParser(ResolverFactory &factory,
- const NodePtr &writer,
- const NodePtr &reader,
- const CompoundLayout &offsets);
- void parse(Reader &reader, uint8_t *address) const final {
- DEBUG_OUT("Reading non-union to union");
- auto *choice = reinterpret_cast<int64_t *>(address + choiceOffset_);
- *choice = choice_;
- auto *setter = reinterpret_cast<GenericUnionSetter *>(address + setFuncOffset_);
- auto *value = reinterpret_cast<uint8_t *>(address + offset_);
- uint8_t *location = (*setter)(value, choice_);
- resolver_->parse(reader, location);
- }
- protected:
- ResolverPtr resolver_;
- size_t choice_;
- size_t offset_;
- size_t choiceOffset_;
- size_t setFuncOffset_;
- };
- class FixedSkipper : public Resolver {
- public:
- FixedSkipper(ResolverFactory &factory, const NodePtr &writer) : Resolver() {
- size_ = writer->fixedSize();
- }
- void parse(Reader &reader, uint8_t *address) const final {
- DEBUG_OUT("Skipping fixed");
- std::unique_ptr<uint8_t[]> val(new uint8_t[size_]);
- reader.readFixed(&val[0], size_);
- }
- protected:
- int size_;
- };
- class FixedParser : public Resolver {
- public:
- FixedParser(ResolverFactory &factory, const NodePtr &writer, const NodePtr &reader, const CompoundLayout &offsets) : Resolver() {
- size_ = writer->fixedSize();
- offset_ = offsets.at(0).offset();
- }
- void parse(Reader &reader, uint8_t *address) const final {
- DEBUG_OUT("Reading fixed");
- auto *location = reinterpret_cast<uint8_t *>(address + offset_);
- reader.readFixed(location, size_);
- }
- protected:
- int size_;
- size_t offset_;
- };
- class ResolverFactory : private boost::noncopyable {
- template<typename T>
- unique_ptr<Resolver>
- constructPrimitiveSkipper(const NodePtr &writer) {
- return unique_ptr<Resolver>(new PrimitiveSkipper<T>());
- }
- template<typename T>
- unique_ptr<Resolver>
- constructPrimitive(const NodePtr &writer, const NodePtr &reader, const Layout &offset) {
- unique_ptr<Resolver> instruction;
- SchemaResolution match = writer->resolve(*reader);
- if (match == RESOLVE_NO_MATCH) {
- instruction = unique_ptr<Resolver>(new PrimitiveSkipper<T>());
- } else if (reader->type() == AVRO_UNION) {
- const auto &compoundLayout = static_cast<const CompoundLayout &>(offset);
- instruction = unique_ptr<Resolver>(new NonUnionToUnionParser(*this, writer, reader, compoundLayout));
- } else if (match == RESOLVE_MATCH) {
- const auto &primitiveLayout = static_cast<const PrimitiveLayout &>(offset);
- instruction = unique_ptr<Resolver>(new PrimitiveParser<T>(primitiveLayout));
- } else if (match == RESOLVE_PROMOTABLE_TO_LONG) {
- const auto &primitiveLayout = static_cast<const PrimitiveLayout &>(offset);
- instruction = unique_ptr<Resolver>(new PrimitivePromoter<T, int64_t>(primitiveLayout));
- } else if (match == RESOLVE_PROMOTABLE_TO_FLOAT) {
- const auto &primitiveLayout = static_cast<const PrimitiveLayout &>(offset);
- instruction = unique_ptr<Resolver>(new PrimitivePromoter<T, float>(primitiveLayout));
- } else if (match == RESOLVE_PROMOTABLE_TO_DOUBLE) {
- const auto &primitiveLayout = static_cast<const PrimitiveLayout &>(offset);
- instruction = unique_ptr<Resolver>(new PrimitivePromoter<T, double>(primitiveLayout));
- } else {
- assert(0);
- }
- return instruction;
- }
- template<typename Skipper>
- unique_ptr<Resolver>
- constructCompoundSkipper(const NodePtr &writer) {
- return unique_ptr<Resolver>(new Skipper(*this, writer));
- }
- template<typename Parser, typename Skipper>
- unique_ptr<Resolver>
- constructCompound(const NodePtr &writer, const NodePtr &reader, const Layout &offset) {
- unique_ptr<Resolver> instruction;
- avro::SchemaResolution match = writer->resolve(*reader);
- if (match == RESOLVE_NO_MATCH) {
- instruction = unique_ptr<Resolver>(new Skipper(*this, writer));
- } else if (writer->type() != AVRO_UNION && reader->type() == AVRO_UNION) {
- const auto &compoundLayout = dynamic_cast<const CompoundLayout &>(offset);
- instruction = unique_ptr<Resolver>(new NonUnionToUnionParser(*this, writer, reader, compoundLayout));
- } else if (writer->type() == AVRO_UNION && reader->type() != AVRO_UNION) {
- instruction = unique_ptr<Resolver>(new UnionToNonUnionParser(*this, writer, reader, offset));
- } else {
- const auto &compoundLayout = dynamic_cast<const CompoundLayout &>(offset);
- instruction = unique_ptr<Resolver>(new Parser(*this, writer, reader, compoundLayout));
- }
- return instruction;
- }
- public:
- unique_ptr<Resolver>
- construct(const NodePtr &writer, const NodePtr &reader, const Layout &offset) {
- typedef unique_ptr<Resolver> (ResolverFactory::*BuilderFunc)(const NodePtr &writer, const NodePtr &reader, const Layout &offset);
- NodePtr currentWriter = (writer->type() == AVRO_SYMBOLIC) ? resolveSymbol(writer) : writer;
- NodePtr currentReader = (reader->type() == AVRO_SYMBOLIC) ? resolveSymbol(reader) : reader;
- static const BuilderFunc funcs[] = {
- &ResolverFactory::constructPrimitive<std::string>,
- &ResolverFactory::constructPrimitive<std::vector<uint8_t>>,
- &ResolverFactory::constructPrimitive<int32_t>,
- &ResolverFactory::constructPrimitive<int64_t>,
- &ResolverFactory::constructPrimitive<float>,
- &ResolverFactory::constructPrimitive<double>,
- &ResolverFactory::constructPrimitive<bool>,
- &ResolverFactory::constructPrimitive<Null>,
- &ResolverFactory::constructCompound<RecordParser, RecordSkipper>,
- &ResolverFactory::constructCompound<EnumParser, EnumSkipper>,
- &ResolverFactory::constructCompound<ArrayParser, ArraySkipper>,
- &ResolverFactory::constructCompound<MapParser, MapSkipper>,
- &ResolverFactory::constructCompound<UnionParser, UnionSkipper>,
- &ResolverFactory::constructCompound<FixedParser, FixedSkipper>};
- static_assert((sizeof(funcs) / sizeof(BuilderFunc)) == (AVRO_NUM_TYPES),
- "Invalid number of builder functions");
- BuilderFunc func = funcs[currentWriter->type()];
- assert(func);
- return ((this)->*(func))(currentWriter, currentReader, offset);
- }
- unique_ptr<Resolver>
- skipper(const NodePtr &writer) {
- typedef unique_ptr<Resolver> (ResolverFactory::*BuilderFunc)(const NodePtr &writer);
- NodePtr currentWriter = (writer->type() == AVRO_SYMBOLIC) ? writer->leafAt(0) : writer;
- static const BuilderFunc funcs[] = {
- &ResolverFactory::constructPrimitiveSkipper<std::string>,
- &ResolverFactory::constructPrimitiveSkipper<std::vector<uint8_t>>,
- &ResolverFactory::constructPrimitiveSkipper<int32_t>,
- &ResolverFactory::constructPrimitiveSkipper<int64_t>,
- &ResolverFactory::constructPrimitiveSkipper<float>,
- &ResolverFactory::constructPrimitiveSkipper<double>,
- &ResolverFactory::constructPrimitiveSkipper<bool>,
- &ResolverFactory::constructPrimitiveSkipper<Null>,
- &ResolverFactory::constructCompoundSkipper<RecordSkipper>,
- &ResolverFactory::constructCompoundSkipper<EnumSkipper>,
- &ResolverFactory::constructCompoundSkipper<ArraySkipper>,
- &ResolverFactory::constructCompoundSkipper<MapSkipper>,
- &ResolverFactory::constructCompoundSkipper<UnionSkipper>,
- &ResolverFactory::constructCompoundSkipper<FixedSkipper>};
- static_assert((sizeof(funcs) / sizeof(BuilderFunc)) == (AVRO_NUM_TYPES),
- "Invalid number of builder functions");
- BuilderFunc func = funcs[currentWriter->type()];
- assert(func);
- return ((this)->*(func))(currentWriter);
- }
- };
- RecordSkipper::RecordSkipper(ResolverFactory &factory, const NodePtr &writer) : Resolver() {
- size_t leaves = writer->leaves();
- resolvers_.reserve(leaves);
- for (size_t i = 0; i < leaves; ++i) {
- const NodePtr &w = writer->leafAt(i);
- resolvers_.push_back(factory.skipper(w));
- }
- }
- RecordParser::RecordParser(ResolverFactory &factory,
- const NodePtr &writer,
- const NodePtr &reader,
- const CompoundLayout &offsets) : Resolver() {
- size_t leaves = writer->leaves();
- resolvers_.reserve(leaves);
- for (size_t i = 0; i < leaves; ++i) {
- const NodePtr &w = writer->leafAt(i);
- const std::string &name = writer->nameAt(i);
- size_t readerIndex = 0;
- bool found = reader->nameIndex(name, readerIndex);
- if (found) {
- const NodePtr &r = reader->leafAt(readerIndex);
- resolvers_.push_back(factory.construct(w, r, offsets.at(readerIndex)));
- } else {
- resolvers_.push_back(factory.skipper(w));
- }
- }
- }
- MapSkipper::MapSkipper(ResolverFactory &factory, const NodePtr &writer) : Resolver(),
- resolver_(factory.skipper(writer->leafAt(1))) {}
- MapParser::MapParser(ResolverFactory &factory,
- const NodePtr &writer,
- const NodePtr &reader,
- const CompoundLayout &offsets) : Resolver(),
- resolver_(factory.construct(writer->leafAt(1), reader->leafAt(1), offsets.at(1))),
- offset_(offsets.offset()),
- setFuncOffset_(offsets.at(0).offset()) {}
- ArraySkipper::ArraySkipper(ResolverFactory &factory, const NodePtr &writer) : Resolver(),
- resolver_(factory.skipper(writer->leafAt(0))) {}
- ArrayParser::ArrayParser(ResolverFactory &factory,
- const NodePtr &writer,
- const NodePtr &reader,
- const CompoundLayout &offsets) : Resolver(),
- resolver_(factory.construct(writer->leafAt(0), reader->leafAt(0), offsets.at(1))),
- offset_(offsets.offset()),
- setFuncOffset_(offsets.at(0).offset()) {}
- UnionSkipper::UnionSkipper(ResolverFactory &factory, const NodePtr &writer) : Resolver() {
- size_t leaves = writer->leaves();
- resolvers_.reserve(leaves);
- for (size_t i = 0; i < leaves; ++i) {
- const NodePtr &w = writer->leafAt(i);
- resolvers_.push_back(factory.skipper(w));
- }
- }
- namespace {
- // assumes the writer is NOT a union, and the reader IS a union
- SchemaResolution
- checkUnionMatch(const NodePtr &writer, const NodePtr &reader, size_t &index) {
- SchemaResolution bestMatch = RESOLVE_NO_MATCH;
- index = 0;
- size_t leaves = reader->leaves();
- for (size_t i = 0; i < leaves; ++i) {
- const NodePtr &leaf = reader->leafAt(i);
- SchemaResolution newMatch = writer->resolve(*leaf);
- if (newMatch == RESOLVE_MATCH) {
- bestMatch = newMatch;
- index = i;
- break;
- }
- if (bestMatch == RESOLVE_NO_MATCH) {
- bestMatch = newMatch;
- index = i;
- }
- }
- return bestMatch;
- }
- } // namespace
- UnionParser::UnionParser(ResolverFactory &factory,
- const NodePtr &writer,
- const NodePtr &reader,
- const CompoundLayout &offsets) : Resolver(),
- offset_(offsets.offset()),
- choiceOffset_(offsets.at(0).offset()),
- setFuncOffset_(offsets.at(1).offset()) {
- size_t leaves = writer->leaves();
- resolvers_.reserve(leaves);
- choiceMapping_.reserve(leaves);
- for (size_t i = 0; i < leaves; ++i) {
- // for each writer, we need a schema match for the reader
- const NodePtr &w = writer->leafAt(i);
- size_t index = 0;
- SchemaResolution match = checkUnionMatch(w, reader, index);
- if (match == RESOLVE_NO_MATCH) {
- resolvers_.push_back(factory.skipper(w));
- // push back a non-sense number
- choiceMapping_.push_back(reader->leaves());
- } else {
- const NodePtr &r = reader->leafAt(index);
- resolvers_.push_back(factory.construct(w, r, offsets.at(index + 2)));
- choiceMapping_.push_back(index);
- }
- }
- }
- NonUnionToUnionParser::NonUnionToUnionParser(ResolverFactory &factory,
- const NodePtr &writer,
- const NodePtr &reader,
- const CompoundLayout &offsets) : Resolver(),
- choice_(0),
- offset_(offsets.offset()),
- choiceOffset_(offsets.at(0).offset()),
- setFuncOffset_(offsets.at(1).offset()) {
- #ifndef NDEBUG
- SchemaResolution bestMatch =
- #endif
- checkUnionMatch(writer, reader, choice_);
- assert(bestMatch != RESOLVE_NO_MATCH);
- resolver_ = factory.construct(writer, reader->leafAt(choice_), offsets.at(choice_ + 2));
- }
- UnionToNonUnionParser::UnionToNonUnionParser(ResolverFactory &factory,
- const NodePtr &writer,
- const NodePtr &reader,
- const Layout &offsets) : Resolver() {
- size_t leaves = writer->leaves();
- resolvers_.reserve(leaves);
- for (size_t i = 0; i < leaves; ++i) {
- const NodePtr &w = writer->leafAt(i);
- resolvers_.push_back(factory.construct(w, reader, offsets));
- }
- }
- unique_ptr<Resolver> constructResolver(const ValidSchema &writerSchema,
- const ValidSchema &readerSchema,
- const Layout &readerLayout) {
- ResolverFactory factory;
- return factory.construct(writerSchema.root(), readerSchema.root(), readerLayout);
- }
- } // namespace avro
|