mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2024-11-25 00:53:43 +00:00
Start putting Redis stuff in a wrapper class.
Nonworking, but the project builds.
This commit is contained in:
parent
db23dcea3f
commit
f7a7015c65
5 changed files with 188 additions and 74 deletions
|
@ -4,6 +4,7 @@ find_package(hiredis 0.11.0 REQUIRED)
|
|||
|
||||
add_library(${PROJECT_NAME} SHARED
|
||||
backend_redis.cpp
|
||||
command.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} SYSTEM
|
||||
|
|
|
@ -20,18 +20,10 @@
|
|||
#include "backends/exposed_functions.hpp"
|
||||
#include "backends/backend_version.hpp"
|
||||
#include <utility>
|
||||
#include <hiredis/hiredis.h>
|
||||
#include <ciso646>
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
namespace dindb {
|
||||
namespace {
|
||||
using RedisReply = std::unique_ptr<redisReply, void(*)(void*)>;
|
||||
|
||||
struct RedisConnectionSettings {
|
||||
std::string address;
|
||||
uint16_t port;
|
||||
|
@ -62,42 +54,20 @@ namespace YAML {
|
|||
} //namespace YAML
|
||||
|
||||
namespace dindb {
|
||||
BackendRedis::BackendRedis(std::string &&parAddress, uint16_t parPort,
|
||||
bool parConnect) :
|
||||
m_conn(nullptr, &redisFree),
|
||||
m_address(std::move(parAddress)),
|
||||
m_port(parPort) {
|
||||
if (parConnect)
|
||||
this->connect();
|
||||
BackendRedis::BackendRedis(std::string &&parAddress, uint16_t parPort, bool parConnect) :
|
||||
m_redis(std::move(parAddress), parPort, parConnect)
|
||||
{
|
||||
}
|
||||
|
||||
BackendRedis::~BackendRedis() noexcept {
|
||||
}
|
||||
|
||||
void BackendRedis::connect() {
|
||||
if (not m_conn) {
|
||||
struct timeval timeout = {5, 500000}; //5.5 seconds?
|
||||
RedisConnection conn(
|
||||
redisConnectWithTimeout(m_address.c_str(), m_port, timeout),
|
||||
&redisFree
|
||||
);
|
||||
if (not conn) {
|
||||
std::ostringstream oss;
|
||||
oss << "Unable to connect to Redis server at " << m_address << ':' << m_port;
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
if (conn->err) {
|
||||
std::ostringstream oss;
|
||||
oss << "Unable to connect to Redis server at " << m_address << ':' << m_port <<
|
||||
": " << conn->errstr;
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
std::swap(conn, m_conn);
|
||||
}
|
||||
}
|
||||
m_redis.connect();
|
||||
}
|
||||
|
||||
void BackendRedis::disconnect() {
|
||||
m_conn.reset();
|
||||
m_redis.disconnect();
|
||||
}
|
||||
|
||||
void BackendRedis::tag_files (const std::vector<FileIDType>& parFiles, const std::vector<boost::string_ref>& parTags, GroupIDType parSet) {
|
||||
|
@ -122,30 +92,7 @@ namespace dindb {
|
|||
}
|
||||
|
||||
void BackendRedis::write_files (const std::vector<mchlib::FileRecordData>& parData, const mchlib::SetRecordDataFull& parSetData, const std::string& parSignature) {
|
||||
assert(is_connected());
|
||||
std::string key;
|
||||
{
|
||||
std::ostringstream key_oss;
|
||||
RedisReply incr_reply(static_cast<redisReply*>(redisCommand(m_conn.get(), "incr set_counter")), &freeReplyObject);
|
||||
key_oss << "sets:" << incr_reply->integer;
|
||||
key = key_oss.str();
|
||||
}
|
||||
|
||||
RedisReply insert_reply(
|
||||
static_cast<redisReply*>(redisCommand(
|
||||
m_conn.get(),
|
||||
"hmset %b name %b disk_label %b fs_uuid %b",
|
||||
key.data(),
|
||||
key.size(),
|
||||
parSetData.name.data(),
|
||||
parSetData.name.size(),
|
||||
parSetData.disk_label.data(),
|
||||
parSetData.disk_label.size(),
|
||||
parSetData.fs_uuid.data(),
|
||||
parSetData.fs_uuid.size()
|
||||
)),
|
||||
&freeReplyObject
|
||||
);
|
||||
//TODO: run command
|
||||
}
|
||||
|
||||
bool BackendRedis::search_file_by_hash (mchlib::FileRecordData& parItem, mchlib::SetRecordDataFull& parSet, const mchlib::TigerHash& parHash) {
|
||||
|
@ -183,10 +130,6 @@ namespace dindb {
|
|||
std::vector<std::string> BackendRedis::find_paths_starting_by (GroupIDType parGroupID, uint16_t parLevel, boost::string_ref parPath) {
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
|
||||
bool BackendRedis::is_connected() const {
|
||||
return m_conn and not m_conn->err;
|
||||
}
|
||||
} //namespace dindb
|
||||
|
||||
extern "C" dindb::Backend* dindexer_create_backend (const YAML::Node* parConfig) {
|
||||
|
|
|
@ -19,12 +19,10 @@
|
|||
#define idB2F92EE07A004D5293FD0657EEE8F75B
|
||||
|
||||
#include "backends/db_backend.hpp"
|
||||
#include <memory>
|
||||
#include "command.hpp"
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
|
||||
struct redisContext;
|
||||
|
||||
namespace dindb {
|
||||
class BackendRedis : public Backend {
|
||||
public:
|
||||
|
@ -58,13 +56,7 @@ namespace dindb {
|
|||
virtual std::vector<std::string> find_paths_starting_by ( GroupIDType parGroupID, uint16_t parLevel, boost::string_ref parPath ) override;
|
||||
|
||||
private:
|
||||
using RedisConnection = std::unique_ptr<redisContext, void(*)(redisContext*)>;
|
||||
|
||||
bool is_connected ( void ) const;
|
||||
|
||||
RedisConnection m_conn;
|
||||
std::string m_address;
|
||||
uint16_t m_port;
|
||||
redis::Command m_redis;
|
||||
};
|
||||
} //namespace dindb
|
||||
|
||||
|
|
105
src/backends/redis/command.cpp
Normal file
105
src/backends/redis/command.cpp
Normal file
|
@ -0,0 +1,105 @@
|
|||
/* Copyright 2015, 2016, Michele Santullo
|
||||
* This file is part of "dindexer".
|
||||
*
|
||||
* "dindexer" is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* "dindexer" is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "command.hpp"
|
||||
#include <hiredis/hiredis.h>
|
||||
#include <ciso646>
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace redis {
|
||||
namespace {
|
||||
using RedisReply = std::unique_ptr<redisReply, void(*)(void*)>;
|
||||
} //unnamed namespace
|
||||
|
||||
Command::Command (std::string&& parAddress, uint16_t parPort, bool parConnect) :
|
||||
m_conn(nullptr, &redisFree),
|
||||
m_address(std::move(parAddress)),
|
||||
m_port(parPort)
|
||||
{
|
||||
if (parConnect)
|
||||
this->connect();
|
||||
}
|
||||
|
||||
Command::~Command() noexcept {
|
||||
}
|
||||
|
||||
void Command::connect() {
|
||||
if (not m_conn) {
|
||||
struct timeval timeout = {5, 500000}; //5.5 seconds?
|
||||
RedisConnection conn(
|
||||
redisConnectWithTimeout(m_address.c_str(), m_port, timeout),
|
||||
&redisFree
|
||||
);
|
||||
if (not conn) {
|
||||
std::ostringstream oss;
|
||||
oss << "Unable to connect to Redis server at " << m_address << ':' << m_port;
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
if (conn->err) {
|
||||
std::ostringstream oss;
|
||||
oss << "Unable to connect to Redis server at " << m_address << ':' << m_port <<
|
||||
": " << conn->errstr;
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
std::swap(conn, m_conn);
|
||||
}
|
||||
}
|
||||
|
||||
void Command::disconnect() {
|
||||
m_conn.reset();
|
||||
}
|
||||
|
||||
void Command::run (const char* parCommand, int parArgc, const char** parArgv, std::size_t* parLengths) {
|
||||
assert(is_connected());
|
||||
|
||||
RedisReply reply(
|
||||
static_cast<redisReply*>(redisCommandArgv(m_conn.get(), parArgc, parArgv, parLengths)),
|
||||
&freeReplyObject
|
||||
);
|
||||
|
||||
//std::string key;
|
||||
//{
|
||||
// std::ostringstream key_oss;
|
||||
// RedisReply incr_reply(static_cast<redisReply*>(redisCommand(m_conn.get(), "incr set_counter")), &freeReplyObject);
|
||||
// key_oss << "sets:" << incr_reply->integer;
|
||||
// key = key_oss.str();
|
||||
//}
|
||||
|
||||
//RedisReply insert_reply(
|
||||
// static_cast<redisReply*>(redisCommand(
|
||||
// m_conn.get(),
|
||||
// "hmset %b name %b disk_label %b fs_uuid %b",
|
||||
// key.data(),
|
||||
// key.size(),
|
||||
// parSetData.name.data(),
|
||||
// parSetData.name.size(),
|
||||
// parSetData.disk_label.data(),
|
||||
// parSetData.disk_label.size(),
|
||||
// parSetData.fs_uuid.data(),
|
||||
// parSetData.fs_uuid.size()
|
||||
// )),
|
||||
// &freeReplyObject
|
||||
//);
|
||||
}
|
||||
|
||||
bool Command::is_connected() const {
|
||||
return m_conn and not m_conn->err;
|
||||
}
|
||||
} //namespace redis
|
73
src/backends/redis/command.hpp
Normal file
73
src/backends/redis/command.hpp
Normal file
|
@ -0,0 +1,73 @@
|
|||
/* Copyright 2015, 2016, Michele Santullo
|
||||
* This file is part of "dindexer".
|
||||
*
|
||||
* "dindexer" is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* "dindexer" is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef idD83EEBFC927840C6B9F32D61A1D1E582
|
||||
#define idD83EEBFC927840C6B9F32D61A1D1E582
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <cassert>
|
||||
|
||||
struct redisContext;
|
||||
|
||||
namespace redis {
|
||||
class Command {
|
||||
public:
|
||||
Command ( std::string&& parAddress, uint16_t parPort, bool parConnect );
|
||||
~Command ( void ) noexcept;
|
||||
|
||||
void connect ( void );
|
||||
void disconnect ( void );
|
||||
|
||||
bool is_connected ( void ) const;
|
||||
|
||||
template <typename... Args>
|
||||
void run ( const char* parCommand, Args&&... parArgs );
|
||||
|
||||
private:
|
||||
using RedisConnection = std::unique_ptr<redisContext, void(*)(redisContext*)>;
|
||||
|
||||
void run ( const char* parCommand, int parArgc, const char** parArgv, std::size_t* parLengths );
|
||||
|
||||
RedisConnection m_conn;
|
||||
std::string m_address;
|
||||
uint16_t m_port;
|
||||
};
|
||||
|
||||
template <typename... Args>
|
||||
void Command::run (const char* parCommand, Args&&... parArgs) {
|
||||
constexpr const std::size_t arg_count = sizeof...(Args);
|
||||
using CharPointerArray = std::array<const char*, arg_count>;
|
||||
using LengthArray = std::array<std::size_t, arg_count>;
|
||||
|
||||
CharPointerArray arguments;
|
||||
LengthArray lengths;
|
||||
assert(false); //TODO write implementation
|
||||
|
||||
this->run(
|
||||
parCommand,
|
||||
static_cast<int>(arg_count),
|
||||
arguments.data(),
|
||||
lengths.data()
|
||||
);
|
||||
}
|
||||
} //namespace redis
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue