ccdserver.cpp 1010 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741
  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 "thorsort.hpp"
  27. #include "thorstats.hpp"
  28. #include "jlzw.hpp"
  29. #include "javahash.hpp"
  30. #include "javahash.tpp"
  31. #include "thorstep.ipp"
  32. #include "thorpipe.hpp"
  33. #include "thorfile.hpp"
  34. #include "eclhelper.hpp"
  35. #include "eclrtl_imp.hpp"
  36. #include "rtlfield.hpp"
  37. #include "rtlds_imp.hpp"
  38. #include "rtlread_imp.hpp"
  39. #include "rtlcommon.hpp"
  40. #include <algorithm>
  41. #include "dafdesc.hpp"
  42. #include "dautils.hpp"
  43. #include "ftbase.ipp"
  44. #include "eclhelper_base.hpp"
  45. #include "ccd.hpp"
  46. #include "ccdserver.hpp"
  47. #include "ccdcontext.hpp"
  48. #include "ccdactivities.hpp"
  49. #include "ccdquery.hpp"
  50. #include "ccdstate.hpp"
  51. #include "ccdqueue.ipp"
  52. #include "ccdsnmp.hpp"
  53. #include "ccddali.hpp"
  54. #include "jsmartsock.hpp"
  55. #include "dllserver.hpp"
  56. #include "workflow.hpp"
  57. #include "roxiemem.hpp"
  58. #include "roxierowbuff.hpp"
  59. #include "roxiehelper.hpp"
  60. #include "roxielmj.hpp"
  61. #include "roxierow.hpp"
  62. #include "thorplugin.hpp"
  63. #include "keybuild.hpp"
  64. #include "thorstrand.hpp"
  65. #include "rtldynfield.hpp"
  66. #define MAX_HTTP_HEADERSIZE 8000
  67. #define MIN_PAYLOAD_SIZE 800
  68. #ifdef _WIN32
  69. #pragma warning(disable : 4355)
  70. #endif
  71. #define DEFAULT_PARALLEL_LOOP_THREADS 1
  72. #define PROBE
  73. #ifdef _DEBUG
  74. //#define FAKE_EXCEPTIONS
  75. //#define TRACE_JOINGROUPS
  76. //#define TRACE_SPLIT
  77. //#define _CHECK_HEAPSORT
  78. //#undef PARALLEL_EXECUTE
  79. //#define TRACE_SEEK_REQUESTS
  80. #endif
  81. using roxiemem::OwnedRoxieRow;
  82. using roxiemem::OwnedRoxieString;
  83. using roxiemem::OwnedConstRoxieRow;
  84. using roxiemem::IRowManager;
  85. using roxiemem::ReleaseRoxieRows;
  86. // 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...
  87. #if _MSC_VER==1200
  88. #define protected public
  89. #endif
  90. #define TRACE_STARTSTOP // This determines if it is available - it is enabled/disabled by a configuration option
  91. #define TRACE_STRANDS
  92. #define MAX_SENSIBLE_STRANDS 1024 // Architecture dependent...
  93. static const SmartStepExtra dummySmartStepExtra(SSEFreadAhead, NULL);
  94. //=================================================================================
  95. class RestartableThread : public CInterface
  96. {
  97. class MyThread : public Thread
  98. {
  99. Linked<RestartableThread> owner;
  100. public:
  101. MyThread(RestartableThread *_owner, const char *name) : Thread(name), owner(_owner)
  102. {
  103. }
  104. virtual int run()
  105. {
  106. owner->started.signal();
  107. return owner->run();
  108. }
  109. };
  110. friend class MyThread;
  111. Semaphore started;
  112. Owned<MyThread> thread;
  113. CriticalSection crit;
  114. StringAttr name;
  115. public:
  116. RestartableThread(const char *_name) : name(_name)
  117. {
  118. }
  119. virtual void start(const char *namePrefix)
  120. {
  121. StringBuffer s(namePrefix);
  122. s.append(name);
  123. {
  124. CriticalBlock b(crit);
  125. assertex(!thread);
  126. thread.setown(new MyThread(this, s));
  127. thread->start();
  128. }
  129. started.wait();
  130. }
  131. virtual void join()
  132. {
  133. {
  134. Owned<Thread> tthread;
  135. {
  136. CriticalBlock b(crit);
  137. tthread.setown(thread.getClear());
  138. }
  139. if (tthread)
  140. tthread->join();
  141. }
  142. }
  143. virtual int run() = 0;
  144. };
  145. //================================================================================
  146. //The following don't link their arguments because that creates a circular reference
  147. //But I wish there was a better way
  148. class IndirectAgentContext : implements IRoxieAgentContext, public CInterface
  149. {
  150. public:
  151. IndirectAgentContext(IRoxieAgentContext * _ctx) : ctx(_ctx) {}
  152. IMPLEMENT_IINTERFACE
  153. // void set(IRoxieAgentContext * _ctx) { ctx = _ctx; }
  154. virtual ICodeContext *queryCodeContext()
  155. {
  156. return ctx->queryCodeContext();
  157. }
  158. virtual void checkAbort()
  159. {
  160. ctx->checkAbort();
  161. }
  162. virtual void notifyAbort(IException *E)
  163. {
  164. ctx->notifyAbort(E);
  165. }
  166. virtual IActivityGraph * queryChildGraph(unsigned id)
  167. {
  168. return ctx->queryChildGraph(id);
  169. }
  170. virtual void noteChildGraph(unsigned id, IActivityGraph *childGraph)
  171. {
  172. ctx->noteChildGraph(id, childGraph) ;
  173. }
  174. virtual IRowManager &queryRowManager()
  175. {
  176. return ctx->queryRowManager();
  177. }
  178. virtual void noteStatistic(StatisticKind kind, unsigned __int64 value) const
  179. {
  180. ctx->noteStatistic(kind, value);
  181. }
  182. virtual void mergeStats(const CRuntimeStatisticCollection &from) const
  183. {
  184. ctx->mergeStats(from);
  185. }
  186. virtual void gatherStats(CRuntimeStatisticCollection & merged) const override
  187. {
  188. ctx->gatherStats(merged);
  189. }
  190. virtual bool collectingDetailedStatistics() const
  191. {
  192. return ctx->collectingDetailedStatistics();
  193. }
  194. virtual void CTXLOGva(const char *format, va_list args) const __attribute__((format(printf,2,0)))
  195. {
  196. ctx->CTXLOGva(format, args);
  197. }
  198. virtual void CTXLOGa(TracingCategory category, const char *prefix, const char *text) const
  199. {
  200. ctx->CTXLOGa(category, prefix, text);
  201. }
  202. virtual void logOperatorExceptionVA(IException *E, const char *file, unsigned line, const char *format, va_list args) const __attribute__((format(printf,5,0)))
  203. {
  204. ctx->logOperatorExceptionVA(E, file, line, format, args);
  205. }
  206. virtual void CTXLOGaeva(IException *E, const char *file, unsigned line, const char *prefix, const char *format, va_list args) const __attribute__((format(printf,6,0)))
  207. {
  208. ctx->CTXLOGaeva(E, file, line, prefix, format, args);
  209. }
  210. virtual void CTXLOGl(LogItem *log) const
  211. {
  212. ctx->CTXLOGl(log);
  213. }
  214. virtual StringBuffer &getLogPrefix(StringBuffer &ret) const override
  215. {
  216. return ctx->getLogPrefix(ret);
  217. }
  218. virtual unsigned queryTraceLevel() const
  219. {
  220. return ctx->queryTraceLevel();
  221. }
  222. virtual bool isIntercepted() const
  223. {
  224. return ctx->isIntercepted();
  225. }
  226. virtual bool isBlind() const
  227. {
  228. return ctx->isBlind();
  229. }
  230. virtual void setGlobalId(const char *id, SocketEndpoint &ep, unsigned pid) override
  231. {
  232. ctx->setGlobalId(id, ep, pid);
  233. }
  234. virtual void setCallerId(const char *id) override
  235. {
  236. ctx->setCallerId(id);
  237. }
  238. virtual const char *queryGlobalId() const
  239. {
  240. return ctx->queryGlobalId();
  241. }
  242. virtual const char *queryCallerId() const override
  243. {
  244. return ctx->queryCallerId();
  245. }
  246. virtual const char *queryLocalId() const
  247. {
  248. return ctx->queryLocalId();
  249. }
  250. virtual void setHttpIdHeaders(const char *global, const char *caller)
  251. {
  252. ctx->setHttpIdHeaders(global, caller);
  253. }
  254. virtual const char *queryGlobalIdHttpHeader() const
  255. {
  256. return ctx->queryGlobalIdHttpHeader();
  257. }
  258. virtual const char *queryCallerIdHttpHeader() const
  259. {
  260. return ctx->queryCallerIdHttpHeader();
  261. }
  262. virtual const QueryOptions &queryOptions() const
  263. {
  264. return ctx->queryOptions();
  265. }
  266. virtual void addAgentsReplyLen(unsigned len)
  267. {
  268. ctx->addAgentsReplyLen(len);
  269. }
  270. virtual const char *queryAuthToken()
  271. {
  272. return ctx->queryAuthToken();
  273. }
  274. virtual const IResolvedFile *resolveLFN(const char *filename, bool isOpt, bool isPrivilegedUser)
  275. {
  276. return ctx->resolveLFN(filename, isOpt, isPrivilegedUser);
  277. }
  278. virtual IRoxieWriteHandler *createLFN(const char *filename, bool overwrite, bool extend, const StringArray &clusters, bool isPrivilegedUser)
  279. {
  280. return ctx->createLFN(filename, overwrite, extend, clusters, isPrivilegedUser);
  281. }
  282. virtual void onFileCallback(const RoxiePacketHeader &header, const char *lfn, bool isOpt, bool isLocal, bool isPrivilegedUser)
  283. {
  284. ctx->onFileCallback(header, lfn, isOpt, isLocal, isPrivilegedUser);
  285. }
  286. virtual IActivityGraph *getLibraryGraph(const LibraryCallFactoryExtra &extra, IRoxieServerActivity *parentActivity)
  287. {
  288. return ctx->getLibraryGraph(extra, parentActivity);
  289. }
  290. virtual IProbeManager *queryProbeManager() const
  291. {
  292. return ctx->queryProbeManager();
  293. }
  294. virtual IDebuggableContext *queryDebugContext() const
  295. {
  296. return ctx->queryDebugContext();
  297. }
  298. virtual void printResults(IXmlWriter *output, const char *name, unsigned sequence)
  299. {
  300. ctx->printResults(output, name, sequence);
  301. }
  302. virtual void setWUState(WUState state)
  303. {
  304. ctx->setWUState(state);
  305. }
  306. virtual bool checkWuAborted()
  307. {
  308. return ctx->checkWuAborted();
  309. }
  310. virtual IWorkUnit *updateWorkUnit() const
  311. {
  312. return ctx->updateWorkUnit();
  313. }
  314. virtual IConstWorkUnit *queryWorkUnit() const
  315. {
  316. return ctx->queryWorkUnit();
  317. }
  318. virtual IRoxieServerContext *queryServerContext()
  319. {
  320. return ctx->queryServerContext();
  321. }
  322. virtual IWorkUnitRowReader *getWorkunitRowReader(const char *wuid, const char * name, unsigned sequence, IXmlToRowTransformer * xmlTransformer, IEngineRowAllocator *rowAllocator, bool isGrouped)
  323. {
  324. return ctx->getWorkunitRowReader(wuid, name, sequence, xmlTransformer, rowAllocator, isGrouped);
  325. }
  326. virtual IEngineRowAllocator *getRowAllocatorEx(IOutputMetaData * meta, unsigned activityId, roxiemem::RoxieHeapFlags flags) const
  327. {
  328. return ctx->getRowAllocatorEx(meta, activityId, flags);
  329. }
  330. protected:
  331. IRoxieAgentContext * ctx;
  332. };
  333. //=================================================================================
  334. #define RESULT_FLUSH_THRESHOLD 10000u
  335. #ifdef _DEBUG
  336. #define SOAP_SPLIT_THRESHOLD 100u
  337. #define SOAP_SPLIT_RESERVE 200u
  338. #else
  339. #define SOAP_SPLIT_THRESHOLD 64000u
  340. #define SOAP_SPLIT_RESERVE 65535u
  341. #endif
  342. //=================================================================================
  343. // General activity statistics
  344. static const StatisticsMapping actStatistics({StWhenFirstRow, StTimeElapsed, StTimeLocalExecute, StTimeTotalExecute, StSizeMaxRowSize,
  345. StNumRowsProcessed, StNumSlaves, StNumStarts, StNumStops, StNumStrands,
  346. StNumScansPerRow, StNumAllocations, StNumAllocationScans,
  347. StTimeFirstExecute, StCycleLocalExecuteCycles, StCycleTotalExecuteCycles});
  348. static const StatisticsMapping joinStatistics({StNumAtmostTriggered}, actStatistics);
  349. static const StatisticsMapping keyedJoinStatistics({ StNumServerCacheHits, StNumIndexSeeks, StNumIndexScans, StNumIndexWildSeeks,
  350. StNumIndexSkips, StNumIndexNullSkips, StNumIndexMerges, StNumIndexMergeCompares,
  351. StNumPreFiltered, StNumPostFiltered, StNumIndexAccepted, StNumIndexRejected,
  352. StNumIndexRowsRead, StNumDiskRowsRead, StNumDiskSeeks, StNumDiskAccepted,
  353. StNumBlobCacheHits, StNumLeafCacheHits, StNumNodeCacheHits,
  354. StNumBlobCacheAdds, StNumLeafCacheAdds, StNumNodeCacheAdds,
  355. StNumDiskRejected}, joinStatistics);
  356. static const StatisticsMapping indexStatistics({StNumServerCacheHits, StNumIndexSeeks, StNumIndexScans, StNumIndexWildSeeks,
  357. StNumIndexSkips, StNumIndexNullSkips, StNumIndexMerges, StNumIndexMergeCompares,
  358. StNumPreFiltered, StNumPostFiltered, StNumIndexAccepted, StNumIndexRejected,
  359. StNumBlobCacheHits, StNumLeafCacheHits, StNumNodeCacheHits,
  360. StNumBlobCacheAdds, StNumLeafCacheAdds, StNumNodeCacheAdds,
  361. StNumIndexRowsRead}, actStatistics);
  362. static const StatisticsMapping diskStatistics({StNumServerCacheHits, StNumDiskRowsRead, StNumDiskSeeks, StNumDiskAccepted,
  363. StNumDiskRejected }, actStatistics);
  364. static const StatisticsMapping soapStatistics({ StTimeSoapcall }, actStatistics);
  365. static const StatisticsMapping groupStatistics({ StNumGroups, StNumGroupMax }, actStatistics);
  366. static const StatisticsMapping sortStatistics({ StTimeSortElapsed }, actStatistics);
  367. static const StatisticsMapping indexWriteStatistics({ StNumDuplicateKeys }, actStatistics);
  368. //=================================================================================
  369. const static unsigned minus1U = (0U-1U);
  370. class CRoxieServerActivityFactoryBase : public CActivityFactory, implements IRoxieServerActivityFactory
  371. {
  372. protected:
  373. IntArray dependencies; // things I am dependent on
  374. IntArray dependencyIndexes; // things I am dependent on
  375. IntArray dependencyControlIds; // things I am dependent on
  376. StringArray dependencyEdgeIds; // How to describe them to the debugger
  377. unsigned dependentCount; // things dependent on me
  378. unsigned optParallel = 0; // -1=enable,0=not specified,1=single-threaded,n=<num threads>
  379. bool optStableInput = true; // is the input forced to ordered?
  380. bool optUnstableInput = false; // is the input forced to unordered?
  381. bool optUnordered = false; // is the output specified as unordered?
  382. bool isCodeSigned = false;
  383. unsigned heapFlags;
  384. mutable RelaxedAtomic<__int64> processed = {0};
  385. mutable RelaxedAtomic<__int64> started = {0};
  386. public:
  387. IMPLEMENT_IINTERFACE;
  388. CRoxieServerActivityFactoryBase(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  389. : CActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  390. {
  391. dependentCount = 0;
  392. optParallel = _graphNode.getPropInt("att[@name='parallel']/@value", 0);
  393. optUnordered = !_graphNode.getPropBool("att[@name='ordered']/@value", true);
  394. heapFlags = _graphNode.getPropInt("hint[@name='heapflags']/@value", _queryFactory.queryOptions().heapFlags);
  395. isCodeSigned = ::isActivityCodeSigned(_graphNode);
  396. }
  397. ~CRoxieServerActivityFactoryBase()
  398. {
  399. }
  400. StringBuffer &toString(StringBuffer &ret) const
  401. {
  402. return ret.appendf("%p", this);
  403. }
  404. virtual void addDependency(unsigned _source, ThorActivityKind _kind, unsigned _sourceIdx, int controlId, const char *edgeId)
  405. {
  406. dependencies.append(_source);
  407. dependencyIndexes.append(_sourceIdx);
  408. dependencyControlIds.append(controlId);
  409. dependencyEdgeIds.append(edgeId);
  410. }
  411. virtual bool isInputOrdered(bool consumerOrdered, unsigned idx) const
  412. {
  413. if (optStableInput)
  414. return true;
  415. if (optUnstableInput)
  416. return false;
  417. if (optUnordered)
  418. return false;
  419. return consumerOrdered;
  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 mergeStats(const CRuntimeStatisticCollection &from) const
  458. {
  459. CActivityFactory::mergeStats(from);
  460. }
  461. virtual void noteProcessed(unsigned _idx, unsigned _processed) const
  462. {
  463. dbgassertex(!_idx);
  464. if (likely(_processed))
  465. processed += _processed;
  466. }
  467. virtual void noteStarted() const
  468. {
  469. started++;
  470. }
  471. virtual void noteStarted(unsigned idx) const
  472. {
  473. throwUnexpected(); // should be implemented/required by multiOutput cases only
  474. }
  475. virtual void getEdgeProgressInfo(unsigned output, IPropertyTree &edge) const
  476. {
  477. if (output == 0)
  478. {
  479. putStatsValue(&edge, "NumRowsProcessed", "sum", processed);
  480. auto _started = started.load();
  481. if (_started)
  482. putStatsValue(&edge, "NumStarts", "sum", _started);
  483. }
  484. else
  485. IERRLOG("unexpected call to getEdgeProcessInfo for output %d in activity %d", output, queryId());
  486. }
  487. virtual void getNodeProgressInfo(IPropertyTree &node) const
  488. {
  489. CActivityFactory::getNodeProgressInfo(node);
  490. auto _started = started.load();
  491. if (_started)
  492. putStatsValue(&node, "_roxieStarted", "sum", _started);
  493. }
  494. virtual void resetNodeProgressInfo()
  495. {
  496. CActivityFactory::resetNodeProgressInfo();
  497. processed = 0;
  498. started = 0;
  499. }
  500. virtual void getActivityMetrics(StringBuffer &reply) const
  501. {
  502. CActivityFactory::getActivityMetrics(reply);
  503. putStatsValue(reply, "NumStarts", "sum", started);
  504. CriticalBlock b(statsCrit);
  505. putStatsValue(reply, "TimeTotalExecute", "sum", (unsigned) (mystats.getSerialStatisticValue(StTimeTotalExecute)/1000));
  506. putStatsValue(reply, "TimeLocalExecute", "sum", (unsigned) (mystats.getSerialStatisticValue(StTimeLocalExecute)/1000));
  507. }
  508. virtual unsigned __int64 queryLocalTimeNs() const
  509. {
  510. return mystats.getSerialStatisticValue(StTimeLocalExecute);
  511. }
  512. virtual IQueryFactory &queryQueryFactory() const
  513. {
  514. return CActivityFactory::queryQueryFactory();
  515. }
  516. virtual ActivityArray *queryChildQuery(unsigned idx, unsigned &id)
  517. {
  518. return CActivityFactory::queryChildQuery(idx, id);
  519. }
  520. virtual void addChildQuery(unsigned id, ActivityArray *childQuery)
  521. {
  522. CActivityFactory::addChildQuery(id, childQuery);
  523. }
  524. virtual void onCreateChildQueries(IRoxieAgentContext *ctx, IHThorArg *colocalArg, IArrayOf<IActivityGraph> &childGraphs, IRoxieServerActivity *parentActivity, IProbeManager *_probeManager, const IRoxieContextLogger &_logctx, unsigned numParallel) const
  525. {
  526. ForEachItemIn(idx, childQueries)
  527. {
  528. childGraphs.append(*createActivityGraph(ctx, NULL, childQueryIndexes.item(idx), childQueries.item(idx), parentActivity, _probeManager, _logctx, numParallel));
  529. ctx->noteChildGraph(childQueryIndexes.item(idx), &childGraphs.item(idx));
  530. childGraphs.item(idx).onCreate(colocalArg);
  531. }
  532. }
  533. IActivityGraph * createChildGraph(IRoxieAgentContext * ctx, IHThorArg *colocalArg, unsigned childId, IRoxieServerActivity *parentActivity, IProbeManager * _probeManager, const IRoxieContextLogger &_logctx) const
  534. {
  535. unsigned match = childQueryIndexes.find(childId);
  536. assertex(match != NotFound);
  537. Owned<IActivityGraph> graph = createActivityGraph(ctx, NULL, childQueryIndexes.item(match), childQueries.item(match), parentActivity, _probeManager, _logctx, 1);
  538. graph->onCreate(colocalArg);
  539. return graph.getClear();
  540. }
  541. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  542. {
  543. // Most activities have nothing to say...
  544. }
  545. virtual const StatisticsMapping &queryStatsMapping() const
  546. {
  547. return actStatistics; // Overridden by anyone that needs more
  548. }
  549. virtual roxiemem::RoxieHeapFlags getHeapFlags() const
  550. {
  551. return (roxiemem::RoxieHeapFlags)heapFlags;
  552. }
  553. virtual bool isActivityCodeSigned() const
  554. {
  555. return isCodeSigned;
  556. }
  557. virtual RecordTranslationMode getEnableFieldTranslation() const
  558. {
  559. return CActivityFactory::getEnableFieldTranslation();
  560. }
  561. };
  562. class CRoxieServerMultiInputInfo
  563. {
  564. private:
  565. UnsignedArray inputs;
  566. UnsignedArray inputIndexes;
  567. public:
  568. void set(unsigned idx, unsigned source, unsigned sourceidx)
  569. {
  570. if (idx==inputs.length())
  571. {
  572. inputs.append(source);
  573. inputIndexes.append(sourceidx);
  574. }
  575. else
  576. {
  577. while (!inputs.isItem(idx))
  578. {
  579. inputs.append(0);
  580. inputIndexes.append(0);
  581. }
  582. inputs.replace(source, idx);
  583. inputIndexes.replace(sourceidx, idx);
  584. }
  585. }
  586. unsigned get(unsigned idx, unsigned &sourceidx) const
  587. {
  588. if (inputs.isItem(idx))
  589. {
  590. sourceidx = inputIndexes.item(idx);
  591. return inputs.item(idx);
  592. }
  593. else
  594. return (unsigned) -1;
  595. }
  596. inline unsigned ordinality() const { return inputs.ordinality(); }
  597. };
  598. class CRoxieServerMultiInputFactory : public CRoxieServerActivityFactoryBase
  599. {
  600. private:
  601. CRoxieServerMultiInputInfo inputs;
  602. public:
  603. CRoxieServerMultiInputFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  604. : CRoxieServerActivityFactoryBase(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  605. {
  606. }
  607. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  608. {
  609. inputs.set(idx, source, sourceidx);
  610. }
  611. virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
  612. {
  613. return inputs.get(idx, sourceidx);
  614. }
  615. virtual unsigned numInputs() const { return inputs.ordinality(); }
  616. };
  617. class DECL_EXCEPTION CWrappedException : implements IException, public CInterface
  618. {
  619. Owned<IException> wrapped;
  620. ThorActivityKind kind;
  621. unsigned queryId;
  622. public:
  623. IMPLEMENT_IINTERFACE;
  624. CWrappedException(IException *_wrapped, ThorActivityKind _kind, unsigned _queryId)
  625. : wrapped(_wrapped), kind(_kind), queryId(_queryId)
  626. {
  627. }
  628. virtual int errorCode() const { return wrapped->errorCode(); }
  629. virtual StringBuffer & errorMessage(StringBuffer &msg) const { return wrapped->errorMessage(msg).appendf(" (in %s %d)", getActivityText(kind), queryId); }
  630. virtual MessageAudience errorAudience() const { return wrapped->errorAudience(); }
  631. };
  632. class CRoxieServerActivityFactory : public CRoxieServerActivityFactoryBase
  633. {
  634. protected:
  635. unsigned input;
  636. unsigned inputidx;
  637. public:
  638. CRoxieServerActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  639. : CRoxieServerActivityFactoryBase(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  640. {
  641. input = (unsigned) -1;
  642. inputidx = 0;
  643. }
  644. inline void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  645. {
  646. if (idx != 0)
  647. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: id = %d : setInput() parameter out of bounds idx = %d at %s(%d)", id, idx, sanitizeSourceFile(__FILE__), __LINE__);
  648. if (input != (unsigned) -1)
  649. 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, sanitizeSourceFile(__FILE__), __LINE__);
  650. input = source;
  651. inputidx = sourceidx;
  652. }
  653. virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
  654. {
  655. if (!idx)
  656. {
  657. sourceidx = inputidx;
  658. return input;
  659. }
  660. return (unsigned) -1;
  661. }
  662. virtual unsigned numInputs() const { return (input == (unsigned)-1) ? 0 : 1; }
  663. };
  664. class CRoxieServerMultiOutputFactory : public CRoxieServerActivityFactory
  665. {
  666. protected:
  667. unsigned numOutputs = 0;
  668. RelaxedAtomic<__int64> *processedArray = nullptr;
  669. RelaxedAtomic<__int64> *startedArray = nullptr;
  670. CRoxieServerMultiOutputFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  671. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  672. {
  673. }
  674. ~CRoxieServerMultiOutputFactory()
  675. {
  676. delete [] processedArray;
  677. delete [] startedArray;
  678. }
  679. void setNumOutputs(unsigned num)
  680. {
  681. numOutputs = num;
  682. if (!num)
  683. num = 1; // Even sink activities like to track how many records they process
  684. processedArray = new RelaxedAtomic<__int64>[num];
  685. startedArray = new RelaxedAtomic<__int64>[num];
  686. for (unsigned i = 0; i < num; i++)
  687. {
  688. processedArray[i] = 0;
  689. startedArray[i] = 0;
  690. }
  691. }
  692. virtual void getEdgeProgressInfo(unsigned idx, IPropertyTree &edge) const
  693. {
  694. assertex(numOutputs ? idx < numOutputs : idx==0);
  695. putStatsValue(&edge, "NumRowsProcessed", "sum", processedArray[idx]);
  696. putStatsValue(&edge, "NumStarts", "sum", startedArray[idx]);
  697. }
  698. virtual void resetNodeProgressInfo()
  699. {
  700. CRoxieServerActivityFactory::resetNodeProgressInfo();
  701. for (unsigned i = 0; i < numOutputs; i++)
  702. {
  703. processedArray[i] = 0;
  704. startedArray[i] = 0;
  705. }
  706. }
  707. virtual void noteProcessed(unsigned idx, unsigned _processed) const
  708. {
  709. assertex(numOutputs ? idx < numOutputs : idx==0);
  710. processedArray[idx] += _processed;
  711. }
  712. virtual void noteStarted(unsigned idx) const
  713. {
  714. assertex(numOutputs ? idx < numOutputs : idx==0);
  715. startedArray[idx]++;
  716. }
  717. };
  718. class CRoxieServerInternalSinkFactory : public CRoxieServerActivityFactory
  719. {
  720. protected:
  721. bool isInternal;
  722. bool isRoot;
  723. unsigned usageCount;
  724. public:
  725. CRoxieServerInternalSinkFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, bool _isRoot)
  726. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  727. {
  728. usageCount = _usageCount;
  729. isRoot = _isRoot;
  730. isInternal = false; // filled in by derived class constructor
  731. }
  732. virtual bool isSink() const
  733. {
  734. //If an internal result has as many dependencies (within the graph) as uses (which includes from outside the graph) then don't execute it unconditionally.
  735. bool internalSpillAllUsesWithinGraph = (isInternal && dependentCount && dependentCount==usageCount);
  736. //only a sink if a root activity
  737. return isRoot && !internalSpillAllUsesWithinGraph;
  738. }
  739. virtual void getEdgeProgressInfo(unsigned idx, IPropertyTree &edge) const
  740. {
  741. // 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
  742. // Just leave it blank is safest.
  743. }
  744. };
  745. typedef enum { STATEreset, STATEstarted, STATEstopped, STATEstarting } activityState;
  746. const char *queryStateText(activityState state)
  747. {
  748. switch (state)
  749. {
  750. case STATEreset: return "reset";
  751. case STATEstarted: return "started";
  752. case STATEstopped: return "stopped";
  753. case STATEstarting: return "starting"; // Used by a splitter to indicate it has seen a stop but not yet seen a start(), nor has it seen stop() on all output adaptors
  754. default: return "unknown";
  755. }
  756. }
  757. typedef ICopyArrayOf<IRoxieServerActivity> IRoxieServerActivityCopyArray;
  758. class CParallelActivityExecutor : public CAsyncFor
  759. {
  760. public:
  761. unsigned parentExtractSize;
  762. const byte * parentExtract;
  763. CParallelActivityExecutor(IRoxieServerActivityCopyArray & _activities, unsigned _parentExtractSize, const byte * _parentExtract) :
  764. parentExtractSize(_parentExtractSize), parentExtract(_parentExtract), activities(_activities) { }
  765. void Do(unsigned i)
  766. {
  767. activities.item(i).execute(parentExtractSize, parentExtract);
  768. }
  769. private:
  770. IRoxieServerActivityCopyArray & activities;
  771. };
  772. extern IEngineRowStream * ensureSingleStream(IRoxieAgentContext *ctx, PointerArrayOf<IEngineRowStream> & instreams, Owned<IStrandJunction> &junction)
  773. {
  774. if (instreams.length() != 1)
  775. {
  776. assertex(instreams.length());
  777. if (!junction)
  778. junction.setown(createStrandJunction(ctx->queryRowManager(), instreams.length(), 1, ctx->queryOptions().strandBlockSize, false));
  779. ForEachItemIn(stream, instreams)
  780. {
  781. junction->setInput(stream, instreams.item(stream));
  782. }
  783. return junction->queryOutput(0);
  784. }
  785. else
  786. return instreams.item(0);
  787. }
  788. extern IEngineRowStream *connectSingleStream(IRoxieAgentContext *ctx, IFinalRoxieInput *input, unsigned idx, Owned<IStrandJunction> &junction, bool consumerOrdered)
  789. {
  790. if (input)
  791. {
  792. PointerArrayOf<IEngineRowStream> instreams;
  793. junction.setown(input->getOutputStreams(ctx, idx, instreams, NULL, consumerOrdered, nullptr));
  794. return ensureSingleStream(ctx, instreams, junction);
  795. }
  796. else
  797. return NULL;
  798. }
  799. extern IEngineRowStream *connectSingleStream(IRoxieAgentContext *ctx, IFinalRoxieInput *input, unsigned idx, bool consumerOrdered)
  800. {
  801. Owned<IStrandJunction> junction;
  802. IEngineRowStream * result = connectSingleStream(ctx, input, idx, junction, consumerOrdered);
  803. assertex(!junction);
  804. return result;
  805. }
  806. class CRoxieServerActivity : implements CInterfaceOf<IRoxieServerActivity>, implements IFinalRoxieInput, implements IEngineRowStream, implements IRoxieContextLogger
  807. {
  808. friend class StrandProcessor;
  809. protected:
  810. IFinalRoxieInput *input;
  811. unsigned sourceIdx;
  812. IEngineRowStream *inputStream;
  813. Owned<IStrandJunction> junction;
  814. IHThorArg &basehelper;
  815. IRoxieAgentContext *ctx;
  816. class InterceptRegisterTimer : public IndirectCodeContext
  817. {
  818. public:
  819. InterceptRegisterTimer(CRoxieServerActivity &_activity) : activity(_activity) {}
  820. virtual ISectionTimer *registerTimer(unsigned activityId, const char * name)
  821. {
  822. if (activityId == activity.queryId())
  823. return activity.registerTimer(activityId, name);
  824. else
  825. return ctx->registerTimer(activityId, name);
  826. }
  827. private:
  828. CRoxieServerActivity &activity;
  829. } interceptedCtx;
  830. const IRoxieServerActivityFactory *factory;
  831. IRoxieServerActivityCopyArray dependencies;
  832. IntArray dependencyIndexes;
  833. IntArray dependencyControlIds;
  834. IArrayOf<IActivityGraph> childGraphs;
  835. CachedOutputMetaData meta;
  836. IHThorArg *colocalParent;
  837. IEngineRowAllocator *rowAllocator;
  838. CriticalSection statecrit;
  839. CriticalSection statscrit;
  840. mutable CRuntimeStatisticCollection stats;
  841. MapStringToMyClass<ThorSectionTimer> functionTimers;
  842. ActivityTimeAccumulator activityStats;
  843. IProbeManager *probeManager = NULL;
  844. unsigned processed;
  845. unsigned numStarts = 0;
  846. unsigned activityId;
  847. activityState state;
  848. bool createPending;
  849. bool debugging;
  850. bool timeActivities;
  851. bool aborted;
  852. bool connected = false;
  853. bool collectFactoryStatistics = false;
  854. public:
  855. IMPLEMENT_IINTERFACE_USING(CInterfaceOf<IRoxieServerActivity>)
  856. CRoxieServerActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  857. : basehelper(_factory->getHelper()),
  858. ctx(_ctx),
  859. interceptedCtx(*this),
  860. factory(_factory),
  861. stats(_factory->queryStatsMapping()),
  862. probeManager(_probeManager),
  863. activityId(_factory->queryId())
  864. {
  865. input = NULL;
  866. sourceIdx = 0;
  867. inputStream = NULL;
  868. meta.set(basehelper.queryOutputMeta());
  869. processed = 0;
  870. state=STATEreset;
  871. rowAllocator = NULL;
  872. debugging = _probeManager != NULL; // Don't want to collect timing stats from debug sessions
  873. colocalParent = NULL;
  874. createPending = true;
  875. timeActivities = defaultTimeActivities;
  876. collectFactoryStatistics = defaultCollectFactoryStatistics && !debugging && factory;
  877. aborted = false;
  878. }
  879. CRoxieServerActivity(IRoxieAgentContext *_ctx, IHThorArg & _helper)
  880. : basehelper(_helper), ctx(_ctx),
  881. interceptedCtx(*this), factory(NULL), stats(allStatistics)
  882. {
  883. activityId = 0;
  884. input = NULL;
  885. sourceIdx = 0;
  886. inputStream = NULL;
  887. ctx = NULL;
  888. meta.set(basehelper.queryOutputMeta());
  889. processed = 0;
  890. state=STATEreset;
  891. rowAllocator = NULL;
  892. debugging = false;
  893. colocalParent = NULL;
  894. createPending = true;
  895. timeActivities = defaultTimeActivities;
  896. collectFactoryStatistics = false; // since no factory
  897. aborted = false;
  898. }
  899. ~CRoxieServerActivity()
  900. {
  901. basehelper.Release();
  902. ::Release(rowAllocator);
  903. }
  904. virtual void beforeDispose() override
  905. {
  906. if (traceStartStop)
  907. {
  908. // There was an old comment here stating // Note- CTXLOG may not be safe
  909. // There were problems in the destruction order of graphs that might mean an IndirectAgentContext
  910. // was released while activities still referenced it.
  911. // However these should now all be fixed
  912. DBGLOG("%p destroy %d state=%s", this, activityId, queryStateText(state));
  913. if (watchActivityId && watchActivityId==activityId)
  914. {
  915. DBGLOG("WATCH: %p destroy %d state=%s", this, activityId, queryStateText(state));
  916. }
  917. }
  918. if (state!=STATEreset)
  919. {
  920. DBGLOG("STATE: Activity %d destroyed but not reset", activityId);
  921. state = STATEreset; // bit pointless but there you go...
  922. }
  923. }
  924. const IResolvedFile *resolveLFNIndex(const char *filename, bool isOpt, bool isPrivilegedUser)
  925. {
  926. const IResolvedFile *ret = resolveLFN(filename, isOpt, isPrivilegedUser);
  927. if (ret && !ret->isKey())
  928. throw MakeStringException(0, "Attempting to read flat file as an index: %s", filename);
  929. return ret;
  930. }
  931. const IResolvedFile *resolveLFNFlat(const char *filename, bool isOpt, bool isPrivilegedUser)
  932. {
  933. const IResolvedFile *ret = resolveLFN(filename, isOpt, isPrivilegedUser);
  934. if (ret && ret->isKey())
  935. throw MakeStringException(0, "Attempting to read index as a flat file: %s", filename);
  936. return ret;
  937. }
  938. virtual void gatherStatistics(IStatisticGatherer * statsBuilder) const override
  939. {
  940. if (!factory)
  941. return;
  942. //Collate the stats for this activity from various different sources.
  943. CRuntimeStatisticCollection mergedStats(stats.queryMapping());
  944. gatherStats(mergedStats);
  945. //Because subgraphs are flattened in roxie the subgraph needs to be selected for each activity
  946. if (statsBuilder)
  947. statsBuilder->beginSubGraphScope(factory->querySubgraphId());
  948. //Still needed even if statsBuilder is null to update values in the factory
  949. updateEdgeStats(statsBuilder);
  950. if (statsBuilder)
  951. {
  952. StatsActivityScope ac(*statsBuilder, activityId);
  953. mergedStats.recordStatistics(*statsBuilder);
  954. //close the subgraph scope from the previous if()
  955. statsBuilder->endScope();
  956. }
  957. //Update the statistics accumulated over all the queries
  958. if (collectFactoryStatistics)
  959. factory->mergeStats(mergedStats);
  960. //Updates the query summary statistics
  961. if (ctx)
  962. ctx->queryCodeContext()->queryContextLogger().mergeStats(mergedStats);
  963. ForEachItemIn(i, childGraphs)
  964. childGraphs.item(i).gatherStatistics(statsBuilder);
  965. }
  966. virtual const IRoxieContextLogger &queryLogCtx()const
  967. {
  968. return *this;
  969. }
  970. inline const StatisticsMapping & queryStatsMapping() const
  971. {
  972. return factory ? factory->queryStatsMapping() : actStatistics;
  973. }
  974. virtual bool collectingDetailedStatistics() const
  975. {
  976. return ctx && ctx->collectingDetailedStatistics();
  977. }
  978. virtual void mergeStats(MemoryBuffer &buf)
  979. {
  980. stats.deserializeMerge(buf);
  981. }
  982. virtual void mergeStats(const CRuntimeStatisticCollection & childStats)
  983. {
  984. CriticalBlock b(statscrit);
  985. stats.merge(childStats);
  986. }
  987. virtual ISectionTimer *registerTimer(unsigned _activityId, const char * name)
  988. {
  989. CriticalBlock b(statscrit); // reuse statscrit to protect functionTimers - it will not be held concurrently
  990. ISectionTimer *timer = functionTimers.getValue(name);
  991. if (!timer)
  992. {
  993. timer = ThorSectionTimer::createTimer(stats, name);
  994. functionTimers.setValue(name, timer);
  995. timer->Release(); // Value returned is not linked
  996. }
  997. return timer;
  998. }
  999. void mergeStrandStats(unsigned strandProcessed, const ActivityTimeAccumulator & strandCycles)
  1000. {
  1001. CriticalBlock cb(statscrit);
  1002. processed += strandProcessed;
  1003. activityStats.merge(strandCycles);
  1004. }
  1005. inline void ensureRowAllocator()
  1006. {
  1007. if (!rowAllocator)
  1008. rowAllocator = createRowAllocator(meta.queryOriginal());
  1009. }
  1010. virtual IEngineRowAllocator * createRowAllocator(IOutputMetaData * metadata)
  1011. {
  1012. return ctx->getRowAllocatorEx(metadata, activityId, factory->getHeapFlags());
  1013. }
  1014. virtual IEngineRowAllocator * createRowAllocatorEx(IOutputMetaData * metadata, roxiemem::RoxieHeapFlags extraFlags)
  1015. {
  1016. return ctx->getRowAllocatorEx(metadata, activityId, (roxiemem::RoxieHeapFlags)(factory->getHeapFlags()|extraFlags));
  1017. }
  1018. inline ICodeContext *queryCodeContext()
  1019. {
  1020. return ctx->queryCodeContext();
  1021. }
  1022. // MORE - most of this is copied from ccd.hpp - can't we refactor?
  1023. virtual void CTXLOGa(TracingCategory category, const char *prefix, const char *text) const
  1024. {
  1025. if (ctx)
  1026. ctx->CTXLOGa(category, prefix, text);
  1027. else
  1028. DBGLOG("[%s] %s", prefix, text);
  1029. }
  1030. virtual void CTXLOGaeva(IException *E, const char *file, unsigned line, const char *prefix, const char *format, va_list args) const __attribute__((format(printf,6,0)))
  1031. {
  1032. if (ctx)
  1033. ctx->CTXLOGaeva(E, file, line, prefix, format, args);
  1034. else
  1035. {
  1036. StringBuffer ss;
  1037. ss.appendf("[%s] ERROR", prefix);
  1038. if (E)
  1039. ss.append(": ").append(E->errorCode());
  1040. if (file)
  1041. ss.appendf(": %s(%d) ", file, line);
  1042. if (E)
  1043. E->errorMessage(ss.append(": "));
  1044. if (format)
  1045. {
  1046. ss.append(": ").valist_appendf(format, args);
  1047. }
  1048. LOG(MCoperatorProgress, unknownJob, "%s", ss.str());
  1049. }
  1050. }
  1051. virtual void CTXLOGl(LogItem *log) const
  1052. {
  1053. if (ctx)
  1054. ctx->CTXLOGl(log);
  1055. else
  1056. {
  1057. assert(ctx);
  1058. log->Release(); // Should never happen
  1059. }
  1060. }
  1061. virtual void noteStatistic(StatisticKind kind, unsigned __int64 value) const
  1062. {
  1063. stats.addStatistic(kind, value);
  1064. }
  1065. virtual void mergeStats(const CRuntimeStatisticCollection &from) const
  1066. {
  1067. stats.merge(from);
  1068. }
  1069. virtual void gatherStats(CRuntimeStatisticCollection & merged) const override
  1070. {
  1071. merged.merge(stats);
  1072. if (rowAllocator)
  1073. rowAllocator->gatherStats(merged);
  1074. activityStats.addStatistics(merged);
  1075. merged.mergeStatistic(StCycleLocalExecuteCycles, queryLocalCycles());
  1076. }
  1077. virtual StringBuffer &getLogPrefix(StringBuffer &ret) const
  1078. {
  1079. if (ctx)
  1080. ctx->getLogPrefix(ret);
  1081. return ret.append('@').append(activityId);
  1082. }
  1083. virtual bool isIntercepted() const
  1084. {
  1085. return ctx ? ctx->isIntercepted() : false;
  1086. }
  1087. virtual bool isBlind() const
  1088. {
  1089. return ctx ? ctx->isBlind() : blindLogging;
  1090. }
  1091. virtual unsigned queryTraceLevel() const
  1092. {
  1093. if (ctx)
  1094. return ctx->queryTraceLevel();
  1095. else
  1096. return traceLevel;
  1097. }
  1098. virtual void setGlobalId(const char *id, SocketEndpoint&ep, unsigned pid) override
  1099. {
  1100. if (ctx)
  1101. ctx->setGlobalId(id, ep, pid);
  1102. }
  1103. virtual void setCallerId(const char *id) override
  1104. {
  1105. if (ctx)
  1106. ctx->setCallerId(id);
  1107. }
  1108. virtual const char *queryGlobalId() const
  1109. {
  1110. return ctx ? ctx->queryGlobalId() : nullptr;
  1111. }
  1112. virtual const char *queryCallerId() const override
  1113. {
  1114. return ctx ? ctx->queryCallerId() : nullptr;
  1115. }
  1116. virtual const char *queryLocalId() const
  1117. {
  1118. return ctx ? ctx->queryLocalId() : nullptr;
  1119. }
  1120. virtual void setHttpIdHeaders(const char *global, const char *caller)
  1121. {
  1122. if (ctx)
  1123. ctx->setHttpIdHeaders(global, caller);
  1124. }
  1125. virtual const char *queryGlobalIdHttpHeader() const
  1126. {
  1127. return ctx ? ctx->queryGlobalIdHttpHeader() : "HPCC-Global-Id";
  1128. }
  1129. virtual const char *queryCallerIdHttpHeader() const
  1130. {
  1131. return ctx ? ctx->queryCallerIdHttpHeader() : "HPCC-Caller-Id";
  1132. }
  1133. virtual bool isPassThrough()
  1134. {
  1135. return false;
  1136. }
  1137. virtual const IResolvedFile *resolveLFN(const char *filename, bool isOpt, bool isPrivilegedUser)
  1138. {
  1139. return ctx->resolveLFN(filename, isOpt, isPrivilegedUser);
  1140. }
  1141. virtual const IResolvedFile *queryVarFileInfo() const
  1142. {
  1143. throwUnexpected(); // should be implemented in more derived class by anyone that has a remote adaptor
  1144. return NULL;
  1145. }
  1146. virtual void serializeSkipInfo(MemoryBuffer &out, unsigned seekLen, const void *rawSeek, unsigned numFields, const void * seek, const SmartStepExtra &stepExtra) const
  1147. {
  1148. throwUnexpected(); // should be implemented in more derived class wherever needed
  1149. }
  1150. virtual IRoxieAgentContext *queryContext()
  1151. {
  1152. return ctx;
  1153. }
  1154. virtual IEngineRowStream *queryConcreteOutputStream(unsigned whichInput) { assertex(whichInput==0); return this; }
  1155. virtual IStrandJunction *queryConcreteOutputJunction(unsigned idx) const { assertex(idx==0); return junction; }
  1156. virtual IRoxieServerActivity *queryActivity() { return this; }
  1157. virtual IIndexReadActivityInfo *queryIndexReadActivity() { return NULL; }
  1158. virtual bool needsAllocator() const { return false; }
  1159. void _onCreate(IHThorArg *_colocalParent, unsigned _numParallel)
  1160. {
  1161. colocalParent = _colocalParent;
  1162. createPending = true;
  1163. if (needsAllocator())
  1164. ensureRowAllocator();
  1165. processed = 0;
  1166. if (factory)
  1167. factory->onCreateChildQueries(ctx, &basehelper, childGraphs, this, probeManager, *this, _numParallel);
  1168. if (ctx)
  1169. {
  1170. timeActivities = ctx->queryOptions().timeActivities;
  1171. collectFactoryStatistics = ctx->queryOptions().collectFactoryStatistics && !debugging && factory;
  1172. }
  1173. }
  1174. virtual void onCreate(IHThorArg *_colocalParent)
  1175. {
  1176. _onCreate(_colocalParent, 1);
  1177. }
  1178. virtual void serializeCreateStartContext(MemoryBuffer &out)
  1179. {
  1180. //This should only be called after onStart has been called on the helper
  1181. assertex(!createPending);
  1182. assertex(state==STATEstarted);
  1183. unsigned startlen = out.length();
  1184. basehelper.serializeCreateContext(out);
  1185. basehelper.serializeStartContext(out);
  1186. if (queryTraceLevel() > 10)
  1187. CTXLOG("serializeCreateStartContext for %d added %d bytes", activityId, out.length()-startlen);
  1188. }
  1189. virtual void serializeExtra(MemoryBuffer &out) {}
  1190. inline void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  1191. {
  1192. CriticalBlock cb(statecrit);
  1193. if (state != STATEreset && state != STATEstarting)
  1194. {
  1195. CTXLOG("STATE: Expected state to be reset, but was %s, in activity %d", queryStateText(state), activityId);
  1196. }
  1197. state=STATEstarted;
  1198. numStarts++;
  1199. #ifdef TRACE_STARTSTOP
  1200. if (traceStartStop)
  1201. {
  1202. CTXLOG("start %p %d", this, activityId);
  1203. if (watchActivityId && watchActivityId==activityId)
  1204. {
  1205. CTXLOG("WATCH: start %p %d", this, activityId);
  1206. }
  1207. }
  1208. #endif
  1209. executeDependencies(parentExtractSize, parentExtract, 0);
  1210. if (input)
  1211. input->start(parentExtractSize, parentExtract, paused);
  1212. ensureCreated();
  1213. basehelper.onStart(parentExtract, NULL);
  1214. if (collectFactoryStatistics)
  1215. factory->noteStarted();
  1216. startJunction(junction);
  1217. }
  1218. void executeDependencies(unsigned parentExtractSize, const byte *parentExtract, unsigned controlId)
  1219. {
  1220. //MORE: Create a filtered list and then use asyncfor
  1221. ForEachItemIn(idx, dependencies)
  1222. {
  1223. if (dependencyControlIds.item(idx) == (int) controlId)
  1224. dependencies.item(idx).execute(parentExtractSize, parentExtract);
  1225. }
  1226. }
  1227. void stopDependencies(unsigned parentExtractSize, const byte *parentExtract, unsigned controlId)
  1228. {
  1229. ForEachItemIn(idx, dependencies)
  1230. {
  1231. if (dependencyControlIds.item(idx) == (int) controlId)
  1232. dependencies.item(idx).stop();
  1233. }
  1234. }
  1235. virtual unsigned __int64 queryTotalCycles() const
  1236. {
  1237. return activityStats.totalCycles;
  1238. }
  1239. virtual unsigned __int64 queryLocalCycles() const
  1240. {
  1241. __int64 ret = activityStats.totalCycles;
  1242. if (input) ret -= input->queryTotalCycles();
  1243. if (ret < 0)
  1244. ret = 0;
  1245. return ret;
  1246. }
  1247. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  1248. {
  1249. if (idx==0)
  1250. return input;
  1251. else
  1252. return NULL;
  1253. }
  1254. inline void ensureCreated()
  1255. {
  1256. if (createPending)
  1257. {
  1258. createPending = false;
  1259. interceptedCtx.set(ctx->queryCodeContext());
  1260. basehelper.onCreate(&interceptedCtx, colocalParent, NULL);
  1261. }
  1262. }
  1263. virtual void stop()
  1264. {
  1265. // NOTE - don't be tempted to skip the stop for activities that are reset - splitters need to see the stops
  1266. if (state != STATEstopped)
  1267. {
  1268. CriticalBlock cb(statecrit);
  1269. if (state != STATEstopped)
  1270. {
  1271. #ifdef TRACE_STARTSTOP
  1272. if (traceStartStop)
  1273. {
  1274. CTXLOG("stop %p %d (state currently %s)", this, activityId, queryStateText(state));
  1275. if (watchActivityId && watchActivityId==activityId)
  1276. {
  1277. CTXLOG("WATCH: stop %p %d", this, activityId);
  1278. }
  1279. }
  1280. #endif
  1281. state=STATEstopped;
  1282. // NOTE - this is needed to ensure that dependencies which were not used are properly stopped
  1283. ForEachItemIn(idx, dependencies)
  1284. {
  1285. if (dependencyControlIds.item(idx) == 0)
  1286. dependencies.item(idx).stopSink(dependencyIndexes.item(idx));
  1287. }
  1288. if (inputStream)
  1289. inputStream->stop();
  1290. }
  1291. }
  1292. }
  1293. virtual void abort()
  1294. {
  1295. aborted = true;
  1296. // MORE - should abort dependencies here?
  1297. stop();
  1298. }
  1299. inline void reset()
  1300. {
  1301. if (state != STATEreset)
  1302. {
  1303. CriticalBlock cb(statecrit);
  1304. if (state != STATEreset)
  1305. {
  1306. if (state==STATEstarted || state==STATEstarting)
  1307. {
  1308. if (ctx->queryOptions().failOnLeaks)
  1309. throw makeStringExceptionV(ROXIE_INTERNAL_ERROR, "STATE: activity %d reset without stop", activityId);
  1310. if (traceStartStop || traceLevel > 2)
  1311. CTXLOG("STATE: activity %d reset without stop", activityId);
  1312. stop();
  1313. }
  1314. state = STATEreset;
  1315. #ifdef TRACE_STARTSTOP
  1316. if (traceStartStop)
  1317. {
  1318. CTXLOG("reset %p %d", this, activityId);
  1319. if (watchActivityId && watchActivityId==activityId)
  1320. {
  1321. CTXLOG("WATCH: reset %p %d", this, activityId);
  1322. }
  1323. }
  1324. #endif
  1325. resetJunction(junction);
  1326. ForEachItemIn(idx, dependencies)
  1327. dependencies.item(idx).reset();
  1328. if (input)
  1329. input->reset();
  1330. }
  1331. }
  1332. aborted = false;
  1333. }
  1334. virtual void addDependency(IRoxieServerActivity &source, unsigned sourceIdx, int controlId)
  1335. {
  1336. dependencies.append(source);
  1337. dependencyIndexes.append(sourceIdx);
  1338. dependencyControlIds.append(controlId);
  1339. }
  1340. virtual void resetEOF()
  1341. {
  1342. //would make more sense if the default implementation (and eof member) were in the base class
  1343. }
  1344. // Sink activities should override this....
  1345. virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
  1346. {
  1347. throw MakeStringException(ROXIE_SINK, "Internal error: execute() requires a sink");
  1348. }
  1349. virtual void executeChild(size32_t & retSize, void * & ret, unsigned parentExtractSize, const byte * parentExtract)
  1350. {
  1351. throw MakeStringException(ROXIE_SINK, "Internal error: executeChild() requires a suitable sink");
  1352. }
  1353. virtual void stopSink(unsigned idx)
  1354. {
  1355. throw MakeStringException(ROXIE_SINK, "Internal error: stopSink() requires a suitable sink");
  1356. }
  1357. virtual void connectInputStreams(bool consumerOrdered)
  1358. {
  1359. if (input && !inputStream)
  1360. inputStream = connectSingleStream(ctx, input, sourceIdx, junction, isInputOrdered(consumerOrdered, 0));
  1361. if (!connected)
  1362. {
  1363. connectDependencies();
  1364. connected = true;
  1365. }
  1366. }
  1367. void connectDependencies()
  1368. {
  1369. ForEachItemIn(i, dependencies)
  1370. dependencies.item(i).connectInputStreams(true);
  1371. }
  1372. virtual __int64 evaluate()
  1373. {
  1374. throw MakeStringException(ROXIE_SINK, "Internal error: evaluate() requires a function");
  1375. }
  1376. virtual IFinalRoxieInput * querySelectOutput(unsigned id)
  1377. {
  1378. return NULL;
  1379. }
  1380. virtual bool querySetStreamInput(unsigned id, unsigned _sourceIdx, IFinalRoxieInput * _input)
  1381. {
  1382. return false;
  1383. }
  1384. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  1385. {
  1386. assertex(!idx);
  1387. input = _in;
  1388. sourceIdx = _sourceIdx;
  1389. inputStream = NULL;
  1390. }
  1391. bool isInputOrdered(bool consumerOrdered, unsigned idx) const
  1392. {
  1393. if (!factory)
  1394. return true;
  1395. //Grouping implies the stream processing must be ordered - I think this always applies
  1396. IFinalRoxieInput * curInput = queryInput(idx);
  1397. if (curInput && curInput->queryOutputMeta()->isGrouped())
  1398. return true;
  1399. return factory->isInputOrdered(consumerOrdered, idx);
  1400. }
  1401. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks)
  1402. {
  1403. assertex(!idx);
  1404. // By default, activities are assumed NOT to support streams
  1405. bool inputOrdered = isInputOrdered(consumerOrdered, 0);
  1406. connectInputStreams(inputOrdered);
  1407. // Return a single stream
  1408. streams.append(this);
  1409. return NULL;
  1410. }
  1411. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  1412. {
  1413. if (idx == (unsigned) -1)
  1414. idx = 0;
  1415. return idx ? NULL : this;
  1416. }
  1417. virtual IOutputMetaData *queryOutputMeta() const
  1418. {
  1419. return meta.queryOriginal();
  1420. }
  1421. virtual unsigned queryId() const
  1422. {
  1423. return activityId;
  1424. }
  1425. virtual unsigned querySubgraphId() const
  1426. {
  1427. return factory->querySubgraphId();
  1428. }
  1429. virtual void checkAbort()
  1430. {
  1431. ctx->checkAbort();
  1432. }
  1433. IException *makeWrappedException(IException *e)
  1434. {
  1435. StringBuffer msg;
  1436. ThorActivityKind activityKind = factory ? factory->getKind() : TAKnone;
  1437. CTXLOG("makeWrappedException - %s (in %s %d)", e->errorMessage(msg).str(), getActivityText(activityKind), activityId);
  1438. if (QUERYINTERFACE(e, CWrappedException) || QUERYINTERFACE(e, IUserException))
  1439. return e;
  1440. else
  1441. return new CWrappedException(e, activityKind, activityId);
  1442. }
  1443. virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract)
  1444. {
  1445. }
  1446. virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract, IProbeManager *probeManager, IArrayOf<IRoxieProbe> &probes)
  1447. {
  1448. }
  1449. virtual void resetOutputsUsed()
  1450. {
  1451. }
  1452. virtual void noteOutputUsed()
  1453. {
  1454. }
  1455. virtual const IRoxieServerActivityFactory *queryFactory() const
  1456. {
  1457. return factory;
  1458. }
  1459. virtual void updateEdgeStats(IStatisticGatherer * statsBuilder) const
  1460. {
  1461. addEdgeStats(statsBuilder, 0, numStarts, processed, 1);
  1462. }
  1463. inline ThorActivityKind getKind() const
  1464. {
  1465. return factory->getKind();
  1466. }
  1467. inline bool isSink() const
  1468. {
  1469. return (factory != NULL) && factory->isSink();
  1470. }
  1471. void addEdgeStats(IStatisticGatherer * statsBuilder, unsigned oid, unsigned starts, unsigned _processed, unsigned _strands) const
  1472. {
  1473. if (statsBuilder)
  1474. {
  1475. StatsEdgeScope scope(*statsBuilder, activityId, oid);
  1476. if (_strands)
  1477. statsBuilder->addStatistic(StNumStrands, _strands);
  1478. if (starts != 0)
  1479. {
  1480. statsBuilder->addStatistic(StNumRowsProcessed, _processed);
  1481. statsBuilder->addStatistic(StNumStarts, starts);
  1482. // Assume number of starts = number of stops since stats only ever gathered when a query is complete
  1483. statsBuilder->addStatistic(StNumStops, starts);
  1484. statsBuilder->addStatistic(StNumSlaves, 1); // Arguable
  1485. }
  1486. }
  1487. if (collectFactoryStatistics)
  1488. factory->noteProcessed(oid, processed);
  1489. }
  1490. protected:
  1491. RecordTranslationMode getEnableFieldTranslation() const
  1492. {
  1493. return factory->getEnableFieldTranslation();
  1494. }
  1495. };
  1496. //=====================================================================================================
  1497. class CRoxieServerLateStartActivity : public CRoxieServerActivity
  1498. {
  1499. protected:
  1500. bool prefiltered;
  1501. bool eof;
  1502. void lateStart(unsigned parentExtractSize, const byte *parentExtract, bool any)
  1503. {
  1504. prefiltered = !any;
  1505. eof = prefiltered;
  1506. if (!prefiltered)
  1507. {
  1508. input->start(parentExtractSize, parentExtract, false);
  1509. startJunction(junction);
  1510. }
  1511. else
  1512. {
  1513. if (traceStartStop)
  1514. CTXLOG("lateStart activity stopping input early as prefiltered");
  1515. inputStream->stop();
  1516. }
  1517. }
  1518. public:
  1519. CRoxieServerLateStartActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  1520. : CRoxieServerActivity(_ctx, _factory, _probeManager)
  1521. {
  1522. prefiltered = false;
  1523. eof = false;
  1524. }
  1525. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  1526. {
  1527. IFinalRoxieInput *saveInput = input;
  1528. Owned<IStrandJunction> saveJunction = junction.getClear();
  1529. input = NULL; // Make sure parent does not start the chain yet
  1530. try
  1531. {
  1532. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  1533. }
  1534. catch (...)
  1535. {
  1536. // Make sure we restore these even if there is an exception thrown during start
  1537. input = saveInput;
  1538. junction.setown(saveJunction.getClear());
  1539. throw;
  1540. }
  1541. input = saveInput;
  1542. junction.setown(saveJunction.getClear());
  1543. }
  1544. virtual void stop()
  1545. {
  1546. if (!prefiltered && inputStream)
  1547. {
  1548. inputStream->stop();
  1549. }
  1550. else if (traceStartStop)
  1551. CTXLOG("lateStart activity NOT stopping input late as prefiltered");
  1552. IEngineRowStream *save = inputStream;
  1553. inputStream = NULL; // We already stopped inputStream - prevent parent from doing so again
  1554. CRoxieServerActivity::stop();
  1555. inputStream = save;
  1556. }
  1557. virtual void reset()
  1558. {
  1559. CRoxieServerActivity::reset();
  1560. prefiltered = false;
  1561. }
  1562. };
  1563. //=================================================================================
  1564. class StrandOptions
  1565. {
  1566. // Typically set from hints, common to many stranded activities
  1567. public:
  1568. explicit StrandOptions(IPropertyTree &_graphNode)
  1569. {
  1570. //PARALLEL(1) can be used to explicitly disable parallel processing.
  1571. numStrands = _graphNode.getPropInt("att[@name='parallel']/@value", 0);
  1572. if ((numStrands == minus1U) || (numStrands > MAX_SENSIBLE_STRANDS))
  1573. numStrands = getAffinityCpus();
  1574. blockSize = _graphNode.getPropInt("hint[@name='strandblocksize']/@value", 0);
  1575. }
  1576. StrandOptions(const StrandOptions &from, IRoxieAgentContext *ctx)
  1577. {
  1578. numStrands = from.numStrands;
  1579. blockSize = from.blockSize;
  1580. if (!blockSize)
  1581. blockSize = ctx->queryOptions().strandBlockSize;
  1582. if (numStrands == 0)
  1583. numStrands = ctx->queryOptions().forceNumStrands;
  1584. }
  1585. public:
  1586. unsigned numStrands = 0; // if 1 it forces single-stranded operations. (Useful for testing.)
  1587. unsigned blockSize = 0;
  1588. };
  1589. class StrandProcessor : public CInterfaceOf<IEngineRowStream>
  1590. {
  1591. protected:
  1592. CRoxieServerActivity &parent;
  1593. IEngineRowAllocator *rowAllocator;
  1594. IEngineRowStream *inputStream;
  1595. ActivityTimeAccumulator activityStats;
  1596. mutable CRuntimeStatisticCollection stats;
  1597. unsigned processed = 0;
  1598. unsigned numProcessedLastGroup = 0;
  1599. const bool timeActivities;
  1600. bool stopped = false;
  1601. std::atomic<bool> abortRequested;
  1602. public:
  1603. explicit StrandProcessor(CRoxieServerActivity &_parent, IEngineRowStream *_inputStream, bool needsAllocator)
  1604. : parent(_parent), inputStream(_inputStream), stats(parent.queryStatsMapping()), timeActivities(_parent.timeActivities), abortRequested(false)
  1605. {
  1606. if (needsAllocator)
  1607. {
  1608. rowAllocator = parent.createRowAllocatorEx(parent.queryOutputMeta(), roxiemem::RHFunique);
  1609. }
  1610. else
  1611. rowAllocator = NULL;
  1612. }
  1613. ~StrandProcessor()
  1614. {
  1615. ::Release(rowAllocator);
  1616. }
  1617. virtual void start()
  1618. {
  1619. processed = 0;
  1620. numProcessedLastGroup = 0;
  1621. activityStats.reset();
  1622. }
  1623. virtual void stop()
  1624. {
  1625. if (!stopped)
  1626. {
  1627. if (inputStream)
  1628. inputStream->stop();
  1629. parent.stop();
  1630. //It would be preferrable to move activityStats (+processed?) to gatherStats(), but because of
  1631. //the way firstRow is associated with startCycles it would require an extra parameter
  1632. //which is not consistent with the other gatherStats() calls, or extra logic in the stats classes.
  1633. parent.mergeStrandStats(processed, activityStats);
  1634. }
  1635. stopped = true;
  1636. }
  1637. virtual void reset()
  1638. {
  1639. stopped = false;
  1640. abortRequested.store(false, std::memory_order_relaxed);
  1641. }
  1642. virtual void resetEOF()
  1643. {
  1644. inputStream->resetEOF();
  1645. }
  1646. inline void requestAbort() { abortRequested.store(true, std::memory_order_relaxed); }
  1647. inline bool isAborting() { return abortRequested.load(std::memory_order_relaxed); }
  1648. void gatherStats(CRuntimeStatisticCollection & mergedStats) const
  1649. {
  1650. mergedStats.merge(stats);
  1651. if (rowAllocator)
  1652. rowAllocator->gatherStats(mergedStats);
  1653. }
  1654. const ActivityTimeAccumulator& queryTimings() const { return activityStats; }
  1655. };
  1656. class CRoxieServerStrandedActivity : public CRoxieServerActivity
  1657. {
  1658. protected:
  1659. StrandOptions strandOptions;
  1660. IArrayOf<StrandProcessor> strands;
  1661. Owned<IStrandBranch> branch;
  1662. Owned<IStrandJunction> splitter;
  1663. Owned<IStrandJunction> sourceJunction; // A junction applied to the output of a source activity
  1664. std::atomic<unsigned> active;
  1665. public:
  1666. CRoxieServerStrandedActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const StrandOptions &_strandOptions)
  1667. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  1668. strandOptions(_strandOptions, ctx)
  1669. {
  1670. active = 0;
  1671. }
  1672. virtual void gatherStats(CRuntimeStatisticCollection & merged) const override
  1673. {
  1674. CRoxieServerActivity::gatherStats(merged);
  1675. ForEachItemIn(i, strands)
  1676. strands.item(i).gatherStats(merged);
  1677. }
  1678. virtual void onCreate(IHThorArg *_colocalArg)
  1679. {
  1680. CRoxieServerActivity::_onCreate(_colocalArg, strands.ordinality());
  1681. }
  1682. //This function is pure (But also implemented out of line) to force the derived classes to implement it.
  1683. //After calling the base class start method, and initialising any values from the helper they must call onStartStrands(),
  1684. //this must also happen before any rows are read from the strands (e.g., by a source junction)
  1685. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused) = 0;
  1686. virtual void reset()
  1687. {
  1688. assertex(active==0);
  1689. activityStats.reset();
  1690. ForEachItemIn(idx, strands)
  1691. {
  1692. strands.item(idx).reset();
  1693. activityStats.merge(strands.item(idx).queryTimings());
  1694. }
  1695. resetJunction(splitter);
  1696. CRoxieServerActivity::reset();
  1697. resetJunction(sourceJunction);
  1698. }
  1699. virtual void stop()
  1700. {
  1701. // Called from the strands... which should ensure that stop is not called more than once per strand
  1702. //The first strand to call
  1703. if (active)
  1704. --active;
  1705. if (!active)
  1706. CRoxieServerActivity::stop();
  1707. }
  1708. void requestAbort()
  1709. {
  1710. ForEachItemIn(idx, strands)
  1711. strands.item(idx).requestAbort();
  1712. }
  1713. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks) override
  1714. {
  1715. assertex(idx == 0);
  1716. assertex(strands.empty());
  1717. CRoxieServerActivity::connectDependencies();
  1718. bool inputOrdered = isInputOrdered(consumerOrdered, idx);
  1719. //Note, numStrands == 1 is an explicit request to disable threading
  1720. if (consumerOptions && (consumerOptions->numStrands != 1) && (strandOptions.numStrands != 1))
  1721. {
  1722. //Check to see if the consumer's settings should override
  1723. if (strandOptions.numStrands == 0)
  1724. {
  1725. strandOptions.numStrands = consumerOptions->numStrands;
  1726. strandOptions.blockSize = consumerOptions->blockSize;
  1727. }
  1728. else if (consumerOptions->numStrands > strandOptions.numStrands)
  1729. {
  1730. strandOptions.numStrands = consumerOptions->numStrands;
  1731. }
  1732. }
  1733. Owned <IStrandJunction> recombiner;
  1734. if (input)
  1735. {
  1736. if (strandOptions.numStrands == 1)
  1737. {
  1738. // 1 means explicitly requested single-strand.
  1739. IEngineRowStream *instream = connectSingleStream(ctx, input, sourceIdx, junction, inputOrdered);
  1740. strands.append(*createStrandProcessor(instream));
  1741. }
  1742. else
  1743. {
  1744. PointerArrayOf<IEngineRowStream> instreams;
  1745. recombiner.setown(input->getOutputStreams(ctx, sourceIdx, instreams, &strandOptions, inputOrdered, orderedCallbacks));
  1746. if ((instreams.length() == 1) && (strandOptions.numStrands != 0)) // 0 means did not specify - we should use the strands that our upstream provides
  1747. {
  1748. assertex(recombiner == NULL);
  1749. // Create a splitter to split the input into n... and a recombiner if need to preserve sorting
  1750. if (inputOrdered)
  1751. {
  1752. branch.setown(createStrandBranch(ctx->queryRowManager(), strandOptions.numStrands, strandOptions.blockSize, true, input->queryOutputMeta()->isGrouped(), false, orderedCallbacks));
  1753. splitter.set(branch->queryInputJunction());
  1754. recombiner.set(branch->queryOutputJunction());
  1755. }
  1756. else
  1757. {
  1758. splitter.setown(createStrandJunction(ctx->queryRowManager(), 1, strandOptions.numStrands, strandOptions.blockSize, false));
  1759. }
  1760. splitter->setInput(0, instreams.item(0));
  1761. for (unsigned strandNo = 0; strandNo < strandOptions.numStrands; strandNo++)
  1762. strands.append(*createStrandProcessor(splitter->queryOutput(strandNo)));
  1763. }
  1764. else
  1765. {
  1766. // Ignore my hint and just use the width already split into...
  1767. ForEachItemIn(strandNo, instreams)
  1768. strands.append(*createStrandProcessor(instreams.item(strandNo)));
  1769. }
  1770. }
  1771. }
  1772. else
  1773. {
  1774. unsigned numStrands = strandOptions.numStrands ? strandOptions.numStrands : 1;
  1775. for (unsigned i=0; i < numStrands; i++)
  1776. strands.append(*createStrandSourceProcessor(inputOrdered));
  1777. if (inputOrdered && (numStrands > 1))
  1778. {
  1779. if (consumerOptions)
  1780. {
  1781. //If the output activities are also stranded then need to create a version of the branch
  1782. bool isGrouped = queryOutputMeta()->isGrouped();
  1783. branch.setown(createStrandBranch(ctx->queryRowManager(), strandOptions.numStrands, strandOptions.blockSize, true, isGrouped, true, orderedCallbacks));
  1784. sourceJunction.set(branch->queryInputJunction());
  1785. recombiner.set(branch->queryOutputJunction());
  1786. assertex((orderedCallbacks && !recombiner) || (!orderedCallbacks && recombiner));
  1787. //This is different from the branch above. The first "junction" has the source activity as the input, and the outputs as the result of the activity
  1788. for (unsigned strandNo = 0; strandNo < strandOptions.numStrands; strandNo++)
  1789. {
  1790. sourceJunction->setInput(strandNo, &strands.item(strandNo));
  1791. streams.append(sourceJunction->queryOutput(strandNo));
  1792. }
  1793. #ifdef TRACE_STRANDS
  1794. if (traceLevel > 2)
  1795. DBGLOG("Executing activity %u with %u strands", activityId, strands.ordinality());
  1796. #endif
  1797. return recombiner.getClear();
  1798. }
  1799. else
  1800. {
  1801. //Feeding into a non threaded activity, so create a M:1 junction to combine the source strands
  1802. recombiner.setown(createStrandJunction(ctx->queryRowManager(), numStrands, 1, strandOptions.blockSize, inputOrdered));
  1803. }
  1804. }
  1805. }
  1806. ForEachItemIn(i, strands)
  1807. streams.append(&strands.item(i));
  1808. #ifdef TRACE_STRANDS
  1809. if (traceLevel > 2)
  1810. DBGLOG("Executing activity %u with %u strands", activityId, strands.ordinality());
  1811. #endif
  1812. return recombiner.getClear();
  1813. }
  1814. virtual StrandProcessor *createStrandProcessor(IEngineRowStream *instream) = 0;
  1815. //MORE: Possibly this class should be split into two for sinks and non sinks...
  1816. virtual StrandProcessor *createStrandSourceProcessor(bool inputOrdered) = 0;
  1817. virtual const void * nextRow()
  1818. {
  1819. throwUnexpected();
  1820. }
  1821. inline unsigned numStrands() const { return strands.ordinality(); }
  1822. virtual void updateEdgeStats(IStatisticGatherer * statsBuilder) const
  1823. {
  1824. addEdgeStats(statsBuilder, 0, numStarts, processed, strands.ordinality());
  1825. }
  1826. protected:
  1827. void onStartStrands()
  1828. {
  1829. ForEachItemIn(idx, strands)
  1830. {
  1831. strands.item(idx).start();
  1832. active++;
  1833. }
  1834. }
  1835. };
  1836. //For some reason gcc doesn't let you specify a function as pure virtual and define it at the same time.
  1837. void CRoxieServerStrandedActivity::start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  1838. {
  1839. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  1840. startJunction(splitter);
  1841. }
  1842. class CRoxieServerStrandedLateStartActivity : public CRoxieServerStrandedActivity
  1843. {
  1844. protected:
  1845. bool prefiltered;
  1846. bool eof;
  1847. void lateStart(unsigned parentExtractSize, const byte *parentExtract, bool any)
  1848. {
  1849. prefiltered = !any;
  1850. eof = prefiltered;
  1851. if (!prefiltered)
  1852. {
  1853. //At this point
  1854. input->start(parentExtractSize, parentExtract, false);
  1855. startJunction(splitter);
  1856. onStartStrands(); // Initialise the strands - the helper will have been correctly initialized by this point.
  1857. startJunction(sourceJunction);
  1858. }
  1859. else
  1860. {
  1861. if (traceStartStop)
  1862. CTXLOG("strandedLateStart activity stopping input early as prefiltered");
  1863. ForEachItemIn(idx, strands)
  1864. strands.item(idx).stop();
  1865. }
  1866. }
  1867. public:
  1868. CRoxieServerStrandedLateStartActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const StrandOptions &_strandOptions)
  1869. : CRoxieServerStrandedActivity(_ctx, _factory, _probeManager, _strandOptions)
  1870. {
  1871. prefiltered = false;
  1872. eof = false;
  1873. }
  1874. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  1875. {
  1876. IFinalRoxieInput *save = input;
  1877. input = NULL; // Make sure parent does not start the chain yet - but we do want to do the dependencies (because the decision about whether to start may depend on them)
  1878. try
  1879. {
  1880. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  1881. }
  1882. catch (...)
  1883. {
  1884. // Make sure we restore these even if there is an exception thrown during start
  1885. input = save;
  1886. throw;
  1887. }
  1888. input = save;
  1889. }
  1890. virtual void reset()
  1891. {
  1892. CRoxieServerStrandedActivity::reset();
  1893. prefiltered = false;
  1894. }
  1895. };
  1896. //=====================================================================================================
  1897. atomic_t nextInstanceId;
  1898. extern unsigned getNextInstanceId()
  1899. {
  1900. return atomic_add_exchange(&nextInstanceId, 1)+1;
  1901. }
  1902. atomic_t nextRuid;
  1903. ruid_t getNextRuid()
  1904. {
  1905. ruid_t ret = atomic_add_exchange(&nextRuid, 1)+1;
  1906. while (ret < RUID_FIRST)
  1907. ret = atomic_add_exchange(&nextRuid, 1)+1; // ruids 0 and 1 are reserved for pings/unwanted discarder.
  1908. return ret;
  1909. }
  1910. void setStartRuid(unsigned restarts)
  1911. {
  1912. atomic_set(&nextRuid, restarts * 0x10000);
  1913. atomic_set(&nextInstanceId, restarts * 10000);
  1914. }
  1915. enum { LimitSkipErrorCode = 0, KeyedLimitSkipErrorCode = 1 };
  1916. class DECL_EXCEPTION LimitSkipException : public IException, public CInterface
  1917. {
  1918. int code;
  1919. public:
  1920. LimitSkipException(int _code) { code = _code; }
  1921. IMPLEMENT_IINTERFACE;
  1922. virtual int errorCode() const { return code; }
  1923. virtual StringBuffer & errorMessage(StringBuffer &msg) const { return msg.append("LimitSkipException"); }
  1924. virtual MessageAudience errorAudience() const { return MSGAUD_programmer; }
  1925. };
  1926. IException *makeLimitSkipException(bool isKeyed)
  1927. {
  1928. // We need to make sure what we throw is IException not something derived from it....
  1929. return new LimitSkipException(isKeyed ? KeyedLimitSkipErrorCode : LimitSkipErrorCode);
  1930. }
  1931. //=================================================================================
  1932. interface IRecordPullerCallback : extends IExceptionHandler
  1933. {
  1934. virtual void processRow(const void *row) = 0;
  1935. virtual void processEOG() = 0;
  1936. virtual void processGroup(const ConstPointerArray &rows) = 0;
  1937. virtual void processDone() = 0;
  1938. };
  1939. class RecordPullerThread : public RestartableThread
  1940. {
  1941. protected:
  1942. IFinalRoxieInput *input;
  1943. Owned<IStrandJunction> junction;
  1944. unsigned sourceIdx = 0;
  1945. IEngineRowStream *inputStream;
  1946. IRecordPullerCallback *helper;
  1947. Semaphore started; // MORE: GH->RKC I'm pretty sure this can be deleted, since handled by RestartableThread
  1948. bool groupAtOnce, eog;
  1949. std::atomic<bool> eof;
  1950. CriticalSection crit;
  1951. public:
  1952. RecordPullerThread(bool _groupAtOnce)
  1953. : RestartableThread("RecordPullerThread"), groupAtOnce(_groupAtOnce)
  1954. {
  1955. input = NULL;
  1956. inputStream = NULL;
  1957. helper = NULL;
  1958. eof = eog = FALSE;
  1959. }
  1960. inline unsigned __int64 queryTotalCycles() const
  1961. {
  1962. return input->queryTotalCycles();
  1963. }
  1964. void setInput(IRecordPullerCallback *_helper, unsigned _sourceIdx, IFinalRoxieInput *_input)
  1965. {
  1966. helper = _helper;
  1967. input = _input;
  1968. sourceIdx = _sourceIdx;
  1969. }
  1970. IFinalRoxieInput *queryInput() const
  1971. {
  1972. return input;
  1973. }
  1974. virtual void connectInputStreams(IRoxieAgentContext *ctx, bool consumerOrdered)
  1975. {
  1976. inputStream = connectSingleStream(ctx, input, sourceIdx, junction, consumerOrdered);
  1977. }
  1978. IEngineRowStream *queryStream() const
  1979. {
  1980. return inputStream;
  1981. }
  1982. void start(unsigned parentExtractSize, const byte *parentExtract, bool paused, unsigned preload, bool noThread, IRoxieAgentContext *ctx)
  1983. {
  1984. eof = false;
  1985. eog = false;
  1986. input->start(parentExtractSize, parentExtract, paused);
  1987. startJunction(junction);
  1988. try
  1989. {
  1990. if (preload && !paused)
  1991. {
  1992. if (traceLevel > 4)
  1993. DBGLOG("Preload fetching first %d records", preload);
  1994. if (groupAtOnce)
  1995. pullGroups(preload);
  1996. else
  1997. pullRecords(preload);
  1998. }
  1999. if (eof)
  2000. {
  2001. if (traceLevel > 4)
  2002. DBGLOG("No need to start puller after preload");
  2003. helper->processDone();
  2004. }
  2005. else
  2006. {
  2007. if (!noThread)
  2008. {
  2009. StringBuffer logPrefix("[");
  2010. if (ctx) ctx->getLogPrefix(logPrefix);
  2011. logPrefix.append("] ");
  2012. RestartableThread::start(logPrefix);
  2013. started.wait();
  2014. }
  2015. }
  2016. }
  2017. catch (IException *e)
  2018. {
  2019. helper->fireException(e);
  2020. }
  2021. catch (...)
  2022. {
  2023. helper->fireException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unexpected exception caught in RecordPullerThread::start"));
  2024. }
  2025. }
  2026. void stop()
  2027. {
  2028. if (traceStartStop)
  2029. DBGLOG("RecordPullerThread::stop");
  2030. {
  2031. 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-nextRow etc etc.
  2032. if (inputStream)
  2033. inputStream->stop();
  2034. eof = true;
  2035. }
  2036. RestartableThread::join();
  2037. }
  2038. void reset()
  2039. {
  2040. input->reset();
  2041. resetJunction(junction);
  2042. }
  2043. virtual int run()
  2044. {
  2045. started.signal();
  2046. try
  2047. {
  2048. if (groupAtOnce)
  2049. pullGroups((unsigned) -1);
  2050. else
  2051. pullRecords((unsigned) -1);
  2052. helper->processDone();
  2053. }
  2054. catch (IException *e)
  2055. {
  2056. helper->fireException(e);
  2057. }
  2058. catch (...)
  2059. {
  2060. helper->fireException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unexpected exception caught in RecordPullerThread::run"));
  2061. }
  2062. return 0;
  2063. }
  2064. void done()
  2065. {
  2066. helper->processDone();
  2067. }
  2068. bool pullRecords(unsigned preload)
  2069. {
  2070. if (eof)
  2071. return false;
  2072. while (preload && !eof)
  2073. {
  2074. const void * row = nullptr;
  2075. {
  2076. CriticalBlock c(crit); // See comments in stop for why this is needed
  2077. if (!eof)
  2078. row = inputStream->nextRow();
  2079. }
  2080. if (row)
  2081. {
  2082. eog = false;
  2083. helper->processRow(row);
  2084. }
  2085. else if (!eog)
  2086. {
  2087. helper->processEOG();
  2088. eog = true;
  2089. }
  2090. else
  2091. {
  2092. eof = true;
  2093. return false;
  2094. }
  2095. if (preload != (unsigned) -1)
  2096. preload--;
  2097. }
  2098. return true;
  2099. }
  2100. void pullGroups(unsigned preload)
  2101. {
  2102. ConstPointerArray thisGroup;
  2103. unsigned rowsDone = 0;
  2104. while (preload && !eof)
  2105. {
  2106. const void *row = nullptr;
  2107. {
  2108. CriticalBlock c(crit);
  2109. if (!eof)
  2110. row = inputStream->nextRow();
  2111. }
  2112. if (row)
  2113. {
  2114. thisGroup.append(row);
  2115. rowsDone++;
  2116. }
  2117. else if (thisGroup.length())
  2118. {
  2119. helper->processGroup(thisGroup);
  2120. thisGroup.kill();
  2121. if (preload != (unsigned) -1)
  2122. {
  2123. if (preload > rowsDone)
  2124. preload -= rowsDone;
  2125. else
  2126. break;
  2127. }
  2128. rowsDone = 0;
  2129. }
  2130. else
  2131. {
  2132. eof = true;
  2133. break;
  2134. }
  2135. }
  2136. }
  2137. };
  2138. //=================================================================================
  2139. #define READAHEAD_SIZE 1000
  2140. // MORE - this code copied from ThreadedConcat code - may be able to common up some.
  2141. class CRoxieServerReadAheadInput : implements IEngineRowStream, implements IFinalRoxieInput, implements IRecordPullerCallback, public CInterface
  2142. {
  2143. QueueOf<const void, true> buffer;
  2144. InterruptableSemaphore ready;
  2145. InterruptableSemaphore space;
  2146. CriticalSection crit;
  2147. bool eof;
  2148. bool disabled;
  2149. IRoxieAgentContext *ctx;
  2150. RecordPullerThread puller;
  2151. unsigned preload;
  2152. cycle_t totalCycles;
  2153. bool timeActivities;
  2154. public:
  2155. IMPLEMENT_IINTERFACE;
  2156. CRoxieServerReadAheadInput(IRoxieAgentContext *_ctx, unsigned _preload) : ctx(_ctx), puller(true), preload(_preload)
  2157. {
  2158. eof = false;
  2159. disabled = (ctx->queryDebugContext() != NULL);
  2160. totalCycles = 0;
  2161. timeActivities = ctx->queryOptions().timeActivities;
  2162. }
  2163. virtual IRoxieServerActivity *queryActivity()
  2164. {
  2165. return puller.queryInput()->queryActivity();
  2166. }
  2167. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks) override
  2168. {
  2169. assertex(!idx);
  2170. puller.connectInputStreams(ctx, consumerOrdered);
  2171. streams.append(this);
  2172. return NULL;
  2173. }
  2174. virtual IEngineRowStream *queryConcreteOutputStream(unsigned whichInput) { return this; }
  2175. virtual IStrandJunction *queryConcreteOutputJunction(unsigned idx) const { return nullptr; }
  2176. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  2177. {
  2178. return puller.queryInput()->queryIndexReadActivity();
  2179. }
  2180. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  2181. {
  2182. eof = false;
  2183. if (disabled)
  2184. puller.queryInput()->start(parentExtractSize, parentExtract, paused);
  2185. else
  2186. {
  2187. space.reinit(READAHEAD_SIZE);
  2188. ready.reinit();
  2189. puller.start(parentExtractSize, parentExtract, paused, preload, false, ctx);
  2190. }
  2191. }
  2192. virtual void stop()
  2193. {
  2194. if (disabled)
  2195. puller.queryStream()->stop();
  2196. else
  2197. {
  2198. space.interrupt();
  2199. ready.interrupt();
  2200. puller.stop();
  2201. }
  2202. }
  2203. virtual void reset()
  2204. {
  2205. if (disabled)
  2206. puller.queryInput()->reset();
  2207. else
  2208. {
  2209. puller.reset();
  2210. ForEachItemIn(idx1, buffer)
  2211. ReleaseRoxieRow(buffer.item(idx1));
  2212. buffer.clear();
  2213. }
  2214. }
  2215. virtual void resetEOF()
  2216. {
  2217. throwUnexpected();
  2218. }
  2219. virtual IOutputMetaData * queryOutputMeta() const
  2220. {
  2221. return puller.queryInput()->queryOutputMeta();
  2222. }
  2223. void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  2224. {
  2225. assertex(!idx);
  2226. puller.setInput(this, _sourceIdx, _in);
  2227. }
  2228. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  2229. {
  2230. if (idx==0)
  2231. return puller.queryInput();
  2232. else
  2233. return NULL;
  2234. }
  2235. virtual unsigned __int64 queryTotalCycles() const
  2236. {
  2237. return totalCycles;
  2238. }
  2239. virtual const void * nextRow()
  2240. {
  2241. SimpleActivityTimer t(totalCycles, timeActivities);
  2242. if (disabled)
  2243. return puller.queryStream()->nextRow();
  2244. else
  2245. {
  2246. for (;;)
  2247. {
  2248. {
  2249. CriticalBlock b(crit);
  2250. if (eof && !buffer.ordinality())
  2251. return NULL; // eof
  2252. }
  2253. ready.wait();
  2254. const void *ret;
  2255. {
  2256. CriticalBlock b(crit);
  2257. ret = buffer.dequeue();
  2258. }
  2259. space.signal();
  2260. return ret;
  2261. }
  2262. }
  2263. }
  2264. virtual bool fireException(IException *e)
  2265. {
  2266. // called from puller thread on failure
  2267. ready.interrupt(LINK(e));
  2268. space.interrupt(e);
  2269. return true;
  2270. }
  2271. virtual void processRow(const void *row)
  2272. {
  2273. {
  2274. CriticalBlock b(crit);
  2275. buffer.enqueue(row);
  2276. }
  2277. ready.signal();
  2278. space.wait();
  2279. }
  2280. virtual void processGroup(const ConstPointerArray &rows)
  2281. {
  2282. // NOTE - a bit bizarre in that it waits for the space AFTER using it.
  2283. // But the space semaphore is only there to stop infinite readahead. And otherwise it would deadlock
  2284. // if group was larger than max(space)
  2285. {
  2286. CriticalBlock b(crit);
  2287. ForEachItemIn(idx, rows)
  2288. buffer.enqueue(rows.item(idx));
  2289. buffer.enqueue(NULL);
  2290. }
  2291. for (unsigned i2 = 0; i2 <= rows.length(); i2++) // note - does 1 extra for the null
  2292. {
  2293. ready.signal();
  2294. space.wait();
  2295. }
  2296. }
  2297. virtual void processEOG()
  2298. {
  2299. // Used when output is not grouped - just ignore
  2300. }
  2301. virtual void processDone()
  2302. {
  2303. CriticalBlock b(crit);
  2304. eof = true;
  2305. ready.signal();
  2306. }
  2307. };
  2308. //=================================================================================
  2309. class CRoxieServerTwoInputActivity : public CRoxieServerActivity
  2310. {
  2311. protected:
  2312. IFinalRoxieInput *input1;
  2313. IEngineRowStream *inputStream1;
  2314. Owned<IStrandJunction> junction1;
  2315. unsigned sourceIdx1 = 0;
  2316. Owned<CRoxieServerReadAheadInput> puller;
  2317. public:
  2318. CRoxieServerTwoInputActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  2319. : CRoxieServerActivity(_ctx, _factory, _probeManager)
  2320. {
  2321. input1 = NULL;
  2322. inputStream1 = NULL;
  2323. }
  2324. ~CRoxieServerTwoInputActivity()
  2325. {
  2326. }
  2327. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  2328. {
  2329. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  2330. input1->start(parentExtractSize, parentExtract, paused);
  2331. startJunction(junction1);
  2332. }
  2333. virtual void stop()
  2334. {
  2335. inputStream1->stop();
  2336. CRoxieServerActivity::stop();
  2337. }
  2338. virtual unsigned __int64 queryLocalCycles() const
  2339. {
  2340. __int64 ret;
  2341. __int64 inputCycles = input->queryTotalCycles();
  2342. __int64 input1Cycles = input1->queryTotalCycles();
  2343. if (puller)
  2344. ret = activityStats.totalCycles - (inputCycles > input1Cycles ? inputCycles : input1Cycles);
  2345. else
  2346. ret = activityStats.totalCycles - (inputCycles + input1Cycles);
  2347. if (ret < 0)
  2348. ret = 0;
  2349. return ret;
  2350. }
  2351. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  2352. {
  2353. switch (idx)
  2354. {
  2355. case 0:
  2356. return input;
  2357. case 1:
  2358. return input1;
  2359. default:
  2360. return NULL;
  2361. }
  2362. }
  2363. virtual void reset()
  2364. {
  2365. CRoxieServerActivity::reset();
  2366. if (input1)
  2367. input1->reset();
  2368. resetJunction(junction1);
  2369. }
  2370. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  2371. {
  2372. switch(idx)
  2373. {
  2374. case 0:
  2375. CRoxieServerActivity::setInput(idx, _sourceIdx, _in);
  2376. break;
  2377. case 1:
  2378. input1 = _in;
  2379. sourceIdx1 = _sourceIdx;
  2380. break;
  2381. default:
  2382. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", sanitizeSourceFile(__FILE__), __LINE__);
  2383. }
  2384. }
  2385. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks) override
  2386. {
  2387. inputStream1 = connectSingleStream(ctx, input1, sourceIdx1, junction1, isInputOrdered(consumerOrdered, 1));
  2388. //Assumes input1 will be stranded if any.
  2389. return CRoxieServerActivity::getOutputStreams(ctx, idx, streams, consumerOptions, consumerOrdered, orderedCallbacks);
  2390. }
  2391. };
  2392. //=================================================================================
  2393. class CRoxieServerMultiInputBaseActivity : public CRoxieServerActivity
  2394. {
  2395. protected:
  2396. unsigned numInputs;
  2397. unsigned numStreams;
  2398. IFinalRoxieInput **inputArray;
  2399. unsigned *sourceIdxArray;
  2400. IEngineRowStream **streamArray;
  2401. Owned<IStrandJunction> *junctionArray;
  2402. public:
  2403. CRoxieServerMultiInputBaseActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  2404. : CRoxieServerActivity(_ctx, _factory, _probeManager), numInputs(_numInputs)
  2405. {
  2406. numStreams = numInputs;
  2407. inputArray = new IFinalRoxieInput*[numInputs];
  2408. sourceIdxArray = new unsigned[numInputs];
  2409. streamArray = new IEngineRowStream*[numStreams];
  2410. junctionArray = new Owned<IStrandJunction>[numStreams];
  2411. for (unsigned i1 = 0; i1 < numInputs; i1++)
  2412. {
  2413. inputArray[i1] = NULL;
  2414. sourceIdxArray[i1] = 0;
  2415. }
  2416. for (unsigned i2 = 0; i2 < numStreams; i2++)
  2417. streamArray[i2] = NULL;
  2418. }
  2419. ~CRoxieServerMultiInputBaseActivity()
  2420. {
  2421. delete [] inputArray;
  2422. delete [] sourceIdxArray;
  2423. delete [] streamArray;
  2424. delete [] junctionArray;
  2425. }
  2426. virtual unsigned __int64 queryLocalCycles() const
  2427. {
  2428. __int64 localCycles = activityStats.totalCycles;
  2429. for (unsigned i = 0; i < numInputs; i++)
  2430. localCycles -= inputArray[i]->queryTotalCycles();
  2431. if (localCycles < 0)
  2432. localCycles = 0;
  2433. return localCycles;
  2434. }
  2435. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  2436. {
  2437. if (idx < numInputs)
  2438. return inputArray[idx];
  2439. else
  2440. return NULL;
  2441. }
  2442. virtual IFinalRoxieInput * queryConcreteInput(unsigned idx)
  2443. {
  2444. return queryInput(idx);
  2445. }
  2446. virtual IEngineRowStream *queryConcreteOutputStream(unsigned whichInput)
  2447. {
  2448. if (whichInput < numInputs)
  2449. return streamArray[whichInput];
  2450. else
  2451. return NULL;
  2452. }
  2453. virtual IStrandJunction *queryConcreteOutputJunction(unsigned idx) const
  2454. {
  2455. if (idx < numInputs)
  2456. return junctionArray[idx];
  2457. else
  2458. return NULL;
  2459. }
  2460. virtual void stop() override
  2461. {
  2462. for (unsigned idx = 0; idx < numStreams; idx++)
  2463. {
  2464. streamArray[idx]->stop();
  2465. }
  2466. CRoxieServerActivity::stop();
  2467. }
  2468. virtual void reset()
  2469. {
  2470. for (unsigned i = 0; i < numInputs; i++)
  2471. inputArray[i]->reset();
  2472. for (unsigned iS = 0; iS < numStreams; iS++)
  2473. {
  2474. resetJunction(junctionArray[iS]);
  2475. }
  2476. CRoxieServerActivity::reset();
  2477. }
  2478. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  2479. {
  2480. assertex(idx < numInputs && idx < numStreams);
  2481. inputArray[idx] = _in;
  2482. sourceIdxArray[idx] = _sourceIdx;
  2483. }
  2484. virtual void connectInputStreams(bool consumerOrdered)
  2485. {
  2486. //There could be situations (e.g., NONEMPTY), where you might want to strand the activity.
  2487. connectSingleInputStreams(consumerOrdered);
  2488. CRoxieServerActivity::connectInputStreams(consumerOrdered);
  2489. }
  2490. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks) override
  2491. {
  2492. return CRoxieServerActivity::getOutputStreams(ctx, idx, streams, NULL, consumerOrdered, nullptr); // The input basesclass does not have an input
  2493. }
  2494. protected:
  2495. void connectSingleInputStreams(bool consumerOrdered)
  2496. {
  2497. for (unsigned i = 0; i < numInputs; i++)
  2498. streamArray[i] = connectSingleStream(ctx, inputArray[i], sourceIdxArray[i], junctionArray[i], consumerOrdered);
  2499. }
  2500. };
  2501. //=================================================================================
  2502. class CRoxieServerMultiInputActivity : public CRoxieServerMultiInputBaseActivity
  2503. {
  2504. public:
  2505. CRoxieServerMultiInputActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  2506. : CRoxieServerMultiInputBaseActivity(_ctx, _factory, _probeManager, _numInputs)
  2507. {
  2508. }
  2509. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  2510. {
  2511. CRoxieServerMultiInputBaseActivity::start(parentExtractSize, parentExtract, paused);
  2512. for (unsigned i = 0; i < numInputs; i++)
  2513. {
  2514. inputArray[i]->start(parentExtractSize, parentExtract, paused);
  2515. }
  2516. for (unsigned iS = 0; iS < numStreams; iS++)
  2517. {
  2518. startJunction(junctionArray[iS]);
  2519. }
  2520. }
  2521. virtual void stop()
  2522. {
  2523. for (unsigned i = 0; i < numStreams; i++)
  2524. {
  2525. streamArray[i]->stop();
  2526. }
  2527. CRoxieServerMultiInputBaseActivity::stop();
  2528. }
  2529. };
  2530. //=====================================================================================================
  2531. class CRoxieServerInternalSinkActivity : public CRoxieServerActivity
  2532. {
  2533. protected:
  2534. unsigned numOutputs;
  2535. bool executed;
  2536. bool *stopped;
  2537. CriticalSection ecrit;
  2538. Owned<IException> exception;
  2539. public:
  2540. CRoxieServerInternalSinkActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numOutputs)
  2541. : CRoxieServerActivity(_ctx, _factory, _probeManager), numOutputs(_numOutputs)
  2542. {
  2543. executed = false;
  2544. stopped = new bool[numOutputs];
  2545. for (unsigned s = 0; s < numOutputs; s++)
  2546. stopped[s] = false;
  2547. }
  2548. ~CRoxieServerInternalSinkActivity()
  2549. {
  2550. delete [] stopped;
  2551. }
  2552. virtual void reset()
  2553. {
  2554. for (unsigned s = 0; s < numOutputs; s++)
  2555. stopped[s] = false;
  2556. executed = false;
  2557. exception.clear();
  2558. CRoxieServerActivity::reset();
  2559. }
  2560. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  2561. {
  2562. return NULL;
  2563. }
  2564. virtual void stopSink(unsigned outputIdx)
  2565. {
  2566. if (outputIdx < numOutputs && !stopped[outputIdx]) // Implicit dependencies on DiskWrite activities do not count as outputs
  2567. {
  2568. stopped[outputIdx] = true;
  2569. for (unsigned s = 0; s < numOutputs; s++)
  2570. if (!stopped[s])
  2571. return;
  2572. stop(); // all outputs stopped - stop parent.
  2573. }
  2574. }
  2575. virtual const void *nextRow()
  2576. {
  2577. throwUnexpected(); // I am nobody's input
  2578. }
  2579. virtual void onExecute() = 0;
  2580. virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
  2581. {
  2582. CriticalBlock b(ecrit);
  2583. if (exception)
  2584. throw exception.getLink();
  2585. if (!executed)
  2586. {
  2587. try
  2588. {
  2589. start(parentExtractSize, parentExtract, false);
  2590. {
  2591. ActivityTimer t(activityStats, timeActivities); // unfortunately this is not really best place for seeing in debugger.
  2592. onExecute();
  2593. }
  2594. stop();
  2595. executed = true;
  2596. }
  2597. catch (IException *E)
  2598. {
  2599. exception.set(E); // (or maybe makeWrappedException?)
  2600. abort();
  2601. throw;
  2602. }
  2603. catch (...)
  2604. {
  2605. exception.set(MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught at %s:%d", sanitizeSourceFile(__FILE__), __LINE__));
  2606. abort();
  2607. throw;
  2608. }
  2609. }
  2610. }
  2611. };
  2612. //=================================================================================
  2613. class CRoxieServerQueryPacket : implements IRoxieServerQueryPacket, public CInterface
  2614. {
  2615. protected:
  2616. Owned<IMessageResult> result;
  2617. Owned<IRoxieQueryPacket> packet;
  2618. Linked<IRoxieServerQueryPacket> continuation;
  2619. unsigned hash;
  2620. unsigned seq;
  2621. unsigned lastDebugSequence;
  2622. Owned<IRoxieQueryPacket> lastDebugResponse;
  2623. ILRUChain *prev;
  2624. ILRUChain *next;
  2625. bool delayed;
  2626. public:
  2627. IMPLEMENT_IINTERFACE;
  2628. CRoxieServerQueryPacket(IRoxieQueryPacket *p) : packet(p)
  2629. {
  2630. hash = 0;
  2631. seq = 0;
  2632. prev = NULL;
  2633. next = NULL;
  2634. delayed = false;
  2635. lastDebugSequence = 0;
  2636. }
  2637. virtual IRoxieQueryPacket *queryPacket() const
  2638. {
  2639. return packet;
  2640. }
  2641. virtual bool isContinuation() const
  2642. {
  2643. return packet && (packet->queryHeader().continueSequence & ~CONTINUE_SEQUENCE_SKIPTO) != 0;
  2644. }
  2645. virtual bool isDelayed() const
  2646. {
  2647. return delayed;
  2648. }
  2649. virtual bool isEnd() const
  2650. {
  2651. return false;
  2652. }
  2653. virtual bool isLimit(unsigned __int64 &_rowLimit, unsigned __int64 &_keyedLimit, unsigned __int64 &_stopAfter) const
  2654. {
  2655. return false;
  2656. }
  2657. virtual bool hasResult() const
  2658. {
  2659. return result != NULL;
  2660. }
  2661. virtual void setDelayed(bool _delayed)
  2662. {
  2663. delayed = _delayed;
  2664. }
  2665. virtual void setPacket(IRoxieQueryPacket *_packet)
  2666. {
  2667. packet.setown(_packet);
  2668. }
  2669. virtual void setSequence(unsigned _seq)
  2670. {
  2671. assertex(!IsShared());
  2672. seq = _seq;
  2673. }
  2674. virtual unsigned getSequence() const
  2675. {
  2676. return seq;
  2677. }
  2678. IMessageResult *getResult()
  2679. {
  2680. return result.getLink();
  2681. }
  2682. IMessageResult *queryResult()
  2683. {
  2684. return result;
  2685. }
  2686. void setResult(IMessageResult *r)
  2687. {
  2688. result.setown(r);
  2689. }
  2690. IRoxieServerQueryPacket *queryContinuation()
  2691. {
  2692. return continuation;
  2693. }
  2694. void setContinuation(IRoxieServerQueryPacket *c)
  2695. {
  2696. continuation.setown(c);
  2697. }
  2698. virtual unsigned queryHash() const
  2699. {
  2700. return hash;
  2701. }
  2702. virtual void setHash(unsigned _hash)
  2703. {
  2704. hash = _hash;
  2705. }
  2706. virtual ILRUChain *queryPrev() const { return prev; }
  2707. virtual ILRUChain *queryNext() const { return next; }
  2708. virtual void setPrev(ILRUChain *p) { prev = p; }
  2709. virtual void setNext(ILRUChain *n) { next = n; }
  2710. virtual void unchain()
  2711. {
  2712. if (prev && next)
  2713. {
  2714. prev->setNext(next);
  2715. next->setPrev(prev);
  2716. }
  2717. next = NULL;
  2718. prev = NULL;
  2719. }
  2720. virtual IRoxieQueryPacket *getDebugResponse(unsigned sequence)
  2721. {
  2722. if (sequence == lastDebugSequence)
  2723. return lastDebugResponse.getLink();
  2724. else if (sequence > lastDebugSequence)
  2725. {
  2726. lastDebugResponse.clear();
  2727. return NULL;
  2728. }
  2729. else
  2730. throwUnexpected();
  2731. }
  2732. virtual void setDebugResponse(unsigned sequence, IRoxieQueryPacket *response)
  2733. {
  2734. lastDebugSequence = sequence;
  2735. lastDebugResponse.set(response);
  2736. }
  2737. };
  2738. class CRoxieServerQueryPacketEndMarker : public CRoxieServerQueryPacket
  2739. {
  2740. public:
  2741. CRoxieServerQueryPacketEndMarker() : CRoxieServerQueryPacket(NULL)
  2742. {
  2743. }
  2744. virtual bool isEnd() const
  2745. {
  2746. return true;
  2747. }
  2748. };
  2749. class CRoxieServerQueryPacketLimitMarker : public CRoxieServerQueryPacket
  2750. {
  2751. unsigned __int64 rowLimit;
  2752. unsigned __int64 keyedLimit;
  2753. unsigned __int64 stopAfter;
  2754. public:
  2755. CRoxieServerQueryPacketLimitMarker(unsigned __int64 _rowLimit, unsigned __int64 _keyedLimit, unsigned __int64 _stopAfter) : CRoxieServerQueryPacket(NULL)
  2756. {
  2757. rowLimit = _rowLimit;
  2758. keyedLimit = _keyedLimit;
  2759. stopAfter = _stopAfter;
  2760. }
  2761. virtual bool isLimit(unsigned __int64 &_rowLimit, unsigned __int64 &_keyedLimit, unsigned __int64 &_stopAfter) const
  2762. {
  2763. _rowLimit = rowLimit;
  2764. _keyedLimit = keyedLimit;
  2765. _stopAfter = stopAfter;
  2766. return true;
  2767. }
  2768. };
  2769. class CRowArrayMessageUnpackCursor : implements IMessageUnpackCursor, public CInterface
  2770. {
  2771. ConstPointerArray &data;
  2772. Linked<IMessageResult> result;
  2773. public:
  2774. IMPLEMENT_IINTERFACE;
  2775. CRowArrayMessageUnpackCursor(ConstPointerArray &_data, IMessageResult *_result)
  2776. : data(_data), result(_result)
  2777. {
  2778. }
  2779. virtual bool atEOF() const
  2780. {
  2781. return data.length()==0;
  2782. }
  2783. virtual bool isSerialized() const
  2784. {
  2785. return false;
  2786. }
  2787. virtual const void * getNext(int length)
  2788. {
  2789. if (!data.length())
  2790. return NULL;
  2791. const void *ret = data.item(0);
  2792. data.remove(0);
  2793. return ret;
  2794. }
  2795. };
  2796. // MORE - should possibly move more over to the lazy version used in indexread?
  2797. class CRowArrayMessageResult : implements IMessageResult, public CInterface
  2798. {
  2799. ConstPointerArray data;
  2800. public:
  2801. IMPLEMENT_IINTERFACE;
  2802. CRowArrayMessageResult()
  2803. {
  2804. }
  2805. ~CRowArrayMessageResult()
  2806. {
  2807. ReleaseRoxieRows(data);
  2808. }
  2809. virtual IMessageUnpackCursor *getCursor(IRowManager *rowMgr) const
  2810. {
  2811. CRowArrayMessageResult *_this = (CRowArrayMessageResult *) this;
  2812. return new CRowArrayMessageUnpackCursor(_this->data, _this);
  2813. }
  2814. virtual const void *getMessageHeader(unsigned &length) const
  2815. {
  2816. throwUnexpected(); // should never get called - I don't have a header available
  2817. }
  2818. virtual const void *getMessageMetadata(unsigned &length) const
  2819. {
  2820. length = 0;
  2821. return NULL;
  2822. }
  2823. virtual void discard() const
  2824. {
  2825. throwUnexpected();
  2826. }
  2827. void append(const void *row)
  2828. {
  2829. data.append(row);
  2830. }
  2831. };
  2832. void throwRemoteException(IMessageUnpackCursor *extra)
  2833. {
  2834. RecordLengthType *rowlen = (RecordLengthType *) extra->getNext(sizeof(RecordLengthType));
  2835. if (rowlen)
  2836. {
  2837. char *xml = (char *) extra->getNext(*rowlen);
  2838. ReleaseRoxieRow(rowlen);
  2839. Owned<IPropertyTree> p = createPTreeFromXMLString(xml, ipt_fast);
  2840. ReleaseRoxieRow(xml);
  2841. unsigned code = p->getPropInt("Code", 0);
  2842. const char *msg = p->queryProp("Message");
  2843. if (!msg)
  2844. msg = xml;
  2845. throw MakeStringException(code, "%s", msg);
  2846. }
  2847. throwUnexpected();
  2848. }
  2849. class CRemoteResultAdaptor : implements IEngineRowStream, implements IFinalRoxieInput, implements IExceptionHandler, public CInterface
  2850. {
  2851. friend class CRemoteResultMerger;
  2852. class CRemoteResultMerger
  2853. {
  2854. class HeapEntry : public CInterface
  2855. {
  2856. private:
  2857. CRemoteResultAdaptor &adaptor;
  2858. IMessageUnpackCursor *cursor;
  2859. public:
  2860. const void *current;
  2861. bool isLast;
  2862. bool lastIsComplete;
  2863. IRoxieServerQueryPacket *packet;
  2864. unsigned seq;
  2865. public:
  2866. inline const void *noteResult(IMessageUnpackCursor *_cursor, bool _lastIsComplete)
  2867. {
  2868. cursor = _cursor;
  2869. lastIsComplete = _lastIsComplete;
  2870. return next();
  2871. }
  2872. public:
  2873. HeapEntry(CRemoteResultAdaptor &_adaptor, IRoxieServerQueryPacket *_packet, unsigned _seq) : adaptor(_adaptor), packet(_packet), seq(_seq)
  2874. {
  2875. cursor = NULL;
  2876. current = NULL;
  2877. isLast = false;
  2878. lastIsComplete = true;
  2879. }
  2880. ~HeapEntry()
  2881. {
  2882. ::Release(packet);
  2883. ::Release(cursor);
  2884. ReleaseRoxieRow(current);
  2885. }
  2886. bool isCompleteMatch() const
  2887. {
  2888. if (!isLast || lastIsComplete)
  2889. return true;
  2890. else
  2891. return false;
  2892. }
  2893. const void *next()
  2894. {
  2895. if (cursor)
  2896. {
  2897. ReleaseClearRoxieRow(current);
  2898. current = adaptor.getRow(cursor);
  2899. isLast = cursor->atEOF();
  2900. if (!current)
  2901. {
  2902. cursor->Release();
  2903. cursor = NULL;
  2904. }
  2905. }
  2906. return current;
  2907. }
  2908. unsigned skipTo(IRangeCompare *compare, const void *seek, unsigned numFields, bool requireExactMatch)
  2909. {
  2910. // MORE - This loop should possibly be a binchop... though it's not absolutely clear that is true (depends on term frequencies)
  2911. unsigned skipped = 0;
  2912. for (;;)
  2913. {
  2914. int c = compare->docompare(current, seek, numFields);
  2915. //If larger than the seek values, then we may be allowed to return an inexact match,
  2916. //if equal then it is required to be an exact match,
  2917. if (c > 0)
  2918. {
  2919. if (!requireExactMatch || isCompleteMatch())
  2920. break;
  2921. }
  2922. else if ((c == 0) && isCompleteMatch())
  2923. break;
  2924. skipped++;
  2925. if (!next())
  2926. break;
  2927. }
  2928. return skipped;
  2929. }
  2930. };
  2931. CRemoteResultAdaptor &adaptor;
  2932. CIArrayOf<HeapEntry> heapEntries;
  2933. UnsignedArray heap;
  2934. IRowManager *rowManager;
  2935. unsigned numPending;
  2936. unsigned numFields;
  2937. bool endSeen;
  2938. bool remakePending;
  2939. IRangeCompare *compare;
  2940. bool deferredContinuation;
  2941. inline int doCompare(unsigned l, unsigned r)
  2942. {
  2943. int ret = compare->docompare(heapEntries.item(l).current, heapEntries.item(r).current, numFields);
  2944. if (!ret) ret = heapEntries.item(l).seq - heapEntries.item(r).seq;
  2945. return ret;
  2946. }
  2947. void makeHeap()
  2948. {
  2949. /* Permute blocks to establish the heap property
  2950. For each element p, the children are p*2+1 and p*2+2 (provided these are in range)
  2951. The children of p must both be greater than or equal to p
  2952. The parent of a child c is given by p = (c-1)/2
  2953. */
  2954. unsigned i;
  2955. unsigned n = heap.length();
  2956. unsigned *s = heap.getArray();
  2957. for (i=1; i<n; i++)
  2958. {
  2959. unsigned r = s[i];
  2960. int c = i; /* child */
  2961. while (c > 0)
  2962. {
  2963. int p = (c-1)/2; /* parent */
  2964. if ( doCompare( s[c], s[p] ) >= 0 )
  2965. break;
  2966. s[c] = s[p];
  2967. s[p] = r;
  2968. c = p;
  2969. }
  2970. }
  2971. remakePending = false;
  2972. }
  2973. void remakeHeap()
  2974. {
  2975. /* The row associated with block[0] will have changed
  2976. This code restores the heap property
  2977. */
  2978. unsigned p = 0; /* parent */
  2979. unsigned n = heap.length();
  2980. unsigned *s = heap.getArray();
  2981. while (1)
  2982. {
  2983. unsigned c = p*2 + 1; /* child */
  2984. if ( c >= n )
  2985. break;
  2986. /* Select smaller child */
  2987. if ( c+1 < n && doCompare( s[c+1], s[c] ) < 0 ) c += 1;
  2988. /* If child is greater or equal than parent then we are done */
  2989. if ( doCompare( s[c], s[p] ) >= 0 )
  2990. break;
  2991. /* Swap parent and child */
  2992. unsigned r = s[c];
  2993. s[c] = s[p];
  2994. s[p] = r;
  2995. /* child becomes parent */
  2996. p = c;
  2997. }
  2998. remakePending = false;
  2999. }
  3000. void append(IRoxieServerQueryPacket *p, unsigned seq)
  3001. {
  3002. HeapEntry &h = *new HeapEntry(adaptor, LINK(p), seq);
  3003. IMessageResult *result = p->queryResult();
  3004. assertex(result);
  3005. if (h.noteResult(result->getCursor(rowManager), isCompleteMatchFlag(result)))
  3006. {
  3007. heapEntries.append(h);
  3008. heap.append(heap.ordinality());
  3009. }
  3010. else
  3011. h.Release();
  3012. }
  3013. void removeHeap(unsigned idx)
  3014. {
  3015. heapEntries.remove(idx);
  3016. ForEachItemIn(i, heap)
  3017. {
  3018. unsigned v = heap.item(i);
  3019. assertex(v != idx);
  3020. if (v > idx)
  3021. heap.replace(v-1, i);
  3022. }
  3023. }
  3024. bool isCompleteMatchFlag(IMessageResult *result)
  3025. {
  3026. unsigned metaLen;
  3027. const byte *metaInfo = (const byte *) result->getMessageMetadata(metaLen);
  3028. if (metaLen)
  3029. {
  3030. unsigned short continuationLen = *(unsigned short *) metaInfo;
  3031. if (continuationLen >= sizeof(bool))
  3032. {
  3033. metaInfo += sizeof(unsigned short);
  3034. return *(bool *) metaInfo;
  3035. }
  3036. }
  3037. return true; // if no continuation info, last row was complete.
  3038. }
  3039. public:
  3040. CRemoteResultMerger(CRemoteResultAdaptor &_adaptor) : adaptor(_adaptor)
  3041. {
  3042. init(NULL, NULL);
  3043. }
  3044. void init(ISteppingMeta *meta, IRowManager *_rowManager)
  3045. {
  3046. if (meta)
  3047. {
  3048. numFields = meta->getNumFields();
  3049. compare = meta->queryCompare();
  3050. }
  3051. else
  3052. {
  3053. numFields = 0;
  3054. compare = NULL;
  3055. }
  3056. rowManager = _rowManager;
  3057. numPending = 0;
  3058. endSeen = false;
  3059. remakePending = false;
  3060. deferredContinuation = false;
  3061. }
  3062. void reset()
  3063. {
  3064. heapEntries.kill();
  3065. heap.kill();
  3066. numPending = 0;
  3067. endSeen = false;
  3068. remakePending = false;
  3069. deferredContinuation = false;
  3070. }
  3071. inline bool noteEndSeen()
  3072. {
  3073. bool hadSeen = endSeen;
  3074. if (!endSeen)
  3075. makeHeap();
  3076. endSeen = true;
  3077. return !hadSeen;
  3078. }
  3079. void noteResult(IRoxieServerQueryPacket *p, unsigned seq)
  3080. {
  3081. if (!p->isContinuation())
  3082. append(p, seq);
  3083. else
  3084. {
  3085. ForEachItemIn(idx, heapEntries)
  3086. {
  3087. HeapEntry &h = heapEntries.item(idx);
  3088. if (h.packet == p)
  3089. {
  3090. IMessageResult *result = p->queryResult();
  3091. if (!h.noteResult(result->getCursor(rowManager), isCompleteMatchFlag(result)))
  3092. {
  3093. heap.zap(idx);
  3094. removeHeap(idx);
  3095. }
  3096. numPending--;
  3097. if (!numPending)
  3098. makeHeap();
  3099. return;
  3100. }
  3101. }
  3102. }
  3103. // If we get here it must be a continuation for one that I have not yet consumed... we don't need to do anything.
  3104. return;
  3105. }
  3106. unsigned skipRows(unsigned &idx, const void *seek, const void *rawSeek, unsigned numFields, unsigned seekLen, const SmartStepExtra & stepExtra)
  3107. {
  3108. HeapEntry &entry = heapEntries.item(idx);
  3109. unsigned skipped = entry.current ? entry.skipTo(compare, seek, numFields, !stepExtra.returnMismatches()) : 0;
  3110. if (!entry.current)
  3111. {
  3112. IRoxieServerQueryPacket *continuation = entry.packet->queryContinuation();
  3113. if (continuation)
  3114. {
  3115. continuation->Link();
  3116. entry.packet->Release();
  3117. entry.packet = continuation;
  3118. if (continuation->hasResult())
  3119. {
  3120. IMessageResult *result = continuation->queryResult();
  3121. bool lastIsCompleteMatch = isCompleteMatchFlag(result);
  3122. entry.noteResult(result->getCursor(rowManager), lastIsCompleteMatch);
  3123. }
  3124. else
  3125. {
  3126. if (continuation->isDelayed())
  3127. {
  3128. continuation->setDelayed(false);
  3129. MemoryBuffer serializedSkip;
  3130. adaptor.activity.serializeSkipInfo(serializedSkip, seekLen, rawSeek, numFields, seek, stepExtra);
  3131. continuation->setPacket(continuation->queryPacket()->insertSkipData(serializedSkip.length(), serializedSkip.toByteArray()));
  3132. ROQ->sendPacket(continuation->queryPacket(), adaptor.activity.queryLogCtx());
  3133. adaptor.sentsome.signal();
  3134. }
  3135. numPending++;
  3136. }
  3137. }
  3138. else
  3139. {
  3140. heap.zap(idx);
  3141. removeHeap(idx);
  3142. idx--;
  3143. }
  3144. }
  3145. return skipped;
  3146. }
  3147. const void * nextRowGE(const void * seek, const void *rawSeek, unsigned numFields, unsigned seeklen, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  3148. {
  3149. // We discard all rows < seekval from all entries in heap
  3150. // If this results in additional agent requests, we return NULL so that we can wait for them
  3151. // If not, we rebuild the heap (if any were skipped) and return the first row
  3152. deferredContinuation = false;
  3153. if (heap.length())
  3154. {
  3155. unsigned skipped = 0;
  3156. unsigned idx = 0;
  3157. while(heapEntries.isItem(idx))
  3158. {
  3159. skipped += skipRows(idx, seek, rawSeek, numFields, seeklen, stepExtra);
  3160. idx++;
  3161. }
  3162. if (numPending)
  3163. return NULL; // can't answer yet, need more results from agents
  3164. else
  3165. {
  3166. if (skipped)
  3167. makeHeap();
  3168. return next(wasCompleteMatch, stepExtra);
  3169. }
  3170. }
  3171. else
  3172. return NULL;
  3173. }
  3174. bool doContinuation(HeapEntry &topEntry, bool canDefer)
  3175. {
  3176. IRoxieServerQueryPacket *continuation = topEntry.packet->queryContinuation();
  3177. if (continuation)
  3178. {
  3179. if (continuation->isDelayed() && canDefer)
  3180. {
  3181. if (adaptor.activity.queryLogCtx().queryTraceLevel() > 10)
  3182. adaptor.activity.queryLogCtx().CTXLOG("Deferring continuation");
  3183. deferredContinuation = true;
  3184. }
  3185. else
  3186. {
  3187. deferredContinuation = false;
  3188. continuation->Link();
  3189. topEntry.packet->Release();
  3190. topEntry.packet = continuation;
  3191. if (continuation->hasResult())
  3192. {
  3193. IMessageResult *result = continuation->queryResult();
  3194. bool lastIsCompleteMatch = isCompleteMatchFlag(result);
  3195. topEntry.noteResult(result->getCursor(rowManager), lastIsCompleteMatch);
  3196. }
  3197. else
  3198. {
  3199. if (continuation->isDelayed()) // has the continuation been requested yet?
  3200. {
  3201. continuation->Link();
  3202. topEntry.packet->Release();
  3203. topEntry.packet = continuation;
  3204. continuation->setDelayed(false);
  3205. if (adaptor.activity.queryLogCtx().queryTraceLevel() > 10)
  3206. adaptor.activity.queryLogCtx().CTXLOG("About to send continuation, from doContinuation");
  3207. ROQ->sendPacket(continuation->queryPacket(), adaptor.activity.queryLogCtx());
  3208. adaptor.sentsome.signal();
  3209. }
  3210. numPending++; // we are waiting for one that is already in flight
  3211. }
  3212. }
  3213. return true; // next not known yet
  3214. }
  3215. else
  3216. return false;
  3217. }
  3218. const void *next(bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
  3219. {
  3220. OwnedConstRoxieRow ret;
  3221. if (heap.length())
  3222. {
  3223. if (deferredContinuation)
  3224. {
  3225. unsigned top = heap.item(0);
  3226. HeapEntry &topEntry = heapEntries.item(top);
  3227. doContinuation(topEntry, false);
  3228. return NULL;
  3229. }
  3230. if (remakePending)
  3231. remakeHeap();
  3232. unsigned top = heap.item(0);
  3233. HeapEntry &topEntry = heapEntries.item(top);
  3234. ret.set(topEntry.current);
  3235. wasCompleteMatch = topEntry.isCompleteMatch();
  3236. const void *next = topEntry.next();
  3237. if (!next)
  3238. {
  3239. if (!doContinuation(topEntry, stepExtra.returnMismatches()))
  3240. {
  3241. unsigned last = heap.popGet();
  3242. if (heap.length())
  3243. heap.replace(last, 0);
  3244. removeHeap(top);
  3245. }
  3246. }
  3247. remakePending = true;
  3248. }
  3249. return ret.getClear();
  3250. }
  3251. bool ready()
  3252. {
  3253. return endSeen && numPending == 0;
  3254. }
  3255. };
  3256. IRoxieServerQueryPacket *createRoxieServerQueryPacket(IRoxieQueryPacket *p)
  3257. {
  3258. return new CRoxieServerQueryPacket(p);
  3259. }
  3260. #ifdef _DEBUG
  3261. void dumpPending()
  3262. {
  3263. CriticalBlock b(pendingCrit);
  3264. ForEachItemIn(idx, pending)
  3265. {
  3266. IRoxieServerQueryPacket &p = pending.item(idx);
  3267. StringBuffer s;
  3268. unsigned __int64 dummy;
  3269. if (p.isEnd())
  3270. s.append("END");
  3271. else if (p.isLimit(dummy, dummy, dummy))
  3272. s.append("LIMIT");
  3273. else
  3274. {
  3275. IRoxieQueryPacket *i = p.queryPacket();
  3276. s.appendf("%s", p.hasResult() ? "COMPLETE " : "PENDING ");
  3277. if (i)
  3278. {
  3279. RoxiePacketHeader &header = i->queryHeader();
  3280. header.toString(s);
  3281. }
  3282. }
  3283. DBGLOG("Pending %d %s", idx, s.str());
  3284. }
  3285. }
  3286. #endif
  3287. void abortPending()
  3288. {
  3289. CriticalBlock b(pendingCrit);
  3290. ForEachItemIn(idx, pending)
  3291. {
  3292. IRoxieServerQueryPacket &p = pending.item(idx);
  3293. if (!p.hasResult())
  3294. {
  3295. IRoxieQueryPacket *i = p.queryPacket();
  3296. if (i)
  3297. {
  3298. RoxiePacketHeader &header = i->queryHeader();
  3299. ROQ->sendAbort(header, activity.queryLogCtx());
  3300. }
  3301. }
  3302. }
  3303. pending.kill();
  3304. }
  3305. void checkDelayed()
  3306. {
  3307. if (ctx->queryDebugContext() && ctx->queryDebugContext()->getExecuteSequentially())
  3308. {
  3309. bool allDelayed = true;
  3310. CriticalBlock b(pendingCrit);
  3311. unsigned sendIdx = (unsigned) -1;
  3312. ForEachItemIn(idx, pending)
  3313. {
  3314. IRoxieServerQueryPacket &p = pending.item(idx);
  3315. if (p.queryPacket())
  3316. {
  3317. if (p.isDelayed())
  3318. {
  3319. if (sendIdx == (unsigned) -1)
  3320. sendIdx = idx;
  3321. }
  3322. else if (!p.hasResult())
  3323. {
  3324. allDelayed = false;
  3325. break;
  3326. }
  3327. }
  3328. }
  3329. if (allDelayed && sendIdx != (unsigned) -1)
  3330. {
  3331. if (activity.queryLogCtx().queryTraceLevel() > 10)
  3332. activity.queryLogCtx().CTXLOG("About to send debug-deferred from next");
  3333. pending.item(sendIdx).setDelayed(false);
  3334. ROQ->sendPacket(pending.item(sendIdx).queryPacket(), activity.queryLogCtx());
  3335. sentsome.signal();
  3336. }
  3337. }
  3338. else if (deferredStart)
  3339. {
  3340. CriticalBlock b(pendingCrit);
  3341. ForEachItemIn(idx, pending)
  3342. {
  3343. IRoxieServerQueryPacket &p = pending.item(idx);
  3344. if (p.isDelayed())
  3345. {
  3346. if (activity.queryLogCtx().queryTraceLevel() > 10)
  3347. activity.queryLogCtx().CTXLOG("About to send deferred start from next");
  3348. p.setDelayed(false);
  3349. ROQ->sendPacket(p.queryPacket(), activity.queryLogCtx());
  3350. sentsome.signal();
  3351. }
  3352. }
  3353. deferredStart = false;
  3354. }
  3355. }
  3356. void retryPending()
  3357. {
  3358. CriticalBlock b(pendingCrit);
  3359. checkDelayed();
  3360. ForEachItemIn(idx, pending)
  3361. {
  3362. IRoxieServerQueryPacket &p = pending.item(idx);
  3363. if (!p.hasResult() && !p.isDelayed())
  3364. {
  3365. IRoxieQueryPacket *i = p.queryPacket();
  3366. if (i)
  3367. {
  3368. if (!i->queryHeader().retry())
  3369. {
  3370. StringBuffer s;
  3371. IException *E = MakeStringException(ROXIE_MULTICAST_ERROR, "Failed to get response from agent(s) for %s in activity %d", i->queryHeader().toString(s).str(), activity.queryId());
  3372. activity.queryLogCtx().logOperatorException(E, __FILE__, __LINE__, "CRemoteResultAdaptor::retry");
  3373. throw E;
  3374. }
  3375. if (!localAgent)
  3376. {
  3377. ROQ->sendPacket(i, activity.queryLogCtx());
  3378. retriesSent++;
  3379. }
  3380. }
  3381. }
  3382. }
  3383. }
  3384. class ChannelBuffer
  3385. {
  3386. protected:
  3387. unsigned bufferLeft;
  3388. MemoryBuffer buffer;
  3389. char *nextBuf;
  3390. unsigned overflowSequence;
  3391. unsigned channel; // == bonded channel
  3392. bool needsFlush;
  3393. InterruptableSemaphore flowController;
  3394. const CRemoteResultAdaptor &owner;
  3395. CriticalSection crit;
  3396. public:
  3397. ChannelBuffer(const CRemoteResultAdaptor &_owner, unsigned _channel) : channel(_channel), flowController(perChannelFlowLimit), owner(_owner)
  3398. {
  3399. overflowSequence = 0;
  3400. needsFlush = false;
  3401. bufferLeft = 0;
  3402. nextBuf = NULL;
  3403. }
  3404. void init(unsigned minSize)
  3405. {
  3406. assertex(!buffer.length());
  3407. if (minSize < MIN_PAYLOAD_SIZE)
  3408. minSize = MIN_PAYLOAD_SIZE;
  3409. unsigned headerSize = sizeof(RoxiePacketHeader)+owner.headerLength();
  3410. unsigned bufferSize = headerSize+minSize;
  3411. if (bufferSize < mtu_size)
  3412. bufferSize = mtu_size;
  3413. buffer.reserveTruncate(bufferSize);
  3414. bufferLeft = bufferSize - headerSize;
  3415. assertex(buffer.toByteArray());
  3416. nextBuf = (char *) buffer.toByteArray() + headerSize;
  3417. needsFlush = false;
  3418. }
  3419. inline IRoxieQueryPacket *flush()
  3420. {
  3421. CriticalBlock cb(crit);
  3422. Owned<IRoxieQueryPacket> ret;
  3423. if (needsFlush)
  3424. {
  3425. buffer.setLength(nextBuf - buffer.toByteArray());
  3426. RoxiePacketHeader *h = (RoxiePacketHeader *) buffer.toByteArray();
  3427. h->init(owner.remoteId, owner.ruid, channel, overflowSequence);
  3428. //patch logPrefix, cachedContext and parent extract into the place reserved in the message buffer
  3429. byte * tgt = (byte*)(h+1);
  3430. owner.copyHeader(tgt, channel);
  3431. ret.setown(createRoxiePacket(buffer));
  3432. if (overflowSequence == OVERFLOWSEQUENCE_MAX)
  3433. overflowSequence = 1; // don't wrap to 0 - that is a bit special
  3434. else
  3435. overflowSequence++;
  3436. needsFlush = false;
  3437. bufferLeft = 0;
  3438. if (owner.flowControlled)
  3439. {
  3440. CriticalUnblock cub(crit);
  3441. while (!flowController.wait(1000))
  3442. {
  3443. StringBuffer s;
  3444. owner.activity.queryLogCtx().CTXLOG("Channel %d blocked by flow control: %s", channel, h->toString(s).str());
  3445. }
  3446. }
  3447. }
  3448. return ret.getClear();
  3449. }
  3450. inline void signal()
  3451. {
  3452. if (owner.flowControlled)
  3453. flowController.signal();
  3454. }
  3455. inline void interrupt(IException *e)
  3456. {
  3457. flowController.interrupt(e);
  3458. }
  3459. inline void *getBuffer(unsigned size)
  3460. {
  3461. CriticalBlock cb(crit);
  3462. if (bufferLeft >= size)
  3463. {
  3464. needsFlush = true;
  3465. void * ret = nextBuf;
  3466. nextBuf += size;
  3467. bufferLeft -= size;
  3468. return ret;
  3469. }
  3470. else if (!needsFlush)
  3471. {
  3472. init(size);
  3473. return getBuffer(size);
  3474. }
  3475. else if (owner.mergeOrder)
  3476. {
  3477. return buffer.reserve(size); // whole query needs to go as single packet if we are to merge
  3478. }
  3479. else
  3480. return NULL; // will force it to flush and start a new packet
  3481. }
  3482. };
  3483. private:
  3484. friend class CRemoteResultMerger;
  3485. bool allread;
  3486. bool contextCached;
  3487. bool preserveOrder;
  3488. bool eogSent = false;
  3489. InterruptableSemaphore sentsome;
  3490. Owned <IMessageCollator> mc;
  3491. Owned<IMessageUnpackCursor> mu;
  3492. Owned<IMessageResult> mr;
  3493. ChannelBuffer **buffers;
  3494. IHThorArg &helper;
  3495. unsigned __int64 stopAfter;
  3496. unsigned resendSequence;
  3497. IHThorArg *colocalArg;
  3498. IArrayOf<IRoxieServerQueryPacket> pending;
  3499. CriticalSection pendingCrit;
  3500. unsigned sentSequence;
  3501. Owned<IOutputRowDeserializer> deserializer;
  3502. Owned<IEngineRowAllocator> rowAllocator;
  3503. CRemoteResultMerger merger;
  3504. // this is only used to avoid recreating a bufferStream for each row. A better solution may be needed
  3505. MemoryBuffer tempRowBuffer;
  3506. Owned<ISerialStream> bufferStream;
  3507. CThorStreamDeserializerSource rowSource;
  3508. protected:
  3509. IRowManager *rowManager;
  3510. unsigned __int64 rowLimit;
  3511. unsigned __int64 keyedLimit;
  3512. IRoxieServerErrorHandler *errorHandler;
  3513. CachedOutputMetaData meta;
  3514. public:
  3515. ISteppingMeta *mergeOrder;
  3516. IRoxieAgentContext *ctx;
  3517. IDebuggableContext *debugContext;
  3518. IRoxieServerActivity &activity;
  3519. unsigned parentExtractSize;
  3520. const byte * parentExtract;
  3521. bool flowControlled;
  3522. bool deferredStart;
  3523. MemoryBuffer logInfo;
  3524. MemoryBuffer cachedContext;
  3525. const RemoteActivityId &remoteId;
  3526. ruid_t ruid;
  3527. mutable CriticalSection buffersCrit;
  3528. unsigned processed;
  3529. cycle_t totalCycles;
  3530. bool timeActivities;
  3531. //private: //vc6 doesn't like this being private yet accessed by nested class...
  3532. const void *getRow(IMessageUnpackCursor *mu)
  3533. {
  3534. if (!mu->isSerialized() || (meta.isFixedSize() && !deserializer))
  3535. return mu->getNext(meta.getFixedSize());
  3536. else
  3537. {
  3538. RecordLengthType *rowlen = (RecordLengthType *) mu->getNext(sizeof(RecordLengthType));
  3539. if (rowlen)
  3540. {
  3541. RecordLengthType len = *rowlen;
  3542. ReleaseRoxieRow(rowlen);
  3543. const void *agentRec = mu->getNext(len);
  3544. if (deserializer && mu->isSerialized())
  3545. {
  3546. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  3547. tempRowBuffer.setBuffer(len, const_cast<void *>(agentRec), false);
  3548. size_t outsize = deserializer->deserialize(rowBuilder, rowSource);
  3549. ReleaseRoxieRow(agentRec);
  3550. return rowBuilder.finalizeRowClear(outsize);
  3551. }
  3552. else
  3553. return agentRec;
  3554. }
  3555. else
  3556. return NULL;
  3557. }
  3558. }
  3559. private:
  3560. ChannelBuffer *queryChannelBuffer(unsigned channel, bool force=false)
  3561. {
  3562. CriticalBlock cb(buffersCrit);
  3563. ChannelBuffer *b = buffers[channel];
  3564. if (!b && force)
  3565. {
  3566. if (!contextCached)
  3567. {
  3568. logInfo.clear();
  3569. unsigned char loggingFlags = LOGGING_FLAGSPRESENT | LOGGING_TRACELEVELSET;
  3570. unsigned char ctxTraceLevel = activity.queryLogCtx().queryTraceLevel() + 1; // Avoid passing a 0
  3571. const QueryOptions &options = ctx->queryOptions();
  3572. if (activity.queryLogCtx().isIntercepted())
  3573. loggingFlags |= LOGGING_INTERCEPTED;
  3574. if (options.timeActivities)
  3575. loggingFlags |= LOGGING_TIMEACTIVITIES;
  3576. if (activity.queryLogCtx().isBlind())
  3577. loggingFlags |= LOGGING_BLIND;
  3578. if (options.checkingHeap)
  3579. loggingFlags |= LOGGING_CHECKINGHEAP;
  3580. if (ctx->queryWorkUnit())
  3581. loggingFlags |= LOGGING_WUID;
  3582. if (debugContext)
  3583. {
  3584. loggingFlags |= LOGGING_DEBUGGERACTIVE;
  3585. logInfo.append(loggingFlags).append(ctxTraceLevel);
  3586. MemoryBuffer bpInfo;
  3587. debugContext->serialize(bpInfo);
  3588. bpInfo.append((__uint64)(memsize_t) &activity);
  3589. logInfo.append((unsigned short) bpInfo.length());
  3590. logInfo.append(bpInfo.length(), bpInfo.toByteArray());
  3591. }
  3592. else
  3593. logInfo.append(loggingFlags).append(ctxTraceLevel);
  3594. StringBuffer logPrefix;
  3595. activity.queryLogCtx().getLogPrefix(logPrefix);
  3596. logInfo.append(logPrefix);
  3597. activity.serializeCreateStartContext(cachedContext.clear());
  3598. activity.serializeExtra(cachedContext);
  3599. if (activity.queryVarFileInfo())
  3600. {
  3601. activity.queryVarFileInfo()->queryTimeStamp().serialize(cachedContext);
  3602. cachedContext.append(activity.queryVarFileInfo()->queryCheckSum());
  3603. }
  3604. contextCached = true;
  3605. }
  3606. b = buffers[channel] = new ChannelBuffer(*this, channel);
  3607. }
  3608. return b;
  3609. }
  3610. void processRow(const void *got)
  3611. {
  3612. processed++;
  3613. if (processed > rowLimit)
  3614. {
  3615. ReleaseRoxieRow(got);
  3616. errorHandler->onLimitExceeded(false); // NOTE - should throw exception
  3617. throwUnexpected();
  3618. }
  3619. else if (processed > keyedLimit)
  3620. {
  3621. ReleaseRoxieRow(got);
  3622. errorHandler->onLimitExceeded(true); // NOTE - should throw exception
  3623. throwUnexpected();
  3624. }
  3625. }
  3626. public:
  3627. IMPLEMENT_IINTERFACE;
  3628. CRemoteResultAdaptor(IRoxieAgentContext *_ctx, IRoxieServerErrorHandler *_errorHandler, const RemoteActivityId &_remoteId, IOutputMetaData *_meta, IHThorArg &_helper, IRoxieServerActivity &_activity, bool _preserveOrder, bool _flowControlled)
  3629. : preserveOrder(_preserveOrder), helper(_helper), merger(*this), meta(_meta), activity(_activity), flowControlled(_flowControlled), remoteId(_remoteId)
  3630. {
  3631. ctx = _ctx;
  3632. errorHandler = _errorHandler;
  3633. colocalArg = NULL;
  3634. allread = false;
  3635. ruid = 0;
  3636. rowLimit = (unsigned __int64) -1;
  3637. keyedLimit = (unsigned __int64) -1;
  3638. contextCached = false;
  3639. stopAfter = I64C(0x7FFFFFFFFFFFFFFF);
  3640. buffers = new ChannelBuffer*[numChannels+1];
  3641. memset(buffers, 0, (numChannels+1)*sizeof(ChannelBuffer *));
  3642. parentExtractSize = 0;
  3643. rowManager = NULL;
  3644. parentExtract = NULL;
  3645. debugContext = NULL;
  3646. mergeOrder = NULL;
  3647. deferredStart = false;
  3648. processed = 0;
  3649. sentSequence = 0;
  3650. resendSequence = 0;
  3651. totalCycles = 0;
  3652. bufferStream.setown(createMemoryBufferSerialStream(tempRowBuffer));
  3653. rowSource.setStream(bufferStream);
  3654. timeActivities = defaultTimeActivities;
  3655. }
  3656. ~CRemoteResultAdaptor()
  3657. {
  3658. if (mc)
  3659. {
  3660. ROQ->queryReceiveManager()->detachCollator(mc);
  3661. mc.clear();
  3662. }
  3663. for (unsigned channel = 0; channel <= numChannels; channel++)
  3664. {
  3665. delete(buffers[channel]);
  3666. }
  3667. delete [] buffers;
  3668. }
  3669. void setMeta(IOutputMetaData *newmeta)
  3670. {
  3671. meta.set(newmeta);
  3672. }
  3673. virtual IEngineRowStream *queryConcreteOutputStream(unsigned whichInput) { assertex(whichInput==0); return this; }
  3674. virtual IStrandJunction *queryConcreteOutputJunction(unsigned idx) const { assertex(idx==0); return nullptr; }
  3675. virtual IRoxieServerActivity *queryActivity()
  3676. {
  3677. return &activity;
  3678. }
  3679. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  3680. {
  3681. return NULL;
  3682. }
  3683. void setMergeInfo(ISteppingMeta *_mergeOrder)
  3684. {
  3685. mergeOrder = _mergeOrder;
  3686. deferredStart = true;
  3687. }
  3688. void send(IRoxieQueryPacket *p)
  3689. {
  3690. if (p)
  3691. {
  3692. Linked<IRoxieQueryPacket> saver(p); // avoids a race with abortPending, without keeping pendingCrit locked over the send which we might prefer not to
  3693. assertex(p->queryHeader().uid==ruid);
  3694. // MORE: Maybe we should base the fastlane flag on some other
  3695. // criteria !! (i.e A Roxie server prediction based on the
  3696. // activity type/activity behaviour/expected reply size .. etc).
  3697. //
  3698. // Currently (code below) based on high priority, seq=0, and none-child activity.
  3699. // But this could still cause too many reply packets on the fatlane
  3700. // (higher priority output Q), which may cause the activities on the
  3701. // low priority output Q to not get service on time.
  3702. if ((colocalArg == 0) && // not a child query activity??
  3703. (p->queryHeader().activityId & (ROXIE_SLA_PRIORITY | ROXIE_HIGH_PRIORITY)) &&
  3704. (p->queryHeader().overflowSequence == 0) &&
  3705. (p->queryHeader().continueSequence & ~CONTINUE_SEQUENCE_SKIPTO)==0)
  3706. p->queryHeader().retries |= ROXIE_FASTLANE;
  3707. if (p->queryHeader().channel)
  3708. {
  3709. IRoxieServerQueryPacket *rsqp = createRoxieServerQueryPacket(p);
  3710. if (deferredStart)
  3711. rsqp->setDelayed(true);
  3712. rsqp->setSequence(sentSequence++);
  3713. {
  3714. CriticalBlock b(pendingCrit);
  3715. pending.append(*rsqp);
  3716. }
  3717. if (!deferredStart)
  3718. {
  3719. ROQ->sendPacket(p, activity.queryLogCtx());
  3720. sentsome.signal();
  3721. }
  3722. }
  3723. else
  3724. {
  3725. // 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
  3726. // Need to add first, then send
  3727. unsigned i;
  3728. for (i = 1; i <= numChannels; i++)
  3729. {
  3730. IRoxieQueryPacket *q = p->clonePacket(i);
  3731. IRoxieServerQueryPacket *rsqp = createRoxieServerQueryPacket(q);
  3732. rsqp->setSequence(sentSequence++);
  3733. if (deferredStart)
  3734. {
  3735. rsqp->setDelayed(true);
  3736. }
  3737. {
  3738. CriticalBlock b(pendingCrit);
  3739. pending.append(*rsqp);
  3740. }
  3741. if (!deferredStart)
  3742. sentsome.signal();
  3743. }
  3744. if (!deferredStart)
  3745. ROQ->sendPacket(p, activity.queryLogCtx());
  3746. buffers[0]->signal(); // since replies won't come back on that channel...
  3747. p->Release();
  3748. }
  3749. }
  3750. }
  3751. void *getMem(unsigned partNo, unsigned fileNo, unsigned size)
  3752. {
  3753. unsigned channel = partNo ? getBondedChannel(partNo) : 0;
  3754. size += sizeof(PartNoType);
  3755. ChannelBuffer *b = queryChannelBuffer(channel, true);
  3756. char *buffer = (char *) b->getBuffer(size);
  3757. if (!buffer)
  3758. {
  3759. send(b->flush());
  3760. buffer = (char *) b->getBuffer(size);
  3761. }
  3762. PartNoType sp;
  3763. sp.partNo = partNo;
  3764. sp.fileNo = fileNo;
  3765. memcpy(buffer, &sp, sizeof(sp));
  3766. buffer += sizeof(sp);
  3767. return buffer;
  3768. }
  3769. void injectResult(IMessageResult *result)
  3770. {
  3771. IRoxieServerQueryPacket *f = new CRoxieServerQueryPacket(NULL);
  3772. f->setSequence(sentSequence++);
  3773. f->setResult(result);
  3774. CriticalBlock b(pendingCrit);
  3775. pending.append(*f);
  3776. sentsome.signal(); // MORE - arguably should only send if there is any point waking up the listener thread, to save context swicth
  3777. }
  3778. void flush()
  3779. {
  3780. for (unsigned channel = 0; channel <= numChannels; channel++)
  3781. {
  3782. ChannelBuffer *b = queryChannelBuffer(channel, false);
  3783. if (b)
  3784. send(b->flush());
  3785. }
  3786. }
  3787. void interruptBuffers(IException *e)
  3788. {
  3789. for (unsigned channel = 0; channel <= numChannels; channel++)
  3790. {
  3791. ChannelBuffer *b = queryChannelBuffer(channel, false);
  3792. if (b)
  3793. b->interrupt(LINK(e));
  3794. }
  3795. }
  3796. void senddone()
  3797. {
  3798. CriticalBlock b(pendingCrit);
  3799. pending.append(*new CRoxieServerQueryPacketEndMarker);
  3800. sentsome.signal();
  3801. }
  3802. bool fireException(IException *e)
  3803. {
  3804. {
  3805. CriticalBlock b(pendingCrit);
  3806. pending.append(*new CRoxieServerQueryPacketEndMarker);
  3807. }
  3808. interruptBuffers(e);
  3809. if (mc)
  3810. mc->interrupt(LINK(e));
  3811. sentsome.interrupt(e);
  3812. return true;
  3813. }
  3814. virtual void onCreate(IHThorArg *_colocalArg)
  3815. {
  3816. debugContext = ctx->queryDebugContext();
  3817. colocalArg = _colocalArg;
  3818. if (meta.needsSerializeDisk())
  3819. {
  3820. deserializer.setown(meta.createDiskDeserializer(ctx->queryCodeContext(), activity.queryId()));
  3821. rowAllocator.setown(activity.createRowAllocator(meta.queryOriginal()));
  3822. }
  3823. if (ctx->queryDebugContext() && ctx->queryDebugContext()->getExecuteSequentially())
  3824. deferredStart = true;
  3825. timeActivities = ctx->queryOptions().timeActivities;
  3826. }
  3827. virtual void onStart(unsigned _parentExtractSize, const byte * _parentExtract)
  3828. {
  3829. #ifdef TRACE_STARTSTOP
  3830. if (traceStartStop)
  3831. activity.queryLogCtx().CTXLOG("RRAonstart");
  3832. #endif
  3833. sentsome.reinit();
  3834. ruid = getNextRuid();
  3835. rowManager = &ctx->queryRowManager();
  3836. if (mergeOrder)
  3837. merger.init(mergeOrder, rowManager);
  3838. if (mc)
  3839. {
  3840. ROQ->queryReceiveManager()->detachCollator(mc); // Should never happen - implies someone forgot to call onReset!
  3841. }
  3842. mc.setown(ROQ->queryReceiveManager()->createMessageCollator(rowManager, ruid));
  3843. allread = false;
  3844. mu.clear();
  3845. contextCached = false;
  3846. processed = 0;
  3847. resendSequence = 0;
  3848. sentSequence = 0;
  3849. for (unsigned channel = 0; channel <= numChannels; channel++)
  3850. {
  3851. delete(buffers[channel]);
  3852. buffers[channel] = NULL;
  3853. }
  3854. flush();
  3855. parentExtractSize = _parentExtractSize;
  3856. parentExtract = _parentExtract;
  3857. }
  3858. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  3859. {
  3860. #ifdef TRACE_STARTSTOP
  3861. if (traceStartStop)
  3862. activity.queryLogCtx().CTXLOG("RRAstart");
  3863. #endif
  3864. activity.start(parentExtractSize, parentExtract, paused);
  3865. }
  3866. void setLimits(unsigned __int64 _rowLimit, unsigned __int64 _keyedLimit, unsigned __int64 _stopAfter)
  3867. {
  3868. if (ctx->queryProbeManager())
  3869. {
  3870. if (_rowLimit != (unsigned __int64) -1) ctx->queryProbeManager()->setNodePropertyInt(&activity, "rowLimit", _rowLimit);
  3871. if (_keyedLimit != (unsigned __int64) -1) ctx->queryProbeManager()->setNodePropertyInt(&activity, "keyedLimit", _keyedLimit);
  3872. if (_stopAfter != I64C(0x7FFFFFFFFFFFFFFF)) ctx->queryProbeManager()->setNodePropertyInt(&activity, "choosenLimit", _stopAfter);
  3873. }
  3874. {
  3875. CriticalBlock b(pendingCrit);
  3876. if (pending.length())
  3877. {
  3878. #ifdef _DEBUG
  3879. dumpPending(); // MORE - only defined in debug build - could have put the ifdef inside the dumpPending method
  3880. #endif
  3881. assertex(pending.length()==0);
  3882. }
  3883. pending.append(*new CRoxieServerQueryPacketLimitMarker(_rowLimit, _keyedLimit, _stopAfter));
  3884. }
  3885. sentsome.signal();
  3886. rowLimit = _rowLimit;
  3887. keyedLimit = _keyedLimit;
  3888. stopAfter = _stopAfter;
  3889. }
  3890. virtual void stop()
  3891. {
  3892. #ifdef TRACE_STARTSTOP
  3893. if (traceStartStop)
  3894. activity.queryLogCtx().CTXLOG("RRAstop");
  3895. #endif
  3896. onStop();
  3897. activity.stop();
  3898. }
  3899. void onStop()
  3900. {
  3901. #ifdef TRACE_STARTSTOP
  3902. if (traceStartStop)
  3903. activity.queryLogCtx().CTXLOG("RRAonstop");
  3904. #endif
  3905. abortPending();
  3906. interruptBuffers(NULL);
  3907. sentsome.interrupt();
  3908. if (mc) // May not be set if start() chain threw exception
  3909. mc->interrupt();
  3910. }
  3911. virtual void reset()
  3912. {
  3913. #ifdef TRACE_STARTSTOP
  3914. if (traceStartStop)
  3915. activity.queryLogCtx().CTXLOG("RRAreset");
  3916. #endif
  3917. activity.reset();
  3918. onReset();
  3919. }
  3920. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks)
  3921. {
  3922. // Make sure parent activity calls up the tree
  3923. PointerArrayOf<IEngineRowStream> instreams;
  3924. IStrandJunction *junction = activity.getOutputStreams(ctx, idx, instreams, consumerOptions, consumerOrdered, orderedCallbacks);
  3925. assertex (junction == NULL);
  3926. // I return a single strand (we could change that sometime I guess...)
  3927. streams.append(this);
  3928. return NULL;
  3929. }
  3930. virtual void resetEOF()
  3931. {
  3932. throwUnexpected();
  3933. }
  3934. virtual void onReset()
  3935. {
  3936. #ifdef TRACE_STARTSTOP
  3937. if (traceStartStop)
  3938. activity.queryLogCtx().CTXLOG("RRAonreset");
  3939. #endif
  3940. if (mc)
  3941. ROQ->queryReceiveManager()->detachCollator(mc);
  3942. merger.reset();
  3943. pending.kill();
  3944. if (mc && ctx)
  3945. ctx->addAgentsReplyLen(mc->queryBytesReceived());
  3946. mc.clear(); // Or we won't free memory for graphs that get recreated
  3947. mu.clear(); //ditto
  3948. deferredStart = false;
  3949. // NOTE: do NOT clear mergeOrder - this is set at create time not per child query
  3950. }
  3951. virtual IOutputMetaData * queryOutputMeta() const
  3952. {
  3953. return helper.queryOutputMeta();
  3954. }
  3955. virtual unsigned __int64 queryTotalCycles() const
  3956. {
  3957. return totalCycles;
  3958. }
  3959. void gatherStats(CRuntimeStatisticCollection & merged) const
  3960. {
  3961. if (rowAllocator)
  3962. rowAllocator->gatherStats(merged);
  3963. }
  3964. const void * nextRowGE(const void *seek, const void *rawSeek, unsigned numFields, unsigned seekLen, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  3965. {
  3966. if (activity.queryLogCtx().queryTraceLevel() > 20)
  3967. {
  3968. StringBuffer recstr;
  3969. unsigned i;
  3970. for (i = 0; i < seekLen; i++)
  3971. {
  3972. recstr.appendf("%02x ", ((unsigned char *) rawSeek)[i]);
  3973. }
  3974. activity.queryLogCtx().CTXLOG("CRemoteResultAdaptor::nextRowGE(rawSeek=%s numFields=%d, seeklen=%d, returnMismatches=%d)", recstr.str(), numFields, seekLen, stepExtra.returnMismatches());
  3975. }
  3976. assertex(mergeOrder);
  3977. if (deferredStart)
  3978. {
  3979. CriticalBlock b(pendingCrit);
  3980. ForEachItemIn(idx, pending)
  3981. {
  3982. IRoxieServerQueryPacket &p = pending.item(idx);
  3983. if (p.isDelayed())
  3984. {
  3985. p.setDelayed(false);
  3986. if (activity.queryLogCtx().queryTraceLevel() > 10)
  3987. activity.queryLogCtx().CTXLOG("About to send deferred start from nextRowGE, setting requireExact to %d", !stepExtra.returnMismatches());
  3988. MemoryBuffer serializedSkip;
  3989. activity.serializeSkipInfo(serializedSkip, seekLen, rawSeek, numFields, seek, stepExtra);
  3990. p.setPacket(p.queryPacket()->insertSkipData(serializedSkip.length(), serializedSkip.toByteArray()));
  3991. ROQ->sendPacket(p.queryPacket(), activity.queryLogCtx());
  3992. sentsome.signal();
  3993. }
  3994. }
  3995. deferredStart = false;
  3996. }
  3997. SimpleActivityTimer t(totalCycles, timeActivities);
  3998. if (processed==stopAfter)
  3999. return NULL;
  4000. if (allread)
  4001. return NULL;
  4002. for (;;)
  4003. {
  4004. if (merger.ready())
  4005. {
  4006. const void *got = merger.nextRowGE(seek, rawSeek, numFields, seekLen, wasCompleteMatch, stepExtra);
  4007. if (got)
  4008. {
  4009. processRow(got);
  4010. return got;
  4011. }
  4012. }
  4013. if (!reload()) // MORE - should pass the seek info here...
  4014. return NULL;
  4015. }
  4016. }
  4017. virtual const void *nextRow()
  4018. {
  4019. // If we are merging then we need to do a heapsort on all
  4020. SimpleActivityTimer t(totalCycles, timeActivities);
  4021. if (activity.queryLogCtx().queryTraceLevel() > 10)
  4022. {
  4023. activity.queryLogCtx().CTXLOG("CRemoteResultAdaptor::nextRow()");
  4024. }
  4025. for (;;)
  4026. {
  4027. checkDelayed();
  4028. if (processed==stopAfter)
  4029. return NULL;
  4030. if (allread)
  4031. return NULL;
  4032. // If we can still consume from the merger or the most recently retrieved mu, do so.
  4033. const void *got = NULL;
  4034. if (mergeOrder && merger.ready())
  4035. {
  4036. bool matched = true;
  4037. got = merger.next(matched, dummySmartStepExtra);
  4038. }
  4039. else if (mu)
  4040. {
  4041. got = getRow(mu);
  4042. if (!got && meta.isGrouped() && !eogSent)
  4043. {
  4044. eogSent = true;
  4045. return NULL;
  4046. }
  4047. }
  4048. if (got)
  4049. {
  4050. processRow(got);
  4051. return got;
  4052. }
  4053. eogSent = false;
  4054. if (!reload())
  4055. return NULL;
  4056. }
  4057. }
  4058. bool reload()
  4059. {
  4060. // Wait for something to be returned from a agent....
  4061. mu.clear();
  4062. sentsome.wait();
  4063. // must be at least an endMarker on the queue since sentsome was signalled
  4064. {
  4065. CriticalBlock b(pendingCrit);
  4066. IRoxieServerQueryPacket &top = pending.item(0);
  4067. if (top.isLimit(rowLimit, keyedLimit, stopAfter)) // This is really a start marker...
  4068. {
  4069. pending.remove(0);
  4070. return true;
  4071. }
  4072. else if (top.isEnd())
  4073. {
  4074. pending.remove(0);
  4075. allread = true;
  4076. if (activity.queryLogCtx().queryTraceLevel() > 5)
  4077. activity.queryLogCtx().CTXLOG("All read on ruid %x", ruid);
  4078. return false;
  4079. }
  4080. else if (mergeOrder)
  4081. {
  4082. unsigned idx = 0;
  4083. bool added = false;
  4084. while (pending.isItem(idx))
  4085. {
  4086. IRoxieServerQueryPacket &item = pending.item(idx);
  4087. if (item.isEnd())
  4088. {
  4089. if (merger.noteEndSeen())
  4090. {
  4091. sentsome.signal(); // Because we waited, yet didn't actually consume anything
  4092. added = true;
  4093. }
  4094. break;
  4095. }
  4096. else if (item.hasResult())
  4097. {
  4098. merger.noteResult(&item, item.getSequence());
  4099. pending.remove(idx);
  4100. added = true;
  4101. }
  4102. else if (item.isContinuation())
  4103. idx++;
  4104. else
  4105. break;
  4106. }
  4107. if (added)
  4108. return true;
  4109. }
  4110. else if (top.hasResult())
  4111. {
  4112. mr.setown(pending.item(0).getResult());
  4113. mu.setown(mr->getCursor(rowManager));
  4114. pending.remove(0);
  4115. return true;
  4116. }
  4117. }
  4118. getNextUnpacker();
  4119. return true;
  4120. }
  4121. void getNextUnpacker()
  4122. {
  4123. mu.clear();
  4124. unsigned ctxTraceLevel = activity.queryLogCtx().queryTraceLevel();
  4125. for (;;)
  4126. {
  4127. checkDelayed();
  4128. unsigned timeout = remoteId.isSLAPriority() ? slaTimeout : (remoteId.isHighPriority() ? highTimeout : lowTimeout);
  4129. activity.queryContext()->checkAbort();
  4130. bool anyActivity;
  4131. if (ctxTraceLevel > 5)
  4132. activity.queryLogCtx().CTXLOG("Calling getNextUnpacker(%d)", timeout);
  4133. mr.setown(mc->getNextResult(timeout, anyActivity));
  4134. if (ctxTraceLevel > 6)
  4135. activity.queryLogCtx().CTXLOG("Called getNextUnpacker(%d), activity=%d", timeout, anyActivity);
  4136. activity.queryContext()->checkAbort();
  4137. if (mr)
  4138. {
  4139. unsigned roxieHeaderLen;
  4140. const RoxiePacketHeader &header = *(const RoxiePacketHeader *) mr->getMessageHeader(roxieHeaderLen);
  4141. #ifdef _DEBUG
  4142. assertex(roxieHeaderLen == sizeof(RoxiePacketHeader));
  4143. #endif
  4144. if (ctxTraceLevel > 5)
  4145. {
  4146. StringBuffer s;
  4147. activity.queryLogCtx().CTXLOG("getNextUnpacker got packet %s", header.toString(s).str());
  4148. }
  4149. CriticalBlock b(pendingCrit);
  4150. unsigned idx = 0;
  4151. IRoxieServerQueryPacket *original = NULL;
  4152. IRoxieQueryPacket *op;
  4153. while (pending.isItem(idx))
  4154. {
  4155. original = &pending.item(idx);
  4156. op = original->queryPacket();
  4157. if (op && header.matchPacket(op->queryHeader()))
  4158. break;
  4159. original = NULL;
  4160. idx++;
  4161. }
  4162. if (!original || original->hasResult())
  4163. {
  4164. switch (header.activityId)
  4165. {
  4166. case ROXIE_FILECALLBACK:
  4167. {
  4168. // tell agent to abort
  4169. //if (ctxTraceLevel > 5)
  4170. {
  4171. StringBuffer s;
  4172. activity.queryLogCtx().CTXLOG("Redundant callback on query %s", header.toString(s).str());
  4173. }
  4174. Owned<IMessageUnpackCursor> callbackData = mr->getCursor(rowManager);
  4175. OwnedConstRoxieRow len = callbackData->getNext(sizeof(RecordLengthType));
  4176. if (len)
  4177. {
  4178. RecordLengthType *rowlen = (RecordLengthType *) len.get();
  4179. OwnedConstRoxieRow row = callbackData->getNext(*rowlen);
  4180. const char *rowdata = (const char *) row.get();
  4181. // bool isOpt = * (bool *) rowdata;
  4182. // bool isLocal = * (bool *) (rowdata+1);
  4183. ROQ->sendAbortCallback(header, rowdata+2, activity.queryLogCtx());
  4184. }
  4185. else
  4186. throwUnexpected();
  4187. break;
  4188. }
  4189. // MORE - ROXIE_ALIVE perhaps should go here too
  4190. default:
  4191. if (ctxTraceLevel > 3)
  4192. activity.queryLogCtx().CTXLOG("Discarding packet %p %x - original %p is NULL or has result already", mr.get(), header.activityId, original);
  4193. mr->discard();
  4194. break;
  4195. }
  4196. mr.clear();
  4197. }
  4198. else
  4199. {
  4200. resultsReceived++;
  4201. switch (header.activityId)
  4202. {
  4203. case ROXIE_DEBUGCALLBACK:
  4204. {
  4205. Owned<IMessageUnpackCursor> callbackData = mr->getCursor(rowManager);
  4206. OwnedConstRoxieRow len = callbackData->getNext(sizeof(RecordLengthType));
  4207. if (len)
  4208. {
  4209. RecordLengthType *rowlen = (RecordLengthType *) len.get();
  4210. OwnedConstRoxieRow row = callbackData->getNext(*rowlen);
  4211. char *rowdata = (char *) row.get();
  4212. if (ctxTraceLevel > 5)
  4213. {
  4214. StringBuffer s;
  4215. activity.queryLogCtx().CTXLOG("Callback on query %s for debug", header.toString(s).str());
  4216. }
  4217. MemoryBuffer agentInfo;
  4218. agentInfo.setBuffer(*rowlen, rowdata, false);
  4219. unsigned debugSequence;
  4220. agentInfo.read(debugSequence);
  4221. Owned<IRoxieQueryPacket> reply = original->getDebugResponse(debugSequence);
  4222. if (!reply)
  4223. reply.setown(activity.queryContext()->queryDebugContext()->onDebugCallback(header, *rowlen, rowdata));
  4224. if (reply)
  4225. {
  4226. original->setDebugResponse(debugSequence, reply);
  4227. ROQ->sendPacket(reply, activity.queryLogCtx());
  4228. }
  4229. }
  4230. else
  4231. throwUnexpected();
  4232. // MORE - somehow we need to make sure agent gets a reply even if I'm not waiting (in udp layer)
  4233. // Leave original message on pending queue in original location - this is not a reply to it.
  4234. break;
  4235. }
  4236. case ROXIE_FILECALLBACK:
  4237. {
  4238. // we need to send back to the agent a message containing the file info requested.
  4239. Owned<IMessageUnpackCursor> callbackData = mr->getCursor(rowManager);
  4240. OwnedConstRoxieRow len = callbackData->getNext(sizeof(RecordLengthType));
  4241. if (len)
  4242. {
  4243. RecordLengthType *rowlen = (RecordLengthType *) len.get();
  4244. OwnedConstRoxieRow row = callbackData->getNext(*rowlen);
  4245. const char *rowdata = (const char *) row.get();
  4246. bool isOpt = * (bool *) rowdata;
  4247. bool isLocal = * (bool *) (rowdata+1);
  4248. const char *lfn = rowdata+2;
  4249. if (ctxTraceLevel > 5)
  4250. {
  4251. StringBuffer s;
  4252. activity.queryLogCtx().CTXLOG("Callback on query %s file %s", header.toString(s).str(),(const char *) lfn);
  4253. }
  4254. activity.queryContext()->onFileCallback(header, lfn, isOpt, isLocal, defaultPrivilegedUser);
  4255. }
  4256. else
  4257. throwUnexpected();
  4258. // MORE - somehow we need to make sure agent gets a reply even if I'm not waiting (in udp layer)
  4259. // Leave original message on pending queue in original location - this is not a reply to it.
  4260. break;
  4261. }
  4262. case ROXIE_KEYEDLIMIT_EXCEEDED:
  4263. activity.queryLogCtx().CTXLOG("ROXIE_KEYEDLIMIT_EXCEEDED");
  4264. errorHandler->onLimitExceeded(true); // NOTE - should throw exception!
  4265. throwUnexpected();
  4266. case ROXIE_LIMIT_EXCEEDED:
  4267. activity.queryLogCtx().CTXLOG("ROXIE_LIMIT_EXCEEDED");
  4268. errorHandler->onLimitExceeded(false); // NOTE - should throw exception!
  4269. throwUnexpected();
  4270. case ROXIE_TRACEINFO:
  4271. {
  4272. Owned<IMessageUnpackCursor> extra = mr->getCursor(rowManager);
  4273. for (;;)
  4274. {
  4275. RecordLengthType *rowlen = (RecordLengthType *) extra->getNext(sizeof(RecordLengthType));
  4276. if (rowlen)
  4277. {
  4278. char *logInfo = (char *) extra->getNext(*rowlen);
  4279. MemoryBuffer buf;
  4280. buf.setBuffer(*rowlen, logInfo, false);
  4281. switch ((TracingCategory) *logInfo)
  4282. {
  4283. case LOG_TRACING:
  4284. case LOG_ERROR:
  4285. activity.queryLogCtx().CTXLOGl(new LogItem(buf));
  4286. break;
  4287. case LOG_STATVALUES:
  4288. buf.skip(1);
  4289. activity.mergeStats(buf);
  4290. break;
  4291. case LOG_CHILDCOUNT:
  4292. case LOG_CHILDSTATS:
  4293. //These need rethinking - all child stats should be serialized in a single block, and should be merged into
  4294. //the information for an activity, ready for reporting later
  4295. unsigned graphId, childId;
  4296. buf.skip(1);
  4297. buf.read(graphId);
  4298. buf.read(childId);
  4299. if (*logInfo == LOG_CHILDCOUNT)
  4300. {
  4301. unsigned idx;
  4302. unsigned childProcessed;
  4303. unsigned childStrands;
  4304. buf.read(idx);
  4305. buf.read(childProcessed);
  4306. buf.read(childStrands);
  4307. if (traceLevel > 5)
  4308. activity.queryLogCtx().CTXLOG("Processing ChildCount %d idx %d strands %d for child %d subgraph %d", childProcessed, idx, childStrands, childId, graphId);
  4309. //activity.queryContext()->noteProcessed(graphId, childId, idx, childProcessed, childStrands);
  4310. }
  4311. else
  4312. {
  4313. CRuntimeStatisticCollection childStats(allStatistics);
  4314. childStats.deserialize(buf);
  4315. if (traceLevel > 5)
  4316. {
  4317. StringBuffer s;
  4318. activity.queryLogCtx().CTXLOG("Processing ChildStats for child %d subgraph %d: %s", childId, graphId, childStats.toStr(s).str());
  4319. }
  4320. //activity.queryContext()->mergeActivityStats(childStats, graphId, childId);
  4321. activity.mergeStats(childStats);
  4322. }
  4323. }
  4324. ReleaseRoxieRow(rowlen);
  4325. ReleaseRoxieRow(logInfo);
  4326. }
  4327. else
  4328. break;
  4329. }
  4330. break;
  4331. }
  4332. case ROXIE_EXCEPTION:
  4333. if (ctxTraceLevel > 1)
  4334. {
  4335. StringBuffer s;
  4336. activity.queryLogCtx().CTXLOG("Exception on query %s", header.toString(s).str());
  4337. }
  4338. op->queryHeader().noteException(header.retries);
  4339. if (op->queryHeader().allChannelsFailed())
  4340. {
  4341. activity.queryLogCtx().CTXLOG("Multiple exceptions on query - aborting");
  4342. Owned<IMessageUnpackCursor> exceptionData = mr->getCursor(rowManager);
  4343. throwRemoteException(exceptionData);
  4344. }
  4345. // Leave it on pending queue in original location
  4346. break;
  4347. case ROXIE_ALIVE:
  4348. if (ctxTraceLevel > 4)
  4349. {
  4350. StringBuffer s;
  4351. activity.queryLogCtx().CTXLOG("ROXIE_ALIVE: %s", header.toString(s).str());
  4352. }
  4353. op->queryHeader().noteAlive(header.retries & ROXIE_RETRIES_MASK);
  4354. // Leave it on pending queue in original location
  4355. break;
  4356. default:
  4357. if (header.retries & ROXIE_RETRIES_MASK)
  4358. retriesNeeded++;
  4359. unsigned metaLen;
  4360. const void *metaData = mr->getMessageMetadata(metaLen);
  4361. if (metaLen)
  4362. {
  4363. // We got back first chunk but there is more.
  4364. // resend the packet, with the cursor info provided.
  4365. // MORE - if smart-stepping, we don't want to send the continuation immediately. Other cases it's not clear that we do.
  4366. if (ctxTraceLevel > 1)
  4367. {
  4368. StringBuffer s;
  4369. activity.queryLogCtx().CTXLOG("Additional data size %d on query %s mergeOrder %p", metaLen, header.toString(s).str(), mergeOrder);
  4370. }
  4371. if (*((unsigned short *) metaData) + sizeof(unsigned short) != metaLen)
  4372. {
  4373. StringBuffer s;
  4374. activity.queryLogCtx().CTXLOG("Additional data size %d on query %s mergeOrder %p", metaLen, header.toString(s).str(), mergeOrder);
  4375. activity.queryLogCtx().CTXLOG("Additional data is corrupt");
  4376. throwUnexpected();
  4377. }
  4378. MemoryBuffer nextQuery;
  4379. nextQuery.append(sizeof(RoxiePacketHeader), &header);
  4380. nextQuery.append(op->getTraceLength(), op->queryTraceInfo());
  4381. nextQuery.append(metaLen, metaData);
  4382. nextQuery.append(op->getContextLength(), op->queryContextData());
  4383. if (resendSequence == CONTINUESEQUENCE_MAX)
  4384. {
  4385. activity.queryLogCtx().CTXLOG("ERROR: Continuation sequence wrapped"); // shouldn't actually matter.... but suggests a very iffy query!
  4386. resendSequence = 1;
  4387. }
  4388. else
  4389. resendSequence++;
  4390. RoxiePacketHeader *newHeader = (RoxiePacketHeader *) nextQuery.toByteArray();
  4391. newHeader->continueSequence = resendSequence; // NOTE - we clear the skipTo flag since continuation of a skip is NOT a skip...
  4392. newHeader->retries &= ~ROXIE_RETRIES_MASK;
  4393. IRoxieQueryPacket *resend = createRoxiePacket(nextQuery);
  4394. CRoxieServerQueryPacket *fqp = new CRoxieServerQueryPacket(resend);
  4395. fqp->setSequence(original->getSequence());
  4396. pending.add(*fqp, idx+1); // note that pending takes ownership. sendPacket does not release.
  4397. original->setContinuation(LINK(fqp));
  4398. if (mergeOrder)
  4399. fqp->setDelayed(true);
  4400. else
  4401. {
  4402. ROQ->sendPacket(resend, activity.queryLogCtx());
  4403. sentsome.signal();
  4404. }
  4405. }
  4406. unsigned channel = header.channel;
  4407. {
  4408. ChannelBuffer *b = queryChannelBuffer(channel); // If not something is wrong, or we sent out on channel 0?
  4409. if (b)
  4410. b->signal();
  4411. }
  4412. original->setResult(mr.getClear());
  4413. sentsome.signal();
  4414. return;
  4415. }
  4416. }
  4417. }
  4418. else
  4419. {
  4420. if (!anyActivity && !localAgent)
  4421. {
  4422. activity.queryLogCtx().CTXLOG("Input has stalled - retry required?");
  4423. retryPending();
  4424. }
  4425. }
  4426. }
  4427. }
  4428. inline unsigned headerLength() const
  4429. {
  4430. return logInfo.length() + cachedContext.length() + sizeof(unsigned) + parentExtractSize;
  4431. }
  4432. void copyHeader(byte *tgt, unsigned channel) const
  4433. {
  4434. unsigned len = logInfo.length();
  4435. memcpy(tgt, logInfo.toByteArray(), len);
  4436. tgt += len;
  4437. *(unsigned *) tgt = parentExtractSize;
  4438. tgt += sizeof(unsigned);
  4439. if (parentExtractSize)
  4440. {
  4441. memcpy(tgt, parentExtract, parentExtractSize);
  4442. tgt += parentExtractSize;
  4443. }
  4444. if (cachedContext.length())
  4445. {
  4446. memcpy(tgt, cachedContext.toByteArray(), cachedContext.length());
  4447. tgt += cachedContext.length();
  4448. }
  4449. }
  4450. };
  4451. class CSkippableRemoteResultAdaptor : public CRemoteResultAdaptor
  4452. {
  4453. Owned <IException> exception;
  4454. bool skipping;
  4455. ConstPointerArray buff;
  4456. unsigned index;
  4457. bool pulled;
  4458. void pullInput()
  4459. {
  4460. try
  4461. {
  4462. if (exception)
  4463. throw exception.getClear();
  4464. unsigned __int64 count = 0;
  4465. for (;;)
  4466. {
  4467. const void * next = CRemoteResultAdaptor::nextRow();
  4468. if (next == NULL)
  4469. {
  4470. next = CRemoteResultAdaptor::nextRow();
  4471. if(next == NULL)
  4472. break;
  4473. buff.append(NULL);
  4474. }
  4475. count++;
  4476. if (count > rowLimit)
  4477. {
  4478. ReleaseRoxieRow(next);
  4479. ReleaseRoxieRows(buff);
  4480. errorHandler->onLimitExceeded(false); // throws an exception - user or LimitSkipException
  4481. throwUnexpected();
  4482. }
  4483. else if (count > keyedLimit)
  4484. {
  4485. ReleaseRoxieRow(next);
  4486. ReleaseRoxieRows(buff);
  4487. errorHandler->onLimitExceeded(true); // throws an exception - user or LimitSkipException
  4488. throwUnexpected();
  4489. }
  4490. buff.append(next);
  4491. }
  4492. }
  4493. catch (IException *E)
  4494. {
  4495. if (QUERYINTERFACE(E, LimitSkipException))
  4496. {
  4497. Owned<IException> cleanup = E;
  4498. ReleaseRoxieRows(buff);
  4499. const void *onfail = errorHandler->createLimitFailRow(E->errorCode() == KeyedLimitSkipErrorCode);
  4500. if (onfail)
  4501. buff.append(onfail);
  4502. }
  4503. else
  4504. throw;
  4505. }
  4506. pulled = true;
  4507. }
  4508. public:
  4509. CSkippableRemoteResultAdaptor(IRoxieAgentContext *_ctx, IRoxieServerErrorHandler *_errorHandler, const RemoteActivityId &_remoteId, IOutputMetaData *_meta, IHThorArg &_helper, IRoxieServerActivity &_activity, bool _preserveOrder, bool _flowControlled, bool _skipping) :
  4510. CRemoteResultAdaptor(_ctx, _errorHandler, _remoteId, _meta, _helper, _activity, _preserveOrder, _flowControlled)
  4511. {
  4512. skipping = _skipping;
  4513. index = 0;
  4514. pulled = false;
  4515. }
  4516. void setException(IException *E)
  4517. {
  4518. exception.setown(E);
  4519. }
  4520. virtual void onReset()
  4521. {
  4522. roxiemem::ReleaseRoxieRowRange(buff.getArray(), index, buff.ordinality());
  4523. buff.kill();
  4524. index = 0;
  4525. pulled = false;
  4526. exception.clear();
  4527. CRemoteResultAdaptor::onReset();
  4528. }
  4529. void onStart(unsigned _parentExtractSize, const byte * _parentExtract)
  4530. {
  4531. index = 0;
  4532. pulled = false;
  4533. CRemoteResultAdaptor::onStart(_parentExtractSize, _parentExtract);
  4534. }
  4535. virtual const void * nextRowGE(const void *seek, const void *rawSeek, unsigned numFields, unsigned seeklen, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  4536. {
  4537. // 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
  4538. if (exception)
  4539. throw exception.getClear();
  4540. return CRemoteResultAdaptor::nextRowGE(seek, rawSeek, numFields, seeklen, wasCompleteMatch, stepExtra);
  4541. }
  4542. virtual const void *nextRow()
  4543. {
  4544. if (skipping)
  4545. {
  4546. if(!pulled)
  4547. pullInput();
  4548. if(buff.isItem(index))
  4549. {
  4550. const void * next = buff.item(index++);
  4551. if(next)
  4552. processed++;
  4553. return next;
  4554. }
  4555. return NULL;
  4556. }
  4557. else
  4558. {
  4559. if (exception)
  4560. throw exception.getClear();
  4561. return CRemoteResultAdaptor::nextRow();
  4562. }
  4563. }
  4564. };
  4565. //=================================================================================
  4566. class CRoxieServerApplyActivity : public CRoxieServerInternalSinkActivity
  4567. {
  4568. IHThorApplyArg &helper;
  4569. public:
  4570. CRoxieServerApplyActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4571. : CRoxieServerInternalSinkActivity(_ctx, _factory, _probeManager, 0), helper((IHThorApplyArg &) basehelper)
  4572. {
  4573. }
  4574. virtual void onExecute()
  4575. {
  4576. helper.start();
  4577. for (;;)
  4578. {
  4579. const void * next = inputStream->ungroupedNextRow();
  4580. if (!next)
  4581. break;
  4582. helper.apply(next);
  4583. ReleaseRoxieRow(next);
  4584. }
  4585. helper.end();
  4586. }
  4587. };
  4588. class CRoxieServerApplyActivityFactory : public CRoxieServerActivityFactory
  4589. {
  4590. bool isRoot;
  4591. public:
  4592. CRoxieServerApplyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  4593. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), isRoot(_isRoot)
  4594. {
  4595. }
  4596. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  4597. {
  4598. return new CRoxieServerApplyActivity(_ctx, this, _probeManager);
  4599. }
  4600. virtual bool isSink() const
  4601. {
  4602. return isRoot;
  4603. }
  4604. };
  4605. IRoxieServerActivityFactory *createRoxieServerApplyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  4606. {
  4607. return new CRoxieServerApplyActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _isRoot);
  4608. }
  4609. //=================================================================================
  4610. class CRoxieServerNullActivity : public CRoxieServerActivity
  4611. {
  4612. public:
  4613. CRoxieServerNullActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4614. : CRoxieServerActivity(_ctx, _factory, _probeManager)
  4615. {
  4616. }
  4617. virtual const void *nextRow()
  4618. {
  4619. return NULL;
  4620. }
  4621. };
  4622. IRoxieServerActivity * createRoxieServerNullActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4623. {
  4624. return new CRoxieServerNullActivity(_ctx, _factory, _probeManager);
  4625. }
  4626. class CRoxieServerNullActivityFactory : public CRoxieServerActivityFactory
  4627. {
  4628. public:
  4629. CRoxieServerNullActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  4630. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  4631. {
  4632. }
  4633. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  4634. {
  4635. return new CRoxieServerNullActivity(_ctx, this, _probeManager);
  4636. }
  4637. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  4638. {
  4639. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for null activity");
  4640. }
  4641. };
  4642. IRoxieServerActivityFactory *createRoxieServerNullActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  4643. {
  4644. return new CRoxieServerNullActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  4645. }
  4646. //=================================================================================
  4647. class CRoxieServerNullSinkActivity : public CRoxieServerInternalSinkActivity
  4648. {
  4649. public:
  4650. CRoxieServerNullSinkActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4651. : CRoxieServerInternalSinkActivity(_ctx, _factory, _probeManager, 0)
  4652. {
  4653. }
  4654. //override execute() to ensure that start is nevr called on any input activities.
  4655. virtual void execute(unsigned parentExtractSize, const byte * parentExtract) override
  4656. {
  4657. CriticalBlock b(ecrit);
  4658. if (!executed)
  4659. {
  4660. executed = true;
  4661. stop();
  4662. }
  4663. }
  4664. virtual void onExecute() override
  4665. {
  4666. }
  4667. };
  4668. IRoxieServerActivity * createRoxieServerNullSinkActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4669. {
  4670. return new CRoxieServerNullSinkActivity(_ctx, _factory, _probeManager);
  4671. }
  4672. //=================================================================================
  4673. class CRoxieServerPassThroughActivity : public CRoxieServerActivity
  4674. {
  4675. public:
  4676. CRoxieServerPassThroughActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4677. : CRoxieServerActivity(_ctx, _factory, _probeManager)
  4678. {
  4679. }
  4680. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  4681. {
  4682. return input->gatherConjunctions(collector);
  4683. }
  4684. virtual void resetEOF()
  4685. {
  4686. inputStream->resetEOF();
  4687. }
  4688. virtual const void *nextRow()
  4689. {
  4690. const void * next = inputStream->nextRow();
  4691. if (next)
  4692. processed++;
  4693. return next;
  4694. }
  4695. virtual bool isPassThrough()
  4696. {
  4697. return true;
  4698. }
  4699. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  4700. {
  4701. ActivityTimer t(activityStats, timeActivities);
  4702. const void * next = inputStream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  4703. if (next)
  4704. processed++;
  4705. return next;
  4706. }
  4707. IInputSteppingMeta * querySteppingMeta()
  4708. {
  4709. return input->querySteppingMeta();
  4710. }
  4711. };
  4712. IRoxieServerActivity * createRoxieServerPassThroughActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4713. {
  4714. return new CRoxieServerPassThroughActivity(_ctx, _factory, _probeManager);
  4715. }
  4716. //=================================================================================
  4717. class CRoxieServerChildBaseActivity : public CRoxieServerActivity
  4718. {
  4719. protected:
  4720. bool eof;
  4721. bool first;
  4722. public:
  4723. CRoxieServerChildBaseActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4724. : CRoxieServerActivity(_ctx, _factory, _probeManager)
  4725. {
  4726. eof = false;
  4727. first = true;
  4728. }
  4729. ~CRoxieServerChildBaseActivity()
  4730. {
  4731. }
  4732. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  4733. {
  4734. eof = false;
  4735. first = true;
  4736. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  4737. }
  4738. };
  4739. class CRoxieServerChildBaseActivityFactory : public CRoxieServerActivityFactory
  4740. {
  4741. public:
  4742. CRoxieServerChildBaseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  4743. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  4744. {
  4745. }
  4746. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  4747. {
  4748. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for %s activity", getActivityText(kind));
  4749. }
  4750. };
  4751. //=================================================================================
  4752. class CRoxieServerChildIteratorActivity : public CRoxieServerChildBaseActivity
  4753. {
  4754. IHThorChildIteratorArg &helper;
  4755. public:
  4756. CRoxieServerChildIteratorActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4757. : CRoxieServerChildBaseActivity(_ctx, _factory, _probeManager), helper((IHThorChildIteratorArg &) basehelper)
  4758. {
  4759. }
  4760. virtual bool needsAllocator() const { return true; }
  4761. virtual const void *nextRow()
  4762. {
  4763. ActivityTimer t(activityStats, timeActivities);
  4764. if (eof)
  4765. return NULL;
  4766. bool ok;
  4767. if (first)
  4768. {
  4769. ok = helper.first();
  4770. first = false;
  4771. }
  4772. else
  4773. ok = helper.next();
  4774. try
  4775. {
  4776. while (ok)
  4777. {
  4778. processed++;
  4779. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  4780. unsigned outSize = helper.transform(rowBuilder);
  4781. if (outSize)
  4782. return rowBuilder.finalizeRowClear(outSize);
  4783. ok = helper.next();
  4784. }
  4785. }
  4786. catch (IException *E)
  4787. {
  4788. throw makeWrappedException(E);
  4789. }
  4790. eof = true;
  4791. return NULL;
  4792. }
  4793. };
  4794. class CRoxieServerChildIteratorActivityFactory : public CRoxieServerChildBaseActivityFactory
  4795. {
  4796. public:
  4797. CRoxieServerChildIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  4798. : CRoxieServerChildBaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  4799. {
  4800. }
  4801. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  4802. {
  4803. return new CRoxieServerChildIteratorActivity(_ctx, this, _probeManager);
  4804. }
  4805. };
  4806. IRoxieServerActivityFactory *createRoxieServerChildIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  4807. {
  4808. return new CRoxieServerChildIteratorActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  4809. }
  4810. //=================================================================================
  4811. class CRoxieServerChildNormalizeActivity : public CRoxieServerChildBaseActivity
  4812. {
  4813. IHThorChildNormalizeArg &helper;
  4814. public:
  4815. CRoxieServerChildNormalizeActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4816. : CRoxieServerChildBaseActivity(_ctx, _factory, _probeManager), helper((IHThorChildNormalizeArg &) basehelper)
  4817. {
  4818. }
  4819. virtual bool needsAllocator() const { return true; }
  4820. virtual const void *nextRow()
  4821. {
  4822. ActivityTimer t(activityStats, timeActivities);
  4823. if (eof)
  4824. return NULL;
  4825. bool ok;
  4826. if (first)
  4827. {
  4828. ok = helper.first();
  4829. first = false;
  4830. }
  4831. else
  4832. ok = helper.next();
  4833. if (ok)
  4834. {
  4835. try
  4836. {
  4837. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  4838. do {
  4839. unsigned outSize = helper.transform(rowBuilder);
  4840. if (outSize)
  4841. {
  4842. processed++;
  4843. return rowBuilder.finalizeRowClear(outSize);
  4844. }
  4845. ok = helper.next();
  4846. }
  4847. while (ok);
  4848. }
  4849. catch (IException *E)
  4850. {
  4851. throw makeWrappedException(E);
  4852. }
  4853. }
  4854. eof = true;
  4855. return NULL;
  4856. }
  4857. };
  4858. class CRoxieServerChildNormalizeActivityFactory : public CRoxieServerChildBaseActivityFactory
  4859. {
  4860. public:
  4861. CRoxieServerChildNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  4862. : CRoxieServerChildBaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  4863. {
  4864. }
  4865. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  4866. {
  4867. return new CRoxieServerChildNormalizeActivity(_ctx, this, _probeManager);
  4868. }
  4869. };
  4870. IRoxieServerActivityFactory *createRoxieServerNewChildNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  4871. {
  4872. return new CRoxieServerChildNormalizeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  4873. }
  4874. //=================================================================================
  4875. class CRoxieServerChildAggregateActivity : public CRoxieServerChildBaseActivity
  4876. {
  4877. IHThorChildAggregateArg &helper;
  4878. public:
  4879. CRoxieServerChildAggregateActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4880. : CRoxieServerChildBaseActivity(_ctx, _factory, _probeManager), helper((IHThorChildAggregateArg &) basehelper)
  4881. {
  4882. }
  4883. virtual bool needsAllocator() const { return true; }
  4884. virtual const void *nextRow()
  4885. {
  4886. ActivityTimer t(activityStats, timeActivities);
  4887. if (eof)
  4888. return NULL;
  4889. eof = true;
  4890. processed++;
  4891. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  4892. helper.clearAggregate(rowBuilder);
  4893. helper.processRows(rowBuilder);
  4894. size32_t finalSize = meta.getRecordSize(rowBuilder.getSelf());
  4895. return rowBuilder.finalizeRowClear(finalSize);
  4896. }
  4897. };
  4898. class CRoxieServerChildAggregateActivityFactory : public CRoxieServerChildBaseActivityFactory
  4899. {
  4900. public:
  4901. CRoxieServerChildAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  4902. : CRoxieServerChildBaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  4903. {
  4904. }
  4905. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  4906. {
  4907. return new CRoxieServerChildAggregateActivity(_ctx, this, _probeManager);
  4908. }
  4909. };
  4910. IRoxieServerActivityFactory *createRoxieServerNewChildAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  4911. {
  4912. return new CRoxieServerChildAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  4913. }
  4914. //=================================================================================
  4915. class CRoxieServerChildGroupAggregateActivity : public CRoxieServerChildBaseActivity, public IHThorGroupAggregateCallback
  4916. {
  4917. IHThorChildGroupAggregateArg &helper;
  4918. RowAggregator aggregated;
  4919. public:
  4920. IMPLEMENT_IINTERFACE_USING(CRoxieServerChildBaseActivity)
  4921. CRoxieServerChildGroupAggregateActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4922. : CRoxieServerChildBaseActivity(_ctx, _factory, _probeManager), helper((IHThorChildGroupAggregateArg &) basehelper),
  4923. aggregated(helper, helper)
  4924. {
  4925. }
  4926. void processRow(const void * next)
  4927. {
  4928. aggregated.addRow(next);
  4929. }
  4930. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  4931. {
  4932. CRoxieServerChildBaseActivity::start(parentExtractSize, parentExtract, paused);
  4933. aggregated.start(rowAllocator, ctx->queryCodeContext(), activityId);
  4934. }
  4935. virtual void reset()
  4936. {
  4937. aggregated.reset();
  4938. CRoxieServerChildBaseActivity::reset();
  4939. }
  4940. virtual bool needsAllocator() const { return true; }
  4941. virtual const void *nextRow()
  4942. {
  4943. ActivityTimer t(activityStats, timeActivities);
  4944. if (eof)
  4945. return NULL;
  4946. if (first)
  4947. {
  4948. helper.processRows(this);
  4949. first = false;
  4950. }
  4951. Owned<AggregateRowBuilder> next = aggregated.nextResult();
  4952. if (next)
  4953. {
  4954. processed++;
  4955. return next->finalizeRowClear();
  4956. }
  4957. eof = true;
  4958. return NULL;
  4959. }
  4960. };
  4961. class CRoxieServerChildGroupAggregateActivityFactory : public CRoxieServerChildBaseActivityFactory
  4962. {
  4963. public:
  4964. CRoxieServerChildGroupAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  4965. : CRoxieServerChildBaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  4966. {
  4967. }
  4968. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  4969. {
  4970. return new CRoxieServerChildGroupAggregateActivity(_ctx, this, _probeManager);
  4971. }
  4972. };
  4973. IRoxieServerActivityFactory *createRoxieServerNewChildGroupAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  4974. {
  4975. return new CRoxieServerChildGroupAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  4976. }
  4977. //=================================================================================
  4978. class CRoxieServerChildThroughNormalizeActivity : public CRoxieServerChildBaseActivity
  4979. {
  4980. IHThorChildThroughNormalizeArg &helper;
  4981. const void * lastInput;
  4982. unsigned numProcessedLastGroup;
  4983. bool ok;
  4984. public:
  4985. CRoxieServerChildThroughNormalizeActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  4986. : CRoxieServerChildBaseActivity(_ctx, _factory, _probeManager), helper((IHThorChildThroughNormalizeArg &) basehelper)
  4987. {
  4988. lastInput = NULL;
  4989. numProcessedLastGroup = 0;
  4990. ok = false;
  4991. }
  4992. virtual void stop()
  4993. {
  4994. CRoxieServerChildBaseActivity::stop();
  4995. ReleaseRoxieRow(lastInput);
  4996. lastInput = NULL;
  4997. }
  4998. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  4999. {
  5000. CRoxieServerChildBaseActivity::start(parentExtractSize, parentExtract, paused);
  5001. numProcessedLastGroup = processed;
  5002. ok = false;
  5003. }
  5004. virtual bool needsAllocator() const { return true; }
  5005. virtual const void *nextRow()
  5006. {
  5007. ActivityTimer t(activityStats, timeActivities);
  5008. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  5009. for (;;)
  5010. {
  5011. if (ok)
  5012. ok = helper.next();
  5013. while (!ok)
  5014. {
  5015. ReleaseRoxieRow(lastInput);
  5016. lastInput = inputStream->nextRow();
  5017. if (!lastInput)
  5018. {
  5019. if (numProcessedLastGroup != processed)
  5020. {
  5021. numProcessedLastGroup = processed;
  5022. return NULL;
  5023. }
  5024. lastInput = inputStream->nextRow();
  5025. if (!lastInput)
  5026. return NULL;
  5027. }
  5028. ok = helper.first(lastInput);
  5029. }
  5030. try
  5031. {
  5032. do
  5033. {
  5034. unsigned outSize = helper.transform(rowBuilder);
  5035. if (outSize)
  5036. {
  5037. processed++;
  5038. return rowBuilder.finalizeRowClear(outSize);
  5039. }
  5040. ok = helper.next();
  5041. } while (ok);
  5042. }
  5043. catch (IException *E)
  5044. {
  5045. throw makeWrappedException(E);
  5046. }
  5047. }
  5048. }
  5049. };
  5050. class CRoxieServerChildThroughNormalizeActivityFactory : public CRoxieServerActivityFactory
  5051. {
  5052. public:
  5053. CRoxieServerChildThroughNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  5054. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  5055. {
  5056. }
  5057. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  5058. {
  5059. return new CRoxieServerChildThroughNormalizeActivity(_ctx, this, _probeManager);
  5060. }
  5061. };
  5062. IRoxieServerActivityFactory *createRoxieServerNewChildThroughNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  5063. {
  5064. return new CRoxieServerChildThroughNormalizeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  5065. }
  5066. //=================================================================================
  5067. class CRoxieServerDistributionActivity : public CRoxieServerInternalSinkActivity
  5068. {
  5069. IHThorDistributionArg &helper;
  5070. public:
  5071. CRoxieServerDistributionActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  5072. : CRoxieServerInternalSinkActivity(_ctx, _factory, _probeManager, 0), helper((IHThorDistributionArg &)basehelper)
  5073. {
  5074. }
  5075. virtual void onExecute()
  5076. {
  5077. MemoryAttr ma;
  5078. IDistributionTable * * accumulator = (IDistributionTable * *)ma.allocate(helper.queryInternalRecordSize()->getMinRecordSize());
  5079. helper.clearAggregate(accumulator);
  5080. OwnedConstRoxieRow nextrec(inputStream->nextRow());
  5081. for (;;)
  5082. {
  5083. if (!nextrec)
  5084. {
  5085. nextrec.setown(inputStream->nextRow());
  5086. if (!nextrec)
  5087. break;
  5088. }
  5089. helper.process(accumulator, nextrec);
  5090. nextrec.setown(inputStream->nextRow());
  5091. }
  5092. StringBuffer result;
  5093. result.append("<XML>");
  5094. helper.gatherResult(accumulator, result);
  5095. result.append("</XML>");
  5096. helper.sendResult(result.length(), result.str());
  5097. helper.destruct(accumulator);
  5098. }
  5099. };
  5100. class CRoxieServerDistributionActivityFactory : public CRoxieServerActivityFactory
  5101. {
  5102. bool isRoot;
  5103. public:
  5104. CRoxieServerDistributionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  5105. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), isRoot(_isRoot)
  5106. {
  5107. }
  5108. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  5109. {
  5110. return new CRoxieServerDistributionActivity(_ctx, this, _probeManager);
  5111. }
  5112. virtual bool isSink() const
  5113. {
  5114. return isRoot;
  5115. }
  5116. };
  5117. IRoxieServerActivityFactory *createRoxieServerDistributionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  5118. {
  5119. return new CRoxieServerDistributionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _isRoot);
  5120. }
  5121. //=================================================================================
  5122. class CRoxieServerLinkedRawIteratorActivity : public CRoxieServerActivity
  5123. {
  5124. IHThorLinkedRawIteratorArg &helper;
  5125. public:
  5126. CRoxieServerLinkedRawIteratorActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  5127. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorLinkedRawIteratorArg &) basehelper)
  5128. {
  5129. }
  5130. virtual const void *nextRow()
  5131. {
  5132. ActivityTimer t(activityStats, timeActivities);
  5133. const void *ret =helper.next();
  5134. if (ret)
  5135. {
  5136. LinkRoxieRow(ret);
  5137. processed++;
  5138. }
  5139. return ret;
  5140. }
  5141. };
  5142. class CRoxieServerLinkedRawIteratorActivityFactory : public CRoxieServerActivityFactory
  5143. {
  5144. public:
  5145. CRoxieServerLinkedRawIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  5146. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  5147. {
  5148. }
  5149. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  5150. {
  5151. return new CRoxieServerLinkedRawIteratorActivity(_ctx, this, _probeManager);
  5152. }
  5153. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  5154. {
  5155. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for %s activity", getActivityText(kind));
  5156. }
  5157. };
  5158. IRoxieServerActivityFactory *createRoxieServerLinkedRawIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  5159. {
  5160. return new CRoxieServerLinkedRawIteratorActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  5161. }
  5162. //=================================================================================
  5163. class CRoxieServerDatasetResultActivity : public CRoxieServerActivity
  5164. {
  5165. public:
  5166. CRoxieServerDatasetResultActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  5167. : CRoxieServerActivity(_ctx, _factory, _probeManager)
  5168. {
  5169. }
  5170. virtual const void *nextRow()
  5171. {
  5172. throwUnexpected();
  5173. }
  5174. virtual void executeChild(size32_t & retSize, void * & ret, unsigned parentExtractSize, const byte * parentExtract)
  5175. {
  5176. try
  5177. {
  5178. start(parentExtractSize, parentExtract, false);
  5179. {
  5180. ActivityTimer t(activityStats, timeActivities);
  5181. MemoryBuffer result;
  5182. IRecordSize * inputMeta = input->queryOutputMeta();
  5183. for (;;)
  5184. {
  5185. const void *nextrec = inputStream->ungroupedNextRow();
  5186. if (!nextrec)
  5187. break;
  5188. result.append(inputMeta->getRecordSize(nextrec), nextrec);
  5189. ReleaseRoxieRow(nextrec);
  5190. }
  5191. retSize = result.length();
  5192. ret = result.detach();
  5193. }
  5194. stop();
  5195. reset();
  5196. }
  5197. catch(IException *E)
  5198. {
  5199. ctx->notifyAbort(E);
  5200. abort();
  5201. reset();
  5202. throw;
  5203. }
  5204. catch(...)
  5205. {
  5206. Owned<IException> E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught at %s:%d", sanitizeSourceFile(__FILE__), __LINE__);
  5207. ctx->notifyAbort(E);
  5208. abort();
  5209. reset();
  5210. throw;
  5211. }
  5212. }
  5213. };
  5214. class CRoxieServerDatasetResultActivityFactory : public CRoxieServerActivityFactory
  5215. {
  5216. bool isRoot;
  5217. public:
  5218. CRoxieServerDatasetResultActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  5219. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), isRoot(_isRoot)
  5220. {
  5221. }
  5222. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  5223. {
  5224. return new CRoxieServerDatasetResultActivity(_ctx, this, _probeManager);
  5225. }
  5226. virtual bool isSink() const
  5227. {
  5228. return isRoot;
  5229. }
  5230. };
  5231. IRoxieServerActivityFactory *createRoxieServerDatasetResultActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  5232. {
  5233. return new CRoxieServerDatasetResultActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _isRoot);
  5234. }
  5235. //=================================================================================
  5236. class CRoxieServerInlineTableActivity : public CRoxieServerActivity
  5237. {
  5238. IHThorInlineTableArg &helper;
  5239. __uint64 curRow;
  5240. __uint64 numRows;
  5241. public:
  5242. CRoxieServerInlineTableActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  5243. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorInlineTableArg &) basehelper)
  5244. {
  5245. curRow = 0;
  5246. numRows = 0;
  5247. }
  5248. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5249. {
  5250. curRow = 0;
  5251. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  5252. numRows = helper.numRows();
  5253. }
  5254. virtual bool needsAllocator() const { return true; }
  5255. virtual const void *nextRow()
  5256. {
  5257. ActivityTimer t(activityStats, timeActivities);
  5258. // Filtering empty rows, returns the next valid row
  5259. while (curRow < numRows)
  5260. {
  5261. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  5262. unsigned outSize = helper.getRow(rowBuilder, curRow++);
  5263. if (outSize)
  5264. {
  5265. processed++;
  5266. return rowBuilder.finalizeRowClear(outSize);
  5267. }
  5268. }
  5269. return NULL;
  5270. }
  5271. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  5272. {
  5273. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  5274. }
  5275. };
  5276. class CRoxieServerStrandedInlineTableActivity : public CRoxieServerStrandedActivity
  5277. {
  5278. IHThorInlineTableArg &helper;
  5279. __uint64 numRows;
  5280. //An inline table that isn't stranded
  5281. class InlineTableSimpleProcessor : public StrandProcessor
  5282. {
  5283. protected:
  5284. IHThorInlineTableArg &helper;
  5285. __uint64 curRow;
  5286. __uint64 maxRows;
  5287. public:
  5288. InlineTableSimpleProcessor(CRoxieServerActivity &_parent, IHThorInlineTableArg &_helper)
  5289. : StrandProcessor(_parent, NULL, true), helper(_helper), curRow(0), maxRows(0)
  5290. {
  5291. }
  5292. virtual void start() override
  5293. {
  5294. StrandProcessor::start();
  5295. curRow = 0;
  5296. maxRows = queryParent().numRows;
  5297. }
  5298. virtual const void * nextRow()
  5299. {
  5300. ActivityTimer t(activityStats, timeActivities);
  5301. //Return rows while the current section is active
  5302. while (curRow < maxRows)
  5303. {
  5304. RtlDynamicRowBuilder rowBuilder(*rowAllocator);
  5305. unsigned outSize = helper.getRow(rowBuilder, curRow++);
  5306. if (outSize)
  5307. {
  5308. processed++;
  5309. return rowBuilder.finalizeRowClear(outSize);
  5310. }
  5311. }
  5312. return NULL;
  5313. }
  5314. CRoxieServerStrandedInlineTableActivity & queryParent() { return static_cast<CRoxieServerStrandedInlineTableActivity&>(parent); }
  5315. };
  5316. class InlineTableStrandProcessor : public StrandProcessor
  5317. {
  5318. protected:
  5319. IHThorInlineTableArg &helper;
  5320. unsigned whichStrand;
  5321. unsigned numStrands = 0;
  5322. __uint64 sectionSize;
  5323. __uint64 curRow = 0;
  5324. __uint64 sectionMaxRows = 0;
  5325. __uint64 maxRows = 0;
  5326. bool isOrdered;
  5327. bool eosPending;
  5328. public:
  5329. InlineTableStrandProcessor(CRoxieServerActivity &_parent, IHThorInlineTableArg &_helper, unsigned _whichStrand, bool _isOrdered)
  5330. : StrandProcessor(_parent, NULL, true), helper(_helper), whichStrand(_whichStrand), isOrdered(_isOrdered)
  5331. {
  5332. sectionSize = queryParent().strandOptions.blockSize;
  5333. eosPending = false;
  5334. }
  5335. virtual void start() override
  5336. {
  5337. StrandProcessor::start();
  5338. numStrands = queryParent().numStrands();
  5339. maxRows = queryParent().numRows;
  5340. curRow = std::min(whichStrand * sectionSize, maxRows);
  5341. sectionMaxRows = std::min(curRow + sectionSize, maxRows);
  5342. eosPending = isOrdered && (curRow != maxRows);
  5343. }
  5344. virtual const void * nextRow()
  5345. {
  5346. ActivityTimer t(activityStats, timeActivities);
  5347. for (;;)
  5348. {
  5349. //Return rows while the current section is active
  5350. while (curRow < sectionMaxRows)
  5351. {
  5352. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  5353. unsigned outSize = helper.getRow(rowBuilder, curRow++);
  5354. if (outSize)
  5355. {
  5356. processed++;
  5357. return rowBuilder.finalizeRowClear(outSize);
  5358. }
  5359. }
  5360. if (curRow == maxRows)
  5361. return NULL;
  5362. //Update current position to the next section
  5363. curRow += (numStrands - 1) * sectionSize;
  5364. if (curRow > maxRows)
  5365. curRow = maxRows;
  5366. sectionMaxRows = std::min(curRow + sectionSize, maxRows);
  5367. //If we have output a block of rows, then send the block as a unit - results now come from the next strand
  5368. if (eosPending)
  5369. {
  5370. //If there is another block, then second an end of section marker after it, otherwise make sure NULL is returned next.
  5371. eosPending = isOrdered && (curRow != maxRows);
  5372. return queryEndOfSectionMarker();
  5373. }
  5374. }
  5375. }
  5376. CRoxieServerStrandedInlineTableActivity & queryParent() { return static_cast<CRoxieServerStrandedInlineTableActivity&>(parent); }
  5377. };
  5378. public:
  5379. CRoxieServerStrandedInlineTableActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const StrandOptions &_strandOptions)
  5380. : CRoxieServerStrandedActivity(_ctx, _factory, _probeManager, _strandOptions), helper((IHThorInlineTableArg &) basehelper)
  5381. {
  5382. numRows = 0;
  5383. }
  5384. virtual StrandProcessor *createStrandProcessor(IEngineRowStream *instream) { throwUnexpected(); }
  5385. virtual StrandProcessor *createStrandSourceProcessor(bool inputOrdered)
  5386. {
  5387. if (strandOptions.numStrands <= 1)
  5388. return new InlineTableSimpleProcessor(*this, helper);
  5389. return new InlineTableStrandProcessor(*this, helper, strands.ordinality(), inputOrdered);
  5390. }
  5391. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5392. {
  5393. CRoxieServerStrandedActivity::start(parentExtractSize, parentExtract, paused);
  5394. numRows = helper.numRows();
  5395. onStartStrands();
  5396. startJunction(sourceJunction); // This must be started *after* all the strands have been initialised
  5397. }
  5398. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  5399. {
  5400. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  5401. }
  5402. };
  5403. class CRoxieServerInlineTableActivityFactory : public CRoxieServerActivityFactory
  5404. {
  5405. StrandOptions strandOptions;
  5406. public:
  5407. CRoxieServerInlineTableActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  5408. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), strandOptions(_graphNode)
  5409. {
  5410. optStableInput = false; // do not force the output to be ordered
  5411. }
  5412. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  5413. {
  5414. return new CRoxieServerStrandedInlineTableActivity(_ctx, this, _probeManager, strandOptions);
  5415. }
  5416. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  5417. {
  5418. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for InlineTable activity");
  5419. }
  5420. };
  5421. IRoxieServerActivityFactory *createRoxieServerInlineTableActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  5422. {
  5423. return new CRoxieServerInlineTableActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  5424. }
  5425. //=================================================================================
  5426. class CRoxieServerWorkUnitReadActivity : public CRoxieServerActivity
  5427. {
  5428. IHThorWorkunitReadArg &helper;
  5429. CriticalSection readerCrit;
  5430. Owned<IWorkUnitRowReader> wuReader;
  5431. public:
  5432. CRoxieServerWorkUnitReadActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  5433. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorWorkunitReadArg &)basehelper)
  5434. {
  5435. }
  5436. virtual void onCreate(IHThorArg *_colocalParent)
  5437. {
  5438. CRoxieServerActivity::onCreate(_colocalParent);
  5439. if (!ctx->queryServerContext())
  5440. {
  5441. throw MakeStringException(ROXIE_INTERNAL_ERROR, "Workunit read activity cannot be executed in agent context");
  5442. }
  5443. }
  5444. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5445. {
  5446. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  5447. IXmlToRowTransformer * xmlTransformer = helper.queryXmlTransformer();
  5448. OwnedRoxieString fromWuid(helper.getWUID());
  5449. wuReader.setown(ctx->getWorkunitRowReader(fromWuid, helper.queryName(), helper.querySequence(), xmlTransformer, rowAllocator, meta.isGrouped()));
  5450. // MORE _ should that be in onCreate?
  5451. }
  5452. virtual void reset()
  5453. {
  5454. {
  5455. CriticalBlock b(readerCrit);
  5456. wuReader.clear();
  5457. }
  5458. CRoxieServerActivity::reset();
  5459. };
  5460. virtual bool needsAllocator() const { return true; }
  5461. virtual const void *nextRow()
  5462. {
  5463. ActivityTimer t(activityStats, timeActivities);
  5464. Linked<IWorkUnitRowReader> useReader;
  5465. {
  5466. CriticalBlock b(readerCrit);
  5467. if (!wuReader)
  5468. return NULL;
  5469. useReader.set(wuReader);
  5470. }
  5471. const void *ret = useReader->nextRow();
  5472. if (ret)
  5473. processed++;
  5474. return ret;
  5475. }
  5476. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  5477. {
  5478. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  5479. }
  5480. };
  5481. class CRoxieServerWorkUnitReadActivityFactory : public CRoxieServerActivityFactory
  5482. {
  5483. public:
  5484. CRoxieServerWorkUnitReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  5485. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  5486. {
  5487. }
  5488. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  5489. {
  5490. return new CRoxieServerWorkUnitReadActivity(_ctx, this, _probeManager);
  5491. }
  5492. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  5493. {
  5494. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for WorkUnitRead activity");
  5495. }
  5496. };
  5497. IRoxieServerActivityFactory *createRoxieServerWorkUnitReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  5498. {
  5499. return new CRoxieServerWorkUnitReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  5500. }
  5501. //=================================================================================
  5502. interface ILocalGraphEx : public IEclGraphResults
  5503. {
  5504. public:
  5505. virtual void setResult(unsigned id, IGraphResult * result) = 0;
  5506. virtual IEngineRowStream * createResultIterator(unsigned id) = 0;
  5507. virtual void setGraphLoopResult(IGraphResult * result) = 0;
  5508. virtual IEngineRowStream * createGraphLoopResultIterator(unsigned id) = 0;
  5509. };
  5510. class CSafeRoxieInput : implements IEngineRowStream, implements IFinalRoxieInput, public CInterface
  5511. {
  5512. public:
  5513. CSafeRoxieInput(unsigned _sourceIdx, IFinalRoxieInput * _input) : input(_input), inputStream(NULL), sourceIdx(_sourceIdx) {}
  5514. IMPLEMENT_IINTERFACE
  5515. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks) override
  5516. {
  5517. connectInputStreams(ctx, consumerOrdered);
  5518. streams.append(this);
  5519. return NULL;
  5520. }
  5521. void connectInputStreams(IRoxieAgentContext *ctx, bool consumerOrdered)
  5522. {
  5523. inputStream.set(connectSingleStream(ctx, input, sourceIdx, junction, consumerOrdered));
  5524. }
  5525. virtual IOutputMetaData * queryOutputMeta() const
  5526. {
  5527. return input->queryOutputMeta();
  5528. }
  5529. virtual unsigned __int64 queryTotalCycles() const
  5530. {
  5531. return input->queryTotalCycles();
  5532. }
  5533. virtual IEngineRowStream *queryConcreteOutputStream(unsigned whichInput) { assertex(whichInput==0); return this; }
  5534. virtual IStrandJunction *queryConcreteOutputJunction(unsigned idx) const { assertex(idx==0); return nullptr; }
  5535. virtual IRoxieServerActivity *queryActivity()
  5536. {
  5537. return input->queryActivity();
  5538. }
  5539. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  5540. {
  5541. return input->queryIndexReadActivity();
  5542. }
  5543. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5544. {
  5545. CriticalBlock procedure(cs);
  5546. input->start(parentExtractSize, parentExtract, paused);
  5547. startJunction(junction);
  5548. }
  5549. virtual void stop()
  5550. {
  5551. CriticalBlock procedure(cs);
  5552. inputStream->stop();
  5553. }
  5554. virtual void reset()
  5555. {
  5556. CriticalBlock procedure(cs);
  5557. input->reset();
  5558. resetJunction(junction);
  5559. }
  5560. virtual void resetEOF()
  5561. {
  5562. CriticalBlock procedure(cs);
  5563. inputStream->resetEOF();
  5564. }
  5565. virtual const void *nextRow()
  5566. {
  5567. CriticalBlock procedure(cs);
  5568. return inputStream->nextRow();
  5569. }
  5570. virtual bool nextGroup(ConstPointerArray & group)
  5571. {
  5572. CriticalBlock procedure(cs);
  5573. return inputStream->nextGroup(group);
  5574. }
  5575. private:
  5576. CriticalSection cs;
  5577. Linked<IFinalRoxieInput> input;
  5578. Linked<IEngineRowStream> inputStream;
  5579. Owned<IStrandJunction> junction;
  5580. unsigned sourceIdx;
  5581. };
  5582. //=================================================================================
  5583. class CPseudoRoxieInput : implements IEngineRowStream, implements IFinalRoxieInput, public CInterface
  5584. {
  5585. public:
  5586. IMPLEMENT_IINTERFACE;
  5587. CPseudoRoxieInput()
  5588. {
  5589. }
  5590. virtual unsigned __int64 queryTotalCycles() const
  5591. {
  5592. return 0;
  5593. }
  5594. virtual IEngineRowStream *queryConcreteOutputStream(unsigned whichInput) { assertex(whichInput==0); return this; }
  5595. virtual IStrandJunction *queryConcreteOutputJunction(unsigned idx) const { assertex(idx==0); return nullptr; }
  5596. virtual IRoxieServerActivity *queryActivity()
  5597. {
  5598. throwUnexpected();
  5599. }
  5600. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  5601. {
  5602. throwUnexpected();
  5603. }
  5604. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks) override
  5605. {
  5606. assertex(!idx);
  5607. streams.append(this);
  5608. return NULL;
  5609. }
  5610. virtual IOutputMetaData * queryOutputMeta() const { throwUnexpected(); }
  5611. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused) { }
  5612. virtual void stop() { }
  5613. virtual void reset() { }
  5614. virtual void checkAbort() { }
  5615. virtual void resetEOF() { }
  5616. };
  5617. class CIndirectRoxieInput : public CPseudoRoxieInput
  5618. {
  5619. public:
  5620. CIndirectRoxieInput() : input(NULL)
  5621. {
  5622. stream = NULL;
  5623. sourceIdx = 0;
  5624. }
  5625. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5626. {
  5627. input->start(parentExtractSize, parentExtract, paused);
  5628. startJunction(junction);
  5629. }
  5630. virtual void stop()
  5631. {
  5632. stream->stop();
  5633. }
  5634. virtual void reset()
  5635. {
  5636. input->reset();
  5637. resetJunction(junction);
  5638. }
  5639. virtual const void * nextRow()
  5640. {
  5641. return stream->nextRow();
  5642. }
  5643. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  5644. {
  5645. return stream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  5646. }
  5647. virtual unsigned __int64 queryTotalCycles() const
  5648. {
  5649. return input ? input->queryTotalCycles() : 0;
  5650. }
  5651. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  5652. {
  5653. return input->gatherConjunctions(collector);
  5654. }
  5655. virtual void resetEOF()
  5656. {
  5657. stream->resetEOF();
  5658. }
  5659. virtual unsigned numConcreteOutputs() const
  5660. {
  5661. return input->numConcreteOutputs();
  5662. }
  5663. virtual IFinalRoxieInput * queryConcreteInput(unsigned idx)
  5664. {
  5665. return input->queryConcreteInput(idx);
  5666. }
  5667. virtual IEngineRowStream *queryConcreteOutputStream(unsigned whichInput)
  5668. {
  5669. return input->queryConcreteOutputStream(whichInput);
  5670. }
  5671. virtual IStrandJunction *queryConcreteOutputJunction(unsigned idx) const
  5672. {
  5673. return input->queryConcreteOutputJunction(idx);
  5674. }
  5675. virtual IOutputMetaData * queryOutputMeta() const
  5676. {
  5677. return input->queryOutputMeta();
  5678. }
  5679. virtual IRoxieServerActivity *queryActivity()
  5680. {
  5681. return input->queryActivity();
  5682. }
  5683. inline void setInput(unsigned _sourceIdx, IFinalRoxieInput * _input)
  5684. {
  5685. input = _input;
  5686. sourceIdx = _sourceIdx;
  5687. }
  5688. inline void connectInputStreams(IRoxieAgentContext *ctx, bool consumerOrdered)
  5689. {
  5690. stream = connectSingleStream(ctx, input, sourceIdx, junction, consumerOrdered);
  5691. }
  5692. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks) override
  5693. {
  5694. assertex(idx == 0);
  5695. connectInputStreams(ctx, consumerOrdered);
  5696. streams.append(this);
  5697. return NULL;
  5698. }
  5699. inline IFinalRoxieInput *queryInput() const
  5700. {
  5701. return input;
  5702. }
  5703. protected:
  5704. unsigned sourceIdx;
  5705. IFinalRoxieInput *input;
  5706. IEngineRowStream *stream;
  5707. Owned<IStrandJunction> junction;
  5708. };
  5709. class CExtractMapperInput : public CIndirectRoxieInput
  5710. {
  5711. unsigned savedParentExtractSize;
  5712. const byte * savedParentExtract;
  5713. public:
  5714. CExtractMapperInput()
  5715. {
  5716. savedParentExtractSize = 0;
  5717. savedParentExtract = NULL;
  5718. }
  5719. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5720. {
  5721. CIndirectRoxieInput::start(savedParentExtractSize, savedParentExtract, paused);
  5722. }
  5723. void setParentExtract(unsigned _savedParentExtractSize, const byte * _savedParentExtract)
  5724. {
  5725. savedParentExtractSize = _savedParentExtractSize;
  5726. savedParentExtract = _savedParentExtract;
  5727. }
  5728. };
  5729. class CGraphResult : implements IGraphResult, public CInterface
  5730. {
  5731. CriticalSection cs;
  5732. const byte **rowset;
  5733. size32_t count;
  5734. bool complete;
  5735. void clear()
  5736. {
  5737. CriticalBlock func(cs);
  5738. rtlReleaseRowset(count, rowset);
  5739. rowset = NULL;
  5740. count = 0;
  5741. complete = false;
  5742. }
  5743. public:
  5744. IMPLEMENT_IINTERFACE
  5745. CGraphResult()
  5746. {
  5747. complete = false; // dummy result is not supposed to be used...
  5748. rowset = NULL;
  5749. count = 0;
  5750. }
  5751. CGraphResult(size32_t _count, const byte **_rowset)
  5752. : rowset(_rowset), count(_count)
  5753. {
  5754. complete = true;
  5755. }
  5756. ~CGraphResult()
  5757. {
  5758. clear();
  5759. }
  5760. // interface IGraphResult
  5761. virtual IEngineRowStream * createIterator()
  5762. {
  5763. if (!complete)
  5764. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Internal Error: Reading uninitialised graph result");
  5765. return new CGraphResultIterator(this);
  5766. }
  5767. virtual void getLinkedResult(unsigned & countResult, const byte * * & result) override
  5768. {
  5769. if (!complete)
  5770. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Internal Error: Reading uninitialised graph result");
  5771. result = rtlLinkRowset(rowset);
  5772. countResult = count;
  5773. }
  5774. virtual const void * getLinkedRowResult()
  5775. {
  5776. if (!complete)
  5777. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Internal Error: Reading uninitialised graph result");
  5778. if (count != 1)
  5779. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Internal Error: Expected a single row result");
  5780. const void * ret = rowset[0];
  5781. LinkRoxieRow(ret);
  5782. return ret;
  5783. }
  5784. //other
  5785. const void * getRow(unsigned i)
  5786. {
  5787. CriticalBlock func(cs);
  5788. if (i >= count)
  5789. return NULL;
  5790. const void * ret = rowset[i];
  5791. if (ret) LinkRoxieRow(ret);
  5792. return ret;
  5793. }
  5794. protected:
  5795. class CGraphResultIterator : public CInterfaceOf<IEngineRowStream>
  5796. {
  5797. unsigned i;
  5798. Linked<CGraphResult> result;
  5799. public:
  5800. CGraphResultIterator(CGraphResult * _result) : result(_result) { i = 0; }
  5801. public:
  5802. virtual const void * nextRow()
  5803. {
  5804. return result->getRow(i++);
  5805. }
  5806. virtual void stop()
  5807. {
  5808. }
  5809. virtual void resetEOF()
  5810. {
  5811. throwUnexpected();
  5812. }
  5813. };
  5814. };
  5815. //=================================================================================
  5816. class CRoxieServerLocalResultReadActivity : public CRoxieServerActivity
  5817. {
  5818. IHThorLocalResultReadArg &helper;
  5819. CriticalSection iterCrit;
  5820. Owned<IEngineRowStream> iter;
  5821. ILocalGraphEx * graph;
  5822. unsigned graphId;
  5823. unsigned sequence;
  5824. public:
  5825. CRoxieServerLocalResultReadActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
  5826. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorLocalResultReadArg &)basehelper), graphId(_graphId)
  5827. {
  5828. graph = NULL;
  5829. sequence = 0;
  5830. }
  5831. virtual void onCreate(IHThorArg *_colocalParent)
  5832. {
  5833. CRoxieServerActivity::onCreate(_colocalParent);
  5834. graph = static_cast<ILocalGraphEx *>(ctx->queryCodeContext()->resolveLocalQuery(graphId));
  5835. sequence = helper.querySequence();
  5836. }
  5837. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  5838. {
  5839. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  5840. iter.setown(graph->createResultIterator(sequence));
  5841. }
  5842. virtual void reset()
  5843. {
  5844. {
  5845. CriticalBlock b(iterCrit);
  5846. iter.clear();
  5847. }
  5848. CRoxieServerActivity::reset();
  5849. };
  5850. virtual const void *nextRow()
  5851. {
  5852. ActivityTimer t(activityStats, timeActivities);
  5853. Linked<IEngineRowStream> useIter;
  5854. {
  5855. CriticalBlock b(iterCrit);
  5856. if (!iter)
  5857. return NULL;
  5858. useIter.set(iter);
  5859. }
  5860. const void * next = useIter->nextRow();
  5861. if (next)
  5862. {
  5863. processed++;
  5864. rowsIn++;
  5865. }
  5866. return next;
  5867. }
  5868. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  5869. {
  5870. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  5871. }
  5872. };
  5873. class CRoxieServerLocalResultReadActivityFactory : public CRoxieServerActivityFactory
  5874. {
  5875. unsigned graphId;
  5876. public:
  5877. CRoxieServerLocalResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _graphId)
  5878. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), graphId(_graphId)
  5879. {
  5880. }
  5881. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  5882. {
  5883. return new CRoxieServerLocalResultReadActivity(_ctx, this, _probeManager, graphId);
  5884. }
  5885. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  5886. {
  5887. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for LocalResultRead activity");
  5888. }
  5889. };
  5890. IRoxieServerActivityFactory *createRoxieServerLocalResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned graphId)
  5891. {
  5892. return new CRoxieServerLocalResultReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, graphId);
  5893. }
  5894. //=================================================================================
  5895. class CRoxieServerLocalResultStreamReadActivity : public CRoxieServerActivity
  5896. {
  5897. IHThorLocalResultReadArg &helper;
  5898. unsigned sequence;
  5899. public:
  5900. CRoxieServerLocalResultStreamReadActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  5901. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorLocalResultReadArg &)basehelper)
  5902. {
  5903. sequence = 0;
  5904. }
  5905. virtual void onCreate(IHThorArg *_colocalParent)
  5906. {
  5907. CRoxieServerActivity::onCreate(_colocalParent);
  5908. sequence = helper.querySequence();
  5909. }
  5910. virtual const void *nextRow()
  5911. {
  5912. ActivityTimer t(activityStats, timeActivities);
  5913. const void * next = inputStream->nextRow();
  5914. if (next)
  5915. {
  5916. processed++;
  5917. rowsIn++;
  5918. }
  5919. return next;
  5920. }
  5921. virtual bool querySetStreamInput(unsigned id, unsigned _sourceIdx, IFinalRoxieInput * _input)
  5922. {
  5923. if (id == sequence)
  5924. {
  5925. CRoxieServerActivity::setInput(0, _sourceIdx, _input);
  5926. connectInputStreams(true);
  5927. return true;
  5928. }
  5929. return false;
  5930. }
  5931. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  5932. {
  5933. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  5934. }
  5935. };
  5936. class CRoxieServerLocalResultStreamReadActivityFactory : public CRoxieServerActivityFactory
  5937. {
  5938. public:
  5939. CRoxieServerLocalResultStreamReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  5940. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  5941. {
  5942. }
  5943. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  5944. {
  5945. return new CRoxieServerLocalResultStreamReadActivity(_ctx, this, _probeManager);
  5946. }
  5947. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  5948. {
  5949. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for LocalResultRead activity");
  5950. }
  5951. };
  5952. IRoxieServerActivityFactory *createRoxieServerLocalResultStreamReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  5953. {
  5954. return new CRoxieServerLocalResultStreamReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  5955. }
  5956. //=====================================================================================================
  5957. class CRoxieServerLocalResultWriteActivity : public CRoxieServerInternalSinkActivity
  5958. {
  5959. IHThorLocalResultWriteArg &helper;
  5960. ILocalGraphEx * graph;
  5961. unsigned graphId;
  5962. public:
  5963. CRoxieServerLocalResultWriteActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId, unsigned _numOutputs)
  5964. : CRoxieServerInternalSinkActivity(_ctx, _factory, _probeManager, _numOutputs), helper((IHThorLocalResultWriteArg &)basehelper), graphId(_graphId)
  5965. {
  5966. graph = NULL;
  5967. }
  5968. virtual bool needsAllocator() const { return true; }
  5969. virtual void onCreate(IHThorArg *_colocalParent)
  5970. {
  5971. CRoxieServerInternalSinkActivity::onCreate(_colocalParent);
  5972. graph = static_cast<ILocalGraphEx *>(ctx->queryCodeContext()->resolveLocalQuery(graphId));
  5973. }
  5974. virtual void onExecute()
  5975. {
  5976. RtlLinkedDatasetBuilder builder(rowAllocator);
  5977. inputStream->readAll(builder);
  5978. Owned<CGraphResult> result = new CGraphResult(builder.getcount(), builder.linkrows());
  5979. graph->setResult(helper.querySequence(), result);
  5980. }
  5981. virtual const void *nextRow()
  5982. {
  5983. return inputStream->nextRow(); // I can act as a passthrough input
  5984. }
  5985. IFinalRoxieInput * querySelectOutput(unsigned id)
  5986. {
  5987. if (id == helper.querySequence())
  5988. {
  5989. executed = true; // Ensure that we don't try to pull as a sink as well as via the passthrough
  5990. return LINK(this);
  5991. }
  5992. return NULL;
  5993. }
  5994. };
  5995. class CRoxieServerLocalResultWriteActivityFactory : public CRoxieServerInternalSinkFactory
  5996. {
  5997. unsigned graphId;
  5998. public:
  5999. CRoxieServerLocalResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, unsigned _graphId, bool _isRoot)
  6000. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _isRoot), graphId(_graphId)
  6001. {
  6002. isInternal = true;
  6003. Owned<IHThorLocalResultWriteArg> helper = (IHThorLocalResultWriteArg *) helperFactory();
  6004. }
  6005. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  6006. {
  6007. return new CRoxieServerLocalResultWriteActivity(_ctx, this, _probeManager, graphId, usageCount);
  6008. }
  6009. };
  6010. IRoxieServerActivityFactory *createRoxieServerLocalResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, unsigned _graphId, bool _isRoot)
  6011. {
  6012. return new CRoxieServerLocalResultWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _graphId, _isRoot);
  6013. }
  6014. //=====================================================================================================
  6015. class CRoxieServerDictionaryResultWriteActivity : public CRoxieServerInternalSinkActivity
  6016. {
  6017. IHThorDictionaryResultWriteArg &helper;
  6018. ILocalGraphEx * graph;
  6019. unsigned graphId;
  6020. public:
  6021. CRoxieServerDictionaryResultWriteActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _usageCount, unsigned _graphId)
  6022. : CRoxieServerInternalSinkActivity(_ctx, _factory, _probeManager, _usageCount), helper((IHThorDictionaryResultWriteArg &)basehelper), graphId(_graphId)
  6023. {
  6024. graph = NULL;
  6025. }
  6026. virtual bool needsAllocator() const { return true; }
  6027. virtual void onCreate(IHThorArg *_colocalParent)
  6028. {
  6029. CRoxieServerInternalSinkActivity::onCreate(_colocalParent);
  6030. graph = static_cast<ILocalGraphEx *>(ctx->queryCodeContext()->resolveLocalQuery(graphId));
  6031. }
  6032. virtual void onExecute()
  6033. {
  6034. RtlLinkedDictionaryBuilder builder(rowAllocator, helper.queryHashLookupInfo());
  6035. for (;;)
  6036. {
  6037. const void *row = inputStream->ungroupedNextRow();
  6038. if (!row)
  6039. break;
  6040. builder.appendOwn(row);
  6041. processed++;
  6042. }
  6043. Owned<CGraphResult> result = new CGraphResult(builder.getcount(), builder.linkrows());
  6044. graph->setResult(helper.querySequence(), result);
  6045. }
  6046. virtual const void *nextRow()
  6047. {
  6048. return inputStream->nextRow(); // I can act as a passthrough input
  6049. }
  6050. IFinalRoxieInput * querySelectOutput(unsigned id)
  6051. {
  6052. if (id == helper.querySequence())
  6053. {
  6054. executed = true; // Ensure that we don't try to pull as a sink as well as via the passthrough
  6055. return LINK(this);
  6056. }
  6057. return NULL;
  6058. }
  6059. };
  6060. class CRoxieServerDictionaryResultWriteActivityFactory : public CRoxieServerInternalSinkFactory
  6061. {
  6062. unsigned graphId;
  6063. public:
  6064. CRoxieServerDictionaryResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, unsigned _graphId, bool _isRoot)
  6065. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _isRoot), graphId(_graphId)
  6066. {
  6067. isInternal = true;
  6068. }
  6069. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  6070. {
  6071. return new CRoxieServerDictionaryResultWriteActivity(_ctx, this, _probeManager, usageCount, graphId);
  6072. }
  6073. };
  6074. IRoxieServerActivityFactory *createRoxieServerDictionaryResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, unsigned _graphId, bool _isRoot)
  6075. {
  6076. return new CRoxieServerDictionaryResultWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _graphId, _isRoot);
  6077. }
  6078. //=================================================================================
  6079. class CRoxieServerGraphLoopResultReadActivity : public CRoxieServerActivity
  6080. {
  6081. protected:
  6082. IHThorGraphLoopResultReadArg &helper;
  6083. CriticalSection iterCrit;
  6084. Owned<IFinalRoxieInput> iterInput;
  6085. Owned<IEngineRowStream> iterStream;
  6086. Owned<IStrandJunction> iterJunction;
  6087. ILocalGraphEx * graph;
  6088. unsigned graphId;
  6089. unsigned sequence;
  6090. public:
  6091. CRoxieServerGraphLoopResultReadActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
  6092. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorGraphLoopResultReadArg &)basehelper), graphId(_graphId)
  6093. {
  6094. graph = NULL;
  6095. sequence = 0;
  6096. }
  6097. virtual void onCreate(IHThorArg *_colocalParent)
  6098. {
  6099. CRoxieServerActivity::onCreate(_colocalParent);
  6100. graph = static_cast<ILocalGraphEx *>(ctx->queryCodeContext()->resolveLocalQuery(graphId));
  6101. }
  6102. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  6103. {
  6104. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  6105. if (iterInput)
  6106. iterInput->start(parentExtractSize, parentExtract, paused);
  6107. else
  6108. {
  6109. sequence = helper.querySequence();
  6110. if ((int)sequence >= 0)
  6111. {
  6112. try
  6113. {
  6114. iterStream.setown(graph->createGraphLoopResultIterator(sequence));
  6115. iterInput.clear();
  6116. }
  6117. catch (IException * E)
  6118. {
  6119. throw makeWrappedException(E);
  6120. }
  6121. }
  6122. }
  6123. startJunction(iterJunction);
  6124. }
  6125. virtual void stop()
  6126. {
  6127. if (iterStream)
  6128. iterStream->stop();
  6129. CRoxieServerActivity::stop();
  6130. }
  6131. virtual void reset()
  6132. {
  6133. {
  6134. CriticalBlock b(iterCrit);
  6135. if (iterInput)
  6136. iterInput->reset();
  6137. resetJunction(iterJunction);
  6138. iterInput.clear();
  6139. iterStream.clear();
  6140. iterJunction.clear();
  6141. }
  6142. CRoxieServerActivity::reset();
  6143. };
  6144. virtual const void *nextRow()
  6145. {
  6146. ActivityTimer t(activityStats, timeActivities);
  6147. Linked<IEngineRowStream> useStream;
  6148. {
  6149. CriticalBlock b(iterCrit);
  6150. if (!iterStream)
  6151. return NULL;
  6152. useStream.set(iterStream);
  6153. }
  6154. const void * next = useStream->nextRow();
  6155. if (next)
  6156. {
  6157. processed++;
  6158. rowsIn++;
  6159. }
  6160. return next;
  6161. }
  6162. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  6163. {
  6164. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  6165. }
  6166. virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract)
  6167. {
  6168. ensureCreated();
  6169. basehelper.onStart(parentExtract, NULL);
  6170. processor.noteUseIteration(helper.querySequence());
  6171. }
  6172. virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract, IProbeManager *probeManager, IArrayOf<IRoxieProbe> &probes)
  6173. {
  6174. //helper already initialised from the gatherIterationUsage() call.
  6175. CRoxieServerActivity::connectInputStreams(true);
  6176. iterInput.set(processor.connectIterationOutput(helper.querySequence(), probeManager, probes, this, 0));
  6177. iterStream.set(connectSingleStream(ctx, iterInput, 0, iterJunction, true));
  6178. }
  6179. virtual IInputSteppingMeta * querySteppingMeta()
  6180. {
  6181. assertex(iterInput);
  6182. return iterInput->querySteppingMeta();
  6183. }
  6184. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  6185. {
  6186. assertex(iterStream);
  6187. return iterStream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  6188. }
  6189. };
  6190. //variety of CRoxieServerGraphLoopResultReadActivity created internally with a predefined sequence number
  6191. class CRoxieServerInternalGraphLoopResultReadActivity : public CRoxieServerGraphLoopResultReadActivity
  6192. {
  6193. public:
  6194. CRoxieServerInternalGraphLoopResultReadActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId, unsigned _sequence)
  6195. : CRoxieServerGraphLoopResultReadActivity(_ctx, _factory, _probeManager, _graphId)
  6196. {
  6197. sequence = _sequence;
  6198. }
  6199. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  6200. {
  6201. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  6202. if ((int)sequence >= 0)
  6203. {
  6204. try
  6205. {
  6206. iterStream.setown(graph->createGraphLoopResultIterator(sequence));
  6207. iterInput.clear();
  6208. }
  6209. catch (IException * E)
  6210. {
  6211. throw makeWrappedException(E);
  6212. }
  6213. }
  6214. }
  6215. };
  6216. class CRoxieServerGraphLoopResultReadActivityFactory : public CRoxieServerActivityFactory
  6217. {
  6218. unsigned graphId;
  6219. public:
  6220. CRoxieServerGraphLoopResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _graphId)
  6221. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), graphId(_graphId)
  6222. {
  6223. }
  6224. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  6225. {
  6226. return new CRoxieServerGraphLoopResultReadActivity(_ctx, this, _probeManager, graphId);
  6227. }
  6228. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  6229. {
  6230. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for GraphLoopResultRead activity");
  6231. }
  6232. };
  6233. IRoxieServerActivityFactory *createRoxieServerGraphLoopResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned graphId)
  6234. {
  6235. return new CRoxieServerGraphLoopResultReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, graphId);
  6236. }
  6237. //=====================================================================================================
  6238. class CRoxieServerGraphLoopResultWriteActivity : public CRoxieServerInternalSinkActivity
  6239. {
  6240. ILocalGraphEx * graph;
  6241. unsigned graphId;
  6242. public:
  6243. CRoxieServerGraphLoopResultWriteActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId, unsigned _numOutputs)
  6244. : CRoxieServerInternalSinkActivity(_ctx, _factory, _probeManager, _numOutputs), graphId(_graphId)
  6245. {
  6246. graph = NULL;
  6247. }
  6248. virtual bool needsAllocator() const { return true; }
  6249. virtual void onCreate(IHThorArg *_colocalParent)
  6250. {
  6251. CRoxieServerInternalSinkActivity::onCreate(_colocalParent);
  6252. graph = static_cast<ILocalGraphEx *>(ctx->queryCodeContext()->resolveLocalQuery(graphId));
  6253. }
  6254. virtual void onExecute()
  6255. {
  6256. RtlLinkedDatasetBuilder builder(rowAllocator);
  6257. inputStream->readAll(builder);
  6258. Owned<CGraphResult> result = new CGraphResult(builder.getcount(), builder.linkrows());
  6259. graph->setGraphLoopResult(result);
  6260. }
  6261. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  6262. {
  6263. if (idx==0)
  6264. return this;
  6265. else
  6266. return NULL;
  6267. }
  6268. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  6269. {
  6270. return input->gatherConjunctions(collector);
  6271. }
  6272. virtual void resetEOF()
  6273. {
  6274. inputStream->resetEOF();
  6275. }
  6276. virtual const void *nextRow()
  6277. {
  6278. const void * next = inputStream->nextRow();
  6279. if (next)
  6280. processed++;
  6281. return next;
  6282. }
  6283. virtual bool isPassThrough()
  6284. {
  6285. return true;
  6286. }
  6287. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  6288. {
  6289. ActivityTimer t(activityStats, timeActivities);
  6290. const void * next = inputStream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  6291. if (next)
  6292. processed++;
  6293. return next;
  6294. }
  6295. IInputSteppingMeta * querySteppingMeta()
  6296. {
  6297. return input->querySteppingMeta();
  6298. }
  6299. virtual IOutputMetaData * queryOutputMeta() const
  6300. {
  6301. return input->queryOutputMeta();
  6302. }
  6303. };
  6304. class CRoxieServerGraphLoopResultWriteActivityFactory : public CRoxieServerInternalSinkFactory
  6305. {
  6306. unsigned graphId;
  6307. public:
  6308. CRoxieServerGraphLoopResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, unsigned _graphId)
  6309. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, true), graphId(_graphId)
  6310. {
  6311. isInternal = true;
  6312. Owned<IHThorGraphLoopResultWriteArg> helper = (IHThorGraphLoopResultWriteArg *) helperFactory();
  6313. }
  6314. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  6315. {
  6316. return new CRoxieServerGraphLoopResultWriteActivity(_ctx, this, _probeManager, graphId, usageCount);
  6317. }
  6318. };
  6319. IRoxieServerActivityFactory *createRoxieServerGraphLoopResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, unsigned _graphId)
  6320. {
  6321. return new CRoxieServerGraphLoopResultWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _graphId);
  6322. }
  6323. //=================================================================================
  6324. class CRoxieServerDedupActivity : public CRoxieServerActivity
  6325. {
  6326. protected:
  6327. IHThorDedupArg &helper;
  6328. unsigned numKept;
  6329. unsigned numToKeep;
  6330. public:
  6331. CRoxieServerDedupActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  6332. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorDedupArg &)basehelper)
  6333. {
  6334. numKept = 0;
  6335. numToKeep = 0;
  6336. }
  6337. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  6338. {
  6339. numKept = 0;
  6340. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  6341. numToKeep = helper.numToKeep();
  6342. }
  6343. };
  6344. class CRoxieServerDedupKeepLeftActivity : public CRoxieServerDedupActivity
  6345. {
  6346. IRangeCompare * stepCompare;
  6347. const void *prev;
  6348. public:
  6349. CRoxieServerDedupKeepLeftActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  6350. : CRoxieServerDedupActivity(_ctx, _factory, _probeManager)
  6351. {
  6352. prev = NULL;
  6353. stepCompare = NULL;
  6354. }
  6355. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  6356. {
  6357. prev = NULL;
  6358. CRoxieServerDedupActivity::start(parentExtractSize, parentExtract, paused);
  6359. IInputSteppingMeta * stepMeta = input->querySteppingMeta();
  6360. stepCompare = NULL;
  6361. if (stepMeta)
  6362. stepCompare = stepMeta->queryCompare();
  6363. }
  6364. virtual void reset()
  6365. {
  6366. ReleaseClearRoxieRow(prev);
  6367. CRoxieServerDedupActivity::reset();
  6368. }
  6369. virtual const void * nextRow()
  6370. {
  6371. ActivityTimer t(activityStats, timeActivities);
  6372. const void * next;
  6373. for (;;)
  6374. {
  6375. next = inputStream->nextRow();
  6376. if (!prev || !next || !helper.matches(prev,next))
  6377. {
  6378. numKept = 0;
  6379. break;
  6380. }
  6381. if (numKept < numToKeep-1)
  6382. {
  6383. numKept++;
  6384. break;
  6385. }
  6386. ReleaseRoxieRow(next);
  6387. }
  6388. ReleaseRoxieRow(prev);
  6389. prev = next;
  6390. if (next)
  6391. {
  6392. LinkRoxieRow(next);
  6393. processed++;
  6394. }
  6395. return next;
  6396. }
  6397. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  6398. {
  6399. ActivityTimer t(activityStats, timeActivities);
  6400. const void * next;
  6401. for (;;)
  6402. {
  6403. next = inputStream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  6404. //If the record was an in-exact match from the index then return it immediately
  6405. //and don't cause it to dedup following legal records.
  6406. if (!wasCompleteMatch)
  6407. {
  6408. assertex(stepExtra.returnMismatches());
  6409. return next;
  6410. }
  6411. if (!prev || !next || !helper.matches(prev,next))
  6412. {
  6413. numKept = 0;
  6414. break;
  6415. }
  6416. if (numKept < numToKeep-1)
  6417. {
  6418. numKept++;
  6419. break;
  6420. }
  6421. //Unusual - deduping by x,y stepped on x,y,z - still want any record back as soon as possible.
  6422. if (stepExtra.returnMismatches())
  6423. {
  6424. //If asked to return mismatches we are only interested in mismatches that will force the stepped
  6425. //condition to advance
  6426. if (stepCompare->docompare(next, seek, numFields) != 0)
  6427. {
  6428. wasCompleteMatch = false;
  6429. break;
  6430. }
  6431. }
  6432. ReleaseRoxieRow(next);
  6433. }
  6434. ReleaseRoxieRow(prev);
  6435. prev = next;
  6436. if (next)
  6437. {
  6438. LinkRoxieRow(next);
  6439. processed++;
  6440. }
  6441. return next;
  6442. }
  6443. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  6444. {
  6445. return input->gatherConjunctions(collector);
  6446. }
  6447. virtual void resetEOF()
  6448. {
  6449. inputStream->resetEOF();
  6450. }
  6451. IInputSteppingMeta * querySteppingMeta()
  6452. {
  6453. return input->querySteppingMeta();
  6454. }
  6455. };
  6456. //=================================================================================
  6457. class CRoxieServerDedupKeepRightActivity : public CRoxieServerDedupActivity
  6458. {
  6459. const void *kept;
  6460. bool first;
  6461. ICompare * compareBest;
  6462. public:
  6463. CRoxieServerDedupKeepRightActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  6464. : CRoxieServerDedupActivity(_ctx, _factory, _probeManager), compareBest(nullptr)
  6465. {
  6466. kept = nullptr;
  6467. first = true;
  6468. if (helper.keepBest())
  6469. compareBest = helper.queryCompareBest();
  6470. }
  6471. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  6472. {
  6473. first = true;
  6474. kept = nullptr;
  6475. CRoxieServerDedupActivity::start(parentExtractSize, parentExtract, paused);
  6476. if (numToKeep>1)
  6477. throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "DEDUP with RIGHT and NumToKeep>1 not supported");
  6478. }
  6479. virtual void reset()
  6480. {
  6481. ReleaseClearRoxieRow(kept);
  6482. CRoxieServerActivity::reset();
  6483. }
  6484. virtual const void * nextRow()
  6485. {
  6486. ActivityTimer t(activityStats, timeActivities);
  6487. if (first)
  6488. {
  6489. kept = inputStream->nextRow();
  6490. first = false;
  6491. }
  6492. const void * next;
  6493. for (;;)
  6494. {
  6495. next = inputStream->nextRow();
  6496. if (!kept || !next || !helper.matches(kept,next))
  6497. break;
  6498. if (compareBest && compareBest->docompare(kept,next) <= 0)
  6499. continue;
  6500. ReleaseRoxieRow(kept);
  6501. kept = next;
  6502. }
  6503. const void * ret = kept;
  6504. kept = next;
  6505. // CTXLOG("dedup returns %p", ret);
  6506. if (ret) processed++;
  6507. return ret;
  6508. }
  6509. };
  6510. class CRoxieServerDedupAllActivity : public CRoxieServerActivity
  6511. {
  6512. IHThorDedupArg &helper;
  6513. unsigned survivorIndex;
  6514. ConstPointerArray survivors;
  6515. bool keepLeft;
  6516. bool eof;
  6517. bool first;
  6518. ICompare *primaryCompare;
  6519. public:
  6520. CRoxieServerDedupAllActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _keepLeft)
  6521. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorDedupArg &)basehelper)
  6522. {
  6523. keepLeft = _keepLeft;
  6524. primaryCompare = helper.queryComparePrimary();
  6525. eof = false;
  6526. first = true;
  6527. survivorIndex = 0;
  6528. }
  6529. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  6530. {
  6531. eof = false;
  6532. first = true;
  6533. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  6534. }
  6535. virtual void reset()
  6536. {
  6537. while (survivors.isItem(survivorIndex))
  6538. {
  6539. ReleaseRoxieRow(survivors.item(survivorIndex++));
  6540. }
  6541. survivors.kill();
  6542. eof = false;
  6543. first = true;
  6544. CRoxieServerActivity::reset();
  6545. }
  6546. void dedupRange(unsigned first, unsigned last, ConstPointerArray & group)
  6547. {
  6548. for (unsigned idxL = first; idxL < last; idxL++)
  6549. {
  6550. const void * left = group.item(idxL);
  6551. if (left)
  6552. {
  6553. for (unsigned idxR = first; idxR < last; idxR++)
  6554. {
  6555. const void * right = group.item(idxR);
  6556. if ((idxL != idxR) && right)
  6557. {
  6558. if (helper.matches(left, right))
  6559. {
  6560. if (keepLeft)
  6561. {
  6562. group.replace(NULL, idxR);
  6563. ReleaseRoxieRow(right);
  6564. }
  6565. else
  6566. {
  6567. group.replace(NULL, idxL);
  6568. ReleaseRoxieRow(left);
  6569. break;
  6570. }
  6571. }
  6572. }
  6573. }
  6574. }
  6575. }
  6576. }
  6577. void dedupRange(unsigned first, unsigned last, void ** rows)
  6578. {
  6579. for (unsigned idxL = first; idxL < last; idxL++)
  6580. {
  6581. void * left = rows[idxL];
  6582. if (left)
  6583. {
  6584. for (unsigned idxR = first; idxR < last; idxR++)
  6585. {
  6586. void * right = rows[idxR];
  6587. if ((idxL != idxR) && right)
  6588. {
  6589. if (helper.matches(left, right))
  6590. {
  6591. if (keepLeft)
  6592. {
  6593. rows[idxR] = NULL;
  6594. ReleaseRoxieRow(right);
  6595. }
  6596. else
  6597. {
  6598. rows[idxL] = NULL;
  6599. ReleaseRoxieRow(left);
  6600. break;
  6601. }
  6602. }
  6603. }
  6604. }
  6605. }
  6606. }
  6607. }
  6608. bool calcNextDedupAll()
  6609. {
  6610. survivors.kill();
  6611. survivorIndex = 0;
  6612. ConstPointerArray group;
  6613. if (eof || !inputStream->nextGroup(group))
  6614. {
  6615. eof = true;
  6616. return false;
  6617. }
  6618. unsigned max = group.ordinality();
  6619. if (primaryCompare)
  6620. {
  6621. //hard, if not impossible, to hit this code once optimisations in place
  6622. MemoryAttr indexbuff(max*sizeof(void *));
  6623. void ** temp = (void **)indexbuff.bufferBase();
  6624. void * *rows = const_cast<void * *>(group.getArray());
  6625. msortvecstableinplace(rows, max, *primaryCompare, temp);
  6626. unsigned first = 0;
  6627. for (unsigned idx = 1; idx < max; idx++)
  6628. {
  6629. if (primaryCompare->docompare(rows[first], rows[idx]) != 0)
  6630. {
  6631. dedupRange(first, idx, rows);
  6632. first = idx;
  6633. }
  6634. }
  6635. dedupRange(first, max, rows);
  6636. for(unsigned idx2=0; idx2<max; ++idx2)
  6637. {
  6638. void * cur = rows[idx2];
  6639. if(cur)
  6640. survivors.append(cur);
  6641. }
  6642. }
  6643. else
  6644. {
  6645. dedupRange(0, max, group);
  6646. for(unsigned idx=0; idx<max; ++idx)
  6647. {
  6648. const void * cur = group.item(idx);
  6649. if(cur)
  6650. survivors.append(cur);
  6651. }
  6652. }
  6653. return true;
  6654. }
  6655. virtual const void *nextRow()
  6656. {
  6657. ActivityTimer t(activityStats, timeActivities);
  6658. if (first)
  6659. {
  6660. calcNextDedupAll();
  6661. first = false;
  6662. }
  6663. while (survivors.isItem(survivorIndex))
  6664. {
  6665. const void *ret = survivors.item(survivorIndex++);
  6666. if (ret)
  6667. {
  6668. processed++;
  6669. return ret;
  6670. }
  6671. }
  6672. calcNextDedupAll();
  6673. return NULL;
  6674. }
  6675. };
  6676. class CRoxieServerDedupActivityFactory : public CRoxieServerActivityFactory
  6677. {
  6678. bool compareAll;
  6679. bool keepLeft;
  6680. bool keepBest;
  6681. public:
  6682. CRoxieServerDedupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  6683. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  6684. {
  6685. Owned<IHThorDedupArg> helper = (IHThorDedupArg *) helperFactory();
  6686. compareAll = helper->compareAll();
  6687. keepLeft = helper->keepLeft();
  6688. keepBest = helper->keepBest();
  6689. }
  6690. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  6691. {
  6692. if (compareAll)
  6693. return new CRoxieServerDedupAllActivity(_ctx, this, _probeManager, keepLeft);
  6694. else if (keepLeft && !keepBest)
  6695. return new CRoxieServerDedupKeepLeftActivity(_ctx, this, _probeManager);
  6696. else
  6697. return new CRoxieServerDedupKeepRightActivity(_ctx, this, _probeManager);
  6698. }
  6699. };
  6700. IRoxieServerActivityFactory *createRoxieServerDedupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  6701. {
  6702. return new CRoxieServerDedupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  6703. }
  6704. //=================================================================================
  6705. class CRoxieServerHashDedupActivity : public CRoxieServerActivity
  6706. {
  6707. bool eof;
  6708. IHThorHashDedupArg &helper;
  6709. class HashDedupElement
  6710. {
  6711. public:
  6712. HashDedupElement(unsigned _hash, const void *_keyRow)
  6713. : hash(_hash), keyRow(_keyRow)
  6714. {
  6715. }
  6716. ~HashDedupElement()
  6717. {
  6718. ReleaseRoxieRow(keyRow);
  6719. }
  6720. inline unsigned queryHash() const
  6721. {
  6722. return hash;
  6723. }
  6724. inline const void *queryRow() const
  6725. {
  6726. return keyRow;
  6727. }
  6728. inline const void *getRowClear()
  6729. {
  6730. const void * row = keyRow;
  6731. keyRow = nullptr;
  6732. return row;
  6733. }
  6734. private:
  6735. unsigned hash;
  6736. const void *keyRow;
  6737. };
  6738. static CClassMeta<HashDedupElement> hashDedupElementMeta;
  6739. class HashDedupTable : public SuperHashTable
  6740. {
  6741. public:
  6742. HashDedupTable(IHThorHashDedupArg & _helper, CRoxieServerHashDedupActivity & _activity)
  6743. : helper(_helper),
  6744. keySize(helper.queryKeySize()),
  6745. activity(_activity)
  6746. {
  6747. bestCompare=helper.queryCompareBest();
  6748. }
  6749. virtual ~HashDedupTable()
  6750. {
  6751. //elementRowAllocator is a unique allocator, so all rows can be freed in a single call
  6752. elementRowAllocator->releaseAllRows();
  6753. tablecount = 0;
  6754. }
  6755. virtual unsigned getHashFromElement(const void *et) const
  6756. {
  6757. const HashDedupElement *element = reinterpret_cast<const HashDedupElement *>(et);
  6758. return element->queryHash();
  6759. }
  6760. virtual unsigned getHashFromFindParam(const void *fp) const { throwUnexpected(); }
  6761. virtual const void * getFindParam(const void *et) const { throwUnexpected(); }
  6762. virtual bool matchesElement(const void *et, const void *searchET) const { throwUnexpected(); }
  6763. virtual bool matchesFindParam(const void *et, const void *key, unsigned fphash) const
  6764. {
  6765. const HashDedupElement *element = reinterpret_cast<const HashDedupElement *>(et);
  6766. if (fphash != element->queryHash())
  6767. return false;
  6768. return (helper.queryKeyCompare()->docompare(element->queryRow(), key) == 0);
  6769. }
  6770. virtual void onAdd(void *et) {}
  6771. virtual void onRemove(void *et)
  6772. {
  6773. ReleaseRoxieRow(et);
  6774. }
  6775. void onCreate(IRoxieAgentContext *ctx)
  6776. {
  6777. keyRowAllocator.setown(activity.createRowAllocator(keySize.queryOriginal()));
  6778. elementRowAllocator.setown(activity.createRowAllocatorEx(&hashDedupElementMeta, roxiemem::RHFunique|roxiemem::RHFscanning|roxiemem::RHFdelayrelease));
  6779. }
  6780. void reset()
  6781. {
  6782. kill();
  6783. }
  6784. bool insert(const void * row)
  6785. {
  6786. unsigned hash = helper.queryHash()->hash(row);
  6787. RtlDynamicRowBuilder keyRowBuilder(keyRowAllocator, true);
  6788. size32_t thisKeySize = helper.recordToKey(keyRowBuilder, row);
  6789. OwnedConstRoxieRow keyRow = keyRowBuilder.finalizeRowClear(thisKeySize);
  6790. if (find(hash, keyRow.get()))
  6791. return false;
  6792. addNew(createElement(hash, keyRow.getClear()), hash);
  6793. return true;
  6794. }
  6795. bool insertBest(const void * nextrow)
  6796. {
  6797. unsigned hash = helper.queryHash()->hash(nextrow);
  6798. const void *et = find(hash, nextrow);
  6799. if (et)
  6800. {
  6801. const HashDedupElement *element = reinterpret_cast<const HashDedupElement *>(et);
  6802. const void * row = element->queryRow();
  6803. if (bestCompare->docompare(row,nextrow) <= 0)
  6804. return false;
  6805. removeExact( const_cast<void *>(et));
  6806. // drop-through to add new row
  6807. }
  6808. addNew(createElement(hash, nextrow), hash);
  6809. return true;
  6810. }
  6811. void gatherStats(CRuntimeStatisticCollection & merged) const
  6812. {
  6813. if (keyRowAllocator)
  6814. keyRowAllocator->gatherStats(merged);
  6815. }
  6816. private:
  6817. inline void *createElement(unsigned hash, const void *row)
  6818. {
  6819. return elementRowAllocator->finalizeRow(sizeof(HashDedupElement), new (elementRowAllocator->createRow()) HashDedupElement(hash, row), sizeof(HashDedupElement));
  6820. }
  6821. IHThorHashDedupArg & helper;
  6822. CachedOutputMetaData keySize;
  6823. Owned<IEngineRowAllocator> keyRowAllocator;
  6824. Owned<IEngineRowAllocator> elementRowAllocator;
  6825. CRoxieServerHashDedupActivity & activity;
  6826. ICompare * bestCompare;
  6827. } table;
  6828. public:
  6829. CRoxieServerHashDedupActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  6830. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorHashDedupArg &)basehelper), table(helper, *this), hashTableFilled(false), hashDedupTableIter(table)
  6831. {
  6832. eof = false;
  6833. keepBest = helper.keepBest();
  6834. }
  6835. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  6836. {
  6837. eof = false;
  6838. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  6839. }
  6840. virtual void onCreate(IHThorArg *_colocalParent)
  6841. {
  6842. CRoxieServerActivity::onCreate(colocalParent);
  6843. table.onCreate(ctx);
  6844. }
  6845. virtual void reset()
  6846. {
  6847. table.reset();
  6848. eof = false;
  6849. CRoxieServerActivity::reset();
  6850. hashTableFilled = false;
  6851. }
  6852. virtual const void *nextRow()
  6853. {
  6854. ActivityTimer t(activityStats, timeActivities);
  6855. if (keepBest)
  6856. {
  6857. if (eof)
  6858. return NULL;
  6859. // Populate hash table with best rows
  6860. if (!hashTableFilled)
  6861. {
  6862. const void * next = inputStream->nextRow();
  6863. while(next)
  6864. {
  6865. if (!table.insertBest(next))
  6866. ReleaseRoxieRow(next);
  6867. next = inputStream->nextRow();
  6868. }
  6869. if (table.count() == 0)
  6870. eof = true;
  6871. hashTableFilled = true;
  6872. hashDedupTableIter.first();
  6873. }
  6874. // Iterate throw hash table returning rows
  6875. if (hashDedupTableIter.isValid())
  6876. {
  6877. HashDedupElement &el = hashDedupTableIter.query();
  6878. const void * row = el.getRowClear();
  6879. hashDedupTableIter.next();
  6880. return row;
  6881. }
  6882. table.reset();
  6883. hashTableFilled = false;
  6884. return NULL;
  6885. }
  6886. else
  6887. {
  6888. while(!eof)
  6889. {
  6890. const void * next = inputStream->nextRow();
  6891. if(!next)
  6892. {
  6893. if (table.count() == 0)
  6894. eof = true;
  6895. table.reset();
  6896. return NULL;
  6897. }
  6898. if(table.insert(next))
  6899. return next;
  6900. else
  6901. ReleaseRoxieRow(next);
  6902. }
  6903. return NULL;
  6904. }
  6905. }
  6906. private:
  6907. bool keepBest;
  6908. bool hashTableFilled;
  6909. SuperHashIteratorOf<HashDedupElement> hashDedupTableIter;
  6910. };
  6911. CClassMeta<CRoxieServerHashDedupActivity::HashDedupElement> CRoxieServerHashDedupActivity::hashDedupElementMeta;
  6912. class CRoxieServerHashDedupActivityFactory : public CRoxieServerActivityFactory
  6913. {
  6914. public:
  6915. CRoxieServerHashDedupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  6916. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  6917. {
  6918. }
  6919. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  6920. {
  6921. return new CRoxieServerHashDedupActivity(_ctx, this, _probeManager);
  6922. }
  6923. };
  6924. IRoxieServerActivityFactory *createRoxieServerHashDedupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  6925. {
  6926. return new CRoxieServerHashDedupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  6927. }
  6928. //=================================================================================
  6929. class CRoxieServerRollupActivity : public CRoxieServerActivity
  6930. {
  6931. IHThorRollupArg &helper;
  6932. OwnedConstRoxieRow left;
  6933. OwnedConstRoxieRow prev;
  6934. OwnedConstRoxieRow right;
  6935. bool readFirstRow;
  6936. public:
  6937. CRoxieServerRollupActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  6938. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorRollupArg &)basehelper)
  6939. {
  6940. readFirstRow = false;
  6941. }
  6942. ~CRoxieServerRollupActivity()
  6943. {
  6944. }
  6945. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  6946. {
  6947. readFirstRow = false;
  6948. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  6949. }
  6950. virtual void reset()
  6951. {
  6952. left.clear();
  6953. prev.clear();
  6954. right.clear();
  6955. CRoxieServerActivity::reset();
  6956. }
  6957. virtual bool needsAllocator() const { return true; }
  6958. virtual const void * nextRow()
  6959. {
  6960. ActivityTimer t(activityStats, timeActivities);
  6961. if (!readFirstRow)
  6962. {
  6963. left.setown(inputStream->nextRow());
  6964. prev.set(left);
  6965. readFirstRow = true;
  6966. }
  6967. for (;;)
  6968. {
  6969. right.setown(inputStream->nextRow());
  6970. if(!prev || !right || !helper.matches(prev,right))
  6971. {
  6972. const void * ret = left.getClear();
  6973. if(ret)
  6974. processed++;
  6975. left.setown(right.getClear());
  6976. prev.set(left);
  6977. return ret;
  6978. }
  6979. try
  6980. {
  6981. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  6982. unsigned outSize = helper.transform(rowBuilder, left, right);
  6983. if (outSize)
  6984. left.setown(rowBuilder.finalizeRowClear(outSize));
  6985. if (helper.getFlags() & RFrolledismatchleft)
  6986. prev.set(left);
  6987. else
  6988. prev.set(right);
  6989. }
  6990. catch(IException * E)
  6991. {
  6992. throw makeWrappedException(E);
  6993. }
  6994. }
  6995. }
  6996. };
  6997. class CRoxieServerRollupActivityFactory : public CRoxieServerActivityFactory
  6998. {
  6999. public:
  7000. CRoxieServerRollupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7001. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  7002. {
  7003. }
  7004. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  7005. {
  7006. return new CRoxieServerRollupActivity(_ctx, this, _probeManager);
  7007. }
  7008. };
  7009. IRoxieServerActivityFactory *createRoxieServerRollupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7010. {
  7011. return new CRoxieServerRollupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  7012. }
  7013. //=================================================================================
  7014. class CRoxieServerNormalizeActivity : public CRoxieServerActivity
  7015. {
  7016. IHThorNormalizeArg &helper;
  7017. unsigned numThisRow;
  7018. unsigned curRow;
  7019. const void *buffer;
  7020. unsigned numProcessedLastGroup;
  7021. public:
  7022. CRoxieServerNormalizeActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  7023. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorNormalizeArg &)basehelper)
  7024. {
  7025. buffer = NULL;
  7026. numThisRow = 0;
  7027. curRow = 0;
  7028. numProcessedLastGroup = 0;
  7029. }
  7030. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  7031. {
  7032. numThisRow = 0;
  7033. curRow = 0;
  7034. numProcessedLastGroup = 0;
  7035. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  7036. }
  7037. virtual void reset()
  7038. {
  7039. ReleaseClearRoxieRow(buffer);
  7040. CRoxieServerActivity::reset();
  7041. }
  7042. virtual bool needsAllocator() const { return true; }
  7043. virtual const void * nextRow()
  7044. {
  7045. ActivityTimer t(activityStats, timeActivities);
  7046. for (;;)
  7047. {
  7048. while (curRow == numThisRow)
  7049. {
  7050. if (buffer)
  7051. ReleaseClearRoxieRow(buffer);
  7052. buffer = inputStream->nextRow();
  7053. if (!buffer && (processed == numProcessedLastGroup))
  7054. buffer = inputStream->nextRow();
  7055. if (!buffer)
  7056. {
  7057. numProcessedLastGroup = processed;
  7058. return NULL;
  7059. }
  7060. curRow = 0;
  7061. numThisRow = helper.numExpandedRows(buffer);
  7062. }
  7063. try
  7064. {
  7065. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  7066. unsigned actualSize = helper.transform(rowBuilder, buffer, ++curRow);
  7067. if (actualSize != 0)
  7068. {
  7069. processed++;
  7070. return rowBuilder.finalizeRowClear(actualSize);
  7071. }
  7072. }
  7073. catch (IException *E)
  7074. {
  7075. throw makeWrappedException(E);
  7076. }
  7077. }
  7078. }
  7079. };
  7080. class CRoxieServerNormalizeActivityFactory : public CRoxieServerActivityFactory
  7081. {
  7082. public:
  7083. CRoxieServerNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7084. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  7085. {
  7086. }
  7087. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  7088. {
  7089. return new CRoxieServerNormalizeActivity(_ctx, this, _probeManager);
  7090. }
  7091. };
  7092. IRoxieServerActivityFactory *createRoxieServerNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7093. {
  7094. return new CRoxieServerNormalizeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  7095. }
  7096. //=================================================================================
  7097. class CRoxieServerNormalizeChildActivity : public CRoxieServerActivity
  7098. {
  7099. IHThorNormalizeChildArg &helper;
  7100. unsigned numThisRow;
  7101. unsigned curRow;
  7102. const void *buffer;
  7103. unsigned numProcessedLastGroup;
  7104. INormalizeChildIterator * cursor;
  7105. const void * curChildRow;
  7106. bool advanceInput()
  7107. {
  7108. for (;;)
  7109. {
  7110. ReleaseClearRoxieRow(buffer);
  7111. buffer = inputStream->nextRow();
  7112. if (!buffer && (processed == numProcessedLastGroup))
  7113. buffer = inputStream->nextRow();
  7114. if (!buffer)
  7115. {
  7116. numProcessedLastGroup = processed;
  7117. return false;
  7118. }
  7119. curChildRow = cursor->first(buffer);
  7120. if (curChildRow)
  7121. {
  7122. curRow = 0;
  7123. return true;
  7124. }
  7125. }
  7126. }
  7127. public:
  7128. CRoxieServerNormalizeChildActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  7129. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorNormalizeChildArg &)basehelper)
  7130. {
  7131. buffer = NULL;
  7132. cursor = NULL;
  7133. numThisRow = 0;
  7134. curRow = 0;
  7135. numProcessedLastGroup = 0;
  7136. curChildRow = NULL;
  7137. }
  7138. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  7139. {
  7140. numThisRow = 0;
  7141. curRow = 0;
  7142. numProcessedLastGroup = 0;
  7143. curChildRow = NULL;
  7144. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  7145. cursor = helper.queryIterator();
  7146. }
  7147. virtual void stop()
  7148. {
  7149. CRoxieServerActivity::stop();
  7150. }
  7151. virtual void reset()
  7152. {
  7153. cursor = NULL;
  7154. ReleaseClearRoxieRow(buffer);
  7155. CRoxieServerActivity::reset();
  7156. }
  7157. virtual bool needsAllocator() const { return true; }
  7158. const void *nextRow()
  7159. {
  7160. ActivityTimer t(activityStats, timeActivities);
  7161. for (;;)
  7162. {
  7163. if (!buffer)
  7164. {
  7165. if (!advanceInput())
  7166. return NULL;
  7167. }
  7168. try
  7169. {
  7170. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  7171. size32_t outSize = helper.transform(rowBuilder, buffer, curChildRow, ++curRow);
  7172. curChildRow = cursor->next();
  7173. if (!curChildRow)
  7174. ReleaseClearRoxieRow(buffer);
  7175. if (outSize != 0)
  7176. {
  7177. processed++;
  7178. return rowBuilder.finalizeRowClear(outSize);
  7179. }
  7180. }
  7181. catch (IException *E)
  7182. {
  7183. throw makeWrappedException(E);
  7184. }
  7185. }
  7186. }
  7187. };
  7188. class CRoxieServerNormalizeChildActivityFactory : public CRoxieServerActivityFactory
  7189. {
  7190. public:
  7191. CRoxieServerNormalizeChildActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7192. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  7193. {
  7194. }
  7195. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  7196. {
  7197. return new CRoxieServerNormalizeChildActivity(_ctx, this, _probeManager);
  7198. }
  7199. };
  7200. IRoxieServerActivityFactory *createRoxieServerNormalizeChildActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7201. {
  7202. return new CRoxieServerNormalizeChildActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  7203. }
  7204. //=================================================================================
  7205. class CRoxieServerNormalizeLinkedChildActivity : public CRoxieServerActivity
  7206. {
  7207. IHThorNormalizeLinkedChildArg &helper;
  7208. OwnedConstRoxieRow curParent;
  7209. OwnedConstRoxieRow curChild;
  7210. unsigned numProcessedLastGroup;
  7211. bool advanceInput()
  7212. {
  7213. for (;;)
  7214. {
  7215. curParent.setown(inputStream->nextRow());
  7216. if (!curParent && (processed == numProcessedLastGroup))
  7217. curParent.setown(inputStream->nextRow());
  7218. if (!curParent)
  7219. {
  7220. numProcessedLastGroup = processed;
  7221. return false;
  7222. }
  7223. curChild.set(helper.first(curParent));
  7224. if (curChild)
  7225. return true;
  7226. }
  7227. }
  7228. public:
  7229. CRoxieServerNormalizeLinkedChildActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  7230. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorNormalizeLinkedChildArg &)basehelper)
  7231. {
  7232. numProcessedLastGroup = 0;
  7233. }
  7234. ~CRoxieServerNormalizeLinkedChildActivity()
  7235. {
  7236. }
  7237. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  7238. {
  7239. numProcessedLastGroup = 0;
  7240. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  7241. }
  7242. virtual void reset()
  7243. {
  7244. curParent.clear();
  7245. curChild.clear();
  7246. CRoxieServerActivity::reset();
  7247. }
  7248. const void *nextRow()
  7249. {
  7250. ActivityTimer t(activityStats, timeActivities);
  7251. for (;;)
  7252. {
  7253. if (!curParent)
  7254. {
  7255. if (!advanceInput())
  7256. return NULL;
  7257. }
  7258. try
  7259. {
  7260. const void *ret = curChild.getClear();
  7261. curChild.set(helper.next());
  7262. if (!curChild)
  7263. curParent.clear();
  7264. if (ret)
  7265. {
  7266. processed++;
  7267. return ret;
  7268. }
  7269. }
  7270. catch (IException *E)
  7271. {
  7272. throw makeWrappedException(E);
  7273. }
  7274. }
  7275. }
  7276. };
  7277. class CRoxieServerNormalizeLinkedChildActivityFactory : public CRoxieServerActivityFactory
  7278. {
  7279. public:
  7280. CRoxieServerNormalizeLinkedChildActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7281. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  7282. {
  7283. }
  7284. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  7285. {
  7286. return new CRoxieServerNormalizeLinkedChildActivity(_ctx, this, _probeManager);
  7287. }
  7288. };
  7289. IRoxieServerActivityFactory *createRoxieServerNormalizeLinkedChildActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7290. {
  7291. return new CRoxieServerNormalizeLinkedChildActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  7292. }
  7293. //=================================================================================
  7294. class CRoxieServerSortActivity : public CRoxieServerActivity
  7295. {
  7296. protected:
  7297. IHThorSortArg &helper;
  7298. ICompare *compare;
  7299. Owned<ISortAlgorithm> sorter;
  7300. bool readInput;
  7301. RoxieSortAlgorithm sortAlgorithm;
  7302. unsigned sortFlags;
  7303. public:
  7304. CRoxieServerSortActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, RoxieSortAlgorithm _sortAlgorithm, unsigned _sortFlags)
  7305. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorSortArg &)basehelper), sortAlgorithm(_sortAlgorithm), sortFlags(_sortFlags)
  7306. {
  7307. compare = helper.queryCompare();
  7308. readInput = false;
  7309. }
  7310. virtual void onCreate(IHThorArg *_colocalParent)
  7311. {
  7312. CRoxieServerActivity::onCreate(_colocalParent);
  7313. if (sortAlgorithm==unknownSortAlgorithm)
  7314. sorter.clear();
  7315. else
  7316. sorter.setown(createSortAlgorithm(sortAlgorithm, compare, ctx->queryRowManager(), meta, ctx->queryCodeContext(), tempDirectory, activityId));
  7317. }
  7318. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  7319. {
  7320. assertex(!readInput);
  7321. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  7322. }
  7323. virtual void reset()
  7324. {
  7325. if (sorter)
  7326. sorter->reset();
  7327. readInput = false;
  7328. CRoxieServerActivity::reset();
  7329. }
  7330. static RoxieSortAlgorithm useAlgorithm(const char *algorithmName, unsigned sortFlags)
  7331. {
  7332. RoxieSortAlgorithm sortAlgorithm = unknownSortAlgorithm;
  7333. if (!algorithmName)
  7334. algorithmName = "";
  7335. if (stricmp(algorithmName, "quicksort")==0)
  7336. {
  7337. switch (sortFlags & (TAFstable|TAFspill))
  7338. {
  7339. case 0: sortAlgorithm = quickSortAlgorithm; break;
  7340. case TAFstable: sortAlgorithm = stableQuickSortAlgorithm; break;
  7341. case TAFspill: sortAlgorithm = spillingQuickSortAlgorithm; break;
  7342. case TAFstable|TAFspill: sortAlgorithm = stableSpillingQuickSortAlgorithm; break;
  7343. }
  7344. }
  7345. else if (stricmp(algorithmName, "parquicksort")==0)
  7346. {
  7347. switch (sortFlags & TAFstable)
  7348. {
  7349. case 0: sortAlgorithm = parallelQuickSortAlgorithm; break;
  7350. case TAFstable: sortAlgorithm = parallelStableQuickSortAlgorithm; break;
  7351. default: throwUnexpected();
  7352. }
  7353. }
  7354. else if (stricmp(algorithmName, "heapsort")==0)
  7355. sortAlgorithm = heapSortAlgorithm; // NOTE - we do allow UNSTABLE('heapsort') in order to facilitate runtime selection. Also explicit selection of heapsort overrides request to spill
  7356. else if (stricmp(algorithmName, "mergesort")==0)
  7357. sortAlgorithm = (sortFlags & TAFspill) ? spillingMergeSortAlgorithm : mergeSortAlgorithm;
  7358. else if (stricmp(algorithmName, "parmergesort")==0)
  7359. sortAlgorithm = (sortFlags & TAFspill) ? spillingParallelMergeSortAlgorithm : parallelMergeSortAlgorithm;
  7360. #ifdef _USE_TBB
  7361. else if (stricmp(algorithmName, "tbbqsort")==0)
  7362. sortAlgorithm = tbbQuickSortAlgorithm;
  7363. else if (stricmp(algorithmName, "tbbstableqsort")==0)
  7364. sortAlgorithm = tbbStableQuickSortAlgorithm;
  7365. #endif
  7366. else
  7367. {
  7368. if (*algorithmName)
  7369. OWARNLOG(ROXIE_UNKNOWN_ALGORITHM, "Ignoring unsupported sort order algorithm '%s', using default", algorithmName);
  7370. if (sortFlags & TAFspill)
  7371. sortAlgorithm = ((sortFlags & TAFunstable) != 0) ? spillingQuickSortAlgorithm : stableSpillingQuickSortAlgorithm;
  7372. else if (sortFlags & TAFunstable)
  7373. sortAlgorithm = quickSortAlgorithm;
  7374. else
  7375. sortAlgorithm = heapSortAlgorithm;
  7376. }
  7377. return sortAlgorithm;
  7378. }
  7379. virtual const void * nextRow()
  7380. {
  7381. ActivityTimer t(activityStats, timeActivities);
  7382. if (!readInput)
  7383. {
  7384. if (sortAlgorithm == unknownSortAlgorithm)
  7385. {
  7386. sorter.clear();
  7387. OwnedRoxieString algorithmName(helper.getAlgorithm());
  7388. sortAlgorithm = useAlgorithm(algorithmName, sortFlags);
  7389. sorter.setown(createSortAlgorithm(sortAlgorithm, compare, ctx->queryRowManager(), meta, ctx->queryCodeContext(), tempDirectory, activityId));
  7390. }
  7391. sorter->prepare(inputStream);
  7392. noteStatistic(StTimeSortElapsed, cycle_to_nanosec(sorter->getElapsedCycles(true)));
  7393. readInput = true;
  7394. }
  7395. const void *ret = sorter->next();
  7396. if (ret)
  7397. processed++;
  7398. else
  7399. {
  7400. sorter->reset();
  7401. readInput = false; // ready for next group
  7402. }
  7403. return ret;
  7404. }
  7405. };
  7406. class CRoxieServerSortActivityFactory : public CRoxieServerActivityFactory
  7407. {
  7408. RoxieSortAlgorithm sortAlgorithm;
  7409. unsigned sortFlags;
  7410. public:
  7411. CRoxieServerSortActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7412. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  7413. {
  7414. Owned<IHThorSortArg> sortHelper = (IHThorSortArg *) helperFactory();
  7415. const char *algorithmName = NULL;
  7416. sortFlags = sortHelper->getAlgorithmFlags();
  7417. if (sortFlags & TAFconstant)
  7418. algorithmName = sortHelper->getAlgorithm();
  7419. bool forceSpill = _queryFactory.queryOptions().allSortsMaySpill || _graphNode.getPropBool("hint[@name='spill']/@value", false);
  7420. if (forceSpill)
  7421. sortFlags |= TAFspill;
  7422. if (!(sortFlags & TAFunstable))
  7423. sortFlags |= TAFstable; // Assume stable unless specifically requested otherwise
  7424. if (sortFlags & TAFconstant)
  7425. sortAlgorithm = CRoxieServerSortActivity::useAlgorithm(algorithmName, sortFlags);
  7426. else
  7427. sortAlgorithm = unknownSortAlgorithm;
  7428. }
  7429. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  7430. {
  7431. return new CRoxieServerSortActivity(_ctx, this, _probeManager, sortAlgorithm, sortFlags);
  7432. }
  7433. virtual const StatisticsMapping &queryStatsMapping() const
  7434. {
  7435. return sortStatistics;
  7436. }
  7437. };
  7438. IRoxieServerActivityFactory *createRoxieServerSortActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7439. {
  7440. return new CRoxieServerSortActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  7441. }
  7442. //=====================================================================================================
  7443. static int compareUint64(const void * _left, const void * _right)
  7444. {
  7445. const unsigned __int64 left = *static_cast<const unsigned __int64 *>(_left);
  7446. const unsigned __int64 right = *static_cast<const unsigned __int64 *>(_right);
  7447. if (left < right)
  7448. return -1;
  7449. if (left > right)
  7450. return -1;
  7451. return 0;
  7452. }
  7453. class CRoxieServerQuantileActivity : public CRoxieServerActivity
  7454. {
  7455. protected:
  7456. Owned<ISortAlgorithm> sorter;
  7457. IHThorQuantileArg &helper;
  7458. ConstPointerArray sorted;
  7459. ICompare *compare;
  7460. unsigned flags;
  7461. double skew;
  7462. unsigned __int64 numDivisions;
  7463. bool rangeIsAll;
  7464. size32_t rangeSize;
  7465. rtlDataAttr rangeValues;
  7466. bool calculated;
  7467. bool processedAny;
  7468. bool anyThisGroup;
  7469. bool eof;
  7470. unsigned curQuantile;
  7471. unsigned curIndex;
  7472. unsigned curIndexExtra;
  7473. unsigned skipSize;
  7474. unsigned skipExtra;
  7475. unsigned prevIndex;
  7476. public:
  7477. CRoxieServerQuantileActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _flags)
  7478. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorQuantileArg &)basehelper), flags(_flags)
  7479. {
  7480. compare = helper.queryCompare();
  7481. skew = 0.0;
  7482. numDivisions = 0;
  7483. calculated = false;
  7484. processedAny = false;
  7485. anyThisGroup = false;
  7486. eof = false;
  7487. rangeIsAll = true;
  7488. rangeSize = 0;
  7489. curQuantile = 0;
  7490. prevIndex = 0;
  7491. curIndex = 0;
  7492. curIndexExtra = 0;
  7493. skipSize = 0;
  7494. skipExtra = 0;
  7495. if (flags & TQFunstable)
  7496. sorter.setown(createQuickSortAlgorithm(compare));
  7497. else
  7498. sorter.setown(createMergeSortAlgorithm(compare));
  7499. }
  7500. virtual void reset()
  7501. {
  7502. sorter->reset();
  7503. calculated = false;
  7504. processedAny = false;
  7505. anyThisGroup = false;
  7506. ReleaseRoxieRows(sorted);
  7507. CRoxieServerActivity::reset();
  7508. }
  7509. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  7510. {
  7511. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  7512. skew = helper.getSkew();
  7513. numDivisions = helper.getNumDivisions();
  7514. //Check for -ve integer values and treat as out of range
  7515. if ((__int64)numDivisions < 1)
  7516. numDivisions = 1;
  7517. if (flags & TQFhasrange)
  7518. helper.getRange(rangeIsAll, rangeSize, rangeValues.refdata());
  7519. else
  7520. {
  7521. rangeIsAll = true;
  7522. rangeSize = 0;
  7523. }
  7524. if (rangeSize)
  7525. {
  7526. //Sort the range items into order to allow quick comparison
  7527. qsort(rangeValues.getdata(), rangeSize / sizeof(unsigned __int64), sizeof(unsigned __int64), compareUint64);
  7528. }
  7529. calculated = false;
  7530. processedAny = false;
  7531. anyThisGroup = false;
  7532. eof = false;
  7533. curQuantile = 0;
  7534. prevIndex = 0;
  7535. curIndex = 0;
  7536. curIndexExtra = 0;
  7537. }
  7538. virtual bool needsAllocator() const { return true; }
  7539. virtual const void * nextRow()
  7540. {
  7541. ActivityTimer t(activityStats, timeActivities);
  7542. if (eof)
  7543. return NULL;
  7544. const void * ret = NULL;
  7545. for(;;)
  7546. {
  7547. if (!calculated)
  7548. {
  7549. if (flags & TQFlocalsorted)
  7550. {
  7551. for (;;)
  7552. {
  7553. const void * next = inputStream->nextRow();
  7554. if (!next)
  7555. break;
  7556. sorted.append(next);
  7557. }
  7558. }
  7559. else
  7560. {
  7561. sorter->prepare(inputStream);
  7562. sorter->getSortedGroup(sorted);
  7563. }
  7564. if (sorted.ordinality() == 0)
  7565. {
  7566. if (processedAny)
  7567. {
  7568. eof = true;
  7569. return NULL;
  7570. }
  7571. //Unusual case 0 rows - add a default row instead
  7572. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  7573. size32_t thisSize = helper.createDefault(rowBuilder);
  7574. sorted.append(rowBuilder.finalizeRowClear(thisSize));
  7575. }
  7576. calculated = true;
  7577. processedAny = true;
  7578. anyThisGroup = false;
  7579. curQuantile = 0;
  7580. curIndex = 0;
  7581. curIndexExtra = (unsigned)((numDivisions-1) / 2); // to ensure correctly rounded up
  7582. prevIndex = curIndex-1; // Ensure it doesn't match
  7583. skipSize = (unsigned)(sorted.ordinality() / numDivisions);
  7584. skipExtra = (unsigned)(sorted.ordinality() % numDivisions);
  7585. }
  7586. if (isQuantileIncluded(curQuantile))
  7587. {
  7588. if (!(flags & TQFdedup) || (prevIndex != curIndex))
  7589. {
  7590. const void * lhs = sorted.item(curIndex);
  7591. if (flags & TQFneedtransform)
  7592. {
  7593. unsigned outSize;
  7594. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  7595. try
  7596. {
  7597. outSize = helper.transform(rowBuilder, lhs, curQuantile);
  7598. }
  7599. catch (IException *E)
  7600. {
  7601. throw makeWrappedException(E);
  7602. }
  7603. if (outSize)
  7604. ret = rowBuilder.finalizeRowClear(outSize);
  7605. }
  7606. else
  7607. {
  7608. LinkRoxieRow(lhs);
  7609. ret = lhs;
  7610. }
  7611. prevIndex = curIndex; // even if output was skipped?
  7612. }
  7613. }
  7614. curIndex += skipSize;
  7615. curIndexExtra += skipExtra;
  7616. if (curIndexExtra >= numDivisions)
  7617. {
  7618. curIndex++;
  7619. curIndexExtra -= numDivisions;
  7620. }
  7621. //Ensure the current index always stays valid.
  7622. if (curIndex >= sorted.ordinality())
  7623. curIndex = sorted.ordinality()-1;
  7624. curQuantile++;
  7625. if (curQuantile > numDivisions)
  7626. {
  7627. ReleaseRoxieRows(sorted);
  7628. sorter->reset();
  7629. calculated = false; // ready for next group
  7630. }
  7631. if (ret)
  7632. {
  7633. anyThisGroup = true;
  7634. processed++;
  7635. return ret;
  7636. }
  7637. if (curQuantile > numDivisions)
  7638. {
  7639. if (anyThisGroup)
  7640. return NULL;
  7641. }
  7642. }
  7643. }
  7644. bool isQuantileIncluded(unsigned quantile) const
  7645. {
  7646. if (quantile == 0)
  7647. return (flags & TQFfirst) != 0;
  7648. if (quantile == numDivisions)
  7649. return (flags & TQFlast) != 0;
  7650. if (rangeIsAll)
  7651. return true;
  7652. //Compare against the list of ranges provided
  7653. //MORE: Since the list is sorted should only need to compare the next (and allow for dups)
  7654. unsigned rangeNum = rangeSize / sizeof(unsigned __int64);
  7655. const unsigned __int64 * ranges = static_cast<const unsigned __int64 *>(rangeValues.getdata());
  7656. for (unsigned i = 0; i < rangeNum; i++)
  7657. {
  7658. if (ranges[i] == quantile)
  7659. return true;
  7660. }
  7661. return false;
  7662. }
  7663. };
  7664. class CRoxieServerQuantileActivityFactory : public CRoxieServerActivityFactory
  7665. {
  7666. unsigned flags;
  7667. public:
  7668. CRoxieServerQuantileActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7669. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  7670. {
  7671. Owned<IHThorQuantileArg> quantileHelper = (IHThorQuantileArg *) helperFactory();
  7672. flags = quantileHelper->getFlags();
  7673. }
  7674. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  7675. {
  7676. return new CRoxieServerQuantileActivity(_ctx, this, _probeManager, flags);
  7677. }
  7678. };
  7679. IRoxieServerActivityFactory *createRoxieServerQuantileActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7680. {
  7681. return new CRoxieServerQuantileActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  7682. }
  7683. //=====================================================================================================
  7684. class CRoxieServerSortedActivity : public CRoxieServerActivity
  7685. {
  7686. IHThorSortedArg &helper;
  7687. ICompare * compare;
  7688. const void *prev;
  7689. IRangeCompare * stepCompare;
  7690. public:
  7691. CRoxieServerSortedActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  7692. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorSortedArg &)basehelper)
  7693. {
  7694. prev = NULL;
  7695. compare = helper.queryCompare();
  7696. stepCompare = NULL;
  7697. }
  7698. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  7699. {
  7700. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  7701. IInputSteppingMeta * stepMeta = input->querySteppingMeta();
  7702. if (stepMeta)
  7703. stepCompare = stepMeta->queryCompare();
  7704. prev = NULL;
  7705. }
  7706. virtual void reset()
  7707. {
  7708. ReleaseClearRoxieRow(prev);
  7709. CRoxieServerActivity::reset();
  7710. }
  7711. virtual const void * nextRow()
  7712. {
  7713. ActivityTimer t(activityStats, timeActivities);
  7714. const void *ret = inputStream->nextRow();
  7715. if (ret && prev && compare->docompare(prev, ret) > 0)
  7716. {
  7717. // MORE - better to give mismatching rows that indexes?
  7718. throw MakeStringException(ROXIE_NOT_SORTED, "SORTED(%u) detected incorrectly sorted rows (row %d, %d))", activityId, processed, processed+1);
  7719. }
  7720. ReleaseRoxieRow(prev);
  7721. prev = ret;
  7722. if (ret)
  7723. {
  7724. LinkRoxieRow(prev);
  7725. processed++;
  7726. }
  7727. return ret;
  7728. }
  7729. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  7730. {
  7731. ActivityTimer t(activityStats, timeActivities);
  7732. const void *ret = inputStream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  7733. if (ret && prev && compare->docompare(prev, ret) > 0)
  7734. {
  7735. // MORE - better to give mismatching rows that indexes?
  7736. throw MakeStringException(ROXIE_NOT_SORTED, "SORTED(%u) detected incorrectly sorted rows (row %d, %d))", activityId, processed, processed+1);
  7737. }
  7738. ReleaseRoxieRow(prev);
  7739. prev = ret;
  7740. if (ret)
  7741. {
  7742. LinkRoxieRow(prev);
  7743. processed++;
  7744. }
  7745. return ret;
  7746. }
  7747. IInputSteppingMeta * querySteppingMeta()
  7748. {
  7749. return input->querySteppingMeta();
  7750. }
  7751. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  7752. {
  7753. return input->gatherConjunctions(collector);
  7754. }
  7755. virtual void resetEOF()
  7756. {
  7757. inputStream->resetEOF();
  7758. }
  7759. };
  7760. class CRoxieServerSortedActivityFactory : public CRoxieServerActivityFactory
  7761. {
  7762. public:
  7763. CRoxieServerSortedActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7764. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  7765. {
  7766. }
  7767. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  7768. {
  7769. return new CRoxieServerSortedActivity(_ctx, this, _probeManager);
  7770. }
  7771. };
  7772. IRoxieServerActivityFactory *createRoxieServerSortedActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  7773. {
  7774. return new CRoxieServerSortedActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  7775. }
  7776. //=====================================================================================================
  7777. class CRoxieServerThroughSpillActivity : public CRoxieServerActivity
  7778. {
  7779. /*
  7780. BE VERY CAREFUL - this code is tricky.
  7781. Note that starts and stops (and resets) can occur in strange orders
  7782. The FIRST start OR stop must initialize the activity but only the first START should call the upstream start.
  7783. The last stop should call the upstream stop.
  7784. The first reset should call the upstream reset.
  7785. 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
  7786. not yet had start or stop called - for this to happen init() is called on all outputs on the first start or stop.
  7787. Some outputs may be completely pruned away when used in a GRAPH - these outputs should not receive any start/stop/reset and should be
  7788. ignored in the minIndex calculation
  7789. */
  7790. public:
  7791. IHThorArg &helper;
  7792. unsigned activeOutputs;
  7793. unsigned numOutputs;
  7794. unsigned numOriginalOutputs;
  7795. QueueOf<const void, true> buffer;
  7796. CriticalSection crit;
  7797. CriticalSection crit2;
  7798. unsigned tailIdx;
  7799. unsigned headIdx;
  7800. Owned<IException> readError;
  7801. Owned<IException> startError;
  7802. class OutputAdaptor : implements IEngineRowStream, implements IFinalRoxieInput, public CInterface
  7803. {
  7804. bool eof, eofpending, stopped;
  7805. public:
  7806. CRoxieServerThroughSpillActivity *parent;
  7807. unsigned idx;
  7808. unsigned oid;
  7809. unsigned processed = 0;
  7810. unsigned numStarts = 0;
  7811. cycle_t totalCycles; // We track this per output so that the pullers get a meaningful value to use when calculating their localtime
  7812. public:
  7813. IMPLEMENT_IINTERFACE;
  7814. OutputAdaptor()
  7815. {
  7816. parent = NULL;
  7817. oid = 0;
  7818. idx = 0;
  7819. processed = 0;
  7820. totalCycles = 0;
  7821. eofpending = false;
  7822. eof = false;
  7823. stopped = false;
  7824. }
  7825. ~OutputAdaptor()
  7826. {
  7827. if (traceStartStop)
  7828. DBGLOG("%p ~OutputAdaptor %d", this, oid);
  7829. }
  7830. void init()
  7831. {
  7832. if (traceStartStop)
  7833. DBGLOG("%p init Input adaptor %d", this, oid);
  7834. idx = 0;
  7835. eofpending = false;
  7836. eof = false;
  7837. stopped = false;
  7838. }
  7839. virtual IEngineRowStream *queryConcreteOutputStream(unsigned whichInput) { assertex(idx==0); return this; }
  7840. virtual IStrandJunction *queryConcreteOutputJunction(unsigned idx) const { assertex(idx==0); return nullptr; }
  7841. virtual IRoxieServerActivity *queryActivity()
  7842. {
  7843. return parent;
  7844. }
  7845. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  7846. {
  7847. return parent->queryIndexReadActivity();
  7848. }
  7849. virtual unsigned __int64 queryTotalCycles() const
  7850. {
  7851. return totalCycles;
  7852. }
  7853. virtual const void * nextRow()
  7854. {
  7855. SimpleActivityTimer t(totalCycles, parent->timeActivities);
  7856. if (eof)
  7857. return NULL;
  7858. const void *ret = parent->readBuffered(idx, oid);
  7859. #ifdef TRACE_SPLIT
  7860. parent->CTXLOG("Adaptor %d got back %p for record %d", oid, ret, idx);
  7861. #endif
  7862. if (ret)
  7863. {
  7864. processed++;
  7865. eofpending = false;
  7866. }
  7867. else if (eofpending)
  7868. eof = true;
  7869. else
  7870. eofpending = true;
  7871. return ret;
  7872. }
  7873. virtual IOutputMetaData * queryOutputMeta() const
  7874. {
  7875. return parent->queryOutputMeta();
  7876. }
  7877. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  7878. {
  7879. // NOTE: it is tempting to move the init() of all output adaptors here. However that is not a good idea,
  7880. // since adaptors that have not yet started or stopped (but are going to) still need to have been init()'ed
  7881. // for minIndex to give the correct answers
  7882. // therefore, we call init() on all adaptors on receipt of the first start() or stop()
  7883. if (traceStartStop)
  7884. parent->CTXLOG("%p start Input adaptor %d stopped = %d", this, oid, stopped);
  7885. parent->start(oid, parentExtractSize, parentExtract, paused);
  7886. numStarts++;
  7887. }
  7888. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks) override
  7889. {
  7890. parent->connectInputStreams(consumerOrdered);
  7891. streams.append(this);
  7892. return NULL;
  7893. }
  7894. virtual void stop()
  7895. {
  7896. if (traceStartStop)
  7897. parent->CTXLOG("%p stop Input adaptor %d stopped = %d", this, oid, stopped);
  7898. if (!stopped)
  7899. {
  7900. parent->stop(oid, idx); // NOTE - may call init()
  7901. stopped = true; // parent code relies on stop being called exactly once per adaptor, so make sure it is!
  7902. }
  7903. };
  7904. virtual void reset()
  7905. {
  7906. if (traceStartStop)
  7907. parent->CTXLOG("%p reset Input adaptor %d stopped = %d", this, oid, stopped);
  7908. parent->reset(oid);
  7909. idx = 0; // value should not be relevant really but this is the safest...
  7910. stopped = false;
  7911. };
  7912. virtual void resetEOF()
  7913. {
  7914. parent->resetEOF();
  7915. }
  7916. virtual void checkAbort()
  7917. {
  7918. parent->checkAbort();
  7919. }
  7920. } *adaptors;
  7921. bool *used;
  7922. unsigned minIndex(unsigned exceptOid)
  7923. {
  7924. // MORE - yukky code (and slow). Could keep them heapsorted by idx or something
  7925. // this is trying to determine whether any of the adaptors will in the future read a given record
  7926. unsigned minIdx = (unsigned) -1;
  7927. for (unsigned i = 0; i < numOriginalOutputs; i++)
  7928. {
  7929. if (i != exceptOid && used[i] && adaptors[i].idx < minIdx)
  7930. minIdx = adaptors[i].idx;
  7931. }
  7932. return minIdx;
  7933. }
  7934. inline bool isLastTailReader(unsigned exceptOid)
  7935. {
  7936. for (unsigned i = 0; i < numOriginalOutputs; i++)
  7937. {
  7938. if (i != exceptOid && adaptors[i].idx == tailIdx && used[i])
  7939. return false;
  7940. }
  7941. return true;
  7942. }
  7943. void initOutputs()
  7944. {
  7945. activeOutputs = numOutputs;
  7946. for (unsigned i = 0; i < numOriginalOutputs; i++)
  7947. if (used[i])
  7948. adaptors[i].init();
  7949. state = STATEstarting;
  7950. }
  7951. public:
  7952. CRoxieServerThroughSpillActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numOutputs)
  7953. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper(basehelper), numOutputs(_numOutputs)
  7954. {
  7955. numOriginalOutputs = numOutputs;
  7956. adaptors = new OutputAdaptor[numOutputs];
  7957. used = new bool[numOutputs];
  7958. for (unsigned i = 0; i < numOutputs; i++)
  7959. {
  7960. adaptors[i].parent = this;
  7961. adaptors[i].oid = i;
  7962. used[i] = false;
  7963. }
  7964. tailIdx = 0;
  7965. headIdx = 0;
  7966. activeOutputs = numOutputs;
  7967. }
  7968. ~CRoxieServerThroughSpillActivity()
  7969. {
  7970. delete [] adaptors;
  7971. delete [] used;
  7972. }
  7973. const void *readBuffered(unsigned &idx, unsigned oid)
  7974. {
  7975. //False positives are fine, false negatives are not.. so headIdx must only be updated when a row will be available.
  7976. const unsigned curIdx = idx;
  7977. if (curIdx == headIdx) // test once without getting the crit2 sec
  7978. {
  7979. CriticalBlock b2(crit2); // but only one puller gets to read the head
  7980. if (curIdx == headIdx) // test again now that we have it
  7981. {
  7982. ActivityTimer t(activityStats, timeActivities); // NOTE - time spent waiting for crit not included here. But it will have been included on the totalTime of the person holding the crit, so that is right
  7983. if (readError)
  7984. throw readError.getLink(); // Read exceptions are only throw if you try to read past the row where they happened
  7985. try
  7986. {
  7987. const void *row = inputStream->nextRow();
  7988. if (activeOutputs==1)
  7989. {
  7990. #ifdef TRACE_SPLIT
  7991. CTXLOG("spill %d optimised return of %p", activityId, row);
  7992. #endif
  7993. headIdx++;
  7994. idx++;
  7995. return row; // optimization for the case where only one output still active.
  7996. }
  7997. CriticalBlock b(crit);
  7998. headIdx++;
  7999. idx++;
  8000. if (activeOutputs==1)
  8001. return row; // optimization for the case where only one output still active.
  8002. buffer.enqueue(row);
  8003. if (row) LinkRoxieRow(row);
  8004. return row;
  8005. }
  8006. catch (IException *E)
  8007. {
  8008. #ifdef TRACE_SPLIT
  8009. CTXLOG("spill %d caught exception", activityId);
  8010. #endif
  8011. readError.set(E);
  8012. throw;
  8013. }
  8014. catch (...)
  8015. {
  8016. IException *E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught in CRoxieServerThroughSpillActivity::readBuffered");
  8017. readError.set(E);
  8018. throw E;
  8019. }
  8020. }
  8021. }
  8022. CriticalBlock b(crit);
  8023. ActivityTimer t(activityStats, timeActivities); // NOTE - time spent waiting for crit not included here. But it will have been included on the totalTime of the person holding the crit, so that is right
  8024. unsigned lidx = curIdx - tailIdx;
  8025. idx++;
  8026. if (!lidx)
  8027. {
  8028. // The numOutputs <= 2 check optimizes slightly the common case of 2-way splitters.
  8029. // The rationale is that I MUST be the last reader if there are only 2 - the other guy must have put the record there in the first place
  8030. if (numOutputs <= 2 || isLastTailReader(oid)) // NOTE - this includes the case where minIndex returns (unsigned) -1, meaning no other active pullers
  8031. {
  8032. tailIdx++;
  8033. const void *ret = buffer.dequeue(); // no need to link - last puller
  8034. #ifdef TRACE_SPLIT
  8035. CTXLOG("last puller return of %p", ret);
  8036. #endif
  8037. return ret;
  8038. }
  8039. }
  8040. const void *ret = buffer.item(lidx);
  8041. if (ret) LinkRoxieRow(ret);
  8042. #ifdef TRACE_SPLIT
  8043. CTXLOG("standard return of %p", ret);
  8044. #endif
  8045. return ret;
  8046. }
  8047. virtual void start(unsigned oid, unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8048. {
  8049. CriticalBlock b(crit);
  8050. if (startError)
  8051. throw startError.getLink();
  8052. if (collectFactoryStatistics)
  8053. factory->noteStarted(oid);
  8054. if (traceStartStop)
  8055. CTXLOG("SPLIT %p: start %d child %d activeOutputs %d numOutputs %d numOriginalOutputs %d state %s", this, activityId, oid, activeOutputs, numOutputs, numOriginalOutputs, queryStateText(state));
  8056. if (state != STATEstarted)
  8057. {
  8058. if (state != STATEstarting)
  8059. initOutputs();
  8060. tailIdx = 0;
  8061. headIdx = 0;
  8062. startError.clear();
  8063. readError.clear();
  8064. try
  8065. {
  8066. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  8067. }
  8068. catch (IException *E)
  8069. {
  8070. #ifdef TRACE_SPLIT
  8071. CTXLOG("spill %d caught exception in start", activityId);
  8072. #endif
  8073. startError.set(E);
  8074. throw;
  8075. }
  8076. catch (...)
  8077. {
  8078. IException *E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught in CRoxieServerThroughSpillActivity::start");
  8079. startError.set(E);
  8080. throw E;
  8081. }
  8082. }
  8083. }
  8084. void stop(unsigned oid, unsigned & idx)
  8085. {
  8086. // Note that OutputAdaptor code ensures that stop is not called more than once per adaptor
  8087. CriticalBlock b(crit);
  8088. #ifdef TRACE_STARTSTOP
  8089. if (traceStartStop)
  8090. {
  8091. CTXLOG("SPLIT %p: stop %d child %d activeOutputs %d numOutputs %d numOriginalOutputs %d state %s", this, activityId, oid, activeOutputs, numOutputs, numOriginalOutputs, queryStateText(state));
  8092. if (watchActivityId && watchActivityId==activityId)
  8093. {
  8094. CTXLOG("WATCH: stop %d", activityId);
  8095. }
  8096. }
  8097. #endif
  8098. if (state != STATEstarting && state != STATEstarted)
  8099. initOutputs();
  8100. if (activeOutputs > 1)
  8101. {
  8102. if (tailIdx==idx)
  8103. {
  8104. // Discard all buffered rows that are there purely for this adaptor to read them
  8105. unsigned min = minIndex(oid);
  8106. if (min != (unsigned) -1)
  8107. // what does -1 signify?? No-one wants anything? In which case can't we kill all rows??
  8108. // Should never happen though if there are still some active.
  8109. // there may be a small window where adaptors are blocked on the semaphore...
  8110. {
  8111. #ifdef TRACE_SPLIT
  8112. CTXLOG("%p: Discarding buffered rows from %d to %d for oid %x (%d outputs active)", this, idx, min, oid, activeOutputs);
  8113. #endif
  8114. while (tailIdx < min)
  8115. {
  8116. ReleaseRoxieRow(buffer.dequeue());
  8117. tailIdx++;
  8118. }
  8119. }
  8120. }
  8121. activeOutputs--;
  8122. idx = (unsigned) -1; // causes minIndex not to save rows for me...
  8123. return;
  8124. }
  8125. #ifdef TRACE_SPLIT
  8126. CTXLOG("%p: All outputs done", this);
  8127. #endif
  8128. activeOutputs = numOutputs;
  8129. CRoxieServerActivity::stop();
  8130. };
  8131. void reset(unsigned oid)
  8132. {
  8133. if (traceStartStop)
  8134. CTXLOG("SPLIT %p: reset %d child %d activeOutputs %d numOutputs %d numOriginalOutputs %d state %s", this, activityId, oid, activeOutputs, numOutputs, numOriginalOutputs, queryStateText(state));
  8135. activeOutputs = numOutputs;
  8136. while (buffer.ordinality())
  8137. ReleaseRoxieRow(buffer.dequeue());
  8138. startError.clear();
  8139. readError.clear();
  8140. if (state != STATEreset) // make sure input is only reset once
  8141. CRoxieServerActivity::reset();
  8142. };
  8143. void connectInputStreams(bool consumerOrdered)
  8144. {
  8145. if (!inputStream)
  8146. CRoxieServerActivity::connectInputStreams(consumerOrdered);
  8147. }
  8148. virtual const void *nextRow()
  8149. {
  8150. throwUnexpected(); // Internal logic error - we are not anybody's input
  8151. }
  8152. virtual IOutputMetaData * queryOutputMeta() const
  8153. {
  8154. // if (outputMeta)
  8155. // return outputMeta;
  8156. // else
  8157. return input->queryOutputMeta(); // not always known (e.g. disk write - though Gavin _could_ fill it in)
  8158. }
  8159. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  8160. {
  8161. assertex(idx < numOriginalOutputs);
  8162. assertex(!used[idx]);
  8163. used[idx] = true;
  8164. return &adaptors[idx];
  8165. }
  8166. virtual void resetOutputsUsed()
  8167. {
  8168. numOutputs = 1;
  8169. activeOutputs = 1;
  8170. // MORE RKC->GH should we be clearing the used array here? anywhere?
  8171. }
  8172. virtual void noteOutputUsed()
  8173. {
  8174. assertex(numOutputs < numOriginalOutputs);
  8175. numOutputs++;
  8176. activeOutputs = numOutputs;
  8177. }
  8178. virtual bool isPassThrough()
  8179. {
  8180. return numOutputs==1;
  8181. }
  8182. virtual void updateEdgeStats(IStatisticGatherer * statsBuilder) const
  8183. {
  8184. for (unsigned i = 0; i < numOutputs; i++)
  8185. addEdgeStats(statsBuilder, i, adaptors[i].numStarts, adaptors[i].processed, 0);
  8186. }
  8187. };
  8188. class CRoxieServerThroughSpillActivityFactory : public CRoxieServerMultiOutputFactory
  8189. {
  8190. public:
  8191. CRoxieServerThroughSpillActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  8192. : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  8193. {
  8194. Owned<IHThorSpillArg> helper = (IHThorSpillArg *) helperFactory();
  8195. setNumOutputs(helper->getTempUsageCount() + 1);
  8196. }
  8197. CRoxieServerThroughSpillActivityFactory(IQueryFactory &_queryFactory, HelperFactory *_helperFactory, unsigned _numOutputs, IPropertyTree &_graphNode)
  8198. : CRoxieServerMultiOutputFactory(0, 0, _queryFactory, _helperFactory, TAKsplit, _graphNode)
  8199. {
  8200. setNumOutputs(_numOutputs);
  8201. }
  8202. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  8203. {
  8204. return new CRoxieServerThroughSpillActivity(_ctx, this, _probeManager, numOutputs);
  8205. }
  8206. };
  8207. IRoxieServerActivityFactory *createRoxieServerThroughSpillActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  8208. {
  8209. return new CRoxieServerThroughSpillActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  8210. }
  8211. IRoxieServerActivityFactory *createRoxieServerThroughSpillActivityFactory(IQueryFactory &_queryFactory, HelperFactory *_factory, unsigned _numOutputs, IPropertyTree &_graphNode)
  8212. {
  8213. return new CRoxieServerThroughSpillActivityFactory(_queryFactory, _factory, _numOutputs, _graphNode);
  8214. }
  8215. //----------------------------------------------------------------------------------------------
  8216. class CRoxieServerSplitActivityFactory : public CRoxieServerMultiOutputFactory
  8217. {
  8218. public:
  8219. CRoxieServerSplitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  8220. : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  8221. {
  8222. Owned<IHThorSplitArg> helper = (IHThorSplitArg *) helperFactory();
  8223. setNumOutputs(helper->numBranches());
  8224. }
  8225. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  8226. {
  8227. return new CRoxieServerThroughSpillActivity(_ctx, this, _probeManager, numOutputs);
  8228. }
  8229. };
  8230. IRoxieServerActivityFactory *createRoxieServerSplitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  8231. {
  8232. return new CRoxieServerSplitActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  8233. }
  8234. //=====================================================================================================
  8235. #define PIPE_BUFSIZE 0x8000
  8236. static IException *createPipeFailureException(const char *cmd, unsigned retcode, IPipeProcess *pipe)
  8237. {
  8238. StringBuffer msg;
  8239. if(pipe->hasError())
  8240. {
  8241. try
  8242. {
  8243. char error[512];
  8244. size32_t sz = pipe->readError(sizeof(error), error);
  8245. if(sz && sz!=(size32_t)-1)
  8246. msg.append(", stderr: '").append(sz, error).append("'");
  8247. }
  8248. catch (IException *e)
  8249. {
  8250. EXCLOG(e, "Error reading pipe stderr");
  8251. e->Release();
  8252. }
  8253. }
  8254. return MakeStringException(ROXIE_PIPE_ERROR, "Pipe process %s returned error %u%s", cmd, retcode, msg.str());
  8255. }
  8256. class CRoxieServerPipeReadActivity : public CRoxieServerActivity
  8257. {
  8258. IHThorPipeReadArg &helper;
  8259. Owned<IPipeProcess> pipe;
  8260. StringAttr pipeCommand;
  8261. Owned<IOutputRowDeserializer> rowDeserializer;
  8262. Owned<IReadRowStream> readTransformer;
  8263. bool groupSignalled;
  8264. public:
  8265. CRoxieServerPipeReadActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8266. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorPipeReadArg &)basehelper)
  8267. {
  8268. groupSignalled = true;
  8269. }
  8270. virtual bool needsAllocator() const { return true; }
  8271. virtual void onCreate(IHThorArg *_colocalParent)
  8272. {
  8273. CRoxieServerActivity::onCreate(_colocalParent);
  8274. rowDeserializer.setown(rowAllocator->createDiskDeserializer(ctx->queryCodeContext()));
  8275. }
  8276. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8277. {
  8278. groupSignalled = true; // i.e. don't start with a NULL row
  8279. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  8280. if (!readTransformer)
  8281. {
  8282. OwnedRoxieString xmlIteratorPath(helper.getXmlIteratorPath());
  8283. readTransformer.setown(createReadRowStream(rowAllocator, rowDeserializer, helper.queryXmlTransformer(), helper.queryCsvTransformer(), xmlIteratorPath, helper.getPipeFlags()));
  8284. }
  8285. OwnedRoxieString pipeProgram(helper.getPipeProgram());
  8286. openPipe(pipeProgram);
  8287. }
  8288. virtual void stop()
  8289. {
  8290. CRoxieServerActivity::stop();
  8291. pipe.clear();
  8292. readTransformer->setStream(NULL);
  8293. }
  8294. virtual const void *nextRow()
  8295. {
  8296. ActivityTimer t(activityStats, timeActivities);
  8297. while (!waitForPipe())
  8298. {
  8299. if (!pipe)
  8300. return NULL;
  8301. if (helper.getPipeFlags() & TPFgroupeachrow)
  8302. {
  8303. if (!groupSignalled)
  8304. {
  8305. groupSignalled = true;
  8306. return NULL;
  8307. }
  8308. }
  8309. }
  8310. const void *ret = readTransformer->next();
  8311. assertex(ret != NULL); // if ret can ever be NULL then we need to recode this logic
  8312. processed++;
  8313. groupSignalled = false;
  8314. return ret;
  8315. }
  8316. protected:
  8317. bool waitForPipe()
  8318. {
  8319. if (!pipe)
  8320. return false; // done
  8321. if (!readTransformer->eos())
  8322. return true;
  8323. verifyPipe();
  8324. return false;
  8325. }
  8326. void openPipe(char const * cmd)
  8327. {
  8328. pipeCommand.setown(cmd);
  8329. pipe.setown(createPipeProcess());
  8330. if(!pipe->run(NULL, cmd, ".", false, true, true, 0x10000))
  8331. {
  8332. // NB: pipe->run can't rely on the child process failing fast enough to return false here, failure picked up later with stderr context.
  8333. WARNLOG(ROXIE_PIPE_ERROR, "Could not run pipe process %s", cmd);
  8334. }
  8335. Owned<ISimpleReadStream> pipeReader = pipe->getOutputStream();
  8336. readTransformer->setStream(pipeReader.get());
  8337. }
  8338. void verifyPipe()
  8339. {
  8340. if (pipe)
  8341. {
  8342. unsigned err = pipe->wait();
  8343. if(err && !(helper.getPipeFlags() & TPFnofail))
  8344. {
  8345. throw createPipeFailureException(pipeCommand.get(), err, pipe);
  8346. }
  8347. pipe.clear();
  8348. }
  8349. }
  8350. };
  8351. class CRoxieServerPipeThroughActivity : public CRoxieServerActivity, implements IRecordPullerCallback
  8352. {
  8353. IHThorPipeThroughArg &helper;
  8354. RecordPullerThread puller;
  8355. Owned<IPipeProcess> pipe;
  8356. StringAttr pipeCommand;
  8357. InterruptableSemaphore pipeVerified;
  8358. InterruptableSemaphore pipeOpened;
  8359. CachedOutputMetaData inputMeta;
  8360. Owned<IOutputRowSerializer> rowSerializer;
  8361. Owned<IOutputRowDeserializer> rowDeserializer;
  8362. Owned<IPipeWriteXformHelper> writeTransformer;
  8363. Owned<IReadRowStream> readTransformer;
  8364. bool firstRead;
  8365. bool recreate;
  8366. bool inputExhausted;
  8367. bool groupSignalled;
  8368. public:
  8369. CRoxieServerPipeThroughActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8370. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorPipeThroughArg &)basehelper), puller(false)
  8371. {
  8372. recreate = helper.recreateEachRow();
  8373. groupSignalled = true;
  8374. firstRead = false;
  8375. inputExhausted = false;
  8376. }
  8377. virtual bool needsAllocator() const { return true; }
  8378. virtual void onCreate(IHThorArg *_colocalParent)
  8379. {
  8380. CRoxieServerActivity::onCreate(_colocalParent);
  8381. rowSerializer.setown(inputMeta.createDiskSerializer(ctx->queryCodeContext(), activityId));
  8382. rowDeserializer.setown(rowAllocator->createDiskDeserializer(ctx->queryCodeContext()));
  8383. writeTransformer.setown(createPipeWriteXformHelper(helper.getPipeFlags(), helper.queryXmlOutput(), helper.queryCsvOutput(), rowSerializer));
  8384. }
  8385. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8386. {
  8387. firstRead = true;
  8388. inputExhausted = false;
  8389. groupSignalled = true; // i.e. don't start with a NULL row
  8390. pipeVerified.reinit();
  8391. pipeOpened.reinit();
  8392. writeTransformer->ready();
  8393. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  8394. if (!readTransformer)
  8395. {
  8396. OwnedRoxieString xmlIterator(helper.getXmlIteratorPath());
  8397. readTransformer.setown(createReadRowStream(rowAllocator, rowDeserializer, helper.queryXmlTransformer(), helper.queryCsvTransformer(), xmlIterator, helper.getPipeFlags()));
  8398. }
  8399. if(!recreate)
  8400. {
  8401. OwnedRoxieString pipeProgram(helper.getPipeProgram());
  8402. openPipe(pipeProgram);
  8403. }
  8404. puller.start(parentExtractSize, parentExtract, paused, 0, false, ctx); // Pipe does not support preload presently - locks up
  8405. }
  8406. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  8407. {
  8408. if (idx)
  8409. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", sanitizeSourceFile(__FILE__), __LINE__);
  8410. puller.setInput(this, _sourceIdx, _in);
  8411. inputMeta.set(_in->queryOutputMeta());
  8412. }
  8413. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks)
  8414. {
  8415. puller.connectInputStreams(ctx, consumerOrdered);
  8416. return CRoxieServerActivity::getOutputStreams(ctx, idx, streams, consumerOptions, consumerOrdered, orderedCallbacks);
  8417. }
  8418. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  8419. {
  8420. if (idx==0)
  8421. return puller.queryInput();
  8422. else
  8423. return NULL;
  8424. }
  8425. virtual void stop()
  8426. {
  8427. pipeVerified.interrupt(NULL);
  8428. pipeOpened.interrupt(NULL);
  8429. puller.stop();
  8430. CRoxieServerActivity::stop();
  8431. pipe.clear();
  8432. if (readTransformer)
  8433. readTransformer->setStream(NULL);
  8434. }
  8435. virtual void reset()
  8436. {
  8437. puller.reset();
  8438. CRoxieServerActivity::reset();
  8439. }
  8440. virtual const void *nextRow()
  8441. {
  8442. ActivityTimer t(activityStats, timeActivities);
  8443. while (!waitForPipe())
  8444. {
  8445. if (!pipe)
  8446. return NULL;
  8447. if (helper.getPipeFlags() & TPFgroupeachrow)
  8448. {
  8449. if (!groupSignalled)
  8450. {
  8451. groupSignalled = true;
  8452. return NULL;
  8453. }
  8454. }
  8455. }
  8456. const void *ret = readTransformer->next();
  8457. assertex(ret != NULL); // if ret can ever be NULL then we need to recode this logic
  8458. processed++;
  8459. groupSignalled = false;
  8460. return ret;
  8461. }
  8462. virtual void processRow(const void *row)
  8463. {
  8464. // called from puller thread
  8465. if(recreate)
  8466. openPipe(helper.getNameFromRow(row));
  8467. writeTransformer->writeTranslatedText(row, pipe);
  8468. ReleaseRoxieRow(row);
  8469. if(recreate)
  8470. {
  8471. closePipe();
  8472. pipeVerified.wait();
  8473. }
  8474. }
  8475. virtual void processDone()
  8476. {
  8477. // called from puller thread
  8478. if(recreate)
  8479. {
  8480. inputExhausted = true;
  8481. pipeOpened.signal();
  8482. }
  8483. else
  8484. {
  8485. closePipe();
  8486. pipeVerified.wait();
  8487. }
  8488. }
  8489. virtual void processEOG()
  8490. {
  8491. }
  8492. void processGroup(const ConstPointerArray &)
  8493. {
  8494. throwUnexpected();
  8495. }
  8496. virtual bool fireException(IException *e)
  8497. {
  8498. pipeOpened.interrupt(LINK(e));
  8499. pipeVerified.interrupt(e);
  8500. return true;
  8501. }
  8502. private:
  8503. bool waitForPipe()
  8504. {
  8505. Owned<IException> pipeException;
  8506. try
  8507. {
  8508. if (firstRead)
  8509. {
  8510. pipeOpened.wait();
  8511. firstRead = false;
  8512. }
  8513. if (!pipe)
  8514. return false; // done
  8515. if (!readTransformer->eos())
  8516. return true;
  8517. }
  8518. catch (IException *e)
  8519. {
  8520. // NB: the original exception is probably a IPipeProcessException, but because InterruptableSemaphore rethrows it, we must catch it as an IException
  8521. pipeException.setown(e);
  8522. }
  8523. verifyPipe();
  8524. if (pipeException) // NB: verifyPipe may throw error based on pipe prog. output 1st.
  8525. throw pipeException.getClear();
  8526. if (recreate && !inputExhausted)
  8527. pipeOpened.wait();
  8528. return false;
  8529. }
  8530. void openPipe(char const * cmd)
  8531. {
  8532. pipeCommand.setown(cmd);
  8533. pipe.setown(createPipeProcess());
  8534. if(!pipe->run(NULL, cmd, ".", true, true, true, 0x10000))
  8535. {
  8536. // NB: pipe->run can't rely on the child process failing fast enough to return false here, failure picked up later with stderr context.
  8537. WARNLOG(ROXIE_PIPE_ERROR, "Could not run pipe process %s", cmd);
  8538. }
  8539. writeTransformer->writeHeader(pipe);
  8540. Owned<ISimpleReadStream> pipeReader = pipe->getOutputStream();
  8541. readTransformer->setStream(pipeReader.get());
  8542. pipeOpened.signal();
  8543. }
  8544. void closePipe()
  8545. {
  8546. writeTransformer->writeFooter(pipe);
  8547. pipe->closeInput();
  8548. }
  8549. void verifyPipe()
  8550. {
  8551. if (pipe)
  8552. {
  8553. unsigned err = pipe->wait();
  8554. if(err && !(helper.getPipeFlags() & TPFnofail))
  8555. {
  8556. throw createPipeFailureException(pipeCommand.get(), err, pipe);
  8557. }
  8558. pipe.clear();
  8559. pipeVerified.signal();
  8560. }
  8561. }
  8562. };
  8563. class CRoxieServerPipeWriteActivity : public CRoxieServerInternalSinkActivity
  8564. {
  8565. IHThorPipeWriteArg &helper;
  8566. Owned<IPipeProcess> pipe;
  8567. StringAttr pipeCommand;
  8568. CachedOutputMetaData inputMeta;
  8569. Owned<IOutputRowSerializer> rowSerializer;
  8570. Owned<IPipeWriteXformHelper> writeTransformer;
  8571. bool firstRead;
  8572. bool recreate;
  8573. bool inputExhausted;
  8574. public:
  8575. CRoxieServerPipeWriteActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numOutputs)
  8576. : CRoxieServerInternalSinkActivity(_ctx, _factory, _probeManager, _numOutputs), helper((IHThorPipeWriteArg &)basehelper)
  8577. {
  8578. recreate = helper.recreateEachRow();
  8579. firstRead = false;
  8580. inputExhausted = false;
  8581. }
  8582. virtual bool needsAllocator() const { return true; }
  8583. virtual void onCreate(IHThorArg *_colocalParent)
  8584. {
  8585. CRoxieServerActivity::onCreate(_colocalParent);
  8586. inputMeta.set(input->queryOutputMeta());
  8587. rowSerializer.setown(inputMeta.createDiskSerializer(ctx->queryCodeContext(), activityId));
  8588. writeTransformer.setown(createPipeWriteXformHelper(helper.getPipeFlags(), helper.queryXmlOutput(), helper.queryCsvOutput(), rowSerializer));
  8589. }
  8590. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8591. {
  8592. firstRead = true;
  8593. inputExhausted = false;
  8594. writeTransformer->ready();
  8595. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  8596. if(!recreate)
  8597. {
  8598. OwnedRoxieString pipeProgram(helper.getPipeProgram());
  8599. openPipe(pipeProgram);
  8600. }
  8601. }
  8602. virtual void stop()
  8603. {
  8604. if (!aborted && helper.getSequence() >= 0)
  8605. {
  8606. WorkunitUpdate wu = ctx->updateWorkUnit();
  8607. if (wu)
  8608. {
  8609. Owned<IWUResult> result = wu->updateResultBySequence(helper.getSequence());
  8610. if (result)
  8611. {
  8612. result->setResultTotalRowCount(processed);
  8613. result->setResultStatus(ResultStatusCalculated);
  8614. }
  8615. }
  8616. }
  8617. CRoxieServerActivity::stop();
  8618. pipe.clear();
  8619. }
  8620. virtual void onExecute()
  8621. {
  8622. Owned<IException> pipeException;
  8623. try
  8624. {
  8625. for (;;)
  8626. {
  8627. OwnedConstRoxieRow row(inputStream->ungroupedNextRow());
  8628. if (!row)
  8629. break;
  8630. processed++;
  8631. if(recreate)
  8632. openPipe(helper.getNameFromRow(row));
  8633. writeTransformer->writeTranslatedText(row, pipe);
  8634. if (recreate)
  8635. {
  8636. closePipe();
  8637. verifyPipe();
  8638. }
  8639. }
  8640. if (!recreate)
  8641. closePipe();
  8642. }
  8643. catch (IException *e)
  8644. {
  8645. pipeException.setown(e);
  8646. }
  8647. verifyPipe();
  8648. if (pipeException) // NB: verifyPipe may throw error based on pipe prog. output 1st.
  8649. throw pipeException.getClear();
  8650. }
  8651. private:
  8652. void openPipe(char const * cmd)
  8653. {
  8654. pipeCommand.setown(cmd);
  8655. pipe.setown(createPipeProcess());
  8656. if(!pipe->run(NULL, cmd, ".", true, false, true, 0x10000))
  8657. {
  8658. // NB: pipe->run can't rely on the child process failing fast enough to return false here, failure picked up later with stderr context.
  8659. WARNLOG(ROXIE_PIPE_ERROR, "Could not run pipe process %s", cmd);
  8660. }
  8661. writeTransformer->writeHeader(pipe);
  8662. }
  8663. void closePipe()
  8664. {
  8665. writeTransformer->writeFooter(pipe);
  8666. pipe->closeInput();
  8667. }
  8668. void verifyPipe()
  8669. {
  8670. if (pipe)
  8671. {
  8672. unsigned err = pipe->wait();
  8673. if(err && !(helper.getPipeFlags() & TPFnofail))
  8674. {
  8675. throw createPipeFailureException(pipeCommand.get(), err, pipe);
  8676. }
  8677. pipe.clear();
  8678. }
  8679. }
  8680. };
  8681. class CRoxieServerPipeReadActivityFactory : public CRoxieServerActivityFactory
  8682. {
  8683. public:
  8684. CRoxieServerPipeReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  8685. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  8686. {
  8687. }
  8688. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  8689. {
  8690. return new CRoxieServerPipeReadActivity(_ctx, this, _probeManager);
  8691. }
  8692. };
  8693. class CRoxieServerPipeThroughActivityFactory : public CRoxieServerActivityFactory
  8694. {
  8695. public:
  8696. CRoxieServerPipeThroughActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  8697. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  8698. {
  8699. }
  8700. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  8701. {
  8702. return new CRoxieServerPipeThroughActivity(_ctx, this, _probeManager);
  8703. }
  8704. };
  8705. class CRoxieServerPipeWriteActivityFactory : public CRoxieServerInternalSinkFactory
  8706. {
  8707. public:
  8708. CRoxieServerPipeWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, bool _isRoot)
  8709. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _isRoot)
  8710. {
  8711. }
  8712. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  8713. {
  8714. return new CRoxieServerPipeWriteActivity(_ctx, this, _probeManager, usageCount);
  8715. }
  8716. };
  8717. IRoxieServerActivityFactory *createRoxieServerPipeReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  8718. {
  8719. return new CRoxieServerPipeReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  8720. }
  8721. IRoxieServerActivityFactory *createRoxieServerPipeThroughActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  8722. {
  8723. return new CRoxieServerPipeThroughActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  8724. }
  8725. IRoxieServerActivityFactory *createRoxieServerPipeWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, bool _isRoot)
  8726. {
  8727. return new CRoxieServerPipeWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _isRoot);
  8728. }
  8729. //=====================================================================================================
  8730. class CRoxieServerStreamedIteratorActivity : public CRoxieServerActivity
  8731. {
  8732. IHThorStreamedIteratorArg &helper;
  8733. Owned<IRowStream> rows;
  8734. public:
  8735. CRoxieServerStreamedIteratorActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8736. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorStreamedIteratorArg &)basehelper)
  8737. {
  8738. }
  8739. ~CRoxieServerStreamedIteratorActivity()
  8740. {
  8741. }
  8742. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8743. {
  8744. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  8745. rows.setown(helper.createInput());
  8746. }
  8747. virtual void stop()
  8748. {
  8749. if (rows)
  8750. {
  8751. rows->stop();
  8752. rows.clear();
  8753. }
  8754. CRoxieServerActivity::stop();
  8755. }
  8756. virtual const void *nextRow()
  8757. {
  8758. ActivityTimer t(activityStats, timeActivities);
  8759. assertex(rows != NULL);
  8760. const void * next = rows->nextRow();
  8761. if (next)
  8762. processed++;
  8763. return next;
  8764. }
  8765. };
  8766. class CRoxieServerStreamedIteratorActivityFactory : public CRoxieServerActivityFactory
  8767. {
  8768. public:
  8769. CRoxieServerStreamedIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  8770. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  8771. {
  8772. }
  8773. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  8774. {
  8775. return new CRoxieServerStreamedIteratorActivity(_ctx, this, _probeManager);
  8776. }
  8777. };
  8778. IRoxieServerActivityFactory *createRoxieServerStreamedIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  8779. {
  8780. return new CRoxieServerStreamedIteratorActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  8781. }
  8782. //=====================================================================================================
  8783. class CRoxieServerFilterActivity : public CRoxieServerLateStartActivity
  8784. {
  8785. IHThorFilterArg &helper;
  8786. bool anyThisGroup;
  8787. IRangeCompare * stepCompare;
  8788. public:
  8789. CRoxieServerFilterActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8790. : CRoxieServerLateStartActivity(_ctx, _factory, _probeManager), helper((IHThorFilterArg &)basehelper)
  8791. {
  8792. anyThisGroup = false;
  8793. stepCompare = NULL;
  8794. }
  8795. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8796. {
  8797. anyThisGroup = false;
  8798. CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
  8799. lateStart(parentExtractSize, parentExtract, helper.canMatchAny());
  8800. stepCompare = NULL;
  8801. if (!eof)
  8802. {
  8803. IInputSteppingMeta * stepMeta = input->querySteppingMeta();
  8804. if (stepMeta)
  8805. stepCompare = stepMeta->queryCompare();
  8806. }
  8807. }
  8808. virtual const void * nextRow()
  8809. {
  8810. ActivityTimer t(activityStats, timeActivities);
  8811. if (eof)
  8812. return NULL;
  8813. for (;;)
  8814. {
  8815. OwnedConstRoxieRow ret(inputStream->nextRow());
  8816. if (!ret)
  8817. {
  8818. //stop returning two NULLs in a row.
  8819. if (anyThisGroup)
  8820. {
  8821. anyThisGroup = false;
  8822. return NULL;
  8823. }
  8824. ret.setown(inputStream->nextRow());
  8825. if (!ret)
  8826. {
  8827. eof = true;
  8828. return NULL; // eof...
  8829. }
  8830. }
  8831. if (helper.isValid(ret))
  8832. {
  8833. anyThisGroup = true;
  8834. processed++;
  8835. return ret.getClear();
  8836. }
  8837. }
  8838. }
  8839. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  8840. {
  8841. //Could assert that this isn't grouped
  8842. // MORE - will need rethinking once we rethink the nextRowGE interface for global smart-stepping.
  8843. ActivityTimer t(activityStats, timeActivities);
  8844. if (eof)
  8845. return NULL;
  8846. for (;;)
  8847. {
  8848. OwnedConstRoxieRow ret(inputStream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra));
  8849. if (!ret)
  8850. {
  8851. eof = true;
  8852. return NULL;
  8853. }
  8854. if (!wasCompleteMatch)
  8855. {
  8856. anyThisGroup = false; // RKC->GH - is this right??
  8857. return ret.getClear();
  8858. }
  8859. if (helper.isValid(ret))
  8860. {
  8861. anyThisGroup = true;
  8862. processed++;
  8863. return ret.getClear();
  8864. }
  8865. if (!stepExtra.returnMismatches())
  8866. {
  8867. ret.clear();
  8868. return nextRow();
  8869. }
  8870. //If asked to return mismatches we are only interested in mismatches that will force the stepped
  8871. //condition to advance
  8872. if (stepCompare->docompare(ret, seek, numFields) != 0)
  8873. {
  8874. wasCompleteMatch = false;
  8875. anyThisGroup = false; // WHY?
  8876. return ret.getClear();
  8877. }
  8878. }
  8879. }
  8880. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  8881. {
  8882. return input->gatherConjunctions(collector);
  8883. }
  8884. virtual void resetEOF()
  8885. {
  8886. eof = prefiltered;
  8887. anyThisGroup = false;
  8888. inputStream->resetEOF();
  8889. }
  8890. IInputSteppingMeta * querySteppingMeta()
  8891. {
  8892. return input->querySteppingMeta();
  8893. }
  8894. };
  8895. class CRoxieServerFilterActivityFactory : public CRoxieServerActivityFactory
  8896. {
  8897. public:
  8898. CRoxieServerFilterActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  8899. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  8900. {
  8901. optStableInput = false;
  8902. }
  8903. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  8904. {
  8905. return new CRoxieServerFilterActivity(_ctx, this, _probeManager);
  8906. }
  8907. };
  8908. IRoxieServerActivityFactory *createRoxieServerFilterActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  8909. {
  8910. return new CRoxieServerFilterActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  8911. }
  8912. //=====================================================================================================
  8913. class CRoxieServerFilterGroupActivity : public CRoxieServerLateStartActivity
  8914. {
  8915. IHThorFilterGroupArg &helper;
  8916. unsigned curIndex;
  8917. ConstPointerArray gathered;
  8918. IRangeCompare * stepCompare;
  8919. public:
  8920. CRoxieServerFilterGroupActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  8921. : CRoxieServerLateStartActivity(_ctx, _factory, _probeManager), helper((IHThorFilterGroupArg &)basehelper)
  8922. {
  8923. curIndex = 0;
  8924. stepCompare = NULL;
  8925. }
  8926. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  8927. {
  8928. CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
  8929. lateStart(parentExtractSize, parentExtract, helper.canMatchAny());//sets eof
  8930. assertex(eof == !helper.canMatchAny());
  8931. curIndex = 0;
  8932. stepCompare = NULL;
  8933. if (!eof)
  8934. {
  8935. IInputSteppingMeta * inputStepping = input->querySteppingMeta();
  8936. if (inputStepping)
  8937. stepCompare = inputStepping->queryCompare();
  8938. }
  8939. }
  8940. virtual void reset()
  8941. {
  8942. releaseGathered();
  8943. CRoxieServerLateStartActivity::reset();
  8944. }
  8945. inline void releaseGathered()
  8946. {
  8947. roxiemem::ReleaseRoxieRowRange(gathered.getArray(), curIndex, gathered.ordinality());
  8948. gathered.kill();
  8949. curIndex = 0;
  8950. }
  8951. virtual const void * nextRow()
  8952. {
  8953. ActivityTimer t(activityStats, timeActivities);
  8954. for (;;)
  8955. {
  8956. if (eof)
  8957. return NULL;
  8958. if (gathered.ordinality())
  8959. {
  8960. if (gathered.isItem(curIndex))
  8961. {
  8962. const void * ret = gathered.item(curIndex++);
  8963. processed++;
  8964. return ret;
  8965. }
  8966. curIndex = 0;
  8967. gathered.kill();
  8968. return NULL;
  8969. }
  8970. const void * ret = inputStream->nextRow();
  8971. while (ret)
  8972. {
  8973. gathered.append(ret);
  8974. ret = inputStream->nextRow();
  8975. }
  8976. unsigned num = gathered.ordinality();
  8977. if (num != 0)
  8978. {
  8979. if (!helper.isValid(num, (const void * *)gathered.getArray()))
  8980. ReleaseRoxieRows(gathered); // read next group
  8981. }
  8982. else
  8983. eof = true;
  8984. }
  8985. }
  8986. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  8987. {
  8988. ActivityTimer t(activityStats, timeActivities);
  8989. if (eof)
  8990. return NULL;
  8991. if (gathered.ordinality())
  8992. {
  8993. while (gathered.isItem(curIndex))
  8994. {
  8995. const void * ret = gathered.item(curIndex++);
  8996. if (stepCompare->docompare(ret, seek, numFields) >= 0)
  8997. {
  8998. processed++;
  8999. return ret;
  9000. }
  9001. ReleaseRoxieRow(ret);
  9002. }
  9003. curIndex = 0;
  9004. gathered.kill();
  9005. //nextRowGE never returns an end of group marker.
  9006. }
  9007. //Not completely sure about this - it could lead the the start of a group being skipped,
  9008. //so the group filter could potentially work on a different group. If so, we'd need to check the
  9009. //next fields were a subset of the grouping fields - more an issue for the group activity.
  9010. //MORE: What do we do with wasCompleteMatch? something like the following????
  9011. #if 0
  9012. for (;;)
  9013. {
  9014. const void * ret;
  9015. if (stepExtra.returnMismatches())
  9016. {
  9017. bool matchedCompletely = true;
  9018. ret = input->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  9019. if (!wasCompleteMatch)
  9020. return ret;
  9021. }
  9022. else
  9023. ret = input->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  9024. #endif
  9025. const void * ret = inputStream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  9026. while (ret)
  9027. {
  9028. gathered.append(ret);
  9029. ret = inputStream->nextRow();
  9030. }
  9031. unsigned num = gathered.ordinality();
  9032. if (num != 0)
  9033. {
  9034. if (!helper.isValid(num, (const void * *)gathered.getArray()))
  9035. ReleaseRoxieRows(gathered); // read next group
  9036. }
  9037. else
  9038. eof = true;
  9039. return IEngineRowStream::ungroupedNextRow();
  9040. }
  9041. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  9042. {
  9043. return input->gatherConjunctions(collector);
  9044. }
  9045. virtual void resetEOF()
  9046. {
  9047. eof = false;
  9048. releaseGathered();
  9049. inputStream->resetEOF();
  9050. }
  9051. IInputSteppingMeta * querySteppingMeta()
  9052. {
  9053. return input->querySteppingMeta();
  9054. }
  9055. };
  9056. class CRoxieServerFilterGroupActivityFactory : public CRoxieServerActivityFactory
  9057. {
  9058. public:
  9059. CRoxieServerFilterGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9060. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  9061. {
  9062. }
  9063. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  9064. {
  9065. return new CRoxieServerFilterGroupActivity(_ctx, this, _probeManager);
  9066. }
  9067. };
  9068. IRoxieServerActivityFactory *createRoxieServerFilterGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9069. {
  9070. return new CRoxieServerFilterGroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  9071. }
  9072. //=================================================================================
  9073. class CRoxieServerSideEffectActivity : public CRoxieServerActivity
  9074. {
  9075. IHThorSideEffectArg &helper;
  9076. CriticalSection ecrit;
  9077. Owned<IException> exception;
  9078. bool executed;
  9079. public:
  9080. CRoxieServerSideEffectActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9081. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorSideEffectArg &)basehelper)
  9082. {
  9083. executed = false;
  9084. }
  9085. virtual const void * nextRow()
  9086. {
  9087. ActivityTimer t(activityStats, timeActivities);
  9088. CriticalBlock b(ecrit);
  9089. if (exception)
  9090. throw(exception.getLink());
  9091. if (!executed)
  9092. {
  9093. try
  9094. {
  9095. executed = true;
  9096. helper.action();
  9097. }
  9098. catch(IException *E)
  9099. {
  9100. exception.set(E);
  9101. throw;
  9102. }
  9103. }
  9104. return NULL;
  9105. }
  9106. virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
  9107. {
  9108. CriticalBlock b(ecrit);
  9109. if (exception)
  9110. throw(exception.getLink());
  9111. if (!executed)
  9112. {
  9113. try
  9114. {
  9115. ActivityTimer t(activityStats, timeActivities);
  9116. executed = true;
  9117. start(parentExtractSize, parentExtract, false);
  9118. helper.action();
  9119. stop();
  9120. }
  9121. catch(IException *E)
  9122. {
  9123. ctx->notifyAbort(E);
  9124. abort();
  9125. exception.set(E);
  9126. throw;
  9127. }
  9128. }
  9129. }
  9130. virtual void reset()
  9131. {
  9132. executed = false;
  9133. exception.clear();
  9134. CRoxieServerActivity::reset();
  9135. }
  9136. };
  9137. class CRoxieServerSideEffectActivityFactory : public CRoxieServerActivityFactory
  9138. {
  9139. bool isRoot;
  9140. public:
  9141. CRoxieServerSideEffectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  9142. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), isRoot(_isRoot)
  9143. {
  9144. }
  9145. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  9146. {
  9147. return new CRoxieServerSideEffectActivity(_ctx, this, _probeManager);
  9148. }
  9149. virtual bool isSink() const
  9150. {
  9151. return isRoot && !meta.queryOriginal();
  9152. }
  9153. };
  9154. IRoxieServerActivityFactory *createRoxieServerSideEffectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  9155. {
  9156. return new CRoxieServerSideEffectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _isRoot);
  9157. }
  9158. //=================================================================================
  9159. class CRoxieServerActionActivity : public CRoxieServerInternalSinkActivity
  9160. {
  9161. IHThorActionArg &helper;
  9162. public:
  9163. CRoxieServerActionActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numOutputs)
  9164. : CRoxieServerInternalSinkActivity(_ctx, _factory, _probeManager, _numOutputs), helper((IHThorActionArg &)basehelper)
  9165. {
  9166. }
  9167. virtual void onExecute()
  9168. {
  9169. helper.action();
  9170. }
  9171. };
  9172. class CRoxieServerActionActivityFactory : public CRoxieServerInternalSinkFactory
  9173. {
  9174. public:
  9175. CRoxieServerActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, bool _isRoot)
  9176. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _isRoot)
  9177. {
  9178. }
  9179. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  9180. {
  9181. return new CRoxieServerActionActivity(_ctx, this, _probeManager, usageCount);
  9182. }
  9183. };
  9184. IRoxieServerActivityFactory *createRoxieServerActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, bool _isRoot)
  9185. {
  9186. return new CRoxieServerActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _isRoot);
  9187. }
  9188. //=================================================================================
  9189. class CRoxieServerSampleActivity : public CRoxieServerActivity
  9190. {
  9191. IHThorSampleArg &helper;
  9192. unsigned numSamples;
  9193. unsigned numToSkip;
  9194. unsigned whichSample;
  9195. bool anyThisGroup;
  9196. bool eof;
  9197. public:
  9198. CRoxieServerSampleActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9199. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorSampleArg &)basehelper)
  9200. {
  9201. numSamples = 0;
  9202. numToSkip = 0;
  9203. whichSample = 0;
  9204. anyThisGroup = false;
  9205. eof = false;
  9206. }
  9207. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9208. {
  9209. anyThisGroup = false;
  9210. eof = false;
  9211. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  9212. numSamples = helper.getProportion();
  9213. whichSample = helper.getSampleNumber();
  9214. numToSkip = (whichSample ? whichSample-1 : 0);
  9215. }
  9216. virtual const void * nextRow()
  9217. {
  9218. ActivityTimer t(activityStats, timeActivities);
  9219. if (eof)
  9220. return NULL;
  9221. for (;;)
  9222. {
  9223. const void * ret = inputStream->nextRow();
  9224. if (!ret)
  9225. {
  9226. //this does work with groups - may or may not be useful...
  9227. //reset the sample for each group.... probably best.
  9228. numToSkip = (whichSample ? whichSample-1 : 0);
  9229. if (anyThisGroup)
  9230. {
  9231. anyThisGroup = false;
  9232. return NULL;
  9233. }
  9234. ret = inputStream->nextRow();
  9235. if (!ret)
  9236. {
  9237. eof = true;
  9238. return NULL; // eof...
  9239. }
  9240. }
  9241. if (numToSkip == 0)
  9242. {
  9243. anyThisGroup = true;
  9244. numToSkip = numSamples-1;
  9245. processed++;
  9246. return ret;
  9247. }
  9248. numToSkip--;
  9249. ReleaseRoxieRow(ret);
  9250. }
  9251. }
  9252. };
  9253. class CRoxieServerSampleActivityFactory : public CRoxieServerActivityFactory
  9254. {
  9255. public:
  9256. CRoxieServerSampleActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9257. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  9258. {
  9259. }
  9260. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  9261. {
  9262. return new CRoxieServerSampleActivity(_ctx, this, _probeManager);
  9263. }
  9264. };
  9265. IRoxieServerActivityFactory *createRoxieServerSampleActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9266. {
  9267. return new CRoxieServerSampleActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  9268. }
  9269. //=================================================================================
  9270. class CRoxieServerChooseSetsActivity : public CRoxieServerActivity
  9271. {
  9272. IHThorChooseSetsArg &helper;
  9273. unsigned numSets;
  9274. unsigned * setCounts;
  9275. bool done;
  9276. public:
  9277. CRoxieServerChooseSetsActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9278. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorChooseSetsArg &)basehelper)
  9279. {
  9280. setCounts = NULL;
  9281. numSets = 0;
  9282. done = false;
  9283. }
  9284. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9285. {
  9286. done = false;
  9287. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  9288. numSets = helper.getNumSets();
  9289. setCounts = new unsigned[numSets];
  9290. memset(setCounts, 0, sizeof(unsigned)*numSets);
  9291. helper.setCounts(setCounts);
  9292. }
  9293. virtual void reset()
  9294. {
  9295. delete [] setCounts;
  9296. setCounts = NULL;
  9297. CRoxieServerActivity::reset();
  9298. }
  9299. virtual const void * nextRow()
  9300. {
  9301. ActivityTimer t(activityStats, timeActivities);
  9302. if (done)
  9303. return NULL;
  9304. for (;;)
  9305. {
  9306. const void * ret = inputStream->ungroupedNextRow();
  9307. if (!ret)
  9308. {
  9309. done = true;
  9310. return NULL;
  9311. }
  9312. processed++;
  9313. switch (helper.getRecordAction(ret))
  9314. {
  9315. case 2:
  9316. done = true;
  9317. return ret;
  9318. case 1:
  9319. return ret;
  9320. }
  9321. ReleaseRoxieRow(ret);
  9322. }
  9323. }
  9324. };
  9325. class CRoxieServerChooseSetsActivityFactory : public CRoxieServerActivityFactory
  9326. {
  9327. public:
  9328. CRoxieServerChooseSetsActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9329. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  9330. {
  9331. }
  9332. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  9333. {
  9334. return new CRoxieServerChooseSetsActivity(_ctx, this, _probeManager);
  9335. }
  9336. };
  9337. IRoxieServerActivityFactory *createRoxieServerChooseSetsActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9338. {
  9339. return new CRoxieServerChooseSetsActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  9340. }
  9341. //=================================================================================
  9342. class CRoxieServerChooseSetsExActivity : public CRoxieServerActivity
  9343. {
  9344. protected:
  9345. IHThorChooseSetsExArg &helper;
  9346. unsigned numSets;
  9347. unsigned curIndex;
  9348. unsigned * setCounts;
  9349. count_t * limits;
  9350. bool done;
  9351. ConstPointerArray gathered;
  9352. virtual bool includeRow(const void * row) = 0;
  9353. virtual void calculateSelection() = 0;
  9354. public:
  9355. CRoxieServerChooseSetsExActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9356. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorChooseSetsExArg &)basehelper)
  9357. {
  9358. setCounts = NULL;
  9359. limits = NULL;
  9360. done = false;
  9361. curIndex = 0;
  9362. numSets = 0;
  9363. }
  9364. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9365. {
  9366. done = false;
  9367. curIndex = 0;
  9368. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  9369. numSets = helper.getNumSets();
  9370. setCounts = new unsigned[numSets];
  9371. memset(setCounts, 0, sizeof(unsigned)*numSets);
  9372. limits = (count_t *)calloc(sizeof(count_t), numSets);
  9373. helper.getLimits(limits);
  9374. }
  9375. virtual void reset()
  9376. {
  9377. delete [] setCounts;
  9378. setCounts = NULL;
  9379. free(limits);
  9380. limits = NULL;
  9381. roxiemem::ReleaseRoxieRowRange(gathered.getArray(), curIndex, gathered.ordinality());
  9382. gathered.kill();
  9383. curIndex = 0;
  9384. CRoxieServerActivity::reset();
  9385. }
  9386. virtual const void * nextRow()
  9387. {
  9388. ActivityTimer t(activityStats, timeActivities);
  9389. if (gathered.ordinality() == 0)
  9390. {
  9391. curIndex = 0;
  9392. if (!inputStream->nextGroup(gathered))
  9393. {
  9394. done = true;
  9395. return NULL;
  9396. }
  9397. ForEachItemIn(idx1, gathered)
  9398. {
  9399. unsigned category = helper.getCategory(gathered.item(idx1));
  9400. if (category)
  9401. setCounts[category-1]++;
  9402. }
  9403. calculateSelection();
  9404. }
  9405. while (gathered.isItem(curIndex))
  9406. {
  9407. const void * row = gathered.item(curIndex);
  9408. gathered.replace(NULL, curIndex);
  9409. curIndex++;
  9410. if (includeRow(row))
  9411. {
  9412. processed++;
  9413. return row;
  9414. }
  9415. ReleaseRoxieRow(row);
  9416. }
  9417. gathered.kill();
  9418. return NULL;
  9419. }
  9420. };
  9421. class CRoxieServerChooseSetsLastActivity : public CRoxieServerChooseSetsExActivity
  9422. {
  9423. unsigned * numToSkip;
  9424. public:
  9425. CRoxieServerChooseSetsLastActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager) : CRoxieServerChooseSetsExActivity(_ctx, _factory, _probeManager)
  9426. {
  9427. numToSkip = NULL;
  9428. }
  9429. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9430. {
  9431. CRoxieServerChooseSetsExActivity::start(parentExtractSize, parentExtract, paused);
  9432. numToSkip = (unsigned *)calloc(sizeof(unsigned), numSets);
  9433. }
  9434. virtual void reset()
  9435. {
  9436. free(numToSkip);
  9437. numToSkip = NULL;
  9438. CRoxieServerChooseSetsExActivity::reset();
  9439. }
  9440. protected:
  9441. virtual void calculateSelection()
  9442. {
  9443. for (unsigned idx=0; idx < numSets; idx++)
  9444. {
  9445. if (setCounts[idx] < limits[idx])
  9446. numToSkip[idx] = 0;
  9447. else
  9448. numToSkip[idx] = (unsigned)(setCounts[idx] - limits[idx]);
  9449. }
  9450. }
  9451. virtual bool includeRow(const void * row)
  9452. {
  9453. unsigned category = helper.getCategory(row);
  9454. if (category)
  9455. {
  9456. if (numToSkip[category-1] == 0)
  9457. return true;
  9458. numToSkip[category-1]--;
  9459. }
  9460. return false;
  9461. }
  9462. };
  9463. class CRoxieServerChooseSetsEnthActivity : public CRoxieServerChooseSetsExActivity
  9464. {
  9465. count_t * counter;
  9466. public:
  9467. CRoxieServerChooseSetsEnthActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager) : CRoxieServerChooseSetsExActivity(_ctx, _factory, _probeManager)
  9468. {
  9469. counter = NULL;
  9470. }
  9471. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9472. {
  9473. CRoxieServerChooseSetsExActivity::start(parentExtractSize, parentExtract, paused);
  9474. counter = (count_t *)calloc(sizeof(count_t), numSets);
  9475. }
  9476. virtual void reset()
  9477. {
  9478. free(counter);
  9479. counter = NULL;
  9480. CRoxieServerChooseSetsExActivity::reset();
  9481. }
  9482. protected:
  9483. virtual void calculateSelection()
  9484. {
  9485. }
  9486. virtual bool includeRow(const void * row)
  9487. {
  9488. unsigned category = helper.getCategory(row);
  9489. if (category)
  9490. {
  9491. assertex(category <= numSets);
  9492. counter[category-1] += limits[category-1];
  9493. if(counter[category-1] >= setCounts[category-1])
  9494. {
  9495. counter[category-1] -= setCounts[category-1];
  9496. return true;
  9497. }
  9498. }
  9499. return false;
  9500. }
  9501. };
  9502. class CRoxieServerChooseSetsEnthActivityFactory : public CRoxieServerActivityFactory
  9503. {
  9504. public:
  9505. CRoxieServerChooseSetsEnthActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9506. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  9507. {
  9508. }
  9509. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  9510. {
  9511. return new CRoxieServerChooseSetsEnthActivity(_ctx, this, _probeManager);
  9512. }
  9513. };
  9514. IRoxieServerActivityFactory *createRoxieServerChooseSetsEnthActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9515. {
  9516. return new CRoxieServerChooseSetsEnthActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  9517. }
  9518. class CRoxieServerChooseSetsLastActivityFactory : public CRoxieServerActivityFactory
  9519. {
  9520. public:
  9521. CRoxieServerChooseSetsLastActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9522. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  9523. {
  9524. }
  9525. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  9526. {
  9527. return new CRoxieServerChooseSetsLastActivity(_ctx, this, _probeManager);
  9528. }
  9529. };
  9530. IRoxieServerActivityFactory *createRoxieServerChooseSetsLastActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9531. {
  9532. return new CRoxieServerChooseSetsLastActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  9533. }
  9534. //=================================================================================
  9535. class CRoxieServerEnthActivity : public CRoxieServerActivity
  9536. {
  9537. IHThorEnthArg &helper;
  9538. unsigned __int64 numerator;
  9539. unsigned __int64 denominator;
  9540. unsigned __int64 counter;
  9541. bool eof;
  9542. public:
  9543. CRoxieServerEnthActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9544. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorEnthArg &)basehelper)
  9545. {
  9546. eof = false;
  9547. numerator = denominator = counter = 0;
  9548. }
  9549. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9550. {
  9551. eof = false;
  9552. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  9553. numerator = helper.getProportionNumerator();
  9554. denominator = helper.getProportionDenominator();
  9555. if(denominator == 0) denominator = 1; //MORE: simplest way to avoid disaster in this case
  9556. counter = (helper.getSampleNumber()-1) * greatestCommonDivisor(numerator, denominator);
  9557. if (counter >= denominator)
  9558. counter %= denominator;
  9559. }
  9560. inline bool wanted()
  9561. {
  9562. counter += numerator;
  9563. if(counter >= denominator)
  9564. {
  9565. counter -= denominator;
  9566. return true;
  9567. }
  9568. return false;
  9569. }
  9570. virtual const void * nextRow()
  9571. {
  9572. ActivityTimer t(activityStats, timeActivities);
  9573. if (eof)
  9574. return NULL;
  9575. const void * ret;
  9576. for (;;)
  9577. {
  9578. ret = inputStream->ungroupedNextRow();
  9579. if (!ret) //eof
  9580. {
  9581. eof = true;
  9582. return ret;
  9583. }
  9584. if (wanted())
  9585. return ret;
  9586. ReleaseRoxieRow(ret);
  9587. }
  9588. }
  9589. };
  9590. class CRoxieServerEnthActivityFactory : public CRoxieServerActivityFactory
  9591. {
  9592. public:
  9593. CRoxieServerEnthActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9594. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  9595. {
  9596. }
  9597. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  9598. {
  9599. return new CRoxieServerEnthActivity(_ctx, this, _probeManager);
  9600. }
  9601. };
  9602. IRoxieServerActivityFactory *createRoxieServerEnthActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9603. {
  9604. return new CRoxieServerEnthActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  9605. }
  9606. //=================================================================================
  9607. class CRoxieServerAggregateActivity : public CRoxieServerActivity
  9608. {
  9609. IHThorAggregateArg &helper;
  9610. bool eof;
  9611. bool isInputGrouped;
  9612. bool abortEarly;
  9613. public:
  9614. CRoxieServerAggregateActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  9615. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorAggregateArg &)basehelper)
  9616. {
  9617. eof = false;
  9618. isInputGrouped = false;
  9619. abortEarly = false;
  9620. }
  9621. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9622. {
  9623. eof = false;
  9624. isInputGrouped = input->queryOutputMeta()->isGrouped(); // could be done earlier, in setInput?
  9625. abortEarly = !isInputGrouped && (factory->getKind() == TAKexistsaggregate); // ditto
  9626. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  9627. }
  9628. virtual bool needsAllocator() const { return true; }
  9629. virtual const void * nextRow()
  9630. {
  9631. ActivityTimer t(activityStats, timeActivities);
  9632. if (eof)
  9633. return NULL;
  9634. const void * next = inputStream->nextRow();
  9635. if (!next && isInputGrouped)
  9636. {
  9637. eof = true;
  9638. return NULL;
  9639. }
  9640. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  9641. size32_t finalSize = helper.clearAggregate(rowBuilder);
  9642. if (next)
  9643. {
  9644. finalSize = helper.processFirst(rowBuilder, next);
  9645. ReleaseRoxieRow(next);
  9646. if (!abortEarly)
  9647. {
  9648. for (;;)
  9649. {
  9650. next = inputStream->nextRow();
  9651. if (!next)
  9652. break;
  9653. finalSize = helper.processNext(rowBuilder, next);
  9654. ReleaseRoxieRow(next);
  9655. }
  9656. }
  9657. }
  9658. if (!isInputGrouped) // either read all, or aborted early
  9659. eof = true;
  9660. processed++;
  9661. return rowBuilder.finalizeRowClear(finalSize);
  9662. }
  9663. };
  9664. class CRoxieServerStrandedAggregateActivity : public CRoxieServerStrandedActivity, implements IOrderedCallbackCollection
  9665. {
  9666. class AggregateProcessor : public StrandProcessor, implements IStrandThreaded, implements IOrderedOutputCallback
  9667. {
  9668. protected:
  9669. IHThorAggregateArg &helper;
  9670. OwnedConstRoxieRow result;
  9671. bool eof = false;
  9672. bool isInputGrouped = false;
  9673. bool abortEarly = false;
  9674. public:
  9675. AggregateProcessor(CRoxieServerActivity &_parent, IEngineRowStream *_inputStream, IHThorAggregateArg &_helper, bool _isInputGrouped, bool _abortEarly)
  9676. : StrandProcessor(_parent, _inputStream, true), helper(_helper), isInputGrouped(_isInputGrouped), abortEarly(_abortEarly)
  9677. {
  9678. }
  9679. virtual const void * nextRow() override
  9680. {
  9681. ActivityTimer t(activityStats, timeActivities);
  9682. if (eof || isAborting())
  9683. return NULL;
  9684. const void * next = inputStream->nextRow();
  9685. if (!next && isInputGrouped)
  9686. {
  9687. eof = true;
  9688. return NULL;
  9689. }
  9690. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  9691. size32_t finalSize = helper.clearAggregate(rowBuilder);
  9692. if (next)
  9693. {
  9694. finalSize = helper.processFirst(rowBuilder, next);
  9695. ReleaseRoxieRow(next);
  9696. if (!abortEarly)
  9697. {
  9698. for (;;)
  9699. {
  9700. next = inputStream->nextRow();
  9701. if (!next)
  9702. break;
  9703. finalSize = helper.processNext(rowBuilder, next);
  9704. ReleaseRoxieRow(next);
  9705. if (isAborting())
  9706. break;
  9707. }
  9708. }
  9709. }
  9710. if (!isInputGrouped) // either read all, or aborted early
  9711. eof = true;
  9712. processed++;
  9713. return rowBuilder.finalizeRowClear(finalSize);
  9714. }
  9715. virtual void threadmain() override
  9716. {
  9717. isInputGrouped = true; // gather no row if there are no input rows.
  9718. result.setown(nextRow());
  9719. queryActivity().noteStrandFinished(inputStream);
  9720. }
  9721. virtual void reset() override
  9722. {
  9723. StrandProcessor::reset();
  9724. result.clear();
  9725. eof = false;
  9726. }
  9727. //interface IOutputOutputCallback
  9728. virtual bool noteEndOfInputChunk() { return true; }
  9729. virtual void noteEndOfInput() {}
  9730. virtual void stopStream()
  9731. {
  9732. inputStream->stop();
  9733. }
  9734. const void * queryResult() const { return result; }
  9735. CRoxieServerStrandedAggregateActivity & queryActivity() { return static_cast<CRoxieServerStrandedAggregateActivity &>(parent); }
  9736. };
  9737. //This class is only used when a single aggregate is being calculated.
  9738. class AggregateCombiner : public CInterfaceOf<IEngineRowStream>
  9739. {
  9740. public:
  9741. explicit AggregateCombiner(CRoxieServerStrandedAggregateActivity & _parent) : parent(_parent)
  9742. {
  9743. }
  9744. virtual const void *nextRow()
  9745. {
  9746. return parent.getCombinedAggregate();
  9747. }
  9748. virtual void stop()
  9749. {
  9750. parent.outputStopped();
  9751. }
  9752. virtual void resetEOF()
  9753. {
  9754. throwUnexpected();
  9755. }
  9756. protected:
  9757. CRoxieServerStrandedAggregateActivity & parent;
  9758. } combiner;
  9759. class NullCallback : implements IOrderedOutputCallback
  9760. {
  9761. public:
  9762. virtual bool noteEndOfInputChunk() { return true; }
  9763. virtual void noteEndOfInput() {}
  9764. } nullCallback;
  9765. public:
  9766. CRoxieServerStrandedAggregateActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const StrandOptions &_strandOptions)
  9767. : CRoxieServerStrandedActivity(_ctx, _factory, _probeManager, _strandOptions), combiner(*this), helper((IHThorAggregateArg &)basehelper)
  9768. {
  9769. isInputGrouped = false;
  9770. abortEarly = false;
  9771. }
  9772. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  9773. {
  9774. CRoxieServerStrandedActivity::setInput(idx, _sourceIdx, _in);
  9775. isInputGrouped = input->queryOutputMeta()->isGrouped(); // could be done earlier, in setInput?
  9776. abortEarly = !isInputGrouped && (factory->getKind() == TAKexistsaggregate); // ditto
  9777. //For EXISTS(), and aggregates that rely on the input order, force to execute single threaded
  9778. //MORE: Ordered should be able to process a block at a time and then merge them, similar to disk write processing.
  9779. if (abortEarly || (!isInputGrouped && queryFactory()->isInputOrdered(false, 0)))
  9780. strandOptions.numStrands = 1;
  9781. else if (!isInputGrouped)
  9782. combineStreams = true;
  9783. }
  9784. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks) override
  9785. {
  9786. PointerArrayOf<IEngineRowStream> localStreams;
  9787. //If streams are being combined, this class should be notified of end of sections, otherwise grouped aggregates can inform a downstream activity.
  9788. IOrderedCallbackCollection * callbacks = combineStreams ? static_cast<IOrderedCallbackCollection *>(this) : orderedCallbacks;
  9789. Owned<IStrandJunction> outJunction = CRoxieServerStrandedActivity::getOutputStreams(ctx, idx, localStreams, consumerOptions, consumerOrdered, callbacks);
  9790. if (localStreams.ordinality() == 1)
  9791. combineStreams = false; // Select a more efficient/simpler code path
  9792. if (combineStreams)
  9793. {
  9794. assertex(!outJunction); // this needs more thought!
  9795. streams.append(&combiner);
  9796. }
  9797. else
  9798. {
  9799. streams.swapWith(localStreams);
  9800. }
  9801. return outJunction.getClear();
  9802. }
  9803. virtual StrandProcessor *createStrandProcessor(IEngineRowStream *instream)
  9804. {
  9805. if (traceLevel > 4)
  9806. DBGLOG("Create aggregate strand processor %u", strandOptions.numStrands);
  9807. return new AggregateProcessor(*this, instream, (IHThorAggregateArg &) basehelper, isInputGrouped && !combineStreams, abortEarly);
  9808. }
  9809. virtual StrandProcessor *createStrandSourceProcessor(bool inputOrdered) { throwUnexpected(); }
  9810. virtual bool needsAllocator() const { return true; }
  9811. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused) override
  9812. {
  9813. CRoxieServerStrandedActivity::start(parentExtractSize, parentExtract, paused);
  9814. onStartStrands();
  9815. if (combineStreams)
  9816. {
  9817. barrier.setown(createStrandBarrier());
  9818. ForEachItemIn(i, strands)
  9819. barrier->startStrand(queryAggregate(i));
  9820. active.store(1);
  9821. }
  9822. else
  9823. active.store(0);
  9824. }
  9825. virtual void reset() override
  9826. {
  9827. CRoxieServerStrandedActivity::reset();
  9828. barrier.clear();
  9829. calculated = false;
  9830. }
  9831. //Helper functions for the single aggregate case
  9832. const void * getCombinedAggregate()
  9833. {
  9834. if (calculated)
  9835. return NULL;
  9836. calculated= true;
  9837. barrier->waitForStrands();
  9838. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  9839. size32_t finalSize = helper.clearAggregate(rowBuilder);
  9840. bool isFirst = true;
  9841. for (unsigned i=0; i < numStrands(); i++)
  9842. {
  9843. const void * next = queryAggregate(i).queryResult();
  9844. if (next)
  9845. {
  9846. if (isFirst)
  9847. {
  9848. finalSize = cloneRow(rowBuilder, next, helper.queryOutputMeta());
  9849. isFirst = false;
  9850. }
  9851. else
  9852. finalSize = helper.mergeAggregate(rowBuilder, next);
  9853. }
  9854. }
  9855. return rowBuilder.finalizeRowClear(finalSize);
  9856. }
  9857. void outputStopped()
  9858. {
  9859. //only called if generating a single result.
  9860. assertex(combineStreams);
  9861. if (barrier)
  9862. {
  9863. requestAbort(); // if strands are still running, abort them
  9864. barrier->waitForStrands();
  9865. }
  9866. else
  9867. {
  9868. ForEachItemIn(i, strands)
  9869. queryAggregate(i).stopStream();
  9870. }
  9871. CRoxieServerStrandedActivity::stop();
  9872. }
  9873. void noteStrandFinished(IRowStream * stream)
  9874. {
  9875. barrier->noteStrandFinished(stream);
  9876. }
  9877. virtual IOrderedOutputCallback * queryCallback(unsigned i)
  9878. {
  9879. //The non-ordered aggregate does not need to process the ordered call back information.
  9880. //If the ordered single aggregate was implemented then it would need to be processed instead of ignored,
  9881. //however it needs some care since the strands are not yet created. So cannot return strand(i).
  9882. return &nullCallback;
  9883. }
  9884. inline AggregateProcessor & queryAggregate(unsigned i)
  9885. {
  9886. return static_cast<AggregateProcessor &>(strands.item(i));
  9887. }
  9888. protected:
  9889. IHThorAggregateArg &helper;
  9890. Owned<IStrandBarrier> barrier;
  9891. IEngineRowStream * combinedStream = nullptr;
  9892. bool isInputGrouped;
  9893. bool abortEarly;
  9894. bool combineStreams = false;
  9895. bool calculated = false;
  9896. };
  9897. class CRoxieServerAggregateActivityFactory : public CRoxieServerActivityFactory
  9898. {
  9899. StrandOptions strandOptions;
  9900. public:
  9901. CRoxieServerAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9902. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), strandOptions(_graphNode)
  9903. {
  9904. Owned<IHThorAggregateArg> helper = (IHThorAggregateArg *) helperFactory();
  9905. if (!(helper->getAggregateFlags() & TAForderedmerge))
  9906. optStableInput = false;
  9907. }
  9908. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  9909. {
  9910. return new CRoxieServerStrandedAggregateActivity(_ctx, this, _probeManager, strandOptions);
  9911. }
  9912. };
  9913. IRoxieServerActivityFactory *createRoxieServerAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9914. {
  9915. return new CRoxieServerAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  9916. }
  9917. //=================================================================================
  9918. typedef unsigned t_hashPrefix;
  9919. class CRoxieServerHashAggregateActivity : public CRoxieServerActivity
  9920. {
  9921. IHThorHashAggregateArg &helper;
  9922. RowAggregator aggregated;
  9923. bool eof;
  9924. bool gathered;
  9925. bool isGroupedAggregate;
  9926. public:
  9927. CRoxieServerHashAggregateActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, bool _isGroupedAggregate, IProbeManager *_probeManager)
  9928. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorHashAggregateArg &)basehelper),
  9929. aggregated(helper, helper),
  9930. isGroupedAggregate(_isGroupedAggregate)
  9931. {
  9932. eof = false;
  9933. gathered = false;
  9934. }
  9935. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  9936. {
  9937. eof = false;
  9938. gathered = false;
  9939. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  9940. }
  9941. virtual void reset()
  9942. {
  9943. aggregated.reset();
  9944. CRoxieServerActivity::reset();
  9945. }
  9946. virtual bool needsAllocator() const { return true; }
  9947. virtual const void * nextRow()
  9948. {
  9949. ActivityTimer t(activityStats, timeActivities);
  9950. if (eof)
  9951. return NULL;
  9952. if (!gathered)
  9953. {
  9954. aggregated.start(rowAllocator, ctx->queryCodeContext(), activityId);
  9955. bool eog = true;
  9956. for (;;)
  9957. {
  9958. const void * next = inputStream->nextRow();
  9959. if (!next)
  9960. {
  9961. if (isGroupedAggregate)
  9962. {
  9963. if (eog)
  9964. eof = true;
  9965. break;
  9966. }
  9967. next = inputStream->nextRow();
  9968. if (!next)
  9969. break;
  9970. }
  9971. eog = false;
  9972. aggregated.addRow(next);
  9973. ReleaseRoxieRow(next);
  9974. }
  9975. gathered = true;
  9976. }
  9977. Owned<AggregateRowBuilder> next = aggregated.nextResult();
  9978. if (next)
  9979. {
  9980. processed++;
  9981. return next->finalizeRowClear();
  9982. }
  9983. if (!isGroupedAggregate)
  9984. eof = true;
  9985. aggregated.reset();
  9986. gathered = false;
  9987. return NULL;
  9988. }
  9989. };
  9990. class CRoxieServerHashAggregateActivityFactory : public CRoxieServerActivityFactory
  9991. {
  9992. public:
  9993. CRoxieServerHashAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  9994. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  9995. {
  9996. isGroupedAggregate = _graphNode.getPropBool("att[@name='grouped']/@value");
  9997. }
  9998. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  9999. {
  10000. return new CRoxieServerHashAggregateActivity(_ctx, this, isGroupedAggregate, _probeManager);
  10001. }
  10002. protected:
  10003. bool isGroupedAggregate;
  10004. };
  10005. IRoxieServerActivityFactory *createRoxieServerHashAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  10006. {
  10007. return new CRoxieServerHashAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  10008. }
  10009. //=================================================================================
  10010. class CRoxieServerDegroupActivity : public CRoxieServerActivity
  10011. {
  10012. bool eof;
  10013. public:
  10014. CRoxieServerDegroupActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  10015. : CRoxieServerActivity(_ctx, _factory, _probeManager)
  10016. {
  10017. eof = false;
  10018. }
  10019. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  10020. {
  10021. eof = false;
  10022. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  10023. }
  10024. virtual const void * nextRow()
  10025. {
  10026. ActivityTimer t(activityStats, timeActivities);
  10027. if (eof)
  10028. return NULL;
  10029. const void * ret = inputStream->ungroupedNextRow();
  10030. if (ret)
  10031. processed++;
  10032. else
  10033. eof = true;
  10034. return ret;
  10035. }
  10036. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  10037. {
  10038. ActivityTimer t(activityStats, timeActivities);
  10039. if (eof)
  10040. return NULL;
  10041. const void * ret = inputStream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  10042. if (ret)
  10043. processed++;
  10044. else
  10045. eof = true;
  10046. return ret;
  10047. }
  10048. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  10049. {
  10050. return input->gatherConjunctions(collector);
  10051. }
  10052. virtual void resetEOF()
  10053. {
  10054. eof = false;
  10055. inputStream->resetEOF();
  10056. }
  10057. IInputSteppingMeta * querySteppingMeta()
  10058. {
  10059. return input->querySteppingMeta();
  10060. }
  10061. };
  10062. class CRoxieServerDegroupActivityFactory : public CRoxieServerActivityFactory
  10063. {
  10064. public:
  10065. CRoxieServerDegroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  10066. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  10067. {
  10068. }
  10069. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  10070. {
  10071. return new CRoxieServerDegroupActivity(_ctx, this, _probeManager);
  10072. }
  10073. };
  10074. IRoxieServerActivityFactory *createRoxieServerDegroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  10075. {
  10076. return new CRoxieServerDegroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  10077. }
  10078. //=================================================================================
  10079. class CRoxieServerSpillReadActivity : public CRoxieServerActivity
  10080. {
  10081. IHThorDiskReadArg &helper;
  10082. bool needTransform;
  10083. bool eof;
  10084. bool anyThisGroup;
  10085. unsigned __int64 rowLimit;
  10086. unsigned __int64 choosenLimit;
  10087. public:
  10088. CRoxieServerSpillReadActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  10089. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorDiskReadArg &)basehelper)
  10090. {
  10091. needTransform = helper.needTransform();
  10092. rowLimit = (unsigned __int64) -1;
  10093. choosenLimit = 0;
  10094. eof = false;
  10095. anyThisGroup = false;
  10096. }
  10097. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  10098. {
  10099. anyThisGroup = false;
  10100. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  10101. if (helper.canMatchAny())
  10102. eof = false;
  10103. else
  10104. eof = true;
  10105. choosenLimit = helper.getChooseNLimit();
  10106. rowLimit = helper.getRowLimit();
  10107. helper.setCallback(NULL); // members should not be called - change if they are
  10108. }
  10109. virtual bool needsAllocator() const { return true; }
  10110. virtual const void * nextRow()
  10111. {
  10112. ActivityTimer t(activityStats, timeActivities);
  10113. if (eof)
  10114. return NULL;
  10115. if (processed==choosenLimit)
  10116. {
  10117. eof = true;
  10118. return NULL;
  10119. }
  10120. if (needTransform)
  10121. {
  10122. for (;;)
  10123. {
  10124. OwnedConstRoxieRow in = inputStream->nextRow();
  10125. if (!in)
  10126. {
  10127. if (anyThisGroup)
  10128. {
  10129. anyThisGroup = false;
  10130. return NULL;
  10131. }
  10132. in.setown(inputStream->nextRow());
  10133. if (!in)
  10134. {
  10135. eof = true;
  10136. return NULL; // eof...
  10137. }
  10138. }
  10139. if (likely(helper.canMatch(in)))
  10140. {
  10141. unsigned outSize;
  10142. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  10143. try
  10144. {
  10145. outSize = helper.transform(rowBuilder, in);
  10146. }
  10147. catch (IException *E)
  10148. {
  10149. throw makeWrappedException(E);
  10150. }
  10151. if (outSize)
  10152. {
  10153. anyThisGroup = true;
  10154. processed++;
  10155. if (processed==rowLimit)
  10156. {
  10157. if (traceLevel > 4)
  10158. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  10159. helper.onLimitExceeded();
  10160. }
  10161. return rowBuilder.finalizeRowClear(outSize);
  10162. }
  10163. }
  10164. }
  10165. }
  10166. else
  10167. {
  10168. const void *ret = inputStream->nextRow();
  10169. if (ret)
  10170. {
  10171. processed++;
  10172. if (processed==rowLimit)
  10173. {
  10174. if (traceLevel > 4)
  10175. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  10176. ReleaseClearRoxieRow(ret);
  10177. helper.onLimitExceeded(); // should not return
  10178. throwUnexpected();
  10179. }
  10180. }
  10181. return ret;
  10182. }
  10183. }
  10184. };
  10185. class CRoxieServerSpillReadActivityFactory : public CRoxieServerActivityFactory
  10186. {
  10187. public:
  10188. CRoxieServerSpillReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  10189. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  10190. {
  10191. }
  10192. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  10193. {
  10194. return new CRoxieServerSpillReadActivity(_ctx, this, _probeManager);
  10195. }
  10196. virtual void addDependency(unsigned source, ThorActivityKind sourceKind, unsigned sourceIdx, int controlId, const char *edgeId)
  10197. {
  10198. if (sourceKind==TAKspill || sourceKind==TAKdiskwrite || sourceKind==TAKspillwrite) // Bit of a hack - codegen probably should differentiate
  10199. setInput(0, source, sourceIdx);
  10200. else
  10201. CRoxieServerActivityFactory::addDependency(source, kind, sourceIdx, controlId, edgeId);
  10202. }
  10203. };
  10204. IRoxieServerActivityFactory *createRoxieServerSpillReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  10205. {
  10206. return new CRoxieServerSpillReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  10207. }
  10208. //=================================================================================
  10209. class CRoxieServerSpillWriteActivity : public CRoxieServerActivity
  10210. {
  10211. public:
  10212. CRoxieServerSpillWriteActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  10213. : CRoxieServerActivity(_ctx, _factory, _probeManager)
  10214. {
  10215. }
  10216. ~CRoxieServerSpillWriteActivity()
  10217. {
  10218. }
  10219. virtual const void *nextRow()
  10220. {
  10221. ActivityTimer t(activityStats, timeActivities);
  10222. return inputStream->nextRow();
  10223. }
  10224. };
  10225. //==================================================================================
  10226. class CRoxieServerDiskWriteActivity : public CRoxieServerInternalSinkActivity, implements IRoxiePublishCallback
  10227. {
  10228. protected:
  10229. Owned<IExtRowWriter> outSeq;
  10230. Owned<IOutputRowSerializer> rowSerializer;
  10231. Linked<IFileIOStream> diskout;
  10232. bool blockcompressed;
  10233. bool extend;
  10234. bool overwrite;
  10235. bool encrypted;
  10236. bool grouped;
  10237. IHThorDiskWriteArg &helper;
  10238. Owned<IRecordSize> diskmeta;
  10239. Owned<IRoxieWriteHandler> writer;
  10240. bool tallycrc;
  10241. unsigned __int64 uncompressedBytesWritten;
  10242. CRC32 crc;
  10243. void updateWorkUnitResult(unsigned __int64 reccount)
  10244. {
  10245. assertex(writer);
  10246. // MORE - a lot of this is common with hthor
  10247. WorkunitUpdate wu = ctx->updateWorkUnit();
  10248. if (wu)
  10249. {
  10250. OwnedRoxieString rawLogicalName = helper.getFileName();
  10251. StringBuffer lfn; // logical filename
  10252. expandLogicalFilename(lfn, rawLogicalName, wu, false, false);
  10253. if (lfn.length())
  10254. {
  10255. unsigned flags = helper.getFlags();
  10256. WUFileKind fileKind;
  10257. if (TDXtemporary & flags)
  10258. fileKind = WUFileTemporary;
  10259. else if(TDXjobtemp & flags)
  10260. fileKind = WUFileJobOwned;
  10261. else if(TDWowned & flags)
  10262. fileKind = WUFileOwned;
  10263. else
  10264. fileKind = WUFileStandard;
  10265. StringArray clusters;
  10266. writer->getClusters(clusters);
  10267. wu->addFile(lfn.str(), &clusters, helper.getTempUsageCount(), fileKind, NULL);
  10268. if (!(flags & TDXtemporary) && helper.getSequence() >= 0)
  10269. {
  10270. Owned<IWUResult> result = wu->updateResultBySequence(helper.getSequence());
  10271. if (result)
  10272. {
  10273. result->setResultTotalRowCount(reccount);
  10274. result->setResultStatus(ResultStatusCalculated);
  10275. if (helper.getFlags() & TDWresult)
  10276. result->setResultFilename(lfn.str());
  10277. else
  10278. result->setResultLogicalName(lfn.str());
  10279. }
  10280. }
  10281. }
  10282. }
  10283. }
  10284. void resolve()
  10285. {
  10286. OwnedRoxieString rawLogicalName = helper.getFileName();
  10287. assertex(rawLogicalName);
  10288. assertex((helper.getFlags() & TDXtemporary) == 0);
  10289. StringArray clusters;
  10290. unsigned clusterIdx = 0;
  10291. while(true)
  10292. {
  10293. OwnedRoxieString cluster(helper.getCluster(clusterIdx));
  10294. if(!cluster)
  10295. break;
  10296. if (isContainerized())
  10297. throw makeStringException(0, "Output clusters not supported in cloud environment");
  10298. clusters.append(cluster);
  10299. clusterIdx++;
  10300. }
  10301. if (clusters.length())
  10302. {
  10303. if (extend)
  10304. throw MakeStringException(0, "Cannot combine EXTEND and CLUSTER flags on disk write of file %s", rawLogicalName.get());
  10305. }
  10306. else
  10307. {
  10308. if (isContainerized() && fileNameServiceDali)
  10309. {
  10310. StringBuffer nasGroupName;
  10311. queryNamedGroupStore().getNasGroupName(nasGroupName, 1);
  10312. clusters.append(nasGroupName);
  10313. }
  10314. else if (roxieName.length())
  10315. clusters.append(roxieName.str());
  10316. else
  10317. clusters.append(".");
  10318. }
  10319. // Using defaultPrivilegedUser as restricted files applies to limits reading of file (not writing of files)
  10320. writer.setown(ctx->createLFN(rawLogicalName, overwrite, extend, clusters, defaultPrivilegedUser));
  10321. // MORE - need to check somewhere that single part if it's an existing file or an external one...
  10322. }
  10323. public:
  10324. CRoxieServerDiskWriteActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  10325. : CRoxieServerInternalSinkActivity(_ctx, _factory, _probeManager, 0), helper((IHThorDiskWriteArg &) basehelper)
  10326. {
  10327. extend = ((helper.getFlags() & TDWextend) != 0);
  10328. overwrite = ((helper.getFlags() & TDWoverwrite) != 0);
  10329. grouped = (helper.getFlags() & TDXgrouped) != 0;
  10330. diskmeta.set(helper.queryDiskRecordSize()->querySerializedDiskMeta());
  10331. if (grouped)
  10332. diskmeta.setown(createDeltaRecordSize(diskmeta, +1));
  10333. size32_t fixedSize = diskmeta->getFixedSize();
  10334. blockcompressed = (((helper.getFlags() & TDWnewcompress) != 0) || (((helper.getFlags() & TDXcompress) != 0) && ((0 == fixedSize) || (fixedSize >= MIN_ROWCOMPRESS_RECSIZE)))); //always use new compression
  10335. encrypted = false; // set later
  10336. tallycrc = true;
  10337. uncompressedBytesWritten = 0;
  10338. }
  10339. ~CRoxieServerDiskWriteActivity()
  10340. {
  10341. }
  10342. virtual bool needsAllocator() const
  10343. {
  10344. return true;
  10345. }
  10346. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  10347. {
  10348. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  10349. resolve();
  10350. Owned<IFileIO> io;
  10351. void *ekey;
  10352. size32_t ekeylen;
  10353. helper.getEncryptKey(ekeylen, ekey);
  10354. Owned<ICompressor> ecomp;
  10355. if (ekeylen!=0)
  10356. {
  10357. ecomp.setown(createAESCompressor256(ekeylen,ekey));
  10358. memset(ekey,0,ekeylen);
  10359. rtlFree(ekey);
  10360. encrypted = true;
  10361. blockcompressed = true;
  10362. }
  10363. if (blockcompressed)
  10364. io.setown(createCompressedFileWriter(writer->queryFile(), (diskmeta->isFixedSize() ? diskmeta->getFixedSize() : 0), extend, true, ecomp, COMPRESS_METHOD_LZW));
  10365. else
  10366. io.setown(writer->queryFile()->open(extend ? IFOwrite : IFOcreate));
  10367. if (!io)
  10368. throw MakeStringException(errno, "Failed to create%s file %s for writing", (encrypted ? " encrypted" : (blockcompressed ? " compressed" : "")), writer->queryFile()->queryFilename());
  10369. diskout.setown(createBufferedIOStream(io));
  10370. if (extend)
  10371. diskout->seek(0, IFSend);
  10372. tallycrc = !blockcompressed; // MORE: Should this be controlled by an activity hint/flag?
  10373. Owned<IRowInterfaces> rowIf = createRowInterfaces(input->queryOutputMeta(), activityId, factory->getHeapFlags(), ctx->queryCodeContext());
  10374. rowSerializer.set(rowIf->queryRowSerializer());
  10375. unsigned rwFlags = rw_autoflush;
  10376. if(grouped)
  10377. rwFlags |= rw_grouped;
  10378. if(tallycrc)
  10379. rwFlags |= rw_crc;
  10380. outSeq.setown(createRowWriter(diskout, rowIf, rwFlags));
  10381. }
  10382. virtual void stop()
  10383. {
  10384. if (aborted)
  10385. {
  10386. if (writer)
  10387. writer->finish(false, this);
  10388. }
  10389. else
  10390. {
  10391. if (outSeq)
  10392. outSeq->flush(&crc);
  10393. if (outSeq)
  10394. uncompressedBytesWritten = outSeq->getPosition();
  10395. outSeq.clear();
  10396. diskout.clear(); // Make sure file is properly closed or date may not match published info
  10397. if (writer)
  10398. {
  10399. updateWorkUnitResult(processed);
  10400. writer->finish(true, this);
  10401. }
  10402. }
  10403. writer.clear();
  10404. CRoxieServerActivity::stop();
  10405. }
  10406. virtual void reset()
  10407. {
  10408. CRoxieServerInternalSinkActivity::reset();
  10409. diskout.clear();
  10410. outSeq.clear();
  10411. writer.clear();
  10412. uncompressedBytesWritten = 0;
  10413. crc.reset();
  10414. }
  10415. virtual void onExecute()
  10416. {
  10417. for (;;)
  10418. {
  10419. const void *row = inputStream->nextRow();
  10420. if (!row)
  10421. {
  10422. row = inputStream->nextRow();
  10423. if (!row)
  10424. break;
  10425. if (grouped)
  10426. outSeq->putRow(NULL);
  10427. }
  10428. processed++;
  10429. outSeq->putRow(row);
  10430. }
  10431. }
  10432. virtual void setFileProperties(IFileDescriptor *desc) const
  10433. {
  10434. IPropertyTree &partProps = desc->queryPart(0)->queryProperties(); //properties of the first file part.
  10435. IPropertyTree &fileProps = desc->queryProperties(); // properties of the logical file
  10436. if (blockcompressed)
  10437. {
  10438. // caller has already set @size from file size...
  10439. fileProps.setPropBool("@blockCompressed", true);
  10440. fileProps.setPropInt64("@compressedSize", partProps.getPropInt64("@size", 0));
  10441. partProps.setPropInt64("@compressedSize", partProps.getPropInt64("@size", 0));
  10442. fileProps.setPropInt64("@size", uncompressedBytesWritten);
  10443. partProps.setPropInt64("@size", uncompressedBytesWritten);
  10444. }
  10445. else if (tallycrc && crc.get())
  10446. partProps.setPropInt64("@fileCrc", crc.get());
  10447. if (encrypted)
  10448. fileProps.setPropBool("@encrypted", true);
  10449. fileProps.setPropInt64("@recordCount", processed);
  10450. partProps.setPropInt64("@recordCount", processed);
  10451. unsigned flags = helper.getFlags();
  10452. if (flags & TDWpersist)
  10453. fileProps.setPropBool("@persistent", true);
  10454. if (grouped)
  10455. fileProps.setPropBool("@grouped", true);
  10456. if (flags & (TDWowned|TDXjobtemp|TDXtemporary))
  10457. fileProps.setPropBool("@owned", true);
  10458. if (flags & TDWresult)
  10459. fileProps.setPropBool("@result", true);
  10460. IConstWorkUnit *workUnit = ctx->queryWorkUnit();
  10461. if (workUnit)
  10462. {
  10463. fileProps.setProp("@owner", workUnit->queryUser());
  10464. fileProps.setProp("@workunit", workUnit->queryWuid());
  10465. fileProps.setProp("@job", workUnit->queryJobName());
  10466. }
  10467. if (flags & TDWexpires)
  10468. setExpiryTime(fileProps, helper.getExpiryDays());
  10469. if (flags & TDWupdate)
  10470. {
  10471. unsigned eclCRC;
  10472. unsigned __int64 totalCRC;
  10473. helper.getUpdateCRCs(eclCRC, totalCRC);
  10474. fileProps.setPropInt("@eclCRC", eclCRC);
  10475. fileProps.setPropInt64("@totalCRC", totalCRC);
  10476. }
  10477. fileProps.setPropInt("@formatCrc", helper.getFormatCrc());
  10478. if (flags & TDWrestricted)
  10479. fileProps.setPropBool("restricted", true);
  10480. IRecordSize * inputMeta = input->queryOutputMeta();
  10481. if ((inputMeta->isFixedSize()) && !isOutputTransformed())
  10482. fileProps.setPropInt("@recordSize", inputMeta->getFixedSize() + (grouped ? 1 : 0));
  10483. const char *recordECL = helper.queryRecordECL();
  10484. if (recordECL && *recordECL)
  10485. fileProps.setProp("ECL", recordECL);
  10486. setRtlFormat(fileProps, helper.queryDiskRecordSize());
  10487. fileProps.setProp("@kind", "flat"); // default, derivitives may override
  10488. }
  10489. virtual IUserDescriptor *queryUserDescriptor() const
  10490. {
  10491. IConstWorkUnit *workUnit = ctx->queryWorkUnit();
  10492. if (workUnit)
  10493. return workUnit->queryUserDescriptor();//ad-hoc mode
  10494. else
  10495. {
  10496. Owned<IRoxieDaliHelper> daliHelper = connectToDali(false);
  10497. if (daliHelper)
  10498. return daliHelper->queryUserDescriptor();//predeployed query mode
  10499. }
  10500. return NULL;
  10501. }
  10502. virtual bool isOutputTransformed() const { return false; }
  10503. };
  10504. //=================================================================================
  10505. class CRoxieServerCsvWriteActivity : public CRoxieServerDiskWriteActivity
  10506. {
  10507. IHThorCsvWriteArg &csvHelper;
  10508. CSVOutputStream csvOutput;
  10509. public:
  10510. CRoxieServerCsvWriteActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  10511. : CRoxieServerDiskWriteActivity(_ctx, _factory, _probeManager), csvHelper(static_cast<IHThorCsvWriteArg &>(helper))
  10512. {
  10513. csvOutput.init(csvHelper.queryCsvParameters(), false);
  10514. }
  10515. virtual void onExecute()
  10516. {
  10517. OwnedRoxieString header(csvHelper.queryCsvParameters()->getHeader());
  10518. if (header)
  10519. {
  10520. csvOutput.beginLine();
  10521. csvOutput.writeHeaderLn(strlen(header), header);
  10522. diskout->write(csvOutput.length(), csvOutput.str());
  10523. }
  10524. for (;;)
  10525. {
  10526. const void *nextrec = inputStream->ungroupedNextRow();
  10527. if (!nextrec)
  10528. break;
  10529. processed++;
  10530. csvOutput.beginLine();
  10531. csvHelper.writeRow((const byte *)nextrec, &csvOutput);
  10532. csvOutput.endLine();
  10533. diskout->write(csvOutput.length(), csvOutput.str());
  10534. ReleaseRoxieRow(nextrec);
  10535. }
  10536. OwnedRoxieString footer(csvHelper.queryCsvParameters()->getFooter());
  10537. if (footer)
  10538. {
  10539. csvOutput.beginLine();
  10540. csvOutput.writeHeaderLn(strlen(footer), footer);
  10541. diskout->write(csvOutput.length(), csvOutput.str());
  10542. }
  10543. }
  10544. virtual void setFileProperties(IFileDescriptor *desc) const
  10545. {
  10546. CRoxieServerDiskWriteActivity::setFileProperties(desc);
  10547. IPropertyTree &props = desc->queryProperties();
  10548. props.setProp("@format","utf8n");
  10549. ICsvParameters *csvParameters = csvHelper.queryCsvParameters();
  10550. StringBuffer separator;
  10551. OwnedRoxieString rs(csvParameters->getSeparator(0));
  10552. const char *s = rs;
  10553. while (s && *s)
  10554. {
  10555. if (',' == *s)
  10556. separator.append("\\,");
  10557. else
  10558. separator.append(*s);
  10559. ++s;
  10560. }
  10561. props.setProp("@csvSeparate", separator.str());
  10562. props.setProp("@csvQuote", rs.setown(csvParameters->getQuote(0)));
  10563. props.setProp("@csvTerminate", rs.setown(csvParameters->getTerminator(0)));
  10564. props.setProp("@csvEscape", rs.setown(csvParameters->getEscape(0)));
  10565. props.setProp("@kind", "csv");
  10566. }
  10567. virtual bool isOutputTransformed() const { return true; }
  10568. };
  10569. class CRoxieServerXmlWriteActivity : public CRoxieServerDiskWriteActivity
  10570. {
  10571. IHThorXmlWriteArg &xmlHelper;
  10572. StringAttr rowTag;
  10573. ThorActivityKind kind;
  10574. unsigned headerLength;
  10575. unsigned footerLength;
  10576. public:
  10577. CRoxieServerXmlWriteActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, ThorActivityKind _kind)
  10578. : CRoxieServerDiskWriteActivity(_ctx, _factory, _probeManager), xmlHelper(static_cast<IHThorXmlWriteArg &>(helper)), kind(_kind), headerLength(0), footerLength(0)
  10579. {
  10580. }
  10581. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  10582. {
  10583. CRoxieServerDiskWriteActivity::start(parentExtractSize, parentExtract, paused);
  10584. OwnedRoxieString xmlpath(xmlHelper.getXmlIteratorPath());
  10585. if (!xmlpath)
  10586. rowTag.set(DEFAULTXMLROWTAG);
  10587. else
  10588. {
  10589. const char *path = xmlpath;
  10590. if (*path == '/') path++;
  10591. if (strchr(path, '/')) UNIMPLEMENTED; // more what do we do with /mydata/row
  10592. rowTag.set(path);
  10593. }
  10594. }
  10595. virtual void onExecute()
  10596. {
  10597. StringBuffer header;
  10598. OwnedRoxieString suppliedHeader(xmlHelper.getHeader());
  10599. if (kind==TAKjsonwrite)
  10600. buildJsonHeader(header, suppliedHeader, rowTag);
  10601. else if (suppliedHeader)
  10602. header.set(suppliedHeader);
  10603. else
  10604. header.append(DEFAULTXMLHEADER).newline();
  10605. headerLength = header.length();
  10606. diskout->write(header.length(), header.str());
  10607. Owned<IXmlWriterExt> writer = createIXmlWriterExt(xmlHelper.getXmlFlags(), 0, NULL, (kind==TAKjsonwrite) ? WTJSONRootless : WTStandard);
  10608. writer->outputBeginArray(rowTag); //need to set this
  10609. writer->clear(); //but not output it
  10610. for (;;)
  10611. {
  10612. OwnedConstRoxieRow nextrec = inputStream->ungroupedNextRow();
  10613. if (!nextrec)
  10614. break;
  10615. processed++;
  10616. writer->clear().outputBeginNested(rowTag, false);
  10617. xmlHelper.toXML((const byte *)nextrec.get(), *writer);
  10618. writer->outputEndNested(rowTag);
  10619. diskout->write(writer->length(), writer->str());
  10620. }
  10621. OwnedRoxieString suppliedFooter(xmlHelper.getFooter());
  10622. StringBuffer footer;
  10623. if (kind==TAKjsonwrite)
  10624. buildJsonFooter(footer.newline(), suppliedFooter, rowTag);
  10625. else if (suppliedFooter)
  10626. footer.append(suppliedFooter);
  10627. else
  10628. footer.append(DEFAULTXMLFOOTER);
  10629. footerLength=footer.length();
  10630. diskout->write(footer.length(), footer);
  10631. }
  10632. virtual void reset()
  10633. {
  10634. CRoxieServerDiskWriteActivity::reset();
  10635. rowTag.clear();
  10636. }
  10637. virtual void setFileProperties(IFileDescriptor *desc) const
  10638. {
  10639. CRoxieServerDiskWriteActivity::setFileProperties(desc);
  10640. desc->queryProperties().setProp("@format","utf8n");
  10641. desc->queryProperties().setProp("@rowTag",rowTag.get());
  10642. desc->queryProperties().setProp("@kind", (kind==TAKjsonwrite) ? "json" : "xml");
  10643. desc->queryProperties().setPropInt(FPheaderLength, headerLength);
  10644. desc->queryProperties().setPropInt(FPfooterLength, footerLength);
  10645. }
  10646. virtual bool isOutputTransformed() const { return true; }
  10647. };
  10648. class CRoxieServerDiskWriteActivityFactory : public CRoxieServerMultiOutputFactory
  10649. {
  10650. bool isTemp;
  10651. public:
  10652. CRoxieServerDiskWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  10653. : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  10654. {
  10655. Owned<IHThorDiskWriteArg> helper = (IHThorDiskWriteArg *) helperFactory();
  10656. isTemp = (helper->getFlags() & TDXtemporary) != 0;
  10657. setNumOutputs(helper->getTempUsageCount());
  10658. if (_kind != TAKspillwrite)
  10659. assertex(numOutputs == 0);
  10660. }
  10661. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  10662. {
  10663. switch (numOutputs)
  10664. {
  10665. case 0:
  10666. switch (kind)
  10667. {
  10668. case TAKdiskwrite:
  10669. return new CRoxieServerDiskWriteActivity(_ctx, this, _probeManager);
  10670. case TAKcsvwrite:
  10671. return new CRoxieServerCsvWriteActivity(_ctx, this, _probeManager);
  10672. case TAKxmlwrite:
  10673. case TAKjsonwrite:
  10674. return new CRoxieServerXmlWriteActivity(_ctx, this, _probeManager, kind);
  10675. case TAKspillwrite:
  10676. return new CRoxieServerNullSinkActivity(_ctx, this, _probeManager);
  10677. };
  10678. throwUnexpected();
  10679. case 1:
  10680. return new CRoxieServerSpillWriteActivity(_ctx, this, _probeManager);
  10681. default:
  10682. return new CRoxieServerThroughSpillActivity(_ctx, this, _probeManager, numOutputs);
  10683. }
  10684. }
  10685. virtual bool isSink() const
  10686. {
  10687. return numOutputs == 0 && (kind==TAKspillwrite || !isTemp); // MORE - check with Gavin if this is right if not a temp but reread in same job...
  10688. }
  10689. };
  10690. IRoxieServerActivityFactory *createRoxieServerDiskWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  10691. {
  10692. return new CRoxieServerDiskWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  10693. }
  10694. //=================================================================================
  10695. class CRoxieServerIndexWriteActivity : public CRoxieServerInternalSinkActivity, implements IRoxiePublishCallback
  10696. {
  10697. IHThorIndexWriteArg &helper;
  10698. bool overwrite;
  10699. Owned<ClusterWriteHandler> clusterHandler;
  10700. Owned<IRoxieWriteHandler> writer;
  10701. unsigned __int64 reccount;
  10702. unsigned int fileCrc;
  10703. StringBuffer filename;
  10704. unsigned __int64 duplicateKeyCount = 0;
  10705. unsigned __int64 cummulativeDuplicateKeyCount = 0;
  10706. void updateWorkUnitResult()
  10707. {
  10708. // MORE - a lot of this is common with hthor
  10709. WorkunitUpdate wu = ctx->updateWorkUnit();
  10710. if (wu)
  10711. {
  10712. OwnedRoxieString rawLogicalName = helper.getFileName();
  10713. StringBuffer lfn; // logical filename
  10714. expandLogicalFilename(lfn, rawLogicalName, wu, false, false);
  10715. if (lfn.length())
  10716. {
  10717. if (helper.getSequence() >= 0)
  10718. {
  10719. Owned<IWUResult> result = wu->updateResultBySequence(helper.getSequence());
  10720. if (result)
  10721. {
  10722. result->setResultTotalRowCount(reccount);
  10723. result->setResultStatus(ResultStatusCalculated);
  10724. result->setResultLogicalName(lfn.str());
  10725. }
  10726. }
  10727. if(clusterHandler)
  10728. clusterHandler->finish(writer->queryFile());
  10729. CTXLOG("Created roxie index file %s", lfn.str());
  10730. }
  10731. }
  10732. }
  10733. virtual void resolve()
  10734. {
  10735. StringArray clusters;
  10736. unsigned clusterIdx = 0;
  10737. while(true)
  10738. {
  10739. OwnedRoxieString cluster(helper.getCluster(clusterIdx));
  10740. if(!cluster)
  10741. break;
  10742. if (isContainerized())
  10743. throw makeStringException(0, "Output clusters not supported in cloud environment");
  10744. clusters.append(cluster);
  10745. clusterIdx++;
  10746. }
  10747. if (!clusters.length())
  10748. {
  10749. if (isContainerized() && fileNameServiceDali)
  10750. {
  10751. StringBuffer nasGroupName;
  10752. queryNamedGroupStore().getNasGroupName(nasGroupName, 1);
  10753. clusters.append(nasGroupName);
  10754. }
  10755. else if (roxieName.length())
  10756. clusters.append(roxieName.str());
  10757. else
  10758. clusters.append(".");
  10759. }
  10760. OwnedRoxieString fname(helper.getFileName());
  10761. writer.setown(ctx->createLFN(fname, overwrite, false, clusters, defaultPrivilegedUser)); // MORE - if there's a workunit, use if for scope.
  10762. filename.set(writer->queryFile()->queryFilename());
  10763. if (writer->queryFile()->exists())
  10764. {
  10765. if (overwrite)
  10766. {
  10767. CTXLOG("Removing existing %s from DFS",filename.str());
  10768. writer->queryFile()->remove();
  10769. }
  10770. else
  10771. throw MakeStringException(99, "Cannot write index file %s, file already exists (missing OVERWRITE attribute?)", filename.str());
  10772. }
  10773. }
  10774. void buildUserMetadata(Owned<IPropertyTree> & metadata)
  10775. {
  10776. size32_t nameLen;
  10777. char * nameBuff;
  10778. size32_t valueLen;
  10779. char * valueBuff;
  10780. unsigned idx = 0;
  10781. while(helper.getIndexMeta(nameLen, nameBuff, valueLen, valueBuff, idx++))
  10782. {
  10783. StringBuffer name(nameLen, nameBuff);
  10784. StringBuffer value(valueLen, valueBuff);
  10785. rtlFree(nameBuff);
  10786. rtlFree(valueBuff);
  10787. if(*name == '_' && !checkReservedMetadataName(name))
  10788. {
  10789. OwnedRoxieString fname(helper.getFileName());
  10790. throw MakeStringException(0, "Invalid name %s in user metadata for index %s (names beginning with underscore are reserved)", name.str(), fname.get());
  10791. }
  10792. if(!validateXMLTag(name.str()))
  10793. {
  10794. OwnedRoxieString fname(helper.getFileName());
  10795. throw MakeStringException(0, "Invalid name %s in user metadata for index %s (not legal XML element name)", name.str(), fname.get());
  10796. }
  10797. if(!metadata)
  10798. metadata.setown(createPTree("metadata", ipt_fast));
  10799. metadata->setProp(name.str(), value.str());
  10800. }
  10801. }
  10802. void buildLayoutMetadata(Owned<IPropertyTree> & metadata)
  10803. {
  10804. if(!metadata)
  10805. metadata.setown(createPTree("metadata", ipt_fast));
  10806. metadata->setProp("_record_ECL", helper.queryRecordECL());
  10807. setRtlFormat(*metadata, helper.queryDiskRecordSize());
  10808. }
  10809. public:
  10810. CRoxieServerIndexWriteActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  10811. : CRoxieServerInternalSinkActivity(_ctx, _factory, _probeManager, 0), helper(static_cast<IHThorIndexWriteArg &>(basehelper))
  10812. {
  10813. overwrite = ((helper.getFlags() & TIWoverwrite) != 0);
  10814. reccount = 0;
  10815. fileCrc = 0;
  10816. }
  10817. ~CRoxieServerIndexWriteActivity()
  10818. {
  10819. }
  10820. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  10821. {
  10822. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  10823. resolve();
  10824. }
  10825. virtual void onExecute()
  10826. {
  10827. bool isVariable = helper.queryDiskRecordSize()->isVariableSize();
  10828. size32_t fileposSize = hasTrailingFileposition(helper.queryDiskRecordSize()->queryTypeInfo()) ? sizeof(offset_t) : 0;
  10829. size32_t maxDiskRecordSize;
  10830. if (isVariable)
  10831. {
  10832. if (helper.getFlags() & TIWmaxlength)
  10833. maxDiskRecordSize = helper.getMaxKeySize();
  10834. else
  10835. maxDiskRecordSize = KEYBUILD_MAXLENGTH; // Current default behaviour, could be improved in the future
  10836. }
  10837. else
  10838. maxDiskRecordSize = helper.queryDiskRecordSize()->getFixedSize()-fileposSize;
  10839. if (maxDiskRecordSize > KEYBUILD_MAXLENGTH)
  10840. throw MakeStringException(99, "Index maximum record length (%d) exceeds 32k internal limit", maxDiskRecordSize);
  10841. OwnedMalloc<char> rowBuffer(maxDiskRecordSize, true);
  10842. fileCrc = -1;
  10843. {
  10844. Owned<IFileIO> io;
  10845. try
  10846. {
  10847. io.setown(writer->queryFile()->open(IFOcreate));
  10848. }
  10849. catch(IException * e)
  10850. {
  10851. e->Release();
  10852. clearKeyStoreCache(false);
  10853. io.setown(writer->queryFile()->open(IFOcreate));
  10854. }
  10855. if(!io)
  10856. throw MakeStringException(errno, "Failed to create file %s for writing", filename.str());
  10857. bool needsSeek = true;
  10858. unsigned flags = COL_PREFIX | HTREE_FULLSORT_KEY;
  10859. if (helper.getFlags() & TIWrowcompress)
  10860. flags |= HTREE_COMPRESSED_KEY|HTREE_QUICK_COMPRESSED_KEY;
  10861. else if (!(helper.getFlags() & TIWnolzwcompress))
  10862. flags |= HTREE_COMPRESSED_KEY;
  10863. if (isVariable)
  10864. flags |= HTREE_VARSIZE;
  10865. Owned<IPropertyTree> metadata;
  10866. buildUserMetadata(metadata);
  10867. buildLayoutMetadata(metadata);
  10868. unsigned nodeSize = metadata->getPropInt("_nodeSize", NODESIZE);
  10869. if (metadata->getPropBool("_noSeek", ctx->queryOptions().noSeekBuildIndex))
  10870. {
  10871. flags |= TRAILING_HEADER_ONLY;
  10872. needsSeek = false;
  10873. }
  10874. if (metadata->getPropBool("_useTrailingHeader", true))
  10875. flags |= USE_TRAILING_HEADER;
  10876. Owned<IFileIOStream> out = createBufferedIOStream(io, 0x100000);
  10877. if (!needsSeek)
  10878. out.setown(createNoSeekIOStream(out));
  10879. Owned<IKeyBuilder> builder = createKeyBuilder(out, flags, maxDiskRecordSize, nodeSize, helper.getKeyedSize(), 0, &helper, true, false);
  10880. class BcWrapper : implements IBlobCreator
  10881. {
  10882. IKeyBuilder *builder;
  10883. public:
  10884. BcWrapper(IKeyBuilder *_builder) : builder(_builder) {}
  10885. virtual unsigned __int64 createBlob(size32_t size, const void * ptr)
  10886. {
  10887. return builder->createBlob(size, (const char *) ptr);
  10888. }
  10889. } bc(builder);
  10890. // Loop thru the results
  10891. for (;;)
  10892. {
  10893. OwnedConstRoxieRow nextrec(inputStream->ungroupedNextRow());
  10894. if (!nextrec)
  10895. break;
  10896. try
  10897. {
  10898. unsigned __int64 fpos=0;
  10899. RtlStaticRowBuilder rowBuilder(rowBuffer, maxDiskRecordSize);
  10900. size32_t thisSize = helper.transform(rowBuilder, nextrec, &bc, fpos);
  10901. builder->processKeyData(rowBuffer, fpos, thisSize);
  10902. }
  10903. catch(IException * e)
  10904. {
  10905. throw makeWrappedException(e);
  10906. }
  10907. reccount++;
  10908. }
  10909. duplicateKeyCount = builder->getDuplicateCount();
  10910. cummulativeDuplicateKeyCount += duplicateKeyCount;
  10911. builder->finish(metadata, &fileCrc);
  10912. clearKeyStoreCache(false);
  10913. }
  10914. }
  10915. virtual void stop()
  10916. {
  10917. if (writer)
  10918. {
  10919. if (!aborted)
  10920. updateWorkUnitResult();
  10921. writer->finish(!aborted, this);
  10922. writer.clear();
  10923. }
  10924. CRoxieServerActivity::stop();
  10925. }
  10926. virtual void reset()
  10927. {
  10928. noteStatistic(StNumDuplicateKeys, cummulativeDuplicateKeyCount);
  10929. CRoxieServerActivity::reset();
  10930. writer.clear();
  10931. }
  10932. //interface IRoxiePublishCallback
  10933. virtual void setFileProperties(IFileDescriptor *desc) const
  10934. {
  10935. // Now publish to name services
  10936. StringBuffer dir,base;
  10937. offset_t indexFileSize = writer->queryFile()->size();
  10938. if(clusterHandler)
  10939. clusterHandler->splitPhysicalFilename(dir, base);
  10940. else
  10941. {
  10942. // Check filename is URL and get localpath, only actually necessary if force remote reads are are on
  10943. RemoteFilename rfn;
  10944. rfn.setRemotePath(filename);
  10945. StringBuffer localPath;
  10946. rfn.getLocalPath(localPath);
  10947. splitFilename(localPath, &dir, &dir, &base, &base);
  10948. }
  10949. desc->setDefaultDir(dir.str());
  10950. //properties of the first file part.
  10951. Owned<IPropertyTree> attrs;
  10952. if(clusterHandler)
  10953. attrs.setown(createPTree("Part", ipt_fast)); // clusterHandler is going to set attributes
  10954. else
  10955. {
  10956. // add cluster
  10957. StringBuffer mygroupname;
  10958. desc->setNumParts(1);
  10959. desc->setPartMask(base.str());
  10960. attrs.set(&desc->queryPart(0)->queryProperties());
  10961. }
  10962. attrs->setPropInt64("@size", indexFileSize);
  10963. attrs->setPropInt64("@recordCount", reccount);
  10964. CDateTime createTime, modifiedTime, accessedTime;
  10965. writer->queryFile()->getTime(&createTime, &modifiedTime, &accessedTime);
  10966. // round file time down to nearest sec. Nanosec accurancy is not preserved elsewhere and can lead to mismatch later.
  10967. unsigned hour, min, sec, nanosec;
  10968. modifiedTime.getTime(hour, min, sec, nanosec);
  10969. modifiedTime.setTime(hour, min, sec, 0);
  10970. StringBuffer timestr;
  10971. modifiedTime.getString(timestr);
  10972. if(timestr.length())
  10973. attrs->setProp("@modified", timestr.str());
  10974. if(clusterHandler)
  10975. clusterHandler->setDescriptorParts(desc, base.str(), attrs);
  10976. // properties of the logical file
  10977. IPropertyTree & properties = desc->queryProperties();
  10978. properties.setProp("@kind", "key");
  10979. properties.setPropInt64("@size", indexFileSize);
  10980. properties.setPropInt64("@recordCount", reccount);
  10981. properties.setPropInt64("@duplicateKeyCount", duplicateKeyCount);
  10982. WorkunitUpdate workUnit = ctx->updateWorkUnit();
  10983. if (workUnit)
  10984. {
  10985. properties.setProp("@owner", workUnit->queryUser());
  10986. properties.setProp("@workunit", workUnit->queryWuid());
  10987. properties.setProp("@job", workUnit->queryJobName());
  10988. }
  10989. char const * rececl = helper.queryRecordECL();
  10990. if(rececl && *rececl)
  10991. properties.setProp("ECL", rececl);
  10992. if (helper.getFlags() & TIWexpires)
  10993. setExpiryTime(properties, helper.getExpiryDays());
  10994. if (helper.getFlags() & TIWupdate)
  10995. {
  10996. unsigned eclCRC;
  10997. unsigned __int64 totalCRC;
  10998. helper.getUpdateCRCs(eclCRC, totalCRC);
  10999. properties.setPropInt("@eclCRC", eclCRC);
  11000. properties.setPropInt64("@totalCRC", totalCRC);
  11001. }
  11002. properties.setPropInt("@fileCrc", fileCrc);
  11003. properties.setPropInt("@formatCrc", helper.getFormatCrc());
  11004. // Legacy record layout info
  11005. void * layoutMetaBuff;
  11006. size32_t layoutMetaSize;
  11007. if(helper.getIndexLayout(layoutMetaSize, layoutMetaBuff))
  11008. {
  11009. properties.setPropBin("_record_layout", layoutMetaSize, layoutMetaBuff);
  11010. rtlFree(layoutMetaBuff);
  11011. }
  11012. if (helper.getFlags() & TIWrestricted)
  11013. properties.setPropBool("restricted", true);
  11014. // New record layout info
  11015. setRtlFormat(properties, helper.queryDiskRecordSize());
  11016. // Bloom info
  11017. const IBloomBuilderInfo * const *bloomFilters = helper.queryBloomInfo();
  11018. while (bloomFilters && *bloomFilters)
  11019. {
  11020. const IBloomBuilderInfo *info = *bloomFilters++;
  11021. IPropertyTree *bloom = properties.addPropTree("Bloom");
  11022. bloom->setPropInt64("@bloomFieldMask", info->getBloomFields());
  11023. bloom->setPropInt64("@bloomLimit", info->getBloomLimit()); // MORE - if we didn't actually build because of the limit that might be interesting. Though that's going to vary by part.
  11024. VStringBuffer pval("%f", info->getBloomProbability());
  11025. bloom->setProp("@bloomProbability", pval.str());
  11026. }
  11027. }
  11028. IUserDescriptor *queryUserDescriptor() const
  11029. {
  11030. IConstWorkUnit *workUnit = ctx->queryWorkUnit();
  11031. if (workUnit)
  11032. return workUnit->queryUserDescriptor();//ad-hoc mode
  11033. else
  11034. {
  11035. Owned<IRoxieDaliHelper> daliHelper = connectToDali(false);
  11036. if (daliHelper)
  11037. return daliHelper->queryUserDescriptor();//predeployed query mode
  11038. }
  11039. return NULL;
  11040. }
  11041. };
  11042. //=================================================================================
  11043. class CRoxieServerIndexWriteActivityFactory : public CRoxieServerMultiOutputFactory
  11044. {
  11045. public:
  11046. CRoxieServerIndexWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  11047. : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  11048. {
  11049. setNumOutputs(0);
  11050. }
  11051. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  11052. {
  11053. return new CRoxieServerIndexWriteActivity(_ctx, this, _probeManager);
  11054. }
  11055. virtual bool isSink() const
  11056. {
  11057. return true;
  11058. }
  11059. virtual const StatisticsMapping &queryStatsMapping() const
  11060. {
  11061. return indexWriteStatistics; // Overridden by anyone that needs more
  11062. }
  11063. };
  11064. IRoxieServerActivityFactory *createRoxieServerIndexWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  11065. {
  11066. return new CRoxieServerIndexWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _isRoot);
  11067. }
  11068. //=================================================================================
  11069. static inline void getLimitType(unsigned flags, bool & limitFail, bool & limitOnFail)
  11070. {
  11071. if((flags & JFmatchAbortLimitSkips) != 0)
  11072. {
  11073. limitFail = false;
  11074. limitOnFail = false;
  11075. }
  11076. else
  11077. {
  11078. limitOnFail = ((flags & JFonfail) != 0);
  11079. limitFail = !limitOnFail;
  11080. }
  11081. }
  11082. class CRoxieServerJoinActivity : public CRoxieServerTwoInputActivity
  11083. {
  11084. enum { JSfill, JSfillleft, JSfillright, JScollate, JScompare, JSleftonly, JSrightonly } state;
  11085. IHThorJoinArg &helper;
  11086. ICompare * collate;
  11087. ICompare * collateupper;
  11088. ThorActivityKind activityKind;
  11089. bool leftOuterJoin;
  11090. bool rightOuterJoin;
  11091. bool exclude;
  11092. bool limitFail;
  11093. bool limitOnFail;
  11094. bool forceSpill;
  11095. unsigned keepLimit;
  11096. unsigned joinLimit;
  11097. unsigned atmostLimit;
  11098. unsigned abortLimit;
  11099. unsigned atmostsTriggered;
  11100. bool betweenjoin;
  11101. OwnedRowArray right;
  11102. const void * left;
  11103. const void * pendingRight;
  11104. unsigned rightIndex;
  11105. unsigned joinCounter;
  11106. BoolArray matchedRight;
  11107. bool matchedLeft;
  11108. Owned<IException> failingLimit;
  11109. ConstPointerArray filteredRight;
  11110. Owned<IRHLimitedCompareHelper> limitedhelper;
  11111. OwnedConstRoxieRow defaultLeft;
  11112. OwnedConstRoxieRow defaultRight;
  11113. Owned<IEngineRowAllocator> defaultLeftAllocator;
  11114. Owned<IEngineRowAllocator> defaultRightAllocator;
  11115. Owned<IGroupedInput> sortedLeft;
  11116. Owned<IGroupedInput> groupedSortedRight;
  11117. bool cloneLeft;
  11118. void createDefaultLeft()
  11119. {
  11120. if (!defaultLeft)
  11121. {
  11122. if (!defaultLeftAllocator)
  11123. defaultLeftAllocator.setown(createRowAllocator(input->queryOutputMeta()));
  11124. RtlDynamicRowBuilder rowBuilder(defaultLeftAllocator);
  11125. size32_t thisSize = helper.createDefaultLeft(rowBuilder);
  11126. defaultLeft.setown(rowBuilder.finalizeRowClear(thisSize));
  11127. }
  11128. }
  11129. void createDefaultRight()
  11130. {
  11131. if (!defaultRight)
  11132. {
  11133. if (!defaultRightAllocator)
  11134. defaultRightAllocator.setown(createRowAllocator(input1->queryOutputMeta()));
  11135. RtlDynamicRowBuilder rowBuilder(defaultRightAllocator);
  11136. size32_t thisSize = helper.createDefaultRight(rowBuilder);
  11137. defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
  11138. }
  11139. }
  11140. public:
  11141. CRoxieServerJoinActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _forceSpill)
  11142. : CRoxieServerTwoInputActivity(_ctx, _factory, _probeManager), helper((IHThorJoinArg &)basehelper), forceSpill(_forceSpill)
  11143. {
  11144. // MORE - some of this should be done in factory
  11145. unsigned joinFlags = helper.getJoinFlags();
  11146. leftOuterJoin = (joinFlags & JFleftouter) != 0;
  11147. rightOuterJoin = (joinFlags & JFrightouter) != 0;
  11148. exclude = (joinFlags & JFexclude) != 0;
  11149. cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
  11150. getLimitType(joinFlags, limitFail, limitOnFail);
  11151. if (joinFlags & JFslidingmatch)
  11152. {
  11153. betweenjoin = true;
  11154. collate = helper.queryCompareLeftRightLower();
  11155. collateupper = helper.queryCompareLeftRightUpper();
  11156. }
  11157. else
  11158. {
  11159. betweenjoin = false;
  11160. collate = collateupper = helper.queryCompareLeftRight();
  11161. }
  11162. rightIndex = 0;
  11163. joinCounter = 0;
  11164. state = JSfill;
  11165. matchedLeft = false;
  11166. joinLimit = 0;
  11167. keepLimit = 0; // wait until ctx available
  11168. atmostLimit = 0; // wait until ctx available
  11169. abortLimit = 0; // wait until ctx available
  11170. atmostsTriggered = 0;
  11171. assertex((joinFlags & (JFfirst | JFfirstleft | JFfirstright)) == 0);
  11172. left = NULL;
  11173. pendingRight = NULL;
  11174. activityKind = _factory->getKind();
  11175. }
  11176. virtual bool needsAllocator() const { return true; }
  11177. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  11178. {
  11179. left = NULL;
  11180. rightIndex = 0;
  11181. joinCounter = 0;
  11182. state = JSfill;
  11183. matchedLeft = false;
  11184. CRoxieServerTwoInputActivity::start(parentExtractSize, parentExtract, paused);
  11185. keepLimit = helper.getKeepLimit();
  11186. if (keepLimit == 0)
  11187. keepLimit = (unsigned)-1;
  11188. atmostsTriggered = 0;
  11189. atmostLimit = helper.getJoinLimit();
  11190. if(atmostLimit == 0)
  11191. atmostLimit = (unsigned)-1;
  11192. else
  11193. assertex(!rightOuterJoin && !betweenjoin);
  11194. abortLimit = helper.getMatchAbortLimit();
  11195. if (abortLimit == 0)
  11196. abortLimit = (unsigned)-1;
  11197. if (rightOuterJoin)
  11198. createDefaultLeft();
  11199. if ((leftOuterJoin && (activityKind==TAKjoin || activityKind==TAKjoinlight || activityKind==TAKdenormalizegroup)) || limitOnFail)
  11200. createDefaultRight();
  11201. bool isStable = (helper.getJoinFlags() & JFunstable) == 0;
  11202. RoxieSortAlgorithm sortAlgorithm;
  11203. if (forceSpill)
  11204. sortAlgorithm = isStable ? stableSpillingQuickSortAlgorithm : spillingQuickSortAlgorithm;
  11205. else
  11206. sortAlgorithm = isStable ? stableQuickSortAlgorithm : quickSortAlgorithm;
  11207. if (helper.isLeftAlreadySorted())
  11208. sortedLeft.setown(createDegroupedInputReader(inputStream));
  11209. else
  11210. sortedLeft.setown(createSortedInputReader(inputStream, createSortAlgorithm(sortAlgorithm, helper.queryCompareLeft(), ctx->queryRowManager(), input->queryOutputMeta(), ctx->queryCodeContext(), tempDirectory, activityId)));
  11211. ICompare *compareRight = helper.queryCompareRight();
  11212. if (helper.isRightAlreadySorted())
  11213. groupedSortedRight.setown(createGroupedInputReader(inputStream1, compareRight));
  11214. else
  11215. groupedSortedRight.setown(createSortedGroupedInputReader(inputStream1, compareRight, createSortAlgorithm(sortAlgorithm, compareRight, ctx->queryRowManager(), input1->queryOutputMeta(), ctx->queryCodeContext(), tempDirectory, activityId)));
  11216. if ((helper.getJoinFlags() & JFlimitedprefixjoin) && helper.getJoinLimit())
  11217. { //limited match join (s[1..n])
  11218. limitedhelper.setown(createRHLimitedCompareHelper());
  11219. limitedhelper->init( helper.getJoinLimit(), groupedSortedRight, collate, helper.queryPrefixCompare() );
  11220. }
  11221. }
  11222. virtual void reset()
  11223. {
  11224. if (atmostsTriggered)
  11225. noteStatistic(StNumAtmostTriggered, atmostsTriggered);
  11226. right.clear();
  11227. ReleaseClearRoxieRow(left);
  11228. ReleaseClearRoxieRow(pendingRight);
  11229. defaultRight.clear();
  11230. defaultLeft.clear();
  11231. sortedLeft.clear();
  11232. groupedSortedRight.clear();
  11233. CRoxieServerTwoInputActivity::reset();
  11234. }
  11235. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  11236. {
  11237. if (!idx && (helper.getJoinFlags() & JFparallel) != 0)
  11238. {
  11239. puller.setown(new CRoxieServerReadAheadInput(ctx, ctx->queryOptions().parallelJoinPreload));
  11240. puller->setInput(0, _sourceIdx, _in);
  11241. _in = puller;
  11242. _sourceIdx = 0;
  11243. }
  11244. CRoxieServerTwoInputActivity::setInput(idx, _sourceIdx, _in);
  11245. }
  11246. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  11247. {
  11248. if (idx==(unsigned)-1)
  11249. idx = 0;
  11250. return idx ? NULL : this;
  11251. }
  11252. void fillLeft()
  11253. {
  11254. matchedLeft = false;
  11255. left = sortedLeft->nextRow(); // NOTE: already degrouped by the IGroupedInput we attached
  11256. if (betweenjoin && left && pendingRight && (collate->docompare(left, pendingRight) >= 0))
  11257. fillRight();
  11258. if (limitedhelper && 0==rightIndex)
  11259. {
  11260. rightIndex = 0;
  11261. joinCounter = 0;
  11262. right.clear();
  11263. matchedRight.kill();
  11264. if (left)
  11265. {
  11266. limitedhelper->getGroup(right,left);
  11267. ForEachItemIn(idx, right)
  11268. matchedRight.append(false);
  11269. }
  11270. }
  11271. }
  11272. void fillRight()
  11273. {
  11274. if (limitedhelper)
  11275. return;
  11276. failingLimit.clear();
  11277. if(betweenjoin && left)
  11278. {
  11279. aindex_t start = 0;
  11280. while(right.isItem(start) && (collateupper->docompare(left, right.item(start)) > 0))
  11281. start++;
  11282. if(start>0)
  11283. right.clearPart(0, start);
  11284. }
  11285. else
  11286. right.clear();
  11287. rightIndex = 0;
  11288. joinCounter = 0;
  11289. unsigned groupCount = 0;
  11290. const void * next;
  11291. while(true)
  11292. {
  11293. if(pendingRight)
  11294. {
  11295. next = pendingRight;
  11296. pendingRight = NULL;
  11297. }
  11298. else
  11299. {
  11300. next = groupedSortedRight->nextRow();
  11301. }
  11302. if(!rightOuterJoin && next && (!left || (collateupper->docompare(left, next) > 0))) // if right is less than left, and not right outer, can skip group
  11303. {
  11304. while(next)
  11305. {
  11306. ReleaseClearRoxieRow(next);
  11307. next = groupedSortedRight->nextRow();
  11308. }
  11309. continue;
  11310. }
  11311. while(next)
  11312. {
  11313. if(groupCount==abortLimit)
  11314. {
  11315. if(limitFail)
  11316. failLimit();
  11317. if (ctx->queryDebugContext())
  11318. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  11319. if(limitOnFail)
  11320. {
  11321. assertex(!failingLimit);
  11322. try
  11323. {
  11324. failLimit();
  11325. }
  11326. catch(IException * except)
  11327. {
  11328. failingLimit.setown(except);
  11329. }
  11330. assertex(failingLimit != NULL);
  11331. }
  11332. right.append(next);
  11333. do
  11334. {
  11335. next = groupedSortedRight->nextRow();
  11336. ReleaseRoxieRow(next);
  11337. } while(next);
  11338. break;
  11339. }
  11340. else if(groupCount==atmostLimit)
  11341. {
  11342. atmostsTriggered++;
  11343. right.clear();
  11344. groupCount = 0;
  11345. while(next)
  11346. {
  11347. ReleaseRoxieRow(next);
  11348. next = groupedSortedRight->nextRow();
  11349. }
  11350. }
  11351. else
  11352. {
  11353. right.append(next);
  11354. groupCount++;
  11355. }
  11356. next = groupedSortedRight->nextRow();
  11357. }
  11358. // 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
  11359. if(betweenjoin && left)
  11360. {
  11361. pendingRight = groupedSortedRight->nextRow();
  11362. if(!pendingRight || (collate->docompare(left, pendingRight) < 0))
  11363. break;
  11364. }
  11365. else
  11366. break;
  11367. }
  11368. matchedRight.kill();
  11369. ForEachItemIn(idx, right)
  11370. matchedRight.append(false);
  11371. }
  11372. const void * joinRecords(const void * curLeft, const void * curRight, unsigned counter, unsigned flags)
  11373. {
  11374. if (cloneLeft)
  11375. {
  11376. LinkRoxieRow(curLeft);
  11377. return curLeft;
  11378. }
  11379. try
  11380. {
  11381. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  11382. size32_t thisSize = helper.transform(rowBuilder, curLeft, curRight, counter, flags);
  11383. if (thisSize)
  11384. return rowBuilder.finalizeRowClear(thisSize);
  11385. else
  11386. return NULL;
  11387. }
  11388. catch (IException *E)
  11389. {
  11390. throw makeWrappedException(E);
  11391. }
  11392. }
  11393. const void * denormalizeRecords(const void * curLeft, ConstPointerArray & rows, unsigned flags)
  11394. {
  11395. try
  11396. {
  11397. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  11398. unsigned numRows = rows.ordinality();
  11399. const void * right = numRows ? rows.item(0) : defaultRight.get();
  11400. if (numRows>0)
  11401. flags |= JTFmatchedright;
  11402. size32_t thisSize = helper.transform(rowBuilder, curLeft, right, numRows, (const void * *)rows.getArray(), flags);
  11403. if (thisSize)
  11404. return rowBuilder.finalizeRowClear(thisSize);
  11405. else
  11406. return NULL;
  11407. }
  11408. catch (IException *E)
  11409. {
  11410. throw makeWrappedException(E);
  11411. }
  11412. }
  11413. const void * joinException(const void * curLeft, IException * except)
  11414. {
  11415. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  11416. size32_t thisSize = helper.onFailTransform(rowBuilder, curLeft, defaultRight, except, JTFmatchedleft);
  11417. return rowBuilder.finalizeRowClear(thisSize);
  11418. }
  11419. void failLimit()
  11420. {
  11421. helper.onMatchAbortLimitExceeded();
  11422. CommonXmlWriter xmlwrite(0);
  11423. if (input->queryOutputMeta() && input->queryOutputMeta()->hasXML())
  11424. {
  11425. input->queryOutputMeta()->toXML((byte *) left, xmlwrite);
  11426. }
  11427. throw MakeStringException(ROXIE_TOO_MANY_RESULTS, "More than %d match candidates in join %d for row %s", abortLimit, queryId(), xmlwrite.str());
  11428. }
  11429. virtual const void * nextRow()
  11430. {
  11431. ActivityTimer t(activityStats, timeActivities);
  11432. for (;;)
  11433. {
  11434. switch (state)
  11435. {
  11436. case JSfill:
  11437. fillLeft();
  11438. state = JSfillright;
  11439. break;
  11440. case JSfillright:
  11441. fillRight();
  11442. state = JScollate;
  11443. break;
  11444. case JSfillleft:
  11445. fillLeft();
  11446. state = JScollate;
  11447. break;
  11448. case JScollate:
  11449. if (right.ordinality() == 0)
  11450. {
  11451. if (left == NULL)
  11452. return NULL;
  11453. state = JSleftonly;
  11454. }
  11455. else
  11456. {
  11457. if (!left)
  11458. state = JSrightonly;
  11459. else
  11460. {
  11461. int diff;
  11462. if(betweenjoin)
  11463. diff = ((collate->docompare(left, right.item(0)) < 0) ? -1 : ((collateupper->docompare(left, right.item(right.ordinality()-1)) > 0) ? +1 : 0));
  11464. else
  11465. diff = collate->docompare(left, right.item(0));
  11466. bool limitExceeded = right.ordinality()>abortLimit;
  11467. if (diff == 0)
  11468. {
  11469. if (limitExceeded)
  11470. {
  11471. const void * ret = NULL;
  11472. if(failingLimit)
  11473. ret = joinException(left, failingLimit);
  11474. ReleaseRoxieRow(left);
  11475. left = NULL;
  11476. state = JSfillleft;
  11477. ForEachItemIn(idx, right)
  11478. matchedRight.replace(true, idx);
  11479. if(ret)
  11480. {
  11481. processed++;
  11482. return ret;
  11483. }
  11484. }
  11485. else
  11486. {
  11487. state = JScompare;
  11488. joinLimit = keepLimit;
  11489. }
  11490. }
  11491. else if (diff < 0)
  11492. state = JSleftonly;
  11493. else if (limitExceeded)
  11494. {
  11495. // MORE - Roxie code seems to think there should be a destroyRowset(right) here....
  11496. state = JSfillright;
  11497. }
  11498. else
  11499. state = JSrightonly;
  11500. }
  11501. }
  11502. break;
  11503. case JSrightonly:
  11504. if (rightOuterJoin)
  11505. {
  11506. switch (activityKind)
  11507. {
  11508. case TAKjoin:
  11509. {
  11510. while (right.isItem(rightIndex))
  11511. {
  11512. if (!matchedRight.item(rightIndex))
  11513. {
  11514. const void * rhs = right.item(rightIndex++);
  11515. const void *ret = joinRecords(defaultLeft, rhs, 0, JTFmatchedright);
  11516. if (ret)
  11517. {
  11518. processed++;
  11519. return ret;
  11520. }
  11521. }
  11522. else
  11523. rightIndex++;
  11524. }
  11525. break;
  11526. }
  11527. //Probably excessive to implement the following, but possibly useful
  11528. case TAKdenormalize:
  11529. {
  11530. OwnedConstRoxieRow newLeft;
  11531. newLeft.set(defaultLeft);
  11532. unsigned rowSize = 0;
  11533. unsigned leftCount = 0;
  11534. while (right.isItem(rightIndex))
  11535. {
  11536. if (!matchedRight.item(rightIndex))
  11537. {
  11538. const void * rhs = right.item(rightIndex);
  11539. try
  11540. {
  11541. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  11542. unsigned thisSize = helper.transform(rowBuilder, newLeft, rhs, ++leftCount, JTFmatchedright);
  11543. if (thisSize)
  11544. {
  11545. rowSize = thisSize;
  11546. newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
  11547. }
  11548. }
  11549. catch (IException *E)
  11550. {
  11551. throw makeWrappedException(E);
  11552. }
  11553. }
  11554. rightIndex++;
  11555. }
  11556. state = JSfillright;
  11557. if (rowSize)
  11558. {
  11559. processed++;
  11560. return newLeft.getClear();
  11561. }
  11562. break;
  11563. }
  11564. case TAKdenormalizegroup:
  11565. {
  11566. filteredRight.kill();
  11567. while (right.isItem(rightIndex))
  11568. {
  11569. if (!matchedRight.item(rightIndex))
  11570. filteredRight.append(right.item(rightIndex));
  11571. rightIndex++;
  11572. }
  11573. state = JSfillright;
  11574. if (filteredRight.ordinality())
  11575. {
  11576. const void * ret = denormalizeRecords(defaultLeft, filteredRight, 0);
  11577. filteredRight.kill();
  11578. if (ret)
  11579. {
  11580. processed++;
  11581. return ret;
  11582. }
  11583. }
  11584. break;
  11585. }
  11586. }
  11587. }
  11588. state = JSfillright;
  11589. break;
  11590. case JSleftonly:
  11591. {
  11592. const void * ret = NULL;
  11593. if (!matchedLeft && leftOuterJoin)
  11594. {
  11595. switch (activityKind)
  11596. {
  11597. case TAKjoin:
  11598. ret = joinRecords(left, defaultRight, 0, JTFmatchedleft);
  11599. break;
  11600. case TAKdenormalize:
  11601. ret = left;
  11602. left = NULL;
  11603. break;
  11604. case TAKdenormalizegroup:
  11605. filteredRight.kill();
  11606. ret = denormalizeRecords(left, filteredRight, JTFmatchedleft);
  11607. break;
  11608. }
  11609. }
  11610. ReleaseRoxieRow(left);
  11611. left = NULL;
  11612. state = JSfillleft;
  11613. if (ret)
  11614. {
  11615. processed++;
  11616. return ret;
  11617. }
  11618. break;
  11619. }
  11620. case JScompare:
  11621. if (joinLimit != 0)
  11622. {
  11623. switch (activityKind)
  11624. {
  11625. case TAKjoin:
  11626. {
  11627. while (right.isItem(rightIndex))
  11628. {
  11629. const void * rhs = right.item(rightIndex++);
  11630. if (helper.match(left, rhs))
  11631. {
  11632. matchedRight.replace(true, rightIndex-1);
  11633. matchedLeft = true;
  11634. if (!exclude)
  11635. {
  11636. const void *ret = joinRecords(left, rhs, ++joinCounter, JTFmatchedleft|JTFmatchedright);
  11637. if (ret)
  11638. {
  11639. processed++;
  11640. joinLimit--;
  11641. return ret;
  11642. }
  11643. }
  11644. }
  11645. }
  11646. break;
  11647. }
  11648. case TAKdenormalize:
  11649. {
  11650. OwnedConstRoxieRow newLeft;
  11651. newLeft.set(left);
  11652. unsigned rowSize = 0;
  11653. unsigned leftCount = 0;
  11654. while (right.isItem(rightIndex) && joinLimit)
  11655. {
  11656. try
  11657. {
  11658. const void * rhs = right.item(rightIndex++);
  11659. if (helper.match(left, rhs))
  11660. {
  11661. matchedRight.replace(true, rightIndex-1);
  11662. matchedLeft = true;
  11663. if (!exclude)
  11664. {
  11665. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  11666. unsigned thisSize = helper.transform(rowBuilder, newLeft, rhs, ++leftCount, JTFmatchedleft|JTFmatchedright);
  11667. if (thisSize)
  11668. {
  11669. rowSize = thisSize;
  11670. newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
  11671. joinLimit--;
  11672. }
  11673. }
  11674. }
  11675. }
  11676. catch (IException *E)
  11677. {
  11678. throw makeWrappedException(E);
  11679. }
  11680. }
  11681. state = JSleftonly;
  11682. rightIndex = 0;
  11683. if (rowSize)
  11684. {
  11685. processed++;
  11686. return newLeft.getClear();
  11687. }
  11688. break;
  11689. }
  11690. case TAKdenormalizegroup:
  11691. {
  11692. filteredRight.kill();
  11693. while (right.isItem(rightIndex))
  11694. {
  11695. const void * rhs = right.item(rightIndex++);
  11696. if (helper.match(left, rhs))
  11697. {
  11698. matchedRight.replace(true, rightIndex-1);
  11699. filteredRight.append(rhs);
  11700. matchedLeft = true;
  11701. if (filteredRight.ordinality()==joinLimit)
  11702. break;
  11703. }
  11704. }
  11705. state = JSleftonly;
  11706. rightIndex = 0;
  11707. if (!exclude && filteredRight.ordinality())
  11708. {
  11709. const void * ret = denormalizeRecords(left, filteredRight, JTFmatchedleft);
  11710. filteredRight.kill();
  11711. if (ret)
  11712. {
  11713. processed++;
  11714. return ret;
  11715. }
  11716. }
  11717. break;
  11718. }
  11719. }
  11720. }
  11721. state = JSleftonly;
  11722. rightIndex = 0;
  11723. joinCounter = 0;
  11724. break;
  11725. }
  11726. }
  11727. }
  11728. };
  11729. class CRoxieServerJoinActivityFactory : public CRoxieServerActivityFactory
  11730. {
  11731. unsigned input2;
  11732. unsigned input2idx;
  11733. bool forceSpill;
  11734. public:
  11735. CRoxieServerJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  11736. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  11737. {
  11738. forceSpill = _graphNode.getPropBool("hint[@name='spill']/@value", _queryFactory.queryOptions().allSortsMaySpill);
  11739. input2 = 0;
  11740. input2idx = 0;
  11741. }
  11742. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  11743. {
  11744. return new CRoxieServerJoinActivity(_ctx, this, _probeManager, forceSpill);
  11745. }
  11746. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  11747. {
  11748. if (idx==1)
  11749. {
  11750. input2 = source;
  11751. input2idx = sourceidx;
  11752. }
  11753. else
  11754. CRoxieServerActivityFactory::setInput(idx, source, sourceidx);
  11755. }
  11756. virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
  11757. {
  11758. switch (idx)
  11759. {
  11760. case 1:
  11761. sourceidx = input2idx;
  11762. return input2;
  11763. case 0:
  11764. return CRoxieServerActivityFactory::getInput(idx, sourceidx);
  11765. default:
  11766. return (unsigned) -1;
  11767. }
  11768. }
  11769. virtual unsigned numInputs() const { return 2; }
  11770. virtual const StatisticsMapping &queryStatsMapping() const
  11771. {
  11772. return joinStatistics;
  11773. }
  11774. };
  11775. IRoxieServerActivityFactory *createRoxieServerJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  11776. {
  11777. return new CRoxieServerJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  11778. }
  11779. //=================================================================================
  11780. #define CONCAT_READAHEAD 1000
  11781. class CRoxieThreadedConcatReader : implements IRecordPullerCallback, public CInterface
  11782. {
  11783. public:
  11784. IMPLEMENT_IINTERFACE;
  11785. CRoxieThreadedConcatReader(InterruptableSemaphore &_ready, bool _grouped)
  11786. : puller(false), ready(_ready), atEog(true), eof(false), grouped(_grouped)
  11787. {
  11788. }
  11789. void start(unsigned parentExtractSize, const byte *parentExtract, bool paused, IRoxieAgentContext *ctx)
  11790. {
  11791. space.reinit(CONCAT_READAHEAD);
  11792. puller.start(parentExtractSize, parentExtract, paused, ctx->queryOptions().concatPreload, false, ctx);
  11793. }
  11794. void stop()
  11795. {
  11796. space.interrupt();
  11797. puller.stop();
  11798. }
  11799. IFinalRoxieInput *queryInput() const
  11800. {
  11801. return puller.queryInput();
  11802. }
  11803. void reset()
  11804. {
  11805. puller.reset();
  11806. ForEachItemIn(idx, buffer)
  11807. ReleaseRoxieRow(buffer.item(idx));
  11808. buffer.clear();
  11809. eof = false;
  11810. atEog = true;
  11811. }
  11812. void setInput(unsigned _sourceIdx, IFinalRoxieInput *_in)
  11813. {
  11814. puller.setInput(this, _sourceIdx, _in);
  11815. }
  11816. void connectInputStreams(IRoxieAgentContext *ctx, bool consumerOrdered)
  11817. {
  11818. puller.connectInputStreams(ctx, consumerOrdered);
  11819. }
  11820. virtual void processRow(const void *row)
  11821. {
  11822. buffer.enqueue(row);
  11823. ready.signal();
  11824. space.wait();
  11825. }
  11826. virtual void processGroup(const ConstPointerArray &rows)
  11827. {
  11828. // We use record-by-record input mode of the puller thread even in grouped mode.
  11829. throwUnexpected();
  11830. }
  11831. virtual void processEOG()
  11832. {
  11833. if (grouped)
  11834. processRow(NULL);
  11835. }
  11836. virtual void processDone()
  11837. {
  11838. processRow(NULL);
  11839. }
  11840. virtual bool fireException(IException *e)
  11841. {
  11842. // called from puller thread on failure
  11843. ready.interrupt(LINK(e));
  11844. space.interrupt(e);
  11845. return true;
  11846. }
  11847. bool peek(const void * &row, bool &anyActive)
  11848. {
  11849. if (!eof)
  11850. {
  11851. if (buffer.ordinality())
  11852. {
  11853. space.signal();
  11854. row = buffer.dequeue();
  11855. if (row==NULL)
  11856. {
  11857. if (atEog)
  11858. {
  11859. eof = true;
  11860. return false;
  11861. }
  11862. else
  11863. atEog = true;
  11864. }
  11865. else if (grouped)
  11866. atEog = false;
  11867. return true;
  11868. }
  11869. anyActive = true;
  11870. }
  11871. return false;
  11872. }
  11873. protected:
  11874. RecordPullerThread puller;
  11875. InterruptableSemaphore space;
  11876. InterruptableSemaphore &ready;
  11877. SafeQueueOf<const void, true> buffer;
  11878. bool atEog;
  11879. bool eof;
  11880. bool grouped;
  11881. };
  11882. typedef CopyReferenceArrayOf<CRoxieThreadedConcatReader> ReaderArray;
  11883. class CRoxieServerThreadedConcatActivity : public CRoxieServerActivity
  11884. {
  11885. InterruptableSemaphore ready;
  11886. ReaderArray pullers;
  11887. unsigned numInputs;
  11888. unsigned nextPuller; // for round robin
  11889. unsigned readyPending;
  11890. bool eof;
  11891. bool inGroup;
  11892. bool grouped;
  11893. public:
  11894. CRoxieServerThreadedConcatActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _grouped, unsigned _numInputs)
  11895. : CRoxieServerActivity(_ctx, _factory, _probeManager), grouped(_grouped)
  11896. {
  11897. numInputs = _numInputs;
  11898. eof = (numInputs==0);
  11899. inGroup = false;
  11900. nextPuller = 0;
  11901. readyPending = 0;
  11902. for (unsigned i = 0; i < numInputs; i++)
  11903. pullers.append(*new CRoxieThreadedConcatReader(ready, _grouped));
  11904. }
  11905. ~CRoxieServerThreadedConcatActivity()
  11906. {
  11907. ForEachItemIn(idx, pullers)
  11908. delete &pullers.item(idx);
  11909. }
  11910. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  11911. {
  11912. eof = (numInputs==0);
  11913. inGroup = false;
  11914. nextPuller = 0;
  11915. readyPending = 0;
  11916. ready.reinit();
  11917. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  11918. ForEachItemIn(idx, pullers)
  11919. {
  11920. pullers.item(idx).start(parentExtractSize, parentExtract, paused, ctx);
  11921. // NOTE - it is ok to start the thread running while parts of the subgraph are still being started, since everything
  11922. // in the part of the subgraph that the thread uses has been started.
  11923. // Note that splitters are supposed to cope with being used when only some outputs have been started.
  11924. }
  11925. }
  11926. virtual void stop()
  11927. {
  11928. ready.interrupt();
  11929. ForEachItemIn(idx, pullers)
  11930. pullers.item(idx).stop();
  11931. CRoxieServerActivity::stop();
  11932. }
  11933. virtual unsigned __int64 queryLocalCycles() const
  11934. {
  11935. return 0;
  11936. }
  11937. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  11938. {
  11939. if (pullers.isItem(idx))
  11940. return pullers.item(idx).queryInput();
  11941. else
  11942. return NULL;
  11943. }
  11944. virtual void reset()
  11945. {
  11946. CRoxieServerActivity::reset();
  11947. ForEachItemIn(idx, pullers)
  11948. pullers.item(idx).reset();
  11949. eof = false;
  11950. inGroup = false;
  11951. nextPuller = 0;
  11952. readyPending = 0;
  11953. }
  11954. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  11955. {
  11956. if (pullers.isItem(idx))
  11957. pullers.item(idx).setInput(_sourceIdx, _in);
  11958. else
  11959. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", sanitizeSourceFile(__FILE__), __LINE__);
  11960. }
  11961. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks)
  11962. {
  11963. ForEachItemIn(i, pullers)
  11964. pullers.item(i).connectInputStreams(ctx, consumerOrdered);
  11965. return CRoxieServerActivity::getOutputStreams(ctx, idx, streams, nullptr, consumerOrdered, nullptr); // No input actually connected
  11966. }
  11967. virtual const void * nextRow()
  11968. {
  11969. ActivityTimer t(activityStats, timeActivities);
  11970. if (eof)
  11971. return NULL;
  11972. for (;;)
  11973. {
  11974. if (readyPending && !inGroup)
  11975. {
  11976. if (readyPending > 1)
  11977. ready.signal(readyPending-1);
  11978. readyPending = 0;
  11979. }
  11980. else
  11981. ready.wait();
  11982. bool anyActive = false;
  11983. ForEachItemIn(unused_index, pullers)
  11984. {
  11985. // NOTE - we round robin not just because it's more efficient, but because it ensures the preservation of grouping information
  11986. const void *ret;
  11987. bool fetched = pullers.item(nextPuller).peek(ret, anyActive);
  11988. if (fetched)
  11989. {
  11990. inGroup = (ret != NULL);
  11991. return ret;
  11992. }
  11993. if (inGroup && grouped)
  11994. {
  11995. // Some other puller has data, but we can't consume it until the group we are reading is complete.
  11996. readyPending++;
  11997. anyActive = true;
  11998. break;
  11999. }
  12000. nextPuller++;
  12001. if (nextPuller==pullers.ordinality())
  12002. nextPuller = 0;
  12003. }
  12004. if (!anyActive)
  12005. break;
  12006. // A ready signal without anything being ready means someone reached end-of-file.
  12007. }
  12008. eof = true;
  12009. return NULL;
  12010. }
  12011. };
  12012. class CRoxieServerOrderedConcatActivity : public CRoxieServerMultiInputActivity
  12013. {
  12014. IEngineRowStream *curStream;
  12015. bool eogSeen;
  12016. bool anyThisGroup;
  12017. bool grouped;
  12018. unsigned streamIdx;
  12019. public:
  12020. CRoxieServerOrderedConcatActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _grouped, unsigned _numInputs)
  12021. : CRoxieServerMultiInputActivity(_ctx, _factory, _probeManager, _numInputs)
  12022. {
  12023. eogSeen = false;
  12024. anyThisGroup = false;
  12025. grouped = _grouped;
  12026. streamIdx = 0;
  12027. curStream = NULL;
  12028. }
  12029. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12030. {
  12031. CRoxieServerMultiInputActivity::start(parentExtractSize, parentExtract, paused);
  12032. streamIdx = 0;
  12033. curStream = streamArray[streamIdx];
  12034. eogSeen = false;
  12035. anyThisGroup = false;
  12036. }
  12037. virtual const void * nextRow()
  12038. {
  12039. ActivityTimer t(activityStats, timeActivities);
  12040. if (!curStream)
  12041. return NULL; // eof
  12042. const void * next = curStream->nextRow();
  12043. if (next)
  12044. {
  12045. anyThisGroup = true;
  12046. eogSeen = false;
  12047. processed++;
  12048. return next;
  12049. }
  12050. else if (!eogSeen)
  12051. {
  12052. eogSeen = true;
  12053. if (grouped)
  12054. {
  12055. if (anyThisGroup)
  12056. {
  12057. anyThisGroup = false;
  12058. return NULL;
  12059. }
  12060. else
  12061. return nextRow();
  12062. }
  12063. else
  12064. return nextRow();
  12065. }
  12066. else if (streamIdx < numStreams-1)
  12067. {
  12068. streamIdx++;
  12069. curStream = streamArray[streamIdx];
  12070. eogSeen = false;
  12071. return nextRow();
  12072. }
  12073. else
  12074. {
  12075. curStream = NULL;
  12076. return NULL;
  12077. }
  12078. }
  12079. };
  12080. class CRoxieServerConcatActivityFactory : public CRoxieServerMultiInputFactory
  12081. {
  12082. bool ordered;
  12083. bool grouped;
  12084. public:
  12085. CRoxieServerConcatActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12086. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  12087. {
  12088. Owned <IHThorFunnelArg> helper = (IHThorFunnelArg *) helperFactory();
  12089. ordered = helper->isOrdered();
  12090. grouped = helper->queryOutputMeta()->isGrouped();
  12091. }
  12092. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  12093. {
  12094. if (ordered || (_probeManager && _probeManager->queryDebugManager()))
  12095. return new CRoxieServerOrderedConcatActivity(_ctx, this, _probeManager, grouped, numInputs());
  12096. else
  12097. return new CRoxieServerThreadedConcatActivity(_ctx, this, _probeManager, grouped, numInputs());
  12098. }
  12099. };
  12100. IRoxieServerActivityFactory *createRoxieServerConcatActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12101. {
  12102. return new CRoxieServerConcatActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  12103. }
  12104. //=================================================================================
  12105. class CRoxieServerNonEmptyActivity : public CRoxieServerMultiInputBaseActivity
  12106. {
  12107. IEngineRowStream* selectedStream;
  12108. unsigned savedParentExtractSize;
  12109. const byte * savedParentExtract;
  12110. bool foundInput;
  12111. bool unusedStopped;
  12112. public:
  12113. CRoxieServerNonEmptyActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  12114. : CRoxieServerMultiInputBaseActivity(_ctx, _factory, _probeManager, _numInputs)
  12115. {
  12116. foundInput = false;
  12117. unusedStopped = false;
  12118. selectedStream = NULL;
  12119. savedParentExtractSize = 0;;
  12120. savedParentExtract = NULL;
  12121. }
  12122. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12123. {
  12124. //Don't start the inputs yet so we can short-circuit...
  12125. CRoxieServerMultiInputBaseActivity::start(parentExtractSize, parentExtract, paused);
  12126. savedParentExtractSize = parentExtractSize;
  12127. savedParentExtract = parentExtract;
  12128. }
  12129. virtual void stop()
  12130. {
  12131. if (unusedStopped)
  12132. {
  12133. if (selectedStream)
  12134. selectedStream->stop();
  12135. }
  12136. else
  12137. {
  12138. //May possibly call stop too many times if input->start() throws an exception (e.g. due to a dependency)
  12139. for (unsigned i = 0; i < numStreams; i++)
  12140. streamArray[i]->stop();
  12141. }
  12142. CRoxieServerMultiInputBaseActivity::stop();
  12143. }
  12144. virtual void reset()
  12145. {
  12146. CRoxieServerMultiInputBaseActivity::reset();
  12147. foundInput = false;
  12148. unusedStopped = false;
  12149. selectedStream = NULL;
  12150. }
  12151. virtual unsigned __int64 queryLocalCycles() const
  12152. {
  12153. return 0; // Can't easily calculate anything reliable but local processing is negligible
  12154. }
  12155. virtual const void * nextRow()
  12156. {
  12157. ActivityTimer t(activityStats, timeActivities);
  12158. if (!foundInput)
  12159. {
  12160. foundInput = true;
  12161. assertex(numInputs == numStreams); // If this ceases to be true we will need to rethink some of this
  12162. //If we get an exception in this loop then stop() will stop any started inputs
  12163. for (unsigned i=0; i < numStreams; i++)
  12164. {
  12165. selectedStream = streamArray[i];
  12166. inputArray[i]->start(savedParentExtractSize, savedParentExtract, false); // Assumes 1:1 mapping streams to inputs
  12167. startJunction(junctionArray[i]);
  12168. const void * next = selectedStream->nextRow();
  12169. if (next)
  12170. {
  12171. //Found a row so stop remaining
  12172. for (unsigned j=i+1; j < numInputs; j++)
  12173. streamArray[j]->stop();
  12174. unusedStopped = true;
  12175. processed++;
  12176. return next;
  12177. }
  12178. selectedStream->stop();
  12179. }
  12180. unusedStopped = true;
  12181. selectedStream = NULL;
  12182. return NULL;
  12183. }
  12184. if (!selectedStream)
  12185. return NULL;
  12186. const void * next = selectedStream->nextRow();
  12187. if (next)
  12188. processed++;
  12189. return next;
  12190. }
  12191. };
  12192. class CRoxieServerNonEmptyActivityFactory : public CRoxieServerMultiInputFactory
  12193. {
  12194. public:
  12195. CRoxieServerNonEmptyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12196. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  12197. {
  12198. }
  12199. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  12200. {
  12201. return new CRoxieServerNonEmptyActivity(_ctx, this, _probeManager, numInputs());
  12202. }
  12203. };
  12204. IRoxieServerActivityFactory *createRoxieServerNonEmptyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12205. {
  12206. return new CRoxieServerNonEmptyActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  12207. }
  12208. //=================================================================================
  12209. class SingleNodeActivityContext : public IThorActivityContext
  12210. {
  12211. public:
  12212. SingleNodeActivityContext(unsigned _numStrands, unsigned _curStrand) : strands(_numStrands), curStrand(_curStrand) { assertex(curStrand < strands); }
  12213. virtual bool isLocal() const override { return false; }
  12214. virtual unsigned numSlaves() const override { return 1; }
  12215. virtual unsigned numStrands() const override { return strands; }
  12216. virtual unsigned querySlave() const override { return 0; }
  12217. virtual unsigned queryStrand() const override { return curStrand; }
  12218. protected:
  12219. unsigned strands;
  12220. unsigned curStrand;
  12221. };
  12222. class CRoxieServerExternalActivity : public CRoxieServerMultiInputActivity
  12223. {
  12224. Owned<IRowStream> rows;
  12225. SingleNodeActivityContext activityContext;
  12226. public:
  12227. CRoxieServerExternalActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  12228. : CRoxieServerMultiInputActivity(_ctx, _factory, _probeManager, _numInputs), activityContext(1, 0)
  12229. {
  12230. }
  12231. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks) override
  12232. {
  12233. Owned<IStrandJunction> junction = CRoxieServerMultiInputActivity::getOutputStreams(ctx, idx, streams, consumerOptions, consumerOrdered, orderedCallbacks);
  12234. associateInputsWithHelper();
  12235. return junction.getClear();
  12236. }
  12237. virtual void connectInputStreams(bool consumerOrdered)
  12238. {
  12239. CRoxieServerMultiInputActivity::connectInputStreams(consumerOrdered);
  12240. associateInputsWithHelper();
  12241. }
  12242. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12243. {
  12244. CRoxieServerMultiInputActivity::start(parentExtractSize, parentExtract, paused);
  12245. if (factory->getKind() != TAKexternalsink)
  12246. {
  12247. IHThorExternalArg & helper = static_cast<IHThorExternalArg &>(basehelper);
  12248. rows.setown(helper.createOutput(&activityContext));
  12249. }
  12250. }
  12251. virtual void stop()
  12252. {
  12253. if (rows)
  12254. {
  12255. rows->stop();
  12256. rows.clear();
  12257. }
  12258. CRoxieServerMultiInputActivity::stop();
  12259. }
  12260. virtual void reset()
  12261. {
  12262. rows.clear();
  12263. CRoxieServerMultiInputActivity::reset();
  12264. }
  12265. virtual const void * nextRow()
  12266. {
  12267. ActivityTimer t(activityStats, timeActivities);
  12268. assertex(rows);
  12269. const void * next = rows->nextRow();
  12270. if (next)
  12271. processed++;
  12272. return next;
  12273. }
  12274. virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
  12275. {
  12276. try
  12277. {
  12278. start(parentExtractSize, parentExtract, false);
  12279. assertex(!rows);
  12280. IHThorExternalArg & helper = static_cast<IHThorExternalArg &>(basehelper);
  12281. helper.execute(&activityContext);
  12282. stop();
  12283. }
  12284. catch (IException * E)
  12285. {
  12286. ctx->notifyAbort(E);
  12287. abort();
  12288. throw;
  12289. }
  12290. }
  12291. protected:
  12292. void associateInputsWithHelper()
  12293. {
  12294. IHThorExternalArg & helper = static_cast<IHThorExternalArg &>(basehelper);
  12295. for (unsigned i = 0; i < numInputs; i++)
  12296. helper.setInput(i, streamArray[i]);
  12297. }
  12298. };
  12299. class CRoxieServerExternalActivityFactory : public CRoxieServerMultiInputFactory
  12300. {
  12301. bool isRoot;
  12302. public:
  12303. CRoxieServerExternalActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  12304. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), isRoot(_isRoot)
  12305. {
  12306. }
  12307. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  12308. {
  12309. return new CRoxieServerExternalActivity(_ctx, this, _probeManager, numInputs());
  12310. }
  12311. virtual bool isSink() const
  12312. {
  12313. return (kind == TAKexternalsink) && isRoot;
  12314. }
  12315. };
  12316. IRoxieServerActivityFactory *createRoxieServerExternalActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  12317. {
  12318. return new CRoxieServerExternalActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _isRoot);
  12319. }
  12320. //=================================================================================
  12321. class CRoxieServerMergeActivity : public CRoxieServerMultiInputActivity
  12322. {
  12323. IHThorMergeArg &helper;
  12324. unsigned *mergeheap;
  12325. unsigned activeInputs;
  12326. const void **pending;
  12327. bool first;
  12328. ICompare *compare;
  12329. bool dedup;
  12330. void permute()
  12331. {
  12332. assertex(activeInputs == 0);
  12333. for(unsigned i = 0; i < numStreams; i++)
  12334. if(pullInput(i))
  12335. mergeheap[activeInputs++] = i;
  12336. // the tree structure: element p has children p*2+1 and p*2+2, or element c has parent (unsigned)(c-1)/2
  12337. // the heap property: no element should be smaller than its parent
  12338. // the dedup variant: if(dedup), the top of the heap should also not be equal to either child
  12339. // 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
  12340. if (activeInputs >= 2)
  12341. for(unsigned p = (activeInputs-2)/2; p > 0; --p)
  12342. siftDown(p);
  12343. if(dedup)
  12344. siftDownDedupTop();
  12345. else
  12346. siftDown(0);
  12347. }
  12348. void readNext()
  12349. {
  12350. if(!pullInput(mergeheap[0]))
  12351. if(!promote(0))
  12352. return;
  12353. // we have changed the element at the top of the heap, so need to sift it down to maintain the heap property
  12354. if(dedup)
  12355. siftDownDedupTop();
  12356. else
  12357. siftDown(0);
  12358. }
  12359. bool pullInput(unsigned i)
  12360. {
  12361. const void *next = streamArray[i]->ungroupedNextRow();
  12362. pending[i] = next;
  12363. return (next != NULL);
  12364. }
  12365. bool promote(unsigned p)
  12366. {
  12367. activeInputs--;
  12368. if(activeInputs == p)
  12369. return false;
  12370. mergeheap[p] = mergeheap[activeInputs];
  12371. return true;
  12372. }
  12373. bool siftDown(unsigned p)
  12374. {
  12375. // assuming that all descendants of p form a heap, sift p down to its correct position, and so include it in the heap
  12376. bool nochange = true;
  12377. while(1)
  12378. {
  12379. unsigned c = p*2 + 1;
  12380. if(c >= activeInputs)
  12381. return nochange;
  12382. if(c+1 < activeInputs)
  12383. {
  12384. int childcmp = BuffCompare(c+1, c);
  12385. if((childcmp < 0) || ((childcmp == 0) && (mergeheap[c+1] < mergeheap[c])))
  12386. ++c;
  12387. }
  12388. int cmp = BuffCompare(c, p);
  12389. if((cmp > 0) || ((cmp == 0) && (mergeheap[c] > mergeheap[p])))
  12390. return nochange;
  12391. nochange = false;
  12392. unsigned r = mergeheap[c];
  12393. mergeheap[c] = mergeheap[p];
  12394. mergeheap[p] = r;
  12395. p = c;
  12396. }
  12397. }
  12398. void siftDownDedupTop()
  12399. {
  12400. // same as siftDown(0), except that it also ensures that the top of the heap is not equal to either of its children
  12401. if(activeInputs < 2)
  12402. return;
  12403. unsigned c = 1;
  12404. int childcmp = 1;
  12405. if(activeInputs >= 3)
  12406. {
  12407. childcmp = BuffCompare(2, 1);
  12408. if(childcmp < 0)
  12409. c = 2;
  12410. }
  12411. int cmp = BuffCompare(c, 0);
  12412. if(cmp > 0)
  12413. return;
  12414. // the following loop ensures the correct property holds on the smaller branch, and that childcmp==0 iff the top matches the other branch
  12415. while(cmp <= 0)
  12416. {
  12417. if(cmp == 0)
  12418. {
  12419. if(mergeheap[c] < mergeheap[0])
  12420. {
  12421. unsigned r = mergeheap[c];
  12422. mergeheap[c] = mergeheap[0];
  12423. mergeheap[0] = r;
  12424. }
  12425. ReleaseClearRoxieRow(pending[mergeheap[c]]);
  12426. if(!pullInput(mergeheap[c]))
  12427. if(!promote(c))
  12428. break;
  12429. siftDown(c);
  12430. }
  12431. else
  12432. {
  12433. unsigned r = mergeheap[c];
  12434. mergeheap[c] = mergeheap[0];
  12435. mergeheap[0] = r;
  12436. if(siftDown(c))
  12437. break;
  12438. }
  12439. cmp = BuffCompare(c, 0);
  12440. }
  12441. // the following loop ensures the uniqueness property holds on the other branch too
  12442. c = 3-c;
  12443. if(activeInputs <= c)
  12444. return;
  12445. while(childcmp == 0)
  12446. {
  12447. if(mergeheap[c] < mergeheap[0])
  12448. {
  12449. unsigned r = mergeheap[c];
  12450. mergeheap[c] = mergeheap[0];
  12451. mergeheap[0] = r;
  12452. }
  12453. ReleaseClearRoxieRow(pending[mergeheap[c]]);
  12454. if(!pullInput(mergeheap[c]))
  12455. if(!promote(c))
  12456. break;
  12457. siftDown(c);
  12458. childcmp = BuffCompare(c, 0);
  12459. }
  12460. }
  12461. inline int BuffCompare(unsigned a, unsigned b)
  12462. {
  12463. return compare->docompare(pending[mergeheap[a]], pending[mergeheap[b]]);
  12464. }
  12465. public:
  12466. CRoxieServerMergeActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  12467. : CRoxieServerMultiInputActivity(_ctx, _factory, _probeManager, _numInputs), helper((IHThorMergeArg &)basehelper)
  12468. {
  12469. activeInputs = 0;
  12470. first = true;
  12471. mergeheap = new unsigned[numInputs];
  12472. pending = new const void *[numStreams];
  12473. compare = helper.queryCompare();
  12474. dedup = helper.dedup();
  12475. for (unsigned i = 0; i < numStreams; i++)
  12476. pending[i] = NULL;
  12477. }
  12478. ~CRoxieServerMergeActivity()
  12479. {
  12480. delete [] mergeheap;
  12481. delete [] pending;
  12482. }
  12483. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12484. {
  12485. activeInputs = 0;
  12486. first = true;
  12487. CRoxieServerMultiInputActivity::start(parentExtractSize, parentExtract, paused);
  12488. }
  12489. virtual void reset()
  12490. {
  12491. for (unsigned i = 0; i < numStreams; i++)
  12492. ReleaseClearRoxieRow(pending[i]);
  12493. CRoxieServerMultiInputActivity::reset();
  12494. }
  12495. virtual const void * nextRow()
  12496. {
  12497. ActivityTimer t(activityStats, timeActivities);
  12498. if (first)
  12499. {
  12500. permute();
  12501. first = false;
  12502. }
  12503. if (activeInputs)
  12504. {
  12505. const void *next = pending[mergeheap[0]];
  12506. readNext();
  12507. if (next)
  12508. processed++;
  12509. return next;
  12510. }
  12511. else
  12512. return NULL;
  12513. }
  12514. };
  12515. class CRoxieServerMergeActivityFactory : public CRoxieServerMultiInputFactory
  12516. {
  12517. public:
  12518. CRoxieServerMergeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12519. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  12520. {
  12521. }
  12522. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  12523. {
  12524. return new CRoxieServerMergeActivity(_ctx, this, _probeManager, numInputs());
  12525. }
  12526. };
  12527. IRoxieServerActivityFactory *createRoxieServerMergeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12528. {
  12529. return new CRoxieServerMergeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  12530. }
  12531. //=================================================================================
  12532. class CRoxieServerRegroupActivity : public CRoxieServerMultiInputActivity
  12533. {
  12534. unsigned streamIndex;
  12535. bool eof;
  12536. unsigned __int64 numProcessedLastGroup;
  12537. public:
  12538. CRoxieServerRegroupActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  12539. : CRoxieServerMultiInputActivity(_ctx, _factory, _probeManager, _numInputs)
  12540. {
  12541. streamIndex = 0;
  12542. eof = false;
  12543. numProcessedLastGroup = 0;
  12544. }
  12545. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12546. {
  12547. streamIndex = 0;
  12548. eof = false;
  12549. numProcessedLastGroup = processed;
  12550. CRoxieServerMultiInputActivity::start(parentExtractSize, parentExtract, paused);
  12551. }
  12552. const void * nextFromInputs()
  12553. {
  12554. unsigned initialStream = streamIndex;
  12555. while (streamIndex < numStreams)
  12556. {
  12557. const void * next = streamArray[streamIndex]->nextRow();
  12558. if (next)
  12559. {
  12560. if ((streamIndex != initialStream) && (streamIndex != initialStream+1))
  12561. {
  12562. ReleaseRoxieRow(next);
  12563. throw MakeStringException(ROXIE_MISMATCH_GROUP_ERROR, "Mismatched groups supplied to Regroup (%d)", factory->queryId());
  12564. }
  12565. return next;
  12566. }
  12567. streamIndex++;
  12568. }
  12569. if ((initialStream != 0) && (initialStream+1 != numStreams))
  12570. throw MakeStringException(ROXIE_MISMATCH_GROUP_ERROR, "Mismatched groups supplied to Regroup (%d)", factory->queryId());
  12571. streamIndex = 0;
  12572. return NULL;
  12573. }
  12574. virtual const void * nextRow()
  12575. {
  12576. ActivityTimer t(activityStats, timeActivities);
  12577. if (eof)
  12578. return NULL;
  12579. const void * ret = nextFromInputs();
  12580. if (ret)
  12581. {
  12582. processed++;
  12583. return ret;
  12584. }
  12585. if (numProcessedLastGroup != processed)
  12586. {
  12587. numProcessedLastGroup = processed;
  12588. return NULL;
  12589. }
  12590. eof = true;
  12591. return NULL;
  12592. }
  12593. };
  12594. class CRoxieServerRegroupActivityFactory : public CRoxieServerMultiInputFactory
  12595. {
  12596. public:
  12597. CRoxieServerRegroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12598. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  12599. {
  12600. }
  12601. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  12602. {
  12603. return new CRoxieServerRegroupActivity(_ctx, this, _probeManager, numInputs());
  12604. }
  12605. };
  12606. IRoxieServerActivityFactory *createRoxieServerRegroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12607. {
  12608. return new CRoxieServerRegroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  12609. }
  12610. //=================================================================================
  12611. class CRoxieServerCombineActivity : public CRoxieServerMultiInputActivity
  12612. {
  12613. IHThorCombineArg &helper;
  12614. unsigned __int64 numProcessedLastGroup;
  12615. public:
  12616. CRoxieServerCombineActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  12617. : CRoxieServerMultiInputActivity(_ctx, _factory, _probeManager, _numInputs), helper((IHThorCombineArg &)basehelper)
  12618. {
  12619. numProcessedLastGroup = 0;
  12620. }
  12621. ~CRoxieServerCombineActivity()
  12622. {
  12623. }
  12624. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12625. {
  12626. numProcessedLastGroup = processed;
  12627. CRoxieServerMultiInputActivity::start(parentExtractSize, parentExtract, paused);
  12628. }
  12629. void nextInputs(ConstPointerArray & out)
  12630. {
  12631. for (unsigned i=0; i < numStreams; i++)
  12632. {
  12633. const void * next = streamArray[i]->nextRow();
  12634. if (next)
  12635. out.append(next);
  12636. }
  12637. }
  12638. virtual bool needsAllocator() const { return true; }
  12639. virtual const void * nextRow()
  12640. {
  12641. ActivityTimer t(activityStats, timeActivities);
  12642. for (;;)
  12643. {
  12644. ConstPointerArray group;
  12645. nextInputs(group);
  12646. if ((group.ordinality() == 0) && (numProcessedLastGroup == processed))
  12647. nextInputs(group);
  12648. if (group.ordinality() == 0)
  12649. {
  12650. numProcessedLastGroup = processed;
  12651. return NULL;
  12652. }
  12653. else if (group.ordinality() != numInputs)
  12654. {
  12655. ReleaseRoxieRows(group);
  12656. throw MakeStringException(ROXIE_MISMATCH_GROUP_ERROR, "Mismatched group input for Combine Activity(%d)", factory->queryId());
  12657. }
  12658. try
  12659. {
  12660. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  12661. size32_t outSize = helper.transform(rowBuilder, group.ordinality(), group.getArray());
  12662. ReleaseRoxieRows(group);
  12663. if (outSize)
  12664. {
  12665. processed++;
  12666. return rowBuilder.finalizeRowClear(outSize);
  12667. }
  12668. }
  12669. catch (IException *E)
  12670. {
  12671. ReleaseRoxieRows(group);
  12672. throw makeWrappedException(E);
  12673. }
  12674. }
  12675. }
  12676. };
  12677. class CRoxieServerCombineActivityFactory : public CRoxieServerMultiInputFactory
  12678. {
  12679. public:
  12680. CRoxieServerCombineActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12681. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  12682. {
  12683. }
  12684. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  12685. {
  12686. return new CRoxieServerCombineActivity(_ctx, this, _probeManager, numInputs());
  12687. }
  12688. };
  12689. IRoxieServerActivityFactory *createRoxieServerCombineActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12690. {
  12691. return new CRoxieServerCombineActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  12692. }
  12693. //=================================================================================
  12694. class CRoxieServerCombineGroupActivity : public CRoxieServerTwoInputActivity
  12695. {
  12696. IHThorCombineGroupArg &helper;
  12697. unsigned __int64 numProcessedLastGroup;
  12698. public:
  12699. CRoxieServerCombineGroupActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  12700. : CRoxieServerTwoInputActivity(_ctx, _factory, _probeManager), helper((IHThorCombineGroupArg &)basehelper)
  12701. {
  12702. numProcessedLastGroup = 0;
  12703. }
  12704. ~CRoxieServerCombineGroupActivity()
  12705. {
  12706. }
  12707. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12708. {
  12709. numProcessedLastGroup = processed;
  12710. CRoxieServerTwoInputActivity::start(parentExtractSize, parentExtract, paused);
  12711. }
  12712. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  12713. {
  12714. if (idx==(unsigned)-1)
  12715. idx = 0;
  12716. return idx ? NULL : this;
  12717. }
  12718. virtual bool needsAllocator() const { return true; }
  12719. virtual const void * nextRow()
  12720. {
  12721. ActivityTimer t(activityStats, timeActivities);
  12722. for (;;)
  12723. {
  12724. const void * left = inputStream->nextRow();
  12725. if (!left && (numProcessedLastGroup == processed))
  12726. left = inputStream->nextRow();
  12727. if (!left)
  12728. {
  12729. if (numProcessedLastGroup == processed)
  12730. {
  12731. const void * nextRight = inputStream1->nextRow();
  12732. if (nextRight)
  12733. {
  12734. ReleaseRoxieRow(nextRight);
  12735. throw MakeStringException(ROXIE_MISSING_GROUP_ERROR, "Missing LEFT record for Combine Group (%d)", factory->queryId());
  12736. }
  12737. }
  12738. else
  12739. numProcessedLastGroup = processed;
  12740. return NULL;
  12741. }
  12742. ConstPointerArray group;
  12743. for (;;)
  12744. {
  12745. const void * in = inputStream1->nextRow();
  12746. if (!in)
  12747. break;
  12748. group.append(in);
  12749. }
  12750. if (group.ordinality() == 0)
  12751. {
  12752. ReleaseRoxieRow(left);
  12753. ReleaseRoxieRows(group);
  12754. throw MakeStringException(ROXIE_MISSING_GROUP_ERROR, "Missing RIGHT group for Combine Group (%d)", factory->queryId());
  12755. }
  12756. try
  12757. {
  12758. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  12759. size32_t outSize = helper.transform(rowBuilder, left, group.ordinality(), (const void * *)group.getArray());
  12760. ReleaseRoxieRow(left);
  12761. ReleaseRoxieRows(group);
  12762. if (outSize)
  12763. {
  12764. processed++;
  12765. return rowBuilder.finalizeRowClear(outSize);
  12766. }
  12767. }
  12768. catch (IException *E)
  12769. {
  12770. ReleaseRoxieRow(left);
  12771. ReleaseRoxieRows(group);
  12772. throw makeWrappedException(E);
  12773. }
  12774. }
  12775. }
  12776. };
  12777. class CRoxieServerCombineGroupActivityFactory : public CRoxieServerActivityFactory
  12778. {
  12779. unsigned input2;
  12780. unsigned input2idx;
  12781. public:
  12782. CRoxieServerCombineGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12783. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  12784. {
  12785. input2 = 0;
  12786. input2idx = 0;
  12787. }
  12788. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  12789. {
  12790. return new CRoxieServerCombineGroupActivity(_ctx, this, _probeManager);
  12791. }
  12792. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  12793. {
  12794. if (idx==1)
  12795. {
  12796. input2 = source;
  12797. input2idx = sourceidx;
  12798. }
  12799. else
  12800. CRoxieServerActivityFactory::setInput(idx, source, sourceidx);
  12801. }
  12802. virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
  12803. {
  12804. switch (idx)
  12805. {
  12806. case 1:
  12807. sourceidx = input2idx;
  12808. return input2;
  12809. case 0:
  12810. return CRoxieServerActivityFactory::getInput(idx, sourceidx);
  12811. default:
  12812. return (unsigned) -1;
  12813. }
  12814. }
  12815. virtual unsigned numInputs() const { return 2; }
  12816. };
  12817. IRoxieServerActivityFactory *createRoxieServerCombineGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12818. {
  12819. return new CRoxieServerCombineGroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  12820. }
  12821. //=================================================================================
  12822. class CRoxieServerRollupGroupActivity : public CRoxieServerActivity
  12823. {
  12824. IHThorRollupGroupArg &helper;
  12825. bool eof;
  12826. public:
  12827. CRoxieServerRollupGroupActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  12828. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  12829. helper((IHThorRollupGroupArg &)basehelper)
  12830. {
  12831. eof = false;
  12832. }
  12833. ~CRoxieServerRollupGroupActivity()
  12834. {
  12835. }
  12836. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12837. {
  12838. eof = false;
  12839. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  12840. }
  12841. virtual bool needsAllocator() const { return true; }
  12842. virtual const void * nextRow()
  12843. {
  12844. ActivityTimer t(activityStats, timeActivities);
  12845. if (eof)
  12846. return NULL;
  12847. for (;;)
  12848. {
  12849. ConstPointerArray group;
  12850. for (;;)
  12851. {
  12852. const void * in = inputStream->nextRow();
  12853. if (!in)
  12854. break;
  12855. group.append(in);
  12856. }
  12857. if (group.ordinality() == 0)
  12858. {
  12859. eof = true;
  12860. return NULL;
  12861. }
  12862. try
  12863. {
  12864. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  12865. size32_t outSize = helper.transform(rowBuilder, group.ordinality(), (const void * *)group.getArray());
  12866. ReleaseRoxieRows(group);
  12867. if (outSize)
  12868. {
  12869. processed++;
  12870. return rowBuilder.finalizeRowClear(outSize);
  12871. }
  12872. }
  12873. catch (IException * E)
  12874. {
  12875. ReleaseRoxieRows(group);
  12876. throw makeWrappedException(E);
  12877. }
  12878. }
  12879. }
  12880. };
  12881. class CRoxieServerRollupGroupActivityFactory : public CRoxieServerActivityFactory
  12882. {
  12883. public:
  12884. CRoxieServerRollupGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12885. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  12886. {
  12887. }
  12888. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  12889. {
  12890. return new CRoxieServerRollupGroupActivity(_ctx, this, _probeManager);
  12891. }
  12892. };
  12893. IRoxieServerActivityFactory *createRoxieServerRollupGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12894. {
  12895. return new CRoxieServerRollupGroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  12896. }
  12897. //=================================================================================
  12898. class CRoxieServerFilterProjectActivity : public CRoxieServerLateStartActivity
  12899. {
  12900. IHThorFilterProjectArg &helper;
  12901. unsigned numProcessedLastGroup;
  12902. unsigned __int64 recordCount;
  12903. public:
  12904. CRoxieServerFilterProjectActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  12905. : CRoxieServerLateStartActivity(_ctx, _factory, _probeManager), helper((IHThorFilterProjectArg &)basehelper)
  12906. {
  12907. numProcessedLastGroup = 0;
  12908. recordCount = 0;
  12909. }
  12910. ~CRoxieServerFilterProjectActivity()
  12911. {
  12912. }
  12913. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  12914. {
  12915. numProcessedLastGroup = 0;
  12916. recordCount = 0;
  12917. CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
  12918. lateStart(parentExtractSize, parentExtract, helper.canMatchAny()); //sets eof
  12919. }
  12920. virtual bool needsAllocator() const { return true; }
  12921. virtual const void * nextRow()
  12922. {
  12923. ActivityTimer t(activityStats, timeActivities);
  12924. if (eof)
  12925. return NULL;
  12926. for (;;)
  12927. {
  12928. const void * in = inputStream->nextRow();
  12929. if (!in)
  12930. {
  12931. recordCount = 0;
  12932. if (numProcessedLastGroup == processed)
  12933. in = inputStream->nextRow();
  12934. if (!in)
  12935. {
  12936. numProcessedLastGroup = processed;
  12937. return NULL;
  12938. }
  12939. }
  12940. try
  12941. {
  12942. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  12943. size32_t outSize = helper.transform(rowBuilder, in, ++recordCount);
  12944. ReleaseRoxieRow(in);
  12945. if (outSize)
  12946. {
  12947. processed++;
  12948. return rowBuilder.finalizeRowClear(outSize);
  12949. }
  12950. }
  12951. catch (IException *E)
  12952. {
  12953. throw makeWrappedException(E);
  12954. }
  12955. }
  12956. }
  12957. };
  12958. class CRoxieServerFilterProjectActivityFactory : public CRoxieServerActivityFactory
  12959. {
  12960. public:
  12961. CRoxieServerFilterProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12962. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  12963. {
  12964. //MORE: Code generator needs to indicate if the count is used.
  12965. //optStableInput = false;
  12966. }
  12967. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  12968. {
  12969. return new CRoxieServerFilterProjectActivity(_ctx, this, _probeManager);
  12970. }
  12971. };
  12972. IRoxieServerActivityFactory *createRoxieServerFilterProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  12973. {
  12974. return new CRoxieServerFilterProjectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  12975. }
  12976. //=====================================================
  12977. class CRoxieServerStrandedProjectActivity : public CRoxieServerStrandedActivity
  12978. {
  12979. class ProjectProcessor : public StrandProcessor
  12980. {
  12981. protected:
  12982. IHThorProjectArg &helper;
  12983. public:
  12984. ProjectProcessor(CRoxieServerActivity &_parent, IEngineRowStream *_inputStream, IHThorProjectArg &_helper)
  12985. : StrandProcessor(_parent, _inputStream, true), helper(_helper)
  12986. {
  12987. }
  12988. virtual const void * nextRow()
  12989. {
  12990. ActivityTimer t(activityStats, timeActivities);
  12991. for (;;)
  12992. {
  12993. OwnedConstRoxieRow in = inputStream->nextRow();
  12994. if (!in)
  12995. {
  12996. if (numProcessedLastGroup == processed)
  12997. in.setown(inputStream->nextRow());
  12998. if (!in)
  12999. {
  13000. numProcessedLastGroup = processed;
  13001. return NULL;
  13002. }
  13003. }
  13004. try
  13005. {
  13006. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  13007. size32_t outSize;
  13008. outSize = helper.transform(rowBuilder, in);
  13009. if (outSize)
  13010. {
  13011. processed++;
  13012. return rowBuilder.finalizeRowClear(outSize);
  13013. }
  13014. }
  13015. catch (IException *E)
  13016. {
  13017. throw parent.makeWrappedException(E);
  13018. }
  13019. }
  13020. }
  13021. };
  13022. public:
  13023. CRoxieServerStrandedProjectActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const StrandOptions &_strandOptions)
  13024. : CRoxieServerStrandedActivity(_ctx, _factory, _probeManager, _strandOptions)
  13025. {
  13026. }
  13027. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13028. {
  13029. CRoxieServerStrandedActivity::start(parentExtractSize, parentExtract, paused);
  13030. onStartStrands();
  13031. }
  13032. virtual StrandProcessor *createStrandProcessor(IEngineRowStream *instream)
  13033. {
  13034. return new ProjectProcessor(*this, instream, (IHThorProjectArg &) basehelper);
  13035. }
  13036. virtual StrandProcessor *createStrandSourceProcessor(bool inputOrdered) { throwUnexpected(); }
  13037. };
  13038. class CRoxieServerProjectActivity : public CRoxieServerActivity
  13039. {
  13040. unsigned numProcessedLastGroup;
  13041. unsigned __int64 recordCount;
  13042. bool count;
  13043. public:
  13044. CRoxieServerProjectActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _count)
  13045. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  13046. count(_count)
  13047. {
  13048. numProcessedLastGroup = 0;
  13049. recordCount = 0;
  13050. }
  13051. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13052. {
  13053. numProcessedLastGroup = 0;
  13054. recordCount = 0;
  13055. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  13056. }
  13057. virtual bool needsAllocator() const { return true; }
  13058. virtual const void * nextRow()
  13059. {
  13060. ActivityTimer t(activityStats, timeActivities);
  13061. for (;;)
  13062. {
  13063. OwnedConstRoxieRow in = inputStream->nextRow();
  13064. if (!in)
  13065. {
  13066. recordCount = 0;
  13067. if (numProcessedLastGroup == processed)
  13068. in.setown(inputStream->nextRow());
  13069. if (!in)
  13070. {
  13071. numProcessedLastGroup = processed;
  13072. return NULL;
  13073. }
  13074. }
  13075. try
  13076. {
  13077. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  13078. size32_t outSize;
  13079. if (count)
  13080. outSize = ((IHThorCountProjectArg &) basehelper).transform(rowBuilder, in, ++recordCount);
  13081. else
  13082. outSize = ((IHThorProjectArg &) basehelper).transform(rowBuilder, in);
  13083. if (outSize)
  13084. {
  13085. processed++;
  13086. return rowBuilder.finalizeRowClear(outSize);
  13087. }
  13088. }
  13089. catch (IException *E)
  13090. {
  13091. throw makeWrappedException(E);
  13092. }
  13093. }
  13094. }
  13095. };
  13096. class CRoxieServerProjectActivityFactory : public CRoxieServerActivityFactory
  13097. {
  13098. protected:
  13099. StrandOptions strandOptions;
  13100. bool count;
  13101. public:
  13102. CRoxieServerProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  13103. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), strandOptions(_graphNode)
  13104. {
  13105. count = (kind==TAKcountproject || kind==TAKprefetchcountproject);
  13106. optStableInput = count;
  13107. }
  13108. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  13109. {
  13110. // NOTE - if they explicitly requested numStrands = 1 via hint, we could use either of the below. For now use the stranded version to ensure it is tested.
  13111. if (kind == TAKproject) // Not supported on prefetch or count projects
  13112. return new CRoxieServerStrandedProjectActivity(_ctx, this, _probeManager, strandOptions);
  13113. else
  13114. return new CRoxieServerProjectActivity(_ctx, this, _probeManager, count);
  13115. }
  13116. };
  13117. IRoxieServerActivityFactory *createRoxieServerProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  13118. {
  13119. return new CRoxieServerProjectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  13120. }
  13121. //=================================================================================
  13122. class CRoxieServerPrefetchProjectActivity : public CRoxieServerActivity, implements IRecordPullerCallback
  13123. {
  13124. unsigned numProcessedLastGroup;
  13125. bool eof;
  13126. bool allPulled;
  13127. bool isThreaded;
  13128. unsigned preload;
  13129. unsigned __int64 recordCount;
  13130. IHThorPrefetchProjectArg &helper;
  13131. RecordPullerThread puller;
  13132. InterruptableSemaphore ready;
  13133. InterruptableSemaphore space;
  13134. class PrefetchInfo : public CInterface
  13135. {
  13136. public:
  13137. inline PrefetchInfo(IHThorPrefetchProjectArg &helper, const void *_in, unsigned __int64 _recordCount)
  13138. {
  13139. if (helper.preTransform(extract, _in, _recordCount))
  13140. {
  13141. in.setown(_in);
  13142. recordCount = _recordCount;
  13143. }
  13144. else
  13145. {
  13146. ReleaseRoxieRow(_in);
  13147. recordCount = 0;
  13148. }
  13149. }
  13150. OwnedConstRoxieRow in;
  13151. unsigned __int64 recordCount;
  13152. rtlRowBuilder extract;
  13153. };
  13154. QueueOf<PrefetchInfo, true> pulled;
  13155. CriticalSection pulledCrit;
  13156. public:
  13157. CRoxieServerPrefetchProjectActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  13158. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  13159. helper((IHThorPrefetchProjectArg &) basehelper),
  13160. puller(false)
  13161. {
  13162. numProcessedLastGroup = 0;
  13163. recordCount = 0;
  13164. eof = false;
  13165. allPulled = false;
  13166. isThreaded = (helper.getFlags() & PPFparallel) != 0;
  13167. preload = 0;
  13168. }
  13169. ~CRoxieServerPrefetchProjectActivity()
  13170. {
  13171. while (pulled.ordinality())
  13172. ::Release(pulled.dequeue());
  13173. }
  13174. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  13175. {
  13176. if (idx)
  13177. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", sanitizeSourceFile(__FILE__), __LINE__);
  13178. puller.setInput(this, _sourceIdx, _in);
  13179. }
  13180. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks)
  13181. {
  13182. puller.connectInputStreams(ctx, consumerOrdered);
  13183. return CRoxieServerActivity::getOutputStreams(ctx, idx, streams, consumerOptions, consumerOrdered, orderedCallbacks);
  13184. }
  13185. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  13186. {
  13187. if (idx==0)
  13188. return puller.queryInput();
  13189. else
  13190. return NULL;
  13191. }
  13192. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13193. {
  13194. numProcessedLastGroup = 0;
  13195. recordCount = 0;
  13196. eof = false;
  13197. allPulled = false;
  13198. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  13199. preload = helper.getLookahead();
  13200. if (!preload)
  13201. preload = ctx->queryOptions().prefetchProjectPreload;
  13202. space.reinit(preload);
  13203. ready.reinit();
  13204. puller.start(parentExtractSize, parentExtract, paused, preload, !isThreaded, ctx);
  13205. }
  13206. virtual void stop()
  13207. {
  13208. space.interrupt();
  13209. ready.interrupt();
  13210. CRoxieServerActivity::stop();
  13211. puller.stop();
  13212. }
  13213. virtual void reset()
  13214. {
  13215. CRoxieServerActivity::reset();
  13216. puller.reset();
  13217. allPulled = false;
  13218. while (pulled.ordinality())
  13219. ::Release(pulled.dequeue());
  13220. }
  13221. virtual PrefetchInfo *readNextRecord()
  13222. {
  13223. if (!isThreaded)
  13224. {
  13225. if (!allPulled) // This looks like it's thread unsafe but we are inside the if(!isThreaded) so should be ok
  13226. puller.pullRecords(1);
  13227. }
  13228. else
  13229. ready.wait();
  13230. CriticalBlock b(pulledCrit);
  13231. PrefetchInfo *ret = pulled.ordinality() ? pulled.dequeue() : NULL;
  13232. space.signal();
  13233. return ret;
  13234. }
  13235. virtual bool needsAllocator() const { return true; }
  13236. virtual const void * nextRow()
  13237. {
  13238. ActivityTimer t(activityStats, timeActivities);
  13239. if (eof)
  13240. return NULL;
  13241. for (;;)
  13242. {
  13243. Owned<PrefetchInfo> in = readNextRecord();
  13244. if (!in)
  13245. {
  13246. recordCount = 0;
  13247. if (numProcessedLastGroup == processed)
  13248. in.setown(readNextRecord());
  13249. if (!in)
  13250. {
  13251. numProcessedLastGroup = processed;
  13252. eof = true;
  13253. return NULL;
  13254. }
  13255. }
  13256. try
  13257. {
  13258. if (in->in)
  13259. {
  13260. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  13261. size32_t outSize;
  13262. IThorChildGraph *child = helper.queryChild();
  13263. Owned<IEclGraphResults> results;
  13264. if (child)
  13265. results.setown(child->evaluate(in->extract.size(), in->extract.getbytes()));
  13266. outSize = helper.transform(rowBuilder, in->in, results, in->recordCount);
  13267. if (outSize)
  13268. {
  13269. processed++;
  13270. return rowBuilder.finalizeRowClear(outSize);
  13271. }
  13272. }
  13273. }
  13274. catch (IException *E)
  13275. {
  13276. throw makeWrappedException(E);
  13277. }
  13278. }
  13279. }
  13280. // interface IExceptionHandler
  13281. virtual bool fireException(IException *e)
  13282. {
  13283. // called from puller thread on failure
  13284. ready.interrupt(LINK(e));
  13285. space.interrupt(e);
  13286. return true;
  13287. }
  13288. // interface IRecordPullerCallback
  13289. virtual void processRow(const void *row)
  13290. {
  13291. {
  13292. CriticalBlock b(pulledCrit);
  13293. pulled.enqueue(new PrefetchInfo(helper, row, ++recordCount));
  13294. }
  13295. if (isThreaded)
  13296. {
  13297. ready.signal();
  13298. space.wait();
  13299. }
  13300. }
  13301. virtual void processEOG()
  13302. {
  13303. {
  13304. CriticalBlock b(pulledCrit);
  13305. pulled.enqueue(NULL);
  13306. }
  13307. if (isThreaded)
  13308. {
  13309. ready.signal();
  13310. space.wait();
  13311. }
  13312. }
  13313. virtual void processGroup(const ConstPointerArray &rows)
  13314. {
  13315. throwUnexpected();
  13316. }
  13317. virtual void processDone()
  13318. {
  13319. CriticalBlock b(pulledCrit);
  13320. allPulled = true;
  13321. if (isThreaded)
  13322. ready.signal();
  13323. }
  13324. };
  13325. class CRoxieServerPrefetchProjectActivityFactory : public CRoxieServerProjectActivityFactory
  13326. {
  13327. public:
  13328. CRoxieServerPrefetchProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  13329. : CRoxieServerProjectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  13330. {
  13331. }
  13332. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  13333. {
  13334. return new CRoxieServerPrefetchProjectActivity(_ctx, this, _probeManager);
  13335. }
  13336. };
  13337. extern IRoxieServerActivityFactory *createRoxieServerPrefetchProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  13338. {
  13339. return new CRoxieServerPrefetchProjectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  13340. }
  13341. //=================================================================================
  13342. class CPointerArrayRoxieInput : public CPseudoRoxieInput
  13343. {
  13344. public:
  13345. CPointerArrayRoxieInput()
  13346. {
  13347. rowset = NULL;
  13348. rowcount = 0;
  13349. curRow = 0;
  13350. }
  13351. void init(size32_t _rowcount, const byte **_rowset)
  13352. {
  13353. rowset = _rowset;
  13354. rowcount = _rowcount;
  13355. curRow = 0;
  13356. }
  13357. virtual const void * nextRow()
  13358. {
  13359. if (curRow < rowcount)
  13360. {
  13361. const void * ret = rowset[curRow];
  13362. if (ret)
  13363. LinkRoxieRow(ret);
  13364. curRow++;
  13365. return ret;
  13366. }
  13367. return NULL;
  13368. }
  13369. protected:
  13370. const byte **rowset;
  13371. size32_t rowcount;
  13372. size32_t curRow;
  13373. };
  13374. class CRoxieServerLoopActivity : public CRoxieServerActivity
  13375. {
  13376. protected:
  13377. IHThorLoopArg &helper;
  13378. ThorActivityKind activityKind;
  13379. unsigned maxIterations;
  13380. bool finishedLooping;
  13381. unsigned flags;
  13382. bool eof;
  13383. rtlRowBuilder loopExtractBuilder;
  13384. unsigned loopGraphId;
  13385. Linked<IOutputMetaData> counterMeta;
  13386. public:
  13387. CRoxieServerLoopActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _loopGraphId, IOutputMetaData * _counterMeta)
  13388. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  13389. helper((IHThorLoopArg &)basehelper), loopGraphId(_loopGraphId), counterMeta(_counterMeta)
  13390. {
  13391. eof = false;
  13392. finishedLooping = false;
  13393. activityKind = factory->getKind();
  13394. flags = helper.getFlags();
  13395. maxIterations = 0;
  13396. }
  13397. virtual bool needsAllocator() const { return true; }
  13398. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13399. {
  13400. eof = false;
  13401. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  13402. int iterations = (int) helper.numIterations();
  13403. maxIterations = (iterations >= 0) ? iterations : 0;
  13404. finishedLooping = ((activityKind == TAKloopcount) && (maxIterations == 0));
  13405. if ((flags & IHThorLoopArg::LFnewloopagain) && !helper.loopFirstTime())
  13406. finishedLooping = true;
  13407. loopExtractBuilder.clear();
  13408. helper.createParentExtract(loopExtractBuilder); // could possibly delay this until execution actually happens
  13409. }
  13410. virtual void stop()
  13411. {
  13412. CRoxieServerActivity::stop();
  13413. loopExtractBuilder.clear();
  13414. }
  13415. void createCounterResult(IRoxieServerChildGraph * graph, unsigned counter)
  13416. {
  13417. if (flags & IHThorLoopArg::LFcounter)
  13418. {
  13419. void * counterRow = ctx->queryRowManager().allocate(sizeof(thor_loop_counter_t), activityId);
  13420. *((thor_loop_counter_t *)counterRow) = counter;
  13421. RtlLinkedDatasetBuilder builder(rowAllocator);
  13422. builder.appendOwn(counterRow);
  13423. Owned<CGraphResult> counterResult = new CGraphResult(builder.getcount(), builder.linkrows());
  13424. graph->setInputResult(2, counterResult);
  13425. }
  13426. }
  13427. };
  13428. //=================================================================================
  13429. class CRoxieServerSequentialLoopActivity : public CRoxieServerLoopActivity
  13430. {
  13431. Owned<IActivityGraph> loopQuery;
  13432. Owned<IRoxieServerChildGraph> loopGraph;
  13433. IEngineRowStream * curStream;
  13434. RtlLinkedDatasetBuilder *loopInputBuilder;
  13435. CPointerArrayRoxieInput arrayInput;
  13436. Linked<IEngineRowStream> resultStream;
  13437. unsigned loopCounter;
  13438. public:
  13439. CRoxieServerSequentialLoopActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _loopGraphId, IOutputMetaData * _counterMeta)
  13440. : CRoxieServerLoopActivity(_ctx, _factory, _probeManager, _loopGraphId, _counterMeta)
  13441. {
  13442. curStream = NULL;
  13443. loopCounter = 0;
  13444. loopInputBuilder = NULL;
  13445. }
  13446. virtual bool needsAllocator() const { return true; }
  13447. virtual void onCreate(IHThorArg *_colocalParent)
  13448. {
  13449. CRoxieServerLoopActivity::onCreate(_colocalParent);
  13450. loopQuery.set(ctx->queryChildGraph(loopGraphId));
  13451. }
  13452. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13453. {
  13454. curStream = inputStream;
  13455. loopCounter = 1;
  13456. CRoxieServerLoopActivity::start(parentExtractSize, parentExtract, paused);
  13457. //MORE: Not sure about this, should IRoxieServerChildGraph be combined with IActivityGraph?
  13458. loopGraph.set(loopQuery->queryLoopGraph());
  13459. loopInputBuilder = new RtlLinkedDatasetBuilder(rowAllocator);
  13460. }
  13461. virtual void stop()
  13462. {
  13463. delete loopInputBuilder;
  13464. loopInputBuilder = NULL;
  13465. CRoxieServerLoopActivity::stop();
  13466. }
  13467. virtual const void * nextRow()
  13468. {
  13469. ActivityTimer t(activityStats, timeActivities);
  13470. if (eof)
  13471. return NULL;
  13472. unsigned emptyIterations = 0;
  13473. for (;;)
  13474. {
  13475. for (;;)
  13476. {
  13477. const void * ret = curStream->nextRow();
  13478. if (!ret)
  13479. {
  13480. ret = curStream->nextRow(); // more cope with groups somehow....
  13481. if (!ret)
  13482. {
  13483. if (finishedLooping)
  13484. {
  13485. eof = true;
  13486. return NULL;
  13487. }
  13488. break;
  13489. }
  13490. }
  13491. if (finishedLooping ||
  13492. ((flags & IHThorLoopArg::LFfiltered) && !helper.sendToLoop(loopCounter, ret)))
  13493. {
  13494. processed++;
  13495. return ret;
  13496. }
  13497. loopInputBuilder->appendOwn(ret);
  13498. }
  13499. switch (activityKind)
  13500. {
  13501. case TAKloopdataset:
  13502. {
  13503. if (!(flags & IHThorLoopArg::LFnewloopagain))
  13504. {
  13505. if (!helper.loopAgain(loopCounter, loopInputBuilder->getcount(), (const void**) loopInputBuilder->queryrows()))
  13506. {
  13507. if (loopInputBuilder->getcount() == 0)
  13508. {
  13509. eof = true;
  13510. return NULL;
  13511. }
  13512. arrayInput.init(loopInputBuilder->getcount(), loopInputBuilder->linkrows());
  13513. // MORE - should builder be cleared here?
  13514. curStream = &arrayInput;
  13515. finishedLooping = true;
  13516. continue; // back to the input loop again
  13517. }
  13518. }
  13519. break;
  13520. }
  13521. case TAKlooprow:
  13522. if (!loopInputBuilder->getcount())
  13523. {
  13524. finishedLooping = true;
  13525. eof = true;
  13526. return NULL;
  13527. }
  13528. break;
  13529. }
  13530. if (loopInputBuilder->getcount())
  13531. emptyIterations = 0;
  13532. else
  13533. {
  13534. //note: any outputs which didn't go around the loop again, would return the record, reinitializing emptyIterations
  13535. emptyIterations++;
  13536. if (emptyIterations > maxEmptyLoopIterations)
  13537. throw MakeStringException(ROXIE_TOO_MANY_EMPTY_LOOP, "Executed LOOP with empty input and output %u times", emptyIterations);
  13538. if (emptyIterations % 32 == 0)
  13539. CTXLOG("Executing LOOP with empty input and output %u times", emptyIterations);
  13540. }
  13541. checkAbort();
  13542. try
  13543. {
  13544. Owned<IRoxieGraphResults> results = executeIteration(loopExtractBuilder.size(), loopExtractBuilder.getbytes(), loopCounter);
  13545. resultStream.setown(results->createIterator(0));
  13546. if (flags & IHThorLoopArg::LFnewloopagain)
  13547. {
  13548. Owned<IEngineRowStream> againResult = results->createIterator(helper.loopAgainResult());
  13549. OwnedConstRoxieRow row = againResult->nextRow();
  13550. assertex(row);
  13551. //Result is a row which contains a single boolean field.
  13552. if (!((const bool *)row.get())[0])
  13553. finishedLooping = true;
  13554. }
  13555. }
  13556. catch (IException *E)
  13557. {
  13558. throw makeWrappedException(E);
  13559. }
  13560. curStream = resultStream.get();
  13561. loopCounter++;
  13562. if ((activityKind == TAKloopcount) && (loopCounter > maxIterations))
  13563. finishedLooping = true;
  13564. }
  13565. }
  13566. IRoxieGraphResults * executeIteration(unsigned parentExtractSize, const byte *parentExtract, unsigned counter)
  13567. {
  13568. try
  13569. {
  13570. loopGraph->beforeExecute();
  13571. Owned<IGraphResult> inputRowsResult = new CGraphResult(loopInputBuilder->getcount(), loopInputBuilder->linkrows());
  13572. loopInputBuilder->clear();
  13573. loopGraph->setInputResult(1, inputRowsResult);
  13574. createCounterResult(loopGraph, counter);
  13575. Owned<IRoxieGraphResults> ret = loopGraph->execute(parentExtractSize, parentExtract);
  13576. loopGraph->afterExecute();
  13577. return ret.getClear();
  13578. }
  13579. catch (...)
  13580. {
  13581. CTXLOG("Exception thrown in loop body - cleaning up");
  13582. loopGraph->afterExecute();
  13583. throw;
  13584. }
  13585. }
  13586. };
  13587. //=================================================================================
  13588. typedef SafeQueueOf<const void, true> SafeRowQueue;
  13589. class CRowQueuePseudoInput : public CPseudoRoxieInput
  13590. {
  13591. public:
  13592. CRowQueuePseudoInput(SafeRowQueue & _input, IOutputMetaData * _meta)
  13593. : input(_input), meta(_meta)
  13594. {
  13595. eof = false;
  13596. }
  13597. virtual const void * nextRow()
  13598. {
  13599. if (eof)
  13600. return NULL;
  13601. const void * ret = input.dequeue();
  13602. if (!ret)
  13603. eof = true;
  13604. return ret;
  13605. }
  13606. virtual IOutputMetaData * queryOutputMeta() const { return meta; }
  13607. protected:
  13608. SafeRowQueue & input;
  13609. IOutputMetaData * meta;
  13610. bool eof;
  13611. };
  13612. class CRoxieServerParallelLoopActivity;
  13613. class LoopFilterPseudoInput : public CIndirectRoxieInput
  13614. {
  13615. public:
  13616. LoopFilterPseudoInput(CRoxieServerParallelLoopActivity * _activity, unsigned _counter) :
  13617. activity(_activity), counter(_counter)
  13618. {
  13619. }
  13620. virtual const void * nextRow();
  13621. protected:
  13622. CRoxieServerParallelLoopActivity * activity;
  13623. unsigned counter;
  13624. };
  13625. class LoopExecutorThread : public RestartableThread
  13626. {
  13627. protected:
  13628. Owned<CSafeRoxieInput> safeInput;
  13629. CRoxieServerParallelLoopActivity * activity;
  13630. bool eof;
  13631. CriticalSection crit;
  13632. unsigned flags;
  13633. SafeRowQueue tempResults[2];
  13634. unsigned savedParentExtractSize;
  13635. const byte * savedParentExtract;
  13636. IArrayOf<IActivityGraph> cachedGraphs;
  13637. IRoxieAgentContext *ctx;
  13638. public:
  13639. LoopExecutorThread()
  13640. : RestartableThread("LoopExecutorThread")
  13641. {
  13642. activity = NULL;
  13643. eof = false;
  13644. flags = 0;
  13645. ctx = NULL;
  13646. savedParentExtract = NULL;
  13647. savedParentExtractSize = 0;
  13648. }
  13649. void setInput(CRoxieServerParallelLoopActivity * _activity, unsigned _sourceIdx, IFinalRoxieInput *_input, unsigned _flags)
  13650. {
  13651. activity = _activity;
  13652. flags = _flags;
  13653. // 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-nextRow etc etc.
  13654. safeInput.setown(new CSafeRoxieInput(_sourceIdx, _input));
  13655. }
  13656. void connectInputStreams(bool consumerOrdered)
  13657. {
  13658. safeInput->connectInputStreams(ctx, consumerOrdered);
  13659. }
  13660. IFinalRoxieInput *queryInput() const
  13661. {
  13662. return safeInput;
  13663. }
  13664. void onCreate(IRoxieAgentContext * _ctx);
  13665. void start(unsigned parentExtractSize, const byte *parentExtract, bool paused);
  13666. void stop();
  13667. void reset();
  13668. virtual int run();
  13669. protected:
  13670. void executeLoop();
  13671. void executeLoopInstance(unsigned counter, unsigned numIterations, unsigned inputIdx, IFinalRoxieInput * input, SafeRowQueue * spillOutput);
  13672. IFinalRoxieInput * createLoopIterationGraph(unsigned i, unsigned inputIdx, IFinalRoxieInput * input, unsigned counter);
  13673. };
  13674. class CRoxieServerParallelLoopActivity : public CRoxieServerLoopActivity
  13675. {
  13676. friend class LoopFilterPseudoInput;
  13677. friend class LoopExecutorThread;
  13678. QueueOf<const void, true> ready;
  13679. CriticalSection helperCS;
  13680. CriticalSection cs;
  13681. unsigned defaultNumParallel;
  13682. LoopExecutorThread executor;
  13683. IProbeManager* probeManager;
  13684. CriticalSection canAccess;
  13685. CriticalSection scrit;
  13686. InterruptableSemaphore readySpace;
  13687. InterruptableSemaphore recordsReady;
  13688. protected:
  13689. bool includeInLoop(unsigned counter, const void * row)
  13690. {
  13691. CriticalBlock b(helperCS);
  13692. return helper.sendToLoop(counter, row);
  13693. }
  13694. public:
  13695. CRoxieServerParallelLoopActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _loopGraphId, IOutputMetaData * _counterMeta)
  13696. : CRoxieServerLoopActivity(_ctx, _factory, _probeManager, _loopGraphId, _counterMeta),
  13697. readySpace(parallelLoopFlowLimit)
  13698. {
  13699. probeManager = _probeManager;
  13700. defaultNumParallel = 0;
  13701. }
  13702. virtual void onCreate(IHThorArg *_colocalParent)
  13703. {
  13704. CRoxieServerActivity::onCreate(_colocalParent);
  13705. executor.onCreate(ctx);
  13706. }
  13707. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13708. {
  13709. CriticalBlock b(scrit); // can stop while still starting, if unlucky...
  13710. readySpace.reinit(parallelLoopFlowLimit);
  13711. recordsReady.reinit();
  13712. CRoxieServerLoopActivity::start(parentExtractSize, parentExtract, paused);
  13713. defaultNumParallel = helper.defaultParallelIterations();
  13714. if (!defaultNumParallel)
  13715. defaultNumParallel = DEFAULT_PARALLEL_LOOP_THREADS;
  13716. //MORE: If numIterations <= number of parallel iterations[1],
  13717. //then we don't need to create a separate thread to do the processing, and the results will also avoid
  13718. //being transferred via a queue
  13719. executor.start(parentExtractSize, parentExtract, paused);
  13720. }
  13721. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  13722. {
  13723. if (idx)
  13724. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", sanitizeSourceFile(__FILE__), __LINE__);
  13725. executor.setInput(this, _sourceIdx, _in, flags);
  13726. }
  13727. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks)
  13728. {
  13729. executor.connectInputStreams(consumerOrdered);
  13730. return CRoxieServerActivity::getOutputStreams(ctx, idx, streams, nullptr, consumerOrdered, nullptr);
  13731. }
  13732. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  13733. {
  13734. if (idx==0)
  13735. return executor.queryInput();
  13736. else
  13737. return NULL;
  13738. }
  13739. virtual void stop()
  13740. {
  13741. CriticalBlock b(scrit); // can stop while still starting, if unlucky...
  13742. readySpace.interrupt();
  13743. recordsReady.interrupt();
  13744. executor.join(); // MORE - may not be needed given stop/reset split
  13745. CRoxieServerLoopActivity::stop();
  13746. }
  13747. virtual void reset()
  13748. {
  13749. while (ready.ordinality())
  13750. ReleaseRoxieRow(ready.dequeue());
  13751. executor.reset();
  13752. CRoxieServerActivity::reset();
  13753. }
  13754. virtual const void * nextRow()
  13755. {
  13756. ActivityTimer t(activityStats, timeActivities);
  13757. for (;;)
  13758. {
  13759. if (eof)
  13760. return NULL;
  13761. recordsReady.wait();
  13762. CriticalBlock procedure(canAccess);
  13763. if (ready.ordinality())
  13764. {
  13765. const void *result = ready.dequeue();
  13766. readySpace.signal();
  13767. if (result)
  13768. processed++;
  13769. return result;
  13770. }
  13771. else
  13772. eof = true;
  13773. }
  13774. }
  13775. unsigned getNumParallel(unsigned iter)
  13776. {
  13777. return defaultNumParallel;
  13778. }
  13779. inline void enqueueResult(const void * row)
  13780. {
  13781. try
  13782. {
  13783. while(!readySpace.wait(1000))
  13784. {
  13785. CTXLOG("Blocked waiting for space in loop %p activity id: %d output queue: %d records in queue", this, queryId(), ready.ordinality());
  13786. }
  13787. }
  13788. catch (...)
  13789. {
  13790. ReleaseRoxieRow(row);
  13791. throw;
  13792. }
  13793. CriticalBlock b2(canAccess);
  13794. ready.enqueue(row);
  13795. recordsReady.signal();
  13796. }
  13797. inline void finishResults()
  13798. {
  13799. recordsReady.signal();
  13800. }
  13801. virtual bool fireException(IException *e)
  13802. {
  13803. readySpace.interrupt(LINK(e));
  13804. recordsReady.interrupt(e);
  13805. return true;
  13806. }
  13807. IActivityGraph * createChildGraphInstance()
  13808. {
  13809. return factory->createChildGraph(ctx, &helper, loopGraphId, this, probeManager, *this);
  13810. }
  13811. IActivityGraph * queryChildGraph()
  13812. {
  13813. return ctx->queryChildGraph(loopGraphId);
  13814. }
  13815. };
  13816. //=================================================================================
  13817. const void * LoopFilterPseudoInput::nextRow()
  13818. {
  13819. for (;;)
  13820. {
  13821. const void * next = stream->nextRow();
  13822. if (!next || activity->includeInLoop(counter, next))
  13823. return next;
  13824. activity->enqueueResult(next);
  13825. }
  13826. }
  13827. void LoopExecutorThread::onCreate(IRoxieAgentContext * _ctx)
  13828. {
  13829. //Initialise the cached graph list with the child instance that will always be created. Other iterations will be created on demand.
  13830. ctx = _ctx;
  13831. cachedGraphs.append(*LINK(activity->queryChildGraph()));
  13832. }
  13833. void LoopExecutorThread::start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  13834. {
  13835. savedParentExtractSize = parentExtractSize;
  13836. savedParentExtract = parentExtract;
  13837. eof = false;
  13838. StringBuffer logPrefix("[");
  13839. ctx->getLogPrefix(logPrefix).append("] ");
  13840. RestartableThread::start(logPrefix);
  13841. }
  13842. int LoopExecutorThread::run()
  13843. {
  13844. try
  13845. {
  13846. executeLoop();
  13847. }
  13848. catch (IException *e)
  13849. {
  13850. activity->fireException(e);
  13851. }
  13852. catch (...)
  13853. {
  13854. activity->fireException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unexpected exception caught in LoopExecutorThread::run"));
  13855. }
  13856. return 0;
  13857. }
  13858. void LoopExecutorThread::stop()
  13859. {
  13860. safeInput->stop();
  13861. RestartableThread::join();
  13862. }
  13863. void LoopExecutorThread::reset()
  13864. {
  13865. safeInput->reset();
  13866. }
  13867. void LoopExecutorThread::executeLoop()
  13868. {
  13869. unsigned iterations = 0;
  13870. unsigned counter = 0;
  13871. unsigned outputIndex = 0;
  13872. //Note, activities don't link inputs, so need to be careful that special inputs remain linked while the activity is executing.
  13873. for (;;)
  13874. {
  13875. if (activity->activityKind == TAKloopcount)
  13876. {
  13877. if (counter == activity->maxIterations)
  13878. break;
  13879. }
  13880. else
  13881. {
  13882. //This condition isn't quite right because it needs to be whether the filtered
  13883. //input is empty. May be ok if we include that in the semantics,
  13884. if (tempResults[1-outputIndex].ordinality() == 0)
  13885. break;
  13886. }
  13887. unsigned numParallel = activity->getNumParallel(iterations);
  13888. Linked<IFinalRoxieInput> curInput;
  13889. if (iterations == 0)
  13890. curInput.set(safeInput);
  13891. else
  13892. {
  13893. SafeRowQueue & inputQueue = tempResults[1-outputIndex];
  13894. inputQueue.enqueue(NULL);
  13895. curInput.setown(new CRowQueuePseudoInput(inputQueue, activity->queryOutputMeta()));
  13896. }
  13897. SafeRowQueue * curOutput = NULL;
  13898. if (counter+numParallel > activity->maxIterations)
  13899. numParallel = activity->maxIterations - counter;
  13900. else if (counter+numParallel < activity->maxIterations)
  13901. curOutput = &tempResults[outputIndex];
  13902. executeLoopInstance(counter, numParallel, 0, curInput, curOutput); // CurInput is always SOME sort of proxy so idx always 0
  13903. outputIndex = 1-outputIndex;
  13904. counter += numParallel;
  13905. iterations++;
  13906. }
  13907. //Check for TAKlooprow, where end of loop couldn't be determined ahead of time
  13908. SafeRowQueue & inputQueue = tempResults[1-outputIndex];
  13909. while (inputQueue.ordinality())
  13910. {
  13911. const void * next = inputQueue.dequeue();
  13912. activity->enqueueResult(next);
  13913. }
  13914. activity->finishResults();
  13915. }
  13916. void LoopExecutorThread::executeLoopInstance(unsigned counter, unsigned numIterations, unsigned inputIdx, IFinalRoxieInput * input, SafeRowQueue * spillOutput)
  13917. {
  13918. IArrayOf<IFinalRoxieInput> savedInputs; // activities don't link their inputs, so this list keeps filters alive.
  13919. Linked<IFinalRoxieInput> curInput = input;
  13920. unsigned i;
  13921. for (i= 0; i != numIterations; i++)
  13922. {
  13923. unsigned thisCounter = counter+i+1;
  13924. IFinalRoxieInput * filtered = curInput;
  13925. if (flags & IHThorLoopArg::LFfiltered)
  13926. {
  13927. LoopFilterPseudoInput *newFiltered = new LoopFilterPseudoInput(activity, thisCounter);
  13928. newFiltered->setInput(inputIdx, curInput);
  13929. filtered = newFiltered;
  13930. savedInputs.append(*filtered);
  13931. inputIdx = 0;
  13932. }
  13933. //graph is kept, so new curInput will be guaranteed to exist
  13934. curInput.setown(createLoopIterationGraph(i, inputIdx, filtered, thisCounter));
  13935. inputIdx = 0;
  13936. }
  13937. Owned<IStrandJunction> curJunction;
  13938. IEngineRowStream *curStream = connectSingleStream(ctx, curInput, inputIdx, curJunction, true);
  13939. Owned<IException> failure;
  13940. try
  13941. {
  13942. curInput->start(savedParentExtractSize, savedParentExtract, false);
  13943. if (spillOutput)
  13944. {
  13945. for (;;)
  13946. {
  13947. const void * next = curStream->nextRow();
  13948. if (!next)
  13949. break;
  13950. spillOutput->enqueue(next);
  13951. }
  13952. }
  13953. else
  13954. {
  13955. for (;;)
  13956. {
  13957. const void * next = curStream->nextRow();
  13958. if (!next)
  13959. break;
  13960. activity->enqueueResult(next);
  13961. }
  13962. }
  13963. }
  13964. catch (IException *E)
  13965. {
  13966. ctx->notifyAbort(E);
  13967. failure.setown(E);
  13968. }
  13969. for (i= 0; i != numIterations; i++)
  13970. {
  13971. IRoxieServerChildGraph * loopGraph = cachedGraphs.item(i).queryLoopGraph();
  13972. loopGraph->afterExecute();
  13973. //NOTE: inputIdx is always 0 - the code above could be cleaned up.
  13974. loopGraph->querySetInputResult(1, 0, nullptr);
  13975. }
  13976. curStream->stop();
  13977. curInput->reset();
  13978. if (failure)
  13979. throw failure.getClear();
  13980. }
  13981. IFinalRoxieInput * LoopExecutorThread::createLoopIterationGraph(unsigned i, unsigned inputIdx, IFinalRoxieInput * input, unsigned counter)
  13982. {
  13983. if (!cachedGraphs.isItem(i))
  13984. cachedGraphs.append(*activity->createChildGraphInstance());
  13985. Linked<IRoxieServerChildGraph> loopGraph = cachedGraphs.item(i).queryLoopGraph();
  13986. loopGraph->beforeExecute();
  13987. if (!loopGraph->querySetInputResult(1, inputIdx, input))
  13988. throwUnexpected(); // a loop which doesn't use the value from the previous iteration. Should probably handle even if daft.
  13989. activity->createCounterResult(loopGraph, counter);
  13990. return loopGraph->selectOutput(0);
  13991. }
  13992. //=================================================================================
  13993. class CCounterRowMetaData : implements IOutputMetaData, public CInterface
  13994. {
  13995. public:
  13996. IMPLEMENT_IINTERFACE
  13997. virtual size32_t getRecordSize(const void *) { return sizeof(thor_loop_counter_t); }
  13998. virtual size32_t getMinRecordSize() const { return sizeof(thor_loop_counter_t); }
  13999. virtual size32_t getFixedSize() const { return sizeof(thor_loop_counter_t); }
  14000. virtual void toXML(const byte * self, IXmlWriter & out) { }
  14001. virtual unsigned getVersion() const { return OUTPUTMETADATA_VERSION; }
  14002. virtual unsigned getMetaFlags() { return 0; }
  14003. virtual const RtlTypeInfo * queryTypeInfo() const { return nullptr; }
  14004. virtual void destruct(byte * self) {}
  14005. virtual IOutputRowSerializer * createDiskSerializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  14006. virtual IOutputRowDeserializer * createDiskDeserializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  14007. virtual ISourceRowPrefetcher * createDiskPrefetcher() { return NULL; }
  14008. virtual IOutputMetaData * querySerializedDiskMeta() { return this; }
  14009. virtual IOutputRowSerializer * createInternalSerializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  14010. virtual IOutputRowDeserializer * createInternalDeserializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  14011. virtual void process(const byte * self, IFieldProcessor & target, unsigned from, unsigned to) {}
  14012. virtual void walkIndirectMembers(const byte * self, IIndirectMemberVisitor & visitor) {}
  14013. virtual IOutputMetaData * queryChildMeta(unsigned i) { return NULL; }
  14014. virtual const RtlRecord &queryRecordAccessor(bool expand) const { throwUnexpected(); } // could provide a static implementation if needed
  14015. };
  14016. class CRoxieServerLoopActivityFactory : public CRoxieServerActivityFactory
  14017. {
  14018. unsigned loopGraphId;
  14019. unsigned flags;
  14020. Linked<IOutputMetaData> counterMeta;
  14021. public:
  14022. CRoxieServerLoopActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _loopGraphId)
  14023. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), loopGraphId(_loopGraphId)
  14024. {
  14025. Owned<IHThorLoopArg> helper = (IHThorLoopArg *) helperFactory();
  14026. flags = helper->getFlags();
  14027. counterMeta.setown(new CCounterRowMetaData);
  14028. }
  14029. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  14030. {
  14031. if (flags & IHThorLoopArg::LFparallel)
  14032. return new CRoxieServerParallelLoopActivity(_ctx, this, _probeManager, loopGraphId, counterMeta);
  14033. else
  14034. return new CRoxieServerSequentialLoopActivity(_ctx, this, _probeManager, loopGraphId, counterMeta);
  14035. }
  14036. };
  14037. IRoxieServerActivityFactory *createRoxieServerLoopActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _loopGraphId)
  14038. {
  14039. return new CRoxieServerLoopActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _loopGraphId);
  14040. }
  14041. //=================================================================================
  14042. class CRoxieServerGraphLoopActivity : public CRoxieServerActivity
  14043. {
  14044. protected:
  14045. IHThorGraphLoopArg &helper;
  14046. unsigned maxIterations;
  14047. unsigned flags;
  14048. rtlRowBuilder GraphExtractBuilder;
  14049. unsigned loopGraphId;
  14050. Linked<IOutputMetaData> counterMeta;
  14051. public:
  14052. CRoxieServerGraphLoopActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _GraphGraphId, IOutputMetaData * _counterMeta)
  14053. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  14054. helper((IHThorGraphLoopArg &)basehelper), loopGraphId(_GraphGraphId), counterMeta(_counterMeta)
  14055. {
  14056. flags = helper.getFlags();
  14057. maxIterations = 0;
  14058. }
  14059. virtual bool needsAllocator() const { return true; }
  14060. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14061. {
  14062. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  14063. int iterations = (int) helper.numIterations();
  14064. maxIterations = (iterations >= 0) ? iterations : 0;
  14065. if (maxIterations > maxGraphLoopIterations)
  14066. throw MakeStringException(ROXIE_TOO_MANY_GRAPH_LOOP, "Attempt to execute graph %u times", maxIterations);
  14067. if (maxIterations != 0)
  14068. {
  14069. GraphExtractBuilder.clear();
  14070. helper.createParentExtract(GraphExtractBuilder);
  14071. }
  14072. }
  14073. virtual void stop()
  14074. {
  14075. CRoxieServerActivity::stop();
  14076. GraphExtractBuilder.clear();
  14077. }
  14078. void createCounterResult(IRoxieServerChildGraph * graph, unsigned counter)
  14079. {
  14080. if (flags & IHThorGraphLoopArg::GLFcounter)
  14081. {
  14082. void * counterRow = ctx->queryRowManager().allocate(sizeof(thor_loop_counter_t), activityId);
  14083. *((thor_loop_counter_t *)counterRow) = counter;
  14084. RtlLinkedDatasetBuilder builder(rowAllocator);
  14085. builder.appendOwn(counterRow);
  14086. Owned<CGraphResult> counterResult = new CGraphResult(builder.getcount(), builder.linkrows());
  14087. graph->setInputResult(0, counterResult);
  14088. }
  14089. }
  14090. };
  14091. //=================================================================================
  14092. class CRoxieServerSequentialGraphLoopActivity : public CRoxieServerGraphLoopActivity
  14093. {
  14094. Owned<IActivityGraph> GraphQuery;
  14095. Owned<IRoxieServerChildGraph> loopGraph;
  14096. Linked<IEngineRowStream> resultStream;
  14097. bool evaluated;
  14098. public:
  14099. CRoxieServerSequentialGraphLoopActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _GraphGraphId, IOutputMetaData * _counterMeta)
  14100. : CRoxieServerGraphLoopActivity(_ctx, _factory, _probeManager, _GraphGraphId, _counterMeta)
  14101. {
  14102. evaluated = false;
  14103. }
  14104. virtual void onCreate(IHThorArg *_colocalParent)
  14105. {
  14106. CRoxieServerGraphLoopActivity::onCreate(_colocalParent);
  14107. GraphQuery.set(ctx->queryChildGraph(loopGraphId));
  14108. }
  14109. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14110. {
  14111. CRoxieServerGraphLoopActivity::start(parentExtractSize, parentExtract, paused);
  14112. //MORE: Not sure about this, should IRoxieServerChildGraph be combined with IActivityGraph?
  14113. loopGraph.set(GraphQuery->queryLoopGraph());
  14114. evaluated = false;
  14115. }
  14116. virtual void stop()
  14117. {
  14118. if (loopGraph)
  14119. loopGraph->clearGraphLoopResults();
  14120. CRoxieServerGraphLoopActivity::stop();
  14121. }
  14122. virtual const void * nextRow()
  14123. {
  14124. ActivityTimer t(activityStats, timeActivities);
  14125. if (!evaluated)
  14126. {
  14127. executeEntireGraph();
  14128. evaluated = true;
  14129. }
  14130. const void * ret = resultStream->nextRow();
  14131. if (ret)
  14132. processed++;
  14133. return ret;
  14134. }
  14135. void executeIteration(unsigned parentExtractSize, const byte *parentExtract, unsigned counter)
  14136. {
  14137. try
  14138. {
  14139. loopGraph->beforeExecute();
  14140. createCounterResult(loopGraph, counter);
  14141. loopGraph->executeGraphLoop(parentExtractSize, parentExtract);
  14142. loopGraph->afterExecute();
  14143. }
  14144. catch (...)
  14145. {
  14146. CTXLOG("Exception thrown in graph body - cleaning up");
  14147. loopGraph->afterExecute();
  14148. throw;
  14149. }
  14150. }
  14151. void createInitialGraphInput()
  14152. {
  14153. loopGraph->clearGraphLoopResults();
  14154. RtlLinkedDatasetBuilder builder(rowAllocator);
  14155. inputStream->readAll(builder);
  14156. Owned<CGraphResult> result = new CGraphResult(builder.getcount(), builder.linkrows());
  14157. loopGraph->setGraphLoopResult(0, result);
  14158. }
  14159. void executeEntireGraph()
  14160. {
  14161. createInitialGraphInput();
  14162. for (unsigned loopCounter=1; loopCounter <= maxIterations; loopCounter++)
  14163. {
  14164. executeIteration(GraphExtractBuilder.size(), GraphExtractBuilder.getbytes(), loopCounter);
  14165. }
  14166. resultStream.setown(loopGraph->getGraphLoopResult(maxIterations));
  14167. }
  14168. };
  14169. //=================================================================================
  14170. struct GraphOutputSplitterArg : public CThorSplitArg
  14171. {
  14172. public:
  14173. virtual unsigned numBranches()
  14174. {
  14175. return 0;
  14176. }
  14177. virtual IOutputMetaData * queryOutputMeta()
  14178. {
  14179. return NULL;// get it from the parent..
  14180. }
  14181. };
  14182. extern "C" IHThorArg * createGraphOutputSplitter() { return new GraphOutputSplitterArg; }
  14183. class CGraphIterationInfo : public CInterface
  14184. {
  14185. private:
  14186. Owned<IRoxieServerActivityFactory> factory; // Note - before sourceAct, so destroyed last
  14187. unsigned sourceIdx;
  14188. Linked<IRoxieServerActivity> sourceAct;
  14189. Linked<IFinalRoxieInput> sourceInput;
  14190. unsigned numUses;
  14191. unsigned iteration;
  14192. public:
  14193. CGraphIterationInfo(IRoxieServerActivity * _sourceAct, IFinalRoxieInput *_input, unsigned _sourceIdx, unsigned _iteration)
  14194. : sourceIdx(_sourceIdx), sourceAct(_sourceAct), sourceInput(_input), iteration(_iteration)
  14195. {
  14196. numUses = 0;
  14197. }
  14198. inline void noteUsed()
  14199. {
  14200. numUses++;
  14201. }
  14202. void createSplitter(IRoxieAgentContext *ctx, IProbeManager *probeManager, IArrayOf<IRoxieProbe> &probes)
  14203. {
  14204. if (numUses > 1)
  14205. {
  14206. Owned<IPropertyTree> splitterNode = createPTree(ipt_fast);
  14207. factory.setown(createRoxieServerThroughSpillActivityFactory(sourceAct->queryFactory()->queryQueryFactory(), createGraphOutputSplitter, numUses, *splitterNode));
  14208. IRoxieServerActivity *splitter = factory->createActivity(ctx, NULL);
  14209. splitter->onCreate(NULL);
  14210. IFinalRoxieInput *input = sourceAct->queryOutput(sourceIdx);
  14211. if (probeManager)
  14212. {
  14213. IRoxieProbe * inputProbe = probeManager->createProbe(static_cast<IInputBase*>(input), sourceAct, splitter, sourceIdx, 0, iteration);
  14214. probes.append(*LINK(inputProbe));
  14215. input = &dynamic_cast<IFinalRoxieInput &>(inputProbe->queryInput());
  14216. sourceIdx = 0;
  14217. }
  14218. sourceAct.setown(splitter);
  14219. sourceAct->setInput(0, sourceIdx, input);
  14220. sourceIdx = 0;
  14221. sourceInput.clear();
  14222. }
  14223. }
  14224. IFinalRoxieInput *connectOutput(IProbeManager *probeManager, IArrayOf<IRoxieProbe> &probes, IRoxieServerActivity *targetAct, unsigned targetIdx)
  14225. {
  14226. // MORE - not really necessary to create splitters in separate pass, is it?
  14227. if (factory) // we created a splitter....
  14228. sourceInput.set(sourceAct->queryOutput(sourceIdx));
  14229. IFinalRoxieInput *ret = sourceInput;
  14230. if (probeManager)
  14231. {
  14232. IRoxieProbe * inputProbe = probeManager->createProbe(sourceInput, sourceAct, targetAct, sourceIdx, targetIdx, iteration);
  14233. probes.append(*LINK(inputProbe));
  14234. ret = &dynamic_cast<IFinalRoxieInput &>(inputProbe->queryInput());
  14235. // MORE - do we need to connect the stream in the probe?
  14236. }
  14237. if (factory) // we created a splitter....
  14238. sourceIdx++;
  14239. return ret;
  14240. }
  14241. };
  14242. class CRoxieServerParallelGraphLoopActivity : public CRoxieServerGraphLoopActivity, implements IRoxieServerLoopResultProcessor
  14243. {
  14244. Owned<IActivityGraph> childGraph;
  14245. IFinalRoxieInput * resultInput;
  14246. IEngineRowStream * resultStream;
  14247. Owned<IStrandJunction> resultJunction;
  14248. CIArrayOf<CGraphIterationInfo> outputs;
  14249. IArrayOf<IRoxieServerChildGraph> iterationGraphs;
  14250. Owned<CExtractMapperInput> inputExtractMapper;
  14251. IProbeManager *probeManager;
  14252. Owned<IStatisticGatherer> childStats;
  14253. unsigned createLoopCounter;
  14254. IArrayOf<IRoxieProbe> probes;
  14255. public:
  14256. CRoxieServerParallelGraphLoopActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _GraphGraphId, IOutputMetaData * _counterMeta)
  14257. : CRoxieServerGraphLoopActivity(_ctx, _factory, _probeManager, _GraphGraphId, _counterMeta), probeManager(_probeManager)
  14258. {
  14259. inputExtractMapper.setown(new CExtractMapperInput);
  14260. resultInput = NULL;
  14261. resultStream = NULL;
  14262. createLoopCounter = 0;
  14263. if (ctx && ctx->collectingDetailedStatistics())
  14264. {
  14265. StatsScopeId rootScope(SSTnone, 0U);
  14266. childStats.setown(createStatisticsGatherer(SCTroxie, "", rootScope));
  14267. }
  14268. }
  14269. virtual void onCreate(IHThorArg *_colocalParent)
  14270. {
  14271. CRoxieServerGraphLoopActivity::onCreate(_colocalParent);
  14272. childGraph.set(ctx->queryChildGraph(loopGraphId));
  14273. }
  14274. virtual void gatherStatistics(IStatisticGatherer * statsBuilder) const override
  14275. {
  14276. CRoxieServerGraphLoopActivity::gatherStatistics(statsBuilder);
  14277. if (statsBuilder && childStats)
  14278. {
  14279. //Merge any stats collected from executed graphs into the full statistics
  14280. Owned<IStatisticCollection> childCollection = childStats->getResult();
  14281. childCollection->mergeInto(*statsBuilder);
  14282. }
  14283. }
  14284. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  14285. {
  14286. //Input needs to be handled very carefully.....
  14287. //We don't want to call onStart on the input unless it is actually used, so don't use the base CRoxieServerActivity implementation.
  14288. //This activity's input needs to be started with (parentExtractSize, parentExtract), but the elements in the graph need to be started with the
  14289. //GraphExtractBuilder parent extract. So we need to wrap the input in a pseudo-input (inputExtractMapper) that passes through a different
  14290. //parentExtract. Something very similar will be needed for query library calls with streaming inputs when they are implemented.
  14291. assertex(idx == 0);
  14292. inputExtractMapper->setInput(_sourceIdx, _in);
  14293. }
  14294. virtual void connectInputStreams(bool consumerOrdered)
  14295. {
  14296. //NB: inputExtractMapper is not connected at this point - only if/when it is used from within the graph
  14297. CRoxieServerActivity::connectInputStreams(consumerOrdered);
  14298. }
  14299. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  14300. {
  14301. if (idx==0)
  14302. return inputExtractMapper->queryInput();
  14303. else
  14304. return NULL;
  14305. }
  14306. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14307. {
  14308. CRoxieServerGraphLoopActivity::start(parentExtractSize, parentExtract, paused); // initialises GraphExtractBuilder
  14309. inputExtractMapper->setParentExtract(parentExtractSize, parentExtract);
  14310. createExpandedGraph(GraphExtractBuilder.size(), GraphExtractBuilder.getbytes(), probeManager);
  14311. resultInput->start(GraphExtractBuilder.size(), GraphExtractBuilder.getbytes(), paused);
  14312. startJunction(resultJunction);
  14313. }
  14314. virtual void stop()
  14315. {
  14316. if (resultStream)
  14317. resultStream->stop();
  14318. CRoxieServerGraphLoopActivity::stop();
  14319. }
  14320. virtual void reset()
  14321. {
  14322. if (resultInput)
  14323. resultInput->reset();
  14324. resetJunction(resultJunction);
  14325. resultInput = NULL;
  14326. resultStream = NULL;
  14327. resultJunction.clear();
  14328. ForEachItemIn(i, iterationGraphs)
  14329. iterationGraphs.item(i).gatherStatistics(childStats);
  14330. outputs.kill();
  14331. iterationGraphs.kill(); // must be done after all activities killed
  14332. if (probeManager)
  14333. {
  14334. probeManager->deleteGraph(NULL, (IArrayOf<IInputBase>*)&probes);
  14335. probes.kill();
  14336. }
  14337. CRoxieServerGraphLoopActivity::reset();
  14338. }
  14339. virtual const void * nextRow()
  14340. {
  14341. ActivityTimer t(activityStats, timeActivities);
  14342. const void * ret = resultStream->nextRow();
  14343. if (ret)
  14344. processed++;
  14345. return ret;
  14346. }
  14347. void createExpandedGraph(unsigned parentExtractSize, const byte *parentExtract, IProbeManager *probeManager)
  14348. {
  14349. //result(0) is the input to the graph.
  14350. resultInput = inputExtractMapper;
  14351. outputs.append(* new CGraphIterationInfo(resultInput->queryActivity(), resultInput, 0, 1));
  14352. for (createLoopCounter=1; createLoopCounter <= maxIterations; createLoopCounter++)
  14353. {
  14354. IRoxieServerChildGraph * graph = childGraph->createGraphLoopInstance(ctx, createLoopCounter, parentExtractSize, parentExtract, *this);
  14355. graph->beforeExecute();
  14356. iterationGraphs.append(*graph);
  14357. graph->gatherIterationUsage(*this);
  14358. CGraphIterationInfo *iteration = graph->selectGraphLoopOutput();
  14359. outputs.append(*iteration);
  14360. }
  14361. createLoopCounter = 0;
  14362. createSplitters(probeManager, probes);
  14363. ForEachItemIn(i2, iterationGraphs)
  14364. iterationGraphs.item(i2).associateIterationOutputs(*this);
  14365. resultInput = outputs.tos().connectOutput(probeManager, probes, this, 0);
  14366. resultStream = connectSingleStream(ctx, resultInput, 0, resultJunction, true);
  14367. }
  14368. void createSplitters(IProbeManager *probeManager, IArrayOf<IRoxieProbe> &probes)
  14369. {
  14370. ForEachItemIn(i, outputs)
  14371. {
  14372. CGraphIterationInfo & next = outputs.item(i);
  14373. next.createSplitter(ctx, probeManager, probes);
  14374. }
  14375. }
  14376. //IRoxieServerLoopResultProcessor
  14377. virtual void noteUseIteration(unsigned _whichIteration)
  14378. {
  14379. int whichIteration = (int) _whichIteration; // May go negative - API is unsigned for historical reasons
  14380. if (whichIteration >= 0)
  14381. {
  14382. if (!outputs.isItem(whichIteration))
  14383. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Error reading graph result %d from iteration %d", whichIteration, createLoopCounter);
  14384. outputs.item(whichIteration).noteUsed();
  14385. }
  14386. }
  14387. virtual IFinalRoxieInput* connectIterationOutput(unsigned whichIteration, IProbeManager *probeManager, IArrayOf<IRoxieProbe> &probes, IRoxieServerActivity *targetAct, unsigned targetIdx)
  14388. {
  14389. if (outputs.isItem(whichIteration))
  14390. {
  14391. CGraphIterationInfo & next = outputs.item(whichIteration);
  14392. return next.connectOutput(probeManager, probes, targetAct, targetIdx);
  14393. }
  14394. return NULL;
  14395. }
  14396. };
  14397. //=================================================================================
  14398. class CRoxieServerGraphLoopActivityFactory : public CRoxieServerActivityFactory
  14399. {
  14400. unsigned loopGraphId;
  14401. unsigned flags;
  14402. Linked<IOutputMetaData> counterMeta;
  14403. public:
  14404. CRoxieServerGraphLoopActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _loopGraphId)
  14405. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), loopGraphId(_loopGraphId)
  14406. {
  14407. Owned<IHThorGraphLoopArg> helper = (IHThorGraphLoopArg *) helperFactory();
  14408. flags = helper->getFlags();
  14409. counterMeta.setown(new CCounterRowMetaData);
  14410. }
  14411. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  14412. {
  14413. if (kind == TAKparallelgraphloop)
  14414. return new CRoxieServerParallelGraphLoopActivity(_ctx, this, _probeManager, loopGraphId, counterMeta);
  14415. else
  14416. return new CRoxieServerSequentialGraphLoopActivity(_ctx, this, _probeManager, loopGraphId, counterMeta);
  14417. }
  14418. };
  14419. IRoxieServerActivityFactory *createRoxieServerGraphLoopActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _loopGraphId)
  14420. {
  14421. return new CRoxieServerGraphLoopActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _loopGraphId);
  14422. }
  14423. //=====================================================================================================
  14424. class CRoxieServerLibraryCallActivity : public CRoxieServerActivity
  14425. {
  14426. class OutputAdaptor : public CExtractMapperInput
  14427. {
  14428. bool stopped;
  14429. public:
  14430. CRoxieServerLibraryCallActivity *parent;
  14431. IOutputMetaData * meta = nullptr;
  14432. unsigned oid;
  14433. unsigned processed;
  14434. unsigned numStarts = 0;
  14435. public:
  14436. OutputAdaptor()
  14437. {
  14438. parent = NULL;
  14439. oid = 0;
  14440. init();
  14441. }
  14442. ~OutputAdaptor()
  14443. {
  14444. if (traceStartStop)
  14445. DBGLOG("%p ~OutputAdaptor %d", this, oid);
  14446. }
  14447. void setParent(CRoxieServerLibraryCallActivity * _parent, IOutputMetaData * _meta, unsigned _oid)
  14448. {
  14449. parent = _parent;
  14450. meta = _meta;
  14451. oid = _oid;
  14452. }
  14453. void init()
  14454. {
  14455. processed = 0;
  14456. stopped = false;
  14457. }
  14458. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14459. {
  14460. parent->start(oid, parentExtractSize, parentExtract, paused);
  14461. CExtractMapperInput::start(parentExtractSize, parentExtract, paused);
  14462. numStarts++;
  14463. }
  14464. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks)
  14465. {
  14466. //MORE value of consumerOrdered depends on whether this activity is marked as ordered.
  14467. if (!oid) // Only need to set up once
  14468. {
  14469. parent->connectInputStreams(true); // To handle dependencies etc
  14470. }
  14471. return CExtractMapperInput::getOutputStreams(ctx, 0, streams, nullptr, true, nullptr);
  14472. }
  14473. //This override should not really be needed, but input is set up too late for the call to connect strands
  14474. virtual IOutputMetaData * queryOutputMeta() const
  14475. {
  14476. return meta;
  14477. }
  14478. virtual void stop()
  14479. {
  14480. if (!stopped)
  14481. {
  14482. stopped = true;
  14483. parent->stop(oid); // parent code relies on stop being called exactly once per adaptor, so make sure it is!
  14484. CExtractMapperInput::stop();
  14485. }
  14486. };
  14487. virtual void reset()
  14488. {
  14489. parent->reset(oid);
  14490. CExtractMapperInput::reset();
  14491. init();
  14492. };
  14493. virtual void checkAbort()
  14494. {
  14495. parent->checkAbort();
  14496. }
  14497. };
  14498. IHThorLibraryCallArg &helper;
  14499. unsigned numInputs;
  14500. unsigned numOutputs;
  14501. unsigned numActiveOutputs;
  14502. bool started;
  14503. OutputAdaptor* outputAdaptors;
  14504. CExtractMapperInput * * inputAdaptors;
  14505. bool * inputUsed;
  14506. bool * outputUsed;
  14507. Owned<IException> error;
  14508. CriticalSection crit;
  14509. rtlRowBuilder libraryExtractBuilder;
  14510. Owned<IActivityGraph> libraryGraph;
  14511. const LibraryCallFactoryExtra & extra;
  14512. public:
  14513. CRoxieServerLibraryCallActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs, unsigned _numOutputs, const LibraryCallFactoryExtra & _extra)
  14514. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  14515. helper((IHThorLibraryCallArg &)basehelper), extra(_extra)
  14516. {
  14517. numInputs = _numInputs;
  14518. numOutputs = _numOutputs;
  14519. numActiveOutputs = numOutputs;
  14520. inputAdaptors = new CExtractMapperInput*[numInputs];
  14521. inputUsed = new bool[numInputs];
  14522. for (unsigned i1 = 0; i1 < numInputs; i1++)
  14523. {
  14524. inputAdaptors[i1] = new CExtractMapperInput;
  14525. inputUsed[i1] = false;
  14526. }
  14527. outputAdaptors = new OutputAdaptor[numOutputs];
  14528. outputUsed = new bool[numOutputs];
  14529. for (unsigned i2 = 0; i2 < numOutputs; i2++)
  14530. {
  14531. unsigned actualOutput = extra.outputs.item(i2);
  14532. outputAdaptors[i2].setParent(this, helper.queryOutputMeta(actualOutput), i2);
  14533. outputUsed[i2] = false;
  14534. }
  14535. started = false;
  14536. }
  14537. ~CRoxieServerLibraryCallActivity()
  14538. {
  14539. for (unsigned i1 = 0; i1 < numInputs; i1++)
  14540. ::Release(inputAdaptors[i1]);
  14541. delete [] inputAdaptors;
  14542. delete [] inputUsed;
  14543. delete [] outputAdaptors;
  14544. delete [] outputUsed;
  14545. }
  14546. virtual void onCreate(IHThorArg *_colocalParent)
  14547. {
  14548. CRoxieServerActivity::onCreate(_colocalParent);
  14549. libraryGraph.setown(ctx->getLibraryGraph(extra, this));
  14550. libraryGraph->onCreate(_colocalParent);
  14551. //Now map the inputs and outputs to the adapters
  14552. IRoxieServerChildGraph * graph = libraryGraph->queryLoopGraph();
  14553. for (unsigned i1=0; i1<numInputs; i1++)
  14554. inputUsed[i1] = graph->querySetInputResult(i1, 0, inputAdaptors[i1]);
  14555. for (unsigned i2=0; i2<numOutputs; i2++)
  14556. {
  14557. unsigned outputIndex = extra.outputs.item(i2);
  14558. Owned<IFinalRoxieInput> output = graph->selectOutput(numInputs+outputIndex);
  14559. outputAdaptors[i2].setInput(0, output); // MORE - no idea if this 0 is right??
  14560. outputAdaptors[i2].connectInputStreams(ctx, true);
  14561. }
  14562. }
  14563. virtual void start(unsigned oid, unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14564. {
  14565. CriticalBlock b(crit);
  14566. if (error)
  14567. throw error.getLink();
  14568. if (collectFactoryStatistics)
  14569. factory->noteStarted(oid);
  14570. if (!started)
  14571. {
  14572. // even though it is not complete, we don't want to run this again if it fails.
  14573. started = true;
  14574. //see notes on splitter above
  14575. try
  14576. {
  14577. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  14578. }
  14579. catch (IException *E)
  14580. {
  14581. #ifdef TRACE_SPLIT
  14582. CTXLOG("spill %d caught exception in start", activityId);
  14583. #endif
  14584. error.set(E);
  14585. throw;
  14586. }
  14587. catch (...)
  14588. {
  14589. IException *E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught in CRoxieServerLibraryCallActivity::start");
  14590. error.set(E);
  14591. throw E;
  14592. }
  14593. //recreate the parent extract, and use it to reinitialize the graphs...
  14594. libraryExtractBuilder.clear();
  14595. helper.createParentExtract(libraryExtractBuilder);
  14596. // 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
  14597. // some inputs before we see start on others.
  14598. for (unsigned i1 = 0; i1 < numInputs; i1++)
  14599. {
  14600. if (inputUsed[i1])
  14601. inputAdaptors[i1]->setParentExtract(parentExtractSize, parentExtract);
  14602. else
  14603. inputAdaptors[i1]->queryInput()->stopall();
  14604. }
  14605. for (unsigned i2 = 0; i2 < numOutputs; i2++)
  14606. outputAdaptors[i2].setParentExtract(libraryExtractBuilder.size(), libraryExtractBuilder.getbytes());
  14607. //call stop on all the unused inputs.
  14608. IRoxieServerChildGraph * graph = libraryGraph->queryLoopGraph();
  14609. graph->beforeExecute();
  14610. ForEachItemIn(i3, extra.unusedOutputs)
  14611. {
  14612. Owned<IFinalRoxieInput> output = graph->selectOutput(numInputs+extra.unusedOutputs.item(i3));
  14613. output->stopall();
  14614. }
  14615. }
  14616. }
  14617. virtual void stop(unsigned oid)
  14618. {
  14619. CriticalBlock b(crit);
  14620. if (--numActiveOutputs == 0)
  14621. {
  14622. //call stop on all the unused inputs.
  14623. IRoxieServerChildGraph * graph = libraryGraph->queryLoopGraph();
  14624. graph->beforeExecute();
  14625. ForEachItemIn(i3, extra.unusedOutputs)
  14626. {
  14627. Owned<IFinalRoxieInput> output = graph->selectOutput(numInputs+extra.unusedOutputs.item(i3));
  14628. output->stopall();
  14629. }
  14630. CRoxieServerActivity::stop();
  14631. }
  14632. }
  14633. void reset(unsigned oid)
  14634. {
  14635. started = false;
  14636. error.clear();
  14637. numActiveOutputs = numOutputs;
  14638. if (state != STATEreset) // make sure input is only reset once
  14639. {
  14640. CRoxieServerActivity::reset();
  14641. libraryGraph->reset();
  14642. //Call reset on all unused inputs/outputs from the graph - no one else will.
  14643. for (unsigned i1 = 0; i1 < numInputs; i1++)
  14644. {
  14645. if (!inputUsed[i1])
  14646. inputAdaptors[i1]->reset();
  14647. }
  14648. IRoxieServerChildGraph * graph = libraryGraph->queryLoopGraph();
  14649. ForEachItemIn(i3, extra.unusedOutputs)
  14650. {
  14651. Owned<IFinalRoxieInput> output = graph->selectOutput(numInputs+extra.unusedOutputs.item(i3));
  14652. output->reset();
  14653. }
  14654. }
  14655. };
  14656. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  14657. {
  14658. inputAdaptors[idx]->setInput(_sourceIdx, _in);
  14659. }
  14660. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  14661. {
  14662. if (idx < numInputs && inputAdaptors[idx])
  14663. return inputAdaptors[idx]->queryInput();
  14664. else
  14665. return NULL;
  14666. }
  14667. public:
  14668. virtual const void *nextRow()
  14669. {
  14670. throwUnexpected(); // Internal logic error - we are not anybody's input
  14671. }
  14672. virtual IOutputMetaData * queryOutputMeta() const
  14673. {
  14674. throwUnexpected(); // should be called on outputs instead
  14675. }
  14676. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  14677. {
  14678. assertex(idx!=(unsigned)-1);
  14679. assertex(!outputUsed[idx]);
  14680. outputUsed[idx] = true;
  14681. return &outputAdaptors[idx];
  14682. }
  14683. virtual void gatherStatistics(IStatisticGatherer * statsBuilder) const override
  14684. {
  14685. if (libraryGraph)
  14686. libraryGraph->gatherStatistics(statsBuilder);
  14687. CRoxieServerActivity::gatherStatistics(statsBuilder);
  14688. }
  14689. virtual void updateEdgeStats(IStatisticGatherer * statsBuilder) const
  14690. {
  14691. for (unsigned i = 0; i < numOutputs; i++)
  14692. addEdgeStats(statsBuilder, i, outputAdaptors[i].numStarts, outputAdaptors[i].processed, 0);
  14693. }
  14694. };
  14695. void LibraryCallFactoryExtra::set(const LibraryCallFactoryExtra & _other)
  14696. {
  14697. ForEachItemIn(i1, _other.outputs)
  14698. outputs.append(_other.outputs.item(i1));
  14699. ForEachItemIn(i2, _other.unusedOutputs)
  14700. unusedOutputs.append(_other.unusedOutputs.item(i2));
  14701. maxOutputs = _other.maxOutputs;
  14702. graphid = _other.graphid;
  14703. libraryName.set(_other.libraryName);
  14704. embeddedGraphName.set(_other.embeddedGraphName);
  14705. interfaceHash = _other.interfaceHash;
  14706. embedded = _other.embedded;
  14707. }
  14708. void LibraryCallFactoryExtra::calcUnused()
  14709. {
  14710. for (unsigned i=0; i < maxOutputs; i++)
  14711. if (!outputs.contains(i))
  14712. unusedOutputs.append(i);
  14713. }
  14714. class CRoxieServerLibraryCallActivityFactory : public CRoxieServerMultiOutputFactory
  14715. {
  14716. private:
  14717. CRoxieServerMultiInputInfo inputs;
  14718. LibraryCallFactoryExtra extra;
  14719. public:
  14720. CRoxieServerLibraryCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, LibraryCallFactoryExtra & _extra)
  14721. : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  14722. {
  14723. extra.set(_extra);
  14724. extra.calcUnused();
  14725. setNumOutputs(extra.outputs.ordinality());
  14726. // Try to resolve library now so that we can detect interface mismatches early. You could argue it should only be a warning - as it is the query
  14727. // will be suspended and will remain suspended even if a suitable library is later published
  14728. if (!extra.embedded && _graphNode.getPropBool("hint[@name='required']/@value", false))
  14729. {
  14730. StringContextLogger logctx;
  14731. Owned<IQueryFactory> libraryQuery = _queryFactory.lookupLibrary(extra.libraryName, extra.interfaceHash, logctx);
  14732. }
  14733. }
  14734. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  14735. {
  14736. return new CRoxieServerLibraryCallActivity(_ctx, this, _probeManager, numInputs(), numOutputs, extra);
  14737. }
  14738. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  14739. {
  14740. inputs.set(idx, source, sourceidx);
  14741. }
  14742. virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
  14743. {
  14744. return inputs.get(idx, sourceidx);
  14745. }
  14746. virtual unsigned numInputs() const { return inputs.ordinality(); }
  14747. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  14748. {
  14749. addXrefLibraryInfo(reply, extra.libraryName);
  14750. }
  14751. };
  14752. IRoxieServerActivityFactory *createRoxieServerLibraryCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, LibraryCallFactoryExtra & _extra)
  14753. {
  14754. return new CRoxieServerLibraryCallActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _extra);
  14755. }
  14756. //=====================================================================================================
  14757. // CRoxieServerNWayInputActivity is a multi-input, multi-output activity, where the outputs are a subset of the inputs
  14758. // Used to implement RANGE(<rowset-expression>, [set-of-indices])
  14759. class CRoxieServerNWayInputBaseActivity : public CRoxieServerMultiInputBaseActivity
  14760. {
  14761. protected:
  14762. PointerArrayOf<IFinalRoxieInput> selectedInputs;
  14763. PointerArrayOf<IEngineRowStream> selectedStreams;
  14764. PointerArrayOf<IStrandJunction> selectedJunctions;
  14765. public:
  14766. CRoxieServerNWayInputBaseActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  14767. : CRoxieServerMultiInputBaseActivity(_ctx, _factory, _probeManager, _numInputs)
  14768. {
  14769. }
  14770. virtual unsigned __int64 queryLocalCycles() const
  14771. {
  14772. __int64 localCycles = activityStats.totalCycles;
  14773. ForEachItemIn(i, selectedInputs)
  14774. {
  14775. localCycles -= selectedInputs.item(i)->queryTotalCycles();
  14776. }
  14777. if (localCycles < 0)
  14778. localCycles = 0;
  14779. return localCycles;
  14780. }
  14781. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  14782. {
  14783. if (selectedInputs.isItem(idx))
  14784. return selectedInputs.item(idx);
  14785. else
  14786. return NULL;
  14787. }
  14788. virtual void reset()
  14789. {
  14790. ForEachItemIn(i, selectedInputs)
  14791. selectedInputs.item(i)->reset();
  14792. selectedInputs.kill();
  14793. selectedStreams.kill();
  14794. selectedJunctions.kill();
  14795. CRoxieServerMultiInputBaseActivity::reset();
  14796. }
  14797. virtual const void * nextRow()
  14798. {
  14799. throwUnexpected();
  14800. }
  14801. virtual unsigned numConcreteOutputs() const
  14802. {
  14803. return selectedInputs.ordinality();
  14804. }
  14805. virtual IFinalRoxieInput * queryConcreteInput(unsigned idx)
  14806. {
  14807. return queryInput(idx);
  14808. }
  14809. virtual IEngineRowStream *queryConcreteOutputStream(unsigned whichInput)
  14810. {
  14811. if (selectedStreams.isItem(whichInput))
  14812. return selectedStreams.item(whichInput);
  14813. return NULL;
  14814. }
  14815. virtual IStrandJunction *queryConcreteOutputJunction(unsigned idx) const
  14816. {
  14817. if (selectedJunctions.isItem(idx))
  14818. return selectedJunctions.item(idx);
  14819. return NULL;
  14820. }
  14821. };
  14822. class CRoxieServerNWayInputActivity : public CRoxieServerNWayInputBaseActivity
  14823. {
  14824. IHThorNWayInputArg & helper;
  14825. public:
  14826. CRoxieServerNWayInputActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  14827. : CRoxieServerNWayInputBaseActivity(_ctx, _factory, _probeManager, _numInputs), helper((IHThorNWayInputArg &)basehelper)
  14828. {
  14829. }
  14830. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14831. {
  14832. CRoxieServerNWayInputBaseActivity::start(parentExtractSize, parentExtract, paused);
  14833. bool selectionIsAll;
  14834. size32_t selectionLen;
  14835. rtlDataAttr selection;
  14836. helper.getInputSelection(selectionIsAll, selectionLen, selection.refdata());
  14837. selectedInputs.kill();
  14838. selectedStreams.kill();
  14839. selectedJunctions.kill();
  14840. assertex(numInputs==numStreams); // Will need refactoring when that ceases to be true
  14841. if (selectionIsAll)
  14842. {
  14843. for (unsigned i=0; i < numInputs; i++)
  14844. {
  14845. selectedInputs.append(inputArray[i]);
  14846. selectedStreams.append(streamArray[i]); // Assumes 1:1 relationship - is that good?
  14847. selectedJunctions.append(junctionArray[i]);
  14848. }
  14849. }
  14850. else
  14851. {
  14852. const size32_t * selections = (const size32_t *)selection.getdata();
  14853. unsigned max = selectionLen/sizeof(size32_t);
  14854. for (unsigned i = 0; i < max; i++)
  14855. {
  14856. unsigned nextIndex = selections[i];
  14857. //Check there are no duplicates..... Assumes there are a fairly small number of inputs, so n^2 search is ok.
  14858. for (unsigned j=i+1; j < max; j++)
  14859. {
  14860. if (nextIndex == selections[j])
  14861. throw MakeStringException(ROXIE_NWAY_INPUT_ERROR, "Selection list for nway input can not contain duplicates");
  14862. }
  14863. if (nextIndex > numInputs || nextIndex > numStreams)
  14864. throw MakeStringException(ROXIE_NWAY_INPUT_ERROR, "Index %d in RANGE selection list is out of range", nextIndex);
  14865. selectedInputs.append(inputArray[nextIndex-1]);
  14866. selectedStreams.append(streamArray[nextIndex-1]); // Assumes 1:1 relationship - is that good?
  14867. selectedJunctions.append(junctionArray[nextIndex-1]);
  14868. }
  14869. }
  14870. // NB: Whatever pulls this nwayinput activity, starts and stops the selectedInputs and selectedJunctions
  14871. // But the N-Way input itself is now done with processing, and can stop itself/dependencies.
  14872. stop();
  14873. }
  14874. };
  14875. class CRoxieServerNWayInputActivityFactory : public CRoxieServerMultiInputFactory
  14876. {
  14877. public:
  14878. CRoxieServerNWayInputActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  14879. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  14880. {
  14881. }
  14882. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  14883. {
  14884. return new CRoxieServerNWayInputActivity(_ctx, this, _probeManager, numInputs());
  14885. }
  14886. };
  14887. IRoxieServerActivityFactory *createRoxieServerNWayInputActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  14888. {
  14889. return new CRoxieServerNWayInputActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  14890. }
  14891. //=====================================================================================================
  14892. class CRoxieServerNWayGraphLoopResultReadActivity : public CRoxieServerNWayInputBaseActivity
  14893. {
  14894. IHThorNWayGraphLoopResultReadArg & helper;
  14895. IArrayOf<IRoxieServerActivity> resultReaders;
  14896. Owned<IStrandJunction> *resultJunctions;
  14897. unsigned graphId;
  14898. bool grouped;
  14899. bool selectionIsAll;
  14900. size32_t selectionLen;
  14901. rtlDataAttr selection;
  14902. public:
  14903. CRoxieServerNWayGraphLoopResultReadActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
  14904. : CRoxieServerNWayInputBaseActivity(_ctx, _factory, _probeManager, 0), helper((IHThorNWayGraphLoopResultReadArg &)basehelper)
  14905. {
  14906. grouped = helper.isGrouped();
  14907. graphId = _graphId;
  14908. selectionIsAll = false;
  14909. selectionLen = 0;
  14910. resultJunctions = NULL;
  14911. }
  14912. ~CRoxieServerNWayGraphLoopResultReadActivity()
  14913. {
  14914. delete [] resultJunctions;
  14915. }
  14916. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  14917. {
  14918. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  14919. if (selectedInputs.ordinality() == 0)
  14920. {
  14921. initInputSelection();
  14922. unsigned max = selectionLen / sizeof(size32_t);
  14923. const size32_t * selections = (const size32_t *)selection.getdata();
  14924. IProbeManager * probeManager = NULL; // MORE!!
  14925. for (unsigned i = 0; i < max; i++)
  14926. {
  14927. CRoxieServerActivity * resultInput = new CRoxieServerInternalGraphLoopResultReadActivity(ctx, factory, probeManager, graphId, selections[i]);
  14928. resultReaders.append(*resultInput);
  14929. selectedInputs.append(resultInput);
  14930. resultInput->onCreate(colocalParent);
  14931. resultInput->start(parentExtractSize, parentExtract, paused);
  14932. selectedStreams.append(connectSingleStream(ctx, resultInput, 0, resultJunctions[i], true));
  14933. selectedJunctions.append(resultJunctions[i]);
  14934. }
  14935. }
  14936. // I have done with my processing - note that this won't stop my inputs
  14937. stop();
  14938. }
  14939. virtual void reset()
  14940. {
  14941. resultReaders.kill();
  14942. CRoxieServerNWayInputBaseActivity::reset();
  14943. ForEachItemIn(i, selectedStreams)
  14944. resetJunction(resultJunctions[i]);
  14945. }
  14946. virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract)
  14947. {
  14948. ensureCreated();
  14949. basehelper.onStart(parentExtract, NULL);
  14950. initInputSelection();
  14951. unsigned max = selectionLen / sizeof(size32_t);
  14952. const size32_t * selections = (const size32_t *)selection.getdata();
  14953. for (unsigned i = 0; i < max; i++)
  14954. processor.noteUseIteration(selections[i]);
  14955. }
  14956. virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract, IProbeManager *probeManager, IArrayOf<IRoxieProbe> &probes)
  14957. {
  14958. //selection etc. already initialised from the gatherIterationUsage() call.
  14959. unsigned max = selectionLen / sizeof(size32_t);
  14960. const size32_t * selections = (const size32_t *)selection.getdata();
  14961. resultJunctions = new Owned<IStrandJunction> [max];
  14962. for (unsigned i = 0; i < max; i++)
  14963. {
  14964. IFinalRoxieInput *in = processor.connectIterationOutput(selections[i], probeManager, probes, this, i);
  14965. selectedInputs.append(in);
  14966. selectedStreams.append(connectSingleStream(ctx, in, 0, resultJunctions[i], true));
  14967. selectedJunctions.append(resultJunctions[i]);
  14968. }
  14969. }
  14970. protected:
  14971. void initInputSelection()
  14972. {
  14973. helper.getInputSelection(selectionIsAll, selectionLen, selection.refdata());
  14974. if (selectionIsAll)
  14975. throw MakeStringException(ROXIE_NWAY_INPUT_ERROR, "ALL not yet supported for NWay graph inputs");
  14976. }
  14977. };
  14978. class CRoxieServerNWayGraphLoopResultReadActivityFactory : public CRoxieServerActivityFactory
  14979. {
  14980. unsigned graphId;
  14981. public:
  14982. CRoxieServerNWayGraphLoopResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _graphId)
  14983. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), graphId(_graphId)
  14984. {
  14985. }
  14986. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  14987. {
  14988. return new CRoxieServerNWayGraphLoopResultReadActivity(_ctx, this, _probeManager, graphId);
  14989. }
  14990. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  14991. {
  14992. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for NWay GraphLoopResultRead activity");
  14993. }
  14994. };
  14995. IRoxieServerActivityFactory *createRoxieServerNWayGraphLoopResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned graphId)
  14996. {
  14997. return new CRoxieServerNWayGraphLoopResultReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, graphId);
  14998. }
  14999. //=================================================================================
  15000. class RoxieSteppedInput : implements ISteppedInput, public CInterface
  15001. {
  15002. public:
  15003. RoxieSteppedInput(IFinalRoxieInput * _input, IEngineRowStream *_stream) { input = _input; inputStream = _stream; }
  15004. IMPLEMENT_IINTERFACE
  15005. protected:
  15006. virtual const void * nextInputRow()
  15007. {
  15008. #ifdef TRACE_SEEK_REQUESTS
  15009. IRoxieContextLogger * logger = input->queryActivity();
  15010. const void * ret = doNextInputRow();
  15011. {
  15012. CommonXmlWriter xmlwrite(XWFtrim|XWFopt|XWFnoindent);
  15013. if (!ret)
  15014. xmlwrite.outputBool(true,"eof");
  15015. else if (input->queryOutputMeta()->hasXML())
  15016. input->queryOutputMeta()->toXML((byte *) ret, xmlwrite);
  15017. logger->CTXLOG("next() returns (%s)", xmlwrite.str());
  15018. }
  15019. return ret;
  15020. #else
  15021. return inputStream->ungroupedNextRow();
  15022. #endif
  15023. }
  15024. virtual const void * nextInputRowGE(const void * seek, unsigned numFields, bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
  15025. {
  15026. #ifdef TRACE_SEEK_REQUESTS
  15027. IRoxieContextLogger * logger = input->queryActivity();
  15028. {
  15029. CommonXmlWriter xmlwrite(XWFtrim|XWFopt|XWFnoindent);
  15030. if (input->queryOutputMeta()->hasXML())
  15031. input->queryOutputMeta()->toXML((byte *) seek, xmlwrite);
  15032. logger->CTXLOG("nextInputRowGE(%d, %s%s%s, %s) seek(%s)",
  15033. numFields,
  15034. stepExtra.readAheadManyResults() ? "readahead " : "",
  15035. stepExtra.returnMismatches() ? "mismatch" : "exact",
  15036. stepExtra.onlyReturnFirstSeekMatch() ? " single-match" : "",
  15037. stepExtra.queryExtraSeeks() ? "multi-seek":"",
  15038. xmlwrite.str());
  15039. }
  15040. const void * ret = doNextInputRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  15041. {
  15042. CommonXmlWriter xmlwrite(XWFtrim|XWFopt|XWFnoindent);
  15043. if (!ret)
  15044. xmlwrite.outputBool(true,"eof");
  15045. else if (input->queryOutputMeta()->hasXML())
  15046. input->queryOutputMeta()->toXML((byte *) ret, xmlwrite);
  15047. logger->CTXLOG("nextInputRowGE(%d, %s%s%s, %s) result(%s)",
  15048. numFields,
  15049. stepExtra.readAheadManyResults() ? "readahead " : "",
  15050. stepExtra.returnMismatches() ? "mismatch" : "exact",
  15051. stepExtra.onlyReturnFirstSeekMatch() ? " single-match" : "",
  15052. stepExtra.queryExtraSeeks() ? "multi-seek":"",
  15053. xmlwrite.str());
  15054. }
  15055. return ret;
  15056. #else
  15057. return doNextInputRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  15058. #endif
  15059. }
  15060. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  15061. {
  15062. return input->gatherConjunctions(collector);
  15063. }
  15064. virtual void resetEOF()
  15065. {
  15066. inputStream->resetEOF();
  15067. }
  15068. virtual IInputSteppingMeta * queryInputSteppingMeta()
  15069. {
  15070. return input->querySteppingMeta();
  15071. }
  15072. inline const void * doNextInputRowGE(const void * seek, unsigned numFields, bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
  15073. {
  15074. assertex(wasCompleteMatch);
  15075. return inputStream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  15076. }
  15077. protected:
  15078. IFinalRoxieInput * input;
  15079. IEngineRowStream * inputStream;
  15080. };
  15081. //=================================================================================
  15082. class CRoxieServerNWayBaseActivity : public CRoxieServerMultiInputBaseActivity
  15083. {
  15084. public:
  15085. CRoxieServerNWayBaseActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  15086. : CRoxieServerMultiInputBaseActivity(_ctx, _factory, _probeManager, _numInputs)
  15087. {
  15088. }
  15089. void startExpandedInputs(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  15090. {
  15091. ForEachItemIn(ei, expandedInputs)
  15092. expandedInputs.item(ei)->start(parentExtractSize, parentExtract, paused);
  15093. ForEachItemIn(idx, expandedInputs)
  15094. startJunction(expandedJunctions.item(idx));
  15095. }
  15096. virtual void stop()
  15097. {
  15098. ForEachItemIn(i, expandedStreams)
  15099. expandedStreams.item(i)->stop();
  15100. CRoxieServerMultiInputBaseActivity::stop();
  15101. }
  15102. virtual void reset()
  15103. {
  15104. ForEachItemIn(idx, expandedInputs)
  15105. resetJunction(expandedJunctions.item(idx));
  15106. expandedInputs.kill();
  15107. expandedStreams.kill();
  15108. expandedJunctions.kill();
  15109. CRoxieServerMultiInputBaseActivity::reset();
  15110. }
  15111. protected:
  15112. PointerArrayOf<IFinalRoxieInput> expandedInputs;
  15113. PointerArrayOf<IEngineRowStream> expandedStreams;
  15114. PointerArrayOf<IStrandJunction> expandedJunctions;
  15115. };
  15116. //=================================================================================
  15117. class CRoxieServerNaryActivity : public CRoxieServerNWayBaseActivity
  15118. {
  15119. public:
  15120. CRoxieServerNaryActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  15121. : CRoxieServerNWayBaseActivity(_ctx, _factory, _probeManager, _numInputs)
  15122. {
  15123. }
  15124. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  15125. {
  15126. CRoxieServerNWayBaseActivity::start(parentExtractSize, parentExtract, paused);
  15127. for (unsigned i=0; i < numInputs; i++)
  15128. {
  15129. IFinalRoxieInput * cur = inputArray[i];
  15130. CRoxieServerNWayInputBaseActivity *nWayInput = dynamic_cast<CRoxieServerNWayInputBaseActivity *>(cur);
  15131. if (nWayInput)
  15132. {
  15133. nWayInput->start(parentExtractSize, parentExtract, paused);
  15134. unsigned numRealInputs = cur->numConcreteOutputs();
  15135. for (unsigned j = 0; j < numRealInputs; j++)
  15136. {
  15137. expandedInputs.append(cur->queryConcreteInput(j));
  15138. expandedStreams.append(cur->queryConcreteOutputStream(j));
  15139. expandedJunctions.append(cur->queryConcreteOutputJunction(j));
  15140. }
  15141. }
  15142. else
  15143. {
  15144. expandedInputs.append(cur);
  15145. // NB: this activities input streams + junction have been setup and held in CRoxieServerMultiInputActivity base
  15146. expandedStreams.append(queryConcreteOutputStream(i));
  15147. expandedJunctions.append(queryConcreteOutputJunction(i));
  15148. }
  15149. }
  15150. startExpandedInputs(parentExtractSize, parentExtract, paused);
  15151. }
  15152. };
  15153. //=================================================================================
  15154. class CRoxieStreamMerger : public CStreamMerger
  15155. {
  15156. public:
  15157. CRoxieStreamMerger() : CStreamMerger(true)
  15158. {
  15159. streamArray = NULL;
  15160. }
  15161. void initInputs(unsigned _numInputs, IEngineRowStream ** _streamArray)
  15162. {
  15163. CStreamMerger::initInputs(_numInputs);
  15164. streamArray = _streamArray;
  15165. }
  15166. virtual bool pullInput(unsigned i, const void * seek, unsigned numFields, const SmartStepExtra * stepExtra)
  15167. {
  15168. const void * next;
  15169. bool matches = true;
  15170. if (seek)
  15171. next = streamArray[i]->nextRowGE(seek, numFields, matches, *stepExtra);
  15172. else
  15173. next = streamArray[i]->ungroupedNextRow();
  15174. pending[i] = next;
  15175. pendingMatches[i] = matches;
  15176. return (next != NULL);
  15177. }
  15178. virtual void releaseRow(const void * row)
  15179. {
  15180. ReleaseRoxieRow(row);
  15181. }
  15182. protected:
  15183. IEngineRowStream **streamArray;
  15184. };
  15185. class CRoxieServerNWayMergeActivity : public CRoxieServerNaryActivity
  15186. {
  15187. public:
  15188. CRoxieServerNWayMergeActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  15189. : CRoxieServerNaryActivity(_ctx, _factory, _probeManager, _numInputs),
  15190. helper((IHThorNWayMergeArg &)basehelper)
  15191. {
  15192. initializedMeta = false;
  15193. }
  15194. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  15195. {
  15196. CRoxieServerNaryActivity::start(parentExtractSize, parentExtract, paused);
  15197. merger.init(helper.queryCompare(), helper.dedup(), helper.querySteppingMeta()->queryCompare());
  15198. merger.initInputs(expandedStreams.length(), expandedStreams.getArray());
  15199. }
  15200. virtual void stop()
  15201. {
  15202. merger.done();
  15203. CRoxieServerNaryActivity::stop();
  15204. }
  15205. virtual void reset()
  15206. {
  15207. merger.cleanup();
  15208. CRoxieServerNaryActivity::reset();
  15209. initializedMeta = false;
  15210. }
  15211. virtual const void * nextRow()
  15212. {
  15213. ActivityTimer t(activityStats, timeActivities);
  15214. const void * next = merger.nextRow();
  15215. if (next)
  15216. processed++;
  15217. return next;
  15218. }
  15219. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
  15220. {
  15221. ActivityTimer t(activityStats, timeActivities);
  15222. const void * next = merger.nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  15223. if (next)
  15224. processed++;
  15225. return next;
  15226. }
  15227. virtual IInputSteppingMeta * querySteppingMeta()
  15228. {
  15229. if (expandedStreams.ordinality() == 0)
  15230. return NULL;
  15231. if (!initializedMeta)
  15232. {
  15233. meta.init(helper.querySteppingMeta(), false);
  15234. ForEachItemIn(i, expandedStreams)
  15235. {
  15236. if (meta.getNumFields() == 0)
  15237. break;
  15238. IInputSteppingMeta * inputMeta = expandedInputs.item(i)->querySteppingMeta();
  15239. meta.intersect(inputMeta);
  15240. }
  15241. initializedMeta = true;
  15242. }
  15243. if (meta.getNumFields() == 0)
  15244. return NULL;
  15245. return &meta;
  15246. }
  15247. protected:
  15248. IHThorNWayMergeArg &helper;
  15249. CRoxieStreamMerger merger;
  15250. CSteppingMeta meta;
  15251. bool initializedMeta;
  15252. };
  15253. class CRoxieServerNWayMergeActivityFactory : public CRoxieServerMultiInputFactory
  15254. {
  15255. public:
  15256. CRoxieServerNWayMergeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15257. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  15258. {
  15259. }
  15260. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  15261. {
  15262. return new CRoxieServerNWayMergeActivity(_ctx, this, _probeManager, numInputs());
  15263. }
  15264. };
  15265. IRoxieServerActivityFactory *createRoxieServerNWayMergeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15266. {
  15267. return new CRoxieServerNWayMergeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  15268. }
  15269. //=================================================================================
  15270. class CRoxieServerNWayMergeJoinActivity : public CRoxieServerNaryActivity
  15271. {
  15272. public:
  15273. CRoxieServerNWayMergeJoinActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs, CMergeJoinProcessor & _processor)
  15274. : CRoxieServerNaryActivity(_ctx, _factory, _probeManager, _numInputs),
  15275. helper((IHThorNWayMergeJoinArg &)basehelper),
  15276. processor(_processor)
  15277. {
  15278. }
  15279. virtual void onCreate(IHThorArg *_colocalParent)
  15280. {
  15281. CRoxieServerNaryActivity::onCreate(_colocalParent);
  15282. inputAllocator.setown(createRowAllocator(helper.queryInputMeta()));
  15283. outputAllocator.setown(createRowAllocator(helper.queryOutputMeta()));
  15284. }
  15285. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  15286. {
  15287. CRoxieServerNaryActivity::start(parentExtractSize, parentExtract, paused);
  15288. ForEachItemIn(i1, expandedInputs)
  15289. {
  15290. Owned<RoxieSteppedInput> stepInput = new RoxieSteppedInput(expandedInputs.item(i1), expandedStreams.item(i1));
  15291. processor.addInput(stepInput);
  15292. }
  15293. processor.beforeProcessing(inputAllocator, outputAllocator);
  15294. }
  15295. virtual void stop()
  15296. {
  15297. processor.afterProcessing();
  15298. CRoxieServerNaryActivity::stop();
  15299. }
  15300. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  15301. {
  15302. return processor.gatherConjunctions(collector);
  15303. }
  15304. virtual void resetEOF()
  15305. {
  15306. processor.queryResetEOF();
  15307. }
  15308. virtual const void * nextRow()
  15309. {
  15310. ActivityTimer t(activityStats, timeActivities);
  15311. const void * next = processor.nextRow();
  15312. if (next)
  15313. processed++;
  15314. return next;
  15315. }
  15316. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  15317. {
  15318. ActivityTimer t(activityStats, timeActivities);
  15319. const void * next = processor.nextGE(seek, numFields, wasCompleteMatch, stepExtra);
  15320. if (next)
  15321. processed++;
  15322. return next;
  15323. }
  15324. virtual IInputSteppingMeta * querySteppingMeta()
  15325. {
  15326. return processor.queryInputSteppingMeta();
  15327. }
  15328. virtual void gatherStats(CRuntimeStatisticCollection & merged) const override
  15329. {
  15330. CRoxieServerNaryActivity::gatherStats(merged);
  15331. if (inputAllocator)
  15332. inputAllocator->gatherStats(merged);
  15333. if (outputAllocator)
  15334. outputAllocator->gatherStats(merged);
  15335. }
  15336. protected:
  15337. IHThorNWayMergeJoinArg & helper;
  15338. CMergeJoinProcessor & processor;
  15339. Owned<IEngineRowAllocator> inputAllocator;
  15340. Owned<IEngineRowAllocator> outputAllocator;
  15341. };
  15342. class CRoxieServerAndMergeJoinActivity : public CRoxieServerNWayMergeJoinActivity
  15343. {
  15344. public:
  15345. CRoxieServerAndMergeJoinActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  15346. : CRoxieServerNWayMergeJoinActivity(_ctx, _factory, _probeManager, _numInputs, andProcessor), andProcessor(helper)
  15347. {
  15348. }
  15349. protected:
  15350. CAndMergeJoinProcessor andProcessor;
  15351. };
  15352. class CRoxieServerAndLeftMergeJoinActivity : public CRoxieServerNWayMergeJoinActivity
  15353. {
  15354. public:
  15355. CRoxieServerAndLeftMergeJoinActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  15356. : CRoxieServerNWayMergeJoinActivity(_ctx, _factory, _probeManager, _numInputs, andLeftProcessor), andLeftProcessor(helper)
  15357. {
  15358. }
  15359. protected:
  15360. CAndLeftMergeJoinProcessor andLeftProcessor;
  15361. };
  15362. class CRoxieServerMofNMergeJoinActivity : public CRoxieServerNWayMergeJoinActivity
  15363. {
  15364. public:
  15365. CRoxieServerMofNMergeJoinActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  15366. : CRoxieServerNWayMergeJoinActivity(_ctx, _factory, _probeManager, _numInputs, mofnProcessor), mofnProcessor(helper)
  15367. {
  15368. }
  15369. protected:
  15370. CMofNMergeJoinProcessor mofnProcessor;
  15371. };
  15372. class CRoxieServerProximityJoinActivity : public CRoxieServerNWayMergeJoinActivity
  15373. {
  15374. public:
  15375. CRoxieServerProximityJoinActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  15376. : CRoxieServerNWayMergeJoinActivity(_ctx, _factory, _probeManager, _numInputs, proximityProcessor), proximityProcessor(helper)
  15377. {
  15378. }
  15379. protected:
  15380. CProximityJoinProcessor proximityProcessor;
  15381. };
  15382. class CRoxieServerNWayMergeJoinActivityFactory : public CRoxieServerMultiInputFactory
  15383. {
  15384. unsigned flags;
  15385. public:
  15386. CRoxieServerNWayMergeJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15387. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  15388. {
  15389. Owned<IHThorNWayMergeJoinArg> helper = (IHThorNWayMergeJoinArg *) helperFactory();
  15390. flags = helper->getJoinFlags();
  15391. }
  15392. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  15393. {
  15394. if (flags & IHThorNWayMergeJoinArg::MJFhasrange)
  15395. return new CRoxieServerProximityJoinActivity(_ctx, this, _probeManager, numInputs());
  15396. switch (flags & IHThorNWayMergeJoinArg::MJFkindmask)
  15397. {
  15398. case IHThorNWayMergeJoinArg::MJFinner:
  15399. return new CRoxieServerAndMergeJoinActivity(_ctx, this, _probeManager, numInputs());
  15400. case IHThorNWayMergeJoinArg::MJFleftonly:
  15401. case IHThorNWayMergeJoinArg::MJFleftouter:
  15402. return new CRoxieServerAndLeftMergeJoinActivity(_ctx, this, _probeManager, numInputs());
  15403. case IHThorNWayMergeJoinArg::MJFmofn:
  15404. return new CRoxieServerMofNMergeJoinActivity(_ctx, this, _probeManager, numInputs());
  15405. default:
  15406. throwUnexpected();
  15407. }
  15408. }
  15409. };
  15410. IRoxieServerActivityFactory *createRoxieServerNWayMergeJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15411. {
  15412. return new CRoxieServerNWayMergeJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  15413. }
  15414. //=================================================================================
  15415. class CRoxieServerNWaySelectActivity : public CRoxieServerNWayBaseActivity
  15416. {
  15417. IHThorNWaySelectArg &helper;
  15418. public:
  15419. CRoxieServerNWaySelectActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  15420. : CRoxieServerNWayBaseActivity(_ctx, _factory, _probeManager, _numInputs),
  15421. helper((IHThorNWaySelectArg &)basehelper)
  15422. {
  15423. }
  15424. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  15425. {
  15426. CRoxieServerNWayBaseActivity::start(parentExtractSize, parentExtract, paused);
  15427. unsigned whichInput = helper.getInputIndex();
  15428. selectedInput = nullptr;
  15429. selectedStream = nullptr;
  15430. if (whichInput--)
  15431. {
  15432. for (unsigned i=0; i < numInputs; i++)
  15433. {
  15434. IFinalRoxieInput * cur = inputArray[i];
  15435. CRoxieServerNWayInputBaseActivity *nWayInput = dynamic_cast<CRoxieServerNWayInputBaseActivity *>(cur);
  15436. if (nWayInput)
  15437. {
  15438. nWayInput->start(parentExtractSize, parentExtract, paused);
  15439. unsigned numRealInputs = cur->numConcreteOutputs();
  15440. if (whichInput < numRealInputs)
  15441. {
  15442. expandedInputs.append(cur->queryConcreteInput(whichInput));
  15443. expandedStreams.append(cur->queryConcreteOutputStream(whichInput));
  15444. expandedJunctions.append(cur->queryConcreteOutputJunction(whichInput));
  15445. break;
  15446. }
  15447. whichInput -= numRealInputs;
  15448. }
  15449. else
  15450. {
  15451. if (whichInput == 0)
  15452. {
  15453. expandedInputs.append(cur);
  15454. // NB: this activities input streams + junction have been setup and held in CRoxieServerMultiInputActivity base
  15455. expandedStreams.append(queryConcreteOutputStream(i));
  15456. expandedJunctions.append(queryConcreteOutputJunction(i));
  15457. break;
  15458. }
  15459. whichInput -= 1;
  15460. }
  15461. }
  15462. }
  15463. if (expandedInputs.ordinality())
  15464. {
  15465. startExpandedInputs(parentExtractSize, parentExtract, paused);
  15466. selectedInput = expandedInputs.item(0);
  15467. selectedStream = expandedStreams.item(0);
  15468. }
  15469. }
  15470. const void * nextRow()
  15471. {
  15472. ActivityTimer t(activityStats, timeActivities);
  15473. if (!selectedStream)
  15474. return NULL;
  15475. return selectedStream->nextRow();
  15476. }
  15477. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  15478. {
  15479. if (!selectedInput)
  15480. return false;
  15481. return selectedInput->gatherConjunctions(collector);
  15482. }
  15483. virtual void resetEOF()
  15484. {
  15485. if (selectedStream)
  15486. selectedStream->resetEOF();
  15487. }
  15488. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  15489. {
  15490. ActivityTimer t(activityStats, timeActivities);
  15491. if (!selectedStream)
  15492. return NULL;
  15493. return selectedStream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  15494. }
  15495. IInputSteppingMeta * querySteppingMeta()
  15496. {
  15497. if (selectedInput)
  15498. return selectedInput->querySteppingMeta();
  15499. return NULL;
  15500. }
  15501. protected:
  15502. IFinalRoxieInput * selectedInput = nullptr;
  15503. IEngineRowStream * selectedStream = nullptr;
  15504. Owned<IStrandJunction> selectedJunction;
  15505. };
  15506. class CRoxieServerNWaySelectActivityFactory : public CRoxieServerMultiInputFactory
  15507. {
  15508. public:
  15509. CRoxieServerNWaySelectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15510. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  15511. {
  15512. }
  15513. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  15514. {
  15515. return new CRoxieServerNWaySelectActivity(_ctx, this, _probeManager, numInputs());
  15516. }
  15517. };
  15518. IRoxieServerActivityFactory *createRoxieServerNWaySelectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15519. {
  15520. return new CRoxieServerNWaySelectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  15521. }
  15522. //=================================================================================
  15523. class CRoxieServerRemoteActivity : public CRoxieServerActivity, implements IRoxieServerErrorHandler
  15524. {
  15525. protected:
  15526. IHThorRemoteArg &helper;
  15527. CRemoteResultAdaptor remote;
  15528. public:
  15529. CRoxieServerRemoteActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteID)
  15530. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  15531. helper((IHThorRemoteArg &)basehelper),
  15532. remote(_ctx, this, _remoteID, meta.queryOriginal(), helper, *this, false, false) // MORE - if they need it stable we'll have to think!
  15533. {
  15534. }
  15535. virtual const IResolvedFile *queryVarFileInfo() const
  15536. {
  15537. return NULL;
  15538. }
  15539. virtual void onCreate(IHThorArg *_colocalParent)
  15540. {
  15541. CRoxieServerActivity::onCreate(_colocalParent);
  15542. remote.onCreate(_colocalParent);
  15543. }
  15544. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  15545. {
  15546. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  15547. remote.onStart(parentExtractSize, parentExtract);
  15548. remote.setLimits(helper.getRowLimit(), (unsigned __int64) -1, I64C(0x7FFFFFFFFFFFFFFF));
  15549. unsigned fileNo = 0; // MORE - superfiles require us to do this per file part... maybe (needs thought)
  15550. remote.getMem(0, fileNo, 0); // the cached context is all we need to send
  15551. remote.flush();
  15552. remote.senddone();
  15553. }
  15554. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  15555. {
  15556. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  15557. }
  15558. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  15559. {
  15560. if (idx==(unsigned)-1)
  15561. idx = 0;
  15562. return idx ? NULL: &remote;
  15563. }
  15564. virtual void reset()
  15565. {
  15566. processed = remote.processed;
  15567. remote.processed = 0;
  15568. CRoxieServerActivity::reset();
  15569. }
  15570. virtual void onLimitExceeded(bool isKeyed)
  15571. {
  15572. if (traceLevel > 4)
  15573. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  15574. helper.onLimitExceeded();
  15575. }
  15576. virtual const void *createLimitFailRow(bool isKeyed)
  15577. {
  15578. UNIMPLEMENTED; // MORE - is there an ONFAIL for a limit folded into a remote?
  15579. }
  15580. virtual const void *nextRow()
  15581. {
  15582. throwUnexpected(); // I am nobody's input
  15583. }
  15584. virtual void gatherStats(CRuntimeStatisticCollection & merged) const override
  15585. {
  15586. CRoxieServerActivity::gatherStats(merged);
  15587. remote.gatherStats(merged);
  15588. }
  15589. };
  15590. class CRoxieServerRemoteActivityFactory : public CRoxieServerActivityFactory
  15591. {
  15592. public:
  15593. RemoteActivityId remoteId;
  15594. bool isRoot;
  15595. CRoxieServerRemoteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId, bool _isRoot)
  15596. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), remoteId(_remoteId), isRoot(_isRoot)
  15597. {
  15598. }
  15599. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  15600. {
  15601. return new CRoxieServerRemoteActivity(_ctx, this, _probeManager, remoteId);
  15602. }
  15603. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  15604. {
  15605. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for %s activity", getActivityText(kind));
  15606. }
  15607. virtual bool isSink() const
  15608. {
  15609. //I don't think the action version of this is implemented - but this would be the code
  15610. return isRoot && !meta.queryOriginal();
  15611. }
  15612. virtual const StatisticsMapping &queryStatsMapping() const
  15613. {
  15614. return allStatistics; // Child queries...
  15615. }
  15616. };
  15617. IRoxieServerActivityFactory *createRoxieServerRemoteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId, bool _isRoot)
  15618. {
  15619. return new CRoxieServerRemoteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId, _isRoot);
  15620. }
  15621. //=================================================================================
  15622. class CRoxieServerIterateActivity : public CRoxieServerActivity
  15623. {
  15624. IHThorIterateArg &helper;
  15625. OwnedConstRoxieRow defaultRecord;
  15626. OwnedConstRoxieRow left;
  15627. OwnedConstRoxieRow right;
  15628. unsigned counter;
  15629. public:
  15630. CRoxieServerIterateActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  15631. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  15632. helper((IHThorIterateArg &)basehelper)
  15633. {
  15634. counter = 0;
  15635. }
  15636. virtual bool needsAllocator() const { return true; }
  15637. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  15638. {
  15639. counter = 0;
  15640. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  15641. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  15642. size32_t thisSize = helper.createDefault(rowBuilder);
  15643. defaultRecord.setown(rowBuilder.finalizeRowClear(thisSize));
  15644. }
  15645. virtual void reset()
  15646. {
  15647. defaultRecord.clear();
  15648. right.clear();
  15649. left.clear();
  15650. CRoxieServerActivity::reset();
  15651. }
  15652. virtual const void * nextRow()
  15653. {
  15654. ActivityTimer t(activityStats, timeActivities);
  15655. for (;;)
  15656. {
  15657. right.setown(inputStream->nextRow());
  15658. if (!right)
  15659. {
  15660. bool skippedGroup = (left == NULL) && (counter > 0); //we have just skipped entire group, but shouldn't output a double null
  15661. left.clear();
  15662. counter = 0;
  15663. if (skippedGroup) continue;
  15664. return NULL;
  15665. }
  15666. try
  15667. {
  15668. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  15669. unsigned outSize = helper.transform(rowBuilder, left ? left : defaultRecord, right, ++counter);
  15670. if (outSize)
  15671. {
  15672. left.setown(rowBuilder.finalizeRowClear(outSize));
  15673. processed++;
  15674. return left.getLink();
  15675. }
  15676. }
  15677. catch (IException *E)
  15678. {
  15679. throw makeWrappedException(E);
  15680. }
  15681. }
  15682. }
  15683. };
  15684. class CRoxieServerIterateActivityFactory : public CRoxieServerActivityFactory
  15685. {
  15686. public:
  15687. CRoxieServerIterateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15688. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  15689. {
  15690. }
  15691. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  15692. {
  15693. return new CRoxieServerIterateActivity(_ctx, this, _probeManager);
  15694. }
  15695. };
  15696. IRoxieServerActivityFactory *createRoxieServerIterateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15697. {
  15698. return new CRoxieServerIterateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  15699. }
  15700. //=================================================================================
  15701. class CRoxieServerProcessActivity : public CRoxieServerActivity
  15702. {
  15703. IHThorProcessArg &helper;
  15704. OwnedConstRoxieRow curRight;
  15705. OwnedConstRoxieRow initialRight;
  15706. unsigned counter;
  15707. Owned<IEngineRowAllocator> rightRowAllocator;
  15708. public:
  15709. CRoxieServerProcessActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  15710. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorProcessArg &)basehelper)
  15711. {
  15712. counter = 0;
  15713. }
  15714. virtual bool needsAllocator() const { return true; }
  15715. virtual void onCreate(IHThorArg *_colocalParent)
  15716. {
  15717. CRoxieServerActivity::onCreate(_colocalParent);
  15718. rightRowAllocator.setown(createRowAllocator(helper.queryRightRecordSize()));
  15719. }
  15720. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  15721. {
  15722. counter = 0;
  15723. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  15724. RtlDynamicRowBuilder rowBuilder(rightRowAllocator);
  15725. size32_t thisSize = helper.createInitialRight(rowBuilder);
  15726. initialRight.setown(rowBuilder.finalizeRowClear(thisSize));
  15727. curRight.set(initialRight);
  15728. }
  15729. virtual const void * nextRow()
  15730. {
  15731. ActivityTimer t(activityStats, timeActivities);
  15732. try
  15733. {
  15734. for (;;)
  15735. {
  15736. const void * in = inputStream->nextRow();
  15737. if (!in)
  15738. {
  15739. bool eog = (curRight != initialRight); // processed any records?
  15740. counter = 0;
  15741. curRight.set(initialRight);
  15742. if (eog)
  15743. return NULL;
  15744. in = inputStream->nextRow();
  15745. if (!in)
  15746. return NULL;
  15747. }
  15748. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  15749. RtlDynamicRowBuilder rightRowBuilder(rightRowAllocator);
  15750. size32_t outSize = helper.transform(rowBuilder, rightRowBuilder, in, curRight, ++counter);
  15751. ReleaseRoxieRow(in);
  15752. if (outSize)
  15753. {
  15754. //MORE: This should be returned...
  15755. size32_t rightSize = rightRowAllocator->queryOutputMeta()->getRecordSize(rightRowBuilder.getSelf());
  15756. curRight.setown(rightRowBuilder.finalizeRowClear(rightSize));
  15757. processed++;
  15758. return rowBuilder.finalizeRowClear(outSize);
  15759. }
  15760. }
  15761. }
  15762. catch (IException *E)
  15763. {
  15764. throw makeWrappedException(E);
  15765. }
  15766. }
  15767. virtual void gatherStats(CRuntimeStatisticCollection & merged) const override
  15768. {
  15769. CRoxieServerActivity::gatherStats(merged);
  15770. if (rightRowAllocator)
  15771. rightRowAllocator->gatherStats(merged);
  15772. }
  15773. };
  15774. class CRoxieServerProcessActivityFactory : public CRoxieServerActivityFactory
  15775. {
  15776. public:
  15777. CRoxieServerProcessActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15778. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  15779. {
  15780. }
  15781. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  15782. {
  15783. return new CRoxieServerProcessActivity(_ctx, this, _probeManager);
  15784. }
  15785. };
  15786. IRoxieServerActivityFactory *createRoxieServerProcessActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15787. {
  15788. return new CRoxieServerProcessActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  15789. }
  15790. //=================================================================================
  15791. class CRoxieServerGroupActivity : public CRoxieServerActivity
  15792. {
  15793. IHThorGroupArg &helper;
  15794. bool endPending;
  15795. bool eof;
  15796. bool first;
  15797. unsigned numGroups;
  15798. unsigned numGroupMax;
  15799. unsigned numProcessedLastGroup;
  15800. const void *next;
  15801. public:
  15802. CRoxieServerGroupActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  15803. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorGroupArg &)basehelper)
  15804. {
  15805. next = NULL;
  15806. endPending = false;
  15807. eof = false;
  15808. first = true;
  15809. numGroups = 0;
  15810. numGroupMax = 0;
  15811. numProcessedLastGroup = 0;
  15812. }
  15813. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  15814. {
  15815. endPending = false;
  15816. eof = false;
  15817. first = true;
  15818. numGroups = 0;
  15819. numGroupMax = 0;
  15820. numProcessedLastGroup = processed;
  15821. assertex(next == NULL);
  15822. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  15823. }
  15824. virtual void reset()
  15825. {
  15826. noteStatistic(StNumGroups, numGroups);
  15827. noteStatistic(StNumGroupMax, numGroupMax);
  15828. ReleaseClearRoxieRow(next);
  15829. CRoxieServerActivity::reset();
  15830. }
  15831. virtual const void * nextRow()
  15832. {
  15833. ActivityTimer t(activityStats, timeActivities);
  15834. if (first)
  15835. {
  15836. next = inputStream->nextRow();
  15837. first = false;
  15838. }
  15839. if (eof || endPending)
  15840. {
  15841. endPending = false;
  15842. return NULL;
  15843. }
  15844. const void * prev = next;
  15845. next = inputStream->ungroupedNextRow();
  15846. if (next)
  15847. {
  15848. assertex(prev);
  15849. if (!helper.isSameGroup(prev, next))
  15850. {
  15851. noteEndOfGroup();
  15852. endPending = true;
  15853. }
  15854. }
  15855. else
  15856. {
  15857. noteEndOfGroup();
  15858. eof = true;
  15859. }
  15860. if (prev)
  15861. processed++;
  15862. return prev;
  15863. }
  15864. inline void noteEndOfGroup()
  15865. {
  15866. unsigned numThisGroup = processed - numProcessedLastGroup;
  15867. if (numThisGroup == 0)
  15868. return;
  15869. numProcessedLastGroup = processed;
  15870. if (numThisGroup > numGroupMax)
  15871. numGroupMax = numThisGroup;
  15872. numGroups++;
  15873. }
  15874. };
  15875. class CRoxieServerGroupActivityFactory : public CRoxieServerActivityFactory
  15876. {
  15877. public:
  15878. CRoxieServerGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15879. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  15880. {
  15881. }
  15882. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  15883. {
  15884. return new CRoxieServerGroupActivity(_ctx, this, _probeManager);
  15885. }
  15886. virtual const StatisticsMapping &queryStatsMapping() const
  15887. {
  15888. return groupStatistics;
  15889. }
  15890. };
  15891. IRoxieServerActivityFactory *createRoxieServerGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15892. {
  15893. return new CRoxieServerGroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  15894. }
  15895. //=================================================================================
  15896. class CRoxieServerFirstNActivity : public CRoxieServerLateStartActivity
  15897. {
  15898. unsigned __int64 limit;
  15899. unsigned __int64 skip;
  15900. unsigned doneThisGroup;
  15901. IHThorFirstNArg &helper;
  15902. public:
  15903. CRoxieServerFirstNActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  15904. : CRoxieServerLateStartActivity(_ctx, _factory, _probeManager), helper((IHThorFirstNArg &)basehelper)
  15905. {
  15906. doneThisGroup = 0;
  15907. limit = 0;
  15908. skip = 0;
  15909. }
  15910. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  15911. {
  15912. doneThisGroup = 0;
  15913. CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
  15914. limit = helper.getLimit();
  15915. skip = helper.numToSkip();
  15916. lateStart(parentExtractSize, parentExtract, limit > 0);
  15917. if (limit + skip >= limit)
  15918. limit += skip;
  15919. }
  15920. const void * nextRow()
  15921. {
  15922. ActivityTimer t(activityStats, timeActivities);
  15923. if (eof)
  15924. return NULL;
  15925. const void *ret;
  15926. for (;;)
  15927. {
  15928. ret = inputStream->nextRow();
  15929. if (!ret)
  15930. {
  15931. if (meta.isGrouped())
  15932. {
  15933. if (doneThisGroup > skip)
  15934. {
  15935. doneThisGroup = 0;
  15936. return NULL;
  15937. }
  15938. doneThisGroup = 0;
  15939. }
  15940. ret = inputStream->nextRow();
  15941. if (!ret)
  15942. {
  15943. eof = true;
  15944. return NULL;
  15945. }
  15946. }
  15947. doneThisGroup++;
  15948. if (doneThisGroup > skip)
  15949. break;
  15950. ReleaseRoxieRow(ret);
  15951. }
  15952. if (doneThisGroup <= limit)
  15953. {
  15954. processed++;
  15955. return ret;
  15956. }
  15957. ReleaseRoxieRow(ret);
  15958. if (meta.isGrouped())
  15959. {
  15960. while ((ret = inputStream->nextRow()) != NULL)
  15961. ReleaseRoxieRow(ret);
  15962. doneThisGroup = 0;
  15963. }
  15964. else
  15965. eof = true;
  15966. return NULL;
  15967. }
  15968. };
  15969. class CRoxieServerFirstNActivityFactory : public CRoxieServerActivityFactory
  15970. {
  15971. public:
  15972. CRoxieServerFirstNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15973. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  15974. {
  15975. }
  15976. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  15977. {
  15978. return new CRoxieServerFirstNActivity(_ctx, this, _probeManager);
  15979. }
  15980. };
  15981. IRoxieServerActivityFactory *createRoxieServerFirstNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  15982. {
  15983. return new CRoxieServerFirstNActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  15984. }
  15985. //=================================================================================
  15986. class CRoxieServerSelectNActivity : public CRoxieServerActivity
  15987. {
  15988. bool done;
  15989. IHThorSelectNArg &helper;
  15990. public:
  15991. CRoxieServerSelectNActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  15992. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorSelectNArg &)basehelper)
  15993. {
  15994. done = false;
  15995. }
  15996. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  15997. {
  15998. done = false;
  15999. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  16000. }
  16001. const void *defaultRow()
  16002. {
  16003. if (!rowAllocator)
  16004. ensureRowAllocator(); // We delay as often not needed...
  16005. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  16006. size32_t thisSize = helper.createDefault(rowBuilder);
  16007. return rowBuilder.finalizeRowClear(thisSize);
  16008. }
  16009. virtual const void * nextRow()
  16010. {
  16011. ActivityTimer t(activityStats, timeActivities);
  16012. if (done)
  16013. return NULL;
  16014. done = true;
  16015. processed++; // always going to return a row!
  16016. unsigned __int64 index = helper.getRowToSelect();
  16017. while (--index)
  16018. {
  16019. const void * next = inputStream->ungroupedNextRow();
  16020. if (!next)
  16021. return defaultRow();
  16022. ReleaseRoxieRow(next);
  16023. }
  16024. const void * next = inputStream->ungroupedNextRow();
  16025. if (!next)
  16026. next = defaultRow();
  16027. return next;
  16028. }
  16029. };
  16030. class CRoxieServerSelectNActivityFactory : public CRoxieServerActivityFactory
  16031. {
  16032. public:
  16033. CRoxieServerSelectNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  16034. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  16035. {
  16036. }
  16037. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  16038. {
  16039. return new CRoxieServerSelectNActivity(_ctx, this, _probeManager);
  16040. }
  16041. };
  16042. IRoxieServerActivityFactory *createRoxieServerSelectNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  16043. {
  16044. return new CRoxieServerSelectNActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  16045. }
  16046. //=================================================================================
  16047. class CRoxieServerSelfJoinActivity : public CRoxieServerActivity
  16048. {
  16049. IHThorJoinArg &helper;
  16050. ICompare *collate;
  16051. OwnedRowArray group;
  16052. bool matchedLeft;
  16053. BoolArray matchedRight;
  16054. bool eof;
  16055. bool first;
  16056. unsigned leftIndex;
  16057. unsigned rightIndex;
  16058. unsigned rightOuterIndex;
  16059. unsigned joinLimit;
  16060. unsigned atmostLimit;
  16061. unsigned atmostsTriggered;
  16062. unsigned abortLimit;
  16063. unsigned keepLimit;
  16064. unsigned joinCounter;
  16065. bool leftOuterJoin;
  16066. bool rightOuterJoin;
  16067. bool exclude;
  16068. bool limitFail;
  16069. bool limitOnFail;
  16070. bool cloneLeft;
  16071. bool forceSpill;
  16072. OwnedConstRoxieRow defaultLeft;
  16073. OwnedConstRoxieRow defaultRight;
  16074. OwnedConstRoxieRow lhs;
  16075. Owned<IException> failingLimit;
  16076. bool failingOuterAtmost;
  16077. Owned<IEngineRowAllocator> defaultAllocator;
  16078. Owned<IRHLimitedCompareHelper> limitedhelper;
  16079. Owned<CRHDualCache> dualcache;
  16080. Owned<IGroupedInput> groupedInput;
  16081. IRowStream *dualCacheInput;
  16082. bool fillGroup()
  16083. {
  16084. group.clear();
  16085. matchedLeft = false;
  16086. matchedRight.kill();
  16087. failingOuterAtmost = false;
  16088. const void * next;
  16089. unsigned groupCount = 0;
  16090. while((next = groupedInput->nextRow()) != NULL)
  16091. {
  16092. if(groupCount==abortLimit)
  16093. {
  16094. if(limitFail)
  16095. failLimit(next);
  16096. if (ctx->queryDebugContext())
  16097. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  16098. if(limitOnFail)
  16099. {
  16100. assertex(!failingLimit);
  16101. try
  16102. {
  16103. failLimit(next);
  16104. }
  16105. catch(IException * except)
  16106. {
  16107. failingLimit.setown(except);
  16108. }
  16109. assertex(failingLimit != NULL);
  16110. group.append(next);
  16111. groupCount++;
  16112. break;
  16113. }
  16114. group.clear();
  16115. groupCount = 0;
  16116. while(next)
  16117. {
  16118. ReleaseRoxieRow(next);
  16119. next = groupedInput->nextRow();
  16120. }
  16121. }
  16122. else if(groupCount==atmostLimit)
  16123. {
  16124. atmostsTriggered++;
  16125. if(leftOuterJoin)
  16126. {
  16127. group.append(next);
  16128. groupCount++;
  16129. failingOuterAtmost = true;
  16130. break;
  16131. }
  16132. else
  16133. {
  16134. group.clear();
  16135. groupCount = 0;
  16136. while (next)
  16137. {
  16138. ReleaseRoxieRow(next);
  16139. next = groupedInput->nextRow();
  16140. }
  16141. }
  16142. }
  16143. else
  16144. {
  16145. group.append(next);
  16146. groupCount++;
  16147. }
  16148. }
  16149. if(group.ordinality()==0)
  16150. {
  16151. eof = true;
  16152. return false;
  16153. }
  16154. leftIndex = 0;
  16155. rightIndex = 0;
  16156. rightOuterIndex = 0;
  16157. joinCounter = 0;
  16158. joinLimit = keepLimit;
  16159. ForEachItemIn(idx, group)
  16160. matchedRight.append(false);
  16161. return true;
  16162. }
  16163. void failLimit(const void * next)
  16164. {
  16165. helper.onMatchAbortLimitExceeded();
  16166. CommonXmlWriter xmlwrite(XWFtrim|XWFopt );
  16167. if (!ctx->isBlind() && input->queryOutputMeta() && input->queryOutputMeta()->hasXML())
  16168. {
  16169. input->queryOutputMeta()->toXML((byte *) next, xmlwrite);
  16170. }
  16171. throw MakeStringException(ROXIE_TOO_MANY_RESULTS, "More than %d match candidates in self-join %d for row %s", abortLimit, queryId(), xmlwrite.str());
  16172. }
  16173. virtual bool needsAllocator() const { return true; }
  16174. const void *joinRecords(const void * curLeft, const void * curRight, unsigned counter, IException * except, unsigned flags)
  16175. {
  16176. try
  16177. {
  16178. if (cloneLeft && !except)
  16179. {
  16180. LinkRoxieRow(curLeft);
  16181. return curLeft;
  16182. }
  16183. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  16184. size32_t outsize = except ? helper.onFailTransform(rowBuilder, curLeft, curRight, except, flags) : helper.transform(rowBuilder, curLeft, curRight, counter, flags);
  16185. if (outsize)
  16186. return rowBuilder.finalizeRowClear(outsize);
  16187. else
  16188. return NULL;
  16189. }
  16190. catch (IException *E)
  16191. {
  16192. throw makeWrappedException(E);
  16193. }
  16194. }
  16195. void createDefaultLeft()
  16196. {
  16197. if (!defaultLeft)
  16198. {
  16199. if (!defaultAllocator)
  16200. defaultAllocator.setown(createRowAllocator(input->queryOutputMeta()));
  16201. RtlDynamicRowBuilder rowBuilder(defaultAllocator);
  16202. size32_t thisSize = helper.createDefaultLeft(rowBuilder);
  16203. defaultLeft.setown(rowBuilder.finalizeRowClear(thisSize));
  16204. }
  16205. }
  16206. void createDefaultRight()
  16207. {
  16208. if (!defaultRight)
  16209. {
  16210. if (!defaultAllocator)
  16211. defaultAllocator.setown(createRowAllocator(input->queryOutputMeta()));
  16212. RtlDynamicRowBuilder rowBuilder(defaultAllocator);
  16213. size32_t thisSize = helper.createDefaultRight(rowBuilder);
  16214. defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
  16215. }
  16216. }
  16217. public:
  16218. CRoxieServerSelfJoinActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _forceSpill)
  16219. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorJoinArg &)basehelper), forceSpill(_forceSpill)
  16220. {
  16221. collate = helper.queryCompareLeftRight();
  16222. eof = false;
  16223. first = true;
  16224. keepLimit = 0;
  16225. atmostLimit = 0;
  16226. atmostsTriggered = 0;
  16227. unsigned joinFlags = helper.getJoinFlags();
  16228. leftOuterJoin = (joinFlags & JFleftouter) != 0;
  16229. rightOuterJoin = (joinFlags & JFrightouter) != 0;
  16230. cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
  16231. exclude = (joinFlags & JFexclude) != 0;
  16232. abortLimit = 0;
  16233. joinLimit = 0;
  16234. assertex((joinFlags & (JFfirst | JFfirstleft | JFfirstright)) == 0); // no longer supported
  16235. getLimitType(joinFlags, limitFail, limitOnFail);
  16236. if((joinFlags & JFslidingmatch) != 0)
  16237. throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Internal Error: Sliding self join not supported");
  16238. failingOuterAtmost = false;
  16239. matchedLeft = false;
  16240. leftIndex = 0;
  16241. rightIndex = 0;
  16242. rightOuterIndex = 0;
  16243. joinCounter = 0;
  16244. dualCacheInput = NULL;
  16245. }
  16246. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  16247. {
  16248. eof = false;
  16249. first = true;
  16250. failingLimit.clear();
  16251. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  16252. keepLimit = helper.getKeepLimit();
  16253. if(keepLimit == 0)
  16254. keepLimit = (unsigned)-1;
  16255. atmostsTriggered = 0;
  16256. atmostLimit = helper.getJoinLimit();
  16257. if(atmostLimit == 0)
  16258. atmostLimit = (unsigned)-1;
  16259. else
  16260. assertex(!rightOuterJoin);
  16261. abortLimit = helper.getMatchAbortLimit();
  16262. if (abortLimit == 0)
  16263. abortLimit = (unsigned)-1;
  16264. if (rightOuterJoin)
  16265. createDefaultLeft();
  16266. if (leftOuterJoin || limitOnFail)
  16267. createDefaultRight();
  16268. ICompare *compareLeft = helper.queryCompareLeft();
  16269. if (helper.isLeftAlreadySorted())
  16270. groupedInput.setown(createGroupedInputReader(inputStream, compareLeft));
  16271. else
  16272. {
  16273. bool isStable = (helper.getJoinFlags() & JFunstable) == 0;
  16274. RoxieSortAlgorithm sortAlgorithm;
  16275. if (forceSpill)
  16276. sortAlgorithm = isStable ? stableSpillingQuickSortAlgorithm : spillingQuickSortAlgorithm;
  16277. else
  16278. sortAlgorithm = isStable ? stableQuickSortAlgorithm : quickSortAlgorithm;
  16279. groupedInput.setown(createSortedGroupedInputReader(inputStream, compareLeft, createSortAlgorithm(sortAlgorithm, compareLeft, ctx->queryRowManager(), input->queryOutputMeta(), ctx->queryCodeContext(), tempDirectory, activityId)));
  16280. }
  16281. if ((helper.getJoinFlags() & JFlimitedprefixjoin) && helper.getJoinLimit())
  16282. { //limited match join (s[1..n])
  16283. dualcache.setown(new CRHDualCache());
  16284. dualcache->init(groupedInput);
  16285. dualCacheInput = dualcache->queryOut1();
  16286. failingOuterAtmost = false;
  16287. matchedLeft = false;
  16288. leftIndex = 0;
  16289. rightOuterIndex = 0;
  16290. joinCounter = 0;
  16291. limitedhelper.setown(createRHLimitedCompareHelper());
  16292. limitedhelper->init( helper.getJoinLimit(), dualcache->queryOut2(), collate, helper.queryPrefixCompare() );
  16293. }
  16294. }
  16295. virtual void reset()
  16296. {
  16297. if (atmostsTriggered)
  16298. noteStatistic(StNumAtmostTriggered, atmostsTriggered);
  16299. group.clear();
  16300. groupedInput.clear();
  16301. CRoxieServerActivity::reset();
  16302. defaultLeft.clear();
  16303. defaultRight.clear();
  16304. }
  16305. virtual const void * nextRow()
  16306. {
  16307. ActivityTimer t(activityStats, timeActivities);
  16308. if (limitedhelper)
  16309. {
  16310. while(!eof) //limited match join
  16311. {
  16312. if (!group.isItem(rightIndex))
  16313. {
  16314. lhs.setown(dualCacheInput->nextRow());
  16315. if (lhs)
  16316. {
  16317. rightIndex = 0;
  16318. joinCounter = 0;
  16319. group.clear();
  16320. limitedhelper->getGroup(group,lhs);
  16321. }
  16322. else
  16323. {
  16324. eof = true;
  16325. }
  16326. }
  16327. if (group.isItem(rightIndex))
  16328. {
  16329. const void * rhs = group.item(rightIndex++);
  16330. if(helper.match(lhs, rhs))
  16331. {
  16332. const void * ret = joinRecords(lhs, rhs, ++joinCounter, NULL, JTFmatchedleft|JTFmatchedright);
  16333. if(ret)
  16334. {
  16335. processed++;
  16336. return ret;
  16337. }
  16338. }
  16339. }
  16340. }
  16341. return NULL;
  16342. }
  16343. else
  16344. {
  16345. if (first)
  16346. {
  16347. first = false;
  16348. fillGroup();
  16349. }
  16350. while(!eof)
  16351. {
  16352. if(failingOuterAtmost)
  16353. while(group.isItem(leftIndex))
  16354. {
  16355. const void * ret = joinRecords(group.item(leftIndex++), defaultRight, 0, NULL, JTFmatchedleft);
  16356. if(ret)
  16357. {
  16358. processed++;
  16359. return ret;
  16360. }
  16361. }
  16362. if((joinLimit == 0) || !group.isItem(rightIndex))
  16363. {
  16364. if(leftOuterJoin && !matchedLeft && !failingLimit)
  16365. {
  16366. const void * ret = joinRecords(group.item(leftIndex), defaultRight, 0, NULL, JTFmatchedleft);
  16367. if(ret)
  16368. {
  16369. matchedLeft = true;
  16370. processed++;
  16371. return ret;
  16372. }
  16373. }
  16374. leftIndex++;
  16375. matchedLeft = false;
  16376. rightIndex = 0;
  16377. joinCounter = 0;
  16378. joinLimit = keepLimit;
  16379. }
  16380. if(!group.isItem(leftIndex))
  16381. {
  16382. if(failingLimit || failingOuterAtmost)
  16383. {
  16384. const void * lhs;
  16385. while((lhs = groupedInput->nextRow()) != NULL) // dualCache never active here
  16386. {
  16387. const void * ret = joinRecords(lhs, defaultRight, 0, failingLimit, JTFmatchedleft);
  16388. ReleaseRoxieRow(lhs);
  16389. if(ret)
  16390. {
  16391. processed++;
  16392. return ret;
  16393. }
  16394. }
  16395. failingLimit.clear();
  16396. }
  16397. if(rightOuterJoin && !failingLimit)
  16398. while(group.isItem(rightOuterIndex))
  16399. if(!matchedRight.item(rightOuterIndex++))
  16400. {
  16401. const void * ret = joinRecords(defaultLeft, group.item(rightOuterIndex-1), 0, NULL, JTFmatchedright);
  16402. if(ret)
  16403. {
  16404. processed++;
  16405. return ret;
  16406. }
  16407. }
  16408. if(!fillGroup())
  16409. return NULL;
  16410. continue;
  16411. }
  16412. const void * lhs = group.item(leftIndex);
  16413. if(failingLimit)
  16414. {
  16415. leftIndex++;
  16416. const void * ret = joinRecords(lhs, defaultRight, 0, failingLimit, JTFmatchedleft);
  16417. if(ret)
  16418. {
  16419. processed++;
  16420. return ret;
  16421. }
  16422. }
  16423. else
  16424. {
  16425. const void * rhs = group.item(rightIndex++);
  16426. if(helper.match(lhs, rhs))
  16427. {
  16428. matchedLeft = true;
  16429. matchedRight.replace(true, rightIndex-1);
  16430. if(!exclude)
  16431. {
  16432. const void * ret = joinRecords(lhs, rhs, ++joinCounter, NULL, JTFmatchedleft|JTFmatchedright);
  16433. if(ret)
  16434. {
  16435. processed++;
  16436. joinLimit--;
  16437. return ret;
  16438. }
  16439. }
  16440. }
  16441. }
  16442. }
  16443. return NULL;
  16444. }
  16445. }
  16446. };
  16447. class CRoxieServerSelfJoinActivityFactory : public CRoxieServerActivityFactory
  16448. {
  16449. bool forceSpill;
  16450. public:
  16451. CRoxieServerSelfJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  16452. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  16453. {
  16454. forceSpill = queryFactory.queryOptions().allSortsMaySpill || _graphNode.getPropBool("hint[@name='spill']/@value", false);;
  16455. }
  16456. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  16457. {
  16458. return new CRoxieServerSelfJoinActivity(_ctx, this, _probeManager, forceSpill);
  16459. }
  16460. virtual const StatisticsMapping &queryStatsMapping() const
  16461. {
  16462. return joinStatistics;
  16463. }
  16464. };
  16465. IRoxieServerActivityFactory *createRoxieServerSelfJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  16466. {
  16467. return new CRoxieServerSelfJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  16468. }
  16469. //=====================================================================================================
  16470. class CRoxieServerLookupJoinActivity : public CRoxieServerTwoInputActivity
  16471. {
  16472. private:
  16473. class LookupTable : public CInterface
  16474. {
  16475. public:
  16476. LookupTable(IHThorHashJoinArg &helper)
  16477. : leftRightCompare(helper.queryCompareLeftRight()), rightCompare(helper.queryCompareRight()),
  16478. leftHash(helper.queryHashLeft()), rightHash(helper.queryHashRight())
  16479. {
  16480. size = 0;
  16481. }
  16482. virtual const void *find(const void * left) const = 0;
  16483. virtual const void *findNext(const void * left) const = 0;
  16484. protected:
  16485. ICompare * leftRightCompare;
  16486. ICompare * rightCompare;
  16487. IHash * leftHash;
  16488. IHash * rightHash;
  16489. unsigned size;
  16490. };
  16491. class DedupLookupTable : public LookupTable
  16492. {
  16493. public:
  16494. DedupLookupTable(ConstPointerArray &rightRows, IHThorHashJoinArg &helper)
  16495. : LookupTable(helper)
  16496. {
  16497. size = (4*rightRows.length())/3 + 1;
  16498. table = (const void * *)calloc(size, sizeof(void *)); // This should probably be allocated from roxiemem (and size rounded up to actual available size)
  16499. ForEachItemIn(idx, rightRows)
  16500. add(rightRows.item(idx));
  16501. }
  16502. ~DedupLookupTable()
  16503. {
  16504. roxiemem::ReleaseRoxieRowArray(size, table);
  16505. free(table);
  16506. }
  16507. virtual const void *find(const void * left) const
  16508. {
  16509. unsigned index = leftHash->hash(left) % size;
  16510. unsigned start = index;
  16511. while (table[index])
  16512. {
  16513. if(leftRightCompare->docompare(left, table[index]) == 0)
  16514. return table[index];
  16515. index++;
  16516. if (index==size)
  16517. index = 0;
  16518. if (index==start)
  16519. throw MakeStringException(ROXIE_JOIN_ERROR, "Internal error in lookup join activity (hash table full on lookup)");
  16520. }
  16521. return NULL;
  16522. }
  16523. virtual const void *findNext(const void * left) const
  16524. {
  16525. return NULL;
  16526. }
  16527. protected:
  16528. void add(const void * right)
  16529. {
  16530. unsigned index = rightHash->hash(right) % size;
  16531. unsigned start = index;
  16532. while (table[index])
  16533. {
  16534. if (rightCompare->docompare(table[index], right) == 0)
  16535. {
  16536. ReleaseRoxieRow(right);
  16537. return;
  16538. }
  16539. index++;
  16540. if (index==size)
  16541. index = 0;
  16542. if (index==start)
  16543. throw MakeStringException(ROXIE_JOIN_ERROR, "Internal error in lookup join activity (hash table full on add)");
  16544. }
  16545. table[index] = right;
  16546. }
  16547. const void * * table;
  16548. };
  16549. class FewLookupTable : public LookupTable
  16550. {
  16551. public:
  16552. FewLookupTable(ConstPointerArray &rightRows, IHThorHashJoinArg &helper)
  16553. : LookupTable(helper)
  16554. {
  16555. size = (4*rightRows.length())/3 + 1;
  16556. table = (const void * *)calloc(size, sizeof(void *)); // This should probably be allocated from roxiemem
  16557. findex = fstart = BadIndex;
  16558. ForEachItemIn(idx, rightRows)
  16559. add(rightRows.item(idx));
  16560. }
  16561. ~FewLookupTable()
  16562. {
  16563. roxiemem::ReleaseRoxieRowArray(size, table);
  16564. free(table);
  16565. }
  16566. virtual const void *find(const void * left) const
  16567. {
  16568. fstart = leftHash->hash(left) % size;
  16569. findex = fstart;
  16570. return doFind(left);
  16571. }
  16572. virtual const void *findNext(const void * left) const
  16573. {
  16574. if (findex == BadIndex)
  16575. return NULL;
  16576. advance();
  16577. return doFind(left);
  16578. }
  16579. protected:
  16580. void add(const void * right)
  16581. {
  16582. unsigned start = rightHash->hash(right) % size;
  16583. unsigned index = start;
  16584. while (table[index])
  16585. {
  16586. index++;
  16587. if (index==size)
  16588. index = 0;
  16589. if (index==start)
  16590. throwUnexpected(); //table is full, should never happen
  16591. }
  16592. table[index] = right;
  16593. }
  16594. void advance() const
  16595. {
  16596. findex++;
  16597. if(findex==size)
  16598. findex = 0;
  16599. if(findex==fstart)
  16600. throw MakeStringException(ROXIE_JOIN_ERROR, "Internal error in lookup join activity (hash table full on lookup)");
  16601. }
  16602. const void *doFind(const void * left) const
  16603. {
  16604. while(table[findex])
  16605. {
  16606. if (leftRightCompare->docompare(left, table[findex]) == 0)
  16607. return table[findex];
  16608. advance();
  16609. }
  16610. findex = BadIndex;
  16611. return NULL;
  16612. }
  16613. const void * * table;
  16614. unsigned mutable fstart;
  16615. unsigned mutable findex;
  16616. static unsigned const BadIndex;
  16617. };
  16618. class ManyLookupTable : public LookupTable
  16619. {
  16620. public:
  16621. ManyLookupTable(ConstPointerArray &rightRows, IHThorHashJoinArg &helper)
  16622. : LookupTable(helper)
  16623. {
  16624. rightRows.swapWith(rowtable);
  16625. UInt64Array groups;
  16626. unsigned numRows = rowtable.length();
  16627. if (numRows)
  16628. {
  16629. unsigned groupStart = 0;
  16630. const void *groupStartRow = rowtable.item(0);
  16631. for (unsigned i=1; i < numRows; i++)
  16632. {
  16633. const void *thisRow = rowtable.item(i);
  16634. if (rightCompare->docompare(groupStartRow, thisRow))
  16635. {
  16636. groups.append(makeint64(groupStart, i-groupStart));
  16637. groupStart = i;
  16638. groupStartRow = thisRow;
  16639. }
  16640. }
  16641. groups.append(makeint64(groupStart, numRows-groupStart));
  16642. }
  16643. size = (4*groups.length())/3 + 1;
  16644. table = (__uint64 *) calloc(size, sizeof(__uint64)); // This should probably be allocated from roxiemem
  16645. ForEachItemIn(idx, groups)
  16646. {
  16647. unsigned __int64 group = groups.item(idx);
  16648. unsigned groupstart = high(group);
  16649. const void *row = rowtable.item(groupstart);
  16650. add(row, group);
  16651. }
  16652. }
  16653. ~ManyLookupTable()
  16654. {
  16655. roxiemem::ReleaseRoxieRows(rowtable);
  16656. free(table);
  16657. }
  16658. void add(const void *row, unsigned __int64 group)
  16659. {
  16660. unsigned start = rightHash->hash(row) % size;
  16661. unsigned index = start;
  16662. while (table[index])
  16663. {
  16664. index++;
  16665. if (index==size)
  16666. index = 0;
  16667. if (index==start)
  16668. throwUnexpected(); //table is full, should never happen
  16669. }
  16670. table[index] = group;
  16671. }
  16672. virtual const void *find(const void * left) const
  16673. {
  16674. unsigned index = leftHash->hash(left) % size;
  16675. unsigned start = index;
  16676. while (table[index])
  16677. {
  16678. __uint64 group = table[index];
  16679. currentMatch = high(group);
  16680. const void *right = rowtable.item(currentMatch);
  16681. if (leftRightCompare->docompare(left, right) == 0)
  16682. {
  16683. currentMatch++;
  16684. matchCount = low(group) - 1;
  16685. return right;
  16686. }
  16687. index++;
  16688. if (index==size)
  16689. index = 0;
  16690. if (index==start)
  16691. throw MakeStringException(ROXIE_JOIN_ERROR, "Internal error in lookup join activity (hash table full on lookup)");
  16692. }
  16693. matchCount = 0;
  16694. return NULL;
  16695. }
  16696. virtual const void *findNext(const void * left) const
  16697. {
  16698. if (!matchCount)
  16699. return NULL;
  16700. matchCount--;
  16701. return rowtable.item(currentMatch++);
  16702. }
  16703. protected:
  16704. __uint64 *table;
  16705. ConstPointerArray rowtable;
  16706. mutable unsigned currentMatch = 0;
  16707. mutable unsigned matchCount = 0;
  16708. };
  16709. IHThorHashJoinArg &helper;
  16710. bool leftOuterJoin;
  16711. bool exclude;
  16712. bool eog;
  16713. bool many;
  16714. bool dedupRHS;
  16715. bool useFewTable;
  16716. bool matchedGroup;
  16717. const void *left;
  16718. OwnedConstRoxieRow defaultRight;
  16719. Owned<LookupTable> table;
  16720. unsigned keepLimit;
  16721. unsigned atmostLimit;
  16722. unsigned atmostsTriggered;
  16723. unsigned limitLimit;
  16724. unsigned joinCounter;
  16725. bool limitFail;
  16726. bool limitOnFail;
  16727. bool hasGroupLimit;
  16728. bool isSmartJoin;
  16729. unsigned keepCount;
  16730. bool gotMatch;
  16731. bool cloneLeft;
  16732. ConstPointerArray rightGroup;
  16733. aindex_t rightGroupIndex;
  16734. Owned<IException> failingLimit;
  16735. ConstPointerArray filteredRight;
  16736. ThorActivityKind activityKind;
  16737. Owned<IEngineRowAllocator> defaultRightAllocator;
  16738. void createDefaultRight()
  16739. {
  16740. if (!defaultRight)
  16741. {
  16742. if (!defaultRightAllocator)
  16743. defaultRightAllocator.setown(createRowAllocator(input1->queryOutputMeta()));
  16744. RtlDynamicRowBuilder rowBuilder(defaultRightAllocator);
  16745. size32_t thisSize = helper.createDefaultRight(rowBuilder);
  16746. defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
  16747. }
  16748. }
  16749. public:
  16750. CRoxieServerLookupJoinActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _useFewTable)
  16751. : CRoxieServerTwoInputActivity(_ctx, _factory, _probeManager), helper((IHThorHashJoinArg &)basehelper), useFewTable(_useFewTable)
  16752. {
  16753. unsigned joinFlags = helper.getJoinFlags();
  16754. leftOuterJoin = (joinFlags & JFleftouter) != 0;
  16755. assertex((joinFlags & JFrightouter) == 0);
  16756. exclude = (joinFlags & JFexclude) != 0;
  16757. many = (joinFlags & JFmanylookup) != 0;
  16758. cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
  16759. dedupRHS = (joinFlags & (JFmanylookup | JFmatchrequired | JFtransformMaySkip)) == 0; // optimisation: can implicitly dedup RHS unless is many lookup, or match required, or transform may skip
  16760. left = NULL;
  16761. activityKind = factory->getKind();
  16762. eog = false;
  16763. matchedGroup = false;
  16764. gotMatch = false;
  16765. keepLimit = 0;
  16766. keepCount = 0;
  16767. joinCounter = 0;
  16768. atmostLimit = 0;
  16769. atmostsTriggered = 0;
  16770. limitLimit = 0;
  16771. rightGroupIndex = 0;
  16772. hasGroupLimit = false;
  16773. isSmartJoin = (joinFlags & JFsmart) != 0;
  16774. getLimitType(joinFlags, limitFail, limitOnFail);
  16775. }
  16776. void loadRight()
  16777. {
  16778. ConstPointerArray rightset;
  16779. try
  16780. {
  16781. const void * next;
  16782. while(true)
  16783. {
  16784. next = inputStream1->ungroupedNextRow();
  16785. if(!next)
  16786. break;
  16787. rightset.append(next);
  16788. }
  16789. if (!dedupRHS)
  16790. {
  16791. if (useFewTable)
  16792. {
  16793. table.setown(new FewLookupTable(rightset, helper)); // NOTE - takes ownership of rightset
  16794. }
  16795. else
  16796. {
  16797. if (!helper.isRightAlreadySorted())
  16798. {
  16799. if (helper.getJoinFlags() & JFunstable)
  16800. {
  16801. qsortvec(const_cast<void * *>(rightset.getArray()), rightset.ordinality(), *helper.queryCompareRight());
  16802. }
  16803. else
  16804. {
  16805. unsigned rightord = rightset.ordinality();
  16806. MemoryAttr tempAttr(rightord*sizeof(void **)); // Temp storage for stable sort. This should probably be allocated from roxiemem
  16807. void **temp = (void **) tempAttr.bufferBase();
  16808. void **_rows = const_cast<void * *>(rightset.getArray());
  16809. msortvecstableinplace(_rows, rightord, *helper.queryCompareRight(), temp);
  16810. }
  16811. }
  16812. table.setown(new ManyLookupTable(rightset, helper)); // NOTE - takes ownership of rightset
  16813. }
  16814. }
  16815. else
  16816. {
  16817. table.setown(new DedupLookupTable(rightset, helper)); // NOTE - takes ownership of rightset
  16818. }
  16819. }
  16820. catch (...)
  16821. {
  16822. roxiemem::ReleaseRoxieRows(rightset);
  16823. throw;
  16824. }
  16825. };
  16826. virtual void reset()
  16827. {
  16828. if (atmostsTriggered)
  16829. noteStatistic(StNumAtmostTriggered, atmostsTriggered);
  16830. CRoxieServerTwoInputActivity::reset();
  16831. ReleaseClearRoxieRow(left);
  16832. defaultRight.clear();
  16833. table.clear();
  16834. }
  16835. virtual bool needsAllocator() const { return true; }
  16836. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  16837. {
  16838. eog = false;
  16839. matchedGroup = false;
  16840. left = NULL;
  16841. CRoxieServerTwoInputActivity::start(parentExtractSize, parentExtract, paused);
  16842. keepLimit = helper.getKeepLimit();
  16843. if(keepLimit==0) keepLimit = static_cast<unsigned>(-1);
  16844. atmostsTriggered = 0;
  16845. atmostLimit = helper.getJoinLimit();
  16846. limitLimit = helper.getMatchAbortLimit();
  16847. hasGroupLimit = ((atmostLimit > 0) || (limitLimit > 0));
  16848. if(atmostLimit==0) atmostLimit = static_cast<unsigned>(-1);
  16849. if(limitLimit==0) limitLimit = static_cast<unsigned>(-1);
  16850. getLimitType(helper.getJoinFlags(), limitFail, limitOnFail);
  16851. switch (activityKind)
  16852. {
  16853. case TAKlookupjoin:
  16854. case TAKlookupdenormalizegroup:
  16855. case TAKsmartjoin:
  16856. case TAKsmartdenormalizegroup:
  16857. if (leftOuterJoin)
  16858. createDefaultRight();
  16859. break;
  16860. }
  16861. if (limitOnFail)
  16862. createDefaultRight();
  16863. }
  16864. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  16865. {
  16866. if (!idx && (helper.getJoinFlags() & JFparallel) != 0)
  16867. {
  16868. puller.setown(new CRoxieServerReadAheadInput(ctx, ctx->queryOptions().parallelJoinPreload));
  16869. puller->setInput(0, _sourceIdx, _in);
  16870. _in = puller;
  16871. _sourceIdx = 0;
  16872. }
  16873. CRoxieServerTwoInputActivity::setInput(idx, _sourceIdx, _in);
  16874. }
  16875. virtual const void * nextRow()
  16876. {
  16877. ActivityTimer t(activityStats, timeActivities);
  16878. if(!table)
  16879. loadRight();
  16880. switch (activityKind)
  16881. {
  16882. case TAKlookupjoin:
  16883. case TAKsmartjoin:
  16884. return nextRowJoin();
  16885. case TAKlookupdenormalize:
  16886. case TAKlookupdenormalizegroup:
  16887. case TAKsmartdenormalize:
  16888. case TAKsmartdenormalizegroup:
  16889. return nextRowDenormalize();
  16890. }
  16891. throwUnexpected();
  16892. }
  16893. private:
  16894. const void * nextRowJoin()
  16895. {
  16896. if(!table)
  16897. loadRight();
  16898. while(true)
  16899. {
  16900. const void * right = NULL;
  16901. if(!left)
  16902. {
  16903. left = inputStream->nextRow();
  16904. keepCount = keepLimit;
  16905. joinCounter = 0;
  16906. if(!left)
  16907. {
  16908. if (isSmartJoin)
  16909. left = inputStream->nextRow();
  16910. if (!left)
  16911. {
  16912. if(matchedGroup || eog)
  16913. {
  16914. matchedGroup = false;
  16915. eog = true;
  16916. return NULL;
  16917. }
  16918. eog = true;
  16919. continue;
  16920. }
  16921. }
  16922. eog = false;
  16923. gotMatch = false;
  16924. right = getRightFirst();
  16925. }
  16926. else
  16927. right = getRightNext();
  16928. const void * ret = NULL;
  16929. if(failingLimit)
  16930. {
  16931. ret = joinException(left, failingLimit);
  16932. }
  16933. else
  16934. {
  16935. while(right)
  16936. {
  16937. if(helper.match(left, right))
  16938. {
  16939. gotMatch = true;
  16940. if(exclude)
  16941. break;
  16942. ret = joinRecords(left, right, ++joinCounter, JTFmatchedleft|JTFmatchedright);
  16943. if(ret)
  16944. {
  16945. processed++;
  16946. break;
  16947. }
  16948. }
  16949. right = getRightNext();
  16950. ret = NULL;
  16951. }
  16952. if(leftOuterJoin && !gotMatch)
  16953. {
  16954. ret = joinRecords(left, defaultRight, 0, JTFmatchedleft);
  16955. gotMatch = true;
  16956. }
  16957. }
  16958. if(ret)
  16959. {
  16960. matchedGroup = true;
  16961. processed++;
  16962. if(!many || (--keepCount == 0) || failingLimit)
  16963. {
  16964. ReleaseClearRoxieRow(left);
  16965. failingLimit.clear();
  16966. }
  16967. return ret;
  16968. }
  16969. ReleaseClearRoxieRow(left);
  16970. }
  16971. }
  16972. const void * nextRowDenormalize()
  16973. {
  16974. while(true)
  16975. {
  16976. left = inputStream->nextRow();
  16977. if(!left)
  16978. {
  16979. if (!matchedGroup || isSmartJoin)
  16980. left = inputStream->nextRow();
  16981. if (!left)
  16982. {
  16983. matchedGroup = false;
  16984. return NULL;
  16985. }
  16986. }
  16987. gotMatch = false;
  16988. const void * right = getRightFirst();
  16989. const void * ret = NULL;
  16990. if (failingLimit)
  16991. ret = joinException(left, failingLimit);
  16992. else if (activityKind == TAKlookupdenormalize || activityKind == TAKsmartdenormalize)
  16993. {
  16994. OwnedConstRoxieRow newLeft;
  16995. newLeft.set(left);
  16996. unsigned rowSize = 0;
  16997. unsigned leftCount = 0;
  16998. keepCount = keepLimit;
  16999. while (right)
  17000. {
  17001. try
  17002. {
  17003. if (helper.match(left, right))
  17004. {
  17005. gotMatch = true;
  17006. if (exclude)
  17007. break;
  17008. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  17009. unsigned thisSize = helper.transform(rowBuilder, newLeft, right, ++leftCount, JTFmatchedleft|JTFmatchedright);
  17010. if (thisSize)
  17011. {
  17012. rowSize = thisSize;
  17013. newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
  17014. }
  17015. if(!many || (--keepCount == 0))
  17016. break;
  17017. }
  17018. right = getRightNext();
  17019. }
  17020. catch (IException *E)
  17021. {
  17022. throw makeWrappedException(E);
  17023. }
  17024. }
  17025. if (rowSize)
  17026. ret = newLeft.getClear();
  17027. else if (leftOuterJoin && !gotMatch)
  17028. {
  17029. ret = left;
  17030. left = NULL;
  17031. }
  17032. }
  17033. else
  17034. {
  17035. filteredRight.kill();
  17036. keepCount = keepLimit;
  17037. while (right)
  17038. {
  17039. if (helper.match(left, right))
  17040. {
  17041. gotMatch = true;
  17042. if(exclude)
  17043. break;
  17044. filteredRight.append(right);
  17045. if(!many || (--keepCount == 0))
  17046. break;
  17047. }
  17048. right = getRightNext();
  17049. }
  17050. if((filteredRight.ordinality() > 0) || (leftOuterJoin && !gotMatch))
  17051. ret = denormalizeRecords(left, filteredRight, JTFmatchedleft);
  17052. filteredRight.kill();
  17053. }
  17054. ReleaseRoxieRow(left);
  17055. left = NULL;
  17056. failingLimit.clear();
  17057. if(ret)
  17058. {
  17059. matchedGroup = true;
  17060. processed++;
  17061. return ret;
  17062. }
  17063. }
  17064. }
  17065. const void * joinRecords(const void * left, const void * right, unsigned counter, unsigned flags)
  17066. {
  17067. if (cloneLeft)
  17068. {
  17069. LinkRoxieRow(left);
  17070. return left;
  17071. }
  17072. try
  17073. {
  17074. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  17075. unsigned outSize = helper.transform(rowBuilder, left, right, counter, flags);
  17076. if (outSize)
  17077. return rowBuilder.finalizeRowClear(outSize);
  17078. else
  17079. return NULL;
  17080. }
  17081. catch (IException *E)
  17082. {
  17083. throw makeWrappedException(E);
  17084. }
  17085. }
  17086. const void * joinException(const void * left, IException * except)
  17087. {
  17088. try
  17089. {
  17090. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  17091. unsigned outSize = helper.onFailTransform(rowBuilder, left, defaultRight, except, JTFmatchedleft);
  17092. if (outSize)
  17093. return rowBuilder.finalizeRowClear(outSize);
  17094. else
  17095. return NULL;
  17096. }
  17097. catch (IException *E)
  17098. {
  17099. throw makeWrappedException(E);
  17100. }
  17101. }
  17102. const void * denormalizeRecords(const void * left, ConstPointerArray & rows, unsigned flags)
  17103. {
  17104. try
  17105. {
  17106. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  17107. unsigned numRows = rows.ordinality();
  17108. const void * right = numRows ? rows.item(0) : defaultRight.get();
  17109. if (numRows>0)
  17110. flags |= JTFmatchedright;
  17111. unsigned outSize = helper.transform(rowBuilder, left, right, numRows, (const void * *)rows.getArray(), flags);
  17112. if (outSize)
  17113. return rowBuilder.finalizeRowClear(outSize);
  17114. else
  17115. return NULL;
  17116. }
  17117. catch (IException *E)
  17118. {
  17119. throw makeWrappedException(E);
  17120. }
  17121. }
  17122. const void * getRightFirst() { if(hasGroupLimit) return fillRightGroup(); else return table->find(left); }
  17123. const void * getRightNext() { if(hasGroupLimit) return readRightGroup(); else return table->findNext(left); }
  17124. const void * readRightGroup() { if(rightGroup.isItem(rightGroupIndex)) return rightGroup.item(rightGroupIndex++); else return NULL; }
  17125. const void *fillRightGroup()
  17126. {
  17127. rightGroup.kill();
  17128. for(const void * right = table->find(left); right; right = table->findNext(left))
  17129. {
  17130. rightGroup.append(right);
  17131. if(rightGroup.ordinality() > limitLimit)
  17132. {
  17133. if(limitFail)
  17134. failLimit();
  17135. gotMatch = true;
  17136. if (ctx->queryDebugContext())
  17137. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  17138. if(limitOnFail)
  17139. {
  17140. assertex(!failingLimit);
  17141. try
  17142. {
  17143. failLimit();
  17144. }
  17145. catch(IException * e)
  17146. {
  17147. failingLimit.setown(e);
  17148. }
  17149. assertex(failingLimit != NULL);
  17150. }
  17151. else
  17152. {
  17153. rightGroup.kill();
  17154. }
  17155. break;
  17156. }
  17157. if(rightGroup.ordinality() > atmostLimit)
  17158. {
  17159. atmostsTriggered++;
  17160. rightGroup.kill();
  17161. break;
  17162. }
  17163. }
  17164. rightGroupIndex = 0;
  17165. return readRightGroup();
  17166. }
  17167. void failLimit()
  17168. {
  17169. helper.onMatchAbortLimitExceeded();
  17170. CommonXmlWriter xmlwrite(XWFtrim|XWFopt );
  17171. if(!ctx->isBlind() && input->queryOutputMeta() && input->queryOutputMeta()->hasXML())
  17172. {
  17173. input->queryOutputMeta()->toXML(static_cast<const unsigned char *>(left), xmlwrite);
  17174. }
  17175. throw MakeStringException(ROXIE_TOO_MANY_RESULTS, "More than %u match candidates in join %d for row %s", limitLimit, queryId(), xmlwrite.str());
  17176. }
  17177. };
  17178. unsigned const CRoxieServerLookupJoinActivity::FewLookupTable::BadIndex(static_cast<unsigned>(-1));
  17179. class CRoxieServerLookupJoinActivityFactory : public CRoxieServerJoinActivityFactory
  17180. {
  17181. public:
  17182. CRoxieServerLookupJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  17183. : CRoxieServerJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  17184. {
  17185. Owned<IHThorHashJoinArg> helper = (IHThorHashJoinArg *) helperFactory();
  17186. useFewTable = _graphNode.getPropBool("hint[@name='usefewtable']/@value", false);
  17187. if((helper->getJoinFlags() & (JFfirst | JFfirstleft | JFfirstright | JFslidingmatch)) != 0)
  17188. throw MakeStringException(ROXIE_INVALID_FLAGS, "Invalid flags for lookup join activity"); // code generator should never create such an activity
  17189. }
  17190. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  17191. {
  17192. return new CRoxieServerLookupJoinActivity(_ctx, this, _probeManager, useFewTable);
  17193. }
  17194. virtual const StatisticsMapping &queryStatsMapping() const
  17195. {
  17196. return joinStatistics;
  17197. }
  17198. protected:
  17199. bool useFewTable;
  17200. };
  17201. IRoxieServerActivityFactory *createRoxieServerLookupJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  17202. {
  17203. return new CRoxieServerLookupJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  17204. }
  17205. //=====================================================================================================
  17206. class CRoxieServerAllJoinActivity : public CRoxieServerTwoInputActivity
  17207. {
  17208. private:
  17209. IHThorAllJoinArg &helper;
  17210. bool leftOuterJoin;
  17211. bool rightOuterJoin;
  17212. bool exclude;
  17213. OwnedConstRoxieRow defaultRight;
  17214. OwnedConstRoxieRow defaultLeft;
  17215. Owned<IEngineRowAllocator> defaultRightAllocator;
  17216. Owned<IEngineRowAllocator> defaultLeftAllocator;
  17217. const void *left;
  17218. unsigned countForLeft;
  17219. ConstPointerArray rightset;
  17220. BoolArray matchedRight; // MORE - could use a bitset...
  17221. unsigned keepLimit;
  17222. bool started;
  17223. bool eog;
  17224. bool eos;
  17225. bool matchedLeft;
  17226. bool matchedGroup;
  17227. bool leftIsGrouped;
  17228. bool cloneLeft;
  17229. unsigned rightIndex;
  17230. unsigned joinCounter;
  17231. unsigned rightOrdinality;
  17232. ThorActivityKind activityKind;
  17233. ConstPointerArray filteredRight;
  17234. void createDefaultLeft()
  17235. {
  17236. if (!defaultLeft)
  17237. {
  17238. if (!defaultLeftAllocator)
  17239. defaultLeftAllocator.setown(createRowAllocator(input->queryOutputMeta()));
  17240. RtlDynamicRowBuilder rowBuilder(defaultLeftAllocator);
  17241. size32_t thisSize = helper.createDefaultLeft(rowBuilder);
  17242. defaultLeft.setown(rowBuilder.finalizeRowClear(thisSize));
  17243. }
  17244. }
  17245. void createDefaultRight()
  17246. {
  17247. if (!defaultRight)
  17248. {
  17249. if (!defaultRightAllocator)
  17250. defaultRightAllocator.setown(createRowAllocator(input1->queryOutputMeta()));
  17251. RtlDynamicRowBuilder rowBuilder(defaultRightAllocator);
  17252. size32_t thisSize = helper.createDefaultRight(rowBuilder);
  17253. defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
  17254. }
  17255. }
  17256. public:
  17257. CRoxieServerAllJoinActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17258. : CRoxieServerTwoInputActivity(_ctx, _factory, _probeManager), helper((IHThorAllJoinArg &)basehelper)
  17259. {
  17260. unsigned joinFlags = helper.getJoinFlags();
  17261. leftOuterJoin = (joinFlags & JFleftouter) != 0;
  17262. rightOuterJoin = (joinFlags & JFrightouter) != 0;
  17263. cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
  17264. keepLimit = (unsigned) -1;
  17265. exclude = (joinFlags & JFexclude) != 0;
  17266. left = NULL;
  17267. started = true;
  17268. eog = false;
  17269. eos = false;
  17270. matchedLeft = false;
  17271. matchedGroup = false;
  17272. activityKind = factory->getKind();
  17273. rightIndex = 0;
  17274. rightOrdinality = 0;
  17275. leftIsGrouped = false;
  17276. countForLeft = 0;
  17277. joinCounter = 0;
  17278. }
  17279. virtual void reset()
  17280. {
  17281. defaultRight.clear();
  17282. defaultLeft.clear();
  17283. ReleaseRoxieRows(rightset);
  17284. if (left)
  17285. {
  17286. ReleaseRoxieRow(left);
  17287. left = nullptr;
  17288. }
  17289. matchedRight.kill();
  17290. CRoxieServerTwoInputActivity::reset();
  17291. }
  17292. virtual bool needsAllocator() const { return true; }
  17293. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  17294. {
  17295. eog = false;
  17296. eos = false;
  17297. matchedLeft = false;
  17298. matchedGroup = false;
  17299. started = false;
  17300. left = NULL;
  17301. CRoxieServerTwoInputActivity::start(parentExtractSize, parentExtract, paused);
  17302. keepLimit = helper.getKeepLimit();
  17303. if(keepLimit==0)
  17304. keepLimit = (unsigned) -1;
  17305. countForLeft = keepLimit;
  17306. joinCounter = 0;
  17307. leftIsGrouped = input->queryOutputMeta()->isGrouped();
  17308. if((activityKind==TAKalljoin || activityKind==TAKalldenormalizegroup) && leftOuterJoin)
  17309. createDefaultRight();
  17310. if(rightOuterJoin)
  17311. createDefaultLeft();
  17312. }
  17313. void loadRight()
  17314. {
  17315. const void * next;
  17316. while(true)
  17317. {
  17318. next = inputStream1->ungroupedNextRow();
  17319. if(!next)
  17320. break;
  17321. rightset.append(next);
  17322. matchedRight.append(false);
  17323. }
  17324. rightIndex = 0;
  17325. joinCounter = 0;
  17326. rightOrdinality = rightset.ordinality();
  17327. }
  17328. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  17329. {
  17330. if (!idx && (helper.getJoinFlags() & JFparallel) != 0)
  17331. {
  17332. puller.setown(new CRoxieServerReadAheadInput(ctx, ctx->queryOptions().parallelJoinPreload));
  17333. puller->setInput(0, _sourceIdx, _in);
  17334. _in = puller;
  17335. _sourceIdx = 0;
  17336. }
  17337. CRoxieServerTwoInputActivity::setInput(idx, _sourceIdx, _in);
  17338. }
  17339. const void * joinRecords(const void * left, const void * right, unsigned counter, unsigned flags)
  17340. {
  17341. // MORE - could share some code with lookup join
  17342. if (cloneLeft)
  17343. {
  17344. LinkRoxieRow(left);
  17345. return left;
  17346. }
  17347. try
  17348. {
  17349. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  17350. unsigned outSize = helper.transform(rowBuilder, left, right, counter, flags);
  17351. if (outSize)
  17352. return rowBuilder.finalizeRowClear(outSize);
  17353. else
  17354. return NULL;
  17355. }
  17356. catch (IException *E)
  17357. {
  17358. throw makeWrappedException(E);
  17359. }
  17360. }
  17361. const void * denormalizeRecords(const void * curLeft, ConstPointerArray & rows, unsigned flags)
  17362. {
  17363. try
  17364. {
  17365. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  17366. unsigned numRows = rows.ordinality();
  17367. const void * right = numRows ? rows.item(0) : defaultRight.get();
  17368. if (numRows>0)
  17369. flags |= JTFmatchedright;
  17370. unsigned outSize = helper.transform(rowBuilder, curLeft, right, numRows, rows.getArray(), flags);
  17371. if (outSize)
  17372. return rowBuilder.finalizeRowClear(outSize);
  17373. else
  17374. return NULL;
  17375. }
  17376. catch (IException *E)
  17377. {
  17378. throw makeWrappedException(E);
  17379. }
  17380. }
  17381. virtual const void *nextRow()
  17382. {
  17383. ActivityTimer t(activityStats, timeActivities);
  17384. if(!started)
  17385. {
  17386. started = true;
  17387. left = inputStream->nextRow();
  17388. matchedLeft = false;
  17389. countForLeft = keepLimit;
  17390. joinCounter = 0;
  17391. if(left == NULL)
  17392. {
  17393. eos = true;
  17394. return NULL;
  17395. }
  17396. loadRight();
  17397. }
  17398. const void * ret;
  17399. const void * right;
  17400. if(eos)
  17401. return NULL;
  17402. while(true)
  17403. {
  17404. ret = NULL;
  17405. if((rightIndex == rightOrdinality) || (countForLeft==0))
  17406. {
  17407. if(leftOuterJoin && left && !matchedLeft)
  17408. {
  17409. switch(activityKind)
  17410. {
  17411. case TAKalljoin:
  17412. ret = joinRecords(left, defaultRight, 0, JTFmatchedleft);
  17413. break;
  17414. case TAKalldenormalize:
  17415. ret = left;
  17416. left = NULL;
  17417. break;
  17418. case TAKalldenormalizegroup:
  17419. filteredRight.kill();
  17420. ret = denormalizeRecords(left, filteredRight, JTFmatchedleft);
  17421. break;
  17422. default:
  17423. throwUnexpected();
  17424. }
  17425. }
  17426. rightIndex = 0;
  17427. joinCounter = 0;
  17428. ReleaseRoxieRow(left);
  17429. left = NULL;
  17430. if(ret)
  17431. {
  17432. matchedGroup = true;
  17433. processed++;
  17434. return ret;
  17435. }
  17436. }
  17437. if(!left)
  17438. {
  17439. left = inputStream->nextRow();
  17440. matchedLeft = false;
  17441. countForLeft = keepLimit;
  17442. joinCounter = 0;
  17443. }
  17444. if(!left)
  17445. {
  17446. if(eog)
  17447. {
  17448. eos = true;
  17449. matchedGroup = false;
  17450. return NULL;
  17451. }
  17452. eog = true;
  17453. if (matchedGroup && leftIsGrouped)
  17454. {
  17455. matchedGroup = false;
  17456. return NULL;
  17457. }
  17458. matchedGroup = false;
  17459. continue;
  17460. }
  17461. eog = false;
  17462. switch(activityKind)
  17463. {
  17464. case TAKalljoin:
  17465. while(rightIndex < rightOrdinality)
  17466. {
  17467. right = rightset.item(rightIndex);
  17468. if(helper.match(left, right))
  17469. {
  17470. matchedLeft = true;
  17471. matchedRight.replace(true, rightIndex);
  17472. if(!exclude)
  17473. ret = joinRecords(left, right, ++joinCounter, JTFmatchedleft|JTFmatchedright);
  17474. }
  17475. rightIndex++;
  17476. if(ret)
  17477. {
  17478. countForLeft--;
  17479. matchedGroup = true;
  17480. processed++;
  17481. return ret;
  17482. }
  17483. }
  17484. break;
  17485. case TAKalldenormalize:
  17486. {
  17487. OwnedConstRoxieRow newLeft;
  17488. newLeft.set(left);
  17489. unsigned rowSize = 0;
  17490. unsigned leftCount = 0;
  17491. while((rightIndex < rightOrdinality) && countForLeft)
  17492. {
  17493. right = rightset.item(rightIndex);
  17494. if(helper.match(left, right))
  17495. {
  17496. matchedLeft = true;
  17497. matchedRight.replace(true, rightIndex);
  17498. if(!exclude)
  17499. {
  17500. try
  17501. {
  17502. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  17503. unsigned thisSize = helper.transform(rowBuilder, newLeft, right, ++leftCount, JTFmatchedleft|JTFmatchedright);
  17504. if(thisSize)
  17505. {
  17506. rowSize = thisSize;
  17507. newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
  17508. --countForLeft;
  17509. }
  17510. }
  17511. catch (IException *e)
  17512. {
  17513. throw makeWrappedException(e);
  17514. }
  17515. }
  17516. }
  17517. rightIndex++;
  17518. }
  17519. if(rowSize)
  17520. {
  17521. processed++;
  17522. return newLeft.getClear();
  17523. }
  17524. }
  17525. break;
  17526. case TAKalldenormalizegroup:
  17527. filteredRight.kill();
  17528. while((rightIndex < rightOrdinality) && countForLeft)
  17529. {
  17530. right = rightset.item(rightIndex);
  17531. if(helper.match(left, right))
  17532. {
  17533. matchedLeft = true;
  17534. matchedRight.replace(true, rightIndex);
  17535. filteredRight.append(right);
  17536. --countForLeft;
  17537. }
  17538. ++rightIndex;
  17539. }
  17540. if(!exclude && filteredRight.ordinality())
  17541. {
  17542. ret = denormalizeRecords(left, filteredRight, JTFmatchedleft);
  17543. filteredRight.kill();
  17544. if(ret)
  17545. {
  17546. processed++;
  17547. return ret;
  17548. }
  17549. }
  17550. break;
  17551. default:
  17552. throwUnexpected();
  17553. }
  17554. }
  17555. }
  17556. };
  17557. class CRoxieServerAllJoinActivityFactory : public CRoxieServerJoinActivityFactory
  17558. {
  17559. public:
  17560. CRoxieServerAllJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  17561. : CRoxieServerJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  17562. {
  17563. Owned<IHThorAllJoinArg> helper = (IHThorAllJoinArg *) helperFactory();
  17564. if((helper->getJoinFlags() & (JFfirst | JFfirstleft | JFfirstright)) != 0)
  17565. throw MakeStringException(ROXIE_INVALID_FLAGS, "Invalid flags for join all activity"); // code generator should never create such an activity
  17566. }
  17567. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  17568. {
  17569. return new CRoxieServerAllJoinActivity(_ctx, this, _probeManager);
  17570. }
  17571. virtual const StatisticsMapping &queryStatsMapping() const
  17572. {
  17573. return joinStatistics;
  17574. }
  17575. };
  17576. IRoxieServerActivityFactory *createRoxieServerAllJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  17577. {
  17578. return new CRoxieServerAllJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  17579. }
  17580. //=====================================================================================================
  17581. class CRoxieServerTopNActivity : public CRoxieServerLateStartActivity
  17582. {
  17583. unsigned limit;
  17584. bool hasBest;
  17585. bool eoi;
  17586. const void **sorted;
  17587. unsigned sortedCount;
  17588. unsigned curIndex;
  17589. IHThorTopNArg &helper;
  17590. ICompare &compare;
  17591. public:
  17592. CRoxieServerTopNActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17593. : CRoxieServerLateStartActivity(_ctx, _factory, _probeManager), helper((IHThorTopNArg &)basehelper), compare(*helper.queryCompare())
  17594. {
  17595. sorted = NULL;
  17596. sortedCount = 0;
  17597. curIndex = 0;
  17598. limit = 0;
  17599. eoi = false;
  17600. hasBest = false;
  17601. }
  17602. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  17603. {
  17604. assertex(sorted == NULL);
  17605. sortedCount = 0;
  17606. curIndex = 0;
  17607. eoi = false;
  17608. CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
  17609. limit = (unsigned) helper.getLimit();
  17610. hasBest = helper.hasBest();
  17611. lateStart(parentExtractSize, parentExtract, limit > 0);
  17612. // MORE - should we use an expanding array instead?
  17613. if (limit > 0)
  17614. sorted = (const void **) ctx->queryRowManager().allocate((limit+1) * sizeof(const void *), activityId);
  17615. }
  17616. virtual void reset()
  17617. {
  17618. if (sorted)
  17619. {
  17620. roxiemem::ReleaseRoxieRowRange(sorted, curIndex, sortedCount);
  17621. curIndex = 0;
  17622. sortedCount = 0;
  17623. ReleaseRoxieRow(sorted);
  17624. sorted = NULL;
  17625. }
  17626. CRoxieServerLateStartActivity::reset();
  17627. }
  17628. bool abortEarly()
  17629. {
  17630. if (hasBest && (sortedCount == limit))
  17631. {
  17632. int compare = helper.compareBest(sorted[sortedCount-1]);
  17633. if (compare == 0)
  17634. {
  17635. if (meta.isGrouped())
  17636. {
  17637. //MORE: This would be more efficient if we had a away of skipping to the end of the incoming group.
  17638. const void * next;
  17639. while ((next = inputStream->nextRow()) != NULL)
  17640. ReleaseRoxieRow(next);
  17641. }
  17642. else
  17643. eoi = true;
  17644. return true;
  17645. }
  17646. //This only checks the lowest element - we could check all elements inserted, but it would increase the number of compares
  17647. if (compare < 0)
  17648. throw MakeStringException(ROXIE_TOPN_ROW_ERROR, "TOPN: row found that exceeds the best value");
  17649. }
  17650. return false;
  17651. }
  17652. void getSorted()
  17653. {
  17654. curIndex = 0;
  17655. sortedCount = 0;
  17656. if(eoi)
  17657. return;
  17658. const void * next;
  17659. while ((next = inputStream->nextRow()) != NULL)
  17660. {
  17661. if (sortedCount < limit)
  17662. {
  17663. binary_vec_insert_stable(next, sorted, sortedCount, compare);
  17664. sortedCount++;
  17665. if(abortEarly())
  17666. return;
  17667. }
  17668. else
  17669. {
  17670. if(limit && compare.docompare(sorted[sortedCount-1], next) > 0) // MORE - if stability is an issue, need to consider whether this should be > or >=
  17671. {
  17672. binary_vec_insert_stable(next, sorted, sortedCount, compare); // MORE - not sure this is stable!
  17673. ReleaseRoxieRow(sorted[sortedCount]);
  17674. if(abortEarly())
  17675. return;
  17676. }
  17677. else
  17678. {
  17679. ReleaseRoxieRow(next); // do not bother with insertion sort if we know next will fall off the end
  17680. }
  17681. }
  17682. }
  17683. }
  17684. virtual const void * nextRow()
  17685. {
  17686. ActivityTimer t(activityStats, timeActivities);
  17687. if (eof)
  17688. return NULL;
  17689. if (curIndex >= sortedCount)
  17690. {
  17691. bool eog = sortedCount != 0;
  17692. getSorted();
  17693. if(sortedCount == 0)
  17694. {
  17695. eof = true;
  17696. return NULL;
  17697. }
  17698. if (eog)
  17699. return NULL;
  17700. }
  17701. processed++;
  17702. return sorted[curIndex++];
  17703. }
  17704. };
  17705. class CRoxieServerTopNActivityFactory : public CRoxieServerActivityFactory
  17706. {
  17707. public:
  17708. CRoxieServerTopNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  17709. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  17710. {
  17711. }
  17712. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  17713. {
  17714. return new CRoxieServerTopNActivity(_ctx, this, _probeManager);
  17715. }
  17716. };
  17717. IRoxieServerActivityFactory *createRoxieServerTopNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  17718. {
  17719. return new CRoxieServerTopNActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  17720. }
  17721. //=================================================================================
  17722. class CRoxieServerLimitActivity : public CRoxieServerActivity
  17723. {
  17724. protected:
  17725. unsigned __int64 rowLimit;
  17726. IHThorLimitArg &helper;
  17727. public:
  17728. CRoxieServerLimitActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17729. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorLimitArg &)basehelper)
  17730. {
  17731. rowLimit = 0;
  17732. }
  17733. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  17734. {
  17735. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  17736. rowLimit = helper.getRowLimit(); // could conceivably depend on context so should not compute any earlier than this
  17737. }
  17738. virtual const void *nextRow()
  17739. {
  17740. ActivityTimer t(activityStats, timeActivities);
  17741. const void * ret = inputStream->nextRow();
  17742. if (ret)
  17743. {
  17744. processed++;
  17745. if (processed > rowLimit)
  17746. {
  17747. ReleaseRoxieRow(ret);
  17748. if (traceLevel > 4)
  17749. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  17750. helper.onLimitExceeded();
  17751. }
  17752. }
  17753. return ret;
  17754. }
  17755. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  17756. {
  17757. ActivityTimer t(activityStats, timeActivities);
  17758. const void * ret = inputStream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  17759. if (ret)
  17760. {
  17761. if (wasCompleteMatch)
  17762. processed++;
  17763. if (processed > rowLimit)
  17764. {
  17765. ReleaseRoxieRow(ret);
  17766. if (traceLevel > 4)
  17767. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  17768. helper.onLimitExceeded();
  17769. }
  17770. }
  17771. return ret;
  17772. }
  17773. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  17774. {
  17775. return input->gatherConjunctions(collector);
  17776. }
  17777. virtual void resetEOF()
  17778. {
  17779. //Do not reset the rowLimit
  17780. inputStream->resetEOF();
  17781. }
  17782. IInputSteppingMeta * querySteppingMeta()
  17783. {
  17784. return input->querySteppingMeta();
  17785. }
  17786. };
  17787. class CRoxieServerLimitActivityFactory : public CRoxieServerActivityFactory
  17788. {
  17789. public:
  17790. CRoxieServerLimitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  17791. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  17792. {
  17793. }
  17794. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  17795. {
  17796. return new CRoxieServerLimitActivity(_ctx, this, _probeManager);
  17797. }
  17798. };
  17799. IRoxieServerActivityFactory *createRoxieServerLimitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  17800. {
  17801. return new CRoxieServerLimitActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  17802. }
  17803. //=====================================================================================================
  17804. class CRoxieServerSkipLimitActivity : public CRoxieServerLimitActivity
  17805. {
  17806. ConstPointerArray buff;
  17807. bool started = false;
  17808. unsigned index = 0;
  17809. bool isFail = false;
  17810. public:
  17811. CRoxieServerSkipLimitActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _onFail)
  17812. : CRoxieServerLimitActivity(_ctx, _factory, _probeManager)
  17813. {
  17814. started = false;
  17815. index = 0;
  17816. isFail = _onFail;
  17817. }
  17818. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  17819. {
  17820. started = false;
  17821. index = 0;
  17822. CRoxieServerLimitActivity::start(parentExtractSize, parentExtract, paused);
  17823. }
  17824. virtual void reset()
  17825. {
  17826. roxiemem::ReleaseRoxieRowRange(buff.getArray(), index, buff.ordinality());
  17827. buff.kill();
  17828. index = 0;
  17829. started = false;
  17830. CRoxieServerLimitActivity::reset();
  17831. }
  17832. virtual const void *nextRow()
  17833. {
  17834. ActivityTimer t(activityStats, timeActivities);
  17835. if (!started)
  17836. pullInput();
  17837. if (buff.isItem(index))
  17838. {
  17839. const void * next = buff.item(index++);
  17840. if(next)
  17841. processed++;
  17842. return next;
  17843. }
  17844. return NULL;
  17845. }
  17846. protected:
  17847. void pullInput()
  17848. {
  17849. unsigned count = 0;
  17850. for (;;)
  17851. {
  17852. const void * next = inputStream->nextRow();
  17853. if (next == NULL)
  17854. {
  17855. next = inputStream->nextRow();
  17856. if(next == NULL)
  17857. break;
  17858. buff.append(NULL);
  17859. }
  17860. count++;
  17861. if (count > rowLimit)
  17862. {
  17863. ReleaseRoxieRow(next);
  17864. ReleaseRoxieRows(buff);
  17865. if (ctx->queryDebugContext())
  17866. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  17867. if (isFail)
  17868. {
  17869. ensureRowAllocator();
  17870. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  17871. size32_t outSize = helper.transformOnLimitExceeded(rowBuilder);
  17872. if (outSize)
  17873. buff.append(rowBuilder.finalizeRowClear(outSize));
  17874. }
  17875. break;
  17876. }
  17877. buff.append(next);
  17878. }
  17879. started = true;
  17880. }
  17881. };
  17882. class CRoxieServerSkipLimitActivityFactory : public CRoxieServerActivityFactory
  17883. {
  17884. public:
  17885. CRoxieServerSkipLimitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  17886. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  17887. {
  17888. }
  17889. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  17890. {
  17891. return new CRoxieServerSkipLimitActivity(_ctx, this, _probeManager, kind==TAKcreaterowlimit);
  17892. }
  17893. };
  17894. IRoxieServerActivityFactory *createRoxieServerSkipLimitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  17895. {
  17896. return new CRoxieServerSkipLimitActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  17897. }
  17898. //=================================================================================
  17899. class CRoxieServerCatchActivity : public CRoxieServerActivity
  17900. {
  17901. IHThorCatchArg &helper;
  17902. public:
  17903. CRoxieServerCatchActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  17904. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorCatchArg &)basehelper)
  17905. {
  17906. }
  17907. // MORE - you could argue we should catch exceptions in START too, though not clear what to do with them
  17908. // See CRoxieServerSkipCatchActivity::start
  17909. // This is less significant now that we properly distinguish start exceptions from read exceptions in ThroughSpill activity
  17910. virtual const void *nextRow()
  17911. {
  17912. ActivityTimer t(activityStats, timeActivities);
  17913. try
  17914. {
  17915. const void *ret = inputStream->nextRow();
  17916. if (ret)
  17917. processed++;
  17918. return ret;
  17919. }
  17920. catch (IException *E)
  17921. {
  17922. E->Release();
  17923. helper.onExceptionCaught();
  17924. }
  17925. catch (...)
  17926. {
  17927. helper.onExceptionCaught();
  17928. }
  17929. throwUnexpected(); // onExceptionCaught should have thrown something
  17930. }
  17931. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  17932. {
  17933. try
  17934. {
  17935. ActivityTimer t(activityStats, timeActivities);
  17936. const void * ret = inputStream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  17937. if (ret && wasCompleteMatch)
  17938. processed++;
  17939. return ret;
  17940. }
  17941. catch (IException *E)
  17942. {
  17943. E->Release();
  17944. helper.onExceptionCaught();
  17945. }
  17946. catch (...)
  17947. {
  17948. helper.onExceptionCaught();
  17949. }
  17950. throwUnexpected(); // onExceptionCaught should have thrown something
  17951. }
  17952. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  17953. {
  17954. return input->gatherConjunctions(collector);
  17955. }
  17956. virtual void resetEOF()
  17957. {
  17958. inputStream->resetEOF(); // MORE - why not in base class?
  17959. }
  17960. IInputSteppingMeta * querySteppingMeta()
  17961. {
  17962. return input->querySteppingMeta();
  17963. }
  17964. };
  17965. class CRoxieServerSkipCatchActivity : public CRoxieServerActivity
  17966. {
  17967. ConstPointerArray buff;
  17968. bool started;
  17969. unsigned index;
  17970. IHThorCatchArg &helper;
  17971. bool createRow;
  17972. public:
  17973. CRoxieServerSkipCatchActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _createRow)
  17974. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorCatchArg &)basehelper), createRow(_createRow)
  17975. {
  17976. started = false;
  17977. index = 0;
  17978. }
  17979. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  17980. {
  17981. started = false;
  17982. index = 0;
  17983. try
  17984. {
  17985. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  17986. }
  17987. catch (IException *E)
  17988. {
  17989. ensureCreated();
  17990. onException(E);
  17991. started = true;
  17992. }
  17993. catch (...)
  17994. {
  17995. ensureCreated();
  17996. onException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught"));
  17997. started = true;
  17998. }
  17999. }
  18000. virtual void reset()
  18001. {
  18002. roxiemem::ReleaseRoxieRowRange(buff.getArray(), index, buff.ordinality());
  18003. buff.kill();
  18004. index = 0;
  18005. started = false;
  18006. CRoxieServerActivity::reset();
  18007. }
  18008. virtual const void *nextRow()
  18009. {
  18010. ActivityTimer t(activityStats, timeActivities);
  18011. if (!started)
  18012. pullInput();
  18013. if (buff.isItem(index))
  18014. {
  18015. const void * next = buff.item(index++);
  18016. if(next)
  18017. processed++;
  18018. return next;
  18019. }
  18020. return NULL;
  18021. }
  18022. protected:
  18023. void onException(IException *E)
  18024. {
  18025. inputStream->stop();
  18026. ReleaseRoxieRows(buff);
  18027. if (createRow)
  18028. {
  18029. ensureRowAllocator();
  18030. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  18031. size32_t outSize = helper.transformOnExceptionCaught(rowBuilder, E);
  18032. if (outSize)
  18033. buff.append(rowBuilder.finalizeRowClear(outSize));
  18034. }
  18035. E->Release();
  18036. }
  18037. void pullInput()
  18038. {
  18039. try
  18040. {
  18041. bool EOGseen = false;
  18042. for (;;)
  18043. {
  18044. const void * next = inputStream->nextRow();
  18045. buff.append(next);
  18046. if (next == NULL)
  18047. {
  18048. if (EOGseen)
  18049. break;
  18050. EOGseen = true;
  18051. }
  18052. else
  18053. EOGseen = false;
  18054. }
  18055. }
  18056. catch (IException *E)
  18057. {
  18058. onException(E);
  18059. }
  18060. catch (...)
  18061. {
  18062. onException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught"));
  18063. }
  18064. started = true;
  18065. }
  18066. };
  18067. class CRoxieServerCatchActivityFactory : public CRoxieServerActivityFactory
  18068. {
  18069. public:
  18070. CRoxieServerCatchActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  18071. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  18072. {
  18073. }
  18074. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  18075. {
  18076. switch (kind)
  18077. {
  18078. case TAKcatch:
  18079. return new CRoxieServerCatchActivity(_ctx, this, _probeManager);
  18080. case TAKskipcatch:
  18081. return new CRoxieServerSkipCatchActivity(_ctx, this, _probeManager, false);
  18082. case TAKcreaterowcatch:
  18083. return new CRoxieServerSkipCatchActivity(_ctx, this, _probeManager, true);
  18084. default:
  18085. throwUnexpected();
  18086. }
  18087. }
  18088. };
  18089. IRoxieServerActivityFactory *createRoxieServerCatchActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  18090. {
  18091. return new CRoxieServerCatchActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  18092. }
  18093. //=================================================================================
  18094. class CRoxieServerPullActivity : public CRoxieServerActivity
  18095. {
  18096. ConstPointerArray buff;
  18097. bool started;
  18098. unsigned index;
  18099. public:
  18100. CRoxieServerPullActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  18101. : CRoxieServerActivity(_ctx, _factory, _probeManager)
  18102. {
  18103. started = false;
  18104. index = 0;
  18105. }
  18106. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18107. {
  18108. started = false;
  18109. index = 0;
  18110. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  18111. }
  18112. virtual void reset()
  18113. {
  18114. while (buff.isItem(index))
  18115. ReleaseRoxieRow(buff.item(index++));
  18116. buff.kill();
  18117. started = false;
  18118. index = 0;
  18119. CRoxieServerActivity::reset();
  18120. }
  18121. virtual const void *nextRow()
  18122. {
  18123. ActivityTimer t(activityStats, timeActivities);
  18124. if (!started)
  18125. pullInput();
  18126. if (buff.isItem(index))
  18127. {
  18128. const void * next = buff.item(index++);
  18129. if (next)
  18130. processed++;
  18131. return next;
  18132. }
  18133. return NULL;
  18134. }
  18135. protected:
  18136. void pullInput()
  18137. {
  18138. bool EOGseen = false;
  18139. for (;;)
  18140. {
  18141. const void * next = inputStream->nextRow();
  18142. buff.append(next);
  18143. if (next == NULL)
  18144. {
  18145. if (EOGseen)
  18146. break;
  18147. EOGseen = true;
  18148. }
  18149. else
  18150. EOGseen = false;
  18151. }
  18152. started = true;
  18153. }
  18154. };
  18155. class CRoxieServerPullActivityFactory : public CRoxieServerActivityFactory
  18156. {
  18157. public:
  18158. CRoxieServerPullActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  18159. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  18160. {
  18161. }
  18162. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  18163. {
  18164. return new CRoxieServerPullActivity(_ctx, this, _probeManager);
  18165. }
  18166. };
  18167. IRoxieServerActivityFactory *createRoxieServerPullActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  18168. {
  18169. return new CRoxieServerPullActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  18170. }
  18171. //=================================================================================
  18172. class CRoxieServerTraceActivity : public CRoxieServerActivity
  18173. {
  18174. public:
  18175. CRoxieServerTraceActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  18176. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorTraceArg &) basehelper),
  18177. keepLimit(0), skip(0), sample(0)
  18178. {
  18179. assertex(meta.hasXML());
  18180. traceEnabled = defaultTraceEnabled && !isBlind();
  18181. }
  18182. virtual void onCreate(IHThorArg *_colocalParent)
  18183. {
  18184. CRoxieServerActivity::onCreate(_colocalParent);
  18185. if (ctx)
  18186. traceEnabled = ctx->queryOptions().traceEnabled && !isBlind();
  18187. }
  18188. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18189. {
  18190. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  18191. if (traceEnabled && helper.canMatchAny())
  18192. {
  18193. keepLimit = helper.getKeepLimit();
  18194. if (keepLimit==(unsigned) -1)
  18195. keepLimit = ctx->queryOptions().traceLimit;
  18196. skip = helper.getSkip();
  18197. sample = helper.getSample();
  18198. if (sample)
  18199. sample--;
  18200. name.setown(helper.getName());
  18201. if (!name)
  18202. name.set("Row");
  18203. }
  18204. else
  18205. keepLimit = 0;
  18206. }
  18207. virtual void stop()
  18208. {
  18209. name.clear();
  18210. CRoxieServerActivity::stop();
  18211. }
  18212. virtual bool isPassThrough()
  18213. {
  18214. return true;
  18215. }
  18216. virtual const void *nextRow()
  18217. {
  18218. ActivityTimer t(activityStats, timeActivities);
  18219. const void *row = inputStream->nextRow();
  18220. if (row)
  18221. {
  18222. onTrace(row);
  18223. processed++;
  18224. }
  18225. return row;
  18226. }
  18227. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  18228. {
  18229. // MORE - will need rethinking once we rethink the nextRowGE interface for global smart-stepping.
  18230. ActivityTimer t(activityStats, timeActivities);
  18231. const void * row = inputStream->nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
  18232. if (row)
  18233. {
  18234. onTrace(row);
  18235. processed++;
  18236. }
  18237. return row;
  18238. }
  18239. virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
  18240. {
  18241. return input->gatherConjunctions(collector);
  18242. }
  18243. virtual void resetEOF()
  18244. {
  18245. inputStream->resetEOF();
  18246. }
  18247. IInputSteppingMeta * querySteppingMeta()
  18248. {
  18249. return input->querySteppingMeta();
  18250. }
  18251. protected:
  18252. void onTrace(const void *row)
  18253. {
  18254. if (keepLimit && helper.isValid(row))
  18255. {
  18256. if (skip)
  18257. skip--;
  18258. else if (sample)
  18259. sample--;
  18260. else
  18261. {
  18262. CommonXmlWriter xmlwrite(XWFnoindent);
  18263. meta.toXML((const byte *) row, xmlwrite);
  18264. CTXLOG("TRACE: <%s>%s<%s>", name.get(), xmlwrite.str(), name.get());
  18265. keepLimit--;
  18266. sample = helper.getSample();
  18267. if (sample)
  18268. sample--;
  18269. }
  18270. }
  18271. }
  18272. OwnedRoxieString name;
  18273. IHThorTraceArg &helper;
  18274. unsigned keepLimit;
  18275. unsigned skip;
  18276. unsigned sample;
  18277. bool traceEnabled;
  18278. };
  18279. class CRoxieServerTraceActivityFactory : public CRoxieServerActivityFactory
  18280. {
  18281. public:
  18282. CRoxieServerTraceActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  18283. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  18284. {
  18285. }
  18286. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  18287. {
  18288. return new CRoxieServerTraceActivity(_ctx, this, _probeManager);
  18289. }
  18290. };
  18291. IRoxieServerActivityFactory *createRoxieServerTraceActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  18292. {
  18293. return new CRoxieServerTraceActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  18294. }
  18295. //=================================================================================
  18296. class CRoxieServerCaseActivity : public CRoxieServerMultiInputBaseActivity
  18297. {
  18298. IHThorCaseArg &helper;
  18299. unsigned cond;
  18300. bool unusedStopped;
  18301. IEngineRowStream *active;
  18302. public:
  18303. CRoxieServerCaseActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
  18304. : CRoxieServerMultiInputBaseActivity(_ctx, _factory, _probeManager, _numInputs), helper((IHThorCaseArg &)basehelper)
  18305. {
  18306. unusedStopped = false;
  18307. cond = 0;
  18308. active = NULL;
  18309. }
  18310. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18311. {
  18312. CRoxieServerMultiInputBaseActivity::start(parentExtractSize, parentExtract, paused);
  18313. cond = helper.getBranch();
  18314. //CHOOSE defaults to the last argument if out of range.
  18315. if (cond >= numInputs)
  18316. cond = numInputs - 1;
  18317. inputArray[cond]->start(parentExtractSize, parentExtract, paused);
  18318. startJunction(junctionArray[cond]);
  18319. assertex(numInputs==numStreams);
  18320. for (unsigned idx = 0; idx < numStreams; idx++)
  18321. {
  18322. if (idx!=cond)
  18323. streamArray[idx]->stop(); // Note: stopping unused branches early helps us avoid buffering splits too long.
  18324. }
  18325. active = streamArray[cond];
  18326. unusedStopped = true;
  18327. }
  18328. virtual void stop()
  18329. {
  18330. for (unsigned idx = 0; idx < numStreams; idx++)
  18331. {
  18332. if (idx==cond || !unusedStopped)
  18333. streamArray[idx]->stop();
  18334. }
  18335. CRoxieServerMultiInputBaseActivity::stop();
  18336. }
  18337. virtual void reset()
  18338. {
  18339. unusedStopped = false;
  18340. active = NULL;
  18341. CRoxieServerMultiInputBaseActivity::reset();
  18342. }
  18343. virtual const void *nextRow()
  18344. {
  18345. ActivityTimer t(activityStats, timeActivities);
  18346. if (active)
  18347. {
  18348. const void *ret = active->nextRow();
  18349. if (ret)
  18350. processed++;
  18351. return ret;
  18352. }
  18353. return NULL;
  18354. }
  18355. };
  18356. class CRoxieServerCaseActivityFactory : public CRoxieServerMultiInputFactory
  18357. {
  18358. bool graphInvariant;
  18359. public:
  18360. CRoxieServerCaseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _graphInvariant)
  18361. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  18362. {
  18363. graphInvariant = _graphInvariant;
  18364. }
  18365. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  18366. {
  18367. return new CRoxieServerCaseActivity(_ctx, this, _probeManager, numInputs());
  18368. }
  18369. virtual bool isGraphInvariant() const
  18370. {
  18371. return graphInvariant;
  18372. }
  18373. };
  18374. IRoxieServerActivityFactory *createRoxieServerCaseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _graphInvariant)
  18375. {
  18376. return new CRoxieServerCaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _graphInvariant);
  18377. }
  18378. //=================================================================================
  18379. class CRoxieServerIfActivity : public CRoxieServerActivity
  18380. {
  18381. IHThorIfArg &helper;
  18382. IFinalRoxieInput *inputTrue;
  18383. IFinalRoxieInput *inputFalse;
  18384. IEngineRowStream *streamTrue;
  18385. IEngineRowStream *streamFalse;
  18386. unsigned sourceIdxFalse = 0;
  18387. unsigned sourceIdxTrue = 0;
  18388. Owned<IStrandJunction> junctionTrue;
  18389. Owned<IStrandJunction> junctionFalse;
  18390. bool cond;
  18391. bool unusedStopped;
  18392. public:
  18393. CRoxieServerIfActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  18394. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorIfArg &)basehelper)
  18395. {
  18396. inputFalse = NULL;
  18397. inputTrue = NULL;
  18398. streamTrue = NULL;
  18399. streamFalse = NULL;
  18400. unusedStopped = false;
  18401. cond = false;
  18402. }
  18403. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18404. {
  18405. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  18406. cond = helper.getCondition();
  18407. if (traceStartStop)
  18408. DBGLOG("IfActivity::start %d - cond = %d", activityId, (int) cond);
  18409. if (cond)
  18410. {
  18411. if (inputTrue)
  18412. {
  18413. inputTrue->start(parentExtractSize, parentExtract, paused);
  18414. startJunction(junctionTrue);
  18415. }
  18416. if (streamFalse)
  18417. streamFalse->stop(); // Note: stopping unused branches early helps us avoid buffering splits too long.
  18418. }
  18419. else
  18420. {
  18421. if (inputFalse)
  18422. {
  18423. inputFalse->start(parentExtractSize, parentExtract, paused);
  18424. startJunction(junctionFalse);
  18425. }
  18426. if (streamTrue)
  18427. streamTrue->stop();
  18428. }
  18429. unusedStopped = true;
  18430. }
  18431. virtual void stop()
  18432. {
  18433. if (streamTrue && (!unusedStopped || cond))
  18434. streamTrue->stop();
  18435. if (streamFalse && (!unusedStopped || !cond))
  18436. streamFalse->stop();
  18437. CRoxieServerActivity::stop();
  18438. }
  18439. virtual unsigned __int64 queryLocalCycles() const
  18440. {
  18441. __int64 localCycles = activityStats.totalCycles;
  18442. if (inputTrue) // if conditional input cannot assume inputTrue
  18443. localCycles -= inputTrue->queryTotalCycles();
  18444. if (inputFalse)
  18445. localCycles -= inputFalse->queryTotalCycles();
  18446. if (localCycles < 0)
  18447. localCycles = 0;
  18448. return localCycles;
  18449. }
  18450. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  18451. {
  18452. switch (idx)
  18453. {
  18454. case 0:
  18455. return inputTrue;
  18456. case 1:
  18457. return inputFalse;
  18458. default:
  18459. return NULL;
  18460. }
  18461. }
  18462. virtual IEngineRowStream *queryConcreteOutputStream(unsigned whichInput) { return this; }
  18463. virtual IStrandJunction *queryConcreteOutputJunction(unsigned idx) const { return nullptr; }
  18464. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  18465. {
  18466. IFinalRoxieInput *in = cond ? inputTrue : inputFalse;
  18467. if (in)
  18468. return in->queryIndexReadActivity();
  18469. return NULL;
  18470. }
  18471. virtual void reset()
  18472. {
  18473. CRoxieServerActivity::reset();
  18474. if (inputTrue)
  18475. inputTrue->reset();
  18476. if (inputFalse)
  18477. inputFalse->reset();
  18478. resetJunction(junctionTrue);
  18479. resetJunction(junctionFalse);
  18480. unusedStopped = false;
  18481. }
  18482. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  18483. {
  18484. if (idx==1)
  18485. {
  18486. inputFalse = _in;
  18487. sourceIdxFalse = _sourceIdx;
  18488. }
  18489. else
  18490. {
  18491. assertex(!idx);
  18492. inputTrue = _in;
  18493. sourceIdxTrue = _sourceIdx;
  18494. }
  18495. }
  18496. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks)
  18497. {
  18498. //It should be possible to stream IF(), but the number of strands would need to match on the two branches.
  18499. streamTrue = connectSingleStream(ctx, inputTrue, sourceIdxTrue, junctionTrue, consumerOrdered);
  18500. streamFalse = connectSingleStream(ctx, inputFalse, sourceIdxFalse, junctionFalse, consumerOrdered);
  18501. return CRoxieServerActivity::getOutputStreams(ctx, idx, streams, nullptr, consumerOrdered, nullptr);
  18502. }
  18503. virtual const void *nextRow()
  18504. {
  18505. ActivityTimer t(activityStats, timeActivities);
  18506. IEngineRowStream *in = cond ? streamTrue : streamFalse;
  18507. if (in)
  18508. {
  18509. const void * ret;
  18510. if ((ret = in->nextRow()) != NULL)
  18511. processed++;
  18512. return ret;
  18513. }
  18514. return NULL;
  18515. }
  18516. };
  18517. class CRoxieServerIfActivityFactory : public CRoxieServerActivityFactory
  18518. {
  18519. unsigned input2;
  18520. unsigned input2idx;
  18521. bool graphInvariant;
  18522. public:
  18523. CRoxieServerIfActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _graphInvariant)
  18524. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  18525. {
  18526. graphInvariant = _graphInvariant;
  18527. input2 = (unsigned)-1;
  18528. input2idx = (unsigned)-1;
  18529. }
  18530. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  18531. {
  18532. return new CRoxieServerIfActivity(_ctx, this, _probeManager);
  18533. }
  18534. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  18535. {
  18536. if (idx==1)
  18537. {
  18538. input2 = source;
  18539. input2idx = sourceidx;
  18540. }
  18541. else
  18542. CRoxieServerActivityFactory::setInput(idx, source, sourceidx);
  18543. }
  18544. virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
  18545. {
  18546. switch (idx)
  18547. {
  18548. case 1:
  18549. sourceidx = input2idx;
  18550. return input2;
  18551. case 0:
  18552. return CRoxieServerActivityFactory::getInput(idx, sourceidx);
  18553. default:
  18554. return (unsigned) -1;
  18555. }
  18556. }
  18557. virtual unsigned numInputs() const { return (input2 == (unsigned)-1) ? 1 : 2; }
  18558. virtual bool isGraphInvariant() const
  18559. {
  18560. return graphInvariant;
  18561. }
  18562. };
  18563. IRoxieServerActivityFactory *createRoxieServerIfActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _graphInvariant)
  18564. {
  18565. return new CRoxieServerIfActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _graphInvariant);
  18566. }
  18567. //=================================================================================
  18568. class CRoxieServerActionBaseActivity : public CRoxieServerActivity
  18569. {
  18570. CriticalSection ecrit;
  18571. Owned<IException> exception;
  18572. bool executed;
  18573. public:
  18574. CRoxieServerActionBaseActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  18575. : CRoxieServerActivity(_ctx, _factory, _probeManager)
  18576. {
  18577. executed = false;
  18578. }
  18579. virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract) = 0;
  18580. virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
  18581. {
  18582. CriticalBlock b(ecrit);
  18583. if (exception)
  18584. throw(exception.getLink());
  18585. if (!executed)
  18586. {
  18587. try
  18588. {
  18589. executed = true;
  18590. start(parentExtractSize, parentExtract, false);
  18591. doExecuteAction(parentExtractSize, parentExtract);
  18592. stop();
  18593. }
  18594. catch (IException * E)
  18595. {
  18596. ctx->notifyAbort(E);
  18597. abort();
  18598. exception.set(E);
  18599. throw;
  18600. }
  18601. }
  18602. }
  18603. virtual void reset()
  18604. {
  18605. executed = false;
  18606. exception.clear();
  18607. CRoxieServerActivity::reset();
  18608. }
  18609. virtual const void *nextRow()
  18610. {
  18611. throwUnexpected(); // I am nobody's input
  18612. }
  18613. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  18614. {
  18615. return NULL;
  18616. }
  18617. };
  18618. class CRoxieServerIfActionActivity : public CRoxieServerActionBaseActivity
  18619. {
  18620. IHThorIfArg &helper;
  18621. public:
  18622. CRoxieServerIfActionActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  18623. : CRoxieServerActionBaseActivity(_ctx, _factory, _probeManager), helper((IHThorIfArg &)basehelper)
  18624. {
  18625. }
  18626. virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract) override
  18627. {
  18628. bool cond;
  18629. {
  18630. ActivityTimer t(activityStats, timeActivities);
  18631. cond = helper.getCondition();
  18632. }
  18633. stopDependencies(parentExtractSize, parentExtract, cond ? 2 : 1);
  18634. executeDependencies(parentExtractSize, parentExtract, cond ? 1 : 2);
  18635. }
  18636. virtual void stop() override
  18637. {
  18638. if (state != STATEstopped)
  18639. {
  18640. ForEachItemIn(idx, dependencies)
  18641. {
  18642. if (dependencyControlIds.item(idx) != 0)
  18643. dependencies.item(idx).stop();
  18644. }
  18645. }
  18646. CRoxieServerActionBaseActivity::stop();
  18647. }
  18648. };
  18649. class CRoxieServerIfActionActivityFactory : public CRoxieServerActivityFactory
  18650. {
  18651. bool isRoot;
  18652. public:
  18653. CRoxieServerIfActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  18654. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), isRoot(_isRoot)
  18655. {
  18656. }
  18657. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  18658. {
  18659. return new CRoxieServerIfActionActivity(_ctx, this, _probeManager);
  18660. }
  18661. virtual bool isSink() const
  18662. {
  18663. return isRoot;
  18664. }
  18665. };
  18666. IRoxieServerActivityFactory *createRoxieServerIfActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  18667. {
  18668. return new CRoxieServerIfActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _isRoot);
  18669. }
  18670. //=================================================================================
  18671. class CRoxieServerParallelActionActivity : public CRoxieServerActionBaseActivity
  18672. {
  18673. public:
  18674. CRoxieServerParallelActionActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  18675. : CRoxieServerActionBaseActivity(_ctx, _factory, _probeManager)
  18676. {
  18677. }
  18678. virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract)
  18679. {
  18680. #ifdef PARALLEL_EXECUTE
  18681. CParallelActivityExecutor afor(dependencies, parentExtractSize, parentExtract);
  18682. afor.For(dependencies.ordinality(), dependencies.ordinality(), true);
  18683. #else
  18684. ForEachItemIn(idx, dependencies)
  18685. {
  18686. dependencies.item(idx).execute(parentExtractSize, parentExtract);
  18687. }
  18688. #endif
  18689. }
  18690. };
  18691. class CRoxieServerParallelActionActivityFactory : public CRoxieServerActivityFactory
  18692. {
  18693. bool isRoot;
  18694. public:
  18695. CRoxieServerParallelActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  18696. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), isRoot(_isRoot)
  18697. {
  18698. assertex(!isRoot); // non-internal should be expanded out..
  18699. }
  18700. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  18701. {
  18702. return new CRoxieServerParallelActionActivity(_ctx, this, _probeManager);
  18703. }
  18704. virtual bool isSink() const
  18705. {
  18706. return isRoot;
  18707. }
  18708. };
  18709. IRoxieServerActivityFactory *createRoxieServerParallelActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  18710. {
  18711. return new CRoxieServerParallelActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _isRoot);
  18712. }
  18713. //=================================================================================
  18714. class CRoxieServerSequentialActionActivity : public CRoxieServerActionBaseActivity
  18715. {
  18716. IHThorSequentialArg &helper;
  18717. public:
  18718. CRoxieServerSequentialActionActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  18719. : CRoxieServerActionBaseActivity(_ctx, _factory, _probeManager), helper((IHThorSequentialArg &)basehelper)
  18720. {
  18721. }
  18722. virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract)
  18723. {
  18724. unsigned numBranches = helper.numBranches();
  18725. try
  18726. {
  18727. for (unsigned branch=1; branch <= numBranches; branch++)
  18728. executeDependencies(parentExtractSize, parentExtract, branch);
  18729. }
  18730. catch (...)
  18731. {
  18732. for (unsigned branch=1; branch <= numBranches; branch++)
  18733. stopDependencies(parentExtractSize, parentExtract, branch);
  18734. throw;
  18735. }
  18736. }
  18737. };
  18738. class CRoxieServerSequentialActionActivityFactory : public CRoxieServerActivityFactory
  18739. {
  18740. bool isRoot;
  18741. public:
  18742. CRoxieServerSequentialActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  18743. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), isRoot(_isRoot)
  18744. {
  18745. }
  18746. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  18747. {
  18748. return new CRoxieServerSequentialActionActivity(_ctx, this, _probeManager);
  18749. }
  18750. virtual bool isSink() const
  18751. {
  18752. return isRoot;
  18753. }
  18754. };
  18755. IRoxieServerActivityFactory *createRoxieServerSequentialActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  18756. {
  18757. return new CRoxieServerSequentialActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _isRoot);
  18758. }
  18759. //=================================================================================
  18760. class CRoxieServerWhenActivity : public CRoxieServerActivity
  18761. {
  18762. public:
  18763. CRoxieServerWhenActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  18764. : CRoxieServerActivity(_ctx, _factory, _probeManager)
  18765. {
  18766. savedExtractSize = 0;
  18767. savedExtract = NULL;
  18768. eofseen = false;
  18769. eogseen = false;
  18770. }
  18771. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18772. {
  18773. savedExtractSize = parentExtractSize;
  18774. savedExtract = parentExtract;
  18775. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  18776. executeDependencies(parentExtractSize, parentExtract, WhenBeforeId);
  18777. executeDependencies(parentExtractSize, parentExtract, WhenParallelId); // MORE: This should probably be done in parallel!
  18778. eofseen = false;
  18779. eogseen = false;
  18780. }
  18781. virtual void stop()
  18782. {
  18783. if (state == STATEreset || (!aborted && !eofseen)) // If someone else aborted, then WHEN clauses don't trigger, whatever
  18784. {
  18785. stopDependencies(savedExtractSize, savedExtract, WhenSuccessId);
  18786. stopDependencies(savedExtractSize, savedExtract, WhenFailureId);
  18787. }
  18788. else if (state != STATEstopped)
  18789. {
  18790. stopDependencies(savedExtractSize, savedExtract, aborted ? WhenSuccessId : WhenFailureId); // These ones don't get executed
  18791. executeDependencies(savedExtractSize, savedExtract, aborted ? WhenFailureId : WhenSuccessId); // These ones do
  18792. }
  18793. CRoxieServerActivity::stop();
  18794. }
  18795. virtual const void *nextRow()
  18796. {
  18797. try
  18798. {
  18799. ActivityTimer t(activityStats, timeActivities); // bit of a waste of time....
  18800. const void *ret = inputStream->nextRow();
  18801. if (!ret)
  18802. {
  18803. if (eogseen)
  18804. eofseen = true;
  18805. else
  18806. eogseen = true;
  18807. }
  18808. else
  18809. eogseen = false;
  18810. return ret;
  18811. }
  18812. catch (...)
  18813. {
  18814. aborted = true;
  18815. throw;
  18816. }
  18817. }
  18818. protected:
  18819. const byte *savedExtract;
  18820. unsigned savedExtractSize;
  18821. bool eofseen;
  18822. bool eogseen;
  18823. };
  18824. class CRoxieServerWhenActivityFactory : public CRoxieServerActivityFactory
  18825. {
  18826. public:
  18827. CRoxieServerWhenActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  18828. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  18829. {
  18830. }
  18831. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  18832. {
  18833. return new CRoxieServerWhenActivity(_ctx, this, _probeManager);
  18834. }
  18835. };
  18836. extern IRoxieServerActivityFactory *createRoxieServerWhenActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  18837. {
  18838. return new CRoxieServerWhenActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  18839. }
  18840. //=================================================================================
  18841. class CRoxieServerWhenActionActivity : public CRoxieServerActionBaseActivity
  18842. {
  18843. public:
  18844. CRoxieServerWhenActionActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  18845. : CRoxieServerActionBaseActivity(_ctx, _factory, _probeManager)
  18846. {
  18847. savedExtractSize = 0;
  18848. savedExtract = NULL;
  18849. }
  18850. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18851. {
  18852. savedExtractSize = parentExtractSize;
  18853. savedExtract = parentExtract;
  18854. CRoxieServerActionBaseActivity::start(parentExtractSize, parentExtract, paused);
  18855. executeDependencies(parentExtractSize, parentExtract, WhenBeforeId);
  18856. executeDependencies(parentExtractSize, parentExtract, WhenParallelId); // MORE: This should probably be done in parallel!
  18857. }
  18858. virtual void stop()
  18859. {
  18860. if (state != STATEstopped)
  18861. {
  18862. stopDependencies(savedExtractSize, savedExtract, aborted ? WhenSuccessId : WhenFailureId); // these are NOT going to execute
  18863. executeDependencies(savedExtractSize, savedExtract, aborted ? WhenFailureId : WhenSuccessId);
  18864. }
  18865. CRoxieServerActionBaseActivity::stop();
  18866. }
  18867. virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract)
  18868. {
  18869. executeDependencies(parentExtractSize, parentExtract, 1);
  18870. }
  18871. protected:
  18872. unsigned savedExtractSize;
  18873. const byte *savedExtract;
  18874. };
  18875. class CRoxieServerWhenActionActivityFactory : public CRoxieServerActivityFactory
  18876. {
  18877. public:
  18878. CRoxieServerWhenActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  18879. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), isRoot(_isRoot)
  18880. {
  18881. }
  18882. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  18883. {
  18884. return new CRoxieServerWhenActionActivity(_ctx, this, _probeManager);
  18885. }
  18886. virtual bool isSink() const
  18887. {
  18888. return isRoot;
  18889. }
  18890. private:
  18891. bool isRoot;
  18892. };
  18893. extern IRoxieServerActivityFactory *createRoxieServerWhenActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  18894. {
  18895. return new CRoxieServerWhenActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _isRoot);
  18896. }
  18897. //=================================================================================
  18898. class CRoxieServerStrandedParseActivity : public CRoxieServerStrandedActivity
  18899. {
  18900. INlpParseAlgorithm * algorithm;
  18901. class ParseProcessor : public StrandProcessor, implements IMatchedAction
  18902. {
  18903. protected:
  18904. IHThorParseArg &helper;
  18905. INlpParser * parser;
  18906. INlpResultIterator * rowIter;
  18907. const void * in;
  18908. char * curSearchText;
  18909. size32_t curSearchTextLen;
  18910. bool processRecord(const void * inRec)
  18911. {
  18912. if (helper.searchTextNeedsFree())
  18913. rtlFree(curSearchText);
  18914. curSearchTextLen = 0;
  18915. curSearchText = NULL;
  18916. helper.getSearchText(curSearchTextLen, curSearchText, inRec);
  18917. return parser->performMatch(*this, in, curSearchTextLen, curSearchText);
  18918. }
  18919. public:
  18920. ParseProcessor(CRoxieServerActivity &_parent, IEngineRowStream *_inputStream, IHThorParseArg &_helper, INlpParseAlgorithm * _algorithm)
  18921. : StrandProcessor(_parent, _inputStream, false), helper(_helper)
  18922. {
  18923. parser = _algorithm->createParser(parent.queryCodeContext(), parent.queryId(), helper.queryHelper(), &helper);
  18924. rowIter = parser->queryResultIter();
  18925. in = NULL;
  18926. curSearchText = NULL;
  18927. curSearchTextLen = 0;
  18928. }
  18929. ~ParseProcessor()
  18930. {
  18931. ::Release(parser);
  18932. }
  18933. virtual void start()
  18934. {
  18935. StrandProcessor::start();
  18936. numProcessedLastGroup = 0;
  18937. curSearchTextLen = 0;
  18938. curSearchText = NULL;
  18939. in = NULL;
  18940. parser->reset();
  18941. }
  18942. virtual void reset()
  18943. {
  18944. if (helper.searchTextNeedsFree())
  18945. rtlFree(curSearchText);
  18946. curSearchText = NULL;
  18947. ReleaseClearRoxieRow(in);
  18948. StrandProcessor::reset();
  18949. }
  18950. virtual const void * nextRow()
  18951. {
  18952. ActivityTimer t(activityStats, timeActivities);
  18953. for (;;)
  18954. {
  18955. if (rowIter->isValid())
  18956. {
  18957. OwnedConstRoxieRow out = rowIter->getRow();
  18958. rowIter->next();
  18959. processed++;
  18960. return out.getClear();
  18961. }
  18962. ReleaseClearRoxieRow(in);
  18963. in = inputStream->nextRow();
  18964. if (!in)
  18965. {
  18966. if (processed == numProcessedLastGroup)
  18967. in = inputStream->nextRow();
  18968. if (!in)
  18969. {
  18970. processed = numProcessedLastGroup;
  18971. return NULL;
  18972. }
  18973. in = inputStream->nextRow();
  18974. if (!in)
  18975. return NULL;
  18976. }
  18977. processRecord(in);
  18978. rowIter->first();
  18979. }
  18980. }
  18981. virtual unsigned onMatch(ARowBuilder & self, const void * curRecord, IMatchedResults * results, IMatchWalker * walker)
  18982. {
  18983. try
  18984. {
  18985. return helper.transform(self, curRecord, results, walker);
  18986. }
  18987. catch (IException *E)
  18988. {
  18989. throw parent.makeWrappedException(E);
  18990. }
  18991. }
  18992. };
  18993. public:
  18994. CRoxieServerStrandedParseActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, INlpParseAlgorithm * _algorithm, const StrandOptions &_strandOptions)
  18995. : CRoxieServerStrandedActivity(_ctx, _factory, _probeManager, _strandOptions), algorithm(_algorithm)
  18996. {
  18997. }
  18998. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  18999. {
  19000. CRoxieServerStrandedActivity::start(parentExtractSize, parentExtract, paused);
  19001. onStartStrands();
  19002. }
  19003. virtual StrandProcessor *createStrandProcessor(IEngineRowStream *instream)
  19004. {
  19005. return new ParseProcessor(*this, instream, (IHThorParseArg &) basehelper, algorithm);
  19006. }
  19007. virtual StrandProcessor *createStrandSourceProcessor(bool inputOrdered) { throwUnexpected(); }
  19008. };
  19009. class CRoxieServerParseActivityFactory : public CRoxieServerActivityFactory
  19010. {
  19011. Owned<INlpParseAlgorithm> algorithm;
  19012. Owned<IHThorParseArg> helper;
  19013. StrandOptions strandOptions;
  19014. public:
  19015. CRoxieServerParseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, IResourceContext *rc)
  19016. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), strandOptions(_graphNode)
  19017. {
  19018. helper.setown((IHThorParseArg *) helperFactory());
  19019. algorithm.setown(createThorParser(rc, *helper));
  19020. optStableInput = false;
  19021. }
  19022. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  19023. {
  19024. return new CRoxieServerStrandedParseActivity(_ctx, this, _probeManager, algorithm, strandOptions);
  19025. }
  19026. };
  19027. IRoxieServerActivityFactory *createRoxieServerParseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, IResourceContext *rc)
  19028. {
  19029. return new CRoxieServerParseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, rc);
  19030. }
  19031. //=====================================================================================================
  19032. class CRoxieServerWorkUnitWriteActivity : public CRoxieServerInternalSinkActivity
  19033. {
  19034. IHThorWorkUnitWriteArg &helper;
  19035. bool isReread;
  19036. bool grouped;
  19037. IRoxieServerContext *serverContext;
  19038. int sequence;
  19039. public:
  19040. CRoxieServerWorkUnitWriteActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _isReread, unsigned _numOutputs)
  19041. : CRoxieServerInternalSinkActivity(_ctx, _factory, _probeManager, _numOutputs), helper((IHThorWorkUnitWriteArg &)basehelper), isReread(_isReread)
  19042. {
  19043. grouped = (helper.getFlags() & POFgrouped) != 0;
  19044. serverContext = ctx->queryServerContext();
  19045. if (!serverContext)
  19046. {
  19047. throw MakeStringException(ROXIE_PIPE_ERROR, "Workunit output activity cannot be executed in agent context");
  19048. }
  19049. sequence = helper.getSequence();
  19050. if (sequence >= LibraryBaseSequence)
  19051. {
  19052. IConstWorkUnit *workunit = serverContext->queryQueryFactory()->queryWorkUnit();
  19053. assertex(workunit);
  19054. const char *storedName = helper.queryName();
  19055. assertex(storedName);
  19056. Owned<IConstWUResult> queryRes = workunit->getQueryResultByName(storedName);
  19057. if (!queryRes)
  19058. throw makeStringExceptionV(0, "Library cannot write to result %s that does not exist in calling query", storedName);
  19059. Owned<IConstWUResult> libraryRes = factory->queryQueryFactory().queryWorkUnit()->getResultBySequence(sequence);
  19060. if (libraryRes) // Should always be present, but rather than assert, just ignore if not
  19061. {
  19062. SCMStringBuffer queryFormat;
  19063. SCMStringBuffer libraryFormat;
  19064. queryRes->getResultEclSchema(queryFormat);
  19065. libraryRes->getResultEclSchema(libraryFormat);
  19066. if (!streq(queryFormat.str(), libraryFormat.str()))
  19067. {
  19068. DBGLOG("Query format: %s", queryFormat.str());
  19069. DBGLOG("Library format: %s", libraryFormat.str());
  19070. throw makeStringExceptionV(0, "Library cannot write to result %s: result type in query does not match", storedName);
  19071. }
  19072. }
  19073. sequence = queryRes->getResultSequence();
  19074. }
  19075. }
  19076. virtual bool needsAllocator() const { return true; }
  19077. virtual void onExecute()
  19078. {
  19079. MemoryBuffer result;
  19080. bool saveInContext = (int) sequence < 0 || isReread;
  19081. if (!meta.queryOriginal()) // this is a bit of a hack - don't know why no meta on an output....
  19082. meta.set(input->queryOutputMeta());
  19083. Owned<IOutputRowSerializer> rowSerializer;
  19084. Owned<IXmlWriter> xmlwriter;
  19085. bool appendRaw = false;
  19086. IHpccProtocolResultsWriter *results = NULL;
  19087. IHpccNativeProtocolResultsWriter *nativeResults = NULL;
  19088. IHpccProtocolResponse *protocol = serverContext->queryProtocol();
  19089. unsigned protocolFlags = (protocol) ? protocol->getFlags() : 0;
  19090. if ((int) sequence >= 0)
  19091. {
  19092. results = (protocol) ? protocol->queryHpccResultsSection() : NULL;
  19093. if (results)
  19094. {
  19095. nativeResults = dynamic_cast<IHpccNativeProtocolResultsWriter*>(results);
  19096. xmlwriter.setown(results->addDataset(helper.queryName(), sequence, "Dataset", appendRaw, serverContext->getXmlFlags(), (helper.getFlags() & POFextend) != 0, serverContext->queryXmlns(sequence))); //xmlwriter only returned if needed
  19097. }
  19098. }
  19099. size32_t outputLimitBytes = 0;
  19100. IConstWorkUnit *workunit = sequence == ResultSequenceInternal ? nullptr : serverContext->queryWorkUnit();
  19101. if (workunit)
  19102. {
  19103. size32_t outputLimit;
  19104. if (helper.getFlags() & POFmaxsize)
  19105. outputLimit = helper.getMaxSize();
  19106. else
  19107. {
  19108. // In absense of OPT_OUTPUTLIMIT check pre 5.2 legacy name OPT_OUTPUTLIMIT_LEGACY
  19109. outputLimit = workunit->getDebugValueInt(OPT_OUTPUTLIMIT, workunit->getDebugValueInt(OPT_OUTPUTLIMIT_LEGACY, defaultDaliResultLimit));
  19110. }
  19111. if (outputLimit>daliResultOutputMax)
  19112. 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.", daliResultOutputMax, defaultDaliResultLimit);
  19113. assertex(outputLimit<=0x1000); // 32bit limit because MemoryBuffer/CMessageBuffers involved etc.
  19114. outputLimitBytes = outputLimit * 0x100000;
  19115. }
  19116. if (workunit != NULL || (results && (protocol->getFlags() & HPCC_PROTOCOL_NATIVE_RAW)))
  19117. {
  19118. ensureRowAllocator();
  19119. rowSerializer.setown(rowAllocator->createDiskSerializer(ctx->queryCodeContext()));
  19120. }
  19121. __int64 initialProcessed = processed;
  19122. RtlLinkedDatasetBuilder builder(rowAllocator);
  19123. for (;;)
  19124. {
  19125. const void *row = inputStream->nextRow();
  19126. if (saveInContext)
  19127. {
  19128. if (row || grouped)
  19129. builder.append(row);
  19130. }
  19131. if (grouped && (processed != initialProcessed))
  19132. {
  19133. if (workunit)
  19134. result.append(row == NULL);
  19135. if (results)
  19136. {
  19137. if ((protocolFlags & HPCC_PROTOCOL_NATIVE_RAW) && nativeResults)
  19138. {
  19139. char val = (char)(row == NULL);
  19140. nativeResults->appendRaw(sequence, 1, &val);
  19141. }
  19142. else if (xmlwriter)
  19143. {
  19144. xmlwriter->outputBeginNested("Row", false);
  19145. xmlwriter->outputCString("1", "@__GroupBreak__");
  19146. xmlwriter->outputEndNested("Row");
  19147. }
  19148. }
  19149. }
  19150. if (!row)
  19151. {
  19152. row = inputStream->nextRow();
  19153. if (!row)
  19154. break;
  19155. if (saveInContext)
  19156. builder.append(row);
  19157. }
  19158. processed++;
  19159. if (workunit)
  19160. {
  19161. CThorDemoRowSerializer serializerTarget(result);
  19162. rowSerializer->serialize(serializerTarget, (const byte *) row);
  19163. }
  19164. if ((int) sequence >= 0)
  19165. {
  19166. if (appendRaw && nativeResults)
  19167. {
  19168. // MORE - should be able to serialize straight to the response...
  19169. MemoryBuffer rowbuff;
  19170. CThorDemoRowSerializer serializerTarget(rowbuff);
  19171. rowSerializer->serialize(serializerTarget, (const byte *) row);
  19172. nativeResults->appendRawRow(sequence, rowbuff.length(), rowbuff.toByteArray());
  19173. }
  19174. else if (xmlwriter)
  19175. {
  19176. xmlwriter->outputBeginNested(DEFAULTXMLROWTAG, false);
  19177. helper.serializeXml((byte *) row, *xmlwriter);
  19178. xmlwriter->outputEndNested(DEFAULTXMLROWTAG);
  19179. results->finalizeXmlRow(sequence);
  19180. }
  19181. else if (nativeResults)
  19182. {
  19183. SimpleOutputWriter x;
  19184. helper.serializeXml((byte *) row, x);
  19185. x.newline();
  19186. nativeResults->appendSimpleRow(sequence, x.str());
  19187. }
  19188. }
  19189. ReleaseRoxieRow(row);
  19190. if (outputLimitBytes && result.length() > outputLimitBytes)
  19191. {
  19192. StringBuffer errMsg("Dataset too large to output to workunit (limit ");
  19193. errMsg.append(outputLimitBytes/0x100000).append(" megabytes), in result (");
  19194. const char *name = helper.queryName();
  19195. if (name)
  19196. errMsg.append("name=").append(name);
  19197. else
  19198. errMsg.append("sequence=").append(helper.getSequence());
  19199. errMsg.append(")");
  19200. throw MakeStringExceptionDirect(0, errMsg.str());
  19201. }
  19202. }
  19203. if (xmlwriter)
  19204. xmlwriter->outputEndArray(DEFAULTXMLROWTAG);
  19205. const char *storedName = helper.queryName();
  19206. if (!storedName)
  19207. storedName = "Dataset";
  19208. if (saveInContext)
  19209. serverContext->appendResultDeserialized(storedName, sequence, builder.getcount(), builder.linkrows(), (helper.getFlags() & POFextend) != 0, LINK(meta.queryOriginal()));
  19210. if (workunit)
  19211. serverContext->appendResultRawContext(storedName, sequence, result.length(), result.toByteArray(), processed, (helper.getFlags() & POFextend) != 0, false); // MORE - shame to do extra copy...
  19212. }
  19213. };
  19214. class CRoxieServerWorkUnitWriteActivityFactory : public CRoxieServerInternalSinkFactory
  19215. {
  19216. bool isReread;
  19217. bool isUnused = false;
  19218. public:
  19219. CRoxieServerWorkUnitWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, bool _isRoot)
  19220. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _isRoot)
  19221. {
  19222. isReread = usageCount > 0;
  19223. Owned<IHThorWorkUnitWriteArg> helper = (IHThorWorkUnitWriteArg *) helperFactory();
  19224. isInternal = (helper->getSequence()==ResultSequenceInternal);
  19225. isUnused = isInternal && (usageCount == 0);
  19226. }
  19227. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  19228. {
  19229. if (isUnused && !CRoxieServerInternalSinkFactory::isSink())
  19230. {
  19231. if (_ctx->queryTraceLevel() > 2)
  19232. DBGLOG("Workunit write %u is unused - create null activity", id);
  19233. //Create a null sink activity that is always executed to ensure that stop() is called on the input.
  19234. return createRoxieServerNullSinkActivity(_ctx, this, _probeManager);
  19235. }
  19236. return new CRoxieServerWorkUnitWriteActivity(_ctx, this, _probeManager, isReread, usageCount);
  19237. }
  19238. virtual bool isSink() const
  19239. {
  19240. return isUnused || CRoxieServerInternalSinkFactory::isSink();
  19241. }
  19242. };
  19243. IRoxieServerActivityFactory *createRoxieServerWorkUnitWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, bool _isRoot)
  19244. {
  19245. return new CRoxieServerWorkUnitWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _isRoot);
  19246. }
  19247. //=====================================================================================================
  19248. class CRoxieServerWorkUnitWriteDictActivity : public CRoxieServerInternalSinkActivity
  19249. {
  19250. IHThorDictionaryWorkUnitWriteArg &helper;
  19251. IRoxieServerContext *serverContext;
  19252. public:
  19253. CRoxieServerWorkUnitWriteDictActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _usageCount)
  19254. : CRoxieServerInternalSinkActivity(_ctx, _factory, _probeManager, _usageCount), helper((IHThorDictionaryWorkUnitWriteArg &)basehelper)
  19255. {
  19256. serverContext = NULL;
  19257. }
  19258. virtual void onCreate(IHThorArg *_colocalParent)
  19259. {
  19260. CRoxieServerInternalSinkActivity::onCreate(_colocalParent);
  19261. serverContext = ctx->queryServerContext();
  19262. if (!serverContext)
  19263. {
  19264. throw MakeStringException(ROXIE_PIPE_ERROR, "Write Dictionary activity cannot be executed in agent context");
  19265. }
  19266. }
  19267. virtual bool needsAllocator() const { return true; }
  19268. virtual void onExecute()
  19269. {
  19270. int sequence = helper.getSequence();
  19271. const char *storedName = helper.queryName();
  19272. assertex(storedName && *storedName);
  19273. assertex(sequence < 0);
  19274. RtlLinkedDictionaryBuilder builder(rowAllocator, helper.queryHashLookupInfo());
  19275. for (;;)
  19276. {
  19277. const void *row = inputStream->ungroupedNextRow();
  19278. if (!row)
  19279. break;
  19280. builder.appendOwn(row);
  19281. processed++;
  19282. }
  19283. serverContext->appendResultDeserialized(storedName, sequence, builder.getcount(), builder.linkrows(), (helper.getFlags() & POFextend) != 0, LINK(meta.queryOriginal()));
  19284. }
  19285. };
  19286. class CRoxieServerWorkUnitWriteDictActivityFactory : public CRoxieServerInternalSinkFactory
  19287. {
  19288. public:
  19289. CRoxieServerWorkUnitWriteDictActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, bool _isRoot)
  19290. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _isRoot)
  19291. {
  19292. Owned<IHThorDictionaryWorkUnitWriteArg> helper = (IHThorDictionaryWorkUnitWriteArg *) helperFactory();
  19293. isInternal = (helper->getSequence()==ResultSequenceInternal);
  19294. }
  19295. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  19296. {
  19297. return new CRoxieServerWorkUnitWriteDictActivity(_ctx, this, _probeManager, usageCount);
  19298. }
  19299. };
  19300. IRoxieServerActivityFactory *createRoxieServerWorkUnitWriteDictActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, bool _isRoot)
  19301. {
  19302. return new CRoxieServerWorkUnitWriteDictActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _isRoot);
  19303. }
  19304. //=================================================================================
  19305. class CRoxieServerRemoteResultActivity : public CRoxieServerInternalSinkActivity
  19306. {
  19307. IHThorRemoteResultArg &helper;
  19308. public:
  19309. CRoxieServerRemoteResultActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numOutputs)
  19310. : CRoxieServerInternalSinkActivity(_ctx, _factory, _probeManager, _numOutputs), helper((IHThorRemoteResultArg &)basehelper)
  19311. {
  19312. }
  19313. virtual void onExecute()
  19314. {
  19315. OwnedConstRoxieRow row = inputStream->nextRow();
  19316. helper.sendResult(row); // should be only one row or something has gone wrong!
  19317. }
  19318. };
  19319. class CRoxieServerRemoteResultActivityFactory : public CRoxieServerInternalSinkFactory
  19320. {
  19321. public:
  19322. CRoxieServerRemoteResultActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, bool _isRoot)
  19323. : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _isRoot)
  19324. {
  19325. Owned<IHThorRemoteResultArg> helper = (IHThorRemoteResultArg *) helperFactory();
  19326. isInternal = (helper->getSequence()==ResultSequenceInternal);
  19327. }
  19328. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  19329. {
  19330. if (dependentCount==0 && !CRoxieServerInternalSinkFactory::isSink())
  19331. return new CRoxieServerNullSinkActivity(_ctx, this, _probeManager);
  19332. else
  19333. return new CRoxieServerRemoteResultActivity(_ctx, this, _probeManager, usageCount);
  19334. }
  19335. virtual bool isSink() const override
  19336. {
  19337. return CRoxieServerInternalSinkFactory::isSink() || dependentCount == 0; // Codegen normally optimizes these away, but if it doesn't we need to treat as a (null) sink rather than a dependency or upstream activities are not stopped properly
  19338. }
  19339. };
  19340. IRoxieServerActivityFactory *createRoxieServerRemoteResultActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, unsigned _usageCount, bool _isRoot)
  19341. {
  19342. return new CRoxieServerRemoteResultActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _usageCount, _isRoot);
  19343. }
  19344. //=================================================================================
  19345. class CRoxieServerXmlParseActivity : public CRoxieServerActivity, implements IXMLSelect
  19346. {
  19347. IHThorXmlParseArg &helper;
  19348. Owned<IXMLParse> xmlParser;
  19349. const void * in;
  19350. char * srchStr;
  19351. unsigned numProcessedLastGroup;
  19352. bool srchStrNeedsFree;
  19353. Owned<IColumnProvider> lastMatch;
  19354. public:
  19355. IMPLEMENT_IINTERFACE_USING(CRoxieServerActivity)
  19356. CRoxieServerXmlParseActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  19357. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  19358. helper((IHThorXmlParseArg &)basehelper)
  19359. {
  19360. srchStrNeedsFree = helper.searchTextNeedsFree();
  19361. numProcessedLastGroup = 0;
  19362. srchStr = NULL;
  19363. in = NULL;
  19364. }
  19365. ~CRoxieServerXmlParseActivity()
  19366. {
  19367. }
  19368. virtual bool needsAllocator() const { return true; }
  19369. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  19370. {
  19371. numProcessedLastGroup = 0;
  19372. srchStr = NULL;
  19373. in = NULL;
  19374. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  19375. }
  19376. virtual void reset()
  19377. {
  19378. if (helper.searchTextNeedsFree())
  19379. rtlFree(srchStr);
  19380. srchStr = NULL;
  19381. ReleaseClearRoxieRow(in);
  19382. xmlParser.clear();
  19383. CRoxieServerActivity::reset();
  19384. }
  19385. virtual void match(IColumnProvider &entry, offset_t startOffset, offset_t endOffset)
  19386. {
  19387. lastMatch.set(&entry);
  19388. }
  19389. virtual const void *nextRow()
  19390. {
  19391. ActivityTimer t(activityStats, timeActivities);
  19392. for (;;)
  19393. {
  19394. if(xmlParser)
  19395. {
  19396. for (;;)
  19397. {
  19398. if(!xmlParser->next())
  19399. {
  19400. if (srchStrNeedsFree)
  19401. {
  19402. rtlFree(srchStr);
  19403. srchStr = NULL;
  19404. }
  19405. xmlParser.clear();
  19406. break;
  19407. }
  19408. if(lastMatch)
  19409. {
  19410. try
  19411. {
  19412. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  19413. size32_t outSize = helper.transform(rowBuilder, in, lastMatch);
  19414. lastMatch.clear();
  19415. if (outSize)
  19416. {
  19417. processed++;
  19418. return rowBuilder.finalizeRowClear(outSize);
  19419. }
  19420. }
  19421. catch (IException *E)
  19422. {
  19423. throw makeWrappedException(E);
  19424. }
  19425. }
  19426. }
  19427. }
  19428. ReleaseClearRoxieRow(in);
  19429. in = inputStream->nextRow();
  19430. if(!in)
  19431. {
  19432. if(numProcessedLastGroup == processed)
  19433. in = inputStream->nextRow();
  19434. if(!in)
  19435. {
  19436. numProcessedLastGroup = processed;
  19437. return NULL;
  19438. }
  19439. }
  19440. size32_t srchLen;
  19441. helper.getSearchText(srchLen, srchStr, in);
  19442. OwnedRoxieString xmlIteratorPath(helper.getXmlIteratorPath());
  19443. xmlParser.setown(createXMLParse(srchStr, srchLen, xmlIteratorPath, *this));
  19444. }
  19445. }
  19446. };
  19447. class CRoxieServerXmlParseActivityFactory : public CRoxieServerActivityFactory
  19448. {
  19449. public:
  19450. CRoxieServerXmlParseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  19451. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  19452. {
  19453. }
  19454. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  19455. {
  19456. return new CRoxieServerXmlParseActivity(_ctx, this, _probeManager);
  19457. }
  19458. };
  19459. IRoxieServerActivityFactory *createRoxieServerXmlParseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  19460. {
  19461. return new CRoxieServerXmlParseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  19462. }
  19463. //=====================================================================================================
  19464. class CRoxieServerDiskReadBaseActivity : public CRoxieServerActivity, implements IRoxieServerErrorHandler, implements IIndexReadContext
  19465. {
  19466. protected:
  19467. IHThorDiskReadBaseArg &helper;
  19468. IHThorSourceLimitTransformExtra * limitTransformExtra = nullptr;
  19469. IHThorCompoundExtra * compoundHelper;
  19470. RemoteActivityId remoteId; // Note we copy it rather than reference
  19471. Owned<CSkippableRemoteResultAdaptor> remote;
  19472. unsigned numParts;
  19473. unsigned __int64 rowLimit;
  19474. unsigned __int64 stopAfter;
  19475. Linked<IInMemoryIndexManager> manager;
  19476. Linked<ITranslatorSet> translators;
  19477. ScoredRowFilter postFilter;
  19478. Owned<IDirectReader> reader;
  19479. bool eof;
  19480. bool variableFileName;
  19481. bool isOpt;
  19482. bool sorted;
  19483. bool maySkip;
  19484. bool isLocal;
  19485. bool forceRemote;
  19486. bool isGrouped = false;
  19487. CachedOutputMetaData diskSize;
  19488. Owned<const IResolvedFile> varFileInfo;
  19489. inline bool useRemote()
  19490. {
  19491. return remote != NULL && (forceRemote || numParts > 1);
  19492. }
  19493. public:
  19494. IMPLEMENT_IINTERFACE_USING(CRoxieServerActivity)
  19495. CRoxieServerDiskReadBaseActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  19496. unsigned _numParts, bool _isLocal, bool _sorted, bool _maySkip, IInMemoryIndexManager *_manager, ITranslatorSet *_translators)
  19497. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  19498. helper((IHThorDiskReadBaseArg &)basehelper),
  19499. remoteId(_remoteId),
  19500. numParts(_numParts),
  19501. manager(_manager),
  19502. translators(_translators),
  19503. sorted(_sorted),
  19504. maySkip(_maySkip),
  19505. isLocal(_isLocal)
  19506. {
  19507. forceRemote = factory->queryQueryFactory().queryOptions().disableLocalOptimizations;
  19508. if ((forceRemote || numParts != 1) && !isLocal) // NOTE : when numParts == 0 (variable case) we create, even though we may not use
  19509. remote.setown(new CSkippableRemoteResultAdaptor(_ctx, this, remoteId, meta.queryOriginal(), helper, *this, sorted, false, _maySkip));
  19510. compoundHelper = NULL;
  19511. eof = false;
  19512. rowLimit = (unsigned __int64) -1;
  19513. stopAfter = I64C(0x7FFFFFFFFFFFFFFF);
  19514. diskSize.set(helper.queryDiskRecordSize()->querySerializedDiskMeta());
  19515. isGrouped = diskSize.isGrouped();
  19516. variableFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((helper.getFlags() & (TDXvarfilename|TDXdynamicfilename)) != 0);
  19517. isOpt = (helper.getFlags() & TDRoptional) != 0;
  19518. }
  19519. virtual const IResolvedFile *queryVarFileInfo() const
  19520. {
  19521. return varFileInfo;
  19522. }
  19523. virtual void onCreate(IHThorArg *_colocalParent)
  19524. {
  19525. CRoxieServerActivity::onCreate(_colocalParent);
  19526. if (remote)
  19527. remote->onCreate(_colocalParent);
  19528. }
  19529. virtual bool needsAllocator() const { return true; }
  19530. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  19531. {
  19532. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  19533. if (compoundHelper)
  19534. {
  19535. rowLimit = compoundHelper->getRowLimit();
  19536. stopAfter = compoundHelper->getChooseNLimit();
  19537. }
  19538. if (!helper.canMatchAny())
  19539. eof = true;
  19540. else
  19541. {
  19542. if (variableFileName)
  19543. {
  19544. OwnedRoxieString fileName(helper.getFileName());
  19545. varFileInfo.setown(resolveLFNFlat(fileName, isOpt, factory->isActivityCodeSigned()));
  19546. numParts = 0;
  19547. if (varFileInfo)
  19548. numParts = varFileInfo->getNumParts();
  19549. }
  19550. if (!numParts)
  19551. {
  19552. eof = true;
  19553. }
  19554. else if (useRemote())
  19555. {
  19556. remote->onStart(parentExtractSize, parentExtract);
  19557. remote->setLimits(rowLimit, (unsigned __int64) -1, stopAfter);
  19558. unsigned fileNo = 0;
  19559. // Translation into a message per channel done elsewhere....
  19560. remote->getMem(0, fileNo, 0);
  19561. remote->flush();
  19562. remote->senddone();
  19563. }
  19564. else
  19565. {
  19566. if (variableFileName)
  19567. {
  19568. unsigned channel = isLocal ? factory->queryQueryFactory().queryChannel() : 0;
  19569. unsigned expectedFormatCrc = helper.getDiskFormatCrc();
  19570. unsigned projectedFormatCrc = helper.getProjectedFormatCrc();
  19571. translators.setown(varFileInfo->getTranslators(projectedFormatCrc, helper.queryProjectedDiskRecordSize(), expectedFormatCrc, helper.queryDiskRecordSize(), getEnableFieldTranslation(), FileFormatMode::flat, queryFactory()->queryQueryFactory().queryQueryName()));
  19572. manager.setown(varFileInfo->getIndexManager(isOpt, channel, translators->queryActualLayout(0), false));
  19573. }
  19574. assertex(manager != NULL);
  19575. postFilter.clear();
  19576. helper.createSegmentMonitors(this);
  19577. const IKeyTranslator *keyTranslator = translators->queryKeyTranslator(0); // any part would do - in-memory requires all actuals to have same layout
  19578. if (keyTranslator)
  19579. keyTranslator->translate(postFilter);
  19580. reader.setown(manager->selectKey(postFilter, translators, *this));
  19581. if (!reader)
  19582. reader.setown(manager->createReader(postFilter, isGrouped, 0, 0, 1, translators));
  19583. assertex(reader);
  19584. helper.setCallback(reader);
  19585. }
  19586. }
  19587. }
  19588. virtual void append(IKeySegmentMonitor *segment)
  19589. {
  19590. segment->Release();
  19591. throwUnexpected();
  19592. }
  19593. virtual void append(FFoption option, const IFieldFilter * filter)
  19594. {
  19595. if (filter->isWild())
  19596. filter->Release();
  19597. else
  19598. postFilter.addFilter(*filter);
  19599. }
  19600. virtual void stop()
  19601. {
  19602. if (useRemote())
  19603. remote->onStop();
  19604. CRoxieServerActivity::stop();
  19605. }
  19606. virtual void reset()
  19607. {
  19608. if (useRemote())
  19609. {
  19610. processed = remote->processed;
  19611. remote->processed = 0;
  19612. remote->onReset();
  19613. }
  19614. varFileInfo.clear();
  19615. eof = false;
  19616. reader.clear();
  19617. CRoxieServerActivity::reset();
  19618. }
  19619. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  19620. {
  19621. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  19622. }
  19623. virtual void onLimitExceeded(bool isKeyed)
  19624. {
  19625. if (traceLevel > 4)
  19626. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  19627. assertex(compoundHelper);
  19628. if (isKeyed) // MORE does this exist for diskread? should it?
  19629. {
  19630. if (helper.getFlags() & (TDRkeyedlimitskips|TDRkeyedlimitcreates))
  19631. {
  19632. if (ctx->queryDebugContext())
  19633. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  19634. throw makeLimitSkipException(true);
  19635. }
  19636. else
  19637. {
  19638. UNIMPLEMENTED;
  19639. //compoundHelper->onKeyedLimitExceeded(); Doesn't exist - though the flags do... interesting...
  19640. }
  19641. }
  19642. else
  19643. {
  19644. if (helper.getFlags() & (TDRlimitskips|TDRlimitcreates))
  19645. {
  19646. if (ctx->queryDebugContext())
  19647. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  19648. throw makeLimitSkipException(false);
  19649. }
  19650. else
  19651. compoundHelper->onLimitExceeded();
  19652. }
  19653. }
  19654. virtual const void * createLimitFailRow(bool isKeyed)
  19655. {
  19656. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  19657. assertex(limitTransformExtra);
  19658. size32_t outSize = isKeyed ? limitTransformExtra->transformOnKeyedLimitExceeded(rowBuilder) : limitTransformExtra->transformOnLimitExceeded(rowBuilder);
  19659. if (outSize)
  19660. return rowBuilder.finalizeRowClear(outSize);
  19661. return NULL;
  19662. }
  19663. virtual void gatherStats(CRuntimeStatisticCollection & merged) const override
  19664. {
  19665. CRoxieServerActivity::gatherStats(merged);
  19666. if (remote)
  19667. remote->gatherStats(merged);
  19668. }
  19669. };
  19670. class CRoxieServerDiskReadActivity : public CRoxieServerDiskReadBaseActivity
  19671. {
  19672. IHThorDiskReadArg * readHelper;
  19673. ConstPointerArray readrows;
  19674. bool readAheadDone;
  19675. unsigned readIndex;
  19676. unsigned lastGroupProcessed;
  19677. bool someInGroup = false;
  19678. public:
  19679. CRoxieServerDiskReadActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, bool _sorted, bool _maySkip, IInMemoryIndexManager *_manager, ITranslatorSet *_translators)
  19680. : CRoxieServerDiskReadBaseActivity(_ctx, _factory, _probeManager, _remoteId, _numParts, _isLocal, _sorted, _maySkip, _manager, _translators)
  19681. {
  19682. compoundHelper = (IHThorDiskReadArg *)&helper;
  19683. readHelper = (IHThorDiskReadArg *)&helper;
  19684. limitTransformExtra = readHelper;
  19685. readAheadDone = false;
  19686. readIndex = 0;
  19687. lastGroupProcessed = processed;
  19688. }
  19689. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  19690. {
  19691. rowLimit = compoundHelper->getRowLimit();
  19692. stopAfter = compoundHelper->getChooseNLimit();
  19693. CRoxieServerDiskReadBaseActivity::start(parentExtractSize, parentExtract, paused);
  19694. readAheadDone = false;
  19695. readIndex = 0;
  19696. lastGroupProcessed = processed;
  19697. someInGroup = false;
  19698. }
  19699. virtual void reset()
  19700. {
  19701. roxiemem::ReleaseRoxieRowRange(readrows.getArray(), readIndex, readrows.ordinality());
  19702. readrows.kill();
  19703. readAheadDone = false;
  19704. readIndex = 0;
  19705. lastGroupProcessed = processed;
  19706. someInGroup = false;
  19707. CRoxieServerDiskReadBaseActivity::reset();
  19708. }
  19709. virtual const void *nextRow()
  19710. {
  19711. ActivityTimer t(activityStats, timeActivities);
  19712. if (eof)
  19713. return NULL;
  19714. else if (useRemote())
  19715. return remote->nextRow();
  19716. else if (maySkip)
  19717. {
  19718. if (!readAheadDone)
  19719. {
  19720. unsigned preprocessed = 0;
  19721. while (!eof)
  19722. {
  19723. const void *row = _nextRow();
  19724. if (row)
  19725. preprocessed++;
  19726. if (preprocessed > rowLimit)
  19727. {
  19728. ReleaseRoxieRow(row);
  19729. roxiemem::ReleaseRoxieRowRange(readrows.getArray(), readIndex, readrows.ordinality());
  19730. readrows.kill();
  19731. readIndex = 0;
  19732. eof = true;
  19733. if (ctx->queryDebugContext())
  19734. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  19735. if (helper.getFlags() & TDRlimitskips)
  19736. return NULL;
  19737. else if (helper.getFlags() & TDRlimitcreates)
  19738. return createLimitFailRow(false);
  19739. else
  19740. throwUnexpected();
  19741. }
  19742. if (preprocessed > stopAfter) // MORE - bit of a strange place to check
  19743. {
  19744. eof = true;
  19745. ReleaseRoxieRow(row);
  19746. break;
  19747. }
  19748. readrows.append(row);
  19749. }
  19750. readAheadDone = true;
  19751. }
  19752. if (readrows.isItem(readIndex))
  19753. {
  19754. const void *ret = readrows.item(readIndex++);
  19755. if (ret)
  19756. processed++;
  19757. return ret;
  19758. }
  19759. else
  19760. {
  19761. eof = true;
  19762. return NULL;
  19763. }
  19764. }
  19765. else
  19766. {
  19767. const void *ret = _nextRow();
  19768. if (ret)
  19769. {
  19770. processed++;
  19771. if (processed > rowLimit)
  19772. {
  19773. if (traceLevel > 4)
  19774. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  19775. ReleaseRoxieRow(ret);
  19776. compoundHelper->onLimitExceeded();
  19777. throwUnexpected(); // onLimitExceeded is not supposed to return
  19778. }
  19779. if (processed > stopAfter) // MORE - bit of a strange place to check
  19780. {
  19781. eof = true;
  19782. ReleaseRoxieRow(ret);
  19783. return NULL;
  19784. }
  19785. }
  19786. return ret;
  19787. }
  19788. }
  19789. const void *_nextRow()
  19790. {
  19791. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  19792. unsigned transformedSize = 0;
  19793. for (;;)
  19794. {
  19795. const byte *nextRec = reader->nextRow();
  19796. if (nextRec)
  19797. {
  19798. someInGroup = true;
  19799. if (likely(readHelper->canMatch(nextRec)))
  19800. {
  19801. transformedSize = readHelper->transform(rowBuilder, nextRec);
  19802. reader->finishedRow();
  19803. if (transformedSize)
  19804. break;
  19805. }
  19806. else
  19807. reader->finishedRow();
  19808. }
  19809. else if (isGrouped)
  19810. {
  19811. if (lastGroupProcessed != processed)
  19812. {
  19813. someInGroup = false;
  19814. lastGroupProcessed = processed;
  19815. return nullptr;
  19816. }
  19817. else if (!someInGroup)
  19818. {
  19819. eof = true;
  19820. return nullptr;
  19821. }
  19822. }
  19823. else
  19824. {
  19825. eof = true;
  19826. return nullptr;
  19827. }
  19828. }
  19829. return rowBuilder.finalizeRowClear(transformedSize);
  19830. }
  19831. };
  19832. class CRoxieServerXmlReadActivity : public CRoxieServerDiskReadBaseActivity, implements IXMLSelect, implements IThorDiskCallback
  19833. {
  19834. IHThorXmlReadArg * readHelper;
  19835. Owned<IXmlToRowTransformer> rowTransformer;
  19836. Owned<IXMLParse> xmlParser;
  19837. Owned<IColumnProvider> lastMatch;
  19838. unsigned __int64 fileoffset;
  19839. IDirectStreamReader *streamReader = nullptr;
  19840. public:
  19841. IMPLEMENT_IINTERFACE_USING(CRoxieServerDiskReadBaseActivity)
  19842. CRoxieServerXmlReadActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  19843. unsigned _numParts, bool _isLocal, bool _sorted, bool _maySkip, IInMemoryIndexManager *_manager, ITranslatorSet *_translators)
  19844. : CRoxieServerDiskReadBaseActivity(_ctx, _factory, _probeManager, _remoteId, _numParts, _isLocal, _sorted, _maySkip, _manager, _translators)
  19845. {
  19846. compoundHelper = NULL;
  19847. readHelper = (IHThorXmlReadArg *)&helper;
  19848. fileoffset = 0;
  19849. }
  19850. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  19851. {
  19852. rowLimit = readHelper->getRowLimit();
  19853. stopAfter = readHelper->getChooseNLimit();
  19854. CRoxieServerDiskReadBaseActivity::start(parentExtractSize, parentExtract, paused);
  19855. if (!useRemote())
  19856. {
  19857. rowTransformer.set(readHelper->queryTransformer());
  19858. assertex(reader != NULL);
  19859. streamReader = reader->queryDirectStreamReader();
  19860. assertex(streamReader != NULL);
  19861. OwnedRoxieString xmlIterator(readHelper->getXmlIteratorPath());
  19862. if (factory->getKind()==TAKjsonread)
  19863. xmlParser.setown(createJSONParse(*streamReader, xmlIterator, *this, (0 != (TDRxmlnoroot & readHelper->getFlags()))?ptr_noRoot:ptr_none, (readHelper->getFlags() & TDRusexmlcontents) != 0));
  19864. else
  19865. xmlParser.setown(createXMLParse(*streamReader, xmlIterator, *this, (0 != (TDRxmlnoroot & readHelper->getFlags()))?ptr_noRoot:ptr_none, (readHelper->getFlags() & TDRusexmlcontents) != 0));
  19866. }
  19867. }
  19868. virtual void reset()
  19869. {
  19870. CRoxieServerDiskReadBaseActivity::reset();
  19871. rowTransformer.clear();
  19872. xmlParser.clear();
  19873. }
  19874. virtual void match(IColumnProvider &entry, offset_t startOffset, offset_t endOffset)
  19875. {
  19876. fileoffset = startOffset;
  19877. lastMatch.set(&entry);
  19878. }
  19879. //interface IThorDiskCallback
  19880. virtual unsigned __int64 getFilePosition(const void * row)
  19881. {
  19882. return fileoffset;
  19883. }
  19884. virtual unsigned __int64 getLocalFilePosition(const void * row)
  19885. {
  19886. return streamReader->makeFilePositionLocal(fileoffset);
  19887. }
  19888. virtual const char * queryLogicalFilename(const void * row)
  19889. {
  19890. return reader->queryLogicalFilename(row);
  19891. }
  19892. virtual const byte * lookupBlob(unsigned __int64 id) { UNIMPLEMENTED; }
  19893. virtual const void *nextRow()
  19894. {
  19895. ActivityTimer t(activityStats, timeActivities);
  19896. if (eof)
  19897. return NULL;
  19898. else if (useRemote())
  19899. return remote->nextRow();
  19900. assertex(xmlParser != NULL);
  19901. try
  19902. {
  19903. while (!eof)
  19904. {
  19905. //call to next() will callback on the IXmlSelect interface
  19906. bool gotNext = false;
  19907. gotNext = xmlParser->next();
  19908. if(!gotNext)
  19909. eof = true;
  19910. else if (lastMatch)
  19911. {
  19912. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  19913. unsigned sizeGot = rowTransformer->transform(rowBuilder, lastMatch, this);
  19914. lastMatch.clear();
  19915. fileoffset = 0;
  19916. if (sizeGot)
  19917. {
  19918. OwnedConstRoxieRow ret = rowBuilder.finalizeRowClear(sizeGot);
  19919. if (processed > rowLimit)
  19920. {
  19921. if (traceLevel > 4)
  19922. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  19923. readHelper->onLimitExceeded();
  19924. throwUnexpected(); // onLimitExceeded is not supposed to return
  19925. }
  19926. processed++;
  19927. if (processed > stopAfter) // MORE - bit of a strange place to check
  19928. {
  19929. eof = true;
  19930. return NULL;
  19931. }
  19932. return ret.getClear();
  19933. }
  19934. }
  19935. }
  19936. return NULL;
  19937. }
  19938. catch(IException *E)
  19939. {
  19940. throw makeWrappedException(E);
  19941. }
  19942. }
  19943. };
  19944. class CRoxieServerCsvReadActivity : public CRoxieServerDiskReadBaseActivity
  19945. {
  19946. IHThorCsvReadArg *readHelper;
  19947. ICsvParameters * csvInfo;
  19948. unsigned headerLines;
  19949. unsigned maxDiskSize;
  19950. CSVSplitter csvSplitter;
  19951. unsigned __int64 localOffset;
  19952. IDirectStreamReader *streamReader = nullptr;
  19953. const char *quotes;
  19954. const char *separators;
  19955. const char *terminators;
  19956. const char *escapes;
  19957. size32_t maxRowSize;
  19958. public:
  19959. CRoxieServerCsvReadActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  19960. unsigned _numParts, bool _isLocal, bool _sorted, bool _maySkip, IInMemoryIndexManager *_manager, ITranslatorSet *_translators,
  19961. const char *_quotes, const char *_separators, const char *_terminators, const char *_escapes, size32_t _maxRowSize)
  19962. : CRoxieServerDiskReadBaseActivity(_ctx, _factory, _probeManager, _remoteId, _numParts, _isLocal, _sorted, _maySkip, _manager, _translators),
  19963. quotes(_quotes), separators(_separators), terminators(_terminators), escapes(_escapes), maxRowSize(_maxRowSize)
  19964. {
  19965. compoundHelper = NULL;
  19966. readHelper = (IHThorCsvReadArg *)&helper;
  19967. rowLimit = readHelper->getRowLimit();
  19968. stopAfter = readHelper->getChooseNLimit();
  19969. csvInfo = readHelper->queryCsvParameters();
  19970. maxDiskSize = csvInfo->queryMaxSize();
  19971. localOffset = 0;
  19972. headerLines = 0;
  19973. }
  19974. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  19975. {
  19976. rowLimit = readHelper->getRowLimit();
  19977. stopAfter = readHelper->getChooseNLimit();
  19978. CRoxieServerDiskReadBaseActivity::start(parentExtractSize, parentExtract, paused);
  19979. if (!useRemote())
  19980. {
  19981. if (!eof)
  19982. {
  19983. assertex(reader != nullptr);
  19984. streamReader = reader->queryDirectStreamReader();
  19985. assertex(streamReader != nullptr);
  19986. headerLines = csvInfo->queryHeaderLen();
  19987. if (headerLines && isLocal && streamReader->queryFilePart() != 1)
  19988. 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...
  19989. if (varFileInfo)
  19990. {
  19991. const IPropertyTree *options = varFileInfo->queryProperties();
  19992. if (options)
  19993. {
  19994. quotes = options->queryProp("@csvQuote");
  19995. separators = options->queryProp("@csvSeparate");
  19996. terminators = options->queryProp("@csvTerminate");
  19997. escapes = options->queryProp("@csvEscape");
  19998. }
  19999. }
  20000. csvSplitter.init(readHelper->getMaxColumns(), csvInfo, quotes, separators, terminators, escapes);
  20001. }
  20002. }
  20003. }
  20004. virtual const void *nextRow()
  20005. {
  20006. ActivityTimer t(activityStats, timeActivities);
  20007. if (eof)
  20008. return NULL;
  20009. else if (useRemote())
  20010. return remote->nextRow();
  20011. try
  20012. {
  20013. while (!eof)
  20014. {
  20015. size32_t thisLineLength = csvSplitter.splitLine(streamReader, maxRowSize);
  20016. if (!thisLineLength)
  20017. break;
  20018. if (headerLines)
  20019. {
  20020. headerLines--;
  20021. streamReader->skip(thisLineLength);
  20022. }
  20023. else
  20024. {
  20025. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  20026. unsigned transformedSize = readHelper->transform(rowBuilder, csvSplitter.queryLengths(), (const char * *)csvSplitter.queryData());
  20027. streamReader->skip(thisLineLength);
  20028. if (transformedSize)
  20029. {
  20030. OwnedConstRoxieRow ret = rowBuilder.finalizeRowClear(transformedSize);
  20031. if (processed > rowLimit)
  20032. {
  20033. readHelper->onLimitExceeded();
  20034. throwUnexpected(); // onLimitExceeded is not supposed to return
  20035. }
  20036. processed++;
  20037. if (processed > stopAfter) // MORE - bit of a strange place to check
  20038. {
  20039. eof = true;
  20040. return NULL;
  20041. }
  20042. return ret.getClear();
  20043. }
  20044. }
  20045. }
  20046. return NULL;
  20047. }
  20048. catch(IException *E)
  20049. {
  20050. throw makeWrappedException(E);
  20051. }
  20052. }
  20053. virtual void reset()
  20054. {
  20055. CRoxieServerDiskReadBaseActivity::reset();
  20056. csvSplitter.reset();
  20057. localOffset = 0;
  20058. }
  20059. virtual unsigned __int64 getFilePosition(const void * row)
  20060. {
  20061. UNIMPLEMENTED; // we know offset in the reader but not sure it helps us much
  20062. }
  20063. virtual unsigned __int64 getLocalFilePosition(const void * row)
  20064. {
  20065. UNIMPLEMENTED;
  20066. }
  20067. };
  20068. class CRoxieServerDiskNormalizeActivity : public CRoxieServerDiskReadBaseActivity
  20069. {
  20070. IHThorDiskNormalizeArg *normalizeHelper;
  20071. bool firstPending;
  20072. public:
  20073. CRoxieServerDiskNormalizeActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, bool _sorted, IInMemoryIndexManager *_manager, ITranslatorSet *_translators)
  20074. : CRoxieServerDiskReadBaseActivity(_ctx, _factory, _probeManager, _remoteId, _numParts, _isLocal, _sorted, false, _manager, _translators)
  20075. {
  20076. compoundHelper = (IHThorDiskNormalizeArg *)&helper;
  20077. normalizeHelper = (IHThorDiskNormalizeArg *)&helper;
  20078. limitTransformExtra = normalizeHelper;
  20079. firstPending = true;
  20080. }
  20081. virtual void reset()
  20082. {
  20083. firstPending = true;
  20084. CRoxieServerDiskReadBaseActivity::reset();
  20085. }
  20086. virtual const void *nextRow()
  20087. {
  20088. ActivityTimer t(activityStats, timeActivities);
  20089. if (eof)
  20090. return NULL;
  20091. else if (useRemote())
  20092. return remote->nextRow();
  20093. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  20094. unsigned transformedSize = 0;
  20095. for (;;)
  20096. {
  20097. while (firstPending)
  20098. {
  20099. const byte *nextRec = reader->nextRow();
  20100. if (!nextRec)
  20101. {
  20102. eof = true;
  20103. return NULL;
  20104. }
  20105. if (normalizeHelper->first(nextRec))
  20106. firstPending = false;
  20107. reader->finishedRow();
  20108. }
  20109. transformedSize = normalizeHelper->transform(rowBuilder);
  20110. firstPending = !normalizeHelper->next();
  20111. if (transformedSize)
  20112. break;
  20113. }
  20114. OwnedConstRoxieRow recBuffer = rowBuilder.finalizeRowClear(transformedSize);
  20115. processed++;
  20116. if (processed > rowLimit)
  20117. {
  20118. compoundHelper->onLimitExceeded();
  20119. throwUnexpected(); // onLimitExceeded is not supposed to return
  20120. }
  20121. if (processed > stopAfter) // MORE - bit of a strange place to check
  20122. {
  20123. eof = true;
  20124. return NULL;
  20125. }
  20126. return recBuffer.getClear();
  20127. }
  20128. };
  20129. class CRoxieServerDiskAggregateBaseActivity : public CRoxieServerDiskReadBaseActivity
  20130. {
  20131. protected:
  20132. bool done;
  20133. public:
  20134. CRoxieServerDiskAggregateBaseActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, IInMemoryIndexManager *_manager, ITranslatorSet *_translators)
  20135. : CRoxieServerDiskReadBaseActivity(_ctx, _factory, _probeManager, _remoteId, _numParts, _isLocal, false, false, _manager, _translators),
  20136. done(false)
  20137. {
  20138. }
  20139. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  20140. {
  20141. done = false;
  20142. CRoxieServerDiskReadBaseActivity::start(parentExtractSize, parentExtract, paused);
  20143. }
  20144. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  20145. {
  20146. if (idx==(unsigned)-1)
  20147. idx = 0;
  20148. return idx ? NULL: this;
  20149. }
  20150. };
  20151. class CRoxieServerDiskCountActivity : public CRoxieServerDiskAggregateBaseActivity
  20152. {
  20153. IHThorDiskCountArg & countHelper;
  20154. unsigned __int64 choosenLimit;
  20155. unsigned __int64 getSkippedCount()
  20156. {
  20157. unsigned flags = countHelper.getFlags();
  20158. if (flags & TDRlimitskips)
  20159. return 0;
  20160. else if (flags & TDRlimitcreates)
  20161. return 1;
  20162. else
  20163. {
  20164. countHelper.onLimitExceeded();
  20165. throwUnexpected(); // onLimitExceeded should always throw exception
  20166. }
  20167. }
  20168. public:
  20169. CRoxieServerDiskCountActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, IInMemoryIndexManager *_manager, ITranslatorSet *_translators)
  20170. : CRoxieServerDiskAggregateBaseActivity(_ctx, _factory, _probeManager, _remoteId, _numParts, _isLocal, _manager, _translators),
  20171. countHelper((IHThorDiskCountArg &)basehelper)
  20172. {
  20173. choosenLimit = 0;
  20174. }
  20175. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  20176. {
  20177. choosenLimit = countHelper.getChooseNLimit();
  20178. rowLimit = countHelper.getRowLimit();
  20179. // keyedLimit = countHelper->getKeyedLimit(); // more - should there be one?
  20180. CRoxieServerDiskAggregateBaseActivity::start(parentExtractSize, parentExtract, paused);
  20181. }
  20182. virtual const void *nextRow()
  20183. {
  20184. ActivityTimer t(activityStats, timeActivities);
  20185. if (done) return NULL;
  20186. done = true;
  20187. unsigned __int64 totalCount = 0;
  20188. if (helper.canMatchAny() && !eof)
  20189. {
  20190. if (useRemote())
  20191. {
  20192. for (;;)
  20193. {
  20194. const void * next = remote->nextRow();
  20195. if (!next)
  20196. break;
  20197. if (meta.getFixedSize() == 1)
  20198. totalCount += *(byte *)next;
  20199. else
  20200. totalCount += *(unsigned __int64 *)next;
  20201. ReleaseRoxieRow(next);
  20202. if (totalCount > rowLimit)
  20203. {
  20204. totalCount = getSkippedCount();
  20205. break;
  20206. }
  20207. else if (totalCount >= choosenLimit)
  20208. {
  20209. totalCount = choosenLimit;
  20210. break;
  20211. }
  20212. }
  20213. }
  20214. else
  20215. {
  20216. for (;;)
  20217. {
  20218. const byte *nextRec = reader->nextRow();
  20219. if (!nextRec)
  20220. break;
  20221. totalCount += countHelper.numValid(nextRec);
  20222. reader->finishedRow();
  20223. if (totalCount > rowLimit)
  20224. {
  20225. totalCount = getSkippedCount();
  20226. break;
  20227. }
  20228. else if (totalCount >= choosenLimit)
  20229. {
  20230. totalCount = choosenLimit;
  20231. break;
  20232. }
  20233. }
  20234. }
  20235. }
  20236. processed++;
  20237. size32_t rowSize = meta.getFixedSize();
  20238. void * result = rowAllocator->createRow();
  20239. if (rowSize == 1)
  20240. *(byte *)result = (byte)totalCount;
  20241. else
  20242. {
  20243. assertex(rowSize == sizeof(unsigned __int64));
  20244. *(unsigned __int64 *)result = totalCount;
  20245. }
  20246. return rowAllocator->finalizeRow(rowSize, result, rowSize);
  20247. }
  20248. };
  20249. class CRoxieServerDiskAggregateActivity : public CRoxieServerDiskAggregateBaseActivity
  20250. {
  20251. IHThorCompoundAggregateExtra & aggregateHelper;
  20252. public:
  20253. CRoxieServerDiskAggregateActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  20254. unsigned _numParts, bool _isLocal, IInMemoryIndexManager *_manager, ITranslatorSet *_translators)
  20255. : CRoxieServerDiskAggregateBaseActivity(_ctx, _factory, _probeManager, _remoteId, _numParts, _isLocal, _manager, _translators),
  20256. aggregateHelper((IHThorDiskAggregateArg &)basehelper)
  20257. {
  20258. }
  20259. const void * gatherMerged()
  20260. {
  20261. RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
  20262. size32_t finalSize = 0;
  20263. if (useRemote())
  20264. {
  20265. const void * firstRow = remote->nextRow();
  20266. if (!firstRow)
  20267. {
  20268. rowBuilder.ensureRow();
  20269. finalSize = aggregateHelper.clearAggregate(rowBuilder);
  20270. }
  20271. else
  20272. {
  20273. // NOTE need to clone this because going to modify below, could special case 1 row only
  20274. finalSize = cloneRow(rowBuilder, firstRow, meta);
  20275. ReleaseRoxieRow(firstRow);
  20276. }
  20277. for (;;)
  20278. {
  20279. const void * next = remote->nextRow();
  20280. if (!next)
  20281. break;
  20282. finalSize = aggregateHelper.mergeAggregate(rowBuilder, next);
  20283. ReleaseRoxieRow(next);
  20284. }
  20285. }
  20286. else
  20287. {
  20288. aggregateHelper.clearAggregate(rowBuilder);
  20289. if (helper.canMatchAny() && !eof)
  20290. {
  20291. for (;;)
  20292. {
  20293. const byte *nextRec = reader->nextRow();
  20294. if (!nextRec)
  20295. break;
  20296. aggregateHelper.processRow(rowBuilder, nextRec);
  20297. reader->finishedRow();
  20298. }
  20299. }
  20300. finalSize = meta.getRecordSize(rowBuilder.getSelf());
  20301. }
  20302. return rowBuilder.finalizeRowClear(finalSize);
  20303. }
  20304. virtual const void *nextRow()
  20305. {
  20306. ActivityTimer t(activityStats, timeActivities);
  20307. if (done) return NULL;
  20308. const void * ret = gatherMerged();
  20309. processed++;
  20310. done = true;
  20311. return ret;
  20312. }
  20313. };
  20314. class CRoxieServerDiskGroupAggregateActivity : public CRoxieServerDiskAggregateBaseActivity
  20315. {
  20316. IHThorDiskGroupAggregateArg & aggregateHelper;
  20317. RowAggregator resultAggregator;
  20318. bool gathered;
  20319. public:
  20320. CRoxieServerDiskGroupAggregateActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, IInMemoryIndexManager *_manager, ITranslatorSet *_translators)
  20321. : CRoxieServerDiskAggregateBaseActivity(_ctx, _factory, _probeManager, _remoteId, _numParts, _isLocal, _manager, _translators),
  20322. aggregateHelper((IHThorDiskGroupAggregateArg &)basehelper),
  20323. resultAggregator(aggregateHelper, aggregateHelper),
  20324. gathered(false)
  20325. {
  20326. }
  20327. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  20328. {
  20329. gathered= false;
  20330. CRoxieServerDiskAggregateBaseActivity::start(parentExtractSize, parentExtract, paused);
  20331. resultAggregator.start(rowAllocator, ctx->queryCodeContext(), activityId);
  20332. }
  20333. virtual void reset()
  20334. {
  20335. resultAggregator.reset();
  20336. CRoxieServerDiskAggregateBaseActivity::reset();
  20337. }
  20338. void gatherMerged()
  20339. {
  20340. if (useRemote())
  20341. {
  20342. for (;;)
  20343. {
  20344. const void * next = remote->nextRow();
  20345. if (!next)
  20346. break;
  20347. resultAggregator.mergeElement(next);
  20348. ReleaseRoxieRow(next);
  20349. }
  20350. }
  20351. else
  20352. {
  20353. if (helper.canMatchAny() && !eof)
  20354. {
  20355. Owned<IInMemoryFileProcessor> processor = createGroupAggregateRecordProcessor(resultAggregator, aggregateHelper, reader);
  20356. processor->doQuery(NULL, 0, 0, 0);
  20357. }
  20358. }
  20359. gathered = true;
  20360. }
  20361. virtual const void *nextRow()
  20362. {
  20363. ActivityTimer t(activityStats, timeActivities);
  20364. if (done)
  20365. return NULL;
  20366. if (!gathered)
  20367. gatherMerged();
  20368. Owned<AggregateRowBuilder> next = resultAggregator.nextResult();
  20369. if (next)
  20370. {
  20371. processed++;
  20372. return next->finalizeRowClear();
  20373. }
  20374. done = true;
  20375. return NULL;
  20376. }
  20377. };
  20378. class CRoxieServerDiskReadActivityFactory : public CRoxieServerActivityFactory
  20379. {
  20380. public:
  20381. RemoteActivityId remoteId;
  20382. bool isLocal;
  20383. bool sorted;
  20384. bool maySkip;
  20385. bool variableFileName;
  20386. Owned<ITranslatorSet> translators;
  20387. Owned<IInMemoryIndexManager> manager;
  20388. Owned<const IResolvedFile> datafile;
  20389. const char *quotes;
  20390. const char *separators;
  20391. const char *terminators;
  20392. const char *escapes;
  20393. size32_t maxCsvRowSize;
  20394. CRoxieServerDiskReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  20395. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), remoteId(_remoteId)
  20396. {
  20397. isLocal = _graphNode.getPropBool("att[@name='local']/@value") && queryFactory.queryChannel()!=0;
  20398. Owned<IHThorDiskReadBaseArg> helper = (IHThorDiskReadBaseArg *) helperFactory();
  20399. sorted = (helper->getFlags() & TDRunsorted) == 0;
  20400. variableFileName = allFilesDynamic || _queryFactory.isDynamic() || ((helper->getFlags() & (TDXvarfilename|TDXdynamicfilename)) != 0);
  20401. maySkip = (helper->getFlags() & (TDRkeyedlimitskips|TDRkeyedlimitcreates|TDRlimitskips|TDRlimitcreates)) != 0;
  20402. quotes = separators = terminators = escapes = NULL;
  20403. if (!variableFileName)
  20404. {
  20405. bool isOpt = (helper->getFlags() & TDRoptional) != 0;
  20406. OwnedRoxieString fileName(helper->getFileName());
  20407. datafile.setown(_queryFactory.queryPackage().lookupFileName(fileName, isOpt, true, true, _queryFactory.queryWorkUnit(), true, isActivityCodeSigned()));
  20408. bool isSimple = (datafile && datafile->getNumParts()==1 && !_queryFactory.queryOptions().disableLocalOptimizations);
  20409. if (isLocal || isSimple)
  20410. {
  20411. if (datafile)
  20412. {
  20413. unsigned channel = isLocal ? queryFactory.queryChannel() : 0;
  20414. unsigned expectedFormatCrc = helper->getDiskFormatCrc();
  20415. unsigned projectedFormatCrc = helper->getProjectedFormatCrc();
  20416. translators.setown(datafile->getTranslators(projectedFormatCrc, helper->queryProjectedDiskRecordSize(), expectedFormatCrc, helper->queryDiskRecordSize(), getEnableFieldTranslation(), expectedFileMode(), queryFactory.queryQueryName()));
  20417. manager.setown(datafile->getIndexManager(isOpt, channel, translators->queryActualLayout(0), _graphNode.getPropBool("att[@name=\"preload\"]/@value", false)));
  20418. const IPropertyTree *options = datafile->queryProperties();
  20419. if (options)
  20420. {
  20421. quotes = options->queryProp("@csvQuote");
  20422. separators = options->queryProp("@csvSeparate");
  20423. terminators = options->queryProp("@csvTerminate");
  20424. escapes = options->queryProp("@csvEscape");
  20425. }
  20426. }
  20427. else
  20428. manager.setown(createInMemoryIndexManager(helper->queryProjectedDiskRecordSize()->queryRecordAccessor(true), true, nullptr));
  20429. }
  20430. }
  20431. switch (kind)
  20432. {
  20433. case TAKcsvread:
  20434. {
  20435. maxCsvRowSize = defaultMaxCsvRowSize * 1024 * 1024;
  20436. IConstWorkUnit *workunit = _queryFactory.queryWorkUnit();
  20437. if (workunit)
  20438. maxCsvRowSize = workunit->getDebugValueInt(OPT_MAXCSVROWSIZE, defaultMaxCsvRowSize) * 1024 * 1024;
  20439. break;
  20440. }
  20441. default:
  20442. maxCsvRowSize = 0; // unused
  20443. }
  20444. }
  20445. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  20446. {
  20447. unsigned numParts = datafile ? datafile->getNumParts() : 0;
  20448. switch (kind)
  20449. {
  20450. case TAKcsvread:
  20451. return new CRoxieServerCsvReadActivity(_ctx, this, _probeManager, remoteId, numParts, isLocal, sorted, maySkip, manager, translators,
  20452. quotes, separators, terminators, escapes, maxCsvRowSize);
  20453. case TAKxmlread:
  20454. case TAKjsonread:
  20455. return new CRoxieServerXmlReadActivity(_ctx, this, _probeManager, remoteId, numParts, isLocal, sorted, maySkip, manager, translators);
  20456. case TAKdiskread:
  20457. case TAKspillread:
  20458. return new CRoxieServerDiskReadActivity(_ctx, this, _probeManager, remoteId, numParts, isLocal, sorted, maySkip, manager, translators);
  20459. case TAKdisknormalize:
  20460. return new CRoxieServerDiskNormalizeActivity(_ctx, this, _probeManager, remoteId, numParts, isLocal, sorted, manager, translators);
  20461. case TAKdiskcount:
  20462. return new CRoxieServerDiskCountActivity(_ctx, this, _probeManager, remoteId, numParts, isLocal, manager, translators);
  20463. case TAKdiskaggregate:
  20464. return new CRoxieServerDiskAggregateActivity(_ctx, this, _probeManager, remoteId, numParts, isLocal, manager, translators);
  20465. case TAKdiskgroupaggregate:
  20466. return new CRoxieServerDiskGroupAggregateActivity(_ctx, this, _probeManager, remoteId, numParts, isLocal, manager, translators);
  20467. }
  20468. throwUnexpected();
  20469. }
  20470. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  20471. {
  20472. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for %s activity", getActivityText(kind));
  20473. }
  20474. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  20475. {
  20476. if (datafile)
  20477. addXrefFileInfo(reply, datafile);
  20478. else
  20479. {
  20480. // Temporarily resolve the file
  20481. Owned<IHThorDiskReadBaseArg> helper = (IHThorDiskReadBaseArg *) helperFactory();
  20482. if ((helper->getFlags() & (TDXvarfilename|TDXdynamicfilename)) == 0)
  20483. {
  20484. OwnedRoxieString fileName(helper->getFileName());
  20485. Owned<const IResolvedFile> temp = queryFactory.queryPackage().lookupFileName(fileName, true, true, false, queryFactory.queryWorkUnit(), true, isActivityCodeSigned());
  20486. if (temp)
  20487. addXrefFileInfo(reply, temp);
  20488. }
  20489. }
  20490. }
  20491. virtual const StatisticsMapping &queryStatsMapping() const
  20492. {
  20493. return diskStatistics;
  20494. }
  20495. };
  20496. IRoxieServerActivityFactory *createRoxieServerDiskReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  20497. {
  20498. return new CRoxieServerDiskReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId);
  20499. }
  20500. //=================================================================================
  20501. class CRoxieServerBaseIndexActivityFactory : public CRoxieServerActivityFactory
  20502. {
  20503. public:
  20504. Owned<IKeyArray> keySet;
  20505. Owned<ITranslatorSet> translators;
  20506. RemoteActivityId remoteId;
  20507. bool isSimple;
  20508. bool isLocal;
  20509. bool maySkip;
  20510. bool sorted;
  20511. bool variableFileName;
  20512. unsigned maxSeekLookahead;
  20513. Owned<const IResolvedFile> indexfile;
  20514. CRoxieServerBaseIndexActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  20515. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), remoteId(_remoteId)
  20516. {
  20517. Owned<IHThorIndexReadBaseArg> indexHelper = (IHThorIndexReadBaseArg *) helperFactory();
  20518. unsigned flags = indexHelper->getFlags();
  20519. sorted = (flags & TIRunordered) == 0;
  20520. isLocal = _graphNode.getPropBool("att[@name='local']/@value") && queryFactory.queryChannel()!=0;
  20521. variableFileName = allFilesDynamic || _queryFactory.isDynamic() || ((flags & (TIRvarfilename|TIRdynamicfilename)) != 0);
  20522. if (!variableFileName)
  20523. {
  20524. bool isOpt = (flags & TIRoptional) != 0;
  20525. OwnedRoxieString indexName(indexHelper->getFileName());
  20526. indexfile.setown(queryFactory.queryPackage().lookupFileName(indexName, isOpt, true, true, queryFactory.queryWorkUnit(), true, isActivityCodeSigned()));
  20527. if (indexfile)
  20528. {
  20529. unsigned projectedCrc = indexHelper->getProjectedFormatCrc();
  20530. unsigned expectedCrc = indexHelper->getDiskFormatCrc();
  20531. IOutputMetaData *projectedMeta = indexHelper->queryProjectedDiskRecordSize();
  20532. IOutputMetaData *expectedMeta = indexHelper->queryDiskRecordSize();
  20533. translators.setown(indexfile->getTranslators(projectedCrc, projectedMeta, expectedCrc, expectedMeta, getEnableFieldTranslation(), FileFormatMode::index, queryFactory.queryQueryName()));
  20534. keySet.setown(indexfile->getKeyArray(isOpt, isLocal ? queryFactory.queryChannel() : 0));
  20535. }
  20536. }
  20537. isSimple = isLocal;
  20538. maySkip = (flags & (TIRkeyedlimitskips|TIRlimitskips|TIRlimitcreates|TIRkeyedlimitcreates)) != 0;
  20539. if (keySet && keySet->length()==1 && !isLocal && (flags & (TIRlimitskips|TIRlimitcreates|TIRkeyedlimitskips|TIRkeyedlimitcreates))==0)
  20540. {
  20541. IKeyIndexBase *thisBase = keySet->queryKeyPart(0);
  20542. if (thisBase->numParts()==1 && !thisBase->queryPart(0)->isTopLevelKey() && !_queryFactory.queryOptions().disableLocalOptimizations)
  20543. isSimple = true;
  20544. }
  20545. maxSeekLookahead = _graphNode.getPropInt("hint[@name='maxseeklookahead']/@value", 0);
  20546. }
  20547. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  20548. {
  20549. if (indexfile)
  20550. addXrefFileInfo(reply, indexfile);
  20551. else
  20552. {
  20553. Owned<IHThorIndexReadBaseArg> indexHelper = (IHThorIndexReadBaseArg *) helperFactory();
  20554. if ((indexHelper->getFlags() & (TIRvarfilename|TIRdynamicfilename)) == 0)
  20555. {
  20556. OwnedRoxieString indexName(indexHelper->getFileName());
  20557. Owned<const IResolvedFile> temp = queryFactory.queryPackage().lookupFileName(indexName, true, true, false, queryFactory.queryWorkUnit(), true, isActivityCodeSigned());
  20558. if (temp)
  20559. addXrefFileInfo(reply, temp);
  20560. }
  20561. }
  20562. }
  20563. virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
  20564. {
  20565. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for indexread activity");
  20566. }
  20567. virtual const StatisticsMapping &queryStatsMapping() const
  20568. {
  20569. return indexStatistics;
  20570. }
  20571. };
  20572. class CRoxieServerIndexActivity : public CRoxieServerActivity, implements IRoxieServerErrorHandler
  20573. {
  20574. protected:
  20575. IHThorIndexReadBaseArg &indexHelper;
  20576. IHThorSteppedSourceExtra *steppedExtra;
  20577. Linked<IKeyArray> keySet;
  20578. Linked<ITranslatorSet> translators;
  20579. CSkippableRemoteResultAdaptor remote;
  20580. CIndexTransformCallback callback;
  20581. bool sorted;
  20582. bool variableFileName;
  20583. bool variableInfoPending;
  20584. bool isOpt;
  20585. bool isLocal;
  20586. unsigned __int64 rowLimit;
  20587. unsigned __int64 keyedLimit;
  20588. unsigned __int64 choosenLimit;
  20589. unsigned accepted;
  20590. unsigned rejected;
  20591. unsigned seekGEOffset;
  20592. Owned<IKeyManager> tlk;
  20593. Owned<const IResolvedFile> varFileInfo;
  20594. const RemoteActivityId &remoteId;
  20595. void setVariableFileInfo()
  20596. {
  20597. OwnedRoxieString indexName(indexHelper.getFileName());
  20598. varFileInfo.setown(resolveLFNIndex(indexName, isOpt, defaultPrivilegedUser));
  20599. if (varFileInfo)
  20600. {
  20601. unsigned projectedCrc = indexHelper.getProjectedFormatCrc();
  20602. unsigned expectedCrc = indexHelper.getDiskFormatCrc();
  20603. IOutputMetaData *projectedMeta = indexHelper.queryProjectedDiskRecordSize();
  20604. IOutputMetaData *expectedMeta = indexHelper.queryDiskRecordSize();
  20605. translators.setown(varFileInfo->getTranslators(projectedCrc, projectedMeta, expectedCrc, expectedMeta, getEnableFieldTranslation(), FileFormatMode::index, factory->queryQueryFactory().queryQueryName()));
  20606. keySet.setown(varFileInfo->getKeyArray(isOpt, isLocal ? factory->queryQueryFactory().queryChannel() : 0));
  20607. }
  20608. variableInfoPending = false;
  20609. }
  20610. public:
  20611. CRoxieServerIndexActivity(IRoxieAgentContext *_ctx, const CRoxieServerBaseIndexActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  20612. bool _sorted, bool _isLocal, bool _maySkip)
  20613. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  20614. indexHelper((IHThorIndexReadBaseArg &)basehelper),
  20615. keySet(_factory->keySet),
  20616. translators(_factory->translators),
  20617. remote(_ctx, this, _remoteId, meta.queryOriginal(), indexHelper, *this, _sorted, false, _maySkip),
  20618. sorted(_sorted),
  20619. isLocal(_isLocal) ,
  20620. remoteId(_remoteId)
  20621. {
  20622. indexHelper.setCallback(&callback);
  20623. steppedExtra = indexHelper.querySteppingExtra();
  20624. variableFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((indexHelper.getFlags() & (TIRvarfilename|TIRdynamicfilename)) != 0);
  20625. variableInfoPending = false;
  20626. isOpt = (indexHelper.getFlags() & TIRoptional) != 0;
  20627. seekGEOffset = 0;
  20628. // started = false;
  20629. rejected = accepted = 0;
  20630. rowLimit = choosenLimit = keyedLimit = 0;
  20631. }
  20632. virtual const IResolvedFile *queryVarFileInfo() const
  20633. {
  20634. return varFileInfo;
  20635. }
  20636. virtual void onCreate(IHThorArg *_colocalParent)
  20637. {
  20638. CRoxieServerActivity::onCreate(_colocalParent);
  20639. remote.onCreate(_colocalParent);
  20640. }
  20641. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  20642. {
  20643. accepted = 0;
  20644. rejected = 0;
  20645. rowLimit = (unsigned __int64) -1;
  20646. keyedLimit = (unsigned __int64 ) -1;
  20647. choosenLimit = I64C(0x7fffffffffffffff);
  20648. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  20649. remote.onStart(parentExtractSize, parentExtract);
  20650. variableInfoPending = variableFileName;
  20651. }
  20652. void processAllKeys()
  20653. {
  20654. try
  20655. {
  20656. bool noLocal = factory->queryQueryFactory().queryOptions().disableLocalOptimizations || seekGEOffset > 0;
  20657. if (indexHelper.canMatchAny())
  20658. {
  20659. if (variableInfoPending)
  20660. setVariableFileInfo();
  20661. remote.setLimits(rowLimit, keyedLimit, choosenLimit);
  20662. if (keySet)
  20663. {
  20664. // MORE - this recreates the segmonitors per part but not per fileno (which is a little backwards).
  20665. // 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
  20666. // However order is probably better to iterate fileno's inside partnos
  20667. // A superkey that mixes single and multipart or tlk and roroot keys might be hard
  20668. for (unsigned partNo = 0; partNo < keySet->length(); partNo++)
  20669. {
  20670. IKeyIndexBase *thisBase = keySet->queryKeyPart(partNo);
  20671. if (thisBase)
  20672. {
  20673. unsigned fileNo = 0;
  20674. IKeyIndex *thisKey = thisBase->queryPart(fileNo);
  20675. if (!thisKey->isTopLevelKey())
  20676. {
  20677. if (keyedLimit != (unsigned __int64) -1)
  20678. {
  20679. if ((indexHelper.getFlags() & TIRcountkeyedlimit) != 0)
  20680. {
  20681. Owned<IKeyManager> countKey;
  20682. countKey.setown(createLocalKeyManager(indexHelper.queryDiskRecordSize()->queryRecordAccessor(true), thisKey, this, indexHelper.hasNewSegmentMonitors(), !isBlind()));
  20683. countKey->setLayoutTranslator(translators->queryTranslator(fileNo));
  20684. createSegmentMonitors(countKey);
  20685. unsigned __int64 count = countKey->checkCount(keyedLimit);
  20686. if (count > keyedLimit)
  20687. {
  20688. if (traceLevel > 4)
  20689. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  20690. onLimitExceeded(true);
  20691. }
  20692. }
  20693. }
  20694. }
  20695. if (seekGEOffset && !thisKey->isTopLevelKey())
  20696. {
  20697. tlk.setown(createSingleKeyMerger(indexHelper.queryDiskRecordSize()->queryRecordAccessor(true), thisKey, seekGEOffset, this, indexHelper.hasNewSegmentMonitors(), !isBlind()));
  20698. tlk->setLayoutTranslator(translators->queryTranslator(fileNo));
  20699. }
  20700. else
  20701. {
  20702. tlk.setown(createLocalKeyManager(indexHelper.queryDiskRecordSize()->queryRecordAccessor(true), thisKey, this, indexHelper.hasNewSegmentMonitors(), !isBlind()));
  20703. if (!thisKey->isTopLevelKey())
  20704. tlk->setLayoutTranslator(translators->queryTranslator(fileNo));
  20705. else
  20706. tlk->setLayoutTranslator(nullptr);
  20707. }
  20708. createSegmentMonitors(tlk);
  20709. if (queryTraceLevel() > 3 || ctx->queryProbeManager())
  20710. {
  20711. StringBuffer out;
  20712. tlk->describeFilter(out);
  20713. CTXLOG("Using filter %s", out.str());
  20714. if (ctx->queryProbeManager())
  20715. ctx->queryProbeManager()->setNodeProperty(this, "filter", out.str());
  20716. }
  20717. tlk->reset();
  20718. for (;;) // for each file part
  20719. {
  20720. //block for TransformCallbackAssociation
  20721. {
  20722. TransformCallbackAssociation associate(callback, tlk);
  20723. if (thisKey->isTopLevelKey())
  20724. {
  20725. if (thisKey->isFullySorted())
  20726. {
  20727. while (tlk->lookup(false))
  20728. {
  20729. unsigned agentPart = (unsigned)extractFpos(tlk);
  20730. if (agentPart)
  20731. {
  20732. accepted++;
  20733. remote.getMem(agentPart, fileNo, 0); // the cached context is all we need to send
  20734. if (sorted && numChannels>1)
  20735. remote.flush(); // don't combine parts if we need result sorted, except on a 1-way
  20736. }
  20737. }
  20738. }
  20739. else
  20740. {
  20741. unsigned agentPart = tlk->getPartition(); // Returns 0 if no partition info, or filter cannot be partitioned
  20742. remote.getMem(agentPart, fileNo, 0);
  20743. }
  20744. }
  20745. else
  20746. {
  20747. // Single - part key. We process locally unless smart-stepping is involved, in which case we need to ensure that
  20748. // we do the proper two-stage merge.
  20749. // Unfortunately processSingleKey will not use the merger we carefully set up before,
  20750. // so we disable the local processing in that case
  20751. if (noLocal)
  20752. remote.getMem(1, fileNo, 0); // 1 because it's a single part key
  20753. else if (processSingleKey(thisKey, translators->queryTranslator(fileNo)))
  20754. break;
  20755. }
  20756. }
  20757. if (++fileNo < thisBase->numParts())
  20758. {
  20759. thisKey = thisBase->queryPart(fileNo);
  20760. tlk->setKey(thisKey);
  20761. if (!thisKey->isTopLevelKey())
  20762. tlk->setLayoutTranslator(translators->queryTranslator(fileNo));
  20763. else
  20764. tlk->setLayoutTranslator(nullptr);
  20765. tlk->reset();
  20766. }
  20767. else
  20768. break;
  20769. }
  20770. tlk->releaseSegmentMonitors();
  20771. tlk->setKey(NULL);
  20772. }
  20773. }
  20774. }
  20775. }
  20776. remote.flush();
  20777. remote.senddone();
  20778. }
  20779. catch (IException *E)
  20780. {
  20781. remote.setException(E);
  20782. }
  20783. }
  20784. virtual void createSegmentMonitors(IKeyManager *key)
  20785. {
  20786. indexHelper.createSegmentMonitors(key);
  20787. key->finishSegmentMonitors();
  20788. }
  20789. virtual bool processSingleKey(IKeyIndex *key, const IDynamicTransform * trans) = 0;
  20790. virtual void reset()
  20791. {
  20792. if (accepted)
  20793. noteStatistic(StNumIndexAccepted, accepted);
  20794. if (rejected)
  20795. noteStatistic(StNumIndexRejected, rejected);
  20796. remote.onReset();
  20797. CRoxieServerActivity::reset();
  20798. if (varFileInfo)
  20799. {
  20800. keySet.clear();
  20801. varFileInfo.clear();
  20802. }
  20803. variableInfoPending = false;
  20804. }
  20805. virtual void stop()
  20806. {
  20807. remote.onStop();
  20808. CRoxieServerActivity::stop();
  20809. }
  20810. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  20811. {
  20812. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  20813. }
  20814. virtual void gatherStats(CRuntimeStatisticCollection & merged) const override
  20815. {
  20816. CRoxieServerActivity::gatherStats(merged);
  20817. remote.gatherStats(merged);
  20818. }
  20819. };
  20820. class CRoxieServerIndexReadBaseActivity : public CRoxieServerIndexActivity
  20821. {
  20822. protected:
  20823. IHThorSourceLimitTransformExtra * limitTransformExtra = nullptr;
  20824. public:
  20825. CRoxieServerIndexReadBaseActivity(IRoxieAgentContext *_ctx, const CRoxieServerBaseIndexActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  20826. bool _sorted, bool _isLocal, bool _maySkip)
  20827. : CRoxieServerIndexActivity(_ctx, _factory, _probeManager, _remoteId, _sorted, _isLocal, _maySkip)
  20828. {
  20829. }
  20830. virtual void reset()
  20831. {
  20832. remote.onReset();
  20833. CRoxieServerIndexActivity::reset();
  20834. }
  20835. virtual const void *nextRow()
  20836. {
  20837. ActivityTimer t(activityStats, timeActivities);
  20838. try
  20839. {
  20840. const void *ret = remote.nextRow();
  20841. if (ret)
  20842. processed++;
  20843. return ret;
  20844. }
  20845. catch (IException *E)
  20846. {
  20847. throw makeWrappedException(E);
  20848. }
  20849. }
  20850. protected:
  20851. virtual const void * createLimitFailRow(bool isKeyed)
  20852. {
  20853. ensureRowAllocator();
  20854. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  20855. assertex(limitTransformExtra);
  20856. size32_t outSize = isKeyed ? limitTransformExtra->transformOnKeyedLimitExceeded(rowBuilder) : limitTransformExtra->transformOnLimitExceeded(rowBuilder);
  20857. if (outSize)
  20858. return rowBuilder.finalizeRowClear(outSize);
  20859. return NULL;
  20860. }
  20861. };
  20862. class CRoxieServerIndexReadActivity : public CRoxieServerIndexReadBaseActivity, implements IIndexReadActivityInfo
  20863. {
  20864. protected:
  20865. IHThorIndexReadArg & readHelper;
  20866. ISteppingMeta *rawMeta = nullptr;
  20867. CSteppingMeta steppingMeta;
  20868. unsigned * seekSizes = nullptr;
  20869. bool optimizeSteppedPostFilter;
  20870. ISteppingMeta * projectedMeta = nullptr;
  20871. unsigned maxSeekLookahead;
  20872. public:
  20873. CRoxieServerIndexReadActivity(IRoxieAgentContext *_ctx, const CRoxieServerBaseIndexActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  20874. bool _sorted, bool _isLocal, bool _maySkip, unsigned _maxSeekLookahead)
  20875. : CRoxieServerIndexReadBaseActivity(_ctx, _factory, _probeManager, _remoteId, _sorted, _isLocal, _maySkip),
  20876. readHelper((IHThorIndexReadArg &)basehelper)
  20877. {
  20878. limitTransformExtra = &readHelper;
  20879. rawMeta = readHelper.queryRawSteppingMeta();
  20880. unsigned flags = indexHelper.getFlags();
  20881. optimizeSteppedPostFilter = (flags & TIRunfilteredtransform) != 0;
  20882. seekSizes = NULL;
  20883. maxSeekLookahead = _maxSeekLookahead;
  20884. if (rawMeta)
  20885. {
  20886. const CFieldOffsetSize * fields = rawMeta->queryFields();
  20887. unsigned maxFields = rawMeta->getNumFields();
  20888. seekGEOffset = fields[0].offset;
  20889. seekSizes = new unsigned[maxFields];
  20890. seekSizes[0] = fields[0].size;
  20891. for (unsigned i=1; i < maxFields; i++)
  20892. seekSizes[i] = seekSizes[i-1] + fields[i].size;
  20893. projectedMeta = readHelper.queryProjectedSteppingMeta();
  20894. ISteppingMeta *useMeta = projectedMeta ? projectedMeta : rawMeta;
  20895. remote.setMergeInfo(useMeta); // also need to consider superfile case where there is a mix of multiway and singleparts.. ?
  20896. bool hasPostFilter = readHelper.transformMayFilter() && optimizeSteppedPostFilter;
  20897. steppingMeta.init(useMeta, hasPostFilter);
  20898. }
  20899. }
  20900. ~CRoxieServerIndexReadActivity()
  20901. {
  20902. delete [] seekSizes;
  20903. }
  20904. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  20905. {
  20906. CRoxieServerIndexReadBaseActivity::start(parentExtractSize, parentExtract, paused);
  20907. steppingMeta.setDistributed();
  20908. if (steppedExtra)
  20909. steppingMeta.setExtra(steppedExtra);
  20910. rowLimit = readHelper.getRowLimit();
  20911. keyedLimit = readHelper.getKeyedLimit();
  20912. choosenLimit = readHelper.getChooseNLimit();
  20913. if (!paused)
  20914. processAllKeys();
  20915. }
  20916. class LazyLocalKeyReader : implements IMessageUnpackCursor, implements IMessageResult, public CInterface
  20917. {
  20918. public:
  20919. IMPLEMENT_IINTERFACE;
  20920. virtual IMessageUnpackCursor *getCursor(roxiemem::IRowManager *rowMgr) const
  20921. {
  20922. Link();
  20923. return const_cast<LazyLocalKeyReader*> (this);
  20924. }
  20925. virtual const void *getMessageHeader(unsigned &length) const
  20926. {
  20927. length = 0;
  20928. return NULL;
  20929. }
  20930. virtual const void *getMessageMetadata(unsigned &length) const
  20931. {
  20932. length = 0;
  20933. return NULL;
  20934. }
  20935. virtual void discard() const
  20936. {
  20937. // nothing to do.
  20938. }
  20939. unsigned keyedCount;
  20940. unsigned matched;
  20941. bool EOFseen;
  20942. Owned<IKeyIndexSet> keySet;
  20943. Owned<IKeyManager> tlk;
  20944. CRoxieServerIndexReadActivity &owner;
  20945. LazyLocalKeyReader(CRoxieServerIndexReadActivity &_owner, IKeyIndex *key, const IDynamicTransform * trans)
  20946. : owner(_owner)
  20947. {
  20948. keyedCount = 0;
  20949. matched = 0;
  20950. EOFseen = false;
  20951. keySet.setown(createKeyIndexSet());
  20952. keySet->addIndex(LINK(key));
  20953. if (owner.seekGEOffset)
  20954. tlk.setown(createKeyMerger(owner.indexHelper.queryDiskRecordSize()->queryRecordAccessor(true), keySet, owner.seekGEOffset, &owner, owner.indexHelper.hasNewSegmentMonitors(), !owner.isBlind()));
  20955. else
  20956. tlk.setown(createLocalKeyManager(owner.indexHelper.queryDiskRecordSize()->queryRecordAccessor(true), keySet->queryPart(0), &owner, owner.indexHelper.hasNewSegmentMonitors(), !owner.isBlind()));
  20957. if (!key->isTopLevelKey())
  20958. tlk->setLayoutTranslator(trans);
  20959. owner.indexHelper.createSegmentMonitors(tlk);
  20960. tlk->finishSegmentMonitors();
  20961. tlk->reset();
  20962. }
  20963. virtual const void *getNext(int length)
  20964. {
  20965. TransformCallbackAssociation associate(owner.callback, tlk);
  20966. while (tlk->lookup(true))
  20967. {
  20968. keyedCount++;
  20969. if (keyedCount > owner.keyedLimit)
  20970. {
  20971. owner.onLimitExceeded(true); // Should throw exception
  20972. throwUnexpected();
  20973. }
  20974. size32_t transformedSize;
  20975. RtlDynamicRowBuilder rowBuilder(owner.rowAllocator);
  20976. byte const * keyRow = tlk->queryKeyBuffer();
  20977. if (likely(owner.readHelper.canMatch(keyRow)))
  20978. {
  20979. try
  20980. {
  20981. transformedSize = owner.readHelper.transform(rowBuilder, keyRow);
  20982. owner.callback.finishedRow();
  20983. }
  20984. catch (IException *E)
  20985. {
  20986. throw owner.makeWrappedException(E);
  20987. }
  20988. if (transformedSize)
  20989. {
  20990. OwnedConstRoxieRow result = rowBuilder.finalizeRowClear(transformedSize);
  20991. matched++;
  20992. if (matched > owner.rowLimit)
  20993. {
  20994. owner.onLimitExceeded(false); // Should throw exception
  20995. throwUnexpected();
  20996. }
  20997. if (matched > owner.choosenLimit) // MORE - bit of a strange place to check
  20998. {
  20999. break;
  21000. }
  21001. owner.accepted++;
  21002. return result.getClear();
  21003. }
  21004. else
  21005. owner.rejected++;
  21006. }
  21007. else
  21008. {
  21009. owner.callback.finishedRow(); // since filter might have accessed a blob
  21010. owner.rejected++;
  21011. }
  21012. }
  21013. EOFseen = true;
  21014. return NULL;
  21015. }
  21016. virtual bool atEOF() const
  21017. {
  21018. return EOFseen;
  21019. }
  21020. virtual bool isSerialized() const
  21021. {
  21022. return false;
  21023. }
  21024. };
  21025. virtual bool processSingleKey(IKeyIndex *key, const IDynamicTransform * trans) override
  21026. {
  21027. ensureRowAllocator();
  21028. remote.injectResult(new LazyLocalKeyReader(*this, key, trans));
  21029. return false;
  21030. }
  21031. virtual void onLimitExceeded(bool isKeyed)
  21032. {
  21033. if (traceLevel > 4)
  21034. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  21035. if (isKeyed)
  21036. {
  21037. if (indexHelper.getFlags() & (TIRkeyedlimitskips|TIRkeyedlimitcreates))
  21038. {
  21039. if (ctx->queryDebugContext())
  21040. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  21041. throw makeLimitSkipException(true);
  21042. }
  21043. else
  21044. readHelper.onKeyedLimitExceeded();
  21045. }
  21046. else
  21047. {
  21048. if (indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates))
  21049. {
  21050. if (ctx->queryDebugContext())
  21051. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  21052. throw makeLimitSkipException(false);
  21053. }
  21054. else
  21055. readHelper.onLimitExceeded();
  21056. }
  21057. }
  21058. virtual void serializeSkipInfo(MemoryBuffer &out, unsigned seekLen, const void *rawSeek, unsigned numFields, const void * seek, const SmartStepExtra &stepExtra) const
  21059. {
  21060. out.append((unsigned short) numFields);
  21061. out.append((unsigned short) seekLen);
  21062. out.append((unsigned short) stepExtra.queryFlags());
  21063. IMultipleStepSeekInfo *seeks = stepExtra.queryExtraSeeks();
  21064. if (seeks)
  21065. {
  21066. unsigned lookahead = 40000/seekLen;
  21067. if (maxSeekLookahead && (lookahead > maxSeekLookahead))
  21068. lookahead = maxSeekLookahead;
  21069. seeks->ensureFilled(seek, numFields, lookahead);
  21070. out.append(lookahead != seeks->ordinality()); // seeksAreEof flag
  21071. unsigned serialized = 1; // rawseek is always serialized...
  21072. unsigned patchLength = out.length();
  21073. out.append(serialized); // NOTE - we come back and patch with the actual value...
  21074. out.append(seekLen, rawSeek);
  21075. if (seeks->ordinality())
  21076. {
  21077. const void *lastSeek = rawSeek;
  21078. byte *nextSeek = NULL;
  21079. if (projectedMeta)
  21080. nextSeek = (byte *) alloca(seekLen);
  21081. for (unsigned i = 0; i < seeks->ordinality(); i++)
  21082. {
  21083. if (projectedMeta)
  21084. {
  21085. RtlStaticRowBuilder rowBuilder(nextSeek-seekGEOffset, seekGEOffset+seekLen);
  21086. readHelper.mapOutputToInput(rowBuilder, seeks->querySeek(i), numFields); // NOTE - weird interface to mapOutputToInput means that it STARTS writing at seekGEOffset...
  21087. }
  21088. else
  21089. nextSeek = (byte *) seeks->querySeek(i)+seekGEOffset;
  21090. int diff = memcmp(nextSeek, lastSeek, seekLen);
  21091. if (diff > 0)
  21092. {
  21093. serialized++;
  21094. out.append(seekLen, nextSeek);
  21095. lastSeek = (const byte *) out.reserve(0) - seekLen;
  21096. }
  21097. }
  21098. unsigned length = out.length();
  21099. out.setWritePos(patchLength);
  21100. out.append(serialized);
  21101. out.setWritePos(length);
  21102. }
  21103. }
  21104. else
  21105. {
  21106. out.append(false);
  21107. out.append(1);
  21108. out.append(seekLen, rawSeek);
  21109. }
  21110. }
  21111. virtual const void * nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  21112. {
  21113. ActivityTimer t(activityStats, timeActivities);
  21114. try
  21115. {
  21116. unsigned seeklen = 0;
  21117. const void *rawSeek = NULL;
  21118. if (seek && numFields)
  21119. {
  21120. seeklen = seekSizes[numFields-1];
  21121. rawSeek = (const byte *)seek + seekGEOffset;
  21122. if (projectedMeta)
  21123. {
  21124. byte * temp = (byte *) alloca(seeklen);
  21125. RtlStaticRowBuilder rawBuilder(temp-seekGEOffset, seekGEOffset+seeklen);
  21126. readHelper.mapOutputToInput(rawBuilder, seek, numFields); // NOTE - weird interface to mapOutputToInput means that it STARTS writing at seekGEOffset...
  21127. rawSeek = (byte *)temp;
  21128. }
  21129. }
  21130. const void *ret = remote.nextRowGE(seek, rawSeek, numFields, seeklen, wasCompleteMatch, stepExtra);
  21131. if (ret && wasCompleteMatch) // GH pleas confirm the wasCompleteMatch I just added here is right
  21132. processed++;
  21133. return ret;
  21134. }
  21135. catch (IException *E)
  21136. {
  21137. throw makeWrappedException(E);
  21138. }
  21139. }
  21140. virtual IInputSteppingMeta * querySteppingMeta()
  21141. {
  21142. if (rawMeta && steppingEnabled && ((indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates|TIRkeyedlimitskips|TIRkeyedlimitcreates)) == 0))
  21143. return &steppingMeta;
  21144. return NULL;
  21145. }
  21146. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  21147. {
  21148. if (variableInfoPending)
  21149. setVariableFileInfo();
  21150. return this;
  21151. }
  21152. virtual IKeyArray *getKeySet() const
  21153. {
  21154. return keySet.getLink();
  21155. }
  21156. virtual const IResolvedFile *getVarFileInfo() const
  21157. {
  21158. return varFileInfo.getLink();
  21159. }
  21160. virtual ITranslatorSet *getTranslators() const override
  21161. {
  21162. return translators.getLink();
  21163. }
  21164. virtual void mergeSegmentMonitors(IIndexReadContext *irc) const
  21165. {
  21166. indexHelper.createSegmentMonitors(irc); // NOTE: they will merge
  21167. }
  21168. virtual IRoxieServerActivity *queryActivity() { return this; }
  21169. virtual const RemoteActivityId& queryRemoteId() const
  21170. {
  21171. return remoteId;
  21172. }
  21173. };
  21174. class CRoxieServerSimpleIndexReadActivity : public CRoxieServerActivity, implements IIndexReadActivityInfo
  21175. {
  21176. IHThorIndexReadArg & indexHelper;
  21177. IHThorSteppedSourceExtra * steppedExtra;
  21178. bool eof;
  21179. Linked<IKeyArray>keySet;
  21180. Owned<IKeyIndexSet>keyIndexSet;
  21181. Owned<IKeyManager> tlk;
  21182. Linked<ITranslatorSet> translators;
  21183. CIndexTransformCallback callback;
  21184. unsigned __int64 keyedLimit;
  21185. unsigned rowLimit;
  21186. unsigned chooseNLimit;
  21187. unsigned accepted;
  21188. unsigned rejected;
  21189. unsigned keyedCount;
  21190. ISteppingMeta * rawMeta;
  21191. ISteppingMeta * projectedMeta;
  21192. size32_t seekGEOffset;
  21193. unsigned * seekSizes;
  21194. CSteppingMeta steppingMeta;
  21195. Owned<const IResolvedFile> varFileInfo;
  21196. const RemoteActivityId &remoteId;
  21197. bool firstRead;
  21198. bool variableFileName;
  21199. bool variableInfoPending;
  21200. bool isOpt;
  21201. bool isLocal;
  21202. bool optimizeSteppedPostFilter;
  21203. // MORE there may be enough in common between this and CRoxieServerIndexActivity to warrant some refactoring
  21204. void initKeySet()
  21205. {
  21206. if (keySet->length() > 1 || rawMeta != NULL)
  21207. {
  21208. if (translators->isTranslatingKeyed())
  21209. throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Keyed Layout translation is not available when merging key parts or smart-stepping, as it may change record order");
  21210. if (!translators->hasConsistentTranslation())
  21211. throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Mixture of layout translation is not available when merging key parts or smart-stepping");
  21212. }
  21213. keyIndexSet.setown(createKeyIndexSet());
  21214. for (unsigned part = 0; part < keySet->length(); part++)
  21215. {
  21216. IKeyIndexBase *kib = keySet->queryKeyPart(part);
  21217. if (kib)
  21218. {
  21219. for (unsigned subpart = 0; subpart < kib->numParts(); subpart++)
  21220. {
  21221. IKeyIndex *k = kib->queryPart(subpart);
  21222. if (k)
  21223. {
  21224. assertex(!k->isTopLevelKey());
  21225. keyIndexSet->addIndex(LINK(k));
  21226. }
  21227. }
  21228. }
  21229. }
  21230. }
  21231. void setVariableFileInfo()
  21232. {
  21233. OwnedRoxieString indexName(indexHelper.getFileName());
  21234. varFileInfo.setown(resolveLFNIndex(indexName, isOpt, defaultPrivilegedUser));
  21235. unsigned projectedCrc = indexHelper.getProjectedFormatCrc();
  21236. unsigned expectedCrc = indexHelper.getDiskFormatCrc();
  21237. IOutputMetaData *projectedMeta = indexHelper.queryProjectedDiskRecordSize();
  21238. IOutputMetaData *expectedMeta = indexHelper.queryDiskRecordSize();
  21239. translators.setown(varFileInfo->getTranslators(projectedCrc, projectedMeta, expectedCrc, expectedMeta, getEnableFieldTranslation(), FileFormatMode::index, factory->queryQueryFactory().queryQueryName()));
  21240. keySet.setown(varFileInfo->getKeyArray(isOpt, isLocal ? factory->queryQueryFactory().queryChannel() : 0));
  21241. initKeySet();
  21242. variableInfoPending = false;
  21243. }
  21244. void onEOF()
  21245. {
  21246. callback.setManager(NULL);
  21247. eof = true;
  21248. tlk.clear();
  21249. }
  21250. public:
  21251. CRoxieServerSimpleIndexReadActivity(IRoxieAgentContext *_ctx, const CRoxieServerBaseIndexActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, bool _isLocal)
  21252. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  21253. indexHelper((IHThorIndexReadArg &)basehelper),
  21254. keySet(_factory->keySet),
  21255. translators(_factory->translators),
  21256. remoteId(_remoteId),
  21257. isLocal(_isLocal)
  21258. {
  21259. rowLimit = 0;
  21260. keyedLimit = 0;
  21261. chooseNLimit = 0;
  21262. indexHelper.setCallback(&callback);
  21263. steppedExtra = indexHelper.querySteppingExtra();
  21264. unsigned flags = indexHelper.getFlags();
  21265. variableFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((flags & (TIRvarfilename|TIRdynamicfilename)) != 0);
  21266. variableInfoPending = false;
  21267. isOpt = (flags & TIRoptional) != 0;
  21268. optimizeSteppedPostFilter = (flags & TIRunfilteredtransform) != 0;
  21269. firstRead = true;
  21270. accepted = 0;
  21271. rejected = 0;
  21272. keyedCount = 0;
  21273. eof = false;
  21274. rawMeta = indexHelper.queryRawSteppingMeta();
  21275. projectedMeta = indexHelper.queryProjectedSteppingMeta();
  21276. seekGEOffset = 0;
  21277. seekSizes = NULL;
  21278. if (rawMeta)
  21279. {
  21280. // MORE - should check all keys in maxFields list can actually be keyed.
  21281. const CFieldOffsetSize * fields = rawMeta->queryFields();
  21282. unsigned maxFields = rawMeta->getNumFields();
  21283. seekGEOffset = fields[0].offset;
  21284. seekSizes = new unsigned[maxFields];
  21285. seekSizes[0] = fields[0].size;
  21286. for (unsigned i=1; i < maxFields; i++)
  21287. seekSizes[i] = seekSizes[i-1] + fields[i].size;
  21288. bool hasPostFilter = indexHelper.transformMayFilter() && optimizeSteppedPostFilter;
  21289. if (projectedMeta)
  21290. steppingMeta.init(projectedMeta, hasPostFilter);
  21291. else
  21292. steppingMeta.init(rawMeta, hasPostFilter);
  21293. }
  21294. }
  21295. virtual const IResolvedFile *queryVarFileInfo() const
  21296. {
  21297. return varFileInfo;
  21298. }
  21299. ~CRoxieServerSimpleIndexReadActivity()
  21300. {
  21301. delete [] seekSizes;
  21302. }
  21303. virtual bool needsAllocator() const { return true; }
  21304. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  21305. {
  21306. firstRead = true;
  21307. accepted = 0;
  21308. rejected = 0;
  21309. keyedCount = 0;
  21310. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  21311. if (steppedExtra)
  21312. steppingMeta.setExtra(steppedExtra);
  21313. eof = !indexHelper.canMatchAny();
  21314. if (variableFileName)
  21315. variableInfoPending = true;
  21316. else
  21317. {
  21318. variableInfoPending = false;
  21319. if (!keyIndexSet)
  21320. initKeySet();
  21321. }
  21322. }
  21323. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  21324. {
  21325. if (variableInfoPending)
  21326. setVariableFileInfo();
  21327. return this;
  21328. }
  21329. virtual IKeyArray *getKeySet() const
  21330. {
  21331. return keySet.getLink();
  21332. }
  21333. virtual const IResolvedFile *getVarFileInfo() const
  21334. {
  21335. return varFileInfo.getLink();
  21336. }
  21337. virtual ITranslatorSet *getTranslators() const override
  21338. {
  21339. return translators.getLink();
  21340. }
  21341. virtual void mergeSegmentMonitors(IIndexReadContext *irc) const
  21342. {
  21343. indexHelper.createSegmentMonitors(irc); // NOTE: they will merge
  21344. }
  21345. virtual IRoxieServerActivity *queryActivity() { return this; }
  21346. virtual const RemoteActivityId& queryRemoteId() const
  21347. {
  21348. return remoteId;
  21349. }
  21350. const void *nextRow()
  21351. {
  21352. bool matched = true;
  21353. return nextRowGE(NULL, 0, matched, dummySmartStepExtra);
  21354. }
  21355. unsigned __int64 checkCount(unsigned __int64 limit)
  21356. {
  21357. unsigned numParts = keyIndexSet->numParts();
  21358. unsigned __int64 result = 0;
  21359. for (unsigned i = 0; i < numParts; i++)
  21360. {
  21361. Owned<IKeyManager> countTlk = createLocalKeyManager(indexHelper.queryDiskRecordSize()->queryRecordAccessor(true), keyIndexSet->queryPart(i), this, indexHelper.hasNewSegmentMonitors(), !isBlind());
  21362. countTlk->setLayoutTranslator(translators->queryTranslator(i));
  21363. indexHelper.createSegmentMonitors(countTlk);
  21364. countTlk->finishSegmentMonitors();
  21365. result += countTlk->checkCount(limit-result);
  21366. if (result > limit)
  21367. break;
  21368. }
  21369. return result;
  21370. }
  21371. virtual const void *nextRowGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
  21372. {
  21373. ActivityTimer t(activityStats, timeActivities);
  21374. if (eof)
  21375. return NULL;
  21376. if (firstRead)
  21377. {
  21378. if (variableInfoPending)
  21379. setVariableFileInfo();
  21380. rowLimit = (unsigned) indexHelper.getRowLimit();
  21381. chooseNLimit = (unsigned) indexHelper.getChooseNLimit();
  21382. unsigned numParts = keyIndexSet->numParts();
  21383. if (!numParts)
  21384. {
  21385. onEOF();
  21386. return NULL;
  21387. }
  21388. if (numParts > 1 || seekGEOffset)
  21389. {
  21390. tlk.setown(createKeyMerger(indexHelper.queryDiskRecordSize()->queryRecordAccessor(true), keyIndexSet, seekGEOffset, this, indexHelper.hasNewSegmentMonitors(), !isBlind()));
  21391. tlk->setLayoutTranslator(translators->queryTranslator(0));
  21392. }
  21393. else
  21394. {
  21395. tlk.setown(createLocalKeyManager(indexHelper.queryDiskRecordSize()->queryRecordAccessor(true), keyIndexSet->queryPart(0), this, indexHelper.hasNewSegmentMonitors(), !isBlind()));
  21396. tlk->setLayoutTranslator(translators->queryTranslator(0));
  21397. }
  21398. indexHelper.createSegmentMonitors(tlk);
  21399. tlk->finishSegmentMonitors();
  21400. if (queryTraceLevel() > 3 || ctx->queryProbeManager())
  21401. {
  21402. StringBuffer out;
  21403. tlk->describeFilter(out);
  21404. CTXLOG("Using filter %s", out.str());
  21405. if (ctx->queryProbeManager())
  21406. ctx->queryProbeManager()->setNodeProperty(this, "filter", out.str());
  21407. }
  21408. tlk->reset();
  21409. callback.setManager(tlk);
  21410. keyedLimit = indexHelper.getKeyedLimit();
  21411. if (keyedLimit != (unsigned __int64) -1)
  21412. {
  21413. if ((indexHelper.getFlags() & TIRcountkeyedlimit) != 0)
  21414. {
  21415. unsigned __int64 count = checkCount(keyedLimit);
  21416. if (count > keyedLimit)
  21417. {
  21418. if ((indexHelper.getFlags() & (TIRkeyedlimitskips|TIRkeyedlimitcreates)) == 0)
  21419. indexHelper.onKeyedLimitExceeded();
  21420. const void * ret = NULL;
  21421. if (indexHelper.getFlags() & TIRkeyedlimitcreates)
  21422. ret = createKeyedLimitOnFailRow();
  21423. onEOF();
  21424. return ret;
  21425. }
  21426. keyedLimit = (unsigned __int64) -1;
  21427. }
  21428. }
  21429. firstRead = false;
  21430. }
  21431. if (accepted == chooseNLimit)
  21432. {
  21433. onEOF();
  21434. return NULL;
  21435. }
  21436. const byte * rawSeek = NULL;
  21437. unsigned seekSize = 0;
  21438. if (seek)
  21439. {
  21440. seekSize = seekSizes[numFields-1];
  21441. rawSeek = (const byte *)seek + seekGEOffset;
  21442. if (projectedMeta)
  21443. {
  21444. byte *temp = (byte *) alloca(seekSize);
  21445. RtlStaticRowBuilder rawBuilder(temp-seekGEOffset, seekGEOffset+seekSize);
  21446. indexHelper.mapOutputToInput(rawBuilder, seek, numFields);// NOTE - weird interface to mapOutputToInput means that it STARTS writing at seekGEOffset...
  21447. rawSeek = (byte *)temp;
  21448. }
  21449. #ifdef _DEBUG
  21450. // StringBuffer seekStr;
  21451. // for (unsigned i = 0; i < seekSize; i++)
  21452. // {
  21453. // seekStr.appendf("%02x ", ((unsigned char *) rawSeek)[i]);
  21454. // }
  21455. // DBGLOG("nextRowGE can skip offset %d size %d value %s", seekGEOffset, seekSize, seekStr.str());
  21456. #endif
  21457. }
  21458. const byte * originalRawSeek = rawSeek;
  21459. RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
  21460. while (rawSeek ? tlk->lookupSkip(rawSeek, seekGEOffset, seekSize) : tlk->lookup(true))
  21461. {
  21462. checkAbort();
  21463. keyedCount++;
  21464. if (keyedCount > keyedLimit)
  21465. {
  21466. indexHelper.onKeyedLimitExceeded();
  21467. break;
  21468. }
  21469. byte const * keyRow = tlk->queryKeyBuffer();
  21470. if (likely(indexHelper.canMatch(keyRow)))
  21471. {
  21472. #ifdef _DEBUG
  21473. // StringBuffer recstr;
  21474. // unsigned size = (tlk->queryRecordSize()<80) ? tlk->queryRecordSize() : 80;
  21475. // for (unsigned i = 0; i < size; i++)
  21476. // {
  21477. // recstr.appendf("%02x ", ((unsigned char *) keyRow)[i]);
  21478. // }
  21479. // DBGLOG("nextRowGE Got %s", recstr.str());
  21480. if (originalRawSeek && memcmp(keyRow + seekGEOffset, originalRawSeek, seekSize) < 0)
  21481. assertex(!"smart seek failure");
  21482. #endif
  21483. size32_t transformedSize;
  21484. rowBuilder.ensureRow();
  21485. try
  21486. {
  21487. transformedSize =indexHelper.transform(rowBuilder, keyRow);
  21488. //if the post filter causes a mismatch, and the stepping condition no longer matches
  21489. //then return a mismatch record - so the join code can start seeking on the other input.
  21490. if (transformedSize == 0 && optimizeSteppedPostFilter && stepExtra.returnMismatches())
  21491. {
  21492. if (memcmp(keyRow + seekGEOffset, originalRawSeek, seekSize) != 0)
  21493. {
  21494. transformedSize = indexHelper.unfilteredTransform(rowBuilder, keyRow);
  21495. if (transformedSize != 0)
  21496. wasCompleteMatch = false;
  21497. }
  21498. }
  21499. callback.finishedRow();
  21500. }
  21501. catch (IException *E)
  21502. {
  21503. throw makeWrappedException(E);
  21504. }
  21505. if (transformedSize)
  21506. {
  21507. accepted++;
  21508. if (accepted > rowLimit)
  21509. {
  21510. if ((indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates)) != 0)
  21511. {
  21512. throwUnexpected(); // should not have used simple variant if maySkip set...
  21513. }
  21514. if (traceLevel > 4)
  21515. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  21516. indexHelper.onLimitExceeded();
  21517. break;
  21518. }
  21519. processed++;
  21520. #ifdef _DEBUG
  21521. // const byte *ret = (const byte *) out.get();
  21522. // CommonXmlWriter xmlwrite(XWFnoindent|XWFtrim|XWFopt);
  21523. // queryOutputMeta()->toXML(ret, xmlwrite);
  21524. // DBGLOG("ROW: {%p} %s", ret, xmlwrite.str());
  21525. #endif
  21526. return rowBuilder.finalizeRowClear(transformedSize);
  21527. }
  21528. else
  21529. rejected++;
  21530. }
  21531. else
  21532. {
  21533. callback.finishedRow(); // since filter might have accessed a blob
  21534. rejected++;
  21535. }
  21536. rawSeek = NULL;
  21537. }
  21538. onEOF();
  21539. return NULL;
  21540. }
  21541. virtual void reset()
  21542. {
  21543. onEOF();
  21544. if (accepted)
  21545. noteStatistic(StNumIndexAccepted, accepted);
  21546. if (rejected)
  21547. noteStatistic(StNumIndexRejected, rejected);
  21548. if (variableFileName)
  21549. {
  21550. varFileInfo.clear();
  21551. translators.clear();
  21552. }
  21553. variableInfoPending = false;
  21554. CRoxieServerActivity::reset();
  21555. }
  21556. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  21557. {
  21558. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
  21559. }
  21560. virtual IInputSteppingMeta * querySteppingMeta()
  21561. {
  21562. if (rawMeta && steppingEnabled && ((indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates|TIRkeyedlimitskips|TIRkeyedlimitcreates)) == 0))
  21563. return &steppingMeta;
  21564. return NULL;
  21565. }
  21566. protected:
  21567. const void * createKeyedLimitOnFailRow()
  21568. {
  21569. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  21570. size32_t outSize = indexHelper.transformOnKeyedLimitExceeded(rowBuilder);
  21571. if (outSize)
  21572. return rowBuilder.finalizeRowClear(outSize);
  21573. return NULL;
  21574. }
  21575. };
  21576. class CRoxieServerIndexReadActivityFactory : public CRoxieServerBaseIndexActivityFactory
  21577. {
  21578. public:
  21579. CRoxieServerIndexReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  21580. : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId)
  21581. {
  21582. }
  21583. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  21584. {
  21585. if (!variableFileName && (keySet==NULL || keySet->length()==0))
  21586. return new CRoxieServerNullActivity(_ctx, this, _probeManager);
  21587. else if (isSimple && !maySkip)
  21588. return new CRoxieServerSimpleIndexReadActivity(_ctx, this, _probeManager, remoteId, isLocal);
  21589. else
  21590. return new CRoxieServerIndexReadActivity(_ctx, this, _probeManager, remoteId, sorted, isLocal, maySkip, maxSeekLookahead);
  21591. }
  21592. };
  21593. IRoxieServerActivityFactory *createRoxieServerIndexReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  21594. {
  21595. return new CRoxieServerIndexReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId);
  21596. }
  21597. //--------------------------------------------------------------------------------------------------------------------------
  21598. class CRoxieServerNullCountActivity : public CRoxieServerActivity
  21599. {
  21600. bool done;
  21601. public:
  21602. CRoxieServerNullCountActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  21603. : CRoxieServerActivity(_ctx, _factory, _probeManager)
  21604. {
  21605. done = false;
  21606. }
  21607. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  21608. {
  21609. done = false;
  21610. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  21611. }
  21612. virtual bool needsAllocator() const { return true; }
  21613. virtual const void *nextRow()
  21614. {
  21615. ActivityTimer t(activityStats, timeActivities);
  21616. if (done) return NULL;
  21617. done = true;
  21618. size32_t rowSize = meta.getFixedSize();
  21619. void * nullRow = rowAllocator->createRow();
  21620. if (rowSize == 1)
  21621. *(byte *)nullRow = 0;
  21622. else
  21623. {
  21624. assertex(rowSize == sizeof(unsigned __int64));
  21625. *(unsigned __int64 *)nullRow = 0;
  21626. }
  21627. return rowAllocator->finalizeRow(rowSize, nullRow, rowSize);
  21628. }
  21629. };
  21630. class CRoxieServerIndexCountActivity : public CRoxieServerIndexActivity
  21631. {
  21632. IHThorIndexCountArg & countHelper;
  21633. bool done;
  21634. public:
  21635. CRoxieServerIndexCountActivity(IRoxieAgentContext *_ctx, const CRoxieServerBaseIndexActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, bool _isLocal)
  21636. : CRoxieServerIndexActivity(_ctx, _factory, _probeManager, _remoteId, false, _isLocal, false),
  21637. countHelper((IHThorIndexCountArg &)basehelper),
  21638. done(false)
  21639. {
  21640. }
  21641. virtual bool needsAllocator() const { return true; }
  21642. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  21643. {
  21644. done = false;
  21645. CRoxieServerIndexActivity::start(parentExtractSize, parentExtract, paused);
  21646. choosenLimit = countHelper.getChooseNLimit();
  21647. rowLimit = countHelper.getRowLimit();
  21648. keyedLimit = countHelper.getKeyedLimit();
  21649. if (!paused)
  21650. processAllKeys();
  21651. }
  21652. virtual bool processSingleKey(IKeyIndex *key, const IDynamicTransform * trans) override
  21653. {
  21654. unsigned __int64 count = 0;
  21655. if (countHelper.hasFilter())
  21656. {
  21657. while (tlk->lookup(true))
  21658. {
  21659. try
  21660. {
  21661. count += countHelper.numValid(tlk->queryKeyBuffer());
  21662. callback.finishedRow();
  21663. }
  21664. catch (IException *E)
  21665. {
  21666. throw makeWrappedException(E);
  21667. }
  21668. accepted++;
  21669. if (count >= choosenLimit) // MORE - what about limit?
  21670. break;
  21671. }
  21672. }
  21673. else
  21674. count = tlk->getCount(); //MORE: GH->RKC There should be value in providing a max limit to getCount()
  21675. if (count)
  21676. {
  21677. Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult();
  21678. if (count > choosenLimit)
  21679. count = choosenLimit;
  21680. void * recBuffer = rowAllocator->createRow();
  21681. if (meta.getFixedSize() == 1)
  21682. *(byte *)recBuffer = (byte)count;
  21683. else
  21684. {
  21685. assertex(meta.getFixedSize() == sizeof(unsigned __int64));
  21686. *(unsigned __int64 *)recBuffer = count;
  21687. }
  21688. recBuffer = rowAllocator->finalizeRow(meta.getFixedSize(), recBuffer, meta.getFixedSize());
  21689. result->append(recBuffer);
  21690. remote.injectResult(result.getClear());
  21691. //GH->RKC for count(,choosen)/exists passing in the previous count would short-circuit this much earlier
  21692. if (count >= choosenLimit)
  21693. return true;
  21694. }
  21695. return false;
  21696. }
  21697. virtual void onLimitExceeded(bool isKeyed)
  21698. {
  21699. if (traceLevel > 4)
  21700. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  21701. if (isKeyed)
  21702. {
  21703. if (indexHelper.getFlags() & (TIRkeyedlimitskips|TIRkeyedlimitcreates))
  21704. {
  21705. if (ctx->queryDebugContext())
  21706. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  21707. throw makeLimitSkipException(true);
  21708. }
  21709. else
  21710. {
  21711. countHelper.onKeyedLimitExceeded();
  21712. }
  21713. }
  21714. else
  21715. {
  21716. if (indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates))
  21717. {
  21718. if (ctx->queryDebugContext())
  21719. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  21720. throw makeLimitSkipException(false);
  21721. }
  21722. else
  21723. {
  21724. countHelper.onLimitExceeded();
  21725. }
  21726. }
  21727. }
  21728. virtual const void *createLimitFailRow(bool isKeyed)
  21729. {
  21730. throwUnexpected();
  21731. }
  21732. virtual const void *nextRow()
  21733. {
  21734. ActivityTimer t(activityStats, timeActivities);
  21735. if (done) return NULL;
  21736. done = true;
  21737. unsigned __int64 totalCount = 0;
  21738. // Checking keyed limit on server is a bit odd - we only know the counts after postfiltering by the time we are on the server
  21739. // The count needs to behave the same as the output, which will regard a keyed limit as exceeded if the sum of the postfiltered outputs exceeds the keyed limit
  21740. bool hasKeyedLimit = (keyedLimit != (unsigned __int64) -1);
  21741. bool hasLimit = rowLimit != (unsigned __int64) -1;
  21742. try
  21743. {
  21744. for (;;)
  21745. {
  21746. const void * next = remote.nextRow();
  21747. if (!next)
  21748. break;
  21749. if (meta.getFixedSize() == 1)
  21750. totalCount += *(byte *)next;
  21751. else
  21752. totalCount += *(unsigned __int64 *) next;
  21753. ReleaseRoxieRow(next);
  21754. if (totalCount > rowLimit || (totalCount > choosenLimit && !hasLimit && !hasKeyedLimit)) // can't break out early if there is a possibility of later agent throwing limit exception
  21755. break;
  21756. }
  21757. if (totalCount > rowLimit)
  21758. {
  21759. unsigned flags = indexHelper.getFlags();
  21760. if (flags & TIRlimitskips)
  21761. totalCount = 0;
  21762. else if (flags & TIRlimitcreates)
  21763. totalCount = 1;
  21764. else
  21765. countHelper.onLimitExceeded();
  21766. }
  21767. else if (totalCount > keyedLimit)
  21768. {
  21769. unsigned flags = indexHelper.getFlags();
  21770. if (flags & TIRkeyedlimitskips)
  21771. totalCount = 0;
  21772. else if (flags & TIRkeyedlimitcreates)
  21773. totalCount = 1;
  21774. else
  21775. countHelper.onLimitExceeded();
  21776. }
  21777. else if (totalCount > choosenLimit)
  21778. totalCount = choosenLimit;
  21779. }
  21780. catch (IException *E)
  21781. {
  21782. if (QUERYINTERFACE(E, LimitSkipException))
  21783. {
  21784. totalCount = 0;
  21785. unsigned flags = indexHelper.getFlags();
  21786. if (E->errorCode() == KeyedLimitSkipErrorCode)
  21787. {
  21788. if (flags & TIRkeyedlimitcreates)
  21789. totalCount++;
  21790. }
  21791. else
  21792. {
  21793. if (flags & TIRlimitcreates)
  21794. totalCount++;
  21795. }
  21796. if (totalCount > choosenLimit)
  21797. totalCount = choosenLimit; // would have to be weird code (and escape the optimizer...)
  21798. E->Release();
  21799. }
  21800. else
  21801. throw ;
  21802. }
  21803. processed++;
  21804. void * result = rowAllocator->createRow();
  21805. if (meta.getFixedSize() == 1)
  21806. *(byte *)result = (byte)totalCount;
  21807. else
  21808. {
  21809. assertex(meta.getFixedSize() == sizeof(unsigned __int64));
  21810. *(unsigned __int64 *)result = totalCount;
  21811. }
  21812. return rowAllocator->finalizeRow(meta.getFixedSize(), result, meta.getFixedSize());
  21813. }
  21814. };
  21815. class CRoxieServerIndexCountActivityFactory : public CRoxieServerBaseIndexActivityFactory
  21816. {
  21817. public:
  21818. CRoxieServerIndexCountActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  21819. : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId)
  21820. {
  21821. }
  21822. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  21823. {
  21824. if (!variableFileName && (keySet==NULL || keySet->length()==0))
  21825. return new CRoxieServerNullCountActivity(_ctx, this, _probeManager);
  21826. // else if (isSimple)
  21827. // return new CRoxieServerSimpleIndexCountActivity(this, keySet->queryKeyPart(0)->queryPart(0));
  21828. else
  21829. return new CRoxieServerIndexCountActivity(_ctx, this, _probeManager, remoteId, isLocal);
  21830. }
  21831. };
  21832. IRoxieServerActivityFactory *createRoxieServerIndexCountActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  21833. {
  21834. return new CRoxieServerIndexCountActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId);
  21835. }
  21836. //--------------------------------------------------------------------------------------------------------------------------
  21837. class CRoxieServerNullIndexAggregateActivity : public CRoxieServerActivity
  21838. {
  21839. IHThorIndexAggregateArg &aggregateHelper;
  21840. bool done;
  21841. public:
  21842. CRoxieServerNullIndexAggregateActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  21843. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  21844. aggregateHelper((IHThorIndexAggregateArg &)basehelper)
  21845. {
  21846. done = false;
  21847. }
  21848. virtual bool needsAllocator() const { return true; }
  21849. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  21850. {
  21851. done = false;
  21852. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  21853. }
  21854. virtual const void *nextRow()
  21855. {
  21856. ActivityTimer t(activityStats, timeActivities);
  21857. if (done) return NULL;
  21858. done = true;
  21859. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  21860. size32_t thisSize = aggregateHelper.clearAggregate(rowBuilder);
  21861. return rowBuilder.finalizeRowClear(thisSize);
  21862. }
  21863. };
  21864. class CRoxieServerIndexAggregateActivity : public CRoxieServerIndexActivity
  21865. {
  21866. IHThorCompoundAggregateExtra & aggregateHelper;
  21867. bool done;
  21868. public:
  21869. CRoxieServerIndexAggregateActivity(IRoxieAgentContext *_ctx, const CRoxieServerBaseIndexActivityFactory *_factory, IProbeManager *_probeManager,
  21870. const RemoteActivityId &_remoteId, bool _isLocal)
  21871. : CRoxieServerIndexActivity(_ctx, _factory, _probeManager, _remoteId, false, _isLocal, false),
  21872. aggregateHelper((IHThorIndexAggregateArg &)basehelper),
  21873. done(false)
  21874. {
  21875. }
  21876. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  21877. {
  21878. done = false;
  21879. CRoxieServerIndexActivity::start(parentExtractSize, parentExtract, paused);
  21880. if (!paused)
  21881. processAllKeys();
  21882. }
  21883. virtual bool needsAllocator() const { return true; }
  21884. virtual bool processSingleKey(IKeyIndex *key, const IDynamicTransform * trans) override
  21885. {
  21886. RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
  21887. while (tlk->lookup(true))
  21888. {
  21889. if (!rowBuilder.exists())
  21890. {
  21891. rowBuilder.ensureRow();
  21892. aggregateHelper.clearAggregate(rowBuilder);
  21893. }
  21894. try
  21895. {
  21896. aggregateHelper.processRow(rowBuilder, tlk->queryKeyBuffer());
  21897. callback.finishedRow();
  21898. }
  21899. catch (IException *E)
  21900. {
  21901. throw makeWrappedException(E);
  21902. }
  21903. accepted++;
  21904. }
  21905. if (aggregateHelper.processedAnyRows())
  21906. {
  21907. size32_t size = meta.getRecordSize(rowBuilder.getSelf());
  21908. const void * recBuffer = rowBuilder.finalizeRowClear(size);
  21909. Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult();
  21910. result->append(recBuffer);
  21911. remote.injectResult(result.getClear());
  21912. }
  21913. return false;
  21914. }
  21915. virtual void onLimitExceeded(bool isKeyed)
  21916. {
  21917. if (traceLevel > 4)
  21918. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  21919. throwUnexpected();
  21920. }
  21921. virtual const void *createLimitFailRow(bool isKeyed)
  21922. {
  21923. throwUnexpected();
  21924. }
  21925. const void * gatherMerged()
  21926. {
  21927. RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
  21928. const void * firstRow = remote.nextRow();
  21929. size32_t finalSize = 0;
  21930. if (!firstRow)
  21931. {
  21932. rowBuilder.ensureRow();
  21933. finalSize = aggregateHelper.clearAggregate(rowBuilder);
  21934. }
  21935. else
  21936. {
  21937. // NOTE need to clone this because going to modify below, could special case 1 row only
  21938. finalSize = cloneRow(rowBuilder, firstRow, meta);
  21939. ReleaseRoxieRow(firstRow);
  21940. }
  21941. for (;;)
  21942. {
  21943. const void * next = remote.nextRow();
  21944. if (!next)
  21945. break;
  21946. finalSize = aggregateHelper.mergeAggregate(rowBuilder, next);
  21947. ReleaseRoxieRow(next);
  21948. }
  21949. return rowBuilder.finalizeRowClear(finalSize);
  21950. }
  21951. virtual const void *nextRow()
  21952. {
  21953. ActivityTimer t(activityStats, timeActivities);
  21954. if (done) return NULL;
  21955. const void * ret = gatherMerged();
  21956. processed++;
  21957. done = true;
  21958. return ret;
  21959. }
  21960. };
  21961. class CRoxieServerIndexAggregateActivityFactory : public CRoxieServerBaseIndexActivityFactory
  21962. {
  21963. public:
  21964. CRoxieServerIndexAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  21965. : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId)
  21966. {
  21967. }
  21968. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  21969. {
  21970. if (!variableFileName && (keySet==NULL || keySet->length()==0))
  21971. return new CRoxieServerNullIndexAggregateActivity(_ctx, this, _probeManager);
  21972. // else if (isSimple)
  21973. // return new CRoxieServerSimpleIndexAggregateActivity(this, keySet->queryKeyPart(0)->queryPart(0));
  21974. else
  21975. return new CRoxieServerIndexAggregateActivity(_ctx, this, _probeManager, remoteId, isLocal);
  21976. }
  21977. };
  21978. IRoxieServerActivityFactory *createRoxieServerIndexAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  21979. {
  21980. return new CRoxieServerIndexAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId);
  21981. }
  21982. //--------------------------------------------------------------------------------------------------------------------------
  21983. class CRoxieServerIndexGroupAggregateActivity : public CRoxieServerIndexActivity, implements IHThorGroupAggregateCallback
  21984. {
  21985. IHThorCompoundGroupAggregateExtra & aggregateHelper;
  21986. RowAggregator singleAggregator;
  21987. RowAggregator resultAggregator;
  21988. unsigned groupSegCount;
  21989. bool gathered;
  21990. bool eof;
  21991. public:
  21992. CRoxieServerIndexGroupAggregateActivity(IRoxieAgentContext *_ctx, const CRoxieServerBaseIndexActivityFactory *_factory, IProbeManager *_probeManager,
  21993. const RemoteActivityId &_remoteId, bool _isLocal)
  21994. : CRoxieServerIndexActivity(_ctx, _factory, _probeManager, _remoteId, false, _isLocal, false),
  21995. aggregateHelper((IHThorIndexGroupAggregateArg &)basehelper),
  21996. singleAggregator(aggregateHelper, aggregateHelper),
  21997. resultAggregator(aggregateHelper, aggregateHelper),
  21998. gathered(false), eof(true)
  21999. {
  22000. groupSegCount = 0;
  22001. }
  22002. IMPLEMENT_IINTERFACE_USING(CRoxieServerIndexActivity)
  22003. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  22004. {
  22005. eof = false;
  22006. gathered= false;
  22007. CRoxieServerIndexActivity::start(parentExtractSize, parentExtract, paused);
  22008. groupSegCount = 0;
  22009. if (!paused)
  22010. processAllKeys();
  22011. resultAggregator.start(rowAllocator, ctx->queryCodeContext(), activityId);
  22012. }
  22013. virtual bool needsAllocator() const { return true; }
  22014. virtual void reset()
  22015. {
  22016. resultAggregator.reset();
  22017. CRoxieServerIndexActivity::reset();
  22018. }
  22019. virtual void processRow(const void * next)
  22020. {
  22021. singleAggregator.addRow(next);
  22022. }
  22023. virtual void createSegmentMonitors(IKeyManager *key)
  22024. {
  22025. ThorActivityKind kind = factory->getKind();
  22026. CRoxieServerIndexActivity::createSegmentMonitors(key);
  22027. if ((kind==TAKindexgroupcount || kind==TAKindexgroupexists))
  22028. groupSegCount = aggregateHelper.getGroupingMaxField();
  22029. else
  22030. groupSegCount = 0;
  22031. }
  22032. virtual bool processSingleKey(IKeyIndex *key, const IDynamicTransform * trans) override
  22033. {
  22034. Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult();
  22035. singleAggregator.start(rowAllocator, ctx->queryCodeContext(), activityId);
  22036. ThorActivityKind kind = factory->getKind();
  22037. while (tlk->lookup(true))
  22038. {
  22039. try
  22040. {
  22041. if (groupSegCount && !trans)
  22042. {
  22043. AggregateRowBuilder &rowBuilder = singleAggregator.addRow(tlk->queryKeyBuffer());
  22044. callback.finishedRow();
  22045. if (kind==TAKindexgroupcount)
  22046. {
  22047. unsigned __int64 count = tlk->getCurrentRangeCount(groupSegCount);
  22048. aggregateHelper.processCountGrouping(rowBuilder, count-1);
  22049. }
  22050. if (!tlk->nextRange(groupSegCount))
  22051. break;
  22052. }
  22053. else
  22054. {
  22055. aggregateHelper.processRow(tlk->queryKeyBuffer(), this);
  22056. callback.finishedRow();
  22057. }
  22058. }
  22059. catch (IException *E)
  22060. {
  22061. throw makeWrappedException(E);
  22062. }
  22063. accepted++;
  22064. }
  22065. for (;;)
  22066. {
  22067. Owned<AggregateRowBuilder> next = singleAggregator.nextResult();
  22068. if (!next)
  22069. break;
  22070. result->append(next->finalizeRowClear());
  22071. }
  22072. remote.injectResult(result.getClear());
  22073. singleAggregator.reset();
  22074. return false;
  22075. }
  22076. virtual void onLimitExceeded(bool isKeyed)
  22077. {
  22078. if (traceLevel > 4)
  22079. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  22080. throwUnexpected();
  22081. }
  22082. virtual const void *createLimitFailRow(bool isKeyed)
  22083. {
  22084. throwUnexpected();
  22085. }
  22086. void gatherMerged()
  22087. {
  22088. gathered = true;
  22089. for (;;)
  22090. {
  22091. const void * next = remote.nextRow();
  22092. if (!next)
  22093. break;
  22094. resultAggregator.mergeElement(next);
  22095. ReleaseRoxieRow(next);
  22096. }
  22097. }
  22098. virtual const void *nextRow()
  22099. {
  22100. ActivityTimer t(activityStats, timeActivities);
  22101. if (eof)
  22102. return NULL;
  22103. if (!gathered)
  22104. gatherMerged();
  22105. Owned<AggregateRowBuilder> next = resultAggregator.nextResult();
  22106. if (next)
  22107. {
  22108. processed++;
  22109. return next->finalizeRowClear();
  22110. }
  22111. eof = true;
  22112. return NULL;
  22113. }
  22114. };
  22115. class CRoxieServerIndexGroupAggregateActivityFactory : public CRoxieServerBaseIndexActivityFactory
  22116. {
  22117. public:
  22118. CRoxieServerIndexGroupAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  22119. : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId)
  22120. {
  22121. }
  22122. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  22123. {
  22124. if (!variableFileName && (keySet==NULL || keySet->length()==0))
  22125. return new CRoxieServerNullActivity(_ctx, this, _probeManager);
  22126. // else if (isSimple)
  22127. // return new CRoxieServerSimpleIndexGroupAggregateActivity(this, keySet->queryKeyPart(0)->queryPart(0));
  22128. else
  22129. return new CRoxieServerIndexGroupAggregateActivity(_ctx, this, _probeManager, remoteId, isLocal);
  22130. }
  22131. };
  22132. IRoxieServerActivityFactory *createRoxieServerIndexGroupAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  22133. {
  22134. return new CRoxieServerIndexGroupAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId);
  22135. }
  22136. //--------------------------------------------------------------------------------------------------------------------------
  22137. class CRoxieServerIndexNormalizeActivity : public CRoxieServerIndexReadBaseActivity
  22138. {
  22139. IHThorIndexNormalizeArg & readHelper;
  22140. public:
  22141. CRoxieServerIndexNormalizeActivity(IRoxieAgentContext *_ctx, const CRoxieServerBaseIndexActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
  22142. bool _sorted, bool _isLocal, bool _maySkip)
  22143. : CRoxieServerIndexReadBaseActivity(_ctx, _factory, _probeManager, _remoteId, _sorted, _isLocal, _maySkip),
  22144. readHelper((IHThorIndexNormalizeArg &)basehelper)
  22145. {
  22146. limitTransformExtra = &readHelper;
  22147. }
  22148. virtual bool needsAllocator() const { return true; }
  22149. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  22150. {
  22151. CRoxieServerIndexReadBaseActivity::start(parentExtractSize, parentExtract, paused);
  22152. rowLimit = readHelper.getRowLimit();
  22153. keyedLimit = readHelper.getKeyedLimit();
  22154. choosenLimit = readHelper.getChooseNLimit();
  22155. if (!paused)
  22156. processAllKeys();
  22157. }
  22158. virtual bool processSingleKey(IKeyIndex *key, const IDynamicTransform * trans) override
  22159. {
  22160. unsigned keyedCount = 0;
  22161. RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
  22162. while (tlk->lookup(true))
  22163. {
  22164. keyedCount++;
  22165. if (keyedCount > keyedLimit)
  22166. {
  22167. if (traceLevel > 4)
  22168. DBGLOG("activityid = %d line = %d", activityId, __LINE__);
  22169. onLimitExceeded(true);
  22170. break;
  22171. }
  22172. size32_t transformedSize;
  22173. if (readHelper.first(tlk->queryKeyBuffer()))
  22174. {
  22175. Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult();
  22176. do
  22177. {
  22178. if (accepted>=choosenLimit)
  22179. break;
  22180. rowBuilder.ensureRow();
  22181. try
  22182. {
  22183. transformedSize = readHelper.transform(rowBuilder);
  22184. }
  22185. catch (IException *E)
  22186. {
  22187. throw makeWrappedException(E);
  22188. }
  22189. if (transformedSize)
  22190. {
  22191. // MORE - would be a good idea to stop these asap if rowlimit exceeded
  22192. result->append(rowBuilder.finalizeRowClear(transformedSize));
  22193. accepted++;
  22194. }
  22195. else
  22196. rejected++;
  22197. } while (readHelper.next());
  22198. remote.injectResult(result.getClear());
  22199. callback.finishedRow();
  22200. if (accepted>=choosenLimit)
  22201. return true;
  22202. }
  22203. }
  22204. return false;
  22205. }
  22206. virtual void onLimitExceeded(bool isKeyed)
  22207. {
  22208. if (traceLevel > 4)
  22209. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  22210. if (isKeyed)
  22211. {
  22212. if (indexHelper.getFlags() & (TIRkeyedlimitskips|TIRkeyedlimitcreates))
  22213. {
  22214. if (ctx->queryDebugContext())
  22215. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  22216. throw makeLimitSkipException(true);
  22217. }
  22218. else
  22219. readHelper.onKeyedLimitExceeded();
  22220. }
  22221. else
  22222. {
  22223. if (indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates))
  22224. {
  22225. if (ctx->queryDebugContext())
  22226. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  22227. throw makeLimitSkipException(false);
  22228. }
  22229. else
  22230. readHelper.onLimitExceeded();
  22231. }
  22232. }
  22233. };
  22234. class CRoxieServerIndexNormalizeActivityFactory : public CRoxieServerBaseIndexActivityFactory
  22235. {
  22236. public:
  22237. CRoxieServerIndexNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  22238. : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId)
  22239. {
  22240. }
  22241. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  22242. {
  22243. if (!variableFileName && (keySet==NULL || keySet->length()==0))
  22244. return new CRoxieServerNullActivity(_ctx, this, _probeManager);
  22245. else
  22246. return new CRoxieServerIndexNormalizeActivity(_ctx, this, _probeManager, remoteId, sorted, isLocal, maySkip);
  22247. }
  22248. };
  22249. IRoxieServerActivityFactory *createRoxieServerIndexNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  22250. {
  22251. return new CRoxieServerIndexNormalizeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId);
  22252. }
  22253. //=================================================================================
  22254. class CRoxieServerFetchActivity : public CRoxieServerActivity, implements IRecordPullerCallback, implements IRoxieServerErrorHandler
  22255. {
  22256. IHThorFetchBaseArg &helper;
  22257. Linked<IFilePartMap> map;
  22258. CRemoteResultAdaptor remote;
  22259. RecordPullerThread puller;
  22260. bool needsRHS;
  22261. bool variableFileName;
  22262. bool isOpt;
  22263. Owned<const IResolvedFile> varFileInfo;
  22264. Owned<IEngineRowAllocator> extractAllocator;
  22265. public:
  22266. CRoxieServerFetchActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, IFilePartMap *_map)
  22267. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorFetchBaseArg &)basehelper), map(_map), remote(_ctx, this, _remoteId, meta.queryOriginal(), helper, *this, true, true), puller(false)
  22268. {
  22269. needsRHS = helper.transformNeedsRhs();
  22270. if (needsRHS)
  22271. extractAllocator.setown(createRowAllocator(helper.queryExtractedSize()));
  22272. variableFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((helper.getFetchFlags() & (FFvarfilename|FFdynamicfilename)) != 0);
  22273. isOpt = (helper.getFetchFlags() & FFdatafileoptional) != 0;
  22274. }
  22275. virtual const IResolvedFile *queryVarFileInfo() const
  22276. {
  22277. return varFileInfo;
  22278. }
  22279. virtual void onCreate(IHThorArg *_colocalParent)
  22280. {
  22281. CRoxieServerActivity::onCreate(_colocalParent);
  22282. remote.onCreate(_colocalParent);
  22283. }
  22284. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  22285. {
  22286. if (idx)
  22287. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", sanitizeSourceFile(__FILE__), __LINE__);
  22288. puller.setInput(this, _sourceIdx, _in);
  22289. }
  22290. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks)
  22291. {
  22292. puller.connectInputStreams(ctx, consumerOrdered);
  22293. return CRoxieServerActivity::getOutputStreams(ctx, idx, streams, consumerOptions, consumerOrdered, orderedCallbacks);
  22294. }
  22295. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  22296. {
  22297. if (idx==0)
  22298. return puller.queryInput();
  22299. else
  22300. return NULL;
  22301. }
  22302. virtual void gatherStats(CRuntimeStatisticCollection & merged) const override
  22303. {
  22304. CRoxieServerActivity::gatherStats(merged);
  22305. remote.gatherStats(merged);
  22306. }
  22307. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  22308. {
  22309. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  22310. remote.onStart(parentExtractSize, parentExtract);
  22311. remote.setLimits(helper.getRowLimit(), (unsigned __int64) -1, I64C(0x7FFFFFFFFFFFFFFF));
  22312. if (variableFileName)
  22313. {
  22314. OwnedRoxieString fname(helper.getFileName());
  22315. varFileInfo.setown(resolveLFNFlat(fname, isOpt, defaultPrivilegedUser));
  22316. if (varFileInfo)
  22317. map.setown(varFileInfo->getFileMap());
  22318. }
  22319. puller.start(parentExtractSize, parentExtract, paused, ctx->queryOptions().fetchPreload, false, ctx);
  22320. }
  22321. virtual void stop()
  22322. {
  22323. // Called from remote, so no need to call back to it....
  22324. puller.stop();
  22325. CRoxieServerActivity::stop();
  22326. }
  22327. virtual void reset()
  22328. {
  22329. processed = remote.processed;
  22330. remote.processed = 0;
  22331. puller.reset();
  22332. if (variableFileName)
  22333. {
  22334. varFileInfo.clear();
  22335. map.clear();
  22336. }
  22337. CRoxieServerActivity::reset();
  22338. }
  22339. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  22340. {
  22341. if (idx==(unsigned)-1)
  22342. idx = 0;
  22343. return idx ? NULL : &remote;
  22344. }
  22345. virtual void processRow(const void *row)
  22346. {
  22347. // called from puller thread
  22348. offset_t rp = helper.extractPosition(row);
  22349. unsigned partNo;
  22350. if (isLocalFpos(rp))
  22351. partNo = getLocalFposPart(rp) + 1;
  22352. else
  22353. partNo = map->mapOffset(rp);
  22354. if (needsRHS)
  22355. {
  22356. RtlDynamicRowBuilder rb(extractAllocator, true);
  22357. unsigned rhsSize = helper.extractJoinFields(rb, row);
  22358. char * block = (char *) remote.getMem(partNo, 0, sizeof(rp) + sizeof(rhsSize) + rhsSize); // MORE - superfiles
  22359. *(offset_t *) block = rp;
  22360. block += sizeof(rp);
  22361. *(unsigned *) block = rhsSize;
  22362. block += sizeof(rhsSize);
  22363. memcpy(block, rb.row(), rhsSize);
  22364. }
  22365. else
  22366. *(offset_t *) remote.getMem(partNo, 0, sizeof(rp)) = rp; // MORE - superfiles
  22367. ReleaseRoxieRow(row);
  22368. }
  22369. void processEOG()
  22370. {
  22371. #ifdef FETCH_PRESERVES_GROUPING
  22372. UNIMPLEMENTED;
  22373. #endif
  22374. // else discard is correct
  22375. }
  22376. void processGroup(const ConstPointerArray &)
  22377. {
  22378. throwUnexpected();
  22379. }
  22380. void processDone()
  22381. {
  22382. // called from puller thread
  22383. remote.flush();
  22384. remote.senddone();
  22385. }
  22386. virtual bool fireException(IException *e)
  22387. {
  22388. return remote.fireException(e);
  22389. }
  22390. virtual void onLimitExceeded(bool isKeyed)
  22391. {
  22392. if (traceLevel > 4)
  22393. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  22394. if (isKeyed)
  22395. throwUnexpected();
  22396. helper.onLimitExceeded();
  22397. }
  22398. virtual const void *createLimitFailRow(bool isKeyed)
  22399. {
  22400. UNIMPLEMENTED;
  22401. }
  22402. virtual const void *nextRow()
  22403. {
  22404. throwUnexpected(); // I am nobody's input
  22405. }
  22406. };
  22407. class CRoxieServerFetchActivityFactory : public CRoxieServerActivityFactory
  22408. {
  22409. RemoteActivityId remoteId;
  22410. Owned<IFilePartMap> map;
  22411. bool variableFileName;
  22412. Owned<const IResolvedFile> datafile;
  22413. public:
  22414. CRoxieServerFetchActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  22415. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), remoteId(_remoteId)
  22416. {
  22417. Owned<IHThorFetchBaseArg> helper = (IHThorFetchBaseArg *) helperFactory();
  22418. variableFileName = allFilesDynamic || _queryFactory.isDynamic() || ((helper->getFetchFlags() & (FFvarfilename|FFdynamicfilename)) != 0);
  22419. if (!variableFileName)
  22420. {
  22421. OwnedRoxieString fname(helper->getFileName());
  22422. datafile.setown(_queryFactory.queryPackage().lookupFileName(fname,
  22423. (helper->getFetchFlags() & FFdatafileoptional) != 0,
  22424. true, true,
  22425. _queryFactory.queryWorkUnit(), true, isActivityCodeSigned()));
  22426. if (datafile)
  22427. map.setown(datafile->getFileMap());
  22428. }
  22429. }
  22430. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  22431. {
  22432. return new CRoxieServerFetchActivity(_ctx, this, _probeManager, remoteId, map);
  22433. }
  22434. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  22435. {
  22436. if (datafile)
  22437. addXrefFileInfo(reply, datafile);
  22438. else
  22439. {
  22440. // Temporarily resolve the file
  22441. Owned<IHThorFetchBaseArg> helper = (IHThorFetchBaseArg *) helperFactory();
  22442. if ((helper->getFetchFlags() & (FFvarfilename|FFdynamicfilename)) == 0)
  22443. {
  22444. OwnedRoxieString fileName(helper->getFileName());
  22445. Owned<const IResolvedFile> temp = queryFactory.queryPackage().lookupFileName(fileName, true, true, false, queryFactory.queryWorkUnit(), true, isActivityCodeSigned());
  22446. if (temp)
  22447. addXrefFileInfo(reply, temp);
  22448. }
  22449. }
  22450. }
  22451. virtual const StatisticsMapping &queryStatsMapping() const
  22452. {
  22453. return diskStatistics;
  22454. }
  22455. };
  22456. IRoxieServerActivityFactory *createRoxieServerFetchActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId)
  22457. {
  22458. return new CRoxieServerFetchActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId);
  22459. }
  22460. // MORE - is there any point keeping this now?
  22461. class CRoxieServerDummyActivityFactory : public CRoxieServerActivityFactory // not a real activity - just used to properly link files
  22462. {
  22463. public:
  22464. Owned<const IResolvedFile> indexfile;
  22465. Owned<const IResolvedFile> datafile;
  22466. StringAttr fileName;
  22467. StringAttr indexName;
  22468. Owned<IKeyArray> keySet;
  22469. Owned<IFileIOArray> files;
  22470. CRoxieServerDummyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool isLoadDataOnly)
  22471. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  22472. {
  22473. try // does not want any missing file errors to be fatal, or throw traps - just log it
  22474. {
  22475. if (_graphNode.getPropBool("att[@name='_isSpill']/@value", false) || _graphNode.getPropBool("att[@name='_isSpillGlobal']/@value", false))
  22476. return; // ignore 'spills'
  22477. bool isLocal = _graphNode.getPropBool("att[@name='local']/@value") && queryFactory.queryChannel()!=0;
  22478. ThorActivityKind kind = getActivityKind(_graphNode);
  22479. if (kind != TAKdiskwrite && kind != TAKspillwrite && kind != TAKindexwrite && kind != TAKpiperead && kind != TAKpipewrite)
  22480. {
  22481. fileName.set(queryNodeFileName(_graphNode, kind));
  22482. indexName.set(queryNodeIndexName(_graphNode, kind));
  22483. if (indexName && !allFilesDynamic && !queryFactory.isDynamic())
  22484. {
  22485. bool isOpt = pretendAllOpt || _graphNode.getPropBool("att[@name='_isIndexOpt']/@value");
  22486. indexfile.setown(queryFactory.queryPackage().lookupFileName(indexName, isOpt, true, true, queryFactory.queryWorkUnit(), true, isActivityCodeSigned()));
  22487. if (indexfile)
  22488. keySet.setown(indexfile->getKeyArray(isOpt, isLocal ? queryFactory.queryChannel() : 0));
  22489. }
  22490. if (fileName && !allFilesDynamic && !queryFactory.isDynamic())
  22491. {
  22492. bool isOpt = pretendAllOpt || _graphNode.getPropBool("att[@name='_isOpt']/@value");
  22493. datafile.setown(_queryFactory.queryPackage().lookupFileName(fileName, isOpt, true, true, queryFactory.queryWorkUnit(), true, isActivityCodeSigned()));
  22494. if (datafile)
  22495. {
  22496. if (isLocal)
  22497. files.setown(datafile->getIFileIOArray(isOpt, queryFactory.queryChannel()));
  22498. }
  22499. }
  22500. }
  22501. }
  22502. catch(IException *E)
  22503. {
  22504. StringBuffer errors;
  22505. E->errorMessage(errors);
  22506. OERRLOG("%s File error = %s", (isLoadDataOnly) ? "LOADDATAONLY" : "SUSPENDED QUERY", errors.str());
  22507. E->Release();
  22508. }
  22509. }
  22510. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, 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(), sanitizeSourceFile(__FILE__), __LINE__); }
  22511. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  22512. {
  22513. if (!allFilesDynamic)
  22514. {
  22515. if (datafile)
  22516. addXrefFileInfo(reply, datafile);
  22517. if (indexfile)
  22518. addXrefFileInfo(reply, indexfile);
  22519. }
  22520. else
  22521. {
  22522. Owned<const IResolvedFile> temp;
  22523. if (fileName.length())
  22524. {
  22525. temp.setown(queryFactory.queryPackage().lookupFileName(fileName, true, true, false, queryFactory.queryWorkUnit(), true, isActivityCodeSigned()));
  22526. if (temp)
  22527. addXrefFileInfo(reply, temp);
  22528. }
  22529. if (indexName.length())
  22530. {
  22531. temp.setown(queryFactory.queryPackage().lookupFileName(indexName, true, true, false, queryFactory.queryWorkUnit(), true, isActivityCodeSigned()));
  22532. if (temp)
  22533. addXrefFileInfo(reply, temp);
  22534. }
  22535. }
  22536. }
  22537. };
  22538. IRoxieServerActivityFactory *createRoxieServerDummyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool isLoadDataOnly)
  22539. {
  22540. return new CRoxieServerDummyActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, isLoadDataOnly);
  22541. }
  22542. //=====================================================================================================
  22543. // Keyed joins...
  22544. //
  22545. // Input records are pulled by a puller thread, which checks each LHS record to determine which (if any) channels it
  22546. // may have RHS matches on, and sends the relevant fields to the relevant agents.
  22547. // A separate thread (the caller's thread) is waiting on agent replies, and once it has all replies for a given LHS record or group of records, calls
  22548. // the transform and returns rows that are created.
  22549. // 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)
  22550. //
  22551. //=====================================================================================================
  22552. class CJoinGroup;
  22553. interface IJoinProcessor
  22554. {
  22555. virtual void processEOG() = 0;
  22556. virtual CJoinGroup *createJoinGroup(const void *row) = 0;
  22557. virtual void noteEndReceived(CJoinGroup *jg, unsigned candidateCount) = 0;
  22558. virtual bool fireException(IException *E) = 0;
  22559. virtual void processCompletedGroups() = 0;
  22560. };
  22561. //------------------------------------------------------------------------------------------------------
  22562. // Class CJoinGroup has a record per LHS row, plus (if preserving grouping) a 'head of group' record
  22563. // It gathers all the corresponding RHS rows, keeping track of how may agent transactions are pending in endMarkersPending
  22564. // If preserving groups, the 'head of group' record keeps track of how many LHS records in the group are still incomplete.
  22565. // CJoinGroup records are allocated out of the Roxie row memory manager by overloading operator new, so that they are included in the
  22566. // per-query limits etc (Note that the pointer array block is not though).
  22567. // Because of that, the exact size is significant - especially whether fit just under or just over a chunking threshold...
  22568. //
  22569. // There are two phases to the life of a JoinGroup - it is created by the puller thread that is also firing off agent requests
  22570. // notePending will be called once for every agent request. Puller thread calls noteEndReceived(0) once when done - this corresponds to the
  22571. // initial count when created.
  22572. // Agent replies and are noted by the consumer thread calling addRightMatch() and noteEndReceived(n).
  22573. // Once endMarkersPending reaches 0, JoinGroup is complete. Last thread to call noteEndReceived will process the rows and destroy the group.
  22574. // There is no need for a critsec because although multiple threads will access at different times, only the consumer thread will
  22575. // access any modifiable member variables while endMarkersPending != 0 (i.e. complete() is false). Once complete returns true there is a single
  22576. // remaining reference and the JoinGroup will be processed and destroyed.
  22577. //
  22578. //------------------------------------------------------------------------------------------------------
  22579. class CJoinGroup : public CInterface
  22580. {
  22581. protected:
  22582. const void *left; // LHS row
  22583. PointerArrayOf<KeyedJoinHeader> rows; // matching RHS rows
  22584. atomic_t endMarkersPending; // How many agent responses still waiting for
  22585. CJoinGroup *groupStart; // Head of group, or NULL if not grouping
  22586. unsigned lastPartNo;
  22587. unsigned pos;
  22588. unsigned candidates; // Number of RHS keyed candidates - note this may not be the same as rows.ordinality()
  22589. public:
  22590. void *operator new(size_t size, IRowManager *a, unsigned activityId)
  22591. {
  22592. return a->allocate(size, activityId);
  22593. }
  22594. void operator delete(void *ptr, IRowManager *a, unsigned activityId)
  22595. {
  22596. ReleaseRoxieRow(ptr);
  22597. }
  22598. void operator delete(void *ptr)
  22599. {
  22600. ReleaseRoxieRow(ptr);
  22601. }
  22602. public:
  22603. CJoinGroup(const void *_left, CJoinGroup *_groupStart)
  22604. {
  22605. #ifdef TRACE_JOINGROUPS
  22606. DBGLOG("Creating joinGroup %p, groupstart %p", this, _groupStart);
  22607. #endif
  22608. candidates = 0;
  22609. lastPartNo = 0;
  22610. pos = 0;
  22611. left = _left;
  22612. groupStart = _groupStart;
  22613. if (_groupStart)
  22614. {
  22615. atomic_inc(&_groupStart->endMarkersPending);
  22616. }
  22617. atomic_set(&endMarkersPending, 1);
  22618. }
  22619. ~CJoinGroup()
  22620. {
  22621. #ifdef TRACE_JOINGROUPS
  22622. DBGLOG("Destroying joinGroup %p", this);
  22623. #endif
  22624. if (left)
  22625. {
  22626. ReleaseRoxieRow(left);
  22627. roxiemem::ReleaseRoxieRowArray(rows.ordinality(), (const void * *)rows.getArray());
  22628. rows.kill();
  22629. }
  22630. }
  22631. inline bool isHeadRecord() const
  22632. {
  22633. return left==NULL;
  22634. }
  22635. inline bool complete() const
  22636. {
  22637. return atomic_read(&endMarkersPending) == 0;
  22638. }
  22639. #ifdef TRACE_JOINGROUPS
  22640. inline void notePending(unsigned lineNo)
  22641. #else
  22642. inline void notePending()
  22643. #endif
  22644. {
  22645. assert(!complete());
  22646. atomic_inc(&endMarkersPending);
  22647. #ifdef TRACE_JOINGROUPS
  22648. DBGLOG("CJoinGroup::notePending %p from %d, count became %d group count %d", this, lineNo, atomic_read(&endMarkersPending), groupStart ? atomic_read(&groupStart->endMarkersPending) : 0);
  22649. #endif
  22650. }
  22651. inline bool inGroup(CJoinGroup *leader) const
  22652. {
  22653. return groupStart==leader;
  22654. }
  22655. inline const KeyedJoinHeader *queryRow(unsigned idx) const
  22656. {
  22657. // Single threaded by now
  22658. assert(complete());
  22659. return rows.item(idx);
  22660. }
  22661. #ifdef TRACE_JOINGROUPS
  22662. bool noteEndReceived(unsigned candidateCount, unsigned lineNo)
  22663. #else
  22664. bool noteEndReceived(unsigned candidateCount)
  22665. #endif
  22666. {
  22667. assert(!complete());
  22668. if (candidateCount)
  22669. {
  22670. candidates += candidateCount;
  22671. }
  22672. #ifdef TRACE_JOINGROUPS
  22673. 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);
  22674. #endif
  22675. // NOTE - as soon as endMarkersPending and groupStart->endMarkersPending are decremented to zero this object may get released asynchronously by other threads
  22676. // There must therefore be nothing in this method after them that acceses member variables. Think of it as a delete this...
  22677. // In particular, we can't safely reference groupStart after the dec_and_test of endMarkersPending, hence copy local first
  22678. CJoinGroup *localGroupStart = groupStart;
  22679. if (atomic_dec_and_test(&endMarkersPending))
  22680. {
  22681. if (localGroupStart)
  22682. return atomic_dec_and_test(&localGroupStart->endMarkersPending);
  22683. else
  22684. return true;
  22685. }
  22686. else
  22687. return false;
  22688. }
  22689. inline const void *queryLeft() const
  22690. {
  22691. return left;
  22692. }
  22693. void addRightMatch(KeyedJoinHeader *right)
  22694. {
  22695. assert(!complete());
  22696. unsigned short partNo = right->partNo;
  22697. if (partNo != lastPartNo)
  22698. {
  22699. // MORE - should we binchop? If we did we would need to be careful to find LAST match
  22700. if (partNo > lastPartNo)
  22701. pos = rows.length();
  22702. while (pos>0)
  22703. {
  22704. if (rows.item(pos-1)->partNo <= partNo)
  22705. break;
  22706. pos--;
  22707. }
  22708. lastPartNo = partNo;
  22709. }
  22710. rows.add(right, pos);
  22711. pos++;
  22712. }
  22713. inline unsigned rowsSeen() const
  22714. {
  22715. assert(complete());
  22716. return rows.length();
  22717. }
  22718. inline unsigned candidateCount() const
  22719. {
  22720. assert(complete());
  22721. return candidates;
  22722. }
  22723. };
  22724. #ifdef TRACE_JOINGROUPS
  22725. #define notePending() notePending(__LINE__)
  22726. #define noteEndReceived(a) noteEndReceived(a, __LINE__)
  22727. #endif
  22728. class KeyedJoinRemoteAdaptor : public CRemoteResultAdaptor // MORE - not sure it should be derived from this - makes processed all wrong, for example
  22729. {
  22730. private:
  22731. SafeQueueOf<const void, true> ready;
  22732. public:
  22733. IHThorKeyedJoinArg &helper;
  22734. unsigned joinProcessed;
  22735. bool isFullKey;
  22736. bool eof;
  22737. bool isSimple;
  22738. bool allPulled;
  22739. ActivityTimeAccumulator activityStats;
  22740. unsigned activityId;
  22741. RecordPullerThread &puller;
  22742. SafeQueueOf<const void, true> injected; // Used in isSimple mode
  22743. Owned<IEngineRowAllocator> ccdRecordAllocator;
  22744. IJoinProcessor &processor;
  22745. KeyedJoinRemoteAdaptor(IRoxieAgentContext *_ctx, IRoxieServerErrorHandler *_errorHandler, const RemoteActivityId &_remoteId, IHThorKeyedJoinArg &_helper,
  22746. IRoxieServerActivity &_activity, bool _isFullKey, bool _isSimple,
  22747. RecordPullerThread &_puller, IJoinProcessor &_processor)
  22748. : CRemoteResultAdaptor(_ctx, _errorHandler, _remoteId, 0, _helper, _activity, true, true),
  22749. helper(_helper),
  22750. isFullKey(_isFullKey),
  22751. isSimple(_isSimple),
  22752. activityId(_activity.queryId()),
  22753. puller(_puller),
  22754. processor(_processor)
  22755. {
  22756. joinProcessed = 0;
  22757. allPulled = false;
  22758. eof = false;
  22759. }
  22760. virtual void onCreate(IHThorArg *_colocalArg)
  22761. {
  22762. CRemoteResultAdaptor::onCreate(_colocalArg);
  22763. ccdRecordAllocator.setown(activity.createRowAllocator(helper.queryJoinFieldsRecordSize()));
  22764. }
  22765. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  22766. {
  22767. eof = false;
  22768. joinProcessed = 0;
  22769. activityStats.totalCycles = 0;
  22770. allPulled = false;
  22771. assertex(ready.ordinality()==0);
  22772. CRemoteResultAdaptor::start(parentExtractSize, parentExtract, paused);
  22773. }
  22774. virtual void reset()
  22775. {
  22776. CRemoteResultAdaptor::reset();
  22777. while (ready.ordinality())
  22778. {
  22779. const void *goer = ready.dequeue();
  22780. if (goer)
  22781. ReleaseRoxieRow(goer);
  22782. }
  22783. while (injected.ordinality())
  22784. {
  22785. const void *goer = injected.dequeue();
  22786. if (goer)
  22787. ReleaseRoxieRow(goer);
  22788. }
  22789. }
  22790. inline void addResult(const void *row)
  22791. {
  22792. ready.enqueue(row);
  22793. }
  22794. virtual unsigned __int64 queryTotalCycles() const
  22795. {
  22796. return activityStats.totalCycles;
  22797. }
  22798. virtual const void *nextRow()
  22799. {
  22800. ActivityTimer t(activityStats, timeActivities);
  22801. for (;;)
  22802. {
  22803. if (eof)
  22804. return NULL;
  22805. processAgentResults();
  22806. if (ready.ordinality())
  22807. {
  22808. const void *result = ready.dequeue();
  22809. if (result)
  22810. joinProcessed++;
  22811. return result;
  22812. }
  22813. else
  22814. eof = true;
  22815. }
  22816. }
  22817. void gatherStats(CRuntimeStatisticCollection & merged) const
  22818. {
  22819. CRemoteResultAdaptor::gatherStats(merged);
  22820. if (ccdRecordAllocator)
  22821. ccdRecordAllocator->gatherStats(merged);
  22822. }
  22823. private:
  22824. void processAgentResults()
  22825. {
  22826. while (!ready.ordinality())
  22827. {
  22828. KeyedJoinHeader *fetchedData;
  22829. if (isSimple)
  22830. {
  22831. while (!allPulled && !injected.ordinality())
  22832. {
  22833. if (!puller.pullRecords(1))
  22834. {
  22835. puller.done();
  22836. allPulled = true;
  22837. }
  22838. }
  22839. fetchedData = (KeyedJoinHeader *) injected.dequeue();
  22840. }
  22841. else
  22842. fetchedData = (KeyedJoinHeader *) CRemoteResultAdaptor::nextRow();
  22843. if (fetchedData)
  22844. {
  22845. CJoinGroup *thisGroup = fetchedData->thisGroup;
  22846. if (fetchedData->partNo == (unsigned short) -1)
  22847. {
  22848. #ifdef TRACE_JOINGROUPS
  22849. CTXLOG("Got end for group %p", thisGroup);
  22850. #endif
  22851. unsigned candidateCount = (unsigned) fetchedData->fpos;
  22852. ReleaseRoxieRow(fetchedData);
  22853. processor.noteEndReceived(thisGroup, candidateCount); // note - this can throw exception. So release fetchdata before calling
  22854. }
  22855. else
  22856. {
  22857. #ifdef TRACE_JOINGROUPS
  22858. CTXLOG("Reading another %d bytes for group %p data", ccdRecordSize, thisGroup);
  22859. #endif
  22860. thisGroup->addRightMatch(fetchedData);
  22861. if (isFullKey)
  22862. {
  22863. #ifdef TRACE_JOINGROUPS
  22864. CTXLOG("Calling noteEndReceived for record returned from FETCH of full keyed join");
  22865. #endif
  22866. processor.noteEndReceived(thisGroup, 0); // note - this can throw exception. So release fetchdata before calling
  22867. }
  22868. }
  22869. }
  22870. else
  22871. break;
  22872. }
  22873. }
  22874. };
  22875. class CRoxieServerFullKeyedJoinHead: public CRoxieServerActivity, implements IRecordPullerCallback, implements IRoxieServerErrorHandler
  22876. {
  22877. IHThorKeyedJoinArg &helper;
  22878. Owned<IKeyManager> tlk;
  22879. Linked<IKeyArray> keySet;
  22880. Linked<ITranslatorSet> translators;
  22881. CRemoteResultAdaptor remote;
  22882. RecordPullerThread puller;
  22883. IOutputMetaData *indexReadMeta;
  22884. IJoinProcessor *joinHandler;
  22885. bool variableIndexFileName;
  22886. bool indexReadInputRecordVariable;
  22887. bool isLocal;
  22888. Owned<IEngineRowAllocator> indexReadAllocator;
  22889. Owned<const IResolvedFile> varFileInfo;
  22890. IFinalRoxieInput *indexReadInput;
  22891. unsigned indexReadIdx = 0;
  22892. IIndexReadActivityInfo *rootIndex;
  22893. public:
  22894. CRoxieServerFullKeyedJoinHead(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, IKeyArray * _keySet, ITranslatorSet *_translators, IOutputMetaData *_indexReadMeta, IJoinProcessor *_joinHandler, bool _isLocal)
  22895. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  22896. helper((IHThorKeyedJoinArg &)basehelper),
  22897. tlk(createLocalKeyManager(helper.queryIndexRecordSize()->queryRecordAccessor(true), NULL, this, helper.hasNewSegmentMonitors(), !isBlind())),
  22898. keySet(_keySet),
  22899. translators(_translators),
  22900. remote(_ctx, this, _remoteId, 0, helper, *this, true, true),
  22901. puller(false),
  22902. indexReadMeta(_indexReadMeta),
  22903. joinHandler(_joinHandler),
  22904. isLocal(_isLocal)
  22905. {
  22906. variableIndexFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((helper.getJoinFlags() & (JFvarindexfilename|JFdynamicindexfilename|JFindexfromactivity)) != 0);
  22907. indexReadInputRecordVariable = indexReadMeta->isVariableSize();
  22908. indexReadInput = NULL;
  22909. rootIndex = NULL;
  22910. }
  22911. virtual const IResolvedFile *queryVarFileInfo() const
  22912. {
  22913. return varFileInfo;
  22914. }
  22915. virtual void onCreate(IHThorArg *_colocalParent)
  22916. {
  22917. CRoxieServerActivity::onCreate(_colocalParent);
  22918. remote.onCreate(_colocalParent);
  22919. indexReadAllocator.setown(createRowAllocator(indexReadMeta));
  22920. }
  22921. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  22922. {
  22923. if (!idx)
  22924. puller.setInput(this, _sourceIdx, _in);
  22925. else if (idx==1)
  22926. {
  22927. indexReadInput = _in;
  22928. indexReadIdx = _sourceIdx;
  22929. }
  22930. else
  22931. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", sanitizeSourceFile(__FILE__), __LINE__);
  22932. }
  22933. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks)
  22934. {
  22935. puller.connectInputStreams(ctx, consumerOrdered);
  22936. //No rows are read from indexReadInput, so no need to extract the streams
  22937. return CRoxieServerActivity::getOutputStreams(ctx, idx, streams, consumerOptions, consumerOrdered, orderedCallbacks);
  22938. }
  22939. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  22940. {
  22941. switch (idx)
  22942. {
  22943. case 0:
  22944. return puller.queryInput();
  22945. case 1:
  22946. return indexReadInput;
  22947. default:
  22948. return NULL;
  22949. }
  22950. }
  22951. virtual void serializeExtra(MemoryBuffer &out)
  22952. {
  22953. if (helper.getJoinFlags() & JFindexfromactivity)
  22954. {
  22955. assertex(rootIndex);
  22956. const RemoteActivityId& indexId = rootIndex->queryRemoteId();
  22957. indexId.serialize(out);
  22958. // could mess about reserving space for length then patching it again, to avoid copy, but probably not worth it
  22959. MemoryBuffer tmp;
  22960. rootIndex->queryActivity()->serializeCreateStartContext(tmp);
  22961. if (rootIndex->queryActivity()->queryVarFileInfo())
  22962. {
  22963. rootIndex->queryActivity()->queryVarFileInfo()->queryTimeStamp().serialize(tmp);
  22964. tmp.append(rootIndex->queryActivity()->queryVarFileInfo()->queryCheckSum());
  22965. }
  22966. unsigned ctxlen = tmp.length();
  22967. out.append(ctxlen).append(tmp);
  22968. }
  22969. }
  22970. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  22971. {
  22972. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  22973. if (indexReadInput)
  22974. {
  22975. indexReadInput->start(parentExtractSize, parentExtract, true); // paused=true because we don't want to actually run the index read
  22976. rootIndex = indexReadInput->queryIndexReadActivity();
  22977. if (!rootIndex)
  22978. throw MakeStringException(ROXIE_INTERNAL_ERROR,"Index in keyed join %d could not be resolved", queryId());
  22979. }
  22980. remote.onStart(parentExtractSize, parentExtract);
  22981. remote.setLimits(helper.getRowLimit(), (unsigned __int64) -1, I64C(0x7FFFFFFFFFFFFFFF));
  22982. if (rootIndex)
  22983. {
  22984. varFileInfo.setown(rootIndex->getVarFileInfo());
  22985. translators.setown(rootIndex->getTranslators());
  22986. keySet.setown(rootIndex->getKeySet());
  22987. }
  22988. else if (variableIndexFileName)
  22989. {
  22990. OwnedRoxieString indexFileName(helper.getIndexFileName());
  22991. varFileInfo.setown(resolveLFNIndex(indexFileName, (helper.getJoinFlags() & JFindexoptional) != 0, factory->isActivityCodeSigned()));
  22992. if (varFileInfo)
  22993. {
  22994. unsigned expectedCrc = helper.getIndexFormatCrc();
  22995. unsigned projectedCrc = helper.getProjectedIndexFormatCrc();
  22996. translators.setown(varFileInfo->getTranslators(projectedCrc, helper.queryProjectedIndexRecordSize(), expectedCrc, helper.queryIndexRecordSize(), getEnableFieldTranslation(), FileFormatMode::index, factory->queryQueryFactory().queryQueryName()));
  22997. keySet.setown(varFileInfo->getKeyArray(false, isLocal ? factory->queryQueryFactory().queryChannel() : 0));
  22998. }
  22999. }
  23000. puller.start(parentExtractSize, parentExtract, paused, ctx->queryOptions().fullKeyedJoinPreload, false, ctx);
  23001. }
  23002. virtual void stop()
  23003. {
  23004. puller.stop();
  23005. CRoxieServerActivity::stop();
  23006. }
  23007. virtual void reset()
  23008. {
  23009. CRoxieServerActivity::reset();
  23010. puller.reset();
  23011. if (varFileInfo)
  23012. {
  23013. keySet.clear();
  23014. varFileInfo.clear();
  23015. }
  23016. }
  23017. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  23018. {
  23019. if (idx==(unsigned)-1)
  23020. idx = 0;
  23021. return idx ? NULL : &remote;
  23022. }
  23023. virtual void processRow(const void *row)
  23024. {
  23025. // MORE - this code seems to be pretty much duplicated below in half-keyed....
  23026. // called from front puller thread
  23027. // buffer up an IndexRead request
  23028. if (keySet && helper.leftCanMatch(row))
  23029. {
  23030. RtlDynamicRowBuilder extractedBuilder(indexReadAllocator);
  23031. unsigned indexReadSize = helper.extractIndexReadFields(extractedBuilder, row);
  23032. OwnedConstRoxieRow extracted;
  23033. if (indexReadSize)
  23034. extracted.setown(extractedBuilder.finalizeRowClear(indexReadSize));
  23035. CJoinGroup *jg = joinHandler->createJoinGroup(row);
  23036. for (unsigned partNo = 0; partNo < keySet->length(); partNo++)
  23037. {
  23038. IKeyIndexBase *thisBase = keySet->queryKeyPart(partNo);
  23039. if (thisBase)
  23040. {
  23041. unsigned fileNo = 0;
  23042. IKeyIndex *thisKey = thisBase->queryPart(fileNo);
  23043. try
  23044. {
  23045. tlk->setKey(thisKey);
  23046. if (!thisKey->isTopLevelKey())
  23047. tlk->setLayoutTranslator(translators->queryTranslator(fileNo));
  23048. else
  23049. tlk->setLayoutTranslator(nullptr);
  23050. helper.createSegmentMonitors(tlk, extracted);
  23051. if (rootIndex)
  23052. rootIndex->mergeSegmentMonitors(tlk);
  23053. tlk->finishSegmentMonitors();
  23054. tlk->reset();
  23055. for (;;)
  23056. {
  23057. typedef const void * cvp;
  23058. if (thisKey->isTopLevelKey())
  23059. {
  23060. bool locallySorted = !thisKey->isFullySorted();
  23061. while (locallySorted || tlk->lookup(false))
  23062. {
  23063. unsigned agentPart = locallySorted ? tlk->getPartition() : (unsigned)extractFpos(tlk);
  23064. if (locallySorted || agentPart)
  23065. {
  23066. cvp *outputBuffer = (cvp *) remote.getMem(agentPart, fileNo, indexReadSize + sizeof(cvp) + (indexReadInputRecordVariable ? sizeof(unsigned) : 0));
  23067. *outputBuffer++ = jg;
  23068. if (indexReadInputRecordVariable)
  23069. {
  23070. *(unsigned *) outputBuffer = indexReadSize;
  23071. outputBuffer = (cvp*) (((unsigned *) outputBuffer) + 1);
  23072. }
  23073. jg->notePending();
  23074. memcpy(outputBuffer, extracted, indexReadSize);
  23075. if (!agentPart)
  23076. {
  23077. for (unsigned i = 1; i < numChannels; i++)
  23078. jg->notePending();
  23079. }
  23080. if (locallySorted)
  23081. break;
  23082. }
  23083. }
  23084. }
  23085. else
  23086. {
  23087. // MORE - this code seems to be duplicated in half keyed
  23088. unsigned accepted = 0;
  23089. unsigned rejected = 0;
  23090. Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult();
  23091. jg->notePending();
  23092. unsigned candidateCount = 0;
  23093. ScopedAtomic<unsigned> indexRecordsRead(::indexRecordsRead);
  23094. ScopedAtomic<unsigned> postFiltered(::postFiltered);
  23095. while (tlk->lookup(true))
  23096. {
  23097. candidateCount++;
  23098. indexRecordsRead++;
  23099. KLBlobProviderAdapter adapter(tlk);
  23100. const byte *indexRow = tlk->queryKeyBuffer();
  23101. size_t fposOffset = tlk->queryRowSize() - sizeof(offset_t);
  23102. offset_t fpos = rtlReadBigUInt8(indexRow + fposOffset);
  23103. if (helper.indexReadMatch(extracted, indexRow, &adapter))
  23104. {
  23105. KeyedJoinHeader *rhs = (KeyedJoinHeader *) ctx->queryRowManager().allocate(KEYEDJOIN_RECORD_SIZE(0), activityId);
  23106. rhs->fpos = fpos;
  23107. rhs->thisGroup = jg;
  23108. rhs->partNo = partNo;
  23109. result->append(rhs);
  23110. }
  23111. else
  23112. {
  23113. rejected++;
  23114. postFiltered++;
  23115. }
  23116. }
  23117. // output an end marker for the matches to this group
  23118. KeyedJoinHeader *endMarker = (KeyedJoinHeader *) ctx->queryRowManager().allocate(KEYEDJOIN_RECORD_SIZE(0), activityId);
  23119. endMarker->fpos = (offset_t) candidateCount;
  23120. endMarker->thisGroup = jg;
  23121. endMarker->partNo = (unsigned short) -1;
  23122. result->append(endMarker);
  23123. remote.injectResult(result.getClear());
  23124. if (accepted)
  23125. noteStatistic(StNumIndexAccepted, accepted);
  23126. if (rejected)
  23127. noteStatistic(StNumIndexRejected, rejected);
  23128. }
  23129. if (++fileNo < thisBase->numParts())
  23130. {
  23131. thisKey = thisBase->queryPart(fileNo);
  23132. tlk->setKey(thisKey);
  23133. if (!thisKey->isTopLevelKey())
  23134. tlk->setLayoutTranslator(translators->queryTranslator(fileNo));
  23135. else
  23136. tlk->setLayoutTranslator(nullptr);
  23137. tlk->reset();
  23138. }
  23139. else
  23140. break;
  23141. }
  23142. tlk->releaseSegmentMonitors();
  23143. tlk->setKey(NULL);
  23144. }
  23145. catch (...)
  23146. {
  23147. tlk->releaseSegmentMonitors();
  23148. tlk->setKey(NULL);
  23149. throw;
  23150. }
  23151. }
  23152. }
  23153. joinHandler->noteEndReceived(jg, 0);
  23154. }
  23155. else
  23156. {
  23157. joinHandler->noteEndReceived(joinHandler->createJoinGroup(row), 0);
  23158. }
  23159. }
  23160. void processGroup(const ConstPointerArray &)
  23161. {
  23162. throwUnexpected();
  23163. }
  23164. virtual void processEOG()
  23165. {
  23166. joinHandler->processEOG();
  23167. }
  23168. virtual void processDone()
  23169. {
  23170. // called from puller thread
  23171. remote.flush();
  23172. remote.senddone();
  23173. }
  23174. virtual void onLimitExceeded(bool isKeyed)
  23175. {
  23176. if (traceLevel > 4)
  23177. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  23178. if (isKeyed)
  23179. throwUnexpected();
  23180. helper.onLimitExceeded();
  23181. }
  23182. virtual const void *createLimitFailRow(bool isKeyed)
  23183. {
  23184. throwUnexpected();
  23185. }
  23186. virtual bool fireException(IException *e)
  23187. {
  23188. // called from puller thread on failure
  23189. remote.fireException(LINK(e));
  23190. return joinHandler->fireException(e);
  23191. }
  23192. virtual const void *nextRow()
  23193. {
  23194. throwUnexpected(); // I am nobody's input
  23195. }
  23196. virtual void gatherStats(CRuntimeStatisticCollection & merged) const override
  23197. {
  23198. CRoxieServerActivity::gatherStats(merged);
  23199. remote.gatherStats(merged);
  23200. if (indexReadAllocator)
  23201. indexReadAllocator->gatherStats(merged);
  23202. }
  23203. };
  23204. class CRoxieServerKeyedJoinBase : public CRoxieServerActivity, implements IRecordPullerCallback, implements IRoxieServerErrorHandler, implements IJoinProcessor
  23205. {
  23206. protected:
  23207. IHThorKeyedJoinArg &helper;
  23208. KeyedJoinRemoteAdaptor remote;
  23209. RecordPullerThread puller;
  23210. OwnedConstRoxieRow defaultRight;
  23211. Owned<IEngineRowAllocator> defaultRightAllocator;
  23212. unsigned joinFlags;
  23213. unsigned atMost;
  23214. unsigned atmostsTriggered;
  23215. unsigned abortLimit;
  23216. unsigned keepLimit;
  23217. bool limitFail;
  23218. bool limitOnFail;
  23219. bool preserveGroups;
  23220. bool cloneLeft;
  23221. bool isSimple;
  23222. bool isLocal;
  23223. ThorActivityKind activityKind;
  23224. CJoinGroup *groupStart;
  23225. CriticalSection groupsCrit;
  23226. QueueOf<CJoinGroup, false> groups;
  23227. IFinalRoxieInput *indexReadInput;
  23228. unsigned indexReadIdx = 0;
  23229. Owned<IStrandJunction> indexReadJunction;
  23230. IEngineRowStream *indexReadStream = NULL; // Never actually pulled
  23231. IIndexReadActivityInfo *rootIndex;
  23232. void createDefaultRight()
  23233. {
  23234. if (!defaultRight)
  23235. {
  23236. if (!defaultRightAllocator)
  23237. defaultRightAllocator.setown(createRowAllocator(helper.queryJoinFieldsRecordSize()));
  23238. RtlDynamicRowBuilder rowBuilder(defaultRightAllocator);
  23239. size32_t thisSize = helper.createDefaultRight(rowBuilder);
  23240. defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
  23241. }
  23242. }
  23243. public:
  23244. CRoxieServerKeyedJoinBase(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _joinFlags
  23245. , bool isFull, bool _isSimple, bool _isLocal)
  23246. : CRoxieServerActivity(_ctx, _factory, _probeManager),
  23247. helper((IHThorKeyedJoinArg &)basehelper),
  23248. remote(_ctx, this, _remoteId, helper, *this, isFull, _isSimple, puller, *this),
  23249. puller(false),
  23250. joinFlags(_joinFlags),
  23251. atMost(0),
  23252. abortLimit(0),
  23253. keepLimit(0),
  23254. limitFail(false),
  23255. limitOnFail(false),
  23256. preserveGroups(meta.isGrouped()),
  23257. cloneLeft(false),
  23258. isSimple(_isSimple),
  23259. isLocal(_isLocal)
  23260. {
  23261. groupStart = NULL;
  23262. activityKind = _factory->getKind();
  23263. indexReadInput = NULL;
  23264. rootIndex = NULL;
  23265. atmostsTriggered = 0;
  23266. // MORE - code would be easier to read if I got more values from helper rather than passing from factory
  23267. }
  23268. virtual void onCreate(IHThorArg *_colocalParent)
  23269. {
  23270. CRoxieServerActivity::onCreate(_colocalParent);
  23271. remote.onCreate(_colocalParent);
  23272. }
  23273. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  23274. {
  23275. if (!idx)
  23276. puller.setInput(this, _sourceIdx, _in);
  23277. else if (idx==1)
  23278. {
  23279. indexReadInput = _in;
  23280. indexReadIdx = _sourceIdx;
  23281. }
  23282. else
  23283. throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", sanitizeSourceFile(__FILE__), __LINE__);
  23284. }
  23285. virtual void connectInputStreams(bool consumerOrdered)
  23286. {
  23287. CRoxieServerActivity::connectInputStreams(consumerOrdered);
  23288. puller.connectInputStreams(ctx, consumerOrdered);
  23289. if (indexReadInput)
  23290. indexReadStream = connectSingleStream(ctx, indexReadInput, indexReadIdx, indexReadJunction, consumerOrdered); // We never actually pull the stream
  23291. }
  23292. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  23293. {
  23294. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  23295. if (indexReadInput)
  23296. {
  23297. indexReadInput->start(parentExtractSize, parentExtract, true); // paused=true because we don't want to actually run the index read
  23298. rootIndex = indexReadInput->queryIndexReadActivity();
  23299. if (!rootIndex)
  23300. throw MakeStringException(ROXIE_INTERNAL_ERROR,"Index in keyed join %d could not be resolved", queryId());
  23301. }
  23302. remote.onStart(parentExtractSize, parentExtract);
  23303. remote.setLimits(helper.getRowLimit(), (unsigned __int64) -1, I64C(0x7FFFFFFFFFFFFFFF));
  23304. atmostsTriggered = 0;
  23305. atMost = helper.getJoinLimit();
  23306. if (atMost == 0) atMost = (unsigned)-1;
  23307. abortLimit = helper.getMatchAbortLimit();
  23308. if (abortLimit == 0 || atMost != (unsigned) -1) abortLimit = (unsigned)-1;
  23309. keepLimit = helper.getKeepLimit();
  23310. if (keepLimit == 0) keepLimit = (unsigned)-1;
  23311. getLimitType(joinFlags, limitFail, limitOnFail);
  23312. cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
  23313. if ((joinFlags & JFleftouter) || limitOnFail)
  23314. createDefaultRight();
  23315. }
  23316. virtual void stop()
  23317. {
  23318. puller.stop();
  23319. if (indexReadStream)
  23320. indexReadStream->stop(); // Could probably do this as soon as re have fetched rootIndex?
  23321. CRoxieServerActivity::stop();
  23322. }
  23323. virtual unsigned __int64 queryLocalCycles() const
  23324. {
  23325. __int64 localCycles = remote.activityStats.totalCycles;
  23326. localCycles -= puller.queryTotalCycles(); // MORE - debatable... but probably fair.
  23327. if (localCycles < 0)
  23328. localCycles = 0;
  23329. return localCycles;
  23330. }
  23331. virtual void gatherStats(CRuntimeStatisticCollection & merged) const override
  23332. {
  23333. CRoxieServerActivity::gatherStats(merged);
  23334. remote.gatherStats(merged);
  23335. }
  23336. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  23337. {
  23338. if (idx==0)
  23339. return puller.queryInput();
  23340. else if (idx==1)
  23341. return indexReadInput;
  23342. else
  23343. return NULL;
  23344. }
  23345. virtual void reset()
  23346. {
  23347. activityStats.totalCycles = remote.activityStats.totalCycles;
  23348. remote.activityStats.totalCycles = 0;
  23349. processed = remote.joinProcessed;
  23350. remote.joinProcessed = 0;
  23351. defaultRight.clear();
  23352. if (indexReadInput)
  23353. indexReadInput->reset();
  23354. if (atmostsTriggered)
  23355. noteStatistic(StNumAtmostTriggered, atmostsTriggered);
  23356. CRoxieServerActivity::reset();
  23357. puller.reset();
  23358. while (groups.ordinality())
  23359. {
  23360. ::Release(groups.dequeue());
  23361. }
  23362. }
  23363. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  23364. {
  23365. if (idx==(unsigned)-1)
  23366. idx = 0;
  23367. return idx ? NULL : &remote;
  23368. }
  23369. virtual CJoinGroup *createJoinGroup(const void *row)
  23370. {
  23371. // NOTE - we need to protect access to queue, since it's also modified by consumer thread. Groupstart is only modified by puller thread.
  23372. CriticalBlock c(groupsCrit);
  23373. if (preserveGroups && !groupStart)
  23374. {
  23375. groupStart = new (&ctx->queryRowManager(), activityId) CJoinGroup(NULL, NULL);
  23376. groups.enqueue(groupStart);
  23377. }
  23378. CJoinGroup *jg = new (&ctx->queryRowManager(), activityId) CJoinGroup(row, groupStart);
  23379. groups.enqueue(jg);
  23380. return jg;
  23381. }
  23382. void endGroup()
  23383. {
  23384. CriticalBlock c(groupsCrit);
  23385. if (groupStart)
  23386. noteEndReceived(groupStart, 0);
  23387. groupStart = NULL;
  23388. }
  23389. virtual void noteEndReceived(CJoinGroup *jg, unsigned candidateCount)
  23390. {
  23391. if (jg->noteEndReceived(candidateCount))
  23392. processCompletedGroups();
  23393. }
  23394. void processCompletedGroups()
  23395. {
  23396. CriticalBlock c(groupsCrit);
  23397. while (groups.ordinality())
  23398. {
  23399. if (!groups.head()->complete())
  23400. break;
  23401. Owned<CJoinGroup> head = groups.dequeue();
  23402. if (preserveGroups)
  23403. {
  23404. assert(head->isHeadRecord());
  23405. assert(groups.head()->inGroup(head));
  23406. unsigned joinGroupSize = 0;
  23407. while (groups.ordinality() && groups.head()->inGroup(head))
  23408. {
  23409. Owned<CJoinGroup> finger = groups.dequeue();
  23410. joinGroupSize += doJoinGroup(finger);
  23411. }
  23412. if (joinGroupSize)
  23413. remote.addResult(NULL);
  23414. }
  23415. else
  23416. doJoinGroup(head);
  23417. }
  23418. }
  23419. void failLimit(const void * left)
  23420. {
  23421. helper.onMatchAbortLimitExceeded();
  23422. CommonXmlWriter xmlwrite(0);
  23423. if (input && input->queryOutputMeta() && input->queryOutputMeta()->hasXML())
  23424. {
  23425. input->queryOutputMeta()->toXML((byte *) left, xmlwrite);
  23426. }
  23427. throw MakeStringException(ROXIE_JOIN_ERROR, "More than %d match candidates in keyed join %d for row %s", abortLimit, queryId(), xmlwrite.str());
  23428. }
  23429. virtual bool needsAllocator() const { return true; }
  23430. unsigned doTransform(const void *left, const void *right, offset_t fpos_or_count, IException *except, const void **group, unsigned counter)
  23431. {
  23432. if (cloneLeft && !except)
  23433. {
  23434. LinkRoxieRow(left);
  23435. remote.addResult((void *) left);
  23436. return 1;
  23437. }
  23438. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  23439. unsigned outSize;
  23440. try
  23441. {
  23442. outSize = except ? helper.onFailTransform(rowBuilder, left, right, fpos_or_count, except) :
  23443. (activityKind == TAKkeyeddenormalizegroup) ? helper.transform(rowBuilder, left, right, (unsigned)fpos_or_count, group) :
  23444. helper.transform(rowBuilder, left, right, fpos_or_count, counter);
  23445. }
  23446. catch (IException *E)
  23447. {
  23448. throw makeWrappedException(E);
  23449. }
  23450. if (outSize)
  23451. {
  23452. const void *shrunk = rowBuilder.finalizeRowClear(outSize);
  23453. remote.addResult(shrunk);
  23454. return 1;
  23455. }
  23456. else
  23457. return 0;
  23458. }
  23459. unsigned doJoinGroup(CJoinGroup *jg)
  23460. {
  23461. unsigned matched = jg->rowsSeen();
  23462. unsigned added = 0;
  23463. const void *left = jg->queryLeft();
  23464. if (jg->candidateCount() > abortLimit)
  23465. {
  23466. if (limitFail)
  23467. failLimit(left);
  23468. if (ctx->queryDebugContext())
  23469. ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
  23470. if (limitOnFail)
  23471. {
  23472. Owned<IException> except;
  23473. try
  23474. {
  23475. failLimit(left);
  23476. }
  23477. catch(IException * e)
  23478. {
  23479. except.setown(e);
  23480. }
  23481. added = doTransform(left, defaultRight, 0, except, NULL, 0);
  23482. }
  23483. }
  23484. else if (!matched || jg->candidateCount() > atMost)
  23485. {
  23486. if (jg->candidateCount() > atMost)
  23487. atmostsTriggered++;
  23488. switch (joinFlags & JFtypemask)
  23489. {
  23490. case JFleftouter:
  23491. case JFleftonly:
  23492. switch (activityKind)
  23493. {
  23494. case TAKkeyedjoin:
  23495. case TAKkeyeddenormalizegroup:
  23496. added = doTransform(left, defaultRight, 0, NULL, NULL, 0);
  23497. break;
  23498. case TAKkeyeddenormalize:
  23499. LinkRoxieRow(left);
  23500. remote.addResult((void *) left);
  23501. added++;
  23502. break;
  23503. }
  23504. break;
  23505. }
  23506. }
  23507. else if (!(joinFlags & JFexclude))
  23508. {
  23509. unsigned idx = 0;
  23510. switch (activityKind)
  23511. {
  23512. case TAKkeyedjoin:
  23513. while (idx < matched)
  23514. {
  23515. const KeyedJoinHeader *rhs = jg->queryRow(idx);
  23516. added += doTransform(left, &rhs->rhsdata, 0, NULL, NULL, idx+1);
  23517. if (added==keepLimit)
  23518. break;
  23519. idx++;
  23520. }
  23521. break;
  23522. case TAKkeyeddenormalize:
  23523. {
  23524. OwnedConstRoxieRow newLeft;
  23525. newLeft.set(left);
  23526. unsigned rowSize = 0;
  23527. unsigned rightAdded = 0;
  23528. while (idx < matched)
  23529. {
  23530. const KeyedJoinHeader *rhs = jg->queryRow(idx);
  23531. try
  23532. {
  23533. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  23534. size32_t transformedSize = helper.transform(rowBuilder, newLeft, &rhs->rhsdata, rhs->fpos, idx+1);
  23535. if (transformedSize)
  23536. {
  23537. rowSize = transformedSize;
  23538. newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
  23539. rightAdded++;
  23540. if (rightAdded==keepLimit)
  23541. break;
  23542. }
  23543. idx++;
  23544. }
  23545. catch (IException *E)
  23546. {
  23547. throw makeWrappedException(E);
  23548. }
  23549. }
  23550. if (rowSize)
  23551. {
  23552. remote.addResult(newLeft.getClear());
  23553. added++;
  23554. }
  23555. }
  23556. break;
  23557. case TAKkeyeddenormalizegroup:
  23558. {
  23559. ConstPointerArray extractedRows;
  23560. while (idx < matched && idx < keepLimit)
  23561. {
  23562. const KeyedJoinHeader *rhs = jg->queryRow(idx);
  23563. extractedRows.append((void *) &rhs->rhsdata);
  23564. idx++;
  23565. }
  23566. added += doTransform(left, extractedRows.item(0), extractedRows.ordinality(), NULL, (const void * *)extractedRows.getArray(), 0);
  23567. }
  23568. break;
  23569. }
  23570. }
  23571. return added;
  23572. }
  23573. virtual void processDone()
  23574. {
  23575. // called from puller thread
  23576. remote.flush();
  23577. remote.senddone();
  23578. }
  23579. virtual void onLimitExceeded(bool isKeyed)
  23580. {
  23581. if (traceLevel > 4)
  23582. DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
  23583. if (isKeyed)
  23584. throwUnexpected();
  23585. helper.onLimitExceeded();
  23586. }
  23587. virtual const void *createLimitFailRow(bool isKeyed)
  23588. {
  23589. throwUnexpected();
  23590. }
  23591. virtual bool fireException(IException *e)
  23592. {
  23593. // called from puller thread on failure
  23594. return remote.fireException(e);
  23595. }
  23596. virtual const void *nextRow()
  23597. {
  23598. throwUnexpected(); // I am nobody's input
  23599. }
  23600. };
  23601. #ifdef _MSC_VER
  23602. #pragma warning ( push )
  23603. #pragma warning ( disable: 4355 )
  23604. #endif
  23605. class CRoxieServerKeyedJoinActivity : public CRoxieServerKeyedJoinBase
  23606. {
  23607. CRoxieServerFullKeyedJoinHead head;
  23608. Owned<IEngineRowAllocator> fetchInputAllocator;
  23609. Linked<IFilePartMap> map;
  23610. bool variableFetchFileName;
  23611. Owned<const IResolvedFile> varFetchFileInfo;
  23612. CachedOutputMetaData fetchInputFields;
  23613. public:
  23614. CRoxieServerKeyedJoinActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_headId, IKeyArray * _key, ITranslatorSet *_keyTranslators, IOutputMetaData *_indexReadMeta,
  23615. const RemoteActivityId &_tailId, IFilePartMap *_map, unsigned _joinFlags, bool _isLocal)
  23616. : CRoxieServerKeyedJoinBase(_ctx, _factory, _probeManager, _tailId, _joinFlags, true, false, _isLocal),
  23617. head(_ctx, _factory, _probeManager, _headId, _key, _keyTranslators, _indexReadMeta, this, _isLocal),
  23618. map(_map)
  23619. {
  23620. CRoxieServerKeyedJoinBase::setInput(0, 0, head.queryOutput(0));
  23621. variableFetchFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((helper.getFetchFlags() & (FFvarfilename|FFdynamicfilename)) != 0);
  23622. }
  23623. virtual const IResolvedFile *queryVarFileInfo() const
  23624. {
  23625. return varFetchFileInfo;
  23626. }
  23627. virtual void onCreate(IHThorArg *_colocalParent)
  23628. {
  23629. CRoxieServerKeyedJoinBase::onCreate(_colocalParent);
  23630. head.onCreate(_colocalParent);
  23631. fetchInputFields.set(helper.queryFetchInputRecordSize());
  23632. fetchInputAllocator.setown(createRowAllocator(helper.queryFetchInputRecordSize()));
  23633. }
  23634. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  23635. {
  23636. CRoxieServerKeyedJoinBase::start(parentExtractSize, parentExtract, paused);
  23637. if (variableFetchFileName)
  23638. {
  23639. bool isFetchOpt = (helper.getFetchFlags() & FFdatafileoptional) != 0;
  23640. OwnedRoxieString fname(helper.getFileName());
  23641. varFetchFileInfo.setown(resolveLFNFlat(fname, isFetchOpt, factory->isActivityCodeSigned()));
  23642. if (varFetchFileInfo)
  23643. {
  23644. // Note - we don't need to do any translation on the disk part of full-keyed joins
  23645. map.setown(varFetchFileInfo->getFileMap());
  23646. }
  23647. }
  23648. puller.start(parentExtractSize, parentExtract, paused, ctx->queryOptions().keyedJoinPreload, false, ctx);
  23649. }
  23650. virtual void setInput(unsigned idx, unsigned _sourceIdx, IFinalRoxieInput *_in)
  23651. {
  23652. head.setInput(idx, _sourceIdx, _in);
  23653. }
  23654. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks)
  23655. {
  23656. connectInputStreams(consumerOrdered);
  23657. return head.getOutputStreams(ctx, idx, streams, consumerOptions, consumerOrdered, orderedCallbacks);
  23658. }
  23659. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  23660. {
  23661. return head.queryInput(idx);
  23662. }
  23663. virtual void processRow(const void *_rhs)
  23664. {
  23665. // called from puller thread
  23666. KeyedJoinHeader *rhs = (KeyedJoinHeader *) _rhs;
  23667. CJoinGroup *jg = rhs->thisGroup;
  23668. if (rhs->partNo != (unsigned short) -1)
  23669. {
  23670. unsigned partNo = map->mapOffset(rhs->fpos);
  23671. RtlDynamicRowBuilder fetchBuilder(fetchInputAllocator, true);
  23672. size32_t fisize = helper.extractFetchFields(fetchBuilder, jg->queryLeft());
  23673. if (fetchInputFields.isVariableSize())
  23674. {
  23675. KeyedJoinHeader *outRow = (KeyedJoinHeader *) remote.getMem(partNo, 0, KEYEDJOIN_RECORD_SIZE(fisize + sizeof(fisize)));
  23676. memcpy(outRow, rhs, KEYEDJOIN_RECORD_SIZE(0)); // MORE - copy constructor might be more appropriate....
  23677. ReleaseRoxieRow(rhs);
  23678. jg->notePending();
  23679. memcpy(&outRow->rhsdata, &fisize, sizeof(fisize));
  23680. memcpy((&outRow->rhsdata)+sizeof(fisize), fetchBuilder.row(), fisize);
  23681. }
  23682. else
  23683. {
  23684. KeyedJoinHeader *outRow = (KeyedJoinHeader *) remote.getMem(partNo, 0, KEYEDJOIN_RECORD_SIZE(fisize));
  23685. memcpy(outRow, rhs, KEYEDJOIN_RECORD_SIZE(0)); // MORE - copy constructor might be more appropriate....
  23686. ReleaseRoxieRow(rhs);
  23687. jg->notePending();
  23688. memcpy(&outRow->rhsdata, fetchBuilder.row(), fisize);
  23689. }
  23690. }
  23691. else
  23692. {
  23693. unsigned candidateCount = (unsigned) rhs->fpos;
  23694. // CTXLOG("Full keyed join - all results back from index");
  23695. ReleaseRoxieRow(rhs);
  23696. noteEndReceived(jg, candidateCount); // may throw exception - so release row before calling
  23697. }
  23698. }
  23699. virtual void processEOG()
  23700. {
  23701. // called from front puller thread
  23702. if (preserveGroups)
  23703. endGroup();
  23704. }
  23705. void processGroup(const ConstPointerArray &)
  23706. {
  23707. throwUnexpected();
  23708. }
  23709. virtual void gatherStats(CRuntimeStatisticCollection & merged) const override
  23710. {
  23711. CRoxieServerKeyedJoinBase::gatherStats(merged);
  23712. if (fetchInputAllocator)
  23713. fetchInputAllocator->gatherStats(merged);
  23714. }
  23715. };
  23716. #ifdef _MSC_VER
  23717. #pragma warning ( pop )
  23718. #endif
  23719. class CRoxieServerHalfKeyedJoinActivity : public CRoxieServerKeyedJoinBase
  23720. {
  23721. IOutputMetaData *indexReadMeta;
  23722. Owned<IEngineRowAllocator> indexReadAllocator;
  23723. Owned<IKeyManager> tlk;
  23724. bool variableIndexFileName;
  23725. bool indexReadInputRecordVariable;
  23726. Owned<const IResolvedFile> varFileInfo;
  23727. Linked<ITranslatorSet> translators;
  23728. Linked<IKeyArray> keySet;
  23729. Owned<IOutputMetaData> joinPrefixedMeta;
  23730. Owned<IEngineRowAllocator> joinFieldsAllocator;
  23731. public:
  23732. CRoxieServerHalfKeyedJoinActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, IKeyArray * _keySet, ITranslatorSet *_translators,
  23733. IOutputMetaData *_indexReadMeta, unsigned _joinFlags, bool _isSimple, bool _isLocal)
  23734. : CRoxieServerKeyedJoinBase(_ctx, _factory, _probeManager, _remoteId, _joinFlags, false, _isSimple, _isLocal),
  23735. indexReadMeta(_indexReadMeta),
  23736. tlk(createLocalKeyManager(helper.queryIndexRecordSize()->queryRecordAccessor(true), NULL, this, helper.hasNewSegmentMonitors(), !isBlind())),
  23737. translators(_translators),
  23738. keySet(_keySet)
  23739. {
  23740. variableIndexFileName = allFilesDynamic || factory->queryQueryFactory().isDynamic() || ((helper.getJoinFlags() & (JFvarindexfilename|JFdynamicindexfilename|JFindexfromactivity)) != 0);
  23741. indexReadInputRecordVariable = indexReadMeta->isVariableSize();
  23742. }
  23743. virtual void serializeExtra(MemoryBuffer &out)
  23744. {
  23745. if (helper.getJoinFlags() & JFindexfromactivity)
  23746. {
  23747. assertex(rootIndex);
  23748. const RemoteActivityId& indexId = rootIndex->queryRemoteId();
  23749. indexId.serialize(out);
  23750. // could mess about reserving space for length then patching it again, to avoid copy, but probably not worth it
  23751. MemoryBuffer tmp;
  23752. rootIndex->queryActivity()->serializeCreateStartContext(tmp);
  23753. if (rootIndex->queryActivity()->queryVarFileInfo())
  23754. {
  23755. rootIndex->queryActivity()->queryVarFileInfo()->queryTimeStamp().serialize(tmp);
  23756. tmp.append(rootIndex->queryActivity()->queryVarFileInfo()->queryCheckSum());
  23757. }
  23758. unsigned ctxlen = tmp.length();
  23759. out.append(ctxlen).append(tmp);
  23760. }
  23761. }
  23762. virtual void onCreate(IHThorArg *_colocalParent)
  23763. {
  23764. CRoxieServerKeyedJoinBase::onCreate(_colocalParent);
  23765. indexReadAllocator.setown(createRowAllocator(indexReadMeta));
  23766. IOutputMetaData *joinFieldsMeta = helper.queryJoinFieldsRecordSize();
  23767. joinPrefixedMeta.setown(new CPrefixedOutputMeta(KEYEDJOIN_RECORD_SIZE(0), joinFieldsMeta)); // MORE - not sure if we really need this
  23768. joinFieldsAllocator.setown(createRowAllocator(joinPrefixedMeta));
  23769. }
  23770. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  23771. {
  23772. CRoxieServerKeyedJoinBase::start(parentExtractSize, parentExtract, paused);
  23773. if (rootIndex)
  23774. {
  23775. varFileInfo.setown(rootIndex->getVarFileInfo());
  23776. translators.setown(rootIndex->getTranslators());
  23777. keySet.setown(rootIndex->getKeySet());
  23778. }
  23779. else if (variableIndexFileName)
  23780. {
  23781. OwnedRoxieString indexFileName(helper.getIndexFileName());
  23782. varFileInfo.setown(resolveLFNIndex(indexFileName, (helper.getJoinFlags() & JFindexoptional) != 0, factory->isActivityCodeSigned()));
  23783. if (varFileInfo)
  23784. {
  23785. unsigned expectedCrc = helper.getIndexFormatCrc();
  23786. unsigned projectedCrc = helper.getProjectedIndexFormatCrc();
  23787. translators.setown(varFileInfo->getTranslators(projectedCrc, helper.queryProjectedIndexRecordSize(), expectedCrc, helper.queryIndexRecordSize(), getEnableFieldTranslation(), FileFormatMode::index, factory->queryQueryFactory().queryQueryName()));
  23788. keySet.setown(varFileInfo->getKeyArray(false, isLocal ? factory->queryQueryFactory().queryChannel() : 0));
  23789. }
  23790. }
  23791. puller.start(parentExtractSize, parentExtract, paused, ctx->queryOptions().keyedJoinPreload, isSimple, ctx);
  23792. }
  23793. virtual const IResolvedFile *queryVarFileInfo() const
  23794. {
  23795. return varFileInfo;
  23796. }
  23797. virtual void reset()
  23798. {
  23799. CRoxieServerKeyedJoinBase::reset();
  23800. if (varFileInfo)
  23801. {
  23802. keySet.clear();
  23803. varFileInfo.clear();
  23804. }
  23805. }
  23806. virtual void processRow(const void *row)
  23807. {
  23808. // called from front puller thread
  23809. // buffer up an IndexRead request
  23810. if (keySet && helper.leftCanMatch(row))
  23811. {
  23812. RtlDynamicRowBuilder extractBuilder(indexReadAllocator);
  23813. unsigned indexReadRecordSize = helper.extractIndexReadFields(extractBuilder, row);
  23814. OwnedConstRoxieRow extracted;
  23815. if (indexReadRecordSize)
  23816. extracted.setown(extractBuilder.finalizeRowClear(indexReadRecordSize));
  23817. CJoinGroup *jg = createJoinGroup(row);
  23818. for (unsigned partNo = 0; partNo < keySet->length(); partNo++)
  23819. {
  23820. IKeyIndexBase *thisBase = keySet->queryKeyPart(partNo);
  23821. if (thisBase)
  23822. {
  23823. unsigned fileNo = 0;
  23824. IKeyIndex *thisKey = thisBase->queryPart(fileNo);
  23825. tlk->setKey(thisKey);
  23826. if (thisKey && !thisKey->isTopLevelKey())
  23827. tlk->setLayoutTranslator(translators->queryTranslator(fileNo));
  23828. else
  23829. tlk->setLayoutTranslator(nullptr);
  23830. helper.createSegmentMonitors(tlk, extracted);
  23831. if (rootIndex)
  23832. rootIndex->mergeSegmentMonitors(tlk);
  23833. tlk->finishSegmentMonitors();
  23834. try
  23835. {
  23836. tlk->reset();
  23837. for (;;)
  23838. {
  23839. typedef const void * cvp;
  23840. if (thisKey && thisKey->isTopLevelKey())
  23841. {
  23842. bool locallySorted = (!thisKey->isFullySorted());
  23843. while (locallySorted || tlk->lookup(false))
  23844. {
  23845. unsigned agentPart = locallySorted ? tlk->getPartition() : (unsigned)extractFpos(tlk);
  23846. if (locallySorted || agentPart)
  23847. {
  23848. cvp *outputBuffer = (cvp *) remote.getMem(agentPart, fileNo, indexReadRecordSize + sizeof(cvp) + (indexReadInputRecordVariable ? sizeof(unsigned) : 0));
  23849. *outputBuffer++ = jg;
  23850. if (indexReadInputRecordVariable)
  23851. {
  23852. *(unsigned *) outputBuffer = indexReadRecordSize;
  23853. outputBuffer = (cvp *) (((unsigned *) outputBuffer) + 1);
  23854. }
  23855. jg->notePending();
  23856. memcpy(outputBuffer, extracted, indexReadRecordSize);
  23857. if (!agentPart)
  23858. {
  23859. for (unsigned i = 1; i < numChannels; i++)
  23860. jg->notePending();
  23861. }
  23862. if (locallySorted)
  23863. break;
  23864. }
  23865. }
  23866. }
  23867. else
  23868. {
  23869. unsigned accepted = 0;
  23870. unsigned rejected = 0;
  23871. Owned<CRowArrayMessageResult> result;
  23872. if (!isSimple)
  23873. result.setown(new CRowArrayMessageResult());
  23874. // MORE - This code seems to be duplicated in keyedJoinHead
  23875. jg->notePending();
  23876. unsigned candidateCount = 0;
  23877. ScopedAtomic<unsigned> indexRecordsRead(::indexRecordsRead);
  23878. ScopedAtomic<unsigned> postFiltered(::postFiltered);
  23879. while (tlk->lookup(true))
  23880. {
  23881. candidateCount++;
  23882. indexRecordsRead++;
  23883. KLBlobProviderAdapter adapter(tlk);
  23884. const byte *indexRow = tlk->queryKeyBuffer();
  23885. size_t fposOffset = tlk->queryRowSize() - sizeof(offset_t);
  23886. offset_t fpos = rtlReadBigUInt8(indexRow + fposOffset);
  23887. if (helper.indexReadMatch(extracted, indexRow, &adapter))
  23888. {
  23889. RtlDynamicRowBuilder rb(joinFieldsAllocator, true);
  23890. CPrefixedRowBuilder pb(KEYEDJOIN_RECORD_SIZE(0), rb);
  23891. accepted++;
  23892. KLBlobProviderAdapter adapter(tlk);
  23893. helper.extractJoinFields(pb, indexRow, &adapter);
  23894. KeyedJoinHeader *rec = (KeyedJoinHeader *) rb.getUnfinalizedClear(); // lack of finalize ok as unserialized data here.
  23895. rec->fpos = fpos;
  23896. rec->thisGroup = jg;
  23897. rec->partNo = partNo;
  23898. if (isSimple)
  23899. remote.injected.enqueue(rec);
  23900. else
  23901. result->append(rec);
  23902. }
  23903. else
  23904. {
  23905. rejected++;
  23906. postFiltered++;
  23907. }
  23908. }
  23909. // output an end marker for the matches to this group
  23910. KeyedJoinHeader *rec = (KeyedJoinHeader *) ctx->queryRowManager().allocate(KEYEDJOIN_RECORD_SIZE(0), activityId);
  23911. rec->fpos = (offset_t) candidateCount;
  23912. rec->thisGroup = jg;
  23913. rec->partNo = (unsigned short) -1;
  23914. if (isSimple)
  23915. remote.injected.enqueue(rec);
  23916. else
  23917. {
  23918. result->append(rec);
  23919. remote.injectResult(result.getClear());
  23920. }
  23921. if (accepted)
  23922. noteStatistic(StNumIndexAccepted, accepted);
  23923. if (rejected)
  23924. noteStatistic(StNumIndexRejected, rejected);
  23925. }
  23926. if (++fileNo < thisBase->numParts())
  23927. {
  23928. thisKey = thisBase->queryPart(fileNo);
  23929. tlk->setKey(thisKey);
  23930. if (thisKey && !thisKey->isTopLevelKey())
  23931. tlk->setLayoutTranslator(translators->queryTranslator(fileNo));
  23932. else
  23933. tlk->setLayoutTranslator(nullptr);
  23934. tlk->reset();
  23935. }
  23936. else
  23937. break;
  23938. }
  23939. tlk->releaseSegmentMonitors();
  23940. tlk->setKey(NULL);
  23941. }
  23942. catch (...)
  23943. {
  23944. tlk->releaseSegmentMonitors();
  23945. tlk->setKey(NULL);
  23946. throw;
  23947. }
  23948. }
  23949. }
  23950. noteEndReceived(jg, 0);
  23951. }
  23952. else
  23953. {
  23954. noteEndReceived(createJoinGroup(row), 0);
  23955. }
  23956. }
  23957. virtual void processEOG()
  23958. {
  23959. // called from front puller thread
  23960. if (preserveGroups)
  23961. endGroup();
  23962. }
  23963. void processGroup(const ConstPointerArray &)
  23964. {
  23965. throwUnexpected();
  23966. }
  23967. virtual void gatherStats(CRuntimeStatisticCollection & merged) const override
  23968. {
  23969. CRoxieServerKeyedJoinBase::gatherStats(merged);
  23970. if (indexReadAllocator)
  23971. indexReadAllocator->gatherStats(merged);
  23972. if (joinFieldsAllocator)
  23973. joinFieldsAllocator->gatherStats(merged);
  23974. }
  23975. };
  23976. class CRoxieServerKeyedJoinActivityFactory : public CRoxieServerMultiInputFactory
  23977. {
  23978. Owned<const IResolvedFile> indexfile;
  23979. Owned<const IResolvedFile> datafile;
  23980. Owned<IKeyArray> keySet;
  23981. Owned<ITranslatorSet> keyTranslators; // For the indexes
  23982. RemoteActivityId headId;
  23983. RemoteActivityId tailId;
  23984. IOutputMetaData *indexReadMeta;
  23985. Owned<IFilePartMap> map;
  23986. Owned<ITranslatorSet> translators; // for the disk files on local full keyed joins
  23987. Owned<IFileIOArray> files;
  23988. unsigned joinFlags;
  23989. bool isHalfKeyed;
  23990. bool isLocal;
  23991. bool variableFetchFileName;
  23992. bool variableIndexFileName;
  23993. bool isSimple;
  23994. public:
  23995. CRoxieServerKeyedJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_headId, const RemoteActivityId &_tailId)
  23996. : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), headId(_headId), tailId(_tailId)
  23997. {
  23998. Owned<IHThorKeyedJoinArg> helper = (IHThorKeyedJoinArg *) helperFactory();
  23999. isLocal = _graphNode.getPropBool("att[@name='local']/@value") && queryFactory.queryChannel()!=0;
  24000. isSimple = isLocal;
  24001. rtlDataAttr indexLayoutMeta;
  24002. size32_t indexLayoutSize = 0;
  24003. if(!helper->getIndexLayout(indexLayoutSize, indexLayoutMeta.refdata()))
  24004. assertex(indexLayoutSize== 0);
  24005. joinFlags = helper->getJoinFlags();
  24006. variableIndexFileName = allFilesDynamic || _queryFactory.isDynamic() || ((joinFlags & (JFvarindexfilename|JFdynamicindexfilename|JFindexfromactivity)) != 0);
  24007. variableFetchFileName = allFilesDynamic || _queryFactory.isDynamic() || ((helper->getFetchFlags() & (FFvarfilename|FFdynamicfilename)) != 0);
  24008. if (!variableIndexFileName)
  24009. {
  24010. bool isOpt = (joinFlags & JFindexoptional) != 0;
  24011. OwnedRoxieString indexFileName(helper->getIndexFileName());
  24012. indexfile.setown(queryFactory.queryPackage().lookupFileName(indexFileName, isOpt, true, true, queryFactory.queryWorkUnit(), true, isActivityCodeSigned()));
  24013. if (indexfile)
  24014. {
  24015. unsigned expectedCrc = helper->getIndexFormatCrc();
  24016. unsigned projectedCrc = helper->getProjectedIndexFormatCrc();
  24017. keyTranslators.setown(indexfile->getTranslators(projectedCrc, helper->queryProjectedIndexRecordSize(), expectedCrc, helper->queryIndexRecordSize(), getEnableFieldTranslation(), FileFormatMode::index, queryFactory.queryQueryName()));
  24018. keySet.setown(indexfile->getKeyArray(isOpt, isLocal ? queryFactory.queryChannel() : 0));
  24019. }
  24020. }
  24021. if (keySet && keySet->length()==1 && !isSimple)
  24022. {
  24023. IKeyIndexBase *thisBase = keySet->queryKeyPart(0);
  24024. if (thisBase->numParts()==1 && !thisBase->queryPart(0)->isTopLevelKey() && !_queryFactory.queryOptions().disableLocalOptimizations)
  24025. isSimple = true;
  24026. // MORE - if it's a variable filename then it MAY be simple, we don't know. Tough.
  24027. }
  24028. if (!simpleLocalKeyedJoins)
  24029. isSimple = false;
  24030. isHalfKeyed = !helper->diskAccessRequired();
  24031. indexReadMeta = QUERYINTERFACE(helper->queryIndexReadInputRecordSize(), IOutputMetaData);
  24032. if (!isHalfKeyed && !variableFetchFileName)
  24033. {
  24034. bool isFetchOpt = (helper->getFetchFlags() & FFdatafileoptional) != 0;
  24035. datafile.setown(_queryFactory.queryPackage().lookupFileName(queryNodeFileName(_graphNode, _kind), isFetchOpt, true, true, _queryFactory.queryWorkUnit(), true, isActivityCodeSigned()));
  24036. if (datafile)
  24037. {
  24038. if (isLocal) // Not sure this works
  24039. {
  24040. unsigned expectedCrc = helper->getDiskFormatCrc();
  24041. unsigned projectedCrc = helper->getProjectedFormatCrc();
  24042. translators.setown(datafile->getTranslators(expectedCrc, helper->queryProjectedDiskRecordSize(), projectedCrc, helper->queryDiskRecordSize(), getEnableFieldTranslation(), FileFormatMode::flat, queryFactory.queryQueryName()));
  24043. files.setown(datafile->getIFileIOArray(isFetchOpt, queryFactory.queryChannel()));
  24044. }
  24045. else
  24046. map.setown(datafile->getFileMap());
  24047. }
  24048. }
  24049. }
  24050. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  24051. {
  24052. if (isHalfKeyed)
  24053. return new CRoxieServerHalfKeyedJoinActivity(_ctx, this, _probeManager,
  24054. headId, keySet, keyTranslators, indexReadMeta, joinFlags, isSimple, isLocal);
  24055. else
  24056. return new CRoxieServerKeyedJoinActivity(_ctx, this, _probeManager,
  24057. headId, keySet, keyTranslators, indexReadMeta,
  24058. tailId, map, joinFlags, isLocal); // MORE - not sure local variant actually works - should be using files not map, for example?
  24059. }
  24060. virtual const StatisticsMapping &queryStatsMapping() const
  24061. {
  24062. return keyedJoinStatistics;
  24063. }
  24064. virtual void getXrefInfo(IPropertyTree &reply, const IRoxieContextLogger &logctx) const
  24065. {
  24066. if (datafile)
  24067. addXrefFileInfo(reply, datafile);
  24068. if (indexfile)
  24069. addXrefFileInfo(reply, indexfile);
  24070. }
  24071. };
  24072. IRoxieServerActivityFactory *createRoxieServerKeyedJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, const RemoteActivityId &_remoteId, const RemoteActivityId &_remoteId2)
  24073. {
  24074. return new CRoxieServerKeyedJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _remoteId, _remoteId2);
  24075. }
  24076. //=================================================================================
  24077. class CRoxieServerSoapActivityBase : public CRoxieServerActivity, implements ISoapCallRowProvider, implements IRoxieAbortMonitor
  24078. {
  24079. protected:
  24080. Owned<ISoapCallHelper> soaphelper;
  24081. IHThorSoapActionArg & helper;
  24082. StringBuffer authToken;
  24083. bool eof;
  24084. CriticalSection crit;
  24085. ClientCertificate *pClientCert;
  24086. public:
  24087. IMPLEMENT_IINTERFACE_USING(CRoxieServerActivity)
  24088. CRoxieServerSoapActivityBase(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  24089. : CRoxieServerActivity(_ctx, _factory, _probeManager), helper((IHThorSoapActionArg &)basehelper)
  24090. {
  24091. eof = false;
  24092. if (clientCert.certificate.length() > 0 && clientCert.privateKey.length() > 0 && clientCert.passphrase.length() > 0)
  24093. pClientCert = &clientCert;
  24094. else
  24095. pClientCert = NULL;
  24096. }
  24097. // ISoapCallRowProvider
  24098. virtual IHThorSoapActionArg * queryActionHelper() { return &helper; };
  24099. virtual IHThorSoapCallArg * queryCallHelper() { return NULL; };
  24100. virtual const void * getNextRow() { return NULL; };
  24101. virtual void releaseRow(const void * r) { ReleaseRoxieRow(r); };
  24102. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  24103. {
  24104. eof = false;
  24105. CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
  24106. authToken.append(ctx->queryAuthToken());
  24107. }
  24108. virtual void reset()
  24109. {
  24110. // MORE - Shouldn't we make sure thread is stopped etc???
  24111. soaphelper.clear();
  24112. CRoxieServerActivity::reset();
  24113. }
  24114. // IRoxieAbortMonitor
  24115. virtual void checkForAbort() { checkAbort(); }
  24116. };
  24117. class CRoxieServerSoapActionBase : public CRoxieServerSoapActivityBase
  24118. {
  24119. public:
  24120. CRoxieServerSoapActionBase(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  24121. : CRoxieServerSoapActivityBase(_ctx, _factory, _probeManager)
  24122. {
  24123. }
  24124. virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
  24125. {
  24126. CriticalBlock b(ecrit);
  24127. if (exception)
  24128. throw(exception.getLink());
  24129. if (!executed)
  24130. {
  24131. try
  24132. {
  24133. executed = true;
  24134. start(parentExtractSize, parentExtract, false);
  24135. {
  24136. ActivityTimer t(activityStats, timeActivities); // unfortunately this is not really best place for seeing in debugger.
  24137. onExecute();
  24138. }
  24139. stop();
  24140. }
  24141. catch (IException *E)
  24142. {
  24143. E = makeWrappedException(E);
  24144. ctx->notifyAbort(E);
  24145. exception.set(E);
  24146. abort();
  24147. throw E;
  24148. }
  24149. }
  24150. }
  24151. virtual void onExecute() = 0;
  24152. virtual void reset() override
  24153. {
  24154. executed = false;
  24155. exception.clear();
  24156. CRoxieServerSoapActivityBase::reset();
  24157. }
  24158. protected:
  24159. bool executed = false;
  24160. Linked<IException> exception;
  24161. CriticalSection ecrit;
  24162. };
  24163. //---------------------------------------------------------------------------
  24164. class CRoxieServerSoapRowCallActivity : public CRoxieServerSoapActivityBase
  24165. {
  24166. IHThorSoapCallArg & callHelper;
  24167. public:
  24168. CRoxieServerSoapRowCallActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  24169. : CRoxieServerSoapActivityBase(_ctx, _factory, _probeManager), callHelper((IHThorSoapCallArg &)basehelper)
  24170. {
  24171. }
  24172. virtual IHThorSoapCallArg * queryCallHelper()
  24173. {
  24174. return &callHelper;
  24175. }
  24176. virtual bool needsAllocator() const { return true; }
  24177. virtual const void *nextRow()
  24178. {
  24179. ActivityTimer t(activityStats, timeActivities);
  24180. if(eof) return NULL;
  24181. try
  24182. {
  24183. if (soaphelper == NULL)
  24184. {
  24185. if (factory->getKind()==TAKhttp_rowdataset)
  24186. soaphelper.setown(createHttpCallHelper(this, rowAllocator, authToken.str(), SCrow, pClientCert, *this, this));
  24187. else
  24188. soaphelper.setown(createSoapCallHelper(this, rowAllocator, authToken.str(), SCrow, pClientCert, *this, this));
  24189. soaphelper->start();
  24190. }
  24191. OwnedConstRoxieRow ret = soaphelper->getRow();
  24192. if (!ret)
  24193. {
  24194. eof = true;
  24195. return NULL;
  24196. }
  24197. ++processed;
  24198. return ret.getClear();
  24199. }
  24200. catch (IException *E)
  24201. {
  24202. throw makeWrappedException(E);
  24203. }
  24204. }
  24205. };
  24206. class CRoxieServerSoapRowCallActivityFactory : public CRoxieServerActivityFactory
  24207. {
  24208. public:
  24209. CRoxieServerSoapRowCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  24210. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  24211. {
  24212. }
  24213. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  24214. {
  24215. return new CRoxieServerSoapRowCallActivity(_ctx, this, _probeManager);
  24216. }
  24217. virtual const StatisticsMapping &queryStatsMapping() const
  24218. {
  24219. return soapStatistics;
  24220. }
  24221. };
  24222. IRoxieServerActivityFactory *createRoxieServerSoapRowCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  24223. {
  24224. return new CRoxieServerSoapRowCallActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  24225. }
  24226. //---------------------------------------------------------------------------
  24227. class CRoxieServerSoapRowActionActivity : public CRoxieServerSoapActionBase
  24228. {
  24229. public:
  24230. CRoxieServerSoapRowActionActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  24231. : CRoxieServerSoapActionBase(_ctx, _factory, _probeManager)
  24232. {}
  24233. virtual void onExecute() override
  24234. {
  24235. soaphelper.setown(createSoapCallHelper(this, NULL, ctx->queryAuthToken(), SCrow, pClientCert, *this, this));
  24236. soaphelper->start();
  24237. soaphelper->waitUntilDone();
  24238. IException *e = soaphelper->getError();
  24239. soaphelper.clear();
  24240. if (e)
  24241. throw e;
  24242. }
  24243. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  24244. {
  24245. return NULL;
  24246. }
  24247. virtual const void *nextRow()
  24248. {
  24249. throwUnexpected(); // I am nobody's input
  24250. }
  24251. };
  24252. class CRoxieServerSoapRowActionActivityFactory : public CRoxieServerActivityFactory
  24253. {
  24254. bool isRoot;
  24255. public:
  24256. CRoxieServerSoapRowActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  24257. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), isRoot(_isRoot)
  24258. {
  24259. }
  24260. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  24261. {
  24262. return new CRoxieServerSoapRowActionActivity(_ctx, this, _probeManager);
  24263. }
  24264. virtual bool isSink() const
  24265. {
  24266. return isRoot;
  24267. }
  24268. virtual const StatisticsMapping &queryStatsMapping() const
  24269. {
  24270. return soapStatistics;
  24271. }
  24272. };
  24273. IRoxieServerActivityFactory *createRoxieServerSoapRowActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  24274. {
  24275. return new CRoxieServerSoapRowActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _isRoot);
  24276. }
  24277. //---------------------------------------------------------------------------
  24278. class CRoxieServerSoapDatasetCallActivity : public CRoxieServerSoapActivityBase
  24279. {
  24280. IHThorSoapCallArg & callHelper;
  24281. public:
  24282. CRoxieServerSoapDatasetCallActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  24283. : CRoxieServerSoapActivityBase(_ctx, _factory, _probeManager), callHelper((IHThorSoapCallArg &)basehelper)
  24284. {
  24285. }
  24286. virtual IHThorSoapCallArg * queryCallHelper()
  24287. {
  24288. return &callHelper;
  24289. }
  24290. virtual const void *getNextRow()
  24291. {
  24292. CriticalBlock b(crit); // MORE - why ?
  24293. return inputStream->ungroupedNextRow();
  24294. }
  24295. virtual bool needsAllocator() const { return true; }
  24296. virtual const void *nextRow()
  24297. {
  24298. ActivityTimer t(activityStats, timeActivities);
  24299. if(eof) return NULL;
  24300. try
  24301. {
  24302. if (soaphelper == NULL)
  24303. {
  24304. soaphelper.setown(createSoapCallHelper(this, rowAllocator, authToken.str(), SCdataset, pClientCert, *this, this));
  24305. soaphelper->start();
  24306. }
  24307. OwnedConstRoxieRow ret = soaphelper->getRow();
  24308. if (!ret)
  24309. {
  24310. eof = true;
  24311. return NULL;
  24312. }
  24313. ++processed;
  24314. return ret.getClear();
  24315. }
  24316. catch (IException *E)
  24317. {
  24318. throw makeWrappedException(E);
  24319. }
  24320. }
  24321. };
  24322. class CRoxieServerSoapDatasetCallActivityFactory : public CRoxieServerActivityFactory
  24323. {
  24324. public:
  24325. CRoxieServerSoapDatasetCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  24326. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode)
  24327. {
  24328. }
  24329. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  24330. {
  24331. return new CRoxieServerSoapDatasetCallActivity(_ctx, this, _probeManager);
  24332. }
  24333. virtual const StatisticsMapping &queryStatsMapping() const
  24334. {
  24335. return soapStatistics;
  24336. }
  24337. };
  24338. IRoxieServerActivityFactory *createRoxieServerSoapDatasetCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
  24339. {
  24340. return new CRoxieServerSoapDatasetCallActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
  24341. }
  24342. //---------------------------------------------------------------------------
  24343. class CRoxieServerSoapDatasetActionActivity : public CRoxieServerSoapActionBase
  24344. {
  24345. public:
  24346. CRoxieServerSoapDatasetActionActivity(IRoxieAgentContext *_ctx, const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
  24347. : CRoxieServerSoapActionBase(_ctx, _factory, _probeManager)
  24348. {}
  24349. virtual const void *getNextRow()
  24350. {
  24351. CriticalBlock b(crit); // MORE - Why?
  24352. const void *nextrec = inputStream->ungroupedNextRow();
  24353. if (nextrec)
  24354. processed++;
  24355. return nextrec;
  24356. }
  24357. virtual void onExecute() override
  24358. {
  24359. soaphelper.setown(createSoapCallHelper(this, NULL, ctx->queryAuthToken(), SCdataset, pClientCert, *this, this));
  24360. soaphelper->start();
  24361. soaphelper->waitUntilDone();
  24362. IException *e = soaphelper->getError();
  24363. soaphelper.clear();
  24364. if (e)
  24365. throw e;
  24366. }
  24367. virtual IFinalRoxieInput *queryOutput(unsigned idx)
  24368. {
  24369. return NULL;
  24370. }
  24371. virtual const void *nextRow()
  24372. {
  24373. throwUnexpected(); // I am nobody's input
  24374. }
  24375. };
  24376. class CRoxieServerSoapDatasetActionActivityFactory : public CRoxieServerActivityFactory
  24377. {
  24378. bool isRoot;
  24379. public:
  24380. CRoxieServerSoapDatasetActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  24381. : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode), isRoot(_isRoot)
  24382. {
  24383. }
  24384. virtual IRoxieServerActivity *createActivity(IRoxieAgentContext *_ctx, IProbeManager *_probeManager) const
  24385. {
  24386. return new CRoxieServerSoapDatasetActionActivity(_ctx, this, _probeManager);
  24387. }
  24388. virtual bool isSink() const
  24389. {
  24390. return isRoot;
  24391. }
  24392. virtual const StatisticsMapping &queryStatsMapping() const
  24393. {
  24394. return soapStatistics;
  24395. }
  24396. };
  24397. IRoxieServerActivityFactory *createRoxieServerSoapDatasetActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool _isRoot)
  24398. {
  24399. return new CRoxieServerSoapDatasetActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, _isRoot);
  24400. }
  24401. //=====================================================================================================
  24402. class CGraphResults : implements IRoxieGraphResults, public CInterface
  24403. {
  24404. IArrayOf<IGraphResult> results;
  24405. CriticalSection cs;
  24406. IGraphResult & select(unsigned idx)
  24407. {
  24408. CriticalBlock procedure(cs);
  24409. if (idx >= results.ordinality())
  24410. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Error reading graph result %d before it is calculated", idx);
  24411. return results.item(idx);
  24412. }
  24413. public:
  24414. IMPLEMENT_IINTERFACE
  24415. void clear()
  24416. {
  24417. CriticalBlock procedure(cs);
  24418. results.kill();
  24419. }
  24420. virtual IEngineRowStream * createIterator(unsigned id)
  24421. {
  24422. return select(id).createIterator();
  24423. }
  24424. virtual void getLinkedResult(unsigned & count, const byte * * & ret, unsigned id) override
  24425. {
  24426. select(id).getLinkedResult(count, ret);
  24427. }
  24428. virtual void getDictionaryResult(unsigned & count, const byte * * & ret, unsigned id) override
  24429. {
  24430. select(id).getLinkedResult(count, ret);
  24431. }
  24432. virtual const void * getLinkedRowResult(unsigned id)
  24433. {
  24434. return select(id).getLinkedRowResult();
  24435. }
  24436. void setResult(unsigned id, IGraphResult * result)
  24437. {
  24438. CriticalBlock procedure(cs);
  24439. if (results.ordinality() <= id)
  24440. {
  24441. while (results.ordinality() < id)
  24442. results.append(*new CGraphResult);
  24443. results.append(*LINK(result));
  24444. }
  24445. else
  24446. results.replace(*LINK(result), id);
  24447. }
  24448. void appendResult(IGraphResult * result)
  24449. {
  24450. CriticalBlock procedure(cs);
  24451. results.append(*LINK(result));
  24452. }
  24453. };
  24454. //===================================================================================================================
  24455. class CPseudoArg : implements CThorSinkArgOf<IHThorArg>
  24456. {
  24457. };
  24458. class CPseudoActivity : public CRoxieServerActivity
  24459. {
  24460. public:
  24461. CPseudoActivity(IRoxieAgentContext *_ctx, IHThorArg & _helper) : CRoxieServerActivity(_ctx, _helper) {}
  24462. virtual const void *nextRow()
  24463. {
  24464. throwUnexpected(); // I am nobody's input
  24465. }
  24466. };
  24467. class CActivityGraph : implements IActivityGraph, implements IThorChildGraph, implements ILocalGraphEx, implements IRoxieServerChildGraph, public CInterface
  24468. {
  24469. protected:
  24470. class ActivityGraphAgentContext : public IndirectAgentContext
  24471. {
  24472. SpinLock abortLock;
  24473. bool aborted;
  24474. Owned<IException> exception;
  24475. public:
  24476. ActivityGraphAgentContext(IRoxieAgentContext *_ctx, const IRoxieContextLogger &_logctx) : IndirectAgentContext(_ctx), logctx(_logctx), loopCounter(0), codeContext(NULL)
  24477. {
  24478. aborted = false;
  24479. }
  24480. // Note - we must track exceptions at the child level in case there is a CATCH in parent
  24481. virtual void notifyAbort(IException *E)
  24482. {
  24483. if (QUERYINTERFACE(E, InterruptedSemaphoreException) == NULL)
  24484. {
  24485. SpinBlock b(abortLock);
  24486. if (!aborted)
  24487. {
  24488. aborted = true;
  24489. exception.set(E);
  24490. }
  24491. }
  24492. }
  24493. virtual void checkAbort()
  24494. {
  24495. if (aborted) // NOTE - don't bother getting lock before reading this (for speed) - a false read is very unlikely and not a problem
  24496. {
  24497. SpinBlock b(abortLock);
  24498. if (!exception)
  24499. exception.setown(MakeStringException(ROXIE_INTERNAL_ERROR, "Query was aborted"));
  24500. throw exception.getLink();
  24501. }
  24502. IndirectAgentContext::checkAbort();
  24503. }
  24504. virtual ICodeContext *queryCodeContext()
  24505. {
  24506. return codeContext;
  24507. }
  24508. void setCodeContext(ICodeContext * _codeContext)
  24509. {
  24510. codeContext = _codeContext;
  24511. }
  24512. void setLoopCounter(unsigned _loopCounter)
  24513. {
  24514. loopCounter = _loopCounter;
  24515. }
  24516. void setPrefix(const char *name)
  24517. {
  24518. prefix.set(name);
  24519. }
  24520. virtual void noteChildGraph(unsigned id, IActivityGraph *childGraph)
  24521. {
  24522. childGraphs.setValue(id, childGraph);
  24523. }
  24524. virtual IActivityGraph * queryChildGraph(unsigned id)
  24525. {
  24526. if (queryTraceLevel() > 10)
  24527. CTXLOG("resolveChildGraph %d", id);
  24528. IActivityGraph *childGraph = childGraphs.getValue(id);
  24529. assertex(childGraph);
  24530. return childGraph;
  24531. }
  24532. // MORE should really redirect the other log context ones too (though mostly doesn't matter). Really should refactor to have a queryLogContext() method in IRoxieAgentContext I think
  24533. virtual StringBuffer &getLogPrefix(StringBuffer &ret) const
  24534. {
  24535. IndirectAgentContext::getLogPrefix(ret);
  24536. if (prefix)
  24537. ret.appendf(":%s", prefix.str());
  24538. if (loopCounter)
  24539. ret.appendf("{%u}", loopCounter);
  24540. return ret;
  24541. }
  24542. protected:
  24543. const IRoxieContextLogger &logctx;
  24544. unsigned loopCounter;
  24545. StringAttr prefix;
  24546. ICodeContext * codeContext;
  24547. MapXToMyClass<unsigned, unsigned, IActivityGraph> childGraphs;
  24548. } graphAgentContext;
  24549. class ActivityGraphCodeContext : public IndirectCodeContext
  24550. {
  24551. public:
  24552. virtual IEclGraphResults * resolveLocalQuery(__int64 activityId)
  24553. {
  24554. if ((unsigned) activityId == container->queryId())
  24555. return container;
  24556. IActivityGraph * match = agentContext->queryChildGraph((unsigned) activityId);
  24557. if (match)
  24558. return match->queryLocalGraph();
  24559. return IndirectCodeContext::resolveLocalQuery(activityId);
  24560. }
  24561. virtual IThorChildGraph * resolveChildQuery(__int64 activityId, IHThorArg * colocal)
  24562. {
  24563. IActivityGraph * match = agentContext->queryChildGraph((unsigned) activityId);
  24564. return LINK(match->queryChildGraph());
  24565. }
  24566. virtual unsigned getGraphLoopCounter() const
  24567. {
  24568. return container->queryLoopCounter(); // only called if value is valid
  24569. }
  24570. void setContainer(IRoxieAgentContext * _agentContext, CActivityGraph * _container)
  24571. {
  24572. agentContext = _agentContext;
  24573. container = _container;
  24574. }
  24575. protected:
  24576. IRoxieAgentContext * agentContext;
  24577. CActivityGraph * container;
  24578. } graphCodeContext;
  24579. class SinkThread : public CInterface, implements IThreaded
  24580. {
  24581. public:
  24582. SinkThread(IActivityGraph &_parent, IRoxieServerActivity &_sink)
  24583. : thread("SinkThread", this), parent(_parent), sink(_sink)
  24584. {}
  24585. virtual void threadmain() override
  24586. {
  24587. try
  24588. {
  24589. sink.execute(parentExtractSize, parentExtract);
  24590. }
  24591. catch (IException *E)
  24592. {
  24593. parent.noteException(E);
  24594. throw;
  24595. }
  24596. }
  24597. void start(unsigned _parentExtractSize, const byte * _parentExtract)
  24598. {
  24599. parentExtract = _parentExtract;
  24600. parentExtractSize = _parentExtractSize;
  24601. thread.start();
  24602. }
  24603. inline void join()
  24604. {
  24605. thread.join(INFINITE);
  24606. }
  24607. private:
  24608. CThreadedPersistent thread;
  24609. IActivityGraph &parent;
  24610. IRoxieServerActivity &sink;
  24611. const byte * parentExtract = nullptr;
  24612. unsigned parentExtractSize = 0;
  24613. };
  24614. // NOTE - destructor order is significant - need to destroy graphCodeContext and graphAgentContext last
  24615. IArrayOf<IRoxieServerActivity> activities;
  24616. IArrayOf<IRoxieProbe> probes;
  24617. CIArrayOf<SinkThread> threads;
  24618. IRoxieServerActivityCopyArray sinks;
  24619. StringAttr graphName;
  24620. Owned<CGraphResults> results;
  24621. CGraphResults graphLoopResults;
  24622. const ActivityArray & graphDefinition;
  24623. CriticalSection evaluateCrit;
  24624. IProbeManager *probeManager;
  24625. IRoxieServerActivity *parentActivity;
  24626. unsigned id;
  24627. unsigned loopCounter;
  24628. public:
  24629. IMPLEMENT_IINTERFACE;
  24630. CActivityGraph(IRoxieAgentContext *_ctx, const char *_graphName, unsigned _id, IRoxieServerActivity *_parentActivity, const ActivityArray &x, IProbeManager *_probeManager, const IRoxieContextLogger &_logctx)
  24631. : graphAgentContext(_ctx, _logctx), graphName(_graphName), graphDefinition(x), probeManager(_probeManager), parentActivity(_parentActivity)
  24632. {
  24633. id = x.getLibraryGraphId();
  24634. if (!id)
  24635. id = _id;
  24636. loopCounter = 0;
  24637. graphAgentContext.setCodeContext(&graphCodeContext);
  24638. graphCodeContext.setContainer(&graphAgentContext, this);
  24639. }
  24640. ~CActivityGraph()
  24641. {
  24642. if (probeManager)
  24643. probeManager->deleteGraph((IArrayOf<IActivityBase>*)&activities, (IArrayOf<IInputBase>*)&probes);
  24644. }
  24645. virtual const char *queryName() const
  24646. {
  24647. return graphName.get();
  24648. }
  24649. virtual void setPrefix(const char *name) override
  24650. {
  24651. graphAgentContext.setPrefix(name);
  24652. }
  24653. void createGraph(IRoxieAgentContext *_ctx)
  24654. {
  24655. if (graphDefinition.isMultiInstance())
  24656. {
  24657. graphCodeContext.set(_ctx->queryCodeContext());
  24658. _ctx = &graphAgentContext;
  24659. }
  24660. ForEachItemIn(idx, graphDefinition)
  24661. {
  24662. IRoxieServerActivityFactory &donor = graphDefinition.serverItem(idx);
  24663. IRoxieServerActivity &activity = *donor.createActivity(_ctx, probeManager);
  24664. activities.append(activity);
  24665. if (donor.isSink())
  24666. {
  24667. sinks.append(activity);
  24668. if (probeManager)
  24669. probeManager->noteSink(&activity);
  24670. }
  24671. }
  24672. ForEachItemIn(idx1, graphDefinition)
  24673. {
  24674. IRoxieServerActivityFactory &donor = graphDefinition.serverItem(idx1);
  24675. IRoxieServerActivity &activity = activities.item(idx1);
  24676. unsigned inputidx = 0;
  24677. for (;;)
  24678. {
  24679. unsigned outputidx;
  24680. unsigned source = donor.getInput(inputidx, outputidx);
  24681. if (source==(unsigned) -1)
  24682. break;
  24683. connectInput(idx1, inputidx, source, outputidx, 0);
  24684. inputidx++;
  24685. }
  24686. IntArray &dependencies = donor.queryDependencies();
  24687. IntArray &dependencyIndexes = donor.queryDependencyIndexes();
  24688. IntArray &dependencyControlIds = donor.queryDependencyControlIds();
  24689. StringArray &dependencyEdgeIds = donor.queryDependencyEdgeIds();
  24690. ForEachItemIn(idx2, dependencies)
  24691. {
  24692. IRoxieServerActivity &dependencySourceActivity = activities.item(dependencies.item(idx2));
  24693. unsigned dependencySourceIndex = dependencyIndexes.item(idx2);
  24694. unsigned dependencyControlId = dependencyControlIds.item(idx2);
  24695. activity.addDependency(dependencySourceActivity, dependencySourceIndex, dependencyControlId);
  24696. if (probeManager)
  24697. probeManager->noteDependency( &dependencySourceActivity, dependencySourceIndex, dependencyControlId, dependencyEdgeIds.item(idx2), &activity);
  24698. }
  24699. }
  24700. ForEachItemIn(idx2, sinks)
  24701. {
  24702. IRoxieServerActivity &sink = sinks.item(idx2);
  24703. sink.connectInputStreams(true);
  24704. }
  24705. }
  24706. void connectInput(unsigned target, unsigned targetIdx, unsigned source, unsigned sourceIdx, unsigned iteration)
  24707. {
  24708. IRoxieServerActivity &targetActivity = activities.item(target);
  24709. IRoxieServerActivity &sourceActivity = activities.item(source);
  24710. IFinalRoxieInput * output = sourceActivity.queryOutput(sourceIdx);
  24711. if (probeManager)
  24712. {
  24713. IRoxieProbe * inputProbe = probeManager->createProbe(static_cast<IInputBase*>(output), &sourceActivity, &targetActivity, sourceIdx, targetIdx, iteration);
  24714. probes.append(*LINK(inputProbe));
  24715. output = &dynamic_cast<IFinalRoxieInput &>(inputProbe->queryInput());
  24716. }
  24717. targetActivity.setInput(targetIdx, sourceIdx, output);
  24718. }
  24719. virtual void onCreate(IHThorArg *_colocalParent)
  24720. {
  24721. ForEachItemIn(idx, activities)
  24722. {
  24723. IRoxieServerActivity *activity = &activities.item(idx);
  24724. if (activity)
  24725. activity->onCreate(_colocalParent);
  24726. }
  24727. }
  24728. virtual void abort()
  24729. {
  24730. ForEachItemIn(idx, sinks)
  24731. {
  24732. IRoxieServerActivity &sink = sinks.item(idx);
  24733. sink.abort();
  24734. }
  24735. }
  24736. virtual void reset()
  24737. {
  24738. ForEachItemIn(idx, sinks)
  24739. {
  24740. IRoxieServerActivity &sink = sinks.item(idx);
  24741. sink.reset();
  24742. }
  24743. }
  24744. Linked<IException> exception;
  24745. CriticalSection eCrit;
  24746. virtual void noteException(IException *E)
  24747. {
  24748. CriticalBlock b(eCrit);
  24749. if (!exception)
  24750. {
  24751. if (graphAgentContext.queryDebugContext())
  24752. {
  24753. graphAgentContext.queryDebugContext()->checkBreakpoint(DebugStateException, NULL, exception);
  24754. }
  24755. exception.set(E);
  24756. }
  24757. }
  24758. virtual void checkAbort()
  24759. {
  24760. CriticalBlock b(eCrit);
  24761. if (exception)
  24762. throw exception.getLink();
  24763. }
  24764. unsigned queryWorkflowId() const
  24765. {
  24766. return graphDefinition.queryWorkflowId();
  24767. }
  24768. virtual void execute()
  24769. {
  24770. results.setown(new CGraphResults);
  24771. doExecute(0, NULL);
  24772. }
  24773. //New child query code...
  24774. virtual IThorChildGraph * queryChildGraph()
  24775. {
  24776. return this;
  24777. }
  24778. virtual IEclGraphResults * queryLocalGraph()
  24779. {
  24780. return this;
  24781. }
  24782. virtual IRoxieServerChildGraph * queryLoopGraph()
  24783. {
  24784. return this;
  24785. }
  24786. inline unsigned queryId() const
  24787. {
  24788. return id;
  24789. }
  24790. inline unsigned queryLoopCounter() const
  24791. {
  24792. return loopCounter;
  24793. }
  24794. void doExecute(unsigned parentExtractSize, const byte * parentExtract)
  24795. {
  24796. if (sinks.ordinality()==1)
  24797. sinks.item(0).execute(parentExtractSize, parentExtract);
  24798. #ifdef PARALLEL_EXECUTE
  24799. else if (!probeManager && !graphDefinition.isSequential())
  24800. {
  24801. #ifdef PARALLEL_PERSISTANT_THREADS
  24802. if (!threads.ordinality())
  24803. {
  24804. for (unsigned i = 0; i < sinks.ordinality()-1; i++)
  24805. {
  24806. threads.append(*new SinkThread(*this, sinks.item(i)));
  24807. }
  24808. }
  24809. for (unsigned i = 0; i < sinks.ordinality()-1; i++)
  24810. threads.item(i).start(parentExtractSize, parentExtract);
  24811. try
  24812. {
  24813. sinks.item(sinks.ordinality()-1).execute(parentExtractSize, parentExtract);
  24814. }
  24815. catch (IException *E)
  24816. {
  24817. noteException(E);
  24818. E->Release();
  24819. }
  24820. for (unsigned i = 0; i < sinks.ordinality()-1; i++)
  24821. {
  24822. try
  24823. {
  24824. threads.item(i).join();
  24825. }
  24826. catch (IException *E)
  24827. {
  24828. noteException(E);
  24829. E->Release();
  24830. }
  24831. }
  24832. checkAbort();
  24833. #else
  24834. class casyncfor: public CAsyncFor
  24835. {
  24836. public:
  24837. IActivityGraph &parent;
  24838. unsigned parentExtractSize;
  24839. const byte * parentExtract;
  24840. casyncfor(IRoxieServerActivityCopyArray &_sinks, IActivityGraph &_parent, unsigned _parentExtractSize, const byte * _parentExtract) :
  24841. sinks(_sinks), parent(_parent), parentExtractSize(_parentExtractSize), parentExtract(_parentExtract) { }
  24842. void Do(unsigned i)
  24843. {
  24844. try
  24845. {
  24846. sinks.item(i).execute(parentExtractSize, parentExtract);
  24847. }
  24848. catch (IException *E)
  24849. {
  24850. parent.noteException(E);
  24851. throw;
  24852. }
  24853. }
  24854. private:
  24855. IRoxieServerActivityCopyArray &sinks;
  24856. } afor(sinks, *this, parentExtractSize, parentExtract);
  24857. afor.For(sinks.ordinality(), sinks.ordinality());
  24858. #endif
  24859. }
  24860. #endif
  24861. else
  24862. {
  24863. ForEachItemIn(idx, sinks)
  24864. {
  24865. IRoxieServerActivity &sink = sinks.item(idx);
  24866. sink.execute(parentExtractSize, parentExtract);
  24867. }
  24868. }
  24869. }
  24870. virtual IEclGraphResults *evaluate(unsigned parentExtractSize, const byte * parentExtract)
  24871. {
  24872. CriticalBlock block(evaluateCrit);
  24873. results.setown(new CGraphResults);
  24874. try
  24875. {
  24876. doExecute(parentExtractSize, parentExtract);
  24877. }
  24878. catch (...)
  24879. {
  24880. DBGLOG("Exception thrown in child query - cleaning up");
  24881. reset();
  24882. throw;
  24883. }
  24884. reset();
  24885. return results.getClear();
  24886. }
  24887. virtual void gatherStatistics(IStatisticGatherer * statsBuilder) const override
  24888. {
  24889. if (id && statsBuilder)
  24890. {
  24891. statsBuilder->beginSubGraphScope(parentActivity->querySubgraphId());
  24892. statsBuilder->beginActivityScope(parentActivity->queryId());
  24893. statsBuilder->beginChildGraphScope(id);
  24894. }
  24895. ForEachItemIn(i, activities)
  24896. activities.item(i).gatherStatistics(statsBuilder);
  24897. if (id && statsBuilder)
  24898. {
  24899. statsBuilder->endScope();
  24900. statsBuilder->endScope();
  24901. statsBuilder->endScope();
  24902. }
  24903. }
  24904. //interface IRoxieServerChildGraph
  24905. virtual void beforeExecute()
  24906. {
  24907. results.setown(new CGraphResults);
  24908. }
  24909. virtual IFinalRoxieInput * startOutput(unsigned id, unsigned parentExtractSize, const byte *parentExtract, bool paused)
  24910. {
  24911. IFinalRoxieInput * ret = selectOutput(id);
  24912. ret->start(parentExtractSize, parentExtract, paused);
  24913. return ret;
  24914. }
  24915. virtual IFinalRoxieInput * selectOutput(unsigned id)
  24916. {
  24917. ForEachItemIn(i, sinks)
  24918. {
  24919. IFinalRoxieInput * ret = sinks.item(i).querySelectOutput(id);
  24920. if (ret)
  24921. return ret;
  24922. }
  24923. throwUnexpected();
  24924. return NULL;
  24925. }
  24926. virtual void setInputResult(unsigned id, IGraphResult * result)
  24927. {
  24928. results->setResult(id, result);
  24929. }
  24930. virtual bool querySetInputResult(unsigned id, unsigned _sourceIdx, IFinalRoxieInput * input)
  24931. {
  24932. ForEachItemIn(i, activities)
  24933. {
  24934. if (activities.item(i).querySetStreamInput(id, _sourceIdx, input))
  24935. return true;
  24936. }
  24937. return false;
  24938. }
  24939. virtual void stopUnusedOutputs()
  24940. {
  24941. //Hmm not sure how to do this...
  24942. }
  24943. virtual void afterExecute()
  24944. {
  24945. ForEachItemIn(i, sinks)
  24946. {
  24947. sinks.item(i).stop();
  24948. }
  24949. if (graphAgentContext.queryDebugContext())
  24950. {
  24951. graphAgentContext.queryDebugContext()->checkBreakpoint(DebugStateGraphFinished, NULL, NULL);
  24952. }
  24953. reset();
  24954. }
  24955. virtual IRoxieGraphResults * execute(size32_t parentExtractSize, const byte *parentExtract)
  24956. {
  24957. doExecute(parentExtractSize, parentExtract);
  24958. return LINK(results);
  24959. }
  24960. virtual void getLinkedResult(unsigned & count, const byte * * & ret, unsigned id) override
  24961. {
  24962. results->getLinkedResult(count, ret, id);
  24963. }
  24964. virtual void getDictionaryResult(unsigned & count, const byte * * & ret, unsigned id) override
  24965. {
  24966. results->getLinkedResult(count, ret, id);
  24967. }
  24968. virtual const void * getLinkedRowResult(unsigned id)
  24969. {
  24970. return results->getLinkedRowResult(id);
  24971. }
  24972. virtual void setResult(unsigned id, IGraphResult * result)
  24973. {
  24974. results->setResult(id, result);
  24975. }
  24976. virtual IEngineRowStream * createResultIterator(unsigned id)
  24977. {
  24978. return results->createIterator(id);
  24979. }
  24980. virtual void setGraphLoopResult(IGraphResult * result)
  24981. {
  24982. graphLoopResults.appendResult(result);
  24983. }
  24984. virtual IEngineRowStream * createGraphLoopResultIterator(unsigned id)
  24985. {
  24986. try
  24987. {
  24988. return graphLoopResults.createIterator(id);
  24989. }
  24990. catch (IException * e)
  24991. {
  24992. e->Release();
  24993. throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Error reading graph result %d before it is calculated", id);
  24994. }
  24995. }
  24996. virtual void clearGraphLoopResults()
  24997. {
  24998. graphLoopResults.clear();
  24999. }
  25000. virtual void executeGraphLoop(size32_t parentExtractSize, const byte *parentExtract)
  25001. {
  25002. doExecute(parentExtractSize, parentExtract);
  25003. }
  25004. virtual void setGraphLoopResult(unsigned id, IGraphResult * result)
  25005. {
  25006. graphLoopResults.setResult(id, result);
  25007. }
  25008. virtual IEngineRowStream * getGraphLoopResult(unsigned id)
  25009. {
  25010. return graphLoopResults.createIterator(id);
  25011. }
  25012. virtual void getProbeResponse(IPropertyTree *query)
  25013. {
  25014. if (probeManager)
  25015. probeManager->getProbeResponse(query);
  25016. }
  25017. virtual IRoxieServerChildGraph * createGraphLoopInstance(IRoxieAgentContext *ctx, unsigned loopCounter, unsigned parentExtractSize, const byte * parentExtract, const IRoxieContextLogger &logctx)
  25018. {
  25019. throwUnexpected();
  25020. }
  25021. virtual CGraphIterationInfo *selectGraphLoopOutput()
  25022. {
  25023. return NULL;
  25024. }
  25025. virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor)
  25026. {
  25027. throwUnexpected();
  25028. }
  25029. virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor)
  25030. {
  25031. throwUnexpected();
  25032. }
  25033. };
  25034. class CProxyActivityGraph : implements IActivityGraph, implements IThorChildGraph, public CInterface
  25035. {
  25036. public:
  25037. IMPLEMENT_IINTERFACE;
  25038. CProxyActivityGraph(IRoxieAgentContext *_ctx, const char *_graphName, unsigned _id, IRoxieServerActivity *_parentActivity, ActivityArray &_graphDefinition, const IRoxieContextLogger &_logctx, unsigned _numParallel)
  25039. : ctx(_ctx), parentActivity(_parentActivity), graphName(_graphName), id(_id), graphDefinition(_graphDefinition), logctx(_logctx), numParallel(_numParallel)
  25040. {
  25041. }
  25042. virtual void abort() override { throwUnexpected(); }
  25043. virtual void reset() override { throwUnexpected(); }
  25044. virtual void execute() override { throwUnexpected(); }
  25045. virtual void getProbeResponse(IPropertyTree *query) override { throwUnexpected(); }
  25046. virtual void onCreate(IHThorArg *_colocalArg) override
  25047. {
  25048. colocalArg = _colocalArg;
  25049. }
  25050. virtual void noteException(IException *E) override { throwUnexpected(); }
  25051. virtual void checkAbort() override { throwUnexpected(); }
  25052. virtual IThorChildGraph * queryChildGraph() override { return this; }
  25053. virtual IEclGraphResults * queryLocalGraph() override { throwUnexpected(); }
  25054. virtual IRoxieServerChildGraph * queryLoopGraph() override { throwUnexpected(); }
  25055. virtual IRoxieServerChildGraph * createGraphLoopInstance(IRoxieAgentContext *ctx, unsigned loopCounter, unsigned parentExtractSize, const byte * parentExtract, const IRoxieContextLogger &logctx) override { throwUnexpected(); }
  25056. virtual const char *queryName() const override { throwUnexpected(); }
  25057. virtual void gatherStatistics(IStatisticGatherer * statsBuilder) const override
  25058. {
  25059. CriticalBlock b(graphCrit);
  25060. ForEachItemIn(i, stack)
  25061. stack.item(i).gatherStatistics(statsBuilder);
  25062. }
  25063. virtual IEclGraphResults * evaluate(unsigned parentExtractSize, const byte * parentExtract) override
  25064. {
  25065. Owned<CActivityGraph> realGraph;
  25066. {
  25067. CriticalBlock b(graphCrit);
  25068. if (stack.length())
  25069. realGraph.setown(&stack.popGet());
  25070. }
  25071. if (!realGraph)
  25072. {
  25073. realGraph.setown(new CActivityGraph(ctx, graphName, id, parentActivity, graphDefinition, NULL, logctx));
  25074. realGraph->createGraph(ctx);
  25075. realGraph->onCreate(colocalArg);
  25076. }
  25077. Owned<IEclGraphResults> results = realGraph->evaluate(parentExtractSize, parentExtract);
  25078. {
  25079. CriticalBlock b(graphCrit);
  25080. stack.append(*realGraph.getClear());
  25081. }
  25082. return results.getClear();
  25083. }
  25084. virtual void setPrefix(const char *) override
  25085. {
  25086. }
  25087. virtual unsigned queryWorkflowId() const override
  25088. {
  25089. return graphDefinition.queryWorkflowId();
  25090. }
  25091. protected:
  25092. IRoxieAgentContext *ctx;
  25093. IRoxieServerActivity *parentActivity;
  25094. StringAttr graphName;
  25095. unsigned id;
  25096. ActivityArray &graphDefinition;
  25097. const IRoxieContextLogger &logctx;
  25098. unsigned numParallel;
  25099. IHThorArg *colocalArg = nullptr;
  25100. mutable CriticalSection graphCrit;
  25101. CIArrayOf<CActivityGraph> stack;
  25102. };
  25103. class CIterationActivityGraph : public CActivityGraph
  25104. {
  25105. IHThorArg * colocalParent;
  25106. unsigned fixedParentExtractSize;
  25107. const byte * fixedParentExtract;
  25108. unsigned graphOutputActivityIndex;
  25109. public:
  25110. CIterationActivityGraph(const char *_graphName, unsigned _id, IRoxieServerActivity *_parentActivity, ActivityArray &x, IProbeManager *_probeManager,
  25111. unsigned _loopCounter, IRoxieAgentContext *_ctx, IHThorArg * _colocalParent, unsigned parentExtractSize, const byte * parentExtract, const IRoxieContextLogger &_logctx)
  25112. : CActivityGraph(_ctx, _graphName, _id, _parentActivity, x, _probeManager, _logctx)
  25113. {
  25114. graphOutputActivityIndex = 0;
  25115. loopCounter = _loopCounter;
  25116. colocalParent = _colocalParent;
  25117. graphAgentContext.setLoopCounter(loopCounter);
  25118. graphCodeContext.set(_ctx->queryCodeContext());
  25119. fixedParentExtractSize = parentExtractSize;
  25120. fixedParentExtract = parentExtract;
  25121. }
  25122. void createIterationGraph(IRoxieAgentContext *_ctx)
  25123. {
  25124. Owned<IRoxieServerActivity> pseudoActivity = new CPseudoActivity(_ctx, *new CPseudoArg);
  25125. ForEachItemIn(idx1, graphDefinition)
  25126. activities.append(*LINK(pseudoActivity));
  25127. graphOutputActivityIndex = queryGraphOutputIndex();
  25128. recursiveCreateGraph(_ctx, graphOutputActivityIndex);
  25129. }
  25130. unsigned queryGraphOutputIndex() const
  25131. {
  25132. ForEachItemIn(i, graphDefinition)
  25133. if (graphDefinition.serverItem(i).getKind() == TAKgraphloopresultwrite)
  25134. return i;
  25135. throwUnexpected();
  25136. }
  25137. void recursiveCreateGraph(IRoxieAgentContext *_ctx, unsigned whichActivity)
  25138. {
  25139. //Check to see if already created
  25140. IRoxieServerActivity & prevActivity = activities.item(whichActivity);
  25141. if (prevActivity.queryId() != 0)
  25142. {
  25143. prevActivity.noteOutputUsed(); //We need to patch up the number of outputs for splitters.
  25144. return;
  25145. }
  25146. IRoxieServerActivityFactory &donor = graphDefinition.serverItem(whichActivity);
  25147. IRoxieServerActivity * activity = NULL;
  25148. if (donor.isGraphInvariant())
  25149. {
  25150. ThorActivityKind kind = donor.getKind();
  25151. switch (kind)
  25152. {
  25153. case TAKif:
  25154. case TAKchildif:
  25155. case TAKcase:
  25156. case TAKchildcase: // MORE RKC->GH - what about FILTER with a graph-invariant condition and other latestart activities?
  25157. {
  25158. Owned<IHThorArg> helper = &donor.getHelper();
  25159. helper->onCreate(&graphCodeContext, colocalParent, NULL);
  25160. helper->onStart(fixedParentExtract, NULL);
  25161. unsigned branch;
  25162. switch (kind)
  25163. {
  25164. case TAKif: case TAKchildif:
  25165. branch = static_cast<IHThorIfArg *>(helper.get())->getCondition() ? 0 : 1;
  25166. break;
  25167. case TAKcase: case TAKchildcase:
  25168. branch = static_cast<IHThorCaseArg *>(helper.get())->getBranch();
  25169. if (branch >= donor.numInputs())
  25170. branch = donor.numInputs() - 1;
  25171. break;
  25172. default:
  25173. throwUnexpected();
  25174. }
  25175. helper.clear();
  25176. unsigned outputidx;
  25177. unsigned source = donor.getInput(branch, outputidx);
  25178. if (source ==(unsigned) -1)
  25179. activity = createRoxieServerNullActivity(&graphAgentContext, &donor, probeManager);
  25180. else
  25181. activity = createRoxieServerPassThroughActivity(&graphAgentContext, &donor, probeManager);
  25182. activities.replace(*activity, whichActivity);
  25183. activity->onCreate(colocalParent);
  25184. if (source ==(unsigned) -1)
  25185. return;
  25186. recursiveCreateGraph(_ctx, source);
  25187. connectInput(whichActivity, 0, source, outputidx, loopCounter);
  25188. break;
  25189. }
  25190. }
  25191. }
  25192. if (!activity)
  25193. {
  25194. activity = donor.createActivity(&graphAgentContext, probeManager);
  25195. activities.replace(*activity, whichActivity);
  25196. activity->onCreate(colocalParent);
  25197. activity->resetOutputsUsed();
  25198. unsigned inputidx = 0;
  25199. for (;;)
  25200. {
  25201. unsigned outputidx;
  25202. unsigned source = donor.getInput(inputidx, outputidx);
  25203. if (source==(unsigned) -1)
  25204. break;
  25205. recursiveCreateGraph(_ctx, source);
  25206. connectInput(whichActivity, inputidx, source, outputidx, loopCounter);
  25207. inputidx++;
  25208. }
  25209. }
  25210. IntArray &dependencies = donor.queryDependencies();
  25211. IntArray &dependencyIndexes = donor.queryDependencyIndexes();
  25212. IntArray &dependencyControlIds = donor.queryDependencyControlIds();
  25213. ForEachItemIn(idx2, dependencies)
  25214. {
  25215. unsigned input = dependencies.item(idx2);
  25216. recursiveCreateGraph(_ctx, input);
  25217. activity->addDependency(activities.item(input),dependencyIndexes.item(idx2),dependencyControlIds.item(idx2));
  25218. }
  25219. }
  25220. virtual CGraphIterationInfo *selectGraphLoopOutput()
  25221. {
  25222. IRoxieServerActivity &sourceActivity = activities.item(graphOutputActivityIndex);
  25223. IFinalRoxieInput *sourceInput = sourceActivity.queryOutput(0);
  25224. return new CGraphIterationInfo(&sourceActivity, sourceInput, 0, loopCounter);
  25225. }
  25226. virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor)
  25227. {
  25228. ForEachItemIn(i, activities)
  25229. activities.item(i).gatherIterationUsage(processor, fixedParentExtractSize, fixedParentExtract);
  25230. }
  25231. virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor)
  25232. {
  25233. ForEachItemIn(i, activities)
  25234. activities.item(i).associateIterationOutputs(processor, fixedParentExtractSize, fixedParentExtract, probeManager, probes);
  25235. }
  25236. };
  25237. class CDelayedActivityGraph : implements IActivityGraph, public CInterface
  25238. {
  25239. StringAttr graphName;
  25240. StringAttr prefix;
  25241. ActivityArray & graphDefinition;
  25242. IProbeManager *probeManager;
  25243. unsigned id;
  25244. IRoxieAgentContext * ctx;
  25245. IHThorArg * colocalParent;
  25246. IRoxieServerActivity *parentActivity;
  25247. public:
  25248. IMPLEMENT_IINTERFACE;
  25249. CDelayedActivityGraph(const char *_graphName, unsigned _id, IRoxieServerActivity *_parentActivity, ActivityArray &x, IProbeManager *_probeManager)
  25250. : graphDefinition(x), probeManager(_probeManager), parentActivity(_parentActivity)
  25251. {
  25252. graphName.set(_graphName);
  25253. id = _id;
  25254. ctx = NULL;
  25255. colocalParent = NULL;
  25256. }
  25257. virtual const char *queryName() const override { return graphName.get(); }
  25258. virtual void abort() override { throwUnexpected(); }
  25259. virtual void reset() override { }
  25260. virtual void execute() override { throwUnexpected(); }
  25261. virtual void getProbeResponse(IPropertyTree *query) override { throwUnexpected(); }
  25262. virtual void noteException(IException *E) override { throwUnexpected(); }
  25263. virtual void checkAbort() override { throwUnexpected(); }
  25264. virtual IThorChildGraph * queryChildGraph() override { throwUnexpected(); }
  25265. virtual IEclGraphResults * queryLocalGraph() override { throwUnexpected(); }
  25266. virtual IRoxieServerChildGraph * queryLoopGraph() override { throwUnexpected(); }
  25267. virtual void gatherStatistics(IStatisticGatherer * statsBuilder) const override { }
  25268. virtual void onCreate(IHThorArg *_colocalParent) override
  25269. {
  25270. colocalParent = _colocalParent;
  25271. }
  25272. virtual void setPrefix(const char *pfx) override
  25273. {
  25274. prefix.set(pfx);
  25275. }
  25276. virtual unsigned queryWorkflowId() const override
  25277. {
  25278. return graphDefinition.queryWorkflowId();
  25279. }
  25280. virtual IRoxieServerChildGraph * createGraphLoopInstance(IRoxieAgentContext *ctx, unsigned loopCounter, unsigned parentExtractSize, const byte * parentExtract, const IRoxieContextLogger &logctx) override
  25281. {
  25282. Owned<CIterationActivityGraph> ret = new CIterationActivityGraph(graphName, id, parentActivity, graphDefinition, probeManager, loopCounter, ctx, colocalParent, parentExtractSize, parentExtract, logctx);
  25283. ret->createIterationGraph(ctx);
  25284. if (prefix)
  25285. ret->setPrefix(prefix);
  25286. return ret.getClear();
  25287. }
  25288. };
  25289. IActivityGraph *createActivityGraph(IRoxieAgentContext *ctx, const char *_graphName, unsigned id, ActivityArray &childFactories, IRoxieServerActivity *parentActivity, IProbeManager *_probeManager, const IRoxieContextLogger &_logctx, unsigned numParallel)
  25290. {
  25291. if (childFactories.isDelayed())
  25292. {
  25293. assertex(numParallel==1);
  25294. return new CDelayedActivityGraph(_graphName, id, parentActivity, childFactories, _probeManager);
  25295. }
  25296. else
  25297. {
  25298. if (numParallel==1 || _probeManager != nullptr)
  25299. {
  25300. Owned<IProbeManager> childProbe;
  25301. if (_probeManager)
  25302. childProbe.setown(_probeManager->startChildGraph(id, parentActivity));
  25303. Owned<CActivityGraph> ret = new CActivityGraph(ctx, _graphName, id, parentActivity, childFactories, childProbe, _logctx);
  25304. ret->createGraph(ctx);
  25305. if (_probeManager)
  25306. _probeManager->endChildGraph(childProbe, parentActivity);
  25307. return ret.getClear();
  25308. }
  25309. else
  25310. return new CProxyActivityGraph(ctx, _graphName, id, parentActivity, childFactories, _logctx, numParallel);
  25311. }
  25312. }
  25313. //================================================================================================================
  25314. #ifdef _USE_CPPUNIT
  25315. #include "unittests.hpp"
  25316. // 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...
  25317. #if _MSC_VER==1200
  25318. #undef protected
  25319. #endif
  25320. static const char *sortAlgorithm;
  25321. class TestMetaData : public CInterface, implements IOutputMetaData
  25322. {
  25323. public:
  25324. IMPLEMENT_IINTERFACE;
  25325. virtual size32_t getRecordSize(const void *) { return 10; }
  25326. virtual size32_t getMinRecordSize() const { return 10; }
  25327. virtual size32_t getFixedSize() const { return 10; }
  25328. virtual void toXML(const byte * self, IXmlWriter & out) {}
  25329. virtual unsigned getVersion() const { return OUTPUTMETADATA_VERSION; }
  25330. virtual unsigned getMetaFlags() { return 0; }
  25331. virtual const RtlTypeInfo * queryTypeInfo() const { return nullptr; }
  25332. virtual void destruct(byte * self) {}
  25333. virtual IOutputRowSerializer * createDiskSerializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  25334. virtual IOutputRowDeserializer * createDiskDeserializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  25335. virtual ISourceRowPrefetcher * createDiskPrefetcher() { return NULL; }
  25336. virtual IOutputMetaData * querySerializedDiskMeta() { return NULL; }
  25337. virtual IOutputRowSerializer * createInternalSerializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  25338. virtual IOutputRowDeserializer * createInternalDeserializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
  25339. virtual void process(const byte * self, IFieldProcessor & target, unsigned from, unsigned to) {}
  25340. virtual void walkIndirectMembers(const byte * self, IIndirectMemberVisitor & visitor) {}
  25341. virtual IOutputMetaData * queryChildMeta(unsigned i) { return NULL; }
  25342. virtual const RtlRecord &queryRecordAccessor(bool expand) const { throwUnexpected(); }
  25343. } testMeta;
  25344. class TestInput : public CInterface, implements IFinalRoxieInput, implements IEngineRowStream
  25345. {
  25346. char const * const *input;
  25347. IRoxieAgentContext *ctx;
  25348. unsigned endSeen;
  25349. bool eof;
  25350. unsigned count;
  25351. ActivityTimeAccumulator activityStats;
  25352. size32_t recordSize;
  25353. unsigned activityId;
  25354. public:
  25355. enum { STATEreset, STATEstarted, STATEstopped } state;
  25356. bool allRead;
  25357. unsigned repeat;
  25358. IMPLEMENT_IINTERFACE;
  25359. TestInput(IRoxieAgentContext *_ctx, char const * const *_input)
  25360. {
  25361. ctx = _ctx;
  25362. input = _input;
  25363. count = 0;
  25364. eof = false;
  25365. allRead = false;
  25366. endSeen = 0;
  25367. recordSize = testMeta.getFixedSize();
  25368. state = STATEreset;
  25369. activityId = 1;
  25370. repeat = 0;
  25371. }
  25372. virtual IOutputMetaData * queryOutputMeta() const { return &testMeta; }
  25373. virtual void prestart(unsigned parentExtractSize, const byte *parentExtract)
  25374. {
  25375. ASSERT(state == STATEreset);
  25376. }
  25377. virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
  25378. {
  25379. ASSERT(state == STATEreset);
  25380. state = STATEstarted;
  25381. }
  25382. virtual IStrandJunction *getOutputStreams(IRoxieAgentContext *ctx, unsigned idx, PointerArrayOf<IEngineRowStream> &streams, const StrandOptions * consumerOptions, bool consumerOrdered, IOrderedCallbackCollection * orderedCallbacks)
  25383. {
  25384. streams.append(this);
  25385. return NULL;
  25386. }
  25387. virtual IEngineRowStream *queryConcreteOutputStream(unsigned whichInput) { assertex(whichInput==0); return this; }
  25388. virtual IStrandJunction *queryConcreteOutputJunction(unsigned idx) const { assertex(idx==0); return nullptr; }
  25389. virtual IRoxieServerActivity *queryActivity()
  25390. {
  25391. throwUnexpected();
  25392. }
  25393. virtual IIndexReadActivityInfo *queryIndexReadActivity()
  25394. {
  25395. throwUnexpected();
  25396. }
  25397. virtual void stop()
  25398. {
  25399. state = STATEstopped;
  25400. }
  25401. virtual void reset()
  25402. {
  25403. ASSERT(state == STATEstopped);
  25404. eof = false; count = 0; endSeen = 0; allRead = false; state = STATEreset;
  25405. }
  25406. virtual void resetEOF()
  25407. {
  25408. throwUnexpected();
  25409. }
  25410. virtual void checkAbort() {}
  25411. virtual unsigned queryId() const { return activityId; };
  25412. virtual const void *nextRow()
  25413. {
  25414. ActivityTimer t(activityStats, ctx->queryOptions().timeActivities);
  25415. ASSERT(state == STATEstarted);
  25416. ASSERT(allRead || !eof);
  25417. if (eof)
  25418. return NULL;
  25419. again:
  25420. const char *nextSource = input[count++];
  25421. if (nextSource)
  25422. {
  25423. endSeen = 0;
  25424. void *ret = ctx->queryRowManager().ALLOCATE(recordSize);
  25425. memset(ret, 0, recordSize);
  25426. strncpy((char *) ret, nextSource, recordSize);
  25427. return ret;
  25428. }
  25429. else if (repeat)
  25430. {
  25431. repeat--;
  25432. count = 0;
  25433. goto again;
  25434. }
  25435. else
  25436. {
  25437. endSeen++;
  25438. if (endSeen==2)
  25439. eof = true;
  25440. return NULL;
  25441. }
  25442. }
  25443. virtual bool nextGroup(ConstPointerArray & group)
  25444. {
  25445. const void * next;
  25446. while ((next = nextRow()) != NULL)
  25447. group.append(next);
  25448. if (group.ordinality())
  25449. return true;
  25450. return false;
  25451. }
  25452. virtual unsigned __int64 queryTotalCycles() const { return activityStats.totalCycles; }
  25453. virtual unsigned __int64 queryLocalCycles() const { return activityStats.totalCycles; }
  25454. virtual IFinalRoxieInput *queryInput(unsigned idx) const
  25455. {
  25456. return NULL;
  25457. }
  25458. };
  25459. struct SortActivityTest : public CThorSortArg {
  25460. public:
  25461. struct CompareClass : public ICompare {
  25462. virtual int docompare(const void * _left, const void * _right) const {
  25463. return memcmp(_left, _right, 5);
  25464. }
  25465. } compare;
  25466. virtual ICompare * queryCompare() { return &compare; }
  25467. virtual IOutputMetaData * queryOutputMeta()
  25468. {
  25469. return &testMeta;
  25470. }
  25471. virtual unsigned getAlgorithmFlags() { return TAFunstable; }
  25472. virtual const char * getAlgorithm() { return sortAlgorithm; }
  25473. };
  25474. extern "C" IHThorArg * sortActivityTestFactory() { return new SortActivityTest; }
  25475. struct MergeActivityTest : public CThorMergeArg {
  25476. static bool isDedup;
  25477. public:
  25478. struct CompareClass : public ICompare {
  25479. virtual int docompare(const void * _left, const void * _right) const {
  25480. return memcmp(_left, _right, 5);
  25481. }
  25482. } compare;
  25483. virtual ICompare * queryCompare() { return &compare; }
  25484. virtual IOutputMetaData * queryOutputMeta()
  25485. {
  25486. return &testMeta;
  25487. }
  25488. virtual bool dedup() { return isDedup; }
  25489. };
  25490. bool MergeActivityTest::isDedup = false;
  25491. extern "C" IHThorArg * mergeActivityTestFactory() { return new MergeActivityTest; }
  25492. struct SplitActivityTest : public CThorSplitArg {
  25493. public:
  25494. virtual unsigned numBranches() { return 2; }
  25495. virtual IOutputMetaData * queryOutputMeta()
  25496. {
  25497. return &testMeta;
  25498. }
  25499. };
  25500. extern "C" IHThorArg * splitActivityTestFactory() { return new SplitActivityTest; }
  25501. class CcdServerTest : public CppUnit::TestFixture
  25502. {
  25503. CPPUNIT_TEST_SUITE(CcdServerTest);
  25504. CPPUNIT_TEST(testSetup);
  25505. CPPUNIT_TEST(testHeapSort);
  25506. CPPUNIT_TEST(testQuickSort);
  25507. CPPUNIT_TEST(testMerge);
  25508. CPPUNIT_TEST(testMergeDedup);
  25509. CPPUNIT_TEST(testMiscellaneous);
  25510. CPPUNIT_TEST(testSplitter);
  25511. CPPUNIT_TEST(testCleanup);
  25512. CPPUNIT_TEST_SUITE_END();
  25513. protected:
  25514. AgentContextLogger logctx;
  25515. Owned<const IQueryDll> queryDll;
  25516. Owned<IRoxiePackage> package;
  25517. Owned<IPropertyTree> stateInfo;
  25518. Owned<IQueryFactory> queryFactory;
  25519. Owned<IRoxieAgentContext> ctx;
  25520. void testSetup()
  25521. {
  25522. selfTestMode = true;
  25523. roxiemem::setTotalMemoryLimit(false, true, false, 100 * 1024 * 1024, 0, NULL, NULL);
  25524. }
  25525. void testCleanup()
  25526. {
  25527. roxiemem::releaseRoxieHeap();
  25528. }
  25529. void init()
  25530. {
  25531. package.setown(createRoxiePackage(NULL, NULL));
  25532. queryDll.setown(createExeQueryDll("roxie"));
  25533. stateInfo.setown(createPTreeFromXMLString("<test memoryLimit='50000000'/>"));
  25534. queryFactory.setown(createServerQueryFactory("test", queryDll.getLink(), *package, stateInfo, false, false));
  25535. ctx.setown(createAgentContext(queryFactory, logctx, NULL, false));
  25536. queryActiveTimer()->reset();
  25537. }
  25538. void testActivity(IRoxieServerActivity *activity, char const * const *input, char const * const *output)
  25539. {
  25540. testActivity(activity, input, NULL, output);
  25541. }
  25542. void testActivity(IRoxieServerActivity *activity, char const * const *input, char const * const *input2, char const * const *output, unsigned outputIdx = 0)
  25543. {
  25544. TestInput in(ctx, input);
  25545. TestInput in2(ctx, input2);
  25546. activity->setInput(0, 0, &in);
  25547. if (input2)
  25548. activity->setInput(1, 0, &in2);
  25549. IFinalRoxieInput *out = activity->queryOutput(outputIdx);
  25550. IEngineRowStream *outStream = connectSingleStream(ctx, out, outputIdx, true);
  25551. IOutputMetaData *meta = out->queryOutputMeta();
  25552. void *buf = alloca(meta->getFixedSize());
  25553. for (unsigned iteration = 0; iteration < 8; iteration++)
  25554. {
  25555. // 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)
  25556. // This should be true whether we read all, some, or none of the data.
  25557. // Should not matter if an activity is not started
  25558. if (iteration % 4 == 0)
  25559. activity->onCreate(NULL);
  25560. unsigned count = 0;
  25561. if (iteration % 4 != 3)
  25562. {
  25563. activity->start(0, NULL, false);
  25564. ASSERT(in.state == TestInput::STATEstarted);
  25565. ASSERT(!input2 || in2.state == TestInput::STATEstarted);
  25566. for (;;)
  25567. {
  25568. const void *next = outStream->nextRow();
  25569. if (!next)
  25570. {
  25571. ASSERT(output[count++] == NULL);
  25572. next = outStream->nextRow();
  25573. if (!next)
  25574. {
  25575. ASSERT(output[count++] == NULL);
  25576. break;
  25577. }
  25578. }
  25579. ASSERT(output[count] != NULL);
  25580. unsigned outsize = meta->getRecordSize(next);
  25581. memset(buf, 0, outsize);
  25582. strncpy((char *) buf, output[count++], outsize);
  25583. ASSERT(memcmp(next, buf, outsize) == 0);
  25584. ReleaseRoxieRow(next);
  25585. if (iteration % 4 == 2)
  25586. break;
  25587. }
  25588. if (iteration % 4 != 2)
  25589. {
  25590. // Check that reading after end is harmless
  25591. in.allRead = true;
  25592. const void *next = outStream->nextRow();
  25593. ASSERT(next == NULL);
  25594. }
  25595. }
  25596. activity->stop();
  25597. ASSERT(in.state == TestInput::STATEstopped);
  25598. ASSERT(!input2 || in2.state == TestInput::STATEstopped);
  25599. activity->reset();
  25600. ASSERT(in.state == TestInput::STATEreset);
  25601. ASSERT(!input2 || in2.state == TestInput::STATEreset);
  25602. ctx->queryRowManager().reportLeaks();
  25603. ASSERT(ctx->queryRowManager().numPagesAfterCleanup(true) == 0);
  25604. }
  25605. }
  25606. void testSplitActivity(IRoxieServerActivityFactory *factory, char const * const *input, char const * const *output, unsigned numOutputs, unsigned repeat)
  25607. {
  25608. Owned <IRoxieServerActivity> activity = factory->createActivity(ctx, NULL);
  25609. TestInput in(ctx, input);
  25610. in.repeat = repeat;
  25611. activity->setInput(0, 0, &in);
  25612. ArrayOf<IFinalRoxieInput *> out;
  25613. for (unsigned i = 0; i < numOutputs; i++)
  25614. {
  25615. out.append(activity->queryOutput(i));
  25616. }
  25617. activity->onCreate(NULL);
  25618. class casyncfor: public CAsyncFor
  25619. {
  25620. public:
  25621. casyncfor(ArrayOf<IFinalRoxieInput *> &_outputs, char const * const *_output, unsigned _repeat)
  25622. : outputs(_outputs), output(_output), repeat(_repeat)
  25623. {}
  25624. void Do(unsigned i)
  25625. {
  25626. IFinalRoxieInput *out = outputs.item(i);
  25627. IEngineRowStream *outStream = connectSingleStream(NULL, out, 0, true);
  25628. out->start(0, NULL, false);
  25629. IOutputMetaData *meta = out->queryOutputMeta();
  25630. void *buf = alloca(meta->getFixedSize());
  25631. unsigned count = 0;
  25632. unsigned repeats = repeat;
  25633. for (;;)
  25634. {
  25635. const void *next = outStream->nextRow();
  25636. if (!next)
  25637. {
  25638. ASSERT(repeats==0);
  25639. ASSERT(output[count] == NULL);
  25640. count++;
  25641. next = outStream->nextRow();
  25642. if (!next)
  25643. {
  25644. ASSERT(output[count] == NULL);
  25645. count++;
  25646. break;
  25647. }
  25648. }
  25649. if (output[count] == NULL && repeats)
  25650. {
  25651. repeats--;
  25652. count = 0;
  25653. }
  25654. ASSERT(output[count] != NULL);
  25655. unsigned outsize = meta->getRecordSize(next);
  25656. memset(buf, 0, outsize);
  25657. strncpy((char *) buf, output[count++], outsize);
  25658. ASSERT(memcmp(next, buf, outsize) == 0);
  25659. ReleaseRoxieRow(next);
  25660. }
  25661. outStream->stop();
  25662. }
  25663. private:
  25664. ArrayOf<IFinalRoxieInput *> &outputs;
  25665. char const * const *output;
  25666. unsigned repeat;
  25667. } afor(out, output, repeat);
  25668. afor.For(numOutputs, numOutputs);
  25669. ASSERT(in.state == TestInput::STATEstopped);
  25670. for (unsigned i2 = 0; i2 < numOutputs; i2++)
  25671. {
  25672. out.item(i2)->reset();
  25673. }
  25674. ASSERT(in.state == TestInput::STATEreset);
  25675. ctx->queryRowManager().reportLeaks();
  25676. ASSERT(ctx->queryRowManager().numPagesAfterCleanup(true) == 0);
  25677. }
  25678. static int compareFunc(const void *l, const void *r)
  25679. {
  25680. return strcmp(*(char **) l, *(char **) r);
  25681. }
  25682. void testSort(unsigned type)
  25683. {
  25684. init();
  25685. sortAlgorithm = NULL;
  25686. if (type==2)
  25687. sortAlgorithm = "heapSort";
  25688. else
  25689. sortAlgorithm = "quickSort";
  25690. DBGLOG("Testing %s activity", sortAlgorithm);
  25691. Owned<IPropertyTree> dummyNode = createPTree("node");
  25692. Owned <IRoxieServerActivityFactory> factory = createRoxieServerSortActivityFactory(1, 1, *queryFactory, sortActivityTestFactory, TAKsort, *dummyNode);
  25693. Owned <IRoxieServerActivity> activity = factory->createActivity(ctx, NULL);
  25694. const char * test[] = { NULL, NULL };
  25695. const char * test12345[] = { "1", "2", "3", "4", "5", NULL, NULL };
  25696. const char * test54321[] = { "5", "4", "3", "2", "1", NULL, NULL };
  25697. const char * test11111[] = { "1", "1", "1", "1", "1", NULL, NULL };
  25698. const char * test11111_12345[] = { "1", "1", "1", "1", "1", NULL, "1", "2", "3", "4", "5", NULL, NULL };
  25699. const char * test11111_54321[] = { "1", "1", "1", "1", "1", NULL, "5", "4", "3", "2", "1", NULL, NULL };
  25700. const char * test54321_54321[] = { "5", "4", "3", "2", "1", NULL, "5", "4", "3", "2", "1", NULL, NULL };
  25701. const char * test12345_12345[] = { "1", "2", "3", "4", "5", NULL, "1", "2", "3", "4", "5", NULL, NULL };
  25702. testActivity(activity, test, test);
  25703. testActivity(activity, test12345, test12345);
  25704. testActivity(activity, test54321, test12345);
  25705. testActivity(activity, test11111, test11111);
  25706. testActivity(activity, test11111_12345, test11111_12345);
  25707. testActivity(activity, test11111_54321, test11111_12345);
  25708. testActivity(activity, test54321_54321, test12345_12345);
  25709. // A few larger tests
  25710. char *input[2002];
  25711. char *output[2002];
  25712. input[2000] = input[2001] = output[2000] = output[2001] = NULL;
  25713. unsigned i;
  25714. // identical
  25715. for (i=0; i<2000; i++)
  25716. {
  25717. input[i] = new char[11];
  25718. output[i] = new char[11];
  25719. sprintf(input[i], "1");
  25720. sprintf(output[i], "1");
  25721. }
  25722. testActivity(activity, input, output);
  25723. // Ascending
  25724. for (i=0; i<2000; i++)
  25725. {
  25726. sprintf(input[i], "%04d", i);
  25727. sprintf(output[i], "%04d", i);
  25728. }
  25729. testActivity(activity, input, output);
  25730. // Almost sorted
  25731. for (i=0; i<20; i++)
  25732. {
  25733. unsigned h = i*100;
  25734. sprintf(input[h], "%04d", 1900-h);
  25735. }
  25736. testActivity(activity, input, output);
  25737. // Descending
  25738. for (i=0; i<2000; i++)
  25739. {
  25740. sprintf(input[i], "%04d", 1999-i);
  25741. sprintf(output[i], "%04d", i);
  25742. }
  25743. testActivity(activity, input, output);
  25744. // Random
  25745. for (i=0; i<2000; i++)
  25746. {
  25747. unsigned r = fastRand() % 1500;
  25748. sprintf(input[i], "%04d", r);
  25749. sprintf(output[i], "%04d", r);
  25750. }
  25751. qsort(output, 2000, sizeof(output[0]), compareFunc);
  25752. testActivity(activity, input, output);
  25753. unsigned __int64 us = factory->queryLocalTimeNs();
  25754. DBGLOG("Simple %s sorts: activity time %u.%u ms", type==2?"Heap" : (type==1 ? "Insertion" : "Quick"), (int)(us/1000), (int)(us%1000));
  25755. factory->resetNodeProgressInfo();
  25756. if (type)
  25757. {
  25758. // Other than quicksort, it's supposed to be stable. Let's check that it is
  25759. // All sort identical
  25760. for (i=0; i<2000; i++)
  25761. {
  25762. sprintf(input[i], "1 %d", i);
  25763. sprintf(output[i], "1 %d", i);
  25764. }
  25765. testActivity(activity, input, output);
  25766. // Already sorted
  25767. for (i=0; i<2000; i++)
  25768. {
  25769. sprintf(input[i], "%04d %d", i / 10, i);
  25770. sprintf(output[i], "%04d %d", i / 10, i);
  25771. }
  25772. testActivity(activity, input, output);
  25773. // Reverse order
  25774. for (i=0; i<2000; i++)
  25775. {
  25776. sprintf(input[i], "%04d %d", 199 - (i / 10), i%10);
  25777. sprintf(output[i], "%04d %d", i / 10, i%10);
  25778. }
  25779. testActivity(activity, input, output);
  25780. }
  25781. for (i=0; i<2000; i++)
  25782. {
  25783. delete [] input[i];
  25784. delete [] output[i];
  25785. }
  25786. DBGLOG("Finished testing %s sort", type==2?"Heap" : (type==1 ? "Insertion" : "Quick"));
  25787. }
  25788. void testQuickSort()
  25789. {
  25790. testSort(0);
  25791. }
  25792. void testHeapSort()
  25793. {
  25794. testSort(2);
  25795. }
  25796. void testMerge()
  25797. {
  25798. DBGLOG("testMerge");
  25799. init();
  25800. Owned<IPropertyTree> splitterNode = createPTree();
  25801. Owned <IRoxieServerActivityFactory> factory = createRoxieServerMergeActivityFactory(1, 1, *queryFactory, mergeActivityTestFactory, TAKmerge, *splitterNode);
  25802. factory->setInput(0,0,0);
  25803. factory->setInput(1,0,0);
  25804. Owned <IRoxieServerActivity> activity = factory->createActivity(ctx, NULL);
  25805. const char * test[] = { NULL, NULL };
  25806. const char * test12345[] = { "1", "2", "3", "4", "5", NULL, NULL };
  25807. const char * test1122334455[] = { "1", "1", "2", "2", "3", "3", "4", "4", "5", "5", NULL, NULL };
  25808. const char * test11111[] = { "1", "1", "1", "1", "1", NULL, NULL };
  25809. const char * test1111111111[] = { "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", NULL, NULL };
  25810. const char * test11111_12345[] = { "1", "1", "1", "1", "1", NULL, "1", "2", "3", "4", "5", NULL, NULL };
  25811. const char * test1111112345[] = { "1", "1", "1", "1", "1", "1", "2", "3", "4", "5", NULL, NULL };
  25812. 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 };
  25813. testActivity(activity, test, test, test);
  25814. testActivity(activity, test12345, test, test12345);
  25815. testActivity(activity, test, test12345, test12345);
  25816. testActivity(activity, test12345, test12345, test1122334455);
  25817. testActivity(activity, test11111, test, test11111);
  25818. testActivity(activity, test, test11111, test11111);
  25819. testActivity(activity, test11111, test11111, test1111111111);
  25820. testActivity(activity, test11111_12345, test, test1111112345);
  25821. testActivity(activity, test11111_12345, test11111_12345, test11111111111122334455);
  25822. // Should really test WHICH side gets kept...
  25823. // Should test with more than 2 inputs...
  25824. DBGLOG("testMerge done");
  25825. }
  25826. void testMergeDedup()
  25827. {
  25828. DBGLOG("testMergeDedup");
  25829. init();
  25830. MergeActivityTest::isDedup = true;
  25831. Owned<IPropertyTree> splitterNode = createPTree();
  25832. Owned <IRoxieServerActivityFactory> factory = createRoxieServerMergeActivityFactory(1, 1, *queryFactory, mergeActivityTestFactory, TAKmerge, *splitterNode);
  25833. factory->setInput(0,0,0);
  25834. factory->setInput(1,0,0);
  25835. Owned <IRoxieServerActivity> activity = factory->createActivity(ctx, NULL);
  25836. const char * test[] = { NULL, NULL };
  25837. const char * test12345[] = { "1", "2", "3", "4", "5", NULL, NULL };
  25838. const char * test11111[] = { "1", "1", "1", "1", "1", NULL, NULL };
  25839. const char * test11111_12345[] = { "1", "1", "1", "1", "1", NULL, "1", "2", "3", "4", "5", NULL, NULL };
  25840. const char * test1111112345[] = { "1", "1", "1", "1", "1", "1", "2", "3", "4", "5", NULL, NULL };
  25841. testActivity(activity, test11111, test, test11111); // No dedup within a stream
  25842. testActivity(activity, test11111, test11111, test11111); // No dedup within a stream
  25843. testActivity(activity, test, test11111, test11111);
  25844. testActivity(activity, test, test, test);
  25845. testActivity(activity, test12345, test, test12345);
  25846. testActivity(activity, test, test12345, test12345);
  25847. testActivity(activity, test12345, test12345, test12345);
  25848. testActivity(activity, test11111_12345, test, test1111112345);
  25849. testActivity(activity, test11111_12345, test11111_12345, test1111112345);
  25850. // Should really test WHICH side gets kept...
  25851. // Should test with more than 2 inputs...
  25852. DBGLOG("testMergeDedup done");
  25853. }
  25854. void doTestSplitter(unsigned numOutputs)
  25855. {
  25856. DBGLOG("testSplit %d", numOutputs);
  25857. init();
  25858. Owned<IPropertyTree> splitterNode = createPTree();
  25859. Owned <IRoxieServerActivityFactory> factory = createRoxieServerThroughSpillActivityFactory(*queryFactory, splitActivityTestFactory, numOutputs, *splitterNode);
  25860. factory->setInput(0,0,0);
  25861. Owned <IRoxieServerActivity> activity = factory->createActivity(ctx, NULL);
  25862. const char * test[] = { NULL, NULL };
  25863. const char * test12345[] = { "1", "2", "3", "4", "5", NULL, NULL };
  25864. unsigned start = msTick();
  25865. testSplitActivity(factory, test, test, numOutputs, 0);
  25866. testSplitActivity(factory, test12345, test12345, numOutputs, 0);
  25867. testSplitActivity(factory, test12345, test12345, numOutputs, 100);
  25868. unsigned elapsed = msTick() - start;
  25869. DBGLOG("testSplit %d done in %dms", numOutputs, elapsed);
  25870. }
  25871. void testSplitter()
  25872. {
  25873. doTestSplitter(1);
  25874. doTestSplitter(2);
  25875. doTestSplitter(3);
  25876. doTestSplitter(10);
  25877. }
  25878. void testMiscellaneous()
  25879. {
  25880. DBGLOG("sizeof(CriticalSection)=%u", (unsigned) sizeof(CriticalSection));
  25881. DBGLOG("sizeof(SpinLock)=%u", (unsigned) sizeof(SpinLock));
  25882. DBGLOG("sizeof(CJoinGroup)=%u", (unsigned) sizeof(CJoinGroup));
  25883. ASSERT(sizeof(CJoinGroup) <= 120);
  25884. }
  25885. };
  25886. CPPUNIT_TEST_SUITE_REGISTRATION( CcdServerTest );
  25887. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CcdServerTest, "CcdServerTest" );
  25888. #endif