-
Notifications
You must be signed in to change notification settings - Fork 0
/
PoolFactory.cpp
95 lines (84 loc) · 2.82 KB
/
PoolFactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "PoolFactory.h"
#include <folly/json.h>
#include "mcrouter/ConfigApiIf.h"
#include "mcrouter/lib/fbi/cpp/util.h"
namespace facebook {
namespace memcache {
namespace mcrouter {
PoolFactory::PoolFactory(const folly::dynamic& config, ConfigApiIf& configApi)
: configApi_(configApi) {
checkLogic(config.isObject(), "config is not an object");
if (auto jpools = config.get_ptr("pools")) {
checkLogic(jpools->isObject(), "config: 'pools' is not an object");
for (const auto& it : jpools->items()) {
pools_.emplace(
it.first.stringPiece(), std::make_pair(it.second, PoolState::NEW));
}
}
}
PoolFactory::PoolFactory(
const folly::dynamic& config,
ConfigApiIf& configApi,
folly::json::metadata_map configMetadataMap)
: PoolFactory(config, configApi) {
configMetadataMap_ = std::move(configMetadataMap);
}
PoolFactory::PoolJson PoolFactory::parseNamedPool(folly::StringPiece name) {
auto existingIt = pools_.find(name);
if (existingIt == pools_.end()) {
// get the pool from ConfigApi
std::string jsonStr;
checkLogic(
configApi_.get(ConfigType::Pool, name.str(), jsonStr),
"Can not read pool: {}",
name);
existingIt =
pools_
.emplace(
name,
std::make_pair(parseJsonString(jsonStr), PoolState::PARSED))
.first;
return PoolJson(existingIt->first, existingIt->second.first);
}
name = existingIt->first;
auto& json = existingIt->second.first;
auto& state = existingIt->second.second;
switch (state) {
case PoolState::PARSED:
return PoolJson(name, json);
case PoolState::PARSING:
throwLogic("Cycle in pool inheritance");
case PoolState::NEW:
state = PoolState::PARSING;
break;
}
if (auto jInherit = json.get_ptr("inherit")) {
checkLogic(jInherit->isString(), "Pool {}: inherit is not a string", name);
auto& newJson = parseNamedPool(jInherit->stringPiece()).json;
json.update_missing(newJson);
json.erase("inherit");
}
state = PoolState::PARSED;
return PoolJson(name, json);
}
PoolFactory::PoolJson PoolFactory::parsePool(const folly::dynamic& json) {
checkLogic(
json.isString() || json.isObject(),
"Pool should be a string (name of pool) or an object");
if (json.isString()) {
return parseNamedPool(json.stringPiece());
}
auto jname = json.get_ptr("name");
checkLogic(jname && jname->isString(), "Pool should have string 'name'");
pools_.emplace(jname->stringPiece(), std::make_pair(json, PoolState::NEW));
return parseNamedPool(jname->stringPiece());
}
} // namespace mcrouter
} // namespace memcache
} // namespace facebook