Рефакторинг Redis

This commit is contained in:
Andrei Astafev 2020-04-23 13:14:15 +03:00
parent b242a96259
commit 16b6b5b322
7 changed files with 35 additions and 37 deletions

View File

@ -33,7 +33,7 @@ public:
/** /**
* @brief Destroys the client * @brief Destroys the client
*/ */
virtual ~Client() = default; ~Client() override = default;
/* /*
* Note: we specifically avoid an overload of connectToHost that * Note: we specifically avoid an overload of connectToHost that

View File

@ -1,16 +1,16 @@
#ifndef MYX_REDIS_CLIENT_P_HPP_ #ifndef MYX_REDIS_CLIENT_P_HPP_
#define MYX_REDIS_CLIENT_P_HPP_ #define MYX_REDIS_CLIENT_P_HPP_
#include <myx/redis/client.hpp>
#include <myx/redis/lexer.hpp>
#include <myx/redis/parser.hpp>
#include <myx/redis/reply.hpp>
#include <myx/redis/request.hpp>
#include <QObject> #include <QObject>
#include <QQueue> #include <QQueue>
#include <QTcpSocket> #include <QTcpSocket>
#include <client.hpp>
#include <reply.hpp>
#include <request.hpp>
#include <lexer.hpp>
#include <parser.hpp>
namespace myx { namespace myx {
namespace redis { namespace redis {
@ -20,7 +20,7 @@ class ClientPrivate : public QObject
Q_OBJECT Q_OBJECT
public: public:
ClientPrivate( Client* ); explicit ClientPrivate( Client* = nullptr );
QTcpSocket socket; QTcpSocket socket;
QQueue< Request* > queue; QQueue< Request* > queue;

View File

@ -14,7 +14,7 @@ namespace redis {
Lexer::Lexer( QIODevice* device, QObject* parent ) : Lexer::Lexer( QIODevice* device, QObject* parent ) :
QObject ( parent ), QObject ( parent ),
m_device( device ), m_device( device ),
m_state ( DoingNothing ), m_state ( kDoingNothing ),
m_crlf ( 0 ), m_crlf ( 0 ),
m_length( 0 ) m_length( 0 )
{ {
@ -28,27 +28,27 @@ void Lexer::readData()
while ( true ) while ( true )
{ {
if ( ( m_state == DoingNothing ) && !readCharacter() ) if ( ( m_state == kDoingNothing ) && !readCharacter() )
{ {
break; break;
} }
switch ( m_state ) switch ( m_state )
{ {
case ReadingLength: case kReadingLength:
case ReadingUnsafeString: case kReadingUnsafeString:
if ( !readUnsafeString() ) { return; } if ( !readUnsafeString() ) { return; }
break; break;
case ReadingSafeString: case kReadingSafeString:
if ( !readSafeString() ) { return; } if ( !readSafeString() ) { return; }
break; break;
case DoingNothing: case kDoingNothing:
break; break;
} }
if ( m_state != ReadingSafeString ) if ( m_state != kReadingSafeString )
{ {
m_state = DoingNothing; m_state = kDoingNothing;
} }
} }
} // Lexer::readData } // Lexer::readData
@ -70,9 +70,9 @@ bool Lexer::readCharacter()
case '-': case '-':
case ':': case ':':
case '*': case '*':
m_state = ReadingUnsafeString; break; m_state = kReadingUnsafeString; break;
case '$': case '$':
m_state = ReadingLength; break; m_state = kReadingLength; break;
} }
Q_EMIT character( c ); Q_EMIT character( c );
@ -92,10 +92,10 @@ bool Lexer::readUnsafeString()
QString s = m_buffer.mid( 0, m_crlf ); QString s = m_buffer.mid( 0, m_crlf );
m_buffer.remove( 0, m_crlf + 2 ); m_buffer.remove( 0, m_crlf + 2 );
if ( m_state == ReadingLength ) if ( m_state == kReadingLength )
{ {
m_length = s.toInt(); m_length = s.toInt();
m_state = ReadingSafeString; m_state = kReadingSafeString;
} }
else else
{ {
@ -119,7 +119,7 @@ bool Lexer::readSafeString()
Q_EMIT safeString( d ); Q_EMIT safeString( d );
m_state = DoingNothing; m_state = kDoingNothing;
return( true ); return( true );
} }

View File

@ -17,8 +17,8 @@ class Lexer : public QObject
public: public:
Lexer( QIODevice*, QObject* = nullptr ); explicit Lexer( QIODevice*, QObject* = nullptr );
virtual ~Lexer() = default; ~Lexer() override = default;
Q_SIGNAL void character( char ); Q_SIGNAL void character( char );
Q_SIGNAL void unsafeString( const QString& ); Q_SIGNAL void unsafeString( const QString& );
@ -38,10 +38,10 @@ private:
enum enum
{ {
DoingNothing, kDoingNothing,
ReadingLength, kReadingLength,
ReadingUnsafeString, kReadingUnsafeString,
ReadingSafeString kReadingSafeString
} m_state; } m_state;
int m_crlf; int m_crlf;

View File

@ -5,8 +5,8 @@
#include <myx/base/config.hpp> #include <myx/base/config.hpp>
#include <myx/redis/lexer.hpp> #include <myx/redis/lexer.hpp>
#include <myx/redis/reply.hpp>
#include <myx/redis/parser_p.hpp> #include <myx/redis/parser_p.hpp>
#include <myx/redis/reply.hpp>
#include <QList> #include <QList>
#include <QPair> #include <QPair>
@ -21,17 +21,16 @@ class Parser : public QObject
Q_OBJECT Q_OBJECT
public: public:
Parser( Lexer*, QObject* = nullptr ); explicit Parser( Lexer*, QObject* = nullptr );
virtual ~Parser() = default; ~Parser() override = default;
Q_SIGNAL void reply( const myx::redis::Reply& ); Q_SIGNAL void reply( const myx::redis::Reply& );
private: private:
Q_SLOT void readCharacter( const char ); Q_SLOT void readCharacter( char );
Q_SLOT void readUnsafeString( const QString& ); Q_SLOT void readUnsafeString( const QString& );
Q_SLOT void readSafeString( const QByteArray& ); Q_SLOT void readSafeString( const QByteArray& );
private:
void descend(); void descend();
QList< ParserTaskPrivate > m_stack; QList< ParserTaskPrivate > m_stack;

View File

@ -75,14 +75,13 @@ public:
/** /**
* @brief Creates an empty reply * @brief Creates an empty reply
*/ */
Reply() : Reply() = default;
m_type( Invalid ) {}
/** /**
* @brief Initializes the reply * @brief Initializes the reply
* @param type the type of the reply * @param type the type of the reply
*/ */
Reply( Type type ) : explicit Reply( Type type ) :
m_type( type ) {} m_type( type ) {}
/** /**
@ -99,7 +98,7 @@ public:
private: private:
Type m_type; Type m_type { Invalid };
QVariant m_value; QVariant m_value;
}; // class Reply }; // class Reply

View File

@ -33,7 +33,7 @@ public:
/** /**
* @brief Destroys the request * @brief Destroys the request
*/ */
virtual ~Request() = default; ~Request() override = default;
/** /**
* @brief Waits for the reply to be received * @brief Waits for the reply to be received