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