mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2025-07-03 14:14:11 +00:00
Add optional database selection parameter in yml file.
This commit is contained in:
parent
8bdb1f237f
commit
04b667485e
4 changed files with 20 additions and 7 deletions
|
@ -29,6 +29,7 @@ namespace dindb {
|
||||||
struct RedisConnectionSettings {
|
struct RedisConnectionSettings {
|
||||||
std::string address;
|
std::string address;
|
||||||
uint16_t port;
|
uint16_t port;
|
||||||
|
uint16_t database;
|
||||||
};
|
};
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
} //namespace dindb
|
} //namespace dindb
|
||||||
|
@ -40,6 +41,7 @@ namespace YAML {
|
||||||
Node node;
|
Node node;
|
||||||
node["address"] = parSettings.address;
|
node["address"] = parSettings.address;
|
||||||
node["port"] = parSettings.port;
|
node["port"] = parSettings.port;
|
||||||
|
node["database"] = parSettings.database;
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,22 +52,33 @@ namespace YAML {
|
||||||
|
|
||||||
parSettings.address = parNode["address"].as<std::string>();
|
parSettings.address = parNode["address"].as<std::string>();
|
||||||
parSettings.port = parNode["port"].as<uint16_t>();
|
parSettings.port = parNode["port"].as<uint16_t>();
|
||||||
|
if (parNode["database"])
|
||||||
|
parSettings.database = parNode["database"].as<uint16_t>();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} //namespace YAML
|
} //namespace YAML
|
||||||
|
|
||||||
namespace dindb {
|
namespace dindb {
|
||||||
BackendRedis::BackendRedis(std::string &&parAddress, uint16_t parPort, bool parConnect) :
|
BackendRedis::BackendRedis(std::string &&parAddress, uint16_t parPort, uint16_t parDatabase, bool parConnect) :
|
||||||
m_redis(std::move(parAddress), parPort, parConnect)
|
m_redis(std::move(parAddress), parPort),
|
||||||
|
m_database(parDatabase)
|
||||||
{
|
{
|
||||||
|
if (parConnect)
|
||||||
|
this->connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
BackendRedis::~BackendRedis() noexcept {
|
BackendRedis::~BackendRedis() noexcept {
|
||||||
}
|
}
|
||||||
|
|
||||||
void BackendRedis::connect() {
|
void BackendRedis::connect() {
|
||||||
|
using boost::lexical_cast;
|
||||||
|
|
||||||
m_redis.connect();
|
m_redis.connect();
|
||||||
|
if (m_redis.is_connected() and m_database > 0) {
|
||||||
|
const std::string command = "SELECT " + lexical_cast<std::string>(m_database);
|
||||||
|
m_redis.run(command.c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BackendRedis::disconnect() {
|
void BackendRedis::disconnect() {
|
||||||
|
@ -146,6 +159,7 @@ extern "C" dindb::Backend* dindexer_create_backend (const YAML::Node* parConfig)
|
||||||
return new dindb::BackendRedis(
|
return new dindb::BackendRedis(
|
||||||
std::move(config.address),
|
std::move(config.address),
|
||||||
config.port,
|
config.port,
|
||||||
|
config.database,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ namespace dindb {
|
||||||
class BackendRedis : public Backend {
|
class BackendRedis : public Backend {
|
||||||
public:
|
public:
|
||||||
BackendRedis ( BackendRedis&& ) = default;
|
BackendRedis ( BackendRedis&& ) = default;
|
||||||
BackendRedis ( std::string&& parAddress, uint16_t parPort, bool parConnect );
|
BackendRedis ( std::string&& parAddress, uint16_t parPort, uint16_t parDatabase, bool parConnect );
|
||||||
virtual ~BackendRedis ( void ) noexcept;
|
virtual ~BackendRedis ( void ) noexcept;
|
||||||
|
|
||||||
virtual void connect ( void ) override;
|
virtual void connect ( void ) override;
|
||||||
|
@ -57,6 +57,7 @@ namespace dindb {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
redis::Command m_redis;
|
redis::Command m_redis;
|
||||||
|
uint16_t m_database;
|
||||||
};
|
};
|
||||||
} //namespace dindb
|
} //namespace dindb
|
||||||
|
|
||||||
|
|
|
@ -61,13 +61,11 @@ namespace redis {
|
||||||
return boost::get<std::vector<RedisReplyType>>(parReply);
|
return boost::get<std::vector<RedisReplyType>>(parReply);
|
||||||
}
|
}
|
||||||
|
|
||||||
Command::Command (std::string&& parAddress, uint16_t parPort, bool parConnect) :
|
Command::Command (std::string&& parAddress, uint16_t parPort) :
|
||||||
m_conn(nullptr, &redisFree),
|
m_conn(nullptr, &redisFree),
|
||||||
m_address(std::move(parAddress)),
|
m_address(std::move(parAddress)),
|
||||||
m_port(parPort)
|
m_port(parPort)
|
||||||
{
|
{
|
||||||
if (parConnect)
|
|
||||||
this->connect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Command::~Command() noexcept {
|
Command::~Command() noexcept {
|
||||||
|
|
|
@ -60,7 +60,7 @@ namespace redis {
|
||||||
|
|
||||||
class Command {
|
class Command {
|
||||||
public:
|
public:
|
||||||
Command ( std::string&& parAddress, uint16_t parPort, bool parConnect );
|
Command ( std::string&& parAddress, uint16_t parPort );
|
||||||
~Command ( void ) noexcept;
|
~Command ( void ) noexcept;
|
||||||
|
|
||||||
void connect ( void );
|
void connect ( void );
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue