1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-19 16:47:14 +02:00

Major plugin refactor and cleanup.

Switched to POCO library for unified platform/library interface.
Deprecated the external module API. It was creating more problems than solving.
Removed most built-in libraries in favor of system libraries for easier maintenance.
Cleaned and secured code with help from static analyzers.
This commit is contained in:
Sandu Liviu Catalin
2021-01-30 08:51:39 +02:00
parent e0e34b4030
commit 4a6bfc086c
6219 changed files with 1209835 additions and 454916 deletions

View File

@ -0,0 +1,322 @@
//
// Array.h
//
// Library: Redis
// Package: Redis
// Module: Array
//
// Definition of the Array class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Redis_Array_INCLUDED
#define Redis_Array_INCLUDED
#include "Poco/Redis/Redis.h"
#include "Poco/Redis/Type.h"
#include "Poco/Redis/Exception.h"
#include <vector>
#include <sstream>
namespace Poco {
namespace Redis {
class Redis_API Array
/// Represents a Redis Array. An Array can contain Integers, Strings,
/// Bulk Strings, Errors and other Arrays. It can also contain a Null
/// value.
{
public:
using const_iterator = std::vector<RedisType::Ptr>::const_iterator;
Array();
/// Creates an Array. As long as there are no elements added,
/// the array will contain a Null value.
Array(const Array& copy);
/// Creates an Array by copying another one.
virtual ~Array();
/// Destroys the Array.
template<typename T>
Array& operator<<(const T& arg)
/// Adds the argument to the array.
///
/// Note: a std::string will be added as a BulkString because this
/// is commonly used for representing strings in Redis. If you
/// really need a simple string, use addSimpleString().
{
return add(arg);
}
Array& operator<<(const char* s);
/// Special implementation for const char*.
///
/// Note: the specialization creates a BulkString. If you need
/// a simple string, call addSimpleString().
Array& operator<<(const std::vector<std::string>& strings);
/// Special implementation for a vector with strings.
/// All strings will be added as a BulkString.
Array& add();
/// Adds an Null BulkString.
template<typename T>
Array& add(const T& arg)
/// Adds an element to the array.
///
/// Note: the specialization for std::string will add a BulkString!
/// If you really need a simple string, call addSimpleString.
{
addRedisType(new Type<T>(arg));
return *this;
}
Array& add(const char* s);
/// Special implementation for const char*.
///
/// Note: the specialization creates a BulkString. If you need
/// a simple string, call addSimpleString.
Array& add(const std::vector<std::string>& strings);
/// Special implementation for a vector with strings.
/// All strings will be added as a BulkString.
Array& addRedisType(RedisType::Ptr value);
/// Adds a Redis element.
Array& addSimpleString(const std::string& value);
/// Adds a simple string (can't contain newline characters!).
const_iterator begin() const;
/// Returns an iterator to the start of the array.
///
/// Note: this can throw a NullValueException when this is a Null array.
void clear();
/// Removes all elements from the array.
const_iterator end() const;
/// Returns an iterator to the end of the array.
///
/// Note: this can throw a NullValueException when this is a Null array.
template<typename T>
T get(size_t pos) const
/// Returns the element on the given position and tries to convert
/// to the template type. A Poco::BadCastException will be thrown when the
/// the conversion fails. An Poco::InvalidArgumentException will be thrown
/// when the index is out of range. When the array is a Null array
/// a Poco::NullValueException is thrown.
{
if ( _elements.isNull() ) throw NullValueException();
if ( pos >= _elements.value().size() ) throw InvalidArgumentException();
RedisType::Ptr element = _elements.value().at(pos);
if ( RedisTypeTraits<T>::TypeId == element->type() )
{
Type<T>* concrete = dynamic_cast<Type<T>* >(element.get());
if ( concrete != NULL ) return concrete->value();
}
throw BadCastException();
}
int getType(size_t pos) const;
/// Returns the type of the element. This can throw a Poco::NullValueException
/// when this array is a Null array. An Poco::InvalidArgumentException will
/// be thrown when the index is out of range.
bool isNull() const;
/// Returns true when this is a Null array.
void makeNull();
/// Turns the array into a Null array. When the array already has some
/// elements, the array will be cleared.
std::string toString() const;
/// Returns the String representation as specified in the
/// Redis Protocol specification.
size_t size() const;
/// Returns the size of the array.
///
/// Note: this can throw a NullValueException when this is a Null array.
private:
void checkNull();
/// Checks for null array and sets a new vector if true.
Nullable<std::vector<RedisType::Ptr>> _elements;
};
//
// inlines
//
inline Array& Array::operator<<(const char* s)
{
BulkString value(s);
return add(value);
}
inline Array& Array::operator<<(const std::vector<std::string>& strings)
{
return add(strings);
}
inline Array& Array::add()
{
BulkString value;
return add(value);
}
template<>
inline Array& Array::add(const std::string& arg)
{
BulkString value(arg);
return add(value);
}
inline Array& Array::add(const char* s)
{
BulkString value(s);
return add(value);
}
inline Array& Array::add(const std::vector<std::string>& strings)
{
for(std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); ++it)
{
add(*it);
}
return *this;
}
inline Array& Array::addSimpleString(const std::string& value)
{
return addRedisType(new Type<std::string>(value));
}
inline Array::const_iterator Array::begin() const
{
return _elements.value().begin();
}
inline void Array::checkNull()
{
std::vector<RedisType::Ptr> v;
if ( _elements.isNull() ) _elements.assign(v);
}
inline void Array::clear()
{
if ( !_elements.isNull() )
{
_elements.value().clear();
}
}
inline Array::const_iterator Array::end() const
{
return _elements.value().end();
}
inline bool Array::isNull() const
{
return _elements.isNull();
}
inline void Array::makeNull()
{
if ( !_elements.isNull() ) _elements.value().clear();
_elements.clear();
}
inline size_t Array::size() const
{
return _elements.value().size();
}
template<>
struct RedisTypeTraits<Array>
{
enum { TypeId = RedisType::REDIS_ARRAY };
static const char marker = '*';
static std::string toString(const Array& value)
{
std::stringstream result;
result << marker;
if ( value.isNull() )
{
result << "-1" << LineEnding::NEWLINE_CRLF;
}
else
{
result << value.size() << LineEnding::NEWLINE_CRLF;
for(std::vector<RedisType::Ptr>::const_iterator it = value.begin();
it != value.end(); ++it)
{
result << (*it)->toString();
}
}
return result.str();
}
static void read(RedisInputStream& input, Array& value)
{
value.clear();
Int64 length = NumberParser::parse64(input.getline());
if ( length != -1 )
{
for(int i = 0; i < length; ++i)
{
char marker = input.get();
RedisType::Ptr element = RedisType::createRedisType(marker);
if ( element.isNull() )
throw RedisException("Wrong answer received from Redis server");
element->read(input);
value.addRedisType(element);
}
}
}
};
} } // namespace Poco::Redis
#endif // Redis_Array_INCLUDED

View File

@ -0,0 +1,98 @@
//
// AsyncReader.h
//
// Library: Redis
// Package: Redis
// Module: AsyncReader
//
// Definition of the AsyncReader class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Redis_AsyncReader_INCLUDED
#define Redis_AsyncReader_INCLUDED
#include "Poco/Redis/Redis.h"
#include "Poco/Redis/Client.h"
#include "Poco/Redis/RedisEventArgs.h"
#include "Poco/Activity.h"
namespace Poco {
namespace Redis {
class Redis_API AsyncReader
/// Wrapper around a Redis client to read messages asynchronously. Use this
/// for publish/subscribe. The redisResponse event is used to notify that
/// a message is received. When a reader is started for a Redis server,
/// you should use execute<void>, because this class is responsible for
/// reading all replies.
{
public:
BasicEvent<RedisEventArgs> redisResponse;
/// Event that is fired when a message is received.
BasicEvent<RedisEventArgs> redisException;
/// Event that is fired when an error occurred.
AsyncReader(Client& client);
/// Creates the AsyncReader using the given Client.
virtual ~AsyncReader();
/// Destroys the AsyncReader.
bool isStopped();
/// Returns true if the activity is not running, false when it is.
void start();
/// Starts the activity to read replies from the Redis server.
void stop();
/// Stops the read activity.
protected:
void runActivity();
private:
AsyncReader(const AsyncReader&);
AsyncReader& operator = (const AsyncReader&);
Client& _client;
Activity<AsyncReader> _activity;
};
//
// inlines
//
inline bool AsyncReader::isStopped()
{
return _activity.isStopped();
}
inline void AsyncReader::start()
{
_activity.start();
}
inline void AsyncReader::stop()
{
_activity.stop();
}
} } // namespace Poco::Redis
#endif //Redis_AsyncReader_INCLUDED

View File

@ -0,0 +1,239 @@
//
// Client.h
//
// Library: Redis
// Package: Redis
// Module: Client
//
// Definition of the Client class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Redis_Client_INCLUDED
#define Redis_Client_INCLUDED
#include "Poco/Redis/Redis.h"
#include "Poco/Redis/Array.h"
#include "Poco/Redis/Error.h"
#include "Poco/Redis/RedisStream.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Timespan.h"
namespace Poco {
namespace Redis {
class Redis_API Client
/// Represents a connection to a Redis server.
///
/// A command is always made from an Array and a reply can be a signed 64
/// bit integer, a simple string, a bulk string, an array or an error. The
/// first element of the command array is the Redis command. A simple string
/// is a string that cannot contain a CR or LF character. A bulk string is
/// implemented as an alias for Poco::Nullable<std::string>. This is
/// because a bulk string can represent a Null value.
///
/// BulkString bs = client.execute<BulkString>(...);
/// if ( bs.isNull() )
/// {
/// // We have a Null value
/// }
/// else
/// {
/// // We have a string value
/// }
///
/// To create Redis commands, the factory methods of the Command class can
/// be used or the Array class can be used directly.
///
/// Command llen = Command::llen("list");
///
/// is the same as:
///
/// Array command;
/// command.add("LLEN").add("list");
///
/// or:
///
/// Array command;
/// command << "LLEN" << "list";
///
/// or even:
///
/// Command command("LLEN");
/// command << "list";
{
public:
using Ptr = SharedPtr<Client>;
Client();
/// Creates an unconnected Client.
/// Use this when you want to connect later on.
Client(const std::string& hostAndPort);
/// Constructor which connects to the given Redis host/port.
/// The host and port must be separated with a colon.
Client(const std::string& host, int port);
/// Constructor which connects to the given Redis host/port.
Client(const Net::SocketAddress& addrs);
/// Constructor which connects to the given Redis host/port.
virtual ~Client();
/// Destroys the Client.
Net::SocketAddress address() const;
/// Returns the address of the Redis connection.
void connect(const std::string& hostAndPort);
/// Connects to the given Redis server. The host and port must be
/// separated with a colon.
void connect(const std::string& host, int port);
/// Connects to the given Redis server.
void connect(const Net::SocketAddress& addrs);
/// Connects to the given Redis server.
void connect(const std::string& hostAndPort, const Timespan& timeout);
/// Connects to the given Redis server. The host and port must be
/// separated with a colon.
void connect(const std::string& host, int port, const Timespan& timeout);
/// Connects to the given Redis server.
void connect(const Net::SocketAddress& addrs, const Timespan& timeout);
/// Connects to the given Redis server.
void disconnect();
/// Disconnects from the Redis server.
bool isConnected() const;
/// Returns true iff the Client is connected to a Redis server.
template<typename T>
T execute(const Array& command)
/// Sends the Redis Command to the server. It gets the reply
/// and tries to convert it to the given template type.
///
/// A specialization exists for type void, which doesn't read
/// the reply. If the server sends a reply, it is your
/// responsibility to read it. Use this for pipelining.
///
/// A Poco::BadCastException will be thrown when the reply couldn't be
/// converted. Supported types are Int64, std::string, BulkString,
/// Array and void. When the reply is an Error, it will throw
/// a RedisException.
{
T result = T();
writeCommand(command, true);
readReply(result);
return result;
}
void flush();
/// Flush the output buffer to Redis. Use this when commands
/// are stored in the buffer to send them all at once to Redis.
RedisType::Ptr sendCommand(const Array& command);
/// Sends a Redis command to the server and returns the reply.
/// Use this when the type of the reply isn't known.
RedisType::Ptr readReply();
/// Read a reply from the Redis server.
template<typename T>
void readReply(T& result)
/// Read a reply from the Redis server and tries to convert that reply
/// to the template type. When the reply is a Redis error, it will
/// throw a RedisException. A BadCastException will be thrown, when
/// the reply is not of the given type.
{
RedisType::Ptr redisResult = readReply();
if (redisResult->type() == RedisTypeTraits<Error>::TypeId)
{
Type<Error>* error = dynamic_cast<Type<Error>*>(redisResult.get());
throw RedisException(error->value().getMessage());
}
if (redisResult->type() == RedisTypeTraits<T>::TypeId)
{
Type<T>* type = dynamic_cast<Type<T>*>(redisResult.get());
if (type != NULL) result = type->value();
}
else throw BadCastException();
}
Array sendCommands(const std::vector<Array>& commands);
/// Sends all commands (pipelining) to the Redis server before
/// getting all replies.
void setReceiveTimeout(const Timespan& timeout);
/// Sets a receive timeout.
private:
Client(const Client&);
Client& operator = (const Client&);
void connect();
/// Connects to the Redis server
void connect(const Timespan& timeout);
/// Connects to the Redis server and sets a timeout.
void writeCommand(const Array& command, bool flush);
/// Sends a request to the Redis server. Use readReply to get the
/// answer. Can also be used for pipelining commands. Make sure you
/// call readReply as many times as you called writeCommand, even when
/// an error occurred on a command.
Net::SocketAddress _address;
Net::StreamSocket _socket;
RedisInputStream* _input;
RedisOutputStream* _output;
};
//
// inlines
//
inline Net::SocketAddress Client::address() const
{
return _address;
}
template<> inline
void Client::execute<void>(const Array& command)
{
writeCommand(command, false);
}
inline void Client::flush()
{
poco_assert(_output);
_output->flush();
}
inline void Client::setReceiveTimeout(const Timespan& timeout)
{
_socket.setReceiveTimeout(timeout);
}
} } // namespace Poco::Redis
#endif // Redis_Client_INCLUDED

View File

@ -0,0 +1,279 @@
//
// Command.h
//
// Library: Redis
// Package: Redis
// Module: Command
//
// Definition of the Command class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Redis_Command_INCLUDED
#define Redis_Command_INCLUDED
#include "Poco/Redis/Redis.h"
#include "Poco/Redis/Array.h"
#include <vector>
#include <map>
namespace Poco {
namespace Redis {
class Redis_API Command: public Array
/// Helper class for creating commands. This class contains
/// factory methods for commonly used Redis commands.
///
/// There are two ways of building commands:
///
/// 1. Use this class and the factory methods
/// 2. Use the Array or Command class and build the command using the add
/// method or << operator.
///
/// For example:
///
/// Command cmd = Command::set("mykey", "Hello");
///
/// is the same as:
///
/// Array cmd;
/// cmd << "SET" << "mykey" << "Hello";
///
{
public:
using StringVec = std::vector<std::string>;
Command(const std::string& command);
/// Creates a command.
Command(const Command& copy);
/// Creates a command by copying another one.
virtual ~Command();
/// Destroys the command.
static Command append(const std::string& key, const std::string& value);
/// Creates and returns an APPEND command.
static Command blpop(const StringVec& lists, Int64 timeout = 0);
/// Creates and returns a BLPOP command.
static Command brpop(const StringVec& lists, Int64 timeout = 0);
/// Creates and returns a BRPOP command.
static Command brpoplpush(const std::string& sourceList, const std::string& destinationList, Int64 timeout = 0);
/// Creates and returns a BRPOPLPUSH command.
static Command decr(const std::string& key, Int64 by = 0);
/// Creates and returns an DECR or DECRBY command. Calls DECR when by is omitted or zero.
static Command del(const std::string& key);
/// Creates and returns an DEL command.
static Command del(const StringVec& keys);
/// Creates and returns an DEL command.
static Command get(const std::string& key);
/// Creates and returns an GET command.
static Command exists(const std::string& key);
/// Creates and returns an EXISTS command.
static Command hdel(const std::string& hash, const std::string& field);
/// Creates and returns an HDEL command.
static Command hdel(const std::string& hash, const StringVec& fields);
/// Creates and returns an HDEL command.
static Command hexists(const std::string& hash, const std::string& field);
/// Creates and returns an HEXISTS command.
static Command hget(const std::string& hash, const std::string& field);
/// Creates and returns an HGET command.
static Command hgetall(const std::string& hash);
/// Creates and returns an HGETALL command.
static Command hincrby(const std::string& hash, const std::string& field, Int64 by = 1);
/// Creates and returns an HINCRBY command.
static Command hkeys(const std::string& hash);
/// Creates and returns an HKEYS command.
static Command hlen(const std::string& hash);
/// Creates and returns an HLEN command.
static Command hmget(const std::string& hash, const StringVec& fields);
/// Creates and returns an HMGET command.
static Command hmset(const std::string& hash, std::map<std::string, std::string>& fields);
/// Creates and returns a HMSET command.
static Command hset(const std::string& hash, const std::string& field, const std::string& value, bool create = true);
/// Creates and returns an HSET or HSETNX (when create is false) command.
static Command hset(const std::string& hash, const std::string& field, Int64 value, bool create = true);
/// Creates and returns an HSET or HSETNX (when create is false) command.
static Command hstrlen(const std::string& hash, const std::string& field);
/// Creates and returns an HSTRLEN command (Available for Redis 3.2).
static Command hvals(const std::string& hash);
/// Creates and returns an HVALS command.
static Command incr(const std::string& key, Int64 by = 0);
/// Creates and returns an INCR or INCRBY command. Calls INCR when by is omitted or zero.
static Command lindex(const std::string& list, Int64 index = 0);
/// Creates and returns a LINDEX command.
static Command linsert(const std::string& list, bool before, const std::string& reference, const std::string& value);
/// Creates and returns a LINSERT command.
static Command llen(const std::string& list);
/// Creates and returns a LLEN command.
static Command lpop(const std::string& list);
/// Creates and returns a LPOP command.
static Command lpush(const std::string& list, const std::string& value, bool create = true);
/// Creates and returns a LPUSH or LPUSHX (when create is false) command.
static Command lpush(const std::string& list, const StringVec& value, bool create = true);
/// Creates and returns a LPUSH or LPUSHX (when create is false) command.
static Command lrange(const std::string& list, Int64 start = 0, Int64 stop = -1);
/// Creates and returns a LRANGE command. When start and stop is omitted an LRANGE
/// command will returned that returns all items of the list.
static Command lrem(const std::string& list, Int64 count, const std::string& value);
/// Creates and returns a LREM command.
static Command lset(const std::string& list, Int64 index, const std::string& value);
/// Creates and returns a LSET command.
static Command ltrim(const std::string& list, Int64 start = 0, Int64 stop = -1);
/// Creates and returns a LTRIM command.
static Command mget(const StringVec& keys);
/// Creates and returns a MGET command.
static Command mset(const std::map<std::string, std::string>& keyvalues, bool create = true);
/// Creates and returns a MSET or MSETNX (when create is false) command.
static Command sadd(const std::string& set, const std::string& value);
/// Creates and returns a SADD command.
static Command sadd(const std::string& set, const StringVec& values);
/// Creates and returns a SADD command.
static Command scard(const std::string& set);
/// Creates and returns a SCARD command.
static Command sdiff(const std::string& set1, const std::string& set2);
/// Creates and returns a SDIFF command.Creates and returns
static Command sdiff(const std::string& set, const StringVec& sets);
/// Creates and returns a SDIFF command.
static Command sdiffstore(const std::string& set, const std::string& set1, const std::string& set2);
/// Creates and returns a SDIFFSTORE command.
static Command sdiffstore(const std::string& set, const StringVec& sets);
/// Creates and returns a SDIFFSTORE command.
static Command set(const std::string& key, const std::string& value, bool overwrite = true, const Poco::Timespan& expireTime = 0, bool create = true);
/// Creates and returns a SET command to set the key with a value.
static Command set(const std::string& key, Int64 value, bool overwrite = true, const Poco::Timespan& expireTime = 0, bool create = true);
/// Creates and returns a SET command to set the key with a value.
static Command sinter(const std::string& set1, const std::string& set2);
/// Creates and returns a SINTER command.
static Command sinter(const std::string& set, const StringVec& sets);
/// Creates and returns a SINTER command.
static Command sinterstore(const std::string& set, const std::string& set1, const std::string& set2);
/// Creates and returns a SINTERSTORE command.
static Command sinterstore(const std::string& set, const StringVec& sets);
/// Creates and returns a SINTERSTORE command.
static Command sismember(const std::string& set, const std::string& member);
/// Creates and returns a SISMEMBER command.
static Command smembers(const std::string& set);
/// Creates and returns a SMEMBERS command.
static Command smove(const std::string& source, const std::string& destination, const std::string& member);
/// Creates and returns a SMOVE command.
static Command spop(const std::string& set, Int64 count = 0);
/// Creates and returns a SPOP command.
static Command srandmember(const std::string& set, Int64 count = 0);
/// Creates and returns a SRANDMEMBER command.
static Command srem(const std::string& set, const std::string& member);
/// Creates and returns a SREM command.
static Command srem(const std::string& set, const StringVec& members);
/// Creates and returns a SREM command.
static Command sunion(const std::string& set1, const std::string& set2);
/// Creates and returns a SUNION command.
static Command sunion(const std::string& set, const StringVec& sets);
/// Creates and returns a SUNION command.
static Command sunionstore(const std::string& set, const std::string& set1, const std::string& set2);
/// Creates and returns a SUNIONSTORE command.
static Command sunionstore(const std::string& set, const StringVec& sets);
/// Creates and returns a SUNIONSTORE command.
static Command rename(const std::string& key, const std::string& newName, bool overwrite = true);
/// Creates and returns a RENAME or RENAMENX when overwrite is false.
static Command rpop(const std::string& list);
/// Creates and returns a RPOP command.
static Command rpoplpush(const std::string& sourceList, const std::string& destinationList);
/// Creates and returns a RPOPLPUSH command.
static Command rpush(const std::string& list, const std::string& value, bool create = true);
/// Creates and returns a RPUSH or RPUSHX (when create is false) command.
static Command rpush(const std::string& list, const StringVec& value, bool create = true);
/// Creates and returns a RPUSH or RPUSHX (when create is false) command.
static Command expire(const std::string& key, Int64 seconds);
/// Creates and returns an EXPIRE command.
static Command ping();
/// Creates and returns a PING command.
static Command multi();
/// Creates and returns a MULTI command.
static Command exec();
/// Creates and returns a EXEC command.
static Command discard();
/// Creates and returns a DISCARD command.
};
} } // namespace Poco::Redis
#endif // Redis_Command_INCLUDED

View File

@ -0,0 +1,91 @@
//
// Error.h
//
// Library: Redis
// Package: Redis
// Module: Error
//
// Definition of the Error class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Redis_Error_INCLUDED
#define Redis_Error_INCLUDED
#include "Poco/Redis/Type.h"
namespace Poco {
namespace Redis {
class Redis_API Error
/// Represent a Redis error.
{
public:
Error();
/// Creates an empty Error.
Error(const std::string& message);
/// Creates an Error with the given message.
virtual ~Error();
/// Destroys the Error.
const std::string& getMessage() const;
/// Returns the error message.
void setMessage(const std::string& message);
/// Sets the error message.
private:
std::string _message;
};
//
// inlines
//
inline const std::string& Error::getMessage() const
{
return _message;
}
inline void Error::setMessage(const std::string& message)
{
_message = message;
}
template<>
struct RedisTypeTraits<Error>
{
enum { TypeId = RedisType::REDIS_ERROR };
static const char marker = '-';
static std::string toString(const Error& value)
{
return marker + value.getMessage() + LineEnding::NEWLINE_CRLF;
}
static void read(RedisInputStream& input, Error& value)
{
value.setMessage(input.getline());
}
};
} } // namespace Poco::Redis
#endif // Redis_Error_INCLUDED

View File

@ -0,0 +1,36 @@
//
// Exception.h
//
// Library: Redis
// Package: Redis
// Module: Exception
//
// Definition of the Exception class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Redis_Exception_INCLUDED
#define Redis_Exception_INCLUDED
#include "Poco/Redis/Redis.h"
#include "Poco/Exception.h"
#include <typeinfo>
namespace Poco {
namespace Redis {
POCO_DECLARE_EXCEPTION(Redis_API, RedisException, Exception)
} } // namespace Poco::Redis
#endif // Redis_Exception_INCLUDED

View File

@ -0,0 +1,114 @@
//
// PoolableConnectionFactory.h
//
// Library: Redis
// Package: Redis
// Module: PoolableConnectionFactory
//
// Definition of the PoolableConnectionFactory class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Redis_PoolableConnectionFactory_INCLUDED
#define Redis_PoolableConnectionFactory_INCLUDED
#include "Poco/Redis/Client.h"
#include "Poco/ObjectPool.h"
#include "Poco/Version.h"
namespace Poco {
template<>
class PoolableObjectFactory<Redis::Client, Redis::Client::Ptr>
/// PoolableObjectFactory specialisation for Client. New connections
/// are created with the given address.
{
public:
PoolableObjectFactory(Net::SocketAddress& address):
_address(address)
{
}
PoolableObjectFactory(const std::string& address):
_address(address)
{
}
Redis::Client::Ptr createObject()
{
return new Redis::Client(_address);
}
bool validateObject(Redis::Client::Ptr pObject)
{
return true;
}
void activateObject(Redis::Client::Ptr pObject)
{
}
void deactivateObject(Redis::Client::Ptr pObject)
{
}
void destroyObject(Redis::Client::Ptr pObject)
{
}
private:
Net::SocketAddress _address;
};
namespace Redis {
class PooledConnection
/// Helper class for borrowing and returning a connection automatically from a pool.
{
public:
PooledConnection(ObjectPool<Client, Client::Ptr>& pool, long timeoutMilliseconds = 0) : _pool(pool)
{
#if POCO_VERSION >= 0x01080000
_client = _pool.borrowObject(timeoutMilliseconds);
#else
_client = _pool.borrowObject();
#endif
}
virtual ~PooledConnection()
{
try
{
_pool.returnObject(_client);
}
catch (...)
{
poco_unexpected();
}
}
operator Client::Ptr()
{
return _client;
}
private:
ObjectPool<Client, Client::Ptr>& _pool;
Client::Ptr _client;
};
} } // namespace Poco::Redis
#endif // Redis_PoolableConnectionFactory_INCLUDED

View File

@ -0,0 +1,62 @@
//
// Redis.h
//
// Library: Redis
// Package: Redis
// Module: Redis
//
// Basic definitions for the Poco Redis library.
// This file must be the first file included by every other Redis
// header file.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef RedisRedis_INCLUDED
#define RedisRedis_INCLUDED
#include "Poco/Foundation.h"
//
// The following block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the Redis_EXPORTS
// symbol defined on the command line. This symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// Redis_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
//
#if defined(_WIN32) && defined(POCO_DLL)
#if defined(Redis_EXPORTS)
#define Redis_API __declspec(dllexport)
#else
#define Redis_API __declspec(dllimport)
#endif
#endif
#if !defined(Redis_API)
#if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined (__GNUC__) && (__GNUC__ >= 4)
#define Redis_API __attribute__ ((visibility ("default")))
#else
#define Redis_API
#endif
#endif
//
// Automatically link Redis library.
//
#if defined(_MSC_VER)
#if !defined(POCO_NO_AUTOMATIC_LIBS) && !defined(Redis_EXPORTS)
#pragma comment(lib, "PocoRedis" POCO_LIB_SUFFIX)
#endif
#endif
#endif // RedisRedis_INCLUDED

View File

@ -0,0 +1,99 @@
//
// RedisEventArgs.h
//
// Library: Redis
// Package: Redis
// Module: RedisEventArgs
//
// Definition of the RedisEventArgs class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Redis_RedisEventArgs_INCLUDED
#define Redis_RedisEventArgs_INCLUDED
#include "Poco/Redis/Type.h"
namespace Poco {
namespace Redis {
class Redis_API RedisEventArgs
/// Event arguments for AsyncReader events.
{
public:
RedisEventArgs(RedisType::Ptr message);
/// Creates the RedisEventArgs from the given message.
RedisEventArgs(Exception* e);
/// Creates the RedisEventArgs from the given Redis Exception.
~RedisEventArgs();
/// Destroys the RedisEventArgs.
RedisType::Ptr message() const;
/// Returns the message retrieved from the Redis server.
/// This can be a NULL pointer when this event is about an exception.
const Exception* exception() const;
/// Returns the exception if any, otherwise it returns null pointer.
void stop();
/// When called, the AsyncReader will stop.
///
/// Note: The AsyncReader will always stop when this is an exception
/// event. Use this for example for pub/sub when there are no
/// subcribers anymore.
bool isStopped() const;
/// Returns true when the AsyncReader will stop.
private:
RedisType::Ptr _message;
Exception* _exception;
bool _stop;
};
//
// inlines
//
inline RedisType::Ptr RedisEventArgs::message() const
{
return _message;
}
inline const Exception* RedisEventArgs::exception() const
{
return _exception;
}
inline bool RedisEventArgs::isStopped() const
{
return _stop;
}
inline void RedisEventArgs::stop()
{
_stop = true;
}
} } // namespace Poco::Redis
#endif // Redis_RedisEventArgs_INCLUDED

View File

@ -0,0 +1,114 @@
//
// RedisStream.h
//
// Library: Redis
// Package: Redis
// Module: RedisStream
//
// Definition of the RedisStream class.
//
// Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Redis_RedisStream_INCLUDED
#define Redis_RedisStream_INCLUDED
#include "Poco/Redis/Redis.h"
#include "Poco/BufferedStreamBuf.h"
#include "Poco/Net/StreamSocket.h"
#include <istream>
#include <ostream>
namespace Poco {
namespace Redis {
class RedisStreamBuf: public BufferedStreamBuf
/// BufferedStreamBuf for Redis.
{
public:
RedisStreamBuf(Net::StreamSocket& redis);
/// Constructor
~RedisStreamBuf();
/// Destructor
std::string readLine();
/// Reads a line from Redis (until \r\n is encountered).
protected:
int readFromDevice(char* buffer, std::streamsize length);
int writeToDevice(const char* buffer, std::streamsize length);
private:
enum
{
STREAM_BUFFER_SIZE = 1024
};
Net::StreamSocket& _redis;
};
class RedisIOS: public virtual std::ios
{
public:
RedisIOS(Net::StreamSocket& redis);
/// Creates the RedisIOS with the given socket.
~RedisIOS();
/// Destroys the RedisIOS.
///
/// Flushes the buffer, but does not close the socket.
RedisStreamBuf* rdbuf();
/// Returns a pointer to the internal RedisStreamBuf.
void close();
/// Flushes the stream.
protected:
RedisStreamBuf _buf;
};
class Redis_API RedisOutputStream: public RedisIOS, public std::ostream
/// An output stream for writing to a Redis server.
{
public:
RedisOutputStream(Net::StreamSocket& redis);
/// Creates the RedisOutputStream with the given socket.
~RedisOutputStream();
/// Destroys the RedisOutputStream.
///
/// Flushes the buffer.
};
class Redis_API RedisInputStream: public RedisIOS, public std::istream
/// An input stream for reading from a Redis server.
{
public:
RedisInputStream(Net::StreamSocket& redis);
/// Creates the RedisInputStream with the given socket.
~RedisInputStream();
/// Destroys the RedisInputStream.
std::string getline();
/// Redis uses \r\n as delimiter. This getline version removes
/// the \r from the result.
};
} } // namespace Poco::Redis
#endif // Redis_RedisStream_INCLUDED

View File

@ -0,0 +1,293 @@
//
// Type.h
//
// Library: Redis
// Package: Redis
// Module: Type
//
// Definition of the Type class.
//
// Copyright (c) 2016, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Redis_Type_INCLUDED
#define Redis_Type_INCLUDED
#include "Poco/LineEndingConverter.h"
#include "Poco/NumberFormatter.h"
#include "Poco/NumberParser.h"
#include "Poco/SharedPtr.h"
#include "Poco/Nullable.h"
#include "Poco/Redis/Redis.h"
#include "Poco/Redis/RedisStream.h"
namespace Poco {
namespace Redis {
class Redis_API RedisType
/// Base class for all Redis types. This class makes it possible to store
/// element with different types in Array.
{
public:
enum Types
{
REDIS_INTEGER, /// Redis Integer
REDIS_SIMPLE_STRING, /// Redis Simple String
REDIS_BULK_STRING, /// Redis Bulkstring
REDIS_ARRAY, /// Redis Array
REDIS_ERROR /// Redis Error
};
using Ptr = SharedPtr<RedisType>;
RedisType();
/// Creates the RedisType.
virtual ~RedisType();
/// Destroys the RedisType.
bool isArray() const;
/// Returns true when the value is a Redis array.
bool isBulkString() const;
/// Returns true when the value is a Redis bulkstring.
bool isError() const;
/// Returns true when the value is a Redis error.
bool isInteger() const;
/// Returns true when the value is a Redis integer (64 bit integer).
bool isSimpleString() const;
/// Returns true when the value is a simple string.
virtual int type() const = 0;
/// Returns the type of the value.
virtual void read(RedisInputStream& input) = 0;
/// Reads the value from the stream.
virtual std::string toString() const = 0;
/// Converts the value to a RESP (REdis Serialization Protocol) string.
static RedisType::Ptr createRedisType(char marker);
/// Create a Redis type based on the marker:
///
/// - '+': a simple string (std::string)
/// - '-': an error (Error)
/// - '$': a bulk string (BulkString)
/// - '*': an array (Array)
/// - ':': a signed 64 bit integer (Int64)
};
//
// inlines
//
inline bool RedisType::isArray() const
{
return type() == REDIS_ARRAY;
}
inline bool RedisType::isBulkString() const
{
return type() == REDIS_BULK_STRING;
}
inline bool RedisType::isError() const
{
return type() == REDIS_ERROR;
}
inline bool RedisType::isInteger() const
{
return type() == REDIS_INTEGER;
}
inline bool RedisType::isSimpleString() const
{
return type() == REDIS_SIMPLE_STRING;
}
template<typename T>
struct RedisTypeTraits
{
};
template<>
struct RedisTypeTraits<Int64>
{
enum
{
TypeId = RedisType::REDIS_INTEGER
};
static const char marker = ':';
static std::string toString(const Int64& value)
{
return marker + NumberFormatter::format(value) + "\r\n";
}
static void read(RedisInputStream& input, Int64& value)
{
std::string number = input.getline();
value = NumberParser::parse64(number);
}
};
template<>
struct RedisTypeTraits<std::string>
{
enum
{
TypeId = RedisType::REDIS_SIMPLE_STRING
};
static const char marker = '+';
static std::string toString(const std::string& value)
{
return marker + value + LineEnding::NEWLINE_CRLF;
}
static void read(RedisInputStream& input, std::string& value)
{
value = input.getline();
}
};
using BulkString = Nullable<std::string>;
/// A bulk string is a string that can contain a NULL value.
/// So, BulkString is an alias for Nullable<std::string>.
template<>
struct RedisTypeTraits<BulkString>
{
enum
{
TypeId = RedisType::REDIS_BULK_STRING
};
static const char marker = '$';
static std::string toString(const BulkString& value)
{
if ( value.isNull() )
{
return marker + std::string("-1") + LineEnding::NEWLINE_CRLF;
}
else
{
std::string s = value.value();
return marker
+ NumberFormatter::format(s.length())
+ LineEnding::NEWLINE_CRLF
+ s
+ LineEnding::NEWLINE_CRLF;
}
}
static void read(RedisInputStream& input, BulkString& value)
{
value.clear();
std::string line = input.getline();
int length = NumberParser::parse(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
}
}
};
template<typename T>
class Type: public RedisType
/// Template class for all Redis types. This class will use
/// RedisTypeTraits structure for calling the type specific code.
{
public:
Type()
/// Creates the Type.
{
}
Type(const T& t) : _value(t)
/// Creates the Type from another one.
{
}
Type(const Type& copy) : _value(copy._value)
/// Creates the Type by copying another one.
{
}
virtual ~Type()
/// Destroys the Type.
{
}
int type() const
/// Returns the type of the value
{
return RedisTypeTraits<T>::TypeId;
}
void read(RedisInputStream& socket)
/// Reads the value from the stream (RESP).
{
RedisTypeTraits<T>::read(socket, _value);
}
std::string toString() const
/// Converts the value to a string based on the RESP protocol.
{
return RedisTypeTraits<T>::toString(_value);
}
T& value()
/// Returns the value
{
return _value;
}
const T& value() const
/// Returns a const value
{
return _value;
}
private:
T _value;
};
} } // namespace Poco/Redis
#endif // Redis_Type_INCLUDED