Scylla has advantage and amazing features. If Redis build on the top of Scylla, it has the above features automatically. It's achived great progress in cluster master managment, data persistence, failover and replication. The benefits to the users are easy to use and develop in their production environment, and taking avantages of Scylla. Using the Ragel to parse the Redis request, server abtains the command name and the parameters from the request, invokes the Scylla's internal API to read and write the data, then replies to client. Signed-off-by: Peng Jian, <pengjian.uestc@gmail.com>
106 lines
3.8 KiB
C++
106 lines
3.8 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/>.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "bytes.hh"
|
|
#include "seastar/core/sharded.hh"
|
|
#include "seastar/core/shared_ptr.hh"
|
|
#include <seastar/core/print.hh>
|
|
#include "seastar/core/scattered_message.hh"
|
|
#include "redis/exceptions.hh"
|
|
|
|
using namespace seastar;
|
|
|
|
namespace redis {
|
|
|
|
class redis_message final {
|
|
seastar::lw_shared_ptr<scattered_message<char>> _message;
|
|
public:
|
|
redis_message() = delete;
|
|
redis_message(const redis_message&) = delete;
|
|
redis_message& operator=(const redis_message&) = delete;
|
|
redis_message(redis_message&& o) noexcept : _message(std::move(o._message)) {}
|
|
redis_message(lw_shared_ptr<scattered_message<char>> m) noexcept : _message(m) {}
|
|
static future<redis_message> ok() {
|
|
auto m = make_lw_shared<scattered_message<char>> ();
|
|
m->append_static("+OK\r\n");
|
|
return make_ready_future<redis_message>(m);
|
|
}
|
|
static future<redis_message> pong() {
|
|
auto m = make_lw_shared<scattered_message<char>> ();
|
|
m->append_static("+PONG\r\n");
|
|
return make_ready_future<redis_message>(m);
|
|
}
|
|
static future<redis_message> err() {
|
|
auto m = make_lw_shared<scattered_message<char>> ();
|
|
m->append_static(":0\r\n");
|
|
return make_ready_future<redis_message>(m);
|
|
}
|
|
static future<redis_message> one() {
|
|
auto m = make_lw_shared<scattered_message<char>> ();
|
|
m->append_static(":1\r\n");
|
|
return make_ready_future<redis_message>(m);
|
|
}
|
|
static future<redis_message> zero() {
|
|
return err();
|
|
}
|
|
static future<redis_message> make_strings_result(bytes result) {
|
|
auto m = make_lw_shared<scattered_message<char>> ();
|
|
write_bytes(m, result);
|
|
return make_ready_future<redis_message>(m);
|
|
}
|
|
static future<redis_message> unknown(const bytes& name) {
|
|
return from_exception(make_message("-ERR unknown command '%s'\r\n", to_sstring(name)));
|
|
}
|
|
static future<redis_message> exception(const redis_exception& e) {
|
|
return from_exception(make_message("-ERR %s\r\n", e.what_message()));
|
|
}
|
|
static future<redis_message> make_exception(const sstring& em) {
|
|
return from_exception(make_message("-ERR %s\r\n", em));
|
|
}
|
|
inline lw_shared_ptr<scattered_message<char>> message() { return _message; }
|
|
private:
|
|
static future<redis_message> from_exception(sstring data) {
|
|
auto m = make_lw_shared<scattered_message<char>> ();
|
|
m->append(data);
|
|
return make_ready_future<redis_message>(m);
|
|
}
|
|
template<typename... Args>
|
|
static inline sstring make_message(const char* fmt, Args&&... args) noexcept {
|
|
try {
|
|
return sprint(fmt, std::forward<Args>(args)...);
|
|
} catch (...) {
|
|
return sstring();
|
|
}
|
|
}
|
|
static sstring to_sstring(const bytes& b) {
|
|
return sstring(reinterpret_cast<const char*>(b.data()), b.size());
|
|
}
|
|
static void write_bytes(lw_shared_ptr<scattered_message<char>> m, bytes& b) {
|
|
m->append(sstring(sprint("$%d\r\n", b.size())));
|
|
m->append(sstring(reinterpret_cast<const char*>(b.data()), b.size()));
|
|
m->append_static("\r\n");
|
|
}
|
|
};
|
|
|
|
}
|