acl  3.5.3.0
redis_set.hpp
浏览该文件的文档.
1 #pragma once
2 #include "../acl_cpp_define.hpp"
3 #include <vector>
4 #include "../stdlib/string.hpp"
5 #include "redis_command.hpp"
6 
7 #if !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
8 
9 namespace acl
10 {
11 
12 class ACL_CPP_API redis_set : virtual public redis_command
13 {
14 public:
15  /**
16  * see redis_command::redis_command()
17  */
18  redis_set(void);
19 
20  /**
21  * see redis_command::redis_command(redis_client*)
22  */
23  redis_set(redis_client* conn);
24 
25  /**
26  * see redis_command::redis_command(redis_client_cluster*)
27  */
29 
31  redis_set(redis_client_cluster* cluster, size_t max_conns);
32 
34 
35  virtual ~redis_set(void);
36 
37  /////////////////////////////////////////////////////////////////////
38 
39  /**
40  * 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素
41  * 将被忽略;
42  * 1) 假如 key 不存在,则创建一个只包含 member 元素作成员的集合
43  * 2) 当 key 不是集合类型时,返回一个错误
44  * add one or more members to a set stored at a key
45  * 1) if the key doesn't exist, a new set by the key will be created,
46  * and add the members to the set
47  * 2) if the key exists and not a set's key, then error happened
48  * @param key {const char*} 集合对象的键
49  * the key of a set
50  * @param first_member {const char*} 第一个非 NULL 的成员
51  * the first member of a variable args which isn't NULL, the last
52  * arg of the args must be NULL indicating the end of args
53  * @return {int} 被添加到集合中的新元素的数量,不包括被忽略的元素
54  * the number of elements that were added to the set, not including
55  * all the elements already present into the set. -1 if error
56  * happened or it isn't a set stored by the key.
57  */
58  int sadd(const char* key, const char* first_member, ...);
59  int sadd(const char* key, const std::vector<const char*>& memsbers);
60  int sadd(const char* key, const std::vector<string>& members);
61  int sadd(const char* key, const char* argv[], size_t argc);
62  int sadd(const char* key, const char* argv[], const size_t lens[],
63  size_t argc);
64 
65  /**
66  * 从集合对象中随机移除并返回某个成员
67  * remove and get one member from the set
68  * @param key {const char*} 集合对象的键
69  * the key of the set
70  * @param buf {string&} 存储被移除的成员
71  * store the member removed from the set
72  * @return {bool} 当 key 不存在或 key 是空集时返回 false
73  * true if one member has been removed and got, false if the key
74  * doesn't exist or it isn't a set stored at the key.
75  */
76  bool spop(const char* key, string& buf);
77 
78  /**
79  * 获得集合对象中成员的数量
80  * get the number of members in a set stored at the key
81  * @param key {const char*} 集合对象的键
82  * the key of the set
83  * @return {int} 返回该集合对象中成员数量,含义如下:
84  * return int value as below:
85  * -1:出错或非集合对象
86  * error or it's not a set by the key
87  * 0:成员数量为空或该 key 不存在
88  * the set is empty or the key doesn't exist
89  * >0:成员数量非空
90  * the number of members in the set
91  */
92  int scard(const char* key);
93 
94  /**
95  * 返回集合 key 中的所有成员
96  * get all the members in a set stored at a key
97  * @param key {const char*} 集合对象的键值
98  * the key of the set
99  * @param members {std::vector<string>*} 非空时存储结果集
100  * if not NULL, it will store the members.
101  * @return {int} 结果集数量,返回 -1 表示出错或有一个 key 非集合对象
102  * the number of elements got, -1 if error happened or it't not
103  * a set by the key.
104  *
105  * 操作成功后可以通过以下任一方式获得数据
106  * if successul, one of below ways can be used to get the result:
107  * 1、在调用方法中传入非空的存储结果对象的地址
108  * the most easily way is to set a non-NULL result parameter
109  * for this function
110  * 2、基类方法 get_value 获得指定下标的元素数据
111  * call redis_command::result_value with the specified subscript
112  * 3、基类方法 get_child 获得指定下标的元素对象(redis_result),然后再通过
113  * redis_result::argv_to_string 方法获得元素数据
114  * get redis_result object with the given subscript, and get the
115  * element by redis_result::argv_to_string
116  * 4、基类方法 get_result 方法取得总结果集对象 redis_result,然后再通过
117  * redis_result::get_child 获得一个元素对象,然后再通过方式 2 中指定
118  * 的方法获得该元素的数据
119  * get redis_result object by redis_command::get_result, and get
120  * the first element by redis_result::get_child, then get the
121  * element by the way same as the way 2 above
122  * 5、基类方法 get_children 获得结果元素数组对象,再通过 redis_result 中
123  * 的方法 argv_to_string 从每一个元素对象中获得元素数据
124  * get child array by redis_command::get_children, and get the
125  * element from one of redis_result array by argv_to_string
126  */
127  int smembers(const char* key, std::vector<string>* members);
128 
129  /**
130  * 将 member 元素从 src 集合移动到 dst 集合
131  * move a member from one set to another
132  * @param src {const char*} 源集合对象的键值
133  * the source key of a set
134  * @param dst {const char*} 目标集合对象的键值
135  * the destination key of a set
136  * @param member {const char*} 源集合对象的成员
137  * the member in the source set
138  * @return {int} 返回值含义如下:
139  * return int value as below:
140  * -1:出错或源/目标对象有一个非集合对象
141  * error happened, or one of source and destination isn't a set
142  * 0:源对象不存在或成员在源对象中不存在
143  * the source set or the member doesn't exist
144  * 1:成功从源对象中将一个成员移动至目标对象中
145  * move successfully the member from source set to
146  * the destination set
147  */
148  int smove(const char* src, const char* dst, const char* member);
149  int smove(const char* src, const char* dst, const string& member);
150  int smove(const char* src, const char* dst,
151  const char* member, size_t len);
152 
153  /**
154  * 返回一个集合的全部成员,该集合是所有给定集合之间的差集
155  * return the members of the set resulting from the difference
156  * between the first set and all the successive sets.
157  * @param members {std::vector<string>*} 非空时存储结果集
158  * if not NULL, it will store the members.
159  * @param first_key {const char*} 第一个非空的集合对象 key
160  * the key of the first set in a variable sets list, the last one
161  * must be NULL indicating the end of the sets list.
162  * @return {int} 结果集数量,返回 -1 表示出错或有一个 key 非集合对象
163  * the number of elements got, -1 if error happened or it't not
164  * a set by the key.
165  * 操作成功后可以通过以下任一方式获得数据
166  * if successul, one of below ways can be used to get the result:
167  * 1、在调用方法中传入非空的存储结果对象的地址
168  * the most easily way is to set a non-NULL result parameter
169  * for this function
170  * 2、基类方法 get_value 获得指定下标的元素数据
171  * get the specified subscript's element by redis_command::get_value
172  * 3、基类方法 get_child 获得指定下标的元素对象(redis_result),然后再通过
173  * redis_result::argv_to_string 方法获得元素数据
174  * get redis_result object with the given subscript, and get the
175  * element by redis_result::argv_to_string
176  * 4、基类方法 get_result 方法取得总结果集对象 redis_result,然后再通过
177  * redis_result::get_child 获得一个元素对象,然后再通过方式 2 中指定
178  * 的方法获得该元素的数据
179  * get redis_result object by redis_command::get_result, and get
180  * the first element by redis_result::get_child, then get the
181  * element by the way same as the way 2 above.
182  * 5、基类方法 get_children 获得结果元素数组对象,再通过 redis_result 中
183  * 的方法 argv_to_string 从每一个元素对象中获得元素数据
184  * get child array by redis_command::get_children, and get the
185  * element from one of redis_result array by argv_to_string
186  */
187  int sdiff(std::vector<string>* members, const char* first_key, ...);
188  int sdiff(const std::vector<const char*>& keys,
189  std::vector<string>* members);
190  int sdiff(const std::vector<string>& keys,
191  std::vector<string>* members);
192 
193  /**
194  * 返回一个集合的全部成员,该集合是所有给定集合的交集
195  * return the members of a set resulting from the intersection of
196  * all the give sets.
197  * @param members {std::vector<string>*} 非空时存储结果集
198  * if not NULL, it will store the result
199  * @param first_key {const char*} 第一个集合对象 key(非NULL)
200  * the key of the first set in a variable set list, which isn't NULL,
201  * the last one must be NULL in the set list.
202  * @return {int} 结果集数量,返回 -1 表示出错或有一个 key 非集合对象
203  * return the number of the members, -1 if error happened or
204  * it't not a set by the key.
205  */
206  int sinter(std::vector<string>* members, const char* first_key, ...);
207  int sinter(const std::vector<const char*>& keys,
208  std::vector<string>* members);
209  int sinter(const std::vector<string>& keys,
210  std::vector<string>* members);
211 
212  /**
213  * 返回一个集合的全部成员,该集合是所有给定集合的并集
214  * return the members of a set resulting from the union of all the
215  * given sets.
216  * @param members {std::vector<string>*} 非空时存储结果集
217  * if not NULL, it will store the result
218  * @param first_key {const char*} 第一个集合对象 key(非NULL)
219  * the key of the first set in a variable set list, which isn't NULL,
220  * and the last arg must be NULL indicating the end of the set list.
221  * @return {int} 结果集数量,返回 -1 表示出错或有一个 key 非集合对象
222  * return the number of members, -1 if error happened or it's not
223  * a set by the key.
224  */
225  int sunion(std::vector<string>* members, const char* first_key, ...);
226  int sunion(const std::vector<const char*>& keys,
227  std::vector<string>* members);
228  int sunion(const std::vector<string>& keys,
229  std::vector<string>* members);
230 
231  /**
232  * 这个命令的作用和 SDIFF 类似,但它将结果保存到 dst 集合,而不是简单地返回结果集
233  * This command is equal to SDIFF, but instead of returning
234  * the resulting set, it is stored in destination.
235  * @param dst {const char*} 目标集合对象键值
236  * the key of the destination set
237  * @param first_key {const char*} 第一个非空的集合对象键值
238  * the key of the first set in a variable set list, which isn't NULL,
239  * and the last arg must be NULL indicating the end of the set list.
240  * @return {int} 结果集中的成员数量
241  * return the number of members, -1 if error happened or it's not
242  * a set by the key.
243  */
244  int sdiffstore(const char* dst, const char* first_key, ...);
245  int sdiffstore(const char* dst, const std::vector<const char*>& keys);
246  int sdiffstore(const char* dst, const std::vector<string>& keys);
247 
248  /**
249  * 这个命令类似于 SINTER 命令,但它将结果保存到 dst 集合,而不是简单地返回结果集
250  * This command is equal to SINTER, but instead of returning
251  * the resulting set, it is stored in destination.
252  * @param dst {const char*} 目标集合对象键值
253  * the key of the destination set
254  * @param first_key {const char*} 第一个非空的集合对象键值
255  * the key of the first set in a variable set list, which isn't NULL,
256  * and the last arg must be NULL indicating the end of the set list.
257  * @return {int} 结果集中的成员数量
258  * return the number of members, -1 if error happened or it's not
259  * a set by the key.
260  */
261  int sinterstore(const char* dst, const char* first_key, ...);
262  int sinterstore(const char* dst, const std::vector<const char*>& keys);
263  int sinterstore(const char* dst, const std::vector<string>& keys);
264 
265  /**
266  * 这个命令类似于 SUNION 命令,但它将结果保存到 dst 集合,而不是简单地返回结果集
267  * This command is equal to SUNION, but instead of returning
268  * the resulting set, it is stored in destination.
269  * @param dst {const char*} 目标集合对象键值
270  * the key of the destination set
271  * @param first_key {const char*} 第一个非空的集合对象键值
272  * the key of the first set in a variable set list, which isn't NULL,
273  * and the last arg must be NULL indicating the end of the set list.
274  * @return {int} 结果集中的成员数量
275  * return the number of members, -1 if error happened or it's not
276  * a set by the key.
277  */
278  int sunionstore(const char* dst, const char* first_key, ...);
279  int sunionstore(const char* dst, const std::vector<const char*>& keys);
280  int sunionstore(const char* dst, const std::vector<string>& keys);
281 
282  /**
283  * 判断 member 元素是否集合 key 的成员
284  * determine if a given value is a member of a set
285  * @param key {const char*} 集合对象的键值
286  * the key of a set
287  * @param member {const char*} 给定值
288  * the given value
289  * @return {bool} 返回 true 表示是,否则可能是因为不是或出错或该 key 对象
290  * 非集合对象
291  * true if the given is a member of the set, false if it's not a
292  * member of the set, or error, or it's not a set by the key.
293  */
294  bool sismember(const char* key, const char* member);
295  bool sismember(const char* key, const char* member, size_t len);
296 
297  /**
298  * 如果命令执行时,只提供了 key 参数,那么返回集合中的一个随机元素,如果还同时指定
299  * 了元素个数,则会返回一个不超过该个数限制的结果集
300  * get one or multiple memebers from a set
301  * @param key {const char*} 集合对象的键值
302  * the key of a set
303  * @param out 存储结果或结果集
304  * store the result
305  * @return {int} 结果的个数,为 -1 表示出错,0 表示没有成员
306  * the number of members, 0 if the set by the key is empty,
307  * -1 if error happened.
308  */
309  int srandmember(const char* key, string& out);
310  int srandmember(const char* key, size_t n, std::vector<string>& out);
311 
312  /**
313  * 移除集合 key 中的一个或多个 member 元素,不存在的 member 元素会被忽略
314  * Remove the specified members from the set stored at key. if the
315  * member doesn't exist, it will be ignored.
316  * @param key {const char*} 集合对象的键值
317  * the key of the set
318  * @param first_member {const char*} 需要被移除的成员列表的第一个非 NULL成员,
319  * 在变参的输入中需要将最后一个变参写 NULL
320  * the first non-NULL member to be removed in a variable member list,
321  * and the last one must be NULL indicating the end of the list.
322  * @retur {int} 被移除的成员元素的个数,当出错或非集合对象时返回 -1;当 key 不
323  * 存在或成员不存在时返回 0
324  * the number of members be removed, 0 if the set is empty or the
325  * key doesn't exist, -1 if error happened or it's not a set by key
326  */
327  int srem(const char* key, const char* first_member, ...);
328  int srem(const char* key, const std::vector<string>& members);
329  int srem(const char* key, const std::vector<const char*>& members);
330  int srem(const char* key, const char* members[],
331  size_t lens[], size_t argc);
332 
333  /**
334  * 命令用于迭代当前数据库中的数据库键
335  * scan the members in a set stored at key
336  * @param key {const char*} 哈希键值
337  * the key of a set
338  * @param cursor {int} 游标值,开始遍历时该值写 0
339  * the cursor value, which is 0 at begin
340  * @param out {std::vector<string>&} 存储结果集,内部以追加方式将本次遍历
341  * 结果集合添加进该数组中,为防止因总结果集过大导致该数组溢出,用户可在调用本
342  * 函数前后清理该数组对象
343  * store result in appending mode.
344  * @param pattern {const char*} 匹配模式,glob 风格,非空时有效
345  * match pattern, effective only on no-NULL
346  * @param count {const size_t*} 限定的结果集数量,非空指针时有效
347  * the max count of one scan process, effective only on no-NULL
348  * @return {int} 下一个游标位置,含义如下:
349  * return the next cursor position, as below:
350  * 0:遍历结束
351  * scan finish
352  * -1: 出错
353  * some error happened
354  * >0: 游标的下一个位置,即使这样,具体有多少结果还需要检查 out,因为有可能为空
355  * the next cursor postion to scan
356  */
357  int sscan(const char* key, int cursor, std::vector<string>& out,
358  const char* pattern = NULL, const size_t* count = NULL);
359 };
360 
361 } // namespace acl
362 
363 #endif // !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
#define ACL_CPP_DEPRECATED
Definition: atomic.hpp:86
#define ACL_CPP_API