From 01d587b7dae4d47929961d4043f69f7c543eb18e Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 10 Jul 2016 13:56:46 +0100 Subject: [PATCH] Move redis range to record data struct into a separate file. --- src/backends/redis/backend_redis.cpp | 77 +------------------ src/backends/redis/command.hpp | 18 +++++ src/backends/redis/record_data_adapt.hpp | 97 ++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 74 deletions(-) create mode 100644 src/backends/redis/record_data_adapt.hpp diff --git a/src/backends/redis/backend_redis.cpp b/src/backends/redis/backend_redis.cpp index 3bd8543..63c6eab 100644 --- a/src/backends/redis/backend_redis.cpp +++ b/src/backends/redis/backend_redis.cpp @@ -22,6 +22,7 @@ #include "helpers/lexical_cast.hpp" #include "dindexerConfig.h" #include "helpers/stringize.h" +#include "record_data_adapt.hpp" #include #include #include @@ -38,65 +39,6 @@ namespace dindb { uint16_t database; }; - std::pair pair_list_to_file_record (const redis::Command::hscan_range& parRange) { - using dinhelp::lexical_cast; - - mchlib::FileRecordData retval; - std::array mime; - std::string group_key; - - for (const auto itm : parRange) { - if (itm.first == "path") - retval.abs_path = itm.second; - else if (itm.first == "hash") - retval.hash = mchlib::string_to_tiger(itm.second); - else if (itm.first == "size") - retval.size = lexical_cast( - itm.second); - else if (itm.first == "level") - retval.level = lexical_cast( - itm.second); - else if (itm.first == "mime_type") - mime[0] = itm.second; - else if (itm.first == "mime_charset") - mime[1] = itm.second; - else if (itm.first == "is_directory") - retval.is_directory = (itm.second[0] == '0' ? false : true); - else if (itm.first == "is_symlink") - retval.is_symlink = (itm.second[0] == '0' ? false : true); - else if (itm.first == "unreadable") - retval.unreadable = (itm.second[0] == '0' ? false : true); - else if (itm.first == "hash_valid") - retval.hash_valid = (itm.second[0] == '0' ? false : true); - else if (itm.first == "group_id") - group_key = itm.second; - } - retval.mime_full = mime[0] + mime[1]; - retval.mime_type_offset = 0; - retval.mime_type_length = retval.mime_charset_offset = static_cast(mime[0].size()); - retval.mime_charset_length = static_cast(mime[1].size()); - return std::make_pair(group_key, std::move(retval)); - } - - mchlib::SetRecordDataFull pair_list_to_set_record (const redis::Command::hscan_range& parRange) { - using dinhelp::lexical_cast; - - mchlib::SetRecordDataFull retval; - for (const auto& itm : parRange) { - if (itm.first == "name") - retval.name = itm.second; - else if (itm.first == "disk_label") - retval.disk_label = itm.second; - else if (itm.first == "fs_uuid") - retval.fs_uuid = itm.second; - else if (itm.first == "type") - retval.type = itm.second[0]; - else if (itm.first == "content_type") - retval.content_type = itm.second[0]; - } - return retval; - } - std::string read_script (const dincore::SearchPaths& parSearch, const char* parName) { const auto full_path = parSearch.first_hit(boost::string_ref(parName)); if (full_path.empty()) { @@ -143,19 +85,6 @@ namespace YAML { }; } //namespace YAML -//namespace redis { -// template <> -// struct RedisStructAdapt { -// static bool decode (const Command::hscan_range& parFrom, mchlib::FileRecordData& parOut) { -// return true; -// } -// -// static void encode (const Command::hscan_range& parFrom, mchlib::FileRecordData& parOut) { -// return true; -// } -// }; -//} - namespace dindb { BackendRedis::BackendRedis(std::string&& parAddress, uint16_t parPort, uint16_t parDatabase, bool parConnect, dincore::SearchPaths&& parLuaPaths) : m_redis(std::move(parAddress), parPort), @@ -294,7 +223,7 @@ namespace dindb { } else { const auto result_id = redis::get_string(hash_reply); - auto set_key_and_file_item = pair_list_to_file_record(m_redis.hscan(result_id)); + auto set_key_and_file_item = redis::range_as(m_redis.hscan(result_id)); parItem = std::move(set_key_and_file_item.second); const std::string group_key = std::move(set_key_and_file_item.first); @@ -303,7 +232,7 @@ namespace dindb { return false; } else { - parSet = pair_list_to_set_record(m_redis.hscan(group_key)); + parSet = redis::range_as(m_redis.hscan(group_key)); return true; } } diff --git a/src/backends/redis/command.hpp b/src/backends/redis/command.hpp index d9c6fbf..17c3172 100644 --- a/src/backends/redis/command.hpp +++ b/src/backends/redis/command.hpp @@ -31,7 +31,9 @@ #include #include #include +#include #include +#include #include namespace redis { @@ -83,6 +85,22 @@ namespace redis { batch.throw_if_failed(); return batch.replies().front(); } + + template + struct StructAdapt; + + template + AS range_conv ( const boost::iterator_range& parRange ); + + template + inline AS range_as (const boost::iterator_range& parRange) { + assert(not boost::empty(parRange)); + AS retval; + const auto success = StructAdapt::decode(parRange, retval); + if (not success) + throw std::runtime_error("Error decoding range"); + return retval; + }; } //namespace redis #endif diff --git a/src/backends/redis/record_data_adapt.hpp b/src/backends/redis/record_data_adapt.hpp new file mode 100644 index 0000000..c0aaaca --- /dev/null +++ b/src/backends/redis/record_data_adapt.hpp @@ -0,0 +1,97 @@ +/* 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 . + */ + +#ifndef id2949D72CC2F246D4A289FFB820CC3A8F +#define id2949D72CC2F246D4A289FFB820CC3A8F + +#include "dindexer-machinery/recorddata.hpp" +#include "helpers/lexical_cast.hpp" +#include +#include +#include +#include + +namespace dindb { + using FileRecordDataWithGroup = std::pair; +} //namespace dindb + +namespace redis { + template <> + struct StructAdapt { + template + static bool decode (const R& parRange, dindb::FileRecordDataWithGroup& parOut) { + using dinhelp::lexical_cast; + + std::array mime; + std::string group_key; + + for (const auto itm : parRange) { + if (itm.first == "path") + parOut.second.abs_path = itm.second; + else if (itm.first == "hash") + parOut.second.hash = mchlib::string_to_tiger(itm.second); + else if (itm.first == "size") + parOut.second.size = lexical_cast(itm.second); + else if (itm.first == "level") + parOut.second.level = lexical_cast(itm.second); + else if (itm.first == "mime_type") + mime[0] = itm.second; + else if (itm.first == "mime_charset") + mime[1] = itm.second; + else if (itm.first == "is_directory") + parOut.second.is_directory = (itm.second[0] == '0' ? false : true); + else if (itm.first == "is_symlink") + parOut.second.is_symlink = (itm.second[0] == '0' ? false : true); + else if (itm.first == "unreadable") + parOut.second.unreadable = (itm.second[0] == '0' ? false : true); + else if (itm.first == "hash_valid") + parOut.second.hash_valid = (itm.second[0] == '0' ? false : true); + else if (itm.first == "group_id") + parOut.first = itm.second; + } + parOut.second.mime_full = mime[0] + mime[1]; + parOut.second.mime_type_offset = 0; + parOut.second.mime_type_length = parOut.second.mime_charset_offset = static_cast(mime[0].size()); + parOut.second.mime_charset_length = static_cast(mime[1].size()); + return true; + } + }; + + template <> + struct StructAdapt { + template + static bool decode (const R& parRange, mchlib::SetRecordDataFull& parOut) { + using dinhelp::lexical_cast; + + for (const auto& itm : parRange) { + if (itm.first == "name") + parOut.name = itm.second; + else if (itm.first == "disk_label") + parOut.disk_label = itm.second; + else if (itm.first == "fs_uuid") + parOut.fs_uuid = itm.second; + else if (itm.first == "type") + parOut.type = itm.second[0]; + else if (itm.first == "content_type") + parOut.content_type = itm.second[0]; + } + return true; + } + }; +} //namespace redis + +#endif