2 Copyright (c) 2018-2019 Nokia.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 #include "private/databaseconfigurationimpl.hpp"
18 #include <arpa/inet.h>
20 using namespace shareddatalayer;
24 const std::string& getDefaultHost()
26 static const std::string defaultHost("localhost");
30 const uint16_t DEFAULT_PORT(6379U);
31 const uint16_t DEFAULT_SENTINEL_PORT(26379U);
34 DatabaseConfigurationImpl::DatabaseConfigurationImpl():
35 dbType(DbType::UNKNOWN)
39 DatabaseConfigurationImpl::~DatabaseConfigurationImpl()
43 void DatabaseConfigurationImpl::checkAndApplyDbType(const std::string& type)
45 if (type == "redis-standalone")
46 dbType = DatabaseConfiguration::DbType::REDIS_STANDALONE;
47 else if (type == "redis-cluster")
48 dbType = DatabaseConfiguration::DbType::REDIS_CLUSTER;
49 else if (type == "redis-sentinel")
50 dbType = DatabaseConfiguration::DbType::REDIS_SENTINEL;
52 throw DatabaseConfiguration::InvalidDbType(type);
55 DatabaseConfiguration::DbType DatabaseConfigurationImpl::getDbType() const
60 void DatabaseConfigurationImpl::checkAndApplyServerAddress(const std::string& address)
62 serverAddresses.push_back(HostAndPort(address, htons(DEFAULT_PORT)));
65 bool DatabaseConfigurationImpl::isEmpty() const
67 return serverAddresses.empty();
70 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getServerAddresses() const
72 return serverAddresses;
75 DatabaseConfiguration::Addresses DatabaseConfigurationImpl::getDefaultServerAddresses() const
77 return { HostAndPort(getDefaultHost(), htons(DEFAULT_PORT)) };
80 void DatabaseConfigurationImpl::checkAndApplySentinelAddress(const std::string& address)
82 sentinelAddress = HostAndPort(address, htons(DEFAULT_SENTINEL_PORT));
85 boost::optional<HostAndPort> DatabaseConfigurationImpl::getSentinelAddress() const
87 return sentinelAddress;
90 void DatabaseConfigurationImpl::checkAndApplySentinelMasterName(const std::string& name)
92 sentinelMasterName = name;
95 std::string DatabaseConfigurationImpl::getSentinelMasterName() const
97 return sentinelMasterName;