Files
scylladb/redis/command_factory.cc
Etienne Adam 19683d04c6 redis: add hget and hset commands
hget and hset commands using hashes internally, thus
they are not using the existing write_strings() function.

Limitations:
 - hset only supports 3 params, instead of multiple field/value
list that is available in official redis-server.
 - hset should return 0 when the key and field already exists,
but I am not sure it's possible to retrieve this information
without doing read-before-write, which would not be atomic.

I factorized a bit the query_* functions to reduce duplication, but
I am not 100% sure of the naming, it may still be a bit confusing
between the schema used (strings, hashes) and the returned format
(currently only string but array should come later with hgetall).

Signed-off-by: Etienne Adam <etienne.adam@gmail.com>
Message-Id: <20200830190128.18534-1-etienne.adam@gmail.com>
2020-08-30 22:05:41 +03:00

59 lines
3.2 KiB
C++

/*
* Copyright (C) 2019 pengjian.uestc @ gmail.com
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#include "redis/command_factory.hh"
#include "service/storage_proxy.hh"
#include "redis/commands.hh"
#include "log.hh"
namespace redis {
static logging::logger logging("command_factory");
shared_ptr<abstract_command> command_factory::create(service::storage_proxy& proxy, request&& req)
{
static thread_local std::unordered_map<bytes, std::function<shared_ptr<abstract_command> (service::storage_proxy& proxy, request&& req)>> _commands =
{
{ "ping", [] (service::storage_proxy& proxy, request&& req) { return commands::ping::prepare(proxy, std::move(req)); } },
{ "select", [] (service::storage_proxy& proxy, request&& req) { return commands::select::prepare(proxy, std::move(req)); } },
{ "get", [] (service::storage_proxy& proxy, request&& req) { return commands::get::prepare(proxy, std::move(req)); } },
{ "exists", [] (service::storage_proxy& proxy, request&& req) { return commands::exists::prepare(proxy, std::move(req)); } },
{ "ttl", [] (service::storage_proxy& proxy, request&& req) { return commands::ttl::prepare(proxy, std::move(req)); } },
{ "strlen", [] (service::storage_proxy& proxy, request&& req) { return commands::strlen::prepare(proxy, std::move(req)); } },
{ "set", [] (service::storage_proxy& proxy, request&& req) { return commands::set::prepare(proxy, std::move(req)); } },
{ "setex", [] (service::storage_proxy& proxy, request&& req) { return commands::setex::prepare(proxy, std::move(req)); } },
{ "del", [] (service::storage_proxy& proxy, request&& req) { return commands::del::prepare(proxy, std::move(req)); } },
{ "echo", [] (service::storage_proxy& proxy, request&& req) { return commands::echo::prepare(proxy, std::move(req)); } },
{ "lolwut", [] (service::storage_proxy& proxy, request&& req) { return commands::lolwut::prepare(proxy, std::move(req)); } },
{ "hget", [] (service::storage_proxy& proxy, request&& req) { return commands::hget::prepare(proxy, std::move(req)); } },
{ "hset", [] (service::storage_proxy& proxy, request&& req) { return commands::hset::prepare(proxy, std::move(req)); } },
};
auto&& command = _commands.find(req._command);
if (command != _commands.end()) {
return (command->second)(proxy, std::move(req));
}
auto& b = req._command;
logging.error("receive unknown command = {}", sstring(reinterpret_cast<const char*>(b.data()), b.size()));
return commands::unknown::prepare(proxy, std::move(req));
}
}