ccdserver.cpp 892 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jmisc.hpp"
  14. #include "jdebug.hpp"
  15. #include "jptree.hpp"
  16. #include "rtlkey.hpp"
  17. #include "jsort.hpp"
  18. #include "jhtree.hpp"
  19. #include "jqueue.tpp"
  20. #include "jisem.hpp"
  21. #include "thorxmlread.hpp"
  22. #include "thorrparse.ipp"
  23. #include "thorxmlwrite.hpp"
  24. #include "thorsoapcall.hpp"
  25. #include "thorcommon.ipp"
  26. #include "jlzw.hpp"
  27. #include "javahash.hpp"
  28. #include "javahash.tpp"
  29. #include "thorstep.ipp"
  30. #include "thorpipe.hpp"
  31. #include "thorfile.hpp"
  32. #include "eclhelper.hpp"
  33. #include "eclrtl_imp.hpp"
  34. #include "rtlfield_imp.hpp"
  35. #include "rtlds_imp.hpp"
  36. #include "rtlread_imp.hpp"
  37. #include "dafdesc.hpp"
  38. #include "dautils.hpp"
  39. namespace ccdserver_hqlhelper
  40. {
  41. #include "eclhelper_base.hpp"
  42. }
  43. #include "ccd.hpp"
  44. #include "ccdserver.hpp"
  45. #include "ccdcontext.hpp"
  46. #include "ccdactivities.hpp"
  47. #include "ccdquery.hpp"
  48. #include "ccdstate.hpp"
  49. #include "ccdqueue.ipp"
  50. #include "ccdsnmp.hpp"
  51. #include "ccddali.hpp"
  52. #include "jsmartsock.hpp"
  53. #include "dllserver.hpp"
  54. #include "workflow.hpp"
  55. #include "roxiemem.hpp"
  56. #include "roxierowbuff.hpp"
  57. #include "roxiehelper.hpp"
  58. #include "roxielmj.hpp"
  59. #include "roxierow.hpp"
  60. #include "thorplugin.hpp"
  61. #include "keybuild.hpp"
  62. #define MAX_HTTP_HEADERSIZE 8000
  63. #define MIN_PAYLOAD_SIZE 800
  64. #pragma warning(disable : 4355)
  65. #define DEFAULT_PARALLEL_LOOP_THREADS 1
  66. #define PROBE
  67. #ifdef _DEBUG
  68. //#define FAKE_EXCEPTIONS
  69. //#define TRACE_JOINGROUPS
  70. //#define TRACE_SPLIT
  71. //#define _CHECK_HEAPSORT
  72. //#undef PARALLEL_EXECUTE
  73. //#define TRACE_SEEK_REQUESTS
  74. #endif
  75. using roxiemem::OwnedRoxieRow;
  76. using roxiemem::OwnedRoxieString;
  77. using roxiemem::OwnedConstRoxieRow;
  78. using roxiemem::IRowManager;
  79. // There is a bug in VC6 implemetation of protected which prevents nested classes from accessing owner's data. It can be tricky to work around - hence...
  80. #if _MSC_VER==1200
  81. #define protected public
  82. #endif
  83. #define TRACE_STARTSTOP // This determines if it is available - it is enabled/disabled by a configuration option
  84. static const SmartStepExtra dummySmartStepExtra(SSEFreadAhead, NULL);
  85. inline void ReleaseRoxieRowSet(ConstPointerArray &data)
  86. {
  87. ForEachItemIn(idx, data)
  88. ReleaseRoxieRow(data.item(idx));
  89. data.kill();
  90. }
  91. //=================================================================================
  92. class RestartableThread : public CInterface
  93. {
  94. class MyThread : public Thread
  95. {
  96. Linked<RestartableThread> owner;
  97. public:
  98. MyThread(RestartableThread *_owner, const char *name) : Thread(name), owner(_owner)
  99. {
  100. }
  101. virtual int run()
  102. {
  103. owner->started.signal();
  104. return owner->run();
  105. }
  106. };
  107. friend class MyThread;
  108. Semaphore started;
  109. Owned<MyThread> thread;
  110. CriticalSection crit;
  111. StringAttr name;
  112. public:
  113. RestartableThread(const char *_name) : name(_name)
  114. {
  115. }
  116. virtual void start(const char *namePrefix)
  117. {
  118. StringBuffer s(namePrefix);
  119. s.append(name);
  120. {
  121. CriticalBlock b(crit);
  122. assertex(!thread);
  123. thread.setown(new MyThread(this, s));
  124. thread->start();
  125. }
  126. started.wait();
  127. }
  128. virtual void join()
  129. {
  130. {
  131. Owned<Thread> tthread;
  132. {
  133. CriticalBlock b(crit);
  134. tthread.setown(thread.getClear());
  135. }
  136. if (tthread)
  137. tthread->join();
  138. }
  139. }
  140. virtual int run() = 0;
  141. };
  142. //================================================================================
  143. // default implementation - can be overridden for efficiency...
  144. bool IRoxieInput::nextGroup(ConstPointerArray & group)
  145. {
  146. // MORE - this should be replaced with a version that reads to a builder
  147. const void * next;
  148. while ((next = nextInGroup()) != NULL)
  149. group.append(next);
  150. if (group.ordinality())
  151. return true;
  152. return false;
  153. }
  154. void IRoxieInput::readAll(RtlLinkedDatasetBuilder &builder)
  155. {
  156. loop
  157. {
  158. const void *nextrec = nextInGroup();
  159. if (!nextrec)
  160. {
  161. nextrec = nextInGroup();
  162. if (!nextrec)
  163. break;
  164. builder.appendEOG();
  165. }
  166. builder.appendOwn(nextrec);
  167. }
  168. }
  169. inline const void * nextUngrouped(IRoxieInput * input)
  170. {
  171. const void * ret = input->nextInGroup();
  172. if (!ret)
  173. ret = input->nextInGroup();
  174. return ret;
  175. };
  176. //=================================================================================
  177. //The following don't link their arguments because that creates a circular reference
  178. //But I wish there was a better way
  179. class IndirectSlaveContext : public CInterface, implements IRoxieSlaveContext
  180. {
  181. public:
  182. IndirectSlaveContext(IRoxieSlaveContext * _ctx = NULL) : ctx(_ctx) {}
  183. IMPLEMENT_IINTERFACE
  184. void set(IRoxieSlaveContext * _ctx) { ctx = _ctx; }
  185. virtual ICodeContext *queryCodeContext()
  186. {
  187. return ctx->queryCodeContext();
  188. }
  189. virtual void checkAbort()
  190. {
  191. ctx->checkAbort();
  192. }
  193. virtual void notifyAbort(IException *E)
  194. {
  195. ctx->notifyAbort(E);
  196. }
  197. virtual IActivityGraph * queryChildGraph(unsigned id)
  198. {
  199. return ctx->queryChildGraph(id);
  200. }
  201. virtual void noteChildGraph(unsigned id, IActivityGraph *childGraph)
  202. {
  203. ctx->noteChildGraph(id, childGraph) ;
  204. }
  205. virtual IRowManager &queryRowManager()
  206. {
  207. return ctx->queryRowManager();
  208. }
  209. virtual void noteStatistic(unsigned statCode, unsigned __int64 value, unsigned count) const
  210. {
  211. ctx->noteStatistic(statCode, value, count);
  212. }
  213. virtual void CTXLOG(const char *format, ...) const
  214. {
  215. va_list args;
  216. va_start(args, format);
  217. ctx->CTXLOGva(format, args);
  218. va_end(args);
  219. }
  220. virtual void CTXLOGva(const char *format, va_list args) const
  221. {
  222. ctx->CTXLOGva(format, args);
  223. }
  224. virtual void CTXLOGa(TracingCategory category, const char *prefix, const char *text) const
  225. {
  226. ctx->CTXLOGa(category, prefix, text);
  227. }
  228. virtual void logOperatorException(IException *E, const char *file, unsigned line, const char *format, ...) const
  229. {
  230. va_list args;
  231. va_start(args, format);
  232. ctx->logOperatorExceptionVA(E, file, line, format, args);
  233. va_end(args);
  234. }
  235. virtual void logOperatorExceptionVA(IException *E, const char *file, unsigned line, const char *format, va_list args) const
  236. {
  237. ctx->logOperatorExceptionVA(E, file, line, format, args);
  238. }
  239. virtual void CTXLOGae(IException *E, const char *file, unsigned line, const char *prefix, const char *format, ...) const
  240. {
  241. va_list args;
  242. va_start(args, format);
  243. ctx->CTXLOGaeva(E, file, line, prefix, format, args);
  244. va_end(args);
  245. }
  246. virtual void CTXLOGaeva(IException *E, const char *file, unsigned line, const char *prefix, const char *format, va_list args) const
  247. {
  248. ctx->CTXLOGaeva(E, file, line, prefix, format, args);
  249. }
  250. virtual void CTXLOGl(LogItem *log) const
  251. {
  252. ctx->CTXLOGl(log);
  253. }
  254. virtual StringBuffer &getLogPrefix(StringBuffer &ret) const
  255. {
  256. return ctx->getLogPrefix(ret);
  257. }
  258. virtual unsigned queryTraceLevel() const
  259. {
  260. return ctx->queryTraceLevel();
  261. }
  262. virtual bool isIntercepted() const
  263. {
  264. return ctx->isIntercepted();
  265. }
  266. virtual bool isBlind() const
  267. {
  268. return ctx->isBlind();
  269. }
  270. virtual unsigned parallelJoinPreload()
  271. {
  272. return ctx->parallelJoinPreload();
  273. }
  274. virtual unsigned concatPreload()
  275. {
  276. return ctx->concatPreload();
  277. }
  278. virtual unsigned fetchPreload()
  279. {
  280. return ctx->fetchPreload();
  281. }
  282. virtual unsigned fullKeyedJoinPreload()
  283. {
  284. return ctx->fullKeyedJoinPreload();
  285. }
  286. virtual unsigned keyedJoinPreload()
  287. {
  288. return ctx->keyedJoinPreload();
  289. }
  290. virtual unsigned prefetchProjectPreload()
  291. {
  292. return ctx->prefetchProjectPreload();
  293. }
  294. virtual void addSlavesReplyLen(unsigned len)
  295. {
  296. ctx->addSlavesReplyLen(len);
  297. }
  298. virtual const char *queryAuthToken()
  299. {
  300. return ctx->queryAuthToken();
  301. }
  302. virtual const IResolvedFile *resolveLFN(const char *filename, bool isOpt)
  303. {
  304. return ctx->resolveLFN(filename, isOpt);
  305. }
  306. virtual IRoxieWriteHandler *createLFN(const char *filename, bool overwrite, bool extend, const StringArray &clusters)
  307. {
  308. return ctx->createLFN(filename, overwrite, extend, clusters);
  309. }
  310. virtual void onFileCallback(const RoxiePacketHeader &header, const char *lfn, bool isOpt, bool isLocal)
  311. {
  312. ctx->onFileCallback(header, lfn, isOpt, isLocal);
  313. }
  314. virtual IActivityGraph *getLibraryGraph(const LibraryCallFactoryExtra &extra, IRoxieServerActivity *parentActivity)
  315. {
  316. return ctx->getLibraryGraph(extra, parentActivity);
  317. }
  318. virtual void noteProcessed(const IRoxieContextLogger &_activityContext, const IRoxieServerActivity *_activity, unsigned _idx, unsigned _processed, unsigned __int64 _totalCycles, unsigned __int64 _localCycles) const
  319. {
  320. ctx->noteProcessed(_activityContext, _activity, _idx, _processed, _totalCycles, _localCycles);
  321. }
  322. virtual IProbeManager *queryProbeManager() const
  323. {
  324. return ctx->queryProbeManager();
  325. }
  326. virtual IDebuggableContext *queryDebugContext() const
  327. {
  328. return ctx->queryDebugContext();
  329. }
  330. virtual bool queryTraceActivityTimes() const
  331. {
  332. return ctx->queryTraceActivityTimes();
  333. }
  334. virtual bool queryCheckingHeap() const
  335. {
  336. return ctx->queryCheckingHeap();
  337. }
  338. virtual bool queryTimeActivities() const
  339. {
  340. return ctx->queryTimeActivities();
  341. }
  342. virtual void printResults(IXmlWriter *output, const char *name, unsigned sequence)
  343. {
  344. ctx->printResults(output, name, sequence);
  345. }
  346. virtual void setWUState(WUState state)
  347. {
  348. ctx->setWUState(state);
  349. }
  350. virtual bool checkWuAborted()
  351. {
  352. return ctx->checkWuAborted();
  353. }
  354. virtual IWorkUnit *updateWorkUnit() const
  355. {
  356. return ctx->updateWorkUnit();
  357. }
  358. virtual IConstWorkUnit *queryWorkUnit() const
  359. {
  360. return ctx->queryWorkUnit();
  361. }
  362. virtual IRoxieServerContext *queryServerContext()
  363. {
  364. return ctx->queryServerContext();
  365. }
  366. virtual IWorkUnitRowReader *getWorkunitRowReader(const char *wuid, const char * name, unsigned sequence, IXmlToRowTransformer * xmlTransformer, IEngineRowAllocator *rowAllocator, bool isGrouped)
  367. {
  368. return ctx->getWorkunitRowReader(wuid, name, sequence, xmlTransformer, rowAllocator, isGrouped);
  369. }
  370. protected:
  371. IRoxieSlaveContext * ctx;
  372. };
  373. //=================================================================================
  374. #define RESULT_FLUSH_THRESHOLD 10000u
  375. #ifdef _DEBUG
  376. #define SOAP_SPLIT_THRESHOLD 100u
  377. #define SOAP_SPLIT_RESERVE 200u
  378. #else
  379. #define SOAP_SPLIT_THRESHOLD 64000u
  380. #define SOAP_SPLIT_RESERVE 65535u
  381. #endif
  382. //=================================================================================
  383. class CRoxieServerActivityFactoryBase : public CActivityFactory, implements IRoxieServerActivityFactory
  384. {
  385. protected:
  386. IntArray dependencies; // things I am dependent on
  387. IntArray dependencyIndexes; // things I am dependent on
  388. IntArray dependencyControlIds; // things I am dependent on
  389. StringArray dependencyEdgeIds; // How to describe them to the debugger
  390. unsigned dependentCount; // things dependent on me
  391. mutable CriticalSection statsCrit;
  392. mutable __int64 processed;
  393. mutable __int64 started;
  394. mutable __int64 totalCycles;
  395. mutable __int64 localCycles;
  396. public:
  397. IMPLEMENT_IINTERFACE;
  398. CRoxieServerActivityFactoryBase(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  399. : CActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  400. {
  401. processed = 0;
  402. started = 0;
  403. totalCycles = 0;
  404. localCycles = 0;
  405. dependentCount = 0;
  406. }
  407. ~CRoxieServerActivityFactoryBase()
  408. {
  409. }
  410. StringBuffer &toString(StringBuffer &ret) const
  411. {
  412. return ret.appendf("%p", this);
  413. }
  414. virtual void addDependency(unsigned _source, ThorActivityKind _kind, unsigned _sourceIdx, int controlId, const char *edgeId)
  415. {
  416. dependencies.append(_source);
  417. dependencyIndexes.append(_sourceIdx);
  418. dependencyControlIds.append(controlId);
  419. dependencyEdgeIds.append(edgeId);
  420. }
  421. virtual void noteDependent(unsigned target)
  422. {
  423. dependentCount++;
  424. }
  425. virtual IntArray &queryDependencies() { return dependencies; }
  426. virtual IntArray &queryDependencyIndexes() { return dependencyIndexes; }
  427. virtual IntArray &queryDependencyControlIds() { return dependencyControlIds; }
  428. virtual StringArray &queryDependencyEdgeIds() { return dependencyEdgeIds; }
  429. virtual unsigned queryId() const { return id; }
  430. virtual unsigned querySubgraphId() const { return subgraphId; }
  431. virtual ThorActivityKind getKind() const { return kind; }
  432. virtual IOutputMetaData * queryOutputMeta() const
  433. {
  434. return meta;
  435. }
  436. virtual bool isSink() const
  437. {
  438. return false;
  439. }
  440. virtual bool isFunction() const
  441. {
  442. return false;
  443. }
  444. virtual bool isGraphInvariant() const
  445. {
  446. return false;
  447. }
  448. virtual IHThorArg &getHelper() const
  449. {
  450. return *helperFactory();
  451. }
  452. virtual IRoxieServerActivity *createFunction(IHThorArg &arg, IProbeManager *_probeManager) const
  453. {
  454. arg.Release();
  455. throwUnexpected();
  456. }
  457. virtual void noteProcessed(unsigned idx, unsigned _processed, unsigned __int64 _totalCycles, unsigned __int64 _localCycles) const
  458. {
  459. if (_processed || _totalCycles || _localCycles)
  460. {
  461. CriticalBlock b(statsCrit);
  462. #ifdef _DEBUG
  463. assertex(_totalCycles >= _localCycles);
  464. #endif
  465. processed += _processed;
  466. totalCycles += _totalCycles;
  467. localCycles += _localCycles;
  468. }
  469. }
  470. virtual void noteStarted() const
  471. {
  472. CriticalBlock b(statsCrit);
  473. started ++;
  474. }
  475. virtual void noteStarted(unsigned idx) const
  476. {
  477. throwUnexpected(); // should be implemented/required by multiOutput cases only
  478. }
  479. virtual void getEdgeProgressInfo(unsigned output, IPropertyTree &edge) const
  480. {
  481. CriticalBlock b(statsCrit);
  482. if (output == 0)
  483. {
  484. putStatsValue(&edge, "count", "sum", processed);
  485. if (started)
  486. putStatsValue(&edge, "started", "sum", started);
  487. }
  488. else
  489. ERRLOG("unexpected call to getEdgeProcessInfo for output %d in activity %d", output, queryId());
  490. }
  491. virtual void getNodeProgressInfo(IPropertyTree &node) const
  492. {
  493. CActivityFactory::getNodeProgressInfo(node);
  494. CriticalBlock b(statsCrit);
  495. if (started)
  496. putStatsValue(&node, "_roxieStarted", "sum", started);
  497. if (totalCycles)
  498. putStatsValue(&node, "totalTime", "sum", (unsigned) (cycle_to_nanosec(totalCycles)/1000));
  499. if (localCycles)
  500. putStatsValue(&node, "localTime", "sum", (unsigned) (cycle_to_nanosec(localCycles)/1000));
  501. }
  502. virtual void resetNodeProgressInfo()
  503. {
  504. CActivityFactory::resetNodeProgressInfo();
  505. CriticalBlock b(statsCrit);
  506. started = 0;
  507. totalCycles = 0;
  508. localCycles = 0;
  509. }
  510. virtual void getActivityMetrics(StringBuffer &reply) const
  511. {
  512. CActivityFactory::getActivityMetrics(reply);
  513. CriticalBlock b(statsCrit);
  514. putStatsValue(reply, "_roxieStarted", "sum", started);
  515. putStatsValue(reply, "totalTime", "sum", (unsigned) (cycle_to_nanosec(totalCycles)/1000));
  516. putStatsValue(reply, "localTime", "sum", (unsigned) (cycle_to_nanosec(localCycles)/1000));
  517. }
  518. virtual unsigned __int64 queryLocalCycles() const
  519. {
  520. return localCycles;
  521. }
  522. virtual IQueryFactory &queryQueryFactory() const
  523. {
  524. return CActivityFactory::queryQueryFactory();
  525. }
  526. virtual ActivityArray *queryChildQuery(unsigned idx, unsigned &id)
  527. {
  528. return CActivityFactory::queryChildQuery(idx, id);
  529. }
  530. virtual void addChildQuery(unsigned id, ActivityArray *childQuery)
  531. {
  532. CActivityFactory::addChildQuery(id, childQuery);
  533. }
  534. virtual void createChildQueries(IArrayOf<IActivityGraph> &childGraphs, IRoxieServerActivity *parentActivity, IProbeManager *_probeManager, const IRoxieContextLogger &_logctx) const
  535. {
  536. ForEachItemIn(idx, childQueries)
  537. {
  538. childGraphs.append(*createActivityGraph(NULL, childQueryIndexes.item(idx), childQueries.item(idx), parentActivity, _probeManager, _logctx));
  539. }
  540. }
  541. virtual void onCreateChildQueries(IRoxieSlaveContext *ctx, IHThorArg *colocalArg, IArrayOf<IActivityGraph> &childGraphs) const
  542. {
  543. ForEachItemIn(idx, childGraphs)
  544. {
  545. ctx->noteChildGraph(childQueryIndexes.item(idx), &childGraphs.item(idx));
  546. childGraphs.item(idx).onCreate(ctx, colocalArg);
  547. }
  548. }
  549. IActivityGraph * createChildGraph(IRoxieSlaveContext * ctx, IHThorArg *colocalArg, unsigned childId, IRoxieServerActivity *parentActivity, IProbeManager * _probeManager, const IRoxieContextLogger &_logctx) const
  550. {
  551. unsigned match = childQueryIndexes.find(childId);
  552. assertex(match != NotFound);
  553. Owned<IActivityGraph> graph = createActivityGraph(NULL, childQueryIndexes.item(match), childQueries.item(match), parentActivity, _probeManager, _logctx);
  554. graph->onCreate(ctx, colocalArg);
  555. return graph.getClear();
  556. }
  557. virtual IRoxieServerSideCache *queryServerSideCache() const
  558. {
  559. return NULL; // Activities that wish to support server-side caching will need to do better....
  560. }
  561. virtual bool getEnableFieldTranslation() const
  562. {
  563. throwUnexpected(); // only implemented by index-related subclasses
  564. }
  565. virtual IDefRecordMeta *queryActivityMeta() const
  566. {
  567. throwUnexpected(); // only implemented by index-related subclasses
  568. }
  569. virtual void noteStatistic(unsigned statCode, unsigned __int64 value, unsigned count) const
  570. {
  571. mystats.noteStatistic(statCode, value, count);
  572. }
  573. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  574. {
  575. // Most activities have nothing to say...
  576. }
  577. };
  578. class CRoxieServerMultiInputInfo
  579. {
  580. private:
  581. UnsignedArray inputs;
  582. UnsignedArray inputIndexes;
  583. public:
  584. void set(unsigned idx, unsigned source, unsigned sourceidx)
  585. {
  586. if (idx==inputs.length())
  587. {
  588. inputs.append(source);
  589. inputIndexes.append(sourceidx);
  590. }
  591. else
  592. {
  593. while (!inputs.isItem(idx))
  594. {
  595. inputs.append(0);
  596. inputIndexes.append(0);
  597. }
  598. inputs.replace(source, idx);
  599. inputIndexes.replace(sourceidx, idx);
  600. }
  601. }
  602. unsigned get(unsigned idx, unsigned &sourceidx) const
  603. {
  604. if (inputs.isItem(idx))
  605. {
  606. sourceidx = inputIndexes.item(idx);
  607. return inputs.item(idx);
  608. }
  609. else
  610. return (unsigned) -1;
  611. }
  612. inline unsigned ordinality() const { return inputs.ordinality(); }
  613. };
  614. class CRoxieServerMultiInputFactory : public CRoxieServerActivityFactoryBase
  615. {
  616. private:
  617. CRoxieServerMultiInputInfo inputs;
  618. public:
  619. IMPLEMENT_IINTERFACE;
  620. CRoxieServerMultiInputFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  621. : CRoxieServerActivityFactoryBase(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  622. {
  623. }
  624. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  625. {
  626. inputs.set(idx, source, sourceidx);
  627. }
  628. virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
  629. {
  630. return inputs.get(idx, sourceidx);
  631. }
  632. virtual unsigned numInputs() const { return inputs.ordinality(); }
  633. };
  634. class CWrappedException : public CInterface, implements IException
  635. {
  636. Owned<IException> wrapped;
  637. ThorActivityKind kind;
  638. unsigned queryId;
  639. public:
  640. IMPLEMENT_IINTERFACE;
  641. CWrappedException(IException *_wrapped, ThorActivityKind _kind, unsigned _queryId)
  642. : wrapped(_wrapped), kind(_kind), queryId(_queryId)
  643. {
  644. }
  645. virtual int errorCode() const { return wrapped->errorCode(); }
  646. virtual StringBuffer & errorMessage(StringBuffer &msg) const { return wrapped->errorMessage(msg).appendf(" (in %s %d)", getActivityText(kind), queryId); }
  647. virtual MessageAudience errorAudience() const { return wrapped->errorAudience(); }
  648. };
  649. class CRoxieServerActivityFactory : public CRoxieServerActivityFactoryBase
  650. {
  651. protected:
  652. unsigned input;
  653. unsigned inputidx;
  654. public:
  655. IMPLEMENT_IINTERFACE;
  656. CRoxieServerActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  657. : CRoxieServerActivityFactoryBase(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  658. {
  659. input = (unsigned) -1;
  660. inputidx = 0;
  661. }
  662. inline void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  663. {
  664. if (idx != 0)
  665. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: id = %d : setInput() parameter out of bounds idx = %d at %s(%d)", id, idx, __FILE__, __LINE__);
  666. if (input != -1)
  667. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: id = %d : setInput() called twice for input = %d source = %d inputidx = %d sourceidx = %d at %s(%d)", id, input, source, inputidx, sourceidx, __FILE__, __LINE__);
  668. input = source;
  669. inputidx = sourceidx;
  670. }
  671. virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
  672. {
  673. if (!idx)
  674. {
  675. sourceidx = inputidx;
  676. return input;
  677. }
  678. return (unsigned) -1;
  679. }
  680. virtual unsigned numInputs() const { return (input == (unsigned)-1) ? 0 : 1; }
  681. };
  682. class CRoxieServerMultiOutputFactory : public CRoxieServerActivityFactory
  683. {
  684. protected:
  685. unsigned numOutputs;
  686. unsigned __int64 *processedArray;
  687. bool *startedArray;
  688. CRoxieServerMultiOutputFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  689. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  690. {
  691. numOutputs = 0;
  692. processedArray = NULL;
  693. startedArray = NULL;
  694. }
  695. ~CRoxieServerMultiOutputFactory()
  696. {
  697. delete [] processedArray;
  698. delete [] startedArray;
  699. }
  700. void setNumOutputs(unsigned num)
  701. {
  702. numOutputs = num;
  703. if (!num)
  704. num = 1; // Even sink activities like to track how many records they process
  705. processedArray = new unsigned __int64[num];
  706. startedArray = new bool[num];
  707. for (unsigned i = 0; i < num; i++)
  708. {
  709. processedArray[i] = 0;
  710. startedArray[i] = 0;
  711. }
  712. }
  713. virtual void getEdgeProgressInfo(unsigned idx, IPropertyTree &edge) const
  714. {
  715. assertex(numOutputs ? idx < numOutputs : idx==0);
  716. CriticalBlock b(statsCrit);
  717. putStatsValue(&edge, "count", "sum", processedArray[idx]);
  718. putStatsValue(&edge, "started", "sum", startedArray[idx]);
  719. }
  720. virtual void noteProcessed(unsigned idx, unsigned _processed, unsigned __int64 _totalCycles, unsigned __int64 _localCycles) const
  721. {
  722. assertex(numOutputs ? idx < numOutputs : idx==0);
  723. CriticalBlock b(statsCrit);
  724. processedArray[idx] += _processed;
  725. totalCycles += _totalCycles;
  726. localCycles += _localCycles;
  727. }
  728. virtual void noteStarted(unsigned idx) const
  729. {
  730. assertex(numOutputs ? idx < numOutputs : idx==0);
  731. CriticalBlock b(statsCrit);
  732. startedArray[idx] = true;
  733. }
  734. };
  735. class CRoxieServerInternalSinkFactory : public CRoxieServerActivityFactory
  736. {
  737. protected:
  738. bool isInternal;
  739. bool isRoot;
  740. unsigned usageCount;
  741. public:
  742. CRoxieServerInternalSinkFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
  743. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  744. {
  745. usageCount = _usageCount;
  746. isRoot = _isRoot;
  747. isInternal = false; // filled in by derived class constructor
  748. }
  749. virtual bool isSink() const
  750. {
  751. //only a sink if a root activity
  752. return isRoot && !(isInternal && dependentCount && dependentCount==usageCount); // MORE - it's possible for this to get the answer wrong still, since usageCount does not include references from main procedure. Gavin?
  753. }
  754. virtual void getEdgeProgressInfo(unsigned idx, IPropertyTree &edge) const
  755. {
  756. // There is no meaningful info to return along the dependency edge - we don't detect how many times the value has been read from the context
  757. // Just leave it blank is safest.
  758. }
  759. };
  760. typedef enum { STATEreset, STATEstarted, STATEstopped, STATEstarting } activityState;
  761. const char *queryStateText(activityState state)
  762. {
  763. switch (state)
  764. {
  765. case STATEreset: return "reset";
  766. case STATEstarted: return "started";
  767. case STATEstopped: return "stopped";
  768. case STATEstarting: return "starting";
  769. default: return "unknown";
  770. }
  771. }
  772. typedef ICopyArrayOf<IRoxieServerActivity> IRoxieServerActivityCopyArray;
  773. class CParallelActivityExecutor : public CAsyncFor
  774. {
  775. public:
  776. unsigned parentExtractSize;
  777. const byte * parentExtract;
  778. CParallelActivityExecutor(IRoxieServerActivityCopyArray & _activities, unsigned _parentExtractSize, const byte * _parentExtract) :
  779. activities(_activities), parentExtractSize(_parentExtractSize), parentExtract(_parentExtract) { }
  780. void Do(unsigned i)
  781. {
  782. activities.item(i).execute(parentExtractSize, parentExtract);
  783. }
  784. private:
  785. IRoxieServerActivityCopyArray & activities;
  786. };
  787. class CRoxieServerActivity : public CInterface, implements IRoxieServerActivity, implements IRoxieInput, implements IRoxieContextLogger
  788. {
  789. protected:
  790. IRoxieInput *input;
  791. IHThorArg &basehelper;
  792. IRoxieSlaveContext *ctx;
  793. const IRoxieServerActivityFactory *factory;
  794. IRoxieServerActivityCopyArray dependencies;
  795. IntArray dependencyControlIds;
  796. IArrayOf<IActivityGraph> childGraphs;
  797. CachedOutputMetaData meta;
  798. IHThorArg *colocalParent;
  799. IEngineRowAllocator *rowAllocator;
  800. CriticalSection statecrit;
  801. mutable StatsCollector stats;
  802. unsigned processed;
  803. unsigned __int64 totalCycles;
  804. unsigned activityId;
  805. activityState state;
  806. bool createPending;
  807. bool debugging;
  808. bool timeActivities;
  809. public:
  810. IMPLEMENT_IINTERFACE;
  811. CRoxieServerActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  812. : factory(_factory),
  813. basehelper(_factory->getHelper()),
  814. activityId(_factory->queryId())
  815. {
  816. input = NULL;
  817. ctx = NULL;
  818. meta.set(basehelper.queryOutputMeta());
  819. processed = 0;
  820. totalCycles = 0;
  821. if (factory)
  822. factory->createChildQueries(childGraphs, this, _probeManager, *this);
  823. state=STATEreset;
  824. rowAllocator = NULL;
  825. debugging = _probeManager != NULL; // Don't want to collect timing stats from debug sessions
  826. colocalParent = NULL;
  827. createPending = true;
  828. timeActivities = defaultTimeActivities;
  829. }
  830. CRoxieServerActivity(IHThorArg & _helper) : factory(NULL), basehelper(_helper)
  831. {
  832. activityId = 0;
  833. input = NULL;
  834. ctx = NULL;
  835. meta.set(basehelper.queryOutputMeta());
  836. processed = 0;
  837. totalCycles = 0;
  838. state=STATEreset;
  839. rowAllocator = NULL;
  840. debugging = false;
  841. colocalParent = NULL;
  842. createPending = true;
  843. timeActivities = defaultTimeActivities;
  844. }
  845. inline ~CRoxieServerActivity()
  846. {
  847. CriticalBlock cb(statecrit);
  848. if (traceStartStop)
  849. DBGLOG("%p destroy state=%s", this, queryStateText(state)); // Note- CTXLOG may not be safe
  850. if (state!=STATEreset)
  851. {
  852. DBGLOG("STATE: Activity %d destroyed but not reset", activityId);
  853. state = STATEreset; // bit pointless but there you go...
  854. }
  855. basehelper.Release();
  856. ::Release(rowAllocator);
  857. }
  858. virtual const IRoxieContextLogger &queryLogCtx()const
  859. {
  860. return *this;
  861. }
  862. inline void createRowAllocator()
  863. {
  864. if (!rowAllocator)
  865. rowAllocator = ctx->queryCodeContext()->getRowAllocator(meta.queryOriginal(), activityId);
  866. }
  867. // MORE - most of this is copied from ccd.hpp - can't we refactor?
  868. virtual void CTXLOG(const char *format, ...) const
  869. {
  870. va_list args;
  871. va_start(args, format);
  872. CTXLOGva(format, args);
  873. va_end(args);
  874. }
  875. virtual void CTXLOGva(const char *format, va_list args) const
  876. {
  877. StringBuffer text, prefix;
  878. getLogPrefix(prefix);
  879. text.valist_appendf(format, args);
  880. CTXLOGa(LOG_TRACING, prefix.str(), text.str());
  881. }
  882. virtual void CTXLOGa(TracingCategory category, const char *prefix, const char *text) const
  883. {
  884. if (ctx)
  885. ctx->CTXLOGa(category, prefix, text);
  886. else
  887. DBGLOG("[%s] %s", prefix, text);
  888. }
  889. virtual void logOperatorException(IException *E, const char *file, unsigned line, const char *format, ...) const
  890. {
  891. va_list args;
  892. va_start(args, format);
  893. StringBuffer prefix;
  894. getLogPrefix(prefix);
  895. CTXLOGaeva(E, file, line, prefix.str(), format, args);
  896. va_end(args);
  897. }
  898. virtual void logOperatorExceptionVA(IException *E, const char *file, unsigned line, const char *format, va_list args) const
  899. {
  900. StringBuffer prefix;
  901. getLogPrefix(prefix);
  902. CTXLOGaeva(E, file, line, prefix.str(), format, args);
  903. }
  904. virtual void CTXLOGae(IException *E, const char *file, unsigned line, const char *prefix, const char *format, ...) const
  905. {
  906. va_list args;
  907. va_start(args, format);
  908. CTXLOGaeva(E, file, line, prefix, format, args);
  909. va_end(args);
  910. }
  911. virtual void CTXLOGaeva(IException *E, const char *file, unsigned line, const char *prefix, const char *format, va_list args) const
  912. {
  913. if (ctx)
  914. ctx->CTXLOGaeva(E, file, line, prefix, format, args);
  915. else
  916. {
  917. StringBuffer ss;
  918. ss.appendf("[%s] ERROR", prefix);
  919. if (E)
  920. ss.append(": ").append(E->errorCode());
  921. if (file)
  922. ss.appendf(": %s(%d) ", file, line);
  923. if (E)
  924. E->errorMessage(ss.append(": "));
  925. if (format)
  926. {
  927. ss.append(": ").valist_appendf(format, args);
  928. }
  929. LOG(MCoperatorProgress, unknownJob, "%s", ss.str());
  930. }
  931. }
  932. virtual void CTXLOGl(LogItem *log) const
  933. {
  934. if (ctx)
  935. ctx->CTXLOGl(log);
  936. else
  937. {
  938. assert(ctx);
  939. log->Release(); // Should never happen
  940. }
  941. }
  942. virtual void noteStatistic(unsigned statCode, unsigned __int64 value, unsigned count) const
  943. {
  944. if (factory)
  945. factory->noteStatistic(statCode, value, count);
  946. if (ctx)
  947. ctx->noteStatistic(statCode, value, count);
  948. stats.noteStatistic(statCode, value, count);
  949. }
  950. virtual StringBuffer &getLogPrefix(StringBuffer &ret) const
  951. {
  952. if (ctx)
  953. ctx->getLogPrefix(ret);
  954. return ret.append('@').append(activityId);
  955. }
  956. virtual bool isIntercepted() const
  957. {
  958. return ctx ? ctx->isIntercepted() : false;
  959. }
  960. virtual bool isBlind() const
  961. {
  962. return ctx ? ctx->isBlind() : blindLogging;
  963. }
  964. virtual unsigned queryTraceLevel() const
  965. {
  966. if (ctx)
  967. return ctx->queryTraceLevel();
  968. else
  969. return traceLevel;
  970. }
  971. virtual bool isPassThrough()
  972. {
  973. return false;
  974. }
  975. virtual const IResolvedFile *resolveLFN(const char *filename, bool isOpt)
  976. {
  977. return ctx->resolveLFN(filename, isOpt);
  978. }
  979. virtual const IResolvedFile *queryVarFileInfo() const
  980. {
  981. throwUnexpected(); // should be implemented in more derived class by anyone that has a remote adaptor
  982. return NULL;
  983. }
  984. virtual void serializeSkipInfo(MemoryBuffer &out, unsigned seekLen, const void *rawSeek, unsigned numFields, const void * seek, const SmartStepExtra &stepExtra) const
  985. {
  986. throwUnexpected(); // should be implemented in more derived class wherever needed
  987. }
  988. virtual IRoxieSlaveContext *queryContext()
  989. {
  990. return ctx;
  991. }
  992. virtual IRoxieServerActivity *queryActivity() { return this; }
  993. virtual IIndexReadActivityInfo *queryIndexReadActivity() { return NULL; }
  994. virtual bool needsAllocator() const { return false; }
  995. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  996. {
  997. ctx = _ctx;
  998. colocalParent = _colocalParent;
  999. createPending = true;
  1000. if (needsAllocator())
  1001. createRowAllocator();
  1002. processed = 0;
  1003. totalCycles = 0;
  1004. if (factory)
  1005. factory->onCreateChildQueries(_ctx, &basehelper, childGraphs);
  1006. if (ctx)
  1007. timeActivities = ctx->queryTimeActivities();
  1008. }
  1009. virtual void serializeCreateStartContext(MemoryBuffer &out)
  1010. {
  1011. //This should only be called after onStart has been called on the helper
  1012. assertex(!createPending);
  1013. assertex(state==STATEstarted);
  1014. unsigned startlen = out.length();
  1015. basehelper.serializeCreateContext(out);
  1016. basehelper.serializeStartContext(out);
  1017. if (queryTraceLevel() > 10)
  1018. CTXLOG("serializeCreateStartContext for %d added %d bytes", activityId, out.length()-startlen);
  1019. }
  1020. virtual void serializeExtra(MemoryBuffer &out) {}
  1021. inline void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  1022. {
  1023. CriticalBlock cb(statecrit);
  1024. if (state != STATEreset && state != STATEstarting)
  1025. {
  1026. CTXLOG("STATE: Expected state to be reset, but was %s, in activity %d", queryStateText(state), activityId);
  1027. }
  1028. state=STATEstarted;
  1029. #ifdef TRACE_STARTSTOP
  1030. if (traceStartStop)
  1031. {
  1032. CTXLOG("start %d", activityId);
  1033. if (watchActivityId && watchActivityId==activityId)
  1034. {
  1035. CTXLOG("WATCH: start %d", activityId);
  1036. }
  1037. }
  1038. #endif
  1039. executeDependencies(parentExtractSize, parentExtract, 0);
  1040. if (input)
  1041. input->start(parentExtractSize, parentExtract, paused);
  1042. ensureCreated();
  1043. basehelper.onStart(parentExtract, NULL);
  1044. if (factory)
  1045. factory->noteStarted();
  1046. }
  1047. void executeDependencies(unsigned parentExtractSize, const byte *parentExtract, unsigned controlId)
  1048. {
  1049. //MORE: Create a filtered list and then use asyncfor
  1050. ForEachItemIn(idx, dependencies)
  1051. {
  1052. if (dependencyControlIds.item(idx) == controlId)
  1053. dependencies.item(idx).execute(parentExtractSize, parentExtract);
  1054. }
  1055. }
  1056. void stopDependencies(unsigned parentExtractSize, const byte *parentExtract, unsigned controlId)
  1057. {
  1058. ForEachItemIn(idx, dependencies)
  1059. {
  1060. if (dependencyControlIds.item(idx) == controlId)
  1061. dependencies.item(idx).stop(false);
  1062. }
  1063. }
  1064. virtual unsigned __int64 queryTotalCycles() const
  1065. {
  1066. return totalCycles;
  1067. }
  1068. virtual unsigned __int64 queryLocalCycles() const
  1069. {
  1070. __int64 ret = totalCycles;
  1071. if (input) ret -= input->queryTotalCycles();
  1072. if (ret < 0)
  1073. ret = 0;
  1074. return ret;
  1075. }
  1076. virtual IRoxieInput *queryInput(unsigned idx) const
  1077. {
  1078. if (idx==0)
  1079. return input;
  1080. else
  1081. return NULL;
  1082. }
  1083. void noteProcessed(unsigned _idx, unsigned _processed, unsigned __int64 _totalCycles, unsigned __int64 _localCycles) const
  1084. {
  1085. if (factory)
  1086. {
  1087. if (!debugging)
  1088. factory->noteProcessed(_idx, _processed, _totalCycles, _localCycles);
  1089. if (ctx)
  1090. ctx->noteProcessed(*this, this, _idx, _processed, _totalCycles, _localCycles);
  1091. }
  1092. }
  1093. inline void ensureCreated()
  1094. {
  1095. if (createPending)
  1096. {
  1097. createPending = false;
  1098. basehelper.onCreate(ctx->queryCodeContext(), colocalParent, NULL);
  1099. }
  1100. }
  1101. inline void stop(bool aborting)
  1102. {
  1103. if (state != STATEstopped)
  1104. {
  1105. CriticalBlock cb(statecrit);
  1106. if (state != STATEstopped)
  1107. {
  1108. state=STATEstopped;
  1109. #ifdef TRACE_STARTSTOP
  1110. if (traceStartStop)
  1111. {
  1112. CTXLOG("stop %d", activityId);
  1113. if (watchActivityId && watchActivityId==activityId)
  1114. {
  1115. CTXLOG("WATCH: stop %d", activityId);
  1116. }
  1117. }
  1118. #endif
  1119. if (input)
  1120. input->stop(aborting);
  1121. }
  1122. }
  1123. }
  1124. inline void reset()
  1125. {
  1126. if (state != STATEreset)
  1127. {
  1128. CriticalBlock cb(statecrit);
  1129. if (state != STATEreset)
  1130. {
  1131. if (state==STATEstarted || state==STATEstarting)
  1132. {
  1133. CTXLOG("STATE: activity %d reset without stop", activityId);
  1134. stop(false);
  1135. }
  1136. if (ctx->queryTraceActivityTimes())
  1137. {
  1138. stats.dumpStats(*this);
  1139. StringBuffer prefix, text;
  1140. getLogPrefix(prefix);
  1141. text.appendf("records processed - %d", processed);
  1142. CTXLOGa(LOG_STATISTICS, prefix.str(), text.str());
  1143. text.clear().appendf("total time - %d us", (unsigned) (cycle_to_nanosec(totalCycles)/1000));
  1144. CTXLOGa(LOG_STATISTICS, prefix.str(), text.str());
  1145. text.clear().appendf("local time - %d us", (unsigned) (cycle_to_nanosec(queryLocalCycles())/1000));
  1146. CTXLOGa(LOG_STATISTICS, prefix.str(), text.str());
  1147. }
  1148. state = STATEreset;
  1149. #ifdef TRACE_STARTSTOP
  1150. if (traceStartStop)
  1151. {
  1152. CTXLOG("reset %d", activityId);
  1153. if (watchActivityId && watchActivityId==activityId)
  1154. {
  1155. CTXLOG("WATCH: reset %d", activityId);
  1156. }
  1157. }
  1158. #endif
  1159. ForEachItemIn(idx, dependencies)
  1160. dependencies.item(idx).reset();
  1161. noteProcessed(0, processed, totalCycles, queryLocalCycles());
  1162. if (input)
  1163. input->reset();
  1164. processed = 0;
  1165. totalCycles = 0;
  1166. }
  1167. }
  1168. }
  1169. virtual void addDependency(IRoxieServerActivity &source, unsigned sourceIdx, int controlId)
  1170. {
  1171. dependencies.append(source);
  1172. dependencyControlIds.append(controlId);
  1173. }
  1174. virtual void resetEOF()
  1175. {
  1176. //would make more sense if the default implementation (and eof member) were in the base class
  1177. }
  1178. // Sink activities should override this....
  1179. virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
  1180. {
  1181. throw MakeStringException(ROXIE_SINK, "Internal error: execute() requires a sink");
  1182. }
  1183. virtual void executeChild(size32_t & retSize, void * & ret, unsigned parentExtractSize, const byte * parentExtract)
  1184. {
  1185. throw MakeStringException(ROXIE_SINK, "Internal error: executeChild() requires a suitable sink");
  1186. }
  1187. virtual __int64 evaluate()
  1188. {
  1189. throw MakeStringException(ROXIE_SINK, "Internal error: evaluate() requires a function");
  1190. }
  1191. virtual IRoxieInput * querySelectOutput(unsigned id)
  1192. {
  1193. return NULL;
  1194. }
  1195. virtual bool querySetStreamInput(unsigned id, IRoxieInput * _input)
  1196. {
  1197. return false;
  1198. }
  1199. virtual void setInput(unsigned idx, IRoxieInput *_in)
  1200. {
  1201. assertex(!idx);
  1202. input = _in;
  1203. }
  1204. virtual IRoxieInput *queryOutput(unsigned idx)
  1205. {
  1206. if (idx == (unsigned) -1)
  1207. idx = 0;
  1208. return idx ? NULL : this;
  1209. }
  1210. virtual IOutputMetaData *queryOutputMeta() const
  1211. {
  1212. return meta.queryOriginal();
  1213. }
  1214. virtual unsigned queryId() const
  1215. {
  1216. return activityId;
  1217. }
  1218. virtual unsigned querySubgraphId() const
  1219. {
  1220. return factory->querySubgraphId();
  1221. }
  1222. virtual void checkAbort()
  1223. {
  1224. ctx->checkAbort();
  1225. }
  1226. IException *makeWrappedException(IException *e)
  1227. {
  1228. StringBuffer msg;
  1229. ThorActivityKind activityKind = factory ? factory->getKind() : TAKnone;
  1230. CTXLOG("makeWrappedException - %s (in %s %d)", e->errorMessage(msg).str(), getActivityText(activityKind), activityId);
  1231. if (QUERYINTERFACE(e, CWrappedException) || QUERYINTERFACE(e, IUserException))
  1232. return e;
  1233. else
  1234. return new CWrappedException(e, activityKind, activityId);
  1235. }
  1236. virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract)
  1237. {
  1238. }
  1239. virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract, IProbeManager *probeManager, IArrayOf<IRoxieInput> &probes)
  1240. {
  1241. }
  1242. virtual void resetOutputsUsed()
  1243. {
  1244. }
  1245. virtual void noteOutputUsed()
  1246. {
  1247. }
  1248. virtual IRoxieServerSideCache *queryServerSideCache() const
  1249. {
  1250. return factory->queryServerSideCache();
  1251. }
  1252. virtual const IRoxieServerActivityFactory *queryFactory() const
  1253. {
  1254. return factory;
  1255. }
  1256. inline ThorActivityKind getKind() const
  1257. {
  1258. return factory->getKind();
  1259. }
  1260. inline bool isSink() const
  1261. {
  1262. return (factory != NULL) && factory->isSink();
  1263. }
  1264. };
  1265. //=====================================================================================================
  1266. class CRoxieServerLateStartActivity : public CRoxieServerActivity
  1267. {
  1268. protected:
  1269. IRoxieInput *input; // Don't use base class input field as we want to delay starts
  1270. bool prefiltered;
  1271. bool eof;
  1272. void lateStart(unsigned parentExtractSize, const byte *parentExtract, bool any)
  1273. {
  1274. prefiltered = !any;
  1275. eof = prefiltered;
  1276. if (!prefiltered)
  1277. input->start(parentExtractSize, parentExtract, false);
  1278. else
  1279. {
  1280. if (traceStartStop)
  1281. CTXLOG("lateStart activity stopping input early as prefiltered");
  1282. input->stop(false);
  1283. }
  1284. }
  1285. public:
  1286. CRoxieServerLateStartActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  1287. : CRoxieServerActivity(_factory, _probeManager)
  1288. {
  1289. input = NULL;
  1290. prefiltered = false;
  1291. eof = false;
  1292. }
  1293. virtual void stop(bool aborting)
  1294. {
  1295. if (!prefiltered)
  1296. {
  1297. input->stop(aborting);
  1298. }
  1299. else if (traceStartStop)
  1300. CTXLOG("lateStart activity NOT stopping input late as prefiltered");
  1301. CRoxieServerActivity::stop(aborting);
  1302. }
  1303. virtual unsigned __int64 queryLocalCycles() const
  1304. {
  1305. __int64 localCycles = totalCycles - input->queryTotalCycles();
  1306. if (localCycles < 0)
  1307. localCycles = 0;
  1308. return localCycles;
  1309. }
  1310. virtual IRoxieInput *queryInput(unsigned idx) const
  1311. {
  1312. if (idx==0)
  1313. return input;
  1314. else
  1315. return NULL;
  1316. }
  1317. virtual void reset()
  1318. {
  1319. CRoxieServerActivity::reset();
  1320. input->reset();
  1321. prefiltered = false;
  1322. }
  1323. virtual void setInput(unsigned idx, IRoxieInput *_in)
  1324. {
  1325. assertex(!idx);
  1326. input = _in;
  1327. }
  1328. };
  1329. //=====================================================================================================
  1330. atomic_t nextInstanceId;
  1331. extern unsigned getNextInstanceId()
  1332. {
  1333. return atomic_add_exchange(&nextInstanceId, 1)+1;
  1334. }
  1335. atomic_t nextRuid;
  1336. ruid_t getNextRuid()
  1337. {
  1338. ruid_t ret = atomic_add_exchange(&nextRuid, 1)+1;
  1339. while (ret < RUID_FIRST)
  1340. ret = atomic_add_exchange(&nextRuid, 1)+1; // ruids 0 and 1 are reserved for pings/unwanted discarder.
  1341. return ret;
  1342. }
  1343. void setStartRuid(unsigned restarts)
  1344. {
  1345. atomic_set(&nextRuid, restarts * 0x10000);
  1346. atomic_set(&nextInstanceId, restarts * 10000);
  1347. }
  1348. enum { LimitSkipErrorCode = 0, KeyedLimitSkipErrorCode = 1 };
  1349. class LimitSkipException : public CInterface, public IException
  1350. {
  1351. int code;
  1352. public:
  1353. LimitSkipException(int _code) { code = _code; }
  1354. IMPLEMENT_IINTERFACE;
  1355. virtual int errorCode() const { return code; }
  1356. virtual StringBuffer & errorMessage(StringBuffer &msg) const { return msg.append("LimitSkipException"); }
  1357. virtual MessageAudience errorAudience() const { return MSGAUD_internal; }
  1358. };
  1359. IException *makeLimitSkipException(bool isKeyed)
  1360. {
  1361. // We need to make sure what we throw is IException not something derived from it....
  1362. return new LimitSkipException(isKeyed ? KeyedLimitSkipErrorCode : LimitSkipErrorCode);
  1363. }
  1364. //=================================================================================
  1365. interface IRecordPullerCallback : extends IExceptionHandler
  1366. {
  1367. virtual void processRow(const void *row) = 0;
  1368. virtual void processEOG() = 0;
  1369. virtual void processGroup(const ConstPointerArray &rows) = 0;
  1370. virtual void processDone() = 0;
  1371. };
  1372. class RecordPullerThread : public RestartableThread
  1373. {
  1374. protected:
  1375. IRoxieInput *input;
  1376. IRecordPullerCallback *helper;
  1377. Semaphore started; // MORE: GH->RKC I'm pretty sure this can be deleted, since handled by RestartableThread
  1378. bool groupAtOnce, eof, eog;
  1379. CriticalSection crit;
  1380. public:
  1381. RecordPullerThread(bool _groupAtOnce)
  1382. : RestartableThread("RecordPullerThread"), groupAtOnce(_groupAtOnce)
  1383. {
  1384. input = NULL;
  1385. helper = NULL;
  1386. eof = eog = FALSE;
  1387. }
  1388. inline unsigned __int64 queryTotalCycles() const
  1389. {
  1390. return input->queryTotalCycles();
  1391. }
  1392. void setInput(IRecordPullerCallback *_helper, IRoxieInput *_input)
  1393. {
  1394. helper = _helper;
  1395. input = _input;
  1396. }
  1397. IRoxieInput *queryInput() const
  1398. {
  1399. return input;
  1400. }
  1401. void start(unsigned parentExtractSize, const byte *parentExtract, bool paused, unsigned preload, bool noThread, IRoxieSlaveContext *ctx)
  1402. {
  1403. eof = false;
  1404. eog = false;
  1405. input->start(parentExtractSize, parentExtract, paused);
  1406. try
  1407. {
  1408. if (preload && !paused)
  1409. {
  1410. if (traceLevel > 4)
  1411. DBGLOG("Preload fetching first %d records", preload);
  1412. if (groupAtOnce)
  1413. pullGroups(preload);
  1414. else
  1415. pullRecords(preload);
  1416. }
  1417. if (eof)
  1418. {
  1419. if (traceLevel > 4)
  1420. DBGLOG("No need to start puller after preload");
  1421. helper->processDone();
  1422. }
  1423. else
  1424. {
  1425. if (!noThread)
  1426. {
  1427. StringBuffer logPrefix("[");
  1428. if (ctx) ctx->getLogPrefix(logPrefix);
  1429. logPrefix.append("] ");
  1430. RestartableThread::start(logPrefix);
  1431. started.wait();
  1432. }
  1433. }
  1434. }
  1435. catch (IException *e)
  1436. {
  1437. helper->fireException(e);
  1438. }
  1439. catch (...)
  1440. {
  1441. helper->fireException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unexpected exception caught in RecordPullerThread::start"));
  1442. }
  1443. }
  1444. void stop(bool aborting)
  1445. {
  1446. if (traceStartStop)
  1447. DBGLOG("RecordPullerThread::stop");
  1448. {
  1449. CriticalBlock c(crit); // stop is called on our consumer's thread. We need to take care calling stop for our input to make sure it is not in mid-nextInGroup etc etc.
  1450. input->stop(aborting);
  1451. }
  1452. RestartableThread::join();
  1453. }
  1454. void reset()
  1455. {
  1456. input->reset();
  1457. }
  1458. virtual int run()
  1459. {
  1460. started.signal();
  1461. try
  1462. {
  1463. if (groupAtOnce)
  1464. pullGroups((unsigned) -1);
  1465. else
  1466. pullRecords((unsigned) -1);
  1467. helper->processDone();
  1468. }
  1469. catch (IException *e)
  1470. {
  1471. helper->fireException(e);
  1472. }
  1473. catch (...)
  1474. {
  1475. helper->fireException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unexpected exception caught in RecordPullerThread::run"));
  1476. }
  1477. return 0;
  1478. }
  1479. void done()
  1480. {
  1481. helper->processDone();
  1482. }
  1483. bool pullRecords(unsigned preload)
  1484. {
  1485. if (eof)
  1486. return false;
  1487. while (preload)
  1488. {
  1489. const void * row;
  1490. {
  1491. CriticalBlock c(crit); // See comments in stop for why this is needed
  1492. row = input->nextInGroup();
  1493. }
  1494. if (row)
  1495. {
  1496. eog = false;
  1497. helper->processRow(row);
  1498. }
  1499. else if (!eog)
  1500. {
  1501. helper->processEOG();
  1502. eog = true;
  1503. }
  1504. else
  1505. {
  1506. eof = true;
  1507. return false;
  1508. }
  1509. if (preload != (unsigned) -1)
  1510. preload--;
  1511. }
  1512. return true;
  1513. }
  1514. void pullGroups(unsigned preload)
  1515. {
  1516. ConstPointerArray thisGroup;
  1517. unsigned rowsDone = 0;
  1518. while (preload && !eof)
  1519. {
  1520. const void *row;
  1521. {
  1522. CriticalBlock c(crit);
  1523. row = input->nextInGroup();
  1524. }
  1525. if (row)
  1526. {
  1527. thisGroup.append(row);
  1528. rowsDone++;
  1529. }
  1530. else if (thisGroup.length())
  1531. {
  1532. helper->processGroup(thisGroup);
  1533. thisGroup.kill();
  1534. if (preload != (unsigned) -1)
  1535. {
  1536. if (preload > rowsDone)
  1537. preload -= rowsDone;
  1538. else
  1539. break;
  1540. }
  1541. rowsDone = 0;
  1542. }
  1543. else
  1544. {
  1545. eof = true;
  1546. break;
  1547. }
  1548. }
  1549. }
  1550. };
  1551. //=================================================================================
  1552. #define READAHEAD_SIZE 1000
  1553. // MORE - this code copied from ThreadedConcat code - may be able to common up some.
  1554. class CRoxieServerReadAheadInput : public CInterface, implements IRoxieInput, implements IRecordPullerCallback
  1555. {
  1556. QueueOf<const void, true> buffer;
  1557. InterruptableSemaphore ready;
  1558. InterruptableSemaphore space;
  1559. CriticalSection crit;
  1560. bool eof;
  1561. bool disabled;
  1562. RecordPullerThread puller;
  1563. unsigned preload;
  1564. unsigned __int64 totalCycles;
  1565. IRoxieSlaveContext *ctx;
  1566. bool timeActivities;
  1567. public:
  1568. IMPLEMENT_IINTERFACE;
  1569. CRoxieServerReadAheadInput(unsigned _preload) : puller(true), preload(_preload)
  1570. {
  1571. eof = false;
  1572. disabled = false;
  1573. totalCycles = 0;
  1574. ctx = NULL;
  1575. timeActivities = defaultTimeActivities;
  1576. }
  1577. void onCreate(IRoxieSlaveContext *_ctx)
  1578. {
  1579. ctx = _ctx;
  1580. disabled = (ctx->queryDebugContext() != NULL);
  1581. if (ctx)
  1582. timeActivities = ctx->queryTimeActivities();
  1583. }
  1584. virtual IRoxieServerActivity *queryActivity()
  1585. {
  1586. return puller.queryInput()->queryActivity();
  1587. }
  1588. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  1589. {
  1590. return puller.queryInput()->queryIndexReadActivity();
  1591. }
  1592. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  1593. {
  1594. eof = false;
  1595. totalCycles = 0;
  1596. if (disabled)
  1597. puller.queryInput()->start(parentExtractSize, parentExtract, paused);
  1598. else
  1599. {
  1600. space.reinit(READAHEAD_SIZE);
  1601. ready.reinit();
  1602. puller.start(parentExtractSize, parentExtract, paused, preload, false, ctx);
  1603. }
  1604. }
  1605. virtual void stop(bool aborting)
  1606. {
  1607. if (disabled)
  1608. puller.queryInput()->stop(aborting);
  1609. else
  1610. {
  1611. space.interrupt();
  1612. ready.interrupt();
  1613. puller.stop(aborting);
  1614. }
  1615. }
  1616. virtual void reset()
  1617. {
  1618. if (disabled)
  1619. puller.queryInput()->reset();
  1620. else
  1621. {
  1622. puller.reset();
  1623. ForEachItemIn(idx1, buffer)
  1624. ReleaseRoxieRow(buffer.item(idx1));
  1625. buffer.clear();
  1626. }
  1627. }
  1628. virtual void resetEOF()
  1629. {
  1630. throwUnexpected();
  1631. }
  1632. virtual IOutputMetaData * queryOutputMeta() const
  1633. {
  1634. return puller.queryInput()->queryOutputMeta();
  1635. }
  1636. virtual void checkAbort()
  1637. {
  1638. puller.queryInput()->checkAbort();
  1639. }
  1640. void setInput(unsigned idx, IRoxieInput *_in)
  1641. {
  1642. assertex(!idx);
  1643. puller.setInput(this, _in);
  1644. }
  1645. virtual unsigned __int64 queryTotalCycles() const
  1646. {
  1647. return totalCycles;
  1648. }
  1649. virtual unsigned __int64 queryLocalCycles() const
  1650. {
  1651. __int64 ret = totalCycles - puller.queryInput()->queryTotalCycles();
  1652. if (ret < 0) ret = 0;
  1653. return ret;
  1654. }
  1655. virtual IRoxieInput *queryInput(unsigned idx) const
  1656. {
  1657. return puller.queryInput()->queryInput(idx);
  1658. }
  1659. virtual const void * nextInGroup()
  1660. {
  1661. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  1662. if (disabled)
  1663. return puller.queryInput()->nextInGroup();
  1664. else
  1665. {
  1666. loop
  1667. {
  1668. {
  1669. CriticalBlock b(crit);
  1670. if (eof && !buffer.ordinality())
  1671. return NULL; // eof
  1672. }
  1673. ready.wait();
  1674. const void *ret;
  1675. {
  1676. CriticalBlock b(crit);
  1677. ret = buffer.dequeue();
  1678. }
  1679. space.signal();
  1680. return ret;
  1681. }
  1682. }
  1683. }
  1684. virtual unsigned queryId() const { throwUnexpected(); }
  1685. virtual bool fireException(IException *e)
  1686. {
  1687. // called from puller thread on failure
  1688. ready.interrupt(LINK(e));
  1689. space.interrupt(e);
  1690. return true;
  1691. }
  1692. virtual void processRow(const void *row)
  1693. {
  1694. {
  1695. CriticalBlock b(crit);
  1696. buffer.enqueue(row);
  1697. }
  1698. ready.signal();
  1699. space.wait();
  1700. }
  1701. virtual void processGroup(const ConstPointerArray &rows)
  1702. {
  1703. // NOTE - a bit bizarre in that it waits for the space AFTER using it.
  1704. // But the space semaphore is only there to stop infinite readahead. And otherwise it would deadlock
  1705. // if group was larger than max(space)
  1706. {
  1707. CriticalBlock b(crit);
  1708. ForEachItemIn(idx, rows)
  1709. buffer.enqueue(rows.item(idx));
  1710. buffer.enqueue(NULL);
  1711. }
  1712. for (unsigned i2 = 0; i2 <= rows.length(); i2++) // note - does 1 extra for the null
  1713. {
  1714. ready.signal();
  1715. space.wait();
  1716. }
  1717. }
  1718. virtual void processEOG()
  1719. {
  1720. // Used when output is not grouped - just ignore
  1721. }
  1722. virtual void processDone()
  1723. {
  1724. CriticalBlock b(crit);
  1725. eof = true;
  1726. ready.signal();
  1727. }
  1728. };
  1729. //=================================================================================
  1730. class CRoxieServerTwoInputActivity : public CRoxieServerActivity
  1731. {
  1732. protected:
  1733. IRoxieInput *input1;
  1734. Owned<CRoxieServerReadAheadInput> puller;
  1735. public:
  1736. CRoxieServerTwoInputActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  1737. : CRoxieServerActivity(_factory, _probeManager)
  1738. {
  1739. input1 = NULL;
  1740. }
  1741. ~CRoxieServerTwoInputActivity()
  1742. {
  1743. }
  1744. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  1745. {
  1746. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  1747. input1->start(parentExtractSize, parentExtract, paused);
  1748. }
  1749. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  1750. {
  1751. if (puller)
  1752. puller->onCreate(_ctx);
  1753. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  1754. }
  1755. virtual void stop(bool aborting)
  1756. {
  1757. input1->stop(aborting);
  1758. CRoxieServerActivity::stop(aborting);
  1759. }
  1760. virtual unsigned __int64 queryLocalCycles() const
  1761. {
  1762. __int64 ret;
  1763. __int64 inputCycles = input->queryTotalCycles();
  1764. __int64 input1Cycles = input1->queryTotalCycles();
  1765. if (puller)
  1766. ret = totalCycles - (inputCycles > input1Cycles ? inputCycles : input1Cycles);
  1767. else
  1768. ret = totalCycles - (inputCycles + input1Cycles);
  1769. if (ret < 0)
  1770. ret = 0;
  1771. return ret;
  1772. }
  1773. virtual IRoxieInput *queryInput(unsigned idx) const
  1774. {
  1775. switch (idx)
  1776. {
  1777. case 0:
  1778. return input;
  1779. case 1:
  1780. return input1;
  1781. default:
  1782. return NULL;
  1783. }
  1784. }
  1785. virtual void reset()
  1786. {
  1787. CRoxieServerActivity::reset();
  1788. if (input1)
  1789. input1->reset();
  1790. }
  1791. virtual void setInput(unsigned idx, IRoxieInput *_in)
  1792. {
  1793. switch(idx)
  1794. {
  1795. case 0:
  1796. input = _in;
  1797. break;
  1798. case 1:
  1799. input1 = _in;
  1800. break;
  1801. default:
  1802. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
  1803. }
  1804. }
  1805. };
  1806. //=================================================================================
  1807. class CRoxieServerMultiInputBaseActivity : public CRoxieServerActivity
  1808. {
  1809. protected:
  1810. unsigned numInputs;
  1811. IRoxieInput **inputArray;
  1812. public:
  1813. CRoxieServerMultiInputBaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  1814. : CRoxieServerActivity(_factory, _probeManager), numInputs(_numInputs)
  1815. {
  1816. inputArray = new IRoxieInput*[numInputs];
  1817. for (unsigned i = 0; i < numInputs; i++)
  1818. inputArray[i] = NULL;
  1819. }
  1820. ~CRoxieServerMultiInputBaseActivity()
  1821. {
  1822. delete [] inputArray;
  1823. }
  1824. virtual unsigned __int64 queryLocalCycles() const
  1825. {
  1826. __int64 localCycles = totalCycles;
  1827. for (unsigned i = 0; i < numInputs; i++)
  1828. localCycles -= inputArray[i]->queryTotalCycles();
  1829. if (localCycles < 0)
  1830. localCycles = 0;
  1831. return localCycles;
  1832. }
  1833. virtual IRoxieInput *queryInput(unsigned idx) const
  1834. {
  1835. if (idx < numInputs)
  1836. return inputArray[idx];
  1837. else
  1838. return NULL;
  1839. }
  1840. virtual void reset()
  1841. {
  1842. for (unsigned i = 0; i < numInputs; i++)
  1843. inputArray[i]->reset();
  1844. CRoxieServerActivity::reset();
  1845. }
  1846. virtual void setInput(unsigned idx, IRoxieInput *_in)
  1847. {
  1848. inputArray[idx] = _in;
  1849. }
  1850. };
  1851. //=================================================================================
  1852. class CRoxieServerMultiInputActivity : public CRoxieServerMultiInputBaseActivity
  1853. {
  1854. public:
  1855. CRoxieServerMultiInputActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  1856. : CRoxieServerMultiInputBaseActivity(_factory, _probeManager, _numInputs)
  1857. {
  1858. }
  1859. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  1860. {
  1861. CRoxieServerMultiInputBaseActivity::start(parentExtractSize, parentExtract, paused);
  1862. for (unsigned i = 0; i < numInputs; i++)
  1863. {
  1864. inputArray[i]->start(parentExtractSize, parentExtract, paused);
  1865. }
  1866. }
  1867. virtual void stop(bool aborting)
  1868. {
  1869. for (unsigned i = 0; i < numInputs; i++)
  1870. {
  1871. inputArray[i]->stop(aborting);
  1872. }
  1873. CRoxieServerMultiInputBaseActivity::stop(aborting);
  1874. }
  1875. };
  1876. //=====================================================================================================
  1877. class CRoxieServerInternalSinkActivity : public CRoxieServerActivity
  1878. {
  1879. protected:
  1880. bool executed;
  1881. CriticalSection ecrit;
  1882. Owned<IException> exception;
  1883. public:
  1884. CRoxieServerInternalSinkActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  1885. : CRoxieServerActivity(_factory, _probeManager)
  1886. {
  1887. executed = false;
  1888. }
  1889. virtual void reset()
  1890. {
  1891. executed = false;
  1892. exception.clear();
  1893. CRoxieServerActivity::reset();
  1894. }
  1895. virtual IRoxieInput *queryOutput(unsigned idx)
  1896. {
  1897. return NULL;
  1898. }
  1899. virtual const void *nextInGroup()
  1900. {
  1901. throwUnexpected(); // I am nobody's input
  1902. }
  1903. virtual void onExecute() = 0;
  1904. virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
  1905. {
  1906. CriticalBlock b(ecrit);
  1907. if (exception)
  1908. throw exception.getLink();
  1909. if (!executed)
  1910. {
  1911. try
  1912. {
  1913. start(parentExtractSize, parentExtract, false);
  1914. {
  1915. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext()); // unfortunately this is not really best place for seeing in debugger.
  1916. onExecute();
  1917. }
  1918. stop(false);
  1919. executed = true;
  1920. }
  1921. catch (IException *E)
  1922. {
  1923. exception.set(E); // (or maybe makeWrappedException?)
  1924. stop(true);
  1925. throw;
  1926. }
  1927. catch (...)
  1928. {
  1929. exception.set(MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught at %s:%d", __FILE__, __LINE__));
  1930. stop(true);
  1931. throw;
  1932. }
  1933. }
  1934. }
  1935. };
  1936. //=================================================================================
  1937. class CRoxieServerQueryPacket : public CInterface, implements IRoxieServerQueryPacket
  1938. {
  1939. protected:
  1940. Owned<IMessageResult> result;
  1941. Owned<IRoxieQueryPacket> packet;
  1942. Linked<IRoxieServerQueryPacket> continuation;
  1943. unsigned hash;
  1944. unsigned seq;
  1945. unsigned lastDebugSequence;
  1946. Owned<IRoxieQueryPacket> lastDebugResponse;
  1947. ILRUChain *prev;
  1948. ILRUChain *next;
  1949. bool delayed;
  1950. public:
  1951. IMPLEMENT_IINTERFACE;
  1952. CRoxieServerQueryPacket(IRoxieQueryPacket *p) : packet(p)
  1953. {
  1954. hash = 0;
  1955. seq = 0;
  1956. prev = NULL;
  1957. next = NULL;
  1958. delayed = false;
  1959. lastDebugSequence = 0;
  1960. }
  1961. virtual IRoxieQueryPacket *queryPacket() const
  1962. {
  1963. return packet;
  1964. }
  1965. virtual bool isContinuation() const
  1966. {
  1967. return packet && (packet->queryHeader().continueSequence & ~CONTINUE_SEQUENCE_SKIPTO) != 0;
  1968. }
  1969. virtual bool isDelayed() const
  1970. {
  1971. return delayed;
  1972. }
  1973. virtual bool isEnd() const
  1974. {
  1975. return false;
  1976. }
  1977. virtual bool isLimit(unsigned __int64 &_rowLimit, unsigned __int64 &_keyedLimit, unsigned __int64 &_stopAfter) const
  1978. {
  1979. return false;
  1980. }
  1981. virtual bool hasResult() const
  1982. {
  1983. return result != NULL;
  1984. }
  1985. virtual bool hasContinuation() const
  1986. {
  1987. return continuation != NULL;
  1988. }
  1989. virtual void setDelayed(bool _delayed)
  1990. {
  1991. delayed = _delayed;
  1992. }
  1993. virtual void setPacket(IRoxieQueryPacket *_packet)
  1994. {
  1995. packet.setown(_packet);
  1996. }
  1997. virtual void setSequence(unsigned _seq)
  1998. {
  1999. assertex(!IsShared());
  2000. seq = _seq;
  2001. }
  2002. virtual unsigned getSequence() const
  2003. {
  2004. return seq;
  2005. }
  2006. IMessageResult *getResult()
  2007. {
  2008. return result.getLink();
  2009. }
  2010. IMessageResult *queryResult()
  2011. {
  2012. return result;
  2013. }
  2014. void setResult(IMessageResult *r)
  2015. {
  2016. result.setown(r);
  2017. }
  2018. IRoxieServerQueryPacket *queryContinuation()
  2019. {
  2020. return continuation;
  2021. }
  2022. void setContinuation(IRoxieServerQueryPacket *c)
  2023. {
  2024. continuation.setown(c);
  2025. }
  2026. virtual unsigned queryHash() const
  2027. {
  2028. return hash;
  2029. }
  2030. virtual void setHash(unsigned _hash)
  2031. {
  2032. hash = _hash;
  2033. }
  2034. virtual ILRUChain *queryPrev() const { return prev; }
  2035. virtual ILRUChain *queryNext() const { return next; }
  2036. virtual void setPrev(ILRUChain *p) { prev = p; }
  2037. virtual void setNext(ILRUChain *n) { next = n; }
  2038. virtual void unchain()
  2039. {
  2040. if (prev && next)
  2041. {
  2042. prev->setNext(next);
  2043. next->setPrev(prev);
  2044. }
  2045. next = NULL;
  2046. prev = NULL;
  2047. }
  2048. virtual IRoxieQueryPacket *getDebugResponse(unsigned sequence)
  2049. {
  2050. if (sequence == lastDebugSequence)
  2051. return lastDebugResponse.getLink();
  2052. else if (sequence > lastDebugSequence)
  2053. {
  2054. lastDebugResponse.clear();
  2055. return NULL;
  2056. }
  2057. else
  2058. throwUnexpected();
  2059. }
  2060. virtual void setDebugResponse(unsigned sequence, IRoxieQueryPacket *response)
  2061. {
  2062. lastDebugSequence = sequence;
  2063. lastDebugResponse.set(response);
  2064. }
  2065. };
  2066. class CRoxieServerQueryPacketEndMarker : public CRoxieServerQueryPacket
  2067. {
  2068. public:
  2069. CRoxieServerQueryPacketEndMarker() : CRoxieServerQueryPacket(NULL)
  2070. {
  2071. }
  2072. virtual bool isEnd() const
  2073. {
  2074. return true;
  2075. }
  2076. };
  2077. class CRoxieServerQueryPacketLimitMarker : public CRoxieServerQueryPacket
  2078. {
  2079. unsigned __int64 rowLimit;
  2080. unsigned __int64 keyedLimit;
  2081. unsigned __int64 stopAfter;
  2082. public:
  2083. CRoxieServerQueryPacketLimitMarker(unsigned __int64 _rowLimit, unsigned __int64 _keyedLimit, unsigned __int64 _stopAfter) : CRoxieServerQueryPacket(NULL)
  2084. {
  2085. rowLimit = _rowLimit;
  2086. keyedLimit = _keyedLimit;
  2087. stopAfter = _stopAfter;
  2088. }
  2089. virtual bool isLimit(unsigned __int64 &_rowLimit, unsigned __int64 &_keyedLimit, unsigned __int64 &_stopAfter) const
  2090. {
  2091. _rowLimit = rowLimit;
  2092. _keyedLimit = keyedLimit;
  2093. _stopAfter = stopAfter;
  2094. return true;
  2095. }
  2096. };
  2097. class CRoxieServerSideCache : implements IRoxieServerSideCache, implements ILRUChain
  2098. {
  2099. protected:
  2100. unsigned cacheTableSize;
  2101. unsigned cacheTableSpace;
  2102. IRoxieServerQueryPacket **cacheTable;
  2103. mutable ILRUChain *prev;
  2104. mutable ILRUChain *next;
  2105. mutable CriticalSection crit;
  2106. virtual ILRUChain *queryPrev() const { return prev; }
  2107. virtual ILRUChain *queryNext() const { return next; }
  2108. virtual void setPrev(ILRUChain *p) { prev = p; }
  2109. virtual void setNext(ILRUChain *n) { next = n; }
  2110. virtual void unchain()
  2111. {
  2112. prev->setNext(next);
  2113. next->setPrev(prev);
  2114. next = NULL;
  2115. prev = NULL;
  2116. }
  2117. void moveToHead(IRoxieServerQueryPacket *mru)
  2118. {
  2119. mru->unchain();
  2120. mru->setNext(next);
  2121. next->setPrev(mru);
  2122. mru->setPrev(this);
  2123. next = mru;
  2124. }
  2125. IRoxieServerQueryPacket *removeLRU()
  2126. {
  2127. if (next==this)
  2128. assertex(next != this);
  2129. IRoxieServerQueryPacket *goer = (IRoxieServerQueryPacket *) next;
  2130. goer->unchain(); // NOTE - this will modify the value of next
  2131. return goer;
  2132. }
  2133. void removeEntry(IRoxieServerQueryPacket *goer)
  2134. {
  2135. unsigned v = goer->queryHash() % cacheTableSize;
  2136. loop
  2137. {
  2138. IRoxieServerQueryPacket *found = cacheTable[v];
  2139. assertex(found);
  2140. if (found == goer)
  2141. {
  2142. cacheTable[v] = NULL;
  2143. unsigned vn = v;
  2144. loop
  2145. {
  2146. vn++;
  2147. if (vn==cacheTableSize) vn = 0;
  2148. IRoxieServerQueryPacket *found2 = cacheTable[vn];
  2149. if (!found2)
  2150. break;
  2151. unsigned vm = found2->queryHash() % cacheTableSize;
  2152. if (((vn+cacheTableSize-vm) % cacheTableSize)>=((vn+cacheTableSize-v) % cacheTableSize)) // diff(vn,vm)>=diff(vn,v)
  2153. {
  2154. cacheTable[v] = found2;
  2155. v = vn;
  2156. cacheTable[v] = NULL;
  2157. }
  2158. }
  2159. cacheTableSpace++;
  2160. break;
  2161. }
  2162. v++;
  2163. if (v==cacheTableSize)
  2164. v = 0;
  2165. }
  2166. goer->Release();
  2167. }
  2168. public:
  2169. CRoxieServerSideCache(unsigned _cacheSize)
  2170. {
  2171. cacheTableSize = (_cacheSize*4)/3;
  2172. cacheTable = new IRoxieServerQueryPacket *[cacheTableSize];
  2173. memset(cacheTable, 0, cacheTableSize * sizeof(IRoxieServerQueryPacket *));
  2174. cacheTableSpace = _cacheSize;
  2175. prev = this;
  2176. next = this;
  2177. }
  2178. ~CRoxieServerSideCache()
  2179. {
  2180. for (unsigned i = 0; i < cacheTableSize; i++)
  2181. {
  2182. ::Release(cacheTable[i]);
  2183. }
  2184. delete [] cacheTable;
  2185. }
  2186. virtual IRoxieServerQueryPacket *findCachedResult(const IRoxieContextLogger &logctx, IRoxieQueryPacket *p) const
  2187. {
  2188. unsigned hash = p->hash();
  2189. unsigned et = hash % cacheTableSize;
  2190. if (traceServerSideCache)
  2191. {
  2192. StringBuffer s;
  2193. logctx.CTXLOG("CRoxieServerSideCache::findCachedResult hash %x slot %d %s", hash, et, p->queryHeader().toString(s).str());
  2194. }
  2195. CriticalBlock b(crit);
  2196. loop
  2197. {
  2198. IRoxieServerQueryPacket *found = cacheTable[et];
  2199. if (!found)
  2200. return NULL;
  2201. if (found->queryHash() == hash && found->queryPacket()->cacheMatch(p))
  2202. {
  2203. const_cast<CRoxieServerSideCache *>(this)->moveToHead(found);
  2204. if (traceServerSideCache)
  2205. logctx.CTXLOG("CRoxieServerSideCache::findCachedResult cache hit");
  2206. logctx.noteStatistic(STATS_SERVERCACHEHIT, 1, 1);
  2207. return NULL;
  2208. // Because IMessageResult cannot be replayed, this echeme is flawed. I'm leaving the code here just as a stats gatherer to see how useful it would have been....
  2209. //IRoxieServerQueryPacket *ret = new CRoxieServerQueryPacket(p);
  2210. //ret->setResult(found->getResult());
  2211. //return ret;
  2212. }
  2213. et++;
  2214. if (et == cacheTableSize)
  2215. et = 0;
  2216. }
  2217. }
  2218. virtual void noteCachedResult(IRoxieServerQueryPacket *out, IMessageResult *in)
  2219. {
  2220. if (true) //!in->getLength()) // MORE - separate caches for hits and nohits
  2221. {
  2222. unsigned hash = out->queryPacket()->hash();
  2223. out->setHash(hash);
  2224. unsigned et = hash % cacheTableSize;
  2225. if (traceServerSideCache)
  2226. {
  2227. StringBuffer s;
  2228. DBGLOG("CRoxieServerSideCache::noteCachedResult hash %x slot %d %s", hash, et, out->queryPacket()->queryHeader().toString(s).str());
  2229. }
  2230. CriticalBlock b(crit);
  2231. loop
  2232. {
  2233. IRoxieServerQueryPacket *found = cacheTable[et];
  2234. if (!found)
  2235. {
  2236. if (cacheTableSpace)
  2237. {
  2238. out->setResult(LINK(in));
  2239. cacheTable[et] = LINK(out);
  2240. cacheTableSpace--;
  2241. moveToHead(out);
  2242. break;
  2243. }
  2244. else
  2245. {
  2246. IRoxieServerQueryPacket *goer = removeLRU();
  2247. removeEntry(goer);
  2248. et = hash % cacheTableSize;
  2249. continue;
  2250. }
  2251. }
  2252. else if (found->queryHash()==hash && found->queryPacket()->cacheMatch(out->queryPacket()))
  2253. {
  2254. moveToHead(found);
  2255. return; // already in the cache. Because we don't cache until we have result, this can happen where
  2256. // multiple copies of a slave query are in-flight at once.
  2257. }
  2258. et++;
  2259. if (et == cacheTableSize)
  2260. et = 0;
  2261. }
  2262. }
  2263. // MORE - do we need to worry about the attachment between the MessageUnpacker and the current row manager. May all fall out ok...
  2264. // Can I easily spot a null result? Do I want to cache null results separately? only?
  2265. }
  2266. // Note that this caching mechanism (unlike the old keyed-join specific one) does not common up cases where multiple
  2267. // identical queries are in-flight at the same time. But if we can make it persistant between queries that will
  2268. // more than make up for it
  2269. };
  2270. class CRowArrayMessageUnpackCursor : public CInterface, implements IMessageUnpackCursor
  2271. {
  2272. ConstPointerArray &data;
  2273. Linked<IMessageResult> result;
  2274. public:
  2275. IMPLEMENT_IINTERFACE;
  2276. CRowArrayMessageUnpackCursor(ConstPointerArray &_data, IMessageResult *_result)
  2277. : data(_data), result(_result)
  2278. {
  2279. }
  2280. virtual bool atEOF() const
  2281. {
  2282. return data.length()==0;
  2283. }
  2284. virtual bool isSerialized() const
  2285. {
  2286. return false;
  2287. }
  2288. virtual const void * getNext(int length)
  2289. {
  2290. if (!data.length())
  2291. return NULL;
  2292. const void *ret = data.item(0);
  2293. data.remove(0);
  2294. return ret;
  2295. }
  2296. };
  2297. // MORE - should possibly move more over to the lazy version used in indexread?
  2298. class CRowArrayMessageResult : public CInterface, implements IMessageResult
  2299. {
  2300. ConstPointerArray data;
  2301. IRowManager &rowManager;
  2302. bool variableSize;
  2303. public:
  2304. IMPLEMENT_IINTERFACE;
  2305. CRowArrayMessageResult(IRowManager &_rowManager, bool _variableSize) : rowManager(_rowManager), variableSize(_variableSize)
  2306. {
  2307. }
  2308. ~CRowArrayMessageResult()
  2309. {
  2310. ReleaseRoxieRowSet(data);
  2311. }
  2312. virtual IMessageUnpackCursor *getCursor(IRowManager *rowMgr) const
  2313. {
  2314. CRowArrayMessageResult *_this = (CRowArrayMessageResult *) this;
  2315. return new CRowArrayMessageUnpackCursor(_this->data, _this);
  2316. }
  2317. virtual const void *getMessageHeader(unsigned &length) const
  2318. {
  2319. throwUnexpected(); // should never get called - I don't have a header available
  2320. length = 0;
  2321. return NULL;
  2322. }
  2323. virtual const void *getMessageMetadata(unsigned &length) const
  2324. {
  2325. length = 0;
  2326. return NULL;
  2327. }
  2328. virtual void discard() const
  2329. {
  2330. throwUnexpected();
  2331. }
  2332. void append(const void *row)
  2333. {
  2334. data.append(row);
  2335. }
  2336. };
  2337. void throwRemoteException(IMessageUnpackCursor *extra)
  2338. {
  2339. RecordLengthType *rowlen = (RecordLengthType *) extra->getNext(sizeof(RecordLengthType));
  2340. if (rowlen)
  2341. {
  2342. char *xml = (char *) extra->getNext(*rowlen);
  2343. ReleaseRoxieRow(rowlen);
  2344. Owned<IPropertyTree> p = createPTreeFromXMLString(xml);
  2345. ReleaseRoxieRow(xml);
  2346. unsigned code = p->getPropInt("Code", 0);
  2347. const char *msg = p->queryProp("Message");
  2348. if (!msg)
  2349. msg = xml;
  2350. throw MakeStringException(code, "%s", msg);
  2351. }
  2352. throwUnexpected();
  2353. }
  2354. class CRemoteResultAdaptor :public CInterface, implements IRoxieInput, implements IExceptionHandler
  2355. {
  2356. friend class CRemoteResultMerger;
  2357. class CRemoteResultMerger
  2358. {
  2359. class HeapEntry : public CInterface
  2360. {
  2361. private:
  2362. CRemoteResultAdaptor &adaptor;
  2363. IMessageUnpackCursor *cursor;
  2364. public:
  2365. const void *current;
  2366. bool isLast;
  2367. bool lastIsComplete;
  2368. IRoxieServerQueryPacket *packet;
  2369. unsigned seq;
  2370. public:
  2371. inline const void *noteResult(IMessageUnpackCursor *_cursor, bool _lastIsComplete)
  2372. {
  2373. cursor = _cursor;
  2374. lastIsComplete = _lastIsComplete;
  2375. return next();
  2376. }
  2377. public:
  2378. HeapEntry(CRemoteResultAdaptor &_adaptor, IRoxieServerQueryPacket *_packet, unsigned _seq) : adaptor(_adaptor), packet(_packet), seq(_seq)
  2379. {
  2380. cursor = NULL;
  2381. current = NULL;
  2382. isLast = false;
  2383. lastIsComplete = true;
  2384. }
  2385. ~HeapEntry()
  2386. {
  2387. ::Release(packet);
  2388. ::Release(cursor);
  2389. ReleaseRoxieRow(current);
  2390. }
  2391. bool isCompleteMatch() const
  2392. {
  2393. if (!isLast || lastIsComplete)
  2394. return true;
  2395. else
  2396. return false;
  2397. }
  2398. const void *next()
  2399. {
  2400. if (cursor)
  2401. {
  2402. ReleaseClearRoxieRow(current);
  2403. current = adaptor.getRow(cursor);
  2404. isLast = cursor->atEOF();
  2405. if (!current)
  2406. {
  2407. cursor->Release();
  2408. cursor = NULL;
  2409. }
  2410. }
  2411. return current;
  2412. }
  2413. unsigned skipTo(IRangeCompare *compare, const void *seek, unsigned numFields, bool requireExactMatch)
  2414. {
  2415. // MORE - This loop should possibly be a binchop... though it's not absolutely clear that is true (depends on term frequencies)
  2416. unsigned skipped = 0;
  2417. loop
  2418. {
  2419. int c = compare->docompare(current, seek, numFields);
  2420. //If larger than the seek values, then we may be allowed to return an inexact match,
  2421. //if equal then it is required to be an exact match,
  2422. if (c > 0)
  2423. {
  2424. if (!requireExactMatch || isCompleteMatch())
  2425. break;
  2426. }
  2427. else if ((c == 0) && isCompleteMatch())
  2428. break;
  2429. skipped++;
  2430. if (!next())
  2431. break;
  2432. }
  2433. return skipped;
  2434. }
  2435. };
  2436. CRemoteResultAdaptor &adaptor;
  2437. CIArrayOf<HeapEntry> heapEntries;
  2438. UnsignedArray heap;
  2439. IRowManager *rowManager;
  2440. unsigned numPending;
  2441. unsigned numFields;
  2442. bool endSeen;
  2443. bool remakePending;
  2444. IRangeCompare *compare;
  2445. bool deferredContinuation;
  2446. inline int doCompare(unsigned l, unsigned r)
  2447. {
  2448. int ret = compare->docompare(heapEntries.item(l).current, heapEntries.item(r).current, numFields);
  2449. if (!ret) ret = heapEntries.item(l).seq - heapEntries.item(r).seq;
  2450. return ret;
  2451. }
  2452. void makeHeap()
  2453. {
  2454. /* Permute blocks to establish the heap property
  2455. For each element p, the children are p*2+1 and p*2+2 (provided these are in range)
  2456. The children of p must both be greater than or equal to p
  2457. The parent of a child c is given by p = (c-1)/2
  2458. */
  2459. unsigned i;
  2460. unsigned n = heap.length();
  2461. unsigned *s = heap.getArray();
  2462. for (i=1; i<n; i++)
  2463. {
  2464. unsigned r = s[i];
  2465. int c = i; /* child */
  2466. while (c > 0)
  2467. {
  2468. int p = (c-1)/2; /* parent */
  2469. if ( doCompare( s[c], s[p] ) >= 0 )
  2470. break;
  2471. s[c] = s[p];
  2472. s[p] = r;
  2473. c = p;
  2474. }
  2475. }
  2476. remakePending = false;
  2477. }
  2478. void remakeHeap()
  2479. {
  2480. /* The row associated with block[0] will have changed
  2481. This code restores the heap property
  2482. */
  2483. unsigned p = 0; /* parent */
  2484. unsigned n = heap.length();
  2485. unsigned *s = heap.getArray();
  2486. while (1)
  2487. {
  2488. unsigned c = p*2 + 1; /* child */
  2489. if ( c >= n )
  2490. break;
  2491. /* Select smaller child */
  2492. if ( c+1 < n && doCompare( s[c+1], s[c] ) < 0 ) c += 1;
  2493. /* If child is greater or equal than parent then we are done */
  2494. if ( doCompare( s[c], s[p] ) >= 0 )
  2495. break;
  2496. /* Swap parent and child */
  2497. unsigned r = s[c];
  2498. s[c] = s[p];
  2499. s[p] = r;
  2500. /* child becomes parent */
  2501. p = c;
  2502. }
  2503. remakePending = false;
  2504. }
  2505. void append(IRoxieServerQueryPacket *p, unsigned seq)
  2506. {
  2507. HeapEntry &h = *new HeapEntry(adaptor, LINK(p), seq);
  2508. IMessageResult *result = p->queryResult();
  2509. assertex(result);
  2510. if (h.noteResult(result->getCursor(rowManager), isCompleteMatchFlag(result)))
  2511. {
  2512. heapEntries.append(h);
  2513. heap.append(heap.ordinality());
  2514. }
  2515. else
  2516. h.Release();
  2517. }
  2518. void removeHeap(unsigned idx)
  2519. {
  2520. heapEntries.remove(idx);
  2521. ForEachItemIn(i, heap)
  2522. {
  2523. unsigned v = heap.item(i);
  2524. assertex(v != idx);
  2525. if (v > idx)
  2526. heap.replace(v-1, i);
  2527. }
  2528. }
  2529. bool isCompleteMatchFlag(IMessageResult *result)
  2530. {
  2531. unsigned metaLen;
  2532. const byte *metaInfo = (const byte *) result->getMessageMetadata(metaLen);
  2533. if (metaLen)
  2534. {
  2535. unsigned short continuationLen = *(unsigned short *) metaInfo;
  2536. if (continuationLen >= sizeof(bool))
  2537. {
  2538. metaInfo += sizeof(unsigned short);
  2539. return *(bool *) metaInfo;
  2540. }
  2541. }
  2542. return true; // if no continuation info, last row was complete.
  2543. }
  2544. public:
  2545. CRemoteResultMerger(CRemoteResultAdaptor &_adaptor) : adaptor(_adaptor)
  2546. {
  2547. init(NULL, NULL);
  2548. }
  2549. void init(ISteppingMeta *meta, IRowManager *_rowManager)
  2550. {
  2551. if (meta)
  2552. {
  2553. numFields = meta->getNumFields();
  2554. compare = meta->queryCompare();
  2555. }
  2556. else
  2557. {
  2558. numFields = 0;
  2559. compare = NULL;
  2560. }
  2561. rowManager = _rowManager;
  2562. numPending = 0;
  2563. endSeen = false;
  2564. remakePending = false;
  2565. deferredContinuation = false;
  2566. }
  2567. void reset()
  2568. {
  2569. heapEntries.kill();
  2570. heap.kill();
  2571. numPending = 0;
  2572. endSeen = false;
  2573. remakePending = false;
  2574. deferredContinuation = false;
  2575. }
  2576. inline bool noteEndSeen()
  2577. {
  2578. bool hadSeen = endSeen;
  2579. if (!endSeen)
  2580. makeHeap();
  2581. endSeen = true;
  2582. return !hadSeen;
  2583. }
  2584. void noteResult(IRoxieServerQueryPacket *p, unsigned seq)
  2585. {
  2586. if (!p->isContinuation())
  2587. append(p, seq);
  2588. else
  2589. {
  2590. ForEachItemIn(idx, heapEntries)
  2591. {
  2592. HeapEntry &h = heapEntries.item(idx);
  2593. if (h.packet == p)
  2594. {
  2595. IMessageResult *result = p->queryResult();
  2596. if (!h.noteResult(result->getCursor(rowManager), isCompleteMatchFlag(result)))
  2597. {
  2598. heap.zap(idx);
  2599. removeHeap(idx);
  2600. }
  2601. numPending--;
  2602. if (!numPending)
  2603. makeHeap();
  2604. return;
  2605. }
  2606. }
  2607. }
  2608. // If we get here it must be a continuation for one that I have not yet consumed... we don't need to do anything.
  2609. return;
  2610. }
  2611. unsigned skipRows(unsigned &idx, const void *seek, const void *rawSeek, unsigned numFields, unsigned seekLen, const SmartStepExtra & stepExtra)
  2612. {
  2613. HeapEntry &entry = heapEntries.item(idx);
  2614. unsigned skipped = entry.current ? entry.skipTo(compare, seek, numFields, !stepExtra.returnMismatches()) : 0;
  2615. if (!entry.current)
  2616. {
  2617. IRoxieServerQueryPacket *continuation = entry.packet->queryContinuation();
  2618. if (continuation)
  2619. {
  2620. continuation->Link();
  2621. entry.packet->Release();
  2622. entry.packet = continuation;
  2623. if (continuation->hasResult())
  2624. {
  2625. IMessageResult *result = continuation->queryResult();
  2626. bool lastIsCompleteMatch = isCompleteMatchFlag(result);
  2627. entry.noteResult(result->getCursor(rowManager), lastIsCompleteMatch);
  2628. }
  2629. else
  2630. {
  2631. if (continuation->isDelayed())
  2632. {
  2633. continuation->setDelayed(false);
  2634. MemoryBuffer serializedSkip;
  2635. adaptor.activity.serializeSkipInfo(serializedSkip, seekLen, rawSeek, numFields, seek, stepExtra);
  2636. continuation->setPacket(continuation->queryPacket()->insertSkipData(serializedSkip.length(), serializedSkip.toByteArray()));
  2637. ROQ->sendPacket(continuation->queryPacket(), adaptor.activity.queryLogCtx());
  2638. adaptor.sentsome.signal();
  2639. }
  2640. numPending++;
  2641. }
  2642. }
  2643. else
  2644. {
  2645. heap.zap(idx);
  2646. removeHeap(idx);
  2647. idx--;
  2648. }
  2649. }
  2650. return skipped;
  2651. }
  2652. const void * nextSteppedGE(const void * seek, const void *rawSeek, unsigned numFields, unsigned seeklen, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  2653. {
  2654. // We discard all rows < seekval from all entries in heap
  2655. // If this results in additional slave requests, we return NULL so that we can wait for them
  2656. // If not, we rebuild the heap (if any were skipped) and return the first row
  2657. deferredContinuation = false;
  2658. if (heap.length())
  2659. {
  2660. unsigned skipped = 0;
  2661. unsigned idx = 0;
  2662. while(heapEntries.isItem(idx))
  2663. {
  2664. skipped += skipRows(idx, seek, rawSeek, numFields, seeklen, stepExtra);
  2665. idx++;
  2666. }
  2667. if (numPending)
  2668. return NULL; // can't answer yet, need more results from slaves
  2669. else
  2670. {
  2671. if (skipped)
  2672. makeHeap();
  2673. return next(wasCompleteMatch, stepExtra);
  2674. }
  2675. }
  2676. else
  2677. return NULL;
  2678. }
  2679. bool doContinuation(HeapEntry &topEntry, bool canDefer)
  2680. {
  2681. IRoxieServerQueryPacket *continuation = topEntry.packet->queryContinuation();
  2682. if (continuation)
  2683. {
  2684. if (continuation->isDelayed() && canDefer)
  2685. {
  2686. if (adaptor.activity.queryLogCtx().queryTraceLevel() > 10)
  2687. adaptor.activity.queryLogCtx().CTXLOG("Deferring continuation");
  2688. deferredContinuation = true;
  2689. }
  2690. else
  2691. {
  2692. deferredContinuation = false;
  2693. continuation->Link();
  2694. topEntry.packet->Release();
  2695. topEntry.packet = continuation;
  2696. if (continuation->hasResult())
  2697. {
  2698. IMessageResult *result = continuation->queryResult();
  2699. bool lastIsCompleteMatch = isCompleteMatchFlag(result);
  2700. topEntry.noteResult(result->getCursor(rowManager), lastIsCompleteMatch);
  2701. }
  2702. else
  2703. {
  2704. if (continuation->isDelayed()) // has the continuation been requested yet?
  2705. {
  2706. continuation->Link();
  2707. topEntry.packet->Release();
  2708. topEntry.packet = continuation;
  2709. continuation->setDelayed(false);
  2710. if (adaptor.activity.queryLogCtx().queryTraceLevel() > 10)
  2711. adaptor.activity.queryLogCtx().CTXLOG("About to send continuation, from doContinuation");
  2712. ROQ->sendPacket(continuation->queryPacket(), adaptor.activity.queryLogCtx());
  2713. adaptor.sentsome.signal();
  2714. }
  2715. numPending++; // we are waiting for one that is already in flight
  2716. }
  2717. }
  2718. return true; // next not known yet
  2719. }
  2720. else
  2721. return false;
  2722. }
  2723. const void *next(bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
  2724. {
  2725. OwnedConstRoxieRow ret;
  2726. if (heap.length())
  2727. {
  2728. if (deferredContinuation)
  2729. {
  2730. unsigned top = heap.item(0);
  2731. HeapEntry &topEntry = heapEntries.item(top);
  2732. doContinuation(topEntry, false);
  2733. return NULL;
  2734. }
  2735. if (remakePending)
  2736. remakeHeap();
  2737. unsigned top = heap.item(0);
  2738. HeapEntry &topEntry = heapEntries.item(top);
  2739. ret.set(topEntry.current);
  2740. wasCompleteMatch = topEntry.isCompleteMatch();
  2741. const void *next = topEntry.next();
  2742. if (!next)
  2743. {
  2744. if (!doContinuation(topEntry, stepExtra.returnMismatches()))
  2745. {
  2746. unsigned last = heap.pop();
  2747. if (heap.length())
  2748. heap.replace(last, 0);
  2749. removeHeap(top);
  2750. }
  2751. }
  2752. remakePending = true;
  2753. }
  2754. return ret.getClear();
  2755. }
  2756. bool ready()
  2757. {
  2758. return endSeen && numPending == 0;
  2759. }
  2760. };
  2761. IRoxieServerQueryPacket *createRoxieServerQueryPacket(IRoxieQueryPacket *p, bool &cached)
  2762. {
  2763. if (serverSideCache && !debugContext)
  2764. {
  2765. IRoxieServerQueryPacket *ret = serverSideCache->findCachedResult(activity.queryLogCtx(), p);
  2766. if (ret)
  2767. {
  2768. p->Release();
  2769. cached = true;
  2770. return ret;
  2771. }
  2772. }
  2773. cached = false;
  2774. return new CRoxieServerQueryPacket(p);
  2775. }
  2776. #ifdef _DEBUG
  2777. void dumpPending()
  2778. {
  2779. CriticalBlock b(pendingCrit);
  2780. ForEachItemIn(idx, pending)
  2781. {
  2782. IRoxieServerQueryPacket &p = pending.item(idx);
  2783. StringBuffer s;
  2784. unsigned __int64 dummy;
  2785. if (p.isEnd())
  2786. s.append("END");
  2787. else if (p.isLimit(dummy, dummy, dummy))
  2788. s.append("LIMIT");
  2789. else
  2790. {
  2791. IRoxieQueryPacket *i = p.queryPacket();
  2792. s.appendf("%s", p.hasResult() ? "COMPLETE " : "PENDING ");
  2793. if (i)
  2794. {
  2795. RoxiePacketHeader &header = i->queryHeader();
  2796. header.toString(s);
  2797. }
  2798. }
  2799. DBGLOG("Pending %d %s", idx, s.str());
  2800. }
  2801. }
  2802. #endif
  2803. void abortPending()
  2804. {
  2805. CriticalBlock b(pendingCrit);
  2806. ForEachItemIn(idx, pending)
  2807. {
  2808. IRoxieServerQueryPacket &p = pending.item(idx);
  2809. if (!p.hasResult())
  2810. {
  2811. IRoxieQueryPacket *i = p.queryPacket();
  2812. if (i)
  2813. {
  2814. RoxiePacketHeader &header = i->queryHeader();
  2815. ROQ->sendAbort(header, activity.queryLogCtx());
  2816. }
  2817. }
  2818. }
  2819. pending.kill();
  2820. }
  2821. void checkDelayed()
  2822. {
  2823. if (ctx->queryDebugContext() && ctx->queryDebugContext()->getExecuteSequentially())
  2824. {
  2825. bool allDelayed = true;
  2826. CriticalBlock b(pendingCrit);
  2827. unsigned sendIdx = (unsigned) -1;
  2828. ForEachItemIn(idx, pending)
  2829. {
  2830. IRoxieServerQueryPacket &p = pending.item(idx);
  2831. if (p.queryPacket())
  2832. {
  2833. if (p.isDelayed())
  2834. {
  2835. if (sendIdx == (unsigned) -1)
  2836. sendIdx = idx;
  2837. }
  2838. else if (!p.hasResult())
  2839. {
  2840. allDelayed = false;
  2841. break;
  2842. }
  2843. }
  2844. }
  2845. if (allDelayed && sendIdx != (unsigned) -1)
  2846. {
  2847. if (activity.queryLogCtx().queryTraceLevel() > 10)
  2848. activity.queryLogCtx().CTXLOG("About to send debug-deferred from next");
  2849. pending.item(sendIdx).setDelayed(false);
  2850. ROQ->sendPacket(pending.item(sendIdx).queryPacket(), activity.queryLogCtx());
  2851. sentsome.signal();
  2852. }
  2853. }
  2854. else if (deferredStart)
  2855. {
  2856. CriticalBlock b(pendingCrit);
  2857. ForEachItemIn(idx, pending)
  2858. {
  2859. IRoxieServerQueryPacket &p = pending.item(idx);
  2860. if (p.isDelayed())
  2861. {
  2862. if (activity.queryLogCtx().queryTraceLevel() > 10)
  2863. activity.queryLogCtx().CTXLOG("About to send deferred start from next");
  2864. p.setDelayed(false);
  2865. ROQ->sendPacket(p.queryPacket(), activity.queryLogCtx());
  2866. sentsome.signal();
  2867. }
  2868. }
  2869. deferredStart = false;
  2870. }
  2871. }
  2872. void retryPending()
  2873. {
  2874. CriticalBlock b(pendingCrit);
  2875. checkDelayed();
  2876. ForEachItemIn(idx, pending)
  2877. {
  2878. IRoxieServerQueryPacket &p = pending.item(idx);
  2879. if (!p.hasResult() && !p.isDelayed())
  2880. {
  2881. IRoxieQueryPacket *i = p.queryPacket();
  2882. if (i)
  2883. {
  2884. if (!i->queryHeader().retry())
  2885. {
  2886. StringBuffer s;
  2887. IException *E = MakeStringException(ROXIE_MULTICAST_ERROR, "Failed to get response from slave(s) for %s in activity %d", i->queryHeader().toString(s).str(), queryId());
  2888. activity.queryLogCtx().logOperatorException(E, __FILE__, __LINE__, "CRemoteResultAdaptor::retry");
  2889. throw E;
  2890. }
  2891. if (!localSlave)
  2892. {
  2893. ROQ->sendPacket(i, activity.queryLogCtx());
  2894. atomic_inc(&retriesSent);
  2895. }
  2896. }
  2897. }
  2898. }
  2899. }
  2900. class ChannelBuffer
  2901. {
  2902. protected:
  2903. unsigned bufferLeft;
  2904. MemoryBuffer buffer;
  2905. char *nextBuf;
  2906. unsigned overflowSequence;
  2907. unsigned channel; // == bonded channel
  2908. bool needsFlush;
  2909. InterruptableSemaphore flowController;
  2910. const CRemoteResultAdaptor &owner;
  2911. CriticalSection crit;
  2912. public:
  2913. ChannelBuffer(const CRemoteResultAdaptor &_owner, unsigned _channel) : owner(_owner), channel(_channel), flowController(perChannelFlowLimit)
  2914. {
  2915. overflowSequence = 0;
  2916. needsFlush = false;
  2917. bufferLeft = 0;
  2918. nextBuf = NULL;
  2919. }
  2920. void init(unsigned minSize)
  2921. {
  2922. assertex(!buffer.length());
  2923. if (minSize < MIN_PAYLOAD_SIZE)
  2924. minSize = MIN_PAYLOAD_SIZE;
  2925. unsigned headerSize = sizeof(RoxiePacketHeader)+owner.headerLength();
  2926. unsigned bufferSize = headerSize+minSize;
  2927. if (bufferSize < mtu_size)
  2928. bufferSize = mtu_size;
  2929. buffer.reserveTruncate(bufferSize);
  2930. bufferLeft = bufferSize - headerSize;
  2931. assertex(buffer.toByteArray());
  2932. nextBuf = (char *) buffer.toByteArray() + headerSize;
  2933. needsFlush = false;
  2934. }
  2935. inline IRoxieQueryPacket *flush()
  2936. {
  2937. CriticalBlock cb(crit);
  2938. Owned<IRoxieQueryPacket> ret;
  2939. if (needsFlush)
  2940. {
  2941. buffer.setLength(nextBuf - buffer.toByteArray());
  2942. RoxiePacketHeader *h = (RoxiePacketHeader *) buffer.toByteArray();
  2943. h->init(owner.remoteId, owner.ruid, channel, overflowSequence);
  2944. //patch logPrefix, cachedContext and parent extract into the place reserved in the message buffer
  2945. byte * tgt = (byte*)(h+1);
  2946. owner.copyHeader(tgt, channel);
  2947. ret.setown(createRoxiePacket(buffer));
  2948. if (overflowSequence == OVERFLOWSEQUENCE_MAX)
  2949. overflowSequence = 1; // don't wrap to 0 - that is a bit special
  2950. else
  2951. overflowSequence++;
  2952. needsFlush = false;
  2953. bufferLeft = 0;
  2954. if (owner.flowControlled)
  2955. {
  2956. CriticalUnblock cub(crit);
  2957. while (!flowController.wait(1000))
  2958. {
  2959. StringBuffer s;
  2960. owner.activity.queryLogCtx().CTXLOG("Channel %d blocked by flow control: %s", channel, h->toString(s).str());
  2961. }
  2962. }
  2963. }
  2964. return ret.getClear();
  2965. }
  2966. inline void signal()
  2967. {
  2968. if (owner.flowControlled)
  2969. flowController.signal();
  2970. }
  2971. inline void interrupt(IException *e)
  2972. {
  2973. flowController.interrupt(e);
  2974. }
  2975. inline void *getBuffer(unsigned size)
  2976. {
  2977. CriticalBlock cb(crit);
  2978. if (bufferLeft >= size)
  2979. {
  2980. needsFlush = true;
  2981. void * ret = nextBuf;
  2982. nextBuf += size;
  2983. bufferLeft -= size;
  2984. return ret;
  2985. }
  2986. else if (!needsFlush)
  2987. {
  2988. init(size);
  2989. return getBuffer(size);
  2990. }
  2991. else if (owner.mergeOrder)
  2992. {
  2993. return buffer.reserve(size); // whole query needs to go as single packet if we are to merge
  2994. }
  2995. else
  2996. return NULL; // will force it to flush and start a new packet
  2997. }
  2998. };
  2999. private:
  3000. friend class CRemoteResultMerger;
  3001. bool allread;
  3002. bool contextCached;
  3003. bool preserveOrder;
  3004. InterruptableSemaphore sentsome;
  3005. Owned <IMessageCollator> mc;
  3006. Owned<IMessageUnpackCursor> mu;
  3007. Owned<IMessageResult> mr;
  3008. ChannelBuffer **buffers;
  3009. IHThorArg &helper;
  3010. unsigned __int64 stopAfter;
  3011. unsigned resendSequence;
  3012. IHThorArg *colocalArg;
  3013. IArrayOf<IRoxieServerQueryPacket> pending;
  3014. CriticalSection pendingCrit;
  3015. IRoxieServerSideCache *serverSideCache;
  3016. unsigned sentSequence;
  3017. Owned<IOutputRowDeserializer> deserializer;
  3018. Owned<IEngineRowAllocator> rowAllocator;
  3019. CRemoteResultMerger merger;
  3020. // this is only used to avoid recreating a bufferStream for each row. A better solution may be needed
  3021. MemoryBuffer tempRowBuffer;
  3022. Owned<ISerialStream> bufferStream;
  3023. CThorStreamDeserializerSource rowSource;
  3024. protected:
  3025. IRowManager *rowManager;
  3026. IRoxieInput *owner;
  3027. unsigned __int64 rowLimit;
  3028. unsigned __int64 keyedLimit;
  3029. IRoxieServerErrorHandler *errorHandler;
  3030. CachedOutputMetaData meta;
  3031. public:
  3032. ISteppingMeta *mergeOrder;
  3033. IRoxieSlaveContext *ctx;
  3034. IDebuggableContext *debugContext;
  3035. IRoxieServerActivity &activity;
  3036. unsigned parentExtractSize;
  3037. const byte * parentExtract;
  3038. bool flowControlled;
  3039. bool deferredStart;
  3040. MemoryBuffer logInfo;
  3041. MemoryBuffer cachedContext;
  3042. const RemoteActivityId &remoteId;
  3043. ruid_t ruid;
  3044. mutable CriticalSection buffersCrit;
  3045. unsigned processed;
  3046. unsigned __int64 totalCycles;
  3047. bool timeActivities;
  3048. //private: //vc6 doesn't like this being private yet accessed by nested class...
  3049. const void *getRow(IMessageUnpackCursor *mu)
  3050. {
  3051. if (!mu->isSerialized() || (meta.isFixedSize() && !deserializer))
  3052. return mu->getNext(meta.getFixedSize());
  3053. else
  3054. {
  3055. RecordLengthType *rowlen = (RecordLengthType *) mu->getNext(sizeof(RecordLengthType));
  3056. if (rowlen)
  3057. {
  3058. RecordLengthType len = *rowlen;
  3059. ReleaseRoxieRow(rowlen);
  3060. const void *slaveRec = mu->getNext(len);
  3061. if (deserializer && mu->isSerialized())
  3062. {
  3063. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  3064. tempRowBuffer.setBuffer(len, const_cast<void *>(slaveRec), false);
  3065. size_t outsize = deserializer->deserialize(rowBuilder, rowSource);
  3066. ReleaseRoxieRow(slaveRec);
  3067. return rowBuilder.finalizeRowClear(outsize);
  3068. }
  3069. else
  3070. return slaveRec;
  3071. }
  3072. else
  3073. return NULL;
  3074. }
  3075. }
  3076. private:
  3077. ChannelBuffer *queryChannelBuffer(unsigned channel, bool force=false)
  3078. {
  3079. CriticalBlock cb(buffersCrit);
  3080. ChannelBuffer *b = buffers[channel];
  3081. if (!b && force)
  3082. {
  3083. if (!contextCached)
  3084. {
  3085. logInfo.clear();
  3086. unsigned char loggingFlags = LOGGING_FLAGSPRESENT | LOGGING_TRACELEVELSET;
  3087. unsigned char ctxTraceLevel = activity.queryLogCtx().queryTraceLevel() + 1; // Avoid passing a 0
  3088. if (activity.queryLogCtx().isIntercepted())
  3089. loggingFlags |= LOGGING_INTERCEPTED;
  3090. if (ctx->queryTraceActivityTimes())
  3091. loggingFlags |= LOGGING_TIMEACTIVITIES;
  3092. if (activity.queryLogCtx().isBlind())
  3093. loggingFlags |= LOGGING_BLIND;
  3094. if (ctx->queryCheckingHeap())
  3095. loggingFlags |= LOGGING_CHECKINGHEAP;
  3096. if (ctx->queryWorkUnit())
  3097. loggingFlags |= LOGGING_WUID;
  3098. if (debugContext)
  3099. {
  3100. loggingFlags |= LOGGING_DEBUGGERACTIVE;
  3101. logInfo.append(loggingFlags).append(ctxTraceLevel);
  3102. MemoryBuffer bpInfo;
  3103. debugContext->serialize(bpInfo);
  3104. bpInfo.append((__uint64)(memsize_t) &activity);
  3105. logInfo.append((unsigned short) bpInfo.length());
  3106. logInfo.append(bpInfo.length(), bpInfo.toByteArray());
  3107. }
  3108. else
  3109. logInfo.append(loggingFlags).append(ctxTraceLevel);
  3110. StringBuffer logPrefix;
  3111. activity.queryLogCtx().getLogPrefix(logPrefix);
  3112. logInfo.append(logPrefix);
  3113. activity.serializeCreateStartContext(cachedContext.clear());
  3114. activity.serializeExtra(cachedContext);
  3115. if (activity.queryVarFileInfo())
  3116. {
  3117. activity.queryVarFileInfo()->queryTimeStamp().serialize(cachedContext);
  3118. cachedContext.append(activity.queryVarFileInfo()->queryCheckSum());
  3119. }
  3120. contextCached = true;
  3121. }
  3122. b = buffers[channel] = new ChannelBuffer(*this, channel);
  3123. }
  3124. return b;
  3125. }
  3126. void processRow(const void *got)
  3127. {
  3128. processed++;
  3129. if (processed > rowLimit)
  3130. {
  3131. ReleaseRoxieRow(got);
  3132. errorHandler->onLimitExceeded(false); // NOTE - should throw exception
  3133. throwUnexpected();
  3134. }
  3135. else if (processed > keyedLimit)
  3136. {
  3137. ReleaseRoxieRow(got);
  3138. errorHandler->onLimitExceeded(true); // NOTE - should throw exception
  3139. throwUnexpected();
  3140. }
  3141. }
  3142. public:
  3143. IMPLEMENT_IINTERFACE;
  3144. CRemoteResultAdaptor(const RemoteActivityId &_remoteId, IOutputMetaData *_meta, IHThorArg &_helper, IRoxieServerActivity &_activity, bool _preserveOrder, bool _flowControlled)
  3145. : remoteId(_remoteId), meta(_meta), activity(_activity), helper(_helper), preserveOrder(_preserveOrder), flowControlled(_flowControlled), merger(*this)
  3146. {
  3147. rowLimit = (unsigned __int64) -1;
  3148. keyedLimit = (unsigned __int64) -1;
  3149. contextCached = false;
  3150. stopAfter = I64C(0x7FFFFFFFFFFFFFFF);
  3151. buffers = new ChannelBuffer*[numChannels+1];
  3152. memset(buffers, 0, (numChannels+1)*sizeof(ChannelBuffer *));
  3153. parentExtractSize = 0;
  3154. parentExtract = NULL;
  3155. owner = NULL;
  3156. mergeOrder = NULL;
  3157. deferredStart = false;
  3158. processed = 0;
  3159. totalCycles = 0;
  3160. sentSequence = 0;
  3161. resendSequence = 0;
  3162. serverSideCache = activity.queryServerSideCache();
  3163. bufferStream.setown(createMemoryBufferSerialStream(tempRowBuffer));
  3164. rowSource.setStream(bufferStream);
  3165. timeActivities = defaultTimeActivities;
  3166. }
  3167. ~CRemoteResultAdaptor()
  3168. {
  3169. if (mc)
  3170. {
  3171. ROQ->queryReceiveManager()->detachCollator(mc);
  3172. mc.clear();
  3173. }
  3174. for (unsigned channel = 0; channel <= numChannels; channel++)
  3175. {
  3176. delete(buffers[channel]);
  3177. }
  3178. delete [] buffers;
  3179. }
  3180. void setMeta(IOutputMetaData *newmeta)
  3181. {
  3182. meta.set(newmeta);
  3183. }
  3184. virtual IRoxieServerActivity *queryActivity()
  3185. {
  3186. return &activity;
  3187. }
  3188. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  3189. {
  3190. return NULL;
  3191. }
  3192. void setMergeInfo(ISteppingMeta *_mergeOrder)
  3193. {
  3194. mergeOrder = _mergeOrder;
  3195. deferredStart = true;
  3196. }
  3197. void send(IRoxieQueryPacket *p)
  3198. {
  3199. if (p)
  3200. {
  3201. Linked<IRoxieQueryPacket> saver(p); // avoids a race with abortPending, without keeping pendingCrit locked over the send which we might prefer not to
  3202. assertex(p->queryHeader().uid==ruid);
  3203. // MORE: Maybe we should base the fastlane flag on some other
  3204. // criteria !! (i.e A Roxie server prediction based on the
  3205. // activity type/activity behaviour/expected reply size .. etc).
  3206. //
  3207. // Currently (code below) based on high priority, seq=0, and none-child activity.
  3208. // But this could still cause too many reply packets on the fatlane
  3209. // (higher priority output Q), which may cause the activities on the
  3210. // low priority output Q to not get service on time.
  3211. if ((colocalArg == 0) && // not a child query activity??
  3212. (p->queryHeader().activityId & (ROXIE_SLA_PRIORITY | ROXIE_HIGH_PRIORITY)) &&
  3213. (p->queryHeader().overflowSequence == 0) &&
  3214. (p->queryHeader().continueSequence & ~CONTINUE_SEQUENCE_SKIPTO)==0)
  3215. p->queryHeader().retries |= ROXIE_FASTLANE;
  3216. if (p->queryHeader().channel)
  3217. {
  3218. bool cached = false;
  3219. IRoxieServerQueryPacket *rsqp = createRoxieServerQueryPacket(p, cached);
  3220. if (deferredStart)
  3221. rsqp->setDelayed(true);
  3222. rsqp->setSequence(sentSequence++);
  3223. {
  3224. CriticalBlock b(pendingCrit);
  3225. pending.append(*rsqp);
  3226. }
  3227. if (!deferredStart)
  3228. {
  3229. if (!cached)
  3230. ROQ->sendPacket(p, activity.queryLogCtx());
  3231. sentsome.signal();
  3232. }
  3233. }
  3234. else
  3235. {
  3236. // Care is needed here. If I send the packet before I add to the pending there is a danger that I'll get results that I discard
  3237. // Need to add first, then send
  3238. unsigned i;
  3239. bool allCached = true;
  3240. for (i = 1; i <= numChannels; i++)
  3241. {
  3242. IRoxieQueryPacket *q = p->clonePacket(i);
  3243. bool thisChannelCached;
  3244. IRoxieServerQueryPacket *rsqp = createRoxieServerQueryPacket(q, thisChannelCached);
  3245. if (!thisChannelCached)
  3246. allCached = false;
  3247. rsqp->setSequence(sentSequence++);
  3248. if (deferredStart)
  3249. {
  3250. rsqp->setDelayed(true);
  3251. }
  3252. {
  3253. CriticalBlock b(pendingCrit);
  3254. pending.append(*rsqp);
  3255. }
  3256. if (!deferredStart)
  3257. sentsome.signal();
  3258. }
  3259. if (!allCached && !deferredStart)
  3260. ROQ->sendPacket(p, activity.queryLogCtx());
  3261. buffers[0]->signal(); // since replies won't come back on that channel...
  3262. p->Release();
  3263. }
  3264. }
  3265. }
  3266. void *getMem(unsigned partNo, unsigned fileNo, unsigned size)
  3267. {
  3268. unsigned channel = partNo ? getBondedChannel(partNo) : 0;
  3269. size += sizeof(PartNoType);
  3270. ChannelBuffer *b = queryChannelBuffer(channel, true);
  3271. char *buffer = (char *) b->getBuffer(size);
  3272. if (!buffer)
  3273. {
  3274. send(b->flush());
  3275. buffer = (char *) b->getBuffer(size);
  3276. }
  3277. PartNoType sp;
  3278. sp.partNo = partNo;
  3279. sp.fileNo = fileNo;
  3280. memcpy(buffer, &sp, sizeof(sp));
  3281. buffer += sizeof(sp);
  3282. return buffer;
  3283. }
  3284. void injectResult(IMessageResult *result)
  3285. {
  3286. IRoxieServerQueryPacket *f = new CRoxieServerQueryPacket(NULL);
  3287. f->setSequence(sentSequence++);
  3288. f->setResult(result);
  3289. CriticalBlock b(pendingCrit);
  3290. pending.append(*f);
  3291. sentsome.signal(); // MORE - arguably should only send if there is any point waking up the listener thread, to save context swicth
  3292. }
  3293. void flush()
  3294. {
  3295. for (unsigned channel = 0; channel <= numChannels; channel++)
  3296. {
  3297. ChannelBuffer *b = queryChannelBuffer(channel, false);
  3298. if (b)
  3299. send(b->flush());
  3300. }
  3301. }
  3302. void interruptBuffers(IException *e)
  3303. {
  3304. for (unsigned channel = 0; channel <= numChannels; channel++)
  3305. {
  3306. ChannelBuffer *b = queryChannelBuffer(channel, false);
  3307. if (b)
  3308. b->interrupt(LINK(e));
  3309. }
  3310. }
  3311. void senddone()
  3312. {
  3313. CriticalBlock b(pendingCrit);
  3314. pending.append(*new CRoxieServerQueryPacketEndMarker);
  3315. sentsome.signal();
  3316. }
  3317. bool fireException(IException *e)
  3318. {
  3319. {
  3320. CriticalBlock b(pendingCrit);
  3321. pending.append(*new CRoxieServerQueryPacketEndMarker);
  3322. }
  3323. interruptBuffers(e);
  3324. if (mc)
  3325. mc->interrupt(LINK(e));
  3326. sentsome.interrupt(e);
  3327. return true;
  3328. }
  3329. virtual void onCreate(IRoxieInput *_owner, IRoxieServerErrorHandler *_errorHandler, IRoxieSlaveContext *_ctx, IHThorArg *_colocalArg)
  3330. {
  3331. owner = _owner;
  3332. errorHandler = _errorHandler;
  3333. ctx = _ctx;
  3334. debugContext = ctx->queryDebugContext();
  3335. colocalArg = _colocalArg;
  3336. if (meta.needsSerializeDisk())
  3337. {
  3338. deserializer.setown(meta.createDiskDeserializer(_ctx->queryCodeContext(), activity.queryId()));
  3339. rowAllocator.setown(ctx->queryCodeContext()->getRowAllocator(meta.queryOriginal(), activity.queryId()));
  3340. }
  3341. if (ctx->queryDebugContext() && ctx->queryDebugContext()->getExecuteSequentially())
  3342. deferredStart = true;
  3343. if (ctx)
  3344. timeActivities = ctx->queryTimeActivities();
  3345. }
  3346. virtual unsigned queryId() const
  3347. {
  3348. return owner->queryId();
  3349. }
  3350. virtual void onStart(unsigned _parentExtractSize, const byte * _parentExtract)
  3351. {
  3352. #ifdef TRACE_STARTSTOP
  3353. if (traceStartStop)
  3354. activity.queryLogCtx().CTXLOG("RRAonstart");
  3355. #endif
  3356. sentsome.reinit();
  3357. ruid = getNextRuid();
  3358. rowManager = &ctx->queryRowManager();
  3359. if (mergeOrder)
  3360. merger.init(mergeOrder, rowManager);
  3361. if (mc)
  3362. {
  3363. ROQ->queryReceiveManager()->detachCollator(mc); // Should never happen - implies someone forgot to call onReset!
  3364. }
  3365. mc.setown(ROQ->queryReceiveManager()->createMessageCollator(rowManager, ruid));
  3366. allread = false;
  3367. mu.clear();
  3368. contextCached = false;
  3369. processed = 0;
  3370. totalCycles = 0;
  3371. resendSequence = 0;
  3372. sentSequence = 0;
  3373. for (unsigned channel = 0; channel <= numChannels; channel++)
  3374. {
  3375. delete(buffers[channel]);
  3376. buffers[channel] = NULL;
  3377. }
  3378. flush();
  3379. parentExtractSize = _parentExtractSize;
  3380. parentExtract = _parentExtract;
  3381. }
  3382. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  3383. {
  3384. #ifdef TRACE_STARTSTOP
  3385. if (traceStartStop)
  3386. activity.queryLogCtx().CTXLOG("RRAstart");
  3387. #endif
  3388. owner->start(parentExtractSize, parentExtract, paused);
  3389. totalCycles = 0;
  3390. }
  3391. void checkAbort()
  3392. {
  3393. owner->checkAbort();
  3394. }
  3395. void setLimits(unsigned __int64 _rowLimit, unsigned __int64 _keyedLimit, unsigned __int64 _stopAfter)
  3396. {
  3397. if (ctx->queryProbeManager())
  3398. {
  3399. if (_rowLimit != (unsigned __int64) -1) ctx->queryProbeManager()->setNodePropertyInt(&activity, "rowLimit", _rowLimit);
  3400. if (_keyedLimit != (unsigned __int64) -1) ctx->queryProbeManager()->setNodePropertyInt(&activity, "keyedLimit", _keyedLimit);
  3401. if (_stopAfter != I64C(0x7FFFFFFFFFFFFFFF)) ctx->queryProbeManager()->setNodePropertyInt(&activity, "choosenLimit", _stopAfter);
  3402. }
  3403. {
  3404. CriticalBlock b(pendingCrit);
  3405. if (pending.length())
  3406. {
  3407. #ifdef _DEBUG
  3408. dumpPending(); // MORE - only defined in debug build - could have put the ifdef inside the dumpPending method
  3409. #endif
  3410. assertex(pending.length()==0);
  3411. }
  3412. pending.append(*new CRoxieServerQueryPacketLimitMarker(_rowLimit, _keyedLimit, _stopAfter));
  3413. }
  3414. sentsome.signal();
  3415. rowLimit = _rowLimit;
  3416. keyedLimit = _keyedLimit;
  3417. stopAfter = _stopAfter;
  3418. }
  3419. virtual void stop(bool aborting)
  3420. {
  3421. #ifdef TRACE_STARTSTOP
  3422. if (traceStartStop)
  3423. activity.queryLogCtx().CTXLOG("RRAstop");
  3424. #endif
  3425. onStop(aborting);
  3426. owner->stop(aborting);
  3427. }
  3428. void onStop(bool aborting)
  3429. {
  3430. #ifdef TRACE_STARTSTOP
  3431. if (traceStartStop)
  3432. activity.queryLogCtx().CTXLOG("RRAonstop");
  3433. #endif
  3434. abortPending();
  3435. interruptBuffers(NULL);
  3436. sentsome.interrupt();
  3437. if (mc) // May not be set if start() chain threw exception
  3438. mc->interrupt();
  3439. }
  3440. virtual void reset()
  3441. {
  3442. #ifdef TRACE_STARTSTOP
  3443. if (traceStartStop)
  3444. activity.queryLogCtx().CTXLOG("RRAreset");
  3445. #endif
  3446. owner->reset();
  3447. onReset();
  3448. }
  3449. virtual void resetEOF()
  3450. {
  3451. throwUnexpected();
  3452. }
  3453. virtual void onReset()
  3454. {
  3455. #ifdef TRACE_STARTSTOP
  3456. if (traceStartStop)
  3457. activity.queryLogCtx().CTXLOG("RRAonreset");
  3458. #endif
  3459. if (mc)
  3460. ROQ->queryReceiveManager()->detachCollator(mc);
  3461. merger.reset();
  3462. pending.kill();
  3463. if (mc && ctx)
  3464. ctx->addSlavesReplyLen(mc->queryBytesReceived());
  3465. mc.clear(); // Or we won't free memory for graphs that get recreated
  3466. mu.clear(); //ditto
  3467. deferredStart = false;
  3468. // NOTE: do NOT clear mergeOrder - this is set at create time not per child query
  3469. }
  3470. virtual IOutputMetaData * queryOutputMeta() const
  3471. {
  3472. return helper.queryOutputMeta();
  3473. }
  3474. virtual unsigned __int64 queryTotalCycles() const
  3475. {
  3476. return totalCycles;
  3477. }
  3478. virtual unsigned __int64 queryLocalCycles() const
  3479. {
  3480. return owner->queryLocalCycles();
  3481. }
  3482. virtual IRoxieInput *queryInput(unsigned idx) const
  3483. {
  3484. return owner->queryInput(idx);
  3485. }
  3486. const void * nextSteppedGE(const void *seek, const void *rawSeek, unsigned numFields, unsigned seekLen, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  3487. {
  3488. if (activity.queryLogCtx().queryTraceLevel() > 20)
  3489. {
  3490. StringBuffer recstr;
  3491. unsigned i;
  3492. for (i = 0; i < seekLen; i++)
  3493. {
  3494. recstr.appendf("%02x ", ((unsigned char *) rawSeek)[i]);
  3495. }
  3496. activity.queryLogCtx().CTXLOG("CRemoteResultAdaptor::nextSteppedGE(rawSeek=%s numFields=%d, seeklen=%d, returnMismatches=%d)", recstr.str(), numFields, seekLen, stepExtra.returnMismatches());
  3497. }
  3498. assertex(mergeOrder);
  3499. if (deferredStart)
  3500. {
  3501. CriticalBlock b(pendingCrit);
  3502. ForEachItemIn(idx, pending)
  3503. {
  3504. IRoxieServerQueryPacket &p = pending.item(idx);
  3505. if (p.isDelayed())
  3506. {
  3507. p.setDelayed(false);
  3508. if (activity.queryLogCtx().queryTraceLevel() > 10)
  3509. activity.queryLogCtx().CTXLOG("About to send deferred start from nextSteppedGE, setting requireExact to %d", !stepExtra.returnMismatches());
  3510. MemoryBuffer serializedSkip;
  3511. activity.serializeSkipInfo(serializedSkip, seekLen, rawSeek, numFields, seek, stepExtra);
  3512. p.setPacket(p.queryPacket()->insertSkipData(serializedSkip.length(), serializedSkip.toByteArray()));
  3513. ROQ->sendPacket(p.queryPacket(), activity.queryLogCtx());
  3514. sentsome.signal();
  3515. }
  3516. }
  3517. deferredStart = false;
  3518. }
  3519. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  3520. if (processed==stopAfter)
  3521. return NULL;
  3522. if (allread)
  3523. return NULL;
  3524. loop
  3525. {
  3526. if (merger.ready())
  3527. {
  3528. const void *got = merger.nextSteppedGE(seek, rawSeek, numFields, seekLen, wasCompleteMatch, stepExtra);
  3529. if (got)
  3530. {
  3531. processRow(got);
  3532. return got;
  3533. }
  3534. }
  3535. if (!reload()) // MORE - should pass the seek info here...
  3536. return NULL;
  3537. }
  3538. }
  3539. virtual const void *nextInGroup()
  3540. {
  3541. // If we are merging then we need to do a heapsort on all
  3542. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  3543. if (activity.queryLogCtx().queryTraceLevel() > 10)
  3544. {
  3545. activity.queryLogCtx().CTXLOG("CRemoteResultAdaptor::nextInGroup()");
  3546. }
  3547. loop
  3548. {
  3549. checkDelayed();
  3550. if (processed==stopAfter)
  3551. return NULL;
  3552. if (allread)
  3553. return NULL;
  3554. // If we can still consume from the merger or the most recently retrieved mu, do so.
  3555. const void *got = NULL;
  3556. if (mergeOrder && merger.ready())
  3557. {
  3558. bool matched = true;
  3559. got = merger.next(matched, dummySmartStepExtra);
  3560. }
  3561. else if (mu)
  3562. got = getRow(mu);
  3563. if (got)
  3564. {
  3565. processRow(got);
  3566. return got;
  3567. }
  3568. if (!reload())
  3569. return NULL;
  3570. }
  3571. }
  3572. bool reload()
  3573. {
  3574. // Wait for something to be returned from a slave....
  3575. mu.clear();
  3576. sentsome.wait();
  3577. // must be at least an endMarker on the queue since sentsome was signalled
  3578. {
  3579. CriticalBlock b(pendingCrit);
  3580. IRoxieServerQueryPacket &top = pending.item(0);
  3581. if (top.isLimit(rowLimit, keyedLimit, stopAfter)) // This is really a start marker...
  3582. {
  3583. pending.remove(0);
  3584. return true;
  3585. }
  3586. else if (top.isEnd())
  3587. {
  3588. pending.remove(0);
  3589. allread = true;
  3590. if (activity.queryLogCtx().queryTraceLevel() > 5)
  3591. activity.queryLogCtx().CTXLOG("All read on ruid %x", ruid);
  3592. return false;
  3593. }
  3594. else if (mergeOrder)
  3595. {
  3596. unsigned idx = 0;
  3597. bool added = false;
  3598. while (pending.isItem(idx))
  3599. {
  3600. IRoxieServerQueryPacket &item = pending.item(idx);
  3601. if (item.isEnd())
  3602. {
  3603. if (merger.noteEndSeen())
  3604. {
  3605. sentsome.signal(); // Because we waited, yet didn't actually consume anything
  3606. added = true;
  3607. }
  3608. break;
  3609. }
  3610. else if (item.hasResult())
  3611. {
  3612. merger.noteResult(&item, item.getSequence());
  3613. pending.remove(idx);
  3614. added = true;
  3615. }
  3616. else if (item.isContinuation())
  3617. idx++;
  3618. else
  3619. break;
  3620. }
  3621. if (added)
  3622. return true;
  3623. }
  3624. else if (top.hasResult())
  3625. {
  3626. mr.setown(pending.item(0).getResult());
  3627. mu.setown(mr->getCursor(rowManager));
  3628. pending.remove(0);
  3629. return true;
  3630. }
  3631. }
  3632. getNextUnpacker();
  3633. return true;
  3634. }
  3635. void getNextUnpacker()
  3636. {
  3637. mu.clear();
  3638. unsigned ctxTraceLevel = activity.queryLogCtx().queryTraceLevel();
  3639. loop
  3640. {
  3641. checkDelayed();
  3642. unsigned timeout = remoteId.isSLAPriority() ? slaTimeout : (remoteId.isHighPriority() ? highTimeout : lowTimeout);
  3643. owner->checkAbort();
  3644. bool anyActivity;
  3645. if (ctxTraceLevel > 5)
  3646. activity.queryLogCtx().CTXLOG("Calling getNextUnpacker(%d)", timeout);
  3647. mr.setown(mc->getNextResult(timeout, anyActivity));
  3648. if (ctxTraceLevel > 6)
  3649. activity.queryLogCtx().CTXLOG("Called getNextUnpacker(%d), activity=%d", timeout, anyActivity);
  3650. owner->checkAbort();
  3651. if (mr)
  3652. {
  3653. unsigned roxieHeaderLen;
  3654. const RoxiePacketHeader &header = *(const RoxiePacketHeader *) mr->getMessageHeader(roxieHeaderLen);
  3655. #ifdef _DEBUG
  3656. assertex(roxieHeaderLen == sizeof(RoxiePacketHeader));
  3657. #endif
  3658. if (ctxTraceLevel > 5)
  3659. {
  3660. StringBuffer s;
  3661. activity.queryLogCtx().CTXLOG("getNextUnpacker got packet %s", header.toString(s).str());
  3662. }
  3663. CriticalBlock b(pendingCrit);
  3664. unsigned idx = 0;
  3665. IRoxieServerQueryPacket *original = NULL;
  3666. IRoxieQueryPacket *op;
  3667. while (pending.isItem(idx))
  3668. {
  3669. original = &pending.item(idx);
  3670. op = original->queryPacket();
  3671. if (op && header.matchPacket(op->queryHeader()))
  3672. break;
  3673. original = NULL;
  3674. idx++;
  3675. }
  3676. if (!original || original->hasResult())
  3677. {
  3678. switch (header.activityId)
  3679. {
  3680. case ROXIE_FILECALLBACK:
  3681. {
  3682. // tell slave to abort
  3683. //if (ctxTraceLevel > 5)
  3684. {
  3685. StringBuffer s;
  3686. activity.queryLogCtx().CTXLOG("Redundant callback on query %s", header.toString(s).str());
  3687. }
  3688. Owned<IMessageUnpackCursor> callbackData = mr->getCursor(rowManager);
  3689. OwnedConstRoxieRow len = callbackData->getNext(sizeof(RecordLengthType));
  3690. if (len)
  3691. {
  3692. RecordLengthType *rowlen = (RecordLengthType *) len.get();
  3693. OwnedConstRoxieRow row = callbackData->getNext(*rowlen);
  3694. const char *rowdata = (const char *) row.get();
  3695. // bool isOpt = * (bool *) rowdata;
  3696. // bool isLocal = * (bool *) (rowdata+1);
  3697. ROQ->sendAbortCallback(header, rowdata+2, activity.queryLogCtx());
  3698. }
  3699. else
  3700. throwUnexpected();
  3701. break;
  3702. }
  3703. // MORE - ROXIE_ALIVE perhaps should go here too
  3704. case ROXIE_TRACEINFO:
  3705. {
  3706. Owned<IMessageUnpackCursor> extra = mr->getCursor(rowManager);
  3707. loop
  3708. {
  3709. RecordLengthType *rowlen = (RecordLengthType *) extra->getNext(sizeof(RecordLengthType));
  3710. if (rowlen)
  3711. {
  3712. char *logInfo = (char *) extra->getNext(*rowlen);
  3713. MemoryBuffer buf;
  3714. buf.setBuffer(*rowlen, logInfo, false);
  3715. activity.queryLogCtx().CTXLOGl(new LogItem(buf));
  3716. ReleaseRoxieRow(rowlen);
  3717. ReleaseRoxieRow(logInfo);
  3718. }
  3719. else
  3720. break;
  3721. }
  3722. break;
  3723. }
  3724. default:
  3725. if (ctxTraceLevel > 3)
  3726. activity.queryLogCtx().CTXLOG("Discarding packet %p - original %p is NULL or has result already", mr.get(), original);
  3727. mr->discard();
  3728. break;
  3729. }
  3730. mr.clear();
  3731. }
  3732. else
  3733. {
  3734. atomic_inc(&resultsReceived);
  3735. switch (header.activityId)
  3736. {
  3737. case ROXIE_DEBUGCALLBACK:
  3738. {
  3739. Owned<IMessageUnpackCursor> callbackData = mr->getCursor(rowManager);
  3740. OwnedConstRoxieRow len = callbackData->getNext(sizeof(RecordLengthType));
  3741. if (len)
  3742. {
  3743. RecordLengthType *rowlen = (RecordLengthType *) len.get();
  3744. OwnedConstRoxieRow row = callbackData->getNext(*rowlen);
  3745. char *rowdata = (char *) row.get();
  3746. if (ctxTraceLevel > 5)
  3747. {
  3748. StringBuffer s;
  3749. activity.queryLogCtx().CTXLOG("Callback on query %s for debug", header.toString(s).str());
  3750. }
  3751. MemoryBuffer slaveInfo;
  3752. slaveInfo.setBuffer(*rowlen, rowdata, false);
  3753. unsigned debugSequence;
  3754. slaveInfo.read(debugSequence);
  3755. Owned<IRoxieQueryPacket> reply = original->getDebugResponse(debugSequence);
  3756. if (!reply)
  3757. reply.setown(activity.queryContext()->queryDebugContext()->onDebugCallback(header, *rowlen, rowdata));
  3758. if (reply)
  3759. {
  3760. original->setDebugResponse(debugSequence, reply);
  3761. ROQ->sendPacket(reply, activity.queryLogCtx());
  3762. }
  3763. }
  3764. else
  3765. throwUnexpected();
  3766. // MORE - somehow we need to make sure slave gets a reply even if I'm not waiting (in udp layer)
  3767. // Leave original message on pending queue in original location - this is not a reply to it.
  3768. break;
  3769. }
  3770. case ROXIE_FILECALLBACK:
  3771. {
  3772. // we need to send back to the slave a message containing the file info requested.
  3773. Owned<IMessageUnpackCursor> callbackData = mr->getCursor(rowManager);
  3774. OwnedConstRoxieRow len = callbackData->getNext(sizeof(RecordLengthType));
  3775. if (len)
  3776. {
  3777. RecordLengthType *rowlen = (RecordLengthType *) len.get();
  3778. OwnedConstRoxieRow row = callbackData->getNext(*rowlen);
  3779. const char *rowdata = (const char *) row.get();
  3780. bool isOpt = * (bool *) rowdata;
  3781. bool isLocal = * (bool *) (rowdata+1);
  3782. const char *lfn = rowdata+2;
  3783. if (ctxTraceLevel > 5)
  3784. {
  3785. StringBuffer s;
  3786. activity.queryLogCtx().CTXLOG("Callback on query %s file %s", header.toString(s).str(),(const char *) lfn);
  3787. }
  3788. activity.queryContext()->onFileCallback(header, lfn, isOpt, isLocal);
  3789. }
  3790. else
  3791. throwUnexpected();
  3792. // MORE - somehow we need to make sure slave gets a reply even if I'm not waiting (in udp layer)
  3793. // Leave original message on pending queue in original location - this is not a reply to it.
  3794. break;
  3795. }
  3796. case ROXIE_KEYEDLIMIT_EXCEEDED:
  3797. activity.queryLogCtx().CTXLOG("ROXIE_KEYEDLIMIT_EXCEEDED");
  3798. errorHandler->onLimitExceeded(true); // NOTE - should throw exception!
  3799. throwUnexpected();
  3800. case ROXIE_LIMIT_EXCEEDED:
  3801. activity.queryLogCtx().CTXLOG("ROXIE_LIMIT_EXCEEDED");
  3802. errorHandler->onLimitExceeded(false); // NOTE - should throw exception!
  3803. throwUnexpected();
  3804. case ROXIE_TRACEINFO:
  3805. {
  3806. Owned<IMessageUnpackCursor> extra = mr->getCursor(rowManager);
  3807. loop
  3808. {
  3809. RecordLengthType *rowlen = (RecordLengthType *) extra->getNext(sizeof(RecordLengthType));
  3810. if (rowlen)
  3811. {
  3812. char *logInfo = (char *) extra->getNext(*rowlen);
  3813. MemoryBuffer buf;
  3814. buf.setBuffer(*rowlen, logInfo, false);
  3815. activity.queryLogCtx().CTXLOGl(new LogItem(buf));
  3816. ReleaseRoxieRow(rowlen);
  3817. ReleaseRoxieRow(logInfo);
  3818. }
  3819. else
  3820. break;
  3821. }
  3822. break;
  3823. }
  3824. case ROXIE_EXCEPTION:
  3825. if (ctxTraceLevel > 1)
  3826. {
  3827. StringBuffer s;
  3828. activity.queryLogCtx().CTXLOG("Exception on query %s", header.toString(s).str());
  3829. }
  3830. op->queryHeader().noteException(header.retries);
  3831. if (op->queryHeader().allChannelsFailed())
  3832. {
  3833. activity.queryLogCtx().CTXLOG("Multiple exceptions on query - aborting");
  3834. Owned<IMessageUnpackCursor> exceptionData = mr->getCursor(rowManager);
  3835. throwRemoteException(exceptionData);
  3836. }
  3837. // Leave it on pending queue in original location
  3838. break;
  3839. case ROXIE_ALIVE:
  3840. if (ctxTraceLevel > 4)
  3841. {
  3842. StringBuffer s;
  3843. activity.queryLogCtx().CTXLOG("ROXIE_ALIVE: %s", header.toString(s).str());
  3844. }
  3845. op->queryHeader().noteAlive(header.retries & ROXIE_RETRIES_MASK);
  3846. // Leave it on pending queue in original location
  3847. break;
  3848. default:
  3849. if (header.retries & ROXIE_RETRIES_MASK)
  3850. atomic_inc(&retriesNeeded);
  3851. unsigned metaLen;
  3852. const void *metaData = mr->getMessageMetadata(metaLen);
  3853. if (metaLen)
  3854. {
  3855. // We got back first chunk but there is more.
  3856. // resend the packet, with the cursor info provided.
  3857. // MORE - if smart-stepping, we don't want to send the continuation immediately. Other cases it's not clear that we do.
  3858. if (ctxTraceLevel > 1)
  3859. {
  3860. StringBuffer s;
  3861. activity.queryLogCtx().CTXLOG("Additional data size %d on query %s mergeOrder %p", metaLen, header.toString(s).str(), mergeOrder);
  3862. }
  3863. if (*((unsigned short *) metaData) + sizeof(unsigned short) != metaLen)
  3864. {
  3865. StringBuffer s;
  3866. activity.queryLogCtx().CTXLOG("Additional data size %d on query %s mergeOrder %p", metaLen, header.toString(s).str(), mergeOrder);
  3867. activity.queryLogCtx().CTXLOG("Additional data is corrupt");
  3868. throwUnexpected();
  3869. }
  3870. MemoryBuffer nextQuery;
  3871. nextQuery.append(sizeof(RoxiePacketHeader), &header);
  3872. nextQuery.append(metaLen, metaData);
  3873. nextQuery.append(op->getTraceLength(), op->queryTraceInfo());
  3874. nextQuery.append(op->getContextLength(), op->queryContextData());
  3875. if (resendSequence == CONTINUESEQUENCE_MAX)
  3876. {
  3877. activity.queryLogCtx().CTXLOG("ERROR: Continuation sequence wrapped"); // shouldn't actually matter.... but suggests a very iffy query!
  3878. resendSequence = 1;
  3879. }
  3880. else
  3881. resendSequence++;
  3882. RoxiePacketHeader *newHeader = (RoxiePacketHeader *) nextQuery.toByteArray();
  3883. newHeader->continueSequence = resendSequence; // NOTE - we clear the skipTo flag since continuation of a skip is NOT a skip...
  3884. newHeader->retries &= ~ROXIE_RETRIES_MASK;
  3885. IRoxieQueryPacket *resend = createRoxiePacket(nextQuery);
  3886. CRoxieServerQueryPacket *fqp = new CRoxieServerQueryPacket(resend);
  3887. fqp->setSequence(original->getSequence());
  3888. pending.add(*fqp, idx+1); // note that pending takes ownership. sendPacket does not release.
  3889. original->setContinuation(LINK(fqp));
  3890. if (mergeOrder)
  3891. fqp->setDelayed(true);
  3892. else
  3893. {
  3894. ROQ->sendPacket(resend, activity.queryLogCtx());
  3895. sentsome.signal();
  3896. }
  3897. // Note that we don't attempt to cache results that have continuation records - too tricky !
  3898. }
  3899. else
  3900. {
  3901. if (serverSideCache)
  3902. serverSideCache->noteCachedResult(original, mr);
  3903. }
  3904. unsigned channel = header.channel;
  3905. {
  3906. ChannelBuffer *b = queryChannelBuffer(channel); // If not something is wrong, or we sent out on channel 0?
  3907. if (b)
  3908. b->signal();
  3909. }
  3910. original->setResult(mr.getClear());
  3911. sentsome.signal();
  3912. return;
  3913. }
  3914. }
  3915. }
  3916. else
  3917. {
  3918. if (!anyActivity)
  3919. {
  3920. activity.queryLogCtx().CTXLOG("Input has stalled - retry required?");
  3921. retryPending();
  3922. }
  3923. }
  3924. }
  3925. }
  3926. inline unsigned headerLength() const
  3927. {
  3928. return logInfo.length() + cachedContext.length() + sizeof(unsigned) + parentExtractSize;
  3929. }
  3930. void copyHeader(byte *tgt, unsigned channel) const
  3931. {
  3932. unsigned len = logInfo.length();
  3933. memcpy(tgt, logInfo.toByteArray(), len);
  3934. tgt += len;
  3935. *(unsigned *) tgt = parentExtractSize;
  3936. tgt += sizeof(unsigned);
  3937. memcpy(tgt, parentExtract, parentExtractSize);
  3938. tgt += parentExtractSize;
  3939. memcpy(tgt, cachedContext.toByteArray(), cachedContext.length());
  3940. tgt += cachedContext.length();
  3941. }
  3942. };
  3943. class CSkippableRemoteResultAdaptor : public CRemoteResultAdaptor
  3944. {
  3945. Owned <IException> exception;
  3946. bool skipping;
  3947. ConstPointerArray buff;
  3948. unsigned index;
  3949. bool pulled;
  3950. void pullInput()
  3951. {
  3952. try
  3953. {
  3954. if (exception)
  3955. throw exception.getClear();
  3956. unsigned __int64 count = 0;
  3957. loop
  3958. {
  3959. const void * next = CRemoteResultAdaptor::nextInGroup();
  3960. if (next == NULL)
  3961. {
  3962. next = CRemoteResultAdaptor::nextInGroup();
  3963. if(next == NULL)
  3964. break;
  3965. buff.append(NULL);
  3966. }
  3967. count++;
  3968. if (count > rowLimit)
  3969. {
  3970. ReleaseRoxieRow(next);
  3971. ReleaseRoxieRowSet(buff);
  3972. errorHandler->onLimitExceeded(false); // throws an exception - user or LimitSkipException
  3973. throwUnexpected();
  3974. }
  3975. else if (count > keyedLimit)
  3976. {
  3977. ReleaseRoxieRow(next);
  3978. ReleaseRoxieRowSet(buff);
  3979. errorHandler->onLimitExceeded(true); // throws an exception - user or LimitSkipException
  3980. throwUnexpected();
  3981. }
  3982. buff.append(next);
  3983. }
  3984. }
  3985. catch (IException *E)
  3986. {
  3987. if (QUERYINTERFACE(E, LimitSkipException))
  3988. {
  3989. Owned<IException> cleanup = E;
  3990. ReleaseRoxieRowSet(buff);
  3991. const void *onfail = errorHandler->createLimitFailRow(E->errorCode() == KeyedLimitSkipErrorCode);
  3992. if (onfail)
  3993. buff.append(onfail);
  3994. }
  3995. else
  3996. throw;
  3997. }
  3998. pulled = true;
  3999. }
  4000. public:
  4001. CSkippableRemoteResultAdaptor(const RemoteActivityId &_remoteId, IOutputMetaData *_meta, IHThorArg &_helper, IRoxieServerActivity &_activity, bool _preserveOrder, bool _flowControlled, bool _skipping) :
  4002. CRemoteResultAdaptor(_remoteId, _meta, _helper, _activity, _preserveOrder, _flowControlled)
  4003. {
  4004. skipping = _skipping;
  4005. index = 0;
  4006. pulled = false;
  4007. }
  4008. void setException(IException *E)
  4009. {
  4010. exception.setown(E);
  4011. }
  4012. virtual void onReset()
  4013. {
  4014. while (buff.isItem(index))
  4015. ReleaseRoxieRow(buff.item(index++));
  4016. buff.kill();
  4017. pulled = false;
  4018. exception.clear();
  4019. CRemoteResultAdaptor::onReset();
  4020. }
  4021. void onStart(unsigned _parentExtractSize, const byte * _parentExtract)
  4022. {
  4023. index = 0;
  4024. pulled = false;
  4025. CRemoteResultAdaptor::onStart(_parentExtractSize, _parentExtract);
  4026. }
  4027. virtual const void * nextSteppedGE(const void *seek, const void *rawSeek, unsigned numFields, unsigned seeklen, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  4028. {
  4029. // MORE - not sure what we need to do about the skip case... but we need at least this to prevent issues with exception getting lost
  4030. if (exception)
  4031. throw exception.getClear();
  4032. return CRemoteResultAdaptor::nextSteppedGE(seek, rawSeek, numFields, seeklen, wasCompleteMatch, stepExtra);
  4033. }
  4034. virtual const void *nextInGroup()
  4035. {
  4036. if (skipping)
  4037. {
  4038. if(!pulled)
  4039. pullInput();
  4040. if(buff.isItem(index))
  4041. {
  4042. const void * next = buff.item(index++);
  4043. if(next)
  4044. processed++;
  4045. return next;
  4046. }
  4047. return NULL;
  4048. }
  4049. else
  4050. {
  4051. if (exception)
  4052. throw exception.getClear();
  4053. return CRemoteResultAdaptor::nextInGroup();
  4054. }
  4055. }
  4056. };
  4057. //=================================================================================
  4058. class CRoxieServerApplyActivity : public CRoxieServerInternalSinkActivity
  4059. {
  4060. IHThorApplyArg &helper;
  4061. public:
  4062. CRoxieServerApplyActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4063. : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorApplyArg &) basehelper)
  4064. {
  4065. }
  4066. virtual void onExecute()
  4067. {
  4068. helper.start();
  4069. loop
  4070. {
  4071. const void * next = input->nextInGroup();
  4072. if (!next)
  4073. {
  4074. next = input->nextInGroup();
  4075. if (!next)
  4076. break;
  4077. }
  4078. helper.apply(next);
  4079. ReleaseRoxieRow(next);
  4080. }
  4081. helper.end();
  4082. }
  4083. };
  4084. class CRoxieServerApplyActivityFactory : public CRoxieServerActivityFactory
  4085. {
  4086. bool isRoot;
  4087. public:
  4088. CRoxieServerApplyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  4089. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
  4090. {
  4091. }
  4092. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  4093. {
  4094. return new CRoxieServerApplyActivity(this, _probeManager);
  4095. }
  4096. virtual bool isSink() const
  4097. {
  4098. return isRoot;
  4099. }
  4100. };
  4101. IRoxieServerActivityFactory *createRoxieServerApplyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  4102. {
  4103. return new CRoxieServerApplyActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
  4104. }
  4105. //=================================================================================
  4106. class CRoxieServerNullActivity : public CRoxieServerActivity
  4107. {
  4108. public:
  4109. CRoxieServerNullActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4110. : CRoxieServerActivity(_factory, _probeManager)
  4111. {
  4112. }
  4113. virtual const void *nextInGroup()
  4114. {
  4115. return NULL;
  4116. }
  4117. };
  4118. IRoxieServerActivity * createRoxieServerNullActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4119. {
  4120. return new CRoxieServerNullActivity(_factory, _probeManager);
  4121. }
  4122. class CRoxieServerNullActivityFactory : public CRoxieServerActivityFactory
  4123. {
  4124. public:
  4125. CRoxieServerNullActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4126. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  4127. {
  4128. }
  4129. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  4130. {
  4131. return new CRoxieServerNullActivity(this, _probeManager);
  4132. }
  4133. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  4134. {
  4135. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for null activity");
  4136. }
  4137. };
  4138. IRoxieServerActivityFactory *createRoxieServerNullActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4139. {
  4140. return new CRoxieServerNullActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  4141. }
  4142. //=================================================================================
  4143. class CRoxieServerPassThroughActivity : public CRoxieServerActivity
  4144. {
  4145. public:
  4146. CRoxieServerPassThroughActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4147. : CRoxieServerActivity(_factory, _probeManager)
  4148. {
  4149. }
  4150. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  4151. {
  4152. return input->gatherConjunctions(collector);
  4153. }
  4154. virtual void resetEOF()
  4155. {
  4156. input->resetEOF();
  4157. }
  4158. virtual const void *nextInGroup()
  4159. {
  4160. const void * next = input->nextInGroup();
  4161. if (next)
  4162. processed++;
  4163. return next;
  4164. }
  4165. virtual bool isPassThrough()
  4166. {
  4167. return true;
  4168. }
  4169. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  4170. {
  4171. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  4172. const void * next = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  4173. if (next)
  4174. processed++;
  4175. return next;
  4176. }
  4177. IInputSteppingMeta * querySteppingMeta()
  4178. {
  4179. return input->querySteppingMeta();
  4180. }
  4181. };
  4182. IRoxieServerActivity * createRoxieServerPassThroughActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4183. {
  4184. return new CRoxieServerPassThroughActivity(_factory, _probeManager);
  4185. }
  4186. //=================================================================================
  4187. class CRoxieServerChildBaseActivity : public CRoxieServerActivity
  4188. {
  4189. protected:
  4190. bool eof;
  4191. bool first;
  4192. public:
  4193. CRoxieServerChildBaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4194. : CRoxieServerActivity(_factory, _probeManager)
  4195. {
  4196. eof = false;
  4197. first = true;
  4198. }
  4199. ~CRoxieServerChildBaseActivity()
  4200. {
  4201. }
  4202. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  4203. {
  4204. eof = false;
  4205. first = true;
  4206. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  4207. }
  4208. };
  4209. class CRoxieServerChildBaseActivityFactory : public CRoxieServerActivityFactory
  4210. {
  4211. public:
  4212. CRoxieServerChildBaseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4213. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  4214. {
  4215. }
  4216. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  4217. {
  4218. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for %s activity", getActivityText(kind));
  4219. }
  4220. };
  4221. //=================================================================================
  4222. class CRoxieServerChildIteratorActivity : public CRoxieServerChildBaseActivity
  4223. {
  4224. IHThorChildIteratorArg &helper;
  4225. public:
  4226. CRoxieServerChildIteratorActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4227. : CRoxieServerChildBaseActivity(_factory, _probeManager), helper((IHThorChildIteratorArg &) basehelper)
  4228. {
  4229. }
  4230. virtual bool needsAllocator() const { return true; }
  4231. virtual const void *nextInGroup()
  4232. {
  4233. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  4234. if (eof)
  4235. return NULL;
  4236. bool ok;
  4237. if (first)
  4238. {
  4239. ok = helper.first();
  4240. first = false;
  4241. }
  4242. else
  4243. ok = helper.next();
  4244. try
  4245. {
  4246. while (ok)
  4247. {
  4248. processed++;
  4249. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  4250. unsigned outSize = helper.transform(rowBuilder);
  4251. if (outSize)
  4252. return rowBuilder.finalizeRowClear(outSize);
  4253. ok = helper.next();
  4254. }
  4255. }
  4256. catch (IException *E)
  4257. {
  4258. throw makeWrappedException(E);
  4259. }
  4260. eof = true;
  4261. return NULL;
  4262. }
  4263. };
  4264. class CRoxieServerChildIteratorActivityFactory : public CRoxieServerChildBaseActivityFactory
  4265. {
  4266. public:
  4267. CRoxieServerChildIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4268. : CRoxieServerChildBaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  4269. {
  4270. }
  4271. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  4272. {
  4273. return new CRoxieServerChildIteratorActivity(this, _probeManager);
  4274. }
  4275. };
  4276. IRoxieServerActivityFactory *createRoxieServerChildIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4277. {
  4278. return new CRoxieServerChildIteratorActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  4279. }
  4280. //=================================================================================
  4281. class CRoxieServerChildNormalizeActivity : public CRoxieServerChildBaseActivity
  4282. {
  4283. IHThorChildNormalizeArg &helper;
  4284. public:
  4285. CRoxieServerChildNormalizeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4286. : CRoxieServerChildBaseActivity(_factory, _probeManager), helper((IHThorChildNormalizeArg &) basehelper)
  4287. {
  4288. }
  4289. virtual bool needsAllocator() const { return true; }
  4290. virtual const void *nextInGroup()
  4291. {
  4292. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  4293. if (eof)
  4294. return NULL;
  4295. bool ok;
  4296. if (first)
  4297. {
  4298. ok = helper.first();
  4299. first = false;
  4300. }
  4301. else
  4302. ok = helper.next();
  4303. if (ok)
  4304. {
  4305. try
  4306. {
  4307. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  4308. do {
  4309. unsigned outSize = helper.transform(rowBuilder);
  4310. if (outSize)
  4311. {
  4312. processed++;
  4313. return rowBuilder.finalizeRowClear(outSize);
  4314. }
  4315. ok = helper.next();
  4316. }
  4317. while (ok);
  4318. }
  4319. catch (IException *E)
  4320. {
  4321. throw makeWrappedException(E);
  4322. }
  4323. }
  4324. eof = true;
  4325. return NULL;
  4326. }
  4327. };
  4328. class CRoxieServerChildNormalizeActivityFactory : public CRoxieServerChildBaseActivityFactory
  4329. {
  4330. public:
  4331. CRoxieServerChildNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4332. : CRoxieServerChildBaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  4333. {
  4334. }
  4335. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  4336. {
  4337. return new CRoxieServerChildNormalizeActivity(this, _probeManager);
  4338. }
  4339. };
  4340. IRoxieServerActivityFactory *createRoxieServerNewChildNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4341. {
  4342. return new CRoxieServerChildNormalizeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  4343. }
  4344. //=================================================================================
  4345. class CRoxieServerChildAggregateActivity : public CRoxieServerChildBaseActivity
  4346. {
  4347. IHThorChildAggregateArg &helper;
  4348. public:
  4349. CRoxieServerChildAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4350. : CRoxieServerChildBaseActivity(_factory, _probeManager), helper((IHThorChildAggregateArg &) basehelper)
  4351. {
  4352. }
  4353. virtual bool needsAllocator() const { return true; }
  4354. virtual const void *nextInGroup()
  4355. {
  4356. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  4357. if (eof)
  4358. return NULL;
  4359. eof = true;
  4360. processed++;
  4361. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  4362. helper.clearAggregate(rowBuilder);
  4363. helper.processRows(rowBuilder);
  4364. size32_t finalSize = meta.getRecordSize(rowBuilder.getSelf());
  4365. return rowBuilder.finalizeRowClear(finalSize);
  4366. }
  4367. };
  4368. class CRoxieServerChildAggregateActivityFactory : public CRoxieServerChildBaseActivityFactory
  4369. {
  4370. public:
  4371. CRoxieServerChildAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4372. : CRoxieServerChildBaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  4373. {
  4374. }
  4375. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  4376. {
  4377. return new CRoxieServerChildAggregateActivity(this, _probeManager);
  4378. }
  4379. };
  4380. IRoxieServerActivityFactory *createRoxieServerNewChildAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4381. {
  4382. return new CRoxieServerChildAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  4383. }
  4384. //=================================================================================
  4385. class CRoxieServerChildGroupAggregateActivity : public CRoxieServerChildBaseActivity, public IHThorGroupAggregateCallback
  4386. {
  4387. IHThorChildGroupAggregateArg &helper;
  4388. RowAggregator aggregated;
  4389. public:
  4390. IMPLEMENT_IINTERFACE
  4391. CRoxieServerChildGroupAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4392. : CRoxieServerChildBaseActivity(_factory, _probeManager), helper((IHThorChildGroupAggregateArg &) basehelper),
  4393. aggregated(helper, helper)
  4394. {
  4395. }
  4396. void processRow(const void * next)
  4397. {
  4398. aggregated.addRow(next);
  4399. }
  4400. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  4401. {
  4402. CRoxieServerChildBaseActivity::start(parentExtractSize, parentExtract, paused);
  4403. aggregated.start(rowAllocator);
  4404. }
  4405. virtual void reset()
  4406. {
  4407. aggregated.reset();
  4408. CRoxieServerChildBaseActivity::reset();
  4409. }
  4410. virtual bool needsAllocator() const { return true; }
  4411. virtual const void *nextInGroup()
  4412. {
  4413. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  4414. if (eof)
  4415. return NULL;
  4416. if (first)
  4417. {
  4418. helper.processRows(this);
  4419. first = false;
  4420. }
  4421. Owned<AggregateRowBuilder> next = aggregated.nextResult();
  4422. if (next)
  4423. {
  4424. processed++;
  4425. return next->finalizeRowClear();
  4426. }
  4427. eof = true;
  4428. return NULL;
  4429. }
  4430. };
  4431. class CRoxieServerChildGroupAggregateActivityFactory : public CRoxieServerChildBaseActivityFactory
  4432. {
  4433. public:
  4434. CRoxieServerChildGroupAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4435. : CRoxieServerChildBaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  4436. {
  4437. }
  4438. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  4439. {
  4440. return new CRoxieServerChildGroupAggregateActivity(this, _probeManager);
  4441. }
  4442. };
  4443. IRoxieServerActivityFactory *createRoxieServerNewChildGroupAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4444. {
  4445. return new CRoxieServerChildGroupAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  4446. }
  4447. //=================================================================================
  4448. class CRoxieServerChildThroughNormalizeActivity : public CRoxieServerChildBaseActivity
  4449. {
  4450. IHThorChildThroughNormalizeArg &helper;
  4451. const void * lastInput;
  4452. unsigned numProcessedLastGroup;
  4453. bool ok;
  4454. public:
  4455. CRoxieServerChildThroughNormalizeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4456. : CRoxieServerChildBaseActivity(_factory, _probeManager), helper((IHThorChildThroughNormalizeArg &) basehelper)
  4457. {
  4458. lastInput = NULL;
  4459. numProcessedLastGroup = 0;
  4460. ok = false;
  4461. }
  4462. virtual void stop(bool aborting)
  4463. {
  4464. CRoxieServerChildBaseActivity::stop(aborting);
  4465. ReleaseRoxieRow(lastInput);
  4466. lastInput = NULL;
  4467. }
  4468. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  4469. {
  4470. CRoxieServerChildBaseActivity::start(parentExtractSize, parentExtract, paused);
  4471. numProcessedLastGroup = processed;
  4472. ok = false;
  4473. }
  4474. virtual bool needsAllocator() const { return true; }
  4475. virtual const void *nextInGroup()
  4476. {
  4477. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  4478. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  4479. loop
  4480. {
  4481. if (ok)
  4482. ok = helper.next();
  4483. while (!ok)
  4484. {
  4485. ReleaseRoxieRow(lastInput);
  4486. lastInput = input->nextInGroup();
  4487. if (!lastInput)
  4488. {
  4489. if (numProcessedLastGroup != processed)
  4490. {
  4491. numProcessedLastGroup = processed;
  4492. return NULL;
  4493. }
  4494. lastInput = input->nextInGroup();
  4495. if (!lastInput)
  4496. return NULL;
  4497. }
  4498. ok = helper.first(lastInput);
  4499. }
  4500. try
  4501. {
  4502. do
  4503. {
  4504. unsigned outSize = helper.transform(rowBuilder);
  4505. if (outSize)
  4506. {
  4507. processed++;
  4508. return rowBuilder.finalizeRowClear(outSize);
  4509. }
  4510. ok = helper.next();
  4511. } while (ok);
  4512. }
  4513. catch (IException *E)
  4514. {
  4515. throw makeWrappedException(E);
  4516. }
  4517. }
  4518. }
  4519. };
  4520. class CRoxieServerChildThroughNormalizeActivityFactory : public CRoxieServerActivityFactory
  4521. {
  4522. public:
  4523. CRoxieServerChildThroughNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4524. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  4525. {
  4526. }
  4527. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  4528. {
  4529. return new CRoxieServerChildThroughNormalizeActivity(this, _probeManager);
  4530. }
  4531. };
  4532. IRoxieServerActivityFactory *createRoxieServerNewChildThroughNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4533. {
  4534. return new CRoxieServerChildThroughNormalizeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  4535. }
  4536. //=================================================================================
  4537. class CRoxieServerDistributionActivity : public CRoxieServerInternalSinkActivity
  4538. {
  4539. IHThorDistributionArg &helper;
  4540. public:
  4541. CRoxieServerDistributionActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4542. : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorDistributionArg &)basehelper)
  4543. {
  4544. }
  4545. virtual void onExecute()
  4546. {
  4547. MemoryAttr ma;
  4548. IDistributionTable * * accumulator = (IDistributionTable * *)ma.allocate(helper.queryInternalRecordSize()->getMinRecordSize());
  4549. helper.clearAggregate(accumulator);
  4550. OwnedConstRoxieRow nextrec(input->nextInGroup());
  4551. loop
  4552. {
  4553. if (!nextrec)
  4554. {
  4555. nextrec.setown(input->nextInGroup());
  4556. if (!nextrec)
  4557. break;
  4558. }
  4559. helper.process(accumulator, nextrec);
  4560. nextrec.setown(input->nextInGroup());
  4561. }
  4562. StringBuffer result;
  4563. result.append("<XML>");
  4564. helper.gatherResult(accumulator, result);
  4565. result.append("</XML>");
  4566. helper.sendResult(result.length(), result.str());
  4567. helper.destruct(accumulator);
  4568. }
  4569. };
  4570. class CRoxieServerDistributionActivityFactory : public CRoxieServerActivityFactory
  4571. {
  4572. bool isRoot;
  4573. public:
  4574. CRoxieServerDistributionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  4575. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
  4576. {
  4577. }
  4578. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  4579. {
  4580. return new CRoxieServerDistributionActivity(this, _probeManager);
  4581. }
  4582. virtual bool isSink() const
  4583. {
  4584. return isRoot;
  4585. }
  4586. };
  4587. IRoxieServerActivityFactory *createRoxieServerDistributionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  4588. {
  4589. return new CRoxieServerDistributionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
  4590. }
  4591. //=================================================================================
  4592. class CRoxieServerLinkedRawIteratorActivity : public CRoxieServerActivity
  4593. {
  4594. IHThorLinkedRawIteratorArg &helper;
  4595. public:
  4596. CRoxieServerLinkedRawIteratorActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4597. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorLinkedRawIteratorArg &) basehelper)
  4598. {
  4599. }
  4600. virtual const void *nextInGroup()
  4601. {
  4602. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  4603. const void *ret =helper.next();
  4604. if (ret)
  4605. {
  4606. LinkRoxieRow(ret);
  4607. processed++;
  4608. }
  4609. return ret;
  4610. }
  4611. };
  4612. class CRoxieServerLinkedRawIteratorActivityFactory : public CRoxieServerActivityFactory
  4613. {
  4614. public:
  4615. CRoxieServerLinkedRawIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4616. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  4617. {
  4618. }
  4619. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  4620. {
  4621. return new CRoxieServerLinkedRawIteratorActivity(this, _probeManager);
  4622. }
  4623. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  4624. {
  4625. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for %s activity", getActivityText(kind));
  4626. }
  4627. };
  4628. IRoxieServerActivityFactory *createRoxieServerLinkedRawIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4629. {
  4630. return new CRoxieServerLinkedRawIteratorActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  4631. }
  4632. //=================================================================================
  4633. class CRoxieServerDatasetResultActivity : public CRoxieServerActivity
  4634. {
  4635. public:
  4636. CRoxieServerDatasetResultActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4637. : CRoxieServerActivity(_factory, _probeManager)
  4638. {
  4639. }
  4640. virtual const void *nextInGroup()
  4641. {
  4642. throwUnexpected();
  4643. }
  4644. virtual void executeChild(size32_t & retSize, void * & ret, unsigned parentExtractSize, const byte * parentExtract)
  4645. {
  4646. try
  4647. {
  4648. start(parentExtractSize, parentExtract, false);
  4649. {
  4650. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  4651. MemoryBuffer result;
  4652. IRecordSize * inputMeta = input->queryOutputMeta();
  4653. loop
  4654. {
  4655. const void *nextrec = input->nextInGroup();
  4656. if (!nextrec)
  4657. {
  4658. nextrec = input->nextInGroup();
  4659. if (!nextrec)
  4660. break;
  4661. }
  4662. result.append(inputMeta->getRecordSize(nextrec), nextrec);
  4663. ReleaseRoxieRow(nextrec);
  4664. }
  4665. retSize = result.length();
  4666. ret = result.detach();
  4667. }
  4668. stop(false);
  4669. reset();
  4670. }
  4671. catch(IException *E)
  4672. {
  4673. ctx->notifyAbort(E);
  4674. stop(true);
  4675. reset();
  4676. throw;
  4677. }
  4678. catch(...)
  4679. {
  4680. Owned<IException> E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught at %s:%d", __FILE__, __LINE__);
  4681. ctx->notifyAbort(E);
  4682. stop(true);
  4683. reset();
  4684. throw;
  4685. }
  4686. }
  4687. };
  4688. class CRoxieServerDatasetResultActivityFactory : public CRoxieServerActivityFactory
  4689. {
  4690. bool isRoot;
  4691. public:
  4692. CRoxieServerDatasetResultActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  4693. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
  4694. {
  4695. }
  4696. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  4697. {
  4698. return new CRoxieServerDatasetResultActivity(this, _probeManager);
  4699. }
  4700. virtual bool isSink() const
  4701. {
  4702. return isRoot;
  4703. }
  4704. };
  4705. IRoxieServerActivityFactory *createRoxieServerDatasetResultActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  4706. {
  4707. return new CRoxieServerDatasetResultActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
  4708. }
  4709. //=================================================================================
  4710. class CRoxieServerInlineTableActivity : public CRoxieServerActivity
  4711. {
  4712. IHThorInlineTableArg &helper;
  4713. __uint64 curRow;
  4714. __uint64 numRows;
  4715. public:
  4716. CRoxieServerInlineTableActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4717. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorInlineTableArg &) basehelper)
  4718. {
  4719. curRow = 0;
  4720. numRows = 0;
  4721. }
  4722. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  4723. {
  4724. curRow = 0;
  4725. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  4726. numRows = helper.numRows();
  4727. }
  4728. virtual bool needsAllocator() const { return true; }
  4729. virtual const void *nextInGroup()
  4730. {
  4731. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  4732. // Filtering empty rows, returns the next valid row
  4733. while (curRow < numRows)
  4734. {
  4735. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  4736. unsigned outSize = helper.getRow(rowBuilder, curRow++);
  4737. if (outSize)
  4738. {
  4739. processed++;
  4740. return rowBuilder.finalizeRowClear(outSize);
  4741. }
  4742. }
  4743. return NULL;
  4744. }
  4745. virtual void setInput(unsigned idx, IRoxieInput *_in)
  4746. {
  4747. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  4748. }
  4749. };
  4750. class CRoxieServerInlineTableActivityFactory : public CRoxieServerActivityFactory
  4751. {
  4752. public:
  4753. CRoxieServerInlineTableActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4754. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  4755. {
  4756. }
  4757. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  4758. {
  4759. return new CRoxieServerInlineTableActivity(this, _probeManager);
  4760. }
  4761. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  4762. {
  4763. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for InlineTable activity");
  4764. }
  4765. };
  4766. IRoxieServerActivityFactory *createRoxieServerInlineTableActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4767. {
  4768. return new CRoxieServerInlineTableActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  4769. }
  4770. //=================================================================================
  4771. class CRoxieServerWorkUnitReadActivity : public CRoxieServerActivity
  4772. {
  4773. IHThorWorkunitReadArg &helper;
  4774. Owned<IWorkUnitRowReader> wuReader; // MORE - can we use IRoxieInput instead?
  4775. public:
  4776. CRoxieServerWorkUnitReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4777. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorWorkunitReadArg &)basehelper)
  4778. {
  4779. }
  4780. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  4781. {
  4782. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  4783. if (!ctx->queryServerContext())
  4784. {
  4785. throw MakeStringException(ROXIE_INTERNAL_ERROR, "Workunit read activity cannot be executed in slave context");
  4786. }
  4787. }
  4788. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  4789. {
  4790. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  4791. IXmlToRowTransformer * xmlTransformer = helper.queryXmlTransformer();
  4792. OwnedRoxieString fromWuid(helper.getWUID());
  4793. wuReader.setown(ctx->getWorkunitRowReader(fromWuid, helper.queryName(), helper.querySequence(), xmlTransformer, rowAllocator, meta.isGrouped()));
  4794. // MORE _ should that be in onCreate?
  4795. }
  4796. virtual void reset()
  4797. {
  4798. wuReader.clear();
  4799. CRoxieServerActivity::reset();
  4800. };
  4801. virtual bool needsAllocator() const { return true; }
  4802. virtual const void *nextInGroup()
  4803. {
  4804. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  4805. const void *ret = wuReader->nextInGroup();
  4806. if (ret)
  4807. processed++;
  4808. return ret;
  4809. }
  4810. virtual void setInput(unsigned idx, IRoxieInput *_in)
  4811. {
  4812. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  4813. }
  4814. };
  4815. class CRoxieServerWorkUnitReadActivityFactory : public CRoxieServerActivityFactory
  4816. {
  4817. public:
  4818. CRoxieServerWorkUnitReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4819. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  4820. {
  4821. }
  4822. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  4823. {
  4824. return new CRoxieServerWorkUnitReadActivity(this, _probeManager);
  4825. }
  4826. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  4827. {
  4828. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for WorkUnitRead activity");
  4829. }
  4830. };
  4831. IRoxieServerActivityFactory *createRoxieServerWorkUnitReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  4832. {
  4833. return new CRoxieServerWorkUnitReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  4834. }
  4835. //=================================================================================
  4836. interface ILocalGraphEx : public IEclGraphResults
  4837. {
  4838. public:
  4839. virtual void setResult(unsigned id, IGraphResult * result) = 0;
  4840. virtual IRoxieInput * createResultIterator(unsigned id) = 0;
  4841. virtual void setGraphLoopResult(IGraphResult * result) = 0;
  4842. virtual IRoxieInput * createGraphLoopResultIterator(unsigned id) = 0;
  4843. };
  4844. class CSafeRoxieInput : public CInterface, implements IRoxieInput
  4845. {
  4846. public:
  4847. CSafeRoxieInput(IRoxieInput * _input) : input(_input) {}
  4848. IMPLEMENT_IINTERFACE
  4849. virtual IOutputMetaData * queryOutputMeta() const
  4850. {
  4851. return input->queryOutputMeta();
  4852. }
  4853. virtual unsigned queryId() const
  4854. {
  4855. return input->queryId();
  4856. }
  4857. virtual unsigned __int64 queryTotalCycles() const
  4858. {
  4859. return input->queryTotalCycles();
  4860. }
  4861. virtual unsigned __int64 queryLocalCycles() const
  4862. {
  4863. return input->queryLocalCycles();
  4864. }
  4865. virtual IRoxieInput *queryInput(unsigned idx) const
  4866. {
  4867. return input->queryInput(idx);
  4868. }
  4869. virtual IRoxieServerActivity *queryActivity()
  4870. {
  4871. return input->queryActivity();
  4872. }
  4873. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  4874. {
  4875. return input->queryIndexReadActivity();
  4876. }
  4877. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  4878. {
  4879. CriticalBlock procedure(cs);
  4880. input->start(parentExtractSize, parentExtract, paused);
  4881. }
  4882. virtual void stop(bool aborting)
  4883. {
  4884. CriticalBlock procedure(cs);
  4885. input->stop(aborting);
  4886. }
  4887. virtual void reset()
  4888. {
  4889. CriticalBlock procedure(cs);
  4890. input->reset();
  4891. }
  4892. virtual void resetEOF()
  4893. {
  4894. CriticalBlock procedure(cs);
  4895. input->resetEOF();
  4896. }
  4897. virtual void checkAbort()
  4898. {
  4899. CriticalBlock procedure(cs);
  4900. input->checkAbort();
  4901. }
  4902. virtual const void *nextInGroup()
  4903. {
  4904. CriticalBlock procedure(cs);
  4905. return input->nextInGroup();
  4906. }
  4907. virtual bool nextGroup(ConstPointerArray & group)
  4908. {
  4909. CriticalBlock procedure(cs);
  4910. return input->nextGroup(group);
  4911. }
  4912. private:
  4913. CriticalSection cs;
  4914. Linked<IRoxieInput> input;
  4915. };
  4916. //=================================================================================
  4917. class CPseudoRoxieInput : public CInterface, implements IRoxieInput
  4918. {
  4919. protected:
  4920. unsigned __int64 totalCycles;
  4921. public:
  4922. IMPLEMENT_IINTERFACE;
  4923. CPseudoRoxieInput()
  4924. {
  4925. totalCycles = 0;
  4926. }
  4927. virtual unsigned __int64 queryTotalCycles() const
  4928. {
  4929. return totalCycles;
  4930. }
  4931. virtual unsigned __int64 queryLocalCycles() const
  4932. {
  4933. return totalCycles;
  4934. }
  4935. virtual IRoxieInput *queryInput(unsigned idx) const
  4936. {
  4937. return NULL;
  4938. }
  4939. virtual IRoxieServerActivity *queryActivity()
  4940. {
  4941. throwUnexpected();
  4942. }
  4943. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  4944. {
  4945. throwUnexpected();
  4946. }
  4947. virtual IOutputMetaData * queryOutputMeta() const { throwUnexpected(); }
  4948. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused) { }
  4949. virtual void stop(bool aborting) { }
  4950. virtual void reset() { totalCycles = 0; }
  4951. virtual void checkAbort() { }
  4952. virtual unsigned queryId() const { throwUnexpected(); }
  4953. virtual void resetEOF() { }
  4954. };
  4955. class CIndirectRoxieInput : public CPseudoRoxieInput
  4956. {
  4957. public:
  4958. CIndirectRoxieInput(IRoxieInput * _input = NULL) : input(_input)
  4959. {
  4960. }
  4961. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  4962. {
  4963. input->start(parentExtractSize, parentExtract, paused);
  4964. }
  4965. virtual void stop(bool aborting)
  4966. {
  4967. input->stop(aborting);
  4968. }
  4969. virtual void reset()
  4970. {
  4971. input->reset();
  4972. totalCycles = 0;
  4973. }
  4974. virtual void checkAbort()
  4975. {
  4976. input->checkAbort();
  4977. }
  4978. virtual const void * nextInGroup()
  4979. {
  4980. return input->nextInGroup();
  4981. }
  4982. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  4983. {
  4984. return input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  4985. }
  4986. virtual unsigned __int64 queryLocalCycles() const
  4987. {
  4988. __int64 ret = totalCycles - input->queryTotalCycles();
  4989. if (ret < 0)
  4990. ret = 0;
  4991. return ret;
  4992. }
  4993. virtual IRoxieInput *queryInput(unsigned idx) const
  4994. {
  4995. return input->queryInput(idx);
  4996. }
  4997. virtual unsigned queryId() const
  4998. {
  4999. return input->queryId();
  5000. }
  5001. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  5002. {
  5003. return input->gatherConjunctions(collector);
  5004. }
  5005. virtual void resetEOF()
  5006. {
  5007. input->resetEOF();
  5008. }
  5009. virtual unsigned numConcreteOutputs() const
  5010. {
  5011. return input->numConcreteOutputs();
  5012. }
  5013. virtual IRoxieInput * queryConcreteInput(unsigned idx)
  5014. {
  5015. return input->queryConcreteInput(idx);
  5016. }
  5017. virtual IOutputMetaData * queryOutputMeta() const
  5018. {
  5019. return input->queryOutputMeta();
  5020. }
  5021. virtual IRoxieServerActivity *queryActivity()
  5022. {
  5023. return input->queryActivity();
  5024. }
  5025. void setInput(IRoxieInput * _input)
  5026. {
  5027. input = _input;
  5028. }
  5029. protected:
  5030. IRoxieInput * input;
  5031. };
  5032. class CExtractMapperInput : public CIndirectRoxieInput
  5033. {
  5034. unsigned savedParentExtractSize;
  5035. const byte * savedParentExtract;
  5036. public:
  5037. CExtractMapperInput(IRoxieInput * _input = NULL) : CIndirectRoxieInput(_input)
  5038. {
  5039. savedParentExtractSize = 0;
  5040. savedParentExtract = NULL;
  5041. }
  5042. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5043. {
  5044. input->start(savedParentExtractSize, savedParentExtract, paused);
  5045. }
  5046. void setParentExtract(unsigned _savedParentExtractSize, const byte * _savedParentExtract)
  5047. {
  5048. savedParentExtractSize = _savedParentExtractSize;
  5049. savedParentExtract = _savedParentExtract;
  5050. }
  5051. };
  5052. class CGraphResult : public CInterface, implements IGraphResult
  5053. {
  5054. CriticalSection cs;
  5055. byte **rowset;
  5056. size32_t count;
  5057. bool complete;
  5058. void clear()
  5059. {
  5060. CriticalBlock func(cs);
  5061. rtlReleaseRowset(count, rowset);
  5062. rowset = NULL;
  5063. count = 0;
  5064. complete = false;
  5065. }
  5066. public:
  5067. IMPLEMENT_IINTERFACE
  5068. CGraphResult()
  5069. {
  5070. complete = false; // dummy result is not supposed to be used...
  5071. rowset = NULL;
  5072. count = 0;
  5073. }
  5074. CGraphResult(size32_t _count, byte **_rowset)
  5075. : count(_count), rowset(_rowset)
  5076. {
  5077. complete = true;
  5078. }
  5079. ~CGraphResult()
  5080. {
  5081. clear();
  5082. }
  5083. // interface IGraphResult
  5084. virtual IRoxieInput * createIterator()
  5085. {
  5086. if (!complete)
  5087. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Internal Error: Reading uninitialised graph result");
  5088. return new CGraphResultIterator(this);
  5089. }
  5090. virtual void getLinkedResult(unsigned & countResult, byte * * & result)
  5091. {
  5092. if (!complete)
  5093. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Internal Error: Reading uninitialised graph result");
  5094. result = rtlLinkRowset(rowset);
  5095. countResult = count;
  5096. }
  5097. virtual const void * getLinkedRowResult()
  5098. {
  5099. if (!complete)
  5100. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Internal Error: Reading uninitialised graph result");
  5101. if (count != 1)
  5102. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Internal Error: Expected a single row result");
  5103. const void * ret = rowset[0];
  5104. LinkRoxieRow(ret);
  5105. return ret;
  5106. }
  5107. //other
  5108. const void * getRow(unsigned i)
  5109. {
  5110. CriticalBlock func(cs);
  5111. if (i >= count)
  5112. return NULL;
  5113. const void * ret = rowset[i];
  5114. if (ret) LinkRoxieRow(ret);
  5115. return ret;
  5116. }
  5117. protected:
  5118. class CGraphResultIterator : public CPseudoRoxieInput
  5119. {
  5120. unsigned i;
  5121. Linked<CGraphResult> result;
  5122. public:
  5123. CGraphResultIterator(CGraphResult * _result) : result(_result) { i = 0; }
  5124. IMPLEMENT_IINTERFACE
  5125. public:
  5126. virtual const void * nextInGroup()
  5127. {
  5128. return result->getRow(i++);
  5129. }
  5130. };
  5131. };
  5132. //=================================================================================
  5133. class CRoxieServerLocalResultReadActivity : public CRoxieServerActivity
  5134. {
  5135. IHThorLocalResultReadArg &helper;
  5136. Owned<IRoxieInput> iter;
  5137. ILocalGraphEx * graph;
  5138. unsigned graphId;
  5139. unsigned sequence;
  5140. public:
  5141. CRoxieServerLocalResultReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
  5142. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorLocalResultReadArg &)basehelper), graphId(_graphId)
  5143. {
  5144. graph = NULL;
  5145. sequence = 0;
  5146. }
  5147. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  5148. {
  5149. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  5150. graph = static_cast<ILocalGraphEx *>(_ctx->queryCodeContext()->resolveLocalQuery(graphId));
  5151. sequence = helper.querySequence();
  5152. }
  5153. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5154. {
  5155. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  5156. iter.setown(graph->createResultIterator(sequence));
  5157. }
  5158. virtual void reset()
  5159. {
  5160. iter.clear();
  5161. CRoxieServerActivity::reset();
  5162. };
  5163. virtual const void *nextInGroup()
  5164. {
  5165. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  5166. const void * next = iter->nextInGroup();
  5167. if (next)
  5168. {
  5169. processed++;
  5170. atomic_inc(&rowsIn);
  5171. }
  5172. return next;
  5173. }
  5174. virtual void setInput(unsigned idx, IRoxieInput *_in)
  5175. {
  5176. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  5177. }
  5178. };
  5179. class CRoxieServerLocalResultReadActivityFactory : public CRoxieServerActivityFactory
  5180. {
  5181. unsigned graphId;
  5182. public:
  5183. CRoxieServerLocalResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _graphId)
  5184. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), graphId(_graphId)
  5185. {
  5186. }
  5187. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  5188. {
  5189. return new CRoxieServerLocalResultReadActivity(this, _probeManager, graphId);
  5190. }
  5191. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  5192. {
  5193. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for LocalResultRead activity");
  5194. }
  5195. };
  5196. IRoxieServerActivityFactory *createRoxieServerLocalResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned graphId)
  5197. {
  5198. return new CRoxieServerLocalResultReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, graphId);
  5199. }
  5200. //=================================================================================
  5201. class CRoxieServerLocalResultStreamReadActivity : public CRoxieServerActivity
  5202. {
  5203. IHThorLocalResultReadArg &helper;
  5204. Owned<IRoxieInput> streamInput;
  5205. unsigned sequence;
  5206. public:
  5207. CRoxieServerLocalResultStreamReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  5208. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorLocalResultReadArg &)basehelper)
  5209. {
  5210. sequence = 0;
  5211. }
  5212. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  5213. {
  5214. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  5215. sequence = helper.querySequence();
  5216. }
  5217. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5218. {
  5219. assertex(streamInput != NULL);
  5220. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  5221. streamInput->start(parentExtractSize, parentExtract, paused);
  5222. }
  5223. virtual void stop(bool aborting)
  5224. {
  5225. CRoxieServerActivity::stop(aborting);
  5226. streamInput->stop(aborting);
  5227. }
  5228. virtual void reset()
  5229. {
  5230. streamInput->reset();
  5231. CRoxieServerActivity::reset();
  5232. };
  5233. virtual const void *nextInGroup()
  5234. {
  5235. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  5236. const void * next = streamInput->nextInGroup();
  5237. if (next)
  5238. {
  5239. processed++;
  5240. atomic_inc(&rowsIn);
  5241. }
  5242. return next;
  5243. }
  5244. virtual bool querySetStreamInput(unsigned id, IRoxieInput * _input)
  5245. {
  5246. if (id == sequence)
  5247. {
  5248. streamInput.set(_input);
  5249. return true;
  5250. }
  5251. return false;
  5252. }
  5253. virtual void setInput(unsigned idx, IRoxieInput *_in)
  5254. {
  5255. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  5256. }
  5257. };
  5258. class CRoxieServerLocalResultStreamReadActivityFactory : public CRoxieServerActivityFactory
  5259. {
  5260. public:
  5261. CRoxieServerLocalResultStreamReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  5262. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  5263. {
  5264. }
  5265. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  5266. {
  5267. return new CRoxieServerLocalResultStreamReadActivity(this, _probeManager);
  5268. }
  5269. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  5270. {
  5271. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for LocalResultRead activity");
  5272. }
  5273. };
  5274. IRoxieServerActivityFactory *createRoxieServerLocalResultStreamReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  5275. {
  5276. return new CRoxieServerLocalResultStreamReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  5277. }
  5278. //=====================================================================================================
  5279. class CRoxieServerLocalResultWriteActivity : public CRoxieServerInternalSinkActivity
  5280. {
  5281. IHThorLocalResultWriteArg &helper;
  5282. ILocalGraphEx * graph;
  5283. unsigned graphId;
  5284. public:
  5285. CRoxieServerLocalResultWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
  5286. : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorLocalResultWriteArg &)basehelper), graphId(_graphId)
  5287. {
  5288. graph = NULL;
  5289. }
  5290. virtual bool needsAllocator() const { return true; }
  5291. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  5292. {
  5293. CRoxieServerInternalSinkActivity::onCreate(_ctx, _colocalParent);
  5294. graph = static_cast<ILocalGraphEx *>(_ctx->queryCodeContext()->resolveLocalQuery(graphId));
  5295. }
  5296. virtual void onExecute()
  5297. {
  5298. RtlLinkedDatasetBuilder builder(rowAllocator);
  5299. input->readAll(builder);
  5300. Owned<CGraphResult> result = new CGraphResult(builder.getcount(), builder.linkrows());
  5301. graph->setResult(helper.querySequence(), result);
  5302. }
  5303. IRoxieInput * querySelectOutput(unsigned id)
  5304. {
  5305. if (id == helper.querySequence())
  5306. {
  5307. executed = true;
  5308. return LINK(input);
  5309. }
  5310. return NULL;
  5311. }
  5312. };
  5313. class CRoxieServerLocalResultWriteActivityFactory : public CRoxieServerInternalSinkFactory
  5314. {
  5315. unsigned graphId;
  5316. public:
  5317. CRoxieServerLocalResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, unsigned _graphId, bool _isRoot)
  5318. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot), graphId(_graphId)
  5319. {
  5320. isInternal = true;
  5321. Owned<IHThorLocalResultWriteArg> helper = (IHThorLocalResultWriteArg *) helperFactory();
  5322. }
  5323. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  5324. {
  5325. return new CRoxieServerLocalResultWriteActivity(this, _probeManager, graphId);
  5326. }
  5327. };
  5328. IRoxieServerActivityFactory *createRoxieServerLocalResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, unsigned _graphId, bool _isRoot)
  5329. {
  5330. return new CRoxieServerLocalResultWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _graphId, _isRoot);
  5331. }
  5332. //=====================================================================================================
  5333. class CRoxieServerDictionaryResultWriteActivity : public CRoxieServerInternalSinkActivity
  5334. {
  5335. IHThorDictionaryResultWriteArg &helper;
  5336. ILocalGraphEx * graph;
  5337. unsigned graphId;
  5338. public:
  5339. CRoxieServerDictionaryResultWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
  5340. : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorDictionaryResultWriteArg &)basehelper), graphId(_graphId)
  5341. {
  5342. graph = NULL;
  5343. }
  5344. virtual bool needsAllocator() const { return true; }
  5345. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  5346. {
  5347. CRoxieServerInternalSinkActivity::onCreate(_ctx, _colocalParent);
  5348. graph = static_cast<ILocalGraphEx *>(_ctx->queryCodeContext()->resolveLocalQuery(graphId));
  5349. }
  5350. virtual void onExecute()
  5351. {
  5352. unsigned sequence = helper.querySequence();
  5353. RtlLinkedDictionaryBuilder builder(rowAllocator, helper.queryHashLookupInfo());
  5354. loop
  5355. {
  5356. const void *row = input->nextInGroup();
  5357. if (!row)
  5358. {
  5359. row = input->nextInGroup();
  5360. if (!row)
  5361. break;
  5362. }
  5363. builder.appendOwn(row);
  5364. processed++;
  5365. }
  5366. Owned<CGraphResult> result = new CGraphResult(builder.getcount(), builder.linkrows());
  5367. graph->setResult(helper.querySequence(), result);
  5368. }
  5369. IRoxieInput * querySelectOutput(unsigned id)
  5370. {
  5371. if (id == helper.querySequence())
  5372. {
  5373. executed = true;
  5374. return LINK(input);
  5375. }
  5376. return NULL;
  5377. }
  5378. };
  5379. class CRoxieServerDictionaryResultWriteActivityFactory : public CRoxieServerInternalSinkFactory
  5380. {
  5381. unsigned graphId;
  5382. public:
  5383. CRoxieServerDictionaryResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, unsigned _graphId, bool _isRoot)
  5384. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot), graphId(_graphId)
  5385. {
  5386. isInternal = true;
  5387. }
  5388. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  5389. {
  5390. return new CRoxieServerDictionaryResultWriteActivity(this, _probeManager, graphId);
  5391. }
  5392. };
  5393. IRoxieServerActivityFactory *createRoxieServerDictionaryResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, unsigned _graphId, bool _isRoot)
  5394. {
  5395. return new CRoxieServerDictionaryResultWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _graphId, _isRoot);
  5396. }
  5397. //=================================================================================
  5398. class CRoxieServerGraphLoopResultReadActivity : public CRoxieServerActivity
  5399. {
  5400. protected:
  5401. IHThorGraphLoopResultReadArg &helper;
  5402. Owned<IRoxieInput> iter;
  5403. ILocalGraphEx * graph;
  5404. unsigned graphId;
  5405. unsigned sequence;
  5406. public:
  5407. CRoxieServerGraphLoopResultReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
  5408. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorGraphLoopResultReadArg &)basehelper), graphId(_graphId)
  5409. {
  5410. graph = NULL;
  5411. sequence = 0;
  5412. }
  5413. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  5414. {
  5415. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  5416. graph = static_cast<ILocalGraphEx *>(_ctx->queryCodeContext()->resolveLocalQuery(graphId));
  5417. }
  5418. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5419. {
  5420. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  5421. if (iter)
  5422. iter->start(parentExtractSize, parentExtract, paused);
  5423. else
  5424. {
  5425. sequence = helper.querySequence();
  5426. if ((int)sequence >= 0)
  5427. {
  5428. try
  5429. {
  5430. iter.setown(graph->createGraphLoopResultIterator(sequence));
  5431. }
  5432. catch (IException * E)
  5433. {
  5434. throw makeWrappedException(E);
  5435. }
  5436. }
  5437. }
  5438. }
  5439. virtual void stop(bool aborting)
  5440. {
  5441. if (iter)
  5442. iter->stop(aborting);
  5443. CRoxieServerActivity::stop(aborting);
  5444. }
  5445. virtual void reset()
  5446. {
  5447. if (iter)
  5448. iter->reset();
  5449. iter.clear();
  5450. CRoxieServerActivity::reset();
  5451. };
  5452. virtual const void *nextInGroup()
  5453. {
  5454. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  5455. const void * next = iter ? iter->nextInGroup() : NULL;
  5456. if (next)
  5457. {
  5458. processed++;
  5459. atomic_inc(&rowsIn);
  5460. }
  5461. return next;
  5462. }
  5463. virtual void setInput(unsigned idx, IRoxieInput *_in)
  5464. {
  5465. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  5466. }
  5467. virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract)
  5468. {
  5469. ensureCreated();
  5470. basehelper.onStart(parentExtract, NULL);
  5471. processor.noteUseIteration(helper.querySequence());
  5472. }
  5473. virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract, IProbeManager *probeManager, IArrayOf<IRoxieInput> &probes)
  5474. {
  5475. //helper already initialised from the gratherIterationUsage() call.
  5476. iter.set(processor.connectIterationOutput(helper.querySequence(), probeManager, probes, this, 0));
  5477. }
  5478. virtual IInputSteppingMeta * querySteppingMeta()
  5479. {
  5480. assertex(iter);
  5481. return iter->querySteppingMeta();
  5482. }
  5483. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  5484. {
  5485. assertex(iter);
  5486. return iter->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  5487. }
  5488. };
  5489. //variety of CRoxieServerGraphLoopResultReadActivity created internally with a predefined sequence number
  5490. class CRoxieServerInternalGraphLoopResultReadActivity : public CRoxieServerGraphLoopResultReadActivity
  5491. {
  5492. public:
  5493. CRoxieServerInternalGraphLoopResultReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId, unsigned _sequence)
  5494. : CRoxieServerGraphLoopResultReadActivity(_factory, _probeManager, _graphId)
  5495. {
  5496. sequence = _sequence;
  5497. }
  5498. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5499. {
  5500. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  5501. if ((int)sequence >= 0)
  5502. {
  5503. try
  5504. {
  5505. iter.setown(graph->createGraphLoopResultIterator(sequence));
  5506. }
  5507. catch (IException * E)
  5508. {
  5509. throw makeWrappedException(E);
  5510. }
  5511. }
  5512. }
  5513. };
  5514. class CRoxieServerGraphLoopResultReadActivityFactory : public CRoxieServerActivityFactory
  5515. {
  5516. unsigned graphId;
  5517. public:
  5518. CRoxieServerGraphLoopResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _graphId)
  5519. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), graphId(_graphId)
  5520. {
  5521. }
  5522. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  5523. {
  5524. return new CRoxieServerGraphLoopResultReadActivity(this, _probeManager, graphId);
  5525. }
  5526. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  5527. {
  5528. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for GraphLoopResultRead activity");
  5529. }
  5530. };
  5531. IRoxieServerActivityFactory *createRoxieServerGraphLoopResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned graphId)
  5532. {
  5533. return new CRoxieServerGraphLoopResultReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, graphId);
  5534. }
  5535. //=====================================================================================================
  5536. class CRoxieServerGraphLoopResultWriteActivity : public CRoxieServerInternalSinkActivity
  5537. {
  5538. IHThorGraphLoopResultWriteArg &helper;
  5539. ILocalGraphEx * graph;
  5540. unsigned graphId;
  5541. public:
  5542. CRoxieServerGraphLoopResultWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
  5543. : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorGraphLoopResultWriteArg &)basehelper), graphId(_graphId)
  5544. {
  5545. graph = NULL;
  5546. }
  5547. virtual bool needsAllocator() const { return true; }
  5548. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  5549. {
  5550. CRoxieServerInternalSinkActivity::onCreate(_ctx, _colocalParent);
  5551. graph = static_cast<ILocalGraphEx *>(_ctx->queryCodeContext()->resolveLocalQuery(graphId));
  5552. }
  5553. virtual void onExecute()
  5554. {
  5555. RtlLinkedDatasetBuilder builder(rowAllocator);
  5556. input->readAll(builder);
  5557. Owned<CGraphResult> result = new CGraphResult(builder.getcount(), builder.linkrows());
  5558. graph->setGraphLoopResult(result);
  5559. }
  5560. virtual IRoxieInput *queryOutput(unsigned idx)
  5561. {
  5562. if (idx==0)
  5563. return this;
  5564. else
  5565. return NULL;
  5566. }
  5567. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  5568. {
  5569. return input->gatherConjunctions(collector);
  5570. }
  5571. virtual void resetEOF()
  5572. {
  5573. input->resetEOF();
  5574. }
  5575. virtual const void *nextInGroup()
  5576. {
  5577. const void * next = input->nextInGroup();
  5578. if (next)
  5579. processed++;
  5580. return next;
  5581. }
  5582. virtual bool isPassThrough()
  5583. {
  5584. return true;
  5585. }
  5586. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  5587. {
  5588. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  5589. const void * next = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  5590. if (next)
  5591. processed++;
  5592. return next;
  5593. }
  5594. IInputSteppingMeta * querySteppingMeta()
  5595. {
  5596. return input->querySteppingMeta();
  5597. }
  5598. virtual IOutputMetaData * queryOutputMeta() const
  5599. {
  5600. return input->queryOutputMeta();
  5601. }
  5602. };
  5603. class CRoxieServerGraphLoopResultWriteActivityFactory : public CRoxieServerInternalSinkFactory
  5604. {
  5605. unsigned graphId;
  5606. public:
  5607. CRoxieServerGraphLoopResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, unsigned _graphId)
  5608. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, true), graphId(_graphId)
  5609. {
  5610. isInternal = true;
  5611. Owned<IHThorGraphLoopResultWriteArg> helper = (IHThorGraphLoopResultWriteArg *) helperFactory();
  5612. }
  5613. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  5614. {
  5615. return new CRoxieServerGraphLoopResultWriteActivity(this, _probeManager, graphId);
  5616. }
  5617. };
  5618. IRoxieServerActivityFactory *createRoxieServerGraphLoopResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, unsigned _graphId)
  5619. {
  5620. return new CRoxieServerGraphLoopResultWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _graphId);
  5621. }
  5622. #if 0
  5623. //=====================================================================================================
  5624. CHThorLocalResultSpillActivity::CHThorLocalResultSpillActivity(IAgentContext &_agent, ActivityId const & _id, IHThorLocalResultSpillArg &_arg)
  5625. : CHThorSimpleActivityBase(_agent, _id, _arg), helper(_arg)
  5626. {
  5627. next = NULL;
  5628. }
  5629. void CHThorLocalResultSpillActivity::ready()
  5630. {
  5631. CHThorSimpleActivityBase::ready();
  5632. next = input->nextInGroup();
  5633. grouped = input->isGrouped();
  5634. rowdata.clear();
  5635. }
  5636. const void * CHThorLocalResultSpillActivity::nextInGroup()
  5637. {
  5638. const void * ret = next;
  5639. next = input->nextInGroup();
  5640. if (!ret && !next)
  5641. return NULL;
  5642. if (ret)
  5643. {
  5644. size32_t thisSize = outputMeta->getRecordSize(ret);
  5645. rowdata.append(thisSize, ret);
  5646. if (grouped)
  5647. rowdata.append(next == NULL);
  5648. }
  5649. return ret;
  5650. }
  5651. void CHThorLocalResultSpillActivity::done()
  5652. {
  5653. loop
  5654. {
  5655. const void * ret = nextInGroup();
  5656. if (!ret)
  5657. {
  5658. ret = nextInGroup();
  5659. if (!ret)
  5660. break;
  5661. }
  5662. ReleaseRoxieRow(ret);
  5663. }
  5664. agent.setLocalResult(helper.querySequence(), rowdata.length(), rowdata.toByteArray());
  5665. CHThorSimpleActivityBase::done();
  5666. }
  5667. #endif
  5668. //=================================================================================
  5669. class CRoxieServerDedupActivity : public CRoxieServerActivity
  5670. {
  5671. protected:
  5672. IHThorDedupArg &helper;
  5673. unsigned numKept;
  5674. unsigned numToKeep;
  5675. public:
  5676. CRoxieServerDedupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  5677. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorDedupArg &)basehelper)
  5678. {
  5679. numKept = 0;
  5680. numToKeep = 0;
  5681. }
  5682. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5683. {
  5684. numKept = 0;
  5685. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  5686. numToKeep = helper.numToKeep();
  5687. }
  5688. };
  5689. class CRoxieServerDedupKeepLeftActivity : public CRoxieServerDedupActivity
  5690. {
  5691. IRangeCompare * stepCompare;
  5692. const void *prev;
  5693. public:
  5694. CRoxieServerDedupKeepLeftActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  5695. : CRoxieServerDedupActivity(_factory, _probeManager)
  5696. {
  5697. prev = NULL;
  5698. stepCompare = NULL;
  5699. }
  5700. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5701. {
  5702. prev = NULL;
  5703. CRoxieServerDedupActivity::start(parentExtractSize, parentExtract, paused);
  5704. IInputSteppingMeta * stepMeta = input->querySteppingMeta();
  5705. stepCompare = NULL;
  5706. if (stepMeta)
  5707. stepCompare = stepMeta->queryCompare();
  5708. }
  5709. virtual void reset()
  5710. {
  5711. ReleaseClearRoxieRow(prev);
  5712. CRoxieServerDedupActivity::reset();
  5713. }
  5714. virtual const void * nextInGroup()
  5715. {
  5716. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  5717. const void * next;
  5718. loop
  5719. {
  5720. next = input->nextInGroup();
  5721. if (!prev || !next || !helper.matches(prev,next))
  5722. {
  5723. numKept = 0;
  5724. break;
  5725. }
  5726. if (numKept < numToKeep-1)
  5727. {
  5728. numKept++;
  5729. break;
  5730. }
  5731. ReleaseRoxieRow(next);
  5732. }
  5733. ReleaseRoxieRow(prev);
  5734. prev = next;
  5735. if (next)
  5736. {
  5737. LinkRoxieRow(next);
  5738. processed++;
  5739. }
  5740. return next;
  5741. }
  5742. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  5743. {
  5744. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  5745. const void * next;
  5746. loop
  5747. {
  5748. next = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  5749. //If the record was an in-exact match from the index then return it immediately
  5750. //and don't cause it to dedup following legal records.
  5751. if (!wasCompleteMatch)
  5752. {
  5753. assertex(stepExtra.returnMismatches());
  5754. return next;
  5755. }
  5756. if (!prev || !next || !helper.matches(prev,next))
  5757. {
  5758. numKept = 0;
  5759. break;
  5760. }
  5761. if (numKept < numToKeep-1)
  5762. {
  5763. numKept++;
  5764. break;
  5765. }
  5766. //Unusual - deduping by x,y stepped on x,y,z - still want any record back as soon as possible.
  5767. if (stepExtra.returnMismatches())
  5768. {
  5769. //If asked to return mismatches we are only interested in mismatches that will force the stepped
  5770. //condition to advance
  5771. if (stepCompare->docompare(next, seek, numFields) != 0)
  5772. {
  5773. wasCompleteMatch = false;
  5774. break;
  5775. }
  5776. }
  5777. ReleaseRoxieRow(next);
  5778. }
  5779. ReleaseRoxieRow(prev);
  5780. prev = next;
  5781. if (next)
  5782. {
  5783. LinkRoxieRow(next);
  5784. processed++;
  5785. }
  5786. return next;
  5787. }
  5788. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  5789. {
  5790. return input->gatherConjunctions(collector);
  5791. }
  5792. virtual void resetEOF()
  5793. {
  5794. input->resetEOF();
  5795. }
  5796. IInputSteppingMeta * querySteppingMeta()
  5797. {
  5798. return input->querySteppingMeta();
  5799. }
  5800. };
  5801. //=================================================================================
  5802. class CRoxieServerDedupKeepRightActivity : public CRoxieServerDedupActivity
  5803. {
  5804. const void *kept;
  5805. bool first;
  5806. public:
  5807. CRoxieServerDedupKeepRightActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  5808. : CRoxieServerDedupActivity(_factory, _probeManager)
  5809. {
  5810. kept = NULL;
  5811. first = true;
  5812. }
  5813. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5814. {
  5815. first = true;
  5816. kept = NULL;
  5817. CRoxieServerDedupActivity::start(parentExtractSize, parentExtract, paused);
  5818. }
  5819. virtual void reset()
  5820. {
  5821. ReleaseClearRoxieRow(kept);
  5822. CRoxieServerActivity::reset();
  5823. }
  5824. virtual const void * nextInGroup()
  5825. {
  5826. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  5827. if (first)
  5828. {
  5829. kept = input->nextInGroup();
  5830. first = false;
  5831. }
  5832. const void * next;
  5833. loop
  5834. {
  5835. next = input->nextInGroup();
  5836. if (!kept || !next || !helper.matches(kept,next))
  5837. {
  5838. numKept = 0;
  5839. break;
  5840. }
  5841. if (numKept < numToKeep-1)
  5842. {
  5843. numKept++;
  5844. break;
  5845. }
  5846. ReleaseRoxieRow(kept);
  5847. kept = next;
  5848. }
  5849. const void * ret = kept;
  5850. kept = next;
  5851. // CTXLOG("dedup returns %p", ret);
  5852. if (ret) processed++;
  5853. return ret;
  5854. }
  5855. };
  5856. class CRoxieServerDedupAllActivity : public CRoxieServerActivity
  5857. {
  5858. IHThorDedupArg &helper;
  5859. unsigned survivorIndex;
  5860. ConstPointerArray survivors;
  5861. bool keepLeft;
  5862. bool eof;
  5863. bool first;
  5864. ICompare *primaryCompare;
  5865. public:
  5866. CRoxieServerDedupAllActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _keepLeft)
  5867. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorDedupArg &)basehelper)
  5868. {
  5869. keepLeft = _keepLeft;
  5870. primaryCompare = helper.queryComparePrimary();
  5871. eof = false;
  5872. first = true;
  5873. survivorIndex = 0;
  5874. }
  5875. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5876. {
  5877. eof = false;
  5878. first = true;
  5879. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  5880. }
  5881. virtual void reset()
  5882. {
  5883. #ifdef _DEBUG
  5884. while (survivors.isItem(survivorIndex))
  5885. {
  5886. ReleaseRoxieRow(survivors.item(survivorIndex++));
  5887. }
  5888. #endif
  5889. survivors.kill();
  5890. eof = false;
  5891. first = true;
  5892. CRoxieServerActivity::reset();
  5893. }
  5894. void dedupRange(unsigned first, unsigned last, ConstPointerArray & group)
  5895. {
  5896. for (unsigned idxL = first; idxL < last; idxL++)
  5897. {
  5898. const void * left = group.item(idxL);
  5899. if (left)
  5900. {
  5901. for (unsigned idxR = first; idxR < last; idxR++)
  5902. {
  5903. const void * right = group.item(idxR);
  5904. if ((idxL != idxR) && right)
  5905. {
  5906. if (helper.matches(left, right))
  5907. {
  5908. if (keepLeft)
  5909. {
  5910. group.replace(NULL, idxR);
  5911. ReleaseRoxieRow(right);
  5912. }
  5913. else
  5914. {
  5915. group.replace(NULL, idxL);
  5916. ReleaseRoxieRow(left);
  5917. break;
  5918. }
  5919. }
  5920. }
  5921. }
  5922. }
  5923. }
  5924. }
  5925. void dedupRangeIndirect(unsigned first, unsigned last, void *** index)
  5926. {
  5927. for (unsigned idxL = first; idxL < last; idxL++)
  5928. {
  5929. void * left = *(index[idxL]);
  5930. if (left)
  5931. {
  5932. for (unsigned idxR = first; idxR < last; idxR++)
  5933. {
  5934. void * right = *(index[idxR]);
  5935. if ((idxL != idxR) && right)
  5936. {
  5937. if (helper.matches(left, right))
  5938. {
  5939. if (keepLeft)
  5940. {
  5941. *(index[idxR]) = NULL;
  5942. ReleaseRoxieRow(right);
  5943. }
  5944. else
  5945. {
  5946. *(index[idxL]) = NULL;
  5947. ReleaseRoxieRow(left);
  5948. break;
  5949. }
  5950. }
  5951. }
  5952. }
  5953. }
  5954. }
  5955. }
  5956. bool calcNextDedupAll()
  5957. {
  5958. survivors.kill();
  5959. survivorIndex = 0;
  5960. ConstPointerArray group;
  5961. if (eof || !input->nextGroup(group))
  5962. {
  5963. eof = true;
  5964. return false;
  5965. }
  5966. unsigned max = group.ordinality();
  5967. if (primaryCompare)
  5968. {
  5969. //hard, if not impossible, to hit this code once optimisations in place
  5970. MemoryAttr indexbuff(max*sizeof(void **));
  5971. void *** index = (void ***)indexbuff.bufferBase();
  5972. qsortvecstable(const_cast<void * *>(group.getArray()), max, *primaryCompare, index);
  5973. unsigned first = 0;
  5974. for (unsigned idx = 1; idx < max; idx++)
  5975. {
  5976. if (primaryCompare->docompare(*(index[first]), *(index[idx])) != 0)
  5977. {
  5978. dedupRangeIndirect(first, idx, index);
  5979. first = idx;
  5980. }
  5981. }
  5982. dedupRangeIndirect(first, max, index);
  5983. for(unsigned idx2=0; idx2<max; ++idx2)
  5984. {
  5985. void * cur = *(index[idx2]);
  5986. if(cur)
  5987. survivors.append(cur);
  5988. }
  5989. }
  5990. else
  5991. {
  5992. dedupRange(0, max, group);
  5993. for(unsigned idx=0; idx<max; ++idx)
  5994. {
  5995. const void * cur = group.item(idx);
  5996. if(cur)
  5997. survivors.append(cur);
  5998. }
  5999. }
  6000. return true;
  6001. }
  6002. virtual const void *nextInGroup()
  6003. {
  6004. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  6005. if (first)
  6006. {
  6007. calcNextDedupAll();
  6008. first = false;
  6009. }
  6010. while (survivors.isItem(survivorIndex))
  6011. {
  6012. const void *ret = survivors.item(survivorIndex++);
  6013. if (ret)
  6014. {
  6015. processed++;
  6016. return ret;
  6017. }
  6018. }
  6019. calcNextDedupAll();
  6020. return NULL;
  6021. }
  6022. };
  6023. class CRoxieServerDedupActivityFactory : public CRoxieServerActivityFactory
  6024. {
  6025. bool compareAll;
  6026. bool keepLeft;
  6027. public:
  6028. CRoxieServerDedupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  6029. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  6030. {
  6031. Owned<IHThorDedupArg> helper = (IHThorDedupArg *) helperFactory();
  6032. compareAll = helper->compareAll();
  6033. keepLeft = helper->keepLeft();
  6034. }
  6035. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  6036. {
  6037. if (compareAll)
  6038. return new CRoxieServerDedupAllActivity(this, _probeManager, keepLeft);
  6039. else if (keepLeft)
  6040. return new CRoxieServerDedupKeepLeftActivity(this, _probeManager);
  6041. else
  6042. return new CRoxieServerDedupKeepRightActivity(this, _probeManager);
  6043. }
  6044. };
  6045. IRoxieServerActivityFactory *createRoxieServerDedupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  6046. {
  6047. return new CRoxieServerDedupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  6048. }
  6049. //=================================================================================
  6050. class CRoxieServerHashDedupActivity : public CRoxieServerActivity
  6051. {
  6052. bool eof;
  6053. IHThorHashDedupArg &helper;
  6054. class HashDedupElement
  6055. {
  6056. public:
  6057. HashDedupElement(unsigned _hash, const void *_keyRow)
  6058. : hash(_hash), keyRow(_keyRow)
  6059. {
  6060. }
  6061. ~HashDedupElement()
  6062. {
  6063. ReleaseRoxieRow(keyRow);
  6064. }
  6065. inline unsigned queryHash() const
  6066. {
  6067. return hash;
  6068. }
  6069. inline const void *queryRow() const
  6070. {
  6071. return keyRow;
  6072. }
  6073. private:
  6074. unsigned hash;
  6075. const void *keyRow;
  6076. };
  6077. class HashDedupTable : public SuperHashTable
  6078. {
  6079. public:
  6080. HashDedupTable(IHThorHashDedupArg & _helper, unsigned _activityId)
  6081. : helper(_helper),
  6082. activityId(_activityId),
  6083. keySize(helper.queryKeySize())
  6084. {
  6085. }
  6086. virtual ~HashDedupTable() { releaseAll(); }
  6087. virtual unsigned getHashFromElement(const void *et) const
  6088. {
  6089. const HashDedupElement *element = reinterpret_cast<const HashDedupElement *>(et);
  6090. return element->queryHash();
  6091. }
  6092. virtual unsigned getHashFromFindParam(const void *fp) const { throwUnexpected(); }
  6093. virtual const void * getFindParam(const void *et) const { throwUnexpected(); }
  6094. virtual bool matchesElement(const void *et, const void *searchET) const { throwUnexpected(); }
  6095. virtual bool matchesFindParam(const void *et, const void *key, unsigned fphash) const
  6096. {
  6097. const HashDedupElement *element = reinterpret_cast<const HashDedupElement *>(et);
  6098. if (fphash != element->queryHash())
  6099. return false;
  6100. return (helper.queryKeyCompare()->docompare(element->queryRow(), key) == 0);
  6101. }
  6102. virtual void onAdd(void *et) {}
  6103. virtual void onRemove(void *et)
  6104. {
  6105. const HashDedupElement *element = reinterpret_cast<const HashDedupElement *>(et);
  6106. delete element;
  6107. }
  6108. void onCreate(IRoxieSlaveContext *ctx)
  6109. {
  6110. keyRowAllocator.setown(ctx->queryCodeContext()->getRowAllocator(keySize.queryOriginal(), activityId));
  6111. }
  6112. void reset()
  6113. {
  6114. kill();
  6115. }
  6116. bool insert(const void * row)
  6117. {
  6118. unsigned hash = helper.queryHash()->hash(row);
  6119. RtlDynamicRowBuilder keyRowBuilder(keyRowAllocator, true);
  6120. size32_t thisKeySize = helper.recordToKey(keyRowBuilder, row);
  6121. OwnedConstRoxieRow keyRow = keyRowBuilder.finalizeRowClear(thisKeySize);
  6122. if (find(hash, keyRow.get()))
  6123. return false;
  6124. addNew(new HashDedupElement(hash, keyRow.getClear()), hash);
  6125. return true;
  6126. }
  6127. private:
  6128. IHThorHashDedupArg & helper;
  6129. CachedOutputMetaData keySize;
  6130. Owned<IEngineRowAllocator> keyRowAllocator;
  6131. unsigned activityId;
  6132. } table;
  6133. public:
  6134. CRoxieServerHashDedupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  6135. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorHashDedupArg &)basehelper), table(helper, activityId)
  6136. {
  6137. eof = false;
  6138. }
  6139. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  6140. {
  6141. eof = false;
  6142. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  6143. }
  6144. virtual void onCreate(IRoxieSlaveContext *ctx, IHThorArg *_colocalParent)
  6145. {
  6146. CRoxieServerActivity::onCreate(ctx, colocalParent);
  6147. table.onCreate(ctx);
  6148. }
  6149. virtual void reset()
  6150. {
  6151. table.reset();
  6152. eof = false;
  6153. CRoxieServerActivity::reset();
  6154. }
  6155. virtual const void *nextInGroup()
  6156. {
  6157. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  6158. while(!eof)
  6159. {
  6160. const void * next = input->nextInGroup();
  6161. if(!next)
  6162. {
  6163. if (table.count() == 0)
  6164. eof = true;
  6165. table.reset();
  6166. return NULL;
  6167. }
  6168. if(table.insert(next))
  6169. return next;
  6170. else
  6171. ReleaseRoxieRow(next);
  6172. }
  6173. return NULL;
  6174. }
  6175. };
  6176. class CRoxieServerHashDedupActivityFactory : public CRoxieServerActivityFactory
  6177. {
  6178. public:
  6179. CRoxieServerHashDedupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  6180. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  6181. {
  6182. }
  6183. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  6184. {
  6185. return new CRoxieServerHashDedupActivity(this, _probeManager);
  6186. }
  6187. };
  6188. IRoxieServerActivityFactory *createRoxieServerHashDedupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  6189. {
  6190. return new CRoxieServerHashDedupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  6191. }
  6192. //=================================================================================
  6193. class CRoxieServerRollupActivity : public CRoxieServerActivity
  6194. {
  6195. IHThorRollupArg &helper;
  6196. OwnedConstRoxieRow left;
  6197. OwnedConstRoxieRow prev;
  6198. OwnedConstRoxieRow right;
  6199. bool readFirstRow;
  6200. public:
  6201. CRoxieServerRollupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  6202. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorRollupArg &)basehelper)
  6203. {
  6204. readFirstRow = false;
  6205. }
  6206. ~CRoxieServerRollupActivity()
  6207. {
  6208. }
  6209. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  6210. {
  6211. readFirstRow = false;
  6212. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  6213. }
  6214. virtual void reset()
  6215. {
  6216. left.clear();
  6217. prev.clear();
  6218. right.clear();
  6219. CRoxieServerActivity::reset();
  6220. }
  6221. virtual bool needsAllocator() const { return true; }
  6222. virtual const void * nextInGroup()
  6223. {
  6224. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  6225. if (!readFirstRow)
  6226. {
  6227. left.setown(input->nextInGroup());
  6228. prev.set(left);
  6229. readFirstRow = true;
  6230. }
  6231. loop
  6232. {
  6233. right.setown(input->nextInGroup());
  6234. if(!prev || !right || !helper.matches(prev,right))
  6235. {
  6236. const void * ret = left.getClear();
  6237. if(ret)
  6238. processed++;
  6239. left.setown(right.getClear());
  6240. prev.set(left);
  6241. return ret;
  6242. }
  6243. try
  6244. {
  6245. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  6246. unsigned outSize = helper.transform(rowBuilder, left, right);
  6247. if (outSize)
  6248. left.setown(rowBuilder.finalizeRowClear(outSize));
  6249. if (helper.getFlags() & RFrolledismatchleft)
  6250. prev.set(left);
  6251. else
  6252. prev.set(right);
  6253. }
  6254. catch(IException * E)
  6255. {
  6256. throw makeWrappedException(E);
  6257. }
  6258. }
  6259. }
  6260. };
  6261. class CRoxieServerRollupActivityFactory : public CRoxieServerActivityFactory
  6262. {
  6263. public:
  6264. CRoxieServerRollupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  6265. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  6266. {
  6267. }
  6268. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  6269. {
  6270. return new CRoxieServerRollupActivity(this, _probeManager);
  6271. }
  6272. };
  6273. IRoxieServerActivityFactory *createRoxieServerRollupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  6274. {
  6275. return new CRoxieServerRollupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  6276. }
  6277. //=================================================================================
  6278. class CRoxieServerNormalizeActivity : public CRoxieServerActivity
  6279. {
  6280. IHThorNormalizeArg &helper;
  6281. unsigned numThisRow;
  6282. unsigned curRow;
  6283. const void *buffer;
  6284. unsigned numProcessedLastGroup;
  6285. public:
  6286. CRoxieServerNormalizeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  6287. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorNormalizeArg &)basehelper)
  6288. {
  6289. buffer = NULL;
  6290. numThisRow = 0;
  6291. curRow = 0;
  6292. numProcessedLastGroup = 0;
  6293. }
  6294. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  6295. {
  6296. numThisRow = 0;
  6297. curRow = 0;
  6298. numProcessedLastGroup = 0;
  6299. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  6300. }
  6301. virtual void reset()
  6302. {
  6303. ReleaseClearRoxieRow(buffer);
  6304. CRoxieServerActivity::reset();
  6305. }
  6306. virtual bool needsAllocator() const { return true; }
  6307. virtual const void * nextInGroup()
  6308. {
  6309. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  6310. loop
  6311. {
  6312. while (curRow == numThisRow)
  6313. {
  6314. if (buffer)
  6315. ReleaseClearRoxieRow(buffer);
  6316. buffer = input->nextInGroup();
  6317. if (!buffer && (processed == numProcessedLastGroup))
  6318. buffer = input->nextInGroup();
  6319. if (!buffer)
  6320. {
  6321. numProcessedLastGroup = processed;
  6322. return NULL;
  6323. }
  6324. curRow = 0;
  6325. numThisRow = helper.numExpandedRows(buffer);
  6326. }
  6327. try
  6328. {
  6329. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  6330. unsigned actualSize = helper.transform(rowBuilder, buffer, ++curRow);
  6331. if (actualSize != 0)
  6332. {
  6333. processed++;
  6334. return rowBuilder.finalizeRowClear(actualSize);
  6335. }
  6336. }
  6337. catch (IException *E)
  6338. {
  6339. throw makeWrappedException(E);
  6340. }
  6341. }
  6342. }
  6343. };
  6344. class CRoxieServerNormalizeActivityFactory : public CRoxieServerActivityFactory
  6345. {
  6346. public:
  6347. CRoxieServerNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  6348. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  6349. {
  6350. }
  6351. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  6352. {
  6353. return new CRoxieServerNormalizeActivity(this, _probeManager);
  6354. }
  6355. };
  6356. IRoxieServerActivityFactory *createRoxieServerNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  6357. {
  6358. return new CRoxieServerNormalizeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  6359. }
  6360. //=================================================================================
  6361. class CRoxieServerNormalizeChildActivity : public CRoxieServerActivity
  6362. {
  6363. IHThorNormalizeChildArg &helper;
  6364. unsigned numThisRow;
  6365. unsigned curRow;
  6366. const void *buffer;
  6367. unsigned numProcessedLastGroup;
  6368. INormalizeChildIterator * cursor;
  6369. const void * curChildRow;
  6370. bool advanceInput()
  6371. {
  6372. loop
  6373. {
  6374. ReleaseClearRoxieRow(buffer);
  6375. buffer = input->nextInGroup();
  6376. if (!buffer && (processed == numProcessedLastGroup))
  6377. buffer = input->nextInGroup();
  6378. if (!buffer)
  6379. {
  6380. numProcessedLastGroup = processed;
  6381. return false;
  6382. }
  6383. curChildRow = cursor->first(buffer);
  6384. if (curChildRow)
  6385. {
  6386. curRow = 0;
  6387. return true;
  6388. }
  6389. }
  6390. }
  6391. public:
  6392. CRoxieServerNormalizeChildActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  6393. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorNormalizeChildArg &)basehelper)
  6394. {
  6395. buffer = NULL;
  6396. cursor = NULL;
  6397. numThisRow = 0;
  6398. curRow = 0;
  6399. numProcessedLastGroup = 0;
  6400. curChildRow = NULL;
  6401. }
  6402. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  6403. {
  6404. numThisRow = 0;
  6405. curRow = 0;
  6406. numProcessedLastGroup = 0;
  6407. curChildRow = NULL;
  6408. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  6409. cursor = helper.queryIterator();
  6410. }
  6411. virtual void stop(bool aborting)
  6412. {
  6413. CRoxieServerActivity::stop(aborting);
  6414. }
  6415. virtual void reset()
  6416. {
  6417. cursor = NULL;
  6418. ReleaseClearRoxieRow(buffer);
  6419. CRoxieServerActivity::reset();
  6420. }
  6421. virtual bool needsAllocator() const { return true; }
  6422. const void *nextInGroup()
  6423. {
  6424. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  6425. loop
  6426. {
  6427. if (!buffer)
  6428. {
  6429. if (!advanceInput())
  6430. return NULL;
  6431. }
  6432. try
  6433. {
  6434. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  6435. size32_t outSize = helper.transform(rowBuilder, buffer, curChildRow, ++curRow);
  6436. curChildRow = cursor->next();
  6437. if (!curChildRow)
  6438. ReleaseClearRoxieRow(buffer);
  6439. if (outSize != 0)
  6440. {
  6441. processed++;
  6442. return rowBuilder.finalizeRowClear(outSize);
  6443. }
  6444. }
  6445. catch (IException *E)
  6446. {
  6447. throw makeWrappedException(E);
  6448. }
  6449. }
  6450. }
  6451. };
  6452. class CRoxieServerNormalizeChildActivityFactory : public CRoxieServerActivityFactory
  6453. {
  6454. public:
  6455. CRoxieServerNormalizeChildActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  6456. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  6457. {
  6458. }
  6459. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  6460. {
  6461. return new CRoxieServerNormalizeChildActivity(this, _probeManager);
  6462. }
  6463. };
  6464. IRoxieServerActivityFactory *createRoxieServerNormalizeChildActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  6465. {
  6466. return new CRoxieServerNormalizeChildActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  6467. }
  6468. //=================================================================================
  6469. class CRoxieServerNormalizeLinkedChildActivity : public CRoxieServerActivity
  6470. {
  6471. IHThorNormalizeLinkedChildArg &helper;
  6472. OwnedConstRoxieRow curParent;
  6473. OwnedConstRoxieRow curChild;
  6474. unsigned numProcessedLastGroup;
  6475. bool advanceInput()
  6476. {
  6477. loop
  6478. {
  6479. curParent.setown(input->nextInGroup());
  6480. if (!curParent && (processed == numProcessedLastGroup))
  6481. curParent.setown(input->nextInGroup());
  6482. if (!curParent)
  6483. {
  6484. numProcessedLastGroup = processed;
  6485. return false;
  6486. }
  6487. curChild.set(helper.first(curParent));
  6488. if (curChild)
  6489. return true;
  6490. }
  6491. }
  6492. public:
  6493. CRoxieServerNormalizeLinkedChildActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  6494. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorNormalizeLinkedChildArg &)basehelper)
  6495. {
  6496. numProcessedLastGroup = 0;
  6497. }
  6498. ~CRoxieServerNormalizeLinkedChildActivity()
  6499. {
  6500. }
  6501. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  6502. {
  6503. numProcessedLastGroup = 0;
  6504. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  6505. }
  6506. virtual void reset()
  6507. {
  6508. curParent.clear();
  6509. curChild.clear();
  6510. CRoxieServerActivity::reset();
  6511. }
  6512. const void *nextInGroup()
  6513. {
  6514. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  6515. loop
  6516. {
  6517. if (!curParent)
  6518. {
  6519. if (!advanceInput())
  6520. return NULL;
  6521. }
  6522. try
  6523. {
  6524. const void *ret = curChild.getClear();
  6525. curChild.set(helper.next());
  6526. if (!curChild)
  6527. curParent.clear();
  6528. if (ret)
  6529. {
  6530. processed++;
  6531. return ret;
  6532. }
  6533. }
  6534. catch (IException *E)
  6535. {
  6536. throw makeWrappedException(E);
  6537. }
  6538. }
  6539. }
  6540. };
  6541. class CRoxieServerNormalizeLinkedChildActivityFactory : public CRoxieServerActivityFactory
  6542. {
  6543. public:
  6544. CRoxieServerNormalizeLinkedChildActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  6545. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  6546. {
  6547. }
  6548. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  6549. {
  6550. return new CRoxieServerNormalizeLinkedChildActivity(this, _probeManager);
  6551. }
  6552. };
  6553. IRoxieServerActivityFactory *createRoxieServerNormalizeLinkedChildActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  6554. {
  6555. return new CRoxieServerNormalizeLinkedChildActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  6556. }
  6557. //=================================================================================
  6558. interface ISortAlgorithm : extends IInterface
  6559. {
  6560. virtual void prepare(IRoxieInput *input) = 0;
  6561. virtual const void *next() = 0;
  6562. virtual void reset() = 0;
  6563. };
  6564. class CQuickSortAlgorithm : implements CInterfaceOf<ISortAlgorithm>
  6565. {
  6566. unsigned curIndex;
  6567. ConstPointerArray sorted;
  6568. ICompare *compare;
  6569. public:
  6570. CQuickSortAlgorithm(ICompare *_compare) : compare(_compare)
  6571. {
  6572. curIndex = 0;
  6573. }
  6574. virtual void prepare(IRoxieInput *input)
  6575. {
  6576. curIndex = 0;
  6577. if (input->nextGroup(sorted))
  6578. qsortvec(const_cast<void * *>(sorted.getArray()), sorted.ordinality(), *compare);
  6579. }
  6580. virtual const void *next()
  6581. {
  6582. if (sorted.isItem(curIndex))
  6583. return sorted.item(curIndex++);
  6584. return NULL;
  6585. }
  6586. virtual void reset()
  6587. {
  6588. while (sorted.isItem(curIndex))
  6589. ReleaseRoxieRow(sorted.item(curIndex++));
  6590. curIndex = 0;
  6591. sorted.kill();
  6592. }
  6593. };
  6594. class CSpillingQuickSortAlgorithm : implements CInterfaceOf<ISortAlgorithm>, implements roxiemem::IBufferedRowCallback
  6595. {
  6596. enum {
  6597. InitialSortElements = 0,
  6598. //The number of rows that can be added without entering a critical section, and therefore also the number
  6599. //of rows that might not get freed when memory gets tight.
  6600. CommitStep=32
  6601. };
  6602. roxiemem::DynamicRoxieOutputRowArray rowsToSort;
  6603. roxiemem::RoxieSimpleInputRowArray sorted;
  6604. ICompare *compare;
  6605. IRoxieSlaveContext * ctx;
  6606. Owned<IDiskMerger> diskMerger;
  6607. Owned<IRowStream> diskReader;
  6608. Owned<IOutputMetaData> rowMeta;
  6609. unsigned activityId;
  6610. public:
  6611. CSpillingQuickSortAlgorithm(ICompare *_compare, IRoxieSlaveContext * _ctx, IOutputMetaData * _rowMeta, unsigned _activityId)
  6612. : rowsToSort(&_ctx->queryRowManager(), InitialSortElements, CommitStep, _activityId), ctx(_ctx), compare(_compare), rowMeta(_rowMeta), activityId(_activityId)
  6613. {
  6614. ctx->queryRowManager().addRowBuffer(this);
  6615. }
  6616. ~CSpillingQuickSortAlgorithm()
  6617. {
  6618. ctx->queryRowManager().removeRowBuffer(this);
  6619. diskReader.clear();
  6620. }
  6621. virtual void prepare(IRoxieInput *input)
  6622. {
  6623. loop
  6624. {
  6625. const void * next = input->nextInGroup();
  6626. if (!next)
  6627. break;
  6628. if (!rowsToSort.append(next))
  6629. {
  6630. {
  6631. roxiemem::RoxieOutputRowArrayLock block(rowsToSort);
  6632. //We should have been called back to free any committed rows, but occasionally it may not (e.g., if
  6633. //the problem is global memory is exhausted) - in which case force a spill here (but add any pending
  6634. //rows first).
  6635. if (rowsToSort.numCommitted() != 0)
  6636. {
  6637. rowsToSort.flush();
  6638. spillRows();
  6639. }
  6640. //Ensure new rows are written to the head of the array. It needs to be a separate call because
  6641. //spillRows() cannot shift active row pointer since it can be called from any thread
  6642. rowsToSort.flush();
  6643. }
  6644. if (!rowsToSort.append(next))
  6645. {
  6646. ReleaseRoxieRow(next);
  6647. throw MakeStringException(ROXIEMM_MEMORY_LIMIT_EXCEEDED, "Insufficient memory to append sort row");
  6648. }
  6649. }
  6650. }
  6651. rowsToSort.flush();
  6652. roxiemem::RoxieOutputRowArrayLock block(rowsToSort);
  6653. if (diskMerger)
  6654. {
  6655. spillRows();
  6656. rowsToSort.kill();
  6657. diskReader.setown(diskMerger->merge(compare));
  6658. }
  6659. else
  6660. {
  6661. unsigned numRows = rowsToSort.numCommitted();
  6662. if (numRows)
  6663. {
  6664. const void * * rows = rowsToSort.getBlock(numRows);
  6665. //MORE: Should this be parallel? Should that be dependent on whether it is grouped? Should be a hint.
  6666. qsortvec(const_cast<void * *>(rows), numRows, *compare);
  6667. }
  6668. sorted.transferFrom(rowsToSort);
  6669. }
  6670. }
  6671. virtual const void *next()
  6672. {
  6673. if(diskReader)
  6674. return diskReader->nextRow();
  6675. return sorted.dequeue();
  6676. }
  6677. virtual void reset()
  6678. {
  6679. //MORE: This could transfer any row pointer from sorted back to rowsToSort. It would trade
  6680. //fewer heap allocations with not freeing up the memory from large group sorts.
  6681. rowsToSort.clearRows();
  6682. sorted.kill();
  6683. //Disk reader must be cleared before the merger - or the files may still be locked.
  6684. diskReader.clear();
  6685. diskMerger.clear();
  6686. }
  6687. //interface roxiemem::IBufferedRowCallback
  6688. virtual unsigned getSpillCost() const
  6689. {
  6690. //Spill global sorts before grouped sorts
  6691. if (rowMeta->isGrouped())
  6692. return 20;
  6693. return 10;
  6694. }
  6695. virtual bool freeBufferedRows(bool critical)
  6696. {
  6697. roxiemem::RoxieOutputRowArrayLock block(rowsToSort);
  6698. return spillRows();
  6699. }
  6700. protected:
  6701. bool spillRows()
  6702. {
  6703. unsigned numRows = rowsToSort.numCommitted();
  6704. if (numRows == 0)
  6705. return false;
  6706. const void * * rows = rowsToSort.getBlock(numRows);
  6707. qsortvec(const_cast<void * *>(rows), numRows, *compare);
  6708. Owned<IRowWriter> out = queryMerger()->createWriteBlock();
  6709. for (unsigned i= 0; i < numRows; i++)
  6710. {
  6711. out->putRow(rows[i]);
  6712. }
  6713. rowsToSort.noteSpilled(numRows);
  6714. return true;
  6715. }
  6716. IDiskMerger * queryMerger()
  6717. {
  6718. if (!diskMerger)
  6719. {
  6720. unsigned __int64 seq = (memsize_t)this ^ get_cycles_now();
  6721. StringBuffer spillBasename;
  6722. spillBasename.append(tempDirectory).append(PATHSEPCHAR).appendf("spill_sort_%"I64F"u", seq);
  6723. Owned<IRowLinkCounter> linker = new RoxieRowLinkCounter();
  6724. Owned<IRowInterfaces> rowInterfaces = createRowInterfaces(rowMeta, activityId, ctx->queryCodeContext());
  6725. diskMerger.setown(createDiskMerger(rowInterfaces, linker, spillBasename));
  6726. }
  6727. return diskMerger;
  6728. }
  6729. };
  6730. #define INSERTION_SORT_BLOCKSIZE 1024
  6731. class SortedBlock : public CInterface, implements IInterface
  6732. {
  6733. unsigned sequence;
  6734. const void **rows;
  6735. unsigned length;
  6736. unsigned pos;
  6737. SortedBlock(const SortedBlock &);
  6738. public:
  6739. IMPLEMENT_IINTERFACE;
  6740. SortedBlock(unsigned _sequence, IRowManager *rowManager, unsigned activityId) : sequence(_sequence)
  6741. {
  6742. rows = (const void **) rowManager->allocate(INSERTION_SORT_BLOCKSIZE * sizeof(void *), activityId);
  6743. length = 0;
  6744. pos = 0;
  6745. }
  6746. ~SortedBlock()
  6747. {
  6748. while (pos < length)
  6749. ReleaseRoxieRow(rows[pos++]);
  6750. ReleaseRoxieRow(rows);
  6751. }
  6752. int compareTo(SortedBlock *r, ICompare *compare)
  6753. {
  6754. int rc = compare->docompare(rows[pos], r->rows[r->pos]);
  6755. if (!rc)
  6756. rc = sequence - r->sequence;
  6757. return rc;
  6758. }
  6759. const void *next()
  6760. {
  6761. if (pos < length)
  6762. return rows[pos++];
  6763. else
  6764. return NULL;
  6765. }
  6766. inline bool eof()
  6767. {
  6768. return pos==length;
  6769. }
  6770. bool insert(const void *next, ICompare *_compare )
  6771. {
  6772. unsigned b = length;
  6773. if (b == INSERTION_SORT_BLOCKSIZE)
  6774. return false;
  6775. else if (b < 7)
  6776. {
  6777. while (b)
  6778. {
  6779. if (_compare->docompare(next, rows[b-1]) >= 0)
  6780. break;
  6781. b--;
  6782. }
  6783. if (b != length)
  6784. memmove(&rows[b+1], &rows[b], (length - b) * sizeof(void *));
  6785. rows[b] = next;
  6786. length++;
  6787. return true;
  6788. }
  6789. else
  6790. {
  6791. unsigned int a = 0;
  6792. while ((int)a<b)
  6793. {
  6794. int i = (a+b)/2;
  6795. int rc = _compare->docompare(next, rows[i]);
  6796. if (rc>=0)
  6797. a = i+1;
  6798. else
  6799. b = i;
  6800. }
  6801. if (a != length)
  6802. memmove(&rows[a+1], &rows[a], (length - a) * sizeof(void *));
  6803. rows[a] = next;
  6804. length++;
  6805. return true;
  6806. }
  6807. }
  6808. };
  6809. class CInsertionSortAlgorithm : implements CInterfaceOf<ISortAlgorithm>
  6810. {
  6811. SortedBlock *curBlock;
  6812. unsigned blockNo;
  6813. IArrayOf<SortedBlock> blocks;
  6814. unsigned activityId;
  6815. IRowManager *rowManager;
  6816. ICompare *compare;
  6817. void newBlock()
  6818. {
  6819. blocks.append(*curBlock);
  6820. curBlock = new SortedBlock(blockNo++, rowManager, activityId);
  6821. }
  6822. inline static int doCompare(SortedBlock &l, SortedBlock &r, ICompare *compare)
  6823. {
  6824. return l.compareTo(&r, compare);
  6825. }
  6826. void makeHeap()
  6827. {
  6828. /* Permute blocks to establish the heap property
  6829. For each element p, the children are p*2+1 and p*2+2 (provided these are in range)
  6830. The children of p must both be greater than or equal to p
  6831. The parent of a child c is given by p = (c-1)/2
  6832. */
  6833. unsigned i;
  6834. unsigned n = blocks.length();
  6835. SortedBlock **s = blocks.getArray();
  6836. for (i=1; i<n; i++)
  6837. {
  6838. SortedBlock * r = s[i];
  6839. int c = i; /* child */
  6840. while (c > 0)
  6841. {
  6842. int p = (c-1)/2; /* parent */
  6843. if ( doCompare( blocks.item(c), blocks.item(p), compare ) >= 0 )
  6844. break;
  6845. s[c] = s[p];
  6846. s[p] = r;
  6847. c = p;
  6848. }
  6849. }
  6850. }
  6851. void remakeHeap()
  6852. {
  6853. /* The row associated with block[0] will have changed
  6854. This code restores the heap property
  6855. */
  6856. unsigned p = 0; /* parent */
  6857. unsigned n = blocks.length();
  6858. SortedBlock **s = blocks.getArray();
  6859. while (1)
  6860. {
  6861. unsigned c = p*2 + 1; /* child */
  6862. if ( c >= n )
  6863. break;
  6864. /* Select smaller child */
  6865. if ( c+1 < n && doCompare( blocks.item(c+1), blocks.item(c), compare ) < 0 ) c += 1;
  6866. /* If child is greater or equal than parent then we are done */
  6867. if ( doCompare( blocks.item(c), blocks.item(p), compare ) >= 0 )
  6868. break;
  6869. /* Swap parent and child */
  6870. SortedBlock *r = s[c];
  6871. s[c] = s[p];
  6872. s[p] = r;
  6873. /* child becomes parent */
  6874. p = c;
  6875. }
  6876. }
  6877. public:
  6878. CInsertionSortAlgorithm(ICompare *_compare, IRowManager *_rowManager, unsigned _activityId)
  6879. : compare(_compare)
  6880. {
  6881. rowManager = _rowManager;
  6882. activityId = _activityId;
  6883. curBlock = NULL;
  6884. blockNo = 0;
  6885. }
  6886. virtual void reset()
  6887. {
  6888. blocks.kill();
  6889. delete curBlock;
  6890. curBlock = NULL;
  6891. blockNo = 0;
  6892. }
  6893. virtual void prepare(IRoxieInput *input)
  6894. {
  6895. blockNo = 0;
  6896. curBlock = new SortedBlock(blockNo++, rowManager, activityId);
  6897. loop
  6898. {
  6899. const void *next = input->nextInGroup();
  6900. if (!next)
  6901. break;
  6902. if (!curBlock->insert(next, compare))
  6903. {
  6904. newBlock();
  6905. curBlock->insert(next, compare);
  6906. }
  6907. }
  6908. if (blockNo > 1)
  6909. {
  6910. blocks.append(*curBlock);
  6911. curBlock = NULL;
  6912. makeHeap();
  6913. }
  6914. }
  6915. virtual const void * next()
  6916. {
  6917. const void *ret;
  6918. if (blockNo==1) // single block case..
  6919. {
  6920. ret = curBlock->next();
  6921. }
  6922. else if (blocks.length())
  6923. {
  6924. SortedBlock &top = blocks.item(0);
  6925. ret = top.next();
  6926. if (top.eof())
  6927. blocks.replace(blocks.popGet(), 0);
  6928. remakeHeap();
  6929. }
  6930. else
  6931. ret = NULL;
  6932. return ret;
  6933. }
  6934. };
  6935. class CHeapSortAlgorithm : implements CInterfaceOf<ISortAlgorithm>
  6936. {
  6937. unsigned curIndex;
  6938. ConstPointerArray sorted;
  6939. bool inputAlreadySorted;
  6940. IntArray sequences;
  6941. bool eof;
  6942. ICompare *compare;
  6943. #ifdef _CHECK_HEAPSORT
  6944. void checkHeap() const
  6945. {
  6946. unsigned n = sorted.ordinality();
  6947. if (n)
  6948. {
  6949. ICompare *_compare = compare;
  6950. void **s = sorted.getArray();
  6951. int *sq = sequences.getArray();
  6952. unsigned p;
  6953. #if 0
  6954. CTXLOG("------------------------%d entries-----------------", n);
  6955. for (p = 0; p < n; p++)
  6956. {
  6957. CTXLOG("HEAP %d: %d %.10s", p, sq[p], s[p] ? s[p] : "..");
  6958. }
  6959. #endif
  6960. for (p = 0; p < n; p++)
  6961. {
  6962. unsigned c = p*2+1;
  6963. if (c<n)
  6964. assertex(!s[c] || (docompare(p, c, _compare, s, sq) <= 0));
  6965. c++;
  6966. if (c<n)
  6967. assertex(!s[c] || (docompare(p, c, _compare, s, sq) <= 0));
  6968. }
  6969. }
  6970. }
  6971. #else
  6972. inline void checkHeap() const {}
  6973. #endif
  6974. const void *removeHeap()
  6975. {
  6976. unsigned n = sorted.ordinality();
  6977. if (n)
  6978. {
  6979. const void *ret = sorted.item(0);
  6980. if (n > 1 && ret)
  6981. {
  6982. ICompare *_compare = compare;
  6983. const void **s = sorted.getArray();
  6984. int *sq = sequences.getArray();
  6985. unsigned v = 0; // vacancy
  6986. loop
  6987. {
  6988. unsigned c = 2*v + 1;
  6989. if (c < n)
  6990. {
  6991. unsigned f = c; // favourite to fill it
  6992. c++;
  6993. if (c < n && s[c] && (!s[f] || (docompare(f, c, _compare, s, sq) > 0))) // is the smaller of the children
  6994. f = c;
  6995. sq[v] = sq[f];
  6996. if ((s[v] = s[f]) != NULL)
  6997. v = f;
  6998. else
  6999. break;
  7000. }
  7001. else
  7002. {
  7003. s[v] = NULL;
  7004. break;
  7005. }
  7006. }
  7007. }
  7008. checkHeap();
  7009. return ret;
  7010. }
  7011. else
  7012. return NULL;
  7013. }
  7014. static inline int docompare(unsigned l, unsigned r, ICompare *_compare, const void **s, int *sq)
  7015. {
  7016. int rc = _compare->docompare(s[l], s[r]);
  7017. if (!rc)
  7018. rc = sq[l] - sq[r];
  7019. return rc;
  7020. }
  7021. void insertHeap(const void *next)
  7022. {
  7023. // Upside-down heap sort
  7024. // Maintain a heap where every parent is lower than each of its children
  7025. // Root (at node 0) is lowest record seen, nodes 2n+1, 2n+2 are the children
  7026. // To insert a row, add it at end then keep swapping with parent as long as parent is greater
  7027. // To remove a row, take row 0, then recreate heap by replacing it with smaller of two children and so on down the tree
  7028. // Nice features:
  7029. // 1. Deterministic
  7030. // 2. Sort time can be overlapped with upstream/downstream processes - there is no delay between receiving last record from input and deliveriing first to output
  7031. // 3. Already sorted case can be spotted at zero cost while reading.
  7032. // 4. If you don't read all the results, you don't have to complete the sort
  7033. // BUT it is NOT stable, so we have to use a parallel array of sequence numbers
  7034. unsigned n = sorted.ordinality();
  7035. sorted.append(next);
  7036. sequences.append(n);
  7037. if (!n)
  7038. return;
  7039. ICompare *_compare = compare;
  7040. const void **s = sorted.getArray();
  7041. if (inputAlreadySorted)
  7042. {
  7043. if (_compare->docompare(next, s[n-1]) >= 0)
  7044. return;
  7045. else
  7046. {
  7047. // MORE - could delay creating sequences until now...
  7048. inputAlreadySorted = false;
  7049. }
  7050. }
  7051. int *sq = sequences.getArray();
  7052. unsigned q = n;
  7053. while (n)
  7054. {
  7055. unsigned parent = (n-1) / 2;
  7056. const void *p = s[parent];
  7057. if (_compare->docompare(p, next) <= 0)
  7058. break;
  7059. s[n] = p;
  7060. sq[n] = sq[parent];
  7061. s[parent] = next;
  7062. sq[parent] = q;
  7063. n = parent;
  7064. }
  7065. }
  7066. public:
  7067. CHeapSortAlgorithm(ICompare *_compare) : compare(_compare)
  7068. {
  7069. inputAlreadySorted = true;
  7070. curIndex = 0;
  7071. eof = false;
  7072. }
  7073. virtual void reset()
  7074. {
  7075. eof = false;
  7076. if (inputAlreadySorted)
  7077. {
  7078. while (sorted.isItem(curIndex))
  7079. ReleaseRoxieRow(sorted.item(curIndex++));
  7080. sorted.kill();
  7081. }
  7082. else
  7083. {
  7084. ReleaseRoxieRowSet(sorted);
  7085. }
  7086. inputAlreadySorted = true;
  7087. sequences.kill();
  7088. }
  7089. virtual void prepare(IRoxieInput *input)
  7090. {
  7091. inputAlreadySorted = true;
  7092. curIndex = 0;
  7093. eof = false;
  7094. assertex(sorted.ordinality()==0);
  7095. const void *next = input->nextInGroup();
  7096. if (!next)
  7097. {
  7098. eof = true;
  7099. return;
  7100. }
  7101. loop
  7102. {
  7103. insertHeap(next);
  7104. next = input->nextInGroup();
  7105. if (!next)
  7106. break;
  7107. }
  7108. checkHeap();
  7109. }
  7110. virtual const void * next()
  7111. {
  7112. if (inputAlreadySorted)
  7113. {
  7114. if (sorted.isItem(curIndex))
  7115. {
  7116. return sorted.item(curIndex++);
  7117. }
  7118. else
  7119. return NULL;
  7120. }
  7121. else
  7122. return removeHeap();
  7123. }
  7124. };
  7125. typedef enum {heapSort, insertionSort, quickSort, spillingQuickSort, unknownSort } RoxieSortAlgorithm;
  7126. class CRoxieServerSortActivity : public CRoxieServerActivity
  7127. {
  7128. protected:
  7129. IHThorSortArg &helper;
  7130. ICompare *compare;
  7131. Owned<ISortAlgorithm> sorter;
  7132. bool readInput;
  7133. RoxieSortAlgorithm sortAlgorithm;
  7134. unsigned sortFlags;
  7135. public:
  7136. CRoxieServerSortActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, RoxieSortAlgorithm _sortAlgorithm, unsigned _sortFlags)
  7137. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorSortArg &)basehelper), sortAlgorithm(_sortAlgorithm), sortFlags(_sortFlags)
  7138. {
  7139. compare = helper.queryCompare();
  7140. readInput = false;
  7141. }
  7142. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  7143. {
  7144. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  7145. switch (sortAlgorithm)
  7146. {
  7147. case heapSort:
  7148. sorter.setown(new CHeapSortAlgorithm(compare));
  7149. break;
  7150. case insertionSort:
  7151. sorter.setown(new CInsertionSortAlgorithm(compare, &ctx->queryRowManager(), activityId));
  7152. break;
  7153. case quickSort:
  7154. sorter.setown(new CQuickSortAlgorithm(compare));
  7155. break;
  7156. case spillingQuickSort:
  7157. sorter.setown(new CSpillingQuickSortAlgorithm(compare, ctx, meta, activityId));
  7158. break;
  7159. case unknownSort:
  7160. sorter.clear(); // create it later....
  7161. break;
  7162. default:
  7163. throwUnexpected();
  7164. break;
  7165. }
  7166. }
  7167. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  7168. {
  7169. assertex(!readInput);
  7170. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  7171. }
  7172. virtual void reset()
  7173. {
  7174. if (sorter)
  7175. sorter->reset();
  7176. readInput = false;
  7177. CRoxieServerActivity::reset();
  7178. }
  7179. virtual const void * nextInGroup()
  7180. {
  7181. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  7182. if (!readInput)
  7183. {
  7184. if (sortAlgorithm == unknownSort)
  7185. {
  7186. sorter.clear();
  7187. IHThorAlgorithm *sortMethod = static_cast<IHThorAlgorithm *>(helper.selectInterface(TAIalgorithm_1));
  7188. OwnedRoxieString useAlgorithm(sortMethod->getAlgorithm());
  7189. if (useAlgorithm)
  7190. {
  7191. if (stricmp(useAlgorithm, "quicksort")==0)
  7192. {
  7193. if (sortFlags & TAFstable)
  7194. throw MakeStringException(ROXIE_UNKNOWN_ALGORITHM, "Invalid stable sort algorithm %s requested", useAlgorithm.get());
  7195. sorter.setown(new CQuickSortAlgorithm(compare));
  7196. }
  7197. else if (stricmp(useAlgorithm, "heapsort")==0)
  7198. sorter.setown(new CHeapSortAlgorithm(compare));
  7199. else if (stricmp(useAlgorithm, "insertionsort")==0)
  7200. sorter.setown(new CInsertionSortAlgorithm(compare, &ctx->queryRowManager(), activityId));
  7201. else
  7202. {
  7203. WARNLOG(ROXIE_UNKNOWN_ALGORITHM, "Ignoring unsupported sort order algorithm '%s', using default", useAlgorithm.get());
  7204. if (sortFlags & TAFunstable)
  7205. sorter.setown(new CQuickSortAlgorithm(compare));
  7206. else
  7207. sorter.setown(new CHeapSortAlgorithm(compare));
  7208. }
  7209. }
  7210. else
  7211. sorter.setown(new CHeapSortAlgorithm(compare)); // shouldn't really happen but there was a vintage of codegen that did not set the flag when algorithm not specified...
  7212. }
  7213. sorter->prepare(input);
  7214. readInput = true;
  7215. }
  7216. const void *ret = sorter->next();
  7217. if (ret)
  7218. processed++;
  7219. else
  7220. {
  7221. sorter->reset();
  7222. readInput = false; // ready for next group
  7223. }
  7224. return ret;
  7225. }
  7226. };
  7227. class CRoxieServerSortActivityFactory : public CRoxieServerActivityFactory
  7228. {
  7229. RoxieSortAlgorithm sortAlgorithm;
  7230. unsigned sortFlags;
  7231. public:
  7232. CRoxieServerSortActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  7233. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  7234. {
  7235. sortAlgorithm = heapSort;
  7236. sortFlags = TAFstable;
  7237. Owned<IHThorSortArg> sortHelper = (IHThorSortArg *) helperFactory();
  7238. IHThorAlgorithm *sortMethod = static_cast<IHThorAlgorithm *>(sortHelper->selectInterface(TAIalgorithm_1));
  7239. if (sortMethod)
  7240. {
  7241. sortFlags = sortMethod->getAlgorithmFlags();
  7242. if (sortFlags & TAFunstable)
  7243. sortAlgorithm = quickSort;
  7244. if (!(sortFlags & TAFconstant))
  7245. sortAlgorithm = unknownSort;
  7246. else
  7247. {
  7248. OwnedRoxieString useAlgorithm(sortMethod->getAlgorithm());
  7249. if (useAlgorithm)
  7250. {
  7251. if (stricmp(useAlgorithm, "quicksort")==0)
  7252. {
  7253. if (sortFlags & TAFstable)
  7254. throw MakeStringException(ROXIE_UNKNOWN_ALGORITHM, "Invalid stable sort algorithm %s requested", useAlgorithm.get());
  7255. sortAlgorithm = quickSort;
  7256. }
  7257. else if (stricmp(useAlgorithm, "spillingquicksort")==0)
  7258. {
  7259. if (sortFlags & TAFstable)
  7260. throw MakeStringException(ROXIE_UNKNOWN_ALGORITHM, "Invalid stable sort algorithm %s requested", useAlgorithm.get());
  7261. sortAlgorithm = spillingQuickSort;
  7262. }
  7263. else if (stricmp(useAlgorithm, "heapsort")==0)
  7264. sortAlgorithm = heapSort; // NOTE - we do allow UNSTABLE('heapsort') in order to facilitate runtime selection
  7265. else if (stricmp(useAlgorithm, "insertionsort")==0)
  7266. sortAlgorithm = insertionSort;
  7267. else
  7268. {
  7269. WARNLOG(ROXIE_UNKNOWN_ALGORITHM, "Ignoring unsupported sort order algorithm '%s', using default", useAlgorithm.get());
  7270. if (sortFlags & TAFunstable)
  7271. sortAlgorithm = quickSort;
  7272. else
  7273. sortAlgorithm = heapSort;
  7274. }
  7275. }
  7276. }
  7277. }
  7278. }
  7279. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  7280. {
  7281. return new CRoxieServerSortActivity(this, _probeManager, sortAlgorithm, sortFlags);
  7282. }
  7283. };
  7284. IRoxieServerActivityFactory *createRoxieServerSortActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  7285. {
  7286. return new CRoxieServerSortActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  7287. }
  7288. //=====================================================================================================
  7289. class CRoxieServerSortedActivity : public CRoxieServerActivity
  7290. {
  7291. IHThorSortedArg &helper;
  7292. ICompare * compare;
  7293. const void *prev;
  7294. IRangeCompare * stepCompare;
  7295. public:
  7296. CRoxieServerSortedActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  7297. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorSortedArg &)basehelper)
  7298. {
  7299. prev = NULL;
  7300. compare = helper.queryCompare();
  7301. stepCompare = NULL;
  7302. }
  7303. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  7304. {
  7305. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  7306. IInputSteppingMeta * stepMeta = input->querySteppingMeta();
  7307. if (stepMeta)
  7308. stepCompare = stepMeta->queryCompare();
  7309. prev = NULL;
  7310. }
  7311. virtual void reset()
  7312. {
  7313. ReleaseClearRoxieRow(prev);
  7314. CRoxieServerActivity::reset();
  7315. }
  7316. virtual const void * nextInGroup()
  7317. {
  7318. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  7319. const void *ret = input->nextInGroup();
  7320. if (ret && prev && compare->docompare(prev, ret) > 0)
  7321. {
  7322. // MORE - better to give mismatching rows that indexes?
  7323. throw MakeStringException(ROXIE_NOT_SORTED, "SORTED(%u) detected incorrectly sorted rows (row %d, %d))", activityId, processed, processed+1);
  7324. }
  7325. ReleaseRoxieRow(prev);
  7326. prev = ret;
  7327. if (ret)
  7328. {
  7329. LinkRoxieRow(prev);
  7330. processed++;
  7331. }
  7332. return ret;
  7333. }
  7334. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  7335. {
  7336. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  7337. const void *ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  7338. if (ret && prev && compare->docompare(prev, ret) > 0)
  7339. {
  7340. // MORE - better to give mismatching rows that indexes?
  7341. throw MakeStringException(ROXIE_NOT_SORTED, "SORTED(%u) detected incorrectly sorted rows (row %d, %d))", activityId, processed, processed+1);
  7342. }
  7343. ReleaseRoxieRow(prev);
  7344. prev = ret;
  7345. if (ret)
  7346. {
  7347. LinkRoxieRow(prev);
  7348. processed++;
  7349. }
  7350. return ret;
  7351. }
  7352. IInputSteppingMeta * querySteppingMeta()
  7353. {
  7354. return input->querySteppingMeta();
  7355. }
  7356. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  7357. {
  7358. return input->gatherConjunctions(collector);
  7359. }
  7360. virtual void resetEOF()
  7361. {
  7362. input->resetEOF();
  7363. }
  7364. };
  7365. class CRoxieServerSortedActivityFactory : public CRoxieServerActivityFactory
  7366. {
  7367. public:
  7368. CRoxieServerSortedActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  7369. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  7370. {
  7371. }
  7372. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  7373. {
  7374. return new CRoxieServerSortedActivity(this, _probeManager);
  7375. }
  7376. };
  7377. IRoxieServerActivityFactory *createRoxieServerSortedActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  7378. {
  7379. return new CRoxieServerSortedActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  7380. }
  7381. //=====================================================================================================
  7382. class CRoxieServerThroughSpillActivity : public CRoxieServerActivity
  7383. {
  7384. /*
  7385. BE VERY CAREFUL - this code is tricky.
  7386. Note that starts and stops (and resets) can occur in strange orders
  7387. The FIRST start OR stop must initialize the activity but only the first START should call the upstream start.
  7388. The last stop should call the upstream stop.
  7389. The first reset should call the upstream reset.
  7390. The calculation of whether a row is needed for other (yet to come) outputs needs to work correctly even if the output in question has
  7391. not yet had start or stop called - for this to happen init() is called on all outputs on the first start or stop.
  7392. Some outputs may be completely pruned away when used in a GRAPH - these outputs should not receive any start/stop/reset and should be
  7393. ignored in the minIndex calculation
  7394. */
  7395. public:
  7396. IHThorArg &helper;
  7397. unsigned activeOutputs;
  7398. unsigned numOutputs;
  7399. unsigned numOriginalOutputs;
  7400. QueueOf<const void, true> buffer;
  7401. CriticalSection crit;
  7402. CriticalSection crit2;
  7403. unsigned tailIdx;
  7404. unsigned headIdx;
  7405. Owned<IException> error;
  7406. class OutputAdaptor : public CInterface, implements IRoxieInput
  7407. {
  7408. bool eof, eofpending, stopped;
  7409. public:
  7410. CRoxieServerThroughSpillActivity *parent;
  7411. unsigned idx;
  7412. unsigned oid;
  7413. unsigned processed;
  7414. unsigned __int64 totalCycles;
  7415. public:
  7416. IMPLEMENT_IINTERFACE;
  7417. OutputAdaptor()
  7418. {
  7419. parent = NULL;
  7420. oid = 0;
  7421. idx = 0;
  7422. processed = 0;
  7423. totalCycles = 0;
  7424. eofpending = false;
  7425. eof = false;
  7426. stopped = false;
  7427. }
  7428. ~OutputAdaptor()
  7429. {
  7430. if (traceStartStop)
  7431. DBGLOG("%p ~OutputAdaptor %d", this, oid);
  7432. }
  7433. void init()
  7434. {
  7435. if (traceStartStop)
  7436. DBGLOG("%p init Input adaptor %d", this, oid);
  7437. idx = 0;
  7438. processed = 0;
  7439. totalCycles = 0;
  7440. eofpending = false;
  7441. eof = false;
  7442. stopped = false;
  7443. }
  7444. virtual unsigned queryId() const
  7445. {
  7446. return parent->queryId();
  7447. }
  7448. virtual IRoxieServerActivity *queryActivity()
  7449. {
  7450. return parent;
  7451. }
  7452. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  7453. {
  7454. return parent->queryIndexReadActivity();
  7455. }
  7456. virtual unsigned __int64 queryTotalCycles() const
  7457. {
  7458. return totalCycles;
  7459. }
  7460. virtual unsigned __int64 queryLocalCycles() const
  7461. {
  7462. return 0;
  7463. }
  7464. virtual IRoxieInput *queryInput(unsigned idx) const
  7465. {
  7466. return parent->queryInput(idx);
  7467. }
  7468. virtual const void * nextInGroup()
  7469. {
  7470. ActivityTimer t(totalCycles, parent->timeActivities, parent->ctx->queryDebugContext());
  7471. if (eof)
  7472. return NULL;
  7473. const void *ret = parent->readBuffered(idx, oid);
  7474. #ifdef TRACE_SPLIT
  7475. parent->CTXLOG("Adaptor %d got back %p for record %d", oid, ret, idx);
  7476. #endif
  7477. idx++;
  7478. if (ret)
  7479. {
  7480. processed++;
  7481. eofpending = false;
  7482. }
  7483. else if (eofpending)
  7484. eof = true;
  7485. else
  7486. eofpending = true;
  7487. return ret;
  7488. }
  7489. virtual IOutputMetaData * queryOutputMeta() const
  7490. {
  7491. return parent->queryOutputMeta();
  7492. }
  7493. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  7494. {
  7495. // NOTE: it is tempting to move the init() of all output adaptors here. However that is not a good idea,
  7496. // since adaptors that have not yet started or stopped (but are going to) still need to have been init()'ed
  7497. // for minIndex to give the correct answers
  7498. // therefore, we call init() on all adaptors on receipt of the first start() or stop()
  7499. if (traceStartStop)
  7500. parent->CTXLOG("%p start Input adaptor %d stopped = %d", this, oid, stopped);
  7501. parent->start(oid, parentExtractSize, parentExtract, paused);
  7502. }
  7503. virtual void stop(bool aborting)
  7504. {
  7505. if (traceStartStop)
  7506. parent->CTXLOG("%p stop Input adaptor %d stopped = %d", this, oid, stopped);
  7507. if (!stopped)
  7508. {
  7509. parent->stop(oid, idx, aborting); // NOTE - may call init()
  7510. stopped = true; // parent code relies on stop being called exactly once per adaptor, so make sure it is!
  7511. idx = (unsigned) -1; // causes minIndex not to save rows for me...
  7512. }
  7513. };
  7514. virtual void reset()
  7515. {
  7516. if (traceStartStop)
  7517. parent->CTXLOG("%p reset Input adaptor %d stopped = %d", this, oid, stopped);
  7518. parent->reset(oid);
  7519. parent->noteProcessed(oid, processed, 0, 0);
  7520. processed = 0;
  7521. idx = 0; // value should not be relevant really but this is the safest...
  7522. stopped = false;
  7523. };
  7524. virtual void resetEOF()
  7525. {
  7526. parent->resetEOF();
  7527. }
  7528. virtual void checkAbort()
  7529. {
  7530. parent->checkAbort();
  7531. }
  7532. } *adaptors;
  7533. bool *used;
  7534. unsigned nextFreeOutput()
  7535. {
  7536. unsigned i = numOutputs;
  7537. while (i)
  7538. {
  7539. i--;
  7540. if (!used[i])
  7541. return i;
  7542. }
  7543. throwUnexpected();
  7544. }
  7545. unsigned minIndex(unsigned exceptOid)
  7546. {
  7547. // MORE - yukky code (and slow). Could keep them heapsorted by idx or something
  7548. // this is trying to determine whethwe any of the adaptors will in the future read a given record
  7549. unsigned minIdx = (unsigned) -1;
  7550. for (unsigned i = 0; i < numOutputs; i++)
  7551. {
  7552. if (i != exceptOid && used[i] && adaptors[i].idx < minIdx)
  7553. minIdx = adaptors[i].idx;
  7554. }
  7555. return minIdx;
  7556. }
  7557. void initOutputs()
  7558. {
  7559. activeOutputs = numOutputs;
  7560. for (unsigned i = 0; i < numOriginalOutputs; i++)
  7561. if (used[i])
  7562. adaptors[i].init();
  7563. state = STATEstarting;
  7564. }
  7565. public:
  7566. CRoxieServerThroughSpillActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numOutputs)
  7567. : CRoxieServerActivity(_factory, _probeManager), helper(basehelper), numOutputs(_numOutputs)
  7568. {
  7569. numOriginalOutputs = numOutputs;
  7570. adaptors = new OutputAdaptor[numOutputs];
  7571. used = new bool[numOutputs];
  7572. for (unsigned i = 0; i < numOutputs; i++)
  7573. {
  7574. adaptors[i].parent = this;
  7575. adaptors[i].oid = i;
  7576. used[i] = false;
  7577. }
  7578. tailIdx = 0;
  7579. headIdx = 0;
  7580. activeOutputs = numOutputs;
  7581. }
  7582. ~CRoxieServerThroughSpillActivity()
  7583. {
  7584. delete [] adaptors;
  7585. delete [] used;
  7586. }
  7587. const void *readBuffered(unsigned idx, unsigned oid)
  7588. {
  7589. CriticalBlock b(crit);
  7590. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext()); // NOTE - time spent waiting for crit not included here. Is that right?
  7591. if (idx == headIdx) // test once without getting the crit2 sec
  7592. {
  7593. CriticalUnblock b1(crit);
  7594. CriticalBlock b2(crit2);
  7595. if (error)
  7596. {
  7597. throw error.getLink();
  7598. }
  7599. if (idx == headIdx) // test again now that we have it
  7600. {
  7601. try
  7602. {
  7603. const void *row = input->nextInGroup();
  7604. CriticalBlock b3(crit);
  7605. headIdx++;
  7606. if (activeOutputs==1)
  7607. {
  7608. #ifdef TRACE_SPLIT
  7609. CTXLOG("spill %d optimised return of %p", activityId, row);
  7610. #endif
  7611. return row; // optimization for the case where only one output still active.
  7612. }
  7613. buffer.enqueue(row);
  7614. }
  7615. catch (IException *E)
  7616. {
  7617. #ifdef TRACE_SPLIT
  7618. CTXLOG("spill %d caught exception", activityId);
  7619. #endif
  7620. error.set(E);
  7621. throw;
  7622. }
  7623. catch (...)
  7624. {
  7625. IException *E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught in CRoxieServerThroughSpillActivity::readBuffered");
  7626. error.set(E);
  7627. throw E;
  7628. }
  7629. }
  7630. }
  7631. idx -= tailIdx;
  7632. if (!idx)
  7633. {
  7634. unsigned min = minIndex(oid);
  7635. if (min > tailIdx)
  7636. {
  7637. tailIdx++;
  7638. const void *ret = buffer.dequeue(); // no need to link - last puller
  7639. #ifdef TRACE_SPLIT
  7640. CTXLOG("last puller return of %p", ret);
  7641. #endif
  7642. return ret;
  7643. }
  7644. }
  7645. const void *ret = buffer.item(idx);
  7646. if (ret) LinkRoxieRow(ret);
  7647. #ifdef TRACE_SPLIT
  7648. CTXLOG("standard return of %p", ret);
  7649. #endif
  7650. return ret;
  7651. }
  7652. virtual void start(unsigned oid, unsigned parentExtractSize, const byte *parentExtract, bool paused)
  7653. {
  7654. CriticalBlock b(crit);
  7655. if (error)
  7656. throw error.getLink();
  7657. if (factory)
  7658. factory->noteStarted(oid);
  7659. if (traceStartStop)
  7660. CTXLOG("SPLIT %p: start %d child %d activeOutputs %d numOutputs %d numOriginalOutputs %d state %s", this, activityId, oid, activeOutputs, numOutputs, numOriginalOutputs, queryStateText(state));
  7661. if (state != STATEstarted)
  7662. {
  7663. if (state != STATEstarting)
  7664. initOutputs();
  7665. tailIdx = 0;
  7666. headIdx = 0;
  7667. error.clear();
  7668. try
  7669. {
  7670. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  7671. }
  7672. catch (IException *E)
  7673. {
  7674. #ifdef TRACE_SPLIT
  7675. CTXLOG("spill %d caught exception in start", activityId);
  7676. #endif
  7677. error.set(E);
  7678. throw;
  7679. }
  7680. catch (...)
  7681. {
  7682. IException *E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught in CRoxieServerThroughSpillActivity::start");
  7683. error.set(E);
  7684. throw E;
  7685. }
  7686. }
  7687. }
  7688. void stop(unsigned oid, unsigned idx, bool aborting)
  7689. {
  7690. // Note that OutputAdaptor code ensures that stop is not called more than once per adaptor
  7691. CriticalBlock b(crit);
  7692. #ifdef TRACE_STARTSTOP
  7693. if (traceStartStop)
  7694. {
  7695. CTXLOG("SPLIT %p: stop %d child %d activeOutputs %d numOutputs %d numOriginalOutputs %d state %s", this, activityId, oid, activeOutputs, numOutputs, numOriginalOutputs, queryStateText(state));
  7696. if (watchActivityId && watchActivityId==activityId)
  7697. {
  7698. CTXLOG("WATCH: stop %d", activityId);
  7699. }
  7700. }
  7701. #endif
  7702. if (state != STATEstarting && state != STATEstarted)
  7703. initOutputs();
  7704. if (activeOutputs > 1)
  7705. {
  7706. if (tailIdx==idx)
  7707. {
  7708. // Discard all buffered rows that are there purely for this adaptor to read them
  7709. unsigned min = minIndex(oid);
  7710. if (min != (unsigned) -1)
  7711. // what does -1 signify?? No-one wants anything? In which case can't we kill all rows??
  7712. // Should never happen though if there are still some active.
  7713. // there may be a small window where adaptors are blocked on the semaphore...
  7714. {
  7715. #ifdef TRACE_SPLIT
  7716. CTXLOG("%p: Discarding buffered rows from %d to %d for oid %x (%d outputs active)", this, idx, min, oid, activeOutputs);
  7717. #endif
  7718. while (tailIdx < min)
  7719. {
  7720. ReleaseRoxieRow(buffer.dequeue());
  7721. tailIdx++;
  7722. }
  7723. }
  7724. }
  7725. activeOutputs--;
  7726. return;
  7727. }
  7728. #ifdef TRACE_SPLIT
  7729. CTXLOG("%p: All outputs done", this);
  7730. #endif
  7731. activeOutputs = numOutputs;
  7732. CRoxieServerActivity::stop(aborting);
  7733. };
  7734. void reset(unsigned oid)
  7735. {
  7736. if (traceStartStop)
  7737. CTXLOG("SPLIT %p: reset %d child %d activeOutputs %d numOutputs %d numOriginalOutputs %d state %s", this, activityId, oid, activeOutputs, numOutputs, numOriginalOutputs, queryStateText(state));
  7738. activeOutputs = numOutputs;
  7739. while (buffer.ordinality())
  7740. ReleaseRoxieRow(buffer.dequeue());
  7741. error.clear();
  7742. if (state != STATEreset) // make sure input is only reset once
  7743. CRoxieServerActivity::reset();
  7744. };
  7745. virtual unsigned __int64 queryLocalCycles() const
  7746. {
  7747. return 0;
  7748. }
  7749. virtual const void *nextInGroup()
  7750. {
  7751. throwUnexpected(); // Internal logic error - we are not anybody's input
  7752. }
  7753. virtual IOutputMetaData * queryOutputMeta() const
  7754. {
  7755. // if (outputMeta)
  7756. // return outputMeta;
  7757. // else
  7758. return input->queryOutputMeta(); // not always known (e.g. disk write - though Gavin _could_ fill it in)
  7759. }
  7760. virtual IRoxieInput *queryOutput(unsigned idx)
  7761. {
  7762. if (idx==(unsigned)-1)
  7763. idx = nextFreeOutput(); // MORE - what is this used for?
  7764. assertex(idx < numOriginalOutputs);
  7765. assertex(!used[idx]);
  7766. used[idx] = true;
  7767. return &adaptors[idx];
  7768. }
  7769. virtual void resetOutputsUsed()
  7770. {
  7771. numOutputs = 1;
  7772. activeOutputs = 1;
  7773. // MORE RKC->GH should we be clearing the used array here? anywhere?
  7774. }
  7775. virtual void noteOutputUsed()
  7776. {
  7777. assertex(numOutputs < numOriginalOutputs);
  7778. numOutputs++;
  7779. activeOutputs = numOutputs;
  7780. }
  7781. virtual bool isPassThrough()
  7782. {
  7783. return numOutputs==1;
  7784. }
  7785. };
  7786. class CRoxieServerThroughSpillActivityFactory : public CRoxieServerMultiOutputFactory
  7787. {
  7788. public:
  7789. CRoxieServerThroughSpillActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  7790. : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  7791. {
  7792. Owned<IHThorSpillArg> helper = (IHThorSpillArg *) helperFactory();
  7793. setNumOutputs(helper->getTempUsageCount() + 1);
  7794. }
  7795. CRoxieServerThroughSpillActivityFactory(IQueryFactory &_queryFactory, HelperFactory *_helperFactory, unsigned _numOutputs)
  7796. : CRoxieServerMultiOutputFactory(0, 0, _queryFactory, _helperFactory, TAKsplit)
  7797. {
  7798. setNumOutputs(_numOutputs);
  7799. }
  7800. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  7801. {
  7802. return new CRoxieServerThroughSpillActivity(this, _probeManager, numOutputs);
  7803. }
  7804. };
  7805. IRoxieServerActivityFactory *createRoxieServerThroughSpillActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  7806. {
  7807. return new CRoxieServerThroughSpillActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  7808. }
  7809. IRoxieServerActivityFactory *createRoxieServerThroughSpillActivityFactory(IQueryFactory &_queryFactory, HelperFactory *_factory, unsigned _numOutputs)
  7810. {
  7811. return new CRoxieServerThroughSpillActivityFactory(_queryFactory, _factory, _numOutputs);
  7812. }
  7813. //----------------------------------------------------------------------------------------------
  7814. class CRoxieServerSplitActivityFactory : public CRoxieServerMultiOutputFactory
  7815. {
  7816. public:
  7817. CRoxieServerSplitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  7818. : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  7819. {
  7820. Owned<IHThorSplitArg> helper = (IHThorSplitArg *) helperFactory();
  7821. setNumOutputs(helper->numBranches());
  7822. }
  7823. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  7824. {
  7825. return new CRoxieServerThroughSpillActivity(this, _probeManager, numOutputs);
  7826. }
  7827. };
  7828. IRoxieServerActivityFactory *createRoxieServerSplitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  7829. {
  7830. return new CRoxieServerSplitActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  7831. }
  7832. //=====================================================================================================
  7833. #define PIPE_BUFSIZE 0x8000
  7834. static IException *createPipeFailureException(const char *cmd, unsigned retcode, IPipeProcess *pipe)
  7835. {
  7836. StringBuffer msg;
  7837. if(pipe->hasError())
  7838. {
  7839. try
  7840. {
  7841. char error[512];
  7842. size32_t sz = pipe->readError(sizeof(error), error);
  7843. if(sz && sz!=(size32_t)-1)
  7844. msg.append(", stderr: '").append(sz, error).append("'");
  7845. }
  7846. catch (IException *e)
  7847. {
  7848. EXCLOG(e, "Error reading pipe stderr");
  7849. e->Release();
  7850. }
  7851. }
  7852. return MakeStringException(ROXIE_PIPE_ERROR, "Pipe process %s returned error %u%s", cmd, retcode, msg.str());
  7853. }
  7854. class CRoxieServerPipeReadActivity : public CRoxieServerActivity
  7855. {
  7856. IHThorPipeReadArg &helper;
  7857. Owned<IPipeProcess> pipe;
  7858. StringAttr pipeCommand;
  7859. Owned<IOutputRowDeserializer> rowDeserializer;
  7860. Owned<IReadRowStream> readTransformer;
  7861. bool groupSignalled;
  7862. public:
  7863. CRoxieServerPipeReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  7864. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorPipeReadArg &)basehelper)
  7865. {
  7866. groupSignalled = true;
  7867. }
  7868. virtual bool needsAllocator() const { return true; }
  7869. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  7870. {
  7871. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  7872. rowDeserializer.setown(rowAllocator->createDiskDeserializer(ctx->queryCodeContext()));
  7873. }
  7874. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  7875. {
  7876. groupSignalled = true; // i.e. don't start with a NULL row
  7877. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  7878. if (!readTransformer)
  7879. {
  7880. OwnedRoxieString xmlIteratorPath(helper.getXmlIteratorPath());
  7881. readTransformer.setown(createReadRowStream(rowAllocator, rowDeserializer, helper.queryXmlTransformer(), helper.queryCsvTransformer(), xmlIteratorPath, helper.getPipeFlags()));
  7882. }
  7883. OwnedRoxieString pipeProgram(helper.getPipeProgram());
  7884. openPipe(pipeProgram);
  7885. }
  7886. virtual void stop(bool aborting)
  7887. {
  7888. CRoxieServerActivity::stop(aborting);
  7889. pipe.clear();
  7890. readTransformer->setStream(NULL);
  7891. }
  7892. virtual const void *nextInGroup()
  7893. {
  7894. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  7895. while (!waitForPipe())
  7896. {
  7897. if (!pipe)
  7898. return NULL;
  7899. if (helper.getPipeFlags() & TPFgroupeachrow)
  7900. {
  7901. if (!groupSignalled)
  7902. {
  7903. groupSignalled = true;
  7904. return NULL;
  7905. }
  7906. }
  7907. }
  7908. const void *ret = readTransformer->next();
  7909. assertex(ret != NULL); // if ret can ever be NULL then we need to recode this logic
  7910. processed++;
  7911. groupSignalled = false;
  7912. return ret;
  7913. }
  7914. protected:
  7915. bool waitForPipe()
  7916. {
  7917. if (!pipe)
  7918. return false; // done
  7919. if (!readTransformer->eos())
  7920. return true;
  7921. verifyPipe();
  7922. return false;
  7923. }
  7924. void openPipe(char const * cmd)
  7925. {
  7926. pipeCommand.setown(cmd);
  7927. pipe.setown(createPipeProcess());
  7928. if(!pipe->run(NULL, cmd, ".", false, true, true, 0x10000))
  7929. throw MakeStringException(ROXIE_PIPE_ERROR, "Could not run pipe process %s", cmd);
  7930. Owned<ISimpleReadStream> pipeReader = pipe->getOutputStream();
  7931. readTransformer->setStream(pipeReader.get());
  7932. }
  7933. void verifyPipe()
  7934. {
  7935. if (pipe)
  7936. {
  7937. unsigned err = pipe->wait();
  7938. if(err && !(helper.getPipeFlags() & TPFnofail))
  7939. {
  7940. throw createPipeFailureException(pipeCommand.get(), err, pipe);
  7941. }
  7942. pipe.clear();
  7943. }
  7944. }
  7945. };
  7946. class CRoxieServerPipeThroughActivity : public CRoxieServerActivity, implements IRecordPullerCallback
  7947. {
  7948. IHThorPipeThroughArg &helper;
  7949. RecordPullerThread puller;
  7950. Owned<IPipeProcess> pipe;
  7951. StringAttr pipeCommand;
  7952. InterruptableSemaphore pipeVerified;
  7953. InterruptableSemaphore pipeOpened;
  7954. CachedOutputMetaData inputMeta;
  7955. Owned<IOutputRowSerializer> rowSerializer;
  7956. Owned<IOutputRowDeserializer> rowDeserializer;
  7957. Owned<IPipeWriteXformHelper> writeTransformer;
  7958. Owned<IReadRowStream> readTransformer;
  7959. bool firstRead;
  7960. bool recreate;
  7961. bool inputExhausted;
  7962. bool groupSignalled;
  7963. public:
  7964. CRoxieServerPipeThroughActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  7965. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorPipeThroughArg &)basehelper), puller(false)
  7966. {
  7967. recreate = helper.recreateEachRow();
  7968. groupSignalled = true;
  7969. firstRead = false;
  7970. inputExhausted = false;
  7971. }
  7972. virtual bool needsAllocator() const { return true; }
  7973. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  7974. {
  7975. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  7976. rowSerializer.setown(inputMeta.createDiskSerializer(ctx->queryCodeContext(), activityId));
  7977. rowDeserializer.setown(rowAllocator->createDiskDeserializer(ctx->queryCodeContext()));
  7978. writeTransformer.setown(createPipeWriteXformHelper(helper.getPipeFlags(), helper.queryXmlOutput(), helper.queryCsvOutput(), rowSerializer));
  7979. }
  7980. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  7981. {
  7982. firstRead = true;
  7983. inputExhausted = false;
  7984. groupSignalled = true; // i.e. don't start with a NULL row
  7985. pipeVerified.reinit();
  7986. pipeOpened.reinit();
  7987. writeTransformer->ready();
  7988. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  7989. if (!readTransformer)
  7990. {
  7991. OwnedRoxieString xmlIterator(helper.getXmlIteratorPath());
  7992. readTransformer.setown(createReadRowStream(rowAllocator, rowDeserializer, helper.queryXmlTransformer(), helper.queryCsvTransformer(), xmlIterator, helper.getPipeFlags()));
  7993. }
  7994. if(!recreate)
  7995. {
  7996. OwnedRoxieString pipeProgram(helper.getPipeProgram());
  7997. openPipe(pipeProgram);
  7998. }
  7999. puller.start(parentExtractSize, parentExtract, paused, 0, false, ctx); // Pipe does not support preload presently - locks up
  8000. }
  8001. virtual void setInput(unsigned idx, IRoxieInput *_in)
  8002. {
  8003. if (idx)
  8004. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
  8005. puller.setInput(this, _in);
  8006. inputMeta.set(_in->queryOutputMeta());
  8007. }
  8008. virtual void stop(bool aborting)
  8009. {
  8010. pipeVerified.interrupt(NULL);
  8011. pipeOpened.interrupt(NULL);
  8012. puller.stop(aborting);
  8013. CRoxieServerActivity::stop(aborting);
  8014. pipe.clear();
  8015. readTransformer->setStream(NULL);
  8016. }
  8017. virtual void reset()
  8018. {
  8019. puller.reset();
  8020. CRoxieServerActivity::reset();
  8021. }
  8022. virtual const void *nextInGroup()
  8023. {
  8024. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  8025. while (!waitForPipe())
  8026. {
  8027. if (!pipe)
  8028. return NULL;
  8029. if (helper.getPipeFlags() & TPFgroupeachrow)
  8030. {
  8031. if (!groupSignalled)
  8032. {
  8033. groupSignalled = true;
  8034. return NULL;
  8035. }
  8036. }
  8037. }
  8038. const void *ret = readTransformer->next();
  8039. assertex(ret != NULL); // if ret can ever be NULL then we need to recode this logic
  8040. processed++;
  8041. groupSignalled = false;
  8042. return ret;
  8043. }
  8044. virtual void processRow(const void *row)
  8045. {
  8046. // called from puller thread
  8047. if(recreate)
  8048. openPipe(helper.getNameFromRow(row));
  8049. writeTransformer->writeTranslatedText(row, pipe);
  8050. ReleaseRoxieRow(row);
  8051. if(recreate)
  8052. {
  8053. closePipe();
  8054. pipeVerified.wait();
  8055. }
  8056. }
  8057. virtual void processDone()
  8058. {
  8059. // called from puller thread
  8060. if(recreate)
  8061. {
  8062. inputExhausted = true;
  8063. pipeOpened.signal();
  8064. }
  8065. else
  8066. {
  8067. closePipe();
  8068. pipeVerified.wait();
  8069. }
  8070. }
  8071. virtual void processEOG()
  8072. {
  8073. }
  8074. void processGroup(const ConstPointerArray &)
  8075. {
  8076. throwUnexpected();
  8077. }
  8078. virtual bool fireException(IException *e)
  8079. {
  8080. pipeOpened.interrupt(LINK(e));
  8081. pipeVerified.interrupt(e);
  8082. return true;
  8083. }
  8084. private:
  8085. bool waitForPipe()
  8086. {
  8087. if (firstRead)
  8088. {
  8089. pipeOpened.wait();
  8090. firstRead = false;
  8091. }
  8092. if (!pipe)
  8093. return false; // done
  8094. if (!readTransformer->eos())
  8095. return true;
  8096. verifyPipe();
  8097. if (recreate && !inputExhausted)
  8098. pipeOpened.wait();
  8099. return false;
  8100. }
  8101. void openPipe(char const * cmd)
  8102. {
  8103. pipeCommand.setown(cmd);
  8104. pipe.setown(createPipeProcess());
  8105. if(!pipe->run(NULL, cmd, ".", true, true, true, 0x10000))
  8106. throw MakeStringException(ROXIE_PIPE_ERROR, "Could not run pipe process %s", cmd);
  8107. writeTransformer->writeHeader(pipe);
  8108. Owned<ISimpleReadStream> pipeReader = pipe->getOutputStream();
  8109. readTransformer->setStream(pipeReader.get());
  8110. pipeOpened.signal();
  8111. }
  8112. void closePipe()
  8113. {
  8114. writeTransformer->writeFooter(pipe);
  8115. pipe->closeInput();
  8116. }
  8117. void verifyPipe()
  8118. {
  8119. if (pipe)
  8120. {
  8121. unsigned err = pipe->wait();
  8122. if(err && !(helper.getPipeFlags() & TPFnofail))
  8123. {
  8124. throw createPipeFailureException(pipeCommand.get(), err, pipe);
  8125. }
  8126. pipe.clear();
  8127. pipeVerified.signal();
  8128. }
  8129. }
  8130. };
  8131. class CRoxieServerPipeWriteActivity : public CRoxieServerInternalSinkActivity
  8132. {
  8133. IHThorPipeWriteArg &helper;
  8134. Owned<IPipeProcess> pipe;
  8135. StringAttr pipeCommand;
  8136. CachedOutputMetaData inputMeta;
  8137. Owned<IOutputRowSerializer> rowSerializer;
  8138. Owned<IPipeWriteXformHelper> writeTransformer;
  8139. bool firstRead;
  8140. bool recreate;
  8141. bool inputExhausted;
  8142. public:
  8143. CRoxieServerPipeWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8144. : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorPipeWriteArg &)basehelper)
  8145. {
  8146. recreate = helper.recreateEachRow();
  8147. firstRead = false;
  8148. inputExhausted = false;
  8149. }
  8150. virtual bool needsAllocator() const { return true; }
  8151. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  8152. {
  8153. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  8154. inputMeta.set(input->queryOutputMeta());
  8155. rowSerializer.setown(inputMeta.createDiskSerializer(ctx->queryCodeContext(), activityId));
  8156. writeTransformer.setown(createPipeWriteXformHelper(helper.getPipeFlags(), helper.queryXmlOutput(), helper.queryCsvOutput(), rowSerializer));
  8157. }
  8158. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8159. {
  8160. firstRead = true;
  8161. inputExhausted = false;
  8162. writeTransformer->ready();
  8163. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  8164. if(!recreate)
  8165. {
  8166. OwnedRoxieString pipeProgram(helper.getPipeProgram());
  8167. openPipe(pipeProgram);
  8168. }
  8169. }
  8170. virtual void stop(bool aborting)
  8171. {
  8172. CRoxieServerActivity::stop(aborting);
  8173. pipe.clear();
  8174. }
  8175. virtual void onExecute()
  8176. {
  8177. loop
  8178. {
  8179. const void *row = input->nextInGroup();
  8180. if (!row)
  8181. {
  8182. row = input->nextInGroup();
  8183. if (!row)
  8184. break;
  8185. }
  8186. processed++;
  8187. if(recreate)
  8188. openPipe(helper.getNameFromRow(row));
  8189. writeTransformer->writeTranslatedText(row, pipe);
  8190. ReleaseRoxieRow(row);
  8191. if(recreate)
  8192. closePipe();
  8193. }
  8194. closePipe();
  8195. }
  8196. private:
  8197. void openPipe(char const * cmd)
  8198. {
  8199. pipeCommand.setown(cmd);
  8200. pipe.setown(createPipeProcess());
  8201. if(!pipe->run(NULL, cmd, ".", true, false, true, 0x10000))
  8202. throw MakeStringException(ROXIE_PIPE_ERROR, "Could not run pipe process %s", cmd);
  8203. writeTransformer->writeHeader(pipe);
  8204. }
  8205. void closePipe()
  8206. {
  8207. writeTransformer->writeFooter(pipe);
  8208. pipe->closeInput();
  8209. unsigned err = pipe->wait();
  8210. if(err && !(helper.getPipeFlags() & TPFnofail))
  8211. {
  8212. throw createPipeFailureException(pipeCommand.get(), err, pipe);
  8213. }
  8214. pipe.clear();
  8215. }
  8216. };
  8217. class CRoxieServerPipeReadActivityFactory : public CRoxieServerActivityFactory
  8218. {
  8219. public:
  8220. CRoxieServerPipeReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8221. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  8222. {
  8223. }
  8224. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  8225. {
  8226. return new CRoxieServerPipeReadActivity(this, _probeManager);
  8227. }
  8228. };
  8229. class CRoxieServerPipeThroughActivityFactory : public CRoxieServerActivityFactory
  8230. {
  8231. public:
  8232. CRoxieServerPipeThroughActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8233. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  8234. {
  8235. }
  8236. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  8237. {
  8238. return new CRoxieServerPipeThroughActivity(this, _probeManager);
  8239. }
  8240. };
  8241. class CRoxieServerPipeWriteActivityFactory : public CRoxieServerInternalSinkFactory
  8242. {
  8243. public:
  8244. CRoxieServerPipeWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
  8245. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot)
  8246. {
  8247. }
  8248. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  8249. {
  8250. return new CRoxieServerPipeWriteActivity(this, _probeManager);
  8251. }
  8252. };
  8253. IRoxieServerActivityFactory *createRoxieServerPipeReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8254. {
  8255. return new CRoxieServerPipeReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  8256. }
  8257. IRoxieServerActivityFactory *createRoxieServerPipeThroughActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8258. {
  8259. return new CRoxieServerPipeThroughActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  8260. }
  8261. IRoxieServerActivityFactory *createRoxieServerPipeWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
  8262. {
  8263. return new CRoxieServerPipeWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot);
  8264. }
  8265. //=====================================================================================================
  8266. class CRoxieServerStreamedIteratorActivity : public CRoxieServerActivity
  8267. {
  8268. IHThorStreamedIteratorArg &helper;
  8269. Owned<IRowStream> rows;
  8270. public:
  8271. CRoxieServerStreamedIteratorActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8272. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorStreamedIteratorArg &)basehelper)
  8273. {
  8274. }
  8275. ~CRoxieServerStreamedIteratorActivity()
  8276. {
  8277. }
  8278. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8279. {
  8280. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  8281. rows.setown(helper.createInput());
  8282. }
  8283. virtual void stop(bool aborting)
  8284. {
  8285. if (rows)
  8286. {
  8287. rows->stop();
  8288. rows.clear();
  8289. }
  8290. CRoxieServerActivity::stop(aborting);
  8291. }
  8292. virtual const void *nextInGroup()
  8293. {
  8294. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  8295. assertex(rows != NULL);
  8296. const void * next = rows->nextRow();
  8297. if (next)
  8298. processed++;
  8299. return next;
  8300. }
  8301. };
  8302. class CRoxieServerStreamedIteratorActivityFactory : public CRoxieServerActivityFactory
  8303. {
  8304. public:
  8305. CRoxieServerStreamedIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8306. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  8307. {
  8308. }
  8309. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  8310. {
  8311. return new CRoxieServerStreamedIteratorActivity(this, _probeManager);
  8312. }
  8313. };
  8314. IRoxieServerActivityFactory *createRoxieServerStreamedIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8315. {
  8316. return new CRoxieServerStreamedIteratorActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  8317. }
  8318. //=====================================================================================================
  8319. class CRoxieServerFilterActivity : public CRoxieServerLateStartActivity
  8320. {
  8321. IHThorFilterArg &helper;
  8322. bool anyThisGroup;
  8323. IRangeCompare * stepCompare;
  8324. public:
  8325. CRoxieServerFilterActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8326. : CRoxieServerLateStartActivity(_factory, _probeManager), helper((IHThorFilterArg &)basehelper)
  8327. {
  8328. anyThisGroup = false;
  8329. stepCompare = NULL;
  8330. }
  8331. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8332. {
  8333. anyThisGroup = false;
  8334. CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
  8335. lateStart(parentExtractSize, parentExtract, helper.canMatchAny());
  8336. stepCompare = NULL;
  8337. if (!eof)
  8338. {
  8339. IInputSteppingMeta * stepMeta = input->querySteppingMeta();
  8340. if (stepMeta)
  8341. stepCompare = stepMeta->queryCompare();
  8342. }
  8343. }
  8344. virtual const void * nextInGroup()
  8345. {
  8346. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  8347. if (eof)
  8348. return NULL;
  8349. loop
  8350. {
  8351. const void * ret = input->nextInGroup();
  8352. if (!ret)
  8353. {
  8354. //stop returning two NULLs in a row.
  8355. if (anyThisGroup)
  8356. {
  8357. anyThisGroup = false;
  8358. return NULL;
  8359. }
  8360. ret = input->nextInGroup();
  8361. if (!ret)
  8362. {
  8363. eof = true;
  8364. return NULL; // eof...
  8365. }
  8366. }
  8367. if (helper.isValid(ret))
  8368. {
  8369. anyThisGroup = true;
  8370. processed++;
  8371. return ret;
  8372. }
  8373. ReleaseRoxieRow(ret);
  8374. }
  8375. }
  8376. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  8377. {
  8378. //Could assert that this isn't grouped
  8379. // MORE - will need rethinking once we rethink the nextSteppedGE interface for global smart-stepping.
  8380. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  8381. if (eof)
  8382. return NULL;
  8383. loop
  8384. {
  8385. const void * ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  8386. if (!ret)
  8387. {
  8388. eof = true;
  8389. return NULL;
  8390. }
  8391. if (!wasCompleteMatch)
  8392. {
  8393. anyThisGroup = false; // RKC->GH - is this right??
  8394. return ret;
  8395. }
  8396. if (helper.isValid(ret))
  8397. {
  8398. anyThisGroup = true;
  8399. processed++;
  8400. return ret;
  8401. }
  8402. if (!stepExtra.returnMismatches())
  8403. {
  8404. ReleaseRoxieRow(ret);
  8405. return nextInGroup();
  8406. }
  8407. //If asked to return mismatches we are only interested in mismatches that will force the stepped
  8408. //condition to advance
  8409. if (stepCompare->docompare(ret, seek, numFields) != 0)
  8410. {
  8411. wasCompleteMatch = false;
  8412. anyThisGroup = false; // WHY?
  8413. return ret;
  8414. }
  8415. ReleaseRoxieRow(ret);
  8416. }
  8417. }
  8418. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  8419. {
  8420. return input->gatherConjunctions(collector);
  8421. }
  8422. virtual void resetEOF()
  8423. {
  8424. eof = prefiltered;
  8425. anyThisGroup = false;
  8426. input->resetEOF();
  8427. }
  8428. IInputSteppingMeta * querySteppingMeta()
  8429. {
  8430. return input->querySteppingMeta();
  8431. }
  8432. };
  8433. class CRoxieServerFilterActivityFactory : public CRoxieServerActivityFactory
  8434. {
  8435. public:
  8436. CRoxieServerFilterActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8437. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  8438. {
  8439. }
  8440. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  8441. {
  8442. return new CRoxieServerFilterActivity(this, _probeManager);
  8443. }
  8444. };
  8445. IRoxieServerActivityFactory *createRoxieServerFilterActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8446. {
  8447. return new CRoxieServerFilterActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  8448. }
  8449. //=====================================================================================================
  8450. class CRoxieServerFilterGroupActivity : public CRoxieServerLateStartActivity
  8451. {
  8452. IHThorFilterGroupArg &helper;
  8453. unsigned curIndex;
  8454. ConstPointerArray gathered;
  8455. IRangeCompare * stepCompare;
  8456. public:
  8457. CRoxieServerFilterGroupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8458. : CRoxieServerLateStartActivity(_factory, _probeManager), helper((IHThorFilterGroupArg &)basehelper)
  8459. {
  8460. curIndex = 0;
  8461. stepCompare = NULL;
  8462. }
  8463. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8464. {
  8465. CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
  8466. lateStart(parentExtractSize, parentExtract, helper.canMatchAny());//sets eof
  8467. assertex(eof == !helper.canMatchAny());
  8468. curIndex = 0;
  8469. stepCompare = NULL;
  8470. if (!eof)
  8471. {
  8472. IInputSteppingMeta * inputStepping = input->querySteppingMeta();
  8473. if (inputStepping)
  8474. stepCompare = inputStepping->queryCompare();
  8475. }
  8476. }
  8477. virtual void reset()
  8478. {
  8479. releaseGathered();
  8480. CRoxieServerLateStartActivity::reset();
  8481. }
  8482. inline void releaseGathered()
  8483. {
  8484. while (gathered.isItem(curIndex))
  8485. ReleaseRoxieRow(gathered.item(curIndex++));
  8486. gathered.kill();
  8487. }
  8488. virtual const void * nextInGroup()
  8489. {
  8490. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  8491. loop
  8492. {
  8493. if (eof)
  8494. return NULL;
  8495. if (gathered.ordinality())
  8496. {
  8497. if (gathered.isItem(curIndex))
  8498. {
  8499. const void * ret = gathered.item(curIndex++);
  8500. processed++;
  8501. return ret;
  8502. }
  8503. curIndex = 0;
  8504. gathered.kill();
  8505. return NULL;
  8506. }
  8507. const void * ret = input->nextInGroup();
  8508. while (ret)
  8509. {
  8510. gathered.append(ret);
  8511. ret = input->nextInGroup();
  8512. }
  8513. unsigned num = gathered.ordinality();
  8514. if (num != 0)
  8515. {
  8516. if (!helper.isValid(num, (const void * *)gathered.getArray()))
  8517. ReleaseRoxieRowSet(gathered); // read next group
  8518. }
  8519. else
  8520. eof = true;
  8521. }
  8522. }
  8523. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  8524. {
  8525. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  8526. if (eof)
  8527. return NULL;
  8528. if (gathered.ordinality())
  8529. {
  8530. while (gathered.isItem(curIndex))
  8531. {
  8532. const void * ret = gathered.item(curIndex++);
  8533. if (stepCompare->docompare(ret, seek, numFields) >= 0)
  8534. {
  8535. processed++;
  8536. return ret;
  8537. }
  8538. ReleaseRoxieRow(ret);
  8539. }
  8540. curIndex = 0;
  8541. gathered.kill();
  8542. //nextSteppedGE never returns an end of group marker.
  8543. }
  8544. //Not completely sure about this - it could lead the the start of a group being skipped,
  8545. //so the group filter could potentially work on a different group. If so, we'd need to check the
  8546. //next fields were a subset of the grouping fields - more an issue for the group activity.
  8547. //MORE: What do we do with wasCompleteMatch? something like the following????
  8548. #if 0
  8549. loop
  8550. {
  8551. const void * ret;
  8552. if (stepExtra.returnMismatches())
  8553. {
  8554. bool matchedCompletely = true;
  8555. ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  8556. if (!wasCompleteMatch)
  8557. return ret;
  8558. }
  8559. else
  8560. ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  8561. #endif
  8562. const void * ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  8563. while (ret)
  8564. {
  8565. gathered.append(ret);
  8566. ret = input->nextInGroup();
  8567. }
  8568. unsigned num = gathered.ordinality();
  8569. if (num != 0)
  8570. {
  8571. if (!helper.isValid(num, (const void * *)gathered.getArray()))
  8572. ReleaseRoxieRowSet(gathered); // read next group
  8573. }
  8574. else
  8575. eof = true;
  8576. return nextUngrouped(this);
  8577. }
  8578. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  8579. {
  8580. return input->gatherConjunctions(collector);
  8581. }
  8582. virtual void resetEOF()
  8583. {
  8584. eof = false;
  8585. releaseGathered();
  8586. input->resetEOF();
  8587. }
  8588. IInputSteppingMeta * querySteppingMeta()
  8589. {
  8590. return input->querySteppingMeta();
  8591. }
  8592. };
  8593. class CRoxieServerFilterGroupActivityFactory : public CRoxieServerActivityFactory
  8594. {
  8595. public:
  8596. CRoxieServerFilterGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8597. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  8598. {
  8599. }
  8600. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  8601. {
  8602. return new CRoxieServerFilterGroupActivity(this, _probeManager);
  8603. }
  8604. };
  8605. IRoxieServerActivityFactory *createRoxieServerFilterGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8606. {
  8607. return new CRoxieServerFilterGroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  8608. }
  8609. //=================================================================================
  8610. class CRoxieServerSideEffectActivity : public CRoxieServerActivity
  8611. {
  8612. IHThorSideEffectArg &helper;
  8613. CriticalSection ecrit;
  8614. Owned<IException> exception;
  8615. bool executed;
  8616. public:
  8617. CRoxieServerSideEffectActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8618. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorSideEffectArg &)basehelper)
  8619. {
  8620. executed = false;
  8621. }
  8622. virtual const void * nextInGroup()
  8623. {
  8624. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  8625. CriticalBlock b(ecrit);
  8626. if (exception)
  8627. throw(exception.getLink());
  8628. if (!executed)
  8629. {
  8630. try
  8631. {
  8632. executed = true;
  8633. helper.action();
  8634. }
  8635. catch(IException *E)
  8636. {
  8637. exception.set(E);
  8638. throw;
  8639. }
  8640. }
  8641. return NULL;
  8642. }
  8643. virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
  8644. {
  8645. CriticalBlock b(ecrit);
  8646. if (exception)
  8647. throw(exception.getLink());
  8648. if (!executed)
  8649. {
  8650. try
  8651. {
  8652. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  8653. executed = true;
  8654. start(parentExtractSize, parentExtract, false);
  8655. helper.action();
  8656. stop(false);
  8657. }
  8658. catch(IException *E)
  8659. {
  8660. ctx->notifyAbort(E);
  8661. stop(true);
  8662. exception.set(E);
  8663. throw;
  8664. }
  8665. }
  8666. }
  8667. virtual void reset()
  8668. {
  8669. executed = false;
  8670. exception.clear();
  8671. CRoxieServerActivity::reset();
  8672. }
  8673. };
  8674. class CRoxieServerSideEffectActivityFactory : public CRoxieServerActivityFactory
  8675. {
  8676. bool isRoot;
  8677. public:
  8678. CRoxieServerSideEffectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  8679. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
  8680. {
  8681. }
  8682. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  8683. {
  8684. return new CRoxieServerSideEffectActivity(this, _probeManager);
  8685. }
  8686. virtual bool isSink() const
  8687. {
  8688. return isRoot && !meta.queryOriginal();
  8689. }
  8690. };
  8691. IRoxieServerActivityFactory *createRoxieServerSideEffectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  8692. {
  8693. return new CRoxieServerSideEffectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
  8694. }
  8695. //=================================================================================
  8696. class CRoxieServerActionActivity : public CRoxieServerInternalSinkActivity
  8697. {
  8698. IHThorActionArg &helper;
  8699. public:
  8700. CRoxieServerActionActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8701. : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorActionArg &)basehelper)
  8702. {
  8703. }
  8704. virtual void onExecute()
  8705. {
  8706. helper.action();
  8707. }
  8708. };
  8709. class CRoxieServerActionActivityFactory : public CRoxieServerInternalSinkFactory
  8710. {
  8711. public:
  8712. CRoxieServerActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
  8713. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot)
  8714. {
  8715. }
  8716. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  8717. {
  8718. return new CRoxieServerActionActivity(this, _probeManager);
  8719. }
  8720. };
  8721. IRoxieServerActivityFactory *createRoxieServerActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
  8722. {
  8723. return new CRoxieServerActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot);
  8724. }
  8725. //=================================================================================
  8726. class CRoxieServerSampleActivity : public CRoxieServerActivity
  8727. {
  8728. IHThorSampleArg &helper;
  8729. unsigned numSamples;
  8730. unsigned numToSkip;
  8731. unsigned whichSample;
  8732. bool anyThisGroup;
  8733. bool eof;
  8734. public:
  8735. CRoxieServerSampleActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8736. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorSampleArg &)basehelper)
  8737. {
  8738. numSamples = 0;
  8739. numToSkip = 0;
  8740. whichSample = 0;
  8741. anyThisGroup = false;
  8742. eof = false;
  8743. }
  8744. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8745. {
  8746. anyThisGroup = false;
  8747. eof = false;
  8748. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  8749. numSamples = helper.getProportion();
  8750. whichSample = helper.getSampleNumber();
  8751. numToSkip = (whichSample ? whichSample-1 : 0);
  8752. }
  8753. virtual const void * nextInGroup()
  8754. {
  8755. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  8756. if (eof)
  8757. return NULL;
  8758. loop
  8759. {
  8760. const void * ret = input->nextInGroup();
  8761. if (!ret)
  8762. {
  8763. //this does work with groups - may or may not be useful...
  8764. //reset the sample for each group.... probably best.
  8765. numToSkip = (whichSample ? whichSample-1 : 0);
  8766. if (anyThisGroup)
  8767. {
  8768. anyThisGroup = false;
  8769. return NULL;
  8770. }
  8771. ret = input->nextInGroup();
  8772. if (!ret)
  8773. {
  8774. eof = true;
  8775. return NULL; // eof...
  8776. }
  8777. }
  8778. if (numToSkip == 0)
  8779. {
  8780. anyThisGroup = true;
  8781. numToSkip = numSamples-1;
  8782. processed++;
  8783. return ret;
  8784. }
  8785. numToSkip--;
  8786. ReleaseRoxieRow(ret);
  8787. }
  8788. }
  8789. };
  8790. class CRoxieServerSampleActivityFactory : public CRoxieServerActivityFactory
  8791. {
  8792. public:
  8793. CRoxieServerSampleActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8794. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  8795. {
  8796. }
  8797. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  8798. {
  8799. return new CRoxieServerSampleActivity(this, _probeManager);
  8800. }
  8801. };
  8802. IRoxieServerActivityFactory *createRoxieServerSampleActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8803. {
  8804. return new CRoxieServerSampleActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  8805. }
  8806. //=================================================================================
  8807. class CRoxieServerChooseSetsActivity : public CRoxieServerActivity
  8808. {
  8809. IHThorChooseSetsArg &helper;
  8810. unsigned numSets;
  8811. unsigned * setCounts;
  8812. bool done;
  8813. public:
  8814. CRoxieServerChooseSetsActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8815. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorChooseSetsArg &)basehelper)
  8816. {
  8817. setCounts = NULL;
  8818. numSets = 0;
  8819. done = false;
  8820. }
  8821. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8822. {
  8823. done = false;
  8824. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  8825. numSets = helper.getNumSets();
  8826. setCounts = new unsigned[numSets];
  8827. memset(setCounts, 0, sizeof(unsigned)*numSets);
  8828. helper.setCounts(setCounts);
  8829. }
  8830. virtual void reset()
  8831. {
  8832. delete [] setCounts;
  8833. setCounts = NULL;
  8834. CRoxieServerActivity::reset();
  8835. }
  8836. virtual const void * nextInGroup()
  8837. {
  8838. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  8839. if (done)
  8840. return NULL;
  8841. loop
  8842. {
  8843. const void * ret = input->nextInGroup();
  8844. if (!ret)
  8845. {
  8846. ret = input->nextInGroup();
  8847. if (!ret)
  8848. {
  8849. done = true;
  8850. return NULL;
  8851. }
  8852. }
  8853. processed++;
  8854. switch (helper.getRecordAction(ret))
  8855. {
  8856. case 2:
  8857. done = true;
  8858. return ret;
  8859. case 1:
  8860. return ret;
  8861. }
  8862. ReleaseRoxieRow(ret);
  8863. }
  8864. }
  8865. };
  8866. class CRoxieServerChooseSetsActivityFactory : public CRoxieServerActivityFactory
  8867. {
  8868. public:
  8869. CRoxieServerChooseSetsActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8870. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  8871. {
  8872. }
  8873. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  8874. {
  8875. return new CRoxieServerChooseSetsActivity(this, _probeManager);
  8876. }
  8877. };
  8878. IRoxieServerActivityFactory *createRoxieServerChooseSetsActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  8879. {
  8880. return new CRoxieServerChooseSetsActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  8881. }
  8882. //=================================================================================
  8883. class CRoxieServerChooseSetsExActivity : public CRoxieServerActivity
  8884. {
  8885. protected:
  8886. IHThorChooseSetsExArg &helper;
  8887. unsigned numSets;
  8888. unsigned curIndex;
  8889. unsigned * setCounts;
  8890. count_t * limits;
  8891. bool done;
  8892. ConstPointerArray gathered;
  8893. virtual bool includeRow(const void * row) = 0;
  8894. virtual void calculateSelection() = 0;
  8895. public:
  8896. CRoxieServerChooseSetsExActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8897. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorChooseSetsExArg &)basehelper)
  8898. {
  8899. setCounts = NULL;
  8900. limits = NULL;
  8901. done = false;
  8902. curIndex = 0;
  8903. numSets = 0;
  8904. }
  8905. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8906. {
  8907. done = false;
  8908. curIndex = 0;
  8909. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  8910. numSets = helper.getNumSets();
  8911. setCounts = new unsigned[numSets];
  8912. memset(setCounts, 0, sizeof(unsigned)*numSets);
  8913. limits = (count_t *)calloc(sizeof(count_t), numSets);
  8914. helper.getLimits(limits);
  8915. }
  8916. virtual void reset()
  8917. {
  8918. delete [] setCounts;
  8919. setCounts = NULL;
  8920. free(limits);
  8921. limits = NULL;
  8922. while (gathered.isItem(curIndex))
  8923. ReleaseRoxieRow(gathered.item(curIndex++));
  8924. gathered.kill();
  8925. CRoxieServerActivity::reset();
  8926. }
  8927. virtual const void * nextInGroup()
  8928. {
  8929. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  8930. if (gathered.ordinality() == 0)
  8931. {
  8932. curIndex = 0;
  8933. if (!input->nextGroup(gathered))
  8934. {
  8935. done = true;
  8936. return NULL;
  8937. }
  8938. ForEachItemIn(idx1, gathered)
  8939. {
  8940. unsigned category = helper.getCategory(gathered.item(idx1));
  8941. if (category)
  8942. setCounts[category-1]++;
  8943. }
  8944. calculateSelection();
  8945. }
  8946. while (gathered.isItem(curIndex))
  8947. {
  8948. const void * row = gathered.item(curIndex);
  8949. gathered.replace(NULL, curIndex);
  8950. curIndex++;
  8951. if (includeRow(row))
  8952. {
  8953. processed++;
  8954. return row;
  8955. }
  8956. ReleaseRoxieRow(row);
  8957. }
  8958. gathered.kill();
  8959. return NULL;
  8960. }
  8961. };
  8962. class CRoxieServerChooseSetsLastActivity : public CRoxieServerChooseSetsExActivity
  8963. {
  8964. unsigned * numToSkip;
  8965. public:
  8966. CRoxieServerChooseSetsLastActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager) : CRoxieServerChooseSetsExActivity(_factory, _probeManager)
  8967. {
  8968. numToSkip = NULL;
  8969. }
  8970. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8971. {
  8972. CRoxieServerChooseSetsExActivity::start(parentExtractSize, parentExtract, paused);
  8973. numToSkip = (unsigned *)calloc(sizeof(unsigned), numSets);
  8974. }
  8975. virtual void reset()
  8976. {
  8977. free(numToSkip);
  8978. numToSkip = NULL;
  8979. CRoxieServerChooseSetsExActivity::reset();
  8980. }
  8981. protected:
  8982. virtual void calculateSelection()
  8983. {
  8984. for (unsigned idx=0; idx < numSets; idx++)
  8985. {
  8986. if (setCounts[idx] < limits[idx])
  8987. numToSkip[idx] = 0;
  8988. else
  8989. numToSkip[idx] = (unsigned)(setCounts[idx] - limits[idx]);
  8990. }
  8991. }
  8992. virtual bool includeRow(const void * row)
  8993. {
  8994. unsigned category = helper.getCategory(row);
  8995. if (category)
  8996. {
  8997. if (numToSkip[category-1] == 0)
  8998. return true;
  8999. numToSkip[category-1]--;
  9000. }
  9001. return false;
  9002. }
  9003. };
  9004. class CRoxieServerChooseSetsEnthActivity : public CRoxieServerChooseSetsExActivity
  9005. {
  9006. count_t * counter;
  9007. public:
  9008. CRoxieServerChooseSetsEnthActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager) : CRoxieServerChooseSetsExActivity(_factory, _probeManager)
  9009. {
  9010. counter = NULL;
  9011. }
  9012. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9013. {
  9014. CRoxieServerChooseSetsExActivity::start(parentExtractSize, parentExtract, paused);
  9015. counter = (count_t *)calloc(sizeof(count_t), numSets);
  9016. }
  9017. virtual void reset()
  9018. {
  9019. free(counter);
  9020. counter = NULL;
  9021. CRoxieServerChooseSetsExActivity::reset();
  9022. }
  9023. protected:
  9024. virtual void calculateSelection()
  9025. {
  9026. }
  9027. virtual bool includeRow(const void * row)
  9028. {
  9029. unsigned category = helper.getCategory(row);
  9030. if (category)
  9031. {
  9032. assertex(category <= numSets);
  9033. counter[category-1] += limits[category-1];
  9034. if(counter[category-1] >= setCounts[category-1])
  9035. {
  9036. counter[category-1] -= setCounts[category-1];
  9037. return true;
  9038. }
  9039. }
  9040. return false;
  9041. }
  9042. };
  9043. class CRoxieServerChooseSetsEnthActivityFactory : public CRoxieServerActivityFactory
  9044. {
  9045. public:
  9046. CRoxieServerChooseSetsEnthActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  9047. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  9048. {
  9049. }
  9050. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  9051. {
  9052. return new CRoxieServerChooseSetsEnthActivity(this, _probeManager);
  9053. }
  9054. };
  9055. IRoxieServerActivityFactory *createRoxieServerChooseSetsEnthActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  9056. {
  9057. return new CRoxieServerChooseSetsEnthActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  9058. }
  9059. class CRoxieServerChooseSetsLastActivityFactory : public CRoxieServerActivityFactory
  9060. {
  9061. public:
  9062. CRoxieServerChooseSetsLastActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  9063. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  9064. {
  9065. }
  9066. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  9067. {
  9068. return new CRoxieServerChooseSetsLastActivity(this, _probeManager);
  9069. }
  9070. };
  9071. IRoxieServerActivityFactory *createRoxieServerChooseSetsLastActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  9072. {
  9073. return new CRoxieServerChooseSetsLastActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  9074. }
  9075. //=================================================================================
  9076. class CRoxieServerEnthActivity : public CRoxieServerActivity
  9077. {
  9078. IHThorEnthArg &helper;
  9079. unsigned __int64 numerator;
  9080. unsigned __int64 denominator;
  9081. unsigned __int64 counter;
  9082. bool eof;
  9083. public:
  9084. CRoxieServerEnthActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9085. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorEnthArg &)basehelper)
  9086. {
  9087. eof = false;
  9088. numerator = denominator = counter = 0;
  9089. }
  9090. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9091. {
  9092. eof = false;
  9093. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  9094. numerator = helper.getProportionNumerator();
  9095. denominator = helper.getProportionDenominator();
  9096. if(denominator == 0) denominator = 1; //MORE: simplest way to avoid disaster in this case
  9097. counter = (helper.getSampleNumber()-1) * greatestCommonDivisor(numerator, denominator);
  9098. if (counter >= denominator)
  9099. counter %= denominator;
  9100. }
  9101. inline bool wanted()
  9102. {
  9103. counter += numerator;
  9104. if(counter >= denominator)
  9105. {
  9106. counter -= denominator;
  9107. return true;
  9108. }
  9109. return false;
  9110. }
  9111. virtual const void * nextInGroup()
  9112. {
  9113. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  9114. if (eof)
  9115. return NULL;
  9116. const void * ret;
  9117. loop
  9118. {
  9119. ret = input->nextInGroup();
  9120. if(!ret) //end of group
  9121. ret = input->nextInGroup();
  9122. if(!ret) //eof
  9123. {
  9124. eof = true;
  9125. return ret;
  9126. }
  9127. if (wanted())
  9128. return ret;
  9129. ReleaseRoxieRow(ret);
  9130. }
  9131. }
  9132. };
  9133. class CRoxieServerEnthActivityFactory : public CRoxieServerActivityFactory
  9134. {
  9135. public:
  9136. CRoxieServerEnthActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  9137. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  9138. {
  9139. }
  9140. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  9141. {
  9142. return new CRoxieServerEnthActivity(this, _probeManager);
  9143. }
  9144. };
  9145. IRoxieServerActivityFactory *createRoxieServerEnthActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  9146. {
  9147. return new CRoxieServerEnthActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  9148. }
  9149. //=================================================================================
  9150. class CRoxieServerAggregateActivity : public CRoxieServerActivity
  9151. {
  9152. IHThorAggregateArg &helper;
  9153. bool eof;
  9154. bool isInputGrouped;
  9155. bool abortEarly;
  9156. public:
  9157. CRoxieServerAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9158. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorAggregateArg &)basehelper)
  9159. {
  9160. eof = false;
  9161. isInputGrouped = false;
  9162. abortEarly = false;
  9163. }
  9164. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9165. {
  9166. eof = false;
  9167. isInputGrouped = input->queryOutputMeta()->isGrouped(); // could be done earlier, in setInput?
  9168. abortEarly = !isInputGrouped && (factory->getKind() == TAKexistsaggregate); // ditto
  9169. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  9170. }
  9171. virtual bool needsAllocator() const { return true; }
  9172. virtual const void * nextInGroup()
  9173. {
  9174. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  9175. if (eof)
  9176. return NULL;
  9177. const void * next = input->nextInGroup();
  9178. if (!next && isInputGrouped)
  9179. {
  9180. eof = true;
  9181. return NULL;
  9182. }
  9183. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  9184. size32_t finalSize = helper.clearAggregate(rowBuilder);
  9185. if (next)
  9186. {
  9187. finalSize = helper.processFirst(rowBuilder, next);
  9188. ReleaseRoxieRow(next);
  9189. if (!abortEarly)
  9190. {
  9191. loop
  9192. {
  9193. next = input->nextInGroup();
  9194. if (!next)
  9195. break;
  9196. finalSize = helper.processNext(rowBuilder, next);
  9197. ReleaseRoxieRow(next);
  9198. }
  9199. }
  9200. }
  9201. if (!isInputGrouped) // either read all, or aborted early
  9202. eof = true;
  9203. processed++;
  9204. return rowBuilder.finalizeRowClear(finalSize);
  9205. }
  9206. };
  9207. class CRoxieServerAggregateActivityFactory : public CRoxieServerActivityFactory
  9208. {
  9209. public:
  9210. CRoxieServerAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  9211. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  9212. {
  9213. }
  9214. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  9215. {
  9216. return new CRoxieServerAggregateActivity(this, _probeManager);
  9217. }
  9218. };
  9219. IRoxieServerActivityFactory *createRoxieServerAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  9220. {
  9221. return new CRoxieServerAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  9222. }
  9223. //=================================================================================
  9224. typedef unsigned t_hashPrefix;
  9225. class CRoxieServerHashAggregateActivity : public CRoxieServerActivity
  9226. {
  9227. IHThorHashAggregateArg &helper;
  9228. RowAggregator aggregated;
  9229. bool eof;
  9230. bool gathered;
  9231. bool isGroupedAggregate;
  9232. public:
  9233. CRoxieServerHashAggregateActivity(const IRoxieServerActivityFactory *_factory, bool _isGroupedAggregate, IProbeManager *_probeManager)
  9234. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorHashAggregateArg &)basehelper),
  9235. isGroupedAggregate(_isGroupedAggregate),
  9236. aggregated(helper, helper)
  9237. {
  9238. eof = false;
  9239. gathered = false;
  9240. }
  9241. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9242. {
  9243. eof = false;
  9244. gathered = false;
  9245. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  9246. }
  9247. virtual void reset()
  9248. {
  9249. aggregated.reset();
  9250. CRoxieServerActivity::reset();
  9251. }
  9252. virtual bool needsAllocator() const { return true; }
  9253. virtual const void * nextInGroup()
  9254. {
  9255. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  9256. if (eof)
  9257. return NULL;
  9258. if (!gathered)
  9259. {
  9260. aggregated.start(rowAllocator);
  9261. bool eog = true;
  9262. loop
  9263. {
  9264. const void * next = input->nextInGroup();
  9265. if (!next)
  9266. {
  9267. if (isGroupedAggregate)
  9268. {
  9269. if (eog)
  9270. eof = true;
  9271. break;
  9272. }
  9273. next = input->nextInGroup();
  9274. if (!next)
  9275. break;
  9276. }
  9277. eog = false;
  9278. aggregated.addRow(next);
  9279. ReleaseRoxieRow(next);
  9280. }
  9281. gathered = true;
  9282. }
  9283. Owned<AggregateRowBuilder> next = aggregated.nextResult();
  9284. if (next)
  9285. {
  9286. processed++;
  9287. return next->finalizeRowClear();
  9288. }
  9289. if (!isGroupedAggregate)
  9290. eof = true;
  9291. aggregated.reset();
  9292. gathered = false;
  9293. return NULL;
  9294. }
  9295. };
  9296. class CRoxieServerHashAggregateActivityFactory : public CRoxieServerActivityFactory
  9297. {
  9298. public:
  9299. CRoxieServerHashAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9300. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  9301. {
  9302. isGroupedAggregate = _graphNode.getPropBool("att[@name='grouped']/@value");
  9303. }
  9304. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  9305. {
  9306. return new CRoxieServerHashAggregateActivity(this, isGroupedAggregate, _probeManager);
  9307. }
  9308. protected:
  9309. bool isGroupedAggregate;
  9310. };
  9311. IRoxieServerActivityFactory *createRoxieServerHashAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9312. {
  9313. return new CRoxieServerHashAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  9314. }
  9315. //=================================================================================
  9316. class CRoxieServerDegroupActivity : public CRoxieServerActivity
  9317. {
  9318. IHThorDegroupArg &helper;
  9319. bool eof;
  9320. public:
  9321. CRoxieServerDegroupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9322. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorDegroupArg &)basehelper)
  9323. {
  9324. eof = false;
  9325. }
  9326. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9327. {
  9328. eof = false;
  9329. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  9330. }
  9331. virtual const void * nextInGroup()
  9332. {
  9333. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  9334. if (eof)
  9335. return NULL;
  9336. const void * ret = input->nextInGroup();
  9337. if (!ret)
  9338. ret = input->nextInGroup();
  9339. if (ret)
  9340. processed++;
  9341. else
  9342. eof = true;
  9343. return ret;
  9344. }
  9345. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  9346. {
  9347. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  9348. if (eof)
  9349. return NULL;
  9350. const void * ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  9351. if (ret)
  9352. processed++;
  9353. else
  9354. eof = true;
  9355. return ret;
  9356. }
  9357. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  9358. {
  9359. return input->gatherConjunctions(collector);
  9360. }
  9361. virtual void resetEOF()
  9362. {
  9363. eof = false;
  9364. input->resetEOF();
  9365. }
  9366. IInputSteppingMeta * querySteppingMeta()
  9367. {
  9368. return input->querySteppingMeta();
  9369. }
  9370. };
  9371. class CRoxieServerDegroupActivityFactory : public CRoxieServerActivityFactory
  9372. {
  9373. public:
  9374. CRoxieServerDegroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  9375. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  9376. {
  9377. }
  9378. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  9379. {
  9380. return new CRoxieServerDegroupActivity(this, _probeManager);
  9381. }
  9382. };
  9383. IRoxieServerActivityFactory *createRoxieServerDegroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  9384. {
  9385. return new CRoxieServerDegroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  9386. }
  9387. //=================================================================================
  9388. class CRoxieServerSpillReadActivity : public CRoxieServerActivity
  9389. {
  9390. IHThorDiskReadArg &helper;
  9391. bool needTransform;
  9392. bool eof;
  9393. bool anyThisGroup;
  9394. unsigned __int64 rowLimit;
  9395. unsigned __int64 choosenLimit;
  9396. public:
  9397. CRoxieServerSpillReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9398. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorDiskReadArg &)basehelper)
  9399. {
  9400. needTransform = helper.needTransform();
  9401. rowLimit = (unsigned __int64) -1;
  9402. choosenLimit = 0;
  9403. eof = false;
  9404. anyThisGroup = false;
  9405. }
  9406. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9407. {
  9408. anyThisGroup = false;
  9409. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  9410. if (helper.canMatchAny())
  9411. eof = false;
  9412. else
  9413. eof = true;
  9414. choosenLimit = helper.getChooseNLimit();
  9415. rowLimit = helper.getRowLimit();
  9416. helper.setCallback(NULL); // members should not be called - change if they are
  9417. }
  9418. virtual bool needsAllocator() const { return true; }
  9419. virtual const void * nextInGroup()
  9420. {
  9421. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  9422. if (eof)
  9423. return NULL;
  9424. if (processed==choosenLimit)
  9425. {
  9426. eof = true;
  9427. return NULL;
  9428. }
  9429. if (needTransform)
  9430. {
  9431. loop
  9432. {
  9433. const void *in = input->nextInGroup();
  9434. if (!in)
  9435. {
  9436. if (anyThisGroup)
  9437. {
  9438. anyThisGroup = false;
  9439. return NULL;
  9440. }
  9441. in = input->nextInGroup();
  9442. if (!in)
  9443. {
  9444. eof = true;
  9445. return NULL; // eof...
  9446. }
  9447. }
  9448. unsigned outSize;
  9449. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  9450. try
  9451. {
  9452. outSize = helper.transform(rowBuilder, in);
  9453. ReleaseRoxieRow(in);
  9454. }
  9455. catch (IException *E)
  9456. {
  9457. throw makeWrappedException(E);
  9458. }
  9459. if (outSize)
  9460. {
  9461. anyThisGroup = true;
  9462. processed++;
  9463. if (processed==rowLimit)
  9464. {
  9465. if (traceLevel > 4)
  9466. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  9467. helper.onLimitExceeded();
  9468. }
  9469. return rowBuilder.finalizeRowClear(outSize);
  9470. }
  9471. }
  9472. }
  9473. else
  9474. {
  9475. const void *ret = input->nextInGroup();
  9476. if (ret)
  9477. {
  9478. processed++;
  9479. if (processed==rowLimit)
  9480. {
  9481. if (traceLevel > 4)
  9482. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  9483. ReleaseClearRoxieRow(ret);
  9484. helper.onLimitExceeded(); // should not return
  9485. throwUnexpected();
  9486. }
  9487. }
  9488. return ret;
  9489. }
  9490. }
  9491. };
  9492. class CRoxieServerSpillReadActivityFactory : public CRoxieServerActivityFactory
  9493. {
  9494. public:
  9495. CRoxieServerSpillReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  9496. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  9497. {
  9498. }
  9499. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  9500. {
  9501. return new CRoxieServerSpillReadActivity(this, _probeManager);
  9502. }
  9503. virtual void addDependency(unsigned source, ThorActivityKind sourceKind, unsigned sourceIdx, int controlId, const char *edgeId)
  9504. {
  9505. if (sourceKind==TAKspill || sourceKind==TAKdiskwrite) // Bit of a hack - codegen probably should differentiate
  9506. setInput(0, source, sourceIdx);
  9507. else
  9508. CRoxieServerActivityFactory::addDependency(source, kind, sourceIdx, controlId, edgeId);
  9509. }
  9510. };
  9511. IRoxieServerActivityFactory *createRoxieServerSpillReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  9512. {
  9513. return new CRoxieServerSpillReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  9514. }
  9515. //=================================================================================
  9516. class CRoxieServerSpillWriteActivity : public CRoxieServerActivity
  9517. {
  9518. public:
  9519. CRoxieServerSpillWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9520. : CRoxieServerActivity(_factory, _probeManager)
  9521. {
  9522. }
  9523. ~CRoxieServerSpillWriteActivity()
  9524. {
  9525. }
  9526. virtual const void *nextInGroup()
  9527. {
  9528. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  9529. return input->nextInGroup();
  9530. }
  9531. };
  9532. //==================================================================================
  9533. class CRoxieServerDiskWriteActivity : public CRoxieServerInternalSinkActivity, implements IRoxiePublishCallback
  9534. {
  9535. protected:
  9536. Owned<IExtRowWriter> outSeq;
  9537. Owned<IOutputRowSerializer> rowSerializer;
  9538. Linked<IFileIOStream> diskout;
  9539. bool blockcompressed;
  9540. bool extend;
  9541. bool overwrite;
  9542. bool encrypted;
  9543. bool grouped;
  9544. IHThorDiskWriteArg &helper;
  9545. StringBuffer lfn; // logical filename
  9546. CachedOutputMetaData diskmeta;
  9547. Owned<IRoxieWriteHandler> writer;
  9548. bool tallycrc;
  9549. unsigned __int64 uncompressedBytesWritten;
  9550. CRC32 crc;
  9551. void updateWorkUnitResult(unsigned __int64 reccount)
  9552. {
  9553. assertex(writer);
  9554. // MORE - a lot of this is common with hthor
  9555. if(lfn.length()) //this is required as long as temp files don't get a name which can be stored in the WU and automatically deleted by the WU
  9556. {
  9557. WorkunitUpdate wu = ctx->updateWorkUnit();
  9558. if (wu)
  9559. {
  9560. unsigned flags = helper.getFlags();
  9561. WUFileKind fileKind;
  9562. if (TDXtemporary & flags)
  9563. fileKind = WUFileTemporary;
  9564. else if(TDXjobtemp & flags)
  9565. fileKind = WUFileJobOwned;
  9566. else if(TDWowned & flags)
  9567. fileKind = WUFileOwned;
  9568. else
  9569. fileKind = WUFileStandard;
  9570. StringArray clusters;
  9571. writer->getClusters(clusters);
  9572. wu->addFile(lfn.str(), &clusters, helper.getTempUsageCount(), fileKind, NULL);
  9573. if (!(flags & TDXtemporary) && helper.getSequence() >= 0)
  9574. {
  9575. Owned<IWUResult> result = wu->updateResultBySequence(helper.getSequence());
  9576. if (result)
  9577. {
  9578. result->setResultTotalRowCount(reccount);
  9579. result->setResultStatus(ResultStatusCalculated);
  9580. if (helper.getFlags() & TDWresult)
  9581. result->setResultFilename(lfn.str());
  9582. else
  9583. result->setResultLogicalName(lfn.str());
  9584. }
  9585. }
  9586. }
  9587. }
  9588. }
  9589. void resolve()
  9590. {
  9591. OwnedRoxieString rawLogicalName = helper.getFileName();
  9592. assertex(rawLogicalName);
  9593. assertex((helper.getFlags() & TDXtemporary) == 0);
  9594. StringArray clusters;
  9595. unsigned clusterIdx = 0;
  9596. while(true)
  9597. {
  9598. OwnedRoxieString cluster(helper.getCluster(clusterIdx));
  9599. if(!cluster)
  9600. break;
  9601. clusters.append(cluster);
  9602. clusterIdx++;
  9603. }
  9604. if (clusters.length())
  9605. {
  9606. if (extend)
  9607. throw MakeStringException(0, "Cannot combine EXTEND and CLUSTER flags on disk write of file %s", rawLogicalName.get());
  9608. }
  9609. else
  9610. {
  9611. if (roxieName.length())
  9612. clusters.append(roxieName.str());
  9613. else
  9614. clusters.append(".");
  9615. }
  9616. writer.setown(ctx->createLFN(rawLogicalName, overwrite, extend, clusters)); // MORE - if there's a workunit, use if for scope.
  9617. // MORE - need to check somewhere that single part if it's an existing file or an external one...
  9618. }
  9619. public:
  9620. CRoxieServerDiskWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9621. : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorDiskWriteArg &)basehelper)
  9622. {
  9623. extend = ((helper.getFlags() & TDWextend) != 0);
  9624. overwrite = ((helper.getFlags() & TDWoverwrite) != 0);
  9625. grouped = false; // don't think we need to support it...
  9626. diskmeta.set(helper.queryDiskRecordSize());
  9627. blockcompressed = (((helper.getFlags() & TDWnewcompress) != 0) || (((helper.getFlags() & TDXcompress) != 0) && (diskmeta.getFixedSize() >= MIN_ROWCOMPRESS_RECSIZE))); //always use new compression
  9628. encrypted = false; // set later
  9629. tallycrc = true;
  9630. uncompressedBytesWritten = 0;
  9631. }
  9632. ~CRoxieServerDiskWriteActivity()
  9633. {
  9634. }
  9635. virtual bool needsAllocator() const
  9636. {
  9637. return true;
  9638. }
  9639. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9640. {
  9641. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  9642. resolve();
  9643. Owned<IFileIO> io;
  9644. void *ekey;
  9645. size32_t ekeylen;
  9646. helper.getEncryptKey(ekeylen, ekey);
  9647. Owned<ICompressor> ecomp;
  9648. if (ekeylen!=0)
  9649. {
  9650. ecomp.setown(createAESCompressor256(ekeylen,ekey));
  9651. memset(ekey,0,ekeylen);
  9652. rtlFree(ekey);
  9653. encrypted = true;
  9654. blockcompressed = true;
  9655. }
  9656. if (blockcompressed)
  9657. io.setown(createCompressedFileWriter(writer->queryFile(), (diskmeta.isFixedSize() ? diskmeta.getFixedSize() : 0), extend, true, ecomp));
  9658. else
  9659. io.setown(writer->queryFile()->open(extend ? IFOwrite : IFOcreate));
  9660. if (!io)
  9661. throw MakeStringException(errno, "Failed to create%s file %s for writing", (encrypted ? " encrypted" : (blockcompressed ? " compressed" : "")), writer->queryFile()->queryFilename());
  9662. diskout.setown(createBufferedIOStream(io));
  9663. if (extend)
  9664. diskout->seek(0, IFSend);
  9665. tallycrc = !factory->queryQueryFactory().getDebugValueBool("skipFileFormatCrcCheck", false) && !(helper.getFlags() & TDRnocrccheck) && !blockcompressed;
  9666. Owned<IRowInterfaces> rowIf = createRowInterfaces(input->queryOutputMeta(), activityId, ctx->queryCodeContext());
  9667. rowSerializer.set(rowIf->queryRowSerializer());
  9668. unsigned rwFlags = rw_autoflush;
  9669. if(grouped)
  9670. rwFlags |= rw_grouped;
  9671. if(tallycrc)
  9672. rwFlags |= rw_crc;
  9673. if(!factory->queryQueryFactory().getDebugValueBool("skipFileFormatCrcCheck", false) && !(helper.getFlags() & TDRnocrccheck))
  9674. rwFlags |= rw_crc;
  9675. outSeq.setown(createRowWriter(diskout, rowIf, rwFlags));
  9676. }
  9677. virtual void stop(bool aborting)
  9678. {
  9679. if (aborting)
  9680. {
  9681. if (writer)
  9682. writer->finish(false, this);
  9683. }
  9684. else
  9685. {
  9686. outSeq->flush(&crc);
  9687. updateWorkUnitResult(processed);
  9688. uncompressedBytesWritten = outSeq->getPosition();
  9689. writer->finish(true, this);
  9690. }
  9691. writer.clear();
  9692. CRoxieServerActivity::stop(aborting);
  9693. }
  9694. virtual void reset()
  9695. {
  9696. CRoxieServerActivity::reset();
  9697. diskout.clear();
  9698. outSeq.clear();
  9699. writer.clear();
  9700. uncompressedBytesWritten = 0;
  9701. crc.reset();
  9702. }
  9703. virtual void onExecute()
  9704. {
  9705. loop
  9706. {
  9707. const void *nextrec = input->nextInGroup();
  9708. if (!nextrec)
  9709. {
  9710. nextrec = input->nextInGroup();
  9711. if (!nextrec)
  9712. break;
  9713. }
  9714. processed++;
  9715. outSeq->putRow(nextrec);
  9716. }
  9717. }
  9718. virtual void setFileProperties(IFileDescriptor *desc) const
  9719. {
  9720. IPropertyTree &partProps = desc->queryPart(0)->queryProperties(); //properties of the first file part.
  9721. IPropertyTree &fileProps = desc->queryProperties(); // properties of the logical file
  9722. if (blockcompressed)
  9723. {
  9724. // caller has already set @size from file size...
  9725. fileProps.setPropBool("@blockCompressed", true);
  9726. fileProps.setPropInt64("@compressedSize", partProps.getPropInt64("@size", 0));
  9727. partProps.setPropInt64("@compressedSize", partProps.getPropInt64("@size", 0));
  9728. fileProps.setPropInt64("@size", uncompressedBytesWritten);
  9729. partProps.setPropInt64("@size", uncompressedBytesWritten);
  9730. }
  9731. else if (tallycrc)
  9732. partProps.setPropInt64("@fileCrc", crc.get());
  9733. if (encrypted)
  9734. fileProps.setPropBool("@encrypted", true);
  9735. fileProps.setPropInt64("@recordCount", processed);
  9736. unsigned flags = helper.getFlags();
  9737. if (flags & TDWpersist)
  9738. fileProps.setPropBool("@persistent", true);
  9739. if (grouped)
  9740. fileProps.setPropBool("@grouped", true);
  9741. if (flags & (TDWowned|TDXjobtemp|TDXtemporary))
  9742. fileProps.setPropBool("@owned", true);
  9743. if (flags & TDWresult)
  9744. fileProps.setPropBool("@result", true);
  9745. IConstWorkUnit *workUnit = ctx->queryWorkUnit();
  9746. if (workUnit)
  9747. {
  9748. SCMStringBuffer owner, wuid, job;
  9749. fileProps.setProp("@owner", workUnit->getUser(owner).str());
  9750. fileProps.setProp("@workunit", workUnit->getWuid(wuid).str());
  9751. fileProps.setProp("@job", workUnit->getJobName(job).str());
  9752. }
  9753. if (flags & TDWexpires)
  9754. setExpiryTime(fileProps, helper.getExpiryDays());
  9755. if (flags & TDWupdate)
  9756. {
  9757. unsigned eclCRC;
  9758. unsigned __int64 totalCRC;
  9759. helper.getUpdateCRCs(eclCRC, totalCRC);
  9760. fileProps.setPropInt("@eclCRC", eclCRC);
  9761. fileProps.setPropInt64("@totalCRC", totalCRC);
  9762. }
  9763. fileProps.setPropInt("@formatCrc", helper.getFormatCrc());
  9764. IRecordSize * inputMeta = input->queryOutputMeta();
  9765. if ((inputMeta->isFixedSize()) && !isOutputTransformed())
  9766. fileProps.setPropInt("@recordSize", inputMeta->getFixedSize() + (grouped ? 1 : 0));
  9767. const char *recordECL = helper.queryRecordECL();
  9768. if (recordECL && *recordECL)
  9769. fileProps.setProp("ECL", recordECL);
  9770. fileProps.setProp("@kind", "flat"); // default, derivitives may override
  9771. }
  9772. virtual IUserDescriptor *queryUserDescriptor() const
  9773. {
  9774. IConstWorkUnit *workUnit = ctx->queryWorkUnit();
  9775. if (workUnit)
  9776. return workUnit->queryUserDescriptor();
  9777. else
  9778. return NULL;
  9779. }
  9780. virtual bool isOutputTransformed() const { return false; }
  9781. };
  9782. //=================================================================================
  9783. class CRoxieServerCsvWriteActivity : public CRoxieServerDiskWriteActivity
  9784. {
  9785. IHThorCsvWriteArg &csvHelper;
  9786. CSVOutputStream csvOutput;
  9787. public:
  9788. CRoxieServerCsvWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9789. : CRoxieServerDiskWriteActivity(_factory, _probeManager), csvHelper(static_cast<IHThorCsvWriteArg &>(helper))
  9790. {
  9791. csvOutput.init(csvHelper.queryCsvParameters(), false);
  9792. }
  9793. virtual void onExecute()
  9794. {
  9795. OwnedRoxieString header(csvHelper.queryCsvParameters()->getHeader());
  9796. if (header)
  9797. {
  9798. csvOutput.beginLine();
  9799. csvOutput.writeHeaderLn(strlen(header), header);
  9800. diskout->write(csvOutput.length(), csvOutput.str());
  9801. }
  9802. loop
  9803. {
  9804. const void *nextrec = input->nextInGroup();
  9805. if (!nextrec)
  9806. {
  9807. nextrec = input->nextInGroup();
  9808. if (!nextrec)
  9809. break;
  9810. }
  9811. processed++;
  9812. csvOutput.beginLine();
  9813. csvHelper.writeRow((const byte *)nextrec, &csvOutput);
  9814. csvOutput.endLine();
  9815. diskout->write(csvOutput.length(), csvOutput.str());
  9816. ReleaseRoxieRow(nextrec);
  9817. }
  9818. OwnedRoxieString footer(csvHelper.queryCsvParameters()->getFooter());
  9819. if (footer)
  9820. {
  9821. csvOutput.beginLine();
  9822. csvOutput.writeHeaderLn(strlen(footer), footer);
  9823. diskout->write(csvOutput.length(), csvOutput.str());
  9824. }
  9825. }
  9826. virtual void setFileProperties(IFileDescriptor *desc) const
  9827. {
  9828. CRoxieServerDiskWriteActivity::setFileProperties(desc);
  9829. IPropertyTree &props = desc->queryProperties();
  9830. props.setProp("@format","utf8n");
  9831. ICsvParameters *csvParameters = csvHelper.queryCsvParameters();
  9832. StringBuffer separator;
  9833. OwnedRoxieString rs(csvParameters->getSeparator(0));
  9834. const char *s = rs;
  9835. while (s && *s)
  9836. {
  9837. if (',' == *s)
  9838. separator.append("\\,");
  9839. else
  9840. separator.append(*s);
  9841. ++s;
  9842. }
  9843. props.setProp("@csvSeparate", separator.str());
  9844. props.setProp("@csvQuote", rs.setown(csvParameters->getQuote(0)));
  9845. props.setProp("@csvTerminate", rs.setown(csvParameters->getTerminator(0)));
  9846. props.setProp("@csvEscape", rs.setown(csvParameters->getEscape(0)));
  9847. props.setProp("@kind", "csv");
  9848. }
  9849. virtual bool isOutputTransformed() const { return true; }
  9850. };
  9851. class CRoxieServerXmlWriteActivity : public CRoxieServerDiskWriteActivity
  9852. {
  9853. IHThorXmlWriteArg &xmlHelper;
  9854. StringAttr rowTag;
  9855. public:
  9856. CRoxieServerXmlWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9857. : CRoxieServerDiskWriteActivity(_factory, _probeManager), xmlHelper(static_cast<IHThorXmlWriteArg &>(helper))
  9858. {
  9859. }
  9860. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9861. {
  9862. CRoxieServerDiskWriteActivity::start(parentExtractSize, parentExtract, paused);
  9863. OwnedRoxieString xmlpath(xmlHelper.getXmlIteratorPath());
  9864. if (!xmlpath)
  9865. rowTag.set("Row");
  9866. else
  9867. {
  9868. const char *path = xmlpath;
  9869. if (*path == '/') path++;
  9870. if (strchr(path, '/')) UNIMPLEMENTED; // more what do we do with /mydata/row
  9871. rowTag.set(path);
  9872. }
  9873. }
  9874. virtual void onExecute()
  9875. {
  9876. OwnedRoxieString suppliedHeader(xmlHelper.getHeader());
  9877. const char *header = suppliedHeader;
  9878. if (!header) header = "<Dataset>\n";
  9879. diskout->write(strlen(header), header);
  9880. CommonXmlWriter xmlOutput(xmlHelper.getXmlFlags());
  9881. loop
  9882. {
  9883. OwnedConstRoxieRow nextrec = input->nextInGroup();
  9884. if (!nextrec)
  9885. {
  9886. nextrec.setown(input->nextInGroup());
  9887. if (!nextrec)
  9888. break;
  9889. }
  9890. processed++;
  9891. xmlOutput.clear().outputBeginNested(rowTag, false);
  9892. xmlHelper.toXML((const byte *)nextrec.get(), xmlOutput);
  9893. xmlOutput.outputEndNested(rowTag);
  9894. diskout->write(xmlOutput.length(), xmlOutput.str());
  9895. }
  9896. OwnedRoxieString suppliedFooter(xmlHelper.getFooter());
  9897. const char * footer = suppliedFooter;
  9898. if (!footer) footer = "</Dataset>\n";
  9899. diskout->write(strlen(footer), footer);
  9900. }
  9901. virtual void reset()
  9902. {
  9903. CRoxieServerDiskWriteActivity::reset();
  9904. rowTag.clear();
  9905. }
  9906. virtual void setFileProperties(IFileDescriptor *desc) const
  9907. {
  9908. CRoxieServerDiskWriteActivity::setFileProperties(desc);
  9909. desc->queryProperties().setProp("@format","utf8n");
  9910. desc->queryProperties().setProp("@rowTag",rowTag.get());
  9911. desc->queryProperties().setProp("@kind", "xml");
  9912. }
  9913. virtual bool isOutputTransformed() const { return true; }
  9914. };
  9915. class CRoxieServerDiskWriteActivityFactory : public CRoxieServerMultiOutputFactory
  9916. {
  9917. bool isRoot;
  9918. bool isTemp;
  9919. public:
  9920. CRoxieServerDiskWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  9921. : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
  9922. {
  9923. Owned<IHThorDiskWriteArg> helper = (IHThorDiskWriteArg *) helperFactory();
  9924. isTemp = (helper->getFlags() & TDXtemporary) != 0;
  9925. setNumOutputs(helper->getTempUsageCount());
  9926. if (_kind!=TAKdiskwrite)
  9927. assertex(numOutputs == 0);
  9928. }
  9929. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  9930. {
  9931. switch (numOutputs)
  9932. {
  9933. case 0:
  9934. switch (kind)
  9935. {
  9936. case TAKdiskwrite: return new CRoxieServerDiskWriteActivity(this, _probeManager);
  9937. case TAKcsvwrite: return new CRoxieServerCsvWriteActivity(this, _probeManager);
  9938. case TAKxmlwrite: return new CRoxieServerXmlWriteActivity(this, _probeManager);
  9939. };
  9940. throwUnexpected();
  9941. case 1:
  9942. return new CRoxieServerSpillWriteActivity(this, _probeManager);
  9943. default:
  9944. return new CRoxieServerThroughSpillActivity(this, _probeManager, numOutputs);
  9945. }
  9946. }
  9947. virtual bool isSink() const
  9948. {
  9949. return numOutputs == 0 && !isTemp; // MORE - check with Gavin if this is right if not a temp but reread in same job...
  9950. }
  9951. };
  9952. IRoxieServerActivityFactory *createRoxieServerDiskWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  9953. {
  9954. return new CRoxieServerDiskWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
  9955. }
  9956. //=================================================================================
  9957. class CRoxieServerIndexWriteActivity : public CRoxieServerInternalSinkActivity, implements IRoxiePublishCallback
  9958. {
  9959. IHThorIndexWriteArg &helper;
  9960. bool overwrite;
  9961. Owned<ClusterWriteHandler> clusterHandler;
  9962. Owned<IRoxieWriteHandler> writer;
  9963. unsigned __int64 reccount;
  9964. unsigned int fileCrc;
  9965. StringBuffer filename;
  9966. void updateWorkUnitResult()
  9967. {
  9968. if(filename.length()) //this is required as long as temp files don't get a name which can be stored in the WU and automatically deleted by the WU
  9969. {
  9970. WorkunitUpdate wu = ctx->updateWorkUnit();
  9971. if (wu)
  9972. {
  9973. if (!(helper.getFlags() & TDXtemporary) && helper.getSequence() >= 0)
  9974. {
  9975. Owned<IWUResult> result = wu->updateResultBySequence(helper.getSequence());
  9976. if (result)
  9977. {
  9978. result->setResultTotalRowCount(reccount);
  9979. result->setResultStatus(ResultStatusCalculated);
  9980. result->setResultLogicalName(filename.str());
  9981. }
  9982. }
  9983. if(clusterHandler)
  9984. clusterHandler->finish(writer->queryFile());
  9985. }
  9986. CTXLOG("Created roxie index file %s", filename.str());
  9987. }
  9988. }
  9989. virtual void resolve()
  9990. {
  9991. StringArray clusters;
  9992. unsigned clusterIdx = 0;
  9993. while(true)
  9994. {
  9995. OwnedRoxieString cluster(helper.getCluster(clusterIdx));
  9996. if(!cluster)
  9997. break;
  9998. clusters.append(cluster);
  9999. clusterIdx++;
  10000. }
  10001. if (roxieName.length())
  10002. clusters.append(roxieName.str());
  10003. else
  10004. clusters.append(".");
  10005. OwnedRoxieString fname(helper.getFileName());
  10006. writer.setown(ctx->createLFN(fname, overwrite, false, clusters)); // MORE - if there's a workunit, use if for scope.
  10007. filename.set(writer->queryFile()->queryFilename());
  10008. if (writer->queryFile()->exists())
  10009. {
  10010. if (overwrite)
  10011. {
  10012. CTXLOG("Removing existing %s from DFS",filename.str());
  10013. writer->queryFile()->remove();
  10014. }
  10015. else
  10016. throw MakeStringException(99, "Cannot write index file %s, file already exists (missing OVERWRITE attribute?)", filename.str());
  10017. }
  10018. }
  10019. void buildUserMetadata(Owned<IPropertyTree> & metadata)
  10020. {
  10021. size32_t nameLen;
  10022. char * nameBuff;
  10023. size32_t valueLen;
  10024. char * valueBuff;
  10025. unsigned idx = 0;
  10026. while(helper.getIndexMeta(nameLen, nameBuff, valueLen, valueBuff, idx++))
  10027. {
  10028. StringBuffer name(nameLen, nameBuff);
  10029. StringBuffer value(valueLen, valueBuff);
  10030. if(*nameBuff == '_' && strcmp(name, "_nodeSize") != 0)
  10031. {
  10032. OwnedRoxieString fname(helper.getFileName());
  10033. throw MakeStringException(0, "Invalid name %s in user metadata for index %s (names beginning with underscore are reserved)", name.str(), fname.get());
  10034. }
  10035. if(!validateXMLTag(name.str()))
  10036. {
  10037. OwnedRoxieString fname(helper.getFileName());
  10038. throw MakeStringException(0, "Invalid name %s in user metadata for index %s (not legal XML element name)", name.str(), fname.get());
  10039. }
  10040. if(!metadata)
  10041. metadata.setown(createPTree("metadata"));
  10042. metadata->setProp(name.str(), value.str());
  10043. }
  10044. }
  10045. void buildLayoutMetadata(Owned<IPropertyTree> & metadata)
  10046. {
  10047. if(!metadata)
  10048. metadata.setown(createPTree("metadata"));
  10049. metadata->setProp("_record_ECL", helper.queryRecordECL());
  10050. void * layoutMetaBuff;
  10051. size32_t layoutMetaSize;
  10052. if(helper.getIndexLayout(layoutMetaSize, layoutMetaBuff))
  10053. {
  10054. metadata->setPropBin("_record_layout", layoutMetaSize, layoutMetaBuff);
  10055. rtlFree(layoutMetaBuff);
  10056. }
  10057. }
  10058. public:
  10059. CRoxieServerIndexWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  10060. : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper(static_cast<IHThorIndexWriteArg &>(basehelper))
  10061. {
  10062. overwrite = ((helper.getFlags() & TIWoverwrite) != 0);
  10063. reccount = 0;
  10064. fileCrc = 0;
  10065. }
  10066. ~CRoxieServerIndexWriteActivity()
  10067. {
  10068. }
  10069. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  10070. {
  10071. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  10072. resolve();
  10073. }
  10074. virtual void onExecute()
  10075. {
  10076. bool isVariable = helper.queryDiskRecordSize()->isVariableSize();
  10077. size32_t maxDiskRecordSize;
  10078. if (isVariable)
  10079. maxDiskRecordSize = 0x8000;
  10080. else
  10081. {
  10082. maxDiskRecordSize = helper.queryDiskRecordSize()->getFixedSize();
  10083. if (maxDiskRecordSize > 0x8000)
  10084. throw MakeStringException(99, "Index minimum record length (%d) exceeds 32k internal limit", maxDiskRecordSize);
  10085. }
  10086. OwnedMalloc<char> rowBuffer(maxDiskRecordSize, true);
  10087. unsigned __int64 fileSize = 0;
  10088. fileCrc = -1;
  10089. OwnedRoxieString dsName(helper.getFileName());
  10090. if (dsName.get())
  10091. {
  10092. Owned<const IResolvedFile> dsFileInfo = resolveLFN(dsName, false);
  10093. if (dsFileInfo)
  10094. {
  10095. fileSize = dsFileInfo->getFileSize();
  10096. }
  10097. }
  10098. {
  10099. Owned<IFileIO> io;
  10100. try
  10101. {
  10102. io.setown(writer->queryFile()->open(IFOcreate));
  10103. }
  10104. catch(IException * e)
  10105. {
  10106. e->Release();
  10107. clearKeyStoreCache(false);
  10108. io.setown(writer->queryFile()->open(IFOcreate));
  10109. }
  10110. if(!io)
  10111. throw MakeStringException(errno, "Failed to create file %s for writing", filename.str());
  10112. Owned<IFileIOStream> out = createIOStream(io);
  10113. unsigned flags = COL_PREFIX | HTREE_FULLSORT_KEY;
  10114. if (helper.getFlags() & TIWrowcompress)
  10115. flags |= HTREE_COMPRESSED_KEY|HTREE_QUICK_COMPRESSED_KEY;
  10116. else if (!(helper.getFlags() & TIWnolzwcompress))
  10117. flags |= HTREE_COMPRESSED_KEY;
  10118. if (isVariable)
  10119. flags |= HTREE_VARSIZE;
  10120. Owned<IPropertyTree> metadata;
  10121. buildUserMetadata(metadata);
  10122. buildLayoutMetadata(metadata);
  10123. unsigned nodeSize = metadata ? metadata->getPropInt("_nodeSize", NODESIZE) : NODESIZE;
  10124. Owned<IKeyBuilder> builder = createKeyBuilder(out, flags, maxDiskRecordSize, fileSize, nodeSize, helper.getKeyedSize(), 0);
  10125. class BcWrapper : implements IBlobCreator
  10126. {
  10127. IKeyBuilder *builder;
  10128. public:
  10129. BcWrapper(IKeyBuilder *_builder) : builder(_builder) {}
  10130. virtual unsigned __int64 createBlob(size32_t size, const void * ptr)
  10131. {
  10132. return builder->createBlob(size, (const char *) ptr);
  10133. }
  10134. } bc(builder);
  10135. // Loop thru the results
  10136. loop
  10137. {
  10138. OwnedConstRoxieRow nextrec(input->nextInGroup());
  10139. if (!nextrec)
  10140. {
  10141. nextrec.setown(input->nextInGroup());
  10142. if (!nextrec)
  10143. break;
  10144. }
  10145. try
  10146. {
  10147. unsigned __int64 fpos;
  10148. RtlStaticRowBuilder rowBuilder(rowBuffer, maxDiskRecordSize);
  10149. size32_t thisSize = helper.transform(rowBuilder, nextrec, &bc, fpos);
  10150. builder->processKeyData(rowBuffer, fpos, thisSize);
  10151. }
  10152. catch(IException * e)
  10153. {
  10154. throw makeWrappedException(e);
  10155. }
  10156. reccount++;
  10157. }
  10158. if(metadata)
  10159. builder->finish(metadata,&fileCrc);
  10160. else
  10161. builder->finish(&fileCrc);
  10162. }
  10163. }
  10164. virtual void stop(bool aborting)
  10165. {
  10166. if (writer)
  10167. {
  10168. if (!aborting)
  10169. updateWorkUnitResult();
  10170. writer->finish(!aborting, this);
  10171. writer.clear();
  10172. }
  10173. CRoxieServerActivity::stop(aborting);
  10174. }
  10175. virtual void reset()
  10176. {
  10177. CRoxieServerActivity::reset();
  10178. writer.clear();
  10179. }
  10180. //interface IRoxiePublishCallback
  10181. virtual void setFileProperties(IFileDescriptor *desc) const
  10182. {
  10183. IPropertyTree &partProps = desc->queryPart(0)->queryProperties(); //properties of the first file part.
  10184. IPropertyTree &fileProps = desc->queryProperties(); // properties of the logical file
  10185. // Now publish to name services
  10186. StringBuffer dir,base;
  10187. offset_t indexFileSize = writer->queryFile()->size();
  10188. if(clusterHandler)
  10189. clusterHandler->splitPhysicalFilename(dir, base);
  10190. else
  10191. splitFilename(filename.str(), &dir, &dir, &base, &base);
  10192. desc->setDefaultDir(dir.str());
  10193. //properties of the first file part.
  10194. Owned<IPropertyTree> attrs;
  10195. if(clusterHandler)
  10196. attrs.setown(createPTree("Part")); // clusterHandler is going to set attributes
  10197. else
  10198. {
  10199. // add cluster
  10200. StringBuffer mygroupname;
  10201. desc->setNumParts(1);
  10202. desc->setPartMask(base.str());
  10203. attrs.set(&desc->queryPart(0)->queryProperties());
  10204. }
  10205. attrs->setPropInt64("@size", indexFileSize);
  10206. CDateTime createTime, modifiedTime, accessedTime;
  10207. writer->queryFile()->getTime(&createTime, &modifiedTime, &accessedTime);
  10208. // round file time down to nearest sec. Nanosec accurancy is not preserved elsewhere and can lead to mismatch later.
  10209. unsigned hour, min, sec, nanosec;
  10210. modifiedTime.getTime(hour, min, sec, nanosec);
  10211. modifiedTime.setTime(hour, min, sec, 0);
  10212. StringBuffer timestr;
  10213. modifiedTime.getString(timestr);
  10214. if(timestr.length())
  10215. attrs->setProp("@modified", timestr.str());
  10216. if(clusterHandler)
  10217. clusterHandler->setDescriptorParts(desc, base.str(), attrs);
  10218. // properties of the logical file
  10219. IPropertyTree & properties = desc->queryProperties();
  10220. properties.setProp("@kind", "key");
  10221. properties.setPropInt64("@size", indexFileSize);
  10222. properties.setPropInt64("@recordCount", reccount);
  10223. SCMStringBuffer info;
  10224. WorkunitUpdate workUnit = ctx->updateWorkUnit();
  10225. if (workUnit)
  10226. {
  10227. properties.setProp("@owner", workUnit->getUser(info).str());
  10228. info.clear();
  10229. properties.setProp("@workunit", workUnit->getWuid(info).str());
  10230. info.clear();
  10231. properties.setProp("@job", workUnit->getJobName(info).str());
  10232. }
  10233. char const * rececl = helper.queryRecordECL();
  10234. if(rececl && *rececl)
  10235. properties.setProp("ECL", rececl);
  10236. if (helper.getFlags() & TIWexpires)
  10237. setExpiryTime(properties, helper.getExpiryDays());
  10238. if (helper.getFlags() & TIWupdate)
  10239. {
  10240. unsigned eclCRC;
  10241. unsigned __int64 totalCRC;
  10242. helper.getUpdateCRCs(eclCRC, totalCRC);
  10243. properties.setPropInt("@eclCRC", eclCRC);
  10244. properties.setPropInt64("@totalCRC", totalCRC);
  10245. }
  10246. properties.setPropInt("@fileCrc", fileCrc);
  10247. properties.setPropInt("@formatCrc", helper.getFormatCrc());
  10248. void * layoutMetaBuff;
  10249. size32_t layoutMetaSize;
  10250. if(helper.getIndexLayout(layoutMetaSize, layoutMetaBuff))
  10251. {
  10252. properties.setPropBin("_record_layout", layoutMetaSize, layoutMetaBuff);
  10253. rtlFree(layoutMetaBuff);
  10254. }
  10255. }
  10256. IUserDescriptor *queryUserDescriptor() const
  10257. {
  10258. IConstWorkUnit *workUnit = ctx->queryWorkUnit();
  10259. if (workUnit)
  10260. return workUnit->queryUserDescriptor();
  10261. else
  10262. return NULL;
  10263. }
  10264. };
  10265. //=================================================================================
  10266. class CRoxieServerIndexWriteActivityFactory : public CRoxieServerMultiOutputFactory
  10267. {
  10268. public:
  10269. CRoxieServerIndexWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  10270. : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  10271. {
  10272. setNumOutputs(0);
  10273. }
  10274. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  10275. {
  10276. return new CRoxieServerIndexWriteActivity(this, _probeManager);
  10277. }
  10278. virtual bool isSink() const
  10279. {
  10280. return true;
  10281. }
  10282. };
  10283. IRoxieServerActivityFactory *createRoxieServerIndexWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  10284. {
  10285. return new CRoxieServerIndexWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
  10286. }
  10287. //=================================================================================
  10288. static inline void getLimitType(unsigned flags, bool & limitFail, bool & limitOnFail)
  10289. {
  10290. if((flags & JFmatchAbortLimitSkips) != 0)
  10291. {
  10292. limitFail = false;
  10293. limitOnFail = false;
  10294. }
  10295. else
  10296. {
  10297. limitOnFail = ((flags & JFonfail) != 0);
  10298. limitFail = !limitOnFail;
  10299. }
  10300. }
  10301. class CRoxieServerJoinActivity : public CRoxieServerTwoInputActivity
  10302. {
  10303. enum { JSfill, JSfillleft, JSfillright, JScollate, JScompare, JSleftonly, JSrightonly } state;
  10304. IHThorJoinArg &helper;
  10305. ICompare * collate;
  10306. ICompare * collateupper;
  10307. ThorActivityKind activityKind;
  10308. bool leftOuterJoin;
  10309. bool rightOuterJoin;
  10310. bool exclude;
  10311. bool limitFail;
  10312. bool limitOnFail;
  10313. unsigned keepLimit;
  10314. unsigned joinLimit;
  10315. unsigned atmostLimit;
  10316. unsigned abortLimit;
  10317. unsigned atmostsTriggered;
  10318. bool betweenjoin;
  10319. OwnedRowArray right;
  10320. const void * left;
  10321. const void * pendingRight;
  10322. unsigned rightIndex;
  10323. BoolArray matchedRight;
  10324. bool matchedLeft;
  10325. Owned<IException> failingLimit;
  10326. ConstPointerArray filteredRight;
  10327. Owned<IRHLimitedCompareHelper> limitedhelper;
  10328. OwnedConstRoxieRow defaultLeft;
  10329. OwnedConstRoxieRow defaultRight;
  10330. Owned<IEngineRowAllocator> defaultLeftAllocator;
  10331. Owned<IEngineRowAllocator> defaultRightAllocator;
  10332. bool cloneLeft;
  10333. void createDefaultLeft()
  10334. {
  10335. if (!defaultLeft)
  10336. {
  10337. if (!defaultLeftAllocator)
  10338. defaultLeftAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input->queryOutputMeta(), activityId));
  10339. RtlDynamicRowBuilder rowBuilder(defaultLeftAllocator);
  10340. size32_t thisSize = helper.createDefaultLeft(rowBuilder);
  10341. defaultLeft.setown(rowBuilder.finalizeRowClear(thisSize));
  10342. }
  10343. }
  10344. void createDefaultRight()
  10345. {
  10346. if (!defaultRight)
  10347. {
  10348. if (!defaultRightAllocator)
  10349. defaultRightAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input1->queryOutputMeta(), activityId));
  10350. RtlDynamicRowBuilder rowBuilder(defaultRightAllocator);
  10351. size32_t thisSize = helper.createDefaultRight(rowBuilder);
  10352. defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
  10353. }
  10354. }
  10355. public:
  10356. CRoxieServerJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  10357. : CRoxieServerTwoInputActivity(_factory, _probeManager), helper((IHThorJoinArg &)basehelper)
  10358. {
  10359. // MORE - some of this should be done in factory
  10360. unsigned joinFlags = helper.getJoinFlags();
  10361. leftOuterJoin = (joinFlags & JFleftouter) != 0;
  10362. rightOuterJoin = (joinFlags & JFrightouter) != 0;
  10363. exclude = (joinFlags & JFexclude) != 0;
  10364. cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
  10365. getLimitType(joinFlags, limitFail, limitOnFail);
  10366. if (joinFlags & JFslidingmatch)
  10367. {
  10368. betweenjoin = true;
  10369. collate = helper.queryCompareLeftRightLower();
  10370. collateupper = helper.queryCompareLeftRightUpper();
  10371. }
  10372. else
  10373. {
  10374. betweenjoin = false;
  10375. collate = collateupper = helper.queryCompareLeftRight();
  10376. }
  10377. rightIndex = 0;
  10378. state = JSfill;
  10379. matchedLeft = false;
  10380. joinLimit = 0;
  10381. keepLimit = 0; // wait until ctx available
  10382. atmostLimit = 0; // wait until ctx available
  10383. abortLimit = 0; // wait until ctx available
  10384. atmostsTriggered = 0;
  10385. assertex((joinFlags & (JFfirst | JFfirstleft | JFfirstright)) == 0);
  10386. left = NULL;
  10387. pendingRight = NULL;
  10388. activityKind = _factory->getKind();
  10389. }
  10390. virtual bool needsAllocator() const { return true; }
  10391. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  10392. {
  10393. left = NULL;
  10394. rightIndex = 0;
  10395. state = JSfill;
  10396. matchedLeft = false;
  10397. CRoxieServerTwoInputActivity::start(parentExtractSize, parentExtract, paused);
  10398. keepLimit = helper.getKeepLimit();
  10399. if (keepLimit == 0)
  10400. keepLimit = (unsigned)-1;
  10401. atmostsTriggered = 0;
  10402. atmostLimit = helper.getJoinLimit();
  10403. if(atmostLimit == 0)
  10404. atmostLimit = (unsigned)-1;
  10405. else
  10406. assertex(!rightOuterJoin && !betweenjoin);
  10407. abortLimit = helper.getMatchAbortLimit();
  10408. if (abortLimit == 0)
  10409. abortLimit = (unsigned)-1;
  10410. if (rightOuterJoin)
  10411. createDefaultLeft();
  10412. if ((leftOuterJoin && (activityKind==TAKjoin || activityKind==TAKjoinlight || activityKind==TAKdenormalizegroup)) || limitOnFail)
  10413. createDefaultRight();
  10414. if ((helper.getJoinFlags() & JFlimitedprefixjoin) && helper.getJoinLimit())
  10415. { //limited match join (s[1..n])
  10416. limitedhelper.setown(createRHLimitedCompareHelper());
  10417. limitedhelper->init( helper.getJoinLimit(), input1, collate, helper.queryPrefixCompare() );
  10418. }
  10419. }
  10420. virtual void reset()
  10421. {
  10422. if (atmostsTriggered)
  10423. noteStatistic(STATS_ATMOST, atmostsTriggered, 1);
  10424. right.clear();
  10425. ReleaseClearRoxieRow(left);
  10426. ReleaseClearRoxieRow(pendingRight);
  10427. defaultRight.clear();
  10428. defaultLeft.clear();
  10429. CRoxieServerTwoInputActivity::reset();
  10430. }
  10431. virtual void setInput(unsigned idx, IRoxieInput *_in)
  10432. {
  10433. switch(idx)
  10434. {
  10435. case 0:
  10436. if ((helper.getJoinFlags() & JFparallel) != 0)
  10437. {
  10438. puller.setown(new CRoxieServerReadAheadInput(0)); // MORE - cant ask context for parallelJoinPreload as context is not yet set up.
  10439. puller->setInput(0, _in);
  10440. _in = puller;
  10441. }
  10442. input = _in;
  10443. break;
  10444. case 1:
  10445. input1 = _in;
  10446. break;
  10447. default:
  10448. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
  10449. }
  10450. }
  10451. virtual IRoxieInput *queryOutput(unsigned idx)
  10452. {
  10453. if (idx==(unsigned)-1)
  10454. idx = 0;
  10455. return idx ? NULL : this;
  10456. }
  10457. void fillLeft()
  10458. {
  10459. matchedLeft = false;
  10460. left = input->nextInGroup();
  10461. if (!left)
  10462. left = input->nextInGroup();
  10463. if(betweenjoin && left && pendingRight && (collate->docompare(left, pendingRight) >= 0))
  10464. fillRight();
  10465. if (limitedhelper && 0==rightIndex)
  10466. {
  10467. rightIndex = 0;
  10468. right.clear();
  10469. matchedRight.kill();
  10470. if (left)
  10471. {
  10472. limitedhelper->getGroup(right,left);
  10473. ForEachItemIn(idx, right)
  10474. matchedRight.append(false);
  10475. }
  10476. }
  10477. }
  10478. void fillRight()
  10479. {
  10480. if (limitedhelper)
  10481. return;
  10482. failingLimit.clear();
  10483. if(betweenjoin && left)
  10484. {
  10485. aindex_t start = 0;
  10486. while(right.isItem(start) && (collateupper->docompare(left, right.item(start)) > 0))
  10487. start++;
  10488. if(start>0)
  10489. right.clearPart(0, start);
  10490. }
  10491. else
  10492. right.clear();
  10493. rightIndex = 0;
  10494. unsigned groupCount = 0;
  10495. const void * next;
  10496. while(true)
  10497. {
  10498. if(pendingRight)
  10499. {
  10500. next = pendingRight;
  10501. pendingRight = NULL;
  10502. }
  10503. else
  10504. {
  10505. next = input1->nextInGroup();
  10506. }
  10507. if(!rightOuterJoin && next && (!left || (collateupper->docompare(left, next) > 0))) // if right is less than left, and not right outer, can skip group
  10508. {
  10509. while(next)
  10510. {
  10511. ReleaseClearRoxieRow(next);
  10512. next = input1->nextInGroup();
  10513. }
  10514. continue;
  10515. }
  10516. while(next)
  10517. {
  10518. if(groupCount==abortLimit)
  10519. {
  10520. if(limitFail)
  10521. failLimit();
  10522. if (ctx->queryDebugContext())
  10523. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  10524. if(limitOnFail)
  10525. {
  10526. assertex(!failingLimit);
  10527. try
  10528. {
  10529. failLimit();
  10530. }
  10531. catch(IException * except)
  10532. {
  10533. failingLimit.setown(except);
  10534. }
  10535. assertex(failingLimit != NULL);
  10536. }
  10537. right.append(next);
  10538. do
  10539. {
  10540. next = input1->nextInGroup();
  10541. ReleaseRoxieRow(next);
  10542. } while(next);
  10543. break;
  10544. }
  10545. else if(groupCount==atmostLimit)
  10546. {
  10547. atmostsTriggered++;
  10548. right.clear();
  10549. groupCount = 0;
  10550. while(next)
  10551. {
  10552. ReleaseRoxieRow(next);
  10553. next = input1->nextInGroup();
  10554. }
  10555. }
  10556. else
  10557. {
  10558. right.append(next);
  10559. groupCount++;
  10560. }
  10561. next = input1->nextInGroup();
  10562. }
  10563. // normally only want to read one right group, but if is between join and next right group is in window for left, need to continue
  10564. if(betweenjoin && left)
  10565. {
  10566. pendingRight = input1->nextInGroup();
  10567. if(!pendingRight || (collate->docompare(left, pendingRight) < 0))
  10568. break;
  10569. }
  10570. else
  10571. break;
  10572. }
  10573. matchedRight.kill();
  10574. ForEachItemIn(idx, right)
  10575. matchedRight.append(false);
  10576. }
  10577. const void * joinRecords(const void * curLeft, const void * curRight)
  10578. {
  10579. if (cloneLeft)
  10580. {
  10581. LinkRoxieRow(curLeft);
  10582. return curLeft;
  10583. }
  10584. try
  10585. {
  10586. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  10587. size32_t thisSize = helper.transform(rowBuilder, curLeft, curRight);
  10588. if (thisSize)
  10589. return rowBuilder.finalizeRowClear(thisSize);
  10590. else
  10591. return NULL;
  10592. }
  10593. catch (IException *E)
  10594. {
  10595. throw makeWrappedException(E);
  10596. }
  10597. }
  10598. const void * denormalizeRecords(const void * curLeft, ConstPointerArray & rows)
  10599. {
  10600. try
  10601. {
  10602. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  10603. unsigned numRows = rows.ordinality();
  10604. const void * right = numRows ? rows.item(0) : defaultRight.get();
  10605. size32_t thisSize = helper.transform(rowBuilder, curLeft, right, numRows, (const void * *)rows.getArray());
  10606. if (thisSize)
  10607. return rowBuilder.finalizeRowClear(thisSize);
  10608. else
  10609. return NULL;
  10610. }
  10611. catch (IException *E)
  10612. {
  10613. throw makeWrappedException(E);
  10614. }
  10615. }
  10616. const void * joinException(const void * curLeft, IException * except)
  10617. {
  10618. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  10619. size32_t thisSize = helper.onFailTransform(rowBuilder, curLeft, defaultRight, except);
  10620. return rowBuilder.finalizeRowClear(thisSize);
  10621. }
  10622. void failLimit()
  10623. {
  10624. helper.onMatchAbortLimitExceeded();
  10625. CommonXmlWriter xmlwrite(0);
  10626. if (input->queryOutputMeta() && input->queryOutputMeta()->hasXML())
  10627. {
  10628. input->queryOutputMeta()->toXML((byte *) left, xmlwrite);
  10629. }
  10630. throw MakeStringException(ROXIE_TOO_MANY_RESULTS, "More than %d match candidates in join %d for row %s", abortLimit, queryId(), xmlwrite.str());
  10631. }
  10632. virtual const void * nextInGroup()
  10633. {
  10634. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  10635. loop
  10636. {
  10637. switch (state)
  10638. {
  10639. case JSfill:
  10640. fillLeft();
  10641. state = JSfillright;
  10642. break;
  10643. case JSfillright:
  10644. fillRight();
  10645. state = JScollate;
  10646. break;
  10647. case JSfillleft:
  10648. fillLeft();
  10649. state = JScollate;
  10650. break;
  10651. case JScollate:
  10652. if (right.ordinality() == 0)
  10653. {
  10654. if (left == NULL)
  10655. return NULL;
  10656. state = JSleftonly;
  10657. }
  10658. else
  10659. {
  10660. if (!left)
  10661. state = JSrightonly;
  10662. else
  10663. {
  10664. int diff;
  10665. if(betweenjoin)
  10666. diff = ((collate->docompare(left, right.item(0)) < 0) ? -1 : ((collateupper->docompare(left, right.item(right.ordinality()-1)) > 0) ? +1 : 0));
  10667. else
  10668. diff = collate->docompare(left, right.item(0));
  10669. bool limitExceeded = right.ordinality()>abortLimit;
  10670. if (diff == 0)
  10671. {
  10672. if (limitExceeded)
  10673. {
  10674. const void * ret = NULL;
  10675. if(failingLimit)
  10676. ret = joinException(left, failingLimit);
  10677. ReleaseRoxieRow(left);
  10678. left = NULL;
  10679. state = JSfillleft;
  10680. ForEachItemIn(idx, right)
  10681. matchedRight.replace(true, idx);
  10682. if(ret)
  10683. {
  10684. processed++;
  10685. return ret;
  10686. }
  10687. }
  10688. else
  10689. {
  10690. state = JScompare;
  10691. joinLimit = keepLimit;
  10692. }
  10693. }
  10694. else if (diff < 0)
  10695. state = JSleftonly;
  10696. else if (limitExceeded)
  10697. {
  10698. // MORE - Roxie code seems to think there should be a destroyRowset(right) here....
  10699. state = JSfillright;
  10700. }
  10701. else
  10702. state = JSrightonly;
  10703. }
  10704. }
  10705. break;
  10706. case JSrightonly:
  10707. if (rightOuterJoin)
  10708. {
  10709. switch (activityKind)
  10710. {
  10711. case TAKjoin:
  10712. {
  10713. while (right.isItem(rightIndex))
  10714. {
  10715. if (!matchedRight.item(rightIndex))
  10716. {
  10717. const void * rhs = right.item(rightIndex++);
  10718. const void *ret = joinRecords(defaultLeft, rhs);
  10719. if (ret)
  10720. {
  10721. processed++;
  10722. return ret;
  10723. }
  10724. }
  10725. rightIndex++;
  10726. }
  10727. break;
  10728. }
  10729. //Probably excessive to implement the following, but possibly useful
  10730. case TAKdenormalize:
  10731. {
  10732. OwnedConstRoxieRow newLeft;
  10733. newLeft.set(defaultLeft);
  10734. unsigned rowSize = 0;
  10735. unsigned leftCount = 0;
  10736. while (right.isItem(rightIndex))
  10737. {
  10738. if (!matchedRight.item(rightIndex))
  10739. {
  10740. const void * rhs = right.item(rightIndex);
  10741. try
  10742. {
  10743. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  10744. unsigned thisSize = helper.transform(rowBuilder, newLeft, rhs, ++leftCount);
  10745. if (thisSize)
  10746. {
  10747. rowSize = thisSize;
  10748. newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
  10749. }
  10750. }
  10751. catch (IException *E)
  10752. {
  10753. throw makeWrappedException(E);
  10754. }
  10755. }
  10756. rightIndex++;
  10757. }
  10758. state = JSfillright;
  10759. if (rowSize)
  10760. {
  10761. processed++;
  10762. return newLeft.getClear();
  10763. }
  10764. break;
  10765. }
  10766. case TAKdenormalizegroup:
  10767. {
  10768. filteredRight.kill();
  10769. while (right.isItem(rightIndex))
  10770. {
  10771. if (!matchedRight.item(rightIndex))
  10772. filteredRight.append(right.item(rightIndex));
  10773. rightIndex++;
  10774. }
  10775. state = JSfillright;
  10776. if (filteredRight.ordinality())
  10777. {
  10778. const void * ret = denormalizeRecords(defaultLeft, filteredRight);
  10779. filteredRight.kill();
  10780. if (ret)
  10781. {
  10782. processed++;
  10783. return ret;
  10784. }
  10785. }
  10786. break;
  10787. }
  10788. }
  10789. }
  10790. state = JSfillright;
  10791. break;
  10792. case JSleftonly:
  10793. {
  10794. const void * ret = NULL;
  10795. if (!matchedLeft && leftOuterJoin)
  10796. {
  10797. switch (activityKind)
  10798. {
  10799. case TAKjoin:
  10800. ret = joinRecords(left, defaultRight);
  10801. break;
  10802. case TAKdenormalize:
  10803. ret = left;
  10804. left = NULL;
  10805. break;
  10806. case TAKdenormalizegroup:
  10807. filteredRight.kill();
  10808. ret = denormalizeRecords(left, filteredRight);
  10809. break;
  10810. }
  10811. }
  10812. ReleaseRoxieRow(left);
  10813. left = NULL;
  10814. state = JSfillleft;
  10815. if (ret)
  10816. {
  10817. processed++;
  10818. return ret;
  10819. }
  10820. break;
  10821. }
  10822. case JScompare:
  10823. if (joinLimit != 0)
  10824. {
  10825. switch (activityKind)
  10826. {
  10827. case TAKjoin:
  10828. {
  10829. while (right.isItem(rightIndex))
  10830. {
  10831. const void * rhs = right.item(rightIndex++);
  10832. if (helper.match(left, rhs))
  10833. {
  10834. matchedRight.replace(true, rightIndex-1);
  10835. matchedLeft = true;
  10836. if (!exclude)
  10837. {
  10838. const void *ret = joinRecords(left, rhs);
  10839. if (ret)
  10840. {
  10841. processed++;
  10842. joinLimit--;
  10843. return ret;
  10844. }
  10845. }
  10846. }
  10847. }
  10848. break;
  10849. }
  10850. case TAKdenormalize:
  10851. {
  10852. OwnedConstRoxieRow newLeft;
  10853. newLeft.set(left);
  10854. unsigned rowSize = 0;
  10855. unsigned leftCount = 0;
  10856. while (right.isItem(rightIndex) && joinLimit)
  10857. {
  10858. try
  10859. {
  10860. const void * rhs = right.item(rightIndex++);
  10861. if (helper.match(left, rhs))
  10862. {
  10863. matchedRight.replace(true, rightIndex-1);
  10864. matchedLeft = true;
  10865. if (!exclude)
  10866. {
  10867. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  10868. unsigned thisSize = helper.transform(rowBuilder, newLeft, rhs, ++leftCount);
  10869. if (thisSize)
  10870. {
  10871. rowSize = thisSize;
  10872. newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
  10873. joinLimit--;
  10874. }
  10875. }
  10876. }
  10877. }
  10878. catch (IException *E)
  10879. {
  10880. throw makeWrappedException(E);
  10881. }
  10882. }
  10883. state = JSleftonly;
  10884. rightIndex = 0;
  10885. if (rowSize)
  10886. {
  10887. processed++;
  10888. return newLeft.getClear();
  10889. }
  10890. break;
  10891. }
  10892. case TAKdenormalizegroup:
  10893. {
  10894. filteredRight.kill();
  10895. while (right.isItem(rightIndex))
  10896. {
  10897. const void * rhs = right.item(rightIndex++);
  10898. if (helper.match(left, rhs))
  10899. {
  10900. matchedRight.replace(true, rightIndex-1);
  10901. filteredRight.append(rhs);
  10902. matchedLeft = true;
  10903. if (filteredRight.ordinality()==joinLimit)
  10904. break;
  10905. }
  10906. }
  10907. state = JSleftonly;
  10908. rightIndex = 0;
  10909. if (!exclude && filteredRight.ordinality())
  10910. {
  10911. const void * ret = denormalizeRecords(left, filteredRight);
  10912. filteredRight.kill();
  10913. if (ret)
  10914. {
  10915. processed++;
  10916. return ret;
  10917. }
  10918. }
  10919. break;
  10920. }
  10921. }
  10922. }
  10923. state = JSleftonly;
  10924. rightIndex = 0;
  10925. break;
  10926. }
  10927. }
  10928. }
  10929. };
  10930. class CRoxieServerJoinActivityFactory : public CRoxieServerActivityFactory
  10931. {
  10932. unsigned input2;
  10933. unsigned input2idx;
  10934. public:
  10935. CRoxieServerJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  10936. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  10937. {
  10938. input2 = 0;
  10939. input2idx = 0;
  10940. }
  10941. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  10942. {
  10943. return new CRoxieServerJoinActivity(this, _probeManager);
  10944. }
  10945. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  10946. {
  10947. if (idx==1)
  10948. {
  10949. input2 = source;
  10950. input2idx = sourceidx;
  10951. }
  10952. else
  10953. CRoxieServerActivityFactory::setInput(idx, source, sourceidx);
  10954. }
  10955. virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
  10956. {
  10957. switch (idx)
  10958. {
  10959. case 1:
  10960. sourceidx = input2idx;
  10961. return input2;
  10962. case 0:
  10963. return CRoxieServerActivityFactory::getInput(idx, sourceidx);
  10964. default:
  10965. return (unsigned) -1;
  10966. }
  10967. }
  10968. virtual unsigned numInputs() const { return 2; }
  10969. };
  10970. IRoxieServerActivityFactory *createRoxieServerJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  10971. {
  10972. return new CRoxieServerJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  10973. }
  10974. //=================================================================================
  10975. #define CONCAT_READAHEAD 1000
  10976. class CRoxieThreadedConcatReader : public CInterface, implements IRecordPullerCallback
  10977. {
  10978. public:
  10979. IMPLEMENT_IINTERFACE;
  10980. CRoxieThreadedConcatReader(InterruptableSemaphore &_ready, bool _grouped)
  10981. : puller(false), grouped(_grouped), atEog(true), ready(_ready), eof(false)
  10982. {
  10983. }
  10984. void start(unsigned parentExtractSize, const byte *parentExtract, bool paused, IRoxieSlaveContext *ctx)
  10985. {
  10986. space.reinit(CONCAT_READAHEAD);
  10987. puller.start(parentExtractSize, parentExtract, paused, ctx->concatPreload(), false, ctx);
  10988. }
  10989. void stop(bool aborting)
  10990. {
  10991. space.interrupt();
  10992. puller.stop(aborting);
  10993. }
  10994. IRoxieInput *queryInput() const
  10995. {
  10996. return puller.queryInput();
  10997. }
  10998. void reset()
  10999. {
  11000. puller.reset();
  11001. ForEachItemIn(idx, buffer)
  11002. ReleaseRoxieRow(buffer.item(idx));
  11003. buffer.clear();
  11004. eof = false;
  11005. atEog = true;
  11006. }
  11007. void setInput(IRoxieInput *_in)
  11008. {
  11009. puller.setInput(this, _in);
  11010. }
  11011. virtual void processRow(const void *row)
  11012. {
  11013. buffer.enqueue(row);
  11014. ready.signal();
  11015. space.wait();
  11016. }
  11017. virtual void processGroup(const ConstPointerArray &rows)
  11018. {
  11019. // We use record-by-record input mode of the puller thread even in grouped mode.
  11020. throwUnexpected();
  11021. }
  11022. virtual void processEOG()
  11023. {
  11024. if (grouped)
  11025. processRow(NULL);
  11026. }
  11027. virtual void processDone()
  11028. {
  11029. processRow(NULL);
  11030. }
  11031. virtual bool fireException(IException *e)
  11032. {
  11033. // called from puller thread on failure
  11034. ready.interrupt(LINK(e));
  11035. space.interrupt(e);
  11036. return true;
  11037. }
  11038. bool peek(const void * &row, bool &anyActive)
  11039. {
  11040. if (!eof)
  11041. {
  11042. if (buffer.ordinality())
  11043. {
  11044. space.signal();
  11045. row = buffer.dequeue();
  11046. if (row==NULL)
  11047. {
  11048. if (atEog)
  11049. {
  11050. eof = true;
  11051. return false;
  11052. }
  11053. else
  11054. atEog = true;
  11055. }
  11056. else if (grouped)
  11057. atEog = false;
  11058. return true;
  11059. }
  11060. anyActive = true;
  11061. }
  11062. return false;
  11063. }
  11064. protected:
  11065. RecordPullerThread puller;
  11066. InterruptableSemaphore space;
  11067. InterruptableSemaphore &ready;
  11068. SafeQueueOf<const void, true> buffer;
  11069. bool atEog;
  11070. bool eof;
  11071. bool grouped;
  11072. };
  11073. MAKEPointerArray(CRoxieThreadedConcatReader, ReaderArray);
  11074. class CRoxieServerThreadedConcatActivity : public CRoxieServerActivity
  11075. {
  11076. InterruptableSemaphore ready;
  11077. ReaderArray pullers;
  11078. unsigned numInputs;
  11079. unsigned nextPuller; // for round robin
  11080. unsigned readyPending;
  11081. bool eof;
  11082. bool inGroup;
  11083. bool grouped;
  11084. public:
  11085. CRoxieServerThreadedConcatActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _grouped, unsigned _numInputs)
  11086. : CRoxieServerActivity(_factory, _probeManager), grouped(_grouped)
  11087. {
  11088. numInputs = _numInputs;
  11089. eof = (numInputs==0);
  11090. inGroup = false;
  11091. nextPuller = 0;
  11092. readyPending = 0;
  11093. for (unsigned i = 0; i < numInputs; i++)
  11094. pullers.append(*new CRoxieThreadedConcatReader(ready, _grouped));
  11095. }
  11096. ~CRoxieServerThreadedConcatActivity()
  11097. {
  11098. ForEachItemIn(idx, pullers)
  11099. delete &pullers.item(idx);
  11100. }
  11101. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  11102. {
  11103. eof = (numInputs==0);
  11104. inGroup = false;
  11105. nextPuller = 0;
  11106. readyPending = 0;
  11107. ready.reinit();
  11108. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  11109. ForEachItemIn(idx, pullers)
  11110. {
  11111. pullers.item(idx).start(parentExtractSize, parentExtract, paused, ctx);
  11112. // NOTE - it is ok to start the thread running while parts of the subgraph are still being started, since everything
  11113. // in the part of the subgraph that the thread uses has been started.
  11114. // Note that splitters are supposed to cope with being used when only some outputs have been started.
  11115. }
  11116. }
  11117. virtual void stop(bool aborting)
  11118. {
  11119. ready.interrupt();
  11120. ForEachItemIn(idx, pullers)
  11121. pullers.item(idx).stop(aborting);
  11122. CRoxieServerActivity::stop(aborting);
  11123. }
  11124. virtual unsigned __int64 queryLocalCycles() const
  11125. {
  11126. return 0;
  11127. }
  11128. virtual IRoxieInput *queryInput(unsigned idx) const
  11129. {
  11130. if (pullers.isItem(idx))
  11131. return pullers.item(idx).queryInput();
  11132. else
  11133. return NULL;
  11134. }
  11135. virtual void reset()
  11136. {
  11137. CRoxieServerActivity::reset();
  11138. ForEachItemIn(idx, pullers)
  11139. pullers.item(idx).reset();
  11140. eof = false;
  11141. inGroup = false;
  11142. nextPuller = 0;
  11143. readyPending = 0;
  11144. }
  11145. virtual void setInput(unsigned idx, IRoxieInput *_in)
  11146. {
  11147. if (pullers.isItem(idx))
  11148. pullers.item(idx).setInput(_in);
  11149. else
  11150. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
  11151. }
  11152. virtual const void * nextInGroup()
  11153. {
  11154. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  11155. if (eof)
  11156. return NULL;
  11157. loop
  11158. {
  11159. if (readyPending && !inGroup)
  11160. {
  11161. if (readyPending > 1)
  11162. ready.signal(readyPending-1);
  11163. readyPending = 0;
  11164. }
  11165. else
  11166. ready.wait();
  11167. bool anyActive = false;
  11168. ForEachItemIn(unused_index, pullers)
  11169. {
  11170. // NOTE - we round robin not just because it's more efficient, but because it ensures the preservation of grouping information
  11171. const void *ret;
  11172. bool fetched = pullers.item(nextPuller).peek(ret, anyActive);
  11173. if (fetched)
  11174. {
  11175. inGroup = (ret != NULL);
  11176. return ret;
  11177. }
  11178. if (inGroup && grouped)
  11179. {
  11180. // Some other puller has data, but we can't consume it until the group we are reading is complete.
  11181. readyPending++;
  11182. anyActive = true;
  11183. break;
  11184. }
  11185. nextPuller++;
  11186. if (nextPuller==pullers.ordinality())
  11187. nextPuller = 0;
  11188. }
  11189. if (!anyActive)
  11190. break;
  11191. // A ready signal without anything being ready means someone reached end-of-file.
  11192. }
  11193. eof = true;
  11194. return NULL;
  11195. }
  11196. };
  11197. class CRoxieServerOrderedConcatActivity : public CRoxieServerActivity
  11198. {
  11199. IRoxieInput *curInput;
  11200. bool eogSeen;
  11201. bool anyThisGroup;
  11202. bool grouped;
  11203. unsigned numInputs;
  11204. unsigned inputIdx;
  11205. IRoxieInput **inputArray;
  11206. public:
  11207. CRoxieServerOrderedConcatActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _grouped, unsigned _numInputs)
  11208. : CRoxieServerActivity(_factory, _probeManager)
  11209. {
  11210. eogSeen = false;
  11211. anyThisGroup = false;
  11212. grouped = _grouped;
  11213. numInputs = _numInputs;
  11214. inputIdx = 0;
  11215. inputArray = new IRoxieInput*[numInputs];
  11216. for (unsigned i = 0; i < numInputs; i++)
  11217. inputArray[i] = NULL;
  11218. curInput = NULL;
  11219. }
  11220. ~CRoxieServerOrderedConcatActivity()
  11221. {
  11222. delete [] inputArray;
  11223. }
  11224. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  11225. {
  11226. inputIdx = 0;
  11227. curInput = inputArray[inputIdx];
  11228. eogSeen = false;
  11229. anyThisGroup = false;
  11230. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  11231. for (unsigned i = 0; i < numInputs; i++)
  11232. inputArray[i]->start(parentExtractSize, parentExtract, paused);
  11233. }
  11234. virtual void stop(bool aborting)
  11235. {
  11236. for (unsigned i = 0; i < numInputs; i++)
  11237. inputArray[i]->stop(aborting);
  11238. CRoxieServerActivity::stop(aborting);
  11239. }
  11240. virtual unsigned __int64 queryLocalCycles() const
  11241. {
  11242. return 0;
  11243. }
  11244. virtual IRoxieInput *queryInput(unsigned idx) const
  11245. {
  11246. if (idx < numInputs)
  11247. return inputArray[idx];
  11248. else
  11249. return NULL;
  11250. }
  11251. virtual void reset()
  11252. {
  11253. CRoxieServerActivity::reset();
  11254. for (unsigned i = 0; i < numInputs; i++)
  11255. inputArray[i]->reset();
  11256. }
  11257. virtual void setInput(unsigned idx, IRoxieInput *_in)
  11258. {
  11259. inputArray[idx] = _in;
  11260. }
  11261. virtual const void * nextInGroup()
  11262. {
  11263. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  11264. if (!curInput)
  11265. return NULL; // eof
  11266. const void * next = curInput->nextInGroup();
  11267. if (next)
  11268. {
  11269. anyThisGroup = true;
  11270. eogSeen = false;
  11271. processed++;
  11272. return next;
  11273. }
  11274. else if (!eogSeen)
  11275. {
  11276. eogSeen = true;
  11277. if (grouped)
  11278. {
  11279. if (anyThisGroup)
  11280. {
  11281. anyThisGroup = false;
  11282. return NULL;
  11283. }
  11284. else
  11285. return nextInGroup();
  11286. }
  11287. else
  11288. return nextInGroup();
  11289. }
  11290. else if (inputIdx < numInputs-1)
  11291. {
  11292. inputIdx++;
  11293. curInput = inputArray[inputIdx];
  11294. eogSeen = false;
  11295. return nextInGroup();
  11296. }
  11297. else
  11298. {
  11299. curInput = NULL;
  11300. return NULL;
  11301. }
  11302. }
  11303. };
  11304. class CRoxieServerConcatActivityFactory : public CRoxieServerMultiInputFactory
  11305. {
  11306. bool ordered;
  11307. bool grouped;
  11308. public:
  11309. CRoxieServerConcatActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  11310. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  11311. {
  11312. Owned <IHThorFunnelArg> helper = (IHThorFunnelArg *) helperFactory();
  11313. ordered = helper->isOrdered();
  11314. grouped = helper->queryOutputMeta()->isGrouped();
  11315. }
  11316. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  11317. {
  11318. if (ordered || (_probeManager && _probeManager->queryDebugManager()))
  11319. return new CRoxieServerOrderedConcatActivity(this, _probeManager, grouped, numInputs());
  11320. else
  11321. return new CRoxieServerThreadedConcatActivity(this, _probeManager, grouped, numInputs());
  11322. }
  11323. };
  11324. IRoxieServerActivityFactory *createRoxieServerConcatActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  11325. {
  11326. return new CRoxieServerConcatActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  11327. }
  11328. //=================================================================================
  11329. class CRoxieServerNonEmptyActivity : public CRoxieServerMultiInputBaseActivity
  11330. {
  11331. IRoxieInput * selectedInput;
  11332. unsigned savedParentExtractSize;
  11333. const byte * savedParentExtract;
  11334. bool foundInput;
  11335. public:
  11336. CRoxieServerNonEmptyActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  11337. : CRoxieServerMultiInputBaseActivity(_factory, _probeManager, _numInputs)
  11338. {
  11339. foundInput = false;
  11340. selectedInput = NULL;
  11341. savedParentExtractSize = 0;;
  11342. savedParentExtract = NULL;
  11343. }
  11344. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  11345. {
  11346. //Don't start the inputs yet so we can short-circuit...
  11347. CRoxieServerMultiInputBaseActivity::start(parentExtractSize, parentExtract, paused);
  11348. savedParentExtractSize = parentExtractSize;
  11349. savedParentExtract = parentExtract;
  11350. }
  11351. virtual void stop(bool aborting)
  11352. {
  11353. if (foundInput)
  11354. {
  11355. if (selectedInput)
  11356. selectedInput->stop(aborting);
  11357. }
  11358. else
  11359. {
  11360. for (unsigned i = 0; i < numInputs; i++)
  11361. inputArray[i]->stop(aborting);
  11362. }
  11363. CRoxieServerMultiInputBaseActivity::stop(aborting);
  11364. }
  11365. virtual void reset()
  11366. {
  11367. CRoxieServerMultiInputBaseActivity::reset();
  11368. foundInput = false;
  11369. selectedInput = NULL;
  11370. }
  11371. virtual unsigned __int64 queryLocalCycles() const
  11372. {
  11373. return 0; // Can't easily calcuate anything reliable but local processing is negligible
  11374. }
  11375. virtual const void * nextInGroup()
  11376. {
  11377. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  11378. if (!foundInput)
  11379. {
  11380. foundInput = true;
  11381. //If we get an exception in this loop then stop() will stop any started inputs
  11382. for (unsigned i=0; i < numInputs; i++)
  11383. {
  11384. selectedInput = inputArray[i];
  11385. selectedInput->start(savedParentExtractSize, savedParentExtract, false);
  11386. const void * next = selectedInput->nextInGroup();
  11387. if (next)
  11388. {
  11389. //Found a row so stop remaining
  11390. for (unsigned j=i+1; j < numInputs; j++)
  11391. inputArray[j]->stop(false);
  11392. processed++;
  11393. return next;
  11394. }
  11395. selectedInput->stop(false);
  11396. }
  11397. selectedInput = NULL;
  11398. return NULL;
  11399. }
  11400. if (!selectedInput)
  11401. return NULL;
  11402. const void * next = selectedInput->nextInGroup();
  11403. if (next)
  11404. processed++;
  11405. return next;
  11406. }
  11407. };
  11408. class CRoxieServerNonEmptyActivityFactory : public CRoxieServerMultiInputFactory
  11409. {
  11410. public:
  11411. CRoxieServerNonEmptyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  11412. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  11413. {
  11414. }
  11415. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  11416. {
  11417. return new CRoxieServerNonEmptyActivity(this, _probeManager, numInputs());
  11418. }
  11419. };
  11420. IRoxieServerActivityFactory *createRoxieServerNonEmptyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  11421. {
  11422. return new CRoxieServerNonEmptyActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  11423. }
  11424. //=================================================================================
  11425. class CRoxieServerMergeActivity : public CRoxieServerActivity
  11426. {
  11427. IHThorMergeArg &helper;
  11428. unsigned *mergeheap;
  11429. unsigned activeInputs;
  11430. unsigned numInputs;
  11431. IRoxieInput **inputArray;
  11432. const void **pending;
  11433. bool first;
  11434. ICompare *compare;
  11435. bool dedup;
  11436. void permute()
  11437. {
  11438. assertex(activeInputs == 0);
  11439. for(unsigned i = 0; i < numInputs; i++)
  11440. if(pullInput(i))
  11441. mergeheap[activeInputs++] = i;
  11442. // the tree structure: element p has children p*2+1 and p*2+2, or element c has parent (unsigned)(c-1)/2
  11443. // the heap property: no element should be smaller than its parent
  11444. // the dedup variant: if(dedup), the top of the heap should also not be equal to either child
  11445. // the method: establish this by starting with the parent of the bottom element and working up to the top element, sifting each down to its correct place
  11446. if (activeInputs >= 2)
  11447. for(unsigned p = (activeInputs-2)/2; p > 0; --p)
  11448. siftDown(p);
  11449. if(dedup)
  11450. siftDownDedupTop();
  11451. else
  11452. siftDown(0);
  11453. }
  11454. void readNext()
  11455. {
  11456. if(!pullInput(mergeheap[0]))
  11457. if(!promote(0))
  11458. return;
  11459. // we have changed the element at the top of the heap, so need to sift it down to maintain the heap property
  11460. if(dedup)
  11461. siftDownDedupTop();
  11462. else
  11463. siftDown(0);
  11464. }
  11465. bool pullInput(unsigned i)
  11466. {
  11467. const void *next = inputArray[i]->nextInGroup();
  11468. if (!next)
  11469. next = inputArray[i]->nextInGroup();
  11470. pending[i] = next;
  11471. return (next != NULL);
  11472. }
  11473. bool promote(unsigned p)
  11474. {
  11475. activeInputs--;
  11476. if(activeInputs == p)
  11477. return false;
  11478. mergeheap[p] = mergeheap[activeInputs];
  11479. return true;
  11480. }
  11481. bool siftDown(unsigned p)
  11482. {
  11483. // assumimg that all descendents of p form a heap, sift p down to its correct position, and so include it in the heap
  11484. bool nochange = true;
  11485. while(1)
  11486. {
  11487. unsigned c = p*2 + 1;
  11488. if(c >= activeInputs)
  11489. return nochange;
  11490. if(c+1 < activeInputs)
  11491. {
  11492. int childcmp = BuffCompare(c+1, c);
  11493. if((childcmp < 0) || ((childcmp == 0) && (mergeheap[c+1] < mergeheap[c])))
  11494. ++c;
  11495. }
  11496. int cmp = BuffCompare(c, p);
  11497. if((cmp > 0) || ((cmp == 0) && (mergeheap[c] > mergeheap[p])))
  11498. return nochange;
  11499. nochange = false;
  11500. unsigned r = mergeheap[c];
  11501. mergeheap[c] = mergeheap[p];
  11502. mergeheap[p] = r;
  11503. p = c;
  11504. }
  11505. }
  11506. void siftDownDedupTop()
  11507. {
  11508. // same as siftDown(0), except that it also ensures that the top of the heap is not equal to either of its children
  11509. if(activeInputs < 2)
  11510. return;
  11511. unsigned c = 1;
  11512. int childcmp = 1;
  11513. if(activeInputs >= 3)
  11514. {
  11515. childcmp = BuffCompare(2, 1);
  11516. if(childcmp < 0)
  11517. c = 2;
  11518. }
  11519. int cmp = BuffCompare(c, 0);
  11520. if(cmp > 0)
  11521. return;
  11522. // the following loop ensures the correct property holds on the smaller branch, and that childcmp==0 iff the top matches the other branch
  11523. while(cmp <= 0)
  11524. {
  11525. if(cmp == 0)
  11526. {
  11527. if(mergeheap[c] < mergeheap[0])
  11528. {
  11529. unsigned r = mergeheap[c];
  11530. mergeheap[c] = mergeheap[0];
  11531. mergeheap[0] = r;
  11532. }
  11533. ReleaseClearRoxieRow(pending[mergeheap[c]]);
  11534. if(!pullInput(mergeheap[c]))
  11535. if(!promote(c))
  11536. break;
  11537. siftDown(c);
  11538. }
  11539. else
  11540. {
  11541. unsigned r = mergeheap[c];
  11542. mergeheap[c] = mergeheap[0];
  11543. mergeheap[0] = r;
  11544. if(siftDown(c))
  11545. break;
  11546. }
  11547. cmp = BuffCompare(c, 0);
  11548. }
  11549. // the following loop ensures the uniqueness property holds on the other branch too
  11550. c = 3-c;
  11551. if(activeInputs <= c)
  11552. return;
  11553. while(childcmp == 0)
  11554. {
  11555. if(mergeheap[c] < mergeheap[0])
  11556. {
  11557. unsigned r = mergeheap[c];
  11558. mergeheap[c] = mergeheap[0];
  11559. mergeheap[0] = r;
  11560. }
  11561. ReleaseClearRoxieRow(pending[mergeheap[c]]);
  11562. if(!pullInput(mergeheap[c]))
  11563. if(!promote(c))
  11564. break;
  11565. siftDown(c);
  11566. childcmp = BuffCompare(c, 0);
  11567. }
  11568. }
  11569. inline int BuffCompare(unsigned a, unsigned b)
  11570. {
  11571. return compare->docompare(pending[mergeheap[a]], pending[mergeheap[b]]);
  11572. }
  11573. public:
  11574. CRoxieServerMergeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  11575. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorMergeArg &)basehelper), numInputs(_numInputs)
  11576. {
  11577. activeInputs = 0;
  11578. first = true;
  11579. mergeheap = new unsigned[numInputs];
  11580. inputArray = new IRoxieInput*[numInputs];
  11581. pending = new const void *[numInputs];
  11582. compare = helper.queryCompare();
  11583. dedup = helper.dedup();
  11584. for (unsigned i = 0; i < numInputs; i++)
  11585. {
  11586. inputArray[i] = NULL;
  11587. pending[i] = NULL;
  11588. }
  11589. }
  11590. ~CRoxieServerMergeActivity()
  11591. {
  11592. delete [] mergeheap;
  11593. delete [] inputArray;
  11594. delete [] pending;
  11595. }
  11596. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  11597. {
  11598. activeInputs = 0;
  11599. first = true;
  11600. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  11601. for (unsigned i = 0; i < numInputs; i++)
  11602. {
  11603. inputArray[i]->start(parentExtractSize, parentExtract, paused);
  11604. }
  11605. }
  11606. virtual void stop(bool aborting)
  11607. {
  11608. for (unsigned i = 0; i < numInputs; i++)
  11609. {
  11610. inputArray[i]->stop(aborting);
  11611. }
  11612. CRoxieServerActivity::stop(aborting);
  11613. }
  11614. virtual void reset()
  11615. {
  11616. for (unsigned i = 0; i < numInputs; i++)
  11617. {
  11618. ReleaseClearRoxieRow(pending[i]);
  11619. inputArray[i]->reset();
  11620. }
  11621. CRoxieServerActivity::reset();
  11622. }
  11623. virtual void setInput(unsigned idx, IRoxieInput *_in)
  11624. {
  11625. inputArray[idx] = _in;
  11626. }
  11627. virtual const void * nextInGroup()
  11628. {
  11629. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  11630. if (first)
  11631. {
  11632. permute();
  11633. first = false;
  11634. }
  11635. if (activeInputs)
  11636. {
  11637. const void *next = pending[mergeheap[0]];
  11638. readNext();
  11639. if (next)
  11640. processed++;
  11641. return next;
  11642. }
  11643. else
  11644. return NULL;
  11645. }
  11646. };
  11647. class CRoxieServerMergeActivityFactory : public CRoxieServerMultiInputFactory
  11648. {
  11649. public:
  11650. CRoxieServerMergeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  11651. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  11652. {
  11653. }
  11654. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  11655. {
  11656. return new CRoxieServerMergeActivity(this, _probeManager, numInputs());
  11657. }
  11658. };
  11659. IRoxieServerActivityFactory *createRoxieServerMergeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  11660. {
  11661. return new CRoxieServerMergeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  11662. }
  11663. //=================================================================================
  11664. class CRoxieServerRegroupActivity : public CRoxieServerMultiInputActivity
  11665. {
  11666. IHThorRegroupArg &helper;
  11667. unsigned inputIndex;
  11668. bool eof;
  11669. unsigned __int64 numProcessedLastGroup;
  11670. public:
  11671. CRoxieServerRegroupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  11672. : CRoxieServerMultiInputActivity(_factory, _probeManager, _numInputs), helper((IHThorRegroupArg &)basehelper)
  11673. {
  11674. inputIndex = 0;
  11675. eof = false;
  11676. numProcessedLastGroup = 0;
  11677. }
  11678. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  11679. {
  11680. inputIndex = 0;
  11681. eof = false;
  11682. numProcessedLastGroup = processed;
  11683. CRoxieServerMultiInputActivity::start(parentExtractSize, parentExtract, paused);
  11684. }
  11685. const void * nextFromInputs()
  11686. {
  11687. unsigned initialInput = inputIndex;
  11688. while (inputIndex < numInputs)
  11689. {
  11690. const void * next = inputArray[inputIndex]->nextInGroup();
  11691. if (next)
  11692. {
  11693. if ((inputIndex != initialInput) && (inputIndex != initialInput+1))
  11694. {
  11695. ReleaseRoxieRow(next);
  11696. throw MakeStringException(ROXIE_MISMATCH_GROUP_ERROR, "Mismatched groups supplied to Regroup (%d)", factory->queryId());
  11697. }
  11698. return next;
  11699. }
  11700. inputIndex++;
  11701. }
  11702. if ((initialInput != 0) && (initialInput+1 != numInputs))
  11703. throw MakeStringException(ROXIE_MISMATCH_GROUP_ERROR, "Mismatched groups supplied to Regroup (%d)", factory->queryId());
  11704. inputIndex = 0;
  11705. return NULL;
  11706. }
  11707. virtual const void * nextInGroup()
  11708. {
  11709. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  11710. if (eof)
  11711. return NULL;
  11712. const void * ret = nextFromInputs();
  11713. if (ret)
  11714. {
  11715. processed++;
  11716. return ret;
  11717. }
  11718. if (numProcessedLastGroup != processed)
  11719. {
  11720. numProcessedLastGroup = processed;
  11721. return NULL;
  11722. }
  11723. eof = true;
  11724. return NULL;
  11725. }
  11726. #if 0
  11727. virtual void setInput(unsigned idx, IRoxieInput *_in)
  11728. {
  11729. //MORE: RKC: Do we want to do this i) always ii) conditionally iii) never
  11730. if (idx)
  11731. {
  11732. puller.setown(new CRoxieServerReadAheadInput(0)); // MORE - cant ask context for parallelJoinPreload as context is not yet set up.
  11733. puller->setInput(0, _in);
  11734. CRoxieServerMultiInputActivity::setInput(idx, puller);
  11735. }
  11736. else
  11737. CRoxieServerMultiInputActivity::setInput(idx, _in);
  11738. }
  11739. #endif
  11740. };
  11741. class CRoxieServerRegroupActivityFactory : public CRoxieServerMultiInputFactory
  11742. {
  11743. public:
  11744. CRoxieServerRegroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  11745. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  11746. {
  11747. }
  11748. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  11749. {
  11750. return new CRoxieServerRegroupActivity(this, _probeManager, numInputs());
  11751. }
  11752. };
  11753. IRoxieServerActivityFactory *createRoxieServerRegroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  11754. {
  11755. return new CRoxieServerRegroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  11756. }
  11757. //=================================================================================
  11758. class CRoxieServerCombineActivity : public CRoxieServerMultiInputActivity
  11759. {
  11760. IHThorCombineArg &helper;
  11761. unsigned __int64 numProcessedLastGroup;
  11762. public:
  11763. CRoxieServerCombineActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  11764. : CRoxieServerMultiInputActivity(_factory, _probeManager, _numInputs), helper((IHThorCombineArg &)basehelper)
  11765. {
  11766. numProcessedLastGroup = 0;
  11767. }
  11768. ~CRoxieServerCombineActivity()
  11769. {
  11770. }
  11771. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  11772. {
  11773. numProcessedLastGroup = processed;
  11774. CRoxieServerMultiInputActivity::start(parentExtractSize, parentExtract, paused);
  11775. }
  11776. void nextInputs(ConstPointerArray & out)
  11777. {
  11778. for (unsigned i=0; i < numInputs; i++)
  11779. {
  11780. const void * next = inputArray[i]->nextInGroup();
  11781. if (next)
  11782. out.append(next);
  11783. }
  11784. }
  11785. virtual bool needsAllocator() const { return true; }
  11786. virtual const void * nextInGroup()
  11787. {
  11788. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  11789. loop
  11790. {
  11791. ConstPointerArray group;
  11792. nextInputs(group);
  11793. if ((group.ordinality() == 0) && (numProcessedLastGroup == processed))
  11794. nextInputs(group);
  11795. if (group.ordinality() == 0)
  11796. {
  11797. numProcessedLastGroup = processed;
  11798. return NULL;
  11799. }
  11800. else if (group.ordinality() != numInputs)
  11801. {
  11802. ReleaseRoxieRowSet(group);
  11803. throw MakeStringException(ROXIE_MISMATCH_GROUP_ERROR, "Mismatched group input for Combine Activity(%d)", factory->queryId());
  11804. }
  11805. try
  11806. {
  11807. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  11808. size32_t outSize = helper.transform(rowBuilder, group.ordinality(), group.getArray());
  11809. ReleaseRoxieRowSet(group);
  11810. if (outSize)
  11811. {
  11812. processed++;
  11813. return rowBuilder.finalizeRowClear(outSize);
  11814. }
  11815. }
  11816. catch (IException *E)
  11817. {
  11818. ReleaseRoxieRowSet(group);
  11819. throw makeWrappedException(E);
  11820. }
  11821. }
  11822. }
  11823. };
  11824. class CRoxieServerCombineActivityFactory : public CRoxieServerMultiInputFactory
  11825. {
  11826. public:
  11827. CRoxieServerCombineActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  11828. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  11829. {
  11830. }
  11831. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  11832. {
  11833. return new CRoxieServerCombineActivity(this, _probeManager, numInputs());
  11834. }
  11835. };
  11836. IRoxieServerActivityFactory *createRoxieServerCombineActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  11837. {
  11838. return new CRoxieServerCombineActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  11839. }
  11840. //=================================================================================
  11841. class CRoxieServerCombineGroupActivity : public CRoxieServerTwoInputActivity
  11842. {
  11843. IHThorCombineGroupArg &helper;
  11844. unsigned __int64 numProcessedLastGroup;
  11845. public:
  11846. CRoxieServerCombineGroupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  11847. : CRoxieServerTwoInputActivity(_factory, _probeManager), helper((IHThorCombineGroupArg &)basehelper)
  11848. {
  11849. numProcessedLastGroup = 0;
  11850. }
  11851. ~CRoxieServerCombineGroupActivity()
  11852. {
  11853. }
  11854. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  11855. {
  11856. numProcessedLastGroup = processed;
  11857. CRoxieServerTwoInputActivity::start(parentExtractSize, parentExtract, paused);
  11858. }
  11859. virtual void setInput(unsigned idx, IRoxieInput *_in)
  11860. {
  11861. switch(idx)
  11862. {
  11863. case 0:
  11864. #if 0
  11865. //MORE: RKC: Do we want to do this i) always ii) conditionally iii) never
  11866. puller.setown(new CRoxieServerReadAheadInput(0)); // MORE - cant ask context for parallelJoinPreload as context is not yet set up.
  11867. puller->setInput(0, _in);
  11868. _in = puller;
  11869. #endif
  11870. input = _in;
  11871. break;
  11872. case 1:
  11873. input1 = _in;
  11874. break;
  11875. default:
  11876. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
  11877. }
  11878. }
  11879. virtual IRoxieInput *queryOutput(unsigned idx)
  11880. {
  11881. if (idx==(unsigned)-1)
  11882. idx = 0;
  11883. return idx ? NULL : this;
  11884. }
  11885. virtual bool needsAllocator() const { return true; }
  11886. virtual const void * nextInGroup()
  11887. {
  11888. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  11889. loop
  11890. {
  11891. const void * left = input->nextInGroup();
  11892. if (!left && (numProcessedLastGroup == processed))
  11893. left = input->nextInGroup();
  11894. if (!left)
  11895. {
  11896. if (numProcessedLastGroup == processed)
  11897. {
  11898. const void * nextRight = input1->nextInGroup();
  11899. if (nextRight)
  11900. {
  11901. ReleaseRoxieRow(nextRight);
  11902. throw MakeStringException(ROXIE_MISSING_GROUP_ERROR, "Missing LEFT record for Combine Group (%d)", factory->queryId());
  11903. }
  11904. }
  11905. else
  11906. numProcessedLastGroup = processed;
  11907. return NULL;
  11908. }
  11909. ConstPointerArray group;
  11910. loop
  11911. {
  11912. const void * in = input1->nextInGroup();
  11913. if (!in)
  11914. break;
  11915. group.append(in);
  11916. }
  11917. if (group.ordinality() == 0)
  11918. {
  11919. ReleaseRoxieRow(left);
  11920. ReleaseRoxieRowSet(group);
  11921. throw MakeStringException(ROXIE_MISSING_GROUP_ERROR, "Missing RIGHT group for Combine Group (%d)", factory->queryId());
  11922. }
  11923. try
  11924. {
  11925. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  11926. size32_t outSize = helper.transform(rowBuilder, left, group.ordinality(), (const void * *)group.getArray());
  11927. ReleaseRoxieRow(left);
  11928. ReleaseRoxieRowSet(group);
  11929. if (outSize)
  11930. {
  11931. processed++;
  11932. return rowBuilder.finalizeRowClear(outSize);
  11933. }
  11934. }
  11935. catch (IException *E)
  11936. {
  11937. ReleaseRoxieRow(left);
  11938. ReleaseRoxieRowSet(group);
  11939. throw makeWrappedException(E);
  11940. }
  11941. }
  11942. }
  11943. };
  11944. class CRoxieServerCombineGroupActivityFactory : public CRoxieServerActivityFactory
  11945. {
  11946. unsigned input2;
  11947. unsigned input2idx;
  11948. public:
  11949. CRoxieServerCombineGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  11950. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  11951. {
  11952. input2 = 0;
  11953. input2idx = 0;
  11954. }
  11955. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  11956. {
  11957. return new CRoxieServerCombineGroupActivity(this, _probeManager);
  11958. }
  11959. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  11960. {
  11961. if (idx==1)
  11962. {
  11963. input2 = source;
  11964. input2idx = sourceidx;
  11965. }
  11966. else
  11967. CRoxieServerActivityFactory::setInput(idx, source, sourceidx);
  11968. }
  11969. virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
  11970. {
  11971. switch (idx)
  11972. {
  11973. case 1:
  11974. sourceidx = input2idx;
  11975. return input2;
  11976. case 0:
  11977. return CRoxieServerActivityFactory::getInput(idx, sourceidx);
  11978. default:
  11979. return (unsigned) -1;
  11980. }
  11981. }
  11982. virtual unsigned numInputs() const { return 2; }
  11983. };
  11984. IRoxieServerActivityFactory *createRoxieServerCombineGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  11985. {
  11986. return new CRoxieServerCombineGroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  11987. }
  11988. //=================================================================================
  11989. class CRoxieServerRollupGroupActivity : public CRoxieServerActivity
  11990. {
  11991. IHThorRollupGroupArg &helper;
  11992. bool eof;
  11993. public:
  11994. CRoxieServerRollupGroupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  11995. : CRoxieServerActivity(_factory, _probeManager),
  11996. helper((IHThorRollupGroupArg &)basehelper)
  11997. {
  11998. eof = false;
  11999. }
  12000. ~CRoxieServerRollupGroupActivity()
  12001. {
  12002. }
  12003. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12004. {
  12005. eof = false;
  12006. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  12007. }
  12008. virtual bool needsAllocator() const { return true; }
  12009. virtual const void * nextInGroup()
  12010. {
  12011. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  12012. if (eof)
  12013. return NULL;
  12014. loop
  12015. {
  12016. ConstPointerArray group;
  12017. loop
  12018. {
  12019. const void * in = input->nextInGroup();
  12020. if (!in)
  12021. break;
  12022. group.append(in);
  12023. }
  12024. if (group.ordinality() == 0)
  12025. {
  12026. eof = true;
  12027. return NULL;
  12028. }
  12029. try
  12030. {
  12031. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  12032. size32_t outSize = helper.transform(rowBuilder, group.ordinality(), (const void * *)group.getArray());
  12033. ReleaseRoxieRowSet(group);
  12034. if (outSize)
  12035. {
  12036. processed++;
  12037. return rowBuilder.finalizeRowClear(outSize);
  12038. }
  12039. }
  12040. catch (IException * E)
  12041. {
  12042. ReleaseRoxieRowSet(group);
  12043. throw makeWrappedException(E);
  12044. }
  12045. }
  12046. }
  12047. };
  12048. class CRoxieServerRollupGroupActivityFactory : public CRoxieServerActivityFactory
  12049. {
  12050. public:
  12051. CRoxieServerRollupGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  12052. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  12053. {
  12054. }
  12055. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  12056. {
  12057. return new CRoxieServerRollupGroupActivity(this, _probeManager);
  12058. }
  12059. };
  12060. IRoxieServerActivityFactory *createRoxieServerRollupGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  12061. {
  12062. return new CRoxieServerRollupGroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  12063. }
  12064. //=================================================================================
  12065. class CRoxieServerFilterProjectActivity : public CRoxieServerLateStartActivity
  12066. {
  12067. IHThorFilterProjectArg &helper;
  12068. unsigned numProcessedLastGroup;
  12069. unsigned __int64 recordCount;
  12070. public:
  12071. CRoxieServerFilterProjectActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  12072. : CRoxieServerLateStartActivity(_factory, _probeManager), helper((IHThorFilterProjectArg &)basehelper)
  12073. {
  12074. numProcessedLastGroup = 0;
  12075. recordCount = 0;
  12076. }
  12077. ~CRoxieServerFilterProjectActivity()
  12078. {
  12079. }
  12080. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12081. {
  12082. numProcessedLastGroup = 0;
  12083. recordCount = 0;
  12084. CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
  12085. lateStart(parentExtractSize, parentExtract, helper.canMatchAny()); //sets eof
  12086. }
  12087. virtual bool needsAllocator() const { return true; }
  12088. virtual const void * nextInGroup()
  12089. {
  12090. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  12091. if (eof)
  12092. return NULL;
  12093. loop
  12094. {
  12095. const void * in = input->nextInGroup();
  12096. if (!in)
  12097. {
  12098. recordCount = 0;
  12099. if (numProcessedLastGroup == processed)
  12100. in = input->nextInGroup();
  12101. if (!in)
  12102. {
  12103. numProcessedLastGroup = processed;
  12104. return NULL;
  12105. }
  12106. }
  12107. try
  12108. {
  12109. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  12110. size32_t outSize = helper.transform(rowBuilder, in, ++recordCount);
  12111. ReleaseRoxieRow(in);
  12112. if (outSize)
  12113. {
  12114. processed++;
  12115. return rowBuilder.finalizeRowClear(outSize);
  12116. }
  12117. }
  12118. catch (IException *E)
  12119. {
  12120. throw makeWrappedException(E);
  12121. }
  12122. }
  12123. }
  12124. };
  12125. class CRoxieServerFilterProjectActivityFactory : public CRoxieServerActivityFactory
  12126. {
  12127. public:
  12128. CRoxieServerFilterProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  12129. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  12130. {
  12131. }
  12132. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  12133. {
  12134. return new CRoxieServerFilterProjectActivity(this, _probeManager);
  12135. }
  12136. };
  12137. IRoxieServerActivityFactory *createRoxieServerFilterProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  12138. {
  12139. return new CRoxieServerFilterProjectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  12140. }
  12141. //=================================================================================
  12142. class CRoxieServerProjectActivity : public CRoxieServerActivity
  12143. {
  12144. unsigned numProcessedLastGroup;
  12145. bool count;
  12146. unsigned __int64 recordCount;
  12147. public:
  12148. CRoxieServerProjectActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _count)
  12149. : CRoxieServerActivity(_factory, _probeManager),
  12150. count(_count)
  12151. {
  12152. numProcessedLastGroup = 0;
  12153. recordCount = 0;
  12154. }
  12155. ~CRoxieServerProjectActivity()
  12156. {
  12157. }
  12158. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12159. {
  12160. numProcessedLastGroup = 0;
  12161. recordCount = 0;
  12162. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  12163. }
  12164. virtual bool needsAllocator() const { return true; }
  12165. virtual const void * nextInGroup()
  12166. {
  12167. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  12168. loop
  12169. {
  12170. OwnedConstRoxieRow in = input->nextInGroup();
  12171. if (!in)
  12172. {
  12173. recordCount = 0;
  12174. if (numProcessedLastGroup == processed)
  12175. in.setown(input->nextInGroup());
  12176. if (!in)
  12177. {
  12178. numProcessedLastGroup = processed;
  12179. return NULL;
  12180. }
  12181. }
  12182. try
  12183. {
  12184. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  12185. size32_t outSize;
  12186. if (count)
  12187. outSize = ((IHThorCountProjectArg &) basehelper).transform(rowBuilder, in, ++recordCount);
  12188. else
  12189. outSize = ((IHThorProjectArg &) basehelper).transform(rowBuilder, in);
  12190. if (outSize)
  12191. {
  12192. processed++;
  12193. return rowBuilder.finalizeRowClear(outSize);
  12194. }
  12195. }
  12196. catch (IException *E)
  12197. {
  12198. throw makeWrappedException(E);
  12199. }
  12200. }
  12201. }
  12202. };
  12203. class CRoxieServerProjectActivityFactory : public CRoxieServerActivityFactory
  12204. {
  12205. protected:
  12206. bool count;
  12207. public:
  12208. CRoxieServerProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  12209. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  12210. {
  12211. count = (_kind==TAKcountproject || _kind==TAKprefetchcountproject);
  12212. }
  12213. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  12214. {
  12215. return new CRoxieServerProjectActivity(this, _probeManager, count);
  12216. }
  12217. };
  12218. IRoxieServerActivityFactory *createRoxieServerProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  12219. {
  12220. return new CRoxieServerProjectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  12221. }
  12222. //=================================================================================
  12223. class CRoxieServerPrefetchProjectActivity : public CRoxieServerActivity, implements IRecordPullerCallback
  12224. {
  12225. unsigned numProcessedLastGroup;
  12226. bool count;
  12227. bool eof;
  12228. bool allPulled;
  12229. bool isThreaded;
  12230. unsigned preload;
  12231. unsigned __int64 recordCount;
  12232. IHThorPrefetchProjectArg &helper;
  12233. RecordPullerThread puller;
  12234. InterruptableSemaphore ready;
  12235. InterruptableSemaphore space;
  12236. class PrefetchInfo : public CInterface
  12237. {
  12238. public:
  12239. inline PrefetchInfo(IHThorPrefetchProjectArg &helper, const void *_in, unsigned __int64 _recordCount)
  12240. {
  12241. if (helper.preTransform(extract, _in, _recordCount))
  12242. {
  12243. in.setown(_in);
  12244. recordCount = _recordCount;
  12245. }
  12246. else
  12247. ReleaseRoxieRow(_in);
  12248. }
  12249. OwnedConstRoxieRow in;
  12250. unsigned __int64 recordCount;
  12251. rtlRowBuilder extract;
  12252. };
  12253. QueueOf<PrefetchInfo, true> pulled;
  12254. CriticalSection pulledCrit;
  12255. public:
  12256. CRoxieServerPrefetchProjectActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _count)
  12257. : CRoxieServerActivity(_factory, _probeManager),
  12258. helper((IHThorPrefetchProjectArg &) basehelper),
  12259. puller(false),
  12260. count(_count)
  12261. {
  12262. numProcessedLastGroup = 0;
  12263. recordCount = 0;
  12264. eof = false;
  12265. allPulled = false;
  12266. isThreaded = (helper.getFlags() & PPFparallel) != 0;
  12267. preload = 0;
  12268. }
  12269. ~CRoxieServerPrefetchProjectActivity()
  12270. {
  12271. while (pulled.ordinality())
  12272. ::Release(pulled.dequeue());
  12273. }
  12274. virtual void setInput(unsigned idx, IRoxieInput *_in)
  12275. {
  12276. if (idx)
  12277. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
  12278. puller.setInput(this, _in);
  12279. }
  12280. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12281. {
  12282. numProcessedLastGroup = 0;
  12283. recordCount = 0;
  12284. eof = false;
  12285. allPulled = false;
  12286. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  12287. preload = helper.getLookahead();
  12288. if (!preload)
  12289. preload = ctx->prefetchProjectPreload();
  12290. space.reinit(preload);
  12291. ready.reinit();
  12292. puller.start(parentExtractSize, parentExtract, paused, preload, !isThreaded, ctx);
  12293. }
  12294. virtual void stop(bool aborting)
  12295. {
  12296. space.interrupt();
  12297. ready.interrupt();
  12298. CRoxieServerActivity::stop(aborting);
  12299. puller.stop(aborting);
  12300. }
  12301. virtual void reset()
  12302. {
  12303. CRoxieServerActivity::reset();
  12304. puller.reset();
  12305. allPulled = false;
  12306. while (pulled.ordinality())
  12307. ::Release(pulled.dequeue());
  12308. }
  12309. virtual PrefetchInfo *readNextRecord()
  12310. {
  12311. if (!isThreaded)
  12312. {
  12313. if (!allPulled) // This looks like it's thread unsafe but we are inside the if(!isThreaded) so should be ok
  12314. puller.pullRecords(1);
  12315. }
  12316. else
  12317. ready.wait();
  12318. CriticalBlock b(pulledCrit);
  12319. PrefetchInfo *ret = pulled.ordinality() ? pulled.dequeue() : NULL;
  12320. space.signal();
  12321. return ret;
  12322. }
  12323. virtual bool needsAllocator() const { return true; }
  12324. virtual const void * nextInGroup()
  12325. {
  12326. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  12327. if (eof)
  12328. return NULL;
  12329. loop
  12330. {
  12331. Owned<PrefetchInfo> in = readNextRecord();
  12332. if (!in)
  12333. {
  12334. recordCount = 0;
  12335. if (numProcessedLastGroup == processed)
  12336. in.setown(readNextRecord());
  12337. if (!in)
  12338. {
  12339. numProcessedLastGroup = processed;
  12340. eof = true;
  12341. return NULL;
  12342. }
  12343. }
  12344. try
  12345. {
  12346. if (in->in)
  12347. {
  12348. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  12349. size32_t outSize;
  12350. IThorChildGraph *child = helper.queryChild();
  12351. Owned<IEclGraphResults> results;
  12352. if (child)
  12353. results.setown(child->evaluate(in->extract.size(), in->extract.getbytes()));
  12354. outSize = helper.transform(rowBuilder, in->in, results, in->recordCount);
  12355. if (outSize)
  12356. {
  12357. processed++;
  12358. return rowBuilder.finalizeRowClear(outSize);
  12359. }
  12360. }
  12361. }
  12362. catch (IException *E)
  12363. {
  12364. throw makeWrappedException(E);
  12365. }
  12366. }
  12367. }
  12368. // interface IExceptionHandler
  12369. virtual bool fireException(IException *e)
  12370. {
  12371. // called from puller thread on failure
  12372. ready.interrupt(LINK(e));
  12373. space.interrupt(e);
  12374. return true;
  12375. }
  12376. // interface IRecordPullerCallback
  12377. virtual void processRow(const void *row)
  12378. {
  12379. {
  12380. CriticalBlock b(pulledCrit);
  12381. pulled.enqueue(new PrefetchInfo(helper, row, ++recordCount));
  12382. }
  12383. if (isThreaded)
  12384. {
  12385. ready.signal();
  12386. space.wait();
  12387. }
  12388. }
  12389. virtual void processEOG()
  12390. {
  12391. {
  12392. CriticalBlock b(pulledCrit);
  12393. pulled.enqueue(NULL);
  12394. }
  12395. if (isThreaded)
  12396. {
  12397. ready.signal();
  12398. space.wait();
  12399. }
  12400. }
  12401. virtual void processGroup(const ConstPointerArray &rows)
  12402. {
  12403. throwUnexpected();
  12404. }
  12405. virtual void processDone()
  12406. {
  12407. CriticalBlock b(pulledCrit);
  12408. allPulled = true;
  12409. if (isThreaded)
  12410. ready.signal();
  12411. }
  12412. };
  12413. class CRoxieServerPrefetchProjectActivityFactory : public CRoxieServerProjectActivityFactory
  12414. {
  12415. public:
  12416. CRoxieServerPrefetchProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  12417. : CRoxieServerProjectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  12418. {
  12419. }
  12420. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  12421. {
  12422. return new CRoxieServerPrefetchProjectActivity(this, _probeManager, count);
  12423. }
  12424. };
  12425. extern IRoxieServerActivityFactory *createRoxieServerPrefetchProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  12426. {
  12427. return new CRoxieServerPrefetchProjectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  12428. }
  12429. //=================================================================================
  12430. class CPointerArrayRoxieInput : public CPseudoRoxieInput
  12431. {
  12432. public:
  12433. CPointerArrayRoxieInput()
  12434. {
  12435. rowset = NULL;
  12436. rowcount = 0;
  12437. curRow = 0;
  12438. }
  12439. void init(size32_t _rowcount, byte **_rowset)
  12440. {
  12441. rowset = _rowset;
  12442. rowcount = _rowcount;
  12443. curRow = 0;
  12444. }
  12445. virtual const void * nextInGroup()
  12446. {
  12447. if (curRow < rowcount)
  12448. {
  12449. const void * ret = rowset[curRow];
  12450. if (ret)
  12451. LinkRoxieRow(ret);
  12452. curRow++;
  12453. return ret;
  12454. }
  12455. return NULL;
  12456. }
  12457. protected:
  12458. byte **rowset;
  12459. size32_t rowcount;
  12460. size32_t curRow;
  12461. };
  12462. class CRoxieServerLoopActivity : public CRoxieServerActivity
  12463. {
  12464. protected:
  12465. IHThorLoopArg &helper;
  12466. ThorActivityKind activityKind;
  12467. unsigned maxIterations;
  12468. bool finishedLooping;
  12469. unsigned flags;
  12470. bool eof;
  12471. rtlRowBuilder loopExtractBuilder;
  12472. unsigned loopGraphId;
  12473. Linked<IOutputMetaData> counterMeta;
  12474. public:
  12475. CRoxieServerLoopActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _loopGraphId, IOutputMetaData * _counterMeta)
  12476. : CRoxieServerActivity(_factory, _probeManager),
  12477. helper((IHThorLoopArg &)basehelper), loopGraphId(_loopGraphId), counterMeta(_counterMeta)
  12478. {
  12479. eof = false;
  12480. finishedLooping = false;
  12481. activityKind = factory->getKind();
  12482. flags = helper.getFlags();
  12483. maxIterations = 0;
  12484. }
  12485. virtual bool needsAllocator() const { return true; }
  12486. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12487. {
  12488. eof = false;
  12489. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  12490. int iterations = (int) helper.numIterations();
  12491. maxIterations = (iterations >= 0) ? iterations : 0;
  12492. finishedLooping = ((activityKind == TAKloopcount) && (maxIterations == 0));
  12493. if ((flags & IHThorLoopArg::LFnewloopagain) && !helper.loopFirstTime())
  12494. finishedLooping = true;
  12495. loopExtractBuilder.clear();
  12496. helper.createParentExtract(loopExtractBuilder); // could possibly delay this until execution actually happens
  12497. }
  12498. virtual void stop(bool aborting)
  12499. {
  12500. CRoxieServerActivity::stop(aborting);
  12501. loopExtractBuilder.clear();
  12502. }
  12503. void createCounterResult(IRoxieServerChildGraph * graph, unsigned counter)
  12504. {
  12505. if (flags & IHThorLoopArg::LFcounter)
  12506. {
  12507. void * counterRow = ctx->queryRowManager().allocate(sizeof(thor_loop_counter_t), activityId);
  12508. *((thor_loop_counter_t *)counterRow) = counter;
  12509. RtlLinkedDatasetBuilder builder(rowAllocator);
  12510. builder.appendOwn(counterRow);
  12511. Owned<CGraphResult> counterResult = new CGraphResult(builder.getcount(), builder.linkrows());
  12512. graph->setInputResult(2, counterResult);
  12513. }
  12514. }
  12515. };
  12516. //=================================================================================
  12517. class CRoxieServerSequentialLoopActivity : public CRoxieServerLoopActivity
  12518. {
  12519. Owned<IActivityGraph> loopQuery;
  12520. Owned<IRoxieServerChildGraph> loopGraph;
  12521. IRoxieInput * curInput;
  12522. RtlLinkedDatasetBuilder *loopInputBuilder;
  12523. CPointerArrayRoxieInput arrayInput;
  12524. Linked<IRoxieInput> resultInput;
  12525. unsigned loopCounter;
  12526. public:
  12527. CRoxieServerSequentialLoopActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _loopGraphId, IOutputMetaData * _counterMeta)
  12528. : CRoxieServerLoopActivity(_factory, _probeManager, _loopGraphId, _counterMeta)
  12529. {
  12530. curInput = NULL;
  12531. loopCounter = 0;
  12532. loopInputBuilder = NULL;
  12533. }
  12534. virtual bool needsAllocator() const { return true; }
  12535. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  12536. {
  12537. CRoxieServerLoopActivity::onCreate(_ctx, _colocalParent);
  12538. loopQuery.set(_ctx->queryChildGraph(loopGraphId));
  12539. }
  12540. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12541. {
  12542. curInput = input;
  12543. loopCounter = 1;
  12544. CRoxieServerLoopActivity::start(parentExtractSize, parentExtract, paused);
  12545. //MORE: Not sure about this, should IRoxieServerChildGraph be combined with IActivityGraph?
  12546. loopGraph.set(loopQuery->queryLoopGraph());
  12547. loopInputBuilder = new RtlLinkedDatasetBuilder(rowAllocator);
  12548. }
  12549. virtual void stop(bool aborting)
  12550. {
  12551. delete loopInputBuilder;
  12552. loopInputBuilder = NULL;
  12553. CRoxieServerLoopActivity::stop(aborting);
  12554. }
  12555. virtual const void * nextInGroup()
  12556. {
  12557. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  12558. if (eof)
  12559. return NULL;
  12560. unsigned emptyIterations = 0;
  12561. loop
  12562. {
  12563. loop
  12564. {
  12565. const void * ret = curInput->nextInGroup();
  12566. if (!ret)
  12567. {
  12568. ret = curInput->nextInGroup(); // more cope with groups somehow....
  12569. if (!ret)
  12570. {
  12571. if (finishedLooping)
  12572. {
  12573. eof = true;
  12574. return NULL;
  12575. }
  12576. break;
  12577. }
  12578. }
  12579. if (finishedLooping ||
  12580. ((flags & IHThorLoopArg::LFfiltered) && !helper.sendToLoop(loopCounter, ret)))
  12581. {
  12582. processed++;
  12583. return ret;
  12584. }
  12585. loopInputBuilder->appendOwn(ret);
  12586. }
  12587. switch (activityKind)
  12588. {
  12589. case TAKloopdataset:
  12590. {
  12591. if (!(flags & IHThorLoopArg::LFnewloopagain))
  12592. {
  12593. if (!helper.loopAgain(loopCounter, loopInputBuilder->getcount(), (const void**) loopInputBuilder->queryrows()))
  12594. {
  12595. if (loopInputBuilder->getcount() == 0)
  12596. {
  12597. eof = true;
  12598. return NULL;
  12599. }
  12600. arrayInput.init(loopInputBuilder->getcount(), loopInputBuilder->linkrows());
  12601. // MORE - should builder be cleared here?
  12602. curInput = &arrayInput;
  12603. finishedLooping = true;
  12604. continue; // back to the input loop again
  12605. }
  12606. }
  12607. break;
  12608. }
  12609. case TAKlooprow:
  12610. if (!loopInputBuilder->getcount())
  12611. {
  12612. finishedLooping = true;
  12613. eof = true;
  12614. return NULL;
  12615. }
  12616. break;
  12617. }
  12618. if (loopInputBuilder->getcount())
  12619. emptyIterations = 0;
  12620. else
  12621. {
  12622. //note: any outputs which didn't go around the loop again, would return the record, reinitializing emptyIterations
  12623. emptyIterations++;
  12624. if (emptyIterations > maxEmptyLoopIterations)
  12625. throw MakeStringException(ROXIE_TOO_MANY_EMPTY_LOOP, "Executed LOOP with empty input and output %u times", emptyIterations);
  12626. if (emptyIterations % 32 == 0)
  12627. CTXLOG("Executing LOOP with empty input and output %u times", emptyIterations);
  12628. }
  12629. checkAbort();
  12630. try
  12631. {
  12632. Owned<IRoxieGraphResults> results = executeIteration(loopExtractBuilder.size(), loopExtractBuilder.getbytes(), loopCounter);
  12633. resultInput.setown(results->createIterator(0));
  12634. if (flags & IHThorLoopArg::LFnewloopagain)
  12635. {
  12636. Owned<IRoxieInput> againResult = results->createIterator(helper.loopAgainResult());
  12637. OwnedConstRoxieRow row = againResult->nextInGroup();
  12638. assertex(row);
  12639. //Result is a row which contains a single boolean field.
  12640. if (!((const bool *)row.get())[0])
  12641. finishedLooping = true;
  12642. }
  12643. }
  12644. catch (IException *E)
  12645. {
  12646. throw makeWrappedException(E);
  12647. }
  12648. curInput = resultInput.get();
  12649. loopCounter++;
  12650. if ((activityKind == TAKloopcount) && (loopCounter > maxIterations))
  12651. finishedLooping = true;
  12652. }
  12653. }
  12654. IRoxieGraphResults * executeIteration(unsigned parentExtractSize, const byte *parentExtract, unsigned counter)
  12655. {
  12656. try
  12657. {
  12658. loopGraph->beforeExecute();
  12659. Owned<IGraphResult> inputRowsResult = new CGraphResult(loopInputBuilder->getcount(), loopInputBuilder->linkrows());
  12660. loopInputBuilder->clear();
  12661. loopGraph->setInputResult(1, inputRowsResult);
  12662. createCounterResult(loopGraph, counter);
  12663. Owned<IRoxieGraphResults> ret = loopGraph->execute(parentExtractSize, parentExtract);
  12664. loopGraph->afterExecute();
  12665. return ret.getClear();
  12666. }
  12667. catch (...)
  12668. {
  12669. CTXLOG("Exception thrown in loop body - cleaning up");
  12670. loopGraph->afterExecute();
  12671. throw;
  12672. }
  12673. }
  12674. };
  12675. //=================================================================================
  12676. typedef SafeQueueOf<const void, true> SafeRowQueue;
  12677. class CRowQueuePseudoInput : public CPseudoRoxieInput
  12678. {
  12679. public:
  12680. CRowQueuePseudoInput(SafeRowQueue & _input) :
  12681. input(_input)
  12682. {
  12683. eof = false;
  12684. }
  12685. virtual const void * nextInGroup()
  12686. {
  12687. if (eof)
  12688. return NULL;
  12689. const void * ret = input.dequeue();
  12690. if (!ret)
  12691. eof = true;
  12692. return ret;
  12693. }
  12694. protected:
  12695. SafeRowQueue & input;
  12696. bool eof;
  12697. };
  12698. class CRoxieServerParallelLoopActivity;
  12699. class LoopFilterPseudoInput : public CIndirectRoxieInput
  12700. {
  12701. public:
  12702. LoopFilterPseudoInput(CRoxieServerParallelLoopActivity * _activity, IRoxieInput * _input, unsigned _counter) :
  12703. CIndirectRoxieInput(_input), activity(_activity), counter(_counter)
  12704. {
  12705. }
  12706. virtual const void * nextInGroup();
  12707. protected:
  12708. CRoxieServerParallelLoopActivity * activity;
  12709. unsigned counter;
  12710. };
  12711. class LoopExecutorThread : public RestartableThread
  12712. {
  12713. protected:
  12714. Owned<IRoxieInput> safeInput;
  12715. CRoxieServerParallelLoopActivity * activity;
  12716. bool eof;
  12717. CriticalSection crit;
  12718. unsigned flags;
  12719. SafeRowQueue tempResults[2];
  12720. unsigned savedParentExtractSize;
  12721. const byte * savedParentExtract;
  12722. IArrayOf<IActivityGraph> cachedGraphs;
  12723. IRoxieSlaveContext *ctx;
  12724. public:
  12725. LoopExecutorThread()
  12726. : RestartableThread("LoopExecutorThread")
  12727. {
  12728. activity = NULL;
  12729. eof = false;
  12730. flags = 0;
  12731. ctx = NULL;
  12732. savedParentExtract = NULL;
  12733. savedParentExtractSize = 0;
  12734. }
  12735. virtual IRoxieInput *queryInput(unsigned idx) const
  12736. {
  12737. return safeInput->queryInput(idx);
  12738. }
  12739. void setInput(CRoxieServerParallelLoopActivity * _activity, IRoxieInput *_input, unsigned _flags)
  12740. {
  12741. activity = _activity;
  12742. flags = _flags;
  12743. // stop is called on our consumer's thread. We need to take care calling stop for our input to make sure it is not in mid-nextInGroup etc etc.
  12744. safeInput.setown(new CSafeRoxieInput(_input));
  12745. }
  12746. IRoxieInput *queryInput() const
  12747. {
  12748. return safeInput;
  12749. }
  12750. void onCreate(IRoxieSlaveContext * _ctx);
  12751. void start(unsigned parentExtractSize, const byte *parentExtract, bool paused);
  12752. void stop(bool aborting);
  12753. void reset();
  12754. virtual int run();
  12755. protected:
  12756. void executeLoop();
  12757. void executeLoopInstance(unsigned counter, unsigned numIterations, IRoxieInput * input, SafeRowQueue * spillOutput);
  12758. IRoxieInput * createLoopIterationGraph(unsigned i, IRoxieInput * input, unsigned counter);
  12759. };
  12760. class CRoxieServerParallelLoopActivity : public CRoxieServerLoopActivity
  12761. {
  12762. friend class LoopFilterPseudoInput;
  12763. friend class LoopExecutorThread;
  12764. QueueOf<const void, true> ready;
  12765. CriticalSection helperCS;
  12766. CriticalSection cs;
  12767. size32_t sizeNumParallel;
  12768. rtlDataAttr listNumParallel;
  12769. unsigned defaultNumParallel;
  12770. LoopExecutorThread executor;
  12771. IProbeManager* probeManager;
  12772. CriticalSection canAccess;
  12773. CriticalSection scrit;
  12774. InterruptableSemaphore readySpace;
  12775. InterruptableSemaphore recordsReady;
  12776. protected:
  12777. bool includeInLoop(unsigned counter, const void * row)
  12778. {
  12779. CriticalBlock b(helperCS);
  12780. return helper.sendToLoop(counter, row);
  12781. }
  12782. public:
  12783. CRoxieServerParallelLoopActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _loopGraphId, IOutputMetaData * _counterMeta)
  12784. : CRoxieServerLoopActivity(_factory, _probeManager, _loopGraphId, _counterMeta),
  12785. readySpace(parallelLoopFlowLimit)
  12786. {
  12787. probeManager = _probeManager;
  12788. defaultNumParallel = 0;
  12789. sizeNumParallel = 0;
  12790. }
  12791. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  12792. {
  12793. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  12794. executor.onCreate(_ctx);
  12795. }
  12796. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12797. {
  12798. CriticalBlock b(scrit); // can stop while still starting, if unlucky...
  12799. readySpace.reinit(parallelLoopFlowLimit);
  12800. recordsReady.reinit();
  12801. CRoxieServerLoopActivity::start(parentExtractSize, parentExtract, paused);
  12802. defaultNumParallel = helper.defaultParallelIterations();
  12803. if (!defaultNumParallel)
  12804. defaultNumParallel = DEFAULT_PARALLEL_LOOP_THREADS;
  12805. helper.numParallelIterations(sizeNumParallel, listNumParallel.refdata());
  12806. //MORE: If numIterations <= number of parallel iterations[1],
  12807. //then we don't need to create a separate thread to do the processing, and the results will also avoid
  12808. //being transferred via a queue
  12809. executor.start(parentExtractSize, parentExtract, paused);
  12810. }
  12811. virtual void setInput(unsigned idx, IRoxieInput *_in)
  12812. {
  12813. if (idx)
  12814. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
  12815. executor.setInput(this, _in, flags);
  12816. }
  12817. virtual void stop(bool aborting)
  12818. {
  12819. CriticalBlock b(scrit); // can stop while still starting, if unlucky...
  12820. readySpace.interrupt();
  12821. recordsReady.interrupt();
  12822. executor.join(); // MORE - may not be needed given stop/reset split
  12823. CRoxieServerLoopActivity::stop(aborting);
  12824. }
  12825. virtual void reset()
  12826. {
  12827. while (ready.ordinality())
  12828. ReleaseRoxieRow(ready.dequeue());
  12829. executor.reset();
  12830. CRoxieServerActivity::reset();
  12831. }
  12832. virtual const void * nextInGroup()
  12833. {
  12834. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  12835. loop
  12836. {
  12837. if (eof)
  12838. return NULL;
  12839. recordsReady.wait();
  12840. CriticalBlock procedure(canAccess);
  12841. if (ready.ordinality())
  12842. {
  12843. const void *result = ready.dequeue();
  12844. readySpace.signal();
  12845. if (result)
  12846. processed++;
  12847. return result;
  12848. }
  12849. else
  12850. eof = true;
  12851. }
  12852. }
  12853. unsigned getNumParallel(unsigned iter)
  12854. {
  12855. if (iter * sizeof(unsigned) >= sizeNumParallel)
  12856. return defaultNumParallel;
  12857. return ((unsigned *)listNumParallel.getdata())[iter];
  12858. }
  12859. inline void enqueueResult(const void * row)
  12860. {
  12861. try
  12862. {
  12863. while(!readySpace.wait(1000))
  12864. {
  12865. CTXLOG("Blocked waiting for space in loop %p activity id: %d output queue: %d records in queue", this, queryId(), ready.ordinality());
  12866. }
  12867. }
  12868. catch (...)
  12869. {
  12870. ReleaseRoxieRow(row);
  12871. throw;
  12872. }
  12873. CriticalBlock b2(canAccess);
  12874. ready.enqueue(row);
  12875. recordsReady.signal();
  12876. }
  12877. inline void finishResults()
  12878. {
  12879. recordsReady.signal();
  12880. }
  12881. virtual bool fireException(IException *e)
  12882. {
  12883. readySpace.interrupt(LINK(e));
  12884. recordsReady.interrupt(e);
  12885. return true;
  12886. }
  12887. IActivityGraph * createChildGraphInstance()
  12888. {
  12889. return factory->createChildGraph(ctx, &helper, loopGraphId, this, probeManager, *this);
  12890. }
  12891. IActivityGraph * queryChildGraph()
  12892. {
  12893. return ctx->queryChildGraph(loopGraphId);
  12894. }
  12895. };
  12896. //=================================================================================
  12897. const void * LoopFilterPseudoInput::nextInGroup()
  12898. {
  12899. loop
  12900. {
  12901. const void * next = input->nextInGroup();
  12902. if (!next || activity->includeInLoop(counter, next))
  12903. return next;
  12904. activity->enqueueResult(next);
  12905. }
  12906. }
  12907. void LoopExecutorThread::onCreate(IRoxieSlaveContext * _ctx)
  12908. {
  12909. //Initialise the cached graph list with the child instance that will always be created. Other iterations will be created on demand.
  12910. ctx = _ctx;
  12911. cachedGraphs.append(*LINK(activity->queryChildGraph()));
  12912. }
  12913. void LoopExecutorThread::start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12914. {
  12915. savedParentExtractSize = parentExtractSize;
  12916. savedParentExtract = parentExtract;
  12917. eof = false;
  12918. StringBuffer logPrefix("[");
  12919. ctx->getLogPrefix(logPrefix).append("] ");
  12920. RestartableThread::start(logPrefix);
  12921. }
  12922. int LoopExecutorThread::run()
  12923. {
  12924. try
  12925. {
  12926. executeLoop();
  12927. }
  12928. catch (IException *e)
  12929. {
  12930. activity->fireException(e);
  12931. }
  12932. catch (...)
  12933. {
  12934. activity->fireException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unexpected exception caught in LoopExecutorThread::run"));
  12935. }
  12936. return 0;
  12937. }
  12938. void LoopExecutorThread::stop(bool aborting)
  12939. {
  12940. safeInput->stop(aborting);
  12941. RestartableThread::join();
  12942. }
  12943. void LoopExecutorThread::reset()
  12944. {
  12945. safeInput->reset();
  12946. }
  12947. void LoopExecutorThread::executeLoop()
  12948. {
  12949. unsigned iterations = 0;
  12950. unsigned counter = 0;
  12951. unsigned outputIndex = 0;
  12952. //Note, activities don't link inputs, so need to be careful that special inputs remain linked while the activity is executing.
  12953. loop
  12954. {
  12955. if (activity->activityKind == TAKloopcount)
  12956. {
  12957. if (counter == activity->maxIterations)
  12958. break;
  12959. }
  12960. else
  12961. {
  12962. //This condition isn't quite right because it needs to be whether the filtered
  12963. //input is empty. May be ok if we include that in the semantics,
  12964. if (tempResults[1-outputIndex].ordinality() == 0)
  12965. break;
  12966. }
  12967. unsigned numParallel = activity->getNumParallel(iterations);
  12968. Linked<IRoxieInput> curInput;
  12969. if (iterations == 0)
  12970. curInput.set(safeInput);
  12971. else
  12972. {
  12973. SafeRowQueue & inputQueue = tempResults[1-outputIndex];
  12974. inputQueue.enqueue(NULL);
  12975. curInput.setown(new CRowQueuePseudoInput(inputQueue));
  12976. }
  12977. SafeRowQueue * curOutput = NULL;
  12978. if (counter+numParallel > activity->maxIterations)
  12979. numParallel = activity->maxIterations - counter;
  12980. else if (counter+numParallel < activity->maxIterations)
  12981. curOutput = &tempResults[outputIndex];
  12982. executeLoopInstance(counter, numParallel, curInput, curOutput);
  12983. outputIndex = 1-outputIndex;
  12984. counter += numParallel;
  12985. iterations++;
  12986. }
  12987. //Check for TAKlooprow, where end of loop couldn't be determined ahead of time
  12988. SafeRowQueue & inputQueue = tempResults[1-outputIndex];
  12989. while (inputQueue.ordinality())
  12990. {
  12991. const void * next = inputQueue.dequeue();
  12992. activity->enqueueResult(next);
  12993. }
  12994. activity->finishResults();
  12995. }
  12996. void LoopExecutorThread::executeLoopInstance(unsigned counter, unsigned numIterations, IRoxieInput * input, SafeRowQueue * spillOutput)
  12997. {
  12998. IArrayOf<IRoxieInput> savedInputs; // activities don't link their inputs, so this list keeps filters alive.
  12999. Linked<IRoxieInput> curInput = input;
  13000. unsigned i;
  13001. for (i= 0; i != numIterations; i++)
  13002. {
  13003. unsigned thisCounter = counter+i+1;
  13004. IRoxieInput * filtered = curInput;
  13005. if (flags & IHThorLoopArg::LFfiltered)
  13006. {
  13007. filtered = new LoopFilterPseudoInput(activity, curInput, thisCounter);
  13008. savedInputs.append(*filtered);
  13009. }
  13010. //graph is kept, so new curInput will be guaranteed to exist
  13011. curInput.setown(createLoopIterationGraph(i, filtered, thisCounter));
  13012. }
  13013. try
  13014. {
  13015. curInput->start(savedParentExtractSize, savedParentExtract, false);
  13016. if (spillOutput)
  13017. {
  13018. loop
  13019. {
  13020. const void * next = curInput->nextInGroup();
  13021. if (!next)
  13022. break;
  13023. spillOutput->enqueue(next);
  13024. }
  13025. }
  13026. else
  13027. {
  13028. loop
  13029. {
  13030. const void * next = curInput->nextInGroup();
  13031. if (!next)
  13032. break;
  13033. activity->enqueueResult(next);
  13034. }
  13035. }
  13036. }
  13037. catch (IException *E)
  13038. {
  13039. ctx->notifyAbort(E);
  13040. for (i= 0; i != numIterations; i++)
  13041. {
  13042. cachedGraphs.item(i).queryLoopGraph()->afterExecute();
  13043. }
  13044. curInput->stop(true);
  13045. curInput->reset();
  13046. throw;
  13047. }
  13048. for (i= 0; i != numIterations; i++)
  13049. {
  13050. cachedGraphs.item(i).queryLoopGraph()->afterExecute();
  13051. }
  13052. curInput->stop(false);
  13053. curInput->reset();
  13054. }
  13055. IRoxieInput * LoopExecutorThread::createLoopIterationGraph(unsigned i, IRoxieInput * input, unsigned counter)
  13056. {
  13057. if (!cachedGraphs.isItem(i))
  13058. cachedGraphs.append(*activity->createChildGraphInstance());
  13059. Linked<IRoxieServerChildGraph> loopGraph = cachedGraphs.item(i).queryLoopGraph();
  13060. loopGraph->beforeExecute();
  13061. if (!loopGraph->querySetInputResult(1, input))
  13062. throwUnexpected(); // a loop which doesn't use the value from the previous iteration. Should probably handle even if daft.
  13063. activity->createCounterResult(loopGraph, counter);
  13064. return loopGraph->selectOutput(0);
  13065. }
  13066. //=================================================================================
  13067. class CCounterRowMetaData : public CInterface, implements IOutputMetaData
  13068. {
  13069. public:
  13070. IMPLEMENT_IINTERFACE
  13071. virtual size32_t getRecordSize(const void *) { return sizeof(thor_loop_counter_t); }
  13072. virtual size32_t getMinRecordSize() const { return sizeof(thor_loop_counter_t); }
  13073. virtual size32_t getFixedSize() const { return sizeof(thor_loop_counter_t); }
  13074. virtual void toXML(const byte * self, IXmlWriter & out) { }
  13075. virtual unsigned getVersion() const { return OUTPUTMETADATA_VERSION; }
  13076. virtual unsigned getMetaFlags() { return 0; }
  13077. virtual void destruct(byte * self) {}
  13078. virtual IOutputRowSerializer * createDiskSerializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  13079. virtual IOutputRowDeserializer * createDiskDeserializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  13080. virtual ISourceRowPrefetcher * createDiskPrefetcher(ICodeContext * ctx, unsigned activityId) { return NULL; }
  13081. virtual IOutputMetaData * querySerializedDiskMeta() { return this; }
  13082. virtual IOutputRowSerializer * createInternalSerializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  13083. virtual IOutputRowDeserializer * createInternalDeserializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  13084. virtual void walkIndirectMembers(const byte * self, IIndirectMemberVisitor & visitor) {}
  13085. virtual IOutputMetaData * queryChildMeta(unsigned i) { return NULL; }
  13086. };
  13087. class CRoxieServerLoopActivityFactory : public CRoxieServerActivityFactory
  13088. {
  13089. unsigned loopGraphId;
  13090. unsigned flags;
  13091. Linked<IOutputMetaData> counterMeta;
  13092. public:
  13093. CRoxieServerLoopActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _loopGraphId)
  13094. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), loopGraphId(_loopGraphId)
  13095. {
  13096. Owned<IHThorLoopArg> helper = (IHThorLoopArg *) helperFactory();
  13097. flags = helper->getFlags();
  13098. counterMeta.setown(new CCounterRowMetaData);
  13099. }
  13100. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  13101. {
  13102. if (flags & IHThorLoopArg::LFparallel)
  13103. return new CRoxieServerParallelLoopActivity(this, _probeManager, loopGraphId, counterMeta);
  13104. else
  13105. return new CRoxieServerSequentialLoopActivity(this, _probeManager, loopGraphId, counterMeta);
  13106. }
  13107. };
  13108. IRoxieServerActivityFactory *createRoxieServerLoopActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _loopGraphId)
  13109. {
  13110. return new CRoxieServerLoopActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _loopGraphId);
  13111. }
  13112. //=================================================================================
  13113. class CRoxieServerGraphLoopActivity : public CRoxieServerActivity
  13114. {
  13115. protected:
  13116. IHThorGraphLoopArg &helper;
  13117. unsigned maxIterations;
  13118. unsigned flags;
  13119. rtlRowBuilder GraphExtractBuilder;
  13120. unsigned loopGraphId;
  13121. Linked<IOutputMetaData> counterMeta;
  13122. public:
  13123. CRoxieServerGraphLoopActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _GraphGraphId, IOutputMetaData * _counterMeta)
  13124. : CRoxieServerActivity(_factory, _probeManager),
  13125. helper((IHThorGraphLoopArg &)basehelper), loopGraphId(_GraphGraphId), counterMeta(_counterMeta)
  13126. {
  13127. flags = helper.getFlags();
  13128. maxIterations = 0;
  13129. }
  13130. virtual bool needsAllocator() const { return true; }
  13131. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13132. {
  13133. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  13134. int iterations = (int) helper.numIterations();
  13135. maxIterations = (iterations >= 0) ? iterations : 0;
  13136. if (maxIterations > maxGraphLoopIterations)
  13137. throw MakeStringException(ROXIE_TOO_MANY_GRAPH_LOOP, "Attempt to execute graph %u times", maxIterations);
  13138. if (maxIterations != 0)
  13139. {
  13140. GraphExtractBuilder.clear();
  13141. helper.createParentExtract(GraphExtractBuilder);
  13142. }
  13143. }
  13144. virtual void stop(bool aborting)
  13145. {
  13146. CRoxieServerActivity::stop(aborting);
  13147. GraphExtractBuilder.clear();
  13148. }
  13149. void createCounterResult(IRoxieServerChildGraph * graph, unsigned counter)
  13150. {
  13151. if (flags & IHThorGraphLoopArg::GLFcounter)
  13152. {
  13153. void * counterRow = ctx->queryRowManager().allocate(sizeof(thor_loop_counter_t), activityId);
  13154. *((thor_loop_counter_t *)counterRow) = counter;
  13155. RtlLinkedDatasetBuilder builder(rowAllocator);
  13156. builder.appendOwn(counterRow);
  13157. Owned<CGraphResult> counterResult = new CGraphResult(builder.getcount(), builder.linkrows());
  13158. graph->setInputResult(0, counterResult);
  13159. }
  13160. }
  13161. };
  13162. //=================================================================================
  13163. class CRoxieServerSequentialGraphLoopActivity : public CRoxieServerGraphLoopActivity
  13164. {
  13165. Owned<IActivityGraph> GraphQuery;
  13166. Owned<IRoxieServerChildGraph> loopGraph;
  13167. Linked<IRoxieInput> resultInput;
  13168. bool evaluated;
  13169. public:
  13170. CRoxieServerSequentialGraphLoopActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _GraphGraphId, IOutputMetaData * _counterMeta)
  13171. : CRoxieServerGraphLoopActivity(_factory, _probeManager, _GraphGraphId, _counterMeta)
  13172. {
  13173. evaluated = false;
  13174. }
  13175. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  13176. {
  13177. CRoxieServerGraphLoopActivity::onCreate(_ctx, _colocalParent);
  13178. GraphQuery.set(_ctx->queryChildGraph(loopGraphId));
  13179. }
  13180. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13181. {
  13182. CRoxieServerGraphLoopActivity::start(parentExtractSize, parentExtract, paused);
  13183. //MORE: Not sure about this, should IRoxieServerChildGraph be combined with IActivityGraph?
  13184. loopGraph.set(GraphQuery->queryLoopGraph());
  13185. evaluated = false;
  13186. }
  13187. virtual void stop(bool aborting)
  13188. {
  13189. if (loopGraph)
  13190. loopGraph->clearGraphLoopResults();
  13191. CRoxieServerGraphLoopActivity::stop(aborting);
  13192. }
  13193. virtual const void * nextInGroup()
  13194. {
  13195. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  13196. if (!evaluated)
  13197. {
  13198. executeEntireGraph();
  13199. evaluated = true;
  13200. }
  13201. const void * ret = resultInput->nextInGroup();
  13202. if (ret)
  13203. processed++;
  13204. return ret;
  13205. }
  13206. void executeIteration(unsigned parentExtractSize, const byte *parentExtract, unsigned counter)
  13207. {
  13208. try
  13209. {
  13210. loopGraph->beforeExecute();
  13211. createCounterResult(loopGraph, counter);
  13212. loopGraph->executeGraphLoop(parentExtractSize, parentExtract);
  13213. loopGraph->afterExecute();
  13214. }
  13215. catch (...)
  13216. {
  13217. CTXLOG("Exception thrown in graph body - cleaning up");
  13218. loopGraph->afterExecute();
  13219. throw;
  13220. }
  13221. }
  13222. void createInitialGraphInput()
  13223. {
  13224. loopGraph->clearGraphLoopResults();
  13225. RtlLinkedDatasetBuilder builder(rowAllocator);
  13226. input->readAll(builder);
  13227. Owned<CGraphResult> result = new CGraphResult(builder.getcount(), builder.linkrows());
  13228. loopGraph->setGraphLoopResult(0, result);
  13229. }
  13230. void executeEntireGraph()
  13231. {
  13232. createInitialGraphInput();
  13233. for (unsigned loopCounter=1; loopCounter <= maxIterations; loopCounter++)
  13234. {
  13235. executeIteration(GraphExtractBuilder.size(), GraphExtractBuilder.getbytes(), loopCounter);
  13236. }
  13237. resultInput.setown(loopGraph->getGraphLoopResult(maxIterations));
  13238. }
  13239. };
  13240. //=================================================================================
  13241. struct GraphOutputSplitterArg : public ccdserver_hqlhelper::CThorSplitArg
  13242. {
  13243. public:
  13244. virtual unsigned numBranches()
  13245. {
  13246. return 0;
  13247. }
  13248. virtual IOutputMetaData * queryOutputMeta()
  13249. {
  13250. return NULL;// get it from the parent..
  13251. }
  13252. };
  13253. extern "C" IHThorArg * createGraphOutputSplitter() { return new GraphOutputSplitterArg; }
  13254. class CGraphIterationInfo : public CInterface
  13255. {
  13256. private:
  13257. Owned<IRoxieServerActivityFactory> factory; // Note - before sourceAct, so destroyed last
  13258. unsigned sourceIdx;
  13259. Linked<IRoxieServerActivity> sourceAct;
  13260. Linked<IRoxieInput> sourceInput;
  13261. unsigned numUses;
  13262. unsigned iteration;
  13263. public:
  13264. CGraphIterationInfo(IRoxieServerActivity * _sourceAct, IRoxieInput *_input, unsigned _sourceIdx, unsigned _iteration)
  13265. : sourceAct(_sourceAct), sourceInput(_input), sourceIdx(_sourceIdx), iteration(_iteration)
  13266. {
  13267. numUses = 0;
  13268. }
  13269. inline void noteUsed()
  13270. {
  13271. numUses++;
  13272. }
  13273. void createSplitter(IRoxieSlaveContext *ctx, IProbeManager *probeManager)
  13274. {
  13275. if (numUses > 1)
  13276. {
  13277. factory.setown(createRoxieServerThroughSpillActivityFactory(sourceAct->queryFactory()->queryQueryFactory(), createGraphOutputSplitter, numUses));
  13278. IRoxieServerActivity *splitter = factory->createActivity(NULL);
  13279. splitter->onCreate(ctx, NULL);
  13280. IRoxieInput *input = sourceAct->queryOutput(sourceIdx);
  13281. if (probeManager)
  13282. {
  13283. IInputBase * inputBase = probeManager->createProbe(static_cast<IInputBase*>(input), sourceAct, splitter, sourceIdx, 0, iteration);
  13284. input = static_cast<IRoxieInput*>(inputBase);
  13285. // MORE - shouldn't this be added to probes?
  13286. }
  13287. sourceAct.setown(splitter);
  13288. sourceAct->setInput(0, input);
  13289. sourceIdx = 0;
  13290. sourceInput.clear();
  13291. }
  13292. }
  13293. IRoxieInput *connectOutput(IProbeManager *probeManager, IArrayOf<IRoxieInput> &probes, IRoxieServerActivity *targetAct, unsigned targetIdx)
  13294. {
  13295. // MORE - not really necessary to create splitters in separate pass, is it?
  13296. if (factory) // we created a splitter....
  13297. sourceInput.set(sourceAct->queryOutput(sourceIdx));
  13298. IRoxieInput *ret = sourceInput;
  13299. if (probeManager)
  13300. {
  13301. IInputBase *inputBase = probeManager->createProbe(ret, sourceAct, targetAct, sourceIdx, targetIdx, iteration);
  13302. ret = static_cast<IRoxieInput *>(inputBase);
  13303. probes.append(*LINK(ret));
  13304. }
  13305. if (factory) // we created a splitter....
  13306. sourceIdx++;
  13307. return ret;
  13308. }
  13309. };
  13310. class CRoxieServerParallelGraphLoopActivity : public CRoxieServerGraphLoopActivity, implements IRoxieServerLoopResultProcessor
  13311. {
  13312. Owned<IActivityGraph> childGraph;
  13313. IRoxieInput * resultInput;
  13314. CIArrayOf<CGraphIterationInfo> outputs;
  13315. IArrayOf<IRoxieServerChildGraph> iterationGraphs;
  13316. Owned<CExtractMapperInput> inputExtractMapper;
  13317. IProbeManager *probeManager;
  13318. unsigned createLoopCounter;
  13319. IArrayOf<IRoxieInput> probes;
  13320. public:
  13321. CRoxieServerParallelGraphLoopActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _GraphGraphId, IOutputMetaData * _counterMeta)
  13322. : CRoxieServerGraphLoopActivity(_factory, _probeManager, _GraphGraphId, _counterMeta), probeManager(_probeManager)
  13323. {
  13324. inputExtractMapper.setown(new CExtractMapperInput);
  13325. resultInput = NULL;
  13326. createLoopCounter = 0;
  13327. }
  13328. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  13329. {
  13330. CRoxieServerGraphLoopActivity::onCreate(_ctx, _colocalParent);
  13331. childGraph.set(_ctx->queryChildGraph(loopGraphId));
  13332. }
  13333. virtual void setInput(unsigned idx, IRoxieInput *_in)
  13334. {
  13335. //Input needs to be handled very carefully.....
  13336. //We don't want to call onStart on the input unless it is actually used, so don't use the base CRoxieServerActivity implementation.
  13337. //This activity's input needs to be started with (parentExtractSize, parentExtract), but the elements in the graph need to be started with the
  13338. //GraphExtractBuilder parent extract. So we need to wrap the input in a pseudo-input (inputExtractMapper) that passes through a different
  13339. //parentExtract. Something very similar will be needed for query library calls with streaming inputs when they are implemented.
  13340. assertex(idx == 0);
  13341. inputExtractMapper->setInput(_in);
  13342. }
  13343. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13344. {
  13345. CRoxieServerGraphLoopActivity::start(parentExtractSize, parentExtract, paused); // initialises GraphExtractBuilder
  13346. inputExtractMapper->setParentExtract(parentExtractSize, parentExtract);
  13347. createExpandedGraph(GraphExtractBuilder.size(), GraphExtractBuilder.getbytes(), probeManager);
  13348. resultInput->start(GraphExtractBuilder.size(), GraphExtractBuilder.getbytes(), paused);
  13349. }
  13350. virtual void stop(bool aborting)
  13351. {
  13352. if (resultInput)
  13353. resultInput->stop(aborting);
  13354. CRoxieServerGraphLoopActivity::stop(aborting);
  13355. }
  13356. virtual void reset()
  13357. {
  13358. if (resultInput)
  13359. resultInput->reset();
  13360. resultInput = NULL;
  13361. iterationGraphs.kill();
  13362. outputs.kill();
  13363. if (probeManager)
  13364. {
  13365. probeManager->deleteGraph(NULL, (IArrayOf<IInputBase>*)&probes);
  13366. probes.kill();
  13367. }
  13368. CRoxieServerGraphLoopActivity::reset();
  13369. }
  13370. virtual const void * nextInGroup()
  13371. {
  13372. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  13373. const void * ret = resultInput->nextInGroup();
  13374. if (ret)
  13375. processed++;
  13376. return ret;
  13377. }
  13378. void createExpandedGraph(unsigned parentExtractSize, const byte *parentExtract, IProbeManager *probeManager)
  13379. {
  13380. //result(0) is the input to the graph.
  13381. resultInput = inputExtractMapper;
  13382. outputs.append(* new CGraphIterationInfo(resultInput->queryActivity(), resultInput, 0, 1));
  13383. for (createLoopCounter=1; createLoopCounter <= maxIterations; createLoopCounter++)
  13384. {
  13385. IRoxieServerChildGraph * graph = childGraph->createGraphLoopInstance(createLoopCounter, parentExtractSize, parentExtract, *this);
  13386. graph->beforeExecute();
  13387. iterationGraphs.append(*graph);
  13388. graph->gatherIterationUsage(*this);
  13389. CGraphIterationInfo *iteration = graph->selectGraphLoopOutput();
  13390. outputs.append(*iteration);
  13391. }
  13392. createLoopCounter = 0;
  13393. createSplitters(probeManager);
  13394. ForEachItemIn(i2, iterationGraphs)
  13395. iterationGraphs.item(i2).associateIterationOutputs(*this);
  13396. resultInput = outputs.tos().connectOutput(probeManager, probes, this, 0);
  13397. }
  13398. void createSplitters(IProbeManager *probeManager)
  13399. {
  13400. ForEachItemIn(i, outputs)
  13401. {
  13402. CGraphIterationInfo & next = outputs.item(i);
  13403. next.createSplitter(ctx, probeManager);
  13404. }
  13405. }
  13406. //IRoxieServerLoopResultProcessor
  13407. virtual void noteUseIteration(unsigned _whichIteration)
  13408. {
  13409. int whichIteration = (int) _whichIteration; // May go negative - API is unsigned for historical reasons
  13410. if (whichIteration >= 0)
  13411. {
  13412. if (!outputs.isItem(whichIteration))
  13413. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Error reading graph result %d from iteration %d", whichIteration, createLoopCounter);
  13414. outputs.item(whichIteration).noteUsed();
  13415. }
  13416. }
  13417. virtual IRoxieInput * connectIterationOutput(unsigned whichIteration, IProbeManager *probeManager, IArrayOf<IRoxieInput> &probes, IRoxieServerActivity *targetAct, unsigned targetIdx)
  13418. {
  13419. if (outputs.isItem(whichIteration))
  13420. {
  13421. CGraphIterationInfo & next = outputs.item(whichIteration);
  13422. return next.connectOutput(probeManager, probes, targetAct, targetIdx);
  13423. }
  13424. return NULL;
  13425. }
  13426. };
  13427. //=================================================================================
  13428. class CRoxieServerGraphLoopActivityFactory : public CRoxieServerActivityFactory
  13429. {
  13430. unsigned loopGraphId;
  13431. unsigned flags;
  13432. Linked<IOutputMetaData> counterMeta;
  13433. public:
  13434. CRoxieServerGraphLoopActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _loopGraphId)
  13435. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), loopGraphId(_loopGraphId)
  13436. {
  13437. Owned<IHThorGraphLoopArg> helper = (IHThorGraphLoopArg *) helperFactory();
  13438. flags = helper->getFlags();
  13439. counterMeta.setown(new CCounterRowMetaData);
  13440. }
  13441. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  13442. {
  13443. if (kind == TAKparallelgraphloop)
  13444. return new CRoxieServerParallelGraphLoopActivity(this, _probeManager, loopGraphId, counterMeta);
  13445. else
  13446. return new CRoxieServerSequentialGraphLoopActivity(this, _probeManager, loopGraphId, counterMeta);
  13447. }
  13448. };
  13449. IRoxieServerActivityFactory *createRoxieServerGraphLoopActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _loopGraphId)
  13450. {
  13451. return new CRoxieServerGraphLoopActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _loopGraphId);
  13452. }
  13453. //=====================================================================================================
  13454. class CRoxieServerLibraryCallActivity : public CRoxieServerActivity
  13455. {
  13456. class OutputAdaptor : public CExtractMapperInput
  13457. {
  13458. bool stopped;
  13459. public:
  13460. CRoxieServerLibraryCallActivity *parent;
  13461. unsigned oid;
  13462. unsigned processed;
  13463. public:
  13464. IMPLEMENT_IINTERFACE;
  13465. OutputAdaptor() : CExtractMapperInput(NULL)
  13466. {
  13467. parent = NULL;
  13468. oid = 0;
  13469. init();
  13470. }
  13471. void init()
  13472. {
  13473. processed = 0;
  13474. stopped = false;
  13475. }
  13476. virtual unsigned queryId() const
  13477. {
  13478. return parent->queryId();
  13479. }
  13480. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13481. {
  13482. parent->start(oid, parentExtractSize, parentExtract, paused);
  13483. CExtractMapperInput::start(parentExtractSize, parentExtract, paused);
  13484. }
  13485. virtual void stop(bool aborting)
  13486. {
  13487. if (!stopped)
  13488. {
  13489. stopped = true;
  13490. parent->stop(oid, aborting); // parent code relies on stop being called exactly once per adaptor, so make sure it is!
  13491. CExtractMapperInput::stop(aborting);
  13492. }
  13493. };
  13494. virtual void reset()
  13495. {
  13496. parent->reset(oid, processed);
  13497. CExtractMapperInput::reset();
  13498. init();
  13499. };
  13500. virtual void checkAbort()
  13501. {
  13502. parent->checkAbort();
  13503. }
  13504. };
  13505. IHThorLibraryCallArg &helper;
  13506. unsigned numInputs;
  13507. unsigned numOutputs;
  13508. unsigned numActiveOutputs;
  13509. bool started;
  13510. OutputAdaptor* outputAdaptors;
  13511. CExtractMapperInput * * inputAdaptors;
  13512. bool * inputUsed;
  13513. bool * outputUsed;
  13514. Owned<IException> error;
  13515. CriticalSection crit;
  13516. rtlRowBuilder libraryExtractBuilder;
  13517. Owned<IActivityGraph> libraryGraph;
  13518. const LibraryCallFactoryExtra & extra;
  13519. public:
  13520. CRoxieServerLibraryCallActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs, unsigned _numOutputs, const LibraryCallFactoryExtra & _extra)
  13521. : CRoxieServerActivity(_factory, _probeManager),
  13522. helper((IHThorLibraryCallArg &)basehelper), extra(_extra)
  13523. {
  13524. numInputs = _numInputs;
  13525. numOutputs = _numOutputs;
  13526. numActiveOutputs = numOutputs;
  13527. inputAdaptors = new CExtractMapperInput*[numInputs];
  13528. inputUsed = new bool[numInputs];
  13529. for (unsigned i1 = 0; i1 < numInputs; i1++)
  13530. {
  13531. inputAdaptors[i1] = new CExtractMapperInput;
  13532. inputUsed[i1] = false;
  13533. }
  13534. outputAdaptors = new OutputAdaptor[numOutputs];
  13535. outputUsed = new bool[numOutputs];
  13536. for (unsigned i2 = 0; i2 < numOutputs; i2++)
  13537. {
  13538. outputAdaptors[i2].parent = this;
  13539. outputAdaptors[i2].oid = i2;
  13540. outputUsed[i2] = false;
  13541. }
  13542. started = false;
  13543. }
  13544. ~CRoxieServerLibraryCallActivity()
  13545. {
  13546. for (unsigned i1 = 0; i1 < numInputs; i1++)
  13547. ::Release(inputAdaptors[i1]);
  13548. delete [] inputAdaptors;
  13549. delete [] inputUsed;
  13550. delete [] outputAdaptors;
  13551. delete [] outputUsed;
  13552. }
  13553. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  13554. {
  13555. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  13556. libraryGraph.setown(_ctx->getLibraryGraph(extra, this));
  13557. libraryGraph->onCreate(_ctx, _colocalParent);
  13558. //Now map the inputs and outputs to the adapters
  13559. IRoxieServerChildGraph * graph = libraryGraph->queryLoopGraph();
  13560. for (unsigned i1=0; i1<numInputs; i1++)
  13561. inputUsed[i1] = graph->querySetInputResult(i1, inputAdaptors[i1]);
  13562. for (unsigned i2=0; i2<numOutputs; i2++)
  13563. {
  13564. unsigned outputIndex = extra.outputs.item(i2);
  13565. Owned<IRoxieInput> output = graph->selectOutput(numInputs+outputIndex);
  13566. outputAdaptors[i2].setInput(output);
  13567. }
  13568. }
  13569. virtual void start(unsigned oid, unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13570. {
  13571. CriticalBlock b(crit);
  13572. if (error)
  13573. throw error.getLink();
  13574. if (factory)
  13575. factory->noteStarted(oid);
  13576. if (!started)
  13577. {
  13578. // even though it is not complete, we don't want to run this again if it fails.
  13579. started = true;
  13580. //see notes on splitter above
  13581. try
  13582. {
  13583. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  13584. }
  13585. catch (IException *E)
  13586. {
  13587. #ifdef TRACE_SPLIT
  13588. CTXLOG("spill %d caught exception in start", activityId);
  13589. #endif
  13590. error.set(E);
  13591. throw;
  13592. }
  13593. catch (...)
  13594. {
  13595. IException *E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught in CRoxieServerLibraryCallActivity::start");
  13596. error.set(E);
  13597. throw E;
  13598. }
  13599. //recreate the parent extract, and use it to reinitialize the graphs...
  13600. libraryExtractBuilder.clear();
  13601. helper.createParentExtract(libraryExtractBuilder);
  13602. // NOTE - do NOT set activeOutputs = numOutputs here - we must rely on the value set in reset and constructor. This is because we can see stop on
  13603. // some inputs before we see start on others.
  13604. for (unsigned i1 = 0; i1 < numInputs; i1++)
  13605. {
  13606. if (inputUsed[i1])
  13607. inputAdaptors[i1]->setParentExtract(parentExtractSize, parentExtract);
  13608. else
  13609. inputAdaptors[i1]->stop(false);
  13610. }
  13611. for (unsigned i2 = 0; i2 < numOutputs; i2++)
  13612. outputAdaptors[i2].setParentExtract(libraryExtractBuilder.size(), libraryExtractBuilder.getbytes());
  13613. //call stop on all the unused inputs.
  13614. IRoxieServerChildGraph * graph = libraryGraph->queryLoopGraph();
  13615. graph->beforeExecute();
  13616. ForEachItemIn(i3, extra.unusedOutputs)
  13617. {
  13618. Owned<IRoxieInput> output = graph->selectOutput(numInputs+extra.unusedOutputs.item(i3));
  13619. output->stop(false);
  13620. }
  13621. }
  13622. }
  13623. virtual void stop(unsigned oid, bool aborting)
  13624. {
  13625. CriticalBlock b(crit);
  13626. if (--numActiveOutputs == 0)
  13627. {
  13628. //call stop on all the unused inputs.
  13629. IRoxieServerChildGraph * graph = libraryGraph->queryLoopGraph();
  13630. graph->beforeExecute();
  13631. ForEachItemIn(i3, extra.unusedOutputs)
  13632. {
  13633. Owned<IRoxieInput> output = graph->selectOutput(numInputs+extra.unusedOutputs.item(i3));
  13634. output->stop(false);
  13635. }
  13636. CRoxieServerActivity::stop(aborting);
  13637. }
  13638. }
  13639. void reset(unsigned oid, unsigned _processed)
  13640. {
  13641. noteProcessed(oid, _processed, 0, 0);
  13642. started = false;
  13643. error.clear();
  13644. numActiveOutputs = numOutputs;
  13645. if (state != STATEreset) // make sure input is only reset once
  13646. {
  13647. CRoxieServerActivity::reset();
  13648. libraryGraph->reset();
  13649. //Call reset on all unused outputs from the graph - no one else will.
  13650. IRoxieServerChildGraph * graph = libraryGraph->queryLoopGraph();
  13651. ForEachItemIn(i3, extra.unusedOutputs)
  13652. {
  13653. Owned<IRoxieInput> output = graph->selectOutput(numInputs+extra.unusedOutputs.item(i3));
  13654. output->reset();
  13655. }
  13656. }
  13657. };
  13658. virtual void setInput(unsigned idx, IRoxieInput *_in)
  13659. {
  13660. inputAdaptors[idx]->setInput(_in);
  13661. }
  13662. public:
  13663. virtual const void *nextInGroup()
  13664. {
  13665. throwUnexpected(); // Internal logic error - we are not anybody's input
  13666. }
  13667. virtual IOutputMetaData * queryOutputMeta() const
  13668. {
  13669. throwUnexpected(); // should be called on outputs instead
  13670. }
  13671. virtual IRoxieInput *queryOutput(unsigned idx)
  13672. {
  13673. assertex(idx!=(unsigned)-1);
  13674. assertex(!outputUsed[idx]);
  13675. outputUsed[idx] = true;
  13676. return &outputAdaptors[idx];
  13677. }
  13678. };
  13679. void LibraryCallFactoryExtra::set(const LibraryCallFactoryExtra & _other)
  13680. {
  13681. ForEachItemIn(i1, _other.outputs)
  13682. outputs.append(_other.outputs.item(i1));
  13683. ForEachItemIn(i2, _other.unusedOutputs)
  13684. unusedOutputs.append(_other.unusedOutputs.item(i2));
  13685. maxOutputs = _other.maxOutputs;
  13686. graphid = _other.graphid;
  13687. libraryName.set(_other.libraryName);
  13688. interfaceHash = _other.interfaceHash;
  13689. embedded = _other.embedded;
  13690. }
  13691. void LibraryCallFactoryExtra::calcUnused()
  13692. {
  13693. for (unsigned i=0; i < maxOutputs; i++)
  13694. if (!outputs.contains(i))
  13695. unusedOutputs.append(i);
  13696. }
  13697. class CRoxieServerLibraryCallActivityFactory : public CRoxieServerMultiOutputFactory
  13698. {
  13699. private:
  13700. CRoxieServerMultiInputInfo inputs;
  13701. LibraryCallFactoryExtra extra;
  13702. public:
  13703. CRoxieServerLibraryCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, LibraryCallFactoryExtra & _extra)
  13704. : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  13705. {
  13706. extra.set(_extra);
  13707. extra.calcUnused();
  13708. setNumOutputs(extra.outputs.ordinality());
  13709. }
  13710. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  13711. {
  13712. return new CRoxieServerLibraryCallActivity(this, _probeManager, numInputs(), numOutputs, extra);
  13713. }
  13714. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  13715. {
  13716. inputs.set(idx, source, sourceidx);
  13717. }
  13718. virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
  13719. {
  13720. return inputs.get(idx, sourceidx);
  13721. }
  13722. virtual unsigned numInputs() const { return inputs.ordinality(); }
  13723. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  13724. {
  13725. addXrefLibraryInfo(reply, extra.libraryName);
  13726. }
  13727. };
  13728. IRoxieServerActivityFactory *createRoxieServerLibraryCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, LibraryCallFactoryExtra & _extra)
  13729. {
  13730. return new CRoxieServerLibraryCallActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _extra);
  13731. }
  13732. //=====================================================================================================
  13733. class CRoxieServerNWayInputActivity : public CRoxieServerActivity
  13734. {
  13735. IHThorNWayInputArg & helper;
  13736. IRoxieInput ** inputs;
  13737. PointerArrayOf<IRoxieInput> selectedInputs;
  13738. unsigned numInputs;
  13739. public:
  13740. CRoxieServerNWayInputActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  13741. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorNWayInputArg &)basehelper), numInputs(_numInputs)
  13742. {
  13743. inputs = new IRoxieInput*[numInputs];
  13744. for (unsigned i = 0; i < numInputs; i++)
  13745. inputs[i] = NULL;
  13746. }
  13747. ~CRoxieServerNWayInputActivity()
  13748. {
  13749. delete [] inputs;
  13750. }
  13751. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13752. {
  13753. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  13754. bool selectionIsAll;
  13755. size32_t selectionLen;
  13756. rtlDataAttr selection;
  13757. helper.getInputSelection(selectionIsAll, selectionLen, selection.refdata());
  13758. selectedInputs.kill();
  13759. if (selectionIsAll)
  13760. {
  13761. for (unsigned i=0; i < numInputs; i++)
  13762. selectedInputs.append(inputs[i]);
  13763. }
  13764. else
  13765. {
  13766. const size32_t * selections = (const size32_t *)selection.getdata();
  13767. unsigned max = selectionLen/sizeof(size32_t);
  13768. for (unsigned i = 0; i < max; i++)
  13769. {
  13770. unsigned nextIndex = selections[i];
  13771. //Check there are no duplicates..... Assumes there are a fairly small number of inputs, so n^2 search is ok.
  13772. for (unsigned j=i+1; j < max; j++)
  13773. {
  13774. if (nextIndex == selections[j])
  13775. throw MakeStringException(ROXIE_NWAY_INPUT_ERROR, "Selection list for nway input can not contain duplicates");
  13776. }
  13777. if (nextIndex > numInputs)
  13778. throw MakeStringException(ROXIE_NWAY_INPUT_ERROR, "Index %d in RANGE selection list is out of range", nextIndex);
  13779. selectedInputs.append(inputs[nextIndex-1]);
  13780. }
  13781. }
  13782. ForEachItemIn(i2, selectedInputs)
  13783. selectedInputs.item(i2)->start(parentExtractSize, parentExtract, paused);
  13784. }
  13785. virtual void stop(bool aborting)
  13786. {
  13787. ForEachItemIn(i2, selectedInputs)
  13788. selectedInputs.item(i2)->stop(aborting);
  13789. CRoxieServerActivity::stop(aborting);
  13790. }
  13791. virtual unsigned __int64 queryLocalCycles() const
  13792. {
  13793. __int64 localCycles = totalCycles;
  13794. ForEachItemIn(i, selectedInputs)
  13795. {
  13796. localCycles -= selectedInputs.item(i)->queryTotalCycles();
  13797. }
  13798. if (localCycles < 0)
  13799. localCycles = 0;
  13800. return localCycles;
  13801. }
  13802. virtual IRoxieInput *queryInput(unsigned idx) const
  13803. {
  13804. if (selectedInputs.isItem(idx))
  13805. return selectedInputs.item(idx);
  13806. else
  13807. return NULL;
  13808. }
  13809. virtual void reset()
  13810. {
  13811. ForEachItemIn(i, selectedInputs)
  13812. selectedInputs.item(i)->reset();
  13813. selectedInputs.kill();
  13814. CRoxieServerActivity::reset();
  13815. }
  13816. virtual void setInput(unsigned idx, IRoxieInput *_in)
  13817. {
  13818. assertex(idx < numInputs);
  13819. inputs[idx] = _in;
  13820. }
  13821. virtual const void * nextInGroup()
  13822. {
  13823. throwUnexpected();
  13824. }
  13825. virtual unsigned numConcreteOutputs() const
  13826. {
  13827. return selectedInputs.ordinality();
  13828. }
  13829. virtual IRoxieInput * queryConcreteInput(unsigned idx)
  13830. {
  13831. if (selectedInputs.isItem(idx))
  13832. return selectedInputs.item(idx);
  13833. return NULL;
  13834. }
  13835. };
  13836. class CRoxieServerNWayInputActivityFactory : public CRoxieServerMultiInputFactory
  13837. {
  13838. // bool ordered;
  13839. public:
  13840. CRoxieServerNWayInputActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  13841. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  13842. {
  13843. }
  13844. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  13845. {
  13846. return new CRoxieServerNWayInputActivity(this, _probeManager, numInputs());
  13847. }
  13848. };
  13849. IRoxieServerActivityFactory *createRoxieServerNWayInputActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  13850. {
  13851. return new CRoxieServerNWayInputActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  13852. }
  13853. //=====================================================================================================
  13854. class CRoxieServerNWayGraphLoopResultReadActivity : public CRoxieServerActivity
  13855. {
  13856. IHThorNWayGraphLoopResultReadArg & helper;
  13857. CIArrayOf<CRoxieServerActivity> resultReaders;
  13858. PointerArrayOf<IRoxieInput> inputs;
  13859. unsigned graphId;
  13860. bool grouped;
  13861. bool selectionIsAll;
  13862. size32_t selectionLen;
  13863. rtlDataAttr selection;
  13864. public:
  13865. CRoxieServerNWayGraphLoopResultReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
  13866. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorNWayGraphLoopResultReadArg &)basehelper)
  13867. {
  13868. grouped = helper.isGrouped();
  13869. graphId = _graphId;
  13870. selectionIsAll = false;
  13871. selectionLen = 0;
  13872. }
  13873. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13874. {
  13875. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  13876. if (inputs.ordinality() == 0)
  13877. {
  13878. initInputSelection();
  13879. unsigned max = selectionLen / sizeof(size32_t);
  13880. const size32_t * selections = (const size32_t *)selection.getdata();
  13881. IProbeManager * probeManager = NULL; // MORE!!
  13882. for (unsigned i = 0; i < max; i++)
  13883. {
  13884. CRoxieServerActivity * resultInput = new CRoxieServerInternalGraphLoopResultReadActivity(factory, probeManager, graphId, selections[i]);
  13885. resultReaders.append(*resultInput);
  13886. inputs.append(resultInput->queryOutput(0));
  13887. resultInput->onCreate(ctx, colocalParent);
  13888. resultInput->start(parentExtractSize, parentExtract, paused);
  13889. }
  13890. }
  13891. else
  13892. {
  13893. ForEachItemIn(i, inputs)
  13894. inputs.item(i)->start(parentExtractSize, parentExtract, paused);
  13895. }
  13896. }
  13897. virtual void stop(bool aborting)
  13898. {
  13899. ForEachItemIn(i, inputs)
  13900. inputs.item(i)->stop(aborting);
  13901. CRoxieServerActivity::stop(aborting);
  13902. }
  13903. virtual void reset()
  13904. {
  13905. ForEachItemIn(i, inputs)
  13906. inputs.item(i)->reset();
  13907. inputs.kill();
  13908. resultReaders.kill();
  13909. CRoxieServerActivity::reset();
  13910. }
  13911. virtual void setInput(unsigned idx, IRoxieInput *_in)
  13912. {
  13913. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for nway graph result read");
  13914. }
  13915. virtual const void * nextInGroup()
  13916. {
  13917. throwUnexpected();
  13918. }
  13919. virtual unsigned numConcreteOutputs() const
  13920. {
  13921. return inputs.ordinality();
  13922. }
  13923. virtual IRoxieInput * queryConcreteInput(unsigned idx)
  13924. {
  13925. if (inputs.isItem(idx))
  13926. return inputs.item(idx);
  13927. return NULL;
  13928. }
  13929. virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract)
  13930. {
  13931. ensureCreated();
  13932. basehelper.onStart(parentExtract, NULL);
  13933. initInputSelection();
  13934. unsigned max = selectionLen / sizeof(size32_t);
  13935. const size32_t * selections = (const size32_t *)selection.getdata();
  13936. for (unsigned i = 0; i < max; i++)
  13937. processor.noteUseIteration(selections[i]);
  13938. }
  13939. virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract, IProbeManager *probeManager, IArrayOf<IRoxieInput> &probes)
  13940. {
  13941. //selection etc. already initialised from the gratherIterationUsage() call.
  13942. unsigned max = selectionLen / sizeof(size32_t);
  13943. const size32_t * selections = (const size32_t *)selection.getdata();
  13944. for (unsigned i = 0; i < max; i++)
  13945. inputs.append(processor.connectIterationOutput(selections[i], probeManager, probes, this, i));
  13946. }
  13947. protected:
  13948. void initInputSelection()
  13949. {
  13950. helper.getInputSelection(selectionIsAll, selectionLen, selection.refdata());
  13951. if (selectionIsAll)
  13952. throw MakeStringException(ROXIE_NWAY_INPUT_ERROR, "ALL not yet supported for NWay graph inputs");
  13953. }
  13954. };
  13955. class CRoxieServerNWayGraphLoopResultReadActivityFactory : public CRoxieServerActivityFactory
  13956. {
  13957. unsigned graphId;
  13958. public:
  13959. CRoxieServerNWayGraphLoopResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _graphId)
  13960. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), graphId(_graphId)
  13961. {
  13962. }
  13963. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  13964. {
  13965. return new CRoxieServerNWayGraphLoopResultReadActivity(this, _probeManager, graphId);
  13966. }
  13967. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  13968. {
  13969. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for NWay GraphLoopResultRead activity");
  13970. }
  13971. };
  13972. IRoxieServerActivityFactory *createRoxieServerNWayGraphLoopResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned graphId)
  13973. {
  13974. return new CRoxieServerNWayGraphLoopResultReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, graphId);
  13975. }
  13976. //=================================================================================
  13977. class RoxieSteppedInput : public CInterface, implements ISteppedInput
  13978. {
  13979. public:
  13980. RoxieSteppedInput(IRoxieInput * _input) { input = _input; }
  13981. IMPLEMENT_IINTERFACE
  13982. protected:
  13983. virtual const void * nextInputRow()
  13984. {
  13985. #ifdef TRACE_SEEK_REQUESTS
  13986. IRoxieContextLogger * logger = input->queryActivity();
  13987. const void * ret = doNextInputRow();
  13988. {
  13989. CommonXmlWriter xmlwrite(XWFtrim|XWFopt|XWFnoindent);
  13990. if (!ret)
  13991. xmlwrite.outputBool(true,"eof");
  13992. else if (input->queryOutputMeta()->hasXML())
  13993. input->queryOutputMeta()->toXML((byte *) ret, xmlwrite);
  13994. logger->CTXLOG("next() returns (%s)", xmlwrite.str());
  13995. }
  13996. return ret;
  13997. #else
  13998. return doNextInputRow();
  13999. #endif
  14000. }
  14001. virtual const void * nextInputRowGE(const void * seek, unsigned numFields, bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
  14002. {
  14003. #ifdef TRACE_SEEK_REQUESTS
  14004. IRoxieContextLogger * logger = input->queryActivity();
  14005. {
  14006. CommonXmlWriter xmlwrite(XWFtrim|XWFopt|XWFnoindent);
  14007. if (input->queryOutputMeta()->hasXML())
  14008. input->queryOutputMeta()->toXML((byte *) seek, xmlwrite);
  14009. logger->CTXLOG("nextInputRowGE(%d, %s%s%s, %s) seek(%s)",
  14010. numFields,
  14011. stepExtra.readAheadManyResults() ? "readahead " : "",
  14012. stepExtra.returnMismatches() ? "mismatch" : "exact",
  14013. stepExtra.onlyReturnFirstSeekMatch() ? " single-match" : "",
  14014. stepExtra.queryExtraSeeks() ? "multi-seek":"",
  14015. xmlwrite.str());
  14016. }
  14017. const void * ret = doNextInputRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  14018. {
  14019. CommonXmlWriter xmlwrite(XWFtrim|XWFopt|XWFnoindent);
  14020. if (!ret)
  14021. xmlwrite.outputBool(true,"eof");
  14022. else if (input->queryOutputMeta()->hasXML())
  14023. input->queryOutputMeta()->toXML((byte *) ret, xmlwrite);
  14024. logger->CTXLOG("nextInputRowGE(%d, %s%s%s, %s) result(%s)",
  14025. numFields,
  14026. stepExtra.readAheadManyResults() ? "readahead " : "",
  14027. stepExtra.returnMismatches() ? "mismatch" : "exact",
  14028. stepExtra.onlyReturnFirstSeekMatch() ? " single-match" : "",
  14029. stepExtra.queryExtraSeeks() ? "multi-seek":"",
  14030. xmlwrite.str());
  14031. }
  14032. return ret;
  14033. #else
  14034. return doNextInputRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  14035. #endif
  14036. }
  14037. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  14038. {
  14039. return input->gatherConjunctions(collector);
  14040. }
  14041. virtual void resetEOF()
  14042. {
  14043. input->resetEOF();
  14044. }
  14045. virtual IInputSteppingMeta * queryInputSteppingMeta()
  14046. {
  14047. return input->querySteppingMeta();
  14048. }
  14049. inline const void * doNextInputRow()
  14050. {
  14051. const void * ret = input->nextInGroup();
  14052. if (!ret)
  14053. ret = input->nextInGroup();
  14054. return ret;
  14055. }
  14056. inline const void * doNextInputRowGE(const void * seek, unsigned numFields, bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
  14057. {
  14058. assertex(wasCompleteMatch);
  14059. return input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  14060. }
  14061. protected:
  14062. IRoxieInput * input;
  14063. };
  14064. //=================================================================================
  14065. class CRoxieServerNaryActivity : public CRoxieServerMultiInputActivity
  14066. {
  14067. public:
  14068. CRoxieServerNaryActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  14069. : CRoxieServerMultiInputActivity(_factory, _probeManager, _numInputs)
  14070. {
  14071. }
  14072. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14073. {
  14074. CRoxieServerMultiInputActivity::start(parentExtractSize, parentExtract, paused);
  14075. for (unsigned i=0; i < numInputs; i++)
  14076. {
  14077. IRoxieInput * cur = inputArray[i];
  14078. unsigned numRealInputs = cur->numConcreteOutputs();
  14079. for (unsigned j = 0; j < numRealInputs; j++)
  14080. {
  14081. IRoxieInput * curReal = cur->queryConcreteInput(j);
  14082. expandedInputs.append(curReal);
  14083. }
  14084. }
  14085. }
  14086. virtual void reset()
  14087. {
  14088. expandedInputs.kill();
  14089. CRoxieServerMultiInputActivity::reset();
  14090. }
  14091. protected:
  14092. PointerArrayOf<IRoxieInput> expandedInputs;
  14093. };
  14094. //=================================================================================
  14095. class CRoxieStreamMerger : public CStreamMerger
  14096. {
  14097. public:
  14098. CRoxieStreamMerger() : CStreamMerger(true)
  14099. {
  14100. inputArray = NULL;
  14101. }
  14102. void initInputs(unsigned _numInputs, IRoxieInput ** _inputArray)
  14103. {
  14104. CStreamMerger::initInputs(_numInputs);
  14105. inputArray = _inputArray;
  14106. }
  14107. virtual bool pullInput(unsigned i, const void * seek, unsigned numFields, const SmartStepExtra * stepExtra)
  14108. {
  14109. const void * next;
  14110. bool matches = true;
  14111. if (seek)
  14112. next = inputArray[i]->nextSteppedGE(seek, numFields, matches, *stepExtra);
  14113. else
  14114. next = nextUngrouped(inputArray[i]);
  14115. pending[i] = next;
  14116. pendingMatches[i] = matches;
  14117. return (next != NULL);
  14118. }
  14119. virtual void releaseRow(const void * row)
  14120. {
  14121. ReleaseRoxieRow(row);
  14122. }
  14123. protected:
  14124. IRoxieInput **inputArray;
  14125. };
  14126. class CRoxieServerNWayMergeActivity : public CRoxieServerNaryActivity
  14127. {
  14128. public:
  14129. CRoxieServerNWayMergeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  14130. : CRoxieServerNaryActivity(_factory, _probeManager, _numInputs),
  14131. helper((IHThorNWayMergeArg &)basehelper)
  14132. {
  14133. initializedMeta = false;
  14134. }
  14135. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14136. {
  14137. CRoxieServerNaryActivity::start(parentExtractSize, parentExtract, paused);
  14138. merger.init(helper.queryCompare(), helper.dedup(), helper.querySteppingMeta()->queryCompare());
  14139. merger.initInputs(expandedInputs.length(), expandedInputs.getArray());
  14140. }
  14141. virtual void stop(bool aborting)
  14142. {
  14143. merger.done();
  14144. CRoxieServerNaryActivity::stop(aborting);
  14145. }
  14146. virtual void reset()
  14147. {
  14148. merger.cleanup();
  14149. CRoxieServerNaryActivity::reset();
  14150. initializedMeta = false;
  14151. }
  14152. virtual const void * nextInGroup()
  14153. {
  14154. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  14155. const void * next = merger.nextRow();
  14156. if (next)
  14157. processed++;
  14158. return next;
  14159. }
  14160. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
  14161. {
  14162. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  14163. const void * next = merger.nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  14164. if (next)
  14165. processed++;
  14166. return next;
  14167. }
  14168. virtual IInputSteppingMeta * querySteppingMeta()
  14169. {
  14170. if (expandedInputs.ordinality() == 0)
  14171. return NULL;
  14172. if (!initializedMeta)
  14173. {
  14174. meta.init(helper.querySteppingMeta(), false);
  14175. ForEachItemIn(i, expandedInputs)
  14176. {
  14177. if (meta.getNumFields() == 0)
  14178. break;
  14179. IInputSteppingMeta * inputMeta = expandedInputs.item(i)->querySteppingMeta();
  14180. meta.intersect(inputMeta);
  14181. }
  14182. initializedMeta = true;
  14183. }
  14184. if (meta.getNumFields() == 0)
  14185. return NULL;
  14186. return &meta;
  14187. }
  14188. protected:
  14189. IHThorNWayMergeArg &helper;
  14190. CRoxieStreamMerger merger;
  14191. CSteppingMeta meta;
  14192. bool initializedMeta;
  14193. };
  14194. class CRoxieServerNWayMergeActivityFactory : public CRoxieServerMultiInputFactory
  14195. {
  14196. public:
  14197. CRoxieServerNWayMergeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14198. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  14199. {
  14200. }
  14201. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  14202. {
  14203. return new CRoxieServerNWayMergeActivity(this, _probeManager, numInputs());
  14204. }
  14205. };
  14206. IRoxieServerActivityFactory *createRoxieServerNWayMergeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14207. {
  14208. return new CRoxieServerNWayMergeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  14209. }
  14210. //=================================================================================
  14211. class CRoxieServerNWayMergeJoinActivity : public CRoxieServerNaryActivity
  14212. {
  14213. public:
  14214. CRoxieServerNWayMergeJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs, CMergeJoinProcessor & _processor)
  14215. : CRoxieServerNaryActivity(_factory, _probeManager, _numInputs),processor(_processor),
  14216. helper((IHThorNWayMergeJoinArg &)basehelper)
  14217. {
  14218. }
  14219. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14220. {
  14221. CRoxieServerNaryActivity::start(parentExtractSize, parentExtract, paused);
  14222. ForEachItemIn(i1, expandedInputs)
  14223. {
  14224. IRoxieInput * cur = expandedInputs.item(i1);
  14225. Owned<RoxieSteppedInput> stepInput = new RoxieSteppedInput(cur);
  14226. processor.addInput(stepInput);
  14227. }
  14228. ICodeContext * codectx = ctx->queryCodeContext();
  14229. Owned<IEngineRowAllocator> inputAllocator = codectx->getRowAllocator(helper.queryInputMeta(), activityId);
  14230. Owned<IEngineRowAllocator> outputAllocator = codectx->getRowAllocator(helper.queryOutputMeta(), activityId);
  14231. processor.beforeProcessing(inputAllocator, outputAllocator);
  14232. }
  14233. virtual void stop(bool aborting)
  14234. {
  14235. processor.afterProcessing();
  14236. CRoxieServerNaryActivity::stop(aborting);
  14237. }
  14238. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  14239. {
  14240. return processor.gatherConjunctions(collector);
  14241. }
  14242. virtual void resetEOF()
  14243. {
  14244. processor.queryResetEOF();
  14245. }
  14246. virtual const void * nextInGroup()
  14247. {
  14248. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  14249. const void * next = processor.nextInGroup();
  14250. if (next)
  14251. processed++;
  14252. return next;
  14253. }
  14254. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  14255. {
  14256. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  14257. const void * next = processor.nextGE(seek, numFields, wasCompleteMatch, stepExtra);
  14258. if (next)
  14259. processed++;
  14260. return next;
  14261. }
  14262. virtual IInputSteppingMeta * querySteppingMeta()
  14263. {
  14264. return processor.queryInputSteppingMeta();
  14265. }
  14266. protected:
  14267. IHThorNWayMergeJoinArg & helper;
  14268. CMergeJoinProcessor & processor;
  14269. };
  14270. class CRoxieServerAndMergeJoinActivity : public CRoxieServerNWayMergeJoinActivity
  14271. {
  14272. public:
  14273. CRoxieServerAndMergeJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  14274. : CRoxieServerNWayMergeJoinActivity(_factory, _probeManager, _numInputs, andProcessor), andProcessor(helper)
  14275. {
  14276. }
  14277. protected:
  14278. CAndMergeJoinProcessor andProcessor;
  14279. };
  14280. class CRoxieServerAndLeftMergeJoinActivity : public CRoxieServerNWayMergeJoinActivity
  14281. {
  14282. public:
  14283. CRoxieServerAndLeftMergeJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  14284. : CRoxieServerNWayMergeJoinActivity(_factory, _probeManager, _numInputs, andLeftProcessor), andLeftProcessor(helper)
  14285. {
  14286. }
  14287. protected:
  14288. CAndLeftMergeJoinProcessor andLeftProcessor;
  14289. };
  14290. class CRoxieServerMofNMergeJoinActivity : public CRoxieServerNWayMergeJoinActivity
  14291. {
  14292. public:
  14293. CRoxieServerMofNMergeJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  14294. : CRoxieServerNWayMergeJoinActivity(_factory, _probeManager, _numInputs, mofnProcessor), mofnProcessor(helper)
  14295. {
  14296. }
  14297. protected:
  14298. CMofNMergeJoinProcessor mofnProcessor;
  14299. };
  14300. class CRoxieServerProximityJoinActivity : public CRoxieServerNWayMergeJoinActivity
  14301. {
  14302. public:
  14303. CRoxieServerProximityJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  14304. : CRoxieServerNWayMergeJoinActivity(_factory, _probeManager, _numInputs, proximityProcessor), proximityProcessor(helper)
  14305. {
  14306. }
  14307. protected:
  14308. CProximityJoinProcessor proximityProcessor;
  14309. };
  14310. class CRoxieServerNWayMergeJoinActivityFactory : public CRoxieServerMultiInputFactory
  14311. {
  14312. unsigned flags;
  14313. public:
  14314. CRoxieServerNWayMergeJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14315. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  14316. {
  14317. Owned<IHThorNWayMergeJoinArg> helper = (IHThorNWayMergeJoinArg *) helperFactory();
  14318. flags = helper->getJoinFlags();
  14319. }
  14320. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  14321. {
  14322. if (flags & IHThorNWayMergeJoinArg::MJFhasrange)
  14323. return new CRoxieServerProximityJoinActivity(this, _probeManager, numInputs());
  14324. switch (flags & IHThorNWayMergeJoinArg::MJFkindmask)
  14325. {
  14326. case IHThorNWayMergeJoinArg::MJFinner:
  14327. return new CRoxieServerAndMergeJoinActivity(this, _probeManager, numInputs());
  14328. case IHThorNWayMergeJoinArg::MJFleftonly:
  14329. case IHThorNWayMergeJoinArg::MJFleftouter:
  14330. return new CRoxieServerAndLeftMergeJoinActivity(this, _probeManager, numInputs());
  14331. case IHThorNWayMergeJoinArg::MJFmofn:
  14332. return new CRoxieServerMofNMergeJoinActivity(this, _probeManager, numInputs());
  14333. default:
  14334. throwUnexpected();
  14335. }
  14336. }
  14337. };
  14338. IRoxieServerActivityFactory *createRoxieServerNWayMergeJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14339. {
  14340. return new CRoxieServerNWayMergeJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  14341. }
  14342. //=================================================================================
  14343. class CRoxieServerNWaySelectActivity : public CRoxieServerMultiInputActivity
  14344. {
  14345. IHThorNWaySelectArg &helper;
  14346. public:
  14347. CRoxieServerNWaySelectActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  14348. : CRoxieServerMultiInputActivity(_factory, _probeManager, _numInputs),
  14349. helper((IHThorNWaySelectArg &)basehelper)
  14350. {
  14351. selectedInput = NULL;
  14352. }
  14353. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14354. {
  14355. CRoxieServerMultiInputActivity::start(parentExtractSize, parentExtract, paused);
  14356. unsigned whichInput = helper.getInputIndex();
  14357. selectedInput = NULL;
  14358. if (whichInput--)
  14359. {
  14360. for (unsigned i=0; i < numInputs; i++)
  14361. {
  14362. IRoxieInput * cur = inputArray[i];
  14363. unsigned numRealInputs = cur->numConcreteOutputs();
  14364. if (whichInput < numRealInputs)
  14365. {
  14366. selectedInput = cur->queryConcreteInput(whichInput);
  14367. break;
  14368. }
  14369. whichInput -= numRealInputs;
  14370. }
  14371. }
  14372. }
  14373. virtual void reset()
  14374. {
  14375. selectedInput = NULL;
  14376. CRoxieServerMultiInputActivity::reset();
  14377. }
  14378. const void * nextInGroup()
  14379. {
  14380. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  14381. if (!selectedInput)
  14382. return NULL;
  14383. return selectedInput->nextInGroup();
  14384. }
  14385. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  14386. {
  14387. if (!selectedInput)
  14388. return false;
  14389. return selectedInput->gatherConjunctions(collector);
  14390. }
  14391. virtual void resetEOF()
  14392. {
  14393. if (selectedInput)
  14394. selectedInput->resetEOF();
  14395. }
  14396. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  14397. {
  14398. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  14399. if (!selectedInput)
  14400. return NULL;
  14401. return selectedInput->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  14402. }
  14403. IInputSteppingMeta * querySteppingMeta()
  14404. {
  14405. if (selectedInput)
  14406. return selectedInput->querySteppingMeta();
  14407. return NULL;
  14408. }
  14409. protected:
  14410. IRoxieInput * selectedInput;
  14411. };
  14412. class CRoxieServerNWaySelectActivityFactory : public CRoxieServerMultiInputFactory
  14413. {
  14414. public:
  14415. CRoxieServerNWaySelectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14416. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  14417. {
  14418. }
  14419. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  14420. {
  14421. return new CRoxieServerNWaySelectActivity(this, _probeManager, numInputs());
  14422. }
  14423. };
  14424. IRoxieServerActivityFactory *createRoxieServerNWaySelectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14425. {
  14426. return new CRoxieServerNWaySelectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  14427. }
  14428. //=================================================================================
  14429. class CRoxieServerRemoteActivity : public CRoxieServerActivity, implements IRoxieServerErrorHandler
  14430. {
  14431. protected:
  14432. IHThorRemoteArg &helper;
  14433. CRemoteResultAdaptor remote;
  14434. public:
  14435. CRoxieServerRemoteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteID)
  14436. : CRoxieServerActivity(_factory, _probeManager),
  14437. helper((IHThorRemoteArg &)basehelper),
  14438. remote(_remoteID, meta.queryOriginal(), helper, *this, false, false) // MORE - if they need it stable we'll have to think!
  14439. {
  14440. }
  14441. virtual const IResolvedFile *queryVarFileInfo() const
  14442. {
  14443. return NULL;
  14444. }
  14445. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  14446. {
  14447. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  14448. remote.onCreate(this, this, _ctx, _colocalParent);
  14449. }
  14450. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14451. {
  14452. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  14453. remote.onStart(parentExtractSize, parentExtract);
  14454. remote.setLimits(helper.getRowLimit(), (unsigned __int64) -1, I64C(0x7FFFFFFFFFFFFFFF));
  14455. unsigned fileNo = 0; // MORE - superfiles require us to do this per file part... maybe (needs thought)
  14456. remote.getMem(0, fileNo, 0); // the cached context is all we need to send
  14457. remote.flush();
  14458. remote.senddone();
  14459. }
  14460. virtual void setInput(unsigned idx, IRoxieInput *_in)
  14461. {
  14462. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  14463. }
  14464. virtual IRoxieInput *queryOutput(unsigned idx)
  14465. {
  14466. if (idx==(unsigned)-1)
  14467. idx = 0;
  14468. return idx ? NULL: &remote;
  14469. }
  14470. virtual void reset()
  14471. {
  14472. processed = remote.processed;
  14473. remote.processed = 0;
  14474. CRoxieServerActivity::reset();
  14475. }
  14476. virtual void onLimitExceeded(bool isKeyed)
  14477. {
  14478. if (traceLevel > 4)
  14479. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  14480. helper.onLimitExceeded();
  14481. }
  14482. virtual const void *createLimitFailRow(bool isKeyed)
  14483. {
  14484. UNIMPLEMENTED; // MORE - is there an ONFAIL for a limit folded into a remote?
  14485. }
  14486. virtual const void *nextInGroup()
  14487. {
  14488. throwUnexpected(); // I am nobody's input
  14489. }
  14490. };
  14491. class CRoxieServerRemoteActivityFactory : public CRoxieServerActivityFactory
  14492. {
  14493. public:
  14494. RemoteActivityId remoteId;
  14495. bool isRoot;
  14496. CRoxieServerRemoteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, bool _isRoot)
  14497. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), remoteId(_remoteId), isRoot(_isRoot)
  14498. {
  14499. }
  14500. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  14501. {
  14502. return new CRoxieServerRemoteActivity(this, _probeManager, remoteId);
  14503. }
  14504. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  14505. {
  14506. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for %s activity", getActivityText(kind));
  14507. }
  14508. virtual bool isSink() const
  14509. {
  14510. //I don't think the action version of this is implemented - but this would be the code
  14511. return isRoot && !meta.queryOriginal();
  14512. }
  14513. };
  14514. IRoxieServerActivityFactory *createRoxieServerRemoteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, bool _isRoot)
  14515. {
  14516. return new CRoxieServerRemoteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _isRoot);
  14517. }
  14518. //=================================================================================
  14519. class CRoxieServerIterateActivity : public CRoxieServerActivity
  14520. {
  14521. IHThorIterateArg &helper;
  14522. OwnedConstRoxieRow defaultRecord;
  14523. OwnedConstRoxieRow left;
  14524. OwnedConstRoxieRow right;
  14525. unsigned counter;
  14526. public:
  14527. CRoxieServerIterateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  14528. : CRoxieServerActivity(_factory, _probeManager),
  14529. helper((IHThorIterateArg &)basehelper)
  14530. {
  14531. counter = 0;
  14532. }
  14533. virtual bool needsAllocator() const { return true; }
  14534. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14535. {
  14536. counter = 0;
  14537. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  14538. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  14539. size32_t thisSize = helper.createDefault(rowBuilder);
  14540. defaultRecord.setown(rowBuilder.finalizeRowClear(thisSize));
  14541. }
  14542. virtual void reset()
  14543. {
  14544. defaultRecord.clear();
  14545. right.clear();
  14546. left.clear();
  14547. CRoxieServerActivity::reset();
  14548. }
  14549. virtual const void * nextInGroup()
  14550. {
  14551. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  14552. loop
  14553. {
  14554. right.setown(input->nextInGroup());
  14555. if (!right)
  14556. {
  14557. bool skippedGroup = (left == NULL) && (counter > 0); //we have just skipped entire group, but shouldn't output a double null
  14558. left.clear();
  14559. counter = 0;
  14560. if (skippedGroup) continue;
  14561. return NULL;
  14562. }
  14563. try
  14564. {
  14565. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  14566. unsigned outSize = helper.transform(rowBuilder, left ? left : defaultRecord, right, ++counter);
  14567. if (outSize)
  14568. {
  14569. left.setown(rowBuilder.finalizeRowClear(outSize));
  14570. processed++;
  14571. return left.getLink();
  14572. }
  14573. }
  14574. catch (IException *E)
  14575. {
  14576. throw makeWrappedException(E);
  14577. }
  14578. }
  14579. }
  14580. };
  14581. class CRoxieServerIterateActivityFactory : public CRoxieServerActivityFactory
  14582. {
  14583. public:
  14584. CRoxieServerIterateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14585. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  14586. {
  14587. }
  14588. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  14589. {
  14590. return new CRoxieServerIterateActivity(this, _probeManager);
  14591. }
  14592. };
  14593. IRoxieServerActivityFactory *createRoxieServerIterateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14594. {
  14595. return new CRoxieServerIterateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  14596. }
  14597. //=================================================================================
  14598. class CRoxieServerProcessActivity : public CRoxieServerActivity
  14599. {
  14600. IHThorProcessArg &helper;
  14601. OwnedConstRoxieRow curRight;
  14602. OwnedConstRoxieRow initialRight;
  14603. unsigned counter;
  14604. Owned<IEngineRowAllocator> rightRowAllocator;
  14605. public:
  14606. CRoxieServerProcessActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  14607. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorProcessArg &)basehelper)
  14608. {
  14609. counter = 0;
  14610. }
  14611. virtual bool needsAllocator() const { return true; }
  14612. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  14613. {
  14614. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  14615. rightRowAllocator.setown(ctx->queryCodeContext()->getRowAllocator(QUERYINTERFACE(helper.queryRightRecordSize(), IOutputMetaData), activityId));
  14616. }
  14617. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14618. {
  14619. counter = 0;
  14620. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  14621. RtlDynamicRowBuilder rowBuilder(rightRowAllocator);
  14622. size32_t thisSize = helper.createInitialRight(rowBuilder);
  14623. initialRight.setown(rowBuilder.finalizeRowClear(thisSize));
  14624. curRight.set(initialRight);
  14625. }
  14626. virtual const void * nextInGroup()
  14627. {
  14628. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  14629. try
  14630. {
  14631. loop
  14632. {
  14633. const void * in = input->nextInGroup();
  14634. if (!in)
  14635. {
  14636. bool eog = (curRight != initialRight); // processed any records?
  14637. counter = 0;
  14638. curRight.set(initialRight);
  14639. if (eog)
  14640. return NULL;
  14641. in = input->nextInGroup();
  14642. if (!in)
  14643. return NULL;
  14644. }
  14645. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  14646. RtlDynamicRowBuilder rightRowBuilder(rightRowAllocator);
  14647. size32_t outSize = helper.transform(rowBuilder, rightRowBuilder, in, curRight, ++counter);
  14648. ReleaseRoxieRow(in);
  14649. if (outSize)
  14650. {
  14651. //MORE: This should be returned...
  14652. size32_t rightSize = rightRowAllocator->queryOutputMeta()->getRecordSize(rightRowBuilder.getSelf());
  14653. curRight.setown(rightRowBuilder.finalizeRowClear(rightSize));
  14654. processed++;
  14655. return rowBuilder.finalizeRowClear(outSize);
  14656. }
  14657. }
  14658. }
  14659. catch (IException *E)
  14660. {
  14661. throw makeWrappedException(E);
  14662. }
  14663. }
  14664. };
  14665. class CRoxieServerProcessActivityFactory : public CRoxieServerActivityFactory
  14666. {
  14667. public:
  14668. CRoxieServerProcessActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14669. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  14670. {
  14671. }
  14672. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  14673. {
  14674. return new CRoxieServerProcessActivity(this, _probeManager);
  14675. }
  14676. };
  14677. IRoxieServerActivityFactory *createRoxieServerProcessActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14678. {
  14679. return new CRoxieServerProcessActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  14680. }
  14681. //=================================================================================
  14682. class CRoxieServerGroupActivity : public CRoxieServerActivity
  14683. {
  14684. IHThorGroupArg &helper;
  14685. bool endPending;
  14686. bool eof;
  14687. bool first;
  14688. const void *next;
  14689. public:
  14690. CRoxieServerGroupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  14691. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorGroupArg &)basehelper)
  14692. {
  14693. next = NULL;
  14694. endPending = false;
  14695. eof = false;
  14696. first = true;
  14697. }
  14698. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14699. {
  14700. endPending = false;
  14701. eof = false;
  14702. first = true;
  14703. assertex(next == NULL);
  14704. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  14705. }
  14706. virtual void reset()
  14707. {
  14708. ReleaseClearRoxieRow(next);
  14709. CRoxieServerActivity::reset();
  14710. }
  14711. virtual const void * nextInGroup()
  14712. {
  14713. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  14714. if (first)
  14715. {
  14716. next = input->nextInGroup();
  14717. first = false;
  14718. }
  14719. if (eof || endPending)
  14720. {
  14721. endPending = false;
  14722. return NULL;
  14723. }
  14724. const void * prev = next;
  14725. next = input->nextInGroup();
  14726. if (!next)
  14727. next = input->nextInGroup();
  14728. if (next)
  14729. {
  14730. assertex(prev);
  14731. if (!helper.isSameGroup(prev, next))
  14732. endPending = true;
  14733. }
  14734. else
  14735. eof = true;
  14736. if (prev)
  14737. processed++;
  14738. return prev;
  14739. }
  14740. };
  14741. class CRoxieServerGroupActivityFactory : public CRoxieServerActivityFactory
  14742. {
  14743. public:
  14744. CRoxieServerGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14745. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  14746. {
  14747. }
  14748. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  14749. {
  14750. return new CRoxieServerGroupActivity(this, _probeManager);
  14751. }
  14752. };
  14753. IRoxieServerActivityFactory *createRoxieServerGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14754. {
  14755. return new CRoxieServerGroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  14756. }
  14757. //=================================================================================
  14758. class CRoxieServerFirstNActivity : public CRoxieServerLateStartActivity
  14759. {
  14760. unsigned __int64 limit;
  14761. unsigned __int64 skip;
  14762. unsigned doneThisGroup;
  14763. IHThorFirstNArg &helper;
  14764. public:
  14765. CRoxieServerFirstNActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  14766. : CRoxieServerLateStartActivity(_factory, _probeManager), helper((IHThorFirstNArg &)basehelper)
  14767. {
  14768. doneThisGroup = 0;
  14769. limit = 0;
  14770. skip = 0;
  14771. }
  14772. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14773. {
  14774. doneThisGroup = 0;
  14775. CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
  14776. limit = helper.getLimit();
  14777. skip = helper.numToSkip();
  14778. lateStart(parentExtractSize, parentExtract, limit > 0);
  14779. if (limit + skip >= limit)
  14780. limit += skip;
  14781. }
  14782. const void * nextInGroup()
  14783. {
  14784. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  14785. if (eof)
  14786. return NULL;
  14787. const void *ret;
  14788. loop
  14789. {
  14790. ret = input->nextInGroup();
  14791. if (!ret)
  14792. {
  14793. if (meta.isGrouped())
  14794. {
  14795. if (doneThisGroup > skip)
  14796. {
  14797. doneThisGroup = 0;
  14798. return NULL;
  14799. }
  14800. doneThisGroup = 0;
  14801. }
  14802. ret = input->nextInGroup();
  14803. if (!ret)
  14804. {
  14805. eof = true;
  14806. return NULL;
  14807. }
  14808. }
  14809. doneThisGroup++;
  14810. if (doneThisGroup > skip)
  14811. break;
  14812. ReleaseRoxieRow(ret);
  14813. }
  14814. if (doneThisGroup <= limit)
  14815. {
  14816. processed++;
  14817. return ret;
  14818. }
  14819. ReleaseRoxieRow(ret);
  14820. if (meta.isGrouped())
  14821. {
  14822. while ((ret = input->nextInGroup()) != NULL)
  14823. ReleaseRoxieRow(ret);
  14824. doneThisGroup = 0;
  14825. }
  14826. else
  14827. eof = true;
  14828. return NULL;
  14829. }
  14830. };
  14831. class CRoxieServerFirstNActivityFactory : public CRoxieServerActivityFactory
  14832. {
  14833. public:
  14834. CRoxieServerFirstNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14835. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  14836. {
  14837. }
  14838. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  14839. {
  14840. return new CRoxieServerFirstNActivity(this, _probeManager);
  14841. }
  14842. };
  14843. IRoxieServerActivityFactory *createRoxieServerFirstNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14844. {
  14845. return new CRoxieServerFirstNActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  14846. }
  14847. //=================================================================================
  14848. class CRoxieServerSelectNActivity : public CRoxieServerActivity
  14849. {
  14850. bool done;
  14851. IHThorSelectNArg &helper;
  14852. public:
  14853. CRoxieServerSelectNActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  14854. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorSelectNArg &)basehelper)
  14855. {
  14856. done = false;
  14857. }
  14858. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14859. {
  14860. done = false;
  14861. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  14862. }
  14863. const void *defaultRow()
  14864. {
  14865. if (!rowAllocator)
  14866. createRowAllocator(); // We delay as often not needed...
  14867. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  14868. size32_t thisSize = helper.createDefault(rowBuilder);
  14869. return rowBuilder.finalizeRowClear(thisSize);
  14870. }
  14871. virtual const void * nextInGroup()
  14872. {
  14873. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  14874. if (done)
  14875. return NULL;
  14876. done = true;
  14877. processed++; // always going to return a row!
  14878. unsigned __int64 index = helper.getRowToSelect();
  14879. while (--index)
  14880. {
  14881. const void * next = input->nextInGroup();
  14882. if (!next)
  14883. next = input->nextInGroup();
  14884. if (!next)
  14885. return defaultRow();
  14886. ReleaseRoxieRow(next);
  14887. }
  14888. const void * next = input->nextInGroup();
  14889. if (!next)
  14890. next = input->nextInGroup();
  14891. if (!next)
  14892. next = defaultRow();
  14893. return next;
  14894. }
  14895. };
  14896. class CRoxieServerSelectNActivityFactory : public CRoxieServerActivityFactory
  14897. {
  14898. public:
  14899. CRoxieServerSelectNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14900. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  14901. {
  14902. }
  14903. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  14904. {
  14905. return new CRoxieServerSelectNActivity(this, _probeManager);
  14906. }
  14907. };
  14908. IRoxieServerActivityFactory *createRoxieServerSelectNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  14909. {
  14910. return new CRoxieServerSelectNActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  14911. }
  14912. //=================================================================================
  14913. class CRoxieServerSelfJoinActivity : public CRoxieServerActivity
  14914. {
  14915. IHThorJoinArg &helper;
  14916. ICompare *collate;
  14917. OwnedRowArray group;
  14918. bool matchedLeft;
  14919. BoolArray matchedRight;
  14920. bool eof;
  14921. bool first;
  14922. unsigned leftIndex;
  14923. unsigned rightIndex;
  14924. unsigned rightOuterIndex;
  14925. unsigned joinLimit;
  14926. unsigned atmostLimit;
  14927. unsigned atmostsTriggered;
  14928. unsigned abortLimit;
  14929. unsigned keepLimit;
  14930. bool leftOuterJoin;
  14931. bool rightOuterJoin;
  14932. bool exclude;
  14933. bool limitFail;
  14934. bool limitOnFail;
  14935. bool cloneLeft;
  14936. OwnedConstRoxieRow defaultLeft;
  14937. OwnedConstRoxieRow defaultRight;
  14938. OwnedConstRoxieRow lhs;
  14939. Owned<IException> failingLimit;
  14940. bool failingOuterAtmost;
  14941. Owned<IEngineRowAllocator> defaultAllocator;
  14942. Owned<IRHLimitedCompareHelper> limitedhelper;
  14943. Owned<CRHDualCache> dualcache;
  14944. IInputBase *dualCacheInput;
  14945. bool fillGroup()
  14946. {
  14947. group.clear();
  14948. matchedLeft = false;
  14949. matchedRight.kill();
  14950. failingOuterAtmost = false;
  14951. const void * next;
  14952. unsigned groupCount = 0;
  14953. while((next = input->nextInGroup()) != NULL)
  14954. {
  14955. if(groupCount==abortLimit)
  14956. {
  14957. if(limitFail)
  14958. failLimit(next);
  14959. if (ctx->queryDebugContext())
  14960. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  14961. if(limitOnFail)
  14962. {
  14963. assertex(!failingLimit);
  14964. try
  14965. {
  14966. failLimit(next);
  14967. }
  14968. catch(IException * except)
  14969. {
  14970. failingLimit.setown(except);
  14971. }
  14972. assertex(failingLimit != NULL);
  14973. group.append(next);
  14974. groupCount++;
  14975. break;
  14976. }
  14977. group.clear();
  14978. groupCount = 0;
  14979. while(next)
  14980. {
  14981. ReleaseRoxieRow(next);
  14982. next = input->nextInGroup();
  14983. }
  14984. }
  14985. else if(groupCount==atmostLimit)
  14986. {
  14987. atmostsTriggered++;
  14988. if(leftOuterJoin)
  14989. {
  14990. group.append(next);
  14991. groupCount++;
  14992. failingOuterAtmost = true;
  14993. break;
  14994. }
  14995. else
  14996. {
  14997. group.clear();
  14998. groupCount = 0;
  14999. while (next)
  15000. {
  15001. ReleaseRoxieRow(next);
  15002. next = input->nextInGroup();
  15003. }
  15004. }
  15005. }
  15006. else
  15007. {
  15008. group.append(next);
  15009. groupCount++;
  15010. }
  15011. }
  15012. if(group.ordinality()==0)
  15013. {
  15014. eof = true;
  15015. return false;
  15016. }
  15017. leftIndex = 0;
  15018. rightIndex = 0;
  15019. rightOuterIndex = 0;
  15020. joinLimit = keepLimit;
  15021. ForEachItemIn(idx, group)
  15022. matchedRight.append(false);
  15023. return true;
  15024. }
  15025. void failLimit(const void * next)
  15026. {
  15027. helper.onMatchAbortLimitExceeded();
  15028. CommonXmlWriter xmlwrite(XWFtrim|XWFopt );
  15029. if (!ctx->isBlind() && input->queryOutputMeta() && input->queryOutputMeta()->hasXML())
  15030. {
  15031. input->queryOutputMeta()->toXML((byte *) next, xmlwrite);
  15032. }
  15033. throw MakeStringException(ROXIE_TOO_MANY_RESULTS, "More than %d match candidates in self-join %d for row %s", abortLimit, queryId(), xmlwrite.str());
  15034. }
  15035. virtual bool needsAllocator() const { return true; }
  15036. const void *joinRecords(const void * curLeft, const void * curRight, IException * except = NULL)
  15037. {
  15038. try
  15039. {
  15040. if (cloneLeft && !except)
  15041. {
  15042. LinkRoxieRow(curLeft);
  15043. return curLeft;
  15044. }
  15045. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  15046. size32_t outsize = except ? helper.onFailTransform(rowBuilder, curLeft, curRight, except) : helper.transform(rowBuilder, curLeft, curRight);
  15047. if (outsize)
  15048. return rowBuilder.finalizeRowClear(outsize);
  15049. else
  15050. return NULL;
  15051. }
  15052. catch (IException *E)
  15053. {
  15054. throw makeWrappedException(E);
  15055. }
  15056. }
  15057. void createDefaultLeft()
  15058. {
  15059. if (!defaultLeft)
  15060. {
  15061. if (!defaultAllocator)
  15062. defaultAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input->queryOutputMeta(), activityId));
  15063. RtlDynamicRowBuilder rowBuilder(defaultAllocator);
  15064. size32_t thisSize = helper.createDefaultLeft(rowBuilder);
  15065. defaultLeft.setown(rowBuilder.finalizeRowClear(thisSize));
  15066. }
  15067. }
  15068. void createDefaultRight()
  15069. {
  15070. if (!defaultRight)
  15071. {
  15072. if (!defaultAllocator)
  15073. defaultAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input->queryOutputMeta(), activityId));
  15074. RtlDynamicRowBuilder rowBuilder(defaultAllocator);
  15075. size32_t thisSize = helper.createDefaultRight(rowBuilder);
  15076. defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
  15077. }
  15078. }
  15079. public:
  15080. CRoxieServerSelfJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  15081. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorJoinArg &)basehelper)
  15082. {
  15083. collate = helper.queryCompareLeftRight();
  15084. eof = false;
  15085. first = true;
  15086. keepLimit = 0;
  15087. atmostLimit = 0;
  15088. atmostsTriggered = 0;
  15089. unsigned joinFlags = helper.getJoinFlags();
  15090. leftOuterJoin = (joinFlags & JFleftouter) != 0;
  15091. rightOuterJoin = (joinFlags & JFrightouter) != 0;
  15092. cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
  15093. exclude = (joinFlags & JFexclude) != 0;
  15094. abortLimit = 0;
  15095. joinLimit = 0;
  15096. assertex((joinFlags & (JFfirst | JFfirstleft | JFfirstright)) == 0); // no longer supported
  15097. getLimitType(joinFlags, limitFail, limitOnFail);
  15098. if((joinFlags & JFslidingmatch) != 0)
  15099. throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Internal Error: Sliding self join not supported");
  15100. failingOuterAtmost = false;
  15101. matchedLeft = false;
  15102. leftIndex = 0;
  15103. rightIndex = 0;
  15104. rightOuterIndex = 0;
  15105. dualCacheInput = NULL;
  15106. }
  15107. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  15108. {
  15109. eof = false;
  15110. first = true;
  15111. failingLimit.clear();
  15112. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  15113. keepLimit = helper.getKeepLimit();
  15114. if(keepLimit == 0)
  15115. keepLimit = (unsigned)-1;
  15116. atmostsTriggered = 0;
  15117. atmostLimit = helper.getJoinLimit();
  15118. if(atmostLimit == 0)
  15119. atmostLimit = (unsigned)-1;
  15120. else
  15121. assertex(!rightOuterJoin);
  15122. abortLimit = helper.getMatchAbortLimit();
  15123. if (abortLimit == 0)
  15124. abortLimit = (unsigned)-1;
  15125. if (rightOuterJoin)
  15126. createDefaultLeft();
  15127. if (leftOuterJoin || limitOnFail)
  15128. createDefaultRight();
  15129. if ((helper.getJoinFlags() & JFlimitedprefixjoin) && helper.getJoinLimit())
  15130. { //limited match join (s[1..n])
  15131. dualcache.setown(new CRHDualCache());
  15132. dualcache->init(CRoxieServerActivity::input);
  15133. dualCacheInput = dualcache->queryOut1();
  15134. failingOuterAtmost = false;
  15135. matchedLeft = false;
  15136. leftIndex = 0;
  15137. rightOuterIndex = 0;
  15138. limitedhelper.setown(createRHLimitedCompareHelper());
  15139. limitedhelper->init( helper.getJoinLimit(), dualcache->queryOut2(), collate, helper.queryPrefixCompare() );
  15140. }
  15141. }
  15142. virtual void reset()
  15143. {
  15144. if (atmostsTriggered)
  15145. noteStatistic(STATS_ATMOST, atmostsTriggered, 1);
  15146. group.clear();
  15147. CRoxieServerActivity::reset();
  15148. defaultLeft.clear();
  15149. defaultRight.clear();
  15150. }
  15151. virtual const void * nextInGroup()
  15152. {
  15153. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  15154. if (limitedhelper)
  15155. {
  15156. while(!eof) //limited match join
  15157. {
  15158. if (!group.isItem(rightIndex))
  15159. {
  15160. lhs.setown(dualCacheInput->nextInGroup());
  15161. if (lhs)
  15162. {
  15163. rightIndex = 0;
  15164. group.clear();
  15165. limitedhelper->getGroup(group,lhs);
  15166. }
  15167. else
  15168. {
  15169. eof = true;
  15170. }
  15171. }
  15172. if (group.isItem(rightIndex))
  15173. {
  15174. const void * rhs = group.item(rightIndex++);
  15175. if(helper.match(lhs, rhs))
  15176. {
  15177. const void * ret = joinRecords(lhs, rhs);
  15178. return ret;
  15179. }
  15180. }
  15181. }
  15182. return NULL;
  15183. }
  15184. else
  15185. {
  15186. if (first)
  15187. {
  15188. first = false;
  15189. fillGroup();
  15190. }
  15191. while(!eof)
  15192. {
  15193. if(failingOuterAtmost)
  15194. while(group.isItem(leftIndex))
  15195. {
  15196. const void * ret = joinRecords(group.item(leftIndex++), defaultRight);
  15197. if(ret)
  15198. {
  15199. processed++;
  15200. return ret;
  15201. }
  15202. }
  15203. if((joinLimit == 0) || !group.isItem(rightIndex))
  15204. {
  15205. if(leftOuterJoin && !matchedLeft && !failingLimit)
  15206. {
  15207. const void * ret = joinRecords(group.item(leftIndex), defaultRight);
  15208. if(ret)
  15209. {
  15210. matchedLeft = true;
  15211. processed++;
  15212. return ret;
  15213. }
  15214. }
  15215. leftIndex++;
  15216. matchedLeft = false;
  15217. rightIndex = 0;
  15218. joinLimit = keepLimit;
  15219. }
  15220. if(!group.isItem(leftIndex))
  15221. {
  15222. if(failingLimit || failingOuterAtmost)
  15223. {
  15224. const void * lhs;
  15225. while((lhs = input->nextInGroup()) != NULL) // dualCache never active here
  15226. {
  15227. const void * ret = joinRecords(lhs, defaultRight, failingLimit);
  15228. ReleaseRoxieRow(lhs);
  15229. if(ret)
  15230. {
  15231. processed++;
  15232. return ret;
  15233. }
  15234. }
  15235. failingLimit.clear();
  15236. }
  15237. if(rightOuterJoin && !failingLimit)
  15238. while(group.isItem(rightOuterIndex))
  15239. if(!matchedRight.item(rightOuterIndex++))
  15240. {
  15241. const void * ret = joinRecords(defaultLeft, group.item(rightOuterIndex-1));
  15242. if(ret)
  15243. {
  15244. processed++;
  15245. return ret;
  15246. }
  15247. }
  15248. if(!fillGroup())
  15249. return NULL;
  15250. continue;
  15251. }
  15252. const void * lhs = group.item(leftIndex);
  15253. if(failingLimit)
  15254. {
  15255. leftIndex++;
  15256. const void * ret = joinRecords(lhs, defaultRight, failingLimit);
  15257. if(ret)
  15258. {
  15259. processed++;
  15260. return ret;
  15261. }
  15262. }
  15263. else
  15264. {
  15265. const void * rhs = group.item(rightIndex++);
  15266. if(helper.match(lhs, rhs))
  15267. {
  15268. matchedLeft = true;
  15269. matchedRight.replace(true, rightIndex-1);
  15270. if(!exclude)
  15271. {
  15272. const void * ret = joinRecords(lhs, rhs);
  15273. if(ret)
  15274. {
  15275. processed++;
  15276. joinLimit--;
  15277. return ret;
  15278. }
  15279. }
  15280. }
  15281. }
  15282. }
  15283. return NULL;
  15284. }
  15285. }
  15286. };
  15287. class CRoxieServerSelfJoinActivityFactory : public CRoxieServerActivityFactory
  15288. {
  15289. public:
  15290. CRoxieServerSelfJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  15291. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  15292. {
  15293. }
  15294. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  15295. {
  15296. return new CRoxieServerSelfJoinActivity(this, _probeManager);
  15297. }
  15298. };
  15299. IRoxieServerActivityFactory *createRoxieServerSelfJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  15300. {
  15301. return new CRoxieServerSelfJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  15302. }
  15303. //=====================================================================================================
  15304. class CRoxieServerLookupJoinActivity : public CRoxieServerTwoInputActivity
  15305. {
  15306. private:
  15307. class LookupTable : public CInterface
  15308. {
  15309. public:
  15310. LookupTable(IHThorHashJoinArg &helper)
  15311. : leftRightCompare(helper.queryCompareLeftRight()), rightCompare(helper.queryCompareRight()),
  15312. leftHash(helper.queryHashLeft()), rightHash(helper.queryHashRight())
  15313. {
  15314. size = 0;
  15315. }
  15316. virtual const void *find(const void * left) const = 0;
  15317. virtual const void *findNext(const void * left) const = 0;
  15318. protected:
  15319. ICompare * leftRightCompare;
  15320. ICompare * rightCompare;
  15321. IHash * leftHash;
  15322. IHash * rightHash;
  15323. unsigned size;
  15324. };
  15325. class DedupLookupTable : public LookupTable
  15326. {
  15327. public:
  15328. DedupLookupTable(ConstPointerArray &rightRows, IHThorHashJoinArg &helper)
  15329. : LookupTable(helper)
  15330. {
  15331. size = (4*rightRows.length())/3 + 1;
  15332. table = (const void * *)calloc(size, sizeof(void *)); // This should probably be allocated from roxiemem (and size rounded up to actual available size)
  15333. ForEachItemIn(idx, rightRows)
  15334. add(rightRows.item(idx));
  15335. }
  15336. ~DedupLookupTable()
  15337. {
  15338. unsigned i;
  15339. for(i=0; i<size; i++)
  15340. ReleaseRoxieRow(table[i]);
  15341. free(table);
  15342. }
  15343. virtual const void *find(const void * left) const
  15344. {
  15345. unsigned index = leftHash->hash(left) % size;
  15346. unsigned start = index;
  15347. while (table[index])
  15348. {
  15349. if(leftRightCompare->docompare(left, table[index]) == 0)
  15350. return table[index];
  15351. index++;
  15352. if (index==size)
  15353. index = 0;
  15354. if (index==start)
  15355. throw MakeStringException(ROXIE_JOIN_ERROR, "Internal error in lookup join activity (hash table full on lookup)");
  15356. }
  15357. return NULL;
  15358. }
  15359. virtual const void *findNext(const void * left) const
  15360. {
  15361. return NULL;
  15362. }
  15363. protected:
  15364. void add(const void * right)
  15365. {
  15366. unsigned index = rightHash->hash(right) % size;
  15367. unsigned start = index;
  15368. while (table[index])
  15369. {
  15370. if (rightCompare->docompare(table[index], right) == 0)
  15371. {
  15372. ReleaseRoxieRow(right);
  15373. return;
  15374. }
  15375. index++;
  15376. if (index==size)
  15377. index = 0;
  15378. if (index==start)
  15379. throw MakeStringException(ROXIE_JOIN_ERROR, "Internal error in lookup join activity (hash table full on add)");
  15380. }
  15381. table[index] = right;
  15382. }
  15383. const void * * table;
  15384. };
  15385. class FewLookupTable : public LookupTable
  15386. {
  15387. public:
  15388. FewLookupTable(ConstPointerArray &rightRows, IHThorHashJoinArg &helper)
  15389. : LookupTable(helper)
  15390. {
  15391. size = (4*rightRows.length())/3 + 1;
  15392. table = (const void * *)calloc(size, sizeof(void *)); // This should probably be allocated from roxiemem
  15393. findex = fstart = BadIndex;
  15394. ForEachItemIn(idx, rightRows)
  15395. add(rightRows.item(idx));
  15396. }
  15397. ~FewLookupTable()
  15398. {
  15399. unsigned i;
  15400. for(i=0; i<size; i++)
  15401. ReleaseRoxieRow(table[i]);
  15402. free(table);
  15403. }
  15404. virtual const void *find(const void * left) const
  15405. {
  15406. fstart = leftHash->hash(left) % size;
  15407. findex = fstart;
  15408. return doFind(left);
  15409. }
  15410. virtual const void *findNext(const void * left) const
  15411. {
  15412. if (findex == BadIndex)
  15413. return NULL;
  15414. advance();
  15415. return doFind(left);
  15416. }
  15417. protected:
  15418. void add(const void * right)
  15419. {
  15420. unsigned start = rightHash->hash(right) % size;
  15421. unsigned index = start;
  15422. while (table[index])
  15423. {
  15424. index++;
  15425. if (index==size)
  15426. index = 0;
  15427. if (index==start)
  15428. throwUnexpected(); //table is full, should never happen
  15429. }
  15430. table[index] = right;
  15431. }
  15432. void advance() const
  15433. {
  15434. findex++;
  15435. if(findex==size)
  15436. findex = 0;
  15437. if(findex==fstart)
  15438. throw MakeStringException(ROXIE_JOIN_ERROR, "Internal error in lookup join activity (hash table full on lookup)");
  15439. }
  15440. const void *doFind(const void * left) const
  15441. {
  15442. while(table[findex])
  15443. {
  15444. if (leftRightCompare->docompare(left, table[findex]) == 0)
  15445. return table[findex];
  15446. advance();
  15447. }
  15448. findex = BadIndex;
  15449. return NULL;
  15450. }
  15451. const void * * table;
  15452. unsigned mutable fstart;
  15453. unsigned mutable findex;
  15454. static unsigned const BadIndex;
  15455. };
  15456. class ManyLookupTable : public LookupTable
  15457. {
  15458. public:
  15459. ManyLookupTable(ConstPointerArray &rightRows, IHThorHashJoinArg &helper)
  15460. : LookupTable(helper)
  15461. {
  15462. rightRows.swapWith(rowtable);
  15463. UInt64Array groups;
  15464. unsigned numRows = rowtable.length();
  15465. if (numRows)
  15466. {
  15467. unsigned groupStart = 0;
  15468. const void *groupStartRow = rowtable.item(0);
  15469. for (unsigned i=1; i < numRows; i++)
  15470. {
  15471. const void *thisRow = rowtable.item(i);
  15472. if (rightCompare->docompare(groupStartRow, thisRow))
  15473. {
  15474. groups.append(makeint64(groupStart, i-groupStart));
  15475. groupStart = i;
  15476. groupStartRow = thisRow;
  15477. }
  15478. }
  15479. groups.append(makeint64(groupStart, numRows-groupStart));
  15480. }
  15481. size = (4*groups.length())/3 + 1;
  15482. table = (__uint64 *) calloc(size, sizeof(__uint64)); // This should probably be allocated from roxiemem
  15483. ForEachItemIn(idx, groups)
  15484. {
  15485. unsigned __int64 group = groups.item(idx);
  15486. unsigned groupstart = high(group);
  15487. const void *row = rowtable.item(groupstart);
  15488. add(row, group);
  15489. }
  15490. }
  15491. ~ManyLookupTable()
  15492. {
  15493. ForEachItemIn(idx, rowtable)
  15494. {
  15495. ReleaseRoxieRow(rowtable.item(idx));
  15496. }
  15497. free(table);
  15498. }
  15499. void add(const void *row, unsigned __int64 group)
  15500. {
  15501. unsigned start = rightHash->hash(row) % size;
  15502. unsigned index = start;
  15503. while (table[index])
  15504. {
  15505. index++;
  15506. if (index==size)
  15507. index = 0;
  15508. if (index==start)
  15509. throwUnexpected(); //table is full, should never happen
  15510. }
  15511. table[index] = group;
  15512. }
  15513. virtual const void *find(const void * left) const
  15514. {
  15515. unsigned index = leftHash->hash(left) % size;
  15516. unsigned start = index;
  15517. while (table[index])
  15518. {
  15519. __uint64 group = table[index];
  15520. currentMatch = high(group);
  15521. const void *right = rowtable.item(currentMatch);
  15522. if (leftRightCompare->docompare(left, right) == 0)
  15523. {
  15524. currentMatch++;
  15525. matchCount = low(group) - 1;
  15526. return right;
  15527. }
  15528. index++;
  15529. if (index==size)
  15530. index = 0;
  15531. if (index==start)
  15532. throw MakeStringException(ROXIE_JOIN_ERROR, "Internal error in lookup join activity (hash table full on lookup)");
  15533. }
  15534. matchCount = 0;
  15535. return NULL;
  15536. }
  15537. virtual const void *findNext(const void * left) const
  15538. {
  15539. if (!matchCount)
  15540. return NULL;
  15541. matchCount--;
  15542. return rowtable.item(currentMatch++);
  15543. }
  15544. protected:
  15545. __uint64 *table;
  15546. ConstPointerArray rowtable;
  15547. mutable unsigned currentMatch;
  15548. mutable unsigned matchCount;
  15549. };
  15550. IHThorHashJoinArg &helper;
  15551. bool leftOuterJoin;
  15552. bool exclude;
  15553. bool eog;
  15554. bool many;
  15555. bool dedupRHS;
  15556. bool useFewTable;
  15557. bool matchedGroup;
  15558. const void *left;
  15559. OwnedConstRoxieRow defaultRight;
  15560. Owned<LookupTable> table;
  15561. unsigned keepLimit;
  15562. unsigned atmostLimit;
  15563. unsigned atmostsTriggered;
  15564. unsigned limitLimit;
  15565. bool limitFail;
  15566. bool limitOnFail;
  15567. bool hasGroupLimit;
  15568. bool isSmartJoin;
  15569. unsigned keepCount;
  15570. bool gotMatch;
  15571. bool cloneLeft;
  15572. ConstPointerArray rightGroup;
  15573. aindex_t rightGroupIndex;
  15574. Owned<IException> failingLimit;
  15575. ConstPointerArray filteredRight;
  15576. ThorActivityKind activityKind;
  15577. Owned<IEngineRowAllocator> defaultRightAllocator;
  15578. void createDefaultRight()
  15579. {
  15580. if (!defaultRight)
  15581. {
  15582. if (!defaultRightAllocator)
  15583. defaultRightAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input1->queryOutputMeta(), activityId));
  15584. RtlDynamicRowBuilder rowBuilder(defaultRightAllocator);
  15585. size32_t thisSize = helper.createDefaultRight(rowBuilder);
  15586. defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
  15587. }
  15588. }
  15589. public:
  15590. CRoxieServerLookupJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _useFewTable)
  15591. : CRoxieServerTwoInputActivity(_factory, _probeManager), helper((IHThorHashJoinArg &)basehelper), useFewTable(_useFewTable)
  15592. {
  15593. unsigned joinFlags = helper.getJoinFlags();
  15594. leftOuterJoin = (joinFlags & JFleftouter) != 0;
  15595. assertex((joinFlags & JFrightouter) == 0);
  15596. exclude = (joinFlags & JFexclude) != 0;
  15597. many = (joinFlags & JFmanylookup) != 0;
  15598. cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
  15599. dedupRHS = (joinFlags & (JFmanylookup | JFmatchrequired | JFtransformMaySkip)) == 0; // optimisation: can implicitly dedup RHS unless is many lookup, or match required, or transform may skip
  15600. left = NULL;
  15601. activityKind = factory->getKind();
  15602. eog = false;
  15603. matchedGroup = false;
  15604. gotMatch = false;
  15605. keepLimit = 0;
  15606. keepCount = 0;
  15607. atmostLimit = 0;
  15608. atmostsTriggered = 0;
  15609. limitLimit = 0;
  15610. rightGroupIndex = 0;
  15611. hasGroupLimit = false;
  15612. isSmartJoin = (joinFlags & JFsmart) != 0;
  15613. getLimitType(joinFlags, limitFail, limitOnFail);
  15614. }
  15615. void loadRight()
  15616. {
  15617. ConstPointerArray rightset;
  15618. try
  15619. {
  15620. const void * next;
  15621. while(true)
  15622. {
  15623. next = input1->nextInGroup();
  15624. if(!next)
  15625. next = input1->nextInGroup();
  15626. if(!next)
  15627. break;
  15628. rightset.append(next);
  15629. }
  15630. if (!dedupRHS)
  15631. {
  15632. if (useFewTable)
  15633. {
  15634. table.setown(new FewLookupTable(rightset, helper)); // NOTE - takes ownership of rightset
  15635. }
  15636. else
  15637. {
  15638. if (!helper.isRightAlreadySorted())
  15639. {
  15640. if (helper.getJoinFlags() & JFunstable)
  15641. {
  15642. qsortvec(const_cast<void * *>(rightset.getArray()), rightset.ordinality(), *helper.queryCompareRight());
  15643. }
  15644. else
  15645. {
  15646. unsigned rightord = rightset.ordinality();
  15647. MemoryAttr tempAttr(rightord*sizeof(void **)); // Temp storage for stable sort. This should probably be allocated from roxiemem
  15648. void **temp = (void **) tempAttr.bufferBase();
  15649. void **_rows = const_cast<void * *>(rightset.getArray());
  15650. memcpy(temp, _rows, rightord*sizeof(void **));
  15651. qsortvecstable(temp, rightord, *helper.queryCompareRight(), (void ***)_rows);
  15652. for (int i = 0; i < rightord; i++)
  15653. {
  15654. *_rows = **((void ***)_rows);
  15655. _rows++;
  15656. }
  15657. }
  15658. }
  15659. table.setown(new ManyLookupTable(rightset, helper)); // NOTE - takes ownership of rightset
  15660. }
  15661. }
  15662. else
  15663. {
  15664. table.setown(new DedupLookupTable(rightset, helper)); // NOTE - takes ownership of rightset
  15665. }
  15666. }
  15667. catch (...)
  15668. {
  15669. ForEachItemIn(idx, rightset)
  15670. ReleaseRoxieRow(rightset.item(idx));
  15671. throw;
  15672. }
  15673. };
  15674. virtual void reset()
  15675. {
  15676. if (atmostsTriggered)
  15677. noteStatistic(STATS_ATMOST, atmostsTriggered, 1);
  15678. CRoxieServerTwoInputActivity::reset();
  15679. ReleaseClearRoxieRow(left);
  15680. defaultRight.clear();
  15681. table.clear();
  15682. }
  15683. virtual bool needsAllocator() const { return true; }
  15684. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  15685. {
  15686. eog = false;
  15687. matchedGroup = false;
  15688. left = NULL;
  15689. CRoxieServerTwoInputActivity::start(parentExtractSize, parentExtract, paused);
  15690. keepLimit = helper.getKeepLimit();
  15691. if(keepLimit==0) keepLimit = static_cast<unsigned>(-1);
  15692. atmostsTriggered = 0;
  15693. atmostLimit = helper.getJoinLimit();
  15694. limitLimit = helper.getMatchAbortLimit();
  15695. hasGroupLimit = ((atmostLimit > 0) || (limitLimit > 0));
  15696. if(atmostLimit==0) atmostLimit = static_cast<unsigned>(-1);
  15697. if(limitLimit==0) limitLimit = static_cast<unsigned>(-1);
  15698. getLimitType(helper.getJoinFlags(), limitFail, limitOnFail);
  15699. switch (activityKind)
  15700. {
  15701. case TAKlookupjoin:
  15702. case TAKlookupdenormalizegroup:
  15703. case TAKsmartjoin:
  15704. case TAKsmartdenormalizegroup:
  15705. if (leftOuterJoin)
  15706. createDefaultRight();
  15707. break;
  15708. }
  15709. if (limitOnFail)
  15710. createDefaultRight();
  15711. }
  15712. virtual void setInput(unsigned idx, IRoxieInput *_in)
  15713. {
  15714. if (idx==1)
  15715. input1 = _in;
  15716. else
  15717. {
  15718. if ((helper.getJoinFlags() & JFparallel) != 0)
  15719. {
  15720. puller.setown(new CRoxieServerReadAheadInput(0)); // MORE - cant ask context for parallelJoinPreload as context is not yet set up.
  15721. puller->setInput(0, _in);
  15722. _in = puller;
  15723. }
  15724. CRoxieServerActivity::setInput(idx, _in);
  15725. }
  15726. }
  15727. virtual const void * nextInGroup()
  15728. {
  15729. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  15730. if(!table)
  15731. loadRight();
  15732. switch (activityKind)
  15733. {
  15734. case TAKlookupjoin:
  15735. case TAKsmartjoin:
  15736. return nextInGroupJoin();
  15737. case TAKlookupdenormalize:
  15738. case TAKlookupdenormalizegroup:
  15739. case TAKsmartdenormalize:
  15740. case TAKsmartdenormalizegroup:
  15741. return nextInGroupDenormalize();
  15742. }
  15743. throwUnexpected();
  15744. }
  15745. private:
  15746. const void * nextInGroupJoin()
  15747. {
  15748. if(!table)
  15749. loadRight();
  15750. while(true)
  15751. {
  15752. const void * right = NULL;
  15753. if(!left)
  15754. {
  15755. left = input->nextInGroup();
  15756. keepCount = keepLimit;
  15757. if(!left)
  15758. {
  15759. if (isSmartJoin)
  15760. left = input->nextInGroup();
  15761. if (!left)
  15762. {
  15763. if(matchedGroup || eog)
  15764. {
  15765. matchedGroup = false;
  15766. eog = true;
  15767. return NULL;
  15768. }
  15769. eog = true;
  15770. continue;
  15771. }
  15772. }
  15773. eog = false;
  15774. gotMatch = false;
  15775. right = getRightFirst();
  15776. }
  15777. else
  15778. right = getRightNext();
  15779. const void * ret = NULL;
  15780. if(failingLimit)
  15781. {
  15782. ret = joinException(left, failingLimit);
  15783. }
  15784. else
  15785. {
  15786. while(right)
  15787. {
  15788. if(helper.match(left, right))
  15789. {
  15790. gotMatch = true;
  15791. if(exclude)
  15792. break;
  15793. ret = joinRecords(left, right);
  15794. if(ret)
  15795. break;
  15796. }
  15797. right = getRightNext();
  15798. ret = NULL;
  15799. }
  15800. if(leftOuterJoin && !gotMatch)
  15801. {
  15802. ret = joinRecords(left, defaultRight);
  15803. gotMatch = true;
  15804. }
  15805. }
  15806. if(ret)
  15807. {
  15808. matchedGroup = true;
  15809. processed++;
  15810. if(!many || (--keepCount == 0) || failingLimit)
  15811. {
  15812. ReleaseClearRoxieRow(left);
  15813. failingLimit.clear();
  15814. }
  15815. return ret;
  15816. }
  15817. ReleaseClearRoxieRow(left);
  15818. }
  15819. }
  15820. const void * nextInGroupDenormalize()
  15821. {
  15822. while(true)
  15823. {
  15824. left = input->nextInGroup();
  15825. if(!left)
  15826. {
  15827. if (!matchedGroup || isSmartJoin)
  15828. left = input->nextInGroup();
  15829. if (!left)
  15830. {
  15831. matchedGroup = false;
  15832. return NULL;
  15833. }
  15834. }
  15835. gotMatch = false;
  15836. const void * right = getRightFirst();
  15837. const void * ret = NULL;
  15838. if (failingLimit)
  15839. ret = joinException(left, failingLimit);
  15840. else if (activityKind == TAKlookupdenormalize || activityKind == TAKsmartdenormalize)
  15841. {
  15842. OwnedConstRoxieRow newLeft;
  15843. newLeft.set(left);
  15844. unsigned rowSize = 0;
  15845. unsigned leftCount = 0;
  15846. keepCount = keepLimit;
  15847. while (right)
  15848. {
  15849. try
  15850. {
  15851. if (helper.match(left, right))
  15852. {
  15853. gotMatch = true;
  15854. if (exclude)
  15855. break;
  15856. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  15857. unsigned thisSize = helper.transform(rowBuilder, newLeft, right, ++leftCount);
  15858. if (thisSize)
  15859. {
  15860. rowSize = thisSize;
  15861. newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
  15862. }
  15863. if(!many || (--keepCount == 0))
  15864. break;
  15865. }
  15866. right = getRightNext();
  15867. }
  15868. catch (IException *E)
  15869. {
  15870. throw makeWrappedException(E);
  15871. }
  15872. }
  15873. if (rowSize)
  15874. ret = newLeft.getClear();
  15875. else if (leftOuterJoin && !gotMatch)
  15876. {
  15877. ret = left;
  15878. left = NULL;
  15879. }
  15880. }
  15881. else
  15882. {
  15883. filteredRight.kill();
  15884. keepCount = keepLimit;
  15885. while (right)
  15886. {
  15887. if (helper.match(left, right))
  15888. {
  15889. gotMatch = true;
  15890. if(exclude)
  15891. break;
  15892. filteredRight.append(right);
  15893. if(!many || (--keepCount == 0))
  15894. break;
  15895. }
  15896. right = getRightNext();
  15897. }
  15898. if((filteredRight.ordinality() > 0) || (leftOuterJoin && !gotMatch))
  15899. ret = denormalizeRecords(left, filteredRight);
  15900. filteredRight.kill();
  15901. }
  15902. ReleaseRoxieRow(left);
  15903. left = NULL;
  15904. failingLimit.clear();
  15905. if(ret)
  15906. {
  15907. matchedGroup = true;
  15908. processed++;
  15909. return ret;
  15910. }
  15911. }
  15912. }
  15913. const void * joinRecords(const void * left, const void * right)
  15914. {
  15915. if (cloneLeft)
  15916. {
  15917. LinkRoxieRow(left);
  15918. return left;
  15919. }
  15920. try
  15921. {
  15922. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  15923. unsigned outSize = helper.transform(rowBuilder, left, right);
  15924. if (outSize)
  15925. return rowBuilder.finalizeRowClear(outSize);
  15926. else
  15927. return NULL;
  15928. }
  15929. catch (IException *E)
  15930. {
  15931. throw makeWrappedException(E);
  15932. }
  15933. }
  15934. const void * joinException(const void * left, IException * except)
  15935. {
  15936. try
  15937. {
  15938. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  15939. unsigned outSize = helper.onFailTransform(rowBuilder, left, defaultRight, except);
  15940. if (outSize)
  15941. return rowBuilder.finalizeRowClear(outSize);
  15942. else
  15943. return NULL;
  15944. }
  15945. catch (IException *E)
  15946. {
  15947. throw makeWrappedException(E);
  15948. }
  15949. }
  15950. const void * denormalizeRecords(const void * left, ConstPointerArray & rows)
  15951. {
  15952. try
  15953. {
  15954. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  15955. unsigned numRows = rows.ordinality();
  15956. const void * right = numRows ? rows.item(0) : defaultRight.get();
  15957. unsigned outSize = helper.transform(rowBuilder, left, right, numRows, (const void * *)rows.getArray());
  15958. if (outSize)
  15959. return rowBuilder.finalizeRowClear(outSize);
  15960. else
  15961. return NULL;
  15962. }
  15963. catch (IException *E)
  15964. {
  15965. throw makeWrappedException(E);
  15966. }
  15967. }
  15968. const void * getRightFirst() { if(hasGroupLimit) return fillRightGroup(); else return table->find(left); }
  15969. const void * getRightNext() { if(hasGroupLimit) return readRightGroup(); else return table->findNext(left); }
  15970. const void * readRightGroup() { if(rightGroup.isItem(rightGroupIndex)) return rightGroup.item(rightGroupIndex++); else return NULL; }
  15971. const void *fillRightGroup()
  15972. {
  15973. rightGroup.kill();
  15974. for(const void * right = table->find(left); right; right = table->findNext(left))
  15975. {
  15976. rightGroup.append(right);
  15977. if(rightGroup.ordinality() > limitLimit)
  15978. {
  15979. if(limitFail)
  15980. failLimit();
  15981. gotMatch = true;
  15982. if (ctx->queryDebugContext())
  15983. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  15984. if(limitOnFail)
  15985. {
  15986. assertex(!failingLimit);
  15987. try
  15988. {
  15989. failLimit();
  15990. }
  15991. catch(IException * e)
  15992. {
  15993. failingLimit.setown(e);
  15994. }
  15995. assertex(failingLimit != NULL);
  15996. }
  15997. else
  15998. {
  15999. rightGroup.kill();
  16000. }
  16001. break;
  16002. }
  16003. if(rightGroup.ordinality() > atmostLimit)
  16004. {
  16005. atmostsTriggered++;
  16006. rightGroup.kill();
  16007. break;
  16008. }
  16009. }
  16010. rightGroupIndex = 0;
  16011. return readRightGroup();
  16012. }
  16013. void failLimit()
  16014. {
  16015. helper.onMatchAbortLimitExceeded();
  16016. CommonXmlWriter xmlwrite(XWFtrim|XWFopt );
  16017. if(!ctx->isBlind() && input->queryOutputMeta() && input->queryOutputMeta()->hasXML())
  16018. {
  16019. input->queryOutputMeta()->toXML(static_cast<const unsigned char *>(left), xmlwrite);
  16020. }
  16021. throw MakeStringException(ROXIE_TOO_MANY_RESULTS, "More than %u match candidates in join %d for row %s", limitLimit, queryId(), xmlwrite.str());
  16022. }
  16023. };
  16024. unsigned const CRoxieServerLookupJoinActivity::FewLookupTable::BadIndex(static_cast<unsigned>(-1));
  16025. class CRoxieServerLookupJoinActivityFactory : public CRoxieServerJoinActivityFactory
  16026. {
  16027. public:
  16028. CRoxieServerLookupJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  16029. : CRoxieServerJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  16030. {
  16031. Owned<IHThorHashJoinArg> helper = (IHThorHashJoinArg *) helperFactory();
  16032. useFewTable = _graphNode.getPropBool("hint[@name='usefewtable']/@value", false);
  16033. if((helper->getJoinFlags() & (JFfirst | JFfirstleft | JFfirstright | JFslidingmatch)) != 0)
  16034. throw MakeStringException(ROXIE_INVALID_FLAGS, "Invalid flags for lookup join activity"); // code generator should never create such an activity
  16035. }
  16036. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  16037. {
  16038. return new CRoxieServerLookupJoinActivity(this, _probeManager, useFewTable);
  16039. }
  16040. protected:
  16041. bool useFewTable;
  16042. };
  16043. IRoxieServerActivityFactory *createRoxieServerLookupJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  16044. {
  16045. return new CRoxieServerLookupJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  16046. }
  16047. //=====================================================================================================
  16048. class CRoxieServerAllJoinActivity : public CRoxieServerTwoInputActivity
  16049. {
  16050. private:
  16051. IHThorAllJoinArg &helper;
  16052. bool leftOuterJoin;
  16053. bool rightOuterJoin;
  16054. bool exclude;
  16055. OwnedConstRoxieRow defaultRight;
  16056. OwnedConstRoxieRow defaultLeft;
  16057. Owned<IEngineRowAllocator> defaultRightAllocator;
  16058. Owned<IEngineRowAllocator> defaultLeftAllocator;
  16059. const void *left;
  16060. unsigned countForLeft;
  16061. ConstPointerArray rightset;
  16062. BoolArray matchedRight; // MORE - could use a bitset...
  16063. unsigned keepLimit;
  16064. bool started;
  16065. bool eog;
  16066. bool eos;
  16067. bool matchedLeft;
  16068. bool matchedGroup;
  16069. bool leftIsGrouped;
  16070. bool cloneLeft;
  16071. unsigned rightIndex;
  16072. unsigned rightOrdinality;
  16073. ThorActivityKind activityKind;
  16074. ConstPointerArray filteredRight;
  16075. void createDefaultLeft()
  16076. {
  16077. if (!defaultLeft)
  16078. {
  16079. if (!defaultLeftAllocator)
  16080. defaultLeftAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input->queryOutputMeta(), activityId));
  16081. RtlDynamicRowBuilder rowBuilder(defaultLeftAllocator);
  16082. size32_t thisSize = helper.createDefaultLeft(rowBuilder);
  16083. defaultLeft.setown(rowBuilder.finalizeRowClear(thisSize));
  16084. }
  16085. }
  16086. void createDefaultRight()
  16087. {
  16088. if (!defaultRight)
  16089. {
  16090. if (!defaultRightAllocator)
  16091. defaultRightAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input1->queryOutputMeta(), activityId));
  16092. RtlDynamicRowBuilder rowBuilder(defaultRightAllocator);
  16093. size32_t thisSize = helper.createDefaultRight(rowBuilder);
  16094. defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
  16095. }
  16096. }
  16097. public:
  16098. CRoxieServerAllJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  16099. : CRoxieServerTwoInputActivity(_factory, _probeManager), helper((IHThorAllJoinArg &)basehelper)
  16100. {
  16101. unsigned joinFlags = helper.getJoinFlags();
  16102. leftOuterJoin = (joinFlags & JFleftouter) != 0;
  16103. rightOuterJoin = (joinFlags & JFrightouter) != 0;
  16104. cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
  16105. keepLimit = (unsigned) -1;
  16106. exclude = (joinFlags & JFexclude) != 0;
  16107. left = NULL;
  16108. started = true;
  16109. eog = false;
  16110. eos = false;
  16111. matchedLeft = false;
  16112. matchedGroup = false;
  16113. activityKind = factory->getKind();
  16114. rightIndex = 0;
  16115. rightOrdinality = 0;
  16116. leftIsGrouped = false;
  16117. countForLeft = 0;
  16118. }
  16119. virtual void reset()
  16120. {
  16121. defaultRight.clear();
  16122. defaultLeft.clear();
  16123. ReleaseRoxieRowSet(rightset);
  16124. matchedRight.kill();
  16125. CRoxieServerTwoInputActivity::reset();
  16126. }
  16127. virtual bool needsAllocator() const { return true; }
  16128. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  16129. {
  16130. eog = false;
  16131. eos = false;
  16132. matchedLeft = false;
  16133. matchedGroup = false;
  16134. started = false;
  16135. left = NULL;
  16136. CRoxieServerTwoInputActivity::start(parentExtractSize, parentExtract, paused);
  16137. keepLimit = helper.getKeepLimit();
  16138. if(keepLimit==0)
  16139. keepLimit = (unsigned) -1;
  16140. countForLeft = keepLimit;
  16141. leftIsGrouped = input->queryOutputMeta()->isGrouped();
  16142. if((activityKind==TAKalljoin || activityKind==TAKalldenormalizegroup) && leftOuterJoin)
  16143. createDefaultRight();
  16144. if(rightOuterJoin)
  16145. createDefaultLeft();
  16146. }
  16147. void loadRight()
  16148. {
  16149. const void * next;
  16150. while(true)
  16151. {
  16152. next = input1->nextInGroup();
  16153. if(!next)
  16154. next = input1->nextInGroup();
  16155. if(!next)
  16156. break;
  16157. rightset.append(next);
  16158. matchedRight.append(false);
  16159. }
  16160. rightIndex = 0;
  16161. rightOrdinality = rightset.ordinality();
  16162. }
  16163. virtual void setInput(unsigned idx, IRoxieInput *_in)
  16164. {
  16165. if (idx==1)
  16166. input1 = _in;
  16167. else
  16168. {
  16169. if ((helper.getJoinFlags() & JFparallel) != 0)
  16170. {
  16171. puller.setown(new CRoxieServerReadAheadInput(0)); // MORE - cant ask context for parallelJoinPreload as context is not yet set up.
  16172. puller->setInput(0, _in);
  16173. _in = puller;
  16174. }
  16175. CRoxieServerActivity::setInput(idx, _in);
  16176. }
  16177. }
  16178. const void * joinRecords(const void * left, const void * right)
  16179. {
  16180. // MORE - could share some code with lookup join
  16181. if (cloneLeft)
  16182. {
  16183. LinkRoxieRow(left);
  16184. return left;
  16185. }
  16186. try
  16187. {
  16188. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  16189. unsigned outSize = helper.transform(rowBuilder, left, right);
  16190. if (outSize)
  16191. return rowBuilder.finalizeRowClear(outSize);
  16192. else
  16193. return NULL;
  16194. }
  16195. catch (IException *E)
  16196. {
  16197. throw makeWrappedException(E);
  16198. }
  16199. }
  16200. const void * denormalizeRecords(const void * curLeft, ConstPointerArray & rows)
  16201. {
  16202. try
  16203. {
  16204. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  16205. unsigned numRows = rows.ordinality();
  16206. const void * right = numRows ? rows.item(0) : defaultRight.get();
  16207. unsigned outSize = helper.transform(rowBuilder, curLeft, right, numRows, rows.getArray());
  16208. if (outSize)
  16209. return rowBuilder.finalizeRowClear(outSize);
  16210. else
  16211. return NULL;
  16212. }
  16213. catch (IException *E)
  16214. {
  16215. throw makeWrappedException(E);
  16216. }
  16217. }
  16218. virtual const void *nextInGroup()
  16219. {
  16220. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  16221. if(!started)
  16222. {
  16223. started = true;
  16224. left = input->nextInGroup();
  16225. matchedLeft = false;
  16226. countForLeft = keepLimit;
  16227. if(left == NULL)
  16228. {
  16229. eos = true;
  16230. return NULL;
  16231. }
  16232. loadRight();
  16233. }
  16234. const void * ret;
  16235. const void * right;
  16236. if(eos)
  16237. return NULL;
  16238. while(true)
  16239. {
  16240. ret = NULL;
  16241. if((rightIndex == rightOrdinality) || (countForLeft==0))
  16242. {
  16243. if(leftOuterJoin && left && !matchedLeft)
  16244. {
  16245. switch(activityKind)
  16246. {
  16247. case TAKalljoin:
  16248. ret = joinRecords(left, defaultRight);
  16249. break;
  16250. case TAKalldenormalize:
  16251. ret = left;
  16252. left = NULL;
  16253. break;
  16254. case TAKalldenormalizegroup:
  16255. filteredRight.kill();
  16256. ret = denormalizeRecords(left, filteredRight);
  16257. break;
  16258. default:
  16259. throwUnexpected();
  16260. }
  16261. }
  16262. rightIndex = 0;
  16263. ReleaseRoxieRow(left);
  16264. left = NULL;
  16265. if(ret)
  16266. {
  16267. matchedGroup = true;
  16268. processed++;
  16269. return ret;
  16270. }
  16271. }
  16272. if(!left)
  16273. {
  16274. left = input->nextInGroup();
  16275. matchedLeft = false;
  16276. countForLeft = keepLimit;
  16277. }
  16278. if(!left)
  16279. {
  16280. if(eog)
  16281. {
  16282. eos = true;
  16283. matchedGroup = false;
  16284. return NULL;
  16285. }
  16286. eog = true;
  16287. if (matchedGroup && leftIsGrouped)
  16288. {
  16289. matchedGroup = false;
  16290. return NULL;
  16291. }
  16292. matchedGroup = false;
  16293. continue;
  16294. }
  16295. eog = false;
  16296. switch(activityKind)
  16297. {
  16298. case TAKalljoin:
  16299. while(rightIndex < rightOrdinality)
  16300. {
  16301. right = rightset.item(rightIndex);
  16302. if(helper.match(left, right))
  16303. {
  16304. matchedLeft = true;
  16305. matchedRight.replace(true, rightIndex);
  16306. if(!exclude)
  16307. ret = joinRecords(left, right);
  16308. }
  16309. rightIndex++;
  16310. if(ret)
  16311. {
  16312. countForLeft--;
  16313. matchedGroup = true;
  16314. processed++;
  16315. return ret;
  16316. }
  16317. }
  16318. break;
  16319. case TAKalldenormalize:
  16320. {
  16321. OwnedConstRoxieRow newLeft;
  16322. newLeft.set(left);
  16323. unsigned rowSize = 0;
  16324. unsigned leftCount = 0;
  16325. while((rightIndex < rightOrdinality) && countForLeft)
  16326. {
  16327. right = rightset.item(rightIndex);
  16328. if(helper.match(left, right))
  16329. {
  16330. matchedLeft = true;
  16331. matchedRight.replace(true, rightIndex);
  16332. if(!exclude)
  16333. {
  16334. try
  16335. {
  16336. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  16337. unsigned thisSize = helper.transform(rowBuilder, newLeft, right, ++leftCount);
  16338. if(thisSize)
  16339. {
  16340. rowSize = thisSize;
  16341. newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
  16342. --countForLeft;
  16343. }
  16344. }
  16345. catch (IException *e)
  16346. {
  16347. throw makeWrappedException(e);
  16348. }
  16349. }
  16350. }
  16351. rightIndex++;
  16352. }
  16353. if(rowSize)
  16354. {
  16355. processed++;
  16356. return newLeft.getClear();
  16357. }
  16358. }
  16359. break;
  16360. case TAKalldenormalizegroup:
  16361. filteredRight.kill();
  16362. while((rightIndex < rightOrdinality) && countForLeft)
  16363. {
  16364. right = rightset.item(rightIndex);
  16365. if(helper.match(left, right))
  16366. {
  16367. matchedLeft = true;
  16368. matchedRight.replace(true, rightIndex);
  16369. filteredRight.append(right);
  16370. --countForLeft;
  16371. }
  16372. ++rightIndex;
  16373. }
  16374. if(!exclude && filteredRight.ordinality())
  16375. {
  16376. ret = denormalizeRecords(left, filteredRight);
  16377. filteredRight.kill();
  16378. if(ret)
  16379. {
  16380. processed++;
  16381. return ret;
  16382. }
  16383. }
  16384. break;
  16385. default:
  16386. throwUnexpected();
  16387. }
  16388. }
  16389. }
  16390. };
  16391. class CRoxieServerAllJoinActivityFactory : public CRoxieServerJoinActivityFactory
  16392. {
  16393. public:
  16394. CRoxieServerAllJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  16395. : CRoxieServerJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  16396. {
  16397. Owned<IHThorAllJoinArg> helper = (IHThorAllJoinArg *) helperFactory();
  16398. if((helper->getJoinFlags() & (JFfirst | JFfirstleft | JFfirstright)) != 0)
  16399. throw MakeStringException(ROXIE_INVALID_FLAGS, "Invalid flags for join all activity"); // code generator should never create such an activity
  16400. }
  16401. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  16402. {
  16403. return new CRoxieServerAllJoinActivity(this, _probeManager);
  16404. }
  16405. };
  16406. IRoxieServerActivityFactory *createRoxieServerAllJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  16407. {
  16408. return new CRoxieServerAllJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  16409. }
  16410. //=====================================================================================================
  16411. class CRoxieServerTopNActivity : public CRoxieServerLateStartActivity
  16412. {
  16413. unsigned limit;
  16414. bool hasBest;
  16415. bool eoi;
  16416. const void **sorted;
  16417. unsigned sortedCount;
  16418. unsigned curIndex;
  16419. IHThorTopNArg &helper;
  16420. ICompare &compare;
  16421. public:
  16422. CRoxieServerTopNActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  16423. : CRoxieServerLateStartActivity(_factory, _probeManager), helper((IHThorTopNArg &)basehelper), compare(*helper.queryCompare())
  16424. {
  16425. sorted = NULL;
  16426. sortedCount = 0;
  16427. curIndex = 0;
  16428. limit = 0;
  16429. eoi = false;
  16430. hasBest = false;
  16431. }
  16432. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  16433. {
  16434. assertex(sorted == NULL);
  16435. sortedCount = 0;
  16436. curIndex = 0;
  16437. eoi = false;
  16438. CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
  16439. limit = (unsigned) helper.getLimit();
  16440. hasBest = helper.hasBest();
  16441. lateStart(parentExtractSize, parentExtract, limit > 0);
  16442. // MORE - should we use an expanding array instead?
  16443. if (limit > 0)
  16444. sorted = (const void **) ctx->queryRowManager().allocate((limit+1) * sizeof(const void *), activityId);
  16445. }
  16446. virtual void reset()
  16447. {
  16448. if (sorted)
  16449. {
  16450. while(curIndex < sortedCount)
  16451. ReleaseRoxieRow(sorted[curIndex++]);
  16452. ReleaseRoxieRow(sorted);
  16453. }
  16454. sorted = NULL;
  16455. CRoxieServerLateStartActivity::reset();
  16456. }
  16457. bool abortEarly()
  16458. {
  16459. if (hasBest && (sortedCount == limit))
  16460. {
  16461. int compare = helper.compareBest(sorted[sortedCount-1]);
  16462. if (compare == 0)
  16463. {
  16464. if (meta.isGrouped())
  16465. {
  16466. //MORE: This would be more efficient if we had a away of skipping to the end of the incoming group.
  16467. const void * next;
  16468. while ((next = input->nextInGroup()) != NULL)
  16469. ReleaseRoxieRow(next);
  16470. }
  16471. else
  16472. eoi = true;
  16473. return true;
  16474. }
  16475. //This only checks the lowest element - we could check all elements inserted, but it would increase the number of compares
  16476. if (compare < 0)
  16477. throw MakeStringException(ROXIE_TOPN_ROW_ERROR, "TOPN: row found that exceeds the best value");
  16478. }
  16479. return false;
  16480. }
  16481. void getSorted()
  16482. {
  16483. curIndex = 0;
  16484. sortedCount = 0;
  16485. if(eoi)
  16486. return;
  16487. const void * next;
  16488. while ((next = input->nextInGroup()) != NULL)
  16489. {
  16490. if (sortedCount < limit)
  16491. {
  16492. binary_vec_insert_stable(next, sorted, sortedCount, compare);
  16493. sortedCount++;
  16494. if(abortEarly())
  16495. return;
  16496. }
  16497. else
  16498. {
  16499. if(limit && compare.docompare(sorted[sortedCount-1], next) > 0) // MORE - if stability is an issue, need to consider whether this should be > or >=
  16500. {
  16501. binary_vec_insert_stable(next, sorted, sortedCount, compare); // MORE - not sure this is stable!
  16502. ReleaseRoxieRow(sorted[sortedCount]);
  16503. if(abortEarly())
  16504. return;
  16505. }
  16506. else
  16507. {
  16508. ReleaseRoxieRow(next); // do not bother with insertion sort if we know next will fall off the end
  16509. }
  16510. }
  16511. }
  16512. }
  16513. virtual const void * nextInGroup()
  16514. {
  16515. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  16516. if (eof)
  16517. return NULL;
  16518. if (curIndex >= sortedCount)
  16519. {
  16520. bool eog = sortedCount != 0;
  16521. getSorted();
  16522. if(sortedCount == 0)
  16523. {
  16524. eof = true;
  16525. return NULL;
  16526. }
  16527. if (eog)
  16528. return NULL;
  16529. }
  16530. processed++;
  16531. return sorted[curIndex++];
  16532. }
  16533. };
  16534. class CRoxieServerTopNActivityFactory : public CRoxieServerActivityFactory
  16535. {
  16536. public:
  16537. CRoxieServerTopNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  16538. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  16539. {
  16540. }
  16541. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  16542. {
  16543. return new CRoxieServerTopNActivity(this, _probeManager);
  16544. }
  16545. };
  16546. IRoxieServerActivityFactory *createRoxieServerTopNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  16547. {
  16548. return new CRoxieServerTopNActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  16549. }
  16550. //=================================================================================
  16551. class CRoxieServerLimitActivity : public CRoxieServerActivity
  16552. {
  16553. protected:
  16554. unsigned __int64 rowLimit;
  16555. IHThorLimitArg &helper;
  16556. public:
  16557. CRoxieServerLimitActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  16558. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorLimitArg &)basehelper)
  16559. {
  16560. rowLimit = 0;
  16561. }
  16562. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  16563. {
  16564. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  16565. rowLimit = helper.getRowLimit(); // could conceivably depend on context so should not compute any earlier than this
  16566. }
  16567. virtual const void *nextInGroup()
  16568. {
  16569. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  16570. const void * ret = input->nextInGroup();
  16571. if (ret)
  16572. {
  16573. processed++;
  16574. if (processed > rowLimit)
  16575. {
  16576. ReleaseRoxieRow(ret);
  16577. if (traceLevel > 4)
  16578. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  16579. helper.onLimitExceeded();
  16580. }
  16581. }
  16582. return ret;
  16583. }
  16584. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  16585. {
  16586. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  16587. const void * ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  16588. if (ret)
  16589. {
  16590. if (wasCompleteMatch)
  16591. processed++;
  16592. if (processed > rowLimit)
  16593. {
  16594. ReleaseRoxieRow(ret);
  16595. if (traceLevel > 4)
  16596. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  16597. helper.onLimitExceeded();
  16598. }
  16599. }
  16600. return ret;
  16601. }
  16602. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  16603. {
  16604. return input->gatherConjunctions(collector);
  16605. }
  16606. virtual void resetEOF()
  16607. {
  16608. //Do not reset the rowLimit
  16609. input->resetEOF();
  16610. }
  16611. IInputSteppingMeta * querySteppingMeta()
  16612. {
  16613. return input->querySteppingMeta();
  16614. }
  16615. };
  16616. class CRoxieServerLimitActivityFactory : public CRoxieServerActivityFactory
  16617. {
  16618. public:
  16619. CRoxieServerLimitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  16620. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  16621. {
  16622. }
  16623. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  16624. {
  16625. return new CRoxieServerLimitActivity(this, _probeManager);
  16626. }
  16627. };
  16628. IRoxieServerActivityFactory *createRoxieServerLimitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  16629. {
  16630. return new CRoxieServerLimitActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  16631. }
  16632. //=====================================================================================================
  16633. class CRoxieServerSkipLimitActivity : public CRoxieServerLimitActivity
  16634. {
  16635. ConstPointerArray buff;
  16636. bool started;
  16637. unsigned index;
  16638. IHThorLimitTransformExtra * transformExtra;
  16639. public:
  16640. CRoxieServerSkipLimitActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _onFail)
  16641. : CRoxieServerLimitActivity(_factory, _probeManager)
  16642. {
  16643. transformExtra = NULL;
  16644. started = false;
  16645. index = 0;
  16646. if (_onFail)
  16647. transformExtra = static_cast<IHThorLimitTransformExtra *>(helper.selectInterface(TAIlimittransformextra_1));
  16648. }
  16649. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  16650. {
  16651. started = false;
  16652. index = 0;
  16653. CRoxieServerLimitActivity::start(parentExtractSize, parentExtract, paused);
  16654. }
  16655. virtual void reset()
  16656. {
  16657. while (buff.isItem(index))
  16658. ReleaseRoxieRow(buff.item(index++));
  16659. buff.kill();
  16660. started = false;
  16661. CRoxieServerLimitActivity::reset();
  16662. }
  16663. virtual const void *nextInGroup()
  16664. {
  16665. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  16666. if (!started)
  16667. pullInput();
  16668. if (buff.isItem(index))
  16669. {
  16670. const void * next = buff.item(index++);
  16671. if(next)
  16672. processed++;
  16673. return next;
  16674. }
  16675. return NULL;
  16676. }
  16677. protected:
  16678. void pullInput()
  16679. {
  16680. unsigned count = 0;
  16681. loop
  16682. {
  16683. const void * next = input->nextInGroup();
  16684. if (next == NULL)
  16685. {
  16686. next = input->nextInGroup();
  16687. if(next == NULL)
  16688. break;
  16689. buff.append(NULL);
  16690. }
  16691. count++;
  16692. if (count > rowLimit)
  16693. {
  16694. ReleaseRoxieRow(next);
  16695. ReleaseRoxieRowSet(buff);
  16696. if (ctx->queryDebugContext())
  16697. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  16698. if (transformExtra)
  16699. {
  16700. createRowAllocator();
  16701. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  16702. size32_t outSize = transformExtra->transformOnLimitExceeded(rowBuilder);
  16703. if (outSize)
  16704. buff.append(rowBuilder.finalizeRowClear(outSize));
  16705. }
  16706. break;
  16707. }
  16708. buff.append(next);
  16709. }
  16710. started = true;
  16711. }
  16712. };
  16713. class CRoxieServerSkipLimitActivityFactory : public CRoxieServerActivityFactory
  16714. {
  16715. public:
  16716. CRoxieServerSkipLimitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  16717. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  16718. {
  16719. }
  16720. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  16721. {
  16722. return new CRoxieServerSkipLimitActivity(this, _probeManager, kind==TAKcreaterowlimit);
  16723. }
  16724. };
  16725. IRoxieServerActivityFactory *createRoxieServerSkipLimitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  16726. {
  16727. return new CRoxieServerSkipLimitActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  16728. }
  16729. //=================================================================================
  16730. class CRoxieServerCatchActivity : public CRoxieServerActivity
  16731. {
  16732. IHThorCatchArg &helper;
  16733. public:
  16734. CRoxieServerCatchActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  16735. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorCatchArg &)basehelper)
  16736. {
  16737. }
  16738. virtual const void *nextInGroup()
  16739. {
  16740. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  16741. try
  16742. {
  16743. const void *ret = input->nextInGroup();
  16744. if (ret)
  16745. processed++;
  16746. return ret;
  16747. }
  16748. catch (IException *E)
  16749. {
  16750. E->Release();
  16751. helper.onExceptionCaught();
  16752. }
  16753. catch (...)
  16754. {
  16755. helper.onExceptionCaught();
  16756. }
  16757. throwUnexpected(); // onExceptionCaught should have thrown something
  16758. }
  16759. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  16760. {
  16761. try
  16762. {
  16763. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  16764. const void * ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
  16765. if (ret && wasCompleteMatch)
  16766. processed++;
  16767. return ret;
  16768. }
  16769. catch (IException *E)
  16770. {
  16771. E->Release();
  16772. helper.onExceptionCaught();
  16773. }
  16774. catch (...)
  16775. {
  16776. helper.onExceptionCaught();
  16777. }
  16778. throwUnexpected(); // onExceptionCaught should have thrown something
  16779. }
  16780. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  16781. {
  16782. return input->gatherConjunctions(collector);
  16783. }
  16784. virtual void resetEOF()
  16785. {
  16786. input->resetEOF(); // MORE - why not in base class?
  16787. }
  16788. IInputSteppingMeta * querySteppingMeta()
  16789. {
  16790. return input->querySteppingMeta();
  16791. }
  16792. };
  16793. class CRoxieServerSkipCatchActivity : public CRoxieServerActivity
  16794. {
  16795. ConstPointerArray buff;
  16796. bool started;
  16797. unsigned index;
  16798. IHThorCatchArg &helper;
  16799. bool createRow;
  16800. public:
  16801. CRoxieServerSkipCatchActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _createRow)
  16802. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorCatchArg &)basehelper), createRow(_createRow)
  16803. {
  16804. started = false;
  16805. index = 0;
  16806. }
  16807. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  16808. {
  16809. started = false;
  16810. index = 0;
  16811. try
  16812. {
  16813. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  16814. }
  16815. catch (IException *E)
  16816. {
  16817. onException(E);
  16818. started = true;
  16819. }
  16820. catch (...)
  16821. {
  16822. onException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught"));
  16823. started = true;
  16824. }
  16825. }
  16826. virtual void reset()
  16827. {
  16828. while (buff.isItem(index))
  16829. ReleaseRoxieRow(buff.item(index++));
  16830. buff.kill();
  16831. started = false;
  16832. CRoxieServerActivity::reset();
  16833. }
  16834. virtual const void *nextInGroup()
  16835. {
  16836. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  16837. if (!started)
  16838. pullInput();
  16839. if (buff.isItem(index))
  16840. {
  16841. const void * next = buff.item(index++);
  16842. if(next)
  16843. processed++;
  16844. return next;
  16845. }
  16846. return NULL;
  16847. }
  16848. protected:
  16849. void onException(IException *E)
  16850. {
  16851. input->stop(true);
  16852. ReleaseRoxieRowSet(buff);
  16853. if (createRow)
  16854. {
  16855. createRowAllocator();
  16856. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  16857. size32_t outSize = helper.transformOnExceptionCaught(rowBuilder, E);
  16858. if (outSize)
  16859. buff.append(rowBuilder.finalizeRowClear(outSize));
  16860. }
  16861. E->Release();
  16862. }
  16863. void pullInput()
  16864. {
  16865. try
  16866. {
  16867. bool EOGseen = false;
  16868. loop
  16869. {
  16870. const void * next = input->nextInGroup();
  16871. buff.append(next);
  16872. if (next == NULL)
  16873. {
  16874. if (EOGseen)
  16875. break;
  16876. EOGseen = true;
  16877. }
  16878. else
  16879. EOGseen = false;
  16880. }
  16881. }
  16882. catch (IException *E)
  16883. {
  16884. onException(E);
  16885. }
  16886. catch (...)
  16887. {
  16888. onException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught"));
  16889. }
  16890. started = true;
  16891. }
  16892. };
  16893. class CRoxieServerCatchActivityFactory : public CRoxieServerActivityFactory
  16894. {
  16895. public:
  16896. CRoxieServerCatchActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  16897. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  16898. {
  16899. }
  16900. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  16901. {
  16902. switch (kind)
  16903. {
  16904. case TAKcatch:
  16905. return new CRoxieServerCatchActivity(this, _probeManager);
  16906. case TAKskipcatch:
  16907. return new CRoxieServerSkipCatchActivity(this, _probeManager, false);
  16908. case TAKcreaterowcatch:
  16909. return new CRoxieServerSkipCatchActivity(this, _probeManager, true);
  16910. default:
  16911. throwUnexpected();
  16912. }
  16913. }
  16914. };
  16915. IRoxieServerActivityFactory *createRoxieServerCatchActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  16916. {
  16917. return new CRoxieServerCatchActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  16918. }
  16919. //=================================================================================
  16920. class CRoxieServerCaseActivity : public CRoxieServerActivity
  16921. {
  16922. IHThorCaseArg &helper;
  16923. IRoxieInput **inputs;
  16924. unsigned cond;
  16925. bool unusedStopped;
  16926. IRoxieInput *in;
  16927. unsigned numInputs;
  16928. public:
  16929. CRoxieServerCaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  16930. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorCaseArg &)basehelper), numInputs(_numInputs)
  16931. {
  16932. unusedStopped = false;
  16933. cond = 0;
  16934. inputs = new IRoxieInput*[numInputs];
  16935. for (unsigned i = 0; i < numInputs; i++)
  16936. inputs[i] = NULL;
  16937. in = NULL;
  16938. }
  16939. ~CRoxieServerCaseActivity()
  16940. {
  16941. delete [] inputs;
  16942. }
  16943. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  16944. {
  16945. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  16946. cond = helper.getBranch();
  16947. //CHOOSE defaults to the last argument if out of range.
  16948. if (cond >= numInputs)
  16949. cond = numInputs - 1;
  16950. inputs[cond]->start(parentExtractSize, parentExtract, paused);
  16951. for (unsigned idx = 0; idx < numInputs; idx++)
  16952. {
  16953. if (idx!=cond)
  16954. inputs[idx]->stop(false); // Note: stopping unused branches early helps us avoid buffering splits too long.
  16955. }
  16956. in = inputs[cond];
  16957. unusedStopped = true;
  16958. }
  16959. virtual void stop(bool aborting)
  16960. {
  16961. for (unsigned idx = 0; idx < numInputs; idx++)
  16962. {
  16963. if (idx==cond || !unusedStopped)
  16964. inputs[idx]->stop(aborting);
  16965. }
  16966. CRoxieServerActivity::stop(aborting);
  16967. }
  16968. virtual void reset()
  16969. {
  16970. for (unsigned idx = 0; idx < numInputs; idx++)
  16971. {
  16972. inputs[idx]->reset();
  16973. }
  16974. unusedStopped = false;
  16975. in = NULL;
  16976. CRoxieServerActivity::reset();
  16977. }
  16978. virtual void setInput(unsigned idx, IRoxieInput *_in)
  16979. {
  16980. assertex(idx < numInputs);
  16981. inputs[idx] = _in;
  16982. }
  16983. virtual const void *nextInGroup()
  16984. {
  16985. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  16986. if (in)
  16987. {
  16988. const void *ret = in->nextInGroup();
  16989. if (ret)
  16990. processed++;
  16991. return ret;
  16992. }
  16993. return NULL;
  16994. }
  16995. };
  16996. class CRoxieServerCaseActivityFactory : public CRoxieServerMultiInputFactory
  16997. {
  16998. bool graphInvariant;
  16999. public:
  17000. CRoxieServerCaseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _graphInvariant)
  17001. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  17002. {
  17003. graphInvariant = _graphInvariant;
  17004. }
  17005. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  17006. {
  17007. return new CRoxieServerCaseActivity(this, _probeManager, numInputs());
  17008. }
  17009. virtual bool isGraphInvariant() const
  17010. {
  17011. return graphInvariant;
  17012. }
  17013. };
  17014. IRoxieServerActivityFactory *createRoxieServerCaseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _graphInvariant)
  17015. {
  17016. return new CRoxieServerCaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphInvariant);
  17017. }
  17018. //=================================================================================
  17019. class CRoxieServerIfActivity : public CRoxieServerActivity
  17020. {
  17021. IHThorIfArg &helper;
  17022. IRoxieInput *inputTrue;
  17023. IRoxieInput *inputFalse;
  17024. bool cond;
  17025. bool unusedStopped;
  17026. public:
  17027. CRoxieServerIfActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17028. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorIfArg &)basehelper)
  17029. {
  17030. inputFalse = NULL;
  17031. inputTrue = NULL;
  17032. unusedStopped = false;
  17033. cond = false;
  17034. }
  17035. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  17036. {
  17037. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  17038. cond = helper.getCondition();
  17039. if (cond)
  17040. {
  17041. inputTrue->start(parentExtractSize, parentExtract, paused);
  17042. if (inputFalse)
  17043. inputFalse->stop(false); // Note: stopping unused branches early helps us avoid buffering splits too long.
  17044. }
  17045. else
  17046. {
  17047. if (inputFalse)
  17048. inputFalse->start(parentExtractSize, parentExtract, paused);
  17049. inputTrue->stop(false);
  17050. }
  17051. unusedStopped = true;
  17052. }
  17053. virtual void stop(bool aborting)
  17054. {
  17055. if (!unusedStopped || cond)
  17056. inputTrue->stop(aborting);
  17057. if (inputFalse && (!unusedStopped || !cond))
  17058. inputFalse->stop(aborting);
  17059. CRoxieServerActivity::stop(aborting);
  17060. }
  17061. virtual unsigned __int64 queryLocalCycles() const
  17062. {
  17063. __int64 localCycles = totalCycles;
  17064. localCycles -= inputTrue->queryTotalCycles();
  17065. if (inputFalse)
  17066. localCycles -= inputFalse->queryTotalCycles();
  17067. if (localCycles < 0)
  17068. localCycles = 0;
  17069. return localCycles;
  17070. }
  17071. virtual IRoxieInput *queryInput(unsigned idx) const
  17072. {
  17073. switch (idx)
  17074. {
  17075. case 0:
  17076. return inputTrue;
  17077. case 1:
  17078. return inputFalse;
  17079. default:
  17080. return NULL;
  17081. }
  17082. }
  17083. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  17084. {
  17085. IRoxieInput *in = cond ? inputTrue : inputFalse;
  17086. if (in)
  17087. return in->queryIndexReadActivity();
  17088. return NULL;
  17089. }
  17090. virtual void reset()
  17091. {
  17092. CRoxieServerActivity::reset();
  17093. inputTrue->reset();
  17094. if (inputFalse)
  17095. inputFalse->reset();
  17096. unusedStopped = false;
  17097. }
  17098. virtual void setInput(unsigned idx, IRoxieInput *_in)
  17099. {
  17100. if (idx==1)
  17101. inputFalse = _in;
  17102. else
  17103. {
  17104. assertex(!idx);
  17105. inputTrue = _in;
  17106. }
  17107. }
  17108. virtual const void *nextInGroup()
  17109. {
  17110. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  17111. IRoxieInput *in = cond ? inputTrue : inputFalse;
  17112. if (in)
  17113. {
  17114. const void * ret;
  17115. if ((ret = in->nextInGroup()) != NULL)
  17116. processed++;
  17117. return ret;
  17118. }
  17119. return NULL;
  17120. }
  17121. };
  17122. class CRoxieServerIfActivityFactory : public CRoxieServerActivityFactory
  17123. {
  17124. unsigned input2;
  17125. unsigned input2idx;
  17126. bool graphInvariant;
  17127. public:
  17128. CRoxieServerIfActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _graphInvariant)
  17129. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  17130. {
  17131. graphInvariant = _graphInvariant;
  17132. input2 = (unsigned)-1;
  17133. input2idx = (unsigned)-1;
  17134. }
  17135. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  17136. {
  17137. return new CRoxieServerIfActivity(this, _probeManager);
  17138. }
  17139. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  17140. {
  17141. if (idx==1)
  17142. {
  17143. input2 = source;
  17144. input2idx = sourceidx;
  17145. }
  17146. else
  17147. CRoxieServerActivityFactory::setInput(idx, source, sourceidx);
  17148. }
  17149. virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
  17150. {
  17151. switch (idx)
  17152. {
  17153. case 1:
  17154. sourceidx = input2idx;
  17155. return input2;
  17156. case 0:
  17157. return CRoxieServerActivityFactory::getInput(idx, sourceidx);
  17158. default:
  17159. return (unsigned) -1;
  17160. }
  17161. }
  17162. virtual unsigned numInputs() const { return (input2 == (unsigned)-1) ? 1 : 2; }
  17163. virtual bool isGraphInvariant() const
  17164. {
  17165. return graphInvariant;
  17166. }
  17167. };
  17168. IRoxieServerActivityFactory *createRoxieServerIfActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _graphInvariant)
  17169. {
  17170. return new CRoxieServerIfActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphInvariant);
  17171. }
  17172. //=================================================================================
  17173. class CRoxieServerActionBaseActivity : public CRoxieServerActivity
  17174. {
  17175. CriticalSection ecrit;
  17176. Owned<IException> exception;
  17177. bool executed;
  17178. public:
  17179. CRoxieServerActionBaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17180. : CRoxieServerActivity(_factory, _probeManager)
  17181. {
  17182. executed = false;
  17183. }
  17184. virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract) = 0;
  17185. virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
  17186. {
  17187. CriticalBlock b(ecrit);
  17188. if (exception)
  17189. throw(exception.getLink());
  17190. if (!executed)
  17191. {
  17192. try
  17193. {
  17194. executed = true;
  17195. start(parentExtractSize, parentExtract, false);
  17196. doExecuteAction(parentExtractSize, parentExtract);
  17197. stop(false);
  17198. }
  17199. catch (IException * E)
  17200. {
  17201. ctx->notifyAbort(E);
  17202. stop(true);
  17203. exception.set(E);
  17204. throw;
  17205. }
  17206. }
  17207. }
  17208. virtual void reset()
  17209. {
  17210. executed = false;
  17211. exception.clear();
  17212. CRoxieServerActivity::reset();
  17213. }
  17214. virtual unsigned __int64 queryLocalCycles() const
  17215. {
  17216. return totalCycles;
  17217. }
  17218. virtual const void *nextInGroup()
  17219. {
  17220. throwUnexpected(); // I am nobody's input
  17221. }
  17222. virtual IRoxieInput *queryInput(unsigned idx) const
  17223. {
  17224. throwUnexpected(); // I am nobody's input
  17225. }
  17226. };
  17227. class CRoxieServerIfActionActivity : public CRoxieServerActionBaseActivity
  17228. {
  17229. IHThorIfArg &helper;
  17230. public:
  17231. CRoxieServerIfActionActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17232. : CRoxieServerActionBaseActivity(_factory, _probeManager), helper((IHThorIfArg &)basehelper)
  17233. {
  17234. }
  17235. virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract)
  17236. {
  17237. bool cond;
  17238. {
  17239. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  17240. cond = helper.getCondition();
  17241. }
  17242. stopDependencies(parentExtractSize, parentExtract, cond ? 2 : 1);
  17243. executeDependencies(parentExtractSize, parentExtract, cond ? 1 : 2);
  17244. }
  17245. };
  17246. class CRoxieServerIfActionActivityFactory : public CRoxieServerActivityFactory
  17247. {
  17248. bool isRoot;
  17249. public:
  17250. CRoxieServerIfActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  17251. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
  17252. {
  17253. }
  17254. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  17255. {
  17256. return new CRoxieServerIfActionActivity(this, _probeManager);
  17257. }
  17258. virtual bool isSink() const
  17259. {
  17260. return isRoot;
  17261. }
  17262. };
  17263. IRoxieServerActivityFactory *createRoxieServerIfActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  17264. {
  17265. return new CRoxieServerIfActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
  17266. }
  17267. //=================================================================================
  17268. class CRoxieServerParallelActionActivity : public CRoxieServerActionBaseActivity
  17269. {
  17270. public:
  17271. CRoxieServerParallelActionActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17272. : CRoxieServerActionBaseActivity(_factory, _probeManager)
  17273. {
  17274. }
  17275. virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract)
  17276. {
  17277. #ifdef PARALLEL_EXECUTE
  17278. CParallelActivityExecutor afor(dependencies, parentExtractSize, parentExtract);
  17279. afor.For(dependencies.ordinality(), dependencies.ordinality(), true);
  17280. #else
  17281. ForEachItemIn(idx, dependencies)
  17282. {
  17283. dependencies.item(idx).execute(parentExtractSize, parentExtract);
  17284. }
  17285. #endif
  17286. }
  17287. };
  17288. class CRoxieServerParallelActionActivityFactory : public CRoxieServerActivityFactory
  17289. {
  17290. bool isRoot;
  17291. public:
  17292. CRoxieServerParallelActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  17293. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
  17294. {
  17295. assertex(!isRoot); // non-internal should be expanded out..
  17296. }
  17297. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  17298. {
  17299. return new CRoxieServerParallelActionActivity(this, _probeManager);
  17300. }
  17301. virtual bool isSink() const
  17302. {
  17303. return isRoot;
  17304. }
  17305. };
  17306. IRoxieServerActivityFactory *createRoxieServerParallelActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  17307. {
  17308. return new CRoxieServerParallelActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
  17309. }
  17310. //=================================================================================
  17311. class CRoxieServerSequentialActionActivity : public CRoxieServerActionBaseActivity
  17312. {
  17313. IHThorSequentialArg &helper;
  17314. public:
  17315. CRoxieServerSequentialActionActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17316. : CRoxieServerActionBaseActivity(_factory, _probeManager), helper((IHThorSequentialArg &)basehelper)
  17317. {
  17318. }
  17319. virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract)
  17320. {
  17321. unsigned numBranches = helper.numBranches();
  17322. for (unsigned branch=1; branch <= numBranches; branch++)
  17323. executeDependencies(parentExtractSize, parentExtract, branch);
  17324. }
  17325. };
  17326. class CRoxieServerSequentialActionActivityFactory : public CRoxieServerActivityFactory
  17327. {
  17328. bool isRoot;
  17329. public:
  17330. CRoxieServerSequentialActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  17331. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
  17332. {
  17333. }
  17334. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  17335. {
  17336. return new CRoxieServerSequentialActionActivity(this, _probeManager);
  17337. }
  17338. virtual bool isSink() const
  17339. {
  17340. return isRoot;
  17341. }
  17342. };
  17343. IRoxieServerActivityFactory *createRoxieServerSequentialActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  17344. {
  17345. return new CRoxieServerSequentialActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
  17346. }
  17347. //=================================================================================
  17348. class CRoxieServerWhenActivity : public CRoxieServerActivity
  17349. {
  17350. public:
  17351. CRoxieServerWhenActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17352. : CRoxieServerActivity(_factory, _probeManager)
  17353. {
  17354. savedExtractSize = 0;
  17355. savedExtract = NULL;
  17356. }
  17357. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  17358. {
  17359. savedExtractSize = parentExtractSize;
  17360. savedExtract = parentExtract;
  17361. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  17362. executeDependencies(parentExtractSize, parentExtract, WhenParallelId); // MORE: This should probably be done in parallel!
  17363. }
  17364. virtual void stop(bool aborting)
  17365. {
  17366. if (state != STATEstopped)
  17367. {
  17368. stopDependencies(savedExtractSize, savedExtract, aborting ? WhenSuccessId : WhenFailureId); // These ones don't get executed
  17369. executeDependencies(savedExtractSize, savedExtract, aborting ? WhenFailureId : WhenSuccessId); // These ones do
  17370. }
  17371. CRoxieServerActivity::stop(aborting);
  17372. }
  17373. virtual const void *nextInGroup()
  17374. {
  17375. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext()); // bit of a waste of time....
  17376. return input->nextInGroup();
  17377. }
  17378. protected:
  17379. unsigned savedExtractSize;
  17380. const byte *savedExtract;
  17381. };
  17382. class CRoxieServerWhenActivityFactory : public CRoxieServerActivityFactory
  17383. {
  17384. public:
  17385. CRoxieServerWhenActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  17386. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  17387. {
  17388. }
  17389. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  17390. {
  17391. return new CRoxieServerWhenActivity(this, _probeManager);
  17392. }
  17393. };
  17394. extern IRoxieServerActivityFactory *createRoxieServerWhenActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  17395. {
  17396. return new CRoxieServerWhenActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  17397. }
  17398. //=================================================================================
  17399. class CRoxieServerWhenActionActivity : public CRoxieServerActionBaseActivity
  17400. {
  17401. public:
  17402. CRoxieServerWhenActionActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17403. : CRoxieServerActionBaseActivity(_factory, _probeManager)
  17404. {
  17405. savedExtractSize = 0;
  17406. savedExtract = NULL;
  17407. }
  17408. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  17409. {
  17410. savedExtractSize = parentExtractSize;
  17411. savedExtract = parentExtract;
  17412. CRoxieServerActionBaseActivity::start(parentExtractSize, parentExtract, paused);
  17413. executeDependencies(parentExtractSize, parentExtract, WhenParallelId); // MORE: This should probably be done in parallel!
  17414. }
  17415. virtual void stop(bool aborting)
  17416. {
  17417. if (state != STATEstopped)
  17418. {
  17419. stopDependencies(savedExtractSize, savedExtract, aborting ? WhenSuccessId : WhenFailureId); // these are NOT going to execute
  17420. executeDependencies(savedExtractSize, savedExtract, aborting ? WhenFailureId : WhenSuccessId);
  17421. }
  17422. CRoxieServerActionBaseActivity::stop(aborting);
  17423. }
  17424. virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract)
  17425. {
  17426. executeDependencies(parentExtractSize, parentExtract, 1);
  17427. }
  17428. protected:
  17429. unsigned savedExtractSize;
  17430. const byte *savedExtract;
  17431. };
  17432. class CRoxieServerWhenActionActivityFactory : public CRoxieServerActivityFactory
  17433. {
  17434. public:
  17435. CRoxieServerWhenActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  17436. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
  17437. {
  17438. }
  17439. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  17440. {
  17441. return new CRoxieServerWhenActionActivity(this, _probeManager);
  17442. }
  17443. virtual bool isSink() const
  17444. {
  17445. return isRoot;
  17446. }
  17447. private:
  17448. bool isRoot;
  17449. };
  17450. extern IRoxieServerActivityFactory *createRoxieServerWhenActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  17451. {
  17452. return new CRoxieServerWhenActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
  17453. }
  17454. //=================================================================================
  17455. class CRoxieServerParseActivity : public CRoxieServerActivity, implements IMatchedAction
  17456. {
  17457. IHThorParseArg &helper;
  17458. INlpParser * parser;
  17459. INlpResultIterator * rowIter;
  17460. const void * in;
  17461. char * curSearchText;
  17462. INlpParseAlgorithm * algorithm;
  17463. size32_t curSearchTextLen;
  17464. bool anyThisGroup;
  17465. bool processRecord(const void * inRec)
  17466. {
  17467. if (helper.searchTextNeedsFree())
  17468. rtlFree(curSearchText);
  17469. curSearchTextLen = 0;
  17470. curSearchText = NULL;
  17471. helper.getSearchText(curSearchTextLen, curSearchText, inRec);
  17472. return parser->performMatch(*this, in, curSearchTextLen, curSearchText);
  17473. }
  17474. public:
  17475. CRoxieServerParseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, INlpParseAlgorithm * _algorithm)
  17476. : CRoxieServerActivity(_factory, _probeManager),
  17477. helper((IHThorParseArg &)basehelper), algorithm(_algorithm)
  17478. {
  17479. parser = NULL;
  17480. rowIter = NULL;
  17481. in = NULL;
  17482. curSearchText = NULL;
  17483. anyThisGroup = false;
  17484. curSearchTextLen = 0;
  17485. }
  17486. ~CRoxieServerParseActivity()
  17487. {
  17488. ::Release(parser);
  17489. }
  17490. virtual bool needsAllocator() const { return true; }
  17491. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  17492. {
  17493. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  17494. parser = algorithm->createParser(_ctx->queryCodeContext(), activityId, helper.queryHelper(), &helper);
  17495. rowIter = parser->queryResultIter();
  17496. }
  17497. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  17498. {
  17499. anyThisGroup = false;
  17500. curSearchTextLen = 0;
  17501. curSearchText = NULL;
  17502. in = NULL;
  17503. parser->reset();
  17504. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  17505. }
  17506. virtual void reset()
  17507. {
  17508. if (helper.searchTextNeedsFree())
  17509. rtlFree(curSearchText);
  17510. curSearchText = NULL;
  17511. ReleaseClearRoxieRow(in);
  17512. CRoxieServerActivity::reset();
  17513. }
  17514. virtual unsigned onMatch(ARowBuilder & self, const void * curRecord, IMatchedResults * results, IMatchWalker * walker)
  17515. {
  17516. try
  17517. {
  17518. return helper.transform(self, curRecord, results, walker);
  17519. }
  17520. catch (IException *E)
  17521. {
  17522. throw makeWrappedException(E);
  17523. }
  17524. }
  17525. virtual const void * nextInGroup()
  17526. {
  17527. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  17528. loop
  17529. {
  17530. if (rowIter->isValid())
  17531. {
  17532. anyThisGroup = true;
  17533. OwnedConstRoxieRow out = rowIter->getRow();
  17534. rowIter->next();
  17535. processed++;
  17536. return out.getClear();
  17537. }
  17538. ReleaseClearRoxieRow(in);
  17539. in = input->nextInGroup();
  17540. if (!in)
  17541. {
  17542. if (anyThisGroup)
  17543. {
  17544. anyThisGroup = false;
  17545. return NULL;
  17546. }
  17547. in = input->nextInGroup();
  17548. if (!in)
  17549. return NULL;
  17550. }
  17551. processRecord(in);
  17552. rowIter->first();
  17553. }
  17554. }
  17555. };
  17556. class CRoxieServerParseActivityFactory : public CRoxieServerActivityFactory
  17557. {
  17558. Owned<INlpParseAlgorithm> algorithm;
  17559. Owned<IHThorParseArg> helper;
  17560. public:
  17561. CRoxieServerParseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IResourceContext *rc)
  17562. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  17563. {
  17564. helper.setown((IHThorParseArg *) helperFactory());
  17565. algorithm.setown(createThorParser(rc, *helper));
  17566. }
  17567. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  17568. {
  17569. return new CRoxieServerParseActivity(this, _probeManager, algorithm);
  17570. }
  17571. };
  17572. IRoxieServerActivityFactory *createRoxieServerParseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IResourceContext *rc)
  17573. {
  17574. return new CRoxieServerParseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, rc);
  17575. }
  17576. //=====================================================================================================
  17577. class CRoxieServerWorkUnitWriteActivity : public CRoxieServerInternalSinkActivity
  17578. {
  17579. IHThorWorkUnitWriteArg &helper;
  17580. bool isReread;
  17581. bool grouped;
  17582. IRoxieServerContext *serverContext;
  17583. public:
  17584. CRoxieServerWorkUnitWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _isReread)
  17585. : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorWorkUnitWriteArg &)basehelper), isReread(_isReread)
  17586. {
  17587. grouped = (helper.getFlags() & POFgrouped) != 0;
  17588. serverContext = NULL;
  17589. }
  17590. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  17591. {
  17592. CRoxieServerInternalSinkActivity::onCreate(_ctx, _colocalParent);
  17593. serverContext = ctx->queryServerContext();
  17594. if (!serverContext)
  17595. {
  17596. throw MakeStringException(ROXIE_PIPE_ERROR, "Pipe output activity cannot be executed in slave context");
  17597. }
  17598. }
  17599. virtual bool needsAllocator() const { return true; }
  17600. virtual void onExecute()
  17601. {
  17602. int sequence = helper.getSequence();
  17603. const char *storedName = helper.queryName();
  17604. if (!storedName)
  17605. storedName = "Dataset";
  17606. MemoryBuffer result;
  17607. FlushingStringBuffer *response = NULL;
  17608. bool saveInContext = (int) sequence < 0 || isReread;
  17609. if (!meta.queryOriginal()) // this is a bit of a hack - don't know why no meta on an output....
  17610. meta.set(input->queryOutputMeta());
  17611. Owned<IOutputRowSerializer> rowSerializer;
  17612. Owned<IXmlWriter> writer;
  17613. if ((int) sequence >= 0)
  17614. {
  17615. response = serverContext->queryResult(sequence);
  17616. if (response)
  17617. {
  17618. const IProperties *xmlns = serverContext->queryXmlns(sequence);
  17619. response->startDataset("Dataset", helper.queryName(), sequence, (helper.getFlags() & POFextend) != 0, xmlns);
  17620. if (response->mlFmt==MarkupFmt_XML || response->mlFmt==MarkupFmt_JSON)
  17621. {
  17622. unsigned int writeFlags = serverContext->getXmlFlags();
  17623. if (response->mlFmt==MarkupFmt_JSON)
  17624. writeFlags |= XWFnoindent;
  17625. writer.setown(createIXmlWriter(writeFlags, 1, response, (response->mlFmt==MarkupFmt_JSON) ? WTJSON : WTStandard));
  17626. writer->outputBeginArray("Row");
  17627. }
  17628. }
  17629. }
  17630. size32_t outputLimitBytes = 0;
  17631. IConstWorkUnit *workunit = serverContext->queryWorkUnit();
  17632. if (workunit)
  17633. {
  17634. size32_t outputLimit;
  17635. if (helper.getFlags() & POFmaxsize)
  17636. outputLimit = helper.getMaxSize();
  17637. else
  17638. outputLimit = workunit->getDebugValueInt("outputLimit", DALI_RESULT_LIMIT_DEFAULT);
  17639. if (outputLimit>DALI_RESULT_OUTPUTMAX)
  17640. throw MakeStringException(0, "Dali result outputs are restricted to a maximum of %d MB, the current limit is %d MB. A huge dali result usually indicates the ECL needs altering.", DALI_RESULT_OUTPUTMAX, DALI_RESULT_LIMIT_DEFAULT);
  17641. assertex(outputLimit<=0x1000); // 32bit limit because MemoryBuffer/CMessageBuffers involved etc.
  17642. outputLimitBytes = outputLimit * 0x100000;
  17643. }
  17644. if (workunit != NULL || (response && response->isRaw))
  17645. {
  17646. createRowAllocator();
  17647. rowSerializer.setown(rowAllocator->createDiskSerializer(ctx->queryCodeContext()));
  17648. }
  17649. __int64 initialProcessed = processed;
  17650. RtlLinkedDatasetBuilder builder(rowAllocator);
  17651. loop
  17652. {
  17653. const void *row = input->nextInGroup();
  17654. if (saveInContext)
  17655. {
  17656. if (row || grouped)
  17657. builder.append(row);
  17658. }
  17659. if (grouped && (processed != initialProcessed))
  17660. {
  17661. if (workunit)
  17662. result.append(row == NULL);
  17663. if (response)
  17664. {
  17665. if (response->isRaw)
  17666. response->append(row == NULL);
  17667. else
  17668. {
  17669. response->append("<Row __GroupBreak__=\"1\"/>"); // sensible, but need to handle on input
  17670. }
  17671. }
  17672. }
  17673. if (!row)
  17674. {
  17675. row = input->nextInGroup();
  17676. if (!row)
  17677. break;
  17678. if (saveInContext)
  17679. builder.append(row);
  17680. }
  17681. processed++;
  17682. if (workunit)
  17683. {
  17684. CThorDemoRowSerializer serializerTarget(result);
  17685. rowSerializer->serialize(serializerTarget, (const byte *) row);
  17686. }
  17687. if (response)
  17688. {
  17689. if (response->isRaw)
  17690. {
  17691. // MORE - should be able to serialize straight to the response...
  17692. MemoryBuffer rowbuff;
  17693. CThorDemoRowSerializer serializerTarget(rowbuff);
  17694. rowSerializer->serialize(serializerTarget, (const byte *) row);
  17695. response->append(rowbuff.length(), rowbuff.toByteArray());
  17696. }
  17697. else if (writer)
  17698. {
  17699. writer->outputBeginNested("Row", false);
  17700. helper.serializeXml((byte *) row, *writer);
  17701. writer->outputEndNested("Row");
  17702. }
  17703. else
  17704. {
  17705. SimpleOutputWriter x;
  17706. helper.serializeXml((byte *) row, x);
  17707. x.newline();
  17708. response->append(x.str());
  17709. }
  17710. response->incrementRowCount();
  17711. response->flush(false);
  17712. }
  17713. ReleaseRoxieRow(row);
  17714. if (outputLimitBytes && result.length() > outputLimitBytes)
  17715. {
  17716. StringBuffer errMsg("Dataset too large to output to workunit (limit ");
  17717. errMsg.append(outputLimitBytes/0x100000).append(" megabytes), in result (");
  17718. const char *name = helper.queryName();
  17719. if (name)
  17720. errMsg.append("name=").append(name);
  17721. else
  17722. errMsg.append("sequence=").append(helper.getSequence());
  17723. errMsg.append(")");
  17724. throw MakeStringExceptionDirect(0, errMsg.str());
  17725. }
  17726. }
  17727. if (writer)
  17728. writer->outputEndArray("Row");
  17729. if (saveInContext)
  17730. serverContext->appendResultDeserialized(storedName, sequence, builder.getcount(), builder.linkrows(), (helper.getFlags() & POFextend) != 0, LINK(meta.queryOriginal()));
  17731. if (workunit)
  17732. serverContext->appendResultRawContext(storedName, sequence, result.length(), result.toByteArray(), processed, (helper.getFlags() & POFextend) != 0, false); // MORE - shame to do extra copy...
  17733. }
  17734. };
  17735. class CRoxieServerWorkUnitWriteActivityFactory : public CRoxieServerInternalSinkFactory
  17736. {
  17737. bool isReread;
  17738. public:
  17739. CRoxieServerWorkUnitWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
  17740. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot)
  17741. {
  17742. isReread = usageCount > 0;
  17743. Owned<IHThorWorkUnitWriteArg> helper = (IHThorWorkUnitWriteArg *) helperFactory();
  17744. isInternal = (helper->getSequence()==ResultSequenceInternal);
  17745. }
  17746. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  17747. {
  17748. return new CRoxieServerWorkUnitWriteActivity(this, _probeManager, isReread);
  17749. }
  17750. };
  17751. IRoxieServerActivityFactory *createRoxieServerWorkUnitWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
  17752. {
  17753. return new CRoxieServerWorkUnitWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot);
  17754. }
  17755. //=====================================================================================================
  17756. class CRoxieServerWorkUnitWriteDictActivity : public CRoxieServerInternalSinkActivity
  17757. {
  17758. IHThorDictionaryWorkUnitWriteArg &helper;
  17759. IRoxieServerContext *serverContext;
  17760. public:
  17761. CRoxieServerWorkUnitWriteDictActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17762. : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorDictionaryWorkUnitWriteArg &)basehelper)
  17763. {
  17764. serverContext = NULL;
  17765. }
  17766. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  17767. {
  17768. CRoxieServerInternalSinkActivity::onCreate(_ctx, _colocalParent);
  17769. serverContext = ctx->queryServerContext();
  17770. if (!serverContext)
  17771. {
  17772. throw MakeStringException(ROXIE_PIPE_ERROR, "Write Dictionary activity cannot be executed in slave context");
  17773. }
  17774. }
  17775. virtual bool needsAllocator() const { return true; }
  17776. virtual void onExecute()
  17777. {
  17778. int sequence = helper.getSequence();
  17779. const char *storedName = helper.queryName();
  17780. assertex(storedName && *storedName);
  17781. assertex(sequence < 0);
  17782. RtlLinkedDictionaryBuilder builder(rowAllocator, helper.queryHashLookupInfo());
  17783. loop
  17784. {
  17785. const void *row = input->nextInGroup();
  17786. if (!row)
  17787. {
  17788. row = input->nextInGroup();
  17789. if (!row)
  17790. break;
  17791. }
  17792. builder.appendOwn(row);
  17793. processed++;
  17794. }
  17795. serverContext->appendResultDeserialized(storedName, sequence, builder.getcount(), builder.linkrows(), (helper.getFlags() & POFextend) != 0, LINK(meta.queryOriginal()));
  17796. }
  17797. };
  17798. class CRoxieServerWorkUnitWriteDictActivityFactory : public CRoxieServerInternalSinkFactory
  17799. {
  17800. public:
  17801. CRoxieServerWorkUnitWriteDictActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
  17802. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot)
  17803. {
  17804. Owned<IHThorDictionaryWorkUnitWriteArg> helper = (IHThorDictionaryWorkUnitWriteArg *) helperFactory();
  17805. isInternal = (helper->getSequence()==ResultSequenceInternal);
  17806. }
  17807. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  17808. {
  17809. return new CRoxieServerWorkUnitWriteDictActivity(this, _probeManager);
  17810. }
  17811. };
  17812. IRoxieServerActivityFactory *createRoxieServerWorkUnitWriteDictActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
  17813. {
  17814. return new CRoxieServerWorkUnitWriteDictActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot);
  17815. }
  17816. //=================================================================================
  17817. class CRoxieServerRemoteResultActivity : public CRoxieServerInternalSinkActivity
  17818. {
  17819. IHThorRemoteResultArg &helper;
  17820. public:
  17821. CRoxieServerRemoteResultActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17822. : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorRemoteResultArg &)basehelper)
  17823. {
  17824. }
  17825. virtual void onExecute()
  17826. {
  17827. OwnedConstRoxieRow row = input->nextInGroup();
  17828. helper.sendResult(row); // should be only one row or something has gone wrong!
  17829. }
  17830. };
  17831. class CRoxieServerRemoteResultActivityFactory : public CRoxieServerInternalSinkFactory
  17832. {
  17833. public:
  17834. CRoxieServerRemoteResultActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
  17835. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot)
  17836. {
  17837. Owned<IHThorRemoteResultArg> helper = (IHThorRemoteResultArg *) helperFactory();
  17838. isInternal = (helper->getSequence()==ResultSequenceInternal);
  17839. }
  17840. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  17841. {
  17842. return new CRoxieServerRemoteResultActivity(this, _probeManager);
  17843. }
  17844. };
  17845. IRoxieServerActivityFactory *createRoxieServerRemoteResultActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
  17846. {
  17847. return new CRoxieServerRemoteResultActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot);
  17848. }
  17849. //=================================================================================
  17850. class CRoxieServerXmlParseActivity : public CRoxieServerActivity, implements IXMLSelect
  17851. {
  17852. IHThorXmlParseArg &helper;
  17853. Owned<IXMLParse> xmlParser;
  17854. const void * in;
  17855. char * srchStr;
  17856. unsigned numProcessedLastGroup;
  17857. bool srchStrNeedsFree;
  17858. Owned<IColumnProvider> lastMatch;
  17859. public:
  17860. IMPLEMENT_IINTERFACE;
  17861. CRoxieServerXmlParseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17862. : CRoxieServerActivity(_factory, _probeManager),
  17863. helper((IHThorXmlParseArg &)basehelper)
  17864. {
  17865. srchStrNeedsFree = helper.searchTextNeedsFree();
  17866. numProcessedLastGroup = 0;
  17867. srchStr = NULL;
  17868. in = NULL;
  17869. }
  17870. ~CRoxieServerXmlParseActivity()
  17871. {
  17872. }
  17873. virtual bool needsAllocator() const { return true; }
  17874. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  17875. {
  17876. numProcessedLastGroup = 0;
  17877. srchStr = NULL;
  17878. in = NULL;
  17879. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  17880. }
  17881. virtual void reset()
  17882. {
  17883. if (helper.searchTextNeedsFree())
  17884. rtlFree(srchStr);
  17885. srchStr = NULL;
  17886. ReleaseClearRoxieRow(in);
  17887. xmlParser.clear();
  17888. CRoxieServerActivity::reset();
  17889. }
  17890. virtual void match(IColumnProvider &entry, offset_t startOffset, offset_t endOffset)
  17891. {
  17892. lastMatch.set(&entry);
  17893. }
  17894. virtual const void *nextInGroup()
  17895. {
  17896. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  17897. loop
  17898. {
  17899. if(xmlParser)
  17900. {
  17901. loop
  17902. {
  17903. if(!xmlParser->next())
  17904. {
  17905. if (srchStrNeedsFree)
  17906. {
  17907. rtlFree(srchStr);
  17908. srchStr = NULL;
  17909. }
  17910. xmlParser.clear();
  17911. break;
  17912. }
  17913. if(lastMatch)
  17914. {
  17915. try
  17916. {
  17917. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  17918. size32_t outSize = helper.transform(rowBuilder, in, lastMatch);
  17919. lastMatch.clear();
  17920. if (outSize)
  17921. {
  17922. processed++;
  17923. return rowBuilder.finalizeRowClear(outSize);
  17924. }
  17925. }
  17926. catch (IException *E)
  17927. {
  17928. throw makeWrappedException(E);
  17929. }
  17930. }
  17931. }
  17932. }
  17933. ReleaseClearRoxieRow(in);
  17934. in = input->nextInGroup();
  17935. if(!in)
  17936. {
  17937. if(numProcessedLastGroup == processed)
  17938. in = input->nextInGroup();
  17939. if(!in)
  17940. {
  17941. numProcessedLastGroup = processed;
  17942. return NULL;
  17943. }
  17944. }
  17945. size32_t srchLen;
  17946. helper.getSearchText(srchLen, srchStr, in);
  17947. OwnedRoxieString xmlIteratorPath(helper.getXmlIteratorPath());
  17948. xmlParser.setown(createXMLParse(srchStr, srchLen, xmlIteratorPath, *this));
  17949. }
  17950. }
  17951. };
  17952. class CRoxieServerXmlParseActivityFactory : public CRoxieServerActivityFactory
  17953. {
  17954. public:
  17955. CRoxieServerXmlParseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  17956. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  17957. {
  17958. }
  17959. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  17960. {
  17961. return new CRoxieServerXmlParseActivity(this, _probeManager);
  17962. }
  17963. };
  17964. IRoxieServerActivityFactory *createRoxieServerXmlParseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  17965. {
  17966. return new CRoxieServerXmlParseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  17967. }
  17968. //=====================================================================================================
  17969. class CRoxieServerDiskReadBaseActivity : public CRoxieServerActivity, implements IRoxieServerErrorHandler, implements IIndexReadContext
  17970. {
  17971. protected:
  17972. IHThorDiskReadBaseArg &helper;
  17973. IHThorCompoundExtra * compoundHelper;
  17974. RemoteActivityId remoteId; // Note we copy it rather than reference
  17975. Owned<CSkippableRemoteResultAdaptor> remote;
  17976. unsigned numParts;
  17977. unsigned __int64 rowLimit;
  17978. unsigned __int64 stopAfter;
  17979. Linked<IInMemoryIndexManager> manager;
  17980. Owned<IInMemoryIndexCursor> cursor;
  17981. Owned<IDirectReader> reader;
  17982. CThorContiguousRowBuffer deserializeSource;
  17983. Owned<ISourceRowPrefetcher> prefetcher;
  17984. bool eof;
  17985. bool isKeyed;
  17986. bool variableFileName;
  17987. bool isOpt;
  17988. bool sorted;
  17989. bool maySkip;
  17990. bool isLocal;
  17991. CachedOutputMetaData diskSize;
  17992. Owned<const IResolvedFile> varFileInfo;
  17993. Owned<IFileIOArray> varFiles;
  17994. inline bool useRemote()
  17995. {
  17996. return remote != NULL && numParts > 1;
  17997. }
  17998. public:
  17999. IMPLEMENT_IINTERFACE;
  18000. CRoxieServerDiskReadBaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, bool _sorted, bool _maySkip, IInMemoryIndexManager *_manager)
  18001. : CRoxieServerActivity(_factory, _probeManager),
  18002. helper((IHThorDiskReadBaseArg &)basehelper),
  18003. numParts(_numParts),
  18004. remoteId(_remoteId),
  18005. manager(_manager),
  18006. isLocal(_isLocal),
  18007. sorted(_sorted),
  18008. maySkip(_maySkip),
  18009. deserializeSource(NULL)
  18010. {
  18011. if (numParts != 1 && !isLocal) // NOTE : when numParts == 0 (variable case) we create, even though we may not use
  18012. remote.setown(new CSkippableRemoteResultAdaptor(remoteId, meta.queryOriginal(), helper, *this, sorted, false, _maySkip));
  18013. compoundHelper = NULL;
  18014. eof = false;
  18015. rowLimit = (unsigned __int64) -1;
  18016. isKeyed = false;
  18017. stopAfter = I64C(0x7FFFFFFFFFFFFFFF);
  18018. diskSize.set(helper.queryDiskRecordSize());
  18019. variableFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((helper.getFlags() & (TDXvarfilename|TDXdynamicfilename)) != 0);
  18020. isOpt = (helper.getFlags() & TDRoptional) != 0;
  18021. }
  18022. virtual const IResolvedFile *queryVarFileInfo() const
  18023. {
  18024. return varFileInfo;
  18025. }
  18026. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  18027. {
  18028. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  18029. if (remote)
  18030. remote->onCreate(this, this, _ctx, _colocalParent);
  18031. }
  18032. virtual bool needsAllocator() const { return true; }
  18033. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18034. {
  18035. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  18036. if (compoundHelper)
  18037. {
  18038. rowLimit = compoundHelper->getRowLimit();
  18039. stopAfter = compoundHelper->getChooseNLimit();
  18040. }
  18041. if (!helper.canMatchAny())
  18042. eof = true;
  18043. else
  18044. {
  18045. if (variableFileName)
  18046. {
  18047. OwnedRoxieString fileName(helper.getFileName());
  18048. varFileInfo.setown(resolveLFN(fileName, isOpt));
  18049. numParts = 0;
  18050. if (varFileInfo)
  18051. {
  18052. Owned<IFilePartMap> map = varFileInfo->getFileMap();
  18053. if (map)
  18054. numParts = map->getNumParts();
  18055. }
  18056. }
  18057. if (!numParts)
  18058. {
  18059. eof = true;
  18060. }
  18061. else if (useRemote())
  18062. {
  18063. remote->onStart(parentExtractSize, parentExtract);
  18064. remote->setLimits(rowLimit, (unsigned __int64) -1, stopAfter);
  18065. unsigned fileNo = 0; // MORE - superfiles require us to do this per file part... maybe (needs thought)
  18066. // Translation into a message per channel done elsewhere....
  18067. remote->getMem(0, fileNo, 0);
  18068. remote->flush();
  18069. remote->senddone();
  18070. }
  18071. else
  18072. {
  18073. if (variableFileName)
  18074. {
  18075. unsigned channel = isLocal ? factory->queryQueryFactory().queryChannel() : 0;
  18076. varFiles.setown(varFileInfo->getIFileIOArray(isOpt, channel));
  18077. manager.setown(varFileInfo->getIndexManager(isOpt, channel, varFiles, diskSize, false, 0));
  18078. }
  18079. assertex(manager != NULL);
  18080. helper.createSegmentMonitors(this);
  18081. if (cursor)
  18082. {
  18083. isKeyed = cursor->selectKey();
  18084. cursor->reset();
  18085. }
  18086. if (!isKeyed)
  18087. {
  18088. reader.setown(manager->createReader(0, 0, 1));
  18089. deserializeSource.setStream(reader);
  18090. prefetcher.setown(diskSize.queryOriginal()->createDiskPrefetcher(ctx->queryCodeContext(), activityId));
  18091. }
  18092. helper.setCallback(reader ? reader->queryThorDiskCallback() : cursor);
  18093. }
  18094. }
  18095. }
  18096. virtual void append(IKeySegmentMonitor *segment)
  18097. {
  18098. if (!segment->isWild())
  18099. {
  18100. if (!cursor)
  18101. cursor.setown(manager->createCursor());
  18102. cursor->append(segment);
  18103. }
  18104. }
  18105. virtual unsigned ordinality() const
  18106. {
  18107. return cursor ? cursor->ordinality() : 0;
  18108. }
  18109. virtual IKeySegmentMonitor *item(unsigned idx) const
  18110. {
  18111. return cursor ? cursor->item(idx) : 0;
  18112. }
  18113. virtual void setMergeBarrier(unsigned barrierOffset)
  18114. {
  18115. // no merging so no issue...
  18116. }
  18117. virtual void stop(bool aborting)
  18118. {
  18119. if (useRemote())
  18120. remote->onStop(aborting);
  18121. CRoxieServerActivity::stop(aborting);
  18122. }
  18123. virtual void reset()
  18124. {
  18125. if (useRemote())
  18126. {
  18127. processed = remote->processed;
  18128. remote->processed = 0;
  18129. remote->onReset();
  18130. }
  18131. varFileInfo.clear();
  18132. eof = false;
  18133. if (cursor)
  18134. cursor->reset();
  18135. deserializeSource.clearStream();
  18136. CRoxieServerActivity::reset();
  18137. }
  18138. virtual void setInput(unsigned idx, IRoxieInput *_in)
  18139. {
  18140. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  18141. }
  18142. virtual void onLimitExceeded(bool isKeyed)
  18143. {
  18144. if (traceLevel > 4)
  18145. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  18146. assertex(compoundHelper);
  18147. if (isKeyed) // MORE does this exist for diskread? should it?
  18148. {
  18149. if (helper.getFlags() & (TDRkeyedlimitskips|TDRkeyedlimitcreates))
  18150. {
  18151. if (ctx->queryDebugContext())
  18152. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  18153. throw makeLimitSkipException(true);
  18154. }
  18155. else
  18156. {
  18157. UNIMPLEMENTED;
  18158. //compoundHelper->onKeyedLimitExceeded(); Doesn't exist - though the flags do... interesting...
  18159. }
  18160. }
  18161. else
  18162. {
  18163. if (helper.getFlags() & (TDRlimitskips|TDRlimitcreates))
  18164. {
  18165. if (ctx->queryDebugContext())
  18166. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  18167. throw makeLimitSkipException(false);
  18168. }
  18169. else
  18170. compoundHelper->onLimitExceeded();
  18171. }
  18172. }
  18173. virtual const void * createLimitFailRow(bool isKeyed)
  18174. {
  18175. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  18176. IHThorSourceLimitTransformExtra *limitTransformExtra = static_cast<IHThorSourceLimitTransformExtra *>(helper.selectInterface(TAIsourcelimittransformextra_1));
  18177. assertex(limitTransformExtra);
  18178. size32_t outSize = isKeyed ? limitTransformExtra->transformOnKeyedLimitExceeded(rowBuilder) : limitTransformExtra->transformOnLimitExceeded(rowBuilder);
  18179. if (outSize)
  18180. return rowBuilder.finalizeRowClear(outSize);
  18181. return NULL;
  18182. }
  18183. };
  18184. class CRoxieServerDiskReadActivity : public CRoxieServerDiskReadBaseActivity
  18185. {
  18186. IHThorCompoundReadExtra * readHelper;
  18187. ConstPointerArray readrows;
  18188. bool readAheadDone;
  18189. unsigned readIndex;
  18190. public:
  18191. CRoxieServerDiskReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, bool _sorted, bool _maySkip, IInMemoryIndexManager *_manager)
  18192. : CRoxieServerDiskReadBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _sorted, _maySkip, _manager)
  18193. {
  18194. compoundHelper = (IHThorDiskReadArg *)&helper;
  18195. readHelper = (IHThorDiskReadArg *)&helper;
  18196. readAheadDone = false;
  18197. readIndex = 0;
  18198. }
  18199. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18200. {
  18201. rowLimit = compoundHelper->getRowLimit();
  18202. stopAfter = compoundHelper->getChooseNLimit();
  18203. CRoxieServerDiskReadBaseActivity::start(parentExtractSize, parentExtract, paused);
  18204. readAheadDone = false;
  18205. readIndex = 0;
  18206. }
  18207. virtual void reset()
  18208. {
  18209. while (readrows.isItem(readIndex))
  18210. ReleaseRoxieRow(readrows.item(readIndex++));
  18211. readrows.kill();
  18212. readAheadDone = false;
  18213. readIndex = 0;
  18214. CRoxieServerDiskReadBaseActivity::reset();
  18215. }
  18216. virtual const void *nextInGroup()
  18217. {
  18218. if (eof)
  18219. return NULL;
  18220. else if (useRemote())
  18221. return remote->nextInGroup();
  18222. else if (maySkip)
  18223. {
  18224. if (!readAheadDone)
  18225. {
  18226. unsigned preprocessed = 0;
  18227. while (!eof)
  18228. {
  18229. const void *row = _nextInGroup();
  18230. if (row)
  18231. preprocessed++;
  18232. if (preprocessed > rowLimit)
  18233. {
  18234. ReleaseRoxieRow(row);
  18235. while (readrows.isItem(readIndex))
  18236. ReleaseRoxieRow(readrows.item(readIndex++));
  18237. readrows.kill();
  18238. eof = true;
  18239. if (ctx->queryDebugContext())
  18240. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  18241. if (helper.getFlags() & TDRlimitskips)
  18242. return NULL;
  18243. else if (helper.getFlags() & TDRlimitcreates)
  18244. return createLimitFailRow(false);
  18245. else
  18246. throwUnexpected();
  18247. }
  18248. if (preprocessed > stopAfter) // MORE - bit of a strange place to check
  18249. {
  18250. eof = true;
  18251. ReleaseRoxieRow(row);
  18252. break;
  18253. }
  18254. readrows.append(row);
  18255. }
  18256. readAheadDone = true;
  18257. }
  18258. if (readrows.isItem(readIndex))
  18259. {
  18260. const void *ret = readrows.item(readIndex++);
  18261. if (ret)
  18262. processed++;
  18263. return ret;
  18264. }
  18265. else
  18266. {
  18267. eof = true;
  18268. return NULL;
  18269. }
  18270. }
  18271. else
  18272. {
  18273. const void *ret = _nextInGroup();
  18274. if (ret)
  18275. {
  18276. processed++;
  18277. if (processed > rowLimit)
  18278. {
  18279. if (traceLevel > 4)
  18280. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  18281. ReleaseRoxieRow(ret);
  18282. compoundHelper->onLimitExceeded();
  18283. throwUnexpected(); // onLimitExceeded is not supposed to return
  18284. }
  18285. if (processed > stopAfter) // MORE - bit of a strange place to check
  18286. {
  18287. eof = true;
  18288. ReleaseRoxieRow(ret);
  18289. return NULL;
  18290. }
  18291. }
  18292. return ret;
  18293. }
  18294. }
  18295. const void *_nextInGroup()
  18296. {
  18297. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  18298. unsigned transformedSize = 0;
  18299. if (isKeyed)
  18300. {
  18301. loop
  18302. {
  18303. const void *nextCandidate = cursor->nextMatch();
  18304. if (!nextCandidate)
  18305. {
  18306. eof = true;
  18307. return NULL;
  18308. }
  18309. transformedSize = readHelper->transform(rowBuilder, nextCandidate);
  18310. if (transformedSize)
  18311. break;
  18312. }
  18313. }
  18314. else // use reader...
  18315. {
  18316. assertex(reader != NULL);
  18317. loop
  18318. {
  18319. if (deserializeSource.eos())
  18320. {
  18321. eof = true;
  18322. return NULL;
  18323. }
  18324. prefetcher->readAhead(deserializeSource);
  18325. const byte *nextRec = deserializeSource.queryRow();
  18326. if (cursor && cursor->isFiltered(nextRec))
  18327. transformedSize = 0;
  18328. else
  18329. transformedSize = readHelper->transform(rowBuilder, nextRec);
  18330. deserializeSource.finishedRow();
  18331. if (transformedSize)
  18332. break;
  18333. }
  18334. }
  18335. return rowBuilder.finalizeRowClear(transformedSize);
  18336. }
  18337. };
  18338. class CRoxieServerXmlReadActivity : public CRoxieServerDiskReadBaseActivity, implements IXMLSelect
  18339. {
  18340. IHThorXmlReadArg * readHelper;
  18341. Owned<IXmlToRowTransformer> rowTransformer;
  18342. Owned<IXMLParse> xmlParser;
  18343. Owned<IColumnProvider> lastMatch;
  18344. unsigned __int64 localOffset;
  18345. public:
  18346. IMPLEMENT_IINTERFACE;
  18347. CRoxieServerXmlReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, bool _sorted, bool _maySkip, IInMemoryIndexManager *_manager)
  18348. : CRoxieServerDiskReadBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _sorted, _maySkip, _manager)
  18349. {
  18350. compoundHelper = NULL;
  18351. readHelper = (IHThorXmlReadArg *)&helper;
  18352. localOffset = 0;
  18353. }
  18354. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18355. {
  18356. rowLimit = readHelper->getRowLimit();
  18357. stopAfter = readHelper->getChooseNLimit();
  18358. CRoxieServerDiskReadBaseActivity::start(parentExtractSize, parentExtract, paused);
  18359. if (!useRemote())
  18360. {
  18361. rowTransformer.set(readHelper->queryTransformer());
  18362. assertex(reader != NULL);
  18363. OwnedRoxieString xmlIterator(readHelper->getXmlIteratorPath());
  18364. xmlParser.setown(createXMLParse(*reader->querySimpleStream(), xmlIterator, *this, (0 != (TDRxmlnoroot & readHelper->getFlags()))?ptr_noRoot:ptr_none, (readHelper->getFlags() & TDRusexmlcontents) != 0));
  18365. }
  18366. }
  18367. virtual void reset()
  18368. {
  18369. CRoxieServerDiskReadBaseActivity::reset();
  18370. rowTransformer.clear();
  18371. xmlParser.clear();
  18372. }
  18373. virtual void match(IColumnProvider &entry, offset_t startOffset, offset_t endOffset)
  18374. {
  18375. localOffset = startOffset;
  18376. lastMatch.set(&entry);
  18377. }
  18378. virtual const void *nextInGroup()
  18379. {
  18380. if (eof)
  18381. return NULL;
  18382. else if (useRemote())
  18383. return remote->nextInGroup();
  18384. assertex(xmlParser != NULL);
  18385. try
  18386. {
  18387. while (!eof)
  18388. {
  18389. //call to next() will callback on the IXmlSelect interface
  18390. bool gotNext = false;
  18391. gotNext = xmlParser->next();
  18392. if(!gotNext)
  18393. eof = true;
  18394. else if (lastMatch)
  18395. {
  18396. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  18397. unsigned sizeGot = rowTransformer->transform(rowBuilder, lastMatch, reader->queryThorDiskCallback());
  18398. lastMatch.clear();
  18399. localOffset = 0;
  18400. if (sizeGot)
  18401. {
  18402. OwnedConstRoxieRow ret = rowBuilder.finalizeRowClear(sizeGot);
  18403. if (processed > rowLimit)
  18404. {
  18405. if (traceLevel > 4)
  18406. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  18407. readHelper->onLimitExceeded();
  18408. throwUnexpected(); // onLimitExceeded is not supposed to return
  18409. }
  18410. processed++;
  18411. if (processed > stopAfter) // MORE - bit of a strange place to check
  18412. {
  18413. eof = true;
  18414. return NULL;
  18415. }
  18416. return ret.getClear();
  18417. }
  18418. }
  18419. }
  18420. return NULL;
  18421. }
  18422. catch(IException *E)
  18423. {
  18424. throw makeWrappedException(E);
  18425. }
  18426. }
  18427. };
  18428. class CRoxieServerCsvReadActivity : public CRoxieServerDiskReadBaseActivity
  18429. {
  18430. IHThorCsvReadArg *readHelper;
  18431. ICsvParameters * csvInfo;
  18432. unsigned headerLines;
  18433. unsigned maxDiskSize;
  18434. CSVSplitter csvSplitter;
  18435. unsigned __int64 localOffset;
  18436. const char *quotes;
  18437. const char *separators;
  18438. const char *terminators;
  18439. const char *escapes;
  18440. public:
  18441. CRoxieServerCsvReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  18442. unsigned _numParts, bool _isLocal, bool _sorted, bool _maySkip, IInMemoryIndexManager *_manager,
  18443. const char *_quotes, const char *_separators, const char *_terminators, const char *_escapes)
  18444. : CRoxieServerDiskReadBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _sorted, _maySkip, _manager),
  18445. quotes(_quotes), separators(_separators), terminators(_terminators), escapes(_escapes)
  18446. {
  18447. compoundHelper = NULL;
  18448. readHelper = (IHThorCsvReadArg *)&helper;
  18449. rowLimit = readHelper->getRowLimit();
  18450. stopAfter = readHelper->getChooseNLimit();
  18451. csvInfo = readHelper->queryCsvParameters();
  18452. maxDiskSize = csvInfo->queryMaxSize();
  18453. localOffset = 0;
  18454. headerLines = 0;
  18455. }
  18456. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18457. {
  18458. rowLimit = readHelper->getRowLimit();
  18459. stopAfter = readHelper->getChooseNLimit();
  18460. CRoxieServerDiskReadBaseActivity::start(parentExtractSize, parentExtract, paused);
  18461. if (!useRemote())
  18462. {
  18463. headerLines = csvInfo->queryHeaderLen();
  18464. if (headerLines && isLocal && reader->queryFilePart() != 1)
  18465. headerLines = 0; // MORE - you could argue that if SINGLE not specified, should skip from all parts. But it would be painful since we have already concatenated and no-one else does...
  18466. if (!eof)
  18467. {
  18468. if (varFileInfo)
  18469. {
  18470. const IPropertyTree *options = varFileInfo->queryProperties();
  18471. if (options)
  18472. {
  18473. quotes = options->queryProp("@csvQuote");
  18474. separators = options->queryProp("@csvSeparate");
  18475. terminators = options->queryProp("@csvTerminate");
  18476. escapes = options->queryProp("@csvEscape");
  18477. }
  18478. }
  18479. csvSplitter.init(readHelper->getMaxColumns(), csvInfo, quotes, separators, terminators, escapes);
  18480. }
  18481. }
  18482. }
  18483. virtual const void *nextInGroup()
  18484. {
  18485. if (eof)
  18486. return NULL;
  18487. else if (useRemote())
  18488. return remote->nextInGroup();
  18489. try
  18490. {
  18491. while (!eof)
  18492. {
  18493. if (reader->eos())
  18494. {
  18495. eof = true;
  18496. break;
  18497. }
  18498. // MORE - there are rumours of a csvSplitter that operates on a stream... if/when it exists, this should use it
  18499. size32_t rowSize = 4096; // MORE - make configurable
  18500. size32_t maxRowSize = 10*1024*1024; // MORE - make configurable
  18501. size32_t thisLineLength;
  18502. loop
  18503. {
  18504. size32_t avail;
  18505. const void *peek = reader->peek(rowSize, avail);
  18506. thisLineLength = csvSplitter.splitLine(avail, (const byte *)peek);
  18507. if (thisLineLength < rowSize || avail < rowSize)
  18508. break;
  18509. if (rowSize == maxRowSize)
  18510. throw MakeStringException(0, "Row too big");
  18511. if (rowSize >= maxRowSize/2)
  18512. rowSize = maxRowSize;
  18513. else
  18514. rowSize += rowSize;
  18515. }
  18516. if (headerLines)
  18517. {
  18518. headerLines--;
  18519. reader->skip(thisLineLength);
  18520. }
  18521. else
  18522. {
  18523. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  18524. unsigned transformedSize = readHelper->transform(rowBuilder, csvSplitter.queryLengths(), (const char * *)csvSplitter.queryData());
  18525. reader->skip(thisLineLength);
  18526. if (transformedSize)
  18527. {
  18528. OwnedConstRoxieRow ret = rowBuilder.finalizeRowClear(transformedSize);
  18529. if (processed > rowLimit)
  18530. {
  18531. readHelper->onLimitExceeded();
  18532. throwUnexpected(); // onLimitExceeded is not supposed to return
  18533. }
  18534. processed++;
  18535. if (processed > stopAfter) // MORE - bit of a strange place to check
  18536. {
  18537. eof = true;
  18538. return NULL;
  18539. }
  18540. return ret.getClear();
  18541. }
  18542. }
  18543. }
  18544. return NULL;
  18545. }
  18546. catch(IException *E)
  18547. {
  18548. throw makeWrappedException(E);
  18549. }
  18550. }
  18551. virtual void reset()
  18552. {
  18553. CRoxieServerDiskReadBaseActivity::reset();
  18554. csvSplitter.reset();
  18555. localOffset = 0;
  18556. }
  18557. virtual unsigned __int64 getFilePosition(const void * row)
  18558. {
  18559. UNIMPLEMENTED; // we know offset in the reader but not sure it helps us much
  18560. }
  18561. virtual unsigned __int64 getLocalFilePosition(const void * row)
  18562. {
  18563. UNIMPLEMENTED;
  18564. }
  18565. };
  18566. class CRoxieServerDiskNormalizeActivity : public CRoxieServerDiskReadBaseActivity
  18567. {
  18568. IHThorDiskNormalizeArg *normalizeHelper;
  18569. bool firstPending;
  18570. public:
  18571. CRoxieServerDiskNormalizeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, bool _sorted, IInMemoryIndexManager *_manager)
  18572. : CRoxieServerDiskReadBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _sorted, false, _manager)
  18573. {
  18574. compoundHelper = (IHThorDiskNormalizeArg *)&helper;
  18575. normalizeHelper = (IHThorDiskNormalizeArg *)&helper;
  18576. firstPending = true;
  18577. }
  18578. virtual void reset()
  18579. {
  18580. firstPending = true;
  18581. CRoxieServerDiskReadBaseActivity::reset();
  18582. }
  18583. virtual const void *nextInGroup()
  18584. {
  18585. if (eof)
  18586. return NULL;
  18587. else if (useRemote())
  18588. return remote->nextInGroup();
  18589. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  18590. unsigned transformedSize = 0;
  18591. if (isKeyed)
  18592. {
  18593. loop
  18594. {
  18595. while (firstPending)
  18596. {
  18597. const void *nextCandidate = cursor->nextMatch();
  18598. if (!nextCandidate)
  18599. {
  18600. eof = true;
  18601. return NULL;
  18602. }
  18603. if (normalizeHelper->first(nextCandidate))
  18604. {
  18605. firstPending = false;
  18606. break;
  18607. }
  18608. }
  18609. transformedSize = normalizeHelper->transform(rowBuilder);
  18610. firstPending = !normalizeHelper->next();
  18611. if (transformedSize)
  18612. break;
  18613. }
  18614. }
  18615. else
  18616. {
  18617. assertex(reader != NULL);
  18618. loop
  18619. {
  18620. while (firstPending)
  18621. {
  18622. if (deserializeSource.eos())
  18623. {
  18624. eof = true;
  18625. return NULL;
  18626. }
  18627. prefetcher->readAhead(deserializeSource);
  18628. const byte *nextRec = deserializeSource.queryRow();
  18629. if (!cursor || !cursor->isFiltered(nextRec))
  18630. {
  18631. if (normalizeHelper->first(nextRec))
  18632. firstPending = false;
  18633. }
  18634. deserializeSource.finishedRow();
  18635. }
  18636. transformedSize = normalizeHelper->transform(rowBuilder);
  18637. firstPending = !normalizeHelper->next();
  18638. if (transformedSize)
  18639. break;
  18640. }
  18641. }
  18642. OwnedConstRoxieRow recBuffer = rowBuilder.finalizeRowClear(transformedSize);
  18643. processed++;
  18644. if (processed > rowLimit)
  18645. {
  18646. compoundHelper->onLimitExceeded();
  18647. throwUnexpected(); // onLimitExceeded is not supposed to return
  18648. }
  18649. if (processed > stopAfter) // MORE - bit of a strange place to check
  18650. {
  18651. eof = true;
  18652. return NULL;
  18653. }
  18654. return recBuffer.getClear();
  18655. }
  18656. };
  18657. class CRoxieServerDiskAggregateBaseActivity : public CRoxieServerDiskReadBaseActivity
  18658. {
  18659. protected:
  18660. bool done;
  18661. public:
  18662. CRoxieServerDiskAggregateBaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, IInMemoryIndexManager *_manager)
  18663. : CRoxieServerDiskReadBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, false, false, _manager),
  18664. done(false)
  18665. {
  18666. }
  18667. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18668. {
  18669. done = false;
  18670. CRoxieServerDiskReadBaseActivity::start(parentExtractSize, parentExtract, paused);
  18671. }
  18672. virtual IRoxieInput *queryOutput(unsigned idx)
  18673. {
  18674. if (idx==(unsigned)-1)
  18675. idx = 0;
  18676. return idx ? NULL: this;
  18677. }
  18678. };
  18679. class CRoxieServerDiskCountActivity : public CRoxieServerDiskAggregateBaseActivity
  18680. {
  18681. IHThorDiskCountArg & countHelper;
  18682. unsigned __int64 choosenLimit;
  18683. IHThorSourceCountLimit *limitHelper;
  18684. unsigned __int64 getSkippedCount()
  18685. {
  18686. unsigned flags = countHelper.getFlags();
  18687. if (flags & TDRlimitskips)
  18688. return 0;
  18689. else if (flags & TDRlimitcreates)
  18690. return 1;
  18691. else
  18692. {
  18693. assertex(limitHelper);
  18694. if (traceLevel > 4)
  18695. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  18696. limitHelper->onLimitExceeded();
  18697. throwUnexpected(); // onLimitExceeded should always throw exception
  18698. }
  18699. }
  18700. public:
  18701. CRoxieServerDiskCountActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, IInMemoryIndexManager *_manager)
  18702. : CRoxieServerDiskAggregateBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _manager),
  18703. countHelper((IHThorDiskCountArg &)basehelper)
  18704. {
  18705. limitHelper = static_cast<IHThorSourceCountLimit *>(basehelper.selectInterface(TAIsourcecountlimit_1));
  18706. choosenLimit = 0;
  18707. }
  18708. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18709. {
  18710. choosenLimit = countHelper.getChooseNLimit();
  18711. if (limitHelper)
  18712. {
  18713. rowLimit = limitHelper->getRowLimit();
  18714. // keyedLimit = limitHelper->getKeyedLimit(); // more - should there be one?
  18715. }
  18716. CRoxieServerDiskAggregateBaseActivity::start(parentExtractSize, parentExtract, paused);
  18717. }
  18718. virtual const void *nextInGroup()
  18719. {
  18720. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  18721. if (done) return NULL;
  18722. done = true;
  18723. unsigned __int64 totalCount = 0;
  18724. if (helper.canMatchAny() && !eof)
  18725. {
  18726. if (useRemote())
  18727. {
  18728. loop
  18729. {
  18730. const void * next = remote->nextInGroup();
  18731. if (!next)
  18732. break;
  18733. if (meta.getFixedSize() == 1)
  18734. totalCount += *(byte *)next;
  18735. else
  18736. totalCount += *(unsigned __int64 *)next;
  18737. ReleaseRoxieRow(next);
  18738. if (totalCount > rowLimit)
  18739. {
  18740. totalCount = getSkippedCount();
  18741. break;
  18742. }
  18743. else if (totalCount >= choosenLimit)
  18744. {
  18745. totalCount = choosenLimit;
  18746. break;
  18747. }
  18748. }
  18749. }
  18750. else
  18751. {
  18752. if (isKeyed)
  18753. {
  18754. loop
  18755. {
  18756. const void *nextCandidate = cursor->nextMatch();
  18757. if (!nextCandidate)
  18758. break;
  18759. totalCount += countHelper.numValid(nextCandidate);
  18760. if (totalCount > rowLimit)
  18761. {
  18762. totalCount = getSkippedCount();
  18763. break;
  18764. }
  18765. else if (totalCount >= choosenLimit)
  18766. {
  18767. totalCount = choosenLimit;
  18768. break;
  18769. }
  18770. }
  18771. }
  18772. else
  18773. {
  18774. assertex(reader != NULL);
  18775. while (!deserializeSource.eos())
  18776. {
  18777. prefetcher->readAhead(deserializeSource);
  18778. const byte *nextRec = deserializeSource.queryRow();
  18779. if (!cursor || !cursor->isFiltered(nextRec))
  18780. {
  18781. totalCount += countHelper.numValid(nextRec);
  18782. }
  18783. deserializeSource.finishedRow();
  18784. if (totalCount > rowLimit)
  18785. {
  18786. totalCount = getSkippedCount();
  18787. break;
  18788. }
  18789. else if (totalCount >= choosenLimit)
  18790. {
  18791. totalCount = choosenLimit;
  18792. break;
  18793. }
  18794. }
  18795. }
  18796. }
  18797. }
  18798. size32_t rowSize = meta.getFixedSize();
  18799. void * result = rowAllocator->createRow();
  18800. if (rowSize == 1)
  18801. *(byte *)result = (byte)totalCount;
  18802. else
  18803. {
  18804. assertex(rowSize == sizeof(unsigned __int64));
  18805. *(unsigned __int64 *)result = totalCount;
  18806. }
  18807. return rowAllocator->finalizeRow(rowSize, result, rowSize);
  18808. }
  18809. };
  18810. class CRoxieServerDiskAggregateActivity : public CRoxieServerDiskAggregateBaseActivity
  18811. {
  18812. IHThorCompoundAggregateExtra & aggregateHelper;
  18813. public:
  18814. CRoxieServerDiskAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  18815. unsigned _numParts, bool _isLocal, IInMemoryIndexManager *_manager)
  18816. : CRoxieServerDiskAggregateBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _manager),
  18817. aggregateHelper((IHThorDiskAggregateArg &)basehelper)
  18818. {
  18819. }
  18820. const void * gatherMerged()
  18821. {
  18822. RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
  18823. size32_t finalSize = 0;
  18824. if (useRemote())
  18825. {
  18826. const void * firstRow = remote->nextInGroup();
  18827. if (!firstRow)
  18828. {
  18829. rowBuilder.ensureRow();
  18830. finalSize = aggregateHelper.clearAggregate(rowBuilder);
  18831. }
  18832. else
  18833. {
  18834. // NOTE need to clone this because going to modify below, could special case 1 row only
  18835. finalSize = cloneRow(rowBuilder, firstRow, meta);
  18836. ReleaseRoxieRow(firstRow);
  18837. }
  18838. loop
  18839. {
  18840. const void * next = remote->nextInGroup();
  18841. if (!next)
  18842. break;
  18843. finalSize = aggregateHelper.mergeAggregate(rowBuilder, next);
  18844. ReleaseRoxieRow(next);
  18845. }
  18846. }
  18847. else
  18848. {
  18849. aggregateHelper.clearAggregate(rowBuilder);
  18850. if (helper.canMatchAny() && !eof)
  18851. {
  18852. if (isKeyed)
  18853. {
  18854. loop
  18855. {
  18856. const void *next = cursor->nextMatch();
  18857. if (!next)
  18858. break;
  18859. aggregateHelper.processRow(rowBuilder, next);
  18860. }
  18861. }
  18862. else
  18863. {
  18864. assertex(reader != NULL);
  18865. while (!deserializeSource.eos())
  18866. {
  18867. prefetcher->readAhead(deserializeSource);
  18868. const byte *nextRec = deserializeSource.queryRow();
  18869. if (!cursor || !cursor->isFiltered(nextRec))
  18870. {
  18871. aggregateHelper.processRow(rowBuilder, nextRec);
  18872. }
  18873. deserializeSource.finishedRow();
  18874. }
  18875. }
  18876. }
  18877. finalSize = meta.getRecordSize(rowBuilder.getSelf());
  18878. }
  18879. return rowBuilder.finalizeRowClear(finalSize);
  18880. }
  18881. virtual const void *nextInGroup()
  18882. {
  18883. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  18884. if (done) return NULL;
  18885. const void * ret = gatherMerged();
  18886. done = true;
  18887. return ret;
  18888. }
  18889. };
  18890. class CRoxieServerDiskGroupAggregateActivity : public CRoxieServerDiskAggregateBaseActivity
  18891. {
  18892. IHThorDiskGroupAggregateArg & aggregateHelper;
  18893. RowAggregator resultAggregator;
  18894. bool gathered;
  18895. public:
  18896. IMPLEMENT_IINTERFACE;
  18897. CRoxieServerDiskGroupAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, IInMemoryIndexManager *_manager)
  18898. : CRoxieServerDiskAggregateBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _manager),
  18899. aggregateHelper((IHThorDiskGroupAggregateArg &)basehelper),
  18900. resultAggregator(aggregateHelper, aggregateHelper),
  18901. gathered(false)
  18902. {
  18903. }
  18904. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18905. {
  18906. gathered= false;
  18907. CRoxieServerDiskAggregateBaseActivity::start(parentExtractSize, parentExtract, paused);
  18908. resultAggregator.start(rowAllocator);
  18909. }
  18910. virtual void reset()
  18911. {
  18912. resultAggregator.reset();
  18913. CRoxieServerDiskAggregateBaseActivity::reset();
  18914. }
  18915. void gatherMerged()
  18916. {
  18917. if (useRemote())
  18918. {
  18919. loop
  18920. {
  18921. const void * next = remote->nextInGroup();
  18922. if (!next)
  18923. break;
  18924. resultAggregator.mergeElement(next);
  18925. ReleaseRoxieRow(next);
  18926. }
  18927. }
  18928. else
  18929. {
  18930. if (helper.canMatchAny() && !eof)
  18931. {
  18932. Owned<IInMemoryFileProcessor> processor = isKeyed ?
  18933. createKeyedGroupAggregateRecordProcessor(cursor, resultAggregator, aggregateHelper) :
  18934. createUnkeyedGroupAggregateRecordProcessor(cursor, resultAggregator, aggregateHelper, manager->createReader(0, 0, 1),
  18935. ctx->queryCodeContext(), activityId);
  18936. processor->doQuery(NULL, 0, 0, 0);
  18937. }
  18938. }
  18939. gathered = true;
  18940. }
  18941. virtual const void *nextInGroup()
  18942. {
  18943. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  18944. if (done)
  18945. return NULL;
  18946. if (!gathered)
  18947. gatherMerged();
  18948. Owned<AggregateRowBuilder> next = resultAggregator.nextResult();
  18949. if (next)
  18950. {
  18951. processed++;
  18952. return next->finalizeRowClear();
  18953. }
  18954. done = true;
  18955. return NULL;
  18956. }
  18957. };
  18958. class CRoxieServerDiskReadActivityFactory : public CRoxieServerActivityFactory
  18959. {
  18960. public:
  18961. RemoteActivityId remoteId;
  18962. bool isLocal;
  18963. bool sorted;
  18964. bool maySkip;
  18965. bool variableFileName;
  18966. Owned<IFilePartMap> map;
  18967. Owned<IFileIOArray> files;
  18968. Owned<IInMemoryIndexManager> manager;
  18969. Owned<const IResolvedFile> datafile;
  18970. const char *quotes;
  18971. const char *separators;
  18972. const char *terminators;
  18973. const char *escapes;
  18974. CRoxieServerDiskReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  18975. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), remoteId(_remoteId)
  18976. {
  18977. isLocal = _graphNode.getPropBool("att[@name='local']/@value") && queryFactory.queryChannel()!=0;
  18978. Owned<IHThorDiskReadBaseArg> helper = (IHThorDiskReadBaseArg *) helperFactory();
  18979. sorted = (helper->getFlags() & TDRunsorted) == 0;
  18980. variableFileName = allFilesDynamic || _queryFactory.isDynamic() || ((helper->getFlags() & (TDXvarfilename|TDXdynamicfilename)) != 0);
  18981. maySkip = (helper->getFlags() & (TDRkeyedlimitskips|TDRkeyedlimitcreates|TDRlimitskips|TDRlimitcreates)) != 0;
  18982. quotes = separators = terminators = escapes = NULL;
  18983. if (!variableFileName)
  18984. {
  18985. bool isOpt = (helper->getFlags() & TDRoptional) != 0;
  18986. OwnedRoxieString fileName(helper->getFileName());
  18987. datafile.setown(_queryFactory.queryPackage().lookupFileName(fileName, isOpt, true, true, _queryFactory.queryWorkUnit()));
  18988. if (datafile)
  18989. map.setown(datafile->getFileMap());
  18990. bool isSimple = (map && map->getNumParts()==1 && !_queryFactory.getDebugValueBool("disableLocalOptimizations", false));
  18991. if (isLocal || isSimple)
  18992. {
  18993. if (datafile)
  18994. {
  18995. unsigned channel = isLocal ? queryFactory.queryChannel() : 0;
  18996. files.setown(datafile->getIFileIOArray(isOpt, channel));
  18997. manager.setown(datafile->getIndexManager(isOpt, channel, files, helper->queryDiskRecordSize(), _graphNode.getPropBool("att[@name=\"preload\"]/@value", false), _graphNode.getPropInt("att[@name=\"_preloadSize\"]/@value", 0)));
  18998. const IPropertyTree *options = datafile->queryProperties();
  18999. if (options)
  19000. {
  19001. quotes = options->queryProp("@csvQuote");
  19002. separators = options->queryProp("@csvSeparate");
  19003. terminators = options->queryProp("@csvTerminate");
  19004. escapes = options->queryProp("@csvEscape");
  19005. }
  19006. }
  19007. else
  19008. manager.setown(getEmptyIndexManager());
  19009. }
  19010. }
  19011. }
  19012. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  19013. {
  19014. unsigned numParts = map ? map->getNumParts() : 0;
  19015. switch (kind)
  19016. {
  19017. case TAKcsvread:
  19018. return new CRoxieServerCsvReadActivity(this, _probeManager, remoteId, numParts, isLocal, sorted, maySkip, manager,
  19019. quotes, separators, terminators, escapes);
  19020. case TAKxmlread:
  19021. return new CRoxieServerXmlReadActivity(this, _probeManager, remoteId, numParts, isLocal, sorted, maySkip, manager);
  19022. case TAKdiskread:
  19023. return new CRoxieServerDiskReadActivity(this, _probeManager, remoteId, numParts, isLocal, sorted, maySkip, manager);
  19024. case TAKdisknormalize:
  19025. return new CRoxieServerDiskNormalizeActivity(this, _probeManager, remoteId, numParts, isLocal, sorted, manager);
  19026. case TAKdiskcount:
  19027. return new CRoxieServerDiskCountActivity(this, _probeManager, remoteId, numParts, isLocal, manager);
  19028. case TAKdiskaggregate:
  19029. return new CRoxieServerDiskAggregateActivity(this, _probeManager, remoteId, numParts, isLocal, manager);
  19030. case TAKdiskgroupaggregate:
  19031. return new CRoxieServerDiskGroupAggregateActivity(this, _probeManager, remoteId, numParts, isLocal, manager);
  19032. }
  19033. throwUnexpected();
  19034. }
  19035. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  19036. {
  19037. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for %s activity", getActivityText(kind));
  19038. }
  19039. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  19040. {
  19041. if (datafile)
  19042. addXrefFileInfo(reply, datafile);
  19043. }
  19044. };
  19045. IRoxieServerActivityFactory *createRoxieServerDiskReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  19046. {
  19047. return new CRoxieServerDiskReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
  19048. }
  19049. //=================================================================================
  19050. class CRoxieServerIndexActivity : public CRoxieServerActivity, implements IRoxieServerErrorHandler
  19051. {
  19052. protected:
  19053. IHThorIndexReadBaseArg &indexHelper;
  19054. IHThorSteppedSourceExtra * steppedExtra;
  19055. Linked<IKeyArray> keySet;
  19056. Linked<TranslatorArray> translators;
  19057. CSkippableRemoteResultAdaptor remote;
  19058. CIndexTransformCallback callback;
  19059. bool sorted;
  19060. bool variableFileName;
  19061. bool variableInfoPending;
  19062. bool isOpt;
  19063. bool isLocal;
  19064. unsigned __int64 rowLimit;
  19065. unsigned __int64 keyedLimit;
  19066. unsigned __int64 choosenLimit;
  19067. unsigned accepted;
  19068. unsigned rejected;
  19069. unsigned seekGEOffset;
  19070. Owned<IKeyManager> tlk;
  19071. Owned<const IResolvedFile> varFileInfo;
  19072. const RemoteActivityId &remoteId;
  19073. void setVariableFileInfo()
  19074. {
  19075. OwnedRoxieString indexName(indexHelper.getFileName());
  19076. varFileInfo.setown(resolveLFN(indexName, isOpt));
  19077. if (varFileInfo)
  19078. {
  19079. translators.setown(new TranslatorArray) ;
  19080. keySet.setown(varFileInfo->getKeyArray(factory->queryActivityMeta(), translators, isOpt, isLocal ? factory->queryQueryFactory().queryChannel() : 0, factory->queryQueryFactory().getEnableFieldTranslation()));
  19081. }
  19082. variableInfoPending = false;
  19083. }
  19084. public:
  19085. CRoxieServerIndexActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  19086. IKeyArray * _keySet, TranslatorArray *_translators, bool _sorted, bool _isLocal, bool _maySkip)
  19087. : CRoxieServerActivity(_factory, _probeManager),
  19088. keySet(_keySet),
  19089. translators(_translators),
  19090. indexHelper((IHThorIndexReadBaseArg &)basehelper),
  19091. remote(_remoteId, meta.queryOriginal(), indexHelper, *this, _sorted, false, _maySkip),
  19092. remoteId(_remoteId),
  19093. sorted(_sorted),
  19094. isLocal(_isLocal)
  19095. {
  19096. indexHelper.setCallback(&callback);
  19097. steppedExtra = static_cast<IHThorSteppedSourceExtra *>(indexHelper.selectInterface(TAIsteppedsourceextra_1));
  19098. variableFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((indexHelper.getFlags() & (TIRvarfilename|TIRdynamicfilename)) != 0);
  19099. variableInfoPending = false;
  19100. isOpt = (indexHelper.getFlags() & TIRoptional) != 0;
  19101. seekGEOffset = 0;
  19102. // started = false;
  19103. rejected = accepted = 0;
  19104. rowLimit = choosenLimit = keyedLimit = 0;
  19105. }
  19106. virtual const IResolvedFile *queryVarFileInfo() const
  19107. {
  19108. return varFileInfo;
  19109. }
  19110. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  19111. {
  19112. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  19113. remote.onCreate(this, this, _ctx, _colocalParent);
  19114. }
  19115. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  19116. {
  19117. accepted = 0;
  19118. rejected = 0;
  19119. rowLimit = (unsigned __int64) -1;
  19120. keyedLimit = (unsigned __int64 ) -1;
  19121. choosenLimit = I64C(0x7fffffffffffffff);
  19122. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  19123. remote.onStart(parentExtractSize, parentExtract);
  19124. variableInfoPending = variableFileName;
  19125. }
  19126. void processAllKeys()
  19127. {
  19128. try
  19129. {
  19130. if (indexHelper.canMatchAny())
  19131. {
  19132. if (variableInfoPending)
  19133. setVariableFileInfo();
  19134. remote.setLimits(rowLimit, keyedLimit, choosenLimit);
  19135. if (keySet)
  19136. {
  19137. // MORE - this recreates the segmonitors per part but not per fileno (which is a little backwards).
  19138. // With soft layout support may need to recreate per fileno too (i.e. different keys in a superkey have different layout) but never per partno
  19139. // However order is probably better to iterate fileno's inside partnos
  19140. // MORE - also not properly supporting STEPPED I fear.
  19141. // A superkey that mixes single and multipart or tlk and roroot keys might be hard
  19142. for (unsigned partNo = 0; partNo < keySet->length(); partNo++)
  19143. {
  19144. IKeyIndexBase *thisBase = keySet->queryKeyPart(partNo);
  19145. if (thisBase)
  19146. {
  19147. unsigned fileNo = 0;
  19148. IKeyIndex *thisKey = thisBase->queryPart(fileNo);
  19149. if (!thisKey->isTopLevelKey())
  19150. {
  19151. if (keyedLimit != (unsigned __int64) -1)
  19152. {
  19153. if ((indexHelper.getFlags() & TIRcountkeyedlimit) != 0)
  19154. {
  19155. Owned<IKeyManager> countKey;
  19156. countKey.setown(createKeyManager(thisKey, 0, this));
  19157. countKey->setLayoutTranslator(translators->item(fileNo));
  19158. createSegmentMonitors(countKey);
  19159. unsigned __int64 count = countKey->checkCount(keyedLimit);
  19160. if (count > keyedLimit)
  19161. {
  19162. if (traceLevel > 4)
  19163. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  19164. onLimitExceeded(true);
  19165. }
  19166. }
  19167. }
  19168. }
  19169. if (seekGEOffset && !thisKey->isTopLevelKey())
  19170. {
  19171. tlk.setown(createSingleKeyMerger(thisKey, 0, seekGEOffset, this));
  19172. }
  19173. else
  19174. {
  19175. tlk.setown(createKeyManager(thisKey, 0, this));
  19176. tlk->setLayoutTranslator(translators->item(fileNo));
  19177. }
  19178. createSegmentMonitors(tlk);
  19179. if (queryTraceLevel() > 3 || ctx->queryProbeManager())
  19180. {
  19181. StringBuffer out;
  19182. printKeyedValues(out, tlk, indexHelper.queryDiskRecordSize());
  19183. CTXLOG("Using filter %s", out.str());
  19184. if (ctx->queryProbeManager())
  19185. ctx->queryProbeManager()->setNodeProperty(this, "filter", out.str());
  19186. }
  19187. tlk->reset();
  19188. loop // for each file part
  19189. {
  19190. //block for TransformCallbackAssociation
  19191. {
  19192. TransformCallbackAssociation associate(callback, tlk);
  19193. if (thisKey->isTopLevelKey())
  19194. {
  19195. if (thisKey->isFullySorted())
  19196. {
  19197. while (tlk->lookup(false))
  19198. {
  19199. unsigned slavePart = (unsigned) tlk->queryFpos();
  19200. if (slavePart)
  19201. {
  19202. accepted++;
  19203. remote.getMem(slavePart, fileNo, 0); // the cached context is all we need to send
  19204. if (sorted && numChannels>1)
  19205. remote.flush(); // don't combine parts if we need result sorted, except on a 1-way
  19206. }
  19207. }
  19208. }
  19209. else
  19210. {
  19211. // MORE - we could check whether there are any matching parts if we wanted.
  19212. // If people are in the habit of sending null values that would be worthwhile
  19213. remote.getMem(0, fileNo, 0);
  19214. }
  19215. }
  19216. else
  19217. {
  19218. if (processSingleKey(thisKey, translators->item(fileNo)))
  19219. break;
  19220. }
  19221. }
  19222. if (++fileNo < thisBase->numParts())
  19223. {
  19224. thisKey = thisBase->queryPart(fileNo);
  19225. tlk->setKey(thisKey);
  19226. tlk->setLayoutTranslator(translators->item(fileNo));
  19227. tlk->reset();
  19228. }
  19229. else
  19230. break;
  19231. }
  19232. tlk->releaseSegmentMonitors();
  19233. tlk->setKey(NULL);
  19234. }
  19235. }
  19236. }
  19237. }
  19238. remote.flush();
  19239. remote.senddone();
  19240. }
  19241. catch (IException *E)
  19242. {
  19243. remote.setException(E);
  19244. }
  19245. }
  19246. virtual void createSegmentMonitors(IKeyManager *key)
  19247. {
  19248. indexHelper.createSegmentMonitors(key);
  19249. key->finishSegmentMonitors();
  19250. }
  19251. virtual bool processSingleKey(IKeyIndex *key, IRecordLayoutTranslator * trans) = 0;
  19252. virtual void reset()
  19253. {
  19254. if (accepted)
  19255. noteStatistic(STATS_ACCEPTED, accepted, 1);
  19256. if (rejected)
  19257. noteStatistic(STATS_REJECTED, rejected, 1);
  19258. remote.onReset();
  19259. CRoxieServerActivity::reset();
  19260. if (varFileInfo)
  19261. {
  19262. keySet.clear();
  19263. varFileInfo.clear();
  19264. }
  19265. variableInfoPending = false;
  19266. }
  19267. virtual void stop(bool aborting)
  19268. {
  19269. remote.onStop(aborting);
  19270. CRoxieServerActivity::stop(aborting);
  19271. }
  19272. virtual void setInput(unsigned idx, IRoxieInput *_in)
  19273. {
  19274. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  19275. }
  19276. };
  19277. class CRoxieServerIndexReadBaseActivity : public CRoxieServerIndexActivity
  19278. {
  19279. IHThorSourceLimitTransformExtra * limitTransformExtra;
  19280. public:
  19281. CRoxieServerIndexReadBaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  19282. IKeyArray * _keySet, TranslatorArray *_translators, bool _sorted, bool _isLocal, bool _maySkip)
  19283. : CRoxieServerIndexActivity(_factory, _probeManager, _remoteId, _keySet, _translators, _sorted, _isLocal, _maySkip)
  19284. {
  19285. limitTransformExtra = static_cast<IHThorSourceLimitTransformExtra *>(indexHelper.selectInterface(TAIsourcelimittransformextra_1));
  19286. }
  19287. virtual void reset()
  19288. {
  19289. remote.onReset();
  19290. CRoxieServerIndexActivity::reset();
  19291. }
  19292. virtual const void *nextInGroup()
  19293. {
  19294. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  19295. try
  19296. {
  19297. const void *ret = remote.nextInGroup();
  19298. if (ret)
  19299. processed++;
  19300. return ret;
  19301. }
  19302. catch (IException *E)
  19303. {
  19304. throw makeWrappedException(E);
  19305. }
  19306. }
  19307. protected:
  19308. virtual const void * createLimitFailRow(bool isKeyed)
  19309. {
  19310. createRowAllocator();
  19311. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  19312. size32_t outSize = isKeyed ? limitTransformExtra->transformOnKeyedLimitExceeded(rowBuilder) : limitTransformExtra->transformOnLimitExceeded(rowBuilder);
  19313. if (outSize)
  19314. return rowBuilder.finalizeRowClear(outSize);
  19315. return NULL;
  19316. }
  19317. };
  19318. class CRoxieServerIndexReadActivity : public CRoxieServerIndexReadBaseActivity, implements IIndexReadActivityInfo
  19319. {
  19320. protected:
  19321. IHThorCompoundReadExtra & readHelper;
  19322. ISteppingMeta *rawMeta;
  19323. CSteppingMeta steppingMeta;
  19324. unsigned * seekSizes;
  19325. bool optimizeSteppedPostFilter;
  19326. ISteppingMeta * projectedMeta;
  19327. unsigned maxSeekLookahead;
  19328. public:
  19329. CRoxieServerIndexReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  19330. IKeyArray * _keySet, TranslatorArray *_translators, bool _sorted, bool _isLocal, bool _maySkip, unsigned _maxSeekLookahead)
  19331. : CRoxieServerIndexReadBaseActivity(_factory, _probeManager, _remoteId, _keySet, _translators, _sorted, _isLocal, _maySkip),
  19332. readHelper((IHThorIndexReadArg &)basehelper)
  19333. {
  19334. rawMeta = readHelper.queryRawSteppingMeta();
  19335. unsigned flags = indexHelper.getFlags();
  19336. optimizeSteppedPostFilter = (flags & TIRunfilteredtransform) != 0;
  19337. seekSizes = NULL;
  19338. maxSeekLookahead = _maxSeekLookahead;
  19339. if (rawMeta)
  19340. {
  19341. const CFieldOffsetSize * fields = rawMeta->queryFields();
  19342. unsigned maxFields = rawMeta->getNumFields();
  19343. seekGEOffset = fields[0].offset;
  19344. seekSizes = new unsigned[maxFields];
  19345. seekSizes[0] = fields[0].size;
  19346. for (unsigned i=1; i < maxFields; i++)
  19347. seekSizes[i] = seekSizes[i-1] + fields[i].size;
  19348. projectedMeta = readHelper.queryProjectedSteppingMeta();
  19349. ISteppingMeta *useMeta = projectedMeta ? projectedMeta : rawMeta;
  19350. remote.setMergeInfo(useMeta); // also need to consider superfile case where there is a mix of multiway and singleparts.. ?
  19351. bool hasPostFilter = readHelper.transformMayFilter() && optimizeSteppedPostFilter;
  19352. steppingMeta.init(useMeta, hasPostFilter);
  19353. }
  19354. }
  19355. ~CRoxieServerIndexReadActivity()
  19356. {
  19357. delete [] seekSizes;
  19358. }
  19359. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  19360. {
  19361. CRoxieServerIndexReadBaseActivity::start(parentExtractSize, parentExtract, paused);
  19362. steppingMeta.setDistributed();
  19363. if (steppedExtra)
  19364. steppingMeta.setExtra(steppedExtra);
  19365. rowLimit = readHelper.getRowLimit();
  19366. keyedLimit = readHelper.getKeyedLimit();
  19367. choosenLimit = readHelper.getChooseNLimit();
  19368. if (!paused)
  19369. processAllKeys();
  19370. }
  19371. class LazyLocalKeyReader : public CInterface, implements IMessageResult, implements IMessageUnpackCursor
  19372. {
  19373. public:
  19374. IMPLEMENT_IINTERFACE;
  19375. virtual IMessageUnpackCursor *getCursor(roxiemem::IRowManager *rowMgr) const
  19376. {
  19377. Link();
  19378. return const_cast<LazyLocalKeyReader*> (this);
  19379. }
  19380. virtual const void *getMessageHeader(unsigned &length) const
  19381. {
  19382. length = 0;
  19383. return NULL;
  19384. }
  19385. virtual const void *getMessageMetadata(unsigned &length) const
  19386. {
  19387. length = 0;
  19388. return NULL;
  19389. }
  19390. virtual void discard() const
  19391. {
  19392. // nothing to do.
  19393. }
  19394. unsigned keyedCount;
  19395. unsigned matched;
  19396. bool EOFseen;
  19397. Owned<IKeyIndexSet> keySet;
  19398. Owned<IKeyManager> tlk;
  19399. CRoxieServerIndexReadActivity &owner;
  19400. LazyLocalKeyReader(CRoxieServerIndexReadActivity &_owner, IKeyIndex *key, IRecordLayoutTranslator * trans)
  19401. : owner(_owner)
  19402. {
  19403. keyedCount = 0;
  19404. matched = 0;
  19405. EOFseen = false;
  19406. keySet.setown(createKeyIndexSet());
  19407. keySet->addIndex(LINK(key));
  19408. if (owner.seekGEOffset)
  19409. tlk.setown(createKeyMerger(keySet, 0, owner.seekGEOffset, &owner));
  19410. else
  19411. tlk.setown(createKeyManager(keySet->queryPart(0), 0, &owner));
  19412. tlk->setLayoutTranslator(trans);
  19413. owner.indexHelper.createSegmentMonitors(tlk);
  19414. tlk->finishSegmentMonitors();
  19415. tlk->reset();
  19416. }
  19417. virtual const void *getNext(int length)
  19418. {
  19419. TransformCallbackAssociation associate(owner.callback, tlk);
  19420. while (tlk->lookup(true))
  19421. {
  19422. keyedCount++;
  19423. if (keyedCount > owner.keyedLimit)
  19424. {
  19425. owner.onLimitExceeded(true); // Should throw exception
  19426. throwUnexpected();
  19427. }
  19428. size32_t transformedSize;
  19429. RtlDynamicRowBuilder rowBuilder(owner.rowAllocator);
  19430. byte const * keyRow = tlk->queryKeyBuffer(owner.callback.getFPosRef());
  19431. try
  19432. {
  19433. transformedSize = owner.readHelper.transform(rowBuilder, keyRow);
  19434. owner.callback.finishedRow();
  19435. }
  19436. catch (IException *E)
  19437. {
  19438. throw owner.makeWrappedException(E);
  19439. }
  19440. if (transformedSize)
  19441. {
  19442. OwnedConstRoxieRow result = rowBuilder.finalizeRowClear(transformedSize);
  19443. matched++;
  19444. if (matched > owner.rowLimit)
  19445. {
  19446. owner.onLimitExceeded(false); // Should throw exception
  19447. throwUnexpected();
  19448. }
  19449. if (matched > owner.choosenLimit) // MORE - bit of a strange place to check
  19450. {
  19451. break;
  19452. }
  19453. owner.accepted++;
  19454. return result.getClear();
  19455. }
  19456. else
  19457. owner.rejected++;
  19458. }
  19459. EOFseen = true;
  19460. return NULL;
  19461. }
  19462. virtual bool atEOF() const
  19463. {
  19464. return EOFseen;
  19465. }
  19466. virtual bool isSerialized() const
  19467. {
  19468. return false;
  19469. }
  19470. };
  19471. virtual bool processSingleKey(IKeyIndex *key, IRecordLayoutTranslator * trans)
  19472. {
  19473. createRowAllocator();
  19474. remote.injectResult(new LazyLocalKeyReader(*this, key, trans));
  19475. return false;
  19476. }
  19477. virtual void onLimitExceeded(bool isKeyed)
  19478. {
  19479. if (traceLevel > 4)
  19480. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  19481. if (isKeyed)
  19482. {
  19483. if (indexHelper.getFlags() & (TIRkeyedlimitskips|TIRkeyedlimitcreates))
  19484. {
  19485. if (ctx->queryDebugContext())
  19486. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  19487. throw makeLimitSkipException(true);
  19488. }
  19489. else
  19490. readHelper.onKeyedLimitExceeded();
  19491. }
  19492. else
  19493. {
  19494. if (indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates))
  19495. {
  19496. if (ctx->queryDebugContext())
  19497. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  19498. throw makeLimitSkipException(false);
  19499. }
  19500. else
  19501. readHelper.onLimitExceeded();
  19502. }
  19503. }
  19504. virtual void serializeSkipInfo(MemoryBuffer &out, unsigned seekLen, const void *rawSeek, unsigned numFields, const void * seek, const SmartStepExtra &stepExtra) const
  19505. {
  19506. out.append((unsigned short) numFields);
  19507. out.append((unsigned short) seekLen);
  19508. out.append((unsigned short) stepExtra.queryFlags());
  19509. IMultipleStepSeekInfo *seeks = stepExtra.queryExtraSeeks();
  19510. if (seeks)
  19511. {
  19512. unsigned lookahead = 40000/seekLen;
  19513. if (maxSeekLookahead && (lookahead > maxSeekLookahead))
  19514. lookahead = maxSeekLookahead;
  19515. seeks->ensureFilled(seek, numFields, lookahead);
  19516. out.append(lookahead != seeks->ordinality()); // seeksAreEof flag
  19517. unsigned serialized = 1; // rawseek is always serialized...
  19518. unsigned patchLength = out.length();
  19519. out.append(serialized); // NOTE - we come back and patch with the actual value...
  19520. out.append(seekLen, rawSeek);
  19521. if (seeks->ordinality())
  19522. {
  19523. const void *lastSeek = rawSeek;
  19524. byte *nextSeek = NULL;
  19525. if (projectedMeta)
  19526. nextSeek = (byte *) alloca(seekLen);
  19527. for (unsigned i = 0; i < seeks->ordinality(); i++)
  19528. {
  19529. if (projectedMeta)
  19530. {
  19531. RtlStaticRowBuilder rowBuilder(nextSeek-seekGEOffset, seekGEOffset+seekLen);
  19532. readHelper.mapOutputToInput(rowBuilder, seeks->querySeek(i), numFields); // NOTE - weird interface to mapOutputToInput means that it STARTS writing at seekGEOffset...
  19533. }
  19534. else
  19535. nextSeek = (byte *) seeks->querySeek(i)+seekGEOffset;
  19536. int diff = memcmp(nextSeek, lastSeek, seekLen);
  19537. if (diff > 0)
  19538. {
  19539. serialized++;
  19540. out.append(seekLen, nextSeek);
  19541. lastSeek = (const byte *) out.reserve(0) - seekLen;
  19542. }
  19543. }
  19544. unsigned length = out.length();
  19545. out.setWritePos(patchLength);
  19546. out.append(serialized);
  19547. out.setWritePos(length);
  19548. }
  19549. }
  19550. else
  19551. {
  19552. out.append(false);
  19553. out.append(1);
  19554. out.append(seekLen, rawSeek);
  19555. }
  19556. }
  19557. virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  19558. {
  19559. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  19560. try
  19561. {
  19562. unsigned seeklen = 0;
  19563. const void *rawSeek = NULL;
  19564. if (seek && numFields)
  19565. {
  19566. seeklen = seekSizes[numFields-1];
  19567. rawSeek = (const byte *)seek + seekGEOffset;
  19568. if (projectedMeta)
  19569. {
  19570. byte * temp = (byte *) alloca(seeklen);
  19571. RtlStaticRowBuilder rawBuilder(temp-seekGEOffset, seekGEOffset+seeklen);
  19572. readHelper.mapOutputToInput(rawBuilder, seek, numFields); // NOTE - weird interface to mapOutputToInput means that it STARTS writing at seekGEOffset...
  19573. rawSeek = (byte *)temp;
  19574. }
  19575. }
  19576. const void *ret = remote.nextSteppedGE(seek, rawSeek, numFields, seeklen, wasCompleteMatch, stepExtra);
  19577. if (ret && wasCompleteMatch) // GH pleas confirm the wasCompleteMatch I just added here is right
  19578. processed++;
  19579. return ret;
  19580. }
  19581. catch (IException *E)
  19582. {
  19583. throw makeWrappedException(E);
  19584. }
  19585. }
  19586. virtual IInputSteppingMeta * querySteppingMeta()
  19587. {
  19588. if (rawMeta && steppingEnabled && ((indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates|TIRkeyedlimitskips|TIRkeyedlimitcreates)) == 0))
  19589. return &steppingMeta;
  19590. return NULL;
  19591. }
  19592. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  19593. {
  19594. if (variableInfoPending)
  19595. setVariableFileInfo();
  19596. return this;
  19597. }
  19598. virtual IKeyArray *getKeySet() const
  19599. {
  19600. return keySet.getLink();
  19601. }
  19602. virtual const IResolvedFile *getVarFileInfo() const
  19603. {
  19604. return varFileInfo.getLink();
  19605. }
  19606. virtual TranslatorArray *getTranslators() const
  19607. {
  19608. return translators.getLink();
  19609. }
  19610. virtual void mergeSegmentMonitors(IIndexReadContext *irc) const
  19611. {
  19612. indexHelper.createSegmentMonitors(irc); // NOTE: they will merge
  19613. }
  19614. virtual IRoxieServerActivity *queryActivity() { return this; }
  19615. virtual const RemoteActivityId& queryRemoteId() const
  19616. {
  19617. return remoteId;
  19618. }
  19619. };
  19620. class CRoxieServerSimpleIndexReadActivity : public CRoxieServerActivity, implements IIndexReadActivityInfo
  19621. {
  19622. IHThorCompoundReadExtra & readHelper;
  19623. IHThorIndexReadBaseArg & indexHelper;
  19624. IHThorSourceLimitTransformExtra * limitTransformExtra;
  19625. IHThorSteppedSourceExtra * steppedExtra;
  19626. bool eof;
  19627. Linked<IKeyArray>keySet;
  19628. Owned<IKeyIndexSet>keyIndexSet;
  19629. Owned<IKeyManager> tlk;
  19630. Linked<TranslatorArray> translators;
  19631. CIndexTransformCallback callback;
  19632. unsigned __int64 keyedLimit;
  19633. unsigned rowLimit;
  19634. unsigned chooseNLimit;
  19635. unsigned accepted;
  19636. unsigned rejected;
  19637. unsigned keyedCount;
  19638. ISteppingMeta * rawMeta;
  19639. ISteppingMeta * projectedMeta;
  19640. size32_t seekGEOffset;
  19641. unsigned * seekSizes;
  19642. CSteppingMeta steppingMeta;
  19643. Owned<const IResolvedFile> varFileInfo;
  19644. const RemoteActivityId &remoteId;
  19645. bool firstRead;
  19646. bool variableFileName;
  19647. bool variableInfoPending;
  19648. bool isOpt;
  19649. bool isLocal;
  19650. bool optimizeSteppedPostFilter;
  19651. // MORE there may be enough in common between this and CRoxieServerIndexActivity to warrant some refactoring
  19652. void initKeySet()
  19653. {
  19654. if ((keySet->length() > 1 || rawMeta != NULL) && translators->needsTranslation())
  19655. {
  19656. throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Layout translation is not available when merging key parts or smart-stepping, as it may change record order");
  19657. }
  19658. keyIndexSet.setown(createKeyIndexSet());
  19659. for (unsigned part = 0; part < keySet->length(); part++)
  19660. {
  19661. IKeyIndexBase *kib = keySet->queryKeyPart(part);
  19662. if (kib)
  19663. {
  19664. for (unsigned subpart = 0; subpart < kib->numParts(); subpart++)
  19665. {
  19666. IKeyIndex *k = kib->queryPart(subpart);
  19667. if (k)
  19668. {
  19669. assertex(!k->isTopLevelKey());
  19670. keyIndexSet->addIndex(LINK(k));
  19671. }
  19672. }
  19673. }
  19674. }
  19675. }
  19676. void setVariableFileInfo()
  19677. {
  19678. OwnedRoxieString indexName(indexHelper.getFileName());
  19679. varFileInfo.setown(resolveLFN(indexName, isOpt));
  19680. translators.setown(new TranslatorArray) ;
  19681. keySet.setown(varFileInfo->getKeyArray(factory->queryActivityMeta(), translators, isOpt, isLocal ? factory->queryQueryFactory().queryChannel() : 0, factory->queryQueryFactory().getEnableFieldTranslation()));
  19682. initKeySet();
  19683. variableInfoPending = false;
  19684. }
  19685. void onEOF()
  19686. {
  19687. callback.setManager(NULL);
  19688. eof = true;
  19689. tlk.clear();
  19690. }
  19691. public:
  19692. CRoxieServerSimpleIndexReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  19693. IKeyArray *_keyArray, TranslatorArray *_translatorArray, bool _isLocal)
  19694. : CRoxieServerActivity(_factory, _probeManager),
  19695. readHelper((IHThorIndexReadArg &)basehelper),
  19696. indexHelper((IHThorIndexReadArg &)basehelper),
  19697. translators(_translatorArray),
  19698. keySet(_keyArray),
  19699. isLocal(_isLocal),
  19700. remoteId(_remoteId)
  19701. {
  19702. rowLimit = 0;
  19703. keyedLimit = 0;
  19704. chooseNLimit = 0;
  19705. indexHelper.setCallback(&callback);
  19706. steppedExtra = static_cast<IHThorSteppedSourceExtra *>(indexHelper.selectInterface(TAIsteppedsourceextra_1));
  19707. limitTransformExtra = static_cast<IHThorSourceLimitTransformExtra *>(indexHelper.selectInterface(TAIsourcelimittransformextra_1));
  19708. unsigned flags = indexHelper.getFlags();
  19709. variableFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((flags & (TIRvarfilename|TIRdynamicfilename)) != 0);
  19710. variableInfoPending = false;
  19711. isOpt = (flags & TIRoptional) != 0;
  19712. optimizeSteppedPostFilter = (flags & TIRunfilteredtransform) != 0;
  19713. firstRead = true;
  19714. accepted = 0;
  19715. rejected = 0;
  19716. keyedCount = 0;
  19717. eof = false;
  19718. rawMeta = readHelper.queryRawSteppingMeta();
  19719. projectedMeta = readHelper.queryProjectedSteppingMeta();
  19720. seekGEOffset = 0;
  19721. seekSizes = NULL;
  19722. if (rawMeta)
  19723. {
  19724. // MORE - should check all keys in maxFields list can actually be keyed.
  19725. const CFieldOffsetSize * fields = rawMeta->queryFields();
  19726. unsigned maxFields = rawMeta->getNumFields();
  19727. seekGEOffset = fields[0].offset;
  19728. seekSizes = new unsigned[maxFields];
  19729. seekSizes[0] = fields[0].size;
  19730. for (unsigned i=1; i < maxFields; i++)
  19731. seekSizes[i] = seekSizes[i-1] + fields[i].size;
  19732. bool hasPostFilter = readHelper.transformMayFilter() && optimizeSteppedPostFilter;
  19733. if (projectedMeta)
  19734. steppingMeta.init(projectedMeta, hasPostFilter);
  19735. else
  19736. steppingMeta.init(rawMeta, hasPostFilter);
  19737. }
  19738. }
  19739. virtual const IResolvedFile *queryVarFileInfo() const
  19740. {
  19741. return varFileInfo;
  19742. }
  19743. ~CRoxieServerSimpleIndexReadActivity()
  19744. {
  19745. delete [] seekSizes;
  19746. }
  19747. virtual bool needsAllocator() const { return true; }
  19748. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  19749. {
  19750. firstRead = true;
  19751. accepted = 0;
  19752. rejected = 0;
  19753. keyedCount = 0;
  19754. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  19755. if (steppedExtra)
  19756. steppingMeta.setExtra(steppedExtra);
  19757. eof = !indexHelper.canMatchAny();
  19758. if (variableFileName)
  19759. variableInfoPending = true;
  19760. else
  19761. {
  19762. variableInfoPending = false;
  19763. if (!keyIndexSet)
  19764. initKeySet();
  19765. }
  19766. }
  19767. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  19768. {
  19769. if (variableInfoPending)
  19770. setVariableFileInfo();
  19771. return this;
  19772. }
  19773. virtual IKeyArray *getKeySet() const
  19774. {
  19775. return keySet.getLink();
  19776. }
  19777. virtual const IResolvedFile *getVarFileInfo() const
  19778. {
  19779. return varFileInfo.getLink();
  19780. }
  19781. virtual TranslatorArray *getTranslators() const
  19782. {
  19783. return translators.getLink();
  19784. }
  19785. virtual void mergeSegmentMonitors(IIndexReadContext *irc) const
  19786. {
  19787. indexHelper.createSegmentMonitors(irc); // NOTE: they will merge
  19788. }
  19789. virtual IRoxieServerActivity *queryActivity() { return this; }
  19790. virtual const RemoteActivityId& queryRemoteId() const
  19791. {
  19792. return remoteId;
  19793. }
  19794. const void *nextInGroup()
  19795. {
  19796. bool matched = true;
  19797. return nextSteppedGE(NULL, 0, matched, dummySmartStepExtra);
  19798. }
  19799. unsigned __int64 checkCount(unsigned __int64 limit)
  19800. {
  19801. unsigned numParts = keyIndexSet->numParts();
  19802. unsigned __int64 result = 0;
  19803. for (unsigned i = 0; i < numParts; i++)
  19804. {
  19805. Owned<IKeyManager> countTlk = createKeyManager(keyIndexSet->queryPart(i), 0, this);
  19806. countTlk->setLayoutTranslator(translators->item(i));
  19807. indexHelper.createSegmentMonitors(countTlk);
  19808. countTlk->finishSegmentMonitors();
  19809. result += countTlk->checkCount(limit-result);
  19810. if (result > limit)
  19811. break;
  19812. }
  19813. return result;
  19814. }
  19815. virtual const void *nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  19816. {
  19817. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  19818. if (eof)
  19819. return NULL;
  19820. if (firstRead)
  19821. {
  19822. if (variableInfoPending)
  19823. setVariableFileInfo();
  19824. rowLimit = (unsigned) readHelper.getRowLimit();
  19825. chooseNLimit = (unsigned) readHelper.getChooseNLimit();
  19826. unsigned numParts = keyIndexSet->numParts();
  19827. if (!numParts)
  19828. {
  19829. onEOF();
  19830. return NULL;
  19831. }
  19832. if (numParts > 1 || seekGEOffset)
  19833. {
  19834. tlk.setown(createKeyMerger(keyIndexSet, 0, seekGEOffset, this));
  19835. // note that we don't set up translator because we don't support it. If that ever changes...
  19836. }
  19837. else
  19838. {
  19839. tlk.setown(createKeyManager(keyIndexSet->queryPart(0), 0, this));
  19840. tlk->setLayoutTranslator(translators->item(0));
  19841. }
  19842. indexHelper.createSegmentMonitors(tlk);
  19843. tlk->finishSegmentMonitors();
  19844. if (queryTraceLevel() > 3 || ctx->queryProbeManager())
  19845. {
  19846. StringBuffer out;
  19847. printKeyedValues(out, tlk, indexHelper.queryDiskRecordSize());
  19848. CTXLOG("Using filter %s", out.str());
  19849. if (ctx->queryProbeManager())
  19850. ctx->queryProbeManager()->setNodeProperty(this, "filter", out.str());
  19851. }
  19852. tlk->reset();
  19853. callback.setManager(tlk);
  19854. keyedLimit = readHelper.getKeyedLimit();
  19855. if (keyedLimit != (unsigned __int64) -1)
  19856. {
  19857. if ((indexHelper.getFlags() & TIRcountkeyedlimit) != 0)
  19858. {
  19859. unsigned __int64 count = checkCount(keyedLimit);
  19860. if (count > keyedLimit)
  19861. {
  19862. if ((indexHelper.getFlags() & (TIRkeyedlimitskips|TIRkeyedlimitcreates)) == 0)
  19863. readHelper.onKeyedLimitExceeded();
  19864. const void * ret = NULL;
  19865. if (indexHelper.getFlags() & TIRkeyedlimitcreates)
  19866. ret = createKeyedLimitOnFailRow();
  19867. onEOF();
  19868. return ret;
  19869. }
  19870. keyedLimit = (unsigned __int64) -1;
  19871. }
  19872. }
  19873. firstRead = false;
  19874. }
  19875. if (accepted == chooseNLimit)
  19876. {
  19877. onEOF();
  19878. return NULL;
  19879. }
  19880. const byte * rawSeek = NULL;
  19881. unsigned seekSize = 0;
  19882. if (seek)
  19883. {
  19884. seekSize = seekSizes[numFields-1];
  19885. rawSeek = (const byte *)seek + seekGEOffset;
  19886. if (projectedMeta)
  19887. {
  19888. byte *temp = (byte *) alloca(seekSize);
  19889. RtlStaticRowBuilder rawBuilder(temp-seekGEOffset, seekGEOffset+seekSize);
  19890. readHelper.mapOutputToInput(rawBuilder, seek, numFields);// NOTE - weird interface to mapOutputToInput means that it STARTS writing at seekGEOffset...
  19891. rawSeek = (byte *)temp;
  19892. }
  19893. #ifdef _DEBUG
  19894. // StringBuffer seekStr;
  19895. // for (unsigned i = 0; i < seekSize; i++)
  19896. // {
  19897. // seekStr.appendf("%02x ", ((unsigned char *) rawSeek)[i]);
  19898. // }
  19899. // DBGLOG("nextSteppedGE can skip offset %d size %d value %s", seekGEOffset, seekSize, seekStr.str());
  19900. #endif
  19901. }
  19902. const byte * originalRawSeek = rawSeek;
  19903. RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
  19904. while (rawSeek ? tlk->lookupSkip(rawSeek, seekGEOffset, seekSize) : tlk->lookup(true))
  19905. {
  19906. checkAbort();
  19907. keyedCount++;
  19908. if (keyedCount > keyedLimit)
  19909. {
  19910. readHelper.onKeyedLimitExceeded();
  19911. break;
  19912. }
  19913. byte const * keyRow = tlk->queryKeyBuffer(callback.getFPosRef());
  19914. #ifdef _DEBUG
  19915. // StringBuffer recstr;
  19916. // unsigned size = (tlk->queryRecordSize()<80) ? tlk->queryRecordSize() : 80;
  19917. // for (unsigned i = 0; i < size; i++)
  19918. // {
  19919. // recstr.appendf("%02x ", ((unsigned char *) keyRow)[i]);
  19920. // }
  19921. // DBGLOG("nextSteppedGE Got %s", recstr.str());
  19922. if (originalRawSeek && memcmp(keyRow + seekGEOffset, originalRawSeek, seekSize) < 0)
  19923. assertex(!"smart seek failure");
  19924. #endif
  19925. size32_t transformedSize;
  19926. rowBuilder.ensureRow();
  19927. try
  19928. {
  19929. transformedSize = readHelper.transform(rowBuilder, keyRow);
  19930. //if the post filter causes a mismatch, and the stepping condition no longer matches
  19931. //then return a mismatch record - so the join code can start seeking on the other input.
  19932. if (transformedSize == 0 && optimizeSteppedPostFilter && stepExtra.returnMismatches())
  19933. {
  19934. if (memcmp(keyRow + seekGEOffset, originalRawSeek, seekSize) != 0)
  19935. {
  19936. transformedSize = readHelper.unfilteredTransform(rowBuilder, keyRow);
  19937. if (transformedSize != 0)
  19938. wasCompleteMatch = false;
  19939. }
  19940. }
  19941. callback.finishedRow();
  19942. }
  19943. catch (IException *E)
  19944. {
  19945. throw makeWrappedException(E);
  19946. }
  19947. if (transformedSize)
  19948. {
  19949. accepted++;
  19950. if (accepted > rowLimit)
  19951. {
  19952. if ((indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates)) != 0)
  19953. {
  19954. throwUnexpected(); // should not have used simple variant if maySkip set...
  19955. }
  19956. if (traceLevel > 4)
  19957. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  19958. readHelper.onLimitExceeded();
  19959. break;
  19960. }
  19961. processed++;
  19962. #ifdef _DEBUG
  19963. // const byte *ret = (const byte *) out.get();
  19964. // CommonXmlWriter xmlwrite(XWFnoindent|XWFtrim|XWFopt);
  19965. // queryOutputMeta()->toXML(ret, xmlwrite);
  19966. // DBGLOG("ROW: {%p} %s", ret, xmlwrite.str());
  19967. #endif
  19968. return rowBuilder.finalizeRowClear(transformedSize);
  19969. }
  19970. else
  19971. rejected++;
  19972. rawSeek = NULL;
  19973. }
  19974. onEOF();
  19975. return NULL;
  19976. }
  19977. virtual void reset()
  19978. {
  19979. onEOF();
  19980. if (accepted)
  19981. noteStatistic(STATS_ACCEPTED, accepted, 1);
  19982. if (rejected)
  19983. noteStatistic(STATS_REJECTED, rejected, 1);
  19984. if (variableFileName)
  19985. {
  19986. varFileInfo.clear();
  19987. translators.clear();
  19988. }
  19989. variableInfoPending = false;
  19990. CRoxieServerActivity::reset();
  19991. }
  19992. virtual void setInput(unsigned idx, IRoxieInput *_in)
  19993. {
  19994. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  19995. }
  19996. virtual IInputSteppingMeta * querySteppingMeta()
  19997. {
  19998. if (rawMeta && steppingEnabled && ((indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates|TIRkeyedlimitskips|TIRkeyedlimitcreates)) == 0))
  19999. return &steppingMeta;
  20000. return NULL;
  20001. }
  20002. protected:
  20003. const void * createKeyedLimitOnFailRow()
  20004. {
  20005. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  20006. size32_t outSize = limitTransformExtra->transformOnKeyedLimitExceeded(rowBuilder);
  20007. if (outSize)
  20008. return rowBuilder.finalizeRowClear(outSize);
  20009. return NULL;
  20010. }
  20011. };
  20012. class CRoxieServerBaseIndexActivityFactory : public CRoxieServerActivityFactory
  20013. {
  20014. public:
  20015. Owned<IKeyArray> keySet;
  20016. Owned<TranslatorArray> translatorArray;
  20017. Owned<IDefRecordMeta> activityMeta;
  20018. RemoteActivityId remoteId;
  20019. bool isSimple;
  20020. bool isLocal;
  20021. bool maySkip;
  20022. bool sorted;
  20023. bool variableFileName;
  20024. bool enableFieldTranslation;
  20025. unsigned maxSeekLookahead;
  20026. Owned<const IResolvedFile> indexfile;
  20027. CRoxieServerSideCache *cache;
  20028. CRoxieServerBaseIndexActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  20029. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), remoteId(_remoteId)
  20030. {
  20031. Owned<IHThorIndexReadBaseArg> indexHelper = (IHThorIndexReadBaseArg *) helperFactory();
  20032. unsigned flags = indexHelper->getFlags();
  20033. sorted = (flags & TIRunordered) == 0;
  20034. isLocal = _graphNode.getPropBool("att[@name='local']/@value") && queryFactory.queryChannel()!=0;
  20035. rtlDataAttr indexLayoutMeta;
  20036. size32_t indexLayoutSize;
  20037. if(!indexHelper->getIndexLayout(indexLayoutSize, indexLayoutMeta.refdata()))
  20038. assertex(indexLayoutSize==0);
  20039. MemoryBuffer m;
  20040. m.setBuffer(indexLayoutSize, indexLayoutMeta.getdata());
  20041. activityMeta.setown(deserializeRecordMeta(m, true));
  20042. enableFieldTranslation = queryFactory.getEnableFieldTranslation();
  20043. translatorArray.setown(new TranslatorArray);
  20044. variableFileName = allFilesDynamic || _queryFactory.isDynamic() || ((flags & (TIRvarfilename|TIRdynamicfilename)) != 0);
  20045. if (!variableFileName)
  20046. {
  20047. bool isOpt = (flags & TIRoptional) != 0;
  20048. OwnedRoxieString indexName(indexHelper->getFileName());
  20049. indexfile.setown(queryFactory.queryPackage().lookupFileName(indexName, isOpt, true, true, queryFactory.queryWorkUnit()));
  20050. if (indexfile)
  20051. keySet.setown(indexfile->getKeyArray(activityMeta, translatorArray, isOpt, isLocal ? queryFactory.queryChannel() : 0, enableFieldTranslation));
  20052. }
  20053. isSimple = isLocal;
  20054. maySkip = (flags & (TIRkeyedlimitskips|TIRlimitskips|TIRlimitcreates|TIRkeyedlimitcreates)) != 0;
  20055. if (keySet && keySet->length()==1 && !isLocal && (flags & (TIRlimitskips|TIRlimitcreates|TIRkeyedlimitskips|TIRkeyedlimitcreates))==0)
  20056. {
  20057. IKeyIndexBase *thisBase = keySet->queryKeyPart(0);
  20058. if (thisBase->numParts()==1 && !thisBase->queryPart(0)->isTopLevelKey() && !_queryFactory.getDebugValueBool("disableLocalOptimizations", false))
  20059. isSimple = true;
  20060. }
  20061. int cacheSize = _graphNode.getPropInt("hint[@name='cachehits']/@value", serverSideCacheSize);
  20062. cache = cacheSize ? new CRoxieServerSideCache(cacheSize) : NULL;
  20063. maxSeekLookahead = _graphNode.getPropInt("hint[@name='maxseeklookahead']/@value", 0);
  20064. }
  20065. ~CRoxieServerBaseIndexActivityFactory()
  20066. {
  20067. delete cache;
  20068. }
  20069. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  20070. {
  20071. if (indexfile)
  20072. addXrefFileInfo(reply, indexfile);
  20073. }
  20074. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  20075. {
  20076. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for indexread activity");
  20077. }
  20078. virtual IRoxieServerSideCache *queryServerSideCache() const
  20079. {
  20080. return cache;
  20081. }
  20082. virtual bool getEnableFieldTranslation() const
  20083. {
  20084. return enableFieldTranslation;
  20085. }
  20086. virtual IDefRecordMeta *queryActivityMeta() const
  20087. {
  20088. return activityMeta;
  20089. }
  20090. };
  20091. class CRoxieServerIndexReadActivityFactory : public CRoxieServerBaseIndexActivityFactory
  20092. {
  20093. public:
  20094. CRoxieServerIndexReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  20095. : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode)
  20096. {
  20097. }
  20098. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  20099. {
  20100. if (!variableFileName && (keySet==NULL || keySet->length()==0))
  20101. return new CRoxieServerNullActivity(this, _probeManager);
  20102. else if (isSimple && !maySkip)
  20103. return new CRoxieServerSimpleIndexReadActivity(this, _probeManager, remoteId, keySet, translatorArray, isLocal);
  20104. else
  20105. return new CRoxieServerIndexReadActivity(this, _probeManager, remoteId, keySet, translatorArray, sorted, isLocal, maySkip, maxSeekLookahead);
  20106. }
  20107. };
  20108. IRoxieServerActivityFactory *createRoxieServerIndexReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  20109. {
  20110. return new CRoxieServerIndexReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
  20111. }
  20112. //--------------------------------------------------------------------------------------------------------------------------
  20113. class CRoxieServerNullCountActivity : public CRoxieServerActivity
  20114. {
  20115. bool done;
  20116. public:
  20117. CRoxieServerNullCountActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  20118. : CRoxieServerActivity(_factory, _probeManager)
  20119. {
  20120. done = false;
  20121. }
  20122. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  20123. {
  20124. done = false;
  20125. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  20126. }
  20127. virtual bool needsAllocator() const { return true; }
  20128. virtual const void *nextInGroup()
  20129. {
  20130. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  20131. if (done) return NULL;
  20132. done = true;
  20133. size32_t rowSize = meta.getFixedSize();
  20134. void * nullRow = rowAllocator->createRow();
  20135. if (rowSize == 1)
  20136. *(byte *)nullRow = 0;
  20137. else
  20138. {
  20139. assertex(rowSize == sizeof(unsigned __int64));
  20140. *(unsigned __int64 *)nullRow = 0;
  20141. }
  20142. return rowAllocator->finalizeRow(rowSize, nullRow, rowSize);
  20143. }
  20144. };
  20145. class CRoxieServerIndexCountActivity : public CRoxieServerIndexActivity
  20146. {
  20147. IHThorCompoundCountExtra & countHelper;
  20148. IHThorSourceCountLimit * limitHelper;
  20149. bool done;
  20150. public:
  20151. CRoxieServerIndexCountActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, IKeyArray * _keySet, TranslatorArray *_translators, bool _isLocal)
  20152. : CRoxieServerIndexActivity(_factory, _probeManager, _remoteId, _keySet, _translators, false, _isLocal, false),
  20153. countHelper((IHThorIndexCountArg &)basehelper),
  20154. done(false)
  20155. {
  20156. limitHelper = static_cast<IHThorSourceCountLimit *>(basehelper.selectInterface(TAIsourcecountlimit_1));
  20157. }
  20158. virtual bool needsAllocator() const { return true; }
  20159. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  20160. {
  20161. done = false;
  20162. CRoxieServerIndexActivity::start(parentExtractSize, parentExtract, paused);
  20163. choosenLimit = countHelper.getChooseNLimit();
  20164. if (limitHelper)
  20165. {
  20166. rowLimit = limitHelper->getRowLimit();
  20167. keyedLimit = limitHelper->getKeyedLimit();
  20168. }
  20169. if (!paused)
  20170. processAllKeys();
  20171. }
  20172. virtual bool processSingleKey(IKeyIndex *key, IRecordLayoutTranslator * trans)
  20173. {
  20174. unsigned __int64 count = 0;
  20175. if (countHelper.hasFilter())
  20176. {
  20177. while (tlk->lookup(true))
  20178. {
  20179. try
  20180. {
  20181. count += countHelper.numValid(tlk->queryKeyBuffer(callback.getFPosRef()));
  20182. callback.finishedRow();
  20183. }
  20184. catch (IException *E)
  20185. {
  20186. throw makeWrappedException(E);
  20187. }
  20188. accepted++;
  20189. if (count >= choosenLimit) // MORE - what about limit?
  20190. break;
  20191. }
  20192. }
  20193. else
  20194. count = tlk->getCount(); //MORE: GH->RKC There should be value in providing a max limit to getCount()
  20195. if (count)
  20196. {
  20197. Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult(ctx->queryRowManager(), false);
  20198. if (count > choosenLimit)
  20199. count = choosenLimit;
  20200. void * recBuffer = rowAllocator->createRow();
  20201. if (meta.getFixedSize() == 1)
  20202. *(byte *)recBuffer = (byte)count;
  20203. else
  20204. {
  20205. assertex(meta.getFixedSize() == sizeof(unsigned __int64));
  20206. *(unsigned __int64 *)recBuffer = count;
  20207. }
  20208. recBuffer = rowAllocator->finalizeRow(meta.getFixedSize(), recBuffer, meta.getFixedSize());
  20209. result->append(recBuffer);
  20210. remote.injectResult(result.getClear());
  20211. //GH->RKC for count(,choosen)/exists passing in the previous count would short-circuit this much earlier
  20212. if (count >= choosenLimit)
  20213. return true;
  20214. }
  20215. return false;
  20216. }
  20217. virtual void onLimitExceeded(bool isKeyed)
  20218. {
  20219. if (traceLevel > 4)
  20220. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  20221. if (isKeyed)
  20222. {
  20223. if (indexHelper.getFlags() & (TIRkeyedlimitskips|TIRkeyedlimitcreates))
  20224. {
  20225. if (ctx->queryDebugContext())
  20226. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  20227. throw makeLimitSkipException(true);
  20228. }
  20229. else
  20230. {
  20231. assertex(limitHelper); // Should not be able to generate exception if there was not one...
  20232. limitHelper->onKeyedLimitExceeded();
  20233. }
  20234. }
  20235. else
  20236. {
  20237. if (indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates))
  20238. {
  20239. if (ctx->queryDebugContext())
  20240. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  20241. throw makeLimitSkipException(false);
  20242. }
  20243. else
  20244. {
  20245. assertex(limitHelper);
  20246. limitHelper->onLimitExceeded();
  20247. }
  20248. }
  20249. }
  20250. virtual const void *createLimitFailRow(bool isKeyed)
  20251. {
  20252. throwUnexpected();
  20253. }
  20254. virtual const void *nextInGroup()
  20255. {
  20256. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  20257. if (done) return NULL;
  20258. done = true;
  20259. unsigned __int64 totalCount = 0;
  20260. bool hasLimit = rowLimit != (unsigned __int64) -1;
  20261. try
  20262. {
  20263. loop
  20264. {
  20265. const void * next = remote.nextInGroup();
  20266. if (!next)
  20267. break;
  20268. if (meta.getFixedSize() == 1)
  20269. totalCount += *(byte *)next;
  20270. else
  20271. totalCount += *(unsigned __int64 *) next;
  20272. ReleaseRoxieRow(next);
  20273. if (totalCount > rowLimit || (totalCount > choosenLimit && !hasLimit)) // can't break out early if there is a possibility of later slave throwing limit exception
  20274. break;
  20275. }
  20276. if (totalCount > rowLimit)
  20277. {
  20278. unsigned flags = indexHelper.getFlags();
  20279. if (flags & TIRlimitskips)
  20280. totalCount = 0;
  20281. else if (flags & TIRlimitcreates)
  20282. totalCount = 1;
  20283. else
  20284. {
  20285. assertex(limitHelper);
  20286. limitHelper->onLimitExceeded();
  20287. }
  20288. }
  20289. else if (totalCount > choosenLimit)
  20290. totalCount = choosenLimit;
  20291. }
  20292. catch (IException *E)
  20293. {
  20294. if (QUERYINTERFACE(E, LimitSkipException))
  20295. {
  20296. totalCount = 0;
  20297. unsigned flags = indexHelper.getFlags();
  20298. if (E->errorCode() == KeyedLimitSkipErrorCode)
  20299. {
  20300. if (flags & TIRkeyedlimitcreates)
  20301. totalCount++;
  20302. }
  20303. else
  20304. {
  20305. if (flags & TIRlimitcreates)
  20306. totalCount++;
  20307. }
  20308. if (totalCount > choosenLimit)
  20309. totalCount = choosenLimit; // would have to be weird code (and escape the optimizer...)
  20310. E->Release();
  20311. }
  20312. else
  20313. throw ;
  20314. }
  20315. void * result = rowAllocator->createRow();
  20316. if (meta.getFixedSize() == 1)
  20317. *(byte *)result = (byte)totalCount;
  20318. else
  20319. {
  20320. assertex(meta.getFixedSize() == sizeof(unsigned __int64));
  20321. *(unsigned __int64 *)result = totalCount;
  20322. }
  20323. return rowAllocator->finalizeRow(meta.getFixedSize(), result, meta.getFixedSize());
  20324. }
  20325. };
  20326. class CRoxieServerIndexCountActivityFactory : public CRoxieServerBaseIndexActivityFactory
  20327. {
  20328. public:
  20329. CRoxieServerIndexCountActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  20330. : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode)
  20331. {
  20332. }
  20333. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  20334. {
  20335. if (!variableFileName && (keySet==NULL || keySet->length()==0))
  20336. return new CRoxieServerNullCountActivity(this, _probeManager);
  20337. // else if (isSimple)
  20338. // return new CRoxieServerSimpleIndexCountActivity(this, keySet->queryKeyPart(0)->queryPart(0));
  20339. else
  20340. return new CRoxieServerIndexCountActivity(this, _probeManager, remoteId, keySet, translatorArray, isLocal);
  20341. }
  20342. };
  20343. IRoxieServerActivityFactory *createRoxieServerIndexCountActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  20344. {
  20345. return new CRoxieServerIndexCountActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
  20346. }
  20347. //--------------------------------------------------------------------------------------------------------------------------
  20348. class CRoxieServerNullIndexAggregateActivity : public CRoxieServerActivity
  20349. {
  20350. IHThorIndexAggregateArg &aggregateHelper;
  20351. bool done;
  20352. public:
  20353. CRoxieServerNullIndexAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  20354. : CRoxieServerActivity(_factory, _probeManager),
  20355. aggregateHelper((IHThorIndexAggregateArg &)basehelper)
  20356. {
  20357. done = false;
  20358. }
  20359. virtual bool needsAllocator() const { return true; }
  20360. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  20361. {
  20362. done = false;
  20363. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  20364. }
  20365. virtual const void *nextInGroup()
  20366. {
  20367. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  20368. if (done) return NULL;
  20369. done = true;
  20370. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  20371. size32_t thisSize = aggregateHelper.clearAggregate(rowBuilder);
  20372. return rowBuilder.finalizeRowClear(thisSize);
  20373. }
  20374. };
  20375. class CRoxieServerIndexAggregateActivity : public CRoxieServerIndexActivity
  20376. {
  20377. IHThorCompoundAggregateExtra & aggregateHelper;
  20378. bool done;
  20379. public:
  20380. CRoxieServerIndexAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  20381. IKeyArray * _keySet, TranslatorArray *_translators, bool _isLocal)
  20382. : CRoxieServerIndexActivity(_factory, _probeManager, _remoteId, _keySet, _translators, false, _isLocal, false),
  20383. aggregateHelper((IHThorIndexAggregateArg &)basehelper),
  20384. done(false)
  20385. {
  20386. }
  20387. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  20388. {
  20389. done = false;
  20390. CRoxieServerIndexActivity::start(parentExtractSize, parentExtract, paused);
  20391. if (!paused)
  20392. processAllKeys();
  20393. }
  20394. virtual bool needsAllocator() const { return true; }
  20395. virtual bool processSingleKey(IKeyIndex *key, IRecordLayoutTranslator * trans)
  20396. {
  20397. RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
  20398. while (tlk->lookup(true))
  20399. {
  20400. if (!rowBuilder.exists())
  20401. {
  20402. rowBuilder.ensureRow();
  20403. aggregateHelper.clearAggregate(rowBuilder);
  20404. }
  20405. try
  20406. {
  20407. aggregateHelper.processRow(rowBuilder, tlk->queryKeyBuffer(callback.getFPosRef()));
  20408. callback.finishedRow();
  20409. }
  20410. catch (IException *E)
  20411. {
  20412. throw makeWrappedException(E);
  20413. }
  20414. accepted++;
  20415. }
  20416. if (aggregateHelper.processedAnyRows())
  20417. {
  20418. size32_t size = meta.getRecordSize(rowBuilder.getSelf());
  20419. const void * recBuffer = rowBuilder.finalizeRowClear(size);
  20420. Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult(ctx->queryRowManager(), meta.isVariableSize());
  20421. result->append(recBuffer);
  20422. remote.injectResult(result.getClear());
  20423. }
  20424. return false;
  20425. }
  20426. virtual void onLimitExceeded(bool isKeyed)
  20427. {
  20428. if (traceLevel > 4)
  20429. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  20430. throwUnexpected();
  20431. }
  20432. virtual const void *createLimitFailRow(bool isKeyed)
  20433. {
  20434. throwUnexpected();
  20435. }
  20436. const void * gatherMerged()
  20437. {
  20438. RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
  20439. const void * firstRow = remote.nextInGroup();
  20440. size32_t finalSize = 0;
  20441. if (!firstRow)
  20442. {
  20443. rowBuilder.ensureRow();
  20444. finalSize = aggregateHelper.clearAggregate(rowBuilder);
  20445. }
  20446. else
  20447. {
  20448. // NOTE need to clone this because going to modify below, could special case 1 row only
  20449. finalSize = cloneRow(rowBuilder, firstRow, meta);
  20450. ReleaseRoxieRow(firstRow);
  20451. }
  20452. loop
  20453. {
  20454. const void * next = remote.nextInGroup();
  20455. if (!next)
  20456. break;
  20457. finalSize = aggregateHelper.mergeAggregate(rowBuilder, next);
  20458. ReleaseRoxieRow(next);
  20459. }
  20460. return rowBuilder.finalizeRowClear(finalSize);
  20461. }
  20462. virtual const void *nextInGroup()
  20463. {
  20464. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  20465. if (done) return NULL;
  20466. const void * ret = gatherMerged();
  20467. done = true;
  20468. return ret;
  20469. }
  20470. };
  20471. class CRoxieServerIndexAggregateActivityFactory : public CRoxieServerBaseIndexActivityFactory
  20472. {
  20473. public:
  20474. CRoxieServerIndexAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  20475. : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode)
  20476. {
  20477. }
  20478. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  20479. {
  20480. if (!variableFileName && (keySet==NULL || keySet->length()==0))
  20481. return new CRoxieServerNullIndexAggregateActivity(this, _probeManager);
  20482. // else if (isSimple)
  20483. // return new CRoxieServerSimpleIndexAggregateActivity(this, keySet->queryKeyPart(0)->queryPart(0));
  20484. else
  20485. return new CRoxieServerIndexAggregateActivity(this, _probeManager, remoteId, keySet, translatorArray, isLocal);
  20486. }
  20487. };
  20488. IRoxieServerActivityFactory *createRoxieServerIndexAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  20489. {
  20490. return new CRoxieServerIndexAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
  20491. }
  20492. //--------------------------------------------------------------------------------------------------------------------------
  20493. class CRoxieServerIndexGroupAggregateActivity : public CRoxieServerIndexActivity, implements IHThorGroupAggregateCallback
  20494. {
  20495. IHThorCompoundGroupAggregateExtra & aggregateHelper;
  20496. RowAggregator singleAggregator;
  20497. RowAggregator resultAggregator;
  20498. unsigned groupSegCount;
  20499. bool gathered;
  20500. bool eof;
  20501. public:
  20502. CRoxieServerIndexGroupAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  20503. IKeyArray * _keySet, TranslatorArray *_translators, bool _isLocal)
  20504. : CRoxieServerIndexActivity(_factory, _probeManager, _remoteId, _keySet, _translators, false, _isLocal, false),
  20505. aggregateHelper((IHThorIndexGroupAggregateArg &)basehelper),
  20506. singleAggregator(aggregateHelper, aggregateHelper),
  20507. resultAggregator(aggregateHelper, aggregateHelper),
  20508. gathered(false), eof(true)
  20509. {
  20510. groupSegCount = 0;
  20511. }
  20512. IMPLEMENT_IINTERFACE
  20513. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  20514. {
  20515. eof = false;
  20516. gathered= false;
  20517. CRoxieServerIndexActivity::start(parentExtractSize, parentExtract, paused);
  20518. groupSegCount = 0;
  20519. if (!paused)
  20520. processAllKeys();
  20521. resultAggregator.start(rowAllocator);
  20522. }
  20523. virtual bool needsAllocator() const { return true; }
  20524. virtual void reset()
  20525. {
  20526. resultAggregator.reset();
  20527. CRoxieServerIndexActivity::reset();
  20528. }
  20529. virtual void processRow(const void * next)
  20530. {
  20531. singleAggregator.addRow(next);
  20532. }
  20533. virtual void createSegmentMonitors(IKeyManager *key)
  20534. {
  20535. unsigned groupSegSize;
  20536. ThorActivityKind kind = factory->getKind();
  20537. if ((kind==TAKindexgroupcount || kind==TAKindexgroupexists))
  20538. groupSegSize = aggregateHelper.getGroupSegmentMonitorsSize();
  20539. else
  20540. groupSegSize = 0;
  20541. if (groupSegSize)
  20542. {
  20543. key->setMergeBarrier(groupSegSize);
  20544. CRoxieServerIndexActivity::createSegmentMonitors(key);
  20545. unsigned numSegs = tlk->ordinality();
  20546. for (unsigned segNo = 0; segNo < numSegs; segNo++)
  20547. {
  20548. IKeySegmentMonitor *seg = tlk->item(segNo);
  20549. if (seg->getOffset()+seg->getSize()==groupSegSize)
  20550. {
  20551. groupSegCount = segNo+1;
  20552. break;
  20553. }
  20554. }
  20555. assertex(groupSegCount);
  20556. }
  20557. else
  20558. CRoxieServerIndexActivity::createSegmentMonitors(key);
  20559. }
  20560. virtual bool processSingleKey(IKeyIndex *key, IRecordLayoutTranslator * trans)
  20561. {
  20562. Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult(ctx->queryRowManager(), meta.isVariableSize());
  20563. singleAggregator.start(rowAllocator);
  20564. ThorActivityKind kind = factory->getKind();
  20565. while (tlk->lookup(true))
  20566. {
  20567. try
  20568. {
  20569. if (groupSegCount && !trans)
  20570. {
  20571. AggregateRowBuilder &rowBuilder = singleAggregator.addRow(tlk->queryKeyBuffer(callback.getFPosRef()));
  20572. callback.finishedRow();
  20573. if (kind==TAKindexgroupcount)
  20574. {
  20575. unsigned __int64 count = tlk->getCurrentRangeCount(groupSegCount);
  20576. aggregateHelper.processCountGrouping(rowBuilder, count-1);
  20577. }
  20578. if (!tlk->nextRange(groupSegCount))
  20579. break;
  20580. }
  20581. else
  20582. {
  20583. aggregateHelper.processRow(tlk->queryKeyBuffer(callback.getFPosRef()), this);
  20584. callback.finishedRow();
  20585. }
  20586. }
  20587. catch (IException *E)
  20588. {
  20589. throw makeWrappedException(E);
  20590. }
  20591. accepted++;
  20592. }
  20593. loop
  20594. {
  20595. Owned<AggregateRowBuilder> next = singleAggregator.nextResult();
  20596. if (!next)
  20597. break;
  20598. size32_t size = next->querySize();
  20599. result->append(next->finalizeRowClear());
  20600. }
  20601. remote.injectResult(result.getClear());
  20602. singleAggregator.reset();
  20603. return false;
  20604. }
  20605. virtual void onLimitExceeded(bool isKeyed)
  20606. {
  20607. if (traceLevel > 4)
  20608. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);DBGLOG("%d activityid = %d", __LINE__, activityId);
  20609. throwUnexpected();
  20610. }
  20611. virtual const void *createLimitFailRow(bool isKeyed)
  20612. {
  20613. throwUnexpected();
  20614. }
  20615. void gatherMerged()
  20616. {
  20617. gathered = true;
  20618. loop
  20619. {
  20620. const void * next = remote.nextInGroup();
  20621. if (!next)
  20622. break;
  20623. resultAggregator.mergeElement(next);
  20624. ReleaseRoxieRow(next);
  20625. }
  20626. }
  20627. virtual const void *nextInGroup()
  20628. {
  20629. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  20630. if (eof)
  20631. return NULL;
  20632. if (!gathered)
  20633. gatherMerged();
  20634. Owned<AggregateRowBuilder> next = resultAggregator.nextResult();
  20635. if (next)
  20636. {
  20637. processed++;
  20638. return next->finalizeRowClear();
  20639. }
  20640. eof = true;
  20641. return NULL;
  20642. }
  20643. };
  20644. class CRoxieServerIndexGroupAggregateActivityFactory : public CRoxieServerBaseIndexActivityFactory
  20645. {
  20646. public:
  20647. CRoxieServerIndexGroupAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  20648. : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode)
  20649. {
  20650. }
  20651. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  20652. {
  20653. if (!variableFileName && (keySet==NULL || keySet->length()==0))
  20654. return new CRoxieServerNullActivity(this, _probeManager);
  20655. // else if (isSimple)
  20656. // return new CRoxieServerSimpleIndexGroupAggregateActivity(this, keySet->queryKeyPart(0)->queryPart(0));
  20657. else
  20658. return new CRoxieServerIndexGroupAggregateActivity(this, _probeManager, remoteId, keySet, translatorArray, isLocal);
  20659. }
  20660. };
  20661. IRoxieServerActivityFactory *createRoxieServerIndexGroupAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  20662. {
  20663. return new CRoxieServerIndexGroupAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
  20664. }
  20665. //--------------------------------------------------------------------------------------------------------------------------
  20666. class CRoxieServerIndexNormalizeActivity : public CRoxieServerIndexReadBaseActivity
  20667. {
  20668. IHThorCompoundNormalizeExtra & readHelper;
  20669. public:
  20670. CRoxieServerIndexNormalizeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  20671. IKeyArray * _keySet, TranslatorArray *_translators, bool _sorted, bool _isLocal)
  20672. : CRoxieServerIndexReadBaseActivity(_factory, _probeManager, _remoteId, _keySet, _translators, _sorted, _isLocal, false),
  20673. readHelper((IHThorIndexNormalizeArg &)basehelper)
  20674. {
  20675. }
  20676. virtual bool needsAllocator() const { return true; }
  20677. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  20678. {
  20679. CRoxieServerIndexReadBaseActivity::start(parentExtractSize, parentExtract, paused);
  20680. rowLimit = readHelper.getRowLimit();
  20681. keyedLimit = readHelper.getKeyedLimit();
  20682. if (!paused)
  20683. processAllKeys();
  20684. }
  20685. virtual bool processSingleKey(IKeyIndex *key, IRecordLayoutTranslator * trans)
  20686. {
  20687. unsigned keyedCount = 0;
  20688. RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
  20689. while (tlk->lookup(true))
  20690. {
  20691. keyedCount++;
  20692. if (keyedCount > keyedLimit)
  20693. {
  20694. if (traceLevel > 4)
  20695. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  20696. onLimitExceeded(true);
  20697. break;
  20698. }
  20699. size32_t transformedSize;
  20700. if (readHelper.first(tlk->queryKeyBuffer(callback.getFPosRef())))
  20701. {
  20702. Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult(ctx->queryRowManager(), meta.isVariableSize());
  20703. do
  20704. {
  20705. rowBuilder.ensureRow();
  20706. try
  20707. {
  20708. transformedSize = readHelper.transform(rowBuilder);
  20709. }
  20710. catch (IException *E)
  20711. {
  20712. throw makeWrappedException(E);
  20713. }
  20714. if (transformedSize)
  20715. {
  20716. // MORE - would be a good idea to stop these asap if rowlimit exceeded
  20717. result->append(rowBuilder.finalizeRowClear(transformedSize));
  20718. accepted++;
  20719. }
  20720. else
  20721. rejected++;
  20722. } while (readHelper.next());
  20723. remote.injectResult(result.getClear());
  20724. callback.finishedRow();
  20725. }
  20726. }
  20727. return false;
  20728. }
  20729. virtual void onLimitExceeded(bool isKeyed)
  20730. {
  20731. if (traceLevel > 4)
  20732. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  20733. if (isKeyed)
  20734. {
  20735. if (indexHelper.getFlags() & (TIRkeyedlimitskips|TIRkeyedlimitcreates))
  20736. {
  20737. if (ctx->queryDebugContext())
  20738. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  20739. throw makeLimitSkipException(true);
  20740. }
  20741. else
  20742. readHelper.onKeyedLimitExceeded();
  20743. }
  20744. else
  20745. {
  20746. if (indexHelper.getFlags() & (TIRlimitskips||TIRlimitcreates))
  20747. {
  20748. if (ctx->queryDebugContext())
  20749. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  20750. throw makeLimitSkipException(false);
  20751. }
  20752. else
  20753. readHelper.onLimitExceeded();
  20754. }
  20755. }
  20756. virtual const void *createLimitFailRow(bool isKeyed)
  20757. {
  20758. UNIMPLEMENTED;
  20759. }
  20760. };
  20761. class CRoxieServerIndexNormalizeActivityFactory : public CRoxieServerBaseIndexActivityFactory
  20762. {
  20763. public:
  20764. CRoxieServerIndexNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  20765. : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode)
  20766. {
  20767. }
  20768. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  20769. {
  20770. if (!variableFileName && (keySet==NULL || keySet->length()==0))
  20771. return new CRoxieServerNullActivity(this, _probeManager);
  20772. else
  20773. return new CRoxieServerIndexNormalizeActivity(this, _probeManager, remoteId, keySet, translatorArray, sorted, isLocal);
  20774. }
  20775. };
  20776. IRoxieServerActivityFactory *createRoxieServerIndexNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  20777. {
  20778. return new CRoxieServerIndexNormalizeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
  20779. }
  20780. //=================================================================================
  20781. class CRoxieServerFetchActivity : public CRoxieServerActivity, implements IRecordPullerCallback, implements IRoxieServerErrorHandler
  20782. {
  20783. IHThorFetchBaseArg &helper;
  20784. IHThorFetchContext * fetchContext;
  20785. Linked<IFilePartMap> map;
  20786. CRemoteResultAdaptor remote;
  20787. RecordPullerThread puller;
  20788. bool needsRHS;
  20789. bool variableFileName;
  20790. bool isOpt;
  20791. Owned<const IResolvedFile> varFileInfo;
  20792. public:
  20793. CRoxieServerFetchActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, IFilePartMap *_map)
  20794. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorFetchBaseArg &)basehelper), map(_map), remote(_remoteId, meta.queryOriginal(), helper, *this, true, true), puller(false)
  20795. {
  20796. fetchContext = static_cast<IHThorFetchContext *>(helper.selectInterface(TAIfetchcontext_1));
  20797. needsRHS = helper.transformNeedsRhs();
  20798. variableFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((fetchContext->getFetchFlags() & (FFvarfilename|FFdynamicfilename)) != 0);
  20799. isOpt = (fetchContext->getFetchFlags() & FFdatafileoptional) != 0;
  20800. }
  20801. virtual const IResolvedFile *queryVarFileInfo() const
  20802. {
  20803. return varFileInfo;
  20804. }
  20805. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  20806. {
  20807. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  20808. remote.onCreate(this, this, _ctx, _colocalParent);
  20809. }
  20810. virtual void setInput(unsigned idx, IRoxieInput *_in)
  20811. {
  20812. if (idx)
  20813. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
  20814. puller.setInput(this, _in);
  20815. }
  20816. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  20817. {
  20818. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  20819. remote.onStart(parentExtractSize, parentExtract);
  20820. remote.setLimits(helper.getRowLimit(), (unsigned __int64) -1, I64C(0x7FFFFFFFFFFFFFFF));
  20821. if (variableFileName)
  20822. {
  20823. OwnedRoxieString fname(fetchContext->getFileName());
  20824. varFileInfo.setown(resolveLFN(fname, isOpt));
  20825. if (varFileInfo)
  20826. map.setown(varFileInfo->getFileMap());
  20827. }
  20828. puller.start(parentExtractSize, parentExtract, paused, ctx->fetchPreload(), false, ctx);
  20829. }
  20830. virtual void stop(bool aborting)
  20831. {
  20832. // Called from remote, so no need to call back to it....
  20833. puller.stop(aborting);
  20834. CRoxieServerActivity::stop(aborting);
  20835. }
  20836. virtual void reset()
  20837. {
  20838. processed = remote.processed;
  20839. remote.processed = 0;
  20840. puller.reset();
  20841. if (variableFileName)
  20842. {
  20843. varFileInfo.clear();
  20844. map.clear();
  20845. }
  20846. CRoxieServerActivity::reset();
  20847. }
  20848. virtual IRoxieInput *queryOutput(unsigned idx)
  20849. {
  20850. if (idx==(unsigned)-1)
  20851. idx = 0;
  20852. return idx ? NULL : &remote;
  20853. }
  20854. virtual void processRow(const void *row)
  20855. {
  20856. // called from puller thread
  20857. offset_t rp = fetchContext->extractPosition(row);
  20858. unsigned partNo;
  20859. if (isLocalFpos(rp))
  20860. partNo = getLocalFposPart(rp) + 1;
  20861. else
  20862. partNo = map->mapOffset(rp);
  20863. if (needsRHS)
  20864. {
  20865. Owned<IEngineRowAllocator> extractAllocator = ctx->queryCodeContext()->getRowAllocator(helper.queryExtractedSize(), activityId);
  20866. RtlDynamicRowBuilder rb(extractAllocator, true);
  20867. unsigned rhsSize = helper.extractJoinFields(rb, row);
  20868. char * block = (char *) remote.getMem(partNo, 0, sizeof(rp) + sizeof(rhsSize) + rhsSize); // MORE - superfiles
  20869. *(offset_t *) block = rp;
  20870. block += sizeof(rp);
  20871. *(unsigned *) block = rhsSize;
  20872. block += sizeof(rhsSize);
  20873. memcpy(block, rb.row(), rhsSize);
  20874. }
  20875. else
  20876. *(offset_t *) remote.getMem(partNo, 0, sizeof(rp)) = rp; // MORE - superfiles
  20877. ReleaseRoxieRow(row);
  20878. }
  20879. void processEOG()
  20880. {
  20881. #ifdef FETCH_PRESERVES_GROUPING
  20882. UNIMPLEMENTED;
  20883. #endif
  20884. // else discard is correct
  20885. }
  20886. void processGroup(const ConstPointerArray &)
  20887. {
  20888. throwUnexpected();
  20889. }
  20890. void processDone()
  20891. {
  20892. // called from puller thread
  20893. remote.flush();
  20894. remote.senddone();
  20895. }
  20896. virtual bool fireException(IException *e)
  20897. {
  20898. return remote.fireException(e);
  20899. }
  20900. virtual void onLimitExceeded(bool isKeyed)
  20901. {
  20902. if (traceLevel > 4)
  20903. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  20904. if (isKeyed)
  20905. throwUnexpected();
  20906. helper.onLimitExceeded();
  20907. }
  20908. virtual const void *createLimitFailRow(bool isKeyed)
  20909. {
  20910. UNIMPLEMENTED;
  20911. }
  20912. virtual const void *nextInGroup()
  20913. {
  20914. throwUnexpected(); // I am nobody's input
  20915. }
  20916. };
  20917. class CRoxieServerFetchActivityFactory : public CRoxieServerActivityFactory
  20918. {
  20919. RemoteActivityId remoteId;
  20920. Owned<IFilePartMap> map;
  20921. bool variableFileName;
  20922. Owned<const IResolvedFile> datafile;
  20923. public:
  20924. CRoxieServerFetchActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  20925. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), remoteId(_remoteId)
  20926. {
  20927. Owned<IHThorFetchBaseArg> helper = (IHThorFetchBaseArg *) helperFactory();
  20928. IHThorFetchContext *fetchContext = static_cast<IHThorFetchContext *>(helper->selectInterface(TAIfetchcontext_1));
  20929. variableFileName = allFilesDynamic || _queryFactory.isDynamic() || ((fetchContext->getFetchFlags() & (FFvarfilename|FFdynamicfilename)) != 0);
  20930. if (!variableFileName)
  20931. {
  20932. OwnedRoxieString fname(fetchContext->getFileName());
  20933. datafile.setown(_queryFactory.queryPackage().lookupFileName(fname,
  20934. (fetchContext->getFetchFlags() & FFdatafileoptional) != 0,
  20935. true, true,
  20936. _queryFactory.queryWorkUnit()));
  20937. if (datafile)
  20938. map.setown(datafile->getFileMap());
  20939. }
  20940. }
  20941. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  20942. {
  20943. return new CRoxieServerFetchActivity(this, _probeManager, remoteId, map);
  20944. }
  20945. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  20946. {
  20947. if (datafile)
  20948. addXrefFileInfo(reply, datafile);
  20949. }
  20950. };
  20951. IRoxieServerActivityFactory *createRoxieServerFetchActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
  20952. {
  20953. return new CRoxieServerFetchActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
  20954. }
  20955. // MORE - is there any point keeping this now?
  20956. class CRoxieServerDummyActivityFactory : public CRoxieServerActivityFactory // not a real activity - just used to properly link files
  20957. {
  20958. public:
  20959. Owned<const IResolvedFile> indexfile;
  20960. Owned<const IResolvedFile> datafile;
  20961. Owned<IKeyArray> keySet;
  20962. Owned<IFileIOArray> files;
  20963. Owned<IFilePartMap> map;
  20964. TranslatorArray layoutTranslators;
  20965. CRoxieServerDummyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool isLoadDataOnly)
  20966. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  20967. {
  20968. try // does not want any missing file errors to be fatal, or throw traps - just log it
  20969. {
  20970. if (_graphNode.getPropBool("att[@name='_isSpill']/@value", false) || _graphNode.getPropBool("att[@name='_isSpillGlobal']/@value", false))
  20971. return; // ignore 'spills'
  20972. bool isLocal = _graphNode.getPropBool("att[@name='local']/@value") && queryFactory.queryChannel()!=0;
  20973. ThorActivityKind kind = getActivityKind(_graphNode);
  20974. if (kind != TAKdiskwrite && kind != TAKindexwrite && kind != TAKpiperead && kind != TAKpipewrite)
  20975. {
  20976. const char *fileName = queryNodeFileName(_graphNode, kind);
  20977. const char *indexName = queryNodeIndexName(_graphNode, kind);
  20978. if (indexName)
  20979. {
  20980. bool isOpt = pretendAllOpt || _graphNode.getPropBool("att[@name='_isIndexOpt']/@value");
  20981. indexfile.setown(queryFactory.queryPackage().lookupFileName(indexName, isOpt, true, true, queryFactory.queryWorkUnit()));
  20982. if (indexfile)
  20983. keySet.setown(indexfile->getKeyArray(NULL, &layoutTranslators, isOpt, isLocal ? queryFactory.queryChannel() : 0, false));
  20984. }
  20985. if (fileName)
  20986. {
  20987. bool isOpt = pretendAllOpt || _graphNode.getPropBool("att[@name='_isOpt']/@value");
  20988. datafile.setown(_queryFactory.queryPackage().lookupFileName(fileName, isOpt, true, true, queryFactory.queryWorkUnit()));
  20989. if (datafile)
  20990. {
  20991. if (isLocal)
  20992. files.setown(datafile->getIFileIOArray(isOpt, queryFactory.queryChannel()));
  20993. else
  20994. map.setown(datafile->getFileMap());
  20995. }
  20996. }
  20997. }
  20998. }
  20999. catch(IException *E)
  21000. {
  21001. StringBuffer errors;
  21002. E->errorMessage(errors);
  21003. DBGLOG("%s File error = %s", (isLoadDataOnly) ? "LOADDATAONLY" : "SUSPENDED QUERY", errors.str());
  21004. E->Release();
  21005. }
  21006. }
  21007. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const { throw MakeStringException(ROXIE_INTERNAL_ERROR, "%s query %s is suspended and cannot be executed - error occurred at %s(%d)", (queryFactory.isQueryLibrary()) ? "Library" : " ", queryFactory.queryQueryName(), __FILE__, __LINE__); }
  21008. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  21009. {
  21010. if (datafile)
  21011. addXrefFileInfo(reply, datafile);
  21012. if (indexfile)
  21013. addXrefFileInfo(reply, indexfile);
  21014. }
  21015. };
  21016. IRoxieServerActivityFactory *createRoxieServerDummyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool isLoadDataOnly)
  21017. {
  21018. return new CRoxieServerDummyActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, isLoadDataOnly);
  21019. }
  21020. //=====================================================================================================
  21021. // Keyed joins...
  21022. //
  21023. // Input records are pulled by a puller thread, which checks each LHS record to determine which (if any) channels it
  21024. // may have RHS matches on, and sends the relevant fields to the relevant slaves.
  21025. // A separate thread (the caller's thread) is waiting on slave replies, and once it has all replies for a given LHS record or group of records, calls
  21026. // the transform and returns rows that are created.
  21027. // For a full-keyed join, there is a third thread that is pulling replies from index part and passing them to fetch part (check this is true)
  21028. //
  21029. //=====================================================================================================
  21030. class CJoinGroup;
  21031. interface IJoinProcessor
  21032. {
  21033. virtual void processEOG() = 0;
  21034. virtual CJoinGroup *createJoinGroup(const void *row) = 0;
  21035. virtual void noteEndReceived(CJoinGroup *jg, unsigned candidateCount) = 0;
  21036. virtual bool fireException(IException *E) = 0;
  21037. virtual void processCompletedGroups() = 0;
  21038. };
  21039. //------------------------------------------------------------------------------------------------------
  21040. // Class CJoinGroup has a record per LHS row, plus (if preserving grouping) a 'head of group' record
  21041. // It gathers all the corresponding RHS rows, keeping track of how may slave transactions are pending in endMarkersPending
  21042. // If preserving groups, the 'head of group' record keeps track of how many LHS records in the group are still incomplete.
  21043. // CJoinGroup records are allocated out of the Roxie row memory manager by overloading operator new, so that they are included in the
  21044. // per-query limits etc (Note that the pointer array block is not though).
  21045. // Because of that, the exact size is significant - especially whether fit just under or just over a chunking threshold...
  21046. //
  21047. // There are two phases to the life of a JoinGroup - it is created by the puller thread that is also firing off slave requests
  21048. // notePending will be called once for every slave request. Puller thread calls noteEndReceived(0) once when done - this corresponds to the
  21049. // initial count when created.
  21050. // Slave replies and are noted by the consumer thread calling addRightMatch() and noteEndReceived(n).
  21051. // Once endMarkersPending reaches 0, JoinGroup is complete. Last thread to call noteEndReceived will process the rows and destroy the group.
  21052. // There is no need for a critsec because although multiple threads will access at different times, only the consumer thread will
  21053. // access any modifiable member variables while endMarkersPending != 0 (i.e. complete() is false). Once complete returns true there is a single
  21054. // remaining reference and the JoinGroup will be processed and destroyed.
  21055. //
  21056. //------------------------------------------------------------------------------------------------------
  21057. class CJoinGroup : public CInterface, implements IInterface
  21058. {
  21059. protected:
  21060. const void *left; // LHS row
  21061. PointerArrayOf<KeyedJoinHeader> rows; // matching RHS rows
  21062. atomic_t endMarkersPending; // How many slave responses still waiting for
  21063. CJoinGroup *groupStart; // Head of group, or NULL if not grouping
  21064. unsigned lastPartNo;
  21065. unsigned pos;
  21066. unsigned candidates; // Number of RHS keyed candidates - note this may not be the same as rows.ordinality()
  21067. public:
  21068. #undef new
  21069. void *operator new(size_t size, IRowManager *a, unsigned activityId)
  21070. {
  21071. return a->allocate(size, activityId);
  21072. }
  21073. #if defined(_DEBUG) && defined(_WIN32) && !defined(USING_MPATROL)
  21074. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  21075. #endif
  21076. void operator delete(void *ptr, IRowManager *a, unsigned activityId)
  21077. {
  21078. ReleaseRoxieRow(ptr);
  21079. }
  21080. void operator delete(void *ptr)
  21081. {
  21082. ReleaseRoxieRow(ptr);
  21083. }
  21084. public:
  21085. IMPLEMENT_IINTERFACE;
  21086. CJoinGroup(const void *_left, CJoinGroup *_groupStart)
  21087. {
  21088. #ifdef TRACE_JOINGROUPS
  21089. DBGLOG("Creating joinGroup %p, groupstart %p", this, _groupStart);
  21090. #endif
  21091. candidates = 0;
  21092. lastPartNo = 0;
  21093. pos = 0;
  21094. left = _left;
  21095. groupStart = _groupStart;
  21096. if (_groupStart)
  21097. {
  21098. atomic_inc(&_groupStart->endMarkersPending);
  21099. }
  21100. atomic_set(&endMarkersPending, 1);
  21101. }
  21102. ~CJoinGroup()
  21103. {
  21104. #ifdef TRACE_JOINGROUPS
  21105. DBGLOG("Destroying joinGroup %p", this);
  21106. #endif
  21107. if (left)
  21108. {
  21109. ReleaseRoxieRow(left);
  21110. ForEachItemIn(idx, rows)
  21111. ReleaseRoxieRow(rows.item(idx));
  21112. rows.kill();
  21113. }
  21114. }
  21115. inline bool isHeadRecord() const
  21116. {
  21117. return left==NULL;
  21118. }
  21119. inline bool complete() const
  21120. {
  21121. return atomic_read(&endMarkersPending) == 0;
  21122. }
  21123. #ifdef TRACE_JOINGROUPS
  21124. inline void notePending(unsigned lineNo)
  21125. #else
  21126. inline void notePending()
  21127. #endif
  21128. {
  21129. assert(!complete());
  21130. atomic_inc(&endMarkersPending);
  21131. #ifdef TRACE_JOINGROUPS
  21132. DBGLOG("CJoinGroup::notePending %p from %d, count became %d group count %d", this, lineNo, atomic_read(&endMarkersPending), groupStart ? atomic_read(&groupStart->endMarkersPending) : 0);
  21133. #endif
  21134. }
  21135. inline bool inGroup(CJoinGroup *leader) const
  21136. {
  21137. return groupStart==leader;
  21138. }
  21139. inline const KeyedJoinHeader *queryRow(unsigned idx) const
  21140. {
  21141. // Single threaded by now
  21142. assert(complete());
  21143. return rows.item(idx);
  21144. }
  21145. #ifdef TRACE_JOINGROUPS
  21146. bool noteEndReceived(unsigned candidateCount, unsigned lineNo)
  21147. #else
  21148. bool noteEndReceived(unsigned candidateCount)
  21149. #endif
  21150. {
  21151. assert(!complete());
  21152. if (candidateCount)
  21153. {
  21154. candidates += candidateCount;
  21155. }
  21156. #ifdef TRACE_JOINGROUPS
  21157. DBGLOG("CJoinGroup::noteEndReceived %p from %d, candidates %d + %d, my count was %d, group count was %d", this, lineNo, candidates, candidateCount, atomic_read(&endMarkersPending), groupStart ? atomic_read(&groupStart->endMarkersPending) : 0);
  21158. #endif
  21159. // NOTE - as soon as endMarkersPending and groupStart->endMarkersPending are decremented to zero this object may get released asynchronously by other threads
  21160. // There must therefore be nothing in this method after them that acceses member variables. Think of it as a delete this...
  21161. // In particular, we can't safely reference groupStart after the dec_and_test of endMarkersPending, hence copy local first
  21162. CJoinGroup *localGroupStart = groupStart;
  21163. if (atomic_dec_and_test(&endMarkersPending))
  21164. {
  21165. if (localGroupStart)
  21166. return atomic_dec_and_test(&localGroupStart->endMarkersPending);
  21167. else
  21168. return true;
  21169. }
  21170. else
  21171. return false;
  21172. }
  21173. inline const void *queryLeft() const
  21174. {
  21175. return left;
  21176. }
  21177. void addRightMatch(KeyedJoinHeader *right)
  21178. {
  21179. assert(!complete());
  21180. unsigned short partNo = right->partNo;
  21181. if (partNo != lastPartNo)
  21182. {
  21183. // MORE - should we binchop? If we did we would need to be careful to find LAST match
  21184. if (partNo > lastPartNo)
  21185. pos = rows.length();
  21186. while (pos>0)
  21187. {
  21188. if (rows.item(pos-1)->partNo <= partNo)
  21189. break;
  21190. pos--;
  21191. }
  21192. lastPartNo = partNo;
  21193. }
  21194. rows.add(right, pos);
  21195. pos++;
  21196. }
  21197. inline unsigned rowsSeen() const
  21198. {
  21199. assert(complete());
  21200. return rows.length();
  21201. }
  21202. inline unsigned candidateCount() const
  21203. {
  21204. assert(complete());
  21205. return candidates;
  21206. }
  21207. };
  21208. #ifdef TRACE_JOINGROUPS
  21209. #define notePending() notePending(__LINE__)
  21210. #define noteEndReceived(a) noteEndReceived(a, __LINE__)
  21211. #endif
  21212. class KeyedJoinRemoteAdaptor : public CRemoteResultAdaptor // MORE - not sure it should be derived from this - makes processed all wrong, for example
  21213. {
  21214. private:
  21215. SafeQueueOf<const void, true> ready;
  21216. public:
  21217. IHThorKeyedJoinArg &helper;
  21218. unsigned joinProcessed;
  21219. bool isFullKey;
  21220. bool eof;
  21221. bool isSimple;
  21222. bool allPulled;
  21223. unsigned __int64 totalCycles;
  21224. unsigned activityId;
  21225. RecordPullerThread &puller;
  21226. SafeQueueOf<const void, true> injected; // Used in isSimple mode
  21227. Owned<IEngineRowAllocator> ccdRecordAllocator;
  21228. IJoinProcessor &processor;
  21229. KeyedJoinRemoteAdaptor(const RemoteActivityId &_remoteId, IHThorKeyedJoinArg &_helper, IRoxieServerActivity &_activity, bool _isFullKey, bool _isSimple,
  21230. RecordPullerThread &_puller, IJoinProcessor &_processor)
  21231. : helper(_helper),
  21232. CRemoteResultAdaptor(_remoteId, 0, _helper, _activity, true, true),
  21233. isFullKey(_isFullKey),
  21234. isSimple(_isSimple),
  21235. puller(_puller),
  21236. processor(_processor),
  21237. activityId(_activity.queryId())
  21238. {
  21239. joinProcessed = 0;
  21240. totalCycles = 0;
  21241. allPulled = false;
  21242. eof = false;
  21243. }
  21244. virtual void onCreate(IRoxieInput *_owner, IRoxieServerErrorHandler *_errorHandler, IRoxieSlaveContext *_ctx, IHThorArg *_colocalArg)
  21245. {
  21246. CRemoteResultAdaptor::onCreate(_owner, _errorHandler, _ctx, _colocalArg);
  21247. ccdRecordAllocator.setown(ctx->queryCodeContext()->getRowAllocator(QUERYINTERFACE(helper.queryJoinFieldsRecordSize(), IOutputMetaData), activityId));
  21248. }
  21249. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  21250. {
  21251. eof = false;
  21252. joinProcessed = 0;
  21253. totalCycles = 0;
  21254. allPulled = false;
  21255. assertex(ready.ordinality()==0);
  21256. CRemoteResultAdaptor::start(parentExtractSize, parentExtract, paused);
  21257. }
  21258. virtual void reset()
  21259. {
  21260. CRemoteResultAdaptor::reset();
  21261. while (ready.ordinality())
  21262. {
  21263. const void *goer = ready.dequeue();
  21264. if (goer)
  21265. ReleaseRoxieRow(goer);
  21266. }
  21267. while (injected.ordinality())
  21268. {
  21269. const void *goer = injected.dequeue();
  21270. if (goer)
  21271. ReleaseRoxieRow(goer);
  21272. }
  21273. }
  21274. inline void addResult(const void *row)
  21275. {
  21276. ready.enqueue(row);
  21277. }
  21278. virtual unsigned __int64 queryTotalCycles() const
  21279. {
  21280. return totalCycles;
  21281. }
  21282. virtual IOutputMetaData * queryOutputMeta() const
  21283. {
  21284. return owner->queryOutputMeta();
  21285. }
  21286. virtual const void *nextInGroup()
  21287. {
  21288. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  21289. loop
  21290. {
  21291. if (eof)
  21292. return NULL;
  21293. processSlaveResults();
  21294. if (ready.ordinality())
  21295. {
  21296. const void *result = ready.dequeue();
  21297. if (result)
  21298. joinProcessed++;
  21299. return result;
  21300. }
  21301. else
  21302. eof = true;
  21303. }
  21304. }
  21305. private:
  21306. void processSlaveResults()
  21307. {
  21308. while (!ready.ordinality())
  21309. {
  21310. KeyedJoinHeader *fetchedData;
  21311. if (isSimple)
  21312. {
  21313. while (!allPulled && !injected.ordinality())
  21314. {
  21315. if (!puller.pullRecords(1))
  21316. {
  21317. puller.done();
  21318. allPulled = true;
  21319. }
  21320. }
  21321. fetchedData = (KeyedJoinHeader *) injected.dequeue();
  21322. }
  21323. else
  21324. fetchedData = (KeyedJoinHeader *) CRemoteResultAdaptor::nextInGroup();
  21325. if (fetchedData)
  21326. {
  21327. CJoinGroup *thisGroup = fetchedData->thisGroup;
  21328. if (fetchedData->partNo == (unsigned short) -1)
  21329. {
  21330. #ifdef TRACE_JOINGROUPS
  21331. CTXLOG("Got end for group %p", thisGroup);
  21332. #endif
  21333. unsigned candidateCount = (unsigned) fetchedData->fpos;
  21334. ReleaseRoxieRow(fetchedData);
  21335. processor.noteEndReceived(thisGroup, candidateCount); // note - this can throw exception. So release fetchdata before calling
  21336. }
  21337. else
  21338. {
  21339. #ifdef TRACE_JOINGROUPS
  21340. CTXLOG("Reading another %d bytes for group %p data", ccdRecordSize, thisGroup);
  21341. #endif
  21342. thisGroup->addRightMatch(fetchedData);
  21343. if (isFullKey)
  21344. {
  21345. #ifdef TRACE_JOINGROUPS
  21346. CTXLOG("Calling noteEndReceived for record returned from FETCH of full keyed join");
  21347. #endif
  21348. processor.noteEndReceived(thisGroup, 0); // note - this can throw exception. So release fetchdata before calling
  21349. }
  21350. }
  21351. }
  21352. else
  21353. break;
  21354. }
  21355. }
  21356. };
  21357. class CRoxieServerFullKeyedJoinHead: public CRoxieServerActivity, implements IRecordPullerCallback, implements IRoxieServerErrorHandler
  21358. {
  21359. IHThorKeyedJoinArg &helper;
  21360. Owned<IKeyManager> tlk;
  21361. Linked<IKeyArray> keySet;
  21362. Linked<TranslatorArray> translators;
  21363. CRemoteResultAdaptor remote;
  21364. RecordPullerThread puller;
  21365. IOutputMetaData *indexReadMeta;
  21366. IJoinProcessor *joinHandler;
  21367. bool variableIndexFileName;
  21368. bool indexReadInputRecordVariable;
  21369. bool isLocal;
  21370. Owned<IEngineRowAllocator> indexReadAllocator;
  21371. Owned<const IResolvedFile> varFileInfo;
  21372. IRoxieInput *indexReadInput;
  21373. IIndexReadActivityInfo *rootIndex;
  21374. public:
  21375. CRoxieServerFullKeyedJoinHead(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, IKeyArray * _keySet, TranslatorArray *_translators, IOutputMetaData *_indexReadMeta, IJoinProcessor *_joinHandler, bool _isLocal)
  21376. : CRoxieServerActivity(_factory, _probeManager),
  21377. helper((IHThorKeyedJoinArg &)basehelper),
  21378. tlk(createKeyManager(NULL, 0, this)),
  21379. translators(_translators),
  21380. keySet(_keySet),
  21381. remote(_remoteId, 0, helper, *this, true, true),
  21382. indexReadMeta(_indexReadMeta),
  21383. joinHandler(_joinHandler),
  21384. puller(false),
  21385. isLocal(_isLocal)
  21386. {
  21387. variableIndexFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((helper.getJoinFlags() & (JFvarindexfilename|JFdynamicindexfilename)) != 0);
  21388. indexReadInputRecordVariable = indexReadMeta->isVariableSize();
  21389. indexReadInput = NULL;
  21390. rootIndex = NULL;
  21391. }
  21392. virtual const IResolvedFile *queryVarFileInfo() const
  21393. {
  21394. return varFileInfo;
  21395. }
  21396. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  21397. {
  21398. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  21399. remote.onCreate(this, this, _ctx, _colocalParent);
  21400. indexReadAllocator.setown(ctx->queryCodeContext()->getRowAllocator(indexReadMeta, activityId));
  21401. }
  21402. virtual void setInput(unsigned idx, IRoxieInput *_in)
  21403. {
  21404. if (!idx)
  21405. puller.setInput(this, _in);
  21406. else if (idx==1)
  21407. indexReadInput = _in;
  21408. else
  21409. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
  21410. }
  21411. virtual void serializeExtra(MemoryBuffer &out)
  21412. {
  21413. if (helper.getJoinFlags() & JFindexfromactivity)
  21414. {
  21415. assertex(rootIndex);
  21416. const RemoteActivityId& indexId = rootIndex->queryRemoteId();
  21417. indexId.serialize(out);
  21418. // could mess about reserving space for length then patching it again, to avoid copy, but probably not worth it
  21419. MemoryBuffer tmp;
  21420. rootIndex->queryActivity()->serializeCreateStartContext(tmp);
  21421. if (rootIndex->queryActivity()->queryVarFileInfo())
  21422. {
  21423. rootIndex->queryActivity()->queryVarFileInfo()->queryTimeStamp().serialize(tmp);
  21424. tmp.append(rootIndex->queryActivity()->queryVarFileInfo()->queryCheckSum());
  21425. }
  21426. unsigned ctxlen = tmp.length();
  21427. out.append(ctxlen).append(tmp);
  21428. }
  21429. }
  21430. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  21431. {
  21432. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  21433. if (indexReadInput)
  21434. {
  21435. indexReadInput->start(parentExtractSize, parentExtract, true); // paused=true because we don't want to actually run the index read
  21436. rootIndex = indexReadInput->queryIndexReadActivity();
  21437. if (!rootIndex)
  21438. throw MakeStringException(ROXIE_INTERNAL_ERROR,"Index in keyed join %d could not be resolved", queryId());
  21439. }
  21440. remote.onStart(parentExtractSize, parentExtract);
  21441. remote.setLimits(helper.getRowLimit(), (unsigned __int64) -1, I64C(0x7FFFFFFFFFFFFFFF));
  21442. if (rootIndex)
  21443. {
  21444. varFileInfo.setown(rootIndex->getVarFileInfo());
  21445. translators.setown(rootIndex->getTranslators());
  21446. keySet.setown(rootIndex->getKeySet());
  21447. }
  21448. else if (variableIndexFileName)
  21449. {
  21450. OwnedRoxieString indexFileName(helper.getIndexFileName());
  21451. varFileInfo.setown(resolveLFN(indexFileName, (helper.getJoinFlags() & JFindexoptional) != 0));
  21452. if (varFileInfo)
  21453. {
  21454. translators.setown(new TranslatorArray);
  21455. keySet.setown(varFileInfo->getKeyArray(factory->queryActivityMeta(), translators, false, isLocal ? factory->queryQueryFactory().queryChannel() : 0, factory->queryQueryFactory().getEnableFieldTranslation())); // MORE - isLocal?
  21456. }
  21457. }
  21458. puller.start(parentExtractSize, parentExtract, paused, ctx->fullKeyedJoinPreload(), false, ctx);
  21459. }
  21460. virtual void stop(bool aborting)
  21461. {
  21462. puller.stop(aborting);
  21463. CRoxieServerActivity::stop(aborting);
  21464. }
  21465. virtual void reset()
  21466. {
  21467. CRoxieServerActivity::reset();
  21468. puller.reset();
  21469. if (varFileInfo)
  21470. {
  21471. keySet.clear();
  21472. varFileInfo.clear();
  21473. }
  21474. }
  21475. virtual IRoxieInput *queryOutput(unsigned idx)
  21476. {
  21477. if (idx==(unsigned)-1)
  21478. idx = 0;
  21479. return idx ? NULL : &remote;
  21480. }
  21481. virtual void processRow(const void *row)
  21482. {
  21483. // MORE - this code seems to be pretty much duplicated below in half-keyed....
  21484. // called from front puller thread
  21485. // buffer up an IndexRead request
  21486. if (helper.leftCanMatch(row))
  21487. {
  21488. RtlDynamicRowBuilder extractedBuilder(indexReadAllocator);
  21489. unsigned indexReadSize = helper.extractIndexReadFields(extractedBuilder, row);
  21490. OwnedConstRoxieRow extracted;
  21491. if (indexReadSize)
  21492. extracted.setown(extractedBuilder.finalizeRowClear(indexReadSize));
  21493. CJoinGroup *jg = joinHandler->createJoinGroup(row);
  21494. for (unsigned partNo = 0; partNo < keySet->length(); partNo++)
  21495. {
  21496. IKeyIndexBase *thisBase = keySet->queryKeyPart(partNo);
  21497. if (thisBase)
  21498. {
  21499. unsigned fileNo = 0;
  21500. IKeyIndex *thisKey = thisBase->queryPart(fileNo);
  21501. try
  21502. {
  21503. tlk->setKey(thisKey);
  21504. tlk->setLayoutTranslator(translators->item(fileNo));
  21505. helper.createSegmentMonitors(tlk, extracted);
  21506. if (rootIndex)
  21507. rootIndex->mergeSegmentMonitors(tlk);
  21508. tlk->finishSegmentMonitors();
  21509. tlk->reset();
  21510. loop
  21511. {
  21512. typedef const void * cvp;
  21513. if (thisKey->isTopLevelKey())
  21514. {
  21515. bool locallySorted = !thisKey->isFullySorted();
  21516. while (locallySorted || tlk->lookup(false))
  21517. {
  21518. unsigned slavePart = locallySorted ? 0 : (unsigned) tlk->queryFpos();
  21519. if (locallySorted || slavePart)
  21520. {
  21521. cvp *outputBuffer = (cvp *) remote.getMem(slavePart, fileNo, indexReadSize + sizeof(cvp) + (indexReadInputRecordVariable ? sizeof(unsigned) : 0));
  21522. *outputBuffer++ = jg;
  21523. if (indexReadInputRecordVariable)
  21524. {
  21525. *(unsigned *) outputBuffer = indexReadSize;
  21526. outputBuffer = (cvp*) (((unsigned *) outputBuffer) + 1);
  21527. }
  21528. jg->notePending();
  21529. memcpy(outputBuffer, extracted, indexReadSize);
  21530. if (locallySorted)
  21531. {
  21532. for (unsigned i = 1; i < numChannels; i++)
  21533. jg->notePending();
  21534. break;
  21535. }
  21536. }
  21537. }
  21538. }
  21539. else
  21540. {
  21541. // MORE - this code seems to be duplicated in half keyed
  21542. unsigned accepted = 0;
  21543. unsigned rejected = 0;
  21544. Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult(ctx->queryRowManager(), true);
  21545. jg->notePending();
  21546. unsigned candidateCount = 0;
  21547. while (tlk->lookup(true))
  21548. {
  21549. candidateCount++;
  21550. atomic_inc(&indexRecordsRead);
  21551. KLBlobProviderAdapter adapter(tlk);
  21552. offset_t recptr;
  21553. const byte *indexRow = tlk->queryKeyBuffer(recptr);
  21554. if (helper.indexReadMatch(extracted, indexRow, recptr, &adapter))
  21555. {
  21556. KeyedJoinHeader *rhs = (KeyedJoinHeader *) ctx->queryRowManager().allocate(KEYEDJOIN_RECORD_SIZE(0), activityId);
  21557. rhs->fpos = recptr;
  21558. rhs->thisGroup = jg;
  21559. rhs->partNo = partNo;
  21560. result->append(rhs);
  21561. }
  21562. else
  21563. {
  21564. rejected++;
  21565. atomic_inc(&postFiltered);
  21566. }
  21567. }
  21568. // output an end marker for the matches to this group
  21569. KeyedJoinHeader *endMarker = (KeyedJoinHeader *) ctx->queryRowManager().allocate(KEYEDJOIN_RECORD_SIZE(0), activityId);
  21570. endMarker->fpos = (offset_t) candidateCount;
  21571. endMarker->thisGroup = jg;
  21572. endMarker->partNo = (unsigned short) -1;
  21573. result->append(endMarker);
  21574. remote.injectResult(result.getClear());
  21575. if (accepted)
  21576. noteStatistic(STATS_ACCEPTED, accepted, 1);
  21577. if (rejected)
  21578. noteStatistic(STATS_REJECTED, rejected, 1);
  21579. }
  21580. if (++fileNo < thisBase->numParts())
  21581. {
  21582. thisKey = thisBase->queryPart(fileNo);
  21583. tlk->setKey(thisKey);
  21584. tlk->setLayoutTranslator(translators->item(fileNo));
  21585. tlk->reset();
  21586. }
  21587. else
  21588. break;
  21589. }
  21590. tlk->releaseSegmentMonitors();
  21591. tlk->setKey(NULL);
  21592. }
  21593. catch (...)
  21594. {
  21595. tlk->releaseSegmentMonitors();
  21596. tlk->setKey(NULL);
  21597. throw;
  21598. }
  21599. }
  21600. }
  21601. joinHandler->noteEndReceived(jg, 0);
  21602. }
  21603. else
  21604. {
  21605. joinHandler->noteEndReceived(joinHandler->createJoinGroup(row), 0);
  21606. }
  21607. }
  21608. void processGroup(const ConstPointerArray &)
  21609. {
  21610. throwUnexpected();
  21611. }
  21612. virtual void processEOG()
  21613. {
  21614. joinHandler->processEOG();
  21615. }
  21616. virtual void processDone()
  21617. {
  21618. // called from puller thread
  21619. remote.flush();
  21620. remote.senddone();
  21621. }
  21622. virtual void onLimitExceeded(bool isKeyed)
  21623. {
  21624. if (traceLevel > 4)
  21625. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  21626. if (isKeyed)
  21627. throwUnexpected();
  21628. helper.onLimitExceeded();
  21629. }
  21630. virtual const void *createLimitFailRow(bool isKeyed)
  21631. {
  21632. throwUnexpected();
  21633. }
  21634. virtual bool fireException(IException *e)
  21635. {
  21636. // called from puller thread on failure
  21637. remote.fireException(LINK(e));
  21638. return joinHandler->fireException(e);
  21639. }
  21640. virtual const void *nextInGroup()
  21641. {
  21642. throwUnexpected(); // I am nobody's input
  21643. }
  21644. };
  21645. class CRoxieServerKeyedJoinBase : public CRoxieServerActivity, implements IRecordPullerCallback, implements IRoxieServerErrorHandler, implements IJoinProcessor
  21646. {
  21647. protected:
  21648. IHThorKeyedJoinArg &helper;
  21649. KeyedJoinRemoteAdaptor remote;
  21650. RecordPullerThread puller;
  21651. OwnedConstRoxieRow defaultRight;
  21652. Owned<IEngineRowAllocator> defaultRightAllocator;
  21653. unsigned joinFlags;
  21654. unsigned atMost;
  21655. unsigned atmostsTriggered;
  21656. unsigned abortLimit;
  21657. unsigned keepLimit;
  21658. bool limitFail;
  21659. bool limitOnFail;
  21660. bool preserveGroups;
  21661. bool cloneLeft;
  21662. bool isSimple;
  21663. bool isLocal;
  21664. ThorActivityKind activityKind;
  21665. CJoinGroup *groupStart;
  21666. CriticalSection groupsCrit;
  21667. QueueOf<CJoinGroup, false> groups;
  21668. IRoxieInput *indexReadInput;
  21669. IIndexReadActivityInfo *rootIndex;
  21670. void createDefaultRight()
  21671. {
  21672. if (!defaultRight)
  21673. {
  21674. if (!defaultRightAllocator)
  21675. defaultRightAllocator.setown(ctx->queryCodeContext()->getRowAllocator(helper.queryJoinFieldsRecordSize(), activityId));
  21676. RtlDynamicRowBuilder rowBuilder(defaultRightAllocator);
  21677. size32_t thisSize = helper.createDefaultRight(rowBuilder);
  21678. defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
  21679. }
  21680. }
  21681. public:
  21682. CRoxieServerKeyedJoinBase(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _joinFlags
  21683. , bool isFull, bool _isSimple, bool _isLocal)
  21684. : CRoxieServerActivity(_factory, _probeManager),
  21685. helper((IHThorKeyedJoinArg &)basehelper),
  21686. remote(_remoteId, helper, *this, isFull, _isSimple, puller, *this),
  21687. joinFlags(_joinFlags),
  21688. preserveGroups(meta.isGrouped()),
  21689. puller(false),
  21690. isSimple(_isSimple),
  21691. isLocal(_isLocal),
  21692. abortLimit(0),
  21693. keepLimit(0),
  21694. atMost(0),
  21695. limitFail(false),
  21696. limitOnFail(false),
  21697. cloneLeft(false)
  21698. {
  21699. groupStart = NULL;
  21700. activityKind = _factory->getKind();
  21701. indexReadInput = NULL;
  21702. rootIndex = NULL;
  21703. atmostsTriggered = 0;
  21704. // MORE - code would be easier to read if I got more values from helper rather than passing from factory
  21705. }
  21706. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  21707. {
  21708. CRoxieServerActivity::onCreate(_ctx, _colocalParent);
  21709. remote.onCreate(this, this, _ctx, _colocalParent);
  21710. }
  21711. virtual void setInput(unsigned idx, IRoxieInput *_in)
  21712. {
  21713. if (!idx)
  21714. puller.setInput(this, _in);
  21715. else if (idx==1)
  21716. indexReadInput = _in;
  21717. else
  21718. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
  21719. }
  21720. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  21721. {
  21722. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  21723. if (indexReadInput)
  21724. {
  21725. indexReadInput->start(parentExtractSize, parentExtract, true); // paused=true because we don't want to actually run the index read
  21726. rootIndex = indexReadInput->queryIndexReadActivity();
  21727. if (!rootIndex)
  21728. throw MakeStringException(ROXIE_INTERNAL_ERROR,"Index in keyed join %d could not be resolved", queryId());
  21729. }
  21730. remote.onStart(parentExtractSize, parentExtract);
  21731. remote.setLimits(helper.getRowLimit(), (unsigned __int64) -1, I64C(0x7FFFFFFFFFFFFFFF));
  21732. atmostsTriggered = 0;
  21733. atMost = helper.getJoinLimit();
  21734. if (atMost == 0) atMost = (unsigned)-1;
  21735. abortLimit = helper.getMatchAbortLimit();
  21736. if (abortLimit == 0 || atMost != (unsigned) -1) abortLimit = (unsigned)-1;
  21737. keepLimit = helper.getKeepLimit();
  21738. if (keepLimit == 0) keepLimit = (unsigned)-1;
  21739. getLimitType(joinFlags, limitFail, limitOnFail);
  21740. cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
  21741. if (joinFlags & JFleftouter)
  21742. createDefaultRight();
  21743. }
  21744. virtual void stop(bool aborting)
  21745. {
  21746. puller.stop(aborting);
  21747. if (indexReadInput)
  21748. indexReadInput->stop(aborting);
  21749. CRoxieServerActivity::stop(aborting);
  21750. }
  21751. virtual unsigned __int64 queryLocalCycles() const
  21752. {
  21753. __int64 localCycles = remote.totalCycles;
  21754. localCycles -= puller.queryTotalCycles(); // MORE - debatable... but probably fair.
  21755. if (localCycles < 0)
  21756. localCycles = 0;
  21757. return localCycles;
  21758. }
  21759. virtual IRoxieInput *queryInput(unsigned idx)
  21760. {
  21761. if (idx==0)
  21762. return puller.queryInput();
  21763. else if (idx==1)
  21764. return indexReadInput;
  21765. else
  21766. return NULL;
  21767. }
  21768. virtual void reset()
  21769. {
  21770. totalCycles = remote.totalCycles;
  21771. remote.totalCycles = 0;
  21772. processed = remote.joinProcessed;
  21773. remote.joinProcessed = 0;
  21774. defaultRight.clear();
  21775. if (indexReadInput)
  21776. indexReadInput->reset();
  21777. if (atmostsTriggered)
  21778. noteStatistic(STATS_ATMOST, atmostsTriggered, 1);
  21779. CRoxieServerActivity::reset();
  21780. puller.reset();
  21781. while (groups.ordinality())
  21782. {
  21783. ::Release(groups.dequeue());
  21784. }
  21785. }
  21786. virtual IRoxieInput *queryOutput(unsigned idx)
  21787. {
  21788. if (idx==(unsigned)-1)
  21789. idx = 0;
  21790. return idx ? NULL : &remote;
  21791. }
  21792. #undef new
  21793. virtual CJoinGroup *createJoinGroup(const void *row)
  21794. {
  21795. // NOTE - we need to protect access to queue, since it's also modified by consumer thread. Groupstart is only modified by puller thread.
  21796. CriticalBlock c(groupsCrit);
  21797. if (preserveGroups && !groupStart)
  21798. {
  21799. groupStart = new (&ctx->queryRowManager(), activityId) CJoinGroup(NULL, NULL);
  21800. groups.enqueue(groupStart);
  21801. }
  21802. CJoinGroup *jg = new (&ctx->queryRowManager(), activityId) CJoinGroup(row, groupStart);
  21803. groups.enqueue(jg);
  21804. return jg;
  21805. }
  21806. #if defined(_DEBUG) && defined(_WIN32) && !defined(USING_MPATROL)
  21807. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  21808. #endif
  21809. void endGroup()
  21810. {
  21811. CriticalBlock c(groupsCrit);
  21812. if (groupStart)
  21813. noteEndReceived(groupStart, 0);
  21814. groupStart = NULL;
  21815. }
  21816. virtual void noteEndReceived(CJoinGroup *jg, unsigned candidateCount)
  21817. {
  21818. if (jg->noteEndReceived(candidateCount))
  21819. processCompletedGroups();
  21820. }
  21821. void processCompletedGroups()
  21822. {
  21823. loop
  21824. {
  21825. CriticalBlock c(groupsCrit);
  21826. if (!groups.head()->complete())
  21827. break;
  21828. Owned<CJoinGroup> head = groups.dequeue();
  21829. if (preserveGroups)
  21830. {
  21831. assert(head->isHeadRecord());
  21832. assert(groups.head()->inGroup(head));
  21833. unsigned joinGroupSize = 0;
  21834. while (groups.ordinality() && groups.head()->inGroup(head))
  21835. {
  21836. Owned<CJoinGroup> finger = groups.dequeue();
  21837. joinGroupSize += doJoinGroup(finger);
  21838. }
  21839. if (joinGroupSize)
  21840. remote.addResult(NULL);
  21841. }
  21842. else
  21843. doJoinGroup(head);
  21844. if (!groups.ordinality())
  21845. break;
  21846. }
  21847. }
  21848. void failLimit(const void * left)
  21849. {
  21850. helper.onMatchAbortLimitExceeded();
  21851. CommonXmlWriter xmlwrite(0);
  21852. if (input && input->queryOutputMeta() && input->queryOutputMeta()->hasXML())
  21853. {
  21854. input->queryOutputMeta()->toXML((byte *) left, xmlwrite);
  21855. }
  21856. throw MakeStringException(ROXIE_JOIN_ERROR, "More than %d match candidates in keyed join %d for row %s", abortLimit, queryId(), xmlwrite.str());
  21857. }
  21858. virtual bool needsAllocator() const { return true; }
  21859. unsigned doTransform(const void *left, const void *right, offset_t fpos_or_count, IException *except, const void **group)
  21860. {
  21861. if (cloneLeft && !except)
  21862. {
  21863. LinkRoxieRow(left);
  21864. remote.addResult((void *) left);
  21865. return 1;
  21866. }
  21867. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  21868. unsigned outSize;
  21869. try
  21870. {
  21871. outSize = except ? helper.onFailTransform(rowBuilder, left, right, fpos_or_count, except) :
  21872. (activityKind == TAKkeyeddenormalizegroup) ? helper.transform(rowBuilder, left, right, (unsigned) fpos_or_count, group) :
  21873. helper.transform(rowBuilder, left, right, fpos_or_count);
  21874. }
  21875. catch (IException *E)
  21876. {
  21877. throw makeWrappedException(E);
  21878. }
  21879. if (outSize)
  21880. {
  21881. const void *shrunk = rowBuilder.finalizeRowClear(outSize);
  21882. remote.addResult(shrunk);
  21883. return 1;
  21884. }
  21885. else
  21886. return 0;
  21887. }
  21888. unsigned doJoinGroup(CJoinGroup *jg)
  21889. {
  21890. unsigned matched = jg->rowsSeen();
  21891. unsigned added = 0;
  21892. const void *left = jg->queryLeft();
  21893. if (jg->candidateCount() > abortLimit)
  21894. {
  21895. if (limitFail)
  21896. failLimit(left);
  21897. if (ctx->queryDebugContext())
  21898. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  21899. if (limitOnFail)
  21900. {
  21901. Owned<IException> except;
  21902. try
  21903. {
  21904. failLimit(left);
  21905. }
  21906. catch(IException * e)
  21907. {
  21908. except.setown(e);
  21909. }
  21910. added = doTransform(left, defaultRight, 0, except, NULL);
  21911. }
  21912. }
  21913. else if (!matched || jg->candidateCount() > atMost)
  21914. {
  21915. if (jg->candidateCount() > atMost)
  21916. atmostsTriggered++;
  21917. switch (joinFlags & JFtypemask)
  21918. {
  21919. case JFleftouter:
  21920. case JFleftonly:
  21921. switch (activityKind)
  21922. {
  21923. case TAKkeyedjoin:
  21924. case TAKkeyeddenormalizegroup:
  21925. added = doTransform(left, defaultRight, 0, NULL, NULL);
  21926. break;
  21927. case TAKkeyeddenormalize:
  21928. LinkRoxieRow(left);
  21929. remote.addResult((void *) left);
  21930. added++;
  21931. break;
  21932. }
  21933. break;
  21934. }
  21935. }
  21936. else if (!(joinFlags & JFexclude))
  21937. {
  21938. unsigned idx = 0;
  21939. switch (activityKind)
  21940. {
  21941. case TAKkeyedjoin:
  21942. while (idx < matched)
  21943. {
  21944. const KeyedJoinHeader *rhs = jg->queryRow(idx);
  21945. added += doTransform(left, &rhs->rhsdata, rhs->fpos, NULL, NULL);
  21946. if (added==keepLimit)
  21947. break;
  21948. idx++;
  21949. }
  21950. break;
  21951. case TAKkeyeddenormalize:
  21952. {
  21953. OwnedConstRoxieRow newLeft;
  21954. newLeft.set(left);
  21955. unsigned rowSize = 0;
  21956. unsigned rightAdded = 0;
  21957. while (idx < matched)
  21958. {
  21959. const KeyedJoinHeader *rhs = jg->queryRow(idx);
  21960. try
  21961. {
  21962. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  21963. size32_t transformedSize = helper.transform(rowBuilder, newLeft, &rhs->rhsdata, rhs->fpos, idx+1);
  21964. if (transformedSize)
  21965. {
  21966. rowSize = transformedSize;
  21967. newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
  21968. rightAdded++;
  21969. if (rightAdded==keepLimit)
  21970. break;
  21971. }
  21972. idx++;
  21973. }
  21974. catch (IException *E)
  21975. {
  21976. throw makeWrappedException(E);
  21977. }
  21978. }
  21979. if (rowSize)
  21980. {
  21981. remote.addResult(newLeft.getClear());
  21982. added++;
  21983. }
  21984. }
  21985. break;
  21986. case TAKkeyeddenormalizegroup:
  21987. {
  21988. ConstPointerArray extractedRows;
  21989. while (idx < matched && idx < keepLimit)
  21990. {
  21991. const KeyedJoinHeader *rhs = jg->queryRow(idx);
  21992. extractedRows.append((void *) &rhs->rhsdata);
  21993. idx++;
  21994. }
  21995. added += doTransform(left, extractedRows.item(0), extractedRows.ordinality(), NULL, (const void * *)extractedRows.getArray());
  21996. }
  21997. break;
  21998. }
  21999. }
  22000. return added;
  22001. }
  22002. virtual void processDone()
  22003. {
  22004. // called from puller thread
  22005. remote.flush();
  22006. remote.senddone();
  22007. }
  22008. virtual void onLimitExceeded(bool isKeyed)
  22009. {
  22010. if (traceLevel > 4)
  22011. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  22012. if (isKeyed)
  22013. throwUnexpected();
  22014. helper.onLimitExceeded();
  22015. }
  22016. virtual const void *createLimitFailRow(bool isKeyed)
  22017. {
  22018. throwUnexpected();
  22019. }
  22020. virtual bool fireException(IException *e)
  22021. {
  22022. // called from puller thread on failure
  22023. return remote.fireException(e);
  22024. }
  22025. virtual const void *nextInGroup()
  22026. {
  22027. throwUnexpected(); // I am nobody's input
  22028. }
  22029. };
  22030. #ifdef _MSC_VER
  22031. #pragma warning ( push )
  22032. #pragma warning ( disable: 4355 )
  22033. #endif
  22034. class CRoxieServerKeyedJoinActivity : public CRoxieServerKeyedJoinBase
  22035. {
  22036. CRoxieServerFullKeyedJoinHead head;
  22037. Owned<IEngineRowAllocator> fetchInputAllocator;
  22038. Linked<IFilePartMap> map;
  22039. bool variableFetchFileName;
  22040. Owned<const IResolvedFile> varFetchFileInfo;
  22041. CachedOutputMetaData fetchInputFields;
  22042. public:
  22043. CRoxieServerKeyedJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_headId, IKeyArray * _key, TranslatorArray *_translators, IOutputMetaData *_indexReadMeta,
  22044. const RemoteActivityId &_tailId, IFilePartMap *_map, unsigned _joinFlags, bool _isLocal)
  22045. : CRoxieServerKeyedJoinBase(_factory, _probeManager, _tailId, _joinFlags, true, false, _isLocal),
  22046. head(_factory, _probeManager, _headId, _key, _translators, _indexReadMeta, this, _isLocal),
  22047. map(_map)
  22048. {
  22049. CRoxieServerKeyedJoinBase::setInput(0, head.queryOutput(0));
  22050. variableFetchFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((helper.getFetchFlags() & (FFvarfilename|FFdynamicfilename)) != 0);
  22051. }
  22052. virtual const IResolvedFile *queryVarFileInfo() const
  22053. {
  22054. return varFetchFileInfo;
  22055. }
  22056. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  22057. {
  22058. CRoxieServerKeyedJoinBase::onCreate(_ctx, _colocalParent);
  22059. head.onCreate(_ctx, _colocalParent);
  22060. fetchInputFields.set(helper.queryFetchInputRecordSize());
  22061. fetchInputAllocator.setown(ctx->queryCodeContext()->getRowAllocator(helper.queryFetchInputRecordSize(), activityId));
  22062. }
  22063. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  22064. {
  22065. CRoxieServerKeyedJoinBase::start(parentExtractSize, parentExtract, paused);
  22066. if (variableFetchFileName)
  22067. {
  22068. bool isFetchOpt = (helper.getFetchFlags() & FFdatafileoptional) != 0;
  22069. OwnedRoxieString fname(helper.getFileName());
  22070. varFetchFileInfo.setown(resolveLFN(fname, isFetchOpt));
  22071. if (varFetchFileInfo)
  22072. map.setown(varFetchFileInfo->getFileMap());
  22073. }
  22074. puller.start(parentExtractSize, parentExtract, paused, ctx->keyedJoinPreload(), false, ctx);
  22075. }
  22076. virtual void setInput(unsigned idx, IRoxieInput *in)
  22077. {
  22078. head.setInput(idx, in);
  22079. }
  22080. virtual void processRow(const void *_rhs)
  22081. {
  22082. // called from puller thread
  22083. KeyedJoinHeader *rhs = (KeyedJoinHeader *) _rhs;
  22084. CJoinGroup *jg = rhs->thisGroup;
  22085. if (rhs->partNo != (unsigned short) -1)
  22086. {
  22087. unsigned partNo = map->mapOffset(rhs->fpos);
  22088. RtlDynamicRowBuilder fetchBuilder(fetchInputAllocator, true);
  22089. size32_t fisize = helper.extractFetchFields(fetchBuilder, jg->queryLeft());
  22090. if (fetchInputFields.isVariableSize())
  22091. {
  22092. KeyedJoinHeader *outRow = (KeyedJoinHeader *) remote.getMem(partNo, 0, KEYEDJOIN_RECORD_SIZE(fisize + sizeof(fisize)));
  22093. memcpy(outRow, rhs, KEYEDJOIN_RECORD_SIZE(0)); // MORE - copy constructor might be more appropriate....
  22094. ReleaseRoxieRow(rhs);
  22095. jg->notePending();
  22096. memcpy(&outRow->rhsdata, &fisize, sizeof(fisize));
  22097. memcpy((&outRow->rhsdata)+sizeof(fisize), fetchBuilder.row(), fisize);
  22098. }
  22099. else
  22100. {
  22101. KeyedJoinHeader *outRow = (KeyedJoinHeader *) remote.getMem(partNo, 0, KEYEDJOIN_RECORD_SIZE(fisize));
  22102. memcpy(outRow, rhs, KEYEDJOIN_RECORD_SIZE(0)); // MORE - copy constructor might be more appropriate....
  22103. ReleaseRoxieRow(rhs);
  22104. jg->notePending();
  22105. memcpy(&outRow->rhsdata, fetchBuilder.row(), fisize);
  22106. }
  22107. }
  22108. else
  22109. {
  22110. unsigned candidateCount = (unsigned) rhs->fpos;
  22111. // CTXLOG("Full keyed join - all results back from index");
  22112. ReleaseRoxieRow(rhs);
  22113. noteEndReceived(jg, candidateCount); // may throw exception - so release row before calling
  22114. }
  22115. }
  22116. virtual void processEOG()
  22117. {
  22118. // called from front puller thread
  22119. if (preserveGroups)
  22120. endGroup();
  22121. }
  22122. void processGroup(const ConstPointerArray &)
  22123. {
  22124. throwUnexpected();
  22125. }
  22126. };
  22127. #ifdef _MSC_VER
  22128. #pragma warning ( pop )
  22129. #endif
  22130. class CRoxieServerHalfKeyedJoinActivity : public CRoxieServerKeyedJoinBase
  22131. {
  22132. IOutputMetaData *indexReadMeta;
  22133. Owned<IEngineRowAllocator> indexReadAllocator;
  22134. Owned<IKeyManager> tlk;
  22135. bool variableIndexFileName;
  22136. bool indexReadInputRecordVariable;
  22137. Owned<const IResolvedFile> varFileInfo;
  22138. Linked<TranslatorArray> translators;
  22139. Linked<IKeyArray> keySet;
  22140. Owned<IOutputMetaData> joinPrefixedMeta;
  22141. Owned<IEngineRowAllocator> joinFieldsAllocator;
  22142. public:
  22143. CRoxieServerHalfKeyedJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, IKeyArray * _keySet, TranslatorArray *_translators,
  22144. IOutputMetaData *_indexReadMeta, unsigned _joinFlags, bool _isSimple, bool _isLocal)
  22145. : CRoxieServerKeyedJoinBase(_factory, _probeManager, _remoteId, _joinFlags, false, _isSimple, _isLocal),
  22146. indexReadMeta(_indexReadMeta),
  22147. tlk(createKeyManager(NULL, 0, this)),
  22148. keySet(_keySet),
  22149. translators(_translators)
  22150. {
  22151. variableIndexFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((helper.getJoinFlags() & (JFvarindexfilename|JFdynamicindexfilename)) != 0);
  22152. indexReadInputRecordVariable = indexReadMeta->isVariableSize();
  22153. }
  22154. virtual void serializeExtra(MemoryBuffer &out)
  22155. {
  22156. if (helper.getJoinFlags() & JFindexfromactivity)
  22157. {
  22158. assertex(rootIndex);
  22159. const RemoteActivityId& indexId = rootIndex->queryRemoteId();
  22160. indexId.serialize(out);
  22161. // could mess about reserving space for length then patching it again, to avoid copy, but probably not worth it
  22162. MemoryBuffer tmp;
  22163. rootIndex->queryActivity()->serializeCreateStartContext(tmp);
  22164. if (rootIndex->queryActivity()->queryVarFileInfo())
  22165. {
  22166. rootIndex->queryActivity()->queryVarFileInfo()->queryTimeStamp().serialize(tmp);
  22167. tmp.append(rootIndex->queryActivity()->queryVarFileInfo()->queryCheckSum());
  22168. }
  22169. unsigned ctxlen = tmp.length();
  22170. out.append(ctxlen).append(tmp);
  22171. }
  22172. }
  22173. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  22174. {
  22175. CRoxieServerKeyedJoinBase::onCreate(_ctx, _colocalParent);
  22176. indexReadAllocator.setown(ctx->queryCodeContext()->getRowAllocator(indexReadMeta, activityId));
  22177. IOutputMetaData *joinFieldsMeta = helper.queryJoinFieldsRecordSize();
  22178. joinPrefixedMeta.setown(new CPrefixedOutputMeta(KEYEDJOIN_RECORD_SIZE(0), joinFieldsMeta)); // MORE - not sure if we really need this
  22179. joinFieldsAllocator.setown(ctx->queryCodeContext()->getRowAllocator(joinPrefixedMeta, activityId));
  22180. }
  22181. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  22182. {
  22183. CRoxieServerKeyedJoinBase::start(parentExtractSize, parentExtract, paused);
  22184. if (rootIndex)
  22185. {
  22186. varFileInfo.setown(rootIndex->getVarFileInfo());
  22187. translators.setown(rootIndex->getTranslators());
  22188. keySet.setown(rootIndex->getKeySet());
  22189. }
  22190. else if (variableIndexFileName)
  22191. {
  22192. OwnedRoxieString indexFileName(helper.getIndexFileName());
  22193. varFileInfo.setown(resolveLFN(indexFileName, (helper.getJoinFlags() & JFindexoptional) != 0));
  22194. if (varFileInfo)
  22195. {
  22196. translators.setown(new TranslatorArray);
  22197. keySet.setown(varFileInfo->getKeyArray(factory->queryActivityMeta(), translators, false, isLocal ? factory->queryQueryFactory().queryChannel() : 0, factory->queryQueryFactory().getEnableFieldTranslation()));
  22198. }
  22199. }
  22200. puller.start(parentExtractSize, parentExtract, paused, ctx->keyedJoinPreload(), isSimple, ctx);
  22201. }
  22202. virtual const IResolvedFile *queryVarFileInfo() const
  22203. {
  22204. return varFileInfo;
  22205. }
  22206. virtual void reset()
  22207. {
  22208. CRoxieServerKeyedJoinBase::reset();
  22209. if (varFileInfo)
  22210. {
  22211. keySet.clear();
  22212. varFileInfo.clear();
  22213. }
  22214. }
  22215. virtual void processRow(const void *row)
  22216. {
  22217. // called from front puller thread
  22218. // buffer up an IndexRead request
  22219. if (helper.leftCanMatch(row) && keySet)
  22220. {
  22221. RtlDynamicRowBuilder extractBuilder(indexReadAllocator);
  22222. unsigned indexReadRecordSize = helper.extractIndexReadFields(extractBuilder, row);
  22223. OwnedConstRoxieRow extracted;
  22224. if (indexReadRecordSize)
  22225. extracted.setown(extractBuilder.finalizeRowClear(indexReadRecordSize));
  22226. CJoinGroup *jg = createJoinGroup(row);
  22227. for (unsigned partNo = 0; partNo < keySet->length(); partNo++)
  22228. {
  22229. IKeyIndexBase *thisBase = keySet->queryKeyPart(partNo);
  22230. if (thisBase)
  22231. {
  22232. unsigned fileNo = 0;
  22233. IKeyIndex *thisKey = thisBase->queryPart(fileNo);
  22234. tlk->setKey(thisKey);
  22235. tlk->setLayoutTranslator(translators->item(fileNo));
  22236. helper.createSegmentMonitors(tlk, extracted);
  22237. if (rootIndex)
  22238. rootIndex->mergeSegmentMonitors(tlk);
  22239. tlk->finishSegmentMonitors();
  22240. try
  22241. {
  22242. tlk->reset();
  22243. loop
  22244. {
  22245. typedef const void * cvp;
  22246. if (thisKey && thisKey->isTopLevelKey())
  22247. {
  22248. bool locallySorted = (!thisKey->isFullySorted());
  22249. while (locallySorted || tlk->lookup(false))
  22250. {
  22251. unsigned slavePart = locallySorted ? 0 : (unsigned) tlk->queryFpos();
  22252. if (locallySorted || slavePart)
  22253. {
  22254. cvp *outputBuffer = (cvp *) remote.getMem(slavePart, fileNo, indexReadRecordSize + sizeof(cvp) + (indexReadInputRecordVariable ? sizeof(unsigned) : 0));
  22255. *outputBuffer++ = jg;
  22256. if (indexReadInputRecordVariable)
  22257. {
  22258. *(unsigned *) outputBuffer = indexReadRecordSize;
  22259. outputBuffer = (cvp *) (((unsigned *) outputBuffer) + 1);
  22260. }
  22261. jg->notePending();
  22262. memcpy(outputBuffer, extracted, indexReadRecordSize);
  22263. if (locallySorted)
  22264. {
  22265. for (unsigned i = 1; i < numChannels; i++)
  22266. jg->notePending();
  22267. break;
  22268. }
  22269. }
  22270. }
  22271. }
  22272. else
  22273. {
  22274. unsigned accepted = 0;
  22275. unsigned rejected = 0;
  22276. Owned<CRowArrayMessageResult> result;
  22277. if (!isSimple)
  22278. result.setown(new CRowArrayMessageResult(ctx->queryRowManager(), true));
  22279. // MORE - This code seems to be duplicated in keyedJoinHead
  22280. jg->notePending();
  22281. unsigned candidateCount = 0;
  22282. while (tlk->lookup(true))
  22283. {
  22284. candidateCount++;
  22285. atomic_inc(&indexRecordsRead);
  22286. KLBlobProviderAdapter adapter(tlk);
  22287. offset_t recptr;
  22288. const byte *indexRow = tlk->queryKeyBuffer(recptr);
  22289. if (helper.indexReadMatch(extracted, indexRow, recptr, &adapter))
  22290. {
  22291. RtlDynamicRowBuilder rb(joinFieldsAllocator, true);
  22292. CPrefixedRowBuilder pb(KEYEDJOIN_RECORD_SIZE(0), rb);
  22293. accepted++;
  22294. KLBlobProviderAdapter adapter(tlk);
  22295. size32_t joinFieldsSize = helper.extractJoinFields(pb, indexRow, recptr, &adapter);
  22296. KeyedJoinHeader *rec = (KeyedJoinHeader *) rb.getUnfinalizedClear(); // lack of finalize ok as unserialized data here.
  22297. rec->fpos = recptr;
  22298. rec->thisGroup = jg;
  22299. rec->partNo = partNo;
  22300. if (isSimple)
  22301. remote.injected.enqueue(rec);
  22302. else
  22303. result->append(rec);
  22304. }
  22305. else
  22306. {
  22307. rejected++;
  22308. atomic_inc(&postFiltered);
  22309. }
  22310. }
  22311. // output an end marker for the matches to this group
  22312. KeyedJoinHeader *rec = (KeyedJoinHeader *) ctx->queryRowManager().allocate(KEYEDJOIN_RECORD_SIZE(0), activityId);
  22313. rec->fpos = (offset_t) candidateCount;
  22314. rec->thisGroup = jg;
  22315. rec->partNo = (unsigned short) -1;
  22316. if (isSimple)
  22317. remote.injected.enqueue(rec);
  22318. else
  22319. {
  22320. result->append(rec);
  22321. remote.injectResult(result.getClear());
  22322. }
  22323. if (accepted)
  22324. noteStatistic(STATS_ACCEPTED, accepted, 1);
  22325. if (rejected)
  22326. noteStatistic(STATS_REJECTED, rejected, 1);
  22327. }
  22328. if (++fileNo < thisBase->numParts())
  22329. {
  22330. thisKey = thisBase->queryPart(fileNo);
  22331. tlk->setKey(thisKey);
  22332. tlk->setLayoutTranslator(translators->item(fileNo));
  22333. tlk->reset();
  22334. }
  22335. else
  22336. break;
  22337. }
  22338. tlk->releaseSegmentMonitors();
  22339. tlk->setKey(NULL);
  22340. }
  22341. catch (...)
  22342. {
  22343. tlk->releaseSegmentMonitors();
  22344. tlk->setKey(NULL);
  22345. throw;
  22346. }
  22347. }
  22348. }
  22349. noteEndReceived(jg, 0);
  22350. }
  22351. else
  22352. {
  22353. noteEndReceived(createJoinGroup(row), 0);
  22354. }
  22355. }
  22356. virtual void processEOG()
  22357. {
  22358. // called from front puller thread
  22359. if (preserveGroups)
  22360. endGroup();
  22361. }
  22362. void processGroup(const ConstPointerArray &)
  22363. {
  22364. throwUnexpected();
  22365. }
  22366. };
  22367. class CRoxieServerKeyedJoinActivityFactory : public CRoxieServerMultiInputFactory
  22368. {
  22369. Owned<const IResolvedFile> indexfile;
  22370. Owned<const IResolvedFile> datafile;
  22371. Owned<IKeyArray> keySet;
  22372. Owned<TranslatorArray> translatorArray;
  22373. Owned<IDefRecordMeta> activityMeta;
  22374. RemoteActivityId headId;
  22375. RemoteActivityId tailId;
  22376. IOutputMetaData *indexReadMeta;
  22377. Owned<IFilePartMap> map;
  22378. Owned<IFileIOArray> files;
  22379. unsigned joinFlags;
  22380. bool isHalfKeyed;
  22381. bool isLocal;
  22382. bool enableFieldTranslation;
  22383. bool variableFetchFileName;
  22384. bool variableIndexFileName;
  22385. bool isSimple;
  22386. public:
  22387. CRoxieServerKeyedJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_headId, const RemoteActivityId &_tailId, IPropertyTree &_graphNode)
  22388. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), headId(_headId), tailId(_tailId)
  22389. {
  22390. Owned<IHThorKeyedJoinArg> helper = (IHThorKeyedJoinArg *) helperFactory();
  22391. isLocal = _graphNode.getPropBool("att[@name='local']/@value") && queryFactory.queryChannel()!=0;
  22392. isSimple = isLocal;
  22393. rtlDataAttr indexLayoutMeta;
  22394. size32_t indexLayoutSize;
  22395. if(!helper->getIndexLayout(indexLayoutSize, indexLayoutMeta.refdata()))
  22396. assertex(indexLayoutSize== 0);
  22397. MemoryBuffer m;
  22398. m.setBuffer(indexLayoutSize, indexLayoutMeta.getdata());
  22399. activityMeta.setown(deserializeRecordMeta(m, true));
  22400. enableFieldTranslation = queryFactory.getEnableFieldTranslation();
  22401. translatorArray.setown(new TranslatorArray);
  22402. joinFlags = helper->getJoinFlags();
  22403. variableIndexFileName = allFilesDynamic || _queryFactory.isDynamic() || ((joinFlags & (JFvarindexfilename|JFdynamicindexfilename)) != 0);
  22404. variableFetchFileName = allFilesDynamic || _queryFactory.isDynamic() || ((helper->getFetchFlags() & (FFvarfilename|FFdynamicfilename)) != 0);
  22405. if (!variableIndexFileName)
  22406. {
  22407. bool isOpt = (joinFlags & JFindexoptional) != 0;
  22408. OwnedRoxieString indexFileName(helper->getIndexFileName());
  22409. indexfile.setown(queryFactory.queryPackage().lookupFileName(indexFileName, isOpt, true, true, queryFactory.queryWorkUnit()));
  22410. if (indexfile)
  22411. keySet.setown(indexfile->getKeyArray(activityMeta, translatorArray, isOpt, isLocal ? queryFactory.queryChannel() : 0, enableFieldTranslation));
  22412. }
  22413. if (keySet && keySet->length()==1 && !isSimple)
  22414. {
  22415. IKeyIndexBase *thisBase = keySet->queryKeyPart(0);
  22416. if (thisBase->numParts()==1 && !thisBase->queryPart(0)->isTopLevelKey() && !_queryFactory.getDebugValueBool("disableLocalOptimizations", false))
  22417. isSimple = true;
  22418. // MORE - if it's a variable filename then it MAY be simple, we don't know. Tough.
  22419. }
  22420. if (!simpleLocalKeyedJoins)
  22421. isSimple = false;
  22422. isHalfKeyed = !helper->diskAccessRequired();
  22423. indexReadMeta = QUERYINTERFACE(helper->queryIndexReadInputRecordSize(), IOutputMetaData);
  22424. if (!isHalfKeyed && !variableFetchFileName)
  22425. {
  22426. bool isFetchOpt = (helper->getFetchFlags() & FFdatafileoptional) != 0;
  22427. datafile.setown(_queryFactory.queryPackage().lookupFileName(queryNodeFileName(_graphNode, _kind), isFetchOpt, true, true, _queryFactory.queryWorkUnit()));
  22428. if (datafile)
  22429. {
  22430. if (isLocal)
  22431. files.setown(datafile->getIFileIOArray(isFetchOpt, queryFactory.queryChannel()));
  22432. else
  22433. map.setown(datafile->getFileMap());
  22434. }
  22435. }
  22436. }
  22437. virtual bool getEnableFieldTranslation() const
  22438. {
  22439. return enableFieldTranslation;
  22440. }
  22441. virtual IDefRecordMeta *queryActivityMeta() const
  22442. {
  22443. return activityMeta;
  22444. }
  22445. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  22446. {
  22447. if (isHalfKeyed)
  22448. return new CRoxieServerHalfKeyedJoinActivity(this, _probeManager,
  22449. headId, keySet, translatorArray, indexReadMeta, joinFlags, isSimple, isLocal);
  22450. else
  22451. return new CRoxieServerKeyedJoinActivity(this, _probeManager,
  22452. headId, keySet, translatorArray, indexReadMeta,
  22453. tailId, map, joinFlags, isLocal);
  22454. }
  22455. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  22456. {
  22457. if (datafile)
  22458. addXrefFileInfo(reply, datafile);
  22459. if (indexfile)
  22460. addXrefFileInfo(reply, indexfile);
  22461. }
  22462. };
  22463. IRoxieServerActivityFactory *createRoxieServerKeyedJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, const RemoteActivityId &_remoteId2, IPropertyTree &_graphNode)
  22464. {
  22465. return new CRoxieServerKeyedJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _remoteId2, _graphNode);
  22466. }
  22467. //=================================================================================
  22468. class CRoxieServerSoapActivityBase : public CRoxieServerActivity, implements ISoapCallRowProvider, implements IRoxieAbortMonitor
  22469. {
  22470. protected:
  22471. Owned<ISoapCallHelper> soaphelper;
  22472. IHThorSoapActionArg & helper;
  22473. StringBuffer authToken;
  22474. bool eof;
  22475. CriticalSection crit;
  22476. ClientCertificate *pClientCert;
  22477. public:
  22478. IMPLEMENT_IINTERFACE;
  22479. CRoxieServerSoapActivityBase(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  22480. : CRoxieServerActivity(_factory, _probeManager), helper((IHThorSoapActionArg &)basehelper)
  22481. {
  22482. eof = false;
  22483. if (clientCert.certificate.length() > 0 && clientCert.privateKey.length() > 0 && clientCert.passphrase.length() > 0)
  22484. pClientCert = &clientCert;
  22485. else
  22486. pClientCert = NULL;
  22487. }
  22488. // ISoapCallRowProvider
  22489. virtual IHThorSoapActionArg * queryActionHelper() { return &helper; };
  22490. virtual IHThorSoapCallArg * queryCallHelper() { return NULL; };
  22491. virtual const void * getNextRow() { return NULL; };
  22492. virtual void releaseRow(const void * r) { ReleaseRoxieRow(r); };
  22493. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  22494. {
  22495. eof = false;
  22496. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  22497. authToken.append(ctx->queryAuthToken());
  22498. }
  22499. virtual void reset()
  22500. {
  22501. // MORE - Shouldn't we make sure thread is stopped etc???
  22502. soaphelper.clear();
  22503. CRoxieServerActivity::reset();
  22504. }
  22505. // IRoxieAbortMonitor
  22506. virtual void checkForAbort() { checkAbort(); }
  22507. };
  22508. //---------------------------------------------------------------------------
  22509. class CRoxieServerSoapRowCallActivity : public CRoxieServerSoapActivityBase
  22510. {
  22511. IHThorSoapCallArg & callHelper;
  22512. public:
  22513. CRoxieServerSoapRowCallActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  22514. : CRoxieServerSoapActivityBase(_factory, _probeManager), callHelper((IHThorSoapCallArg &)basehelper)
  22515. {
  22516. }
  22517. virtual IHThorSoapCallArg * queryCallHelper()
  22518. {
  22519. return &callHelper;
  22520. }
  22521. virtual bool needsAllocator() const { return true; }
  22522. virtual const void *nextInGroup()
  22523. {
  22524. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  22525. if(eof) return NULL;
  22526. if (soaphelper == NULL)
  22527. {
  22528. if (factory->getKind()==TAKhttp_rowdataset)
  22529. soaphelper.setown(createHttpCallHelper(this, rowAllocator, authToken.str(), SCrow, pClientCert, *ctx, this));
  22530. else
  22531. soaphelper.setown(createSoapCallHelper(this, rowAllocator, authToken.str(), SCrow, pClientCert, *ctx, this));
  22532. soaphelper->start();
  22533. }
  22534. OwnedConstRoxieRow ret = soaphelper->getRow();
  22535. if (!ret)
  22536. {
  22537. eof = true;
  22538. return NULL;
  22539. }
  22540. ++processed;
  22541. return ret.getClear();
  22542. }
  22543. };
  22544. class CRoxieServerSoapRowCallActivityFactory : public CRoxieServerActivityFactory
  22545. {
  22546. public:
  22547. CRoxieServerSoapRowCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  22548. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  22549. {
  22550. }
  22551. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  22552. {
  22553. return new CRoxieServerSoapRowCallActivity(this, _probeManager);
  22554. }
  22555. };
  22556. IRoxieServerActivityFactory *createRoxieServerSoapRowCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  22557. {
  22558. return new CRoxieServerSoapRowCallActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  22559. }
  22560. //---------------------------------------------------------------------------
  22561. class CRoxieServerSoapRowActionActivity : public CRoxieServerSoapActivityBase
  22562. {
  22563. public:
  22564. CRoxieServerSoapRowActionActivity (const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  22565. : CRoxieServerSoapActivityBase(_factory, _probeManager)
  22566. {}
  22567. virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
  22568. {
  22569. //MORE: parentExtract not passed to start - although shouldn't be a problem.
  22570. soaphelper.setown(createSoapCallHelper(this, NULL, ctx->queryAuthToken(), SCrow, pClientCert, *ctx, this));
  22571. soaphelper->start();
  22572. soaphelper->waitUntilDone();
  22573. IException *e = soaphelper->getError();
  22574. soaphelper.clear();
  22575. if (e)
  22576. throw e;
  22577. }
  22578. virtual IRoxieInput *queryOutput(unsigned idx)
  22579. {
  22580. return NULL;
  22581. }
  22582. virtual const void *nextInGroup()
  22583. {
  22584. throwUnexpected(); // I am nobody's input
  22585. }
  22586. };
  22587. class CRoxieServerSoapRowActionActivityFactory : public CRoxieServerActivityFactory
  22588. {
  22589. bool isRoot;
  22590. public:
  22591. CRoxieServerSoapRowActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  22592. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
  22593. {
  22594. }
  22595. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  22596. {
  22597. return new CRoxieServerSoapRowActionActivity(this, _probeManager);
  22598. }
  22599. virtual bool isSink() const
  22600. {
  22601. return isRoot;
  22602. }
  22603. };
  22604. IRoxieServerActivityFactory *createRoxieServerSoapRowActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  22605. {
  22606. return new CRoxieServerSoapRowActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
  22607. }
  22608. //---------------------------------------------------------------------------
  22609. class CRoxieServerSoapDatasetCallActivity : public CRoxieServerSoapActivityBase
  22610. {
  22611. IHThorSoapCallArg & callHelper;
  22612. public:
  22613. CRoxieServerSoapDatasetCallActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  22614. : CRoxieServerSoapActivityBase(_factory, _probeManager), callHelper((IHThorSoapCallArg &)basehelper)
  22615. {
  22616. }
  22617. virtual IHThorSoapCallArg * queryCallHelper()
  22618. {
  22619. return &callHelper;
  22620. }
  22621. virtual const void *getNextRow()
  22622. {
  22623. CriticalBlock b(crit);
  22624. const void *nextrec = input->nextInGroup();
  22625. if (!nextrec)
  22626. {
  22627. nextrec = input->nextInGroup();
  22628. }
  22629. return nextrec;
  22630. }
  22631. virtual bool needsAllocator() const { return true; }
  22632. virtual const void *nextInGroup()
  22633. {
  22634. ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
  22635. if(eof) return NULL;
  22636. if (soaphelper == NULL)
  22637. {
  22638. soaphelper.setown(createSoapCallHelper(this, rowAllocator, authToken.str(), SCdataset, pClientCert, *ctx, this));
  22639. soaphelper->start();
  22640. }
  22641. OwnedConstRoxieRow ret = soaphelper->getRow();
  22642. if (!ret)
  22643. {
  22644. eof = true;
  22645. return NULL;
  22646. }
  22647. ++processed;
  22648. return ret.getClear();
  22649. }
  22650. };
  22651. class CRoxieServerSoapDatasetCallActivityFactory : public CRoxieServerActivityFactory
  22652. {
  22653. public:
  22654. CRoxieServerSoapDatasetCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  22655. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
  22656. {
  22657. }
  22658. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  22659. {
  22660. return new CRoxieServerSoapDatasetCallActivity(this, _probeManager);
  22661. }
  22662. };
  22663. IRoxieServerActivityFactory *createRoxieServerSoapDatasetCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
  22664. {
  22665. return new CRoxieServerSoapDatasetCallActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
  22666. }
  22667. //---------------------------------------------------------------------------
  22668. class CRoxieServerSoapDatasetActionActivity : public CRoxieServerSoapActivityBase
  22669. {
  22670. public:
  22671. CRoxieServerSoapDatasetActionActivity (const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  22672. : CRoxieServerSoapActivityBase(_factory, _probeManager)
  22673. {}
  22674. virtual const void *getNextRow()
  22675. {
  22676. CriticalBlock b(crit);
  22677. const void *nextrec = input->nextInGroup();
  22678. if (!nextrec)
  22679. {
  22680. nextrec = input->nextInGroup();
  22681. }
  22682. if (nextrec)
  22683. processed++;
  22684. return nextrec;
  22685. }
  22686. virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
  22687. {
  22688. try
  22689. {
  22690. start(parentExtractSize, parentExtract, false);
  22691. soaphelper.setown(createSoapCallHelper(this, NULL, ctx->queryAuthToken(), SCdataset, pClientCert, *ctx, this));
  22692. soaphelper->start();
  22693. soaphelper->waitUntilDone();
  22694. IException *e = soaphelper->getError();
  22695. soaphelper.clear();
  22696. if (e)
  22697. throw e;
  22698. stop(false);
  22699. }
  22700. catch (IException *E)
  22701. {
  22702. ctx->notifyAbort(E);
  22703. stop(true);
  22704. throw;
  22705. }
  22706. catch(...)
  22707. {
  22708. Owned<IException> E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught at %s:%d", __FILE__, __LINE__);
  22709. ctx->notifyAbort(E);
  22710. stop(true);
  22711. throw;
  22712. }
  22713. }
  22714. virtual IRoxieInput *queryOutput(unsigned idx)
  22715. {
  22716. return NULL;
  22717. }
  22718. virtual const void *nextInGroup()
  22719. {
  22720. throwUnexpected(); // I am nobody's input
  22721. }
  22722. };
  22723. class CRoxieServerSoapDatasetActionActivityFactory : public CRoxieServerActivityFactory
  22724. {
  22725. bool isRoot;
  22726. public:
  22727. CRoxieServerSoapDatasetActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  22728. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
  22729. {
  22730. }
  22731. virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
  22732. {
  22733. return new CRoxieServerSoapDatasetActionActivity(this, _probeManager);
  22734. }
  22735. virtual bool isSink() const
  22736. {
  22737. return isRoot;
  22738. }
  22739. };
  22740. IRoxieServerActivityFactory *createRoxieServerSoapDatasetActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
  22741. {
  22742. return new CRoxieServerSoapDatasetActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
  22743. }
  22744. //=====================================================================================================
  22745. class CGraphResults : public CInterface, implements IRoxieGraphResults
  22746. {
  22747. IArrayOf<IGraphResult> results;
  22748. CriticalSection cs;
  22749. IGraphResult & select(unsigned idx)
  22750. {
  22751. CriticalBlock procedure(cs);
  22752. if (idx >= results.ordinality())
  22753. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Error reading graph result %d before it is calculated", idx);
  22754. return results.item(idx);
  22755. }
  22756. public:
  22757. IMPLEMENT_IINTERFACE
  22758. void clear()
  22759. {
  22760. CriticalBlock procedure(cs);
  22761. results.kill();
  22762. }
  22763. IRoxieInput * createIterator(unsigned id)
  22764. {
  22765. return select(id).createIterator();
  22766. }
  22767. virtual void getLinkedResult(unsigned & count, byte * * & ret, unsigned id)
  22768. {
  22769. select(id).getLinkedResult(count, ret);
  22770. }
  22771. virtual void getDictionaryResult(unsigned & count, byte * * & ret, unsigned id)
  22772. {
  22773. select(id).getLinkedResult(count, ret);
  22774. }
  22775. virtual const void * getLinkedRowResult(unsigned id)
  22776. {
  22777. return select(id).getLinkedRowResult();
  22778. }
  22779. void setResult(unsigned id, IGraphResult * result)
  22780. {
  22781. CriticalBlock procedure(cs);
  22782. if (results.ordinality() <= id)
  22783. {
  22784. while (results.ordinality() < id)
  22785. results.append(*new CGraphResult);
  22786. results.append(*LINK(result));
  22787. }
  22788. else
  22789. results.replace(*LINK(result), id);
  22790. }
  22791. void appendResult(IGraphResult * result)
  22792. {
  22793. CriticalBlock procedure(cs);
  22794. results.append(*LINK(result));
  22795. }
  22796. };
  22797. //===================================================================================================================
  22798. class CPseudoArg : public CInterface, implements IHThorArg
  22799. {
  22800. public:
  22801. IMPLEMENT_IINTERFACE
  22802. virtual IOutputMetaData * queryOutputMeta() { return NULL; }
  22803. };
  22804. class CPseudoActivity : public CRoxieServerActivity
  22805. {
  22806. public:
  22807. CPseudoActivity(IHThorArg & _helper) : CRoxieServerActivity(_helper) {}
  22808. virtual const void *nextInGroup()
  22809. {
  22810. throwUnexpected(); // I am nobody's input
  22811. }
  22812. };
  22813. class CActivityGraph : public CInterface, implements IActivityGraph, implements IThorChildGraph, implements ILocalGraphEx, implements IRoxieServerChildGraph
  22814. {
  22815. protected:
  22816. IArrayOf<IRoxieServerActivity> activities;
  22817. IArrayOf<IRoxieInput> probes;
  22818. IRoxieServerActivityCopyArray sinks;
  22819. StringAttr graphName;
  22820. Owned<CGraphResults> results;
  22821. CGraphResults graphLoopResults;
  22822. ActivityArray & graphDefinition;
  22823. CriticalSection evaluateCrit;
  22824. IProbeManager *probeManager;
  22825. unsigned id;
  22826. unsigned loopCounter;
  22827. class ActivityGraphSlaveContext : public IndirectSlaveContext
  22828. {
  22829. SpinLock abortLock;
  22830. bool aborted;
  22831. Owned<IException> exception;
  22832. public:
  22833. ActivityGraphSlaveContext(const IRoxieContextLogger &_logctx) : logctx(_logctx), loopCounter(0), codeContext(NULL)
  22834. {
  22835. aborted = false;
  22836. }
  22837. // Note - we must track exceptions at the child level in case there is a CATCH in parent
  22838. virtual void notifyAbort(IException *E)
  22839. {
  22840. SpinBlock b(abortLock);
  22841. if (!aborted && QUERYINTERFACE(E, InterruptedSemaphoreException) == NULL)
  22842. {
  22843. aborted = true;
  22844. exception.set(E);
  22845. }
  22846. }
  22847. virtual void checkAbort()
  22848. {
  22849. if (aborted) // NOTE - don't bother getting lock before reading this (for speed) - a false read is very unlikely and not a problem
  22850. {
  22851. SpinBlock b(abortLock);
  22852. if (!exception)
  22853. exception.setown(MakeStringException(ROXIE_INTERNAL_ERROR, "Query was aborted"));
  22854. throw exception.getLink();
  22855. }
  22856. IndirectSlaveContext::checkAbort();
  22857. }
  22858. virtual ICodeContext *queryCodeContext()
  22859. {
  22860. return codeContext;
  22861. }
  22862. void setCodeContext(ICodeContext * _codeContext)
  22863. {
  22864. codeContext = _codeContext;
  22865. }
  22866. void setLoopCounter(unsigned _loopCounter)
  22867. {
  22868. loopCounter = _loopCounter;
  22869. }
  22870. virtual void noteChildGraph(unsigned id, IActivityGraph *childGraph)
  22871. {
  22872. childGraphs.setValue(id, childGraph);
  22873. }
  22874. virtual IActivityGraph * queryChildGraph(unsigned id)
  22875. {
  22876. if (queryTraceLevel() > 10)
  22877. CTXLOG("resolveChildGraph %d", id);
  22878. IActivityGraph *childGraph = childGraphs.getValue(id);
  22879. assertex(childGraph);
  22880. return childGraph;
  22881. }
  22882. // MORE should really redirect the other log context ones too (though mostly doesn't matter). Really should refactor to have a queryLogContext() method in IRoxieSlaveContext I think
  22883. virtual StringBuffer &getLogPrefix(StringBuffer &ret) const
  22884. {
  22885. logctx.getLogPrefix(ret);
  22886. if (loopCounter)
  22887. ret.appendf("{%u}", loopCounter);
  22888. return ret;
  22889. }
  22890. protected:
  22891. const IRoxieContextLogger &logctx;
  22892. unsigned loopCounter;
  22893. ICodeContext * codeContext;
  22894. MapXToMyClass<unsigned, unsigned, IActivityGraph> childGraphs;
  22895. } graphSlaveContext;
  22896. class ActivityGraphCodeContext : public IndirectCodeContext
  22897. {
  22898. public:
  22899. virtual IEclGraphResults * resolveLocalQuery(__int64 activityId)
  22900. {
  22901. if ((unsigned) activityId == container->queryId())
  22902. return container;
  22903. IActivityGraph * match = slaveContext->queryChildGraph((unsigned) activityId);
  22904. if (match)
  22905. return match->queryLocalGraph();
  22906. return IndirectCodeContext::resolveLocalQuery(activityId);
  22907. }
  22908. virtual IThorChildGraph * resolveChildQuery(__int64 activityId, IHThorArg * colocal)
  22909. {
  22910. IActivityGraph * match = slaveContext->queryChildGraph((unsigned) activityId);
  22911. return LINK(match->queryChildGraph());
  22912. }
  22913. virtual unsigned getGraphLoopCounter() const
  22914. {
  22915. return container->queryLoopCounter(); // only called if value is valid
  22916. }
  22917. void setContainer(IRoxieSlaveContext * _slaveContext, CActivityGraph * _container)
  22918. {
  22919. slaveContext = _slaveContext;
  22920. container = _container;
  22921. }
  22922. protected:
  22923. IRoxieSlaveContext * slaveContext;
  22924. CActivityGraph * container;
  22925. } graphCodeContext;
  22926. public:
  22927. IMPLEMENT_IINTERFACE;
  22928. CActivityGraph(const char *_graphName, unsigned _id, ActivityArray &x, IProbeManager *_probeManager, const IRoxieContextLogger &_logctx)
  22929. : probeManager(_probeManager), graphDefinition(x), graphName(_graphName), graphSlaveContext(_logctx)
  22930. {
  22931. id = x.getLibraryGraphId();
  22932. if (!id)
  22933. id = _id;
  22934. loopCounter = 0;
  22935. graphSlaveContext.setCodeContext(&graphCodeContext);
  22936. graphCodeContext.setContainer(&graphSlaveContext, this);
  22937. }
  22938. ~CActivityGraph()
  22939. {
  22940. if (probeManager)
  22941. probeManager->deleteGraph((IArrayOf<IActivityBase>*)&activities, (IArrayOf<IInputBase>*)&probes);
  22942. }
  22943. virtual const char *queryName() const
  22944. {
  22945. return graphName.get();
  22946. }
  22947. void createGraph()
  22948. {
  22949. ForEachItemIn(idx, graphDefinition)
  22950. {
  22951. IRoxieServerActivityFactory &donor = graphDefinition.serverItem(idx);
  22952. IRoxieServerActivity &activity = *donor.createActivity(probeManager);
  22953. activities.append(activity);
  22954. if (donor.isSink())
  22955. {
  22956. sinks.append(activity);
  22957. if (probeManager)
  22958. probeManager->noteSink(&activity);
  22959. }
  22960. }
  22961. ForEachItemIn(idx1, graphDefinition)
  22962. {
  22963. IRoxieServerActivityFactory &donor = graphDefinition.serverItem(idx1);
  22964. IRoxieServerActivity &activity = activities.item(idx1);
  22965. unsigned inputidx = 0;
  22966. loop
  22967. {
  22968. unsigned outputidx;
  22969. unsigned source = donor.getInput(inputidx, outputidx);
  22970. if (source==(unsigned) -1)
  22971. break;
  22972. connectInput(idx1, inputidx, source, outputidx, 0);
  22973. inputidx++;
  22974. }
  22975. IntArray &dependencies = donor.queryDependencies();
  22976. IntArray &dependencyIndexes = donor.queryDependencyIndexes();
  22977. IntArray &dependencyControlIds = donor.queryDependencyControlIds();
  22978. StringArray &dependencyEdgeIds = donor.queryDependencyEdgeIds();
  22979. ForEachItemIn(idx2, dependencies)
  22980. {
  22981. IRoxieServerActivity &dependencySourceActivity = activities.item(dependencies.item(idx2));
  22982. unsigned dependencySourceIndex = dependencyIndexes.item(idx2);
  22983. unsigned dependencyControlId = dependencyControlIds.item(idx2);
  22984. activity.addDependency(dependencySourceActivity, dependencySourceIndex, dependencyControlId);
  22985. if (probeManager)
  22986. probeManager->noteDependency( &dependencySourceActivity, dependencySourceIndex, dependencyControlId, dependencyEdgeIds.item(idx2), &activity);
  22987. }
  22988. }
  22989. }
  22990. void connectInput(unsigned target, unsigned targetIdx, unsigned source, unsigned sourceIdx, unsigned iteration)
  22991. {
  22992. IRoxieServerActivity &targetActivity = activities.item(target);
  22993. IRoxieServerActivity &sourceActivity = activities.item(source);
  22994. IRoxieInput * output = sourceActivity.queryOutput(sourceIdx);
  22995. if (probeManager)
  22996. {
  22997. IInputBase * inputBase = probeManager->createProbe(static_cast<IInputBase*>(output), &sourceActivity, &targetActivity, sourceIdx, targetIdx, iteration);
  22998. output = static_cast<IRoxieInput*>(inputBase);
  22999. probes.append(*LINK(output));
  23000. }
  23001. targetActivity.setInput(targetIdx, output);
  23002. }
  23003. virtual void onCreate(IRoxieSlaveContext *ctx, IHThorArg *_colocalParent)
  23004. {
  23005. graphSlaveContext.set(ctx);
  23006. if (graphDefinition.isMultiInstance())
  23007. {
  23008. graphCodeContext.set(ctx->queryCodeContext());
  23009. ctx = &graphSlaveContext;
  23010. }
  23011. ForEachItemIn(idx, activities)
  23012. {
  23013. IRoxieServerActivity *activity = &activities.item(idx);
  23014. if (activity)
  23015. activity->onCreate(ctx, _colocalParent);
  23016. }
  23017. }
  23018. virtual void abort()
  23019. {
  23020. ForEachItemIn(idx, sinks)
  23021. {
  23022. IRoxieServerActivity &sink = sinks.item(idx);
  23023. sink.stop(true);
  23024. }
  23025. }
  23026. virtual void reset()
  23027. {
  23028. ForEachItemIn(idx, sinks)
  23029. {
  23030. IRoxieServerActivity &sink = sinks.item(idx);
  23031. sink.reset();
  23032. }
  23033. }
  23034. Linked<IException> exception;
  23035. CriticalSection eCrit;
  23036. virtual void noteException(IException *E)
  23037. {
  23038. CriticalBlock b(eCrit);
  23039. if (!exception)
  23040. {
  23041. if (graphSlaveContext.queryDebugContext())
  23042. {
  23043. graphSlaveContext.queryDebugContext()->checkBreakpoint(DebugStateException, NULL, exception);
  23044. }
  23045. exception.set(E);
  23046. }
  23047. }
  23048. virtual void checkAbort()
  23049. {
  23050. CriticalBlock b(eCrit);
  23051. if (exception)
  23052. throw exception.getLink();
  23053. }
  23054. virtual void execute()
  23055. {
  23056. results.setown(new CGraphResults);
  23057. doExecute(0, NULL);
  23058. }
  23059. //New child query code...
  23060. virtual IThorChildGraph * queryChildGraph()
  23061. {
  23062. return this;
  23063. }
  23064. virtual IEclGraphResults * queryLocalGraph()
  23065. {
  23066. return this;
  23067. }
  23068. virtual IRoxieServerChildGraph * queryLoopGraph()
  23069. {
  23070. return this;
  23071. }
  23072. inline unsigned queryId() const
  23073. {
  23074. return id;
  23075. }
  23076. inline unsigned queryLoopCounter() const
  23077. {
  23078. return loopCounter;
  23079. }
  23080. void doExecute(unsigned parentExtractSize, const byte * parentExtract)
  23081. {
  23082. if (sinks.ordinality()==1)
  23083. sinks.item(0).execute(parentExtractSize, parentExtract);
  23084. #ifdef PARALLEL_EXECUTE
  23085. else if (!probeManager && !graphDefinition.isSequential())
  23086. {
  23087. class casyncfor: public CAsyncFor
  23088. {
  23089. public:
  23090. IActivityGraph &parent;
  23091. unsigned parentExtractSize;
  23092. const byte * parentExtract;
  23093. casyncfor(IRoxieServerActivityCopyArray &_sinks, IActivityGraph &_parent, unsigned _parentExtractSize, const byte * _parentExtract) :
  23094. sinks(_sinks), parent(_parent), parentExtractSize(_parentExtractSize), parentExtract(_parentExtract) { }
  23095. void Do(unsigned i)
  23096. {
  23097. try
  23098. {
  23099. sinks.item(i).execute(parentExtractSize, parentExtract);
  23100. }
  23101. catch (IException *E)
  23102. {
  23103. parent.noteException(E);
  23104. throw;
  23105. }
  23106. }
  23107. private:
  23108. IRoxieServerActivityCopyArray &sinks;
  23109. } afor(sinks, *this, parentExtractSize, parentExtract);
  23110. afor.For(sinks.ordinality(), sinks.ordinality());
  23111. }
  23112. #endif
  23113. else
  23114. {
  23115. ForEachItemIn(idx, sinks)
  23116. {
  23117. IRoxieServerActivity &sink = sinks.item(idx);
  23118. sink.execute(parentExtractSize, parentExtract);
  23119. }
  23120. }
  23121. }
  23122. virtual IEclGraphResults *evaluate(unsigned parentExtractSize, const byte * parentExtract)
  23123. {
  23124. CriticalBlock block(evaluateCrit);
  23125. results.setown(new CGraphResults);
  23126. try
  23127. {
  23128. doExecute(parentExtractSize, parentExtract);
  23129. }
  23130. catch (...)
  23131. {
  23132. DBGLOG("Exception thrown in child query - cleaning up");
  23133. reset();
  23134. throw;
  23135. }
  23136. reset();
  23137. return results.getClear();
  23138. }
  23139. //interface IRoxieServerChildGraph
  23140. virtual void beforeExecute()
  23141. {
  23142. results.setown(new CGraphResults);
  23143. }
  23144. virtual IRoxieInput * startOutput(unsigned id, unsigned parentExtractSize, const byte *parentExtract, bool paused)
  23145. {
  23146. IRoxieInput * ret = selectOutput(id);
  23147. ret->start(parentExtractSize, parentExtract, paused);
  23148. return ret;
  23149. }
  23150. virtual IRoxieInput * selectOutput(unsigned id)
  23151. {
  23152. ForEachItemIn(i, sinks)
  23153. {
  23154. IRoxieInput * ret = sinks.item(i).querySelectOutput(id);
  23155. if (ret)
  23156. return ret;
  23157. }
  23158. throwUnexpected();
  23159. return NULL;
  23160. }
  23161. virtual void setInputResult(unsigned id, IGraphResult * result)
  23162. {
  23163. results->setResult(id, result);
  23164. }
  23165. virtual bool querySetInputResult(unsigned id, IRoxieInput * input)
  23166. {
  23167. ForEachItemIn(i, activities)
  23168. {
  23169. if (activities.item(i).querySetStreamInput(id, input))
  23170. return true;
  23171. }
  23172. return false;
  23173. }
  23174. virtual void stopUnusedOutputs()
  23175. {
  23176. //Hmm not sure how to do this...
  23177. }
  23178. virtual void afterExecute()
  23179. {
  23180. ForEachItemIn(i, sinks)
  23181. {
  23182. sinks.item(i).stop(false);
  23183. }
  23184. if (graphSlaveContext.queryDebugContext())
  23185. {
  23186. graphSlaveContext.queryDebugContext()->checkBreakpoint(DebugStateGraphFinished, NULL, NULL);
  23187. }
  23188. reset();
  23189. }
  23190. virtual IRoxieGraphResults * execute(size32_t parentExtractSize, const byte *parentExtract)
  23191. {
  23192. doExecute(parentExtractSize, parentExtract);
  23193. return LINK(results);
  23194. }
  23195. virtual void getLinkedResult(unsigned & count, byte * * & ret, unsigned id)
  23196. {
  23197. results->getLinkedResult(count, ret, id);
  23198. }
  23199. virtual void getDictionaryResult(unsigned & count, byte * * & ret, unsigned id)
  23200. {
  23201. results->getLinkedResult(count, ret, id);
  23202. }
  23203. virtual const void * getLinkedRowResult(unsigned id)
  23204. {
  23205. return results->getLinkedRowResult(id);
  23206. }
  23207. virtual void setResult(unsigned id, IGraphResult * result)
  23208. {
  23209. results->setResult(id, result);
  23210. }
  23211. virtual IRoxieInput * createResultIterator(unsigned id)
  23212. {
  23213. return results->createIterator(id);
  23214. }
  23215. virtual void setGraphLoopResult(IGraphResult * result)
  23216. {
  23217. graphLoopResults.appendResult(result);
  23218. }
  23219. virtual IRoxieInput * createGraphLoopResultIterator(unsigned id)
  23220. {
  23221. try
  23222. {
  23223. return graphLoopResults.createIterator(id);
  23224. }
  23225. catch (IException * e)
  23226. {
  23227. e->Release();
  23228. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Error reading graph result %d before it is calculated", id);
  23229. }
  23230. }
  23231. virtual void clearGraphLoopResults()
  23232. {
  23233. graphLoopResults.clear();
  23234. }
  23235. virtual void executeGraphLoop(size32_t parentExtractSize, const byte *parentExtract)
  23236. {
  23237. doExecute(parentExtractSize, parentExtract);
  23238. }
  23239. virtual void setGraphLoopResult(unsigned id, IGraphResult * result)
  23240. {
  23241. graphLoopResults.setResult(id, result);
  23242. }
  23243. virtual IRoxieInput * getGraphLoopResult(unsigned id)
  23244. {
  23245. return graphLoopResults.createIterator(id);
  23246. }
  23247. virtual void getProbeResponse(IPropertyTree *query)
  23248. {
  23249. if (probeManager)
  23250. probeManager->getProbeResponse(query);
  23251. }
  23252. virtual IRoxieServerChildGraph * createGraphLoopInstance(unsigned loopCounter, unsigned parentExtractSize, const byte * parentExtract, const IRoxieContextLogger &logctx)
  23253. {
  23254. throwUnexpected();
  23255. }
  23256. virtual CGraphIterationInfo *selectGraphLoopOutput()
  23257. {
  23258. return NULL;
  23259. }
  23260. virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor)
  23261. {
  23262. throwUnexpected();
  23263. }
  23264. virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor)
  23265. {
  23266. throwUnexpected();
  23267. }
  23268. };
  23269. class CIterationActivityGraph : public CActivityGraph
  23270. {
  23271. IHThorArg * colocalParent;
  23272. unsigned fixedParentExtractSize;
  23273. const byte * fixedParentExtract;
  23274. unsigned graphOutputActivityIndex;
  23275. public:
  23276. CIterationActivityGraph(const char *_graphName, unsigned _id, ActivityArray &x, IProbeManager *_probeManager,
  23277. unsigned _loopCounter, IRoxieSlaveContext *ctx, IHThorArg * _colocalParent, unsigned parentExtractSize, const byte * parentExtract, const IRoxieContextLogger &_logctx)
  23278. : CActivityGraph(_graphName, _id, x, _probeManager, _logctx)
  23279. {
  23280. graphOutputActivityIndex = 0;
  23281. loopCounter = _loopCounter;
  23282. colocalParent = _colocalParent;
  23283. graphSlaveContext.set(ctx);
  23284. graphSlaveContext.setLoopCounter(loopCounter);
  23285. graphCodeContext.set(ctx->queryCodeContext());
  23286. fixedParentExtractSize = parentExtractSize;
  23287. fixedParentExtract = parentExtract;
  23288. }
  23289. void createIterationGraph()
  23290. {
  23291. Owned<IRoxieServerActivity> pseudoActivity = new CPseudoActivity(*new CPseudoArg);
  23292. ForEachItemIn(idx1, graphDefinition)
  23293. activities.append(*LINK(pseudoActivity));
  23294. graphOutputActivityIndex = queryGraphOutputIndex();
  23295. recursiveCreateGraph(graphOutputActivityIndex);
  23296. }
  23297. unsigned queryGraphOutputIndex() const
  23298. {
  23299. ForEachItemIn(i, graphDefinition)
  23300. if (graphDefinition.serverItem(i).getKind() == TAKgraphloopresultwrite)
  23301. return i;
  23302. throwUnexpected();
  23303. }
  23304. void recursiveCreateGraph(unsigned whichActivity)
  23305. {
  23306. //Check to see if already created
  23307. IRoxieServerActivity & prevActivity = activities.item(whichActivity);
  23308. if (prevActivity.queryId() != 0)
  23309. {
  23310. prevActivity.noteOutputUsed(); //We need to patch up the number of outputs for splitters.
  23311. return;
  23312. }
  23313. IRoxieServerActivityFactory &donor = graphDefinition.serverItem(whichActivity);
  23314. IRoxieServerActivity * activity = NULL;
  23315. if (donor.isGraphInvariant())
  23316. {
  23317. ThorActivityKind kind = donor.getKind();
  23318. switch (kind)
  23319. {
  23320. case TAKif:
  23321. case TAKchildif:
  23322. case TAKcase:
  23323. case TAKchildcase: // MORE RKC->GH - what about FILTER with a graph-invariant condition and other latestart activities?
  23324. {
  23325. Owned<IHThorArg> helper = &donor.getHelper();
  23326. helper->onCreate(&graphCodeContext, colocalParent, NULL);
  23327. helper->onStart(fixedParentExtract, NULL);
  23328. unsigned branch;
  23329. switch (kind)
  23330. {
  23331. case TAKif: case TAKchildif:
  23332. branch = static_cast<IHThorIfArg *>(helper.get())->getCondition() ? 0 : 1;
  23333. break;
  23334. case TAKcase: case TAKchildcase:
  23335. branch = static_cast<IHThorCaseArg *>(helper.get())->getBranch();
  23336. if (branch >= donor.numInputs())
  23337. branch = donor.numInputs() - 1;
  23338. break;
  23339. default:
  23340. throwUnexpected();
  23341. }
  23342. helper.clear();
  23343. unsigned outputidx;
  23344. unsigned source = donor.getInput(branch, outputidx);
  23345. if (source ==(unsigned) -1)
  23346. activity = createRoxieServerNullActivity(&donor, probeManager);
  23347. else
  23348. activity = createRoxieServerPassThroughActivity(&donor, probeManager);
  23349. activities.replace(*activity, whichActivity);
  23350. activity->onCreate(&graphSlaveContext, colocalParent);
  23351. if (source ==(unsigned) -1)
  23352. return;
  23353. recursiveCreateGraph(source);
  23354. connectInput(whichActivity, 0, source, outputidx, loopCounter);
  23355. break;
  23356. }
  23357. }
  23358. }
  23359. if (!activity)
  23360. {
  23361. activity = donor.createActivity(probeManager);
  23362. activities.replace(*activity, whichActivity);
  23363. activity->onCreate(&graphSlaveContext, colocalParent);
  23364. activity->resetOutputsUsed();
  23365. unsigned inputidx = 0;
  23366. loop
  23367. {
  23368. unsigned outputidx;
  23369. unsigned source = donor.getInput(inputidx, outputidx);
  23370. if (source==(unsigned) -1)
  23371. break;
  23372. recursiveCreateGraph(source);
  23373. connectInput(whichActivity, inputidx, source, outputidx, loopCounter);
  23374. inputidx++;
  23375. }
  23376. }
  23377. IntArray &dependencies = donor.queryDependencies();
  23378. IntArray &dependencyIndexes = donor.queryDependencyIndexes();
  23379. IntArray &dependencyControlIds = donor.queryDependencyControlIds();
  23380. ForEachItemIn(idx2, dependencies)
  23381. {
  23382. unsigned input = dependencies.item(idx2);
  23383. recursiveCreateGraph(input);
  23384. activity->addDependency(activities.item(input),dependencyIndexes.item(idx2),dependencyControlIds.item(idx2));
  23385. }
  23386. }
  23387. virtual CGraphIterationInfo *selectGraphLoopOutput()
  23388. {
  23389. IRoxieServerActivity &sourceActivity = activities.item(graphOutputActivityIndex);
  23390. return new CGraphIterationInfo(&sourceActivity, sourceActivity.queryOutput(0), 0, loopCounter);
  23391. }
  23392. virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor)
  23393. {
  23394. ForEachItemIn(i, activities)
  23395. activities.item(i).gatherIterationUsage(processor, fixedParentExtractSize, fixedParentExtract);
  23396. }
  23397. virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor)
  23398. {
  23399. ForEachItemIn(i, activities)
  23400. activities.item(i).associateIterationOutputs(processor, fixedParentExtractSize, fixedParentExtract, probeManager, probes);
  23401. }
  23402. };
  23403. class CDelayedActivityGraph : public CInterface, implements IActivityGraph
  23404. {
  23405. StringAttr graphName;
  23406. ActivityArray & graphDefinition;
  23407. IProbeManager *probeManager;
  23408. unsigned id;
  23409. IRoxieSlaveContext * ctx;
  23410. IHThorArg * colocalParent;
  23411. public:
  23412. IMPLEMENT_IINTERFACE;
  23413. CDelayedActivityGraph(const char *_graphName, unsigned _id, ActivityArray &x, IProbeManager *_probeManager)
  23414. : probeManager(_probeManager), graphDefinition(x)
  23415. {
  23416. graphName.set(_graphName);
  23417. id = _id;
  23418. ctx = NULL;
  23419. colocalParent = NULL;
  23420. }
  23421. virtual const char *queryName() const { return graphName.get(); }
  23422. virtual void abort() { throwUnexpected(); }
  23423. virtual void reset() { }
  23424. virtual void execute() { throwUnexpected(); }
  23425. virtual void getProbeResponse(IPropertyTree *query) { throwUnexpected(); }
  23426. virtual void noteException(IException *E) { throwUnexpected(); }
  23427. virtual void checkAbort() { throwUnexpected(); }
  23428. virtual IThorChildGraph * queryChildGraph() { throwUnexpected(); }
  23429. virtual IEclGraphResults * queryLocalGraph() { throwUnexpected(); }
  23430. virtual IRoxieServerChildGraph * queryLoopGraph() { throwUnexpected(); }
  23431. virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
  23432. {
  23433. ctx = _ctx;
  23434. colocalParent = _colocalParent;
  23435. }
  23436. virtual IRoxieServerChildGraph * createGraphLoopInstance(unsigned loopCounter, unsigned parentExtractSize, const byte * parentExtract, const IRoxieContextLogger &logctx)
  23437. {
  23438. Owned<CIterationActivityGraph> ret = new CIterationActivityGraph(graphName, id, graphDefinition, probeManager, loopCounter, ctx, colocalParent, parentExtractSize, parentExtract, logctx);
  23439. ret->createIterationGraph();
  23440. return ret.getClear();
  23441. }
  23442. };
  23443. IActivityGraph *createActivityGraph(const char *_graphName, unsigned id, ActivityArray &childFactories, IRoxieServerActivity *parentActivity, IProbeManager *_probeManager, const IRoxieContextLogger &_logctx)
  23444. {
  23445. if (childFactories.isDelayed())
  23446. {
  23447. return new CDelayedActivityGraph(_graphName, id, childFactories, _probeManager);
  23448. }
  23449. else
  23450. {
  23451. Owned<IProbeManager> childProbe;
  23452. if (_probeManager)
  23453. childProbe.setown(_probeManager->startChildGraph(id, parentActivity));
  23454. Owned<CActivityGraph> ret = new CActivityGraph(_graphName, id, childFactories, childProbe, _logctx);
  23455. ret->createGraph();
  23456. if (_probeManager)
  23457. _probeManager->endChildGraph(childProbe, parentActivity);
  23458. return ret.getClear();
  23459. }
  23460. }
  23461. //================================================================================================================
  23462. #ifdef _USE_CPPUNIT
  23463. #include "unittests.hpp"
  23464. // There is a bug in VC6 implemetation of protected which prevents nested classes from accessing owner's data. It can be tricky to work around - hence...
  23465. #if _MSC_VER==1200
  23466. #undef protected
  23467. #endif
  23468. static const char *sortAlgorithm;
  23469. class TestMetaData : public CInterface, implements IOutputMetaData
  23470. {
  23471. public:
  23472. IMPLEMENT_IINTERFACE;
  23473. virtual size32_t getRecordSize(const void *) { return 10; }
  23474. virtual size32_t getMinRecordSize() const { return 10; }
  23475. virtual size32_t getFixedSize() const { return 10; }
  23476. virtual void toXML(const byte * self, IXmlWriter & out) {}
  23477. virtual unsigned getVersion() const { return OUTPUTMETADATA_VERSION; }
  23478. virtual unsigned getMetaFlags() { return 0; }
  23479. virtual void destruct(byte * self) {}
  23480. virtual IOutputRowSerializer * createDiskSerializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  23481. virtual IOutputRowDeserializer * createDiskDeserializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  23482. virtual ISourceRowPrefetcher * createDiskPrefetcher(ICodeContext * ctx, unsigned activityId) { return NULL; }
  23483. virtual IOutputMetaData * querySerializedDiskMeta() { return NULL; }
  23484. virtual IOutputRowSerializer * createInternalSerializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  23485. virtual IOutputRowDeserializer * createInternalDeserializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  23486. virtual void walkIndirectMembers(const byte * self, IIndirectMemberVisitor & visitor) {}
  23487. virtual IOutputMetaData * queryChildMeta(unsigned i) { return NULL; }
  23488. } testMeta;
  23489. class TestInput : public CInterface, implements IRoxieInput
  23490. {
  23491. char const * const *input;
  23492. IRoxieSlaveContext *ctx;
  23493. unsigned endSeen;
  23494. bool eof;
  23495. unsigned count;
  23496. unsigned __int64 totalCycles;
  23497. size32_t recordSize;
  23498. unsigned activityId;
  23499. public:
  23500. enum { STATEreset, STATEstarted, STATEstopped } state;
  23501. bool allRead;
  23502. IMPLEMENT_IINTERFACE;
  23503. TestInput(IRoxieSlaveContext *_ctx, char const * const *_input)
  23504. {
  23505. ctx = _ctx;
  23506. input = _input;
  23507. count = 0;
  23508. eof = false;
  23509. allRead = false;
  23510. endSeen = 0;
  23511. recordSize = testMeta.getFixedSize();
  23512. state = STATEreset;
  23513. totalCycles = 0;
  23514. activityId = 1;
  23515. }
  23516. virtual IOutputMetaData * queryOutputMeta() const { return &testMeta; }
  23517. virtual void prestart(unsigned parentExtractSize, const byte *parentExtract)
  23518. {
  23519. ASSERT(state == STATEreset);
  23520. }
  23521. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  23522. {
  23523. ASSERT(state == STATEreset);
  23524. state = STATEstarted;
  23525. }
  23526. virtual IRoxieServerActivity *queryActivity()
  23527. {
  23528. throwUnexpected();
  23529. }
  23530. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  23531. {
  23532. throwUnexpected();
  23533. }
  23534. virtual void stop(bool aborting)
  23535. {
  23536. state = STATEstopped;
  23537. }
  23538. virtual void reset()
  23539. {
  23540. ASSERT(state == STATEstopped);
  23541. eof = false; count = 0; endSeen = 0; allRead = false; state = STATEreset; totalCycles = 0;
  23542. }
  23543. virtual void resetEOF()
  23544. {
  23545. throwUnexpected();
  23546. }
  23547. virtual void checkAbort() {}
  23548. virtual unsigned queryId() const { return activityId; };
  23549. virtual const void *nextInGroup()
  23550. {
  23551. ActivityTimer t(totalCycles, ctx->queryTimeActivities(), ctx->queryDebugContext());
  23552. ASSERT(state == STATEstarted);
  23553. ASSERT(allRead || !eof);
  23554. if (eof)
  23555. return NULL;
  23556. const char *nextSource = input[count++];
  23557. if (nextSource)
  23558. {
  23559. endSeen = 0;
  23560. void *ret = ctx->queryRowManager().ALLOCATE(recordSize);
  23561. memset(ret, 0, recordSize);
  23562. strncpy((char *) ret, nextSource, recordSize);
  23563. return ret;
  23564. }
  23565. else
  23566. {
  23567. endSeen++;
  23568. if (endSeen==2)
  23569. eof = true;
  23570. return NULL;
  23571. }
  23572. }
  23573. virtual bool nextGroup(ConstPointerArray & group)
  23574. {
  23575. const void * next;
  23576. while ((next = nextInGroup()) != NULL)
  23577. group.append(next);
  23578. if (group.ordinality())
  23579. return true;
  23580. return false;
  23581. }
  23582. virtual unsigned __int64 queryTotalCycles() const { return totalCycles; }
  23583. virtual unsigned __int64 queryLocalCycles() const { return totalCycles; }
  23584. virtual IRoxieInput *queryInput(unsigned idx) const
  23585. {
  23586. return NULL;
  23587. }
  23588. };
  23589. struct SortActivityTest : public ccdserver_hqlhelper::CThorSortArg {
  23590. public:
  23591. struct CompareClass : public ICompare {
  23592. virtual int docompare(const void * _left, const void * _right) const {
  23593. return memcmp(_left, _right, 5);
  23594. }
  23595. } compare;
  23596. virtual ICompare * queryCompare() { return &compare; }
  23597. virtual IOutputMetaData * queryOutputMeta()
  23598. {
  23599. return &testMeta;
  23600. }
  23601. virtual unsigned getAlgorithmFlags() { return TAFunstable; }
  23602. virtual const char * getAlgorithm() { return sortAlgorithm; }
  23603. };
  23604. extern "C" IHThorArg * sortActivityTestFactory() { return new SortActivityTest; }
  23605. struct MergeActivityTest : public ccdserver_hqlhelper::CThorMergeArg {
  23606. static bool isDedup;
  23607. public:
  23608. struct CompareClass : public ICompare {
  23609. virtual int docompare(const void * _left, const void * _right) const {
  23610. return memcmp(_left, _right, 5);
  23611. }
  23612. } compare;
  23613. virtual ICompare * queryCompare() { return &compare; }
  23614. virtual IOutputMetaData * queryOutputMeta()
  23615. {
  23616. return &testMeta;
  23617. }
  23618. virtual bool dedup() { return isDedup; }
  23619. };
  23620. bool MergeActivityTest::isDedup = false;
  23621. extern "C" IHThorArg * mergeActivityTestFactory() { return new MergeActivityTest; }
  23622. class CcdServerTest : public CppUnit::TestFixture
  23623. {
  23624. CPPUNIT_TEST_SUITE(CcdServerTest);
  23625. CPPUNIT_TEST(testSetup);
  23626. CPPUNIT_TEST(testHeapSort);
  23627. CPPUNIT_TEST(testInsertionSort);
  23628. CPPUNIT_TEST(testQuickSort);
  23629. CPPUNIT_TEST(testMerge);
  23630. CPPUNIT_TEST(testMergeDedup);
  23631. CPPUNIT_TEST(testMiscellaneous);
  23632. CPPUNIT_TEST(testCleanup);
  23633. CPPUNIT_TEST_SUITE_END();
  23634. protected:
  23635. SlaveContextLogger logctx;
  23636. Owned<const IQueryDll> queryDll;
  23637. Owned<IRoxiePackage> package;
  23638. Owned<IRoxieSlaveContext> ctx;
  23639. Owned<IQueryFactory> queryFactory;
  23640. void testSetup()
  23641. {
  23642. roxiemem::setTotalMemoryLimit(false, 100 * 1024 * 1024, 0, NULL);
  23643. }
  23644. void testCleanup()
  23645. {
  23646. roxiemem::releaseRoxieHeap();
  23647. }
  23648. void init()
  23649. {
  23650. package.setown(createRoxiePackage(NULL, NULL));
  23651. ctx.setown(createSlaveContext(NULL, logctx, 0, 50*1024*1024, NULL));
  23652. queryDll.setown(createExeQueryDll("roxie"));
  23653. queryFactory.setown(createServerQueryFactory("test", queryDll.getLink(), *package, NULL, false, false));
  23654. timer->reset();
  23655. }
  23656. void testActivity(IRoxieServerActivity *activity, char const * const *input, char const * const *output)
  23657. {
  23658. testActivity(activity, input, NULL, output);
  23659. }
  23660. void testActivity(IRoxieServerActivity *activity, char const * const *input, char const * const *input2, char const * const *output)
  23661. {
  23662. TestInput in(ctx, input);
  23663. TestInput in2(ctx, input2);
  23664. IRoxieInput *out = activity->queryOutput(0);
  23665. IOutputMetaData *meta = out->queryOutputMeta();
  23666. activity->setInput(0, &in);
  23667. if (input2)
  23668. activity->setInput(1, &in2);
  23669. void *buf = alloca(meta->getFixedSize());
  23670. for (unsigned iteration = 0; iteration < 8; iteration++)
  23671. {
  23672. // All activities should be able to be restarted multiple times in the same context (for child queries) or in a new context (for graph pooling, if we ever wanted it)
  23673. // This should be true whether we read all, some, or none of the data.
  23674. // Should not matter if an activity is not started
  23675. if (iteration % 4 == 0)
  23676. activity->onCreate(ctx, NULL);
  23677. unsigned count = 0;
  23678. if (iteration % 4 != 3)
  23679. {
  23680. activity->start(0, NULL, false);
  23681. ASSERT(in.state == TestInput::STATEstarted);
  23682. ASSERT(!input2 || in2.state == TestInput::STATEstarted);
  23683. loop
  23684. {
  23685. const void *next = out->nextInGroup();
  23686. if (!next)
  23687. {
  23688. ASSERT(output[count++] == NULL);
  23689. next = out->nextInGroup();
  23690. if (!next)
  23691. {
  23692. ASSERT(output[count++] == NULL);
  23693. break;
  23694. }
  23695. }
  23696. ASSERT(output[count] != NULL);
  23697. unsigned outsize = meta->getRecordSize(next);
  23698. memset(buf, 0, outsize);
  23699. strncpy((char *) buf, output[count++], outsize);
  23700. ASSERT(memcmp(next, buf, outsize) == 0);
  23701. ReleaseRoxieRow(next);
  23702. if (iteration % 4 == 2)
  23703. break;
  23704. }
  23705. if (iteration % 4 != 2)
  23706. {
  23707. // Check that reading after end is harmless
  23708. in.allRead = true;
  23709. const void *next = out->nextInGroup();
  23710. ASSERT(next == NULL);
  23711. }
  23712. }
  23713. activity->stop(false);
  23714. ASSERT(in.state == TestInput::STATEstopped);
  23715. ASSERT(!input2 || in2.state == TestInput::STATEstopped);
  23716. activity->reset();
  23717. ASSERT(in.state == TestInput::STATEreset);
  23718. ASSERT(!input2 || in2.state == TestInput::STATEreset);
  23719. ctx->queryRowManager().reportLeaks();
  23720. ASSERT(ctx->queryRowManager().numPagesAfterCleanup(true) == 0);
  23721. }
  23722. }
  23723. static int compareFunc(const void *l, const void *r)
  23724. {
  23725. return strcmp(*(char **) l, *(char **) r);
  23726. }
  23727. void testSort(unsigned type)
  23728. {
  23729. init();
  23730. sortAlgorithm = NULL;
  23731. if (type==2)
  23732. sortAlgorithm = "heapSort";
  23733. else if (type == 1)
  23734. sortAlgorithm = "insertionSort";
  23735. else
  23736. sortAlgorithm = "quickSort";
  23737. DBGLOG("Testing %s activity", sortAlgorithm);
  23738. Owned <IRoxieServerActivityFactory> factory = createRoxieServerSortActivityFactory(1, 1, *queryFactory, sortActivityTestFactory, TAKsort);
  23739. Owned <IRoxieServerActivity> activity = factory->createActivity(NULL);
  23740. const char * test[] = { NULL, NULL };
  23741. const char * test12345[] = { "1", "2", "3", "4", "5", NULL, NULL };
  23742. const char * test54321[] = { "5", "4", "3", "2", "1", NULL, NULL };
  23743. const char * test11111[] = { "1", "1", "1", "1", "1", NULL, NULL };
  23744. const char * test11111_12345[] = { "1", "1", "1", "1", "1", NULL, "1", "2", "3", "4", "5", NULL, NULL };
  23745. const char * test11111_54321[] = { "1", "1", "1", "1", "1", NULL, "5", "4", "3", "2", "1", NULL, NULL };
  23746. const char * test54321_54321[] = { "5", "4", "3", "2", "1", NULL, "5", "4", "3", "2", "1", NULL, NULL };
  23747. const char * test12345_12345[] = { "1", "2", "3", "4", "5", NULL, "1", "2", "3", "4", "5", NULL, NULL };
  23748. testActivity(activity, test, test);
  23749. testActivity(activity, test12345, test12345);
  23750. testActivity(activity, test54321, test12345);
  23751. testActivity(activity, test11111, test11111);
  23752. testActivity(activity, test11111_12345, test11111_12345);
  23753. testActivity(activity, test11111_54321, test11111_12345);
  23754. testActivity(activity, test54321_54321, test12345_12345);
  23755. // A few larger tests
  23756. char *input[2002];
  23757. char *output[2002];
  23758. input[2000] = input[2001] = output[2000] = output[2001] = NULL;
  23759. unsigned i;
  23760. // identical
  23761. for (i=0; i<2000; i++)
  23762. {
  23763. input[i] = new char[11];
  23764. output[i] = new char[11];
  23765. sprintf(input[i], "1");
  23766. sprintf(output[i], "1");
  23767. }
  23768. testActivity(activity, input, output);
  23769. // Ascending
  23770. for (i=0; i<2000; i++)
  23771. {
  23772. sprintf(input[i], "%04d", i);
  23773. sprintf(output[i], "%04d", i);
  23774. }
  23775. testActivity(activity, input, output);
  23776. // Almost sorted
  23777. for (i=0; i<20; i++)
  23778. {
  23779. unsigned h = i*100;
  23780. sprintf(input[h], "%04d", 1900-h);
  23781. }
  23782. testActivity(activity, input, output);
  23783. // Descending
  23784. for (i=0; i<2000; i++)
  23785. {
  23786. sprintf(input[i], "%04d", 1999-i);
  23787. sprintf(output[i], "%04d", i);
  23788. }
  23789. testActivity(activity, input, output);
  23790. // Random
  23791. for (i=0; i<2000; i++)
  23792. {
  23793. unsigned r = rand() % 1500;
  23794. sprintf(input[i], "%04d", r);
  23795. sprintf(output[i], "%04d", r);
  23796. }
  23797. qsort(output, 2000, sizeof(output[0]), compareFunc);
  23798. testActivity(activity, input, output);
  23799. #if 0
  23800. // Random
  23801. #define BIGSORTSIZE 1000000
  23802. char **linput = new char*[BIGSORTSIZE +2];
  23803. char **loutput = new char *[BIGSORTSIZE+2];
  23804. linput[BIGSORTSIZE] = linput[BIGSORTSIZE+1] = loutput[BIGSORTSIZE] = loutput[BIGSORTSIZE+1] = NULL;
  23805. for (i=0; i<BIGSORTSIZE; i++)
  23806. {
  23807. unsigned r = rand() % 15000;
  23808. linput[i] = loutput[i] = new char[11];
  23809. sprintf(linput[i], "%04d", r);
  23810. }
  23811. qsort(loutput, BIGSORTSIZE, 4, compareFunc);
  23812. testActivity(activity, linput, loutput);
  23813. for (i=0; i<BIGSORTSIZE; i++)
  23814. {
  23815. delete [] linput[i];
  23816. }
  23817. delete [] linput;
  23818. delete [] loutput;
  23819. #endif
  23820. unsigned __int64 us = cycle_to_nanosec(factory->queryLocalCycles()/1000);
  23821. DBGLOG("Simple %s sorts: activity time %u.%u ms", type==2?"Heap" : (type==1 ? "Insertion" : "Quick"), (int)(us/1000), (int)(us%1000));
  23822. factory->resetNodeProgressInfo();
  23823. if (type)
  23824. {
  23825. // Other than quicksort, it's supposed to be stable. Let's check that it is
  23826. // All sort identical
  23827. for (i=0; i<2000; i++)
  23828. {
  23829. sprintf(input[i], "1 %d", i);
  23830. sprintf(output[i], "1 %d", i);
  23831. }
  23832. testActivity(activity, input, output);
  23833. // Already sorted
  23834. for (i=0; i<2000; i++)
  23835. {
  23836. sprintf(input[i], "%04d %d", i / 10, i);
  23837. sprintf(output[i], "%04d %d", i / 10, i);
  23838. }
  23839. testActivity(activity, input, output);
  23840. // Reverse order
  23841. for (i=0; i<2000; i++)
  23842. {
  23843. sprintf(input[i], "%04d %d", 199 - (i / 10), i%10);
  23844. sprintf(output[i], "%04d %d", i / 10, i%10);
  23845. }
  23846. testActivity(activity, input, output);
  23847. }
  23848. for (i=0; i<2000; i++)
  23849. {
  23850. delete [] input[i];
  23851. delete [] output[i];
  23852. }
  23853. DBGLOG("Finished testing %s sort", type==2?"Heap" : (type==1 ? "Insertion" : "Quick"));
  23854. }
  23855. void testQuickSort()
  23856. {
  23857. testSort(0);
  23858. }
  23859. void testInsertionSort()
  23860. {
  23861. testSort(1);
  23862. }
  23863. void testHeapSort()
  23864. {
  23865. testSort(2);
  23866. }
  23867. void testMerge()
  23868. {
  23869. DBGLOG("testMerge");
  23870. init();
  23871. Owned <IRoxieServerActivityFactory> factory = createRoxieServerMergeActivityFactory(1, 1, *queryFactory, mergeActivityTestFactory, TAKmerge);
  23872. factory->setInput(0,0,0);
  23873. factory->setInput(1,0,0);
  23874. Owned <IRoxieServerActivity> activity = factory->createActivity(NULL);
  23875. const char * test[] = { NULL, NULL };
  23876. const char * test12345[] = { "1", "2", "3", "4", "5", NULL, NULL };
  23877. const char * test1122334455[] = { "1", "1", "2", "2", "3", "3", "4", "4", "5", "5", NULL, NULL };
  23878. const char * test11111[] = { "1", "1", "1", "1", "1", NULL, NULL };
  23879. const char * test1111111111[] = { "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", NULL, NULL };
  23880. const char * test11111_12345[] = { "1", "1", "1", "1", "1", NULL, "1", "2", "3", "4", "5", NULL, NULL };
  23881. const char * test1111112345[] = { "1", "1", "1", "1", "1", "1", "2", "3", "4", "5", NULL, NULL };
  23882. const char * test11111111111122334455[] = { "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "2", "2", "3", "3", "4", "4", "5", "5", NULL, NULL };
  23883. testActivity(activity, test, test, test);
  23884. testActivity(activity, test12345, test, test12345);
  23885. testActivity(activity, test, test12345, test12345);
  23886. testActivity(activity, test12345, test12345, test1122334455);
  23887. testActivity(activity, test11111, test, test11111);
  23888. testActivity(activity, test, test11111, test11111);
  23889. testActivity(activity, test11111, test11111, test1111111111);
  23890. testActivity(activity, test11111_12345, test, test1111112345);
  23891. testActivity(activity, test11111_12345, test11111_12345, test11111111111122334455);
  23892. // Should really test WHICH side gets kept...
  23893. // Should test with more than 2 inputs...
  23894. DBGLOG("testMerge done");
  23895. }
  23896. void testMergeDedup()
  23897. {
  23898. DBGLOG("testMergeDedup");
  23899. init();
  23900. MergeActivityTest::isDedup = true;
  23901. Owned <IRoxieServerActivityFactory> factory = createRoxieServerMergeActivityFactory(1, 1, *queryFactory, mergeActivityTestFactory, TAKmerge);
  23902. factory->setInput(0,0,0);
  23903. factory->setInput(1,0,0);
  23904. Owned <IRoxieServerActivity> activity = factory->createActivity(NULL);
  23905. const char * test[] = { NULL, NULL };
  23906. const char * test12345[] = { "1", "2", "3", "4", "5", NULL, NULL };
  23907. const char * test11111[] = { "1", "1", "1", "1", "1", NULL, NULL };
  23908. const char * test11111_12345[] = { "1", "1", "1", "1", "1", NULL, "1", "2", "3", "4", "5", NULL, NULL };
  23909. const char * test1111112345[] = { "1", "1", "1", "1", "1", "1", "2", "3", "4", "5", NULL, NULL };
  23910. testActivity(activity, test11111, test, test11111); // No dedup within a stream
  23911. testActivity(activity, test11111, test11111, test11111); // No dedup within a stream
  23912. testActivity(activity, test, test11111, test11111);
  23913. testActivity(activity, test, test, test);
  23914. testActivity(activity, test12345, test, test12345);
  23915. testActivity(activity, test, test12345, test12345);
  23916. testActivity(activity, test12345, test12345, test12345);
  23917. testActivity(activity, test11111_12345, test, test1111112345);
  23918. testActivity(activity, test11111_12345, test11111_12345, test1111112345);
  23919. // Should really test WHICH side gets kept...
  23920. // Should test with more than 2 inputs...
  23921. DBGLOG("testMergeDedup done");
  23922. }
  23923. void testMiscellaneous()
  23924. {
  23925. DBGLOG("sizeof(CriticalSection)=%u", (unsigned) sizeof(CriticalSection));
  23926. DBGLOG("sizeof(SpinLock)=%u", (unsigned) sizeof(SpinLock));
  23927. DBGLOG("sizeof(CJoinGroup)=%u", (unsigned) sizeof(CJoinGroup));
  23928. ASSERT(sizeof(CJoinGroup) <= 120);
  23929. }
  23930. };
  23931. CPPUNIT_TEST_SUITE_REGISTRATION( CcdServerTest );
  23932. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CcdServerTest, "CcdServerTest" );
  23933. #endif