Добавлен клиент Redis

This commit is contained in:
Andrei Astafev 2020-04-26 21:38:35 +03:00
parent c1740b2223
commit b41f17a9b2
20 changed files with 2450 additions and 968 deletions

View File

@ -1,6 +1,7 @@
#include "client.hpp"
#include <myx/redis/pubsub.hpp>
#include <myx/redis/client.hpp>
#include <QCoreApplication>
#include <QDebug>
@ -12,6 +13,7 @@ int main( int argc, char** argv )
QCoreApplication app( argc, argv );
MR::PubSub a;
MR::Client b;
RedisClient c( &a );
a.start();

View File

@ -1,72 +0,0 @@
# Название основной цели и имя библиотеки в текущем каталоге
set(TRGT redis)
# cmake-format: off
# Список файлов исходных текстов
set(TRGT_cpp
${CMAKE_CURRENT_SOURCE_DIR}/client.cpp
${CMAKE_CURRENT_SOURCE_DIR}/lexer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/parser.cpp
${CMAKE_CURRENT_SOURCE_DIR}/request.cpp)
# Список заголовочных файлов (используется для установки)
set(TRGT_moc_hpp
${CMAKE_CURRENT_SOURCE_DIR}/client.hpp
${CMAKE_CURRENT_SOURCE_DIR}/lexer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/parser.hpp
${CMAKE_CURRENT_SOURCE_DIR}/request.hpp)
set(TRGT_hpp
${CMAKE_CURRENT_SOURCE_DIR}/reply.hpp
${CMAKE_CURRENT_SOURCE_DIR}/client-inl.hpp
${CMAKE_CURRENT_SOURCE_DIR}/lexer-inl.hpp
${CMAKE_CURRENT_SOURCE_DIR}/parser-inl.hpp
${CMAKE_CURRENT_SOURCE_DIR}/request-inl.hpp)
set(TRGT_headers ${TRGT_moc_hpp} ${TRGT_hpp})
# cmake-format: on
qt5_wrap_cpp(TRGT_moc_cpp ${TRGT_moc_hpp})
add_library(${TRGT}-header-only INTERFACE)
target_include_directories(
${TRGT}-header-only SYSTEM INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
if(MYXLIB_BUILD_LIBRARIES)
add_common_library(${TRGT} OUTPUT_NAME myx-${TRGT} SOURCES ${TRGT_moc_cpp} ${TRGT_cpp} ${TRGT_headers})
common_target_properties(${TRGT})
# Создание цели для проверки утилитой clang-tidy
add_clang_tidy_check(${TRGT} ${TRGT_cpp} ${TRGT_headers})
# Создание цели для проверки утилитой clang-analyze
add_clang_analyze_check(${TRGT} ${TRGT_cpp} ${TRGT_headers})
# Создание цели для проверки утилитой clazy
add_clazy_check(${TRGT} ${TRGT_cpp} ${TRGT_headers})
# Создание цели для проверки утилитой pvs-studio
add_pvs_check(${TRGT})
# Создание цели для автоматического форматирования кода
add_format_sources(${TRGT} ${TRGT_cpp} ${TRGT_headers})
target_compile_definitions(${TRGT} PUBLIC MYXLIB_BUILD_LIBRARIES)
target_include_directories(${TRGT} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
target_include_directories(${TRGT} SYSTEM PUBLIC ${Qt5Network_INCLUDE_DIRS})
target_include_directories(${TRGT} SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src)
cotire(${TRGT})
install(TARGETS ${TRGT}_static COMPONENT libs-dev ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(BUILD_SHARED_LIBS)
install(TARGETS ${TRGT}_shared COMPONENT main LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
endif()
generate_pkgconfig(myx-${TRGT} COMPONENT base-dev INSTALL_LIBRARY ${MYXLIB_BUILD_LIBRARIES})
install(FILES ${TRGT_headers} COMPONENT base-dev DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/${TRGT})
# Цель, используемая только для установки заголовочных файлов без компиляции проекта
add_custom_target(${TRGT}-install-headers COMMAND ${CMAKE_COMMAND} -DCMAKE_INSTALL_COMPONENT=base-dev -P
"${CMAKE_BINARY_DIR}/cmake_install.cmake")

View File

@ -1,74 +0,0 @@
#ifndef MYX_REDIS_CLIENT_INL_HPP_
#define MYX_REDIS_CLIENT_INL_HPP_
#pragma once
#ifndef MYXLIB_HEADER_ONLY
#include <myx/redis/client.hpp>
#endif
namespace myx {
namespace redis {
MYXLIB_INLINE Client::Client( QObject* parent ) :
QObject( parent ),
d ( new ClientPrivate( this ) )
{
}
MYXLIB_INLINE void Client::connectToHost( const QString& hostName, quint16 port )
{
d->m_socket.connectToHost( hostName, port );
}
MYXLIB_INLINE void Client::disconnectFromHost()
{
d->m_socket.disconnectFromHost();
}
MYXLIB_INLINE bool Client::isConnected() const
{
return( d->m_socket.state() == QAbstractSocket::ConnectedState );
}
MYXLIB_INLINE Request* Client::sendCommand( const QByteArray& command )
{
d->m_socket.write( command + "\r\n" );
auto* request = new Request( this );
d->m_queue.enqueue( request );
return( request );
}
MYXLIB_INLINE Request* Client::subscribeToChannel( const QByteArray& channel )
{
d->m_socket.write( "subscribe " + channel + "\r\n" );
auto* request = new Request( channel );
d->m_list.append( request );
return( request );
}
MYXLIB_INLINE bool Client::waitForConnected( int msecs )
{
return( d->m_socket.waitForConnected( msecs ) );
}
MYXLIB_INLINE bool Client::waitForDisconnected( int msecs )
{
return( d->m_socket.waitForDisconnected( msecs ) );
}
} // namespace redis
} // namespace myx
#endif // ifndef MYX_REDIS_CLIENT_INL_HPP_

View File

@ -1,180 +0,0 @@
#ifndef MYX_REDIS_CLIENT_HPP_
#define MYX_REDIS_CLIENT_HPP_
#pragma once
#include <myx/redis/lexer.hpp>
#include <myx/redis/parser.hpp>
#include <myx/redis/request.hpp>
#include <QQueue>
#include <QScopedPointer>
#include <QTcpSocket>
namespace myx {
namespace redis {
QT_FORWARD_DECLARE_CLASS( ClientPrivate )
/**
* @brief Provides access to a Redis server
*/
class Client : public QObject
{
Q_OBJECT
public:
/**
* @brief Creates a Redis client
* @param parent the parent QObject
*/
explicit Client( QObject* parent = nullptr );
Client( const Client& ) = delete;
Client& operator=( const Client& ) = delete;
Client( Client&& ) = delete;
Client& operator=( Client&& ) = delete;
/**
* @brief Destroys the client
*/
~Client() override = default;
/*
* Note: we specifically avoid an overload of connectToHost that
* uses QHostAddress since that would force anyone using the client
* library to use the QtNetwork module, which we wish to avoid.
*/
/**
* @brief Attempts to connect to the specified Redis server
* @param hostName the hostname of the Redis server
* @param port the port that the Redis server is listening on
*
* If the connection was successful, the connected() signal will be
* emitted.
*/
void connectToHost( const QString& hostName, quint16 port = 6379 );
/**
* @brief Disconnects from the Redis server
*/
void disconnectFromHost();
/**
* @brief Indicates whether the client is connected to a Redis server
* @return true if the client is connected
*/
bool isConnected() const;
/**
* @brief Sends the specified command to the Redis server
* @param command the command to execute
* @return an object representing the request
*/
Request* sendCommand( const QByteArray& command );
Request* subscribeToChannel( const QByteArray& channel );
/**
* @brief Attempts to set the specified key to the specified value
* @param name the name of the key
* @param value the value of the key
* @return the request issued
*/
Request* set( const QByteArray& name, const QByteArray& value );
/**
* @brief Waits for the socket to finish connecting
* @param msecs the amount of time in milliseconds to wait
* @return true if the connection was completed
*
* Note: to wait indefinitely, pass a value of -1.
*/
bool waitForConnected( int msecs = 30000 );
/**
* @brief Waits for the socket to finish disconnecting
* @param msecs the amount of time in milliseconds to wait
* @return true if the disconnection was completed
*/
bool waitForDisconnected( int msecs = 30000 );
/**
* @brief Emitted when the client establishes a connection with the Redis server
*/
Q_SIGNAL void connected();
/**
* @brief Emitted when the client disconnects from the Redis server
*/
Q_SIGNAL void disconnected();
private:
const QScopedPointer< ClientPrivate > d;
}; // class Client
class ClientPrivate : public QObject
{
Q_OBJECT
friend class Client;
explicit ClientPrivate( Client* client = nullptr ) :
m_lexer ( &m_socket ),
m_parser( &m_lexer )
{
connect( &m_socket, &QTcpSocket::connected, client, &Client::connected );
connect( &m_socket, &QTcpSocket::disconnected, client, &Client::disconnected );
connect( &m_parser, &Parser::reply, this, &ClientPrivate::sendReply );
connect( &m_parser, &Parser::replyMultiBulk, this, &ClientPrivate::sendReplyMultiBulk );
}
QTcpSocket m_socket;
QQueue< Request* > m_queue;
QList< Request* > m_list;
Lexer m_lexer;
Parser m_parser;
Q_SLOT void sendReply( const myx::redis::Reply& reply )
{
if ( m_queue.isEmpty() ) { return; }
Q_EMIT m_queue.dequeue()->reply( const_cast< myx::redis::Reply& >( reply ) );
}
Q_SLOT void sendReplyMultiBulk( const myx::redis::Reply& reply )
{
if ( m_list.isEmpty() ) { return; }
QVariant v = const_cast< myx::redis::Reply& >( reply ).value();
if ( !v.isValid() ) { return; }
if ( !v.canConvert< QVariantList >() ) { return; }
auto l = v.toList();
if ( l.size() != 3 ) { return; }
auto name = l[1].value< Reply >().value().toByteArray();
for ( auto& request: m_list )
{
if ( request->channel() == name )
{
Q_EMIT request->reply( const_cast< myx::redis::Reply& >( reply ) );
}
}
}
}; // class ClientPrivate
} // namespace redis
} // namespace myx
#ifdef MYXLIB_HEADER_ONLY
#include "client-inl.hpp"
#endif
#endif // ifndef MYX_REDIS_CLIENT_HPP_

View File

@ -1,130 +0,0 @@
#ifndef MYX_REDIS_LEXER_INL_HPP_
#define MYX_REDIS_LEXER_INL_HPP_
#pragma once
#ifndef MYXLIB_HEADER_ONLY
#include <myx/redis/lexer.hpp>
#endif
namespace myx {
namespace redis {
MYXLIB_INLINE Lexer::Lexer( QIODevice* device, QObject* parent ) :
QObject ( parent ),
m_device( device ),
m_state ( kDoingNothing ),
m_crlf ( 0 ),
m_length( 0 )
{
connect( device, &QIODevice::readyRead, this, &Lexer::readData );
}
MYXLIB_INLINE void Lexer::readData()
{
m_buffer.append( m_device->readAll() );
while ( true )
{
if ( ( m_state == kDoingNothing ) && !readCharacter() )
{
break;
}
switch ( m_state )
{
case kReadingLength:
case kReadingUnsafeString:
if ( !readUnsafeString() ) { return; }
break;
case kReadingSafeString:
if ( !readSafeString() ) { return; }
break;
case kDoingNothing:
break;
}
if ( m_state != kReadingSafeString )
{
m_state = kDoingNothing;
}
}
} // Lexer::readData
MYXLIB_INLINE bool Lexer::readCharacter()
{
if ( m_buffer.isEmpty() )
{
return( false );
}
char c = m_buffer.at( 0 );
m_buffer.remove( 0, 1 );
switch ( c )
{
case '+':
case '-':
case ':':
case '*':
m_state = kReadingUnsafeString; break;
case '$':
m_state = kReadingLength; break;
}
Q_EMIT character( c );
return( true );
} // Lexer::readCharacter
MYXLIB_INLINE bool Lexer::readUnsafeString()
{
m_crlf = m_buffer.indexOf( "\r\n", m_crlf );
if ( m_crlf == -1 )
{
m_crlf = m_buffer.size();
return( false );
}
QString s = m_buffer.mid( 0, m_crlf );
m_buffer.remove( 0, m_crlf + 2 );
if ( m_state == kReadingLength )
{
m_length = s.toInt();
m_state = kReadingSafeString;
}
else
{
Q_EMIT unsafeString( s );
}
m_crlf = 0;
return( true );
} // Lexer::readUnsafeString
MYXLIB_INLINE bool Lexer::readSafeString()
{
if ( m_buffer.size() - m_length < 2 )
{
return( false );
}
QByteArray d = m_buffer.mid( 0, m_length );
m_buffer.remove( 0, m_length + 2 );
Q_EMIT safeString( d );
m_state = kDoingNothing;
return( true );
}
} // namespace redis
} // namespace myx
#endif // ifndef MYX_REDIS_LEXER_INL_HPP_

View File

@ -1,5 +0,0 @@
#ifndef MYXLIB_BUILD_LIBRARIES
#error Define MYXLIB_BUILD_LIBRARIES to compile this file.
#endif
#include <myx/redis/lexer-inl.hpp>

View File

@ -1,66 +0,0 @@
#ifndef MYX_REDIS_LEXER_HPP_
#define MYX_REDIS_LEXER_HPP_
#pragma once
#include <myx/base/config.hpp>
#include <QIODevice>
namespace myx {
namespace redis {
class Lexer : public QObject
{
Q_OBJECT
public:
explicit Lexer( QIODevice*, QObject* = nullptr );
Lexer( const Lexer& ) = delete;
Lexer& operator=( const Lexer& ) = delete;
Lexer( Lexer&& ) = delete;
Lexer& operator=( Lexer&& ) = delete;
~Lexer() override = default;
Q_SIGNAL void character( char );
Q_SIGNAL void unsafeString( const QString& );
Q_SIGNAL void safeString( const QByteArray& );
private:
Q_SLOT void readData();
bool readCharacter();
bool readLength();
bool readUnsafeString();
bool readSafeString();
QIODevice* m_device;
QByteArray m_buffer;
enum
{
kDoingNothing,
kReadingLength,
kReadingUnsafeString,
kReadingSafeString
} m_state;
int m_crlf;
int m_length;
}; // class Lexer
} // namespace redis
} // namespace myx
#ifdef MYXLIB_HEADER_ONLY
#include "lexer-inl.hpp"
#endif
#endif // ifndef MYX_REDIS_LEXER_HPP_

View File

@ -1,106 +0,0 @@
#ifndef MYX_REDIS_PARSER_INL_HPP_
#define MYX_REDIS_PARSER_INL_HPP_
#pragma once
#ifndef MYXLIB_HEADER_ONLY
#include <myx/redis/parser.hpp>
#endif
#include <QDebug>
namespace myx {
namespace redis {
MYXLIB_INLINE Parser::Parser( Lexer* lexer, QObject* parent ) :
QObject( parent ),
m_lexer( lexer )
{
connect( m_lexer, &Lexer::character, this, &Parser::readCharacter );
connect( m_lexer, &Lexer::unsafeString, this, &Parser::readUnsafeString );
connect( m_lexer, &Lexer::safeString, this, &Parser::readSafeString );
}
MYXLIB_INLINE void Parser::readCharacter( const char c )
{
switch ( c )
{
case '+':
m_stack.append( Task( Reply::kStatus ) ); break;
case '-':
m_stack.append( Task( Reply::kError ) ); break;
case ':':
m_stack.append( Task( Reply::kInteger ) ); break;
case '$':
m_stack.append( Task( Reply::kBulk ) ); break;
case '*':
m_stack.append( Task( Reply::kMultiBulk ) ); break;
default:
break;
}
}
MYXLIB_INLINE void Parser::readUnsafeString( const QString& value )
{
if ( tos().m_reply.type() == Reply::kMultiBulk )
{
tos().m_count = value.toInt();
}
else
{
tos().m_reply.value() = value;
}
descend();
}
MYXLIB_INLINE void Parser::readSafeString( const QByteArray& value )
{
tos().m_reply.value() = value;
descend();
}
MYXLIB_INLINE void Parser::descend()
{
while ( true )
{
auto& task = tos();
if ( ( task.m_reply.type() == Reply::kMultiBulk ) &&
( task.m_reply.value().toList().count() < task.m_count ) )
{
return;
}
if ( m_stack.count() == 1 )
{
if ( task.m_reply.type() == Reply::kMultiBulk )
{
Q_EMIT replyMultiBulk( m_stack.takeLast().m_reply );
}
else
{
Q_EMIT reply( m_stack.takeLast().m_reply );
}
return;
}
auto r = m_stack.takeLast().m_reply;
Task t = m_stack.takeLast();
auto l = t.m_reply.value().toList();
l.append( QVariant::fromValue( r ) );
t.m_reply.value().setValue( l );
m_stack.append( t );
}
} // Parser::descend
} // namespace redis
} // namespace myx
#endif // ifndef MYX_REDIS_PARSER_INL_HPP_

View File

@ -1,5 +0,0 @@
#ifndef MYXLIB_BUILD_LIBRARIES
#error Define MYXLIB_BUILD_LIBRARIES to compile this file.
#endif
#include <myx/redis/parser-inl.hpp>

View File

@ -1,70 +0,0 @@
#ifndef MYX_REDIS_PARSER_HPP_
#define MYX_REDIS_PARSER_HPP_
#pragma once
#include <myx/base/config.hpp>
#include <myx/redis/lexer.hpp>
#include <myx/redis/reply.hpp>
#include <QList>
#include <QPair>
#include <QVariant>
namespace myx {
namespace redis {
class Parser : public QObject
{
Q_OBJECT
public:
explicit Parser( Lexer*, QObject* = nullptr );
Parser( const Parser& ) = delete;
Parser& operator=( const Parser& ) = delete;
Parser( Parser&& ) = delete;
Parser& operator=( Parser&& ) = delete;
~Parser() override = default;
Q_SIGNAL void reply( const myx::redis::Reply& );
Q_SIGNAL void replyMultiBulk( const myx::redis::Reply& );
private:
Q_SLOT void readCharacter( char );
Q_SLOT void readUnsafeString( const QString& );
Q_SLOT void readSafeString( const QByteArray& );
void descend();
class Task
{
friend class Parser;
enum { kUnknown = -2 };
explicit Task( Reply::Type type ) :
m_reply( type ),
m_count( kUnknown ) {}
Reply m_reply;
int m_count;
};
Lexer* m_lexer;
QList< Task > m_stack;
Task& tos() { return( m_stack.last() ); }
}; // class Parser
} // namespace redis
} // namespace myx
#ifdef MYXLIB_HEADER_ONLY
#include "parser-inl.hpp"
#endif
#endif // ifndef MYX_REDIS_PARSER_HPP_

View File

@ -1,111 +0,0 @@
#ifndef MYX_REDIS_REPLY_HPP_
#define MYX_REDIS_REPLY_HPP_
#pragma once
#include <myx/base/config.hpp>
#include <QVariant>
namespace myx {
namespace redis {
/**
* @brief Represents a Redis reply
*/
class Reply
{
public:
/**
* @brief Reply types
*/
enum Type
{
/**
* @brief An invalid reply
*
* This value is only set when the default constructor is used.
*/
kInvalid,
/**
* @brief A status reply
*
* The value property will contain the status message returned
* by the server as a QString.
*/
kStatus,
/**
* @brief An error reply
*
* The value property will contain the error message returned by
* the server as a QString.
*/
kError,
/**
* @brief An integer reply
*
* The value property will contain the integer value returned by
* the server as a qlonglong.
*/
kInteger,
/**
* @brief A bulk reply
*
* The value property will contain the bulk reply returned by
* the server as a QByteArray.
*/
kBulk,
/**
* @brief A multi-bulk reply
*
* The value property will contain the multi-bulk reply returned
* by the server as a QVariantList. Each entry in the list is of
* type Reply.
*/
kMultiBulk
};
/**
* @brief Creates an empty reply
*/
Reply() = default;
/**
* @brief Initializes the reply
* @param type the type of the reply
*/
explicit Reply( Type type ) :
m_type( type ) {}
/**
* @brief Returns the type of the reply
* @return the reply type
*/
Type type() const { return( m_type ); }
/**
* @brief Returns a reference to the value of the reply
* @return the reply value
*/
QVariant& value() { return( m_value ); }
private:
Type m_type { kInvalid };
QVariant m_value;
}; // class Reply
} // namespace redis
} // namespace myx
Q_DECLARE_METATYPE( myx::redis::Reply ) // NOLINT
#endif // ifndef MYX_REDIS_REPLY_HPP_

View File

@ -1,54 +0,0 @@
#ifndef MYX_REDIS_REQUEST_INL_HPP_
#define MYX_REDIS_REQUEST_INL_HPP_
#pragma once
#include <myx/base/config.hpp>
#ifndef MYXLIB_HEADER_ONLY
#include <myx/redis/request.hpp>
#endif
#include <QTimer>
namespace myx {
namespace redis {
MYXLIB_INLINE Request::Request( QObject* parent ) :
QObject( parent ),
d ( new Loop )
{
connect( this, &Request::reply, this, &Request::deleteLater );
}
MYXLIB_INLINE Request::Request( const QByteArray& channel, QObject* parent ) :
QObject( parent ),
d ( new Loop )
{
m_channel = channel;
}
MYXLIB_INLINE bool Request::waitForReply( int msecs )
{
QTimer timer;
timer.setInterval( msecs );
timer.setSingleShot( true );
connect( &timer, &QTimer::timeout, &d->m_loop, &QEventLoop::quit );
connect( this, &Request::reply, d.data(), &Loop::quitEventLoop );
/*
* If the timer fires, the return value will be 0.
* Otherwise, quitEventLoop() will terminate the loop with 1.
*/
return( ( d->m_loop.exec( QEventLoop::ExcludeUserInputEvents ) != 0 ) );
}
} // namespace redis
} // namespace myx
#endif // ifndef MYX_REDIS_REQUEST_INL_HPP_

View File

@ -1,5 +0,0 @@
#ifndef MYXLIB_BUILD_LIBRARIES
#error Define MYXLIB_BUILD_LIBRARIES to compile this file.
#endif
#include <myx/redis/request-inl.hpp>

View File

@ -1,83 +0,0 @@
#ifndef MYX_REDIS_REQUEST_HPP_
#define MYX_REDIS_REQUEST_HPP_
#pragma once
#include <myx/base/config.hpp>
#include <myx/redis/reply.hpp>
#include <QEventLoop>
#include <QScopedPointer>
namespace myx {
namespace redis {
class Loop : public QObject
{
Q_OBJECT
friend class Request;
explicit Loop( QObject* parent = nullptr ) { Q_UNUSED( parent ) }
QEventLoop m_loop;
Q_SLOT void quitEventLoop() { m_loop.exit( 1 ); }
};
/**
* @brief Represents a Redis command and its response
*/
class Request : public QObject
{
Q_OBJECT
public:
/**
* @brief Initializes the request
* @param parent the parent QObject
*/
explicit Request( QObject* parent = nullptr );
explicit Request( const QByteArray& channel, QObject* parent = nullptr );
Request( const Request& ) = delete;
Request& operator=( const Request& ) = delete;
Request( Request&& ) = delete;
Request& operator=( Request&& ) = delete;
/**
* @brief Destroys the request
*/
~Request() override = default;
/**
* @brief Waits for the reply to be received
* @param msecs the amount of time in milliseconds to wait
* @return true if the reply was received
*/
bool waitForReply( int msecs = 10000 );
/**
* @brief Emitted when a reply is received
* @param reply the reply received
*/
Q_SIGNAL void reply( myx::redis::Reply& );
QByteArray channel() const { return( m_channel ); }
private:
QByteArray m_channel;
const QScopedPointer< Loop > d;
}; // class Request
} // namespace redis
} // namespace myx
#ifdef MYXLIB_HEADER_ONLY
#include "request-inl.hpp"
#endif
#endif // ifndef MYX_REDIS_REQUEST_HPP_

View File

@ -5,16 +5,19 @@ set(TRGT redis)
# Список файлов исходных текстов
set(TRGT_cpp
${CMAKE_CURRENT_SOURCE_DIR}/base.cpp
${CMAKE_CURRENT_SOURCE_DIR}/client.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pubsub.cpp
${CMAKE_CURRENT_SOURCE_DIR}/containers.cpp)
# Список заголовочных файлов
set(TRGT_moc_hpp
${CMAKE_CURRENT_SOURCE_DIR}/base.hpp
${CMAKE_CURRENT_SOURCE_DIR}/client.hpp
${CMAKE_CURRENT_SOURCE_DIR}/pubsub.hpp)
set(TRGT_hpp
${CMAKE_CURRENT_SOURCE_DIR}/base-inl.hpp
${CMAKE_CURRENT_SOURCE_DIR}/client-inl.hpp
${CMAKE_CURRENT_SOURCE_DIR}/containers.hpp
${CMAKE_CURRENT_SOURCE_DIR}/containers-inl.hpp
${CMAKE_CURRENT_SOURCE_DIR}/pubsub-inl.hpp)

View File

@ -118,7 +118,7 @@ MYXLIB_INLINE void Base::onReadyRead()
/*!
* \brief Добавляет к состоянию флаг Read
* \brief Добавляет к состоянию флаг Write
*/
MYXLIB_INLINE void Base::onBytesWritten()
{

2280
src/myx/redis/client-inl.hpp Normal file

File diff suppressed because it is too large Load Diff

157
src/myx/redis/client.hpp Normal file
View File

@ -0,0 +1,157 @@
#ifndef MYX_REDIS_CLIENT_HPP_
#define MYX_REDIS_CLIENT_HPP_
#pragma once
#include <myx/redis/base.hpp>
#include <QVariant>
#include <QJsonArray>
#include <QJsonObject>
#include <unordered_set>
// QT_FORWARD_DECLARE_CLASS( QTcpSocket )
namespace myx {
namespace redis {
/*!
* \brief Реализует клиентскую часть в соответствии с RESP
*/
class Client : public Base
{
Q_OBJECT
public:
Client( int database = 0, QObject* parent = nullptr );
virtual ~Client();
int database() const;
int exists( const QStringList& keys );
void select( int database );
bool flushdb();
bool del( const QStringList& keys );
bool expire( const QString& key, int secs );
bool pexpire( const QString& key, int msecs );
bool persist( const QString& key );
bool publish( const QString& channel, const QByteArray& message );
static bool publish( const QString& channel, const QByteArray& message,
const QString& host, quint16 port = 6379 );
bool set( const QString& key, const QVariant& value );
static bool set( const QString& key, const QVariant& value,
int database, const QString& host = "127.0.0.1", quint16 port = 6379 );
QByteArray scriptLoad( const QByteArray& script );
QByteArray get( const QString& key );
template< typename T >
T get( const QString& key );
static QByteArray get( const QString& key, int database,
const QString& host = "127.0.0.1", quint16 port = 6379 );
template< typename T >
static T get( const QString& key, int database,
const QString& host = "127.0.0.1", quint16 port = 6379 );
bool hdel( const QString& redisKey, const QStringList& hashKeys );
bool hset( const QString& key, const QString& field, const QVariant& value );
bool hmset( const QString& key, const QVariantHash& hash );
static bool hmset( const QString& key, const QVariantHash& hash, int database,
const QString& host = "127.0.0.1", quint16 port = 6379 );
QVariantHash hgetall( const QString& key );
static QVariantHash hgetall( const QString& key, int database,
const QString& host = "127.0.0.1", quint16 port = 6379 );
QByteArray hget( const QString& redisKey, const QString& hashKey );
static QByteArray hget( const QString& redisKey, const QString& hashKey,
int database, const QString& host = "127.0.0.1", quint16 port = 6379 );
bool rpush( const QString& key, const QVariantList& values );
bool lpush( const QString& key, const QVariantList& values );
bool lset( const QString& key, int index, const QVariant& value );
bool lrem( const QString& key, const QVariant& value, int count = 1 );
bool ltrim( const QString& key, int from, int to );
int llen( const QString& key );
QVariantList lrange( const QString& key, int from, int to );
template< typename T >
std::vector< T > lrange( const QString& key );
static QByteArrayList lrange( const QString& key, int from, int to, int database,
const QString& host = "127.0.0.1", quint16 port = 6379 );
template< typename T >
static std::vector< T > lrange( const QString& key, int from, int to, int database,
const QString& host = "127.0.0.1", quint16 port = 6379 );
bool sadd( const QString& key, const QVariantList& values );
bool srem( const QString& key, const QVariantList& values );
bool sismember( const QString& key, const QVariant& value );
static bool sismember( const QString& key, const QVariant& value, int database,
const QString& host = "127.0.0.1", quint16 port = 6379 );
int scard( const QString& key );
QVariantList smembers( const QString& key );
template< typename T >
std::vector< T > smembers( const QString& key );
QByteArray rpop( const QString& key );
QByteArray lindex( const QString& key, int index );
QStringList keys( const QString& pattern = {} );
static QStringList keys( const QString& pattern, int database,
const QString& host, quint16 port = 6379 );
template< typename T >
T evalSha( const QByteArray& sha, const QVariantList& keys, const QVariantList& argv );
template< typename T >
static T evalSha( const QByteArray& sha, const QVariantList& keys, const QVariantList& argv,
int database, const QString& host, quint16 port = 6379 );
QByteArrayList evalSha( const QByteArray& sha, const QVariantList& keys,
const QVariantList& argv );
static QByteArrayList evalSha( const QByteArray& sha, const QVariantList& keys,
const QVariantList& argv,
int database, const QString& host, quint16 port = 6379 );
private:
/*!
* \brief Класс для синхронизации порядка чтения/записи в poolSocket;
*/
struct Session
{
Session( uint& count );
~Session();
private:
uint& _count; //!< Счетчик открытых сессий
};
int _database; //!< Номер БД в redis
uint _sessionCount { 0 }; //!< Счетчик открытых сессий
std::vector< QTcpSocket* > _session; //!< Сокеты для синхронных сессий чтения/записи
/*!
* \brief Содержит TCP соединения, связанные с БД, отличной от текущей
*/
std::unordered_set< QTcpSocket* > _outOfDatabase;
static bool setupSocket( QTcpSocket* socket, int database, const QString& host, quint16 port );
static bool select( QTcpSocket* socket, int database );
QTcpSocket* session( int timeout );
}; // class Client
template<>
QVariant Client::evalSha< QVariant >( const QByteArray& sha, const QVariantList& keys,
const QVariantList& argv );
template<>
QVariantList Client::evalSha< QVariantList >( const QByteArray& sha, const QVariantList& keys,
const QVariantList& argv );
template<>
QVariantHash Client::evalSha< QVariantHash >( const QByteArray& sha, const QVariantList& keys,
const QVariantList& argv );
template<>
QJsonArray Client::evalSha< QJsonArray >( const QByteArray& sha, const QVariantList& keys,
const QVariantList& argv );
template<>
QJsonArray Client::evalSha< QJsonArray >( const QByteArray& sha, const QVariantList& keys,
const QVariantList& argv,
int database, const QString& host, quint16 port );
template<>
std::vector< QJsonObject > Client::lrange< QJsonObject >( const QString & key, int from, int to,
int database, const QString & host, quint16 );
} // namespace redis
} // namespace myx
#ifdef MYXLIB_HEADER_ONLY
#include "client-inl.hpp"
#endif
// #include "client_redis.tcc"
#endif // ifndef MYX_REDIS_CLIENT_HPP_

View File

@ -30,14 +30,15 @@ MYXLIB_INLINE PubSub::PubSub( QObject* parent ) :
{
connect( m_socket, &QAbstractSocket::stateChanged, this, &PubSub::onSocketStateChanged );
connect( m_socket, &QIODevice::readyRead, this, &PubSub::read );
connect( m_socket, &QIODevice::readyRead, this, &PubSub::onReadyRead );
connect( m_socket, &QIODevice::bytesWritten, this, &PubSub::onBytesWritten );
connect( m_socket, static_cast< void ( QAbstractSocket::* )( QAbstractSocket::SocketError ) >
( &QAbstractSocket::error ), this, [this]() {
qDebug() << QDateTime::currentDateTimeUtc().toString( QStringLiteral( "hh:mm:ss:zzz" ) )
<< QByteArray( "redis pubsub connection error" ) << m_socket->errorString();
} );
connect( m_socket, &QIODevice::readyRead, this, &PubSub::read );
m_connectionTimer->setInterval( connectionTimeout() );
m_connectionTimer->setSingleShot( true );
@ -81,7 +82,7 @@ MYXLIB_INLINE void PubSub::stop()
/*!
* \brief Создаёт подписку на канал сообщений
* \brief Создает подписку на канал сообщений
* \param channel Канал подписки
* \param subscriber Объект-подписчик
* \param method Имя метода, вызываемого при изменении получении серверного сообщения
@ -120,7 +121,7 @@ MYXLIB_INLINE void PubSub::subscribe( const QString& channel, QObject* subscribe
/*!
* \brief Создаёт подписку на канал изменения данных
* \brief Создает подписку на канал изменения данных
* \param channel Канал подписки
* \param subscriber Объект-подписчик
* \param method Имя метода, вызываемого при изменении получении серверного сообщения
@ -161,7 +162,7 @@ MYXLIB_INLINE void PubSub::subscribe( QString channel, QObject* subscriber, cons
/*!
* \brief Создаёт подписку на шаблон канала сообщений
* \brief Создает подписку на шаблон канала сообщений
* \param channel Шаблон канала подписки
* \param subscriber Объект-подписчик
* \param method Имя метода, вызываемого при изменении получении серверного сообщения
@ -200,7 +201,7 @@ MYXLIB_INLINE void PubSub::psubscribe( const QString& channel, QObject* subscrib
/*!
* \brief Создаёт подписку на шаблон канала изменения данных
* \brief Создает подписку на шаблон канала изменения данных
* \param channel Шаблон канала подписки
* \param subscriber Объект-подписчик
* \param method Имя метода, вызываемого при изменении получении серверного сообщения
@ -503,7 +504,7 @@ MYXLIB_INLINE void PubSub::onSocketStateChanged()
/*!
* \brief При удалении подписчика уничтожает все его подписки
* \param subscriber Удалённый подписчик
* \param subscriber Удаленный подписчик
*/
MYXLIB_INLINE void PubSub::onSubscriberDestroyed( QObject* subscriber )
{