1 //
2 // Command.cpp
3 //
4 // Library: Redis
5 // Package: Redis
6 // Module:  Command
7 //
8 // Implementation of the Command class.
9 //
10 // Copyright (c) 2015, Applied Informatics Software Engineering GmbH.
11 // and Contributors.
12 //
13 // SPDX-License-Identifier:	BSL-1.0
14 //
15 
16 
17 #include "Poco/Redis/Command.h"
18 #include "Poco/NumberFormatter.h"
19 
20 
21 namespace Poco {
22 namespace Redis {
23 
24 
Command(const std::string & command)25 Command::Command(const std::string& command): Array()
26 {
27 	add(command);
28 }
29 
30 
Command(const Command & copy)31 Command::Command(const Command& copy): Array(copy)
32 {
33 }
34 
35 
~Command()36 Command::~Command()
37 {
38 }
39 
40 
append(const std::string & key,const std::string & value)41 Command Command::append(const std::string& key, const std::string& value)
42 {
43 	Command cmd("APPEND");
44 
45 	cmd << key << value;
46 
47 	return cmd;
48 }
49 
50 
blpop(const StringVec & lists,Int64 timeout)51 Command Command::blpop(const StringVec& lists, Int64 timeout)
52 {
53 	Command cmd("BLPOP");
54 
55 	cmd << lists << NumberFormatter::format(timeout);
56 
57 	return cmd;
58 }
59 
60 
brpop(const StringVec & lists,Int64 timeout)61 Command Command::brpop(const StringVec& lists, Int64 timeout)
62 {
63 	Command cmd("BRPOP");
64 
65 	cmd << lists << NumberFormatter::format(timeout);
66 
67 	return cmd;
68 }
69 
70 
brpoplpush(const std::string & sourceList,const std::string & destinationList,Int64 timeout)71 Command Command::brpoplpush(const std::string& sourceList, const std::string& destinationList, Int64 timeout)
72 {
73 	Command cmd("BRPOPLPUSH");
74 
75 	cmd << sourceList << destinationList << NumberFormatter::format(timeout);
76 
77 	return cmd;
78 }
79 
80 
decr(const std::string & key,Int64 by)81 Command Command::decr(const std::string& key, Int64 by)
82 {
83 	Command cmd(by == 0 ? "DECR" : "DECRBY");
84 
85 	cmd << key;
86 	if ( by > 0 ) cmd << NumberFormatter::format(by);
87 
88 	return cmd;
89 }
90 
91 
del(const std::string & key)92 Command Command::del(const std::string& key)
93 {
94 	Command cmd("DEL");
95 
96 	cmd << key;
97 
98 	return cmd;
99 }
100 
101 
del(const StringVec & keys)102 Command Command::del(const StringVec& keys)
103 {
104 	Command cmd("DEL");
105 
106 	cmd << keys;
107 
108 	return cmd;
109 }
110 
111 
get(const std::string & key)112 Command Command::get(const std::string& key)
113 {
114 	Command cmd("GET");
115 
116 	cmd << key;
117 
118 	return cmd;
119 }
120 
121 
exists(const std::string & key)122 Command Command::exists(const std::string& key)
123 {
124 	Command cmd("EXISTS");
125 
126 	cmd << key;
127 
128 	return cmd;
129 }
130 
131 
hdel(const std::string & hash,const std::string & field)132 Command Command::hdel(const std::string& hash, const std::string& field)
133 {
134 	Command cmd("HDEL");
135 
136 	cmd << hash << field;
137 
138 	return cmd;
139 }
140 
141 
hdel(const std::string & hash,const StringVec & fields)142 Command Command::hdel(const std::string& hash, const StringVec& fields)
143 {
144 	Command cmd("HDEL");
145 
146 	cmd << hash << fields;
147 
148 	return cmd;
149 }
150 
151 
hexists(const std::string & hash,const std::string & field)152 Command Command::hexists(const std::string& hash, const std::string& field)
153 {
154 	Command cmd("HEXISTS");
155 
156 	cmd << hash << field;
157 
158 	return cmd;
159 }
160 
161 
hget(const std::string & hash,const std::string & field)162 Command Command::hget(const std::string& hash, const std::string& field)
163 {
164 	Command cmd("HGET");
165 
166 	cmd << hash << field;
167 
168 	return cmd;
169 }
170 
171 
hgetall(const std::string & hash)172 Command Command::hgetall(const std::string& hash)
173 {
174 	Command cmd("HGETALL");
175 
176 	cmd << hash;
177 
178 	return cmd;
179 }
180 
181 
hincrby(const std::string & hash,const std::string & field,Int64 by)182 Command Command::hincrby(const std::string& hash, const std::string& field, Int64 by)
183 {
184 	Command cmd("HINCRBY");
185 
186 	cmd << hash << field << NumberFormatter::format(by);
187 
188 	return cmd;
189 }
190 
191 
hkeys(const std::string & hash)192 Command Command::hkeys(const std::string& hash)
193 {
194 	Command cmd("HKEYS");
195 
196 	cmd << hash;
197 
198 	return cmd;
199 }
200 
201 
hlen(const std::string & hash)202 Command Command::hlen(const std::string& hash)
203 {
204 	Command cmd("HLEN");
205 
206 	cmd << hash;
207 
208 	return cmd;
209 }
210 
211 
hmget(const std::string & hash,const StringVec & fields)212 Command Command::hmget(const std::string& hash, const StringVec& fields)
213 {
214 	Command cmd("HMGET");
215 
216 	cmd << hash << fields;
217 
218 	return cmd;
219 }
220 
221 
hmset(const std::string & hash,std::map<std::string,std::string> & fields)222 Command Command::hmset(const std::string& hash, std::map<std::string, std::string>& fields)
223 {
224 	Command cmd("HMSET");
225 
226 	cmd << hash;
227 	for(std::map<std::string, std::string>::const_iterator it = fields.begin(); it != fields.end(); ++it)
228 	{
229 		cmd << it->first << it->second;
230 	}
231 
232 	return cmd;
233 }
234 
235 
hset(const std::string & hash,const std::string & field,const std::string & value,bool create)236 Command Command::hset(const std::string& hash, const std::string& field, const std::string& value, bool create)
237 {
238 	Command cmd(create ? "HSET" : "HSETNX");
239 
240 	cmd << hash << field << value;
241 
242 	return cmd;
243 }
244 
245 
hset(const std::string & hash,const std::string & field,Int64 value,bool create)246 Command Command::hset(const std::string& hash, const std::string& field, Int64 value, bool create)
247 {
248 	return hset(hash, field, NumberFormatter::format(value), create);
249 }
250 
251 
hstrlen(const std::string & hash,const std::string & field)252 Command Command::hstrlen(const std::string& hash, const std::string& field)
253 {
254 	Command cmd("HSTRLEN");
255 
256 	cmd << hash << field;
257 
258 	return cmd;
259 }
260 
261 
hvals(const std::string & hash)262 Command Command::hvals(const std::string& hash)
263 {
264 	Command cmd("HVALS");
265 
266 	cmd << hash;
267 
268 	return cmd;
269 }
270 
271 
incr(const std::string & key,Int64 by)272 Command Command::incr(const std::string& key, Int64 by)
273 {
274 	Command cmd(by == 0 ? "INCR" : "INCRBY");
275 
276 	cmd << key;
277 	if ( by > 0 ) cmd << NumberFormatter::format(by);
278 
279 	return cmd;
280 }
281 
282 
lindex(const std::string & list,Int64 index)283 Command Command::lindex(const std::string& list, Int64 index)
284 {
285 	Command cmd("LINDEX");
286 
287 	cmd << list << NumberFormatter::format(index);
288 
289 	return cmd;
290 }
291 
292 
linsert(const std::string & list,bool before,const std::string & reference,const std::string & value)293 Command Command::linsert(const std::string& list, bool before, const std::string& reference, const std::string& value)
294 {
295 	Command cmd("LINSERT");
296 
297 	cmd << list << (before ? "BEFORE" : "AFTER") << reference << value;
298 	return cmd;
299 }
300 
301 
llen(const std::string & list)302 Command Command::llen(const std::string& list)
303 {
304 	Command cmd("LLEN");
305 
306 	cmd << list;
307 
308 	return cmd;
309 }
310 
311 
lpop(const std::string & list)312 Command Command::lpop(const std::string& list)
313 {
314 	Command cmd("LPOP");
315 
316 	cmd << list;
317 
318 	return cmd;
319 }
320 
321 
lpush(const std::string & list,const std::string & value,bool create)322 Command Command::lpush(const std::string& list, const std::string& value, bool create)
323 {
324 	Command cmd(create ? "LPUSH" : "LPUSHX");
325 
326 	cmd << list << value;
327 
328 	return cmd;
329 }
330 
331 
lpush(const std::string & list,const StringVec & values,bool create)332 Command Command::lpush(const std::string& list, const StringVec& values, bool create)
333 {
334 	Command cmd(create ? "LPUSH" : "LPUSHX");
335 
336 	cmd << list << values;
337 
338 	return cmd;
339 }
340 
341 
lrange(const std::string & list,Int64 start,Int64 stop)342 Command Command::lrange(const std::string& list, Int64 start, Int64 stop)
343 {
344 	Command cmd("LRANGE");
345 
346 	cmd << list << NumberFormatter::format(start) << NumberFormatter::format(stop);
347 
348 	return cmd;
349 }
350 
351 
lrem(const std::string & list,Int64 count,const std::string & value)352 Command Command::lrem(const std::string& list, Int64 count, const std::string& value)
353 {
354 	Command cmd("LREM");
355 
356 	cmd << list << NumberFormatter::format(count) << value;
357 
358 	return cmd;
359 }
360 
361 
lset(const std::string & list,Int64 index,const std::string & value)362 Command Command::lset(const std::string& list, Int64 index, const std::string& value)
363 {
364 	Command cmd("LSET");
365 
366 	cmd << list << NumberFormatter::format(index) << value;
367 
368 	return cmd;
369 }
370 
371 
ltrim(const std::string & list,Int64 start,Int64 stop)372 Command Command::ltrim(const std::string& list, Int64 start, Int64 stop)
373 {
374 	Command cmd("LTRIM");
375 
376 	cmd << list << NumberFormatter::format(start) << NumberFormatter::format(stop);
377 
378 	return cmd;
379 }
380 
381 
mget(const StringVec & keys)382 Command Command::mget(const StringVec& keys)
383 {
384 	Command cmd("MGET");
385 
386 	cmd << keys;
387 
388 	return cmd;
389 }
390 
391 
mset(const std::map<std::string,std::string> & keyvalues,bool create)392 Command Command::mset(const std::map<std::string, std::string>& keyvalues, bool create)
393 {
394 	Command cmd(create ? "MSET" : "MSETNX");
395 
396 	for(std::map<std::string, std::string>::const_iterator it = keyvalues.begin(); it != keyvalues.end(); ++it)
397 	{
398 		cmd << it->first << it->second;
399 	}
400 
401 	return cmd;
402 }
403 
404 
sadd(const std::string & set,const std::string & value)405 Command Command::sadd(const std::string& set, const std::string& value)
406 {
407 	Command cmd("SADD");
408 
409 	cmd << set << value;
410 
411 	return cmd;
412 }
413 
414 
sadd(const std::string & set,const StringVec & values)415 Command Command::sadd(const std::string& set, const StringVec& values)
416 {
417 	Command cmd("SADD");
418 
419 	cmd << set << values;
420 
421 	return cmd;
422 }
423 
424 
scard(const std::string & set)425 Command Command::scard(const std::string& set)
426 {
427 	Command cmd("SCARD");
428 
429 	cmd << set;
430 
431 	return cmd;
432 }
433 
434 
sdiff(const std::string & set1,const std::string & set2)435 Command Command::sdiff(const std::string& set1, const std::string& set2)
436 {
437 	Command cmd("SDIFF");
438 
439 	cmd << set1 << set2;
440 
441 	return cmd;
442 }
443 
444 
sdiff(const std::string & set,const StringVec & sets)445 Command Command::sdiff(const std::string& set, const StringVec& sets)
446 {
447 	Command cmd("SDIFF");
448 
449 	cmd << set << sets;
450 
451 	return cmd;
452 }
453 
454 
sdiffstore(const std::string & set,const std::string & set1,const std::string & set2)455 Command Command::sdiffstore(const std::string& set, const std::string& set1, const std::string& set2)
456 {
457 	Command cmd("SDIFFSTORE");
458 
459 	cmd << set << set1 << set2;
460 
461 	return cmd;
462 }
463 
464 
sdiffstore(const std::string & set,const StringVec & sets)465 Command Command::sdiffstore(const std::string& set, const StringVec& sets)
466 {
467 	Command cmd("SDIFFSTORE");
468 
469 	cmd << set << sets;
470 
471 	return cmd;
472 }
473 
474 
set(const std::string & key,const std::string & value,bool overwrite,const Poco::Timespan & expireTime,bool create)475 Command Command::set(const std::string& key, const std::string& value, bool overwrite, const Poco::Timespan& expireTime, bool create)
476 {
477 	Command cmd("SET");
478 
479 	cmd << key << value;
480 	if (! overwrite) cmd << "NX";
481 	if (! create) cmd << "XX";
482 	if (expireTime.totalMicroseconds() > 0) cmd << "PX" << expireTime.totalMilliseconds();
483 
484 	return cmd;
485 }
486 
487 
set(const std::string & key,Int64 value,bool overwrite,const Poco::Timespan & expireTime,bool create)488 Command Command::set(const std::string& key, Int64 value, bool overwrite, const Poco::Timespan& expireTime, bool create)
489 {
490 	return set(key, NumberFormatter::format(value), overwrite, expireTime, create);
491 }
492 
493 
sinter(const std::string & set1,const std::string & set2)494 Command Command::sinter(const std::string& set1, const std::string& set2)
495 {
496 	Command cmd("SINTER");
497 
498 	cmd << set1 << set2;
499 
500 	return cmd;
501 }
502 
503 
sinter(const std::string & set,const StringVec & sets)504 Command Command::sinter(const std::string& set, const StringVec& sets)
505 {
506 	Command cmd("SINTER");
507 
508 	cmd << set << sets;
509 
510 	return cmd;
511 }
512 
513 
sinterstore(const std::string & set,const std::string & set1,const std::string & set2)514 Command Command::sinterstore(const std::string& set, const std::string& set1, const std::string& set2)
515 {
516 	Command cmd("SINTERSTORE");
517 
518 	cmd << set << set1 << set2;
519 
520 	return cmd;
521 }
522 
523 
sinterstore(const std::string & set,const StringVec & sets)524 Command Command::sinterstore(const std::string& set, const StringVec& sets)
525 {
526 	Command cmd("SINTERSTORE");
527 
528 	cmd << set << sets;
529 
530 	return cmd;
531 }
532 
533 
sismember(const std::string & set,const std::string & member)534 Command Command::sismember(const std::string& set, const std::string& member)
535 {
536 	Command cmd("SISMEMBER");
537 
538 	cmd << set << member;
539 
540 	return cmd;
541 }
542 
543 
smembers(const std::string & set)544 Command Command::smembers(const std::string& set)
545 {
546 	Command cmd("SMEMBERS");
547 
548 	cmd << set;
549 
550 	return cmd;
551 }
552 
553 
smove(const std::string & source,const std::string & destination,const std::string & member)554 Command Command::smove(const std::string& source, const std::string& destination, const std::string& member)
555 {
556 	Command cmd("SMOVE");
557 
558 	cmd << source  << destination << member;
559 
560 	return cmd;
561 }
562 
563 
spop(const std::string & set,Int64 count)564 Command Command::spop(const std::string& set, Int64 count)
565 {
566 	Command cmd("SPOP");
567 
568 	cmd << set;
569 	if( count != 0 ) cmd << NumberFormatter::format(count);
570 
571 	return cmd;
572 }
573 
574 
srandmember(const std::string & set,Int64 count)575 Command Command::srandmember(const std::string& set, Int64 count)
576 {
577 	Command cmd("SRANDMEMBER");
578 
579 	cmd << set;
580 	if( count != 0 ) cmd << NumberFormatter::format(count);
581 
582 	return cmd;
583 }
584 
585 
srem(const std::string & set1,const std::string & member)586 Command Command::srem(const std::string& set1, const std::string& member)
587 {
588 	Command cmd("SREM");
589 
590 	cmd << set1 << member;
591 
592 	return cmd;
593 }
594 
595 
srem(const std::string & set,const StringVec & members)596 Command Command::srem(const std::string& set, const StringVec& members)
597 {
598 	Command cmd("SREM");
599 
600 	cmd << set << members;
601 
602 	return cmd;
603 }
604 
605 
sunion(const std::string & set1,const std::string & set2)606 Command Command::sunion(const std::string& set1, const std::string& set2)
607 {
608 	Command cmd("SUNION");
609 
610 	cmd << set1 << set2;
611 
612 	return cmd;
613 }
614 
615 
sunion(const std::string & set,const StringVec & sets)616 Command Command::sunion(const std::string& set, const StringVec& sets)
617 {
618 	Command cmd("SUNION");
619 
620 	cmd << set << sets;
621 
622 	return cmd;
623 }
624 
625 
sunionstore(const std::string & set,const std::string & set1,const std::string & set2)626 Command Command::sunionstore(const std::string& set, const std::string& set1, const std::string& set2)
627 {
628 	Command cmd("SUNIONSTORE");
629 
630 	cmd << set << set1 << set2;
631 
632 	return cmd;
633 }
634 
635 
sunionstore(const std::string & set,const StringVec & sets)636 Command Command::sunionstore(const std::string& set, const StringVec& sets)
637 {
638 	Command cmd("SUNIONSTORE");
639 
640 	cmd << set << sets;
641 
642 	return cmd;
643 }
644 
645 
rename(const std::string & key,const std::string & newName,bool overwrite)646 Command Command::rename(const std::string& key, const std::string& newName, bool overwrite)
647 {
648 	Command cmd(overwrite ? "RENAME" : "RENAMENX");
649 
650 	cmd << key << newName;
651 
652 	return cmd;
653 }
654 
655 
rpop(const std::string & list)656 Command Command::rpop(const std::string& list)
657 {
658 	Command cmd("RPOP");
659 
660 	cmd << list;
661 
662 	return cmd;
663 }
664 
665 
rpoplpush(const std::string & sourceList,const std::string & destinationList)666 Command Command::rpoplpush(const std::string& sourceList, const std::string& destinationList)
667 {
668 	Command cmd("RPOPLPUSH");
669 
670 	cmd << sourceList << destinationList;
671 
672 	return cmd;
673 }
674 
675 
rpush(const std::string & list,const std::string & value,bool create)676 Command Command::rpush(const std::string& list, const std::string& value, bool create)
677 {
678 	Command cmd(create ? "RPUSH" : "RPUSHX");
679 
680 	cmd << list << value;
681 
682 	return cmd;
683 }
684 
685 
rpush(const std::string & list,const StringVec & values,bool create)686 Command Command::rpush(const std::string& list, const StringVec& values, bool create)
687 {
688 	Command cmd(create ? "RPUSH" : "RPUSHX");
689 
690 	cmd << list << values;
691 
692 	return cmd;
693 }
694 
695 
expire(const std::string & key,Int64 seconds)696 Command Command::expire(const std::string& key, Int64 seconds)
697 {
698 	Command cmd("EXPIRE");
699 
700 	cmd << key << NumberFormatter::format(seconds);
701 
702 	return cmd;
703 }
704 
705 
ping()706 Command Command::ping()
707 {
708 	Command cmd("PING");
709 
710 	return cmd;
711 }
712 
713 
multi()714 Command Command::multi()
715 {
716 	Command cmd("MULTI");
717 
718 	return cmd;
719 }
720 
721 
exec()722 Command Command::exec()
723 {
724 	Command cmd("EXEC");
725 
726 	return cmd;
727 }
728 
729 
discard()730 Command Command::discard()
731 {
732 	Command cmd("DISCARD");
733 
734 	return cmd;
735 }
736 
737 
738 } } // namespace Poco::Redis
739