1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-07-03 14:14:11 +00:00

Move redis range to record data struct into a separate file.

This commit is contained in:
King_DuckZ 2016-07-10 13:56:46 +01:00
parent d6e95eac8e
commit 01d587b7da
3 changed files with 118 additions and 74 deletions

View file

@ -22,6 +22,7 @@
#include "helpers/lexical_cast.hpp" #include "helpers/lexical_cast.hpp"
#include "dindexerConfig.h" #include "dindexerConfig.h"
#include "helpers/stringize.h" #include "helpers/stringize.h"
#include "record_data_adapt.hpp"
#include <utility> #include <utility>
#include <yaml-cpp/yaml.h> #include <yaml-cpp/yaml.h>
#include <array> #include <array>
@ -38,65 +39,6 @@ namespace dindb {
uint16_t database; uint16_t database;
}; };
std::pair<std::string, mchlib::FileRecordData> pair_list_to_file_record (const redis::Command::hscan_range& parRange) {
using dinhelp::lexical_cast;
mchlib::FileRecordData retval;
std::array<std::string, 2> 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<decltype(retval.size)>(
itm.second);
else if (itm.first == "level")
retval.level = lexical_cast<decltype(retval.level)>(
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<uint16_t>(mime[0].size());
retval.mime_charset_length = static_cast<uint16_t>(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) { std::string read_script (const dincore::SearchPaths& parSearch, const char* parName) {
const auto full_path = parSearch.first_hit(boost::string_ref(parName)); const auto full_path = parSearch.first_hit(boost::string_ref(parName));
if (full_path.empty()) { if (full_path.empty()) {
@ -143,19 +85,6 @@ namespace YAML {
}; };
} //namespace YAML } //namespace YAML
//namespace redis {
// template <>
// struct RedisStructAdapt<mchlib::FileRecordData> {
// 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 { namespace dindb {
BackendRedis::BackendRedis(std::string&& parAddress, uint16_t parPort, uint16_t parDatabase, bool parConnect, dincore::SearchPaths&& parLuaPaths) : BackendRedis::BackendRedis(std::string&& parAddress, uint16_t parPort, uint16_t parDatabase, bool parConnect, dincore::SearchPaths&& parLuaPaths) :
m_redis(std::move(parAddress), parPort), m_redis(std::move(parAddress), parPort),
@ -294,7 +223,7 @@ namespace dindb {
} }
else { else {
const auto result_id = redis::get_string(hash_reply); 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<FileRecordDataWithGroup>(m_redis.hscan(result_id));
parItem = std::move(set_key_and_file_item.second); parItem = std::move(set_key_and_file_item.second);
const std::string group_key = std::move(set_key_and_file_item.first); const std::string group_key = std::move(set_key_and_file_item.first);
@ -303,7 +232,7 @@ namespace dindb {
return false; return false;
} }
else { else {
parSet = pair_list_to_set_record(m_redis.hscan(group_key)); parSet = redis::range_as<mchlib::SetRecordDataFull>(m_redis.hscan(group_key));
return true; return true;
} }
} }

View file

@ -31,7 +31,9 @@
#include <vector> #include <vector>
#include <utility> #include <utility>
#include <boost/range/iterator_range_core.hpp> #include <boost/range/iterator_range_core.hpp>
#include <boost/range/empty.hpp>
#include <boost/utility/string_ref.hpp> #include <boost/utility/string_ref.hpp>
#include <ciso646>
#include <stdexcept> #include <stdexcept>
namespace redis { namespace redis {
@ -83,6 +85,22 @@ namespace redis {
batch.throw_if_failed(); batch.throw_if_failed();
return batch.replies().front(); return batch.replies().front();
} }
template <typename T>
struct StructAdapt;
template <typename AS, typename I>
AS range_conv ( const boost::iterator_range<I>& parRange );
template <typename AS, typename I>
inline AS range_as (const boost::iterator_range<I>& parRange) {
assert(not boost::empty(parRange));
AS retval;
const auto success = StructAdapt<AS>::decode(parRange, retval);
if (not success)
throw std::runtime_error("Error decoding range");
return retval;
};
} //namespace redis } //namespace redis
#endif #endif

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef id2949D72CC2F246D4A289FFB820CC3A8F
#define id2949D72CC2F246D4A289FFB820CC3A8F
#include "dindexer-machinery/recorddata.hpp"
#include "helpers/lexical_cast.hpp"
#include <array>
#include <string>
#include <cstdint>
#include <utility>
namespace dindb {
using FileRecordDataWithGroup = std::pair<std::string, mchlib::FileRecordData>;
} //namespace dindb
namespace redis {
template <>
struct StructAdapt<dindb::FileRecordDataWithGroup> {
template <typename R>
static bool decode (const R& parRange, dindb::FileRecordDataWithGroup& parOut) {
using dinhelp::lexical_cast;
std::array<std::string, 2> 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<decltype(parOut.second.size)>(itm.second);
else if (itm.first == "level")
parOut.second.level = lexical_cast<decltype(parOut.second.level)>(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<uint16_t>(mime[0].size());
parOut.second.mime_charset_length = static_cast<uint16_t>(mime[1].size());
return true;
}
};
template <>
struct StructAdapt<mchlib::SetRecordDataFull> {
template <typename R>
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