|
12 | 12 | #include <database/backend_selector.h> |
13 | 13 | #include <database/db_exceptions.h> |
14 | 14 | #include <database/server_selector.h> |
| 15 | +#include <util/optional_value.h> |
15 | 16 | #include <functional> |
16 | 17 | #include <list> |
17 | 18 | #include <string> |
@@ -75,7 +76,7 @@ class BaseConfigBackendPool { |
75 | 76 | /// const BackendSelector& selector, |
76 | 77 | /// const ServerSelector& server_selector) const { |
77 | 78 | /// Subnet4Ptr subnet; |
78 | | - /// getPropertyPtrConst<Subnet4Ptr, const SubnetID&, ConfigBackendDHCPv4::getSubnet4> |
| 79 | + /// getPropertyPtrConst<Subnet4Ptr, const SubnetID&> |
79 | 80 | /// (&ConfigBackendDHCPv4::getSubnet4, selector, subnet, subnet_id); |
80 | 81 | /// return (subnet); |
81 | 82 | /// } |
@@ -142,6 +143,84 @@ class BaseConfigBackendPool { |
142 | 143 | } |
143 | 144 | } |
144 | 145 |
|
| 146 | + /// @brief Retrieve a single value encapsulated in the @c OptionalValue |
| 147 | + /// template. |
| 148 | + /// |
| 149 | + /// This is common method for retrieving a single configuration property |
| 150 | + /// from the databases. The property is encapsulated in the @c OptionalValue |
| 151 | + /// class. The value is set to "unspecified" if it is null in the database. |
| 152 | + /// The following is the example implementation of the method retrieving |
| 153 | + /// global conifguration value: |
| 154 | + /// |
| 155 | + /// @code |
| 156 | + /// OptionalValue<std::string> |
| 157 | + /// getGlobalParameter4(const std::string& parameter_name, |
| 158 | + /// const BackendSelector& selector, |
| 159 | + /// const ServerSelector& server_selector) const { |
| 160 | + /// std::string parameter; |
| 161 | + /// getOptionalPropertyConst<std::string, const std::string&> |
| 162 | + /// (&ConfigBackendDHCPv4::getGlobalParameter4, selector, server_selector, |
| 163 | + /// parameter, parameter_name); |
| 164 | + /// return (parameter); |
| 165 | + /// } |
| 166 | + /// @endcode |
| 167 | + /// |
| 168 | + /// where @c ConfigBackendDHCPv4::getGlobalParameter has the following signature: |
| 169 | + /// |
| 170 | + /// @code |
| 171 | + /// std::string getGlobalParameter4(const ServerSelector&, const std::string&) const; |
| 172 | + /// @endcode |
| 173 | + /// |
| 174 | + /// |
| 175 | + /// @tparam PropertyType Type of the object returned by the backend call. |
| 176 | + /// @tparam InputType Type of the objects used as input to the backend call. |
| 177 | + /// |
| 178 | + /// @param MethodPointer Pointer to the backend method to be called. |
| 179 | + /// @param selector Backend selector. |
| 180 | + /// @param server_selector Server selector. |
| 181 | + /// @param [out] property Reference to the shared pointer where retrieved |
| 182 | + /// property should be assigned. |
| 183 | + /// @param input Values to be used as input to the backend call. |
| 184 | + /// |
| 185 | + /// @throw db::NoSuchDatabase if no database matching the given selector |
| 186 | + /// was found. |
| 187 | + template<typename PropertyType, typename... InputType> |
| 188 | + void getOptionalPropertyConst(util::OptionalValue<PropertyType> |
| 189 | + (ConfigBackendType::*MethodPointer) |
| 190 | + (const db::ServerSelector&, InputType...) const, |
| 191 | + const db::BackendSelector& selector, |
| 192 | + const db::ServerSelector& server_selector, |
| 193 | + util::OptionalValue<PropertyType>& property, |
| 194 | + InputType... input) const { |
| 195 | + |
| 196 | + // If no particular backend is selected, call each backend and return |
| 197 | + // the first non-null (non zero) value. |
| 198 | + if (selector.amUnspecified()) { |
| 199 | + for (auto backend : backends_) { |
| 200 | + property = ((*backend).*MethodPointer)(server_selector, input...); |
| 201 | + if (property.isSpecified()) { |
| 202 | + break; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + } else { |
| 207 | + // Backend selected, find the one that matches selection. |
| 208 | + auto backends = selectBackends(selector); |
| 209 | + if (!backends.empty()) { |
| 210 | + for (auto backend : backends) { |
| 211 | + property = ((*backend).*MethodPointer)(server_selector, input...); |
| 212 | + if (property.isSpecified()) { |
| 213 | + break; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + } else { |
| 218 | + isc_throw(db::NoSuchDatabase, "no such database found for selector: " |
| 219 | + << selector.toText()); |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + |
145 | 224 | /// @brief Retrieve multiple configuration properties from the pool. |
146 | 225 | /// |
147 | 226 | /// This is a common method for retrieving multiple configuration properties |
|
0 commit comments