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

This commit is contained in:
Andrei Astafev 2020-04-23 12:49:49 +03:00
parent f308e935fb
commit b242a96259
8 changed files with 58 additions and 39 deletions

View File

@ -18,6 +18,9 @@ set(TRGT_moc_hpp
${CMAKE_CURRENT_SOURCE_DIR}/request.hpp) ${CMAKE_CURRENT_SOURCE_DIR}/request.hpp)
set(TRGT_hpp set(TRGT_hpp
${CMAKE_CURRENT_SOURCE_DIR}/client_p.hpp
${CMAKE_CURRENT_SOURCE_DIR}/request_p.hpp
${CMAKE_CURRENT_SOURCE_DIR}/parser_p.hpp
${CMAKE_CURRENT_SOURCE_DIR}/client-inl.hpp ${CMAKE_CURRENT_SOURCE_DIR}/client-inl.hpp
${CMAKE_CURRENT_SOURCE_DIR}/lexer-inl.hpp ${CMAKE_CURRENT_SOURCE_DIR}/lexer-inl.hpp
${CMAKE_CURRENT_SOURCE_DIR}/parser-inl.hpp ${CMAKE_CURRENT_SOURCE_DIR}/parser-inl.hpp

View File

@ -29,10 +29,11 @@ public:
Parser parser; Parser parser;
private: private:
Q_SLOT void sendReply(const myx::redis::Reply & ); Q_SLOT void sendReply( const myx::redis::Reply& );
}; // class ClientPrivate }; // class ClientPrivate
} } // namespace redis
}
} // namespace myx
#endif // MYX_REDIS_CLIENT_P_HPP_ #endif // MYX_REDIS_CLIENT_P_HPP_

View File

@ -28,8 +28,6 @@ private:
Q_SLOT void readData(); Q_SLOT void readData();
private:
bool readCharacter(); bool readCharacter();
bool readLength(); bool readLength();
bool readUnsafeString(); bool readUnsafeString();

View File

@ -25,15 +25,15 @@ void Parser::readCharacter( const char c )
switch ( c ) switch ( c )
{ {
case '+': case '+':
stack.append( Task( Reply::Status ) ); break; m_stack.append( ParserTaskPrivate( Reply::Status ) ); break;
case '-': case '-':
stack.append( Task( Reply::Error ) ); break; m_stack.append( ParserTaskPrivate( Reply::Error ) ); break;
case ':': case ':':
stack.append( Task( Reply::Integer ) ); break; m_stack.append( ParserTaskPrivate( Reply::Integer ) ); break;
case '$': case '$':
stack.append( Task( Reply::Bulk ) ); break; m_stack.append( ParserTaskPrivate( Reply::Bulk ) ); break;
case '*': case '*':
stack.append( Task( Reply::MultiBulk ) ); break; m_stack.append( ParserTaskPrivate( Reply::MultiBulk ) ); break;
default: default:
break; break;
} }
@ -72,14 +72,14 @@ void Parser::descend()
return; return;
} }
if ( stack.count() == 1 ) if ( m_stack.count() == 1 )
{ {
auto r = stack.takeLast().reply; auto r = m_stack.takeLast().reply;
Q_EMIT reply( r ); Q_EMIT reply( r );
return; return;
} }
auto r = stack.takeLast().reply; auto r = m_stack.takeLast().reply;
tos().reply.value().toList().append( QVariant::fromValue( r ) ); tos().reply.value().toList().append( QVariant::fromValue( r ) );
} }
} }

View File

@ -4,8 +4,9 @@
#pragma once #pragma once
#include <myx/base/config.hpp> #include <myx/base/config.hpp>
#include <myx/redis/reply.hpp>
#include <myx/redis/lexer.hpp> #include <myx/redis/lexer.hpp>
#include <myx/redis/reply.hpp>
#include <myx/redis/parser_p.hpp>
#include <QList> #include <QList>
#include <QPair> #include <QPair>
@ -33,23 +34,9 @@ private:
private: private:
void descend(); void descend();
class Task QList< ParserTaskPrivate > m_stack;
{
public:
enum { Unknown = -2 }; ParserTaskPrivate& tos() { return( m_stack.last() ); }
Task( Reply::Type type ) :
reply( type ),
count( Unknown ) {}
Reply reply;
int count;
};
QList< Task > stack;
Task& tos() { return( stack.last() ); }
}; // class Parser }; // class Parser
} // namespace redis } // namespace redis

View File

@ -0,0 +1,30 @@
#ifndef MYX_REDIS_PARSER_P_HPP_
#define MYX_REDIS_PARSER_P_HPP_
#pragma once
#include <myx/redis/reply.hpp>
namespace myx {
namespace redis {
class ParserTaskPrivate
{
friend class Parser;
enum { Unknown = -2 };
ParserTaskPrivate( Reply::Type type ) :
reply( type ),
count( Unknown ) {}
Reply reply;
int count;
};
} // namespace redis
} // namespace myx
#endif // MYX_REDIS_PARSER_P_HPP_

View File

@ -76,32 +76,32 @@ public:
* @brief Creates an empty reply * @brief Creates an empty reply
*/ */
Reply() : Reply() :
_type( Invalid ) {} 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 ) : Reply( Type type ) :
_type( type ) {} m_type( type ) {}
/** /**
* @brief Returns the type of the reply * @brief Returns the type of the reply
* @return the reply type * @return the reply type
*/ */
Type type() const { return( _type ); } Type type() const { return( m_type ); }
/** /**
* @brief Returns a reference to the value of the reply * @brief Returns a reference to the value of the reply
* @return the reply value * @return the reply value
*/ */
QVariant& value() { return( _value ); } QVariant& value() { return( m_value ); }
private: private:
Type _type; Type m_type;
QVariant _value; QVariant m_value;
}; // class MYX_REDIS_EXPORT Reply }; // class Reply
} // namespace redis } // namespace redis

View File

@ -19,7 +19,7 @@ public:
Q_SLOT void quitEventLoop(); Q_SLOT void quitEventLoop();
}; };
} } // namespace redis
} } // namespace myx
#endif // MYX_REDIS_REQUEST_P_HPP_ #endif // MYX_REDIS_REQUEST_P_HPP_