diff --git a/velox/common/config/Config.h b/velox/common/config/Config.h new file mode 100644 index 0000000000000..3391c74253715 --- /dev/null +++ b/velox/common/config/Config.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * Licensed 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. + */ + +#pragma once + +#include +#include +#include +#include "folly/Conv.h" + +namespace facebook::velox::common { +// The concrete config class would inherit the config base +// and then just define all the entries. +template +class ConfigBase { + public: + template + class Entry { + Entry( + const std::string& key, + const T& val, + std::function toStr = + [](const T& val) { return folly::to(val); }, + std::function toT = + [](const std::string& val) { return folly::to(val); }) + : key_{key}, default_{val}, toStr_{toStr}, toT_{toT} {} + + const std::string key_; + const T default_; + const std::function toStr_; + const std::function toT_; + + friend ConfigBase; + friend ConcreteConfig; + }; + + template + ConfigBase& set(const Entry& entry, const T& val) { + configs_[entry.key_] = entry.toStr_(val); + return *this; + } + + template + ConfigBase& unset(const Entry& entry) { + configs_.erase(entry.key_); + return *this; + } + + ConfigBase& reset() { + configs_.clear(); + return *this; + } + + template + T get(const Entry& entry) const { + auto iter = configs_.find(entry.key_); + return iter != configs_.end() ? entry.toT_(iter->second) : entry.default_; + } + + protected: + std::unordered_map configs_; +}; + +} // namespace facebook::velox::common diff --git a/velox/dwio/dwrf/common/Config.h b/velox/dwio/dwrf/common/Config.h index a5a7c4bbbda5a..5d7dc8c35f183 100644 --- a/velox/dwio/dwrf/common/Config.h +++ b/velox/dwio/dwrf/common/Config.h @@ -18,65 +18,16 @@ #include #include -#include "folly/Conv.h" +#include "velox/common/config/Config.h" #include "velox/dwio/common/Common.h" #include "velox/dwio/dwrf/common/Common.h" namespace facebook::velox::dwrf { -class Config { +class Config : public common::ConfigBase { public: template - class Entry { - Entry( - const std::string& key, - const T& val, - std::function toStr = - [](const T& val) { return folly::to(val); }, - std::function toT = - [](const std::string& val) { return folly::to(val); }) - : key_{key}, default_{val}, toStr_{toStr}, toT_{toT} {} - - const std::string key_; - const T default_; - const std::function toStr_; - const std::function toT_; - - friend Config; - }; - - template - Config& set(const Entry& entry, const T& val) { - configs_[entry.key_] = entry.toStr_(val); - return *this; - } - - template - Config& unset(const Entry& entry) { - auto iter = configs_.find(entry.key_); - if (iter != configs_.end()) { - configs_.erase(iter); - } - return *this; - } - - Config& reset() { - configs_.clear(); - return *this; - } - - template - T get(const Entry& entry) const { - auto iter = configs_.find(entry.key_); - return iter != configs_.end() ? entry.toT_(iter->second) : entry.default_; - } - - static std::shared_ptr fromMap( - std::map map) { - auto ret = std::make_shared(); - ret->configs_.insert(map.begin(), map.end()); - return ret; - } + using Entry = common::ConfigBase::Entry; static Entry WRITER_VERSION; static Entry COMPRESSION; @@ -124,8 +75,12 @@ class Config { static Entry RAW_DATA_SIZE_PER_BATCH; static Entry MAP_STATISTICS; - private: - std::unordered_map configs_; + static std::shared_ptr fromMap( + std::map map) { + auto ret = std::make_shared(); + ret->configs_.insert(map.begin(), map.end()); + return ret; + } }; } // namespace facebook::velox::dwrf diff --git a/velox/dwio/dwrf/test/E2EFilterTest.cpp b/velox/dwio/dwrf/test/E2EFilterTest.cpp index 822694c943e2b..852112a4520cb 100644 --- a/velox/dwio/dwrf/test/E2EFilterTest.cpp +++ b/velox/dwio/dwrf/test/E2EFilterTest.cpp @@ -135,11 +135,11 @@ class E2EFilterTest : public E2EFilterTestBase { } flatmapNodeIdsAsStruct_[child->id] = mapFlatColsStructKeys[i]; } - config->set(Config::FLATTEN_MAP, true); + config->set(dwrf::Config::FLATTEN_MAP, true); config->set>( - Config::MAP_FLAT_COLS, mapFlatCols); + dwrf::Config::MAP_FLAT_COLS, mapFlatCols); config->set>>( - Config::MAP_FLAT_COLS_STRUCT_KEYS, mapFlatColsStructKeys); + dwrf::Config::MAP_FLAT_COLS_STRUCT_KEYS, mapFlatColsStructKeys); } WriterOptions options; options.config = config;