This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
configuration: refactor configuration related APIs (#54)
- Loading branch information
1 parent
e9b1dde
commit bda73a1
Showing
23 changed files
with
298 additions
and
300 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015 Microsoft Corporation | ||
* | ||
* -=- Robust Distributed System Nucleus (rDSN) -=- | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <cstdint> | ||
|
||
/// load a ini configuration file, and replace specific strings in file with arguments. | ||
/// | ||
/// the rules of replacement is as follows: | ||
/// 1. arguments are with format of k1=v1,k2=v2,k3=v3 | ||
/// 2. if a string if file is %k1%, it will be replaced with v1. | ||
/// %k2%, %k3% will be replaced to v2 and v3 in a similar fashion | ||
/// | ||
/// for example: | ||
/// | ||
/// the file content: | ||
/// " | ||
/// [example_section] | ||
/// %replace_key% = real_value | ||
/// real_port = %host_port% | ||
/// " | ||
/// | ||
/// and the arguments "replace_key=key,host_port=8080 | ||
/// | ||
/// so the loaded config will be: | ||
/// " | ||
/// [example_section] | ||
/// key = real_value | ||
/// real_port = 8080 | ||
/// | ||
/// return true if load config file succeed, otherwise false | ||
/// | ||
/// please call this function at the beginning of your process, otherwise non of the | ||
/// dsn_config_xxx function works | ||
/// | ||
/// the function is not thread safe. | ||
bool dsn_config_load(const char *file, const char *arguments); | ||
|
||
/// dump the global configuration | ||
void dsn_config_dump(std::ostream &os); | ||
|
||
/// the section format: | ||
/// [section] | ||
/// key = value | ||
/// | ||
/// this function get the value of a key in some section. | ||
/// if <key, value> is not present in section, then return default_value. | ||
/// dsptr is the description of the key-value pairs. | ||
/// | ||
/// this function is not thread safe if get/set are concurrently called | ||
const char *dsn_config_get_value_string(const char *section, | ||
const char *key, | ||
const char *default_value, | ||
const char *dsptr); | ||
|
||
/// get value of a key in some section, the value should be "true" or "false" | ||
/// this function is not thread safe if dsn_config_set is concurrently called | ||
bool dsn_config_get_value_bool(const char *section, | ||
const char *key, | ||
bool default_value, | ||
const char *dsptr); | ||
|
||
/// get value of a key in some section, the value should be decimal in range of uint64_t | ||
/// this function is not thread safe if dsn_config_set is concurrently called | ||
uint64_t dsn_config_get_value_uint64(const char *section, | ||
const char *key, | ||
uint64_t default_value, | ||
const char *dsptr); | ||
|
||
/// get value of a key in some section, the value should be decimal in range of double | ||
/// this function is not thread safe if dsn_config_set is concurrently called | ||
double dsn_config_get_value_double(const char *section, | ||
const char *key, | ||
double default_value, | ||
const char *dsptr); | ||
|
||
/// get the names of all sections | ||
/// this function is not thread safe if dsn_config_set is concurrently called | ||
void dsn_config_get_all_sections(/*out*/ std::vector<std::string> §ions); | ||
|
||
/// get the names of all sections | ||
/// this function is not thread safe if dsn_config_set is concurrently called | ||
void dsn_config_get_all_sections(/*out*/ std::vector<const char *> §ions); | ||
|
||
/// get all keys in some specific section | ||
/// this function is not thread safe if dsn_config_set is concurrently called | ||
void dsn_config_get_all_keys(const char *section, /*out*/ std::vector<std::string> &keys); | ||
|
||
/// get all keys in some specific section | ||
/// this function is not thread safe if dsn_config_set is concurrently called | ||
void dsn_config_get_all_keys(const char *section, /*out*/ std::vector<const char *> &keys); | ||
|
||
/// set value for a key of some section. | ||
/// if the section doesn't exsit, a new one will be created. | ||
/// if the key doesn't exist, a new one will be created | ||
/// | ||
/// multiple concurrent set are thread safe. | ||
/// | ||
/// any of dsn_config_get_xxx may corrupt if called concurrently with dsn_config_set | ||
void dsn_config_set(const char *section, const char *key, const char *value, const char *dsptr); |
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
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,72 @@ | ||
#include <fstream> | ||
#include <dsn/utility/config_api.h> | ||
#include <dsn/utility/configuration.h> | ||
|
||
dsn::configuration g_config; | ||
|
||
bool dsn_config_load(const char *file, const char *arguments) | ||
{ | ||
return g_config.load(file, arguments); | ||
} | ||
|
||
void dsn_config_dump(std::ostream &os) { g_config.dump(os); } | ||
|
||
const char *dsn_config_get_value_string(const char *section, | ||
const char *key, | ||
const char *default_value, | ||
const char *dsptr) | ||
{ | ||
return g_config.get_string_value(section, key, default_value, dsptr); | ||
} | ||
|
||
bool dsn_config_get_value_bool(const char *section, | ||
const char *key, | ||
bool default_value, | ||
const char *dsptr) | ||
{ | ||
return g_config.get_value<bool>(section, key, default_value, dsptr); | ||
} | ||
|
||
uint64_t dsn_config_get_value_uint64(const char *section, | ||
const char *key, | ||
uint64_t default_value, | ||
const char *dsptr) | ||
{ | ||
return g_config.get_value<uint64_t>(section, key, default_value, dsptr); | ||
} | ||
|
||
double dsn_config_get_value_double(const char *section, | ||
const char *key, | ||
double default_value, | ||
const char *dsptr) | ||
{ | ||
return g_config.get_value<double>(section, key, default_value, dsptr); | ||
} | ||
|
||
void dsn_config_get_all_sections(/*out*/ std::vector<std::string> §ions) | ||
{ | ||
g_config.get_all_sections(sections); | ||
} | ||
|
||
void dsn_config_get_all_sections(/*out*/ std::vector<const char *> §ions) | ||
{ | ||
g_config.get_all_section_ptrs(sections); | ||
} | ||
|
||
void dsn_config_get_all_keys(const char *section, std::vector<std::string> &keys) | ||
{ | ||
std::vector<const char *> key_ptrs; | ||
g_config.get_all_keys(section, key_ptrs); | ||
for (const char *p : key_ptrs) | ||
keys.emplace_back(std::string(p)); | ||
} | ||
|
||
void dsn_config_get_all_keys(const char *section, /*out*/ std::vector<const char *> &keys) | ||
{ | ||
g_config.get_all_keys(section, keys); | ||
} | ||
|
||
void dsn_config_set(const char *section, const char *key, const char *value, const char *dsptr) | ||
{ | ||
g_config.set(section, key, value, dsptr); | ||
} |
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
Oops, something went wrong.