Skip to content

Commit

Permalink
make exception
Browse files Browse the repository at this point in the history
  • Loading branch information
fastio committed Jun 20, 2019
1 parent 602854e commit 81af093
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 28 deletions.
4 changes: 3 additions & 1 deletion redis/abstract_command.hh
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ inline bytes double2bytes(double d) {
}
inline bool is_number(const bytes& b)
{
return !b.empty() && std::find_if(b.begin(), b.end(), [] (auto c) { return !std::isdigit((char)c); }) == b.end();
return !b.empty() && std::find_if(b.begin(), b.end(), [] (auto c) {
return !(std::isdigit((char)c) || c == '.' || c == '+' || c == '-');
}) == b.end();
}
class abstract_command : public enable_shared_from_this<abstract_command> {
protected:
Expand Down
5 changes: 3 additions & 2 deletions redis/command_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ shared_ptr<abstract_command> command_factory::create(service::storage_proxy& pro
{ "zrem", [] (service::storage_proxy& proxy, const service::client_state& cs, request&& req) { return commands::zrem::prepare(proxy, cs, std::move(req)); } },
{ "zremrangebyrank", [] (service::storage_proxy& proxy, const service::client_state& cs, request&& req) { return commands::zremrangebyrank::prepare(proxy, cs, std::move(req)); } },
{ "zremrangebyscore", [] (service::storage_proxy& proxy, const service::client_state& cs, request&& req) { return commands::zremrangebyscore::prepare(proxy, cs, std::move(req)); } },
//{ "cluster", [] (service::storage_proxy& proxy, const service::client_state& cs, request&& req) { return commands::cluster_slots::prepare(proxy, cs, std::move(req)); } },
{ "cluster", [] (service::storage_proxy& proxy, const service::client_state& cs, request&& req) { return commands::cluster_slots::prepare(proxy, cs, std::move(req)); } },
};
auto&& command = _commands.find(req._command);
if (command != _commands.end()) {
return (command->second)(proxy, cs, std::move(req));
}
logging.error("unkown command = {}", req._command);
auto& b = req._command;
logging.error("unkown command = {}", sstring(reinterpret_cast<const char*>(b.data()), b.size()));
return commands::unexpected::prepare(std::move(req._command));
}

Expand Down
4 changes: 2 additions & 2 deletions redis/commands/cluster_slots.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ static const bytes slots {"slots"};
shared_ptr<abstract_command> cluster_slots::prepare(service::storage_proxy& proxy, const service::client_state& cs, request&& req)
{
if (req._args_count != 1) {
return unexpected::prepare(std::move(req._command), std::move(to_bytes(sprint("-wrong number of arguments (given %ld, expected 1)\r\n", req._args_count))));
return unexpected::make_exception(std::move(req._command), sprint("-wrong number of arguments (given %ld, expected 1)\r\n", req._args_count));
}
auto& slots = req._args[0];
std::transform(slots.begin(), slots.end(), slots.begin(), ::tolower);
if (slots != slots) {
return unexpected::prepare(std::move(req._command), std::move(to_bytes(sprint("-unknown cluster command '%s'\r\n", slots))));
return unexpected::make_exception(std::move(req._command), sprint("-unknown cluster command '%s'\r\n", slots));
}
return seastar::make_shared<cluster_slots> (std::move(req._command));
}
Expand Down
2 changes: 1 addition & 1 deletion redis/commands/expire.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ shared_ptr<abstract_command> expire::prepare(service::storage_proxy& proxy, cons
try {
ttl = bytes2long(req._args[1]);
} catch(std::exception&) {
return unexpected::make_wrong_arguments_exception(std::move(req._command), to_bytes("-ERR value is not an integer or out of range"));
return unexpected::make_exception(std::move(req._command), "-ERR value is not an integer or out of range");
}
std::vector<schema_ptr> schemas {
simple_objects_schema(proxy, cs.get_keyspace()),
Expand Down
2 changes: 1 addition & 1 deletion redis/commands/hget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace commands {
shared_ptr<abstract_command> hget::prepare(service::storage_proxy& proxy, const service::client_state& cs, request&& req, bool multi)
{
if (req._args_count < 2 || (!multi && req._args_count != 2)) {
return unexpected::prepare(std::move(req._command), std::move(bytes {"-ERR wrong number of arguments for HMGET\r\n"}));
return unexpected::prepare(std::move(req._command), sstring("-ERR wrong number of arguments for HMGET\r\n"));
}
std::vector<bytes> map_keys;
for (size_t i = 1; i < req._args_count; i++) {
Expand Down
2 changes: 1 addition & 1 deletion redis/commands/hincrby.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace commands {
shared_ptr<abstract_command> hincrby::prepare(service::storage_proxy& proxy, const service::client_state& cs, request&& req)
{
if (req._args_count < 3) {
return unexpected::prepare(std::move(req._command), std::move(bytes {"-ERR wrong number of arguments for HINCRBY\r\n"}));
return unexpected::make_exception(std::move(req._command), sprint("-wrong number of arguments (given %ld, expected 1)\r\n", req._args_count));
}
return seastar::make_shared<hincrby>(std::move(req._command), maps_schema(proxy, cs.get_keyspace()), std::move(req._args[0]), std::move(req._args[1]), bytes2double(req._args[2]));
}
Expand Down
2 changes: 1 addition & 1 deletion redis/commands/hset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace commands {
shared_ptr<abstract_command> hset::prepare(service::storage_proxy& proxy, const service::client_state& cs, request&& req, bool multi)
{
if (req._args_count < 3 || req._args_count % 2 != 1 || (!multi && req._args_count > 3)) {
return unexpected::prepare(std::move(req._command), std::move(bytes {"-ERR wrong number of arguments for HMSET\r\n"}));
return unexpected::prepare(std::move(req._command), sstring("-ERR wrong number of arguments for HMSET\r\n"));
}
std::unordered_map<bytes, bytes> data;
for (size_t i = 1; i < req._args_count; i+= 2) {
Expand Down
2 changes: 1 addition & 1 deletion redis/commands/scard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace commands {
shared_ptr<abstract_command> scard::prepare(service::storage_proxy& proxy, const service::client_state& cs, request&& req)
{
if (req._args_count != 1) {
return unexpected::prepare(std::move(req._command), std::move(to_bytes(sprint("-wrong number of arguments (given %ld, expected 1)\r\n", req._args_count))));
return unexpected::make_exception(std::move(req._command), sprint("-wrong number of arguments (given %ld, expected 1)\r\n", req._args_count));
}
return seastar::make_shared<scard> (std::move(req._command), sets_schema(proxy, cs.get_keyspace()), std::move(req._args[0]));
}
Expand Down
2 changes: 1 addition & 1 deletion redis/commands/set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ shared_ptr<abstract_command> setex::prepare(service::storage_proxy& proxy, const
try {
ttl = bytes2long(req._args[1]);
} catch(std::exception&) {
return unexpected::make_wrong_arguments_exception(std::move(req._command), to_bytes("-ERR value is not an integer or out of range"));
return unexpected::make_exception(std::move(req._command), sstring("-ERR value is not an integer or out of range"));
}
//std::chrono::seconds(ttl));
return seastar::make_shared<setex> (std::move(req._command), simple_objects_schema(proxy, cs.get_keyspace()), std::move(req._args[0]), std::move(req._args[2]), ttl);
Expand Down
2 changes: 1 addition & 1 deletion redis/commands/spop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace commands {
shared_ptr<abstract_command> spop::prepare(service::storage_proxy& proxy, const service::client_state& cs, request&& req)
{
if (req._args_count != 1) {
return unexpected::prepare(std::move(req._command), std::move(to_bytes(sprint("-wrong number of arguments (given %ld, expected 1)\r\n", req._args_count))));
return unexpected::make_exception(std::move(req._command), sprint("-wrong number of arguments (given %ld, expected 1)\r\n", req._args_count));
}
return seastar::make_shared<spop> (std::move(req._command), sets_schema(proxy, cs.get_keyspace()), std::move(req._args[0]));
}
Expand Down
2 changes: 1 addition & 1 deletion redis/commands/srandmember.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace commands {
shared_ptr<abstract_command> srandmember::prepare(service::storage_proxy& proxy, const service::client_state& cs, request&& req)
{
if (req._args_count < 1) {
return unexpected::prepare(std::move(req._command), std::move(to_bytes(sprint("-wrong number of arguments (given %ld, expected 1)\r\n", req._args_count))));
return unexpected::make_exception(std::move(req._command), sprint("-wrong number of arguments (given %ld, expected 1)\r\n", req._args_count));
}
long count = 0;
if (req._args_count > 1) {
Expand Down
26 changes: 14 additions & 12 deletions redis/commands/unexpected.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@
namespace redis {
namespace commands {
class unexpected : public abstract_command {
bytes _exception_message;
bytes default_exception_message() {
bytes m { to_bytes(sprint("-ERR Unknown or disabled command '" + sstring(reinterpret_cast<const char*>(_name.data()), _name.size()) + "'\r\n")) };
return m;
sstring _exception_message;
sstring default_exception_message() {
return sstring { sprint("-ERR Unknown or disabled command '" + sstring(reinterpret_cast<const char*>(_name.data()), _name.size()) + "'\r\n") };
}
public:
unexpected(bytes&& name) : abstract_command(std::move(name)), _exception_message(default_exception_message()) {}
unexpected(bytes&& name, bytes&& exception_message) : abstract_command(std::move(name)), _exception_message(std::move(exception_message)) {}
unexpected(bytes&& name, sstring exception_message)
: abstract_command(std::move(name))
, _exception_message(exception_message)
{
}
virtual ~unexpected() {}
static shared_ptr<abstract_command> prepare(bytes&& name) {
return make_shared<unexpected>(std::move(name));
}
static shared_ptr<abstract_command> make_wrong_arguments_exception(bytes&& name, size_t except, size_t given) {
return make_shared<unexpected>(std::move(name), std::move(to_bytes(sprint("-ERR wrong number of arguments (given %ld, expected %ld)\r\n", given, except))));
return make_shared<unexpected>(std::move(name), sstring(sprint("-ERR wrong number of arguments (given %ld, expected %ld)\r\n", given, except)));
}
static shared_ptr<abstract_command> make_wrong_arguments_exception(bytes&& name, bytes&& message) {
return make_shared<unexpected>(std::move(name), std::move(message));
static shared_ptr<abstract_command> make_exception(bytes&& name, sstring message) {
return make_shared<unexpected>(std::move(name), message);
}
static shared_ptr<abstract_command> prepare(bytes&& name, bytes&& message) {
return make_shared<unexpected>(std::move(name), std::move(message));
static shared_ptr<abstract_command> prepare(bytes&& name, sstring message) {
return make_shared<unexpected>(std::move(name), message);
}
virtual future<redis_message> execute(service::storage_proxy&, db::consistency_level, db::timeout_clock::time_point, const timeout_config&, service::client_state&) override {
// return redis_message::make_exception(std::move(_exception_message));
return redis_message::ok();
return redis_message::make_exception(_exception_message);
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions redis/commands/zadd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ shared_ptr<abstract_command> zadd::prepare(service::storage_proxy& proxy, const
}
*/
if (is_number(req._args[i]) == false) {
//FIXME
return unexpected::make_wrong_arguments_exception(std::move(req._command), 3, req._args_count);
//value is not a valid float
return unexpected::make_exception(std::move(req._command), sstring("-ERR value is not a valid float\r\n"));
}
data.emplace_back(std::make_pair(req._args[i + 1], req._args[i]));
}
Expand Down
3 changes: 2 additions & 1 deletion redis/query_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
#include "redis/abstract_command.hh"
#include <seastar/core/metrics.hh>
#include "timeout_config.hh"

#include "log.hh"
namespace redis {

static logging::logger logging("redisqp");
distributed<query_processor> _the_query_processor;

query_processor::query_processor(service::storage_proxy& proxy, distributed<database>& db)
Expand Down

0 comments on commit 81af093

Please sign in to comment.