Rename ElementTraits to RedisTypeTraits / Move read to traits

This commit is contained in:
fbraem 2015-11-12 19:06:35 +01:00
parent b7bdbf89f0
commit cd20602320
6 changed files with 77 additions and 80 deletions

View File

@ -94,7 +94,7 @@ public:
if ( pos >= _elements.value().size() ) throw InvalidArgumentException(); if ( pos >= _elements.value().size() ) throw InvalidArgumentException();
RedisType::Ptr element = _elements.value().at(pos); RedisType::Ptr element = _elements.value().at(pos);
if ( ElementTraits<T>::TypeId == element->type() ) if ( RedisTypeTraits<T>::TypeId == element->type() )
{ {
Type<T>* concrete = dynamic_cast<Type<T>* >(element.get()); Type<T>* concrete = dynamic_cast<Type<T>* >(element.get());
if ( concrete != NULL ) return concrete->value(); if ( concrete != NULL ) return concrete->value();
@ -198,7 +198,7 @@ inline size_t Array::size() const
template<> template<>
struct ElementTraits<Array> struct RedisTypeTraits<Array>
{ {
enum { TypeId = RedisType::REDIS_ARRAY }; enum { TypeId = RedisType::REDIS_ARRAY };
@ -223,28 +223,29 @@ struct ElementTraits<Array>
} }
return result.str(); return result.str();
} }
};
template<> inline static void read(RedisInputStream& input, Array& value)
void Type<Array>::read(RedisInputStream& input)
{
Int64 length = NumberParser::parse64(input.getline());
if ( length != -1 )
{ {
for(int i = 0; i < length; ++i) value.clear();
Int64 length = NumberParser::parse64(input.getline());
if ( length != -1 )
{ {
char marker = input.get(); for(int i = 0; i < length; ++i)
RedisType::Ptr element = Type::createRedisType(marker); {
char marker = input.get();
RedisType::Ptr element = RedisType::createRedisType(marker);
if ( element.isNull() ) if ( element.isNull() )
throw RedisException("Wrong answer received from Redis server"); throw RedisException("Wrong answer received from Redis server");
element->read(input); element->read(input);
_value.add(element); value.add(element);
}
} }
} }
} };
}} }}

View File

@ -143,13 +143,13 @@ public:
/// the reply is not of the given type. /// the reply is not of the given type.
{ {
RedisType::Ptr redisResult = readReply(); RedisType::Ptr redisResult = readReply();
if (redisResult->type() == ElementTraits<Error>::TypeId) if (redisResult->type() == RedisTypeTraits<Error>::TypeId)
{ {
Type<Error>* error = dynamic_cast<Type<Error>*>(redisResult.get()); Type<Error>* error = dynamic_cast<Type<Error>*>(redisResult.get());
throw RedisException(error->value().getMessage()); throw RedisException(error->value().getMessage());
} }
if (redisResult->type() == ElementTraits<T>::TypeId) if (redisResult->type() == RedisTypeTraits<T>::TypeId)
{ {
Type<T>* type = dynamic_cast<Type<T>*>(redisResult.get()); Type<T>* type = dynamic_cast<Type<T>*>(redisResult.get());
if (type != NULL) result = type->value(); if (type != NULL) result = type->value();

View File

@ -51,7 +51,7 @@ inline void Error::setMessage(const std::string& message)
} }
template<> template<>
struct ElementTraits<Error> struct RedisTypeTraits<Error>
{ {
enum { TypeId = RedisType::REDIS_ERROR }; enum { TypeId = RedisType::REDIS_ERROR };
@ -61,15 +61,13 @@ struct ElementTraits<Error>
{ {
return marker + value.getMessage() + LineEnding::NEWLINE_CRLF; return marker + value.getMessage() + LineEnding::NEWLINE_CRLF;
} }
static void read(RedisInputStream& input, Error& value)
{
value.setMessage(input.getline());
}
}; };
template<> inline
void Type<Error>::read(RedisInputStream& input)
{
_value = input.getline();
}
}} // Namespace Poco::Redis }} // Namespace Poco::Redis
#endif // Redis_Error_INCLUDED #endif // Redis_Error_INCLUDED

View File

@ -102,27 +102,34 @@ inline bool RedisType::isSimpleString() const
} }
template<typename T> template<typename T>
struct ElementTraits struct RedisTypeTraits
{ {
}; };
template<> template<>
struct ElementTraits<Poco::Int64> struct RedisTypeTraits<Int64>
{ {
enum { TypeId = RedisType::REDIS_INTEGER }; enum { TypeId = RedisType::REDIS_INTEGER };
static const char marker = ':'; static const char marker = ':';
static std::string toString(const Poco::Int64& value) static std::string toString(const Int64& value)
{ {
return marker + NumberFormatter::format(value) + "\r\n"; return marker + NumberFormatter::format(value) + "\r\n";
} }
static void read(RedisInputStream& input, Int64& value)
{
std::string number = input.getline();
value = NumberParser::parse64(number);
}
}; };
template<> template<>
struct ElementTraits<std::string> struct RedisTypeTraits<std::string>
{ {
enum { TypeId = RedisType::REDIS_SIMPLE_STRING }; enum { TypeId = RedisType::REDIS_SIMPLE_STRING };
@ -132,6 +139,11 @@ struct ElementTraits<std::string>
{ {
return marker + value + LineEnding::NEWLINE_CRLF; return marker + value + LineEnding::NEWLINE_CRLF;
} }
static void read(RedisInputStream& input, std::string& value)
{
value = input.getline();
}
}; };
@ -141,7 +153,7 @@ typedef Nullable<std::string> BulkString;
template<> template<>
struct ElementTraits<BulkString> struct RedisTypeTraits<BulkString>
{ {
enum { TypeId = RedisType::REDIS_BULK_STRING }; enum { TypeId = RedisType::REDIS_BULK_STRING };
@ -163,6 +175,24 @@ struct ElementTraits<BulkString>
+ LineEnding::NEWLINE_CRLF; + LineEnding::NEWLINE_CRLF;
} }
} }
static void read(RedisInputStream& input, BulkString& value)
{
value.clear();
std::string line = input.getline();
int length = NumberParser::parse64(line);
if ( length >= 0 )
{
std::string s;
s.resize(length, ' ');
input.read(&*s.begin(), length);
value.assign(s);
input.getline(); // Read and ignore /r/n
}
}
}; };
@ -189,14 +219,17 @@ public:
int type() const int type() const
{ {
return ElementTraits<T>::TypeId; return RedisTypeTraits<T>::TypeId;
} }
virtual void read(RedisInputStream& socket); void read(RedisInputStream& socket)
virtual std::string toString() const
{ {
return ElementTraits<T>::toString(_value); RedisTypeTraits<T>::read(socket, _value);
}
std::string toString() const
{
return RedisTypeTraits<T>::toString(_value);
} }
T& value() T& value()
@ -214,41 +247,6 @@ private:
T _value; T _value;
}; };
template<> inline }} // Namespace Poco/Redis
void Type<Int64>::read(RedisInputStream& input)
{
std::string number = input.getline();
_value = NumberParser::parse64(number);
}
template<> inline
void Type<std::string>::read(RedisInputStream& input)
{
_value.clear();
_value = input.getline();
}
template<> inline
void Type<BulkString>::read(RedisInputStream& input)
{
_value.clear();
std::string line = input.getline();
int length = NumberParser::parse64(line);
if ( length >= 0 )
{
std::string s;
s.resize(length, ' ');
input.read(&*s.begin(), length);
_value.assign(s);
line = input.getline();
}
}
}}
#endif // Redis_Type_INCLUDED #endif // Redis_Type_INCLUDED

View File

@ -46,9 +46,9 @@ Array& Array::add(RedisType::Ptr value)
} }
std::string Array::toString() const std::string Array::toString() const
{ {
return ElementTraits<Array>::toString(*this); return RedisTypeTraits<Array>::toString(*this);
} }
} } } }

View File

@ -38,19 +38,19 @@ RedisType::Ptr RedisType::createRedisType(char marker)
switch(marker) switch(marker)
{ {
case ElementTraits<Int64>::marker : case RedisTypeTraits<Int64>::marker :
result = new Type<Int64>(); result = new Type<Int64>();
break; break;
case ElementTraits<std::string>::marker : case RedisTypeTraits<std::string>::marker :
result = new Type<std::string>(); result = new Type<std::string>();
break; break;
case ElementTraits<BulkString>::marker : case RedisTypeTraits<BulkString>::marker :
result = new Type<BulkString>(); result = new Type<BulkString>();
break; break;
case ElementTraits<Array>::marker : case RedisTypeTraits<Array>::marker :
result = new Type<Array>(); result = new Type<Array>();
break; break;
case ElementTraits<Error>::marker : case RedisTypeTraits<Error>::marker :
result = new Type<Error>(); result = new Type<Error>();
break; break;
} }