1
0
Fork 0
mirror of https://github.com/KingDuckZ/dindexer.git synced 2025-02-19 12:04:54 +00:00

Stub implementation of a Redis backend.

This commit is contained in:
King_DuckZ 2016-06-07 22:45:30 +01:00
parent d5cf8e21b2
commit 0ab66fa1f1
6 changed files with 257 additions and 2 deletions

View file

@ -87,7 +87,6 @@ configure_file(
target_include_directories(${PROJECT_NAME} SYSTEM
INTERFACE ${Boost_INCLUDE_DIRS}
INTERFACE ${PostgreSQL_INCLUDE_DIRS}
)
target_compile_features(${PROJECT_NAME}
@ -147,7 +146,6 @@ if (BUILD_TESTING)
endif()
target_link_libraries(${PROJECT_NAME}
INTERFACE ${PostgreSQL_LIBRARIES}
INTERFACE ${Boost_LIBRARIES}
INTERFACE ${bare_name}-pq
INTERFACE ${bare_name}-inc

View file

@ -25,5 +25,7 @@ target_link_libraries(${PROJECT_NAME}
#)
add_subdirectory(postgresql)
add_subdirectory(redis)
add_dependencies(${PROJECT_NAME} ${bare_name}-backend-postgresql)
add_dependencies(${PROJECT_NAME} ${bare_name}-backend-redis)

View file

@ -0,0 +1,24 @@
project(${bare_name}-backend-redis CXX)
find_package(hiredis 0.11.0 REQUIRED)
add_library(${PROJECT_NAME} SHARED
backend_redis.cpp
)
target_include_directories(${PROJECT_NAME} SYSTEM
PUBLIC ${Boost_INCLUDE_DIRS}
PRIVATE ${HIREDIS_INCLUDE_DIRS}
)
target_link_libraries(${PROJECT_NAME}
PRIVATE ${bare_name}-inc
PRIVATE ${bare_name}-pq
PRIVATE ${HIREDIS_LIBRARIES}
)
install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
ARCHIVE DESTINATION lib/static
)

View file

@ -0,0 +1,158 @@
/* 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 "backend_redis.hpp"
#include "dindexer-machinery/recorddata.hpp"
#include <utility>
#include <hiredis/hiredis.h>
#include <ciso646>
#include <stdexcept>
#include <sstream>
#include <algorithm>
#include <cassert>
namespace dindb {
namespace {
using RedisReply = std::unique_ptr<redisReply, void(*)(void*)>;
} //unnamed namespace
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() 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);
}
}
void BackendRedis::disconnect() {
m_conn.reset();
}
void BackendRedis::tag_files (const std::vector<FileIDType>& parFiles, const std::vector<boost::string_ref>& parTags, GroupIDType parSet) {
}
void BackendRedis::tag_files (const std::vector<std::string>& parRegexes, const std::vector<boost::string_ref>& parTags, GroupIDType parSet) {
}
void BackendRedis::delete_tags (const std::vector<FileIDType>& parFiles, const std::vector<boost::string_ref>& parTags, GroupIDType parSet) {
}
void BackendRedis::delete_tags (const std::vector<std::string>& parRegexes, const std::vector<boost::string_ref>& parTags, GroupIDType parSet) {
}
void BackendRedis::delete_all_tags (const std::vector<FileIDType>& parFiles, GroupIDType parSet) {
}
void BackendRedis::delete_all_tags (const std::vector<std::string>& parRegexes, GroupIDType parSet) {
}
void BackendRedis::delete_group (const std::vector<uint32_t>& parIDs, ConfirmDeleCallback parConf) {
}
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
);
}
bool BackendRedis::search_file_by_hash (mchlib::FileRecordData& parItem, mchlib::SetRecordDataFull& parSet, const mchlib::TigerHash& parHash) {
return false;
}
std::vector<LocatedItem> BackendRedis::locate_in_db (const std::string& parSearch, const TagList& parTags) {
return std::vector<LocatedItem>();
}
std::vector<LocatedItem> BackendRedis::locate_in_db (const mchlib::TigerHash& parSearch, const TagList& parTags) {
return std::vector<LocatedItem>();
}
std::vector<LocatedSet> BackendRedis::locate_sets_in_db (const std::string& parSearch, bool parCaseInsensitive) {
return std::vector<LocatedSet>();
}
std::vector<LocatedSet> BackendRedis::locate_sets_in_db (const std::string& parSearch, const std::vector<GroupIDType>& parSets, bool parCaseInsensitive) {
return std::vector<LocatedSet>();
}
std::vector<GroupIDType> BackendRedis::find_all_sets() {
return std::vector<GroupIDType>();
}
std::vector<dinhelp::MaxSizedArray<std::string, 4>> BackendRedis::find_set_details (const std::vector<GroupIDType>& parSets) {
return std::vector<dinhelp::MaxSizedArray<std::string, 4>>();
}
std::vector<dinhelp::MaxSizedArray<std::string, 1>> BackendRedis::find_file_details (GroupIDType parSetID, uint16_t parLevel, boost::string_ref parDir) {
return std::vector<dinhelp::MaxSizedArray<std::string, 1>>();
}
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

View file

@ -0,0 +1,71 @@
/* 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 idB2F92EE07A004D5293FD0657EEE8F75B
#define idB2F92EE07A004D5293FD0657EEE8F75B
#include "backends/db_backend.hpp"
#include <memory>
#include <string>
#include <cstdint>
struct redisContext;
namespace dindb {
class BackendRedis : public Backend {
public:
BackendRedis ( BackendRedis&& ) = default;
BackendRedis ( std::string&& parAddress, uint16_t parPort, bool parConnect );
virtual ~BackendRedis ( void ) noexcept;
virtual void connect ( void ) override;
virtual void disconnect ( void ) override;
virtual void tag_files ( const std::vector<FileIDType>& parFiles, const std::vector<boost::string_ref>& parTags, GroupIDType parSet ) override;
virtual void tag_files ( const std::vector<std::string>& parRegexes, const std::vector<boost::string_ref>& parTags, GroupIDType parSet ) override;
virtual void delete_tags ( const std::vector<FileIDType>& parFiles, const std::vector<boost::string_ref>& parTags, GroupIDType parSet ) override;
virtual void delete_tags ( const std::vector<std::string>& parRegexes, const std::vector<boost::string_ref>& parTags, GroupIDType parSet ) override;
virtual void delete_all_tags ( const std::vector<FileIDType>& parFiles, GroupIDType parSet ) override;
virtual void delete_all_tags ( const std::vector<std::string>& parRegexes, GroupIDType parSet ) override;
virtual void delete_group ( const std::vector<uint32_t>& parIDs, ConfirmDeleCallback parConf ) override;
virtual void write_files ( const std::vector<mchlib::FileRecordData>& parData, const mchlib::SetRecordDataFull& parSetData, const std::string& parSignature ) override;
virtual bool search_file_by_hash ( mchlib::FileRecordData& parItem, mchlib::SetRecordDataFull& parSet, const mchlib::TigerHash& parHash ) override;
virtual std::vector<LocatedItem> locate_in_db ( const std::string& parSearch, const TagList& parTags ) override;
virtual std::vector<LocatedItem> locate_in_db ( const mchlib::TigerHash& parSearch, const TagList& parTags ) override;
virtual std::vector<LocatedSet> locate_sets_in_db ( const std::string& parSearch, bool parCaseInsensitive ) override;
virtual std::vector<LocatedSet> locate_sets_in_db ( const std::string& parSearch, const std::vector<GroupIDType>& parSets, bool parCaseInsensitive ) override;
virtual std::vector<GroupIDType> find_all_sets ( void ) override;
virtual std::vector<dinhelp::MaxSizedArray<std::string, 4>> find_set_details ( const std::vector<GroupIDType>& parSets ) override;
virtual std::vector<dinhelp::MaxSizedArray<std::string, 1>> find_file_details ( GroupIDType parSetID, uint16_t parLevel, boost::string_ref parDir ) override;
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;
};
} //namespace dindb
#endif

View file

@ -12,10 +12,12 @@ add_library(${PROJECT_NAME} STATIC
target_include_directories(${PROJECT_NAME}
PRIVATE ${libpqtypes_INCLUDE_DIRS}
PRIVATE ${PostgreSQL_INCLUDE_DIRS}
)
target_link_libraries(${PROJECT_NAME}
PRIVATE ${bare_name}-if
PRIVATE pqtypes
PRIVATE ${PostgreSQL_LIBRARIES}
)
target_compile_features(${PROJECT_NAME}