123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- /**************************************************************************
- Copyright (c) 2017 sewenew
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- *************************************************************************/
- #include "command.h"
- #include <cassert>
- namespace sw {
- namespace redis {
- namespace cmd {
- // KEY commands.
- void restore(Connection &connection,
- const StringView &key,
- const StringView &val,
- long long ttl,
- bool replace) {
- CmdArgs args;
- args << "RESTORE" << key << ttl << val;
- if (replace) {
- args << "REPLACE";
- }
- connection.send(args);
- }
- // STRING commands.
- void bitop(Connection &connection,
- BitOp op,
- const StringView &destination,
- const StringView &key) {
- CmdArgs args;
- detail::set_bitop(args, op);
- args << destination << key;
- connection.send(args);
- }
- void set(Connection &connection,
- const StringView &key,
- const StringView &val,
- long long ttl,
- UpdateType type) {
- CmdArgs args;
- args << "SET" << key << val;
- if (ttl > 0) {
- args << "PX" << ttl;
- }
- detail::set_update_type(args, type);
- connection.send(args);
- }
- // LIST commands.
- void linsert(Connection &connection,
- const StringView &key,
- InsertPosition position,
- const StringView &pivot,
- const StringView &val) {
- std::string pos;
- switch (position) {
- case InsertPosition::BEFORE:
- pos = "BEFORE";
- break;
- case InsertPosition::AFTER:
- pos = "AFTER";
- break;
- default:
- assert(false);
- }
- connection.send("LINSERT %b %s %b %b",
- key.data(), key.size(),
- pos.c_str(),
- pivot.data(), pivot.size(),
- val.data(), val.size());
- }
- // GEO commands.
- void geodist(Connection &connection,
- const StringView &key,
- const StringView &member1,
- const StringView &member2,
- GeoUnit unit) {
- CmdArgs args;
- args << "GEODIST" << key << member1 << member2;
- detail::set_geo_unit(args, unit);
- connection.send(args);
- }
- void georadius_store(Connection &connection,
- const StringView &key,
- const std::pair<double, double> &loc,
- double radius,
- GeoUnit unit,
- const StringView &destination,
- bool store_dist,
- long long count) {
- CmdArgs args;
- args << "GEORADIUS" << key << loc.first << loc.second;
- detail::set_georadius_store_parameters(args,
- radius,
- unit,
- destination,
- store_dist,
- count);
- connection.send(args);
- }
- void georadius(Connection &connection,
- const StringView &key,
- const std::pair<double, double> &loc,
- double radius,
- GeoUnit unit,
- long long count,
- bool asc,
- bool with_coord,
- bool with_dist,
- bool with_hash) {
- CmdArgs args;
- args << "GEORADIUS" << key << loc.first << loc.second;
- detail::set_georadius_parameters(args,
- radius,
- unit,
- count,
- asc,
- with_coord,
- with_dist,
- with_hash);
- connection.send(args);
- }
- void georadiusbymember(Connection &connection,
- const StringView &key,
- const StringView &member,
- double radius,
- GeoUnit unit,
- long long count,
- bool asc,
- bool with_coord,
- bool with_dist,
- bool with_hash) {
- CmdArgs args;
- args << "GEORADIUSBYMEMBER" << key << member;
- detail::set_georadius_parameters(args,
- radius,
- unit,
- count,
- asc,
- with_coord,
- with_dist,
- with_hash);
- connection.send(args);
- }
- void georadiusbymember_store(Connection &connection,
- const StringView &key,
- const StringView &member,
- double radius,
- GeoUnit unit,
- const StringView &destination,
- bool store_dist,
- long long count) {
- CmdArgs args;
- args << "GEORADIUSBYMEMBER" << key << member;
- detail::set_georadius_store_parameters(args,
- radius,
- unit,
- destination,
- store_dist,
- count);
- connection.send(args);
- }
- // Stream commands.
- void xtrim(Connection &connection, const StringView &key, long long count, bool approx) {
- CmdArgs args;
- args << "XTRIM" << key << "MAXLEN";
- if (approx) {
- args << "~";
- }
- args << count;
- connection.send(args);
- }
- namespace detail {
- void set_bitop(CmdArgs &args, BitOp op) {
- args << "BITOP";
- switch (op) {
- case BitOp::AND:
- args << "AND";
- break;
- case BitOp::OR:
- args << "OR";
- break;
- case BitOp::XOR:
- args << "XOR";
- break;
- case BitOp::NOT:
- args << "NOT";
- break;
- default:
- throw Error("Unknown bit operations");
- }
- }
- void set_update_type(CmdArgs &args, UpdateType type) {
- switch (type) {
- case UpdateType::EXIST:
- args << "XX";
- break;
- case UpdateType::NOT_EXIST:
- args << "NX";
- break;
- case UpdateType::ALWAYS:
- // Do nothing.
- break;
- default:
- throw Error("Unknown update type");
- }
- }
- void set_aggregation_type(CmdArgs &args, Aggregation aggr) {
- args << "AGGREGATE";
- switch (aggr) {
- case Aggregation::SUM:
- args << "SUM";
- break;
- case Aggregation::MIN:
- args << "MIN";
- break;
- case Aggregation::MAX:
- args << "MAX";
- break;
- default:
- throw Error("Unknown aggregation type");
- }
- }
- void set_geo_unit(CmdArgs &args, GeoUnit unit) {
- switch (unit) {
- case GeoUnit::M:
- args << "m";
- break;
- case GeoUnit::KM:
- args << "km";
- break;
- case GeoUnit::MI:
- args << "mi";
- break;
- case GeoUnit::FT:
- args << "ft";
- break;
- default:
- throw Error("Unknown geo unit type");
- break;
- }
- }
- void set_georadius_store_parameters(CmdArgs &args,
- double radius,
- GeoUnit unit,
- const StringView &destination,
- bool store_dist,
- long long count) {
- args << radius;
- detail::set_geo_unit(args, unit);
- args << "COUNT" << count;
- if (store_dist) {
- args << "STOREDIST";
- } else {
- args << "STORE";
- }
- args << destination;
- }
- void set_georadius_parameters(CmdArgs &args,
- double radius,
- GeoUnit unit,
- long long count,
- bool asc,
- bool with_coord,
- bool with_dist,
- bool with_hash) {
- args << radius;
- detail::set_geo_unit(args, unit);
- if (with_coord) {
- args << "WITHCOORD";
- }
- if (with_dist) {
- args << "WITHDIST";
- }
- if (with_hash) {
- args << "WITHHASH";
- }
- args << "COUNT" << count;
- if (asc) {
- args << "ASC";
- } else {
- args << "DESC";
- }
- }
- }
- }
- }
- }
|