Skip to content

Commit

Permalink
add built-in commands for AsyncRedisCluster
Browse files Browse the repository at this point in the history
  • Loading branch information
sewenew committed Jul 24, 2021
1 parent 5fd7575 commit 9b95986
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 337 deletions.
55 changes: 4 additions & 51 deletions src/sw/redis++/async_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,10 @@ class AsyncConnection : public std::enable_shared_from_this<AsyncConnection> {
template <typename Result, typename ResultParser>
Future<Result> send(FormattedCommand cmd);

template <typename Result, typename ...Args>
Future<Result> send(const std::shared_ptr<AsyncShardsPool> &pool,
const StringView &key,
const char *format, Args &&...args);

template <typename Result, typename ResultParser = DefaultResultParser<Result>>
Future<Result> send(const std::shared_ptr<AsyncShardsPool> &pool,
const StringView &key,
int argc, const char **argv, const std::size_t *argv_len);

template <typename Result, typename ResultParser = DefaultResultParser<Result>>
template <typename Result, typename ResultParser>
Future<Result> send(const std::shared_ptr<AsyncShardsPool> &pool,
const StringView &key,
CmdArgs &args);
FormattedCommand cmd);

void send(AsyncEventUPtr event);

Expand Down Expand Up @@ -163,11 +153,6 @@ class AsyncConnection : public std::enable_shared_from_this<AsyncConnection> {

void _fail_events(std::exception_ptr err);

template <typename Result, typename ResultParser>
Future<Result> _send(const std::shared_ptr<AsyncShardsPool> &pool,
const StringView &key,
char *data, int len);

static void _clean_async_context(void *data);

struct AsyncContextDeleter {
Expand Down Expand Up @@ -367,36 +352,6 @@ class ClusterEvent : public CommandEvent<Result, ResultParser> {
template <typename Result, typename ResultParser>
using ClusterEventUPtr = std::unique_ptr<ClusterEvent<Result, ResultParser>>;

template <typename Result, typename ...Args>
Future<Result> AsyncConnection::send(const std::shared_ptr<AsyncShardsPool> &pool,
const StringView &key,
const char *format, Args &&...args) {
char *data = nullptr;
auto len = redisFormatCommand(&data, format, std::forward<Args>(args)...);

return _send<Result, DefaultResultParser<Result>>(pool, key, data, len);
}

template <typename Result, typename ResultParser>
Future<Result> AsyncConnection::send(const std::shared_ptr<AsyncShardsPool> &pool,
const StringView &key,
int argc, const char **argv, const std::size_t *argv_len) {
char *data = nullptr;
auto len = redisFormatCommandArgv(&data, argc, argv, argv_len);

return _send<Result, ResultParser>(pool, key, data, len);
}

template <typename Result, typename ResultParser>
Future<Result> AsyncConnection::send(const std::shared_ptr<AsyncShardsPool> &pool,
const StringView &key,
CmdArgs &args) {
char *data = nullptr;
auto len = redisFormatCommandArgv(&data, args.size(), args.argv(), args.argv_len());

return _send<Result, ResultParser>(pool, key, data, len);
}

template <typename Result, typename ResultParser>
Future<Result> AsyncConnection::send(FormattedCommand cmd) {
auto event = CommandEventUPtr<Result, ResultParser>(
Expand All @@ -416,11 +371,9 @@ Future<Result> AsyncConnection::send(FormattedCommand cmd) {
}

template <typename Result, typename ResultParser>
Future<Result> AsyncConnection::_send(const std::shared_ptr<AsyncShardsPool> &pool,
Future<Result> AsyncConnection::send(const std::shared_ptr<AsyncShardsPool> &pool,
const StringView &key,
char *data, int len) {
FormattedCommand cmd(data, len);

FormattedCommand cmd) {
auto event = ClusterEventUPtr<Result, ResultParser>(
new ClusterEvent<Result, ResultParser>(pool, key, std::move(cmd)));

Expand Down
1 change: 1 addition & 0 deletions src/sw/redis++/async_redis++.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
#define SEWENEW_REDISPLUSPLUS_ASYNC_REDISPLUSPLUS_H

#include "async_redis.h"
#include "async_redis_cluster.h"

#endif // end SEWENEW_REDISPLUSPLUS_ASYNC_REDISPLUSPLUS_H
4 changes: 2 additions & 2 deletions src/sw/redis++/async_redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -769,12 +769,12 @@ class AsyncRedis {

template <typename Result, typename ResultParser, typename Formatter, typename ...Args>
Future<Result> _command_with_parser(Formatter formatter, Args &&...args) {
auto formated_cmd = formatter(std::forward<Args>(args)...);
auto formatted_cmd = formatter(std::forward<Args>(args)...);

assert(_pool);
SafeAsyncConnection connection(*_pool);

return connection.connection().send<Result, ResultParser>(std::move(formated_cmd));
return connection.connection().send<Result, ResultParser>(std::move(formatted_cmd));
}

EventLoopSPtr _loop;
Expand Down
33 changes: 0 additions & 33 deletions src/sw/redis++/async_redis_cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@
#include "async_redis_cluster.h"
#include <cassert>

namespace {

struct SetResultParser {
bool operator()(redisReply &reply) const {
sw::redis::reply::rewrite_set_reply(reply);
return sw::redis::reply::parse<bool>(reply);
}
};

}

namespace sw {

namespace redis {
Expand All @@ -43,28 +32,6 @@ AsyncRedisCluster::AsyncRedisCluster(const ConnectionOptions &opts,
_pool = std::make_shared<AsyncShardsPool>(_loop, pool_opts, opts, role);
}

Future<OptionalString> AsyncRedisCluster::get(const StringView &key) {
return _command<OptionalString>(key, "GET %b", key.data(), key.size());
}

/*
Future<bool> AsyncRedisCluster::set(const StringView &key,
const StringView &val,
const std::chrono::milliseconds &ttl,
UpdateType type) {
CmdArgs args;
args << "SET" << key << val;
if (ttl > std::chrono::milliseconds(0)) {
args << "PX" << ttl.count();
}
cmd::detail::set_update_type(args, type);
return _command_with_parser<bool, SetResultParser>(key, args);
}
*/

}

}
Loading

0 comments on commit 9b95986

Please sign in to comment.