-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate dwrf config definition from impl (#3393)
Summary: Pull Request resolved: #3393 Separate the config definition from the actual mechanism. Rename the impl class as `ConfigBase` and concrete config classes will derive `ConfigBase<ConcreteConfig>` like `dwrf::Config` and just define all the entries. Reviewed By: xiaoxmeng, Yuhta Differential Revision: D41658954 fbshipit-source-id: d3fa63c0a6bb6d86701142fa3be834fc590ba338
- Loading branch information
1 parent
f076c30
commit 475a1d3
Showing
3 changed files
with
89 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <functional> | ||
#include <map> | ||
#include <unordered_map> | ||
#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 ConcreteConfig> | ||
class ConfigBase { | ||
public: | ||
template <typename T> | ||
class Entry { | ||
Entry( | ||
const std::string& key, | ||
const T& val, | ||
std::function<std::string(const T&)> toStr = | ||
[](const T& val) { return folly::to<std::string>(val); }, | ||
std::function<T(const std::string&)> toT = | ||
[](const std::string& val) { return folly::to<T>(val); }) | ||
: key_{key}, default_{val}, toStr_{toStr}, toT_{toT} {} | ||
|
||
const std::string key_; | ||
const T default_; | ||
const std::function<std::string(const T&)> toStr_; | ||
const std::function<T(const std::string&)> toT_; | ||
|
||
friend ConfigBase; | ||
friend ConcreteConfig; | ||
}; | ||
|
||
template <typename T> | ||
ConfigBase& set(const Entry<T>& entry, const T& val) { | ||
configs_[entry.key_] = entry.toStr_(val); | ||
return *this; | ||
} | ||
|
||
template <typename T> | ||
ConfigBase& unset(const Entry<T>& entry) { | ||
configs_.erase(entry.key_); | ||
return *this; | ||
} | ||
|
||
ConfigBase& reset() { | ||
configs_.clear(); | ||
return *this; | ||
} | ||
|
||
template <typename T> | ||
T get(const Entry<T>& entry) const { | ||
auto iter = configs_.find(entry.key_); | ||
return iter != configs_.end() ? entry.toT_(iter->second) : entry.default_; | ||
} | ||
|
||
protected: | ||
std::unordered_map<std::string, std::string> configs_; | ||
}; | ||
|
||
} // namespace facebook::velox::common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters