Рефакторинг 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)
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}/lexer-inl.hpp
${CMAKE_CURRENT_SOURCE_DIR}/parser-inl.hpp

View File

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

View File

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

View File

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

View File

@ -4,8 +4,9 @@
#pragma once
#include <myx/base/config.hpp>
#include <myx/redis/reply.hpp>
#include <myx/redis/lexer.hpp>
#include <myx/redis/reply.hpp>
#include <myx/redis/parser_p.hpp>
#include <QList>
#include <QPair>
@ -33,23 +34,9 @@ private:
private:
void descend();
class Task
{
public:
QList< ParserTaskPrivate > m_stack;
enum { Unknown = -2 };
Task( Reply::Type type ) :
reply( type ),
count( Unknown ) {}
Reply reply;
int count;
};
QList< Task > stack;
Task& tos() { return( stack.last() ); }
ParserTaskPrivate& tos() { return( m_stack.last() ); }
}; // class Parser
} // 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
*/
Reply() :
_type( Invalid ) {}
m_type( Invalid ) {}
/**
* @brief Initializes the reply
* @param type the type of the reply
*/
Reply( Type type ) :
_type( type ) {}
m_type( type ) {}
/**
* @brief Returns the type of the reply
* @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
* @return the reply value
*/
QVariant& value() { return( _value ); }
QVariant& value() { return( m_value ); }
private:
Type _type;
QVariant _value;
}; // class MYX_REDIS_EXPORT Reply
Type m_type;
QVariant m_value;
}; // class Reply
} // namespace redis

View File

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