# HG changeset patch # User František Kučera # Date 1621771149 -7200 # Node ID da114916734bebe7d6a8e231e40b14d0ee3e0bc7 # Parent 5d1c556ff7db00bf3eee74cc66fab8e912cfbb01 multiple modes infrastructure diff -r 5d1c556ff7db -r da114916734b nbproject/configurations.xml --- a/nbproject/configurations.xml Fri May 21 20:16:03 2021 +0200 +++ b/nbproject/configurations.xml Sun May 23 13:59:09 2021 +0200 @@ -42,6 +42,9 @@ + DataMode.h + MetadataMode.h + Mode.h relpipe-tr-infertypes.cpp @@ -93,6 +96,12 @@ true + + + + + + @@ -133,6 +142,12 @@ true + + + + + + diff -r 5d1c556ff7db -r da114916734b src/DataMode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/DataMode.h Sun May 23 13:59:09 2021 +0200 @@ -0,0 +1,47 @@ +/** + * Relational pipes + * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include +#include + +#include + +#include "Mode.h" + +namespace relpipe { +namespace tr { +namespace infertypes { + +class DataMode : public Mode { +private: +public: + + DataMode(shared_ptr relationalWriter) : Mode(relationalWriter) { + } + + void startRelation(relpipe::common::type::StringX name, std::vector attributes) override { + } + + void attribute(const relpipe::common::type::StringX& value) override { + } +}; + + +} +} +} diff -r 5d1c556ff7db -r da114916734b src/InferTypesHandler.h --- a/src/InferTypesHandler.h Fri May 21 20:16:03 2021 +0200 +++ b/src/InferTypesHandler.h Sun May 23 13:59:09 2021 +0200 @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -35,6 +36,9 @@ #include #include "Configuration.h" +#include "Mode.h" +#include "DataMode.h" +#include "MetadataMode.h" namespace relpipe { namespace tr { @@ -49,8 +53,16 @@ private: shared_ptr relationalWriter; Configuration configuration; + std::shared_ptr currentMode; - integer_t currentAttributeIndex = 0; + vector toWriterMetadata(vector attributes) { + // TODO: move to a reusable method (or use same metadata on both reader and writer side?) + vector writerMetadata; + for (AttributeMetadata readerMetadata : attributes) { + writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())}); + } + return writerMetadata; + } public: @@ -58,24 +70,27 @@ } void startRelation(string_t name, vector attributes) override { - // TODO: move to a reusable method (or use same metadata on both reader and writer side?) - vector writerMetadata; - for (AttributeMetadata readerMetadata : attributes) { - writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())}); + // TODO: move this logic to relpipe-lib-infertypes and share with certain modules like relpipe-in-csv + for (RelationConfiguration rc : configuration.relationConfigurations) { + if (std::regex_match(name, rc.relationPattern)) { + if (rc.mode == MODE::METADATA || rc.mode == MODE::AUTO && MetadataMode::willInfer(attributes)) currentMode.reset(new MetadataMode(relationalWriter)); + else if (rc.mode == MODE::DATA) currentMode.reset(new DataMode(relationalWriter)); + else throw std::logic_error("Unsupported mode: " + std::to_string((int) rc.mode)); + break; + } } - relationalWriter->startRelation(name, writerMetadata, true); + if (currentMode) currentMode->startRelation(name, attributes); + else relationalWriter->startRelation(name, toWriterMetadata(attributes), true); } void attribute(const string_t& value) override { - relationalWriter->writeAttribute(value); - - currentAttributeIndex++; - //currentAttributeIndex = currentAttributeIndex % currentRules.size(); + if (currentMode) currentMode->attribute(value); + else relationalWriter->writeAttribute(value); } void endOfPipe() { - + currentMode.reset(); } }; diff -r 5d1c556ff7db -r da114916734b src/MetadataMode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/MetadataMode.h Sun May 23 13:59:09 2021 +0200 @@ -0,0 +1,56 @@ +/** + * Relational pipes + * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include +#include + +#include + +#include "Mode.h" + +namespace relpipe { +namespace tr { +namespace infertypes { + +class MetadataMode : public Mode { +private: + static std::wregex pattern; +public: + + MetadataMode(shared_ptr relationalWriter) : Mode(relationalWriter) { + } + + static bool willInfer(std::vector attributes) { + for (relpipe::reader::handlers::AttributeMetadata a : attributes) if (!std::regex_match(a.getAttributeName(), pattern)) return false; + return true; + } + + void startRelation(relpipe::common::type::StringX name, std::vector attributes) override { + // TODO: share this code through relpipe-lib-infertypes (when available) + } + + void attribute(const relpipe::common::type::StringX& value) override { + relationalWriter->writeAttribute(value); + } +}; + +std::wregex MetadataMode::pattern = std::wregex(L"(.*)::(.*)"); + +} +} +} diff -r 5d1c556ff7db -r da114916734b src/Mode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Mode.h Sun May 23 13:59:09 2021 +0200 @@ -0,0 +1,41 @@ +/** + * Relational pipes + * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include +#include + +namespace relpipe { +namespace tr { +namespace infertypes { + +class Mode { +protected: + shared_ptr relationalWriter; + + Mode(shared_ptr relationalWriter) : relationalWriter(relationalWriter) { + } + +public: + virtual ~Mode() = default; + virtual void startRelation(const relpipe::common::type::StringX name, std::vector attributes) = 0; + virtual void attribute(const relpipe::common::type::StringX& value) = 0; +}; + +} +} +}