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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472
|
/* -*- Mode: C; tab-width: 4 -*- */
/*
+----------------------------------------------------------------------+
| Copyright (c) 1997-2009 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Original Author: Michael Grunder <michael.grunder@gmail.com |
| Maintainer: Nicolas Favre-Felix <n.favre-felix@owlient.eu> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "redis_commands.h"
#include "php_network.h"
#ifndef PHP_WIN32
#include <netinet/tcp.h> /* TCP_KEEPALIVE */
#else
#include <winsock.h>
#endif
#include <zend_exceptions.h>
/* Georadius sort type */
typedef enum geoSortType {
SORT_NONE,
SORT_ASC,
SORT_DESC
} geoSortType;
/* Georadius store type */
typedef enum geoStoreType {
STORE_NONE,
STORE_COORD,
STORE_DIST
} geoStoreType;
/* Georadius options structure */
typedef struct geoOptions {
int withcoord;
int withdist;
int withhash;
long count;
zend_bool any;
geoSortType sort;
geoStoreType store;
zend_string *key;
} geoOptions;
typedef struct redisLcsOptions {
zend_bool len;
zend_bool idx;
zend_long minmatchlen;
zend_bool withmatchlen;
} redisLcsOptions;
typedef struct redisRestoreOptions {
zend_bool replace;
zend_bool absttl;
zend_long idletime;
zend_long freq;
} redisRestoreOptions;
#define REDIS_ZCMD_HAS_DST_KEY (1 << 0)
#define REDIS_ZCMD_HAS_WITHSCORES (1 << 1)
#define REDIS_ZCMD_HAS_BY_LEX_SCORE (1 << 2)
#define REDIS_ZCMD_HAS_REV (1 << 3)
#define REDIS_ZCMD_HAS_LIMIT (1 << 4)
#define REDIS_ZCMD_INT_RANGE (1 << 5)
#define REDIS_ZCMD_HAS_AGGREGATE (1 << 6)
/* ZRANGE, ZRANGEBYSCORE, ZRANGESTORE options */
typedef struct redisZcmdOptions {
zend_bool withscores;
zend_bool byscore;
zend_bool bylex;
zend_bool rev;
zend_string *aggregate;
struct {
zend_bool enabled;
zend_long offset;
zend_long count;
} limit;
} redisZcmdOptions;
/* Local passthrough macro for command construction. Given that these methods
* are generic (so they work whether the caller is Redis or RedisCluster) we
* will always have redis_sock, slot*, and */
#define REDIS_CMD_SPPRINTF(ret, kw, fmt, ...) \
redis_spprintf(redis_sock, slot, ret, kw, fmt, ##__VA_ARGS__)
/* Generic commands based on method signature and what kind of things we're
* processing. Lots of Redis commands take something like key, value, or
* key, value long. Each unique signature like this is written only once */
/* A command that takes no arguments */
int redis_empty_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "");
return SUCCESS;
}
/* Helper to construct a raw command. Given that the cluster and non cluster
* versions are different (RedisCluster needs an additional argument to direct
* the command) we take the start of our array and count */
int redis_build_raw_cmd(zval *z_args, int argc, char **cmd, int *cmd_len)
{
smart_string cmdstr = {0};
int i;
/* Make sure our first argument is a string */
if (Z_TYPE(z_args[0]) != IS_STRING) {
php_error_docref(NULL, E_WARNING,
"When sending a 'raw' command, the first argument must be a string!");
return FAILURE;
}
/* Initialize our command string */
redis_cmd_init_sstr(&cmdstr, argc-1, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
for (i = 1; i < argc; i++) {
switch (Z_TYPE(z_args[i])) {
case IS_STRING:
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[i]),
Z_STRLEN(z_args[i]));
break;
case IS_LONG:
redis_cmd_append_sstr_long(&cmdstr,Z_LVAL(z_args[i]));
break;
case IS_DOUBLE:
redis_cmd_append_sstr_dbl(&cmdstr,Z_DVAL(z_args[i]));
break;
default:
php_error_docref(NULL, E_WARNING,
"Raw command arguments must be scalar values!");
efree(cmdstr.c);
return FAILURE;
}
}
/* Push command and length to caller */
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
smart_string *
redis_build_script_cmd(smart_string *cmd, int argc, zval *z_args)
{
int i;
zend_string *zstr;
if (Z_TYPE(z_args[0]) != IS_STRING) {
return NULL;
}
// Branch based on the directive
if (zend_string_equals_literal_ci(Z_STR(z_args[0]), "kill")) {
// Simple SCRIPT_KILL command
REDIS_CMD_INIT_SSTR_STATIC(cmd, argc, "SCRIPT");
redis_cmd_append_sstr(cmd, ZEND_STRL("KILL"));
} else if (zend_string_equals_literal_ci(Z_STR(z_args[0]), "flush")) {
// Simple SCRIPT FLUSH [ASYNC | SYNC]
if (argc > 1 && (
Z_TYPE(z_args[1]) != IS_STRING || (
!zend_string_equals_literal_ci(Z_STR(z_args[1]), "sync") &&
!zend_string_equals_literal_ci(Z_STR(z_args[1]), "async")
)
)) {
return NULL;
}
REDIS_CMD_INIT_SSTR_STATIC(cmd, argc, "SCRIPT");
redis_cmd_append_sstr(cmd, ZEND_STRL("FLUSH"));
if (argc > 1) {
redis_cmd_append_sstr(cmd, Z_STRVAL(z_args[1]), Z_STRLEN(z_args[1]));
}
} else if (zend_string_equals_literal_ci(Z_STR(z_args[0]), "load")) {
// Make sure we have a second argument, and it's not empty. If it is
// empty, we can just return an empty array (which is what Redis does)
if (argc < 2 || Z_TYPE(z_args[1]) != IS_STRING || Z_STRLEN(z_args[1]) < 1) {
return NULL;
}
// Format our SCRIPT LOAD command
REDIS_CMD_INIT_SSTR_STATIC(cmd, argc, "SCRIPT");
redis_cmd_append_sstr(cmd, ZEND_STRL("LOAD"));
redis_cmd_append_sstr(cmd, Z_STRVAL(z_args[1]), Z_STRLEN(z_args[1]));
} else if (zend_string_equals_literal_ci(Z_STR(z_args[0]), "exists")) {
// Make sure we have a second argument
if (argc < 2) {
return NULL;
}
/* Construct our SCRIPT EXISTS command */
REDIS_CMD_INIT_SSTR_STATIC(cmd, argc, "SCRIPT");
redis_cmd_append_sstr(cmd, ZEND_STRL("EXISTS"));
for (i = 1; i < argc; ++i) {
zstr = zval_get_string(&z_args[i]);
redis_cmd_append_sstr(cmd, ZSTR_VAL(zstr), ZSTR_LEN(zstr));
zend_string_release(zstr);
}
} else {
/* Unknown directive */
return NULL;
}
return cmd;
}
/* Command that takes one optional string */
int redis_opt_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char *kw,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *arg = NULL;
size_t arglen;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &arg, &arglen) == FAILURE) {
return FAILURE;
}
if (arg != NULL) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "s", arg, arglen);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "");
}
return SUCCESS;
}
/* Generic command where we just take a string and do nothing to it*/
int redis_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char *kw,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *arg;
size_t arg_len;
// Parse args
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len)
==FAILURE)
{
return FAILURE;
}
// Build the command without molesting the string
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "s", arg, arg_len);
return SUCCESS;
}
/* Key, long, zval (serialized) */
int redis_key_long_val_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key = NULL;
size_t key_len;
zend_long expire;
zval *z_val;
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_STRING(key, key_len)
Z_PARAM_LONG(expire)
Z_PARAM_ZVAL(z_val)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "klv", key, key_len, expire, z_val);
return SUCCESS;
}
/* Generic key, long, string (unserialized) */
int redis_key_long_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key, *val;
size_t key_len, val_len;
zend_long lval;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &key, &key_len,
&lval, &val, &val_len) == FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kds", key, key_len, (int)lval, val, val_len);
return SUCCESS;
}
/* Generic command construction when we just take a key and value */
int redis_kv_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key;
size_t key_len;
zval *z_val;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &key, &key_len,
&z_val) == FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kv", key, key_len, z_val);
return SUCCESS;
}
/* Generic command that takes a key and an unserialized value */
int redis_key_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key, *val;
size_t key_len, val_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &key, &key_len,
&val, &val_len) == FAILURE)
{
return FAILURE;
}
// Construct command
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "ks", key, key_len, val, val_len);
return SUCCESS;
}
/* Key, string, string without serialization (ZCOUNT, ZREMRANGEBYSCORE) */
int redis_key_str_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *k, *v1, *v2;
size_t klen, v1len, v2len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss", &k, &klen,
&v1, &v1len, &v2, &v2len) == FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kss", k, klen, v1, v1len, v2, v2len);
// Success!
return SUCCESS;
}
/* Generic command that takes two keys */
int redis_key_key_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zend_string *key1 = NULL, *key2 = NULL;
smart_string cmdstr = {0};
short slot2;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(key1)
Z_PARAM_STR(key2)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
redis_cmd_init_sstr(&cmdstr, 2, kw, strlen(kw));
redis_cmd_append_sstr_key_zstr(&cmdstr, key1, redis_sock, slot);
redis_cmd_append_sstr_key_zstr(&cmdstr, key2, redis_sock, slot ? &slot2 : NULL);
if (slot && *slot != slot2) {
php_error_docref(0, E_WARNING, "Keys don't hash to the same slot");
smart_string_free(&cmdstr);
return FAILURE;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* Generic command construction where we take a key and a long */
int redis_key_long_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zend_string *key = NULL;
zend_long lval = 0;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(key)
Z_PARAM_LONG(lval)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kl", ZSTR_VAL(key), ZSTR_LEN(key), lval);
return SUCCESS;
}
/* long, long */
int redis_long_long_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zend_long l1 = 0, l2 = 0;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_LONG(l1)
Z_PARAM_LONG(l2)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "ll", l1, l2);
return SUCCESS;
}
/* key, long, long */
int redis_key_long_long_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key;
size_t key_len;
zend_long val1, val2;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sll", &key, &key_len,
&val1, &val2) == FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kll", key, key_len, val1, val2);
return SUCCESS;
}
/* Generic command where we take a single key */
int redis_key_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key;
size_t key_len;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(key, key_len);
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "k", key, key_len);
return SUCCESS;
}
int
redis_failover_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
int argc;
smart_string cmdstr = {0};
zend_bool abort = 0, force = 0;
zend_long timeout = 0, port = 0;
zend_string *zkey, *host = NULL;
zval *z_to = NULL, *z_ele;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|a!bl",
&z_to, &abort, &timeout) == FAILURE)
{
return FAILURE;
}
if (z_to != NULL) {
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(z_to), zkey, z_ele) {
if (zkey != NULL) {
ZVAL_DEREF(z_ele);
if (zend_string_equals_literal_ci(zkey, "host")) {
host = zval_get_string(z_ele);
} else if (zend_string_equals_literal_ci(zkey, "port")) {
port = zval_get_long(z_ele);
} else if (zend_string_equals_literal_ci(zkey, "force")) {
force = zval_is_true(z_ele);
}
}
} ZEND_HASH_FOREACH_END();
if (!host || !port) {
php_error_docref(NULL, E_WARNING, "host and port must be provided!");
if (host) zend_string_release(host);
return FAILURE;
}
}
argc = (host && port ? 3 + force : 0) + abort + (timeout > 0 ? 2 : 0);
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "FAILOVER");
if (host && port) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "TO");
redis_cmd_append_sstr_zstr(&cmdstr, host);
redis_cmd_append_sstr_int(&cmdstr, port);
if (force) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "FORCE");
}
zend_string_release(host);
}
if (abort) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "ABORT");
}
if (timeout > 0) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "TIMEOUT");
redis_cmd_append_sstr_long(&cmdstr, timeout);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int redis_flush_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
zend_bool sync = 0;
zend_bool is_null = 1;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL_OR_NULL(sync, is_null)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
redis_cmd_init_sstr(&cmdstr, !is_null, kw, strlen(kw));
if (!is_null) {
ZEND_ASSERT(sync == 0 || sync == 1);
if (sync == 0) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "ASYNC");
} else {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "SYNC");
}
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* Generic command where we take a key and a double */
int redis_key_dbl_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key;
size_t key_len;
double val;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sd", &key, &key_len,
&val) == FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kf", key, key_len, val);
return SUCCESS;
}
/* Generic to construct SCAN and variant commands */
int redis_fmt_scan_cmd(char **cmd, REDIS_SCAN_TYPE type, char *key, int key_len,
uint64_t it, char *pat, int pat_len, long count)
{
static char *kw[] = {"SCAN","SSCAN","HSCAN","ZSCAN"};
int argc;
smart_string cmdstr = {0};
// Figure out our argument count
argc = 1 + (type!=TYPE_SCAN) + (pat_len>0?2:0) + (count>0?2:0);
redis_cmd_init_sstr(&cmdstr, argc, kw[type], strlen(kw[type]));
// Append our key if it's not a regular SCAN command
if (type != TYPE_SCAN) {
redis_cmd_append_sstr(&cmdstr, key, key_len);
}
// Append cursor
redis_cmd_append_sstr_u64(&cmdstr, it);
// Append count if we've got one
if (count) {
redis_cmd_append_sstr(&cmdstr, ZEND_STRL("COUNT"));
redis_cmd_append_sstr_long(&cmdstr, count);
}
// Append pattern if we've got one
if (pat_len) {
redis_cmd_append_sstr(&cmdstr, ZEND_STRL("MATCH"));
redis_cmd_append_sstr(&cmdstr,pat,pat_len);
}
// Push command to the caller, return length
*cmd = cmdstr.c;
return cmdstr.len;
}
void redis_get_zcmd_options(redisZcmdOptions *dst, zval *src, int flags) {
zval *zv, *zoff, *zcnt;
zend_string *key;
ZEND_ASSERT(dst != NULL);
memset(dst, 0, sizeof(*dst));
if (src == NULL)
return;
if (Z_TYPE_P(src) != IS_ARRAY) {
if (Z_TYPE_P(src) == IS_TRUE && (flags & REDIS_ZCMD_HAS_WITHSCORES))
dst->withscores = 1;
return;
}
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(src), key, zv) {
ZVAL_DEREF(zv);
if (key) {
if ((flags & REDIS_ZCMD_HAS_WITHSCORES) && zend_string_equals_literal_ci(key, "WITHSCORES"))
dst->withscores = zval_is_true(zv);
else if ((flags & REDIS_ZCMD_HAS_LIMIT) && zend_string_equals_literal_ci(key, "LIMIT") &&
Z_TYPE_P(zv) == IS_ARRAY)
{
if ((zoff = zend_hash_index_find(Z_ARRVAL_P(zv), 0)) != NULL &&
(zcnt = zend_hash_index_find(Z_ARRVAL_P(zv), 1)) != NULL)
{
dst->limit.enabled = 1;
dst->limit.offset = zval_get_long(zoff);
dst->limit.count = zval_get_long(zcnt);
} else {
php_error_docref(NULL, E_WARNING, "LIMIT offset and count must be an array with twe elements");
}
} else if ((flags & REDIS_ZCMD_HAS_AGGREGATE && zend_string_equals_literal_ci(key, "AGGREGATE")) &&
Z_TYPE_P(zv) == IS_STRING)
{
if (Z_TYPE_P(zv) != IS_STRING || (!zend_string_equals_literal_ci(Z_STR_P(zv), "SUM") &&
!zend_string_equals_literal_ci(Z_STR_P(zv), "MIN") &&
!zend_string_equals_literal_ci(Z_STR_P(zv), "MAX")))
{
php_error_docref(NULL, E_WARNING, "Valid AGGREGATE options are 'SUM', 'MIN', or 'MAX'");
} else {
dst->aggregate = Z_STR_P(zv);
}
}
} else if (Z_TYPE_P(zv) == IS_STRING) {
key = Z_STR_P(zv);
if ((flags & REDIS_ZCMD_HAS_BY_LEX_SCORE) && zend_string_equals_literal_ci(key, "BYSCORE"))
dst->byscore = 1, dst->bylex = 0;
else if ((flags & REDIS_ZCMD_HAS_BY_LEX_SCORE) && zend_string_equals_literal_ci(key, "BYLEX"))
dst->bylex = 1, dst->byscore = 0;
else if ((flags & REDIS_ZCMD_HAS_REV) && zend_string_equals_literal_ci(key, "REV"))
dst->rev = 1;
else if ((flags & REDIS_ZCMD_HAS_WITHSCORES && zend_string_equals_literal_ci(key, "WITHSCORES")))
dst->withscores = 1;
}
} ZEND_HASH_FOREACH_END();
}
// + ZRANGE key start stop [BYSCORE | BYLEX] [REV] [LIMIT offset count] [WITHSCORES]
// + ZRANGESTORE dst src min max [BYSCORE | BYLEX] [REV] [LIMIT offset count]
// + ZREVRANGE key start stop [WITHSCORES]
// + ZRANGEBYSCORE key min max [LIMIT offset count] [WITHSCORES]
// + ZREVRANGEBYSCORE key max min [LIMIT offset count] [WITHSCORES]
// - ZRANGEBYLEX key min max [LIMIT offset count]
// - ZREVRANGEBYLEX key max min [LIMIT offset count]
// - ZDIFF [WITHSCORES]
// - ZUNION [WITHSCORES] [AGGREGATE X]
// - ZINTER [WITHSCORES] [AGGREGATE X]
static int redis_get_zcmd_flags(const char *kw) {
size_t len = strlen(kw);
if (REDIS_STRICMP_STATIC(kw, len, "ZRANGESTORE")) {
return REDIS_ZCMD_HAS_DST_KEY |
REDIS_ZCMD_HAS_WITHSCORES |
REDIS_ZCMD_HAS_BY_LEX_SCORE |
REDIS_ZCMD_HAS_REV |
REDIS_ZCMD_HAS_LIMIT;
} else if (REDIS_STRICMP_STATIC(kw, len, "ZRANGE")) {
return REDIS_ZCMD_HAS_WITHSCORES |
REDIS_ZCMD_HAS_BY_LEX_SCORE |
REDIS_ZCMD_HAS_REV |
REDIS_ZCMD_HAS_LIMIT;
} else if (REDIS_STRICMP_STATIC(kw, len, "ZREVRANGE")) {
return REDIS_ZCMD_HAS_WITHSCORES |
REDIS_ZCMD_INT_RANGE;
} else if (REDIS_STRICMP_STATIC(kw, len, "ZRANGEBYSCORE") ||
REDIS_STRICMP_STATIC(kw, len, "ZREVRANGEBYSCORE"))
{
return REDIS_ZCMD_HAS_LIMIT |
REDIS_ZCMD_HAS_WITHSCORES;
} else if (REDIS_STRICMP_STATIC(kw, len, "ZRANGEBYLEX") ||
REDIS_STRICMP_STATIC(kw, len, "ZREVRANGEBYLEX"))
{
return REDIS_ZCMD_HAS_LIMIT;
} else if (REDIS_STRICMP_STATIC(kw, len, "ZDIFF")) {
return REDIS_ZCMD_HAS_WITHSCORES;
} else if (REDIS_STRICMP_STATIC(kw, len, "ZINTER") ||
REDIS_STRICMP_STATIC(kw, len, "ZUNION"))
{
return REDIS_ZCMD_HAS_WITHSCORES |
REDIS_ZCMD_HAS_AGGREGATE;
}
/* Reaching this line means a compile-time error */
ZEND_ASSERT(0);
}
/* Validate ZLEX* min/max argument strings */
static int validate_zlex_arg(const char *str, size_t len) {
return (len > 1 && (*str == '[' || *str == '(')) ||
(len == 1 && (*str == '+' || *str == '-'));
}
static int validate_zlex_arg_zval(zval *z) {
return Z_TYPE_P(z) == IS_STRING && validate_zlex_arg(Z_STRVAL_P(z), Z_STRLEN_P(z));
}
int redis_zrange_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zval *zoptions = NULL, *zstart = NULL, *zend = NULL;
zend_string *dst = NULL, *src = NULL;
zend_long start = 0, end = 0;
smart_string cmdstr = {0};
redisZcmdOptions opt;
int min_argc, flags;
short slot2;
flags = redis_get_zcmd_flags(kw);
min_argc = 3 + (flags & REDIS_ZCMD_HAS_DST_KEY);
ZEND_PARSE_PARAMETERS_START(min_argc, min_argc + 1)
if (flags & REDIS_ZCMD_HAS_DST_KEY) {
Z_PARAM_STR(dst)
}
Z_PARAM_STR(src)
if (flags & REDIS_ZCMD_INT_RANGE) {
Z_PARAM_LONG(start)
Z_PARAM_LONG(end)
} else {
Z_PARAM_ZVAL(zstart)
Z_PARAM_ZVAL(zend)
}
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL_OR_NULL(zoptions)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
redis_get_zcmd_options(&opt, zoptions, flags);
if (opt.bylex) {
ZEND_ASSERT(!(flags & REDIS_ZCMD_INT_RANGE));
if (!validate_zlex_arg_zval(zstart) || !validate_zlex_arg_zval(zend)) {
php_error_docref(NULL, E_WARNING, "Legographical args must start with '[' or '(' or be '+' or '-'");
return FAILURE;
}
}
redis_cmd_init_sstr(&cmdstr, min_argc + !!opt.bylex + !!opt.byscore +
!!opt.rev + !!opt.withscores +
(opt.limit.enabled ? 3 : 0), kw, strlen(kw));
if (flags & REDIS_ZCMD_HAS_DST_KEY)
redis_cmd_append_sstr_key_zstr(&cmdstr, dst, redis_sock, slot);
redis_cmd_append_sstr_key_zstr(&cmdstr, src, redis_sock, &slot2);
/* Protect the user from crossslot errors */
if ((flags & REDIS_ZCMD_HAS_DST_KEY) && slot && *slot != slot2) {
php_error_docref(NULL, E_WARNING, "destination and source keys must map to the same slot");
efree(cmdstr.c);
return FAILURE;
}
if (flags & REDIS_ZCMD_INT_RANGE) {
redis_cmd_append_sstr_long(&cmdstr, start);
redis_cmd_append_sstr_long(&cmdstr, end);
} else {
redis_cmd_append_sstr_zval(&cmdstr, zstart, NULL);
redis_cmd_append_sstr_zval(&cmdstr, zend, NULL);
}
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, opt.byscore, "BYSCORE");
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, opt.bylex, "BYLEX");
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, opt.rev, "REV");
if (opt.limit.enabled) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "LIMIT");
redis_cmd_append_sstr_long(&cmdstr, opt.limit.offset);
redis_cmd_append_sstr_long(&cmdstr, opt.limit.count);
}
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, opt.withscores, "WITHSCORES");
if (slot) *slot = slot2;
*ctx = opt.withscores ? PHPREDIS_CTX_PTR : NULL;
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
static int redis_build_config_get_cmd(smart_string *dst, zval *val) {
zend_string *zstr;
int ncfg;
zval *zv;
if (val == NULL || (Z_TYPE_P(val) != IS_STRING && Z_TYPE_P(val) != IS_ARRAY)) {
php_error_docref(NULL, E_WARNING, "Must pass a string or array of values to CONFIG GET");
return FAILURE;
} else if (Z_TYPE_P(val) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(val)) == 0) {
php_error_docref(NULL, E_WARNING, "Cannot pass an empty array to CONFIG GET");
return FAILURE;
}
ncfg = Z_TYPE_P(val) == IS_STRING ? 1 : zend_hash_num_elements(Z_ARRVAL_P(val));
REDIS_CMD_INIT_SSTR_STATIC(dst, 1 + ncfg, "CONFIG");
REDIS_CMD_APPEND_SSTR_STATIC(dst, "GET");
if (Z_TYPE_P(val) == IS_STRING) {
redis_cmd_append_sstr_zstr(dst, Z_STR_P(val));
} else {
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(val), zv) {
ZVAL_DEREF(zv);
zstr = zval_get_string(zv);
redis_cmd_append_sstr_zstr(dst, zstr);
zend_string_release(zstr);
} ZEND_HASH_FOREACH_END();
}
return SUCCESS;
}
static int redis_build_config_set_cmd(smart_string *dst, zval *key, zend_string *val) {
zend_string *zkey, *zstr;
zval *zv;
/* Legacy case: CONFIG SET <string> <string> */
if (key != NULL && val != NULL) {
REDIS_CMD_INIT_SSTR_STATIC(dst, 3, "CONFIG");
REDIS_CMD_APPEND_SSTR_STATIC(dst, "SET");
zstr = zval_get_string(key);
redis_cmd_append_sstr_zstr(dst, zstr);
zend_string_release(zstr);
redis_cmd_append_sstr_zstr(dst, val);
return SUCCESS;
}
/* Now we must have an array with at least one element */
if (key == NULL || Z_TYPE_P(key) != IS_ARRAY || zend_hash_num_elements(Z_ARRVAL_P(key)) == 0) {
php_error_docref(NULL, E_WARNING, "Must either pass two strings to CONFIG SET or a non-empty array of values");
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(dst, 1 + (2 * zend_hash_num_elements(Z_ARRVAL_P(key))), "CONFIG");
REDIS_CMD_APPEND_SSTR_STATIC(dst, "SET");
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(key), zkey, zv) {
if (zkey == NULL)
goto fail;
ZVAL_DEREF(zv);
redis_cmd_append_sstr_zstr(dst, zkey);
zstr = zval_get_string(zv);
redis_cmd_append_sstr_zstr(dst, zstr);
zend_string_release(zstr);
} ZEND_HASH_FOREACH_END();
return SUCCESS;
fail:
php_error_docref(NULL, E_WARNING, "Must pass an associate array of config keys and values");
efree(dst->c);
memset(dst, 0, sizeof(*dst));
return FAILURE;
}
int
redis_config_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *op = NULL, *arg = NULL;
smart_string cmdstr = {0};
int res = FAILURE;
zval *key = NULL;
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STR(op)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL_OR_NULL(key)
Z_PARAM_STR_OR_NULL(arg)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_string_equals_literal_ci(op, "RESETSTAT") ||
zend_string_equals_literal_ci(op, "REWRITE"))
{
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1, "CONFIG");
redis_cmd_append_sstr_zstr(&cmdstr, op);
*ctx = redis_boolean_response;
res = SUCCESS;
} else if (zend_string_equals_literal_ci(op, "GET")) {
res = redis_build_config_get_cmd(&cmdstr, key);
*ctx = redis_mbulk_reply_zipped_raw;
} else if (zend_string_equals_literal_ci(op, "SET")) {
res = redis_build_config_set_cmd(&cmdstr, key, arg);
*ctx = redis_boolean_response;
} else {
php_error_docref(NULL, E_WARNING, "Unknown operation '%s'", ZSTR_VAL(op));
return FAILURE;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return res;
}
int
redis_function_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
zend_string *op = NULL, *arg;
zval *argv = NULL;
int i, argc = 0;
ZEND_PARSE_PARAMETERS_START(1, -1)
Z_PARAM_STR(op)
Z_PARAM_OPTIONAL
Z_PARAM_VARIADIC('*', argv, argc)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
for (i = 0; i < argc; ++i) {
if (Z_TYPE(argv[i]) != IS_STRING) {
php_error_docref(NULL, E_WARNING, "invalid argument");
return FAILURE;
}
}
if (zend_string_equals_literal_ci(op, "DELETE")) {
if (argc < 1) {
php_error_docref(NULL, E_WARNING, "argument required");
return FAILURE;
}
} else if (zend_string_equals_literal_ci(op, "DUMP")) {
*ctx = PHPREDIS_CTX_PTR;
} else if (zend_string_equals_literal_ci(op, "FLUSH")) {
if (argc > 0 &&
!zend_string_equals_literal_ci(Z_STR(argv[0]), "SYNC") &&
!zend_string_equals_literal_ci(Z_STR(argv[0]), "ASYNC")
) {
php_error_docref(NULL, E_WARNING, "invalid argument");
return FAILURE;
}
} else if (zend_string_equals_literal_ci(op, "KILL")) {
// noop
} else if (zend_string_equals_literal_ci(op, "LIST")) {
if (argc > 0) {
if (zend_string_equals_literal_ci(Z_STR(argv[0]), "LIBRARYNAME")) {
if (argc < 2) {
php_error_docref(NULL, E_WARNING, "argument required");
return FAILURE;
}
} else if (!zend_string_equals_literal_ci(Z_STR(argv[0]), "WITHCODE")) {
php_error_docref(NULL, E_WARNING, "invalid argument");
return FAILURE;
}
}
*ctx = PHPREDIS_CTX_PTR + 1;
} else if (zend_string_equals_literal_ci(op, "LOAD")) {
if (argc < 1 || (
zend_string_equals_literal_ci(Z_STR(argv[0]), "REPLACE") && argc < 2
)) {
php_error_docref(NULL, E_WARNING, "argument required");
return FAILURE;
}
*ctx = PHPREDIS_CTX_PTR;
} else if (zend_string_equals_literal_ci(op, "RESTORE")) {
if (argc < 1 || (
argc > 1 &&
!zend_string_equals_literal_ci(Z_STR(argv[1]), "FLUSH") &&
!zend_string_equals_literal_ci(Z_STR(argv[1]), "APPEND") &&
!zend_string_equals_literal_ci(Z_STR(argv[1]), "REPLACE")
)) {
php_error_docref(NULL, E_WARNING, "invalid argument");
return FAILURE;
}
} else if (zend_string_equals_literal_ci(op, "STATS")) {
*ctx = PHPREDIS_CTX_PTR + 1;
} else {
php_error_docref(NULL, E_WARNING, "Unknown operation '%s'", ZSTR_VAL(op));
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + argc, "FUNCTION");
redis_cmd_append_sstr_zstr(&cmdstr, op);
for (i = 0; i < argc; i++) {
arg = zval_get_string(&argv[i]);
redis_cmd_append_sstr_zstr(&cmdstr, arg);
zend_string_release(arg);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int
redis_fcall_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx)
{
HashTable *keys = NULL, *args = NULL;
smart_string cmdstr = {0};
zend_string *fn = NULL;
zval *zv;
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STR(fn)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT(keys)
Z_PARAM_ARRAY_HT(args)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
redis_cmd_init_sstr(&cmdstr, 2 + (keys ? zend_hash_num_elements(keys) : 0) +
(args ? zend_hash_num_elements(args) : 0), kw, strlen(kw));
redis_cmd_append_sstr_zstr(&cmdstr, fn);
redis_cmd_append_sstr_long(&cmdstr, keys ? zend_hash_num_elements(keys) : 0);
if (keys != NULL) {
ZEND_HASH_FOREACH_VAL(keys, zv) {
redis_cmd_append_sstr_key_zval(&cmdstr, zv, redis_sock, slot);
} ZEND_HASH_FOREACH_END();
}
if (args != NULL) {
ZEND_HASH_FOREACH_VAL(args, zv) {
redis_cmd_append_sstr_zval(&cmdstr, zv, redis_sock);
} ZEND_HASH_FOREACH_END();
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int
redis_zrandmember_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key;
int count = 0;
size_t key_len;
smart_string cmdstr = {0};
zend_bool withscores = 0;
zval *z_opts = NULL, *z_ele;
zend_string *zkey;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|a",
&key, &key_len, &z_opts) == FAILURE)
{
return FAILURE;
}
if (z_opts != NULL) {
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(z_opts), zkey, z_ele) {
if (zkey != NULL) {
ZVAL_DEREF(z_ele);
if (zend_string_equals_literal_ci(zkey, "count")) {
count = zval_get_long(z_ele);
} else if (zend_string_equals_literal_ci(zkey, "withscores")) {
withscores = zval_is_true(z_ele);
}
}
} ZEND_HASH_FOREACH_END();
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + (count != 0) + withscores, "ZRANDMEMBER");
redis_cmd_append_sstr_key(&cmdstr, key, key_len, redis_sock, slot);
if (count != 0) {
redis_cmd_append_sstr_long(&cmdstr, count);
*ctx = PHPREDIS_CTX_PTR;
}
if (withscores) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "WITHSCORES");
*ctx = PHPREDIS_CTX_PTR + 1;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int
redis_zdiff_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zval *z_keys, *z_opts = NULL, *z_key;
redisZcmdOptions opts = {0};
smart_string cmdstr = {0};
int numkeys, flags;
short s2 = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|a",
&z_keys, &z_opts) == FAILURE)
{
return FAILURE;
}
if ((numkeys = zend_hash_num_elements(Z_ARRVAL_P(z_keys))) == 0) {
return FAILURE;
}
flags = redis_get_zcmd_flags("ZDIFF");
redis_get_zcmd_options(&opts, z_opts, flags);
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + numkeys + opts.withscores, "ZDIFF");
redis_cmd_append_sstr_long(&cmdstr, numkeys);
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(z_keys), z_key) {
ZVAL_DEREF(z_key);
redis_cmd_append_sstr_key_zval(&cmdstr, z_key, redis_sock, slot);
if (slot && s2 && s2 != *slot) {
php_error_docref(NULL, E_WARNING, "Not all keys map to the same slot!");
efree(cmdstr.c);
return FAILURE;
}
if (slot) s2 = *slot;
} ZEND_HASH_FOREACH_END();
if (opts.withscores) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "WITHSCORES");
*ctx = PHPREDIS_CTX_PTR;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
static int redis_cmd_append_sstr_score(smart_string *dst, zval *score) {
zend_uchar type;
zend_long lval;
size_t cmdlen;
double dval;
/* Get current command length */
cmdlen = dst->len;
if (Z_TYPE_P(score) == IS_LONG) {
redis_cmd_append_sstr_long(dst, Z_LVAL_P(score));
} else if (Z_TYPE_P(score) == IS_DOUBLE) {
redis_cmd_append_sstr_dbl(dst, Z_DVAL_P(score));
} else if (Z_TYPE_P(score) == IS_STRING) {
type = is_numeric_string(Z_STRVAL_P(score), Z_STRLEN_P(score), &lval, &dval, 0);
if (type == IS_LONG) {
redis_cmd_append_sstr_long(dst, lval);
} else if (type == IS_DOUBLE) {
redis_cmd_append_sstr_dbl(dst, dval);
} else if (zend_string_equals_literal_ci(Z_STR_P(score), "-inf") ||
zend_string_equals_literal_ci(Z_STR_P(score), "+inf") ||
zend_string_equals_literal_ci(Z_STR_P(score), "inf"))
{
redis_cmd_append_sstr_zstr(dst, Z_STR_P(score));
}
}
/* Success if we appended something */
if (dst->len > cmdlen)
return SUCCESS;
/* Nothing appended, failure */
php_error_docref(NULL, E_WARNING, "scores must be numeric or '-inf', 'inf', '+inf'");
return FAILURE;
}
int redis_intercard_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
smart_string cmdstr = {0};
zend_long limit = -1;
HashTable *keys;
zend_string *key;
zval *zv;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ARRAY_HT(keys)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(limit)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_hash_num_elements(keys) == 0) {
php_error_docref(NULL, E_WARNING, "Must pass at least one key");
return FAILURE;
} else if (ZEND_NUM_ARGS() == 2 && limit < 0) {
php_error_docref(NULL, E_WARNING, "LIMIT cannot be negative");
return FAILURE;
}
redis_cmd_init_sstr(&cmdstr, 1 + zend_hash_num_elements(keys) + (limit > 0 ? 2 : 0), kw, strlen(kw));
redis_cmd_append_sstr_long(&cmdstr, zend_hash_num_elements(keys));
if (slot) *slot = -1;
ZEND_HASH_FOREACH_VAL(keys, zv) {
key = redis_key_prefix_zval(redis_sock, zv);
if (slot) {
if (*slot == -1) {
*slot = cluster_hash_key_zstr(key);
} else if (*slot != cluster_hash_key_zstr(key)) {
php_error_docref(NULL, E_WARNING, "All keys don't hash to the same slot");
efree(cmdstr.c);
zend_string_release(key);
return FAILURE;
}
}
redis_cmd_append_sstr_zstr(&cmdstr, key);
zend_string_release(key);
} ZEND_HASH_FOREACH_END();
if (limit > 0) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "LIMIT");
redis_cmd_append_sstr_long(&cmdstr, limit);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int redis_replicaof_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zend_string *host = NULL;
zend_long port = 6379;
ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_STR(host)
Z_PARAM_LONG(port)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (port < 0 || port > UINT16_MAX) {
php_error_docref(NULL, E_WARNING, "Invalid port %ld", (long)port);
return FAILURE;
}
if (ZEND_NUM_ARGS() == 2) {
*cmd_len = REDIS_SPPRINTF(cmd, kw, "Sd", host, (int)port);
} else {
*cmd_len = REDIS_SPPRINTF(cmd, kw, "ss", ZEND_STRL("NO"), ZEND_STRL("ONE"));
}
return SUCCESS;
}
int
redis_zinterunion_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zval *z_keys, *z_weights = NULL, *z_opts = NULL, *z_ele;
redisZcmdOptions opts = {0};
smart_string cmdstr = {0};
int numkeys, flags;
short s2 = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|a!a",
&z_keys, &z_weights, &z_opts) == FAILURE)
{
return FAILURE;
}
if ((numkeys = zend_hash_num_elements(Z_ARRVAL_P(z_keys))) == 0) {
return FAILURE;
}
if (z_weights && zend_hash_num_elements(Z_ARRVAL_P(z_weights)) != numkeys) {
php_error_docref(NULL, E_WARNING, "WEIGHTS and keys array should be the same size!");
return FAILURE;
}
flags = redis_get_zcmd_flags(kw);
redis_get_zcmd_options(&opts, z_opts, flags);
redis_cmd_init_sstr(&cmdstr, 1 + numkeys + (z_weights ? 1 + numkeys : 0) + (opts.aggregate ? 2 : 0) + opts.withscores, kw, strlen(kw));
redis_cmd_append_sstr_long(&cmdstr, numkeys);
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(z_keys), z_ele) {
ZVAL_DEREF(z_ele);
redis_cmd_append_sstr_key_zval(&cmdstr, z_ele, redis_sock, slot);
if (slot) {
if (s2 && s2 != *slot) {
php_error_docref(NULL, E_WARNING, "Not all keys hash to the same slot");
efree(cmdstr.c);
return FAILURE;
}
s2 = *slot;
}
} ZEND_HASH_FOREACH_END();
if (z_weights) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "WEIGHTS");
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(z_weights), z_ele) {
ZVAL_DEREF(z_ele);
if (redis_cmd_append_sstr_score(&cmdstr, z_ele) == FAILURE) {
efree(cmdstr.c);
return FAILURE;
}
} ZEND_HASH_FOREACH_END();
}
if (opts.aggregate) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "AGGREGATE");
redis_cmd_append_sstr_zstr(&cmdstr, opts.aggregate);
}
if (opts.withscores) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "WITHSCORES");
*ctx = PHPREDIS_CTX_PTR;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int
redis_zdiffstore_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
zend_string *dst = NULL;
HashTable *keys = NULL;
zend_ulong nkeys;
short s2 = 0;
zval *zkey;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(dst)
Z_PARAM_ARRAY_HT(keys)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
nkeys = zend_hash_num_elements(keys);
if (nkeys == 0)
return FAILURE;
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2 + nkeys, "ZDIFFSTORE");
redis_cmd_append_sstr_key_zstr(&cmdstr, dst, redis_sock, slot);
redis_cmd_append_sstr_long(&cmdstr, nkeys);
ZEND_HASH_FOREACH_VAL(keys, zkey) {
ZVAL_DEREF(zkey);
redis_cmd_append_sstr_key_zval(&cmdstr, zkey, redis_sock, slot ? &s2 : NULL);
if (slot && *slot != s2) {
php_error_docref(NULL, E_WARNING, "All keys must hash to the same slot");
efree(cmdstr.c);
return FAILURE;
}
} ZEND_HASH_FOREACH_END();
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* ZUNIONSTORE, ZINTERSTORE */
int
redis_zinterunionstore_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
HashTable *keys = NULL, *weights = NULL;
smart_string cmdstr = {0};
zend_string *dst = NULL;
zend_string *agg = NULL;
zend_ulong nkeys;
zval *zv = NULL;
short s2 = 0;
ZEND_PARSE_PARAMETERS_START(2, 4)
Z_PARAM_STR(dst)
Z_PARAM_ARRAY_HT(keys)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT_OR_NULL(weights)
Z_PARAM_STR_OR_NULL(agg)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
nkeys = zend_hash_num_elements(keys);
if (nkeys == 0)
return FAILURE;
if (weights != NULL && zend_hash_num_elements(weights) != nkeys) {
php_error_docref(NULL, E_WARNING, "WEIGHTS and keys array must be the same size!");
return FAILURE;
}
// AGGREGATE option
if (agg != NULL && (!zend_string_equals_literal_ci(agg, "SUM") &&
!zend_string_equals_literal_ci(agg, "MIN") &&
!zend_string_equals_literal_ci(agg, "MAX")))
{
php_error_docref(NULL, E_WARNING, "AGGREGATE option must be 'SUM', 'MIN', or 'MAX'");
return FAILURE;
}
redis_cmd_init_sstr(&cmdstr, 2 + nkeys + (weights ? 1 + nkeys : 0) + (agg ? 2 : 0), kw, strlen(kw));
redis_cmd_append_sstr_key_zstr(&cmdstr, dst, redis_sock, slot);
redis_cmd_append_sstr_int(&cmdstr, nkeys);
ZEND_HASH_FOREACH_VAL(keys, zv) {
ZVAL_DEREF(zv);
redis_cmd_append_sstr_key_zval(&cmdstr, zv, redis_sock, slot ? &s2 : NULL);
if (slot && s2 != *slot) {
php_error_docref(NULL, E_WARNING, "All keys don't hash to the same slot!");
efree(cmdstr.c);
return FAILURE;
}
} ZEND_HASH_FOREACH_END();
if (weights) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "WEIGHTS");
ZEND_HASH_FOREACH_VAL(weights, zv) {
ZVAL_DEREF(zv);
if (redis_cmd_append_sstr_score(&cmdstr, zv) == FAILURE) {
efree(cmdstr.c);
return FAILURE;
}
} ZEND_HASH_FOREACH_END();
}
if (agg) {
redis_cmd_append_sstr(&cmdstr, ZEND_STRL("AGGREGATE"));
redis_cmd_append_sstr_zstr(&cmdstr, agg);
}
// Push out values
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int redis_pubsub_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
HashTable *channels = NULL;
smart_string cmdstr = {0};
zend_string *op, *pattern = NULL;
zval *arg = NULL, *z_chan;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(op)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(arg)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_string_equals_literal_ci(op, "NUMPAT")) {
*ctx = NULL;
} else if (zend_string_equals_literal_ci(op, "CHANNELS") ||
zend_string_equals_literal_ci(op, "SHARDCHANNELS")
) {
if (arg != NULL) {
if (Z_TYPE_P(arg) != IS_STRING) {
php_error_docref(NULL, E_WARNING, "Invalid pattern value");
return FAILURE;
}
pattern = zval_get_string(arg);
}
*ctx = PHPREDIS_CTX_PTR;
} else if (zend_string_equals_literal_ci(op, "NUMSUB") ||
zend_string_equals_literal_ci(op, "SHARDNUMSUB")
) {
if (arg != NULL) {
if (Z_TYPE_P(arg) != IS_ARRAY) {
php_error_docref(NULL, E_WARNING, "Invalid channels value");
return FAILURE;
}
channels = Z_ARRVAL_P(arg);
}
*ctx = PHPREDIS_CTX_PTR + 1;
} else {
php_error_docref(NULL, E_WARNING, "Unknown PUBSUB operation '%s'", ZSTR_VAL(op));
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + !!pattern + (channels ? zend_hash_num_elements(channels) : 0), "PUBSUB");
redis_cmd_append_sstr_zstr(&cmdstr, op);
if (pattern != NULL) {
redis_cmd_append_sstr_zstr(&cmdstr, pattern);
zend_string_release(pattern);
} else if (channels != NULL) {
ZEND_HASH_FOREACH_VAL(channels, z_chan) {
redis_cmd_append_sstr_key_zval(&cmdstr, z_chan, redis_sock, slot);
} ZEND_HASH_FOREACH_END();
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* SUBSCRIBE/PSUBSCRIBE/SSUBSCRIBE */
int redis_subscribe_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zval *z_arr, *z_chan;
HashTable *ht_chan;
smart_string cmdstr = {0};
subscribeContext *sctx = ecalloc(1, sizeof(*sctx));
unsigned short shardslot = REDIS_CLUSTER_SLOTS;
short s2;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "af", &z_arr,
&sctx->cb.fci, &sctx->cb.fci_cache) == FAILURE)
{
efree(sctx);
return FAILURE;
}
ht_chan = Z_ARRVAL_P(z_arr);
sctx->kw = kw;
sctx->argc = zend_hash_num_elements(ht_chan);
if (sctx->argc == 0) {
efree(sctx);
return FAILURE;
}
if (strcasecmp(kw, "ssubscribe") == 0) {
zend_hash_internal_pointer_reset(ht_chan);
if ((z_chan = zend_hash_get_current_data(ht_chan)) == NULL) {
php_error_docref(NULL, E_WARNING, "Internal Zend HashTable error");
efree(sctx);
return FAILURE;
}
shardslot = cluster_hash_key_zval(z_chan);
}
// Start command construction
redis_cmd_init_sstr(&cmdstr, sctx->argc, kw, strlen(kw));
// Iterate over channels
ZEND_HASH_FOREACH_VAL(ht_chan, z_chan) {
redis_cmd_append_sstr_key_zval(&cmdstr, z_chan, redis_sock, slot ? &s2 : NULL);
if (slot && (shardslot != REDIS_CLUSTER_SLOTS && s2 != shardslot)) {
php_error_docref(NULL, E_WARNING, "All shard channels needs to belong to a single slot");
smart_string_free(&cmdstr);
efree(sctx);
return FAILURE;
}
} ZEND_HASH_FOREACH_END();
// Push values out
*cmd_len = cmdstr.len;
*cmd = cmdstr.c;
*ctx = (void*)sctx;
if (shardslot != REDIS_CLUSTER_SLOTS) {
if (slot) *slot = shardslot;
} else {
// Pick a slot at random
CMD_RAND_SLOT(slot);
}
return SUCCESS;
}
/* UNSUBSCRIBE/PUNSUBSCRIBE/SUNSUBSCRIBE */
int redis_unsubscribe_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
smart_string cmdstr = {0};
subscribeContext *sctx;
HashTable *channels;
zval *channel;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY_HT(channels)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_hash_num_elements(channels) == 0)
return FAILURE;
sctx = ecalloc(1, sizeof(*sctx));
sctx->kw = kw;
sctx->argc = zend_hash_num_elements(channels);
redis_cmd_init_sstr(&cmdstr, sctx->argc, kw, strlen(kw));
ZEND_HASH_FOREACH_VAL(channels, channel) {
redis_cmd_append_sstr_key_zval(&cmdstr, channel, redis_sock, slot);
} ZEND_HASH_FOREACH_END();
*cmd_len = cmdstr.len;
*cmd = cmdstr.c;
*ctx = sctx;
return SUCCESS;
}
/* ZRANGEBYLEX/ZREVRANGEBYLEX */
int redis_zrangebylex_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key, *min, *max;
size_t key_len, min_len, max_len;
int argc = ZEND_NUM_ARGS();
zend_long offset, count;
/* We need either 3 or 5 arguments for this to be valid */
if (argc != 3 && argc != 5) {
php_error_docref(0, E_WARNING, "Must pass either 3 or 5 arguments");
return FAILURE;
}
if (zend_parse_parameters(argc, "sss|ll", &key, &key_len, &min, &min_len,
&max, &max_len, &offset, &count) == FAILURE)
{
return FAILURE;
}
/* min and max must start with '(' or '[', or be either '-' or '+' */
if (!validate_zlex_arg(min, min_len) || !validate_zlex_arg(max, max_len)) {
php_error_docref(NULL, E_WARNING,
"Min/Max args can be '-' or '+', or start with '[' or '('");
return FAILURE;
}
/* Construct command */
if (argc == 3) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kss", key, key_len, min, min_len,
max, max_len);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "ksssll", key, key_len, min, min_len,
max, max_len, "LIMIT", 5, offset, count);
}
return SUCCESS;
}
/* ZLEXCOUNT/ZREMRANGEBYLEX */
int redis_gen_zlex_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key, *min, *max;
size_t key_len, min_len, max_len;
/* Parse args */
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss", &key, &key_len,
&min, &min_len, &max, &max_len) == FAILURE)
{
return FAILURE;
}
/* Quick sanity check on min/max */
if (!validate_zlex_arg(min, min_len) || !validate_zlex_arg(max, max_len)) {
php_error_docref(NULL, E_WARNING,
"Min/Max args can be '-' or '+', or start with '[' or '('");
return FAILURE;
}
/* Construct command */
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kss", key, key_len, min, min_len,
max, max_len);
return SUCCESS;
}
/* EVAL and EVALSHA */
int redis_eval_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char *kw,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *lua;
int argc = 0;
zval *z_arr = NULL, *z_ele;
HashTable *ht_arr;
zend_long num_keys = 0;
smart_string cmdstr = {0};
size_t lua_len;
zend_string *zstr;
short prevslot = -1;
/* Parse args */
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|al", &lua, &lua_len,
&z_arr, &num_keys) == FAILURE)
{
return FAILURE;
}
/* Grab arg count */
if (z_arr != NULL) {
ht_arr = Z_ARRVAL_P(z_arr);
argc = zend_hash_num_elements(ht_arr);
}
/* EVAL[SHA] {script || sha1} {num keys} */
redis_cmd_init_sstr(&cmdstr, 2 + argc, kw, strlen(kw));
redis_cmd_append_sstr(&cmdstr, lua, lua_len);
redis_cmd_append_sstr_long(&cmdstr, num_keys);
// Iterate over our args if we have any
if (argc > 0) {
ZEND_HASH_FOREACH_VAL(ht_arr, z_ele) {
zstr = zval_get_string(z_ele);
/* If we're still on a key, prefix it check slot */
if (num_keys-- > 0) {
redis_cmd_append_sstr_key(&cmdstr, ZSTR_VAL(zstr), ZSTR_LEN(zstr), redis_sock, slot);
/* If we have been passed a slot, all keys must match */
if (slot) {
if (prevslot != -1 && prevslot != *slot) {
zend_string_release(zstr);
php_error_docref(0, E_WARNING, "All keys do not map to the same slot");
return FAILURE;
}
prevslot = *slot;
}
} else {
redis_cmd_append_sstr(&cmdstr, ZSTR_VAL(zstr), ZSTR_LEN(zstr));
}
zend_string_release(zstr);
} ZEND_HASH_FOREACH_END();
} else {
/* Any slot will do */
CMD_RAND_SLOT(slot);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* Commands that take a key followed by a variable list of serializable
* values (RPUSH, LPUSH, SADD, SREM, etc...) */
int redis_key_varval_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zval *args = NULL;
zend_string *key = NULL;
smart_string cmdstr = {0};
size_t i;
int argc = 0;
ZEND_PARSE_PARAMETERS_START(2, -1)
Z_PARAM_STR(key)
Z_PARAM_VARIADIC('*', args, argc)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
/* Initialize our command */
redis_cmd_init_sstr(&cmdstr, argc + 1, kw, strlen(kw));
/* Append key */
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
/* Add members */
for (i = 0; i < argc; i++) {
redis_cmd_append_sstr_zval(&cmdstr, &args[i], redis_sock);
}
// Push out values
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
// Success!
return SUCCESS;
}
/* Commands that take a key and then an array of values */
static int gen_key_arr_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, zend_bool pack_values, char **cmd, int *cmd_len,
short *slot, void **ctx)
{
smart_string cmdstr = {0};
HashTable *values = NULL;
zend_string *key = NULL;
zval *zv;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(key)
Z_PARAM_ARRAY_HT(values)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_hash_num_elements(values) == 0)
return FAILURE;
redis_cmd_init_sstr(&cmdstr, 1 + zend_hash_num_elements(values), kw, strlen(kw));
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
ZEND_HASH_FOREACH_VAL(values, zv) {
redis_cmd_append_sstr_zval(&cmdstr, zv, pack_values ? redis_sock : NULL);
} ZEND_HASH_FOREACH_END();
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int redis_key_val_arr_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
return gen_key_arr_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, kw,
1, cmd, cmd_len, slot, ctx);
}
int redis_key_str_arr_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
return gen_key_arr_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, kw,
0, cmd, cmd_len, slot, ctx);
}
/* Generic function that takes one or more non-serialized arguments */
static int
gen_vararg_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
uint32_t min_argc, char *kw, char **cmd, int *cmd_len,
short *slot, void **ctx)
{
smart_string cmdstr = {0};
zval *argv = NULL;
int argc = 0;
uint32_t i;
ZEND_PARSE_PARAMETERS_START(min_argc, -1)
Z_PARAM_VARIADIC('*', argv, argc)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
redis_cmd_init_sstr(&cmdstr, argc, kw, strlen(kw));
for (i = 0; i < argc; i++) {
redis_cmd_append_sstr_zval(&cmdstr, &argv[i], NULL);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int redis_mset_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
smart_string cmdstr = {0};
HashTable *kvals = NULL;
zend_string *key;
zend_ulong idx;
zval *zv;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY_HT(kvals)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_hash_num_elements(kvals) == 0)
return FAILURE;
redis_cmd_init_sstr(&cmdstr, zend_hash_num_elements(kvals) * 2, kw, strlen(kw));
ZEND_HASH_FOREACH_KEY_VAL(kvals, idx, key, zv) {
ZVAL_DEREF(zv);
if (key) {
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, NULL);
} else {
redis_cmd_append_sstr_key_long(&cmdstr, idx, redis_sock, NULL);
}
redis_cmd_append_sstr_zval(&cmdstr, zv, redis_sock);
} ZEND_HASH_FOREACH_END();
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* Generic function that takes a variable number of keys, with an optional
* timeout value. This can handle various SUNION/SUNIONSTORE/BRPOP type
* commands. */
static int gen_varkey_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, int kw_len, zend_bool has_timeout,
char **cmd, int *cmd_len, short *slot)
{
zval *argv = NULL, ztimeout = {0}, *zv;
smart_string cmdstr = {0};
uint32_t min_argc;
short kslot = -1;
int single_array;
int argc = 0;
min_argc = has_timeout ? 2 : 1;
ZEND_PARSE_PARAMETERS_START(min_argc, -1)
Z_PARAM_VARIADIC('*', argv, argc)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
single_array = argc == min_argc && Z_TYPE(argv[0]) == IS_ARRAY;
if (has_timeout) {
if (single_array)
ZVAL_COPY_VALUE(&ztimeout, &argv[1]);
else
ZVAL_COPY_VALUE(&ztimeout, &argv[argc - 1]);
if (Z_TYPE(ztimeout) != IS_LONG && Z_TYPE(ztimeout) != IS_DOUBLE) {
php_error_docref(NULL, E_WARNING, "Timeout must be a long or double");
return FAILURE;
}
}
// If we're running a single array, rework args
if (single_array) {
/* Need at least one argument */
argc = zend_hash_num_elements(Z_ARRVAL(argv[0]));
if (argc == 0)
return FAILURE;
if (has_timeout) argc++;
}
// Begin construction of our command
redis_cmd_init_sstr(&cmdstr, argc, kw, kw_len);
if (single_array) {
ZEND_HASH_FOREACH_VAL(Z_ARRVAL(argv[0]), zv) {
redis_cmd_append_sstr_key_zval(&cmdstr, zv, redis_sock, slot);
if (slot) {
if (kslot != -1 && *slot != kslot)
goto cross_slot;
kslot = *slot;
}
} ZEND_HASH_FOREACH_END();
} else {
uint32_t i;
for(i = 0; i < argc - !!has_timeout; i++) {
redis_cmd_append_sstr_key_zval(&cmdstr, &argv[i], redis_sock, slot);
if (slot) {
if (kslot != -1 && *slot != kslot)
goto cross_slot;
kslot = *slot;
}
}
}
if (Z_TYPE(ztimeout) == IS_DOUBLE) {
redis_cmd_append_sstr_dbl(&cmdstr, Z_DVAL(ztimeout));
} else if (Z_TYPE(ztimeout) == IS_LONG) {
redis_cmd_append_sstr_long(&cmdstr, Z_LVAL(ztimeout));
}
// Push out parameters
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
cross_slot:
efree(cmdstr.c);
php_error_docref(NULL, E_WARNING, "Not all keys hash to the same slot!");
return FAILURE;
}
int redis_mpop_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char *kw,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
int argc, blocking, is_zmpop;
smart_string cmdstr = {0};
zend_string *from = NULL;
HashTable *keys = NULL;
double timeout = 0.0;
zend_long count = 1;
short slot2 = -1;
zval *zv;
/* Sanity check on our keyword */
ZEND_ASSERT(kw != NULL && *kw != '\0' && *(kw+1) != '\0');
blocking = tolower(*kw) == 'b';
is_zmpop = tolower(kw[blocking]) == 'z';
ZEND_PARSE_PARAMETERS_START(2 + blocking, 3 + blocking) {
if (blocking) {
Z_PARAM_DOUBLE(timeout)
}
Z_PARAM_ARRAY_HT(keys)
Z_PARAM_STR(from);
Z_PARAM_OPTIONAL
Z_PARAM_LONG(count);
} ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_hash_num_elements(keys) == 0) {
php_error_docref(NULL, E_WARNING, "Must pass at least one key");
return FAILURE;
} else if (count < 1) {
php_error_docref(NULL, E_WARNING, "Count must be > 0");
return FAILURE;
} else if (!is_zmpop && !(zend_string_equals_literal_ci(from, "LEFT") ||
zend_string_equals_literal_ci(from, "RIGHT")))
{
php_error_docref(NULL, E_WARNING, "from must be either 'LEFT' or 'RIGHT'");
return FAILURE;
} else if (is_zmpop && !(zend_string_equals_literal_ci(from, "MIN") ||
zend_string_equals_literal_ci(from, "MAX")))
{
php_error_docref(NULL, E_WARNING, "from must be either 'MIN' or 'MAX'");
return FAILURE;
}
argc = 2 + !!blocking + zend_hash_num_elements(keys) + (count != 1 ? 2 : 0);
redis_cmd_init_sstr(&cmdstr, argc, kw, strlen(kw));
if (blocking) redis_cmd_append_sstr_dbl(&cmdstr, timeout);
redis_cmd_append_sstr_long(&cmdstr, zend_hash_num_elements(keys));
if (slot) *slot = -1;
ZEND_HASH_FOREACH_VAL(keys, zv) {
redis_cmd_append_sstr_key_zval(&cmdstr, zv, redis_sock, slot);
if (slot) {
if (slot2 != -1 && *slot != slot2) {
php_error_docref(NULL, E_WARNING, "All keys don't hash to the same slot");
efree(cmdstr.c);
return FAILURE;
}
slot2 = *slot;
}
} ZEND_HASH_FOREACH_END();
redis_cmd_append_sstr_zstr(&cmdstr, from);
if (count != 1) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "COUNT");
redis_cmd_append_sstr_long(&cmdstr, count);
}
*ctx = is_zmpop ? PHPREDIS_CTX_PTR : NULL;
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int redis_info_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
return gen_vararg_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, 0,
"INFO", cmd, cmd_len, slot, ctx);
}
int redis_script_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
int argc = 0;
smart_string cmdstr = {0};
zval *argv = NULL;
ZEND_PARSE_PARAMETERS_START(1, -1)
Z_PARAM_VARIADIC('*', argv, argc)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (redis_build_script_cmd(&cmdstr, argc, argv) == NULL) {
return FAILURE;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* Generic handling of every blocking pop command (BLPOP, BZPOP[MIN/MAX], etc */
int redis_blocking_pop_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
return gen_varkey_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, kw,
strlen(kw), 1, cmd, cmd_len, slot);
}
/*
* Commands with specific signatures or that need unique functions because they
* have specific processing (argument validation, etc) that make them unique
*/
int
redis_pop_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key;
size_t key_len;
smart_string cmdstr = {0};
zend_long count = 0;
// Make sure the function is being called correctly
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l",
&key, &key_len, &count) == FAILURE)
{
return FAILURE;
}
redis_cmd_init_sstr(&cmdstr, 1 + (count > 0), kw, strlen(kw));
redis_cmd_append_sstr_key(&cmdstr, key, key_len, redis_sock, slot);
if (count > 0) {
redis_cmd_append_sstr_long(&cmdstr, (long)count);
*ctx = PHPREDIS_CTX_PTR;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int
redis_acl_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
zend_string *op, *zstr;
zval *z_args = NULL;
int argc = 0, i;
ZEND_PARSE_PARAMETERS_START(1, -1)
Z_PARAM_STR(op)
Z_PARAM_OPTIONAL
Z_PARAM_VARIADIC('*', z_args, argc)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_string_equals_literal_ci(op, "CAT") ||
zend_string_equals_literal_ci(op, "LIST") ||
zend_string_equals_literal_ci(op, "USERS")
) {
*ctx = NULL;
} else if (zend_string_equals_literal_ci(op, "LOAD") ||
zend_string_equals_literal_ci(op, "SAVE")
) {
*ctx = PHPREDIS_CTX_PTR;
} else if (zend_string_equals_literal_ci(op, "GENPASS") ||
zend_string_equals_literal_ci(op, "WHOAMI")
) {
*ctx = PHPREDIS_CTX_PTR + 1;
} else if (zend_string_equals_literal_ci(op, "SETUSER")) {
if (argc < 1) {
php_error_docref(NULL, E_WARNING, "ACL SETUSER requires at least one argument");
return FAILURE;
}
*ctx = PHPREDIS_CTX_PTR;
} else if (zend_string_equals_literal_ci(op, "DELUSER")) {
if (argc < 1) {
php_error_docref(NULL, E_WARNING, "ACL DELUSER requires at least one argument");
return FAILURE;
}
*ctx = PHPREDIS_CTX_PTR + 2;
} else if (zend_string_equals_literal_ci(op, "GETUSER")) {
if (argc < 1) {
php_error_docref(NULL, E_WARNING, "ACL GETUSER requires at least one argument");
return FAILURE;
}
*ctx = PHPREDIS_CTX_PTR + 3;
} else if (zend_string_equals_literal_ci(op, "DRYRUN")) {
if (argc < 2) {
php_error_docref(NULL, E_WARNING, "ACL DRYRUN requires at least two arguments");
return FAILURE;
}
*ctx = PHPREDIS_CTX_PTR;
} else if (zend_string_equals_literal_ci(op, "LOG")) {
if (argc > 0 && Z_TYPE(z_args[0]) == IS_STRING && ZVAL_STRICMP_STATIC(&z_args[0], "RESET")) {
*ctx = PHPREDIS_CTX_PTR;
} else {
*ctx = PHPREDIS_CTX_PTR + 4;
}
} else {
php_error_docref(NULL, E_WARNING, "Unknown ACL operation '%s'", ZSTR_VAL(op));
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + argc, "ACL");
redis_cmd_append_sstr_zstr(&cmdstr, op);
for (i = 0; i < argc; ++i) {
zstr = zval_get_string(&z_args[i]);
redis_cmd_append_sstr_zstr(&cmdstr, zstr);
zend_string_release(zstr);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int redis_waitaof_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_long numlocal, numreplicas, timeout;
smart_string cmdstr = {0};
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_LONG(numlocal)
Z_PARAM_LONG(numreplicas)
Z_PARAM_LONG(timeout)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (numlocal < 0 || numreplicas < 0 || timeout < 0) {
php_error_docref(NULL, E_WARNING, "No arguments can be negative");
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 3, "WAITAOF");
redis_cmd_append_sstr_long(&cmdstr, numlocal);
redis_cmd_append_sstr_long(&cmdstr, numreplicas);
redis_cmd_append_sstr_long(&cmdstr, timeout);
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* Attempt to pull a long expiry from a zval. We're more restrictave than zval_get_long
* because that function will return integers from things like open file descriptors
* which should simply fail as a TTL */
static int redis_try_get_expiry(zval *zv, zend_long *lval) {
double dval;
/* Success on an actual long or double */
if (Z_TYPE_P(zv) == IS_LONG || Z_TYPE_P(zv) == IS_DOUBLE) {
*lval = zval_get_long(zv);
return SUCCESS;
}
/* Automatically fail if we're not a string */
if (Z_TYPE_P(zv) != IS_STRING)
return FAILURE;
/* Attempt to get a long from the string */
switch (is_numeric_string(Z_STRVAL_P(zv), Z_STRLEN_P(zv), lval, &dval, 0)) {
case IS_DOUBLE:
*lval = dval;
REDIS_FALLTHROUGH;
case IS_LONG:
return SUCCESS;
default:
return FAILURE;
}
}
/* SET */
int redis_set_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key = NULL, *exp_type = NULL, *set_type = NULL;
zval *z_value, *z_opts = NULL, *ifeq = NULL;
zend_string *zstr = NULL, *tmp = NULL;
smart_string cmdstr = {0};
zend_long expire = -1;
zend_bool get = 0;
long keep_ttl = 0;
size_t key_len;
#define setExpiryWarning(zv) \
php_error_docref(NULL, E_WARNING, "%s passed as EXPIRY is invalid " \
"(must be an int, float, or numeric string >= 1)", \
zend_zval_type_name((zv)))
// Make sure the function is being called correctly
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz|z", &key, &key_len,
&z_value, &z_opts) == FAILURE)
{
return FAILURE;
}
// Check for an options array
if (z_opts && Z_TYPE_P(z_opts) == IS_ARRAY) {
HashTable *kt = Z_ARRVAL_P(z_opts);
zend_string *zkey;
zval *v;
/* Iterate our option array */
ZEND_HASH_FOREACH_STR_KEY_VAL(kt, zkey, v) {
ZVAL_DEREF(v);
/* Detect PX or EX argument and validate timeout */
if (zkey && (zend_string_equals_literal_ci(zkey, "EX") ||
zend_string_equals_literal_ci(zkey, "PX") ||
zend_string_equals_literal_ci(zkey, "EXAT") ||
zend_string_equals_literal_ci(zkey, "PXAT"))
) {
if (redis_try_get_expiry(v, &expire) == FAILURE || expire < 1) {
setExpiryWarning(v);
return FAILURE;
}
exp_type = ZSTR_VAL(zkey);
} else if (zkey && zend_string_equals_literal_ci(zkey, "IFEQ")) {
ifeq = v;
} else if (Z_TYPE_P(v) == IS_STRING) {
if (zend_string_equals_literal_ci(Z_STR_P(v), "KEEPTTL")) {
keep_ttl = 1;
} else if (zend_string_equals_literal_ci(Z_STR_P(v), "GET")) {
get = 1;
} else if (zend_string_equals_literal_ci(Z_STR_P(v), "NX") ||
zend_string_equals_literal_ci(Z_STR_P(v), "XX"))
{
set_type = Z_STRVAL_P(v);
}
}
} ZEND_HASH_FOREACH_END();
} else if (z_opts && Z_TYPE_P(z_opts) != IS_NULL) {
if (redis_try_get_expiry(z_opts, &expire) == FAILURE || expire < 1) {
setExpiryWarning(z_opts);
return FAILURE;
}
}
/* Protect the user from syntax errors but give them some info about what's wrong */
if (exp_type && keep_ttl) {
php_error_docref(NULL, E_WARNING, "KEEPTTL can't be combined with EX or PX option");
return FAILURE;
}
/* You can't use IFEQ with NX or XX */
if (set_type && ifeq) {
php_error_docref(NULL, E_WARNING, "IFEQ can't be combined with NX or XX option");
return FAILURE;
}
/* Backward compatibility: If we are passed no options except an EXPIRE ttl, we
* actually execute a SETEX command */
if (expire > 0 && !exp_type && !set_type && !keep_ttl) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "SETEX", "klv", key, key_len, expire, z_value);
return SUCCESS;
}
/* Calculate argc based on options set */
int argc = 2 + (ifeq ? 2 : 0) + (exp_type ? 2 : 0) + (set_type != NULL) +
(keep_ttl != 0) + get;
/* Initial SET <key> <value> */
redis_cmd_init_sstr(&cmdstr, argc, "SET", 3);
redis_cmd_append_sstr_key(&cmdstr, key, key_len, redis_sock, slot);
redis_cmd_append_sstr_zval(&cmdstr, z_value, redis_sock);
if (ifeq) {
zstr = zval_get_tmp_string(ifeq, &tmp);
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "IFEQ");
redis_cmd_append_sstr_zstr(&cmdstr, zstr);
zend_tmp_string_release(tmp);
} else if (set_type) {
redis_cmd_append_sstr(&cmdstr, set_type, strlen(set_type));
}
if (get) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "GET");
*ctx = PHPREDIS_CTX_PTR;
}
if (exp_type) {
redis_cmd_append_sstr(&cmdstr, exp_type, strlen(exp_type));
redis_cmd_append_sstr_long(&cmdstr, (long)expire);
} else if (keep_ttl) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "KEEPTTL");
}
/* Push command and length to the caller */
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
#undef setExpiryWarning
}
/* MGET */
int redis_mget_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
HashTable *keys = NULL;
zval *zkey;
/* RedisCluster has a custom MGET implementation */
ZEND_ASSERT(slot == NULL);
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY_HT(keys)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_hash_num_elements(keys) == 0)
return FAILURE;
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, zend_hash_num_elements(keys), "MGET");
ZEND_HASH_FOREACH_VAL(keys, zkey) {
ZVAL_DEREF(zkey);
redis_cmd_append_sstr_key_zval(&cmdstr, zkey, redis_sock, slot);
} ZEND_HASH_FOREACH_END();
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int
redis_getex_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
char *key, *exp_type = NULL;
zval *z_opts = NULL, *z_ele;
zend_long expire = -1;
zend_bool persist = 0;
zend_string *zkey;
size_t key_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|a",
&key, &key_len, &z_opts) == FAILURE)
{
return FAILURE;
}
if (z_opts != NULL) {
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(z_opts), zkey, z_ele) {
if (zkey != NULL) {
ZVAL_DEREF(z_ele);
if (ZSTR_STRICMP_STATIC(zkey, "EX") ||
ZSTR_STRICMP_STATIC(zkey, "PX") ||
ZSTR_STRICMP_STATIC(zkey, "EXAT") ||
ZSTR_STRICMP_STATIC(zkey, "PXAT")
) {
exp_type = ZSTR_VAL(zkey);
expire = zval_get_long(z_ele);
persist = 0;
} else if (ZSTR_STRICMP_STATIC(zkey, "PERSIST")) {
persist = zval_is_true(z_ele);
exp_type = NULL;
}
} else if (Z_TYPE_P(z_ele) == IS_STRING &&
zend_string_equals_literal_ci(Z_STR_P(z_ele), "PERSIST"))
{
persist = zval_is_true(z_ele);
exp_type = NULL;
}
} ZEND_HASH_FOREACH_END();
}
if (exp_type != NULL && expire < 1) {
php_error_docref(NULL, E_WARNING, "EXPIRE can't be < 1");
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + (exp_type ? 2 : persist), "GETEX");
redis_cmd_append_sstr_key(&cmdstr, key, key_len, redis_sock, slot);
if (exp_type != NULL) {
redis_cmd_append_sstr(&cmdstr, exp_type, strlen(exp_type));
redis_cmd_append_sstr_long(&cmdstr, expire);
} else if (persist) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "PERSIST");
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* BRPOPLPUSH */
int redis_brpoplpush_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *src = NULL, *dst = NULL;
double timeout = 0;
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_STR(src)
Z_PARAM_STR(dst)
Z_PARAM_DOUBLE(timeout)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
src = redis_key_prefix_zstr(redis_sock, src);
dst = redis_key_prefix_zstr(redis_sock, dst);
if (slot && (*slot = cluster_hash_key_zstr(src)) != cluster_hash_key_zstr(dst)) {
php_error_docref(NULL, E_WARNING, "Keys must hash to the same slot");
zend_string_release(src);
zend_string_release(dst);
return FAILURE;
}
/* Consistency with Redis. If timeout < 0 use RPOPLPUSH */
if (timeout < 0) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "RPOPLPUSH", "SS", src, dst);
} else if (fabs(timeout - (long)timeout) < .0001) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "BRPOPLPUSH", "SSd", src, dst, (long)timeout);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "BRPOPLPUSH", "SSf", src, dst, timeout);
}
zend_string_release(src);
zend_string_release(dst);
return SUCCESS;
}
/* To maintain backward compatibility with earlier versions of phpredis, we
* allow for an optional "increment by" argument for INCR and DECR even though
* that's not how Redis proper works */
#define TYPE_INCR 0
#define TYPE_DECR 1
/* Handle INCR(BY) and DECR(BY) depending on optional increment value */
static int
redis_atomic_increment(INTERNAL_FUNCTION_PARAMETERS, int type,
RedisSock *redis_sock, char **cmd, int *cmd_len,
short *slot, void **ctx)
{
char *key;
size_t key_len;
zend_long val = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &key, &key_len,
&val) == FAILURE)
{
return FAILURE;
}
/* If our value is 1 we use INCR/DECR. For other values, treat the call as
* an INCRBY or DECRBY call */
if (type == TYPE_INCR) {
if (val == 1) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "INCR", "k", key, key_len);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "INCRBY", "kl", key, key_len, val);
}
} else {
if (val == 1) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "DECR", "k", key, key_len);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "DECRBY", "kl", key, key_len, val);
}
}
/* Success */
return SUCCESS;
}
/* INCR */
int redis_incr_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
return redis_atomic_increment(INTERNAL_FUNCTION_PARAM_PASSTHRU,
TYPE_INCR, redis_sock, cmd, cmd_len, slot, ctx);
}
/* DECR */
int redis_decr_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
return redis_atomic_increment(INTERNAL_FUNCTION_PARAM_PASSTHRU,
TYPE_DECR, redis_sock, cmd, cmd_len, slot, ctx);
}
/* HINCRBY */
int redis_hincrby_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key, *mem;
size_t key_len, mem_len;
zend_long byval;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssl", &key, &key_len,
&mem, &mem_len, &byval) == FAILURE)
{
return FAILURE;
}
// Construct command
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "HINCRBY", "ksl", key, key_len, mem, mem_len, byval);
// Success
return SUCCESS;
}
/* HINCRBYFLOAT */
int redis_hincrbyfloat_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key, *mem;
size_t key_len, mem_len;
double byval;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssd", &key, &key_len,
&mem, &mem_len, &byval) == FAILURE)
{
return FAILURE;
}
// Construct command
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "HINCRBYFLOAT", "ksf", key, key_len, mem,
mem_len, byval);
// Success
return SUCCESS;
}
/* HMGET */
int redis_hmget_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zval *field = NULL, *zctx = NULL;
smart_string cmdstr = {0};
HashTable *fields = NULL;
zend_string *key = NULL;
zend_ulong valid = 0, i;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(key)
Z_PARAM_ARRAY_HT(fields)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_hash_num_elements(fields) == 0)
return FAILURE;
zctx = ecalloc(1 + zend_hash_num_elements(fields), sizeof(*zctx));
ZEND_HASH_FOREACH_VAL(fields, field) {
ZVAL_DEREF(field);
if (!((Z_TYPE_P(field) == IS_STRING && Z_STRLEN_P(field) > 0) || Z_TYPE_P(field) == IS_LONG))
continue;
ZVAL_COPY(&zctx[valid++], field);
} ZEND_HASH_FOREACH_END();
if (valid == 0) {
efree(zctx);
return FAILURE;
}
ZVAL_NULL(&zctx[valid]);
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + valid, "HMGET");
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
for (i = 0; i < valid; i++) {
redis_cmd_append_sstr_zval(&cmdstr, &zctx[i], NULL);
}
// Push out command, length, and key context
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
*ctx = zctx;
return SUCCESS;
}
/* HMSET */
int redis_hmset_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
zend_string *key = NULL;
HashTable *ht = NULL;
uint32_t fields;
zend_ulong idx;
zval *zv;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(key)
Z_PARAM_ARRAY_HT(ht)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
fields = zend_hash_num_elements(ht);
if (fields == 0)
return FAILURE;
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + (2 * fields), "HMSET");
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
ZEND_HASH_FOREACH_KEY_VAL(ht, idx, key, zv) {
if (key) {
redis_cmd_append_sstr_zstr(&cmdstr, key);
} else {
redis_cmd_append_sstr_long(&cmdstr, idx);
}
redis_cmd_append_sstr_zval(&cmdstr, zv, redis_sock);
} ZEND_HASH_FOREACH_END();
*cmd_len = cmdstr.len;
*cmd = cmdstr.c;
return SUCCESS;
}
/* HSTRLEN */
int
redis_hstrlen_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key, *field;
size_t key_len, field_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &key, &key_len,
&field, &field_len) == FAILURE
) {
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "HSTRLEN", "ks", key, key_len, field, field_len);
return SUCCESS;
}
static void redis_get_lcs_options(redisLcsOptions *dst, HashTable *ht) {
zend_string *key;
zval *zv;
ZEND_ASSERT(dst != NULL);
memset(dst, 0, sizeof(*dst));
if (ht == NULL)
return;
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, zv) {
if (key) {
if (zend_string_equals_literal_ci(key, "LEN")) {
dst->idx = 0;
dst->len = zval_is_true(zv);
} else if (zend_string_equals_literal_ci(key, "IDX")) {
dst->len = 0;
dst->idx = zval_is_true(zv);
} else if (zend_string_equals_literal_ci(key, "MINMATCHLEN")) {
dst->minmatchlen = zval_get_long(zv);
} else if (zend_string_equals_literal_ci(key, "WITHMATCHLEN")) {
dst->withmatchlen = zval_is_true(zv);
} else {
php_error_docref(NULL, E_WARNING, "Unknown LCS option '%s'", ZSTR_VAL(key));
}
} else if (Z_TYPE_P(zv) == IS_STRING) {
if (zend_string_equals_literal_ci(Z_STR_P(zv), "LEN")) {
dst->idx = 0;
dst->len = 1;
} else if (zend_string_equals_literal_ci(Z_STR_P(zv), "IDX")) {
dst->idx = 1;
dst->len = 0;
} else if (zend_string_equals_literal_ci(Z_STR_P(zv), "WITHMATCHLEN")) {
dst->withmatchlen = 1;
}
}
} ZEND_HASH_FOREACH_END();
}
/* LCS */
int redis_lcs_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *key1 = NULL, *key2 = NULL;
smart_string cmdstr = {0};
HashTable *ht = NULL;
redisLcsOptions opt;
int argc;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(key1)
Z_PARAM_STR(key2)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT_OR_NULL(ht)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
key1 = redis_key_prefix_zstr(redis_sock, key1);
key2 = redis_key_prefix_zstr(redis_sock, key2);
if (slot) {
*slot = cluster_hash_key_zstr(key1);
if (*slot != cluster_hash_key_zstr(key2)) {
php_error_docref(NULL, E_WARNING, "Warning, not all keys hash to the same slot!");
zend_string_release(key1);
zend_string_release(key2);
return FAILURE;
}
}
redis_get_lcs_options(&opt, ht);
argc = 2 + !!opt.idx + !!opt.len + !!opt.withmatchlen + (opt.minmatchlen ? 2 : 0);
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "LCS");
redis_cmd_append_sstr_zstr(&cmdstr, key1);
redis_cmd_append_sstr_zstr(&cmdstr, key2);
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, opt.idx, "IDX");
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, opt.len, "LEN");
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, opt.withmatchlen, "WITHMATCHLEN");
if (opt.minmatchlen) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "MINMATCHLEN");
redis_cmd_append_sstr_long(&cmdstr, opt.minmatchlen);
}
zend_string_release(key1);
zend_string_release(key2);
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int redis_slowlog_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
enum {SLOWLOG_GET, SLOWLOG_LEN, SLOWLOG_RESET} mode;
smart_string cmdstr = {0};
zend_string *op = NULL;
zend_long arg = 0;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(op)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(arg)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_string_equals_literal_ci(op, "GET")) {
mode = SLOWLOG_GET;
} else if (zend_string_equals_literal_ci(op, "LEN")) {
mode = SLOWLOG_LEN;
} else if (zend_string_equals_literal_ci(op, "RESET")) {
mode = SLOWLOG_RESET;
} else {
php_error_docref(NULL, E_WARNING, "Unknown SLOWLOG operation '%s'", ZSTR_VAL(op));
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + (mode == SLOWLOG_GET && ZEND_NUM_ARGS() == 2), "SLOWLOG");
redis_cmd_append_sstr_zstr(&cmdstr, op);
if (mode == SLOWLOG_GET && ZEND_NUM_ARGS() == 2)
redis_cmd_append_sstr_long(&cmdstr, arg);
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
void redis_get_restore_options(redisRestoreOptions *dst, HashTable *ht) {
zend_string *key;
zend_long lval;
zval *zv;
ZEND_ASSERT(dst != NULL);
memset(dst, 0, sizeof(*dst));
dst->idletime = dst->freq = -1;
if (ht == NULL)
return;
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, zv) {
ZVAL_DEREF(zv);
if (key) {
if (zend_string_equals_literal_ci(key, "IDLETIME")) {
lval = zval_get_long(zv);
if (lval < 0) {
php_error_docref(NULL, E_WARNING, "IDLETIME must be >= 0");
} else {
dst->idletime = lval;
dst->freq = -1;
}
} else if (zend_string_equals_literal_ci(key, "FREQ")) {
lval = zval_get_long(zv);
if (lval < 0 || lval > 255) {
php_error_docref(NULL, E_WARNING, "FREQ must be >= 0 and <= 255");
} else {
dst->freq = lval;
dst->idletime = -1;
}
} else {
php_error_docref(NULL, E_WARNING, "Unknown RESTORE option '%s'", ZSTR_VAL(key));
}
} else if (Z_TYPE_P(zv) == IS_STRING) {
if (zend_string_equals_literal_ci(Z_STR_P(zv), "REPLACE")) {
dst->replace = 1;
} else if (zend_string_equals_literal_ci(Z_STR_P(zv), "ABSTTL")) {
dst->absttl = 1;
} else {
php_error_docref(NULL, E_WARNING, "Unknown RESTORE option '%s'", Z_STRVAL_P(zv));
}
}
} ZEND_HASH_FOREACH_END();
}
/* RESTORE */
int redis_restore_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *key, *value = NULL;
smart_string cmdstr = {0};
HashTable *options = NULL;
redisRestoreOptions opt;
zend_long timeout = 0;
int argc;
ZEND_PARSE_PARAMETERS_START(3, 4) {
Z_PARAM_STR(key)
Z_PARAM_LONG(timeout)
Z_PARAM_STR(value)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT_OR_NULL(options)
} ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
redis_get_restore_options(&opt, options);
argc = 3 + (opt.idletime>-1?2:0) + (opt.freq>-1?2:0) + !!opt.absttl + !!opt.replace;
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "RESTORE");
redis_cmd_append_sstr_key(&cmdstr, ZSTR_VAL(key), ZSTR_LEN(key), redis_sock, slot);
redis_cmd_append_sstr_long(&cmdstr, timeout);
redis_cmd_append_sstr_zstr(&cmdstr, value);
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, opt.replace, "REPLACE");
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, opt.absttl, "ABSTTL");
if (opt.idletime > -1) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "IDLETIME");
redis_cmd_append_sstr_long(&cmdstr, opt.idletime);
}
if (opt.freq > -1) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "FREQ");
redis_cmd_append_sstr_long(&cmdstr, opt.freq);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* BITPOS key bit [start [end [BYTE | BIT]]] */
int redis_bitpos_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_long start = 0, end = -1;
zend_bool bit = 0, bybit = 0;
smart_string cmdstr = {0};
zend_string *key = NULL;
ZEND_PARSE_PARAMETERS_START(2, 5)
Z_PARAM_STR(key)
Z_PARAM_BOOL(bit)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(start)
Z_PARAM_LONG(end)
Z_PARAM_BOOL(bybit)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2 + (ZEND_NUM_ARGS() > 2 ? 2 : 0) + !!bybit, "BITPOS");
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
redis_cmd_append_sstr_long(&cmdstr, bit);
/* Start and length if we were passed either */
if (ZEND_NUM_ARGS() > 2) {
redis_cmd_append_sstr_long(&cmdstr, start);
redis_cmd_append_sstr_long(&cmdstr, end);
}
/* Finally, BIT or BYTE if we were passed that argument */
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, !!bybit, "BIT");
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* BITOP */
int redis_bitop_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zval *z_args;
int i, argc = ZEND_NUM_ARGS();
smart_string cmdstr = {0};
short s2;
// Allocate space for args, parse them as an array
z_args = emalloc(argc * sizeof(zval));
if (zend_get_parameters_array(ht, argc, z_args) == FAILURE ||
argc < 3 || Z_TYPE(z_args[0]) != IS_STRING)
{
efree(z_args);
return FAILURE;
}
// If we were passed a slot pointer, init to a sentinel value
if (slot) *slot = -1;
// Initialize command construction, add our operation argument
redis_cmd_init_sstr(&cmdstr, argc, ZEND_STRL("BITOP"));
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
// Now iterate over our keys argument
for (i = 1; i < argc; i++) {
// Append the key
redis_cmd_append_sstr_key_zval(&cmdstr, &z_args[i], redis_sock, slot ? &s2 : NULL);
// Verify slot if this is a Cluster request
if (slot) {
if (*slot != -1 && s2 != *slot) {
php_error_docref(NULL, E_WARNING, "Warning, not all keys hash to the same slot!");
efree(z_args);
efree(cmdstr.c);
return FAILURE;
}
*slot = s2;
}
}
// Free our argument array
efree(z_args);
// Push out variables
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* BITCOUNT */
int redis_bitcount_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key;
size_t key_len;
zend_long start = 0, end = -1;
zend_bool isbit = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|llb", &key, &key_len,
&start, &end, &isbit) == FAILURE)
{
return FAILURE;
}
if (isbit) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "BITCOUNT", "kdds", key, key_len,
(int)start, (int)end, "BIT", 3);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "BITCOUNT", "kdd", key, key_len,
(int)start, (int)end);
}
return SUCCESS;
}
/* PFADD and PFMERGE are the same except that in one case we serialize,
* and in the other case we key prefix */
static int redis_gen_pf_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, int kw_len, int is_keys, char **cmd,
int *cmd_len, short *slot)
{
smart_string cmdstr = {0};
zend_string *key = NULL;
HashTable *ht = NULL;
zval *z_ele;
int argc=1;
short s2;
// Parse arguments
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(key)
Z_PARAM_ARRAY_HT(ht)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
argc += zend_hash_num_elements(ht);
// We need at least two arguments
if (argc < 2) {
return FAILURE;
}
redis_cmd_init_sstr(&cmdstr, argc, kw, kw_len);
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
// Append our array of keys or serialized values */
ZEND_HASH_FOREACH_VAL(ht, z_ele) {
if (is_keys) {
redis_cmd_append_sstr_key_zval(&cmdstr, z_ele, redis_sock, slot ? &s2 : NULL);
if (slot && *slot != s2) {
php_error_docref(0, E_WARNING, "All keys must hash to the same slot!");
return FAILURE;
}
} else {
redis_cmd_append_sstr_zval(&cmdstr, z_ele, redis_sock);
}
} ZEND_HASH_FOREACH_END();
// Push output arguments
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* PFADD */
int redis_pfadd_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
return redis_gen_pf_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock,
ZEND_STRL("PFADD"), 0, cmd, cmd_len, slot);
}
/* PFMERGE */
int redis_pfmerge_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
return redis_gen_pf_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock,
ZEND_STRL("PFMERGE"), 1, cmd, cmd_len, slot);
}
/* PFCOUNT */
int redis_pfcount_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
zval *zarg = NULL, *zv;
short slot2 = -1;
uint32_t keys;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(zarg)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (Z_TYPE_P(zarg) == IS_STRING) {
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1, "PFCOUNT");
redis_cmd_append_sstr_key_zstr(&cmdstr, Z_STR_P(zarg), redis_sock, slot);
} else if (Z_TYPE_P(zarg) == IS_ARRAY) {
keys = zend_hash_num_elements(Z_ARRVAL_P(zarg));
if (keys == 0)
return FAILURE;
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, keys, "PFCOUNT");
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zarg), zv) {
redis_cmd_append_sstr_key_zval(&cmdstr, zv, redis_sock, slot);
if (slot) {
if (slot2 != -1 && slot2 != *slot)
goto cross_slot;
slot2 = *slot;
}
} ZEND_HASH_FOREACH_END();
} else {
php_error_docref(NULL, E_WARNING, "Argument must be either an array or a string");
return FAILURE;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
cross_slot:
php_error_docref(NULL, E_WARNING, "Not all keys hash to the same slot!");
efree(cmdstr.c);
return FAILURE;
}
int redis_auth_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *user = NULL, *pass = NULL;
zval *ztest;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z!", &ztest) == FAILURE ||
redis_extract_auth_info(ztest, &user, &pass) == FAILURE)
{
return FAILURE;
}
/* Construct either AUTH <user> <pass> or AUTH <pass> */
if (user && pass) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "AUTH", "SS", user, pass);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "AUTH", "S", pass);
}
redis_sock_set_auth(redis_sock, user, pass);
if (user) zend_string_release(user);
if (pass) zend_string_release(pass);
return SUCCESS;
}
/* SETBIT */
int redis_setbit_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key;
size_t key_len;
zend_long offset;
zend_bool val;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "slb", &key, &key_len,
&offset, &val) == FAILURE)
{
return FAILURE;
}
// Validate our offset
if (offset < BITOP_MIN_OFFSET || offset > BITOP_MAX_OFFSET) {
php_error_docref(0, E_WARNING,
"Invalid OFFSET for bitop command (must be between 0-2^32-1)");
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "SETBIT", "kld", key, key_len, offset, (int)val);
return SUCCESS;
}
int redis_lmove_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *src = NULL, *dst = NULL, *from = NULL, *to = NULL;
smart_string cmdstr = {0};
double timeout = 0.0;
short slot2 = 0;
int blocking;
blocking = toupper(*kw) == 'B';
ZEND_PARSE_PARAMETERS_START(4 + !!blocking, 4 + !!blocking)
Z_PARAM_STR(src)
Z_PARAM_STR(dst)
Z_PARAM_STR(from)
Z_PARAM_STR(to)
if (blocking) {
Z_PARAM_DOUBLE(timeout)
}
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (!zend_string_equals_literal_ci(from, "LEFT") && !zend_string_equals_literal_ci(from, "RIGHT")) {
php_error_docref(NULL, E_WARNING, "Wherefrom argument must be 'LEFT' or 'RIGHT'");
return FAILURE;
} else if (!zend_string_equals_literal_ci(to, "LEFT") && !zend_string_equals_literal_ci(to, "RIGHT")) {
php_error_docref(NULL, E_WARNING, "Whereto argument must be 'LEFT' or 'RIGHT'");
return FAILURE;
}
redis_cmd_init_sstr(&cmdstr, 4 + !!blocking, kw, strlen(kw));
redis_cmd_append_sstr_key_zstr(&cmdstr, src, redis_sock, slot);
redis_cmd_append_sstr_key_zstr(&cmdstr, dst, redis_sock, slot ? &slot2 : NULL);
/* Protect the user from CROSSLOT errors */
if (slot && slot2 != *slot) {
php_error_docref(NULL, E_WARNING, "Both keys must hash to the same slot!");
efree(cmdstr.c);
return FAILURE;
}
redis_cmd_append_sstr_zstr(&cmdstr, from);
redis_cmd_append_sstr_zstr(&cmdstr, to);
if (blocking) redis_cmd_append_sstr_dbl(&cmdstr, timeout);
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* LINSERT */
int redis_linsert_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key, *pos;
size_t key_len, pos_len;
zval *z_val, *z_pivot;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sszz", &key, &key_len,
&pos, &pos_len, &z_pivot, &z_val) == FAILURE)
{
return FAILURE;
}
// Validate position
if (strcasecmp(pos, "after") && strcasecmp(pos, "before")) {
php_error_docref(NULL, E_WARNING,
"Position must be either 'BEFORE' or 'AFTER'");
return FAILURE;
}
/* Construct command */
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "LINSERT", "ksvv", key, key_len, pos,
pos_len, z_pivot, z_val);
// Success
return SUCCESS;
}
/* LREM */
int redis_lrem_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key;
size_t key_len;
zend_long count = 0;
zval *z_val;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz|l", &key, &key_len,
&z_val, &count) == FAILURE)
{
return FAILURE;
}
/* Construct command */
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "LREM", "kdv", key, key_len, count, z_val);
// Success!
return SUCCESS;
}
int
redis_lpos_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key;
int argc = 2;
size_t key_len;
smart_string cmdstr = {0};
zend_bool withrank = 0;
zend_long rank = 0, count = -1, maxlen = -1;
zend_string *zkey;
zval *z_val, *z_ele, *z_opts = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz|a",
&key, &key_len, &z_val, &z_opts) == FAILURE)
{
return FAILURE;
}
if (z_opts != NULL) {
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(z_opts), zkey, z_ele) {
if (zkey != NULL) {
ZVAL_DEREF(z_ele);
if (zend_string_equals_literal_ci(zkey, "count")) {
count = zval_get_long(z_ele);
} else if (zend_string_equals_literal_ci(zkey, "maxlen")) {
maxlen = zval_get_long(z_ele);
} else if (zend_string_equals_literal_ci(zkey, "rank")) {
rank = zval_get_long(z_ele);
withrank = 1;
}
}
} ZEND_HASH_FOREACH_END();
}
argc += (withrank ? 2 : 0) + (count >= 0 ? 2 : 0) + (maxlen >= 0 ? 2 : 0);
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "LPOS");
redis_cmd_append_sstr_key(&cmdstr, key, key_len, redis_sock, slot);
redis_cmd_append_sstr_zval(&cmdstr, z_val, redis_sock);
if (withrank) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "RANK");
redis_cmd_append_sstr_long(&cmdstr, rank);
}
if (count >= 0) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "COUNT");
redis_cmd_append_sstr_long(&cmdstr, count);
*ctx = PHPREDIS_CTX_PTR;
}
if (maxlen >= 0) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "MAXLEN");
redis_cmd_append_sstr_long(&cmdstr, maxlen);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int redis_smove_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *src = NULL, *dst = NULL;
smart_string cmdstr = {0};
zval *zv = NULL;
short slot2;
ZEND_PARSE_PARAMETERS_START(3, 3) {
Z_PARAM_STR(src)
Z_PARAM_STR(dst)
Z_PARAM_ZVAL(zv)
} ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 3, "SMOVE");
redis_cmd_append_sstr_key_zstr(&cmdstr, src, redis_sock, slot);
redis_cmd_append_sstr_key_zstr(&cmdstr, dst, redis_sock, slot ? &slot2 : NULL);
redis_cmd_append_sstr_zval(&cmdstr, zv, redis_sock);
if (slot && *slot != slot2) {
php_error_docref(0, E_WARNING, "Source and destination keys don't hash to the same slot!");
efree(cmdstr.c);
return FAILURE;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* HSET */
int redis_hset_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
int i, argc;
smart_string cmdstr = {0};
zend_string *key, *zkey;
zval *args, *z_ele;
ZEND_PARSE_PARAMETERS_START(2, -1)
Z_PARAM_STR(key)
Z_PARAM_VARIADIC('*', args, argc)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (argc == 1) {
if (Z_TYPE_P(args) != IS_ARRAY || zend_hash_num_elements(Z_ARRVAL_P(args)) == 0) {
return FAILURE;
}
/* Initialize our command */
redis_cmd_init_sstr(&cmdstr, 1 + zend_hash_num_elements(Z_ARRVAL_P(args)) * 2, ZEND_STRL("HSET"));
/* Append key */
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(args), zkey, z_ele) {
if (zkey != NULL) {
ZVAL_DEREF(z_ele);
redis_cmd_append_sstr(&cmdstr, ZSTR_VAL(zkey), ZSTR_LEN(zkey));
redis_cmd_append_sstr_zval(&cmdstr, z_ele, redis_sock);
}
} ZEND_HASH_FOREACH_END();
} else {
if (argc % 2 != 0) {
return FAILURE;
}
/* Initialize our command */
redis_cmd_init_sstr(&cmdstr, argc + 1, ZEND_STRL("HSET"));
/* Append key */
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
for (i = 0; i < argc; ++i) {
if (i % 2) {
redis_cmd_append_sstr_zval(&cmdstr, &args[i], redis_sock);
} else {
redis_cmd_append_sstr_zval(&cmdstr, &args[i], NULL);
}
}
}
// Push out values
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* HSETNX */
int redis_hsetnx_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key, *mem;
size_t key_len, mem_len;
zval *z_val;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssz", &key, &key_len,
&mem, &mem_len, &z_val) == FAILURE)
{
return FAILURE;
}
/* Construct command */
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "HSETNX", "ksv", key, key_len, mem, mem_len, z_val);
// Success
return SUCCESS;
}
int
redis_hrandfield_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key;
int count = 0;
size_t key_len;
smart_string cmdstr = {0};
zend_bool withvalues = 0;
zval *z_opts = NULL, *z_ele;
zend_string *zkey;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|a",
&key, &key_len, &z_opts) == FAILURE)
{
return FAILURE;
}
if (z_opts != NULL) {
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(z_opts), zkey, z_ele) {
if (zkey != NULL) {
ZVAL_DEREF(z_ele);
if (zend_string_equals_literal_ci(zkey, "count")) {
count = zval_get_long(z_ele);
} else if (zend_string_equals_literal_ci(zkey, "withvalues")) {
withvalues = zval_is_true(z_ele);
}
} else if (Z_TYPE_P(z_ele) == IS_STRING) {
if (zend_string_equals_literal_ci(Z_STR_P(z_ele), "WITHVALUES")) {
withvalues = 1;
}
}
} ZEND_HASH_FOREACH_END();
}
/* If we're sending WITHVALUES we must also send a count */
if (count == 0 && withvalues)
count = 1;
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + (count != 0) + withvalues, "HRANDFIELD");
redis_cmd_append_sstr_key(&cmdstr, key, key_len, redis_sock, slot);
if (count != 0) {
redis_cmd_append_sstr_long(&cmdstr, count);
*ctx = PHPREDIS_CTX_PTR;
}
if (withvalues) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "WITHVALUES");
*ctx = PHPREDIS_CTX_PTR + 1;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int redis_select_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_long db = 0;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(db)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (db < 0 || db > INT_MAX)
return FAILURE;
*ctx = (void*)(uintptr_t)db;
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "SELECT", "d", db);
return SUCCESS;
}
/* SRANDMEMBER */
int redis_srandmember_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
uint32_t argc = ZEND_NUM_ARGS();
smart_string cmdstr = {0};
zend_string *key = NULL;
zend_long count = 0;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(key)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(count)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, ZEND_NUM_ARGS(), "SRANDMEMBER");
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
if (argc == 2)
redis_cmd_append_sstr_long(&cmdstr, count);
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
*ctx = argc == 2 ? PHPREDIS_CTX_PTR : NULL;
return SUCCESS;
}
/* ZINCRBY */
int redis_zincrby_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key;
size_t key_len;
double incrby;
zval *z_val;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sdz", &key, &key_len,
&incrby, &z_val) == FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "ZINCRBY", "kfv", key, key_len, incrby, z_val);
return SUCCESS;
}
/* SORT */
int redis_sort_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx)
{
zval *z_opts=NULL, *z_ele, z_argv;
char *key;
HashTable *ht_opts;
smart_string cmdstr = {0};
size_t key_len;
int key_free;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|a", &key, &key_len,
&z_opts) == FAILURE)
{
return FAILURE;
}
// If we don't have an options array, the command is quite simple
if (!z_opts || zend_hash_num_elements(Z_ARRVAL_P(z_opts)) == 0) {
// Construct command
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "k", key, key_len);
return SUCCESS;
}
// Create our hash table to hold our sort arguments
array_init(&z_argv);
// SORT <key>
key_free = redis_key_prefix(redis_sock, &key, &key_len);
add_next_index_stringl(&z_argv, key, key_len);
if (key_free) efree(key);
// Set slot
CMD_SET_SLOT(slot,key,key_len);
// Grab the hash table
ht_opts = Z_ARRVAL_P(z_opts);
// Handle BY pattern
if (((z_ele = zend_hash_str_find(ht_opts, "by", sizeof("by") - 1)) != NULL ||
(z_ele = zend_hash_str_find(ht_opts, "BY", sizeof("BY") - 1)) != NULL
) && Z_TYPE_P(z_ele) == IS_STRING
) {
// "BY" option is disabled in cluster
if (slot) {
php_error_docref(NULL, E_WARNING,
"SORT BY option is not allowed in Redis Cluster");
zval_dtor(&z_argv);
return FAILURE;
}
// ... BY <pattern>
add_next_index_stringl(&z_argv, "BY", sizeof("BY") - 1);
add_next_index_stringl(&z_argv, Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele));
}
// Handle ASC/DESC option
if (((z_ele = zend_hash_str_find(ht_opts, "sort", sizeof("sort") - 1)) != NULL ||
(z_ele = zend_hash_str_find(ht_opts, "SORT", sizeof("SORT") - 1)) != NULL
) && Z_TYPE_P(z_ele) == IS_STRING
) {
// 'asc'|'desc'
add_next_index_stringl(&z_argv, Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele));
}
// STORE option
if (((z_ele = zend_hash_str_find(ht_opts, "store", sizeof("store") - 1)) != NULL ||
(z_ele = zend_hash_str_find(ht_opts, "STORE", sizeof("STORE") - 1)) != NULL
) && Z_TYPE_P(z_ele) == IS_STRING
) {
// Slot verification
int cross_slot = slot && *slot != cluster_hash_key(
Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele));
if (cross_slot) {
php_error_docref(0, E_WARNING,
"Error, SORT key and STORE key have different slots!");
zval_dtor(&z_argv);
return FAILURE;
}
// STORE <key>
add_next_index_stringl(&z_argv, "STORE", sizeof("STORE") - 1);
add_next_index_stringl(&z_argv, Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele));
// We are using STORE
*ctx = PHPREDIS_CTX_PTR;
}
// GET option
if (((z_ele = zend_hash_str_find(ht_opts, "get", sizeof("get") - 1)) != NULL ||
(z_ele = zend_hash_str_find(ht_opts, "GET", sizeof("GET") - 1)) != NULL
) && (Z_TYPE_P(z_ele) == IS_STRING || Z_TYPE_P(z_ele) == IS_ARRAY)
) {
// Disabled in cluster
if (slot) {
php_error_docref(NULL, E_WARNING,
"GET option for SORT disabled in Redis Cluster");
zval_dtor(&z_argv);
return FAILURE;
}
// If it's a string just add it
if (Z_TYPE_P(z_ele) == IS_STRING) {
add_next_index_stringl(&z_argv, "GET", sizeof("GET") - 1);
add_next_index_stringl(&z_argv, Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele));
} else {
int added = 0;
zval *z_key;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(z_ele), z_key) {
// If we can't get the data, or it's not a string, skip
if (z_key == NULL || Z_TYPE_P(z_key) != IS_STRING) {
continue;
}
/* Add get per thing we're getting */
add_next_index_stringl(&z_argv, "GET", sizeof("GET") - 1);
// Add this key to our argv array
add_next_index_stringl(&z_argv, Z_STRVAL_P(z_key), Z_STRLEN_P(z_key));
added++;
} ZEND_HASH_FOREACH_END();
// Make sure we were able to add at least one
if (added == 0) {
php_error_docref(NULL, E_WARNING,
"Array of GET values requested, but none are valid");
zval_dtor(&z_argv);
return FAILURE;
}
}
}
// ALPHA
if (((z_ele = zend_hash_str_find(ht_opts, "alpha", sizeof("alpha") - 1)) != NULL ||
(z_ele = zend_hash_str_find(ht_opts, "ALPHA", sizeof("ALPHA") - 1)) != NULL) &&
zval_is_true(z_ele)
) {
add_next_index_stringl(&z_argv, "ALPHA", sizeof("ALPHA") - 1);
}
// LIMIT <offset> <count>
if (((z_ele = zend_hash_str_find(ht_opts, "limit", sizeof("limit") - 1)) != NULL ||
(z_ele = zend_hash_str_find(ht_opts, "LIMIT", sizeof("LIMIT") - 1)) != NULL
) && Z_TYPE_P(z_ele) == IS_ARRAY
) {
HashTable *ht_off = Z_ARRVAL_P(z_ele);
zval *z_off, *z_cnt;
if ((z_off = zend_hash_index_find(ht_off, 0)) != NULL &&
(z_cnt = zend_hash_index_find(ht_off, 1)) != NULL
) {
if ((Z_TYPE_P(z_off) != IS_STRING && Z_TYPE_P(z_off) != IS_LONG) ||
(Z_TYPE_P(z_cnt) != IS_STRING && Z_TYPE_P(z_cnt) != IS_LONG)
) {
php_error_docref(NULL, E_WARNING,
"LIMIT options on SORT command must be longs or strings");
zval_dtor(&z_argv);
return FAILURE;
}
// Add LIMIT argument
add_next_index_stringl(&z_argv, "LIMIT", sizeof("LIMIT") - 1);
long low, high;
if (Z_TYPE_P(z_off) == IS_STRING) {
low = atol(Z_STRVAL_P(z_off));
} else {
low = Z_LVAL_P(z_off);
}
if (Z_TYPE_P(z_cnt) == IS_STRING) {
high = atol(Z_STRVAL_P(z_cnt));
} else {
high = Z_LVAL_P(z_cnt);
}
// Add our two LIMIT arguments
add_next_index_long(&z_argv, low);
add_next_index_long(&z_argv, high);
}
}
// Start constructing our command
HashTable *ht_argv = Z_ARRVAL_P(&z_argv);
redis_cmd_init_sstr(&cmdstr, zend_hash_num_elements(ht_argv), kw, strlen(kw));
// Iterate through our arguments
ZEND_HASH_FOREACH_VAL(ht_argv, z_ele) {
// Args are strings or longs
if (Z_TYPE_P(z_ele) == IS_STRING) {
redis_cmd_append_sstr(&cmdstr,Z_STRVAL_P(z_ele),
Z_STRLEN_P(z_ele));
} else {
redis_cmd_append_sstr_long(&cmdstr, Z_LVAL_P(z_ele));
}
} ZEND_HASH_FOREACH_END();
/* Clean up our arguments array. Note we don't have to free any prefixed
* key as that we didn't duplicate the pointer if we prefixed */
zval_dtor(&z_argv);
// Push our length and command
*cmd_len = cmdstr.len;
*cmd = cmdstr.c;
// Success!
return SUCCESS;
}
/* HDEL */
int redis_hdel_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
zend_string *key = NULL;
int i;
int argc = 0;
zval *args;
ZEND_PARSE_PARAMETERS_START(2, -1)
Z_PARAM_STR(key)
Z_PARAM_VARIADIC('*', args, argc)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
// Start command construction
redis_cmd_init_sstr(&cmdstr, argc + 1, ZEND_STRL("HDEL"));
// Append key
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
// Iterate through the members we're removing
for (i = 0; i < argc; i++) {
redis_cmd_append_sstr_zval(&cmdstr, &args[i], NULL);
}
// Push out values
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
// Success!
return SUCCESS;
}
/* ZADD */
int redis_zadd_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *zstr, *key = NULL, *exp_type = NULL, *range_type = NULL;
zend_bool ch = 0, incr = 0;
smart_string cmdstr = {0};
zval *argv = NULL, *z_opt;
int argc = 0, pos = 0;
ZEND_PARSE_PARAMETERS_START(3, -1)
Z_PARAM_STR(key)
Z_PARAM_VARIADIC('*', argv, argc)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
// Need key, [NX|XX] [LT|GT] [CH] [INCR] score, value, [score, value...] */
if (argc % 2 != 0) {
if (Z_TYPE(argv[0]) != IS_ARRAY) {
return FAILURE;
}
ZEND_HASH_FOREACH_VAL(Z_ARRVAL(argv[0]), z_opt) {
if (Z_TYPE_P(z_opt) == IS_STRING) {
zstr = Z_STR_P(z_opt);
if (zend_string_equals_literal_ci(zstr, "NX") || zend_string_equals_literal_ci(zstr, "XX")) {
exp_type = Z_STR_P(z_opt);
} else if (zend_string_equals_literal_ci(zstr, "LT") || zend_string_equals_literal_ci(zstr, "GT")) {
range_type = Z_STR_P(z_opt);
} else if (zend_string_equals_literal_ci(zstr, "CH")) {
ch = 1;
} else if (zend_string_equals_literal_ci(zstr, "INCR")) {
if (argc != 3) {
// Only one score-element pair can be specified in this mode.
return FAILURE;
}
incr = 1;
}
}
} ZEND_HASH_FOREACH_END();
pos++;
}
// Start command construction
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + (argc - pos) + !!exp_type + !!range_type + !!ch + !!incr, "ZADD");
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
if (exp_type) redis_cmd_append_sstr_zstr(&cmdstr, exp_type);
if (range_type) redis_cmd_append_sstr_zstr(&cmdstr, range_type);
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, ch, "CH");
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, incr, "INCR");
// Now the rest of our arguments
while (pos < argc) {
// Append score and member
if (redis_cmd_append_sstr_score(&cmdstr, &argv[pos]) == FAILURE) {
smart_string_free(&cmdstr);
return FAILURE;
}
redis_cmd_append_sstr_zval(&cmdstr, &argv[pos+1], redis_sock);
pos += 2;
}
// Push output values
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
*ctx = incr ? PHPREDIS_CTX_PTR : NULL;
return SUCCESS;
}
/* OBJECT */
int redis_object_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *subcmd = NULL, *key = NULL;
smart_string cmdstr = {0};
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STR(subcmd)
Z_PARAM_STR(key)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_string_equals_literal_ci(subcmd, "REFCOUNT") ||
zend_string_equals_literal_ci(subcmd, "IDLETIME"))
{
*ctx = PHPREDIS_CTX_PTR;
} else if (zend_string_equals_literal_ci(subcmd, "ENCODING")) {
*ctx = PHPREDIS_CTX_PTR + 1;
} else {
php_error_docref(NULL, E_WARNING, "Invalid subcommand sent to OBJECT");
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2, "OBJECT");
redis_cmd_append_sstr_zstr(&cmdstr, subcmd);
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int
redis_geoadd_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zval *z_args, *z_ele;
smart_string cmdstr = {0};
zend_bool ch = 0;
zend_string *zstr;
char *mode = NULL;
int argc, i;
// We at least need a key and three values
if ((argc = ZEND_NUM_ARGS()) < 4 || (argc % 3 != 1 && argc % 3 != 2)) {
zend_wrong_param_count();
return FAILURE;
}
// Make sure we at least have a key, and we can get other args
z_args = ecalloc(argc, sizeof(*z_args));
if (zend_get_parameters_array(ht, argc, z_args) == FAILURE) {
efree(z_args);
return FAILURE;
}
if (argc % 3 == 2) {
argc--;
if (Z_TYPE(z_args[argc]) != IS_ARRAY) {
php_error_docref(NULL, E_WARNING, "Invalid options value");
efree(z_args);
return FAILURE;
}
ZEND_HASH_FOREACH_VAL(Z_ARRVAL(z_args[argc]), z_ele) {
ZVAL_DEREF(z_ele);
if (Z_TYPE_P(z_ele) == IS_STRING) {
if (zend_string_equals_literal_ci(Z_STR_P(z_ele), "NX") ||
zend_string_equals_literal_ci(Z_STR_P(z_ele), "XX"))
{
mode = Z_STRVAL_P(z_ele);
} else if (zend_string_equals_literal_ci(Z_STR_P(z_ele), "CH")) {
ch = 1;
}
}
} ZEND_HASH_FOREACH_END();
}
/* Initialize our command */
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc + (mode != NULL) + ch, "GEOADD");
/* Append key */
zstr = zval_get_string(&z_args[0]);
redis_cmd_append_sstr_key(&cmdstr, ZSTR_VAL(zstr), ZSTR_LEN(zstr), redis_sock, slot);
zend_string_release(zstr);
/* Append options */
if (mode != NULL) {
redis_cmd_append_sstr(&cmdstr, mode, strlen(mode));
}
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, ch, "CH");
/* Append members */
for (i = 1; i < argc; ++i) {
redis_cmd_append_sstr_zval(&cmdstr, &z_args[i], redis_sock);
}
// Cleanup arg array
efree(z_args);
// Push out values
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* GEODIST */
int redis_geodist_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key, *source, *dest, *unit = NULL;
size_t keylen, sourcelen, destlen, unitlen;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|s", &key, &keylen,
&source, &sourcelen, &dest, &destlen, &unit,
&unitlen) == FAILURE)
{
return FAILURE;
}
/* Construct command */
if (unit != NULL) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "GEODIST", "ksss", key, keylen, source,
sourcelen, dest, destlen, unit, unitlen);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "GEODIST", "kss", key, keylen, source,
sourcelen, dest, destlen);
}
return SUCCESS;
}
geoStoreType get_georadius_store_type(zend_string *key) {
if (ZSTR_LEN(key) == 5 && !strcasecmp(ZSTR_VAL(key), "store")) {
return STORE_COORD;
} else if (ZSTR_LEN(key) == 9 && !strcasecmp(ZSTR_VAL(key), "storedist")) {
return STORE_DIST;
}
return STORE_NONE;
}
/* Helper function to get COUNT and possible ANY flag which is passable to
* both GEORADIUS and GEOSEARCH */
static int get_georadius_count_options(zval *optval, geoOptions *opts) {
zval *z_tmp;
/* Short circuit on bad options */
if (Z_TYPE_P(optval) != IS_ARRAY && Z_TYPE_P(optval) != IS_LONG)
goto error;
if (Z_TYPE_P(optval) == IS_ARRAY) {
z_tmp = zend_hash_index_find(Z_ARRVAL_P(optval), 0);
if (z_tmp) {
if (Z_TYPE_P(z_tmp) != IS_LONG || Z_LVAL_P(z_tmp) <= 0)
goto error;
opts->count = Z_LVAL_P(z_tmp);
}
z_tmp = zend_hash_index_find(Z_ARRVAL_P(optval), 1);
if (z_tmp) {
opts->any = zval_is_true(z_tmp);
}
} else {
if (Z_LVAL_P(optval) <= 0)
goto error;
opts->count = Z_LVAL_P(optval);
}
return SUCCESS;
error:
php_error_docref(NULL, E_WARNING, "Invalid COUNT value");
return FAILURE;
}
/* Helper function to extract optional arguments for GEORADIUS and GEORADIUSBYMEMBER */
static int get_georadius_opts(HashTable *ht, geoOptions *opts) {
zend_string *zkey;
char *optstr;
zval *optval;
/* Iterate over our argument array, collating which ones we have */
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, zkey, optval) {
ZVAL_DEREF(optval);
/* If the key is numeric it's a non value option */
if (zkey) {
if (zend_string_equals_literal_ci(zkey, "COUNT")) {
if (get_georadius_count_options(optval, opts) == FAILURE) {
if (opts->key) zend_string_release(opts->key);
return FAILURE;
}
} else if (opts->store == STORE_NONE) {
opts->store = get_georadius_store_type(zkey);
if (opts->store != STORE_NONE) {
opts->key = zval_get_string(optval);
}
}
} else {
/* Option needs to be a string */
if (Z_TYPE_P(optval) != IS_STRING) continue;
optstr = Z_STRVAL_P(optval);
if (!strcasecmp(optstr, "withcoord")) {
opts->withcoord = 1;
} else if (!strcasecmp(optstr, "withdist")) {
opts->withdist = 1;
} else if (!strcasecmp(optstr, "withhash")) {
opts->withhash = 1;
} else if (!strcasecmp(optstr, "asc")) {
opts->sort = SORT_ASC;
} else if (!strcasecmp(optstr, "desc")) {
opts->sort = SORT_DESC;
}
}
} ZEND_HASH_FOREACH_END();
/* STORE and STOREDIST are not compatible with the WITH* options */
if (opts->key != NULL && (opts->withcoord || opts->withdist || opts->withhash)) {
php_error_docref(NULL, E_WARNING,
"STORE[DIST] is not compatible with WITHCOORD, WITHDIST or WITHHASH");
if (opts->key) zend_string_release(opts->key);
return FAILURE;
}
/* Success */
return SUCCESS;
}
/* Helper to append options to a GEORADIUS or GEORADIUSBYMEMBER command */
void append_georadius_opts(RedisSock *redis_sock, smart_string *str, short *slot,
geoOptions *opt)
{
if (opt->withcoord)
REDIS_CMD_APPEND_SSTR_STATIC(str, "WITHCOORD");
if (opt->withdist)
REDIS_CMD_APPEND_SSTR_STATIC(str, "WITHDIST");
if (opt->withhash)
REDIS_CMD_APPEND_SSTR_STATIC(str, "WITHHASH");
/* Append sort if it's not GEO_NONE */
if (opt->sort == SORT_ASC) {
REDIS_CMD_APPEND_SSTR_STATIC(str, "ASC");
} else if (opt->sort == SORT_DESC) {
REDIS_CMD_APPEND_SSTR_STATIC(str, "DESC");
}
/* Append our count if we've got one */
if (opt->count) {
REDIS_CMD_APPEND_SSTR_STATIC(str, "COUNT");
redis_cmd_append_sstr_long(str, opt->count);
if (opt->any) {
REDIS_CMD_APPEND_SSTR_STATIC(str, "ANY");
}
}
/* Append store options if we've got them */
if (opt->store != STORE_NONE && opt->key != NULL) {
if (opt->store == STORE_COORD) {
REDIS_CMD_APPEND_SSTR_STATIC(str, "STORE");
} else {
REDIS_CMD_APPEND_SSTR_STATIC(str, "STOREDIST");
}
redis_cmd_append_sstr_key_zstr(str, opt->key, redis_sock, slot);
}
}
/* GEORADIUS / GEORADIUS_RO */
int redis_georadius_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zend_string *key = NULL, *unit = NULL;
double lng = 0, lat = 0, radius = 0;
smart_string cmdstr = {0};
HashTable *opts = NULL;
geoOptions gopts = {0};
short store_slot = -1;
uint32_t argc;
ZEND_PARSE_PARAMETERS_START(5, 6)
Z_PARAM_STR(key)
Z_PARAM_DOUBLE(lng)
Z_PARAM_DOUBLE(lat)
Z_PARAM_DOUBLE(radius)
Z_PARAM_STR(unit)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT_OR_NULL(opts)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
/* Parse any GEORADIUS options we have */
if (opts != NULL && get_georadius_opts(opts, &gopts) != SUCCESS)
return FAILURE;
/* Increment argc depending on options */
argc = 5 + gopts.withcoord + gopts.withdist + gopts.withhash +
(gopts.sort != SORT_NONE) + (gopts.count ? 2 + gopts.any : 0) +
(gopts.store != STORE_NONE ? 2 : 0);
/* Begin construction of our command */
redis_cmd_init_sstr(&cmdstr, argc, kw, strlen(kw));
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
/* Append required arguments */
redis_cmd_append_sstr_dbl(&cmdstr, lng);
redis_cmd_append_sstr_dbl(&cmdstr, lat);
redis_cmd_append_sstr_dbl(&cmdstr, radius);
redis_cmd_append_sstr_zstr(&cmdstr, unit);
/* Append optional arguments */
append_georadius_opts(redis_sock, &cmdstr, slot ? &store_slot : NULL, &gopts);
/* Free key if it was prefixed */
if (gopts.key) zend_string_release(gopts.key);
/* Protect the user from CROSSSLOT if we're in cluster */
if (slot && gopts.store != STORE_NONE && *slot != store_slot) {
php_error_docref(NULL, E_WARNING,
"Key and STORE[DIST] key must hash to the same slot");
efree(cmdstr.c);
return FAILURE;
}
/* Set slot, command and len, and return */
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* GEORADIUSBYMEMBER/GEORADIUSBYMEMBER_RO
* key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] */
int redis_georadiusbymember_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key, *mem, *unit;
size_t keylen, memlen, unitlen;
short store_slot = 0;
int keyfree, argc = 4;
double radius;
geoOptions gopts = {0};
zval *opts = NULL;
smart_string cmdstr = {0};
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssds|a", &key, &keylen,
&mem, &memlen, &radius, &unit, &unitlen, &opts) == FAILURE)
{
return FAILURE;
}
if (opts != NULL) {
/* Attempt to parse our options array */
if (get_georadius_opts(Z_ARRVAL_P(opts), &gopts) == FAILURE) {
return FAILURE;
}
}
/* Increment argc based on options */
argc += gopts.withcoord + gopts.withdist + gopts.withhash +
(gopts.sort != SORT_NONE) + (gopts.count ? 2 + gopts.any : 0) +
(gopts.store != STORE_NONE ? 2 : 0);
/* Begin command construction*/
redis_cmd_init_sstr(&cmdstr, argc, kw, strlen(kw));
/* Prefix our key if we're prefixing and set the slot */
keyfree = redis_key_prefix(redis_sock, &key, &keylen);
CMD_SET_SLOT(slot, key, keylen);
/* Append required arguments */
redis_cmd_append_sstr(&cmdstr, key, keylen);
redis_cmd_append_sstr(&cmdstr, mem, memlen);
redis_cmd_append_sstr_long(&cmdstr, radius);
redis_cmd_append_sstr(&cmdstr, unit, unitlen);
/* Append options */
append_georadius_opts(redis_sock, &cmdstr, slot ? &store_slot : NULL, &gopts);
/* Free key if we prefixed */
if (keyfree) efree(key);
if (gopts.key) zend_string_release(gopts.key);
/* Protect the user from CROSSSLOT if we're in cluster */
if (slot && gopts.store != STORE_NONE && *slot != store_slot) {
php_error_docref(NULL, E_WARNING,
"Key and STORE[DIST] key must hash to the same slot");
efree(cmdstr.c);
return FAILURE;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int
redis_geosearch_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *key, *unit;
int argc = 2;
size_t keylen, unitlen;
geoOptions gopts = {0};
smart_string cmdstr = {0};
zval *position, *shape, *opts = NULL, *z_ele;
zend_string *zkey, *zstr;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "szzs|a",
&key, &keylen, &position, &shape,
&unit, &unitlen, &opts) == FAILURE)
{
return FAILURE;
}
if (Z_TYPE_P(position) == IS_STRING && Z_STRLEN_P(position) > 0) {
argc += 2;
} else if (Z_TYPE_P(position) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(position)) == 2) {
argc += 3;
} else {
php_error_docref(NULL, E_WARNING, "Invalid position");
return FAILURE;
}
if (Z_TYPE_P(shape) == IS_LONG || Z_TYPE_P(shape) == IS_DOUBLE) {
argc += 2;
} else if (Z_TYPE_P(shape) == IS_ARRAY) {
argc += 3;
} else {
php_error_docref(NULL, E_WARNING, "Invalid shape dimensions");
return FAILURE;
}
/* Attempt to parse our options array */
if (opts != NULL) {
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(opts), zkey, z_ele) {
ZVAL_DEREF(z_ele);
if (zkey != NULL && zend_string_equals_literal_ci(zkey, "COUNT")) {
if (get_georadius_count_options(z_ele, &gopts) == FAILURE) {
return FAILURE;
}
} else if (Z_TYPE_P(z_ele) == IS_STRING) {
zstr = Z_STR_P(z_ele);
if (zend_string_equals_literal_ci(zstr, "WITHCOORD")) {
gopts.withcoord = 1;
} else if (zend_string_equals_literal_ci(zstr, "WITHDIST")) {
gopts.withdist = 1;
} else if (zend_string_equals_literal_ci(zstr, "WITHHASH")) {
gopts.withhash = 1;
} else if (zend_string_equals_literal_ci(zstr, "ASC")) {
gopts.sort = SORT_ASC;
} else if (zend_string_equals_literal_ci(zstr, "DESC")) {
gopts.sort = SORT_DESC;
}
}
} ZEND_HASH_FOREACH_END();
}
/* Increment argc based on options */
argc += gopts.withcoord + gopts.withdist + gopts.withhash
+ (gopts.sort != SORT_NONE) + (gopts.count ? 2 + gopts.any : 0);
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "GEOSEARCH");
redis_cmd_append_sstr_key(&cmdstr, key, keylen, redis_sock, slot);
if (Z_TYPE_P(position) == IS_ARRAY) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "FROMLONLAT");
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(position), z_ele) {
ZVAL_DEREF(z_ele);
redis_cmd_append_sstr_dbl(&cmdstr, zval_get_double(z_ele));
} ZEND_HASH_FOREACH_END();
} else {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "FROMMEMBER");
redis_cmd_append_sstr(&cmdstr, Z_STRVAL_P(position), Z_STRLEN_P(position));
}
if (Z_TYPE_P(shape) == IS_ARRAY) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "BYBOX");
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(shape), z_ele) {
ZVAL_DEREF(z_ele);
redis_cmd_append_sstr_dbl(&cmdstr, zval_get_double(z_ele));
} ZEND_HASH_FOREACH_END();
} else {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "BYRADIUS");
redis_cmd_append_sstr_dbl(&cmdstr, zval_get_double(shape));
}
redis_cmd_append_sstr(&cmdstr, unit, unitlen);
/* Append optional arguments */
if (gopts.withcoord) REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "WITHCOORD");
if (gopts.withdist) REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "WITHDIST");
if (gopts.withhash) REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "WITHHASH");
/* Append sort if it's not GEO_NONE */
if (gopts.sort == SORT_ASC) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "ASC");
} else if (gopts.sort == SORT_DESC) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "DESC");
}
/* Append our count if we've got one */
if (gopts.count) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "COUNT");
redis_cmd_append_sstr_long(&cmdstr, gopts.count);
if (gopts.any) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "ANY");
}
}
if ((argc = gopts.withcoord + gopts.withdist + gopts.withhash) > 0) {
*ctx = PHPREDIS_CTX_PTR;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int
redis_geosearchstore_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
int argc = 3;
char *dest, *src, *unit;
size_t destlen, srclen, unitlen;
geoOptions gopts = {0};
smart_string cmdstr = {0};
zval *position, *shape, *opts = NULL, *z_ele;
zend_string *zkey;
short s2 = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sszzs|a",
&dest, &destlen, &src, &srclen, &position, &shape,
&unit, &unitlen, &opts) == FAILURE)
{
return FAILURE;
}
if (Z_TYPE_P(position) == IS_STRING && Z_STRLEN_P(position) > 0) {
argc += 2;
} else if (Z_TYPE_P(position) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(position)) == 2) {
argc += 3;
} else {
php_error_docref(NULL, E_WARNING, "Invalid position");
return FAILURE;
}
if (Z_TYPE_P(shape) == IS_LONG || Z_TYPE_P(shape) == IS_DOUBLE) {
argc += 2;
} else if (Z_TYPE_P(shape) == IS_ARRAY) {
argc += 3;
} else {
php_error_docref(NULL, E_WARNING, "Invalid shape dimensions");
return FAILURE;
}
/* Attempt to parse our options array */
if (opts != NULL) {
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(opts), zkey, z_ele) {
ZVAL_DEREF(z_ele);
if (zkey != NULL) {
if (zend_string_equals_literal_ci(zkey, "COUNT")) {
if (Z_TYPE_P(z_ele) != IS_LONG || Z_LVAL_P(z_ele) <= 0) {
php_error_docref(NULL, E_WARNING, "COUNT must be an integer > 0!");
return FAILURE;
}
gopts.count = Z_LVAL_P(z_ele);
}
} else if (Z_TYPE_P(z_ele) == IS_STRING) {
if (!strcasecmp(Z_STRVAL_P(z_ele), "ASC")) {
gopts.sort = SORT_ASC;
} else if (!strcasecmp(Z_STRVAL_P(z_ele), "DESC")) {
gopts.sort = SORT_DESC;
} else if (!strcasecmp(Z_STRVAL_P(z_ele), "STOREDIST")) {
gopts.store = STORE_DIST;
}
}
} ZEND_HASH_FOREACH_END();
}
/* Increment argc based on options */
argc += gopts.withcoord + gopts.withdist + gopts.withhash
+ (gopts.sort != SORT_NONE) + (gopts.count ? 2 : 0)
+ (gopts.store != STORE_NONE);
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "GEOSEARCHSTORE");
redis_cmd_append_sstr_key(&cmdstr, dest, destlen, redis_sock, slot);
redis_cmd_append_sstr_key(&cmdstr, src, srclen, redis_sock, slot ? &s2 : NULL);
if (slot && *slot != s2) {
php_error_docref(NULL, E_WARNING, "All keys must hash to the same slot");
efree(cmdstr.c);
return FAILURE;
}
if (Z_TYPE_P(position) == IS_ARRAY) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "FROMLONLAT");
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(position), z_ele) {
ZVAL_DEREF(z_ele);
redis_cmd_append_sstr_dbl(&cmdstr, zval_get_double(z_ele));
} ZEND_HASH_FOREACH_END();
} else {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "FROMMEMBER");
redis_cmd_append_sstr(&cmdstr, Z_STRVAL_P(position), Z_STRLEN_P(position));
}
if (Z_TYPE_P(shape) == IS_ARRAY) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "BYBOX");
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(shape), z_ele) {
ZVAL_DEREF(z_ele);
redis_cmd_append_sstr_dbl(&cmdstr, zval_get_double(z_ele));
} ZEND_HASH_FOREACH_END();
} else {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "BYRADIUS");
redis_cmd_append_sstr_dbl(&cmdstr, zval_get_double(shape));
}
redis_cmd_append_sstr(&cmdstr, unit, unitlen);
/* Append sort if it's not GEO_NONE */
if (gopts.sort == SORT_ASC) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "ASC");
} else if (gopts.sort == SORT_DESC) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "DESC");
}
/* Append our count if we've got one */
if (gopts.count) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "COUNT");
redis_cmd_append_sstr_long(&cmdstr, gopts.count);
}
if (gopts.store == STORE_DIST) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "STOREDIST");
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* MIGRATE host port <key | ""> destination-db timeout [COPY] [REPLACE]
[[AUTH password] | [AUTH2 username password]] [KEYS key [key ...]]
Starting with Redis version 3.0.0: Added the COPY and REPLACE options.
Starting with Redis version 3.0.6: Added the KEYS option.
Starting with Redis version 4.0.7: Added the AUTH option.
Starting with Redis version 6.0.0: Added the AUTH2 option.
*/
/* MIGRATE */
int redis_migrate_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *host = NULL, *key = NULL, *user = NULL, *pass = NULL;
zend_long destdb = 0, port = 0, timeout = 0;
zval *zkeys = NULL, *zkey, *zauth = NULL;
zend_bool copy = 0, replace = 0;
smart_string cmdstr = {0};
int argc;
ZEND_PARSE_PARAMETERS_START(5, 8)
Z_PARAM_STR(host)
Z_PARAM_LONG(port)
Z_PARAM_ZVAL(zkeys)
Z_PARAM_LONG(destdb)
Z_PARAM_LONG(timeout)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(copy)
Z_PARAM_BOOL(replace)
Z_PARAM_ZVAL_OR_NULL(zauth)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
/* Sanity check on our optional AUTH argument */
if (zauth && redis_extract_auth_info(zauth, &user, &pass) == FAILURE) {
php_error_docref(NULL, E_WARNING, "AUTH must be a string or an array with one or two strings");
user = pass = NULL;
}
/* Protect against being passed an array with zero elements */
if (Z_TYPE_P(zkeys) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(zkeys)) == 0) {
php_error_docref(NULL, E_WARNING, "Keys array cannot be empty");
return FAILURE;
}
/* host, port, key|"", dest-db, timeout, [copy, replace] [KEYS key1..keyN] */
argc = 5 + copy + replace + (user||pass ? 1 : 0) + (user != NULL) + (pass != NULL);
if (Z_TYPE_P(zkeys) == IS_ARRAY) {
/* +1 for the "KEYS" argument itself */
argc += 1 + zend_hash_num_elements(Z_ARRVAL_P(zkeys));
}
/* Initialize MIGRATE command with host and port */
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "MIGRATE");
redis_cmd_append_sstr_zstr(&cmdstr, host);
redis_cmd_append_sstr_long(&cmdstr, port);
/* If passed a keys array the keys come later, otherwise pass the key to
* migrate here */
if (Z_TYPE_P(zkeys) == IS_ARRAY) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "");
} else {
key = redis_key_prefix_zval(redis_sock, zkeys);
redis_cmd_append_sstr_zstr(&cmdstr, key);
zend_string_release(key);
}
redis_cmd_append_sstr_long(&cmdstr, destdb);
redis_cmd_append_sstr_long(&cmdstr, timeout);
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, copy, "COPY");
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, replace, "REPLACE");
if (user && pass) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "AUTH2");
redis_cmd_append_sstr_zstr(&cmdstr, user);
redis_cmd_append_sstr_zstr(&cmdstr, pass);
} else if (pass) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "AUTH");
redis_cmd_append_sstr_zstr(&cmdstr, pass);
}
/* Append actual keys if we've got a keys array */
if (Z_TYPE_P(zkeys) == IS_ARRAY) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "KEYS");
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zkeys), zkey) {
key = redis_key_prefix_zval(redis_sock, zkey);
redis_cmd_append_sstr_zstr(&cmdstr, key);
zend_string_release(key);
} ZEND_HASH_FOREACH_END();
}
if (user) zend_string_release(user);
if (pass) zend_string_release(pass);
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* A generic passthru function for variadic key commands that take one or more
* keys. This is essentially all of them except ones that STORE data. */
int redis_varkey_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx)
{
return gen_varkey_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock,
kw, strlen(kw), 0, cmd, cmd_len, slot);
}
static int
redis_build_client_list_command(smart_string *cmdstr, int argc, zval *z_args)
{
zend_string *zkey;
zval *z_ele, *type = NULL, *id = NULL;
if (argc > 0) {
if (Z_TYPE(z_args[0]) != IS_ARRAY) {
return FAILURE;
}
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL(z_args[0]), zkey, z_ele) {
if (zkey != NULL) {
ZVAL_DEREF(z_ele);
if (zend_string_equals_literal_ci(zkey, "type")) {
if (Z_TYPE_P(z_ele) != IS_STRING || (
!ZVAL_STRICMP_STATIC(z_ele, "normal") &&
!ZVAL_STRICMP_STATIC(z_ele, "master") &&
!ZVAL_STRICMP_STATIC(z_ele, "replica") &&
!ZVAL_STRICMP_STATIC(z_ele, "pubsub")
)) {
return FAILURE;
}
type = z_ele;
} else if (zend_string_equals_literal_ci(zkey, "id")) {
if (Z_TYPE_P(z_ele) != IS_STRING && (
Z_TYPE_P(z_ele) != IS_ARRAY ||
!zend_hash_num_elements(Z_ARRVAL_P(z_ele))
)) {
return FAILURE;
}
id = z_ele;
}
}
} ZEND_HASH_FOREACH_END();
}
REDIS_CMD_INIT_SSTR_STATIC(cmdstr, 1 + (type ? 2 : 0) + (
id ? (Z_TYPE_P(id) == IS_ARRAY ? 1 + zend_hash_num_elements(Z_ARRVAL_P(id)) : 2) : 0
), "CLIENT");
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "LIST");
if (type != NULL) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "TYPE");
redis_cmd_append_sstr(cmdstr, Z_STRVAL_P(type), Z_STRLEN_P(type));
}
if (id != NULL) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "ID");
if (Z_TYPE_P(id) == IS_ARRAY) {
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(id), z_ele) {
if (Z_TYPE_P(z_ele) == IS_STRING) {
redis_cmd_append_sstr(cmdstr, Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele));
} else {
zkey = zval_get_string(z_ele);
redis_cmd_append_sstr(cmdstr, ZSTR_VAL(zkey), ZSTR_LEN(zkey));
zend_string_release(zkey);
}
} ZEND_HASH_FOREACH_END();
} else {
redis_cmd_append_sstr(cmdstr, Z_STRVAL_P(id), Z_STRLEN_P(id));
}
}
return SUCCESS;
}
static int
redis_build_client_kill_command(smart_string *cmdstr, int argc, zval *z_args)
{
zend_string *zkey;
zval *z_ele, *id = NULL, *type = NULL, *address = NULL, *opts = NULL,
*user = NULL, *addr = NULL, *laddr = NULL, *skipme = NULL;
if (argc > 0) {
if (argc > 1) {
if (Z_TYPE(z_args[0]) != IS_STRING || Z_TYPE(z_args[1]) != IS_ARRAY) {
return FAILURE;
}
address = &z_args[0];
opts = &z_args[1];
} else if (Z_TYPE(z_args[0]) == IS_STRING) {
address = &z_args[0];
} else if (Z_TYPE(z_args[0]) == IS_ARRAY) {
opts = &z_args[0];
} else {
return FAILURE;
}
if (opts != NULL) {
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(opts), zkey, z_ele) {
if (zkey != NULL) {
ZVAL_DEREF(z_ele);
if (Z_TYPE_P(z_ele) != IS_STRING) {
return FAILURE;
}
if (zend_string_equals_literal_ci(zkey, "id")) {
id = z_ele;
} else if (zend_string_equals_literal_ci(zkey, "type")) {
if (!ZVAL_STRICMP_STATIC(z_ele, "normal") &&
!ZVAL_STRICMP_STATIC(z_ele, "master") &&
!ZVAL_STRICMP_STATIC(z_ele, "slave") &&
!ZVAL_STRICMP_STATIC(z_ele, "replica") &&
!ZVAL_STRICMP_STATIC(z_ele, "pubsub")
) {
return FAILURE;
}
type = z_ele;
} else if (zend_string_equals_literal_ci(zkey, "user")) {
user = z_ele;
} else if (zend_string_equals_literal_ci(zkey, "addr")) {
addr = z_ele;
} else if (zend_string_equals_literal_ci(zkey, "laddr")) {
laddr = z_ele;
} else if (zend_string_equals_literal_ci(zkey, "skipme")) {
if (!ZVAL_STRICMP_STATIC(z_ele, "yes") &&
!ZVAL_STRICMP_STATIC(z_ele, "no")
) {
return FAILURE;
}
skipme = z_ele;
}
}
} ZEND_HASH_FOREACH_END();
}
}
REDIS_CMD_INIT_SSTR_STATIC(cmdstr, 1 + (address != 0) + (id ? 2 : 0)
+ (type ? 2 : 0) + (user ? 2 : 0) + (addr ? 2 : 0) + (laddr ? 2 : 0)
+ (skipme ? 2 : 0), "CLIENT");
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "KILL");
if (address != NULL) {
redis_cmd_append_sstr(cmdstr, Z_STRVAL_P(address), Z_STRLEN_P(address));
}
if (id != NULL) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "ID");
redis_cmd_append_sstr(cmdstr, Z_STRVAL_P(id), Z_STRLEN_P(id));
}
if (type != NULL) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "TYPE");
redis_cmd_append_sstr(cmdstr, Z_STRVAL_P(type), Z_STRLEN_P(type));
}
if (user != NULL) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "USER");
redis_cmd_append_sstr(cmdstr, Z_STRVAL_P(user), Z_STRLEN_P(user));
}
if (addr != NULL) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "ADDR");
redis_cmd_append_sstr(cmdstr, Z_STRVAL_P(addr), Z_STRLEN_P(addr));
}
if (laddr != NULL) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "LADDR");
redis_cmd_append_sstr(cmdstr, Z_STRVAL_P(laddr), Z_STRLEN_P(laddr));
}
if (skipme != NULL) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "SKIPME");
redis_cmd_append_sstr(cmdstr, Z_STRVAL_P(skipme), Z_STRLEN_P(skipme));
}
return SUCCESS;
}
static int
redis_build_client_tracking_command(smart_string *cmdstr, int argc, zval *z_args)
{
zend_string *zkey;
zval *z_ele, *redirect = NULL, *prefix = NULL;
zend_bool bcast = 0, optin = 0, optout = 0, noloop = 0;
if (argc < 1) {
return FAILURE;
}
if (argc > 1) {
if (Z_TYPE(z_args[1]) != IS_ARRAY) {
return FAILURE;
}
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL(z_args[1]), zkey, z_ele) {
if (zkey != NULL) {
ZVAL_DEREF(z_ele);
if (zend_string_equals_literal_ci(zkey, "redirect")) {
if (Z_TYPE_P(z_ele) != IS_STRING) {
return FAILURE;
}
redirect = z_ele;
} else if (zend_string_equals_literal_ci(zkey, "prefix")) {
if (Z_TYPE_P(z_ele) != IS_STRING && Z_TYPE_P(z_ele) != IS_ARRAY) {
return FAILURE;
}
prefix = z_ele;
} else if (zend_string_equals_literal_ci(zkey, "bcast")) {
bcast = zval_is_true(z_ele);
} else if (zend_string_equals_literal_ci(zkey, "optin")) {
optin = zval_is_true(z_ele);
} else if (zend_string_equals_literal_ci(zkey, "optout")) {
optout = zval_is_true(z_ele);
} else if (zend_string_equals_literal_ci(zkey, "noloop")) {
noloop = zval_is_true(z_ele);
}
}
} ZEND_HASH_FOREACH_END();
}
REDIS_CMD_INIT_SSTR_STATIC(cmdstr, 2 + (redirect ? 2 : 0)
+ (prefix ? 2 * zend_hash_num_elements(Z_ARRVAL_P(prefix)) : 0)
+ bcast + optin + optout + noloop, "CLIENT");
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "TRACKING");
if (Z_TYPE(z_args[0]) == IS_STRING && (
ZVAL_STRICMP_STATIC(&z_args[0], "on") ||
ZVAL_STRICMP_STATIC(&z_args[0], "off")
)) {
redis_cmd_append_sstr(cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
} else if (zval_is_true(&z_args[0])) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "ON");
} else {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "OFF");
}
if (redirect != NULL) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "REDIRECT");
redis_cmd_append_sstr(cmdstr, Z_STRVAL_P(redirect), Z_STRLEN_P(redirect));
}
if (prefix != NULL) {
if (Z_TYPE_P(prefix) == IS_ARRAY) {
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(prefix), z_ele) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "PREFIX");
if (Z_TYPE_P(z_ele) == IS_STRING) {
redis_cmd_append_sstr(cmdstr, Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele));
} else {
zkey = zval_get_string(z_ele);
redis_cmd_append_sstr(cmdstr, ZSTR_VAL(zkey), ZSTR_LEN(zkey));
zend_string_release(zkey);
}
} ZEND_HASH_FOREACH_END();
} else {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "PREFIX");
redis_cmd_append_sstr(cmdstr, Z_STRVAL_P(prefix), Z_STRLEN_P(prefix));
}
}
if (bcast) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "BCAST");
}
if (optin) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "OPTIN");
}
if (optout) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "OPTOUT");
}
if (noloop) {
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "NOLOOP");
}
return SUCCESS;
}
int
redis_client_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
zend_string *op = NULL;
zval *z_args = NULL;
int argc = 0;
ZEND_PARSE_PARAMETERS_START(1, -1)
Z_PARAM_STR(op)
Z_PARAM_OPTIONAL
Z_PARAM_VARIADIC('*', z_args, argc)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_string_equals_literal_ci(op, "INFO")) {
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1, "CLIENT");
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "INFO");
} else if (zend_string_equals_literal_ci(op, "LIST")) {
if (redis_build_client_list_command(&cmdstr, argc, z_args) != 0) {
return FAILURE;
}
*ctx = PHPREDIS_CTX_PTR;
} else if (zend_string_equals_literal_ci(op, "CACHING")) {
if (argc < 1) {
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2, "CLIENT");
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "CACHING");
if (Z_TYPE(z_args[0]) == IS_STRING && (
ZVAL_STRICMP_STATIC(&z_args[0], "yes") ||
ZVAL_STRICMP_STATIC(&z_args[0], "no")
)) {
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
} else if (zval_is_true(&z_args[0])) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "YES");
} else {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "NO");
}
*ctx = PHPREDIS_CTX_PTR + 1;
} else if (zend_string_equals_literal_ci(op, "GETNAME")) {
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1, "CLIENT");
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "GETNAME");
*ctx = PHPREDIS_CTX_PTR + 3;
} else if (zend_string_equals_literal_ci(op, "GETREDIR") || zend_string_equals_literal_ci(op, "ID")) {
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1, "CLIENT");
redis_cmd_append_sstr(&cmdstr, ZSTR_VAL(op), ZSTR_LEN(op));
*ctx = PHPREDIS_CTX_PTR + 2;
} else if (zend_string_equals_literal_ci(op, "KILL")) {
if (redis_build_client_kill_command(&cmdstr, argc, z_args) != 0) {
return FAILURE;
}
*ctx = PHPREDIS_CTX_PTR + 1;
} else if (zend_string_equals_literal_ci(op, "NO-EVICT")) {
if (argc < 1) {
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2, "CLIENT");
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "NO-EVICT");
if (Z_TYPE(z_args[0]) == IS_STRING && (
ZVAL_STRICMP_STATIC(&z_args[0], "on") ||
ZVAL_STRICMP_STATIC(&z_args[0], "off")
)) {
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
} else if (zval_is_true(&z_args[0])) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "ON");
} else {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "OFF");
}
*ctx = PHPREDIS_CTX_PTR + 1;
} else if (zend_string_equals_literal_ci(op, "PAUSE")) {
if (argc < 1 || Z_TYPE(z_args[0]) != IS_LONG || (
argc > 1 && (
Z_TYPE(z_args[1]) != IS_STRING || (
!ZVAL_STRICMP_STATIC(&z_args[1], "write") &&
!ZVAL_STRICMP_STATIC(&z_args[1], "all")
)
)
)) {
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc > 1 ? 3 : 2, "CLIENT");
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "PAUSE");
redis_cmd_append_sstr_long(&cmdstr, Z_LVAL(z_args[0]));
if (argc > 1) {
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[1]), Z_STRLEN(z_args[1]));
}
*ctx = PHPREDIS_CTX_PTR + 1;
} else if (zend_string_equals_literal_ci(op, "REPLY")) {
if (argc > 0 && (
Z_TYPE(z_args[0]) != IS_STRING || (
!ZVAL_STRICMP_STATIC(&z_args[0], "on") &&
!ZVAL_STRICMP_STATIC(&z_args[0], "off") &&
!ZVAL_STRICMP_STATIC(&z_args[0], "skip")
)
)) {
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc > 0 ? 2 : 1, "CLIENT");
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "REPLY");
if (argc > 0) {
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
}
*ctx = PHPREDIS_CTX_PTR + 1;
} else if (zend_string_equals_literal_ci(op, "SETNAME")) {
if (argc < 1 || Z_TYPE(z_args[0]) != IS_STRING) {
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2, "CLIENT");
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "SETNAME");
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
*ctx = PHPREDIS_CTX_PTR + 1;
} else if (zend_string_equals_literal_ci(op, "TRACKING")) {
if (redis_build_client_tracking_command(&cmdstr, argc, z_args) != 0) {
return FAILURE;
}
*ctx = PHPREDIS_CTX_PTR + 1;
} else if (zend_string_equals_literal_ci(op, "TRACKINGINFO")) {
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1, "CLIENT");
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "TRACKINGINFO");
*ctx = PHPREDIS_CTX_PTR + 4;
} else if (zend_string_equals_literal_ci(op, "UNBLOCK")) {
if (argc < 1 || Z_TYPE(z_args[0]) != IS_STRING || (
argc > 1 && (
Z_TYPE(z_args[1]) != IS_STRING || (
!ZVAL_STRICMP_STATIC(&z_args[1], "timeout") &&
!ZVAL_STRICMP_STATIC(&z_args[1], "error")
)
)
)) {
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc > 1 ? 3 : 2, "CLIENT");
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "UNBLOCK");
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
if (argc > 1) {
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[1]), Z_STRLEN(z_args[1]));
}
*ctx = PHPREDIS_CTX_PTR + 2;
} else if (zend_string_equals_literal_ci(op, "UNPAUSE")) {
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2, "CLIENT");
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "UNPAUSE");
*ctx = PHPREDIS_CTX_PTR + 1;
} else {
return FAILURE;
}
// Push out values
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* COMMAND */
int redis_command_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
zend_string *op = NULL, *zstr;
zval *z_args = NULL;
int i, argc = 0;
ZEND_PARSE_PARAMETERS_START(0, -1)
Z_PARAM_OPTIONAL
Z_PARAM_STR(op)
Z_PARAM_VARIADIC('*', z_args, argc)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (op == NULL) {
*ctx = NULL;
argc = 0;
} else if (zend_string_equals_literal_ci(op, "COUNT")) {
*ctx = PHPREDIS_CTX_PTR;
argc = 0;
} else if (zend_string_equals_literal_ci(op, "DOCS") ||
zend_string_equals_literal_ci(op, "INFO")
) {
*ctx = NULL;
} else if (zend_string_equals_literal_ci(op, "GETKEYS") ||
zend_string_equals_literal_ci(op, "LIST")
) {
*ctx = PHPREDIS_CTX_PTR + 1;
} else if (zend_string_equals_literal_ci(op, "GETKEYSANDFLAGS")) {
*ctx = PHPREDIS_CTX_PTR + 2;
} else {
php_error_docref(NULL, E_WARNING, "Unknown COMMAND operation '%s'", ZSTR_VAL(op));
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, !!op + argc, "COMMAND");
if (op) redis_cmd_append_sstr_zstr(&cmdstr, op);
for (i = 0; i < argc; ++i) {
zstr = zval_get_string(&z_args[i]);
redis_cmd_append_sstr(&cmdstr, ZSTR_VAL(zstr), ZSTR_LEN(zstr));
zend_string_release(zstr);
}
// Push out values
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
/* Any slot will do */
CMD_RAND_SLOT(slot);
return SUCCESS;
}
int
redis_copy_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *src = NULL, *dst = NULL;
smart_string cmdstr = {0};
HashTable *opts = NULL;
zend_bool replace = 0;
zend_string *zkey;
zend_long db = -1;
short slot2;
zval *zv;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(src)
Z_PARAM_STR(dst)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT_OR_NULL(opts)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (opts != NULL) {
ZEND_HASH_FOREACH_STR_KEY_VAL(opts, zkey, zv) {
if (zkey == NULL)
continue;
ZVAL_DEREF(zv);
if (zend_string_equals_literal_ci(zkey, "db")) {
db = zval_get_long(zv);
} else if (zend_string_equals_literal_ci(zkey, "replace")) {
replace = zval_is_true(zv);
}
} ZEND_HASH_FOREACH_END();
}
if (slot && db != -1) {
php_error_docref(NULL, E_WARNING, "Can't copy to a specific DB in cluster mode");
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2 + (db > -1 ? 2 : 0) + replace, "COPY");
redis_cmd_append_sstr_key_zstr(&cmdstr, src, redis_sock, slot);
redis_cmd_append_sstr_key_zstr(&cmdstr, dst, redis_sock, slot ? &slot2 : NULL);
if (slot && *slot != slot2) {
php_error_docref(NULL, E_WARNING, "Keys must hash to the same slot!");
efree(cmdstr.c);
return FAILURE;
}
if (db > -1) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "DB");
redis_cmd_append_sstr_long(&cmdstr, db);
}
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, replace, "REPLACE");
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* XADD */
int redis_xadd_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
zend_string *arrkey;
zval *z_fields, *value;
zend_long maxlen = 0;
zend_bool approx = 0, nomkstream = 0;
zend_ulong idx;
HashTable *ht_fields;
int fcount, argc;
char *key, *id;
size_t keylen, idlen;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssa|lbb", &key, &keylen,
&id, &idlen, &z_fields, &maxlen, &approx,
&nomkstream) == FAILURE)
{
return FAILURE;
}
/* At least one field and string are required */
ht_fields = Z_ARRVAL_P(z_fields);
if ((fcount = zend_hash_num_elements(ht_fields)) == 0) {
return FAILURE;
}
if (maxlen < 0 || (maxlen == 0 && approx != 0)) {
php_error_docref(NULL, E_WARNING,
"Warning: Invalid MAXLEN argument or approximate flag");
}
/* Calculate argc for XADD. It's a bit complex because we've got
* an optional MAXLEN argument which can either take the form MAXLEN N
* or MAXLEN ~ N */
argc = 2 + nomkstream + (fcount * 2) + (maxlen > 0 ? (approx ? 3 : 2) : 0);
/* XADD key ID field string [field string ...] */
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "XADD");
redis_cmd_append_sstr_key(&cmdstr, key, keylen, redis_sock, slot);
if (nomkstream) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "NOMKSTREAM");
}
/* Now append our MAXLEN bits if we've got them */
if (maxlen > 0) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "MAXLEN");
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, approx, "~");
redis_cmd_append_sstr_long(&cmdstr, maxlen);
}
/* Now append ID and field(s) */
redis_cmd_append_sstr(&cmdstr, id, idlen);
ZEND_HASH_FOREACH_KEY_VAL(ht_fields, idx, arrkey, value) {
redis_cmd_append_sstr_arrkey(&cmdstr, arrkey, idx);
redis_cmd_append_sstr_zval(&cmdstr, value, redis_sock);
} ZEND_HASH_FOREACH_END();
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
// XPENDING key group [start end count [consumer] [idle]]
int redis_xpending_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *key = NULL, *group = NULL, *start = NULL, *end = NULL,
*consumer = NULL;
zend_long count = -1, idle = 0;
smart_string cmdstr = {0};
int argc;
ZEND_PARSE_PARAMETERS_START(2, 7)
Z_PARAM_STR(key)
Z_PARAM_STR(group)
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_NULL(start)
Z_PARAM_STR_OR_NULL(end)
Z_PARAM_LONG(count)
Z_PARAM_STR_OR_NULL(consumer)
Z_PARAM_LONG(idle)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
/* If we've been passed a start argument, we also need end and count */
if (start != NULL && (end == NULL || count < 0)) {
php_error_docref(NULL, E_WARNING, "'$start' must be accompanied by '$end' and '$count' arguments");
return FAILURE;
}
/* Calculate argc. It's either 2, 5, 6 or 7 */
argc = 2 + (start != NULL ? 3 + (consumer != NULL) + (idle != 0) : 0);
/* Construct command and add required arguments */
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "XPENDING");
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
redis_cmd_append_sstr_zstr(&cmdstr, group);
/* Add optional argumentst */
if (start) {
if (idle != 0) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "IDLE");
redis_cmd_append_sstr_long(&cmdstr, (long)idle);
}
redis_cmd_append_sstr_zstr(&cmdstr, start);
redis_cmd_append_sstr_zstr(&cmdstr, end);
redis_cmd_append_sstr_long(&cmdstr, (long)count);
/* Finally add consumer if we have it */
if (consumer) redis_cmd_append_sstr_zstr(&cmdstr, consumer);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* X[REV]RANGE key start end [COUNT count] */
int redis_xrange_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
smart_string cmdstr = {0};
char *key, *start, *end;
size_t keylen, startlen, endlen;
zend_long count = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|l", &key, &keylen,
&start, &startlen, &end, &endlen, &count)
== FAILURE)
{
return FAILURE;
}
redis_cmd_init_sstr(&cmdstr, 3 + (2 * (count > -1)), kw, strlen(kw));
redis_cmd_append_sstr_key(&cmdstr, key, keylen, redis_sock, slot);
redis_cmd_append_sstr(&cmdstr, start, startlen);
redis_cmd_append_sstr(&cmdstr, end, endlen);
if (count > -1) {
redis_cmd_append_sstr(&cmdstr, ZEND_STRL("COUNT"));
redis_cmd_append_sstr_long(&cmdstr, count);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* Helper function to take an associative array and append the Redis
* STREAMS stream [stream...] id [id ...] arguments to a command string. */
static int
append_stream_args(smart_string *cmdstr, HashTable *ht, RedisSock *redis_sock,
short *slot)
{
char *kptr, kbuf[40];
int klen, i, pos = 0;
zend_string *key, *idstr;
short oldslot = -1;
zval **id;
zend_ulong idx;
/* Append STREAM qualifier */
REDIS_CMD_APPEND_SSTR_STATIC(cmdstr, "STREAMS");
/* Allocate memory to keep IDs */
id = emalloc(sizeof(*id) * zend_hash_num_elements(ht));
/* Iterate over our stream => id array appending streams and retaining each
* value for final arguments */
ZEND_HASH_FOREACH_KEY_VAL(ht, idx, key, id[pos++]) {
if (key) {
klen = ZSTR_LEN(key);
kptr = ZSTR_VAL(key);
} else {
klen = snprintf(kbuf, sizeof(kbuf), "%ld", (long)idx);
kptr = (char*)kbuf;
}
/* Append stream key */
redis_cmd_append_sstr_key(cmdstr, kptr, klen, redis_sock, slot);
/* Protect the user against CROSSSLOT to avoid confusion */
if (slot) {
if (oldslot != -1 && *slot != oldslot) {
php_error_docref(NULL, E_WARNING,
"Warning, not all keys hash to the same slot!");
efree(id);
return FAILURE;
}
oldslot = *slot;
}
} ZEND_HASH_FOREACH_END();
/* Add our IDs */
for (i = 0; i < pos; i++) {
idstr = zval_get_string(id[i]);
redis_cmd_append_sstr(cmdstr, ZSTR_VAL(idstr), ZSTR_LEN(idstr));
zend_string_release(idstr);
}
/* Clean up ID container array */
efree(id);
return 0;
}
/* XREAD [COUNT count] [BLOCK ms] STREAMS key [key ...] id [id ...] */
int redis_xread_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
zend_long count = -1, block = -1;
zval *z_streams;
int argc, scount;
HashTable *kt;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|ll", &z_streams,
&count, &block) == FAILURE)
{
return FAILURE;
}
/* At least one stream and ID is required */
kt = Z_ARRVAL_P(z_streams);
if ((scount = zend_hash_num_elements(kt)) < 1) {
return FAILURE;
}
/* Calculate argc and start constructing command */
argc = 1 + (2 * scount) + (2 * (count > -1)) + (2 * (block > -1));
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "XREAD");
/* Append COUNT if we have it */
if (count > -1) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "COUNT");
redis_cmd_append_sstr_long(&cmdstr, count);
}
/* Append BLOCK if we have it */
if (block > -1) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "BLOCK");
redis_cmd_append_sstr_long(&cmdstr, block);
}
/* Append final STREAM key [key ...] id [id ...] arguments */
if (append_stream_args(&cmdstr, kt, redis_sock, slot) < 0) {
efree(cmdstr.c);
return FAILURE;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* XREADGROUP GROUP group consumer [COUNT count] [BLOCK ms]
* STREAMS key [key ...] id [id ...] */
int redis_xreadgroup_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
zval *z_streams;
HashTable *kt;
char *group, *consumer;
size_t grouplen, consumerlen;
int scount, argc;
zend_long count, block;
zend_bool no_count = 1, no_block = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssa|l!l!", &group,
&grouplen, &consumer, &consumerlen, &z_streams,
&count, &no_count, &block, &no_block) == FAILURE)
{
return FAILURE;
}
/* Negative COUNT or BLOCK is illegal so abort immediately */
if ((!no_count && count < 0) || (!no_block && block < 0)) {
php_error_docref(NULL, E_WARNING, "Negative values for COUNT or BLOCK are illegal.");
return FAILURE;
}
/* Redis requires at least one stream */
kt = Z_ARRVAL_P(z_streams);
if ((scount = zend_hash_num_elements(kt)) < 1) {
return FAILURE;
}
/* Calculate argc and start constructing commands */
argc = 4 + (2 * scount) + (2 * !no_count) + (2 * !no_block);
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "XREADGROUP");
/* Group and consumer */
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "GROUP");
redis_cmd_append_sstr(&cmdstr, group, grouplen);
redis_cmd_append_sstr(&cmdstr, consumer, consumerlen);
/* Append COUNT if we have it */
if (!no_count) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "COUNT");
redis_cmd_append_sstr_long(&cmdstr, count);
}
/* Append BLOCK argument if we have it */
if (!no_block) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "BLOCK");
redis_cmd_append_sstr_long(&cmdstr, block);
}
/* Finally append stream and id args */
if (append_stream_args(&cmdstr, kt, redis_sock, slot) < 0) {
efree(cmdstr.c);
return FAILURE;
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* XACK key group id [id ...] */
int redis_xack_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
char *key, *group;
size_t keylen, grouplen;
zend_string *idstr;
zval *z_ids, *z_id;
HashTable *ht_ids;
int idcount;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssa", &key, &keylen,
&group, &grouplen, &z_ids) == FAILURE)
{
return FAILURE;
}
ht_ids = Z_ARRVAL_P(z_ids);
if ((idcount = zend_hash_num_elements(ht_ids)) < 1) {
return FAILURE;
}
/* Create command and add initial arguments */
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 2 + idcount, "XACK");
redis_cmd_append_sstr_key(&cmdstr, key, keylen, redis_sock, slot);
redis_cmd_append_sstr(&cmdstr, group, grouplen);
/* Append IDs */
ZEND_HASH_FOREACH_VAL(ht_ids, z_id) {
idstr = zval_get_string(z_id);
redis_cmd_append_sstr(&cmdstr, ZSTR_VAL(idstr), ZSTR_LEN(idstr));
zend_string_release(idstr);
} ZEND_HASH_FOREACH_END();
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* XCLAIM options container */
typedef struct xclaimOptions {
struct {
char *type;
int64_t time;
} idle;
zend_long retrycount;
int force;
int justid;
} xclaimOptions;
/* Attempt to extract an int64_t from the provided zval */
static int zval_get_i64(zval *zv, int64_t *retval) {
if (Z_TYPE_P(zv) == IS_LONG) {
*retval = (int64_t)Z_LVAL_P(zv);
return SUCCESS;
} else if (Z_TYPE_P(zv) == IS_DOUBLE) {
*retval = (int64_t)Z_DVAL_P(zv);
return SUCCESS;
} else if (Z_TYPE_P(zv) == IS_STRING) {
zend_long lval;
double dval;
switch (is_numeric_string(Z_STRVAL_P(zv), Z_STRLEN_P(zv), &lval, &dval, 1)) {
case IS_LONG:
*retval = (int64_t)lval;
return SUCCESS;
case IS_DOUBLE:
*retval = (int64_t)dval;
return SUCCESS;
}
}
/* If we make it here we have failed */
return FAILURE;
}
/* Helper function to get an integer XCLAIM argument. This can overflow a
* 32-bit PHP long so we have to extract it as an int64_t. If the value is
* not a valid number or negative, we'll inform the user of the problem and
* that the argument is being ignored. */
static int64_t get_xclaim_i64_arg(const char *key, zval *zv) {
int64_t retval = -1;
/* Extract an i64, and if we can't let the user know there is an issue. */
if (zval_get_i64(zv, &retval) == FAILURE || retval < 0) {
php_error_docref(NULL, E_WARNING,
"Invalid XCLAIM option '%s' will be ignored", key);
}
return retval;
}
/* Helper to extract XCLAIM options */
static void get_xclaim_options(zval *z_arr, xclaimOptions *opt) {
zend_string *zkey;
HashTable *ht;
zval *zv;
/* Initialize options array to sane defaults */
memset(opt, 0, sizeof(*opt));
opt->retrycount = -1;
opt->idle.time = -1;
/* Early return if we don't have any options */
if (z_arr == NULL)
return;
/* Iterate over our options array */
ht = Z_ARRVAL_P(z_arr);
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, zkey, zv) {
if (zkey) {
if (zend_string_equals_literal_ci(zkey, "TIME")) {
opt->idle.type = "TIME";
opt->idle.time = get_xclaim_i64_arg("TIME", zv);
} else if (zend_string_equals_literal_ci(zkey, "IDLE")) {
opt->idle.type = "IDLE";
opt->idle.time = get_xclaim_i64_arg("IDLE", zv);
} else if (zend_string_equals_literal_ci(zkey, "RETRYCOUNT")) {
opt->retrycount = zval_get_long(zv);
}
} else if (Z_TYPE_P(zv) == IS_STRING) {
if (zend_string_equals_literal_ci(Z_STR_P(zv), "FORCE")) {
opt->force = 1;
} else if (zend_string_equals_literal_ci(Z_STR_P(zv), "JUSTID")) {
opt->justid = 1;
}
}
} ZEND_HASH_FOREACH_END();
}
/* Count argc for any options we may have */
static int xclaim_options_argc(xclaimOptions *opt) {
int argc = 0;
if (opt->idle.type != NULL && opt->idle.time != -1)
argc += 2;
if (opt->retrycount != -1)
argc += 2;
if (opt->force)
argc++;
if (opt->justid)
argc++;
return argc;
}
/* Append XCLAIM options */
static void append_xclaim_options(smart_string *cmd, xclaimOptions *opt) {
/* IDLE/TIME long */
if (opt->idle.type != NULL && opt->idle.time != -1) {
redis_cmd_append_sstr(cmd, opt->idle.type, strlen(opt->idle.type));
redis_cmd_append_sstr_i64(cmd, opt->idle.time);
}
/* RETRYCOUNT */
if (opt->retrycount != -1) {
REDIS_CMD_APPEND_SSTR_STATIC(cmd, "RETRYCOUNT");
redis_cmd_append_sstr_long(cmd, opt->retrycount);
}
/* FORCE and JUSTID */
if (opt->force)
REDIS_CMD_APPEND_SSTR_STATIC(cmd, "FORCE");
if (opt->justid)
REDIS_CMD_APPEND_SSTR_STATIC(cmd, "JUSTID");
}
int
redis_xautoclaim_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
char *key, *group, *consumer, *start;
size_t keylen, grouplen, consumerlen, startlen;
zend_long min_idle, count = -1;
zend_bool justid = 0;
int argc;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sssls|lb", &key, &keylen,
&group, &grouplen, &consumer, &consumerlen,
&min_idle, &start, &startlen, &count, &justid
) == FAILURE)
{
return FAILURE;
}
argc = 5 + (count > 0 ? 2 : 0) + justid;
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "XAUTOCLAIM");
redis_cmd_append_sstr_key(&cmdstr, key, keylen, redis_sock, slot);
redis_cmd_append_sstr(&cmdstr, group, grouplen);
redis_cmd_append_sstr(&cmdstr, consumer, consumerlen);
redis_cmd_append_sstr_long(&cmdstr, min_idle);
redis_cmd_append_sstr(&cmdstr, start, startlen);
if (count > 0) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "COUNT");
redis_cmd_append_sstr_long(&cmdstr, count);
}
if (justid) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "JUSTID");
}
// Set the context to distinguish XCLAIM from XAUTOCLAIM which
// have slightly different reply structures.
*ctx = PHPREDIS_CTX_PTR;
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* XCLAIM <key> <group> <consumer> <min-idle-time> <ID-1> <ID-2>
[IDLE <milliseconds>] [TIME <mstime>] [RETRYCOUNT <count>]
[FORCE] [JUSTID] */
int redis_xclaim_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
smart_string cmdstr = {0};
char *key, *group, *consumer;
size_t keylen, grouplen, consumerlen;
zend_long min_idle;
int argc, id_count;
zval *z_ids, *z_id, *z_opts = NULL;
zend_string *zstr;
HashTable *ht_ids;
xclaimOptions opts;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sssla|a", &key, &keylen,
&group, &grouplen, &consumer, &consumerlen, &min_idle,
&z_ids, &z_opts) == FAILURE)
{
return FAILURE;
}
/* At least one id is required */
ht_ids = Z_ARRVAL_P(z_ids);
if ((id_count = zend_hash_num_elements(ht_ids)) < 1) {
return FAILURE;
}
/* Extract options array if we've got them */
get_xclaim_options(z_opts, &opts);
/* Now we have enough information to calculate argc */
argc = 4 + id_count + xclaim_options_argc(&opts);
/* Start constructing our command */
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "XCLAIM");
redis_cmd_append_sstr_key(&cmdstr, key, keylen, redis_sock, slot);
redis_cmd_append_sstr(&cmdstr, group, grouplen);
redis_cmd_append_sstr(&cmdstr, consumer, consumerlen);
redis_cmd_append_sstr_long(&cmdstr, min_idle);
/* Add IDs */
ZEND_HASH_FOREACH_VAL(ht_ids, z_id) {
zstr = zval_get_string(z_id);
redis_cmd_append_sstr(&cmdstr, ZSTR_VAL(zstr), ZSTR_LEN(zstr));
zend_string_release(zstr);
} ZEND_HASH_FOREACH_END();
/* Finally add our options */
append_xclaim_options(&cmdstr, &opts);
/* Success */
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* XGROUP HELP
* XGROUP CREATE key group id [MKSTREAM] [ENTRIESREAD <n>]
* XGROUP SETID key group id [ENTRIESREAD <n>]
* XGROUP CREATECONSUMER key group consumer
* XGROUP DELCONSUMER key group consumer
* XGROUP DESTROY key group
*/
int redis_xgroup_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *op = NULL, *key = NULL, *group = NULL, *id_or_consumer = NULL;
int nargs, is_create = 0, is_setid = 0;
zend_long entries_read = -2;
smart_string cmdstr = {0};
zend_bool mkstream = 0;
ZEND_PARSE_PARAMETERS_START(1, 6)
Z_PARAM_STR(op)
Z_PARAM_OPTIONAL
Z_PARAM_STR(key)
Z_PARAM_STR(group)
Z_PARAM_STR(id_or_consumer)
Z_PARAM_BOOL(mkstream)
Z_PARAM_LONG(entries_read)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (zend_string_equals_literal_ci(op, "HELP")) {
nargs = 0;
} else if ((is_create = zend_string_equals_literal_ci(op, "CREATE")) ||
(is_setid = zend_string_equals_literal_ci(op, "SETID")) ||
zend_string_equals_literal_ci(op, "CREATECONSUMER") ||
zend_string_equals_literal_ci(op, "DELCONSUMER"))
{
nargs = 3;
} else if (zend_string_equals_literal_ci(op, "DESTROY")) {
nargs = 2;
} else {
php_error_docref(NULL, E_WARNING, "Unknown XGROUP operation '%s'", ZSTR_VAL(op));
return FAILURE;
}
if (ZEND_NUM_ARGS() < nargs) {
php_error_docref(NULL, E_WARNING, "Operation '%s' requires %d arguments", ZSTR_VAL(op), nargs);
return FAILURE;
}
mkstream &= is_create;
if (!(is_create || is_setid))
entries_read = -2;
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + nargs + !!mkstream + (entries_read != -2 ? 2 : 0), "XGROUP");
redis_cmd_append_sstr_zstr(&cmdstr, op);
if (nargs-- > 0) redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
if (nargs-- > 0) redis_cmd_append_sstr_zstr(&cmdstr, group);
if (nargs-- > 0) redis_cmd_append_sstr_zstr(&cmdstr, id_or_consumer);
REDIS_CMD_APPEND_SSTR_OPT_STATIC(&cmdstr, !!mkstream, "MKSTREAM");
if (entries_read != -2) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "ENTRIESREAD");
redis_cmd_append_sstr_long(&cmdstr, entries_read);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* XINFO CONSUMERS key group
* XINFO GROUPS key
* XINFO STREAM key [FULL [COUNT N]]
* XINFO HELP */
int redis_xinfo_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *op = NULL, *key = NULL, *arg = NULL;
smart_string cmdstr = {0};
zend_long count = -1;
ZEND_PARSE_PARAMETERS_START(1, 4)
Z_PARAM_STR(op)
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_NULL(key)
Z_PARAM_STR_OR_NULL(arg)
Z_PARAM_LONG(count)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if ((arg != NULL && key == NULL) || (count != -1 && (key == NULL || arg == NULL))) {
php_error_docref(NULL, E_WARNING, "Cannot pass a non-null optional argument after a NULL one.");
return FAILURE;
}
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, 1 + (key != NULL) + (arg != NULL) + (count > -1 ? 2 : 0), "XINFO");
redis_cmd_append_sstr_zstr(&cmdstr, op);
if (key != NULL)
redis_cmd_append_sstr_key(&cmdstr, ZSTR_VAL(key), ZSTR_LEN(key), redis_sock, slot);
if (arg != NULL)
redis_cmd_append_sstr_zstr(&cmdstr, arg);
if (count > -1) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "COUNT");
redis_cmd_append_sstr_long(&cmdstr, count);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
// XTRIM key <MAXLEN | MINID> [= | ~] threshold [LIMIT count]
int redis_xtrim_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *key = NULL, *threshold = NULL;
zend_bool approx = 0, minid = 0;
smart_string cmdstr = {0};
zend_long limit = -1;
int argc;
ZEND_PARSE_PARAMETERS_START(2, 5)
Z_PARAM_STR(key)
Z_PARAM_STR(threshold)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(approx)
Z_PARAM_BOOL(minid)
Z_PARAM_LONG(limit)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
argc = 4 + (approx && limit > -1 ? 2 : 0);
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "XTRIM");
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
if (minid) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "MINID");
} else {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "MAXLEN");
}
if (approx) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "~");
} else {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "=");
}
redis_cmd_append_sstr_zstr(&cmdstr, threshold);
if (limit > -1 && approx) {
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "LIMIT");
redis_cmd_append_sstr_long(&cmdstr, limit);
} else if (limit > -1) {
php_error_docref(NULL, E_WARNING, "Cannot use LIMIT without an approximate match, ignoring");
} else if (ZEND_NUM_ARGS() == 5) {
php_error_docref(NULL, E_WARNING, "Limit must be >= 0");
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
// [P]EXPIRE[AT] [NX | XX | GT | LT]
int redis_expire_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zend_string *key = NULL, *mode = NULL;
smart_string cmdstr = {0};
zend_long timeout = 0;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(key)
Z_PARAM_LONG(timeout)
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_NULL(mode)
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
if (mode != NULL && !(zend_string_equals_literal_ci(mode, "NX") ||
zend_string_equals_literal_ci(mode, "XX") ||
zend_string_equals_literal_ci(mode, "LT") ||
zend_string_equals_literal_ci(mode, "GT")))
{
php_error_docref(NULL, E_WARNING, "Unknown expiration modifier '%s'", ZSTR_VAL(mode));
return FAILURE;
}
redis_cmd_init_sstr(&cmdstr, 2 + (mode != NULL), kw, strlen(kw));
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
redis_cmd_append_sstr_long(&cmdstr, timeout);
if (mode != NULL) redis_cmd_append_sstr_zstr(&cmdstr, mode);
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
static int
generic_expiremember_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, size_t kw_len, int has_unit, char **cmd,
int *cmd_len, short *slot)
{
zend_string *key, *mem, *unit = NULL;
smart_string cmdstr = {0};
zend_long expiry;
ZEND_PARSE_PARAMETERS_START(3, has_unit ? 4 : 3)
Z_PARAM_STR(key)
Z_PARAM_STR(mem)
Z_PARAM_LONG(expiry)
if (has_unit) {
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_NULL(unit)
}
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
redis_cmd_init_sstr(&cmdstr, 3 + (unit != NULL), kw, kw_len);
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
redis_cmd_append_sstr_zstr(&cmdstr, mem);
redis_cmd_append_sstr_long(&cmdstr, expiry);
if (unit != NULL) {
redis_cmd_append_sstr_zstr(&cmdstr, unit);
}
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
int redis_expiremember_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
return generic_expiremember_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,
redis_sock, ZEND_STRL("EXPIREMEMBER"), 1,
cmd, cmd_len, slot);
}
int redis_expirememberat_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
return generic_expiremember_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU,
redis_sock, ZEND_STRL("EXPIREMEMBERAT"), 0,
cmd, cmd_len, slot);
}
int
redis_sentinel_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx)
{
if (zend_parse_parameters_none() == FAILURE) {
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "SENTINEL", "s", kw, strlen(kw));
return SUCCESS;
}
int
redis_sentinel_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_string *name;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name) == FAILURE) {
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "SENTINEL", "sS", kw, strlen(kw), name);
return SUCCESS;
}
/*
* Redis commands that don't deal with the server at all. The RedisSock*
* pointer is the only thing retrieved differently, so we just take that
* in addition to the standard INTERNAL_FUNCTION_PARAMETERS for arg parsing,
* return value handling, and thread safety. */
void redis_getoption_handler(INTERNAL_FUNCTION_PARAMETERS,
RedisSock *redis_sock, redisCluster *c)
{
zend_long option;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &option)
== FAILURE)
{
RETURN_FALSE;
}
// Return the requested option
switch(option) {
case REDIS_OPT_SERIALIZER:
RETURN_LONG(redis_sock->serializer);
case REDIS_OPT_COMPRESSION:
RETURN_LONG(redis_sock->compression);
case REDIS_OPT_COMPRESSION_LEVEL:
RETURN_LONG(redis_sock->compression_level);
case REDIS_OPT_PACK_IGNORE_NUMBERS:
RETURN_BOOL(redis_sock->pack_ignore_numbers);
case REDIS_OPT_PREFIX:
if (redis_sock->prefix) {
RETURN_STRINGL(ZSTR_VAL(redis_sock->prefix), ZSTR_LEN(redis_sock->prefix));
}
RETURN_NULL();
case REDIS_OPT_READ_TIMEOUT:
RETURN_DOUBLE(redis_sock->read_timeout);
case REDIS_OPT_TCP_KEEPALIVE:
RETURN_LONG(redis_sock->tcp_keepalive);
case REDIS_OPT_SCAN:
RETURN_LONG(redis_sock->scan);
case REDIS_OPT_REPLY_LITERAL:
RETURN_LONG(redis_sock->reply_literal);
case REDIS_OPT_NULL_MBULK_AS_NULL:
RETURN_LONG(redis_sock->null_mbulk_as_null);
case REDIS_OPT_FAILOVER:
RETURN_LONG(c->failover);
case REDIS_OPT_MAX_RETRIES:
RETURN_LONG(redis_sock->max_retries);
case REDIS_OPT_BACKOFF_ALGORITHM:
RETURN_LONG(redis_sock->backoff.algorithm);
case REDIS_OPT_BACKOFF_BASE:
RETURN_LONG(redis_sock->backoff.base / 1000);
case REDIS_OPT_BACKOFF_CAP:
RETURN_LONG(redis_sock->backoff.cap / 1000);
default:
RETURN_FALSE;
}
}
void redis_setoption_handler(INTERNAL_FUNCTION_PARAMETERS,
RedisSock *redis_sock, redisCluster *c)
{
zend_long val_long, option;
zval *val;
zend_string *val_str;
struct timeval read_tv;
int tcp_keepalive = 0;
php_netstream_data_t *sock;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "lz", &option,
&val) == FAILURE)
{
RETURN_FALSE;
}
switch(option) {
case REDIS_OPT_SERIALIZER:
val_long = zval_get_long(val);
if (val_long == REDIS_SERIALIZER_NONE
|| val_long == REDIS_SERIALIZER_PHP
|| val_long == REDIS_SERIALIZER_JSON
#ifdef HAVE_REDIS_IGBINARY
|| val_long == REDIS_SERIALIZER_IGBINARY
#endif
#ifdef HAVE_REDIS_MSGPACK
|| val_long == REDIS_SERIALIZER_MSGPACK
#endif
) {
redis_sock->serializer = val_long;
RETURN_TRUE;
}
break;
case REDIS_OPT_REPLY_LITERAL:
val_long = zval_get_long(val);
redis_sock->reply_literal = val_long != 0;
RETURN_TRUE;
case REDIS_OPT_NULL_MBULK_AS_NULL:
val_long = zval_get_long(val);
redis_sock->null_mbulk_as_null = val_long != 0;
RETURN_TRUE;
case REDIS_OPT_COMPRESSION:
val_long = zval_get_long(val);
if (val_long == REDIS_COMPRESSION_NONE
#ifdef HAVE_REDIS_LZF
|| val_long == REDIS_COMPRESSION_LZF
#endif
#ifdef HAVE_REDIS_ZSTD
|| val_long == REDIS_COMPRESSION_ZSTD
#endif
#ifdef HAVE_REDIS_LZ4
|| val_long == REDIS_COMPRESSION_LZ4
#endif
) {
redis_sock->compression = val_long;
RETURN_TRUE;
}
break;
case REDIS_OPT_PACK_IGNORE_NUMBERS:
redis_sock->pack_ignore_numbers = zval_is_true(val);
RETURN_TRUE;
case REDIS_OPT_COMPRESSION_LEVEL:
val_long = zval_get_long(val);
redis_sock->compression_level = val_long;
RETURN_TRUE;
case REDIS_OPT_PREFIX:
if (redis_sock->prefix) {
zend_string_release(redis_sock->prefix);
redis_sock->prefix = NULL;
}
val_str = zval_get_string(val);
if (ZSTR_LEN(val_str) > 0) {
redis_sock->prefix = val_str;
} else {
zend_string_release(val_str);
}
RETURN_TRUE;
case REDIS_OPT_READ_TIMEOUT:
redis_sock->read_timeout = zval_get_double(val);
if (redis_sock->stream) {
read_tv.tv_sec = (time_t)redis_sock->read_timeout;
read_tv.tv_usec = (int)((redis_sock->read_timeout -
read_tv.tv_sec) * 1000000);
php_stream_set_option(redis_sock->stream,
PHP_STREAM_OPTION_READ_TIMEOUT, 0,
&read_tv);
}
RETURN_TRUE;
case REDIS_OPT_TCP_KEEPALIVE:
/* Don't set TCP_KEEPALIVE if we're using a unix socket. */
if (ZSTR_VAL(redis_sock->host)[0] == '/' && redis_sock->port < 1) {
RETURN_FALSE;
}
tcp_keepalive = zval_get_long(val) > 0 ? 1 : 0;
if (redis_sock->tcp_keepalive == tcp_keepalive) {
RETURN_TRUE;
}
if (redis_sock->stream) {
/* set TCP_KEEPALIVE */
sock = (php_netstream_data_t*)redis_sock->stream->abstract;
if (setsockopt(sock->socket, SOL_SOCKET, SO_KEEPALIVE, (char*)&tcp_keepalive,
sizeof(tcp_keepalive)) == -1) {
RETURN_FALSE;
}
redis_sock->tcp_keepalive = tcp_keepalive;
}
RETURN_TRUE;
case REDIS_OPT_SCAN:
val_long = zval_get_long(val);
if (val_long == REDIS_SCAN_NORETRY) {
redis_sock->scan &= ~REDIS_SCAN_RETRY;
} else if (val_long == REDIS_SCAN_NOPREFIX) {
redis_sock->scan &= ~REDIS_SCAN_PREFIX;
} else if (val_long == REDIS_SCAN_RETRY || val_long == REDIS_SCAN_PREFIX) {
redis_sock->scan |= val_long;
} else {
break;
}
RETURN_TRUE;
case REDIS_OPT_FAILOVER:
if (c == NULL) RETURN_FALSE;
val_long = zval_get_long(val);
if (val_long == REDIS_FAILOVER_NONE ||
val_long == REDIS_FAILOVER_ERROR ||
val_long == REDIS_FAILOVER_DISTRIBUTE ||
val_long == REDIS_FAILOVER_DISTRIBUTE_SLAVES)
{
c->failover = val_long;
RETURN_TRUE;
}
break;
case REDIS_OPT_MAX_RETRIES:
val_long = zval_get_long(val);
if(val_long >= 0) {
redis_sock->max_retries = val_long;
RETURN_TRUE;
}
break;
case REDIS_OPT_BACKOFF_ALGORITHM:
val_long = zval_get_long(val);
if(val_long >= 0 &&
val_long < REDIS_BACKOFF_ALGORITHMS) {
redis_sock->backoff.algorithm = val_long;
RETURN_TRUE;
}
break;
case REDIS_OPT_BACKOFF_BASE:
val_long = zval_get_long(val);
if(val_long >= 0) {
redis_sock->backoff.base = val_long * 1000;
RETURN_TRUE;
}
break;
case REDIS_OPT_BACKOFF_CAP:
val_long = zval_get_long(val);
if(val_long >= 0) {
redis_sock->backoff.cap = val_long * 1000;
RETURN_TRUE;
}
break;
default:
php_error_docref(NULL, E_WARNING, "Unknown option '" ZEND_LONG_FMT "'", option);
break;
}
RETURN_FALSE;
}
void redis_prefix_handler(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock) {
char *key;
size_t key_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &key, &key_len)
==FAILURE)
{
RETURN_FALSE;
}
if (redis_sock->prefix) {
int keyfree = redis_key_prefix(redis_sock, &key, &key_len);
RETVAL_STRINGL(key, key_len);
if (keyfree) efree(key);
} else {
RETURN_STRINGL(key, key_len);
}
}
void redis_serialize_handler(INTERNAL_FUNCTION_PARAMETERS,
RedisSock *redis_sock)
{
zval *z_val;
char *val;
size_t val_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &z_val) == FAILURE) {
RETURN_FALSE;
}
int val_free = redis_serialize(redis_sock, z_val, &val, &val_len);
RETVAL_STRINGL(val, val_len);
if (val_free) efree(val);
}
void redis_unserialize_handler(INTERNAL_FUNCTION_PARAMETERS,
RedisSock *redis_sock, zend_class_entry *ex)
{
char *value;
size_t value_len;
// Parse our arguments
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &value, &value_len)
== FAILURE)
{
RETURN_FALSE;
}
// We only need to attempt unserialization if we have a serializer running
if (redis_sock->serializer == REDIS_SERIALIZER_NONE) {
// Just return the value that was passed to us
RETURN_STRINGL(value, value_len);
}
zval z_ret;
if (!redis_unserialize(redis_sock, value, value_len, &z_ret)) {
// Badly formed input, throw an exception
zend_throw_exception(ex, "Invalid serialized data, or unserialization error", 0);
RETURN_FALSE;
}
RETURN_ZVAL(&z_ret, 0, 0);
}
void redis_compress_handler(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock) {
zend_string *zstr;
size_t len;
char *buf;
int cmp_free;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &zstr) == FAILURE) {
RETURN_FALSE;
}
cmp_free = redis_compress(redis_sock, &buf, &len, ZSTR_VAL(zstr), ZSTR_LEN(zstr));
RETVAL_STRINGL(buf, len);
if (cmp_free) efree(buf);
}
void redis_uncompress_handler(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
zend_class_entry *ex)
{
zend_string *zstr;
size_t len;
char *buf;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &zstr) == FAILURE) {
RETURN_FALSE;
} else if (ZSTR_LEN(zstr) == 0 || redis_sock->compression == REDIS_COMPRESSION_NONE) {
RETURN_STR_COPY(zstr);
}
if (!redis_uncompress(redis_sock, &buf, &len, ZSTR_VAL(zstr), ZSTR_LEN(zstr))) {
zend_throw_exception(ex, "Invalid compressed data or uncompression error", 0);
RETURN_FALSE;
}
RETVAL_STRINGL(buf, len);
efree(buf);
}
void redis_pack_handler(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock) {
int valfree;
size_t len;
char *val;
zval *zv;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zv) == FAILURE) {
RETURN_FALSE;
}
valfree = redis_pack(redis_sock, zv, &val, &len);
RETVAL_STRINGL(val, len);
if (valfree) efree(val);
}
void redis_unpack_handler(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock) {
zend_string *str;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &str) == FAILURE) {
RETURN_FALSE;
}
if (redis_unpack(redis_sock, ZSTR_VAL(str), ZSTR_LEN(str), return_value) == 0) {
RETURN_STR_COPY(str);
}
}
/* vim: set tabstop=4 softtabstop=4 expandtab shiftwidth=4: */
|