1 Star 0 Fork 347

Yurnero/swoole-src

forked from swoole/swoole-src 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
swoole_redis_coro.c 123.99 KB
一键复制 编辑 原始数据 按行查看 历史
wuwenbin 提交于 2017-03-30 03:40 . optimize
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache 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.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
#ifdef SW_COROUTINE
#include "swoole_coroutine.h"
#ifdef SW_USE_REDIS
#include <hiredis/hiredis.h>
#include <hiredis/async.h>
#include <ext/standard/php_var.h>
#if PHP_MAJOR_VERSION < 7
#include <ext/standard/php_smart_str.h>
#define SW_REDIS_COMMAND_ALLOC_ARGS_ARR zval **z_args = emalloc(argc*sizeof(zval*));
#define SW_REDIS_COMMAND_ARGS_TYPE(arg) Z_TYPE_P(arg)
#define SW_REDIS_COMMAND_ARGS_LVAL(arg) Z_LVAL_P(arg)
#define SW_REDIS_COMMAND_ARGS_DVAL(arg) Z_DVAL_P(arg)
#define SW_REDIS_COMMAND_ARGS_ARRVAL(arg) Z_ARRVAL_P(arg)
#define SW_REDIS_COMMAND_ARGS_STRVAL(arg) Z_STRVAL_P(arg)
#define SW_REDIS_COMMAND_ARGS_STRLEN(arg) Z_STRLEN_P(arg)
#define SW_REDIS_COMMAND_ARGS_REF(arg) arg
#else
#define SW_REDIS_COMMAND_ALLOC_ARGS_ARR zval *z_args = emalloc(argc*sizeof(zval));
#define SW_REDIS_COMMAND_ARGS_TYPE(arg) Z_TYPE(arg)
#define SW_REDIS_COMMAND_ARGS_LVAL(arg) Z_LVAL(arg)
#define SW_REDIS_COMMAND_ARGS_DVAL(arg) Z_DVAL(arg)
#define SW_REDIS_COMMAND_ARGS_ARRVAL(arg) Z_ARRVAL(arg)
#define SW_REDIS_COMMAND_ARGS_STRVAL(arg) Z_STRVAL(arg)
#define SW_REDIS_COMMAND_ARGS_STRLEN(arg) Z_STRLEN(arg)
#define SW_REDIS_COMMAND_ARGS_REF(arg) &arg
#endif
#define SW_REDIS_COMMAND_BUFFER_SIZE 64
#define SW_BITOP_MIN_OFFSET 0
#define SW_BITOP_MAX_OFFSET 4294967295
#define SW_REDIS_NOT_FOUND 0
#define SW_REDIS_STRING 1
#define SW_REDIS_SET 2
#define SW_REDIS_LIST 3
#define SW_REDIS_ZSET 4
#define SW_REDIS_HASH 5
/* the same errCode define with hiredis */
#define SW_REDIS_ERR_IO 1 /* Error in read or write */
#define SW_REDIS_ERR_EOF 3 /* End of file */
#define SW_REDIS_ERR_PROTOCOL 4 /* Protocol error */
#define SW_REDIS_ERR_OOM 5 /* Out of memory */
#define SW_REDIS_ERR_OTHER 2 /* Everything else... */
/* Extended SET argument detection */
#define IS_EX_ARG(a) \
((a[0]=='e' || a[0]=='E') && (a[1]=='x' || a[1]=='X') && a[2]=='\0')
#define IS_PX_ARG(a) \
((a[0]=='p' || a[0]=='P') && (a[1]=='x' || a[1]=='X') && a[2]=='\0')
#define IS_NX_ARG(a) \
((a[0]=='n' || a[0]=='N') && (a[1]=='x' || a[1]=='X') && a[2]=='\0')
#define IS_XX_ARG(a) \
((a[0]=='x' || a[0]=='X') && (a[1]=='x' || a[1]=='X') && a[2]=='\0')
static zend_class_entry swoole_redis_coro_ce;
static zend_class_entry *swoole_redis_coro_class_entry_ptr;
static int isset_event_callback = 0;
#define IS_EX_PX_ARG(a) (IS_EX_ARG(a) || IS_PX_ARG(a))
#define IS_NX_XX_ARG(a) (IS_NX_ARG(a) || IS_XX_ARG(a))
#define SW_REDIS_COMMAND_CHECK \
swRedisClient *redis = swoole_get_object(getThis()); \
if (redis->iowait == SW_REDIS_CORO_STATUS_WAIT) \
{ \
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC); \
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), "redis client is waiting for response." TSRMLS_CC); \
RETURN_FALSE; \
} \
if (redis->iowait == SW_REDIS_CORO_STATUS_DONE) \
{ \
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC); \
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), "redis client is waiting for calling recv." TSRMLS_CC); \
RETURN_FALSE; \
} \
switch (redis->state) \
{ \
case SWOOLE_REDIS_CORO_STATE_CONNECT: \
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC); \
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), "redis client is not connected." TSRMLS_CC); \
RETURN_FALSE; \
break; \
case SWOOLE_REDIS_CORO_STATE_SUBSCRIBE: \
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC); \
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), "redis client is waiting for subscribe message." TSRMLS_CC); \
RETURN_FALSE; \
break; \
case SWOOLE_REDIS_CORO_STATE_CLOSED: \
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC); \
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), "redis client connection is closed." TSRMLS_CC); \
RETURN_FALSE; \
break; \
default: \
break; \
}
#define SW_REDIS_COMMAND_CHECK_WITH_FREE_Z_ARGS \
swRedisClient *redis = swoole_get_object(getThis()); \
if (redis->iowait == SW_REDIS_CORO_STATUS_WAIT) \
{ \
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC); \
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), "redis client is waiting for response." TSRMLS_CC); \
efree(z_args); \
RETURN_FALSE; \
} \
if (redis->iowait == SW_REDIS_CORO_STATUS_DONE) \
{ \
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC); \
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), "redis client is waiting for calling recv." TSRMLS_CC); \
RETURN_FALSE; \
} \
switch (redis->state) \
{ \
case SWOOLE_REDIS_CORO_STATE_CONNECT: \
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC); \
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), "redis client is not connected." TSRMLS_CC); \
efree(z_args); \
RETURN_FALSE; \
break; \
case SWOOLE_REDIS_CORO_STATE_SUBSCRIBE: \
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC); \
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), "redis client is waiting for subscribe message." TSRMLS_CC); \
efree(z_args); \
RETURN_FALSE; \
break; \
case SWOOLE_REDIS_CORO_STATE_CLOSED: \
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC); \
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), "redis client connection is closed." TSRMLS_CC); \
efree(z_args); \
RETURN_FALSE; \
break; \
default: \
break; \
}
#define SW_REDIS_COMMAND_YIELD \
if (redis->state == SWOOLE_REDIS_CORO_STATE_MULTI || redis->state == SWOOLE_REDIS_CORO_STATE_PIPELINE) \
{ \
redis->queued_cmd_count++; \
RETURN_ZVAL(getThis(), 1, 0); \
} \
else \
{ \
redis->iowait = SW_REDIS_CORO_STATUS_WAIT; \
if (redis->defer) \
{ \
RETURN_TRUE; \
} \
php_context *context = swoole_get_property(getThis(), 0); \
coro_save(context); \
coro_yield(); \
}
#define SW_REDIS_COMMAND_ARGV_FILL(str, str_len) \
argvlen[i] = str_len; \
argv[i] = estrndup(str, str_len); \
i++;
#if (PHP_MAJOR_VERSION < 7)
#define SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(_val) \
if (redis->serialize) { \
smart_str sstr = {0}; \
php_serialize_data_t s_ht; \
PHP_VAR_SERIALIZE_INIT(s_ht); \
php_var_serialize(&sstr, &_val, &s_ht TSRMLS_CC); \
argvlen[i] = (size_t)sstr.len; \
argv[i] = sstr.c; \
PHP_VAR_SERIALIZE_DESTROY(s_ht); \
} else { \
convert_to_string(_val); \
argvlen[i] = Z_STRLEN_P(_val); \
argv[i] = estrndup(Z_STRVAL_P(_val), argvlen[i]); \
} \
i++;
#else
#define SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(_val) \
if (redis->serialize) { \
smart_str sstr = {0}; \
php_serialize_data_t s_ht; \
PHP_VAR_SERIALIZE_INIT(s_ht); \
php_var_serialize(&sstr, _val, &s_ht TSRMLS_CC); \
argvlen[i] = (size_t)sstr.s->len; \
argv[i] = estrndup(sstr.s->val, sstr.s->len); \
zend_string_release(sstr.s); \
PHP_VAR_SERIALIZE_DESTROY(s_ht); \
} else { \
zend_string *convert_str = zval_get_string(_val); \
argvlen[i] = convert_str->len; \
argv[i] = estrndup(convert_str->val, convert_str->len); \
zend_string_release(convert_str); \
} \
i++;
#endif
#define SW_REDIS_COMMAND_ALLOC_ARGV \
size_t stack_argvlen[SW_REDIS_COMMAND_BUFFER_SIZE]; \
char *stack_argv[SW_REDIS_COMMAND_BUFFER_SIZE]; \
size_t *argvlen; \
char **argv; \
zend_bool free_mm = 0; \
if (argc > SW_REDIS_COMMAND_BUFFER_SIZE) \
{ \
argvlen = emalloc(sizeof(size_t) * (argc)); \
argv = emalloc(sizeof(char*) * (argc)); \
free_mm = 1; \
} \
else \
{ \
argvlen = stack_argvlen; \
argv = stack_argv; \
}
#define SW_REDIS_COMMAND_FREE_ARGV \
if (free_mm) \
{ \
efree(argvlen); \
efree(argv); \
}
#define SW_REDIS_COMMAND(argc) \
if (redisAsyncCommandArgv(redis->context, swoole_redis_coro_onResult, NULL, argc, (const char **) argv, (const size_t *) argvlen) < 0) \
{ \
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC); \
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), "redisAsyncCommandArgv() failed." TSRMLS_CC); \
RETURN_FALSE; \
} \
for (i = 0; i < argc; i++) \
{ \
efree(argv[i]); \
}
typedef enum {SW_REDIS_CORO_STATUS_CLOSED, SW_REDIS_CORO_STATUS_READY, SW_REDIS_CORO_STATUS_WAIT, SW_REDIS_CORO_STATUS_DONE} swoole_redis_coro_io_status;
typedef enum
{
SWOOLE_REDIS_CORO_STATE_CONNECT,
SWOOLE_REDIS_CORO_STATE_READY,
SWOOLE_REDIS_CORO_STATE_SUBSCRIBE,
SWOOLE_REDIS_CORO_STATE_MULTI,
SWOOLE_REDIS_CORO_STATE_PIPELINE,
SWOOLE_REDIS_CORO_STATE_CLOSED,
SWOOLE_REDIS_CORO_STATE_CLOSING,
} swoole_redis_coro_state;
typedef struct
{
redisAsyncContext *context;
zend_bool defer;
zend_bool _defer;
swoole_redis_coro_state state;
swoole_redis_coro_io_status iowait;
uint16_t queued_cmd_count;
zval *pipeline_result;
zval *result;
zend_bool serialize;
zval *object;
zval _object;
} swRedisClient;
enum {SW_REDIS_MODE_MULTI, SW_REDIS_MODE_PIPELINE};
static void swoole_redis_coro_event_AddRead(void *privdata);
static void swoole_redis_coro_event_AddWrite(void *privdata);
static void swoole_redis_coro_event_DelRead(void *privdata);
static void swoole_redis_coro_event_DelWrite(void *privdata);
static void swoole_redis_coro_event_Cleanup(void *privdata);
static void swoole_redis_coro_onConnect(const redisAsyncContext *c, int status);
static void swoole_redis_coro_onClose(const redisAsyncContext *c, int status);
static int swoole_redis_coro_onRead(swReactor *reactor, swEvent *event);
static int swoole_redis_coro_onWrite(swReactor *reactor, swEvent *event);
static int swoole_redis_coro_onError(swReactor *reactor, swEvent *event);
static void swoole_redis_coro_onResult(redisAsyncContext *c, void *r, void *privdata);
static void swoole_redis_coro_parse_result(swRedisClient *redis, zval* return_value, redisReply* reply TSRMLS_DC);
static sw_inline void sw_redis_command_empty(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len)
{
SW_REDIS_COMMAND_CHECK
int i =0;
size_t argvlen[1];
char *argv[1];
SW_REDIS_COMMAND_ARGV_FILL(cmd, cmd_len)
SW_REDIS_COMMAND(1)
SW_REDIS_COMMAND_YIELD
}
static sw_inline void sw_redis_command_var_key(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len, int min_argc, int has_timeout)
{
long timeout;
int argc = ZEND_NUM_ARGS();
if(argc < min_argc) {
RETURN_FALSE;
}
SW_REDIS_COMMAND_ALLOC_ARGS_ARR
if(argc == 0 || zend_get_parameters_array(ht, argc, z_args) == FAILURE) {
efree(z_args);
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK_WITH_FREE_Z_ARGS
zend_bool single_array = 0;
if(has_timeout == 0) {
single_array = argc==1 && SW_REDIS_COMMAND_ARGS_TYPE(z_args[0])==IS_ARRAY;
} else {
single_array = argc==2 && SW_REDIS_COMMAND_ARGS_TYPE(z_args[0])==IS_ARRAY &&
SW_REDIS_COMMAND_ARGS_TYPE(z_args[1])==IS_LONG;
timeout = SW_REDIS_COMMAND_ARGS_LVAL(z_args[1]);
}
if (single_array)
{
argc = zend_hash_num_elements(SW_REDIS_COMMAND_ARGS_ARRVAL(z_args[0])) + 1;
}
else
{
argc++;
}
SW_REDIS_COMMAND_ALLOC_ARGV
int i = 0;
SW_REDIS_COMMAND_ARGV_FILL(cmd, cmd_len)
char buf[32];
size_t buf_len;
if (single_array)
{
zval *value;
SW_HASHTABLE_FOREACH_START(SW_REDIS_COMMAND_ARGS_ARRVAL(z_args[0]), value)
#if PHP_MAJOR_VERSION < 7
convert_to_string(value);
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(value), Z_STRLEN_P(value))
#else
zend_string *convert_str = zval_get_string(value);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
SW_HASHTABLE_FOREACH_END();
if(has_timeout) {
buf_len = snprintf(buf, sizeof(buf), "%ld", timeout);
SW_REDIS_COMMAND_ARGV_FILL((char*)buf, buf_len);
}
}
else
{
if(has_timeout && SW_REDIS_COMMAND_ARGS_TYPE(z_args[argc-2]) != IS_LONG) {
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), "Timeout value must be a LONG" TSRMLS_CC);
efree(z_args);
RETURN_FALSE;
}
int j, tail;
tail = has_timeout ? argc - 2 : argc - 1;
for (j = 0; j < tail; ++j)
{
#if PHP_MAJOR_VERSION < 7
convert_to_string(z_args[j]);
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[j]), SW_REDIS_COMMAND_ARGS_STRLEN(z_args[j]))
#else
zend_string *convert_str = zval_get_string(&z_args[j]);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
}
if(has_timeout) {
buf_len = snprintf(buf, sizeof(buf), "%ld", SW_REDIS_COMMAND_ARGS_LVAL(z_args[tail]));
SW_REDIS_COMMAND_ARGV_FILL((char*)buf, buf_len);
}
}
efree(z_args);
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static sw_inline void sw_redis_command_key(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len)
{
char *key;
zend_size_t key_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &key_len) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i =0;
size_t argvlen[2];
char *argv[2];
SW_REDIS_COMMAND_ARGV_FILL(cmd, cmd_len)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND(2)
SW_REDIS_COMMAND_YIELD
}
static sw_inline void sw_redis_command_key_var_val(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len)
{
int argc = ZEND_NUM_ARGS();
// We at least need a key and one value
if(argc < 2) {
RETURN_FALSE;
}
// Make sure we at least have a key, and we can get other args
SW_REDIS_COMMAND_ALLOC_ARGS_ARR
if(zend_get_parameters_array(ht, argc, z_args) == FAILURE) {
efree(z_args);
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK_WITH_FREE_Z_ARGS
int i = 0, j;
argc++;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL(cmd, cmd_len)
#if PHP_MAJOR_VERSION < 7
convert_to_string(z_args[0]);
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[0]), SW_REDIS_COMMAND_ARGS_STRLEN(z_args[0]))
#else
zend_string *convert_str = zval_get_string(&z_args[0]);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
for (j = 1; j < argc - 1; ++j)
{
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(SW_REDIS_COMMAND_ARGS_REF(z_args[j]))
}
efree(z_args);
SW_REDIS_COMMAND(argc);
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static sw_inline void sw_redis_command_key_long_val(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len)
{
char *key;
zend_size_t key_len;
long l_val;
zval *z_value;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "slz", &key, &key_len, &l_val, &z_value) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[4];
char *argv[4];
SW_REDIS_COMMAND_ARGV_FILL(cmd, cmd_len)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char str[32];
sprintf(str, "%ld", l_val);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(z_value)
SW_REDIS_COMMAND(4);
SW_REDIS_COMMAND_YIELD
}
static sw_inline void sw_redis_command_key_long_str(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len)
{
char *key, *val;
zend_size_t key_len, val_len;
long l_val;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sls", &key, &key_len, &l_val, &val, &val_len)==FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[4];
char *argv[4];
SW_REDIS_COMMAND_ARGV_FILL(cmd, cmd_len)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char str[32];
sprintf(str, "%ld", l_val);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
SW_REDIS_COMMAND_ARGV_FILL(val, val_len)
SW_REDIS_COMMAND(4);
SW_REDIS_COMMAND_YIELD
}
static sw_inline void sw_redis_command_key_long(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len)
{
char *key;
zend_size_t key_len;
long l_val;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &key, &key_len, &l_val)==FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[3];
char *argv[3];
SW_REDIS_COMMAND_ARGV_FILL(cmd, cmd_len)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char str[32];
sprintf(str, "%ld", l_val);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
SW_REDIS_COMMAND(3);
SW_REDIS_COMMAND_YIELD
}
static sw_inline void sw_redis_command_key_long_long(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len)
{
char *key;
zend_size_t key_len;
long l1_val, l2_val;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &key, &key_len, &l1_val, &l2_val)==FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[4];
char *argv[4];
SW_REDIS_COMMAND_ARGV_FILL(cmd, cmd_len)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char str[32];
sprintf(str, "%ld", l1_val);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
sprintf(str, "%ld", l2_val);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
SW_REDIS_COMMAND(4);
SW_REDIS_COMMAND_YIELD
}
static sw_inline void sw_redis_command_key_dbl(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len)
{
char *key;
zend_size_t key_len;
double d_val;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sd", &key, &key_len, &d_val) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i =0;
size_t argvlen[3];
char *argv[3];
SW_REDIS_COMMAND_ARGV_FILL(cmd, cmd_len)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char str[32];
sprintf(str, "%f", d_val);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
SW_REDIS_COMMAND(3)
SW_REDIS_COMMAND_YIELD
}
static sw_inline void sw_redis_command_key_key(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len)
{
char *key1, *key2;
zend_size_t key1_len, key2_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &key1, &key1_len, &key2, &key2_len) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i =0;
size_t argvlen[3];
char *argv[3];
SW_REDIS_COMMAND_ARGV_FILL(cmd, cmd_len)
SW_REDIS_COMMAND_ARGV_FILL(key1, key1_len)
SW_REDIS_COMMAND_ARGV_FILL(key2, key2_len)
SW_REDIS_COMMAND(3)
SW_REDIS_COMMAND_YIELD
}
static sw_inline void sw_redis_command_key_val(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len)
{
char *key;
zend_size_t key_len;
zval *z_value;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &key, &key_len, &z_value) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i =0;
size_t argvlen[3];
char *argv[3];
SW_REDIS_COMMAND_ARGV_FILL(cmd, cmd_len)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(z_value)
SW_REDIS_COMMAND(3)
SW_REDIS_COMMAND_YIELD
}
static sw_inline void sw_redis_command_key_str(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len)
{
char *key, *val;
zend_size_t key_len, val_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &key, &key_len, &val, &val_len) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i =0;
size_t argvlen[3];
char *argv[3];
SW_REDIS_COMMAND_ARGV_FILL(cmd, cmd_len)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL(val, val_len)
SW_REDIS_COMMAND(3)
SW_REDIS_COMMAND_YIELD
}
static sw_inline void sw_redis_command_key_str_str(INTERNAL_FUNCTION_PARAMETERS, char *cmd, int cmd_len)
{
char *key, *val1, *val2;
zend_size_t key_len, val1_len, val2_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &key, &key_len, &val1, &val1_len, &val2, &val2_len) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i =0;
size_t argvlen[4];
char *argv[4];
SW_REDIS_COMMAND_ARGV_FILL(cmd, cmd_len)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL(val1, val1_len)
SW_REDIS_COMMAND_ARGV_FILL(val2, val2_len)
SW_REDIS_COMMAND(4)
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, __construct);
static PHP_METHOD(swoole_redis_coro, __destruct);
static PHP_METHOD(swoole_redis_coro, connect);
static PHP_METHOD(swoole_redis_coro, setDefer);
static PHP_METHOD(swoole_redis_coro, getDefer);
static PHP_METHOD(swoole_redis_coro, recv);
static PHP_METHOD(swoole_redis_coro, set);
static PHP_METHOD(swoole_redis_coro, setBit);
static PHP_METHOD(swoole_redis_coro, setEx);
static PHP_METHOD(swoole_redis_coro, psetEx);
static PHP_METHOD(swoole_redis_coro, lSet);
static PHP_METHOD(swoole_redis_coro, get);
static PHP_METHOD(swoole_redis_coro, mGet);
static PHP_METHOD(swoole_redis_coro, del);
static PHP_METHOD(swoole_redis_coro, hDel);
static PHP_METHOD(swoole_redis_coro, hSet);
static PHP_METHOD(swoole_redis_coro, hMSet);
static PHP_METHOD(swoole_redis_coro, hSetNx);
static PHP_METHOD(swoole_redis_coro, mSet);
static PHP_METHOD(swoole_redis_coro, mSetNx);
static PHP_METHOD(swoole_redis_coro, getKeys);
static PHP_METHOD(swoole_redis_coro, exists);
static PHP_METHOD(swoole_redis_coro, type);
static PHP_METHOD(swoole_redis_coro, strLen);
static PHP_METHOD(swoole_redis_coro, lPop);
static PHP_METHOD(swoole_redis_coro, blPop);
static PHP_METHOD(swoole_redis_coro, rPop);
static PHP_METHOD(swoole_redis_coro, brPop);
static PHP_METHOD(swoole_redis_coro, bRPopLPush);
static PHP_METHOD(swoole_redis_coro, lSize);
static PHP_METHOD(swoole_redis_coro, sSize);
static PHP_METHOD(swoole_redis_coro, sPop);
static PHP_METHOD(swoole_redis_coro, sMembers);
static PHP_METHOD(swoole_redis_coro, sRandMember);
static PHP_METHOD(swoole_redis_coro, persist);
static PHP_METHOD(swoole_redis_coro, ttl);
static PHP_METHOD(swoole_redis_coro, pttl);
static PHP_METHOD(swoole_redis_coro, zCard);
static PHP_METHOD(swoole_redis_coro, hLen);
static PHP_METHOD(swoole_redis_coro, hKeys);
static PHP_METHOD(swoole_redis_coro, hVals);
static PHP_METHOD(swoole_redis_coro, hGetAll);
static PHP_METHOD(swoole_redis_coro, restore);
static PHP_METHOD(swoole_redis_coro, dump);
static PHP_METHOD(swoole_redis_coro, debug);
static PHP_METHOD(swoole_redis_coro, renameKey);
static PHP_METHOD(swoole_redis_coro, renameNx);
static PHP_METHOD(swoole_redis_coro, rpoplpush);
static PHP_METHOD(swoole_redis_coro, randomKey);
static PHP_METHOD(swoole_redis_coro, ping);
static PHP_METHOD(swoole_redis_coro, auth);
static PHP_METHOD(swoole_redis_coro, unwatch);
static PHP_METHOD(swoole_redis_coro, watch);
static PHP_METHOD(swoole_redis_coro, save);
static PHP_METHOD(swoole_redis_coro, bgSave);
static PHP_METHOD(swoole_redis_coro, lastSave);
static PHP_METHOD(swoole_redis_coro, flushDB);
static PHP_METHOD(swoole_redis_coro, flushAll);
static PHP_METHOD(swoole_redis_coro, dbSize);
static PHP_METHOD(swoole_redis_coro, bgrewriteaof);
static PHP_METHOD(swoole_redis_coro, time);
static PHP_METHOD(swoole_redis_coro, role);
static PHP_METHOD(swoole_redis_coro, setRange);
static PHP_METHOD(swoole_redis_coro, setNx);
static PHP_METHOD(swoole_redis_coro, getSet);
static PHP_METHOD(swoole_redis_coro, append);
static PHP_METHOD(swoole_redis_coro, lPushx);
static PHP_METHOD(swoole_redis_coro, lPush);
static PHP_METHOD(swoole_redis_coro, rPush);
static PHP_METHOD(swoole_redis_coro, rPushx);
static PHP_METHOD(swoole_redis_coro, sContains);
static PHP_METHOD(swoole_redis_coro, zScore);
static PHP_METHOD(swoole_redis_coro, zRank);
static PHP_METHOD(swoole_redis_coro, zRevRank);
static PHP_METHOD(swoole_redis_coro, hGet);
static PHP_METHOD(swoole_redis_coro, hMGet);
static PHP_METHOD(swoole_redis_coro, hExists);
static PHP_METHOD(swoole_redis_coro, publish);
static PHP_METHOD(swoole_redis_coro, zIncrBy);
static PHP_METHOD(swoole_redis_coro, zAdd);
static PHP_METHOD(swoole_redis_coro, zDeleteRangeByScore);
static PHP_METHOD(swoole_redis_coro, zCount);
static PHP_METHOD(swoole_redis_coro, zRange);
static PHP_METHOD(swoole_redis_coro, zRevRange);
static PHP_METHOD(swoole_redis_coro, zRangeByScore);
static PHP_METHOD(swoole_redis_coro, zRevRangeByScore);
static PHP_METHOD(swoole_redis_coro, zRangeByLex);
static PHP_METHOD(swoole_redis_coro, zRevRangeByLex);
static PHP_METHOD(swoole_redis_coro, zInter);
static PHP_METHOD(swoole_redis_coro, zUnion);
static PHP_METHOD(swoole_redis_coro, incrBy);
static PHP_METHOD(swoole_redis_coro, hIncrBy);
static PHP_METHOD(swoole_redis_coro, incr);
static PHP_METHOD(swoole_redis_coro, decrBy);
static PHP_METHOD(swoole_redis_coro, decr);
static PHP_METHOD(swoole_redis_coro, getBit);
static PHP_METHOD(swoole_redis_coro, lGet);
static PHP_METHOD(swoole_redis_coro, lInsert);
static PHP_METHOD(swoole_redis_coro, setTimeout);
static PHP_METHOD(swoole_redis_coro, pexpire);
static PHP_METHOD(swoole_redis_coro, expireAt);
static PHP_METHOD(swoole_redis_coro, pexpireAt);
static PHP_METHOD(swoole_redis_coro, move);
static PHP_METHOD(swoole_redis_coro, select);
static PHP_METHOD(swoole_redis_coro, getRange);
static PHP_METHOD(swoole_redis_coro, listTrim);
static PHP_METHOD(swoole_redis_coro, lGetRange);
static PHP_METHOD(swoole_redis_coro, lRem);
static PHP_METHOD(swoole_redis_coro, zDeleteRangeByRank);
static PHP_METHOD(swoole_redis_coro, incrByFloat);
static PHP_METHOD(swoole_redis_coro, hIncrByFloat);
static PHP_METHOD(swoole_redis_coro, bitCount);
static PHP_METHOD(swoole_redis_coro, bitOp);
static PHP_METHOD(swoole_redis_coro, sAdd);
static PHP_METHOD(swoole_redis_coro, sMove);
static PHP_METHOD(swoole_redis_coro, sDiff);
static PHP_METHOD(swoole_redis_coro, sDiffStore);
static PHP_METHOD(swoole_redis_coro, sUnion);
static PHP_METHOD(swoole_redis_coro, sUnionStore);
static PHP_METHOD(swoole_redis_coro, sInter);
static PHP_METHOD(swoole_redis_coro, sInterStore);
static PHP_METHOD(swoole_redis_coro, sRemove);
static PHP_METHOD(swoole_redis_coro, zDelete);
static PHP_METHOD(swoole_redis_coro, subscribe);
static PHP_METHOD(swoole_redis_coro, pSubscribe);
static PHP_METHOD(swoole_redis_coro, close);
static PHP_METHOD(swoole_redis_coro, multi);
static PHP_METHOD(swoole_redis_coro, exec);
static PHP_METHOD(swoole_redis_coro, eval);
static PHP_METHOD(swoole_redis_coro, evalSha);
static PHP_METHOD(swoole_redis_coro, script);
static const zend_function_entry swoole_redis_coro_methods[] =
{
PHP_ME(swoole_redis_coro, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_redis_coro, __destruct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(swoole_redis_coro, connect, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, setDefer, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, getDefer, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, recv, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, close, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, set, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, setBit, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, setEx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, psetEx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, lSet, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, get, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, mGet, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, del, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, hDel, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, hSet, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, hMSet, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, hSetNx, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, delete, del, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, mSet, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, mSetNx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, getKeys, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, keys, getKeys, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, exists, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, type, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, strLen, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, lPop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, blPop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, rPop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, brPop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, bRPopLPush, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, lSize, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, lLen, lSize, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sSize, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, scard, sSize, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sPop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sMembers, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, sGetMembers, sMembers, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sRandMember, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, persist, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, ttl, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, pttl, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zCard, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, zSize, zCard, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, hLen, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, hKeys, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, hVals, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, hGetAll, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, debug, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, restore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, dump, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, renameKey, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, rename, renameKey, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, renameNx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, rpoplpush, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, randomKey, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, ping, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, auth, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, unwatch, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, watch, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, save, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, bgSave, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, lastSave, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, flushDB, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, flushAll, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, dbSize, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, bgrewriteaof, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, time, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, role, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, setRange, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, setNx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, getSet, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, append, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, lPushx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, lPush, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, rPush, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, rPushx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sContains, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, sismember, sContains, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zScore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zRank, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zRevRank, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, hGet, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, hMGet, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, hExists, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, publish, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zIncrBy, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zAdd, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zDeleteRangeByScore, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, zRemRangeByScore, zDeleteRangeByScore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zCount, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zRange, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zRevRange, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zRangeByScore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zRevRangeByScore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zRangeByLex, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zRevRangeByLex, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zInter, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, zinterstore, zInter, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zUnion, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, zunionstore, zUnion, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, incrBy, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, hIncrBy, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, incr, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, decrBy, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, decr, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, getBit, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, lInsert, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, lGet, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, lIndex, lGet, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, setTimeout, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, expire, setTimeout, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, pexpire, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, expireAt, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, pexpireAt, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, move, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, select, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, getRange, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, listTrim, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, ltrim, listTrim, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, lGetRange, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, lRange, lGetRange, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, lRem, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, lRemove,lRem, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zDeleteRangeByRank, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, zRemRangeByRank, zDeleteRangeByRank, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, incrByFloat, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, hIncrByFloat, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, bitCount, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, bitOp, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sAdd, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sMove, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sDiff, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sDiffStore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sUnion, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sUnionStore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sInter, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sInterStore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, sRemove, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, srem, sRemove, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, zDelete, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, zRemove, zDelete, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_redis_coro, zRem, zDelete, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, pSubscribe, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, subscribe, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, multi, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, exec, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, eval, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, evalSha, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_coro, script, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
void swoole_redis_coro_init(int module_number TSRMLS_DC)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_redis_coro_ce, "swoole_redis_coro", "Swoole\\Coroutine\\Redis", swoole_redis_coro_methods);
swoole_redis_coro_class_entry_ptr = zend_register_internal_class(&swoole_redis_coro_ce TSRMLS_CC);
zend_declare_property_long(swoole_redis_coro_class_entry_ptr, SW_STRL("errCode")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_string(swoole_redis_coro_class_entry_ptr, SW_STRL("errMsg")-1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
REGISTER_LONG_CONSTANT("SWOOLE_REDIS_MODE_MULTI", SW_REDIS_MODE_MULTI, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_REDIS_MODE_PIPELINE", SW_REDIS_MODE_PIPELINE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_REDIS_TYPE_NOT_FOUND", SW_REDIS_NOT_FOUND, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_REDIS_TYPE_STRING", SW_REDIS_STRING, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_REDIS_TYPE_SET", SW_REDIS_SET, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_REDIS_TYPE_LIST", SW_REDIS_LIST, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_REDIS_TYPE_ZSET", SW_REDIS_ZSET, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_REDIS_TYPE_HASH", SW_REDIS_HASH, CONST_CS | CONST_PERSISTENT);
}
static PHP_METHOD(swoole_redis_coro, __construct)
{
coro_check(TSRMLS_C);
swRedisClient *redis = emalloc(sizeof(swRedisClient));
bzero(redis, sizeof(swRedisClient));
redis->object = getThis();
sw_copy_to_stack(redis->object, redis->_object);
swoole_set_object(getThis(), redis);
redis->state = SWOOLE_REDIS_CORO_STATE_CONNECT;
redis->iowait = SW_REDIS_CORO_STATUS_READY;
redis->pipeline_result = NULL;
}
static PHP_METHOD(swoole_redis_coro, connect)
{
char *host;
zend_size_t host_len;
long port;
zend_bool serialize = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|b", &host, &host_len, &port, &serialize) == FAILURE)
{
return;
}
if (host_len <= 0)
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "host is empty." TSRMLS_CC);
RETURN_FALSE;
}
swRedisClient *redis = swoole_get_object(getThis());
redis->serialize = serialize;
redisAsyncContext *context;
if (redis->state != SWOOLE_REDIS_CORO_STATE_CONNECT
|| redis->state != SWOOLE_REDIS_CORO_STATE_CLOSED)
{
//This is reconnect, close previous connection
zval *retval = NULL;
zval *zobject = getThis();
sw_zend_call_method_with_0_params(&zobject, swoole_redis_coro_class_entry_ptr, NULL, "close", &retval);
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
}
if (strncasecmp(host, ZEND_STRL("unix:/")) == 0)
{
context = redisAsyncConnectUnix(host + 5);
}
else
{
if (port <= 1 || port > 65535)
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "port is invalid." TSRMLS_CC);
RETURN_FALSE;
}
context = redisAsyncConnect(host, (int) port);
}
if (context->err)
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), context->err TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), context->errstr TSRMLS_CC);
RETURN_FALSE;
}
php_swoole_check_reactor();
if (!isset_event_callback)
{
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_REDIS | SW_EVENT_READ, swoole_redis_coro_onRead);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_REDIS | SW_EVENT_WRITE, swoole_redis_coro_onWrite);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_REDIS | SW_EVENT_ERROR, swoole_redis_coro_onError);
isset_event_callback = 1;
}
redisAsyncSetConnectCallback(context, swoole_redis_coro_onConnect);
redisAsyncSetDisconnectCallback(context, swoole_redis_coro_onClose);
redis->context = context;
context->ev.addRead = swoole_redis_coro_event_AddRead;
context->ev.delRead = swoole_redis_coro_event_DelRead;
context->ev.addWrite = swoole_redis_coro_event_AddWrite;
context->ev.delWrite = swoole_redis_coro_event_DelWrite;
context->ev.cleanup = swoole_redis_coro_event_Cleanup;
context->ev.data = redis;
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("host"), host TSRMLS_CC);
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("port"), port TSRMLS_CC);
if (SwooleG.main_reactor->add(SwooleG.main_reactor, redis->context->c.fd, PHP_SWOOLE_FD_REDIS | SW_EVENT_WRITE) < 0)
{
swoole_php_fatal_error(E_WARNING, "swoole_event_add failed. Erorr: %s[%d].", redis->context->errstr, redis->context->err);
RETURN_FALSE;
}
#if PHP_MAJOR_VERSION < 7
sw_zval_add_ref(&getThis());
#endif
swConnection *conn = swReactor_get(SwooleG.main_reactor, redis->context->c.fd);
conn->object = redis;
php_context *sw_current_context = swoole_get_property(getThis(), 0);
if (!sw_current_context)
{
sw_current_context = emalloc(sizeof(php_context));
swoole_set_property(getThis(), 0, sw_current_context);
}
coro_save(sw_current_context);
coro_yield();
}
static PHP_METHOD(swoole_redis_coro, getDefer)
{
swRedisClient *redis = swoole_get_object(getThis());
RETURN_BOOL(redis->defer);
}
static PHP_METHOD(swoole_redis_coro, setDefer)
{
zend_bool defer = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &defer) == FAILURE)
{
return;
}
swRedisClient *redis = swoole_get_object(getThis());
if (redis->iowait > SW_REDIS_CORO_STATUS_READY)
{
RETURN_BOOL(defer);
}
redis->defer = defer;
RETURN_TRUE;
}
static PHP_METHOD(swoole_redis_coro, recv)
{
swRedisClient *redis = swoole_get_object(getThis());
if (!redis->defer)
{
swoole_php_fatal_error(E_WARNING, "you should not use recv without defer ");
RETURN_FALSE;
}
if (redis->iowait == SW_REDIS_CORO_STATUS_DONE)
{
redis->iowait = SW_REDIS_CORO_STATUS_READY;
#if PHP_MAJOR_VERSION >= 7
zval _result = *redis->result;
efree(redis->result);
zval *result = &_result;
#else
zval *result = redis->result;
#endif
redis->result = NULL;
RETURN_ZVAL(result, 0, 1);
}
if (redis->iowait != SW_REDIS_CORO_STATUS_WAIT)
{
RETURN_FALSE;
}
redis->_defer = 1;
php_context *sw_current_context = swoole_get_property(getThis(), 0);
coro_save(sw_current_context);
coro_yield();
}
static PHP_METHOD(swoole_redis_coro, close)
{
swRedisClient *redis = swoole_get_object(getThis());
if (redis->state == SWOOLE_REDIS_CORO_STATE_CONNECT)
{
RETURN_TRUE;
}
if (redis->state == SWOOLE_REDIS_CORO_STATE_CLOSED)
{
RETURN_TRUE;
}
redis->state = SWOOLE_REDIS_CORO_STATE_CLOSING;
redis->iowait = SW_REDIS_CORO_STATUS_CLOSED;
redisCallback *head = redis->context->replies.head;
redisCallback *cb = head;
while (head != NULL)
{
head = cb->next;
free(cb);
cb = head;
}
redis->context->replies.head = NULL;
redisAsyncDisconnect(redis->context);
RETURN_TRUE;
}
static PHP_METHOD(swoole_redis_coro, __destruct)
{
php_context *sw_current_context = swoole_get_property(getThis(), 0);
if (sw_current_context)
{
efree(sw_current_context);
swoole_set_property(getThis(), 0, NULL);
}
swRedisClient *redis = swoole_get_object(getThis());
if (!redis)
{
return;
}
if (redis->state != SWOOLE_REDIS_CORO_STATE_CONNECT && redis->state != SWOOLE_REDIS_CORO_STATE_CLOSED)
{
zval *retval = NULL;
zval *zobject = getThis();
sw_zend_call_method_with_0_params(&zobject, swoole_redis_coro_class_entry_ptr, NULL, "close", &retval);
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
}
efree(redis);
}
static PHP_METHOD(swoole_redis_coro, set)
{
char *key, *exp_type = NULL, *set_type = NULL;
zend_size_t key_len, argc = 3;
zval *z_value, *z_opts = NULL;
long expire = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|z", &key, &key_len, &z_value, &z_opts) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
if (z_opts && Z_TYPE_P(z_opts) != IS_LONG && Z_TYPE_P(z_opts) != IS_ARRAY
&& Z_TYPE_P(z_opts) != IS_NULL)
{
RETURN_FALSE;
}
if (z_opts && Z_TYPE_P(z_opts) == IS_ARRAY) {
HashTable *kt = Z_ARRVAL_P(z_opts);
#if PHP_MAJOR_VERSION >= 7
zend_string *zkey;
zend_ulong idx;
zval *v;
/* Iterate our option array */
ZEND_HASH_FOREACH_KEY_VAL(kt, idx, zkey, v) {
/* Detect PX or EX argument and validate timeout */
if (zkey && IS_EX_PX_ARG(zkey->val)) {
/* Set expire type */
exp_type = zkey->val;
/* Try to extract timeout */
if (Z_TYPE_P(v) == IS_LONG) {
expire = Z_LVAL_P(v);
} else if (Z_TYPE_P(v) == IS_STRING) {
expire = atol(Z_STRVAL_P(v));
}
/* Expiry can't be set < 1 */
if (expire < 1) RETURN_FALSE;
argc += 2;
} else if (Z_TYPE_P(v) == IS_STRING && IS_NX_XX_ARG(Z_STRVAL_P(v))) {
argc += 1;
set_type = Z_STRVAL_P(v);
}
(void) idx;
} ZEND_HASH_FOREACH_END();
#else
int type;
unsigned int ht_key_len;
unsigned long idx;
char *k;
zval **v;
/* Iterate our option array */
for(zend_hash_internal_pointer_reset(kt);
zend_hash_has_more_elements(kt) == SUCCESS;
zend_hash_move_forward(kt))
{
// Grab key and value
type = zend_hash_get_current_key_ex(kt, &k, &ht_key_len, &idx, 0, NULL);
zend_hash_get_current_data(kt, (void**)&v);
/* Detect PX or EX argument and validate timeout */
if (type == HASH_KEY_IS_STRING && IS_EX_PX_ARG(k)) {
/* Set expire type */
exp_type = k;
/* Try to extract timeout */
if (Z_TYPE_PP(v) == IS_LONG) {
expire = Z_LVAL_PP(v);
} else if (Z_TYPE_PP(v) == IS_STRING) {
expire = atol(Z_STRVAL_PP(v));
}
/* Expiry can't be set < 1 */
if (expire < 1) RETURN_FALSE;
argc += 2;
} else if (Z_TYPE_PP(v) == IS_STRING && IS_NX_XX_ARG(Z_STRVAL_PP(v))) {
argc += 1;
set_type = Z_STRVAL_PP(v);
}
(void) idx;
}
#endif
} else if(z_opts && Z_TYPE_P(z_opts) == IS_LONG) {
/* Grab expiry and fail if it's < 1 */
expire = Z_LVAL_P(z_opts);
if (expire < 1) RETURN_FALSE;
argc += 1;
}
SW_REDIS_COMMAND_ALLOC_ARGV
int i = 0;
if (exp_type || set_type)
{
SW_REDIS_COMMAND_ARGV_FILL("SET", 3)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(z_value)
if (set_type)
{
SW_REDIS_COMMAND_ARGV_FILL(set_type, (size_t) strlen(set_type))
}
if (exp_type)
{
SW_REDIS_COMMAND_ARGV_FILL(exp_type, (size_t) strlen(exp_type))
char str[32];
sprintf(str, "%ld", expire);
SW_REDIS_COMMAND_ARGV_FILL(str, (size_t) strlen(str))
}
} else if (expire > 0) {
SW_REDIS_COMMAND_ARGV_FILL("SETEX", 5)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char str[32];
sprintf(str, "%ld", expire);
SW_REDIS_COMMAND_ARGV_FILL(str, (size_t) strlen(str))
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(z_value)
} else {
SW_REDIS_COMMAND_ARGV_FILL("SET", 3)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(z_value)
}
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, setBit)
{
char *key;
zend_size_t key_len;
long offset;
zend_bool val;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "slb", &key, &key_len,
&offset, &val)==FAILURE)
{
return;
}
// Validate our offset
if(offset < SW_BITOP_MIN_OFFSET || offset >SW_BITOP_MAX_OFFSET) {
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "Invalid OFFSET for bitop command (must be between 0-2^32-1)" TSRMLS_CC);
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[4];
char *argv[4];
SW_REDIS_COMMAND_ARGV_FILL("SETBIT", 6)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char str[32];
sprintf(str, "%ld", offset);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
SW_REDIS_COMMAND_ARGV_FILL(val ? "1" : "0", 1)
SW_REDIS_COMMAND(4);
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, setEx)
{
sw_redis_command_key_long_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SETEX", 5);
}
static PHP_METHOD(swoole_redis_coro, psetEx)
{
sw_redis_command_key_long_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "PSETEX", 6);
}
static PHP_METHOD(swoole_redis_coro, lSet)
{
sw_redis_command_key_long_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "LSET", 4);
}
static PHP_METHOD(swoole_redis_coro, restore)
{
sw_redis_command_key_long_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "RESTORE", 7);
}
static PHP_METHOD(swoole_redis_coro, dump)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DUMP", 4);
}
static PHP_METHOD(swoole_redis_coro, debug)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DEBUG", 5);
}
static PHP_METHOD(swoole_redis_coro, get)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "GET", 3);
}
static PHP_METHOD(swoole_redis_coro, mGet)
{
zval *z_args;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &z_args)==FAILURE)
{
return;
}
int argc;
argc = zend_hash_num_elements(Z_ARRVAL_P(z_args));
if (argc == 0)
{
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK
argc++;
SW_REDIS_COMMAND_ALLOC_ARGV
int i = 0;
zval *value;
SW_REDIS_COMMAND_ARGV_FILL("MGET", 4)
SW_HASHTABLE_FOREACH_START(Z_ARRVAL_P(z_args), value)
#if PHP_MAJOR_VERSION < 7
convert_to_string(value);
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(value), Z_STRLEN_P(value))
#else
zend_string *convert_str = zval_get_string(value);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
SW_HASHTABLE_FOREACH_END();
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, hSet)
{
char *key, *field;
zend_size_t key_len, field_len;
zval *z_val;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssz", &key, &key_len,
&field, &field_len, &z_val) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[4];
char *argv[4];
SW_REDIS_COMMAND_ARGV_FILL("HSET", 4)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL(field, field_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(z_val)
SW_REDIS_COMMAND(4)
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, hMSet)
{
char *key;
zend_size_t key_len, argc;
zval *z_arr;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &key, &key_len,
&z_arr)==FAILURE)
{
return;
}
if((argc = zend_hash_num_elements(Z_ARRVAL_P(z_arr))) == 0) {
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
argc = argc * 2 + 2;
zval *value;
char buf[32];
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("HMSET", 5)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
#if (PHP_MAJOR_VERSION < 7)
int keytype;
SW_HASHTABLE_FOREACH_START2(Z_ARRVAL_P(z_arr), key, key_len, keytype, value)
{
if (HASH_KEY_IS_STRING != keytype)
{
key_len = snprintf(buf, sizeof(buf), "%ld", (long)idx);
key = (char*)buf;
}
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(value)
}
SW_HASHTABLE_FOREACH_END();
#else
zend_ulong idx;
zend_string *_key;
ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(z_arr), idx, _key, value) {
if (_key == NULL) {
key_len = snprintf(buf, sizeof(buf), "%ld", (long)idx);
key = (char*)buf;
} else {
key_len = ZSTR_LEN(_key);
key = ZSTR_VAL(_key);
}
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(value)
} ZEND_HASH_FOREACH_END();
#endif
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, hSetNx)
{
char *key, *field;
zend_size_t key_len, field_len;
zval *z_val;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssz", &key, &key_len,
&field, &field_len, &z_val) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[4];
char *argv[4];
SW_REDIS_COMMAND_ARGV_FILL("HSETNX", 6)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL(field, field_len)
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(z_val), Z_STRLEN_P(z_val))
SW_REDIS_COMMAND(4)
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, hDel)
{
int argc = ZEND_NUM_ARGS();
SW_REDIS_COMMAND_ALLOC_ARGS_ARR
if(argc < 2 || zend_get_parameters_array(ht, argc, z_args) == FAILURE) {
efree(z_args);
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK_WITH_FREE_Z_ARGS
argc++;
int i = 0, j;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("HDEL", 4)
for (j = 0; j < argc-1; ++j)
{
#if PHP_MAJOR_VERSION < 7
convert_to_string(z_args[j]);
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[j]), SW_REDIS_COMMAND_ARGS_STRLEN(z_args[j]))
#else
zend_string *convert_str = zval_get_string(&z_args[j]);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
}
efree(z_args);
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, watch)
{
sw_redis_command_var_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "WATCH", 5, 1, 0);
}
static PHP_METHOD(swoole_redis_coro, del)
{
sw_redis_command_var_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DEL", 3, 1, 0);
}
static PHP_METHOD(swoole_redis_coro, sDiff)
{
sw_redis_command_var_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SDIFF", 5, 1, 0);
}
static PHP_METHOD(swoole_redis_coro, sDiffStore)
{
sw_redis_command_var_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SDIFFSTORE", 10, 1, 0);
}
static PHP_METHOD(swoole_redis_coro, sUnion)
{
sw_redis_command_var_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SUNION", 6, 1, 0);
}
static PHP_METHOD(swoole_redis_coro, sUnionStore)
{
sw_redis_command_var_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SUNIONSTORE", 11, 1, 0);
}
static PHP_METHOD(swoole_redis_coro, sInter)
{
sw_redis_command_var_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SINTER", 6, 1, 0);
}
static PHP_METHOD(swoole_redis_coro, sInterStore)
{
sw_redis_command_var_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SINTERSTORE", 11, 1, 0);
}
static PHP_METHOD(swoole_redis_coro, mSet)
{
zval *z_args;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &z_args) == FAILURE)
{
return;
}
int argc;
argc = zend_hash_num_elements(Z_ARRVAL_P(z_args));
if (argc == 0)
{
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK
argc *= 2;
argc++;
SW_REDIS_COMMAND_ALLOC_ARGV
int i = 0;
SW_REDIS_COMMAND_ARGV_FILL("MSET", 4)
zval *value;
char buf[32];
char *key;
uint32_t key_len;
#if (PHP_MAJOR_VERSION < 7)
int keytype;
SW_HASHTABLE_FOREACH_START2(Z_ARRVAL_P(z_args), key, key_len, keytype, value)
{
if (HASH_KEY_IS_STRING != keytype)
{
key_len = snprintf(buf, sizeof(buf), "%ld", (long)idx);
key = (char*)buf;
}
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(value)
}
SW_HASHTABLE_FOREACH_END();
#else
zend_ulong idx;
zend_string *_key;
ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(z_args), idx, _key, value) {
if (_key == NULL) {
key_len = snprintf(buf, sizeof(buf), "%ld", (long)idx);
key = (char*)buf;
} else {
key_len = ZSTR_LEN(_key);
key = ZSTR_VAL(_key);
}
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(value)
} ZEND_HASH_FOREACH_END();
#endif
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, mSetNx)
{
zval *z_args;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &z_args)==FAILURE)
{
return;
}
int argc;
argc = zend_hash_num_elements(Z_ARRVAL_P(z_args));
if (argc == 0)
{
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK
argc *= 2;
argc++;
SW_REDIS_COMMAND_ALLOC_ARGV
int i = 0;
SW_REDIS_COMMAND_ARGV_FILL("MSETNX", 6)
zval *value;
char buf[32];
char *key;
uint32_t key_len;
#if (PHP_MAJOR_VERSION < 7)
int keytype;
SW_HASHTABLE_FOREACH_START2(Z_ARRVAL_P(z_args), key, key_len, keytype, value)
{
if (HASH_KEY_IS_STRING != keytype)
{
key_len = snprintf(buf, sizeof(buf), "%ld", (long)idx);
key = (char*)buf;
}
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(value)
}
SW_HASHTABLE_FOREACH_END();
#else
zend_ulong idx;
zend_string *_key;
ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(z_args), idx, _key, value) {
if (_key == NULL) {
key_len = snprintf(buf, sizeof(buf), "%ld", (long)idx);
key = (char*)buf;
} else {
key_len = ZSTR_LEN(_key);
key = ZSTR_VAL(_key);
}
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(value)
} ZEND_HASH_FOREACH_END();
#endif
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, getKeys)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "KEYS", 4);
}
static PHP_METHOD(swoole_redis_coro, exists)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "EXISTS", 6);
}
static PHP_METHOD(swoole_redis_coro, type)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "TYPE", 4);
}
static PHP_METHOD(swoole_redis_coro, strLen)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "STRLEN", 6);
}
static PHP_METHOD(swoole_redis_coro, lPop)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "LPOP", 4);
}
static PHP_METHOD(swoole_redis_coro, bRPopLPush)
{
char *key1, *key2;
zend_size_t key1_len, key2_len;
long timeout;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssl", &key1, &key1_len,
&key2, &key2_len, &timeout) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int argc, i = 0;
argc = timeout < 0 ? 3 : 4;
SW_REDIS_COMMAND_ALLOC_ARGV
if (timeout < 0)
{
SW_REDIS_COMMAND_ARGV_FILL("RPOPLPUSH", 9)
SW_REDIS_COMMAND_ARGV_FILL(key1, key1_len)
SW_REDIS_COMMAND_ARGV_FILL(key2, key2_len)
}
else
{
SW_REDIS_COMMAND_ARGV_FILL("BRPOPLPUSH", 10)
SW_REDIS_COMMAND_ARGV_FILL(key1, key1_len)
SW_REDIS_COMMAND_ARGV_FILL(key2, key2_len)
char str[32];
sprintf(str, "%ld", timeout);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
}
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, blPop)
{
int argc = ZEND_NUM_ARGS();
SW_REDIS_COMMAND_ALLOC_ARGS_ARR
if(zend_get_parameters_array(ht, argc, z_args) == FAILURE || argc < 1)
{
efree(z_args);
return;
}
SW_REDIS_COMMAND_CHECK_WITH_FREE_Z_ARGS
zend_bool single_array = 0;
if (argc == 2 && SW_REDIS_COMMAND_ARGS_TYPE(z_args[0]) == IS_ARRAY)
{
argc = zend_hash_num_elements(SW_REDIS_COMMAND_ARGS_ARRVAL(z_args[0])) + 2;
single_array = 1;
}
else
{
argc += 1;
}
int i = 0;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("BLPOP", 5)
if (single_array)
{
zval *value;
SW_HASHTABLE_FOREACH_START(SW_REDIS_COMMAND_ARGS_ARRVAL(z_args[0]), value)
#if PHP_MAJOR_VERSION < 7
convert_to_string(value);
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(value), Z_STRLEN_P(value))
#else
zend_string *convert_str = zval_get_string(value);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
SW_HASHTABLE_FOREACH_END();
#if PHP_MAJOR_VERSION < 7
convert_to_string(z_args[1]);
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[1]), SW_REDIS_COMMAND_ARGS_STRLEN(z_args[1]))
#else
zend_string *convert_str = zval_get_string(&z_args[1]);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
}
else
{
int j;
for (j = 0; j < argc - 1; ++j)
{
#if PHP_MAJOR_VERSION < 7
convert_to_string(z_args[j]);
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[j]), SW_REDIS_COMMAND_ARGS_STRLEN(z_args[j]))
#else
zend_string *convert_str = zval_get_string(&z_args[j]);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
}
}
efree(z_args);
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, brPop)
{
int argc = ZEND_NUM_ARGS();
SW_REDIS_COMMAND_ALLOC_ARGS_ARR
if(zend_get_parameters_array(ht, argc, z_args) == FAILURE || argc < 1)
{
efree(z_args);
return;
}
SW_REDIS_COMMAND_CHECK_WITH_FREE_Z_ARGS
zend_bool single_array = 0;
if (argc == 2 && SW_REDIS_COMMAND_ARGS_TYPE(z_args[0]) == IS_ARRAY)
{
argc = zend_hash_num_elements(SW_REDIS_COMMAND_ARGS_ARRVAL(z_args[0])) + 2;
single_array = 1;
}
else
{
argc += 1;
}
int i = 0;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("BRPOP", 5)
if (single_array)
{
zval *value;
SW_HASHTABLE_FOREACH_START(SW_REDIS_COMMAND_ARGS_ARRVAL(z_args[0]), value)
#if PHP_MAJOR_VERSION < 7
convert_to_string(value);
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(value), Z_STRLEN_P(value))
#else
zend_string *convert_str = zval_get_string(value);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
SW_HASHTABLE_FOREACH_END();
#if PHP_MAJOR_VERSION < 7
convert_to_string(z_args[1]);
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[1]), SW_REDIS_COMMAND_ARGS_STRLEN(z_args[1]))
#else
zend_string *convert_str = zval_get_string(&z_args[1]);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
}
else
{
int j;
for (j = 0; j < argc - 1; ++j)
{
#if PHP_MAJOR_VERSION < 7
convert_to_string(z_args[j]);
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[j]), SW_REDIS_COMMAND_ARGS_STRLEN(z_args[j]))
#else
zend_string *convert_str = zval_get_string(&z_args[j]);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
}
}
efree(z_args);
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, rPop)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "RPOP", 4);
}
static PHP_METHOD(swoole_redis_coro, lSize)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "LLEN", 4);
}
static PHP_METHOD(swoole_redis_coro, sSize)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SCARD", 5);
}
static PHP_METHOD(swoole_redis_coro, sPop)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SPOP", 4);
}
static PHP_METHOD(swoole_redis_coro, sMembers)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SMEMBERS", 8);
}
static PHP_METHOD(swoole_redis_coro, sRandMember)
{
char *key;
zend_size_t key_len;
long count;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &key, &key_len,
&count) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0, argc, buf_len;
char buf[32];
argc = ZEND_NUM_ARGS() == 2 ? 3 : 2;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("SRANDMEMBER", 11);
SW_REDIS_COMMAND_ARGV_FILL(key, key_len);
if (argc == 3)
{
buf_len = snprintf(buf, sizeof(buf), "%ld", count);
SW_REDIS_COMMAND_ARGV_FILL((char *)buf, buf_len);
}
SW_REDIS_COMMAND(argc);
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, persist)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "PERSIST", 7);
}
static PHP_METHOD(swoole_redis_coro, ttl)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "TTL", 3);
}
static PHP_METHOD(swoole_redis_coro, pttl)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "PTTL", 4);
}
static PHP_METHOD(swoole_redis_coro, zCard)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZCARD", 5);
}
static PHP_METHOD(swoole_redis_coro, hLen)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "HLEN", 4);
}
static PHP_METHOD(swoole_redis_coro, hKeys)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "HKEYS", 5);
}
static PHP_METHOD(swoole_redis_coro, hVals)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "HVALS", 5);
}
static PHP_METHOD(swoole_redis_coro, hGetAll)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "HGETALL", 7);
}
static PHP_METHOD(swoole_redis_coro, renameKey)
{
sw_redis_command_key_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "RENAME", 6);
}
static PHP_METHOD(swoole_redis_coro, renameNx)
{
sw_redis_command_key_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "RENAMENX", 8);
}
static PHP_METHOD(swoole_redis_coro, rpoplpush)
{
sw_redis_command_key_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "RPOPLPUSH", 9);
}
static PHP_METHOD(swoole_redis_coro, randomKey)
{
sw_redis_command_empty(INTERNAL_FUNCTION_PARAM_PASSTHRU, "RANDOMKEY", 9);
}
static PHP_METHOD(swoole_redis_coro, unwatch)
{
sw_redis_command_empty(INTERNAL_FUNCTION_PARAM_PASSTHRU, "UNWATCH", 7);
}
static PHP_METHOD(swoole_redis_coro, ping)
{
sw_redis_command_empty(INTERNAL_FUNCTION_PARAM_PASSTHRU, "PING", 4);
}
static PHP_METHOD(swoole_redis_coro, auth)
{
char *pw;
zend_size_t pw_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &pw, &pw_len) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[2];
char *argv[2];
SW_REDIS_COMMAND_ARGV_FILL("AUTH", 4)
SW_REDIS_COMMAND_ARGV_FILL(pw, pw_len)
SW_REDIS_COMMAND(2)
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, save)
{
sw_redis_command_empty(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SAVE", 4);
}
static PHP_METHOD(swoole_redis_coro, bgSave)
{
sw_redis_command_empty(INTERNAL_FUNCTION_PARAM_PASSTHRU, "BGSAVE", 6);
}
static PHP_METHOD(swoole_redis_coro, lastSave)
{
sw_redis_command_empty(INTERNAL_FUNCTION_PARAM_PASSTHRU, "LASTSAVE", 8);
}
static PHP_METHOD(swoole_redis_coro, flushDB)
{
sw_redis_command_empty(INTERNAL_FUNCTION_PARAM_PASSTHRU, "FLUSHDB", 7);
}
static PHP_METHOD(swoole_redis_coro, flushAll)
{
sw_redis_command_empty(INTERNAL_FUNCTION_PARAM_PASSTHRU, "FLUSHALL", 8);
}
static PHP_METHOD(swoole_redis_coro, dbSize)
{
sw_redis_command_empty(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DBSIZE", 6);
}
static PHP_METHOD(swoole_redis_coro, bgrewriteaof)
{
sw_redis_command_empty(INTERNAL_FUNCTION_PARAM_PASSTHRU, "BGREWRITEAOF", 12);
}
static PHP_METHOD(swoole_redis_coro, time)
{
sw_redis_command_empty(INTERNAL_FUNCTION_PARAM_PASSTHRU, "TIME", 4);
}
static PHP_METHOD(swoole_redis_coro, role)
{
sw_redis_command_empty(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ROLE", 4);
}
static PHP_METHOD(swoole_redis_coro, setRange)
{
sw_redis_command_key_long_str(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SETRANGE", 8);
}
static PHP_METHOD(swoole_redis_coro, setNx)
{
sw_redis_command_key_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SETNX", 5);
}
static PHP_METHOD(swoole_redis_coro, getSet)
{
sw_redis_command_key_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "GETSET", 6);
}
static PHP_METHOD(swoole_redis_coro, append)
{
sw_redis_command_key_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "APPEND", 6);
}
static PHP_METHOD(swoole_redis_coro, lPushx)
{
sw_redis_command_key_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "LPUSHX", 6);
}
static PHP_METHOD(swoole_redis_coro, lPush)
{
sw_redis_command_key_var_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "LPUSH", 5);
}
static PHP_METHOD(swoole_redis_coro, rPush)
{
sw_redis_command_key_var_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "RPUSH", 5);
}
static PHP_METHOD(swoole_redis_coro, rPushx)
{
sw_redis_command_key_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "RPUSHX", 6);
}
static PHP_METHOD(swoole_redis_coro, sContains)
{
sw_redis_command_key_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SISMEMBER", 9);
}
static PHP_METHOD(swoole_redis_coro, zRange)
{
char *key;
zend_size_t key_len;
long start, end;
zend_bool ws = 0;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll|b", &key, &key_len,
&start, &end, &ws) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0, argc;
argc = ZEND_NUM_ARGS() + 1;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("ZRANGE", 6)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char buf[32];
size_t buf_len;
buf_len = snprintf(buf, sizeof(buf), "%ld", start);
SW_REDIS_COMMAND_ARGV_FILL((char*)buf, buf_len)
buf_len = snprintf(buf, sizeof(buf), "%ld", end);
SW_REDIS_COMMAND_ARGV_FILL((char*)buf, buf_len)
if (ws)
{
SW_REDIS_COMMAND_ARGV_FILL("WITHSCORES", 10)
}
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, zRevRange)
{
char *key;
zend_size_t key_len;
long start, end;
zend_bool ws = 0;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll|b", &key, &key_len,
&start, &end, &ws) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0, argc;
argc = ZEND_NUM_ARGS() + 1;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("ZREVRANGE", 9)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char buf[32];
size_t buf_len;
buf_len = snprintf(buf, sizeof(buf), "%ld", start);
SW_REDIS_COMMAND_ARGV_FILL((char*)buf, buf_len)
buf_len = snprintf(buf, sizeof(buf), "%ld", end);
SW_REDIS_COMMAND_ARGV_FILL((char*)buf, buf_len)
if (ws)
{
SW_REDIS_COMMAND_ARGV_FILL("WITHSCORES", 10)
}
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, zUnion)
{
char *key, *agg_op;
zend_size_t key_len;
zval *z_keys, *z_weights=NULL;
HashTable *ht_keys, *ht_weights=NULL;
zend_size_t argc = 2, agg_op_len=0, keys_count;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|a!s", &key,
&key_len, &z_keys, &z_weights, &agg_op,
&agg_op_len) == FAILURE)
{
return;
}
ht_keys = Z_ARRVAL_P(z_keys);
if((keys_count = zend_hash_num_elements(ht_keys)) == 0) {
RETURN_FALSE;
} else {
argc += keys_count + 1;
}
if(z_weights != NULL) {
ht_weights = Z_ARRVAL_P(z_weights);
if(zend_hash_num_elements(ht_weights) != keys_count) {
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "WEIGHTS and keys array should be the same size!" TSRMLS_CC);
RETURN_FALSE;
}
argc += keys_count + 1;
}
// AGGREGATE option
if(agg_op_len != 0) {
if(strncasecmp(agg_op, "SUM", sizeof("SUM")) &&
strncasecmp(agg_op, "MIN", sizeof("MIN")) &&
strncasecmp(agg_op, "MAX", sizeof("MAX")))
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "Invalid AGGREGATE option provided!" TSRMLS_CC);
RETURN_FALSE;
}
// "AGGREGATE" + type
argc += 2;
}
SW_REDIS_COMMAND_CHECK
int i = 0, j;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("ZUNIONSTORE", 11)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char buf[32];
size_t buf_len;
buf_len = sprintf(buf, "%d", keys_count);
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
// Process input keys
zval *value;
SW_HASHTABLE_FOREACH_START(ht_keys, value)
#if PHP_MAJOR_VERSION < 7
convert_to_string(value);
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(value), Z_STRLEN_P(value))
#else
zend_string *convert_str = zval_get_string(value);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
SW_HASHTABLE_FOREACH_END();
// Weights
if(ht_weights != NULL) {
SW_REDIS_COMMAND_ARGV_FILL("WEIGHTS", 7)
SW_HASHTABLE_FOREACH_START(ht_weights, value)
if(SW_Z_TYPE_P(value) != IS_LONG && SW_Z_TYPE_P(value) != IS_DOUBLE &&
strncasecmp(Z_STRVAL_P(value),"inf",sizeof("inf")) != 0 &&
strncasecmp(Z_STRVAL_P(value),"-inf",sizeof("-inf")) != 0 &&
strncasecmp(Z_STRVAL_P(value),"+inf",sizeof("+inf")) != 0)
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), "Weights must be numeric or '-inf','inf','+inf'" TSRMLS_CC);
for (j = 0; j < i; j++)
{
efree((void* )argv[j]);
}
SW_REDIS_COMMAND_FREE_ARGV
RETURN_FALSE;
}
switch (SW_Z_TYPE_P(value)) {
case IS_LONG:
buf_len = sprintf(buf, "%ld", Z_LVAL_P(value));
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
break;
case IS_DOUBLE:
buf_len = sprintf(buf, "%f", Z_DVAL_P(value));
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
break;
case IS_STRING:
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(value), Z_STRLEN_P(value))
break;
}
SW_HASHTABLE_FOREACH_END();
}
// AGGREGATE
if(agg_op_len != 0) {
SW_REDIS_COMMAND_ARGV_FILL("AGGREGATE", 9)
SW_REDIS_COMMAND_ARGV_FILL(agg_op, agg_op_len)
}
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, zInter)
{
char *key, *agg_op;
zend_size_t key_len;
zval *z_keys, *z_weights=NULL;
HashTable *ht_keys, *ht_weights=NULL;
zend_size_t argc = 2, agg_op_len=0, keys_count;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|a!s", &key,
&key_len, &z_keys, &z_weights, &agg_op,
&agg_op_len) == FAILURE)
{
return;
}
ht_keys = Z_ARRVAL_P(z_keys);
if((keys_count = zend_hash_num_elements(ht_keys)) == 0) {
RETURN_FALSE;
} else {
argc += keys_count + 1;
}
if(z_weights != NULL) {
ht_weights = Z_ARRVAL_P(z_weights);
if(zend_hash_num_elements(ht_weights) != keys_count) {
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "WEIGHTS and keys array should be the same size!" TSRMLS_CC);
RETURN_FALSE;
}
argc += keys_count + 1;
}
// AGGREGATE option
if(agg_op_len != 0) {
if(strncasecmp(agg_op, "SUM", sizeof("SUM")) &&
strncasecmp(agg_op, "MIN", sizeof("MIN")) &&
strncasecmp(agg_op, "MAX", sizeof("MAX")))
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "Invalid AGGREGATE option provided!" TSRMLS_CC);
RETURN_FALSE;
}
// "AGGREGATE" + type
argc += 2;
}
SW_REDIS_COMMAND_CHECK
int i = 0, j;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("ZINTERSTORE", 11)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char buf[32];
size_t buf_len;
buf_len = sprintf(buf, "%d", keys_count);
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
// Process input keys
zval *value;
SW_HASHTABLE_FOREACH_START(ht_keys, value)
#if PHP_MAJOR_VERSION < 7
convert_to_string(value);
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(value), Z_STRLEN_P(value))
#else
zend_string *convert_str = zval_get_string(value);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
SW_HASHTABLE_FOREACH_END();
// Weights
if(ht_weights != NULL) {
SW_REDIS_COMMAND_ARGV_FILL("WEIGHTS", 7)
SW_HASHTABLE_FOREACH_START(ht_weights, value)
if(SW_Z_TYPE_P(value) != IS_LONG && SW_Z_TYPE_P(value) != IS_DOUBLE &&
strncasecmp(Z_STRVAL_P(value),"inf",sizeof("inf")) != 0 &&
strncasecmp(Z_STRVAL_P(value),"-inf",sizeof("-inf")) != 0 &&
strncasecmp(Z_STRVAL_P(value),"+inf",sizeof("+inf")) != 0)
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "Weights must be numeric or '-inf','inf','+inf'" TSRMLS_CC);
for (j = 0; j < i; j++)
{
efree((void* )argv[j]);
}
SW_REDIS_COMMAND_FREE_ARGV
RETURN_FALSE;
}
switch (SW_Z_TYPE_P(value)) {
case IS_LONG:
buf_len = sprintf(buf, "%ld", Z_LVAL_P(value));
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
break;
case IS_DOUBLE:
buf_len = sprintf(buf, "%f", Z_DVAL_P(value));
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
break;
case IS_STRING:
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(value), Z_STRLEN_P(value))
break;
}
SW_HASHTABLE_FOREACH_END();
}
// AGGREGATE
if(agg_op_len != 0) {
SW_REDIS_COMMAND_ARGV_FILL("AGGREGATE", 9)
SW_REDIS_COMMAND_ARGV_FILL(agg_op, agg_op_len)
}
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, zRangeByLex)
{
char *key, *min, *max;
zend_size_t key_len, min_len, max_len;
long offset, count;
zend_size_t argc = ZEND_NUM_ARGS();
/* We need either 3 or 5 arguments for this to be valid */
if(argc != 3 && argc != 5) {
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "Must pass either 3 or 5 arguments" TSRMLS_CC);
RETURN_FALSE;
}
if(zend_parse_parameters(argc TSRMLS_CC, "sss|ll", &key,
&key_len, &min, &min_len, &max, &max_len,
&offset, &count)==FAILURE)
{
RETURN_FALSE;
}
/* min and max must start with '(' or '[', or be either '-' or '+' */
if(min_len < 1 || max_len < 1 ||
(min[0] != '(' && min[0] != '[' &&
(min[0] != '-' || min_len > 1) && (min[0] != '+' || min_len > 1)) ||
(max[0] != '(' && max[0] != '[' &&
(max[0] != '-' || max_len > 1) && (max[0] != '+' || max_len > 1)))
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "min and max arguments must start with '[' or '('" TSRMLS_CC);
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK
argc = argc == 3 ? 4 : 7;
int i = 0;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("ZRANGEBYLEX", 11)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL(min, min_len)
SW_REDIS_COMMAND_ARGV_FILL(max, max_len)
if (argc == 7)
{
SW_REDIS_COMMAND_ARGV_FILL("LIMIT", 5)
char buf[32];
size_t buf_len;
buf_len = sprintf(buf, "%ld", offset);
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
buf_len = sprintf(buf, "%ld", count);
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
}
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, zRevRangeByLex)
{
char *key, *min, *max;
zend_size_t key_len, min_len, max_len;
long offset, count;
int argc = ZEND_NUM_ARGS();
/* We need either 3 or 5 arguments for this to be valid */
if(argc != 3 && argc != 5) {
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "Must pass either 3 or 5 arguments" TSRMLS_CC);
RETURN_FALSE;
}
if(zend_parse_parameters(argc TSRMLS_CC, "sss|ll", &key,
&key_len, &min, &min_len, &max, &max_len,
&offset, &count)==FAILURE)
{
RETURN_FALSE;
}
/* min and max must start with '(' or '[', or be either '-' or '+' */
if(min_len < 1 || max_len < 1 ||
(min[0] != '(' && min[0] != '[' &&
(min[0] != '-' || min_len > 1) && (min[0] != '+' || min_len > 1)) ||
(max[0] != '(' && max[0] != '[' &&
(max[0] != '-' || max_len > 1) && (max[0] != '+' || max_len > 1)))
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "min and max arguments must start with '[' or '('" TSRMLS_CC);
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK
argc = argc == 3 ? 4 : 7;
int i = 0;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("ZREVRANGEBYLEX", 14)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL(min, min_len)
SW_REDIS_COMMAND_ARGV_FILL(max, max_len)
if (argc == 7)
{
SW_REDIS_COMMAND_ARGV_FILL("LIMIT", 5)
char buf[32];
size_t buf_len;
buf_len = sprintf(buf, "%ld", offset);
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
buf_len = sprintf(buf, "%ld", count);
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
}
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, zRangeByScore)
{
char *key;
zend_size_t key_len;
char *start, *end;
zend_size_t start_len, end_len;
long limit_low, limit_high;
zval *z_opt=NULL, *z_ele;
zend_bool withscores = 0, has_limit = 0;
HashTable *ht_opt;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|a", &key, &key_len,
&start, &start_len, &end, &end_len, &z_opt)
==FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int argc = 4, i = 0;
// Check for an options array
if(z_opt && Z_TYPE_P(z_opt) == IS_ARRAY) {
ht_opt = Z_ARRVAL_P(z_opt);
// Check for WITHSCORES
if (sw_zend_hash_find(ht_opt, ZEND_STRS("withscores"), (void **) &z_ele) == SUCCESS
#if PHP_MAJOR_VERSION < 7
&& Z_TYPE_P(z_ele) == IS_BOOL && Z_BVAL_P(z_ele) == 1
#else
&& Z_TYPE_P(z_ele) == IS_TRUE
#endif
)
{
withscores = 1;
argc++;
}
// LIMIT
if (sw_zend_hash_find(ht_opt, ZEND_STRS("limit"), (void **) &z_ele) == SUCCESS)
{
HashTable *ht_limit = Z_ARRVAL_P(z_ele);
#if PHP_MAJOR_VERSION < 7
zval **z_off, **z_cnt;
if (zend_hash_index_find(ht_limit,0,(void**)&z_off) == SUCCESS &&
zend_hash_index_find(ht_limit,1,(void**)&z_cnt) == SUCCESS &&
SW_Z_TYPE_PP(z_off) == IS_LONG && SW_Z_TYPE_PP(z_cnt) == IS_LONG)
{
has_limit = 1;
limit_low = Z_LVAL_PP(z_off);
limit_high = Z_LVAL_PP(z_cnt);
argc += 3;
}
#else
zval *z_off, *z_cnt;
z_off = zend_hash_index_find(ht_limit,0);
z_cnt = zend_hash_index_find(ht_limit,1);
if (z_off && z_cnt &&
SW_Z_TYPE_P(z_off) == IS_LONG && SW_Z_TYPE_P(z_cnt) == IS_LONG)
{
has_limit = 1;
limit_low = Z_LVAL_P(z_off);
limit_high = Z_LVAL_P(z_cnt);
argc += 3;
}
#endif
}
}
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("ZRANGEBYSCORE", 13)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL(start, start_len)
SW_REDIS_COMMAND_ARGV_FILL(end, end_len)
if (withscores)
{
SW_REDIS_COMMAND_ARGV_FILL("WITHSCORES", 10)
}
if (has_limit)
{
SW_REDIS_COMMAND_ARGV_FILL("LIMIT", 5)
char buf[32];
size_t buf_len;
buf_len = sprintf(buf, "%ld", limit_low);
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
buf_len = sprintf(buf, "%ld", limit_high);
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
}
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, zRevRangeByScore)
{
char *key;
zend_size_t key_len;
char *start, *end;
zend_size_t start_len, end_len;
long limit_low, limit_high;
zval *z_opt=NULL, *z_ele;
zend_bool withscores = 0, has_limit = 0;
HashTable *ht_opt;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|a", &key, &key_len,
&start, &start_len, &end, &end_len, &z_opt)
==FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int argc = 4, i = 0;
// Check for an options array
if(z_opt && Z_TYPE_P(z_opt) == IS_ARRAY) {
ht_opt = Z_ARRVAL_P(z_opt);
// Check for WITHSCORES
if (sw_zend_hash_find(ht_opt, ZEND_STRS("withscores"), (void **) &z_ele) == SUCCESS
#if PHP_MAJOR_VERSION < 7
&& Z_TYPE_P(z_ele) == IS_BOOL && Z_BVAL_P(z_ele) == 1
#else
&& Z_TYPE_P(z_ele) == IS_TRUE
#endif
)
{
withscores = 1;
argc++;
}
// LIMIT
if (sw_zend_hash_find(ht_opt, ZEND_STRS("limit"), (void **) &z_ele) == SUCCESS)
{
HashTable *ht_limit = Z_ARRVAL_P(z_ele);
#if PHP_MAJOR_VERSION < 7
zval **z_off, **z_cnt;
if (zend_hash_index_find(ht_limit,0,(void**)&z_off) == SUCCESS &&
zend_hash_index_find(ht_limit,1,(void**)&z_cnt) == SUCCESS &&
SW_Z_TYPE_PP(z_off) == IS_LONG && SW_Z_TYPE_PP(z_cnt) == IS_LONG)
{
has_limit = 1;
limit_low = Z_LVAL_PP(z_off);
limit_high = Z_LVAL_PP(z_cnt);
argc += 3;
}
#else
zval *z_off, *z_cnt;
z_off = zend_hash_index_find(ht_limit,0);
z_cnt = zend_hash_index_find(ht_limit,1);
if (z_off && z_cnt &&
SW_Z_TYPE_P(z_off) == IS_LONG && SW_Z_TYPE_P(z_cnt) == IS_LONG)
{
has_limit = 1;
limit_low = Z_LVAL_P(z_off);
limit_high = Z_LVAL_P(z_cnt);
argc += 3;
}
#endif
}
}
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("ZREVRANGEBYSCORE", 16)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL(start, start_len)
SW_REDIS_COMMAND_ARGV_FILL(end, end_len)
if (withscores)
{
SW_REDIS_COMMAND_ARGV_FILL("WITHSCORES", 10)
}
if (has_limit)
{
SW_REDIS_COMMAND_ARGV_FILL("LIMIT", 5)
char buf[32];
size_t buf_len;
buf_len = sprintf(buf, "%ld", limit_low);
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
buf_len = sprintf(buf, "%ld", limit_high);
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
}
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, zIncrBy)
{
char *key;
zend_size_t key_len;
double incrby;
zval *z_val;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sdz", &key, &key_len,
&incrby, &z_val) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[4];
char *argv[4];
SW_REDIS_COMMAND_ARGV_FILL("ZINCRBY", 7)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char buf[32];
size_t buf_len;
buf_len = sprintf(buf, "%f", incrby);
SW_REDIS_COMMAND_ARGV_FILL(buf, buf_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(z_val)
SW_REDIS_COMMAND(4)
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, zAdd)
{
int argc = ZEND_NUM_ARGS();
SW_REDIS_COMMAND_ALLOC_ARGS_ARR
if(zend_get_parameters_array(ht, argc, z_args)==FAILURE) {
efree(z_args);
RETURN_FALSE;
}
#if PHP_MAJOR_VERSION < 7
if (argc > 0) convert_to_string(z_args[0]);
#else
zend_bool convert = 0;
if (argc > 0)
{
convert_to_string(&z_args[0]);
convert = 1;
}
#endif
if (argc < 3 || SW_REDIS_COMMAND_ARGS_TYPE(z_args[0]) != IS_STRING) {
efree(z_args);
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK_WITH_FREE_Z_ARGS
int i = 0, j, k, valid_params;
valid_params = argc - 1;
argc++;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("ZADD", 4)
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[0]), (size_t)SW_REDIS_COMMAND_ARGS_STRLEN(z_args[0]))
#if PHP_MAJOR_VERSION >= 7
if (convert) zval_ptr_dtor(&z_args[0]);
#endif
k = 1;
if (SW_REDIS_COMMAND_ARGS_TYPE(z_args[k]) == IS_STRING && IS_NX_XX_ARG(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[k])))
{
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[k]), (size_t)SW_REDIS_COMMAND_ARGS_STRLEN(z_args[k]))
k++;
valid_params--;
}
if (SW_REDIS_COMMAND_ARGS_TYPE(z_args[k]) == IS_STRING && strncasecmp(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[k]), "CH", 2) == 0)
{
SW_REDIS_COMMAND_ARGV_FILL("CH", 2)
k++;
valid_params--;
}
if (SW_REDIS_COMMAND_ARGS_TYPE(z_args[k]) == IS_STRING && strncasecmp(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[k]), "INCR", 4) == 0)
{
SW_REDIS_COMMAND_ARGV_FILL("INCR", 4)
k++;
valid_params--;
}
if (valid_params % 2 != 0)
{
for (i = 0; i < 1 + k; i++)
{
efree((void* )argv[i]);
}
SW_REDIS_COMMAND_FREE_ARGV
efree(z_args);
RETURN_FALSE;
}
char buf[32];
size_t buf_len;
for (j = k; j < argc-1; j += 2) {
convert_to_double(SW_REDIS_COMMAND_ARGS_REF(z_args[j])); buf_len = snprintf(buf, sizeof(buf), "%f", SW_REDIS_COMMAND_ARGS_DVAL(z_args[j]));
SW_REDIS_COMMAND_ARGV_FILL((char*)buf, buf_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(SW_REDIS_COMMAND_ARGS_REF(z_args[j+1]))
}
efree(z_args);
SW_REDIS_COMMAND(argc);
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, zScore)
{
sw_redis_command_key_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZSCORE", 6);
}
static PHP_METHOD(swoole_redis_coro, zRank)
{
sw_redis_command_key_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZRANK", 5);
}
static PHP_METHOD(swoole_redis_coro, zRevRank)
{
sw_redis_command_key_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZREVRANK", 8);
}
static PHP_METHOD(swoole_redis_coro, hGet)
{
sw_redis_command_key_str(INTERNAL_FUNCTION_PARAM_PASSTHRU, "HGET", 4);
}
static PHP_METHOD(swoole_redis_coro, hMGet)
{
char *key;
zval *z_arr;
zend_size_t argc, key_len;
HashTable *ht_chan;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &key, &key_len,
&z_arr)==FAILURE)
{
return;
}
ht_chan = Z_ARRVAL_P(z_arr);
if((argc = zend_hash_num_elements(ht_chan)) == 0) {
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK
zval *value;
int i = 0;
argc = argc + 2;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("HMGET", 5)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_HASHTABLE_FOREACH_START(ht_chan, value)
#if PHP_MAJOR_VERSION < 7
convert_to_string(value);
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(value), Z_STRLEN_P(value))
#else
zend_string *convert_str = zval_get_string(value);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
SW_HASHTABLE_FOREACH_END();
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, hExists)
{
sw_redis_command_key_str(INTERNAL_FUNCTION_PARAM_PASSTHRU, "HEXISTS", 7);
}
static PHP_METHOD(swoole_redis_coro, publish)
{
sw_redis_command_key_str(INTERNAL_FUNCTION_PARAM_PASSTHRU, "PUBLISH", 7);
}
static PHP_METHOD(swoole_redis_coro, zDeleteRangeByScore)
{
sw_redis_command_key_str_str(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZREMRANGEBYSCORE", 16);
}
static PHP_METHOD(swoole_redis_coro, zCount)
{
sw_redis_command_key_long_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZCOUNT", 6);
}
static PHP_METHOD(swoole_redis_coro, incrBy)
{
sw_redis_command_key_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "INCRBY", 6);
}
static PHP_METHOD(swoole_redis_coro, hIncrBy)
{
char *key, *mem;
zend_size_t key_len, mem_len;
long byval;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssl", &key, &key_len,
&mem, &mem_len, &byval)==FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[4];
char *argv[4];
SW_REDIS_COMMAND_ARGV_FILL("HINCRBY", 7)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL(mem, mem_len)
char str[32];
sprintf(str, "%ld", byval);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
SW_REDIS_COMMAND(4)
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, hIncrByFloat)
{
char *key, *mem;
zend_size_t key_len, mem_len;
double byval;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssd", &key, &key_len,
&mem, &mem_len, &byval)==FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[4];
char *argv[4];
SW_REDIS_COMMAND_ARGV_FILL("HINCRBYFLOAT", 12)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL(mem, mem_len)
char str[32];
sprintf(str, "%f", byval);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
SW_REDIS_COMMAND(4)
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, incr)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "INCR", 4);
}
static PHP_METHOD(swoole_redis_coro, decrBy)
{
sw_redis_command_key_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DECRBY", 6);
}
static PHP_METHOD(swoole_redis_coro, decr)
{
sw_redis_command_key(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DECR", 4);
}
static PHP_METHOD(swoole_redis_coro, getBit)
{
sw_redis_command_key_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "GETBIT", 6);
}
static PHP_METHOD(swoole_redis_coro, lInsert)
{
char *key, *pos;
zend_size_t key_len, pos_len;
zval *z_val, *z_pivot;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sszz", &key, &key_len,
&pos, &pos_len, &z_pivot, &z_val) == FAILURE)
{
return;
}
if (strncasecmp(pos, "after", 5) && strncasecmp(pos, "before", 6)) {
swoole_php_error(E_WARNING, "Position must be either 'BEFORE' or 'AFTER'");
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[5];
char *argv[5];
SW_REDIS_COMMAND_ARGV_FILL("LINSERT", 7)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
SW_REDIS_COMMAND_ARGV_FILL(pos, pos_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(z_pivot)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(z_val)
SW_REDIS_COMMAND(5);
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, lGet)
{
sw_redis_command_key_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "LINDEX", 6);
}
static PHP_METHOD(swoole_redis_coro, setTimeout)
{
sw_redis_command_key_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "EXPIRE", 6);
}
static PHP_METHOD(swoole_redis_coro, pexpire)
{
sw_redis_command_key_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "PEXPIRE", 7);
}
static PHP_METHOD(swoole_redis_coro, expireAt)
{
sw_redis_command_key_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "EXPIREAT", 8);
}
static PHP_METHOD(swoole_redis_coro, pexpireAt)
{
sw_redis_command_key_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "PEXPIREAT", 9);
}
static PHP_METHOD(swoole_redis_coro, move)
{
sw_redis_command_key_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "MOVE", 4);
}
static PHP_METHOD(swoole_redis_coro, select)
{
long db_number;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &db_number) == FAILURE) {
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[2];
char *argv[2];
SW_REDIS_COMMAND_ARGV_FILL("SELECT", 6)
char str[32];
sprintf(str, "%ld", db_number);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
SW_REDIS_COMMAND(2);
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, getRange)
{
sw_redis_command_key_long_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "GETRANGE", 8);
}
static PHP_METHOD(swoole_redis_coro, listTrim)
{
sw_redis_command_key_long_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "LTRIM", 5);
}
static PHP_METHOD(swoole_redis_coro, lGetRange)
{
sw_redis_command_key_long_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "LRANGE", 6);
}
static PHP_METHOD(swoole_redis_coro, lRem)
{
char *key;
zend_size_t key_len;
long count = 0;
zval *z_val;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &key, &key_len,
&z_val, &count) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[4];
char *argv[4];
SW_REDIS_COMMAND_ARGV_FILL("LREM", 4)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char str[32];
sprintf(str, "%d", (int)count);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(z_val)
SW_REDIS_COMMAND(4)
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, zDeleteRangeByRank)
{
sw_redis_command_key_long_long(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZREMRANGEBYRANK", 15);
}
static PHP_METHOD(swoole_redis_coro, incrByFloat)
{
sw_redis_command_key_dbl(INTERNAL_FUNCTION_PARAM_PASSTHRU, "INCRBYFLOAT", 11);
}
static PHP_METHOD(swoole_redis_coro, bitCount)
{
char *key;
zend_size_t key_len;
long start = 0, end = -1;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &key, &key_len,
&start, &end)==FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[4];
char *argv[4];
SW_REDIS_COMMAND_ARGV_FILL("BITCOUNT", 8)
SW_REDIS_COMMAND_ARGV_FILL(key, key_len)
char str[32];
sprintf(str, "%d", (int)start);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
sprintf(str, "%d", (int)end);
SW_REDIS_COMMAND_ARGV_FILL(str, strlen(str))
SW_REDIS_COMMAND(4)
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, bitOp)
{
int argc = ZEND_NUM_ARGS();
SW_REDIS_COMMAND_ALLOC_ARGS_ARR
if(zend_get_parameters_array(ht, argc, z_args) == FAILURE ||
argc < 3 || SW_REDIS_COMMAND_ARGS_TYPE(z_args[0]) != IS_STRING)
{
efree(z_args);
return;
}
SW_REDIS_COMMAND_CHECK_WITH_FREE_Z_ARGS
int j, i = 0;
argc++;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("BITOP", 5)
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[0]), SW_REDIS_COMMAND_ARGS_STRLEN(z_args[0]))
for(j = 1; j < argc - 1; j++) {
#if PHP_MAJOR_VERSION < 7
convert_to_string(z_args[j]);
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[j]), SW_REDIS_COMMAND_ARGS_STRLEN(z_args[j]))
#else
zend_string *convert_str = zval_get_string(&z_args[j]);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
}
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
efree(z_args);
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, sMove)
{
char *src, *dst;
zend_size_t src_len, dst_len;
zval *z_val;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssz", &src, &src_len,
&dst, &dst_len, &z_val) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t argvlen[4];
char *argv[4];
SW_REDIS_COMMAND_ARGV_FILL("SMOVE", 5)
SW_REDIS_COMMAND_ARGV_FILL(src, src_len)
SW_REDIS_COMMAND_ARGV_FILL(dst, dst_len)
SW_REDIS_COMMAND_ARGV_FILL_WITH_SERIALIZE(z_val)
SW_REDIS_COMMAND(4)
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, sAdd)
{
sw_redis_command_key_var_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SADD", 4);
}
static PHP_METHOD(swoole_redis_coro, sRemove)
{
sw_redis_command_key_var_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SREM", 4);
}
static PHP_METHOD(swoole_redis_coro, zDelete)
{
sw_redis_command_key_var_val(INTERNAL_FUNCTION_PARAM_PASSTHRU, "ZREM", 4);
}
static PHP_METHOD(swoole_redis_coro, pSubscribe)
{
zval *z_arr;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &z_arr) == FAILURE)
{
return;
}
swRedisClient *redis = swoole_get_object(getThis());
if (redis->defer)
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "psubscribe cannot be used with defer enabled" TSRMLS_CC);
RETURN_FALSE;
}
php_context *context = swoole_get_property(getThis(), 0);
switch (redis->state)
{
case SWOOLE_REDIS_CORO_STATE_SUBSCRIBE:
coro_save(context);
coro_yield();
break;
case SWOOLE_REDIS_CORO_STATE_CONNECT:
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "redis client is not connected." TSRMLS_CC);
RETURN_FALSE;
break;
case SWOOLE_REDIS_CORO_STATE_CLOSED:
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "redis client connection is closed." TSRMLS_CC);
RETURN_FALSE;
break;
case SWOOLE_REDIS_CORO_STATE_MULTI:
case SWOOLE_REDIS_CORO_STATE_PIPELINE:
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "redis state mode is multi or pipeline, cann't use subscribe cmd." TSRMLS_CC);
RETURN_FALSE;
break;
default:
break;
}
HashTable *ht_chan = Z_ARRVAL_P(z_arr);
int argc = 1 + zend_hash_num_elements(ht_chan), i = 0;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("PSUBSCRIBE", 10)
zval *value;
SW_HASHTABLE_FOREACH_START(ht_chan, value)
#if PHP_MAJOR_VERSION < 7
convert_to_string(value);
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(value), Z_STRLEN_P(value))
#else
zend_string *convert_str = zval_get_string(value);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
SW_HASHTABLE_FOREACH_END();
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
redis->state = SWOOLE_REDIS_CORO_STATE_SUBSCRIBE;
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, subscribe)
{
zval *z_arr;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &z_arr) == FAILURE)
{
return;
}
swRedisClient *redis = swoole_get_object(getThis());
if (redis->defer)
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "subscribe cannot be used with defer enabled" TSRMLS_CC);
RETURN_FALSE;
}
php_context *context = swoole_get_property(getThis(), 0);
switch (redis->state)
{
case SWOOLE_REDIS_CORO_STATE_SUBSCRIBE:
coro_save(context);
coro_yield();
break;
case SWOOLE_REDIS_CORO_STATE_CONNECT:
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "redis client is not connected." TSRMLS_CC);
RETURN_FALSE;
break;
case SWOOLE_REDIS_CORO_STATE_CLOSED:
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "redis client connection is closed." TSRMLS_CC);
RETURN_FALSE;
break;
case SWOOLE_REDIS_CORO_STATE_MULTI:
case SWOOLE_REDIS_CORO_STATE_PIPELINE:
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "redis state mode is multi or pipeline, cann't use subscribe cmd." TSRMLS_CC);
RETURN_FALSE;
break;
default:
break;
}
HashTable *ht_chan = Z_ARRVAL_P(z_arr);
int argc = 1 + zend_hash_num_elements(ht_chan), i = 0;
SW_REDIS_COMMAND_ALLOC_ARGV
SW_REDIS_COMMAND_ARGV_FILL("SUBSCRIBE", 9)
zval *value;
SW_HASHTABLE_FOREACH_START(ht_chan, value)
#if PHP_MAJOR_VERSION < 7
convert_to_string(value);
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(value), Z_STRLEN_P(value))
#else
zend_string *convert_str = zval_get_string(value);
SW_REDIS_COMMAND_ARGV_FILL(convert_str->val, convert_str->len)
zend_string_release(convert_str);
#endif
SW_HASHTABLE_FOREACH_END();
SW_REDIS_COMMAND(argc)
SW_REDIS_COMMAND_FREE_ARGV
redis->state = SWOOLE_REDIS_CORO_STATE_SUBSCRIBE;
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, multi)
{
long mode = SW_REDIS_MODE_MULTI;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &mode) == FAILURE)
{
return;
}
SW_REDIS_COMMAND_CHECK
if (mode == SW_REDIS_MODE_MULTI)
{
redis->state = SWOOLE_REDIS_CORO_STATE_MULTI;
size_t argvlen[1];
char *argv[1];
argvlen[0] = 5;
argv[0] = estrndup("MULTI", 5);
if (redisAsyncCommandArgv(redis->context, swoole_redis_coro_onResult, NULL, 1, (const char **) argv, (const size_t *) argvlen) < 0)
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "redisAsyncCommandArgv() failed." TSRMLS_CC);
RETURN_FALSE;
}
efree(argv[0]);
redis->queued_cmd_count = 2;
}
else
{
redis->state = SWOOLE_REDIS_CORO_STATE_PIPELINE;
redis->queued_cmd_count = 0;
}
RETURN_ZVAL(getThis(), 1, 0);
}
static PHP_METHOD(swoole_redis_coro, exec)
{
swRedisClient *redis = swoole_get_object(getThis());
if (redis->state != SWOOLE_REDIS_CORO_STATE_MULTI && redis->state != SWOOLE_REDIS_CORO_STATE_PIPELINE)
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "redis state mode is neither multi nor pipeline!" TSRMLS_CC);
RETURN_FALSE;
}
if (redis->state == SWOOLE_REDIS_CORO_STATE_MULTI)
{
size_t argvlen[1];
char *argv[1];
argvlen[0] = 4;
argv[0] = estrndup("EXEC", 4);
if (redisAsyncCommandArgv(redis->context, swoole_redis_coro_onResult, NULL, 1, (const char **) argv, (const size_t *) argvlen) < 0)
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errCode"), SW_REDIS_ERR_OTHER TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, getThis(), ZEND_STRL("errMsg"), "redisAsyncCommandArgv() failed." TSRMLS_CC);
RETURN_FALSE;
}
efree(argv[0]);
}
redis->iowait = SW_REDIS_CORO_STATUS_WAIT;
if (redis->defer)
{
RETURN_TRUE;
}
php_context *context = swoole_get_property(getThis(), 0);
coro_save(context);
coro_yield();
}
static PHP_METHOD(swoole_redis_coro, eval)
{
char *script;
zend_size_t script_len;
zval *params = NULL;
long keys_num = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|al", &script, &script_len, &params, &keys_num) == FAILURE)
{
return;
}
HashTable *params_ht = NULL;
uint32_t params_num = 0;
if (params) {
params_ht = Z_ARRVAL_P(params);
params_num = zend_hash_num_elements(params_ht);
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t *argvlen = emalloc(sizeof(size_t) * (params_num + 3));
char **argv = emalloc(sizeof(char *) * (params_num + 3));
SW_REDIS_COMMAND_ARGV_FILL("EVAL", 4)
SW_REDIS_COMMAND_ARGV_FILL(script, script_len)
char keys_num_str[32] = {0};
sprintf(keys_num_str, "%ld", keys_num);
SW_REDIS_COMMAND_ARGV_FILL(keys_num_str, strlen(keys_num_str));
if (params_ht) {
zval *param;
SW_HASHTABLE_FOREACH_START(params_ht, param)
#if PHP_MAJOR_VERSION < 7
convert_to_string(param);
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(param), Z_STRLEN_P(param))
#else
zend_string *param_str = zval_get_string(param);
SW_REDIS_COMMAND_ARGV_FILL(param_str->val, param_str->len)
zend_string_release(param_str);
#endif
SW_HASHTABLE_FOREACH_END();
}
SW_REDIS_COMMAND(params_num + 3)
efree(argvlen);
efree(argv);
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, evalSha)
{
char *sha;
zend_size_t sha_len;
zval *params = NULL;
long keys_num = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|al", &sha, &sha_len, &params, &keys_num) == FAILURE)
{
return;
}
HashTable *params_ht = NULL;
uint32_t params_num = 0;
if (params) {
params_ht = Z_ARRVAL_P(params);
params_num = zend_hash_num_elements(params_ht);
}
SW_REDIS_COMMAND_CHECK
int i = 0;
size_t *argvlen = emalloc(sizeof(size_t) * (params_num + 3));
char **argv = emalloc(sizeof(char *) * (params_num + 3));
SW_REDIS_COMMAND_ARGV_FILL("EVALSHA", 7)
SW_REDIS_COMMAND_ARGV_FILL(sha, sha_len)
char keys_num_str[32] = {0};
sprintf(keys_num_str, "%ld", keys_num);
SW_REDIS_COMMAND_ARGV_FILL(keys_num_str, strlen(keys_num_str));
if (params) {
zval *param;
SW_HASHTABLE_FOREACH_START(params_ht, param)
#if PHP_MAJOR_VERSION < 7
convert_to_string(param);
SW_REDIS_COMMAND_ARGV_FILL(Z_STRVAL_P(param), Z_STRLEN_P(param))
#else
zend_string *param_str = zval_get_string(param);
SW_REDIS_COMMAND_ARGV_FILL(param_str->val, param_str->len)
zend_string_release(param_str);
#endif
SW_HASHTABLE_FOREACH_END();
}
SW_REDIS_COMMAND(params_num + 3)
efree(argvlen);
efree(argv);
SW_REDIS_COMMAND_YIELD
}
static PHP_METHOD(swoole_redis_coro, script)
{
int argc = ZEND_NUM_ARGS();
if (argc < 1) {
RETURN_FALSE;
}
SW_REDIS_COMMAND_ALLOC_ARGS_ARR
if (zend_get_parameters_array(ht, argc, z_args) == FAILURE || SW_REDIS_COMMAND_ARGS_TYPE(z_args[0]) != IS_STRING) {
efree(z_args);
RETURN_FALSE;
}
SW_REDIS_COMMAND_CHECK
int i = 0;
if (! strcasecmp(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[0]), "flush") || ! strcasecmp(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[0]), "kill")) {
size_t argvlen[2];
char *argv[2];
SW_REDIS_COMMAND_ARGV_FILL("SCRIPT", 6)
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[0]), SW_REDIS_COMMAND_ARGS_STRLEN(z_args[0]))
SW_REDIS_COMMAND(2)
efree(z_args);
SW_REDIS_COMMAND_YIELD
} else if (! strcasecmp(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[0]), "exists")) {
if (argc < 2) {
efree(z_args);
RETURN_FALSE;
} else {
size_t *argvlen = emalloc(sizeof(size_t) * (argc + 1));
char **argv = emalloc(sizeof(char *) * (argc + 1));
SW_REDIS_COMMAND_ARGV_FILL("SCRIPT", 6)
SW_REDIS_COMMAND_ARGV_FILL("EXISTS", 6)
int j = 1;
for (; j < argc; j++) {
#if PHP_MAJOR_VERSION < 7
convert_to_string(z_args[j]);
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[j]), SW_REDIS_COMMAND_ARGS_STRLEN(z_args[j]))
#else
zend_string *z_arg_str = zval_get_string(&z_args[j]);
SW_REDIS_COMMAND_ARGV_FILL(z_arg_str->val, z_arg_str->len)
zend_string_release(z_arg_str);
#endif
}
SW_REDIS_COMMAND(argc + 1)
efree(argvlen);
efree(argv);
efree(z_args);
SW_REDIS_COMMAND_YIELD
}
} else if (! strcasecmp(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[0]), "load")) {
if (argc < 2 || SW_REDIS_COMMAND_ARGS_TYPE(z_args[1]) != IS_STRING) {
efree(z_args);
RETURN_FALSE;
} else {
size_t argvlen[3];
char *argv[3];
SW_REDIS_COMMAND_ARGV_FILL("SCRIPT", 6)
SW_REDIS_COMMAND_ARGV_FILL("LOAD", 4)
SW_REDIS_COMMAND_ARGV_FILL(SW_REDIS_COMMAND_ARGS_STRVAL(z_args[1]), SW_REDIS_COMMAND_ARGS_STRLEN(z_args[1]))
SW_REDIS_COMMAND(3)
efree(z_args);
SW_REDIS_COMMAND_YIELD
}
} else {
efree(z_args);
RETURN_FALSE;
}
}
static void swoole_redis_coro_parse_result(swRedisClient *redis, zval* return_value, redisReply* reply TSRMLS_DC)
{
zval *val;
int j;
#if PHP_MAJOR_VERSION >= 7
zval _val;
val = &_val;
bzero(val, sizeof(zval));
#endif
switch (reply->type)
{
case REDIS_REPLY_INTEGER:
ZVAL_LONG(return_value, reply->integer);
break;
case REDIS_REPLY_ERROR:
ZVAL_FALSE(return_value);
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), redis->context->err TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), redis->context->errstr TSRMLS_CC);
break;
case REDIS_REPLY_STATUS:
if (redis->context->err == 0)
{
if (reply->len > 0)
{
if (strncmp(reply->str, "OK", 2) == 0)
{
ZVAL_BOOL(return_value, 1);
break;
}
long l;
if (strncmp(reply->str, "string", 6) == 0) {
l = SW_REDIS_STRING;
} else if (strncmp(reply->str, "set", 3) == 0){
l = SW_REDIS_SET;
} else if (strncmp(reply->str, "list", 4) == 0){
l = SW_REDIS_LIST;
} else if (strncmp(reply->str, "zset", 4) == 0){
l = SW_REDIS_ZSET;
} else if (strncmp(reply->str, "hash", 4) == 0){
l = SW_REDIS_HASH;
} else {
l = SW_REDIS_NOT_FOUND;
}
ZVAL_LONG(return_value, l);
}
else
{
ZVAL_TRUE(return_value);
}
}
else
{
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), redis->context->err TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), redis->context->errstr TSRMLS_CC);
}
break;
case REDIS_REPLY_STRING:
if (redis->serialize)
{
char *reserve_str = reply->str;
php_unserialize_data_t s_ht;
PHP_VAR_UNSERIALIZE_INIT(s_ht);
#if PHP_MAJOR_VERSION < 7
if(!php_var_unserialize(&return_value,
#else
if(!php_var_unserialize(return_value,
#endif
(const unsigned char**)&reply->str,
(const unsigned char*)reply->str + reply->len, &s_ht TSRMLS_CC)) {
SW_ZVAL_STRINGL(return_value, reply->str, reply->len, 1);
}
PHP_VAR_UNSERIALIZE_DESTROY(s_ht);
reply->str = reserve_str;
}
else
{
SW_ZVAL_STRINGL(return_value, reply->str, reply->len, 1);
}
break;
case REDIS_REPLY_ARRAY:
array_init(return_value);
for (j = 0; j < reply->elements; j++)
{
#if PHP_MAJOR_VERSION < 7
SW_ALLOC_INIT_ZVAL(val);
#endif
swoole_redis_coro_parse_result(redis, val, reply->element[j] TSRMLS_CC);
add_next_index_zval(return_value, val);
}
break;
case REDIS_REPLY_NIL:
default:
ZVAL_NULL(return_value);
return;
}
}
static void swoole_redis_coro_resume(void *data)
{
swRedisClient *redis = (swRedisClient *)data;
redis->iowait = SW_REDIS_CORO_STATUS_READY;
php_context *sw_current_context = swoole_get_property(redis->object, 0);
zval *retval = NULL;
zval *redis_result = redis->result;
int ret = coro_resume(sw_current_context, redis->result, &retval);
if (ret == CORO_END && retval)
{
sw_zval_ptr_dtor(&retval);
}
sw_zval_free(redis_result);
}
static void swoole_redis_coro_onResult(redisAsyncContext *c, void *r, void *privdata)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
redisReply *reply = r;
zval *result;
#if PHP_MAJOR_VERSION < 7
zval **type;
#else
zval *type;
#endif
SW_ALLOC_INIT_ZVAL(result);
swRedisClient *redis = c->ev.data;
if (reply == NULL)
{
if (redis->state == SWOOLE_REDIS_CORO_STATE_CLOSING)
{
sw_zval_free(result);
return;
}
ZVAL_FALSE(result);
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), c->err TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), c->errstr TSRMLS_CC);
if (redis->pipeline_result != NULL)
{
sw_zval_free(redis->pipeline_result);
redis->pipeline_result = NULL;
}
} else {
swoole_redis_coro_parse_result(redis, result, reply TSRMLS_CC);
switch (redis->state) {
case SWOOLE_REDIS_CORO_STATE_PIPELINE:
if (redis->pipeline_result == NULL)
{
SW_ALLOC_INIT_ZVAL(redis->pipeline_result);
array_init(redis->pipeline_result);
}
redis->queued_cmd_count--;
add_next_index_zval(redis->pipeline_result, result);
#if PHP_MAJOR_VERSION >= 7
efree(result);
#endif
if (redis->queued_cmd_count > 0)
{
return;
}
result = redis->pipeline_result;
redis->pipeline_result = NULL;
redis->state = SWOOLE_REDIS_CORO_STATE_READY;
break;
case SWOOLE_REDIS_CORO_STATE_MULTI:
redis->queued_cmd_count--;
if (redis->queued_cmd_count > 0)
{
sw_zval_free(result);
return;
}
redis->state = SWOOLE_REDIS_CORO_STATE_READY;
break;
case SWOOLE_REDIS_CORO_STATE_SUBSCRIBE:
#if PHP_MAJOR_VERSION < 7
if (zend_hash_index_find(Z_ARRVAL_P(result), 0, (void **)&type) == FAILURE)
#else
type = zend_hash_index_find(Z_ARRVAL_P(result), 0);
if (!type)
#endif
{
sw_zval_free(result);
return;
}
#if PHP_MAJOR_VERSION < 7
if (strncasecmp(Z_STRVAL_PP(type), "subscribe", 9) == 0 || strncasecmp(Z_STRVAL_PP(type), "psubscribe", 10) == 0)
#else
if (strncasecmp(Z_STRVAL_P(type), "subscribe", 9) == 0 || strncasecmp(Z_STRVAL_P(type), "psubscribe", 10) == 0)
#endif
{
sw_zval_free(result);
return;
}
break;
default:
redis->state = SWOOLE_REDIS_CORO_STATE_READY;
break;
}
}
/* et reactor defer callback */
redis->iowait = SW_REDIS_CORO_STATUS_DONE;
redis->result = result;
if (!redis->defer || redis->_defer)
{
redis->_defer = 0;
SwooleG.main_reactor->defer(SwooleG.main_reactor, swoole_redis_coro_resume, redis);
}
}
void swoole_redis_coro_onConnect(const redisAsyncContext *c, int status)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
swRedisClient *redis = c->ev.data;
zval *result;
SW_ALLOC_INIT_ZVAL(result);
if (status != REDIS_OK)
{
ZVAL_BOOL(result, 0);
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), c->err TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), c->errstr TSRMLS_CC);
}
else
{
ZVAL_BOOL(result, 1);
redis->state = SWOOLE_REDIS_CORO_STATE_READY;
redis->iowait = SW_REDIS_CORO_STATUS_READY;
}
redis->result = result;
SwooleG.main_reactor->defer(SwooleG.main_reactor, swoole_redis_coro_resume, redis);
}
static void swoole_redis_coro_onClose(const redisAsyncContext *c, int status)
{
swRedisClient *redis = c->ev.data;
redis->state = SWOOLE_REDIS_CORO_STATE_CLOSED;
redis->context = NULL;
#if PHP_MAJOR_VERSION < 7
sw_zval_ptr_dtor(&redis->object);
#endif
}
static void swoole_redis_coro_event_AddRead(void *privdata)
{
swRedisClient *redis = (swRedisClient*) privdata;
if (redis->context && SwooleG.main_reactor)
{
swReactor_add_event(SwooleG.main_reactor, redis->context->c.fd, SW_EVENT_READ);
}
}
static void swoole_redis_coro_event_DelRead(void *privdata)
{
swRedisClient *redis = (swRedisClient*) privdata;
if (redis->context && SwooleG.main_reactor)
{
swReactor_del_event(SwooleG.main_reactor, redis->context->c.fd, SW_EVENT_READ);
}
}
static void swoole_redis_coro_event_AddWrite(void *privdata)
{
swRedisClient *redis = (swRedisClient*) privdata;
if (redis->context && SwooleG.main_reactor)
{
swReactor_add_event(SwooleG.main_reactor, redis->context->c.fd, SW_EVENT_WRITE);
}
}
static void swoole_redis_coro_event_DelWrite(void *privdata)
{
swRedisClient *redis = (swRedisClient*) privdata;
if (redis->context && SwooleG.main_reactor)
{
swReactor_del_event(SwooleG.main_reactor, redis->context->c.fd, SW_EVENT_WRITE);
}
}
static void swoole_redis_coro_event_Cleanup(void *privdata)
{
swRedisClient *redis = (swRedisClient*) privdata;
redis->state = SWOOLE_REDIS_CORO_STATE_CLOSED;
if (redis->context && SwooleG.main_reactor)
{
SwooleG.main_reactor->del(SwooleG.main_reactor, redis->context->c.fd);
}
}
static int swoole_redis_coro_onError(swReactor *reactor, swEvent *event)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
swRedisClient *redis = event->socket->object;
redisAsyncContext *c = redis->context;
zend_update_property_long(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errCode"), c->err TSRMLS_CC);
zend_update_property_string(swoole_redis_coro_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), c->errstr TSRMLS_CC);
zval *retval = NULL;
sw_zend_call_method_with_0_params(&redis->object, swoole_redis_coro_class_entry_ptr, NULL, "close", &retval);
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
return SW_OK;
}
static int swoole_redis_coro_onRead(swReactor *reactor, swEvent *event)
{
swRedisClient *redis = event->socket->object;
redisAsyncHandleRead(redis->context);
return SW_OK;
}
static int swoole_redis_coro_onWrite(swReactor *reactor, swEvent *event)
{
swRedisClient *redis = event->socket->object;
redisAsyncHandleWrite(redis->context);
return SW_OK;
}
#endif
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/zhangcunli/swoole.git
git@gitee.com:zhangcunli/swoole.git
zhangcunli
swoole
swoole-src
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385