forked from sonic-net/sonic-swss-common
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrediscommand.h
More file actions
83 lines (63 loc) · 2.29 KB
/
rediscommand.h
File metadata and controls
83 lines (63 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once
#include <cstring>
#include <utility>
#include <tuple>
#include <vector>
#include <string>
#include <stdexcept>
namespace swss {
typedef std::pair<std::string, std::string> FieldValueTuple;
#define fvField std::get<0>
#define fvValue std::get<1>
typedef std::tuple<std::string, std::string, std::vector<FieldValueTuple> > KeyOpFieldsValuesTuple;
#define kfvKey std::get<0>
#define kfvOp std::get<1>
#define kfvFieldsValues std::get<2>
class RedisCommand {
public:
RedisCommand();
~RedisCommand();
RedisCommand(RedisCommand& that) = delete;
RedisCommand& operator=(RedisCommand& that) = delete;
void format(const char *fmt, ...);
void formatArgv(int argc, const char **argv, const size_t *argvlen);
/* Format HMSET key multiple field value command */
#ifndef SWIG
__attribute__((deprecated))
#endif
void formatHMSET(const std::string &key,
const std::vector<FieldValueTuple> &values);
template<typename InputIterator>
void formatHMSET(const std::string &key,
InputIterator start, InputIterator stop);
/* Format HSET key field value command */
void formatHSET(const std::string& key, const std::string& field,
const std::string& value);
/* Format HGET key field command */
void formatHGET(const std::string& key, const std::string& field);
/* Format HDEL key field command */
void formatHDEL(const std::string& key, const std::string& field);
/* Format HDEL key multiple fields command */
void formatHDEL(const std::string& key, const std::vector<std::string>& fields);
/* Format EXPIRE key field command */
void formatEXPIRE(const std::string& key, const int64_t& ttl);
const char *c_str() const;
size_t length() const;
private:
char *temp;
};
template<typename InputIterator>
void RedisCommand::formatHMSET(const std::string &key,
InputIterator start, InputIterator stop)
{
if (start == stop) throw std::invalid_argument("empty values");
const char* cmd = "HMSET";
std::vector<const char*> args = { cmd, key.c_str() };
for (auto i = start; i != stop; i++)
{
args.push_back(fvField(*i).c_str());
args.push_back(fvValue(*i).c_str());
}
formatArgv((int)args.size(), args.data(), NULL);
}
}