mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2025-02-20 12:14:55 +00:00
Restore adding sets into Redis using the new run() method.
Partial implementation - not for use in real world cases.
This commit is contained in:
parent
f7a7015c65
commit
49a8e81fed
4 changed files with 156 additions and 8 deletions
70
src/backends/redis/arg_to_bin_safe.hpp
Normal file
70
src/backends/redis/arg_to_bin_safe.hpp
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
/* 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 id9348909738B047B7B6912D73CB519039
|
||||||
|
#define id9348909738B047B7B6912D73CB519039
|
||||||
|
|
||||||
|
#include "helpers/compatibility.h"
|
||||||
|
#include <cstddef>
|
||||||
|
#include <boost/utility/string_ref.hpp>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace redis {
|
||||||
|
namespace implem {
|
||||||
|
template <typename T>
|
||||||
|
const char* arg_to_bin_safe_char ( const T& parArg );
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::size_t arg_to_bin_safe_length ( const T& parArg ) a_pure;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct MakeCharInfo;
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct MakeCharInfo<std::string> {
|
||||||
|
MakeCharInfo ( const std::string& parData ) : m_string(parData) {}
|
||||||
|
const char* data ( void ) const { return m_string.data(); }
|
||||||
|
std::size_t size ( void ) const { return m_string.size(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const std::string& m_string;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct MakeCharInfo<boost::string_ref> {
|
||||||
|
MakeCharInfo ( const boost::string_ref& parData ) : m_data(parData.data()), m_size(parData.size()) {}
|
||||||
|
const char* data ( void ) const { return m_data; }
|
||||||
|
std::size_t size ( void ) const { return m_size; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const char* const m_data;
|
||||||
|
const std::size_t m_size;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline const char* arg_to_bin_safe_char (const T& parArg) {
|
||||||
|
return MakeCharInfo<T>(parArg).data();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline std::size_t arg_to_bin_safe_length (const T& parArg) {
|
||||||
|
return MakeCharInfo<T>(parArg).size();
|
||||||
|
}
|
||||||
|
} //namespace implem
|
||||||
|
} //namespace redis
|
||||||
|
|
||||||
|
#endif
|
|
@ -19,8 +19,10 @@
|
||||||
#include "dindexer-machinery/recorddata.hpp"
|
#include "dindexer-machinery/recorddata.hpp"
|
||||||
#include "backends/exposed_functions.hpp"
|
#include "backends/exposed_functions.hpp"
|
||||||
#include "backends/backend_version.hpp"
|
#include "backends/backend_version.hpp"
|
||||||
|
#include "dindexerConfig.h"
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <yaml-cpp/yaml.h>
|
#include <yaml-cpp/yaml.h>
|
||||||
|
#include <boost/lexical_cast.hpp>
|
||||||
|
|
||||||
namespace dindb {
|
namespace dindb {
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -92,7 +94,11 @@ namespace dindb {
|
||||||
}
|
}
|
||||||
|
|
||||||
void BackendRedis::write_files (const std::vector<mchlib::FileRecordData>& parData, const mchlib::SetRecordDataFull& parSetData, const std::string& parSignature) {
|
void BackendRedis::write_files (const std::vector<mchlib::FileRecordData>& parData, const mchlib::SetRecordDataFull& parSetData, const std::string& parSignature) {
|
||||||
//TODO: run command
|
using boost::lexical_cast;
|
||||||
|
|
||||||
|
auto incr_reply = m_redis.run("HINCRBY " PROGRAM_NAME ":indices set 1");
|
||||||
|
const std::string set_key = PROGRAM_NAME ":set:" + lexical_cast<std::string>(redis::get_integer(incr_reply));
|
||||||
|
auto insert_set_reply = m_redis.run("HMSET %b name %b disk_label %b fs_uuid %b", set_key, parSetData.name, parSetData.disk_label, parSetData.fs_uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BackendRedis::search_file_by_hash (mchlib::FileRecordData& parItem, mchlib::SetRecordDataFull& parSet, const mchlib::TigerHash& parHash) {
|
bool BackendRedis::search_file_by_hash (mchlib::FileRecordData& parItem, mchlib::SetRecordDataFull& parSet, const mchlib::TigerHash& parHash) {
|
||||||
|
|
|
@ -22,12 +22,45 @@
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <boost/iterator/transform_iterator.hpp>
|
||||||
|
#include <boost/variant/get.hpp>
|
||||||
|
|
||||||
namespace redis {
|
namespace redis {
|
||||||
namespace {
|
namespace {
|
||||||
using RedisReply = std::unique_ptr<redisReply, void(*)(void*)>;
|
using RedisReply = std::unique_ptr<redisReply, void(*)(void*)>;
|
||||||
|
|
||||||
|
RedisReplyType make_redis_reply_type (redisReply* parReply) {
|
||||||
|
using boost::transform_iterator;
|
||||||
|
using PtrToReplyIterator = transform_iterator<RedisReplyType(*)(redisReply*), redisReply**>;
|
||||||
|
|
||||||
|
switch (parReply->type) {
|
||||||
|
case REDIS_REPLY_INTEGER:
|
||||||
|
return parReply->integer;
|
||||||
|
case REDIS_REPLY_STRING:
|
||||||
|
return std::string(parReply->str, parReply->len);
|
||||||
|
case REDIS_REPLY_ARRAY:
|
||||||
|
return std::vector<RedisReplyType>(
|
||||||
|
PtrToReplyIterator(parReply->element, &make_redis_reply_type),
|
||||||
|
PtrToReplyIterator(parReply->element + parReply->elements, &make_redis_reply_type)
|
||||||
|
);
|
||||||
|
default:
|
||||||
|
return RedisReplyType();
|
||||||
|
};
|
||||||
|
}
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
|
long long get_integer (const RedisReplyType& parReply) {
|
||||||
|
return boost::get<long long>(parReply);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string get_string (const RedisReplyType& parReply) {
|
||||||
|
return boost::get<std::string>(parReply);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<RedisReplyType> get_array (const 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, bool parConnect) :
|
||||||
m_conn(nullptr, &redisFree),
|
m_conn(nullptr, &redisFree),
|
||||||
m_address(std::move(parAddress)),
|
m_address(std::move(parAddress)),
|
||||||
|
@ -66,7 +99,10 @@ namespace redis {
|
||||||
m_conn.reset();
|
m_conn.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Command::run (const char* parCommand, int parArgc, const char** parArgv, std::size_t* parLengths) {
|
RedisReplyType Command::run_pvt (const char* parCommand, int parArgc, const char** parArgv, std::size_t* parLengths) {
|
||||||
|
assert(parCommand);
|
||||||
|
assert(parArgv);
|
||||||
|
assert(parLengths); //This /could/ be null, but I don't see why it should
|
||||||
assert(is_connected());
|
assert(is_connected());
|
||||||
|
|
||||||
RedisReply reply(
|
RedisReply reply(
|
||||||
|
@ -74,6 +110,8 @@ namespace redis {
|
||||||
&freeReplyObject
|
&freeReplyObject
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return make_redis_reply_type(reply.get());
|
||||||
|
|
||||||
//std::string key;
|
//std::string key;
|
||||||
//{
|
//{
|
||||||
// std::ostringstream key_oss;
|
// std::ostringstream key_oss;
|
||||||
|
|
|
@ -18,16 +18,46 @@
|
||||||
#ifndef idD83EEBFC927840C6B9F32D61A1D1E582
|
#ifndef idD83EEBFC927840C6B9F32D61A1D1E582
|
||||||
#define idD83EEBFC927840C6B9F32D61A1D1E582
|
#define idD83EEBFC927840C6B9F32D61A1D1E582
|
||||||
|
|
||||||
|
#include "arg_to_bin_safe.hpp"
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <boost/variant/variant.hpp>
|
||||||
|
#include <boost/variant/recursive_wrapper.hpp>
|
||||||
|
#include <vector>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
struct redisContext;
|
struct redisContext;
|
||||||
|
|
||||||
namespace redis {
|
namespace redis {
|
||||||
|
class RedisReplyType;
|
||||||
|
|
||||||
|
namespace implem {
|
||||||
|
using RedisVariantType = boost::variant<
|
||||||
|
long long,
|
||||||
|
std::string,
|
||||||
|
boost::recursive_wrapper<std::vector<RedisReplyType>>
|
||||||
|
>;
|
||||||
|
enum RedisVariantTypes {
|
||||||
|
RedisVariantType_Integer = 0,
|
||||||
|
RedisVariantType_String,
|
||||||
|
RedisVariantType_Array
|
||||||
|
};
|
||||||
|
} //namespace implem
|
||||||
|
|
||||||
|
struct RedisReplyType : implem::RedisVariantType {
|
||||||
|
using base_class = implem::RedisVariantType;
|
||||||
|
|
||||||
|
RedisReplyType ( void ) = default;
|
||||||
|
RedisReplyType ( long long parVal ) : base_class(parVal) {}
|
||||||
|
RedisReplyType ( std::string&& parVal ) : base_class(std::move(parVal)) {}
|
||||||
|
RedisReplyType ( std::vector<RedisReplyType>&& parVal ) : base_class(std::move(parVal)) {}
|
||||||
|
~RedisReplyType ( void ) noexcept = default;
|
||||||
|
};
|
||||||
|
|
||||||
class Command {
|
class Command {
|
||||||
public:
|
public:
|
||||||
Command ( std::string&& parAddress, uint16_t parPort, bool parConnect );
|
Command ( std::string&& parAddress, uint16_t parPort, bool parConnect );
|
||||||
|
@ -39,12 +69,12 @@ namespace redis {
|
||||||
bool is_connected ( void ) const;
|
bool is_connected ( void ) const;
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void run ( const char* parCommand, Args&&... parArgs );
|
RedisReplyType run ( const char* parCommand, Args&&... parArgs );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using RedisConnection = std::unique_ptr<redisContext, void(*)(redisContext*)>;
|
using RedisConnection = std::unique_ptr<redisContext, void(*)(redisContext*)>;
|
||||||
|
|
||||||
void run ( const char* parCommand, int parArgc, const char** parArgv, std::size_t* parLengths );
|
RedisReplyType run_pvt ( const char* parCommand, int parArgc, const char** parArgv, std::size_t* parLengths );
|
||||||
|
|
||||||
RedisConnection m_conn;
|
RedisConnection m_conn;
|
||||||
std::string m_address;
|
std::string m_address;
|
||||||
|
@ -52,7 +82,7 @@ namespace redis {
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void Command::run (const char* parCommand, Args&&... parArgs) {
|
RedisReplyType Command::run (const char* parCommand, Args&&... parArgs) {
|
||||||
constexpr const std::size_t arg_count = sizeof...(Args);
|
constexpr const std::size_t arg_count = sizeof...(Args);
|
||||||
using CharPointerArray = std::array<const char*, arg_count>;
|
using CharPointerArray = std::array<const char*, arg_count>;
|
||||||
using LengthArray = std::array<std::size_t, arg_count>;
|
using LengthArray = std::array<std::size_t, arg_count>;
|
||||||
|
@ -61,13 +91,17 @@ namespace redis {
|
||||||
LengthArray lengths;
|
LengthArray lengths;
|
||||||
assert(false); //TODO write implementation
|
assert(false); //TODO write implementation
|
||||||
|
|
||||||
this->run(
|
return this->run_pvt(
|
||||||
parCommand,
|
parCommand,
|
||||||
static_cast<int>(arg_count),
|
static_cast<int>(arg_count),
|
||||||
arguments.data(),
|
CharPointerArray{ implem::arg_to_bin_safe_char(std::forward<Args>(parArgs))... }.data(),
|
||||||
lengths.data()
|
LengthArray{ implem::arg_to_bin_safe_length(std::forward<Args>(parArgs))... }.data()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long long get_integer ( const RedisReplyType& parReply );
|
||||||
|
std::string get_string ( const RedisReplyType& parReply );
|
||||||
|
std::vector<RedisReplyType> get_array ( const RedisReplyType& parReply );
|
||||||
} //namespace redis
|
} //namespace redis
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue