1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-02-16 11:35:49 +00:00

Implement locate_in_db() by hash.

This commit is contained in:
King_DuckZ 2016-07-12 16:42:27 +01:00
parent 01be1a94e5
commit afb2e68849
5 changed files with 71 additions and 2 deletions

View file

@ -250,7 +250,7 @@ namespace dindb {
}
std::vector<LocatedItem> BackendRedis::locate_in_db (const mchlib::TigerHash& parSearch, const TagList& parTags) {
return std::vector<LocatedItem>();
return dindb::locate_in_db(m_redis, parSearch, parTags);
}
std::vector<LocatedSet> BackendRedis::locate_sets_in_db (const std::string& parSearch, bool parCaseInsensitive) {

View file

@ -20,9 +20,13 @@
#include "helpers/lexical_cast.hpp"
#include "dindexerConfig.h"
#include "dindexer-core/split_tags.hpp"
#include "dindexer-machinery/tiger.hpp"
#include <boost/regex.hpp>
#include <ciso646>
#include <algorithm>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/iterator/zip_iterator.hpp>
namespace dindb {
namespace {
@ -62,6 +66,20 @@ namespace dindb {
++id_index;
}
}
//See: http://stackoverflow.com/questions/12552277/whats-the-best-way-to-iterate-over-two-or-more-containers-simultaneously/12553437#12553437
//(referenced from http://stackoverflow.com/questions/16982190/c-use-boost-range-transformed-adaptor-with-binary-function)
//What became of this? http://marc.info/?l=boost-users&m=129619765731342
template <typename... Conts>
auto zip_range (Conts&... parConts) -> decltype(boost::make_iterator_range(
boost::make_zip_iterator(boost::make_tuple(parConts.begin()...)),
boost::make_zip_iterator(boost::make_tuple(parConts.end()...)))
) {
return {
boost::make_zip_iterator(boost::make_tuple(parConts.begin()...)),
boost::make_zip_iterator(boost::make_tuple(parConts.end()...))
};
}
} //unnamed namespace
std::vector<GroupIDType> find_all_sets (redis::IncRedis& parRedis) {
@ -104,4 +122,49 @@ namespace dindb {
store_matching_paths(batch, retval, ids, search, parTags);
return retval;
}
std::vector<LocatedItem> locate_in_db (redis::IncRedis& parRedis, const mchlib::TigerHash& parSearch, const TagList& parTags) {
using boost::adaptors::filtered;
using boost::adaptors::transformed;
using boost::tuple;
using boost::make_tuple;
using redis::get_string;
using redis::Reply;
using std::vector;
using dinhelp::lexical_cast;
const auto hash_key = PROGRAM_NAME ":hash:" + mchlib::tiger_to_string(parSearch, false);
const auto file_ids = parRedis.smembers(hash_key);
vector<std::string> ids;
if (file_ids) {
auto batch = parRedis.make_batch();
for (auto&& file_id : *file_ids) {
if (not file_id)
continue;
const auto file_key = PROGRAM_NAME ":file:" + *file_id;
ids.emplace_back(std::move(*file_id));
batch.hmget(file_key, "path", "group_id", "tags");
}
batch.throw_if_failed();
assert(batch.replies().size() == ids.size());
return boost::copy_range<vector<LocatedItem>>(
zip_range(batch.replies(), ids) |
transformed([](const tuple<Reply, std::string>& r)
{ return make_tuple(redis::get_array(r.get<0>()), r.get<1>()); }
) |
filtered([&parTags](const tuple<vector<Reply>, std::string>& t)
{ return parTags.empty() or all_tags_match(parTags, get_string(t.get<0>()[2])); }
) |
transformed([&ids](const tuple<vector<Reply>, std::string>& t)
{ return LocatedItem{ get_string(t.get<0>()[0]), lexical_cast<FileIDType>(t.get<1>()), lexical_cast<GroupIDType>(get_string(t.get<0>()[1])) }; }
)
);
}
else {
return std::vector<LocatedItem>();
}
}
} //namespace dindb

View file

@ -28,6 +28,7 @@ namespace redis {
namespace dindb {
std::vector<GroupIDType> find_all_sets ( redis::IncRedis& parRedis );
std::vector<LocatedItem> locate_in_db ( redis::IncRedis& parRedis, const std::string& parSearch, const TagList& parTags );
std::vector<LocatedItem> locate_in_db ( redis::IncRedis& parRedis, const mchlib::TigerHash& parSearch, const TagList& parTags );
} //namespace dindb
#endif

View file

@ -24,7 +24,7 @@
namespace redis {
namespace {
inline IncRedis::opt_string optional_string ( const Reply& parReply ) a_always_inline;
inline IncRedis::opt_string_list optional_string_list ( const Reply& parReply ) a_always_inline;
IncRedis::opt_string_list optional_string_list ( const Reply& parReply );
IncRedis::opt_string optional_string (const Reply& parReply) {
assert(parReply.which() == RedisVariantType_Nil or parReply.which() == RedisVariantType_String);
@ -115,6 +115,10 @@ namespace redis {
return optional_string(m_command.run("SRANDMEMBER", parKey));
}
auto IncRedis::smembers (boost::string_ref parKey) -> opt_string_list {
return optional_string_list(m_command.run("SMEMBERS", parKey));
}
bool IncRedis::script_flush() {
const auto ret = get<StatusString>(m_command.run("SCRIPT", "FLUSH"));
return ret.is_ok();

View file

@ -74,6 +74,7 @@ namespace redis {
//Set
opt_string_list srandmember ( boost::string_ref parKey, int parCount );
opt_string srandmember ( boost::string_ref parKey );
opt_string_list smembers ( boost::string_ref parKey );
//Script
bool script_flush ( void );