123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661 |
- /*##############################################################################
- Copyright (C) 2011 HPCC Systems.
- All rights reserved. This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- ############################################################################## */
- #include "jmisc.hpp"
- #include "jdebug.hpp"
- #include "jptree.hpp"
- #include "rtlkey.hpp"
- #include "jsort.hpp"
- #include "jhtree.hpp"
- #include "jqueue.tpp"
- #include "jisem.hpp"
- #include "thorxmlread.hpp"
- #include "thorrparse.ipp"
- #include "thorxmlwrite.hpp"
- #include "thorsoapcall.hpp"
- #include "thorcommon.ipp"
- #include "jlzw.hpp"
- #include "jregexp.hpp"
- #include "javahash.hpp"
- #include "javahash.tpp"
- #include "thorstep.ipp"
- #include "thorpipe.hpp"
- #include "thorfile.hpp"
- #include "eclhelper.hpp"
- #include "eclrtl_imp.hpp"
- #include "rtlfield_imp.hpp"
- #include "rtlds_imp.hpp"
- #include "rtlread_imp.hpp"
- #include "dafdesc.hpp"
- #include "dautils.hpp"
- #include "wujobq.hpp"
- namespace ccdserver_hqlhelper
- {
- #include "eclhelper_base.hpp"
- }
- #include "ccd.hpp"
- #include "ccdserver.hpp"
- #include "ccdactivities.hpp"
- #include "ccdquery.hpp"
- #include "ccdstate.hpp"
- #include "ccdqueue.ipp"
- #include "ccdsnmp.hpp"
- #include "ccddali.hpp"
- #include "jsmartsock.hpp"
- #include "dllserver.hpp"
- #include "workflow.hpp"
- #include "nbcd.hpp"
- #include "roxiemem.hpp"
- #include "roxierowbuff.hpp"
- #include "roxiehelper.hpp"
- #include "roxielmj.hpp"
- #include "roxierow.hpp"
- #include "thorplugin.hpp"
- #include "keybuild.hpp"
- #define MAX_HTTP_HEADERSIZE 8000
- #define MIN_PAYLOAD_SIZE 800
- #pragma warning(disable : 4355)
- #define DEFAULT_PARALLEL_LOOP_THREADS 1
- #define PROBE
- #ifdef _DEBUG
- //#define FAKE_EXCEPTIONS
- //#define TRACE_JOINGROUPS
- //#define TRACE_SPLIT
- //#define _CHECK_HEAPSORT
- //#undef PARALLEL_EXECUTE
- //#define TRACE_SEEK_REQUESTS
- #endif
- using roxiemem::OwnedRoxieRow;
- using roxiemem::OwnedConstRoxieRow;
- using roxiemem::IRowManager;
- // 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...
- #if _MSC_VER==1200
- #define protected public
- #endif
- #define TRACE_STARTSTOP // This determines if it is available - it is enabled/disabled by a configuration option
-
- static const SmartStepExtra dummySmartStepExtra(SSEFreadAhead, NULL);
- typedef IEclProcess* (* EclProcessFactory)();
- inline void ReleaseRoxieRowSet(ConstPointerArray &data)
- {
- ForEachItemIn(idx, data)
- ReleaseRoxieRow(data.item(idx));
- data.kill();
- }
- //=================================================================================
- class RestartableThread : public CInterface
- {
- class MyThread : public Thread
- {
- Linked<RestartableThread> owner;
- public:
- MyThread(RestartableThread *_owner, const char *name) : Thread(name), owner(_owner)
- {
- }
- virtual int run()
- {
- owner->started.signal();
- return owner->run();
- }
- };
- friend class MyThread;
- Semaphore started;
- Owned<MyThread> thread;
- CriticalSection crit;
- StringAttr name;
- public:
- RestartableThread(const char *_name) : name(_name)
- {
- }
- virtual void start(const char *namePrefix)
- {
- StringBuffer s(namePrefix);
- s.append(name);
- {
- CriticalBlock b(crit);
- assertex(!thread);
- thread.setown(new MyThread(this, s));
- thread->start();
- }
- started.wait();
- }
- virtual void join()
- {
- {
- Owned<Thread> tthread;
- {
- CriticalBlock b(crit);
- tthread.setown(thread.getClear());
- }
- if (tthread)
- tthread->join();
- }
- }
- virtual int run() = 0;
- };
- //================================================================================
- // default implementation - can be overridden for efficiency...
- bool IRoxieInput::nextGroup(ConstPointerArray & group)
- {
- const void * next;
- while ((next = nextInGroup()) != NULL)
- group.append(next);
- if (group.ordinality())
- return true;
- return false;
- }
- inline const void * nextUngrouped(IRoxieInput * input)
- {
- const void * ret = input->nextInGroup();
- if (!ret)
- ret = input->nextInGroup();
- return ret;
- };
- //=================================================================================
- //The following don't link their arguments because that creates a circular reference
- //But I wish there was a better way
- class IndirectSlaveContext : public CInterface, implements IRoxieSlaveContext
- {
- public:
- IndirectSlaveContext(IRoxieSlaveContext * _ctx = NULL) : ctx(_ctx) {}
- IMPLEMENT_IINTERFACE
- void set(IRoxieSlaveContext * _ctx) { ctx = _ctx; }
- virtual ICodeContext *queryCodeContext()
- {
- return ctx->queryCodeContext();
- }
- virtual void checkAbort()
- {
- ctx->checkAbort();
- }
- virtual void notifyAbort(IException *E)
- {
- ctx->notifyAbort(E);
- }
- virtual IActivityGraph * queryChildGraph(unsigned id)
- {
- return ctx->queryChildGraph(id);
- }
- virtual void noteChildGraph(unsigned id, IActivityGraph *childGraph)
- {
- ctx->noteChildGraph(id, childGraph) ;
- }
- virtual IRowManager &queryRowManager()
- {
- return ctx->queryRowManager();
- }
- virtual void noteStatistic(unsigned statCode, unsigned __int64 value, unsigned count) const
- {
- ctx->noteStatistic(statCode, value, count);
- }
- virtual void CTXLOG(const char *format, ...) const
- {
- va_list args;
- va_start(args, format);
- ctx->CTXLOGva(format, args);
- va_end(args);
- }
- virtual void CTXLOGva(const char *format, va_list args) const
- {
- ctx->CTXLOGva(format, args);
- }
- virtual void CTXLOGa(TracingCategory category, const char *prefix, const char *text) const
- {
- ctx->CTXLOGa(category, prefix, text);
- }
- virtual void logOperatorException(IException *E, const char *file, unsigned line, const char *format, ...) const
- {
- va_list args;
- va_start(args, format);
- ctx->logOperatorExceptionVA(E, file, line, format, args);
- va_end(args);
- }
- virtual void logOperatorExceptionVA(IException *E, const char *file, unsigned line, const char *format, va_list args) const
- {
- ctx->logOperatorExceptionVA(E, file, line, format, args);
- }
- virtual void CTXLOGae(IException *E, const char *file, unsigned line, const char *prefix, const char *format, ...) const
- {
- va_list args;
- va_start(args, format);
- ctx->CTXLOGaeva(E, file, line, prefix, format, args);
- va_end(args);
- }
- virtual void CTXLOGaeva(IException *E, const char *file, unsigned line, const char *prefix, const char *format, va_list args) const
- {
- ctx->CTXLOGaeva(E, file, line, prefix, format, args);
- }
- virtual void CTXLOGl(LogItem *log) const
- {
- ctx->CTXLOGl(log);
- }
- virtual StringBuffer &getLogPrefix(StringBuffer &ret) const
- {
- return ctx->getLogPrefix(ret);
- }
- virtual unsigned queryTraceLevel() const
- {
- return ctx->queryTraceLevel();
- }
- virtual bool isIntercepted() const
- {
- return ctx->isIntercepted();
- }
- virtual bool isBlind() const
- {
- return ctx->isBlind();
- }
- virtual unsigned parallelJoinPreload()
- {
- return ctx->parallelJoinPreload();
- }
- virtual unsigned concatPreload()
- {
- return ctx->concatPreload();
- }
- virtual unsigned fetchPreload()
- {
- return ctx->fetchPreload();
- }
- virtual unsigned fullKeyedJoinPreload()
- {
- return ctx->fullKeyedJoinPreload();
- }
- virtual unsigned keyedJoinPreload()
- {
- return ctx->keyedJoinPreload();
- }
- virtual unsigned prefetchProjectPreload()
- {
- return ctx->prefetchProjectPreload();
- }
- virtual void addSlavesReplyLen(unsigned len)
- {
- ctx->addSlavesReplyLen(len);
- }
- virtual const char *queryAuthToken()
- {
- return ctx->queryAuthToken();
- }
- virtual const IResolvedFile *resolveLFN(const char *filename, bool isOpt)
- {
- return ctx->resolveLFN(filename, isOpt);
- }
- virtual IRoxieWriteHandler *createLFN(const char *filename, bool overwrite, bool extend, const StringArray &clusters)
- {
- return ctx->createLFN(filename, overwrite, extend, clusters);
- }
- virtual void onFileCallback(const RoxiePacketHeader &header, const char *lfn, bool isOpt, bool isLocal)
- {
- ctx->onFileCallback(header, lfn, isOpt, isLocal);
- }
- virtual IActivityGraph *getLibraryGraph(const LibraryCallFactoryExtra &extra, IRoxieServerActivity *parentActivity)
- {
- return ctx->getLibraryGraph(extra, parentActivity);
- }
- virtual void noteProcessed(const IRoxieContextLogger &_activityContext, const IRoxieServerActivity *_activity, unsigned _idx, unsigned _processed, unsigned __int64 _totalCycles, unsigned __int64 _localCycles) const
- {
- ctx->noteProcessed(_activityContext, _activity, _idx, _processed, _totalCycles, _localCycles);
- }
- virtual IProbeManager *queryProbeManager() const
- {
- return ctx->queryProbeManager();
- }
- virtual IDebuggableContext *queryDebugContext() const
- {
- return ctx->queryDebugContext();
- }
- virtual bool queryTimeActivities() const
- {
- return ctx->queryTimeActivities();
- }
- virtual void printResults(IXmlWriter *output, const char *name, unsigned sequence)
- {
- ctx->printResults(output, name, sequence);
- }
- virtual void setWUState(WUState state)
- {
- ctx->setWUState(state);
- }
- virtual bool checkWuAborted()
- {
- return ctx->checkWuAborted();
- }
- virtual IWorkUnit *updateWorkUnit() const
- {
- return ctx->updateWorkUnit();
- }
- virtual IConstWorkUnit *queryWorkUnit() const
- {
- return ctx->queryWorkUnit();
- }
- virtual IRoxieServerContext *queryServerContext()
- {
- return ctx->queryServerContext();
- }
- protected:
- IRoxieSlaveContext * ctx;
- };
- //=================================================================================
- #define RESULT_FLUSH_THRESHOLD 10000u
- #ifdef _DEBUG
- #define SOAP_SPLIT_THRESHOLD 100u
- #define SOAP_SPLIT_RESERVE 200u
- #else
- #define SOAP_SPLIT_THRESHOLD 64000u
- #define SOAP_SPLIT_RESERVE 65535u
- #endif
- //=================================================================================
- class CRoxieServerActivityFactoryBase : public CActivityFactory, implements IRoxieServerActivityFactory
- {
- protected:
- IntArray dependencies; // things I am dependent on
- IntArray dependencyIndexes; // things I am dependent on
- IntArray dependencyControlIds; // things I am dependent on
- StringArray dependencyEdgeIds; // How to describe them to the debugger
- unsigned dependentCount; // things dependent on me
- mutable CriticalSection statsCrit;
- mutable __int64 processed;
- mutable __int64 started;
- mutable __int64 totalCycles;
- mutable __int64 localCycles;
- public:
- IMPLEMENT_IINTERFACE;
- CRoxieServerActivityFactoryBase(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- processed = 0;
- started = 0;
- totalCycles = 0;
- localCycles = 0;
- dependentCount = 0;
- }
-
- ~CRoxieServerActivityFactoryBase()
- {
- }
- StringBuffer &toString(StringBuffer &ret) const
- {
- return ret.appendf("%p", this);
- }
- virtual void addDependency(unsigned _source, ThorActivityKind _kind, unsigned _sourceIdx, int controlId, const char *edgeId)
- {
- dependencies.append(_source);
- dependencyIndexes.append(_sourceIdx);
- dependencyControlIds.append(controlId);
- dependencyEdgeIds.append(edgeId);
- }
- virtual void noteDependent(unsigned target)
- {
- dependentCount++;
- }
- virtual IntArray &queryDependencies() { return dependencies; }
- virtual IntArray &queryDependencyIndexes() { return dependencyIndexes; }
- virtual IntArray &queryDependencyControlIds() { return dependencyControlIds; }
- virtual StringArray &queryDependencyEdgeIds() { return dependencyEdgeIds; }
- virtual unsigned queryId() const { return id; }
- virtual unsigned querySubgraphId() const { return subgraphId; }
- virtual ThorActivityKind getKind() const { return kind; }
- virtual IOutputMetaData * queryOutputMeta() const
- {
- return meta;
- }
- virtual bool isSink() const
- {
- return false;
- }
- virtual bool isFunction() const
- {
- return false;
- }
- virtual bool isGraphInvariant() const
- {
- return false;
- }
- virtual IHThorArg &getHelper() const
- {
- return *helperFactory();
- }
- virtual IRoxieServerActivity *createFunction(IHThorArg &arg, IProbeManager *_probeManager) const
- {
- arg.Release();
- throwUnexpected();
- }
- virtual void noteProcessed(unsigned idx, unsigned _processed, unsigned __int64 _totalCycles, unsigned __int64 _localCycles) const
- {
- if (_processed || _totalCycles || _localCycles)
- {
- CriticalBlock b(statsCrit);
- #ifdef _DEBUG
- assertex(_totalCycles >= _localCycles);
- #endif
- processed += _processed;
- totalCycles += _totalCycles;
- localCycles += _localCycles;
- }
- }
- virtual void noteStarted() const
- {
- CriticalBlock b(statsCrit);
- started ++;
- }
- virtual void noteStarted(unsigned idx) const
- {
- throwUnexpected(); // should be implemented/required by multiOutput cases only
- }
- virtual void getEdgeProgressInfo(unsigned output, IPropertyTree &edge) const
- {
- CriticalBlock b(statsCrit);
- if (output == 0)
- {
- putStatsValue(&edge, "count", "sum", processed);
- if (started)
- putStatsValue(&edge, "started", "sum", started);
- }
- else
- ERRLOG("unexpected call to getEdgeProcessInfo for output %d in activity %d", output, queryId());
- }
- virtual void getNodeProgressInfo(IPropertyTree &node) const
- {
- CActivityFactory::getNodeProgressInfo(node);
- CriticalBlock b(statsCrit);
- if (started)
- putStatsValue(&node, "_roxieStarted", "sum", started);
- if (totalCycles)
- putStatsValue(&node, "totalTime", "sum", (unsigned) (cycle_to_nanosec(totalCycles)/1000));
- if (localCycles)
- putStatsValue(&node, "localTime", "sum", (unsigned) (cycle_to_nanosec(localCycles)/1000));
- }
- virtual void resetNodeProgressInfo()
- {
- CActivityFactory::resetNodeProgressInfo();
- CriticalBlock b(statsCrit);
- started = 0;
- totalCycles = 0;
- localCycles = 0;
- }
- virtual void getActivityMetrics(StringBuffer &reply) const
- {
- CActivityFactory::getActivityMetrics(reply);
- CriticalBlock b(statsCrit);
- putStatsValue(reply, "_roxieStarted", "sum", started);
- putStatsValue(reply, "totalTime", "sum", (unsigned) (cycle_to_nanosec(totalCycles)/1000));
- putStatsValue(reply, "localTime", "sum", (unsigned) (cycle_to_nanosec(localCycles)/1000));
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- return localCycles;
- }
- virtual IQueryFactory &queryQueryFactory() const
- {
- return CActivityFactory::queryQueryFactory();
- }
- virtual ActivityArray *queryChildQuery(unsigned idx, unsigned &id)
- {
- return CActivityFactory::queryChildQuery(idx, id);
- }
- virtual void addChildQuery(unsigned id, ActivityArray *childQuery)
- {
- CActivityFactory::addChildQuery(id, childQuery);
- }
- virtual void createChildQueries(IArrayOf<IActivityGraph> &childGraphs, IRoxieServerActivity *parentActivity, IProbeManager *_probeManager, const IRoxieContextLogger &_logctx) const
- {
- ForEachItemIn(idx, childQueries)
- {
- childGraphs.append(*createActivityGraph(NULL, childQueryIndexes.item(idx), childQueries.item(idx), parentActivity, _probeManager, _logctx));
- }
- }
- virtual void onCreateChildQueries(IRoxieSlaveContext *ctx, IHThorArg *colocalArg, IArrayOf<IActivityGraph> &childGraphs) const
- {
- ForEachItemIn(idx, childGraphs)
- {
- ctx->noteChildGraph(childQueryIndexes.item(idx), &childGraphs.item(idx));
- childGraphs.item(idx).onCreate(ctx, colocalArg);
- }
- }
- IActivityGraph * createChildGraph(IRoxieSlaveContext * ctx, IHThorArg *colocalArg, unsigned childId, IRoxieServerActivity *parentActivity, IProbeManager * _probeManager, const IRoxieContextLogger &_logctx) const
- {
- unsigned match = childQueryIndexes.find(childId);
- assertex(match != NotFound);
- Owned<IActivityGraph> graph = createActivityGraph(NULL, childQueryIndexes.item(match), childQueries.item(match), parentActivity, _probeManager, _logctx);
- graph->onCreate(ctx, colocalArg);
- return graph.getClear();
- }
- virtual IRoxieServerSideCache *queryServerSideCache() const
- {
- return NULL; // Activities that wish to support server-side caching will need to do better....
- }
- virtual bool getEnableFieldTranslation() const
- {
- throwUnexpected(); // only implemented by index-related subclasses
- }
- virtual IDefRecordMeta *queryActivityMeta() const
- {
- throwUnexpected(); // only implemented by index-related subclasses
- }
- virtual void noteStatistic(unsigned statCode, unsigned __int64 value, unsigned count) const
- {
- mystats.noteStatistic(statCode, value, count);
- }
- };
- class CRoxieServerMultiInputInfo
- {
- private:
- UnsignedArray inputs;
- UnsignedArray inputIndexes;
- public:
- void set(unsigned idx, unsigned source, unsigned sourceidx)
- {
- if (idx==inputs.length())
- {
- inputs.append(source);
- inputIndexes.append(sourceidx);
- }
- else
- {
- while (!inputs.isItem(idx))
- {
- inputs.append(0);
- inputIndexes.append(0);
- }
- inputs.replace(source, idx);
- inputIndexes.replace(sourceidx, idx);
- }
- }
- unsigned get(unsigned idx, unsigned &sourceidx) const
- {
- if (inputs.isItem(idx))
- {
- sourceidx = inputIndexes.item(idx);
- return inputs.item(idx);
- }
- else
- return (unsigned) -1;
- }
- inline unsigned ordinality() const { return inputs.ordinality(); }
- };
- class CRoxieServerMultiInputFactory : public CRoxieServerActivityFactoryBase
- {
- private:
- CRoxieServerMultiInputInfo inputs;
- public:
- IMPLEMENT_IINTERFACE;
- CRoxieServerMultiInputFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactoryBase(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- inputs.set(idx, source, sourceidx);
- }
- virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
- {
- return inputs.get(idx, sourceidx);
- }
- inline unsigned numInputs() const { return inputs.ordinality(); }
- };
- class CWrappedException : public CInterface, implements IException
- {
- Owned<IException> wrapped;
- ThorActivityKind kind;
- unsigned queryId;
- public:
- IMPLEMENT_IINTERFACE;
- CWrappedException(IException *_wrapped, ThorActivityKind _kind, unsigned _queryId)
- : wrapped(_wrapped), kind(_kind), queryId(_queryId)
- {
- }
- virtual int errorCode() const { return wrapped->errorCode(); }
- virtual StringBuffer & errorMessage(StringBuffer &msg) const { return wrapped->errorMessage(msg).appendf(" (in %s %d)", getActivityText(kind), queryId); }
- virtual MessageAudience errorAudience() const { return wrapped->errorAudience(); }
- };
- class CRoxieServerActivityFactory : public CRoxieServerActivityFactoryBase
- {
- protected:
- unsigned input;
- unsigned inputidx;
- public:
- IMPLEMENT_IINTERFACE;
- CRoxieServerActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactoryBase(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- input = (unsigned) -1;
- inputidx = 0;
- }
- inline void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- if (idx != 0)
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: id = %d : setInput() parameter out of bounds idx = %d at %s(%d)", id, idx, __FILE__, __LINE__);
- if (input != -1)
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: id = %d : setInput() called twice for input = %d source = %d inputidx = %d sourceidx = %d at %s(%d)", id, input, source, inputidx, sourceidx, __FILE__, __LINE__);
- input = source;
- inputidx = sourceidx;
- }
- virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
- {
- if (!idx)
- {
- sourceidx = inputidx;
- return input;
- }
- return (unsigned) -1;
- }
- };
- class CRoxieServerMultiOutputFactory : public CRoxieServerActivityFactory
- {
- protected:
- unsigned numOutputs;
- unsigned __int64 *processedArray;
- bool *startedArray;
- CRoxieServerMultiOutputFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- numOutputs = 0;
- processedArray = NULL;
- startedArray = NULL;
- }
- ~CRoxieServerMultiOutputFactory()
- {
- delete [] processedArray;
- delete [] startedArray;
- }
- void setNumOutputs(unsigned num)
- {
- numOutputs = num;
- if (!num)
- num = 1; // Even sink activities like to track how many records they process
- processedArray = new unsigned __int64[num];
- startedArray = new bool[num];
- for (unsigned i = 0; i < num; i++)
- {
- processedArray[i] = 0;
- startedArray[i] = 0;
- }
- }
- virtual void getEdgeProgressInfo(unsigned idx, IPropertyTree &edge) const
- {
- assertex(numOutputs ? idx < numOutputs : idx==0);
- CriticalBlock b(statsCrit);
- putStatsValue(&edge, "count", "sum", processedArray[idx]);
- putStatsValue(&edge, "started", "sum", startedArray[idx]);
- }
- virtual void noteProcessed(unsigned idx, unsigned _processed, unsigned __int64 _totalCycles, unsigned __int64 _localCycles) const
- {
- assertex(numOutputs ? idx < numOutputs : idx==0);
- CriticalBlock b(statsCrit);
- processedArray[idx] += _processed;
- totalCycles += _totalCycles;
- localCycles += _localCycles;
- }
- virtual void noteStarted(unsigned idx) const
- {
- assertex(numOutputs ? idx < numOutputs : idx==0);
- CriticalBlock b(statsCrit);
- startedArray[idx] = true;
- }
- };
- class CRoxieServerInternalSinkFactory : public CRoxieServerActivityFactory
- {
- protected:
- bool isInternal;
- bool isRoot;
- unsigned usageCount;
- public:
- CRoxieServerInternalSinkFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- usageCount = _usageCount;
- isRoot = _isRoot;
- isInternal = false; // filled in by derived class constructor
- }
- virtual bool isSink() const
- {
- //only a sink if a root activity
- return isRoot && !(isInternal && dependentCount && dependentCount==usageCount); // MORE - it's possible for this to get the answer wrong still, since usageCount does not include references from main procedure. Gavin?
- }
- virtual void getEdgeProgressInfo(unsigned idx, IPropertyTree &edge) const
- {
- // 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
- // Just leave it blank is safest.
- }
- };
- typedef enum { STATEreset, STATEstarted, STATEstopped, STATEstarting } activityState;
- const char *queryStateText(activityState state)
- {
- switch (state)
- {
- case STATEreset: return "reset";
- case STATEstarted: return "started";
- case STATEstopped: return "stopped";
- case STATEstarting: return "starting";
- default: return "unknown";
- }
- }
- typedef ICopyArrayOf<IRoxieServerActivity> IRoxieServerActivityCopyArray;
- class CParallelActivityExecutor : public CAsyncFor
- {
- public:
- unsigned parentExtractSize;
- const byte * parentExtract;
- CParallelActivityExecutor(IRoxieServerActivityCopyArray & _activities, unsigned _parentExtractSize, const byte * _parentExtract) :
- activities(_activities), parentExtractSize(_parentExtractSize), parentExtract(_parentExtract) { }
- void Do(unsigned i)
- {
- activities.item(i).execute(parentExtractSize, parentExtract);
- }
- private:
- IRoxieServerActivityCopyArray & activities;
- };
- class CRoxieServerActivity : public CInterface, implements IRoxieServerActivity, implements IRoxieInput, implements IRoxieContextLogger
- {
- protected:
- IRoxieInput *input;
- IHThorArg &basehelper;
- IRoxieSlaveContext *ctx;
- const IRoxieServerActivityFactory *factory;
- IRoxieServerActivityCopyArray dependencies;
- IntArray dependencyControlIds;
- IArrayOf<IActivityGraph> childGraphs;
- CachedOutputMetaData meta;
- IHThorArg *colocalParent;
- IEngineRowAllocator *rowAllocator;
- CriticalSection statecrit;
- mutable StatsCollector stats;
- unsigned processed;
- unsigned __int64 totalCycles;
- unsigned activityId;
- activityState state;
- bool createPending;
- bool debugging;
- public:
- IMPLEMENT_IINTERFACE;
- CRoxieServerActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : factory(_factory),
- basehelper(_factory->getHelper()),
- activityId(_factory->queryId())
- {
- input = NULL;
- ctx = NULL;
- meta.set(basehelper.queryOutputMeta());
- processed = 0;
- totalCycles = 0;
- if (factory)
- factory->createChildQueries(childGraphs, this, _probeManager, *this);
- state=STATEreset;
- rowAllocator = NULL;
- debugging = _probeManager != NULL; // Don't want to collect timing stats from debug sessions
- colocalParent = NULL;
- createPending = true;
- }
-
- CRoxieServerActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, IHThorArg &_helper)
- : factory(_factory), basehelper(_helper)
- {
- input = NULL;
- ctx = NULL;
- meta.set(basehelper.queryOutputMeta());
- processed = 0;
- totalCycles = 0;
- if (factory)
- factory->createChildQueries(childGraphs, this, _probeManager, *this);
- state=STATEreset;
- rowAllocator = NULL;
- debugging = _probeManager != NULL; // Don't want to collect timing stats from debug sessions
- colocalParent = NULL;
- createPending = true;
- }
- CRoxieServerActivity(IHThorArg & _helper) : factory(NULL), basehelper(_helper)
- {
- activityId = 0;
- input = NULL;
- ctx = NULL;
- meta.set(basehelper.queryOutputMeta());
- processed = 0;
- totalCycles = 0;
- state=STATEreset;
- rowAllocator = NULL;
- debugging = false;
- colocalParent = NULL;
- createPending = true;
- }
- inline ~CRoxieServerActivity()
- {
- CriticalBlock cb(statecrit);
- if (traceStartStop)
- DBGLOG("%p destroy state=%s", this, queryStateText(state)); // Note- CTXLOG may not be safe
- if (state!=STATEreset)
- {
- DBGLOG("STATE: Activity %d destroyed but not reset", activityId);
- state = STATEreset; // bit pointless but there you go...
- }
- basehelper.Release();
- ::Release(rowAllocator);
- }
- virtual const IRoxieContextLogger &queryLogCtx()const
- {
- return *this;
- }
- inline void createRowAllocator()
- {
- if (!rowAllocator)
- rowAllocator = ctx->queryCodeContext()->getRowAllocator(meta.queryOriginal(), activityId);
- }
- // MORE - most of this is copied from ccd.hpp - can't we refactor?
- virtual void CTXLOG(const char *format, ...) const
- {
- va_list args;
- va_start(args, format);
- CTXLOGva(format, args);
- va_end(args);
- }
- virtual void CTXLOGva(const char *format, va_list args) const
- {
- StringBuffer text, prefix;
- getLogPrefix(prefix);
- text.valist_appendf(format, args);
- CTXLOGa(LOG_TRACING, prefix.str(), text.str());
- }
- virtual void CTXLOGa(TracingCategory category, const char *prefix, const char *text) const
- {
- if (ctx)
- ctx->CTXLOGa(category, prefix, text);
- else
- DBGLOG("[%s] %s", prefix, text);
- }
- virtual void logOperatorException(IException *E, const char *file, unsigned line, const char *format, ...) const
- {
- va_list args;
- va_start(args, format);
- StringBuffer prefix;
- getLogPrefix(prefix);
- CTXLOGaeva(E, file, line, prefix.str(), format, args);
- va_end(args);
- }
- virtual void logOperatorExceptionVA(IException *E, const char *file, unsigned line, const char *format, va_list args) const
- {
- StringBuffer prefix;
- getLogPrefix(prefix);
- CTXLOGaeva(E, file, line, prefix.str(), format, args);
- }
- virtual void CTXLOGae(IException *E, const char *file, unsigned line, const char *prefix, const char *format, ...) const
- {
- va_list args;
- va_start(args, format);
- CTXLOGaeva(E, file, line, prefix, format, args);
- va_end(args);
- }
- virtual void CTXLOGaeva(IException *E, const char *file, unsigned line, const char *prefix, const char *format, va_list args) const
- {
- if (ctx)
- ctx->CTXLOGaeva(E, file, line, prefix, format, args);
- else
- {
- StringBuffer ss;
- ss.appendf("[%s] ERROR", prefix);
- if (E)
- ss.append(": ").append(E->errorCode());
- if (file)
- ss.appendf(": %s(%d) ", file, line);
- if (E)
- E->errorMessage(ss.append(": "));
- if (format)
- {
- ss.append(": ").valist_appendf(format, args);
- }
- LOG(MCoperatorProgress, unknownJob, "%s", ss.str());
- }
- }
- virtual void CTXLOGl(LogItem *log) const
- {
- if (ctx)
- ctx->CTXLOGl(log);
- else
- {
- assert(ctx);
- log->Release(); // Should never happen
- }
- }
- virtual void noteStatistic(unsigned statCode, unsigned __int64 value, unsigned count) const
- {
- if (factory)
- factory->noteStatistic(statCode, value, count);
- if (ctx)
- ctx->noteStatistic(statCode, value, count);
- stats.noteStatistic(statCode, value, count);
- }
- virtual StringBuffer &getLogPrefix(StringBuffer &ret) const
- {
- if (ctx)
- ctx->getLogPrefix(ret);
- return ret.append('@').append(activityId);
- }
- virtual bool isIntercepted() const
- {
- return ctx ? ctx->isIntercepted() : false;
- }
- virtual bool isBlind() const
- {
- return ctx ? ctx->isBlind() : blindLogging;
- }
- virtual unsigned queryTraceLevel() const
- {
- if (ctx)
- return ctx->queryTraceLevel();
- else
- return traceLevel;
- }
- virtual bool isPassThrough()
- {
- return false;
- }
- virtual const IResolvedFile *resolveLFN(const char *filename, bool isOpt)
- {
- return ctx->resolveLFN(filename, isOpt);
- }
- virtual const IResolvedFile *queryVarFileInfo() const
- {
- throwUnexpected(); // should be implemented in more derived class by anyone that has a remote adaptor
- return NULL;
- }
- virtual void serializeSkipInfo(MemoryBuffer &out, unsigned seekLen, const void *rawSeek, unsigned numFields, const void * seek, const SmartStepExtra &stepExtra) const
- {
- throwUnexpected(); // should be implemented in more derived class wherever needed
- }
- virtual IRoxieSlaveContext *queryContext()
- {
- return ctx;
- }
- virtual IRoxieServerActivity *queryActivity() { return this; }
- virtual IIndexReadActivityInfo *queryIndexReadActivity() { return NULL; }
- virtual bool needsAllocator() const { return false; }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- ctx = _ctx;
- colocalParent = _colocalParent;
- createPending = true;
- if (needsAllocator())
- createRowAllocator();
- processed = 0;
- totalCycles = 0;
- if (factory)
- factory->onCreateChildQueries(_ctx, &basehelper, childGraphs);
- }
- virtual void serializeCreateStartContext(MemoryBuffer &out)
- {
- //This should only be called after onStart has been called on the helper
- assertex(!createPending);
- assertex(state==STATEstarted);
- unsigned startlen = out.length();
- basehelper.serializeCreateContext(out);
- basehelper.serializeStartContext(out);
- if (queryTraceLevel() > 10)
- CTXLOG("serializeCreateStartContext for %d added %d bytes", activityId, out.length()-startlen);
- }
- virtual void serializeExtra(MemoryBuffer &out) {}
- inline void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CriticalBlock cb(statecrit);
- if (state != STATEreset && state != STATEstarting)
- {
- CTXLOG("STATE: Expected state to be reset, but was %s, in activity %d", queryStateText(state), activityId);
- }
- state=STATEstarted;
- #ifdef TRACE_STARTSTOP
- if (traceStartStop)
- {
- CTXLOG("start %d", activityId);
- if (watchActivityId && watchActivityId==activityId)
- {
- CTXLOG("WATCH: start %d", activityId);
- }
- }
- #endif
- executeDependencies(parentExtractSize, parentExtract, 0);
- if (input)
- input->start(parentExtractSize, parentExtract, paused);
- ensureCreated();
- basehelper.onStart(parentExtract, NULL);
- if (factory)
- factory->noteStarted();
- }
- void executeDependencies(unsigned parentExtractSize, const byte *parentExtract, unsigned controlId)
- {
- //MORE: Create a filtered list and then use asyncfor
- ForEachItemIn(idx, dependencies)
- {
- if (dependencyControlIds.item(idx) == controlId)
- dependencies.item(idx).execute(parentExtractSize, parentExtract);
- }
- }
- virtual unsigned __int64 queryTotalCycles() const
- {
- return totalCycles;
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- __int64 ret = totalCycles;
- if (input) ret -= input->queryTotalCycles();
- if (ret < 0)
- ret = 0;
- return ret;
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- if (idx==0)
- return input;
- else
- return NULL;
- }
- void noteProcessed(unsigned _idx, unsigned _processed, unsigned __int64 _totalCycles, unsigned __int64 _localCycles) const
- {
- if (factory)
- {
- if (!debugging)
- factory->noteProcessed(_idx, _processed, _totalCycles, _localCycles);
- if (ctx)
- ctx->noteProcessed(*this, this, _idx, _processed, _totalCycles, _localCycles);
- }
- }
- inline void ensureCreated()
- {
- if (createPending)
- {
- createPending = false;
- basehelper.onCreate(ctx->queryCodeContext(), colocalParent, NULL);
- }
- }
- inline void stop(bool aborting)
- {
- if (state != STATEstopped)
- {
- CriticalBlock cb(statecrit);
- if (state != STATEstopped)
- {
- state=STATEstopped;
- #ifdef TRACE_STARTSTOP
- if (traceStartStop)
- {
- CTXLOG("stop %d", activityId);
- if (watchActivityId && watchActivityId==activityId)
- {
- CTXLOG("WATCH: stop %d", activityId);
- }
- }
- #endif
- if (input)
- input->stop(aborting);
- }
- }
- }
- inline void reset()
- {
- if (state != STATEreset)
- {
- CriticalBlock cb(statecrit);
- if (state != STATEreset)
- {
- if (state==STATEstarted || state==STATEstarting)
- {
- CTXLOG("STATE: activity %d reset without stop", activityId);
- stop(false);
- }
- if (ctx->queryTimeActivities())
- {
- stats.dumpStats(*this);
- StringBuffer prefix, text;
- getLogPrefix(prefix);
- text.appendf("records processed - %d", processed);
- CTXLOGa(LOG_STATISTICS, prefix.str(), text.str());
- text.clear().appendf("total time - %d us", (unsigned) (cycle_to_nanosec(totalCycles)/1000));
- CTXLOGa(LOG_STATISTICS, prefix.str(), text.str());
- text.clear().appendf("local time - %d us", (unsigned) (cycle_to_nanosec(queryLocalCycles())/1000));
- CTXLOGa(LOG_STATISTICS, prefix.str(), text.str());
- }
- state = STATEreset;
- #ifdef TRACE_STARTSTOP
- if (traceStartStop)
- {
- CTXLOG("reset %d", activityId);
- if (watchActivityId && watchActivityId==activityId)
- {
- CTXLOG("WATCH: reset %d", activityId);
- }
- }
- #endif
- ForEachItemIn(idx, dependencies)
- dependencies.item(idx).reset();
- noteProcessed(0, processed, totalCycles, queryLocalCycles());
- if (input)
- input->reset();
- processed = 0;
- totalCycles = 0;
- }
- }
- }
- virtual void addDependency(IRoxieServerActivity &source, unsigned sourceIdx, int controlId)
- {
- dependencies.append(source);
- dependencyControlIds.append(controlId);
- }
- virtual void resetEOF()
- {
- //would make more sense if the default implementation (and eof member) were in the base class
- }
- // Sink activities should override this....
- virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
- {
- throw MakeStringException(ROXIE_SINK, "Internal error: execute() requires a sink");
- }
- virtual void executeChild(size32_t & retSize, void * & ret, unsigned parentExtractSize, const byte * parentExtract)
- {
- throw MakeStringException(ROXIE_SINK, "Internal error: executeChild() requires a suitable sink");
- }
- virtual __int64 evaluate()
- {
- throw MakeStringException(ROXIE_SINK, "Internal error: evaluate() requires a function");
- }
- virtual IRoxieInput * querySelectOutput(unsigned id)
- {
- return NULL;
- }
- virtual bool querySetStreamInput(unsigned id, IRoxieInput * _input)
- {
- return false;
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- assertex(!idx);
- input = _in;
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- if (idx == (unsigned) -1)
- idx = 0;
- return idx ? NULL : this;
- }
- virtual IOutputMetaData *queryOutputMeta() const
- {
- return meta.queryOriginal();
- }
- virtual unsigned queryId() const
- {
- return activityId;
- }
- virtual unsigned querySubgraphId() const
- {
- return factory->querySubgraphId();
- }
- virtual void checkAbort()
- {
- ctx->checkAbort();
- }
- IException *makeWrappedException(IException *e)
- {
- StringBuffer msg;
- ThorActivityKind activityKind = factory ? factory->getKind() : TAKnone;
- CTXLOG("makeWrappedException - %s (in %s %d)", e->errorMessage(msg).str(), getActivityText(activityKind), activityId);
- if (QUERYINTERFACE(e, CWrappedException) || QUERYINTERFACE(e, IUserException))
- return e;
- else
- return new CWrappedException(e, activityKind, activityId);
- }
- virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract)
- {
- }
- virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract, IProbeManager *probeManager, IArrayOf<IRoxieInput> &probes)
- {
- }
- virtual void resetOutputsUsed()
- {
- }
- virtual void noteOutputUsed()
- {
- }
- virtual IRoxieServerSideCache *queryServerSideCache() const
- {
- return factory->queryServerSideCache();
- }
- virtual const IRoxieServerActivityFactory *queryFactory() const
- {
- return factory;
- }
- inline ThorActivityKind getKind() const
- {
- return factory->getKind();
- }
- inline bool isSink() const
- {
- return (factory != NULL) && factory->isSink();
- }
- };
- //=====================================================================================================
- class CRoxieServerLateStartActivity : public CRoxieServerActivity
- {
- protected:
- IRoxieInput *input; // Don't use base class input field as we want to delay starts
- bool prefiltered;
- bool eof;
- void lateStart(unsigned parentExtractSize, const byte *parentExtract, bool any)
- {
- prefiltered = !any;
- eof = prefiltered;
- if (!prefiltered)
- input->start(parentExtractSize, parentExtract, false);
- else
- {
- if (traceStartStop)
- CTXLOG("lateStart activity stopping input early as prefiltered");
- input->stop(false);
- }
- }
- public:
- CRoxieServerLateStartActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- prefiltered = false;
- eof = false;
- }
- virtual void stop(bool aborting)
- {
- if (!prefiltered)
- {
- input->stop(aborting);
- }
- else if (traceStartStop)
- CTXLOG("lateStart activity NOT stopping input late as prefiltered");
- CRoxieServerActivity::stop(aborting);
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- __int64 localCycles = totalCycles - input->queryTotalCycles();
- if (localCycles < 0)
- localCycles = 0;
- return localCycles;
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- if (idx==0)
- return input;
- else
- return NULL;
- }
- virtual void reset()
- {
- CRoxieServerActivity::reset();
- input->reset();
- prefiltered = false;
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- assertex(!idx);
- input = _in;
- }
- };
- //=====================================================================================================
- atomic_t nextInstanceId;
- extern unsigned getNextInstanceId()
- {
- return atomic_add_exchange(&nextInstanceId, 1)+1;
- }
- atomic_t nextRuid;
- ruid_t getNextRuid()
- {
- ruid_t ret = atomic_add_exchange(&nextRuid, 1)+1;
- while (ret < RUID_FIRST)
- ret = atomic_add_exchange(&nextRuid, 1)+1; // ruids 0 and 1 are reserved for pings/unwanted discarder.
- return ret;
- }
- void setStartRuid(unsigned restarts)
- {
- atomic_set(&nextRuid, restarts * 0x10000);
- atomic_set(&nextInstanceId, restarts * 10000);
- }
- enum { LimitSkipErrorCode = 0, KeyedLimitSkipErrorCode = 1 };
- class LimitSkipException : public CInterface, public IException
- {
- int code;
- public:
- LimitSkipException(int _code) { code = _code; }
- IMPLEMENT_IINTERFACE;
- virtual int errorCode() const { return code; }
- virtual StringBuffer & errorMessage(StringBuffer &msg) const { return msg.append("LimitSkipException"); }
- virtual MessageAudience errorAudience() const { return MSGAUD_internal; }
- };
- IException *makeLimitSkipException(bool isKeyed)
- {
- // We need to make sure what we throw is IException not something derived from it....
- return new LimitSkipException(isKeyed ? KeyedLimitSkipErrorCode : LimitSkipErrorCode);
- }
- //=================================================================================
- interface IRecordPullerCallback : extends IExceptionHandler
- {
- virtual void processRow(const void *row) = 0;
- virtual void processEOG() = 0;
- virtual void processGroup(const ConstPointerArray &rows) = 0;
- virtual void processDone() = 0;
- };
- class RecordPullerThread : public RestartableThread
- {
- protected:
- IRoxieInput *input;
- IRecordPullerCallback *helper;
- Semaphore started; // MORE: GH->RKC I'm pretty sure this can be deleted, since handled by RestartableThread
- bool groupAtOnce, eof, eog;
- CriticalSection crit;
- public:
- RecordPullerThread(bool _groupAtOnce)
- : RestartableThread("RecordPullerThread"), groupAtOnce(_groupAtOnce)
- {
- input = NULL;
- helper = NULL;
- eof = eog = FALSE;
- }
- inline unsigned __int64 queryTotalCycles() const
- {
- return input->queryTotalCycles();
- }
- void setInput(IRecordPullerCallback *_helper, IRoxieInput *_input)
- {
- helper = _helper;
- input = _input;
- }
- IRoxieInput *queryInput() const
- {
- return input;
- }
- void start(unsigned parentExtractSize, const byte *parentExtract, bool paused, unsigned preload, bool noThread, IRoxieSlaveContext *ctx)
- {
- eof = false;
- eog = false;
- input->start(parentExtractSize, parentExtract, paused);
- try
- {
- if (preload && !paused)
- {
- if (traceLevel > 4)
- DBGLOG("Preload fetching first %d records", preload);
- if (groupAtOnce)
- pullGroups(preload);
- else
- pullRecords(preload);
- }
- if (eof)
- {
- if (traceLevel > 4)
- DBGLOG("No need to start puller after preload");
- helper->processDone();
- }
- else
- {
- if (!noThread)
- {
- StringBuffer logPrefix("[");
- if (ctx) ctx->getLogPrefix(logPrefix);
- logPrefix.append("] ");
- RestartableThread::start(logPrefix);
- started.wait();
- }
- }
- }
- catch (IException *e)
- {
- helper->fireException(e);
- }
- catch (...)
- {
- helper->fireException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unexpected exception caught in RecordPullerThread::start"));
- }
- }
- void stop(bool aborting)
- {
- if (traceStartStop)
- DBGLOG("RecordPullerThread::stop");
- {
- CriticalBlock c(crit); // stop is called on our consumer's thread. We need to take care calling stop for our input to make sure it is not in mid-nextInGroup etc etc.
- input->stop(aborting);
- }
- RestartableThread::join();
- }
- void reset()
- {
- input->reset();
- }
- virtual int run()
- {
- started.signal();
- try
- {
- if (groupAtOnce)
- pullGroups((unsigned) -1);
- else
- pullRecords((unsigned) -1);
- helper->processDone();
- }
- catch (IException *e)
- {
- helper->fireException(e);
- }
- catch (...)
- {
- helper->fireException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unexpected exception caught in RecordPullerThread::run"));
- }
- return 0;
- }
- void done()
- {
- helper->processDone();
- }
- bool pullRecords(unsigned preload)
- {
- if (eof)
- return false;
- while (preload)
- {
- const void * row;
- {
- CriticalBlock c(crit); // See comments in stop for why this is needed
- row = input->nextInGroup();
- }
- if (row)
- {
- eog = false;
- helper->processRow(row);
- }
- else if (!eog)
- {
- helper->processEOG();
- eog = true;
- }
- else
- {
- eof = true;
- return false;
- }
- if (preload != (unsigned) -1)
- preload--;
- }
- return true;
- }
- void pullGroups(unsigned preload)
- {
- ConstPointerArray thisGroup;
- unsigned rowsDone = 0;
- while (preload && !eof)
- {
- const void *row;
- {
- CriticalBlock c(crit);
- row = input->nextInGroup();
- }
- if (row)
- {
- thisGroup.append(row);
- rowsDone++;
- }
- else if (thisGroup.length())
- {
- helper->processGroup(thisGroup);
- thisGroup.kill();
- if (preload != (unsigned) -1)
- {
- if (preload > rowsDone)
- preload -= rowsDone;
- else
- break;
- }
- rowsDone = 0;
- }
- else
- {
- eof = true;
- break;
- }
- }
- }
- };
- //=================================================================================
- #define READAHEAD_SIZE 1000
- // MORE - this code copied from ThreadedConcat code - may be able to common up some.
- class CRoxieServerReadAheadInput : public CInterface, implements IRoxieInput, implements IRecordPullerCallback
- {
- QueueOf<const void, true> buffer;
- InterruptableSemaphore ready;
- InterruptableSemaphore space;
- CriticalSection crit;
- bool eof;
- bool disabled;
- RecordPullerThread puller;
- unsigned preload;
- unsigned __int64 totalCycles;
- IRoxieSlaveContext *ctx;
- public:
- IMPLEMENT_IINTERFACE;
- CRoxieServerReadAheadInput(unsigned _preload) : puller(true), preload(_preload)
- {
- eof = false;
- disabled = false;
- totalCycles = 0;
- ctx = NULL;
- }
- void onCreate(IRoxieSlaveContext *_ctx)
- {
- ctx = _ctx;
- disabled = (ctx->queryDebugContext() != NULL);
- }
- virtual IRoxieServerActivity *queryActivity()
- {
- return puller.queryInput()->queryActivity();
- }
- virtual IIndexReadActivityInfo *queryIndexReadActivity()
- {
- return puller.queryInput()->queryIndexReadActivity();
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- totalCycles = 0;
- if (disabled)
- puller.queryInput()->start(parentExtractSize, parentExtract, paused);
- else
- {
- space.reinit(READAHEAD_SIZE);
- ready.reinit();
- puller.start(parentExtractSize, parentExtract, paused, preload, false, ctx);
- }
- }
- virtual void stop(bool aborting)
- {
- if (disabled)
- puller.queryInput()->stop(aborting);
- else
- {
- space.interrupt();
- ready.interrupt();
- puller.stop(aborting);
- }
- }
- virtual void reset()
- {
- if (disabled)
- puller.queryInput()->reset();
- else
- {
- puller.reset();
- ForEachItemIn(idx1, buffer)
- ReleaseRoxieRow(buffer.item(idx1));
- buffer.clear();
- }
- }
- virtual void resetEOF()
- {
- throwUnexpected();
- }
- virtual IOutputMetaData * queryOutputMeta() const
- {
- return puller.queryInput()->queryOutputMeta();
- }
- virtual void checkAbort()
- {
- puller.queryInput()->checkAbort();
- }
- void setInput(unsigned idx, IRoxieInput *_in)
- {
- assertex(!idx);
- puller.setInput(this, _in);
- }
- virtual unsigned __int64 queryTotalCycles() const
- {
- return totalCycles;
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- __int64 ret = totalCycles - puller.queryInput()->queryTotalCycles();
- if (ret < 0) ret = 0;
- return ret;
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- return puller.queryInput()->queryInput(idx);
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (disabled)
- return puller.queryInput()->nextInGroup();
- else
- {
- loop
- {
- {
- CriticalBlock b(crit);
- if (eof && !buffer.ordinality())
- return NULL; // eof
- }
- ready.wait();
- const void *ret;
- {
- CriticalBlock b(crit);
- ret = buffer.dequeue();
- }
- space.signal();
- return ret;
- }
- }
- }
- virtual unsigned queryId() const { throwUnexpected(); }
- virtual bool fireException(IException *e)
- {
- // called from puller thread on failure
- ready.interrupt(LINK(e));
- space.interrupt(e);
- return true;
- }
- virtual void processRow(const void *row)
- {
- {
- CriticalBlock b(crit);
- buffer.enqueue(row);
- }
- ready.signal();
- space.wait();
- }
- virtual void processGroup(const ConstPointerArray &rows)
- {
- // NOTE - a bit bizarre in that it waits for the space AFTER using it.
- // But the space semaphore is only there to stop infinite readahead. And otherwise it would deadlock
- // if group was larger than max(space)
- {
- CriticalBlock b(crit);
- ForEachItemIn(idx, rows)
- buffer.enqueue(rows.item(idx));
- buffer.enqueue(NULL);
- }
- for (unsigned i2 = 0; i2 <= rows.length(); i2++) // note - does 1 extra for the null
- {
- ready.signal();
- space.wait();
- }
- }
- virtual void processEOG()
- {
- // Used when output is not grouped - just ignore
- }
- virtual void processDone()
- {
- CriticalBlock b(crit);
- eof = true;
- ready.signal();
- }
- };
- //=================================================================================
- class CRoxieServerTwoInputActivity : public CRoxieServerActivity
- {
- protected:
- IRoxieInput *input1;
- Owned<CRoxieServerReadAheadInput> puller;
- public:
- CRoxieServerTwoInputActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- input1 = NULL;
- }
- ~CRoxieServerTwoInputActivity()
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- input1->start(parentExtractSize, parentExtract, paused);
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- if (puller)
- puller->onCreate(_ctx);
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- }
- virtual void stop(bool aborting)
- {
- input1->stop(aborting);
- CRoxieServerActivity::stop(aborting);
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- __int64 ret;
- __int64 inputCycles = input->queryTotalCycles();
- __int64 input1Cycles = input1->queryTotalCycles();
- if (puller)
- ret = totalCycles - (inputCycles > input1Cycles ? inputCycles : input1Cycles);
- else
- ret = totalCycles - (inputCycles + input1Cycles);
- if (ret < 0)
- ret = 0;
- return ret;
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- switch (idx)
- {
- case 0:
- return input;
- case 1:
- return input1;
- default:
- return NULL;
- }
- }
- virtual void reset()
- {
- CRoxieServerActivity::reset();
- if (input1)
- input1->reset();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- switch(idx)
- {
- case 0:
- input = _in;
- break;
- case 1:
- input1 = _in;
- break;
- default:
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
- }
- }
- };
- //=================================================================================
- class CRoxieServerMultiInputBaseActivity : public CRoxieServerActivity
- {
- protected:
- unsigned numInputs;
- IRoxieInput **inputArray;
- public:
- CRoxieServerMultiInputBaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerActivity(_factory, _probeManager), numInputs(_numInputs)
- {
- inputArray = new IRoxieInput*[numInputs];
- for (unsigned i = 0; i < numInputs; i++)
- inputArray[i] = NULL;
- }
- ~CRoxieServerMultiInputBaseActivity()
- {
- delete [] inputArray;
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- __int64 localCycles = totalCycles;
- for (unsigned i = 0; i < numInputs; i++)
- localCycles -= inputArray[i]->queryTotalCycles();
- if (localCycles < 0)
- localCycles = 0;
- return localCycles;
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- if (idx < numInputs)
- return inputArray[idx];
- else
- return NULL;
- }
- virtual void reset()
- {
- for (unsigned i = 0; i < numInputs; i++)
- inputArray[i]->reset();
- CRoxieServerActivity::reset();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- inputArray[idx] = _in;
- }
- };
- //=================================================================================
- class CRoxieServerMultiInputActivity : public CRoxieServerMultiInputBaseActivity
- {
- public:
- CRoxieServerMultiInputActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerMultiInputBaseActivity(_factory, _probeManager, _numInputs)
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerMultiInputBaseActivity::start(parentExtractSize, parentExtract, paused);
- for (unsigned i = 0; i < numInputs; i++)
- {
- inputArray[i]->start(parentExtractSize, parentExtract, paused);
- }
- }
- virtual void stop(bool aborting)
- {
- for (unsigned i = 0; i < numInputs; i++)
- {
- inputArray[i]->stop(aborting);
- }
- CRoxieServerMultiInputBaseActivity::stop(aborting);
- }
- };
- //=====================================================================================================
- class CRoxieServerInternalSinkActivity : public CRoxieServerActivity
- {
- protected:
- bool executed;
- CriticalSection ecrit;
- Owned<IException> exception;
- public:
- CRoxieServerInternalSinkActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- executed = false;
- }
- virtual void reset()
- {
- executed = false;
- exception.clear();
- CRoxieServerActivity::reset();
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- return NULL;
- }
- virtual const void *nextInGroup()
- {
- throwUnexpected(); // I am nobody's input
- }
- virtual void onExecute() = 0;
- virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
- {
- CriticalBlock b(ecrit);
- if (exception)
- throw exception.getLink();
- if (!executed)
- {
- try
- {
- start(parentExtractSize, parentExtract, false);
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext()); // unfortunately this is not really best place for seeing in debugger.
- onExecute();
- }
- stop(false);
- executed = true;
- }
- catch (IException *E)
- {
- exception.set(E); // (or maybe makeWrappedException?)
- stop(true);
- throw;
- }
- catch (...)
- {
- exception.set(MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught at %s:%d", __FILE__, __LINE__));
- stop(true);
- throw;
- }
- }
- }
- };
- //=================================================================================
- class CRoxieServerQueryPacket : public CInterface, implements IRoxieServerQueryPacket
- {
- protected:
- Owned<IMessageResult> result;
- Owned<IRoxieQueryPacket> packet;
- Linked<IRoxieServerQueryPacket> continuation;
- unsigned hash;
- unsigned seq;
- unsigned lastDebugSequence;
- Owned<IRoxieQueryPacket> lastDebugResponse;
- ILRUChain *prev;
- ILRUChain *next;
- bool delayed;
- public:
- IMPLEMENT_IINTERFACE;
- CRoxieServerQueryPacket(IRoxieQueryPacket *p) : packet(p)
- {
- hash = 0;
- seq = 0;
- prev = NULL;
- next = NULL;
- delayed = false;
- lastDebugSequence = 0;
- }
- virtual IRoxieQueryPacket *queryPacket() const
- {
- return packet;
- }
- virtual bool isContinuation() const
- {
- return packet && (packet->queryHeader().continueSequence & ~CONTINUE_SEQUENCE_SKIPTO) != 0;
- }
- virtual bool isDelayed() const
- {
- return delayed;
- }
- virtual bool isEnd() const
- {
- return false;
- }
- virtual bool isLimit(unsigned __int64 &_rowLimit, unsigned __int64 &_keyedLimit, unsigned __int64 &_stopAfter) const
- {
- return false;
- }
- virtual bool hasResult() const
- {
- return result != NULL;
- }
- virtual bool hasContinuation() const
- {
- return continuation != NULL;
- }
- virtual void setDelayed(bool _delayed)
- {
- delayed = _delayed;
- }
- virtual void setPacket(IRoxieQueryPacket *_packet)
- {
- packet.setown(_packet);
- }
- virtual void setSequence(unsigned _seq)
- {
- assertex(!IsShared());
- seq = _seq;
- }
- virtual unsigned getSequence() const
- {
- return seq;
- }
- IMessageResult *getResult()
- {
- return result.getLink();
- }
- IMessageResult *queryResult()
- {
- return result;
- }
- void setResult(IMessageResult *r)
- {
- result.setown(r);
- }
- IRoxieServerQueryPacket *queryContinuation()
- {
- return continuation;
- }
- void setContinuation(IRoxieServerQueryPacket *c)
- {
- continuation.setown(c);
- }
- virtual unsigned queryHash() const
- {
- return hash;
- }
- virtual void setHash(unsigned _hash)
- {
- hash = _hash;
- }
- virtual ILRUChain *queryPrev() const { return prev; }
- virtual ILRUChain *queryNext() const { return next; }
- virtual void setPrev(ILRUChain *p) { prev = p; }
- virtual void setNext(ILRUChain *n) { next = n; }
- virtual void unchain()
- {
- if (prev && next)
- {
- prev->setNext(next);
- next->setPrev(prev);
- }
- next = NULL;
- prev = NULL;
- }
- virtual IRoxieQueryPacket *getDebugResponse(unsigned sequence)
- {
- if (sequence == lastDebugSequence)
- return lastDebugResponse.getLink();
- else if (sequence > lastDebugSequence)
- {
- lastDebugResponse.clear();
- return NULL;
- }
- else
- throwUnexpected();
- }
- virtual void setDebugResponse(unsigned sequence, IRoxieQueryPacket *response)
- {
- lastDebugSequence = sequence;
- lastDebugResponse.set(response);
- }
- };
- class CRoxieServerQueryPacketEndMarker : public CRoxieServerQueryPacket
- {
- public:
- CRoxieServerQueryPacketEndMarker() : CRoxieServerQueryPacket(NULL)
- {
- }
- virtual bool isEnd() const
- {
- return true;
- }
- };
- class CRoxieServerQueryPacketLimitMarker : public CRoxieServerQueryPacket
- {
- unsigned __int64 rowLimit;
- unsigned __int64 keyedLimit;
- unsigned __int64 stopAfter;
- public:
- CRoxieServerQueryPacketLimitMarker(unsigned __int64 _rowLimit, unsigned __int64 _keyedLimit, unsigned __int64 _stopAfter) : CRoxieServerQueryPacket(NULL)
- {
- rowLimit = _rowLimit;
- keyedLimit = _keyedLimit;
- stopAfter = _stopAfter;
- }
- virtual bool isLimit(unsigned __int64 &_rowLimit, unsigned __int64 &_keyedLimit, unsigned __int64 &_stopAfter) const
- {
- _rowLimit = rowLimit;
- _keyedLimit = keyedLimit;
- _stopAfter = stopAfter;
- return true;
- }
- };
- class CRoxieServerSideCache : implements IRoxieServerSideCache, implements ILRUChain
- {
- protected:
- unsigned cacheTableSize;
- unsigned cacheTableSpace;
- IRoxieServerQueryPacket **cacheTable;
- mutable ILRUChain *prev;
- mutable ILRUChain *next;
- mutable CriticalSection crit;
- virtual ILRUChain *queryPrev() const { return prev; }
- virtual ILRUChain *queryNext() const { return next; }
- virtual void setPrev(ILRUChain *p) { prev = p; }
- virtual void setNext(ILRUChain *n) { next = n; }
- virtual void unchain()
- {
- prev->setNext(next);
- next->setPrev(prev);
- next = NULL;
- prev = NULL;
- }
- void moveToHead(IRoxieServerQueryPacket *mru)
- {
- mru->unchain();
- mru->setNext(next);
- next->setPrev(mru);
- mru->setPrev(this);
- next = mru;
- }
- IRoxieServerQueryPacket *removeLRU()
- {
- if (next==this)
- assertex(next != this);
- IRoxieServerQueryPacket *goer = (IRoxieServerQueryPacket *) next;
- goer->unchain(); // NOTE - this will modify the value of next
- return goer;
- }
- void removeEntry(IRoxieServerQueryPacket *goer)
- {
- unsigned v = goer->queryHash() % cacheTableSize;
- loop
- {
- IRoxieServerQueryPacket *found = cacheTable[v];
- assertex(found);
- if (found == goer)
- {
- cacheTable[v] = NULL;
- unsigned vn = v;
- loop
- {
- vn++;
- if (vn==cacheTableSize) vn = 0;
- IRoxieServerQueryPacket *found2 = cacheTable[vn];
- if (!found2)
- break;
- unsigned vm = found2->queryHash() % cacheTableSize;
- if (((vn+cacheTableSize-vm) % cacheTableSize)>=((vn+cacheTableSize-v) % cacheTableSize)) // diff(vn,vm)>=diff(vn,v)
- {
- cacheTable[v] = found2;
- v = vn;
- cacheTable[v] = NULL;
- }
- }
- cacheTableSpace++;
- break;
- }
- v++;
- if (v==cacheTableSize)
- v = 0;
- }
- goer->Release();
- }
- public:
- CRoxieServerSideCache(unsigned _cacheSize)
- {
- cacheTableSize = (_cacheSize*4)/3;
- cacheTable = new IRoxieServerQueryPacket *[cacheTableSize];
- memset(cacheTable, 0, cacheTableSize * sizeof(IRoxieServerQueryPacket *));
- cacheTableSpace = _cacheSize;
- prev = this;
- next = this;
- }
- ~CRoxieServerSideCache()
- {
- for (unsigned i = 0; i < cacheTableSize; i++)
- {
- ::Release(cacheTable[i]);
- }
- delete [] cacheTable;
- }
- virtual IRoxieServerQueryPacket *findCachedResult(const IRoxieContextLogger &logctx, IRoxieQueryPacket *p) const
- {
- unsigned hash = p->hash();
- unsigned et = hash % cacheTableSize;
- if (traceServerSideCache)
- {
- StringBuffer s;
- logctx.CTXLOG("CRoxieServerSideCache::findCachedResult hash %x slot %d %s", hash, et, p->queryHeader().toString(s).str());
- }
- CriticalBlock b(crit);
- loop
- {
- IRoxieServerQueryPacket *found = cacheTable[et];
- if (!found)
- return NULL;
- if (found->queryHash() == hash && found->queryPacket()->cacheMatch(p))
- {
- const_cast<CRoxieServerSideCache *>(this)->moveToHead(found);
- if (traceServerSideCache)
- logctx.CTXLOG("CRoxieServerSideCache::findCachedResult cache hit");
- logctx.noteStatistic(STATS_SERVERCACHEHIT, 1, 1);
- return NULL;
- // Because IMessageResult cannot be replayed, this echeme is flawed. I'm leaving the code here just as a stats gatherer to see how useful it would have been....
- //IRoxieServerQueryPacket *ret = new CRoxieServerQueryPacket(p);
- //ret->setResult(found->getResult());
- //return ret;
- }
- et++;
- if (et == cacheTableSize)
- et = 0;
- }
- }
- virtual void noteCachedResult(IRoxieServerQueryPacket *out, IMessageResult *in)
- {
- if (true) //!in->getLength()) // MORE - separate caches for hits and nohits
- {
- unsigned hash = out->queryPacket()->hash();
- out->setHash(hash);
- unsigned et = hash % cacheTableSize;
- if (traceServerSideCache)
- {
- StringBuffer s;
- DBGLOG("CRoxieServerSideCache::noteCachedResult hash %x slot %d %s", hash, et, out->queryPacket()->queryHeader().toString(s).str());
- }
- CriticalBlock b(crit);
- loop
- {
- IRoxieServerQueryPacket *found = cacheTable[et];
- if (!found)
- {
- if (cacheTableSpace)
- {
- out->setResult(LINK(in));
- cacheTable[et] = LINK(out);
- cacheTableSpace--;
- moveToHead(out);
- break;
- }
- else
- {
- IRoxieServerQueryPacket *goer = removeLRU();
- removeEntry(goer);
- et = hash % cacheTableSize;
- continue;
- }
- }
- else if (found->queryHash()==hash && found->queryPacket()->cacheMatch(out->queryPacket()))
- {
- moveToHead(found);
- return; // already in the cache. Because we don't cache until we have result, this can happen where
- // multiple copies of a slave query are in-flight at once.
- }
- et++;
- if (et == cacheTableSize)
- et = 0;
- }
- }
- // MORE - do we need to worry about the attachment between the MessageUnpacker and the current row manager. May all fall out ok...
- // Can I easily spot a null result? Do I want to cache null results separately? only?
- }
- // Note that this caching mechanism (unlike the old keyed-join specific one) does not common up cases where multiple
- // identical queries are in-flight at the same time. But if we can make it persistant between queries that will
- // more than make up for it
- };
- class CRowArrayMessageUnpackCursor : public CInterface, implements IMessageUnpackCursor
- {
- ConstPointerArray &data;
- Linked<IMessageResult> result;
- public:
- IMPLEMENT_IINTERFACE;
- CRowArrayMessageUnpackCursor(ConstPointerArray &_data, IMessageResult *_result)
- : data(_data), result(_result)
- {
- }
- virtual bool atEOF() const
- {
- return data.length()==0;
- }
- virtual bool isSerialized() const
- {
- return false;
- }
- virtual const void * getNext(int length)
- {
- if (!data.length())
- return NULL;
- const void *ret = data.item(0);
- data.remove(0);
- return ret;
- }
- };
- // MORE - should possibly move more over to the lazy version used in indexread?
- class CRowArrayMessageResult : public CInterface, implements IMessageResult
- {
- ConstPointerArray data;
- IRowManager &rowManager;
- bool variableSize;
- public:
- IMPLEMENT_IINTERFACE;
- CRowArrayMessageResult(IRowManager &_rowManager, bool _variableSize) : rowManager(_rowManager), variableSize(_variableSize)
- {
- }
- ~CRowArrayMessageResult()
- {
- ReleaseRoxieRowSet(data);
- }
- virtual IMessageUnpackCursor *getCursor(IRowManager *rowMgr) const
- {
- CRowArrayMessageResult *_this = (CRowArrayMessageResult *) this;
- return new CRowArrayMessageUnpackCursor(_this->data, _this);
- }
- virtual const void *getMessageHeader(unsigned &length) const
- {
- throwUnexpected(); // should never get called - I don't have a header available
- length = 0;
- return NULL;
- }
- virtual const void *getMessageMetadata(unsigned &length) const
- {
- length = 0;
- return NULL;
- }
- virtual void discard() const
- {
- throwUnexpected();
- }
- void append(const void *row)
- {
- data.append(row);
- }
- };
- void throwRemoteException(IMessageUnpackCursor *extra)
- {
- RecordLengthType *rowlen = (RecordLengthType *) extra->getNext(sizeof(RecordLengthType));
- if (rowlen)
- {
- char *xml = (char *) extra->getNext(*rowlen);
- ReleaseRoxieRow(rowlen);
- Owned<IPropertyTree> p = createPTreeFromXMLString(xml);
- ReleaseRoxieRow(xml);
- unsigned code = p->getPropInt("Code", 0);
- const char *msg = p->queryProp("Message");
- if (!msg)
- msg = xml;
- throw MakeStringException(code, "%s", msg);
- }
- throwUnexpected();
- }
- class CRemoteResultAdaptor :public CInterface, implements IRoxieInput, implements IExceptionHandler
- {
- friend class CRemoteResultMerger;
- class CRemoteResultMerger
- {
- class HeapEntry : public CInterface
- {
- private:
- CRemoteResultAdaptor &adaptor;
- IMessageUnpackCursor *cursor;
- public:
- const void *current;
- bool isLast;
- bool lastIsComplete;
- IRoxieServerQueryPacket *packet;
- unsigned seq;
- public:
- inline const void *noteResult(IMessageUnpackCursor *_cursor, bool _lastIsComplete)
- {
- cursor = _cursor;
- lastIsComplete = _lastIsComplete;
- return next();
- }
- public:
- HeapEntry(CRemoteResultAdaptor &_adaptor, IRoxieServerQueryPacket *_packet, unsigned _seq) : adaptor(_adaptor), packet(_packet), seq(_seq)
- {
- cursor = NULL;
- current = NULL;
- isLast = false;
- lastIsComplete = true;
- }
- ~HeapEntry()
- {
- ::Release(packet);
- ::Release(cursor);
- ReleaseRoxieRow(current);
- }
- bool isCompleteMatch() const
- {
- if (!isLast || lastIsComplete)
- return true;
- else
- return false;
- }
- const void *next()
- {
- if (cursor)
- {
- ReleaseClearRoxieRow(current);
- current = adaptor.getRow(cursor);
- isLast = cursor->atEOF();
- if (!current)
- {
- cursor->Release();
- cursor = NULL;
- }
- }
- return current;
- }
- unsigned skipTo(IRangeCompare *compare, const void *seek, unsigned numFields, bool requireExactMatch)
- {
- // MORE - This loop should possibly be a binchop... though it's not absolutely clear that is true (depends on term frequencies)
- unsigned skipped = 0;
- loop
- {
- int c = compare->docompare(current, seek, numFields);
- //If larger than the seek values, then we may be allowed to return an inexact match,
- //if equal then it is required to be an exact match,
- if (c > 0)
- {
- if (!requireExactMatch || isCompleteMatch())
- break;
- }
- else if ((c == 0) && isCompleteMatch())
- break;
- skipped++;
- if (!next())
- break;
- }
- return skipped;
- }
- };
- CRemoteResultAdaptor &adaptor;
- CIArrayOf<HeapEntry> heapEntries;
- UnsignedArray heap;
- IRowManager *rowManager;
- unsigned numPending;
- unsigned numFields;
- bool endSeen;
- bool remakePending;
- IRangeCompare *compare;
- bool deferredContinuation;
- inline int doCompare(unsigned l, unsigned r)
- {
- int ret = compare->docompare(heapEntries.item(l).current, heapEntries.item(r).current, numFields);
- if (!ret) ret = heapEntries.item(l).seq - heapEntries.item(r).seq;
- return ret;
- }
- void makeHeap()
- {
- /* Permute blocks to establish the heap property
- For each element p, the children are p*2+1 and p*2+2 (provided these are in range)
- The children of p must both be greater than or equal to p
- The parent of a child c is given by p = (c-1)/2
- */
- unsigned i;
- unsigned n = heap.length();
- unsigned *s = heap.getArray();
- for (i=1; i<n; i++)
- {
- unsigned r = s[i];
- int c = i; /* child */
- while (c > 0)
- {
- int p = (c-1)/2; /* parent */
- if ( doCompare( s[c], s[p] ) >= 0 )
- break;
- s[c] = s[p];
- s[p] = r;
- c = p;
- }
- }
- remakePending = false;
- }
- void remakeHeap()
- {
- /* The row associated with block[0] will have changed
- This code restores the heap property
- */
- unsigned p = 0; /* parent */
- unsigned n = heap.length();
- unsigned *s = heap.getArray();
- while (1)
- {
- unsigned c = p*2 + 1; /* child */
- if ( c >= n )
- break;
- /* Select smaller child */
- if ( c+1 < n && doCompare( s[c+1], s[c] ) < 0 ) c += 1;
- /* If child is greater or equal than parent then we are done */
- if ( doCompare( s[c], s[p] ) >= 0 )
- break;
- /* Swap parent and child */
- unsigned r = s[c];
- s[c] = s[p];
- s[p] = r;
- /* child becomes parent */
- p = c;
- }
- remakePending = false;
- }
- void append(IRoxieServerQueryPacket *p, unsigned seq)
- {
- HeapEntry &h = *new HeapEntry(adaptor, LINK(p), seq);
- IMessageResult *result = p->queryResult();
- assertex(result);
- if (h.noteResult(result->getCursor(rowManager), isCompleteMatchFlag(result)))
- {
- heapEntries.append(h);
- heap.append(heap.ordinality());
- }
- else
- h.Release();
- }
- void removeHeap(unsigned idx)
- {
- heapEntries.remove(idx);
- ForEachItemIn(i, heap)
- {
- unsigned v = heap.item(i);
- assertex(v != idx);
- if (v > idx)
- heap.replace(v-1, i);
- }
- }
- bool isCompleteMatchFlag(IMessageResult *result)
- {
- unsigned metaLen;
- const byte *metaInfo = (const byte *) result->getMessageMetadata(metaLen);
- if (metaLen)
- {
- unsigned short continuationLen = *(unsigned short *) metaInfo;
- if (continuationLen >= sizeof(bool))
- {
- metaInfo += sizeof(unsigned short);
- return *(bool *) metaInfo;
- }
- }
- return true; // if no continuation info, last row was complete.
- }
- public:
- CRemoteResultMerger(CRemoteResultAdaptor &_adaptor) : adaptor(_adaptor)
- {
- init(NULL, NULL);
- }
- void init(ISteppingMeta *meta, IRowManager *_rowManager)
- {
- if (meta)
- {
- numFields = meta->getNumFields();
- compare = meta->queryCompare();
- }
- else
- {
- numFields = 0;
- compare = NULL;
- }
- rowManager = _rowManager;
- numPending = 0;
- endSeen = false;
- remakePending = false;
- deferredContinuation = false;
- }
- void reset()
- {
- heapEntries.kill();
- heap.kill();
- numPending = 0;
- endSeen = false;
- remakePending = false;
- deferredContinuation = false;
- }
- inline bool noteEndSeen()
- {
- bool hadSeen = endSeen;
- if (!endSeen)
- makeHeap();
- endSeen = true;
- return !hadSeen;
- }
- void noteResult(IRoxieServerQueryPacket *p, unsigned seq)
- {
- if (!p->isContinuation())
- append(p, seq);
- else
- {
- ForEachItemIn(idx, heapEntries)
- {
- HeapEntry &h = heapEntries.item(idx);
- if (h.packet == p)
- {
- IMessageResult *result = p->queryResult();
- if (!h.noteResult(result->getCursor(rowManager), isCompleteMatchFlag(result)))
- {
- heap.zap(idx);
- removeHeap(idx);
- }
- numPending--;
- if (!numPending)
- makeHeap();
- return;
- }
- }
- }
- // If we get here it must be a continuation for one that I have not yet consumed... we don't need to do anything.
- return;
- }
- unsigned skipRows(unsigned &idx, const void *seek, const void *rawSeek, unsigned numFields, unsigned seekLen, const SmartStepExtra & stepExtra)
- {
- HeapEntry &entry = heapEntries.item(idx);
- unsigned skipped = entry.current ? entry.skipTo(compare, seek, numFields, !stepExtra.returnMismatches()) : 0;
- if (!entry.current)
- {
- IRoxieServerQueryPacket *continuation = entry.packet->queryContinuation();
- if (continuation)
- {
- continuation->Link();
- entry.packet->Release();
- entry.packet = continuation;
- if (continuation->hasResult())
- {
- IMessageResult *result = continuation->queryResult();
- bool lastIsCompleteMatch = isCompleteMatchFlag(result);
- entry.noteResult(result->getCursor(rowManager), lastIsCompleteMatch);
- }
- else
- {
- if (continuation->isDelayed())
- {
- continuation->setDelayed(false);
- MemoryBuffer serializedSkip;
- adaptor.activity.serializeSkipInfo(serializedSkip, seekLen, rawSeek, numFields, seek, stepExtra);
- continuation->setPacket(continuation->queryPacket()->insertSkipData(serializedSkip.length(), serializedSkip.toByteArray()));
- ROQ->sendPacket(continuation->queryPacket(), adaptor.activity.queryLogCtx());
- adaptor.sentsome.signal();
- }
- numPending++;
- }
- }
- else
- {
- heap.zap(idx);
- removeHeap(idx);
- idx--;
- }
- }
- return skipped;
- }
- const void * nextSteppedGE(const void * seek, const void *rawSeek, unsigned numFields, unsigned seeklen, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- // We discard all rows < seekval from all entries in heap
- // If this results in additional slave requests, we return NULL so that we can wait for them
- // If not, we rebuild the heap (if any were skipped) and return the first row
- deferredContinuation = false;
- if (heap.length())
- {
- unsigned skipped = 0;
- unsigned idx = 0;
- while(heapEntries.isItem(idx))
- {
- skipped += skipRows(idx, seek, rawSeek, numFields, seeklen, stepExtra);
- idx++;
- }
- if (numPending)
- return NULL; // can't answer yet, need more results from slaves
- else
- {
- if (skipped)
- makeHeap();
- return next(wasCompleteMatch, stepExtra);
- }
- }
- else
- return NULL;
- }
- bool doContinuation(HeapEntry &topEntry, bool canDefer)
- {
- IRoxieServerQueryPacket *continuation = topEntry.packet->queryContinuation();
- if (continuation)
- {
- if (continuation->isDelayed() && canDefer)
- {
- if (adaptor.activity.queryLogCtx().queryTraceLevel() > 10)
- adaptor.activity.queryLogCtx().CTXLOG("Deferring continuation");
- deferredContinuation = true;
- }
- else
- {
- deferredContinuation = false;
- continuation->Link();
- topEntry.packet->Release();
- topEntry.packet = continuation;
- if (continuation->hasResult())
- {
- IMessageResult *result = continuation->queryResult();
- bool lastIsCompleteMatch = isCompleteMatchFlag(result);
- topEntry.noteResult(result->getCursor(rowManager), lastIsCompleteMatch);
- }
- else
- {
- if (continuation->isDelayed()) // has the continuation been requested yet?
- {
- continuation->Link();
- topEntry.packet->Release();
- topEntry.packet = continuation;
- continuation->setDelayed(false);
- if (adaptor.activity.queryLogCtx().queryTraceLevel() > 10)
- adaptor.activity.queryLogCtx().CTXLOG("About to send continuation, from doContinuation");
- ROQ->sendPacket(continuation->queryPacket(), adaptor.activity.queryLogCtx());
- adaptor.sentsome.signal();
- }
- numPending++; // we are waiting for one that is already in flight
- }
- }
- return true; // next not known yet
- }
- else
- return false;
- }
- const void *next(bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- OwnedConstRoxieRow ret;
- if (heap.length())
- {
- if (deferredContinuation)
- {
- unsigned top = heap.item(0);
- HeapEntry &topEntry = heapEntries.item(top);
- doContinuation(topEntry, false);
- return NULL;
- }
- if (remakePending)
- remakeHeap();
- unsigned top = heap.item(0);
- HeapEntry &topEntry = heapEntries.item(top);
- ret.set(topEntry.current);
- wasCompleteMatch = topEntry.isCompleteMatch();
- const void *next = topEntry.next();
- if (!next)
- {
- if (!doContinuation(topEntry, stepExtra.returnMismatches()))
- {
- unsigned last = heap.pop();
- if (heap.length())
- heap.replace(last, 0);
- removeHeap(top);
- }
- }
- remakePending = true;
- }
- return ret.getClear();
- }
- bool ready()
- {
- return endSeen && numPending == 0;
- }
- };
- IRoxieServerQueryPacket *createRoxieServerQueryPacket(IRoxieQueryPacket *p, bool &cached)
- {
- if (serverSideCache && !debugContext)
- {
- IRoxieServerQueryPacket *ret = serverSideCache->findCachedResult(activity.queryLogCtx(), p);
- if (ret)
- {
- p->Release();
- cached = true;
- return ret;
- }
- }
- cached = false;
- return new CRoxieServerQueryPacket(p);
- }
- #ifdef _DEBUG
- void dumpPending()
- {
- CriticalBlock b(pendingCrit);
- ForEachItemIn(idx, pending)
- {
- IRoxieServerQueryPacket &p = pending.item(idx);
- StringBuffer s;
- unsigned __int64 dummy;
- if (p.isEnd())
- s.append("END");
- else if (p.isLimit(dummy, dummy, dummy))
- s.append("LIMIT");
- else
- {
- IRoxieQueryPacket *i = p.queryPacket();
- s.appendf("%s", p.hasResult() ? "COMPLETE " : "PENDING ");
- if (i)
- {
- RoxiePacketHeader &header = i->queryHeader();
- header.toString(s);
- }
- }
- DBGLOG("Pending %d %s", idx, s.str());
- }
- }
- #endif
- void abortPending()
- {
- CriticalBlock b(pendingCrit);
- ForEachItemIn(idx, pending)
- {
- IRoxieServerQueryPacket &p = pending.item(idx);
- if (!p.hasResult())
- {
- IRoxieQueryPacket *i = p.queryPacket();
- if (i)
- {
- RoxiePacketHeader &header = i->queryHeader();
- ROQ->sendAbort(header, activity.queryLogCtx());
- }
- }
- }
- pending.kill();
- }
- void checkDelayed()
- {
- if (ctx->queryDebugContext() && ctx->queryDebugContext()->getExecuteSequentially())
- {
- bool allDelayed = true;
- CriticalBlock b(pendingCrit);
- unsigned sendIdx = (unsigned) -1;
- ForEachItemIn(idx, pending)
- {
- IRoxieServerQueryPacket &p = pending.item(idx);
- if (p.queryPacket())
- {
- if (p.isDelayed())
- {
- if (sendIdx == (unsigned) -1)
- sendIdx = idx;
- }
- else if (!p.hasResult())
- {
- allDelayed = false;
- break;
- }
- }
- }
- if (allDelayed && sendIdx != (unsigned) -1)
- {
- if (activity.queryLogCtx().queryTraceLevel() > 10)
- activity.queryLogCtx().CTXLOG("About to send debug-deferred from next");
- pending.item(sendIdx).setDelayed(false);
- ROQ->sendPacket(pending.item(sendIdx).queryPacket(), activity.queryLogCtx());
- sentsome.signal();
- }
- }
- else if (deferredStart)
- {
- CriticalBlock b(pendingCrit);
- ForEachItemIn(idx, pending)
- {
- IRoxieServerQueryPacket &p = pending.item(idx);
- if (p.isDelayed())
- {
- if (activity.queryLogCtx().queryTraceLevel() > 10)
- activity.queryLogCtx().CTXLOG("About to send deferred start from next");
- p.setDelayed(false);
- ROQ->sendPacket(p.queryPacket(), activity.queryLogCtx());
- sentsome.signal();
- }
- }
- deferredStart = false;
- }
- }
- void retryPending()
- {
- CriticalBlock b(pendingCrit);
- checkDelayed();
- ForEachItemIn(idx, pending)
- {
- IRoxieServerQueryPacket &p = pending.item(idx);
- if (!p.hasResult() && !p.isDelayed())
- {
- IRoxieQueryPacket *i = p.queryPacket();
- if (i)
- {
- if (!i->queryHeader().retry())
- {
- StringBuffer s;
- IException *E = MakeStringException(ROXIE_MULTICAST_ERROR, "Failed to get response from slave(s) for %s in activity %d", i->queryHeader().toString(s).str(), queryId());
- activity.queryLogCtx().logOperatorException(E, __FILE__, __LINE__, "CRemoteResultAdaptor::retry");
- throw E;
- }
- if (!localSlave)
- {
- ROQ->sendPacket(i, activity.queryLogCtx());
- atomic_inc(&retriesSent);
- }
- }
- }
- }
- }
- class ChannelBuffer
- {
- protected:
- unsigned bufferLeft;
- MemoryBuffer buffer;
- char *nextBuf;
- unsigned overflowSequence;
- unsigned channel; // == bonded channel
- bool needsFlush;
- InterruptableSemaphore flowController;
- const CRemoteResultAdaptor &owner;
- CriticalSection crit;
- public:
- ChannelBuffer(const CRemoteResultAdaptor &_owner, unsigned _channel) : owner(_owner), channel(_channel), flowController(perChannelFlowLimit)
- {
- overflowSequence = 0;
- needsFlush = false;
- bufferLeft = 0;
- nextBuf = NULL;
- }
- void init(unsigned minSize)
- {
- assertex(!buffer.length());
- if (minSize < MIN_PAYLOAD_SIZE)
- minSize = MIN_PAYLOAD_SIZE;
- unsigned headerSize = sizeof(RoxiePacketHeader)+owner.headerLength();
- unsigned bufferSize = headerSize+minSize;
- if (bufferSize < mtu_size)
- bufferSize = mtu_size;
- buffer.reserveTruncate(bufferSize);
- bufferLeft = bufferSize - headerSize;
- assertex(buffer.toByteArray());
- nextBuf = (char *) buffer.toByteArray() + headerSize;
- needsFlush = false;
- }
- inline IRoxieQueryPacket *flush()
- {
- CriticalBlock cb(crit);
- Owned<IRoxieQueryPacket> ret;
- if (needsFlush)
- {
- buffer.setLength(nextBuf - buffer.toByteArray());
- RoxiePacketHeader *h = (RoxiePacketHeader *) buffer.toByteArray();
- h->init(owner.remoteId, owner.ruid, channel, overflowSequence);
- //patch logPrefix, cachedContext and parent extract into the place reserved in the message buffer
- byte * tgt = (byte*)(h+1);
- owner.copyHeader(tgt, channel);
- ret.setown(createRoxiePacket(buffer));
- if (overflowSequence == OVERFLOWSEQUENCE_MAX)
- overflowSequence = 1; // don't wrap to 0 - that is a bit special
- else
- overflowSequence++;
- needsFlush = false;
- bufferLeft = 0;
- if (owner.flowControlled)
- {
- CriticalUnblock cub(crit);
- while (!flowController.wait(1000))
- {
- StringBuffer s;
- owner.activity.queryLogCtx().CTXLOG("Channel %d blocked by flow control: %s", channel, h->toString(s).str());
- }
- }
- }
- return ret.getClear();
- }
- inline void signal()
- {
- if (owner.flowControlled)
- flowController.signal();
- }
- inline void interrupt(IException *e)
- {
- flowController.interrupt(e);
- }
- inline void *getBuffer(unsigned size)
- {
- CriticalBlock cb(crit);
- if (bufferLeft >= size)
- {
- needsFlush = true;
- void * ret = nextBuf;
- nextBuf += size;
- bufferLeft -= size;
- return ret;
- }
- else if (!needsFlush)
- {
- init(size);
- return getBuffer(size);
- }
- else if (owner.mergeOrder)
- {
- return buffer.reserve(size); // whole query needs to go as single packet if we are to merge
- }
- else
- return NULL; // will force it to flush and start a new packet
- }
- };
- private:
- friend class CRemoteResultMerger;
- bool allread;
- bool contextCached;
- bool preserveOrder;
- InterruptableSemaphore sentsome;
- Owned <IMessageCollator> mc;
- Owned<IMessageUnpackCursor> mu;
- Owned<IMessageResult> mr;
- ChannelBuffer **buffers;
- IHThorArg &helper;
- unsigned __int64 stopAfter;
- unsigned resendSequence;
- IHThorArg *colocalArg;
- IArrayOf<IRoxieServerQueryPacket> pending;
- CriticalSection pendingCrit;
- IRoxieServerSideCache *serverSideCache;
- unsigned sentSequence;
- Owned<IOutputRowDeserializer> deserializer;
- Owned<IEngineRowAllocator> rowAllocator;
- CRemoteResultMerger merger;
- // this is only used to avoid recreating a bufferStream for each row. A better solution may be needed
- MemoryBuffer tempRowBuffer;
- Owned<ISerialStream> bufferStream;
- CThorStreamDeserializerSource rowSource;
- protected:
- IRowManager *rowManager;
- IRoxieInput *owner;
- unsigned __int64 rowLimit;
- unsigned __int64 keyedLimit;
- IRoxieServerErrorHandler *errorHandler;
- CachedOutputMetaData meta;
- public:
- ISteppingMeta *mergeOrder;
- IRoxieSlaveContext *ctx;
- IDebuggableContext *debugContext;
- IRoxieServerActivity &activity;
- unsigned parentExtractSize;
- const byte * parentExtract;
- bool flowControlled;
- bool deferredStart;
- MemoryBuffer logInfo;
- MemoryBuffer cachedContext;
- const RemoteActivityId &remoteId;
- ruid_t ruid;
- mutable CriticalSection buffersCrit;
- unsigned processed;
- unsigned __int64 totalCycles;
- //private: //vc6 doesn't like this being private yet accessed by nested class...
- const void *getRow(IMessageUnpackCursor *mu)
- {
- if (!mu->isSerialized() || (meta.isFixedSize() && !deserializer))
- return mu->getNext(meta.getFixedSize());
- else
- {
- RecordLengthType *rowlen = (RecordLengthType *) mu->getNext(sizeof(RecordLengthType));
- if (rowlen)
- {
- RecordLengthType len = *rowlen;
- ReleaseRoxieRow(rowlen);
- const void *slaveRec = mu->getNext(len);
- if (deserializer && mu->isSerialized())
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- tempRowBuffer.setBuffer(len, const_cast<void *>(slaveRec), false);
- size_t outsize = deserializer->deserialize(rowBuilder, rowSource);
- ReleaseRoxieRow(slaveRec);
- return rowBuilder.finalizeRowClear(outsize);
- }
- else
- return slaveRec;
- }
- else
- return NULL;
- }
- }
- private:
- ChannelBuffer *queryChannelBuffer(unsigned channel, bool force=false)
- {
- CriticalBlock cb(buffersCrit);
- ChannelBuffer *b = buffers[channel];
- if (!b && force)
- {
- if (!contextCached)
- {
- logInfo.clear();
- unsigned char loggingFlags = LOGGING_FLAGSPRESENT | LOGGING_TRACELEVELSET;
- unsigned char ctxTraceLevel = activity.queryLogCtx().queryTraceLevel() + 1; // Avoid passing a 0
- if (activity.queryLogCtx().isIntercepted())
- loggingFlags |= LOGGING_INTERCEPTED;
- if (ctx->queryTimeActivities())
- loggingFlags |= LOGGING_TIMEACTIVITIES;
- if (activity.queryLogCtx().isBlind())
- loggingFlags |= LOGGING_BLIND;
- if (debugContext)
- {
- loggingFlags |= LOGGING_DEBUGGERACTIVE;
- logInfo.append(loggingFlags).append(ctxTraceLevel);
- MemoryBuffer bpInfo;
- debugContext->serialize(bpInfo);
- bpInfo.append((__uint64)(memsize_t) &activity);
- logInfo.append((unsigned short) bpInfo.length());
- logInfo.append(bpInfo.length(), bpInfo.toByteArray());
- }
- else
- logInfo.append(loggingFlags).append(ctxTraceLevel);
- StringBuffer logPrefix;
- activity.queryLogCtx().getLogPrefix(logPrefix);
- logInfo.append(logPrefix);
- activity.serializeCreateStartContext(cachedContext.clear());
- activity.serializeExtra(cachedContext);
- if (activity.queryVarFileInfo())
- activity.queryVarFileInfo()->queryTimeStamp().serialize(cachedContext);
- contextCached = true;
- }
- b = buffers[channel] = new ChannelBuffer(*this, channel);
- }
- return b;
- }
- void processRow(const void *got)
- {
- processed++;
- if (processed > rowLimit)
- {
- ReleaseRoxieRow(got);
- errorHandler->onLimitExceeded(false); // NOTE - should throw exception
- throwUnexpected();
- }
- else if (processed > keyedLimit)
- {
- ReleaseRoxieRow(got);
- errorHandler->onLimitExceeded(true); // NOTE - should throw exception
- throwUnexpected();
- }
- }
- public:
- IMPLEMENT_IINTERFACE;
- CRemoteResultAdaptor(const RemoteActivityId &_remoteId, IOutputMetaData *_meta, IHThorArg &_helper, IRoxieServerActivity &_activity, bool _preserveOrder, bool _flowControlled)
- : remoteId(_remoteId), meta(_meta), activity(_activity), helper(_helper), preserveOrder(_preserveOrder), flowControlled(_flowControlled), merger(*this)
- {
- rowLimit = (unsigned __int64) -1;
- keyedLimit = (unsigned __int64) -1;
- contextCached = false;
- stopAfter = I64C(0x7FFFFFFFFFFFFFFF);
- buffers = new ChannelBuffer*[numChannels+1];
- memset(buffers, 0, (numChannels+1)*sizeof(ChannelBuffer *));
- parentExtractSize = 0;
- parentExtract = NULL;
- owner = NULL;
- mergeOrder = NULL;
- deferredStart = false;
- processed = 0;
- totalCycles = 0;
- sentSequence = 0;
- serverSideCache = activity.queryServerSideCache();
- bufferStream.setown(createMemoryBufferSerialStream(tempRowBuffer));
- rowSource.setStream(bufferStream);
- }
- ~CRemoteResultAdaptor()
- {
- if (mc)
- {
- ROQ->queryReceiveManager()->detachCollator(mc);
- mc.clear();
- }
- for (unsigned channel = 0; channel <= numChannels; channel++)
- {
- delete(buffers[channel]);
- }
- delete [] buffers;
- }
- void setMeta(IOutputMetaData *newmeta)
- {
- meta.set(newmeta);
- }
- virtual IRoxieServerActivity *queryActivity()
- {
- return &activity;
- }
- virtual IIndexReadActivityInfo *queryIndexReadActivity()
- {
- return NULL;
- }
- void setMergeInfo(ISteppingMeta *_mergeOrder)
- {
- mergeOrder = _mergeOrder;
- deferredStart = true;
- }
- void send(IRoxieQueryPacket *p)
- {
- if (p)
- {
- Linked<IRoxieQueryPacket> saver(p); // avoids a race with abortPending, without keeping pendingCrit locked over the send which we might prefer not to
- assertex(p->queryHeader().uid==ruid);
- // MORE: Maybe we should base the fastlane flag on some other
- // criteria !! (i.e A Roxie server prediction based on the
- // activity type/activity behaviour/expected reply size .. etc).
- //
- // Currently (code below) based on high priority, seq=0, and none-child activity.
- // But this could still cause too many reply packets on the fatlane
- // (higher priority output Q), which may cause the activities on the
- // low priority output Q to not get service on time.
- if ((colocalArg == 0) && // not a child query activity??
- (p->queryHeader().activityId & (ROXIE_SLA_PRIORITY | ROXIE_HIGH_PRIORITY)) &&
- (p->queryHeader().overflowSequence == 0) &&
- (p->queryHeader().continueSequence & ~CONTINUE_SEQUENCE_SKIPTO)==0)
- p->queryHeader().retries |= ROXIE_FASTLANE;
- if (p->queryHeader().channel)
- {
- bool cached = false;
- IRoxieServerQueryPacket *rsqp = createRoxieServerQueryPacket(p, cached);
- if (deferredStart)
- rsqp->setDelayed(true);
- rsqp->setSequence(sentSequence++);
- {
- CriticalBlock b(pendingCrit);
- pending.append(*rsqp);
- }
- if (!deferredStart)
- {
- if (!cached)
- ROQ->sendPacket(p, activity.queryLogCtx());
- sentsome.signal();
- }
- }
- else
- {
- // 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
- // Need to add first, then send
- unsigned i;
- bool allCached = true;
- for (i = 1; i <= numActiveChannels; i++)
- {
- IRoxieQueryPacket *q = p->clonePacket(i);
- bool thisChannelCached;
- IRoxieServerQueryPacket *rsqp = createRoxieServerQueryPacket(q, thisChannelCached);
- if (!thisChannelCached)
- allCached = false;
- rsqp->setSequence(sentSequence++);
- if (deferredStart)
- {
- rsqp->setDelayed(true);
- }
- {
- CriticalBlock b(pendingCrit);
- pending.append(*rsqp);
- }
- if (!deferredStart)
- sentsome.signal();
- }
- if (!allCached && !deferredStart)
- ROQ->sendPacket(p, activity.queryLogCtx());
- buffers[0]->signal(); // since replies won't come back on that channel...
- p->Release();
- }
- }
- }
- void *getMem(unsigned partNo, unsigned fileNo, unsigned size)
- {
- unsigned channel = partNo ? getBondedChannel(partNo) : 0;
- size += sizeof(PartNoType);
- ChannelBuffer *b = queryChannelBuffer(channel, true);
- char *buffer = (char *) b->getBuffer(size);
- if (!buffer)
- {
- send(b->flush());
- buffer = (char *) b->getBuffer(size);
- }
- PartNoType sp;
- sp.partNo = partNo;
- sp.fileNo = fileNo;
- memcpy(buffer, &sp, sizeof(sp));
- buffer += sizeof(sp);
- return buffer;
- }
- void injectResult(IMessageResult *result)
- {
- IRoxieServerQueryPacket *f = new CRoxieServerQueryPacket(NULL);
- f->setSequence(sentSequence++);
- f->setResult(result);
- CriticalBlock b(pendingCrit);
- pending.append(*f);
- sentsome.signal(); // MORE - arguably should only send if there is any point waking up the listener thread, to save context swicth
- }
- void flush()
- {
- for (unsigned channel = 0; channel <= numChannels; channel++)
- {
- ChannelBuffer *b = queryChannelBuffer(channel, false);
- if (b)
- send(b->flush());
- }
- }
- void interruptBuffers(IException *e)
- {
- for (unsigned channel = 0; channel <= numChannels; channel++)
- {
- ChannelBuffer *b = queryChannelBuffer(channel, false);
- if (b)
- b->interrupt(LINK(e));
- }
- }
- void senddone()
- {
- CriticalBlock b(pendingCrit);
- pending.append(*new CRoxieServerQueryPacketEndMarker);
- sentsome.signal();
- }
- bool fireException(IException *e)
- {
- {
- CriticalBlock b(pendingCrit);
- pending.append(*new CRoxieServerQueryPacketEndMarker);
- }
- interruptBuffers(e);
- if (mc)
- mc->interrupt(LINK(e));
- sentsome.interrupt(e);
- return true;
- }
- virtual void onCreate(IRoxieInput *_owner, IRoxieServerErrorHandler *_errorHandler, IRoxieSlaveContext *_ctx, IHThorArg *_colocalArg)
- {
- owner = _owner;
- errorHandler = _errorHandler;
- ctx = _ctx;
- debugContext = ctx->queryDebugContext();
- colocalArg = _colocalArg;
- if (meta.needsSerialize())
- {
- deserializer.setown(meta.createRowDeserializer(_ctx->queryCodeContext(), activity.queryId()));
- rowAllocator.setown(ctx->queryCodeContext()->getRowAllocator(meta.queryOriginal(), activity.queryId()));
- }
- if (ctx->queryDebugContext() && ctx->queryDebugContext()->getExecuteSequentially())
- deferredStart = true;
- }
- virtual unsigned queryId() const
- {
- return owner->queryId();
- }
- virtual void onStart(unsigned _parentExtractSize, const byte * _parentExtract)
- {
- #ifdef TRACE_STARTSTOP
- if (traceStartStop)
- activity.queryLogCtx().CTXLOG("RRAonstart");
- #endif
- sentsome.reinit();
- ruid = getNextRuid();
- rowManager = &ctx->queryRowManager();
- if (mergeOrder)
- merger.init(mergeOrder, rowManager);
- if (mc)
- {
- ROQ->queryReceiveManager()->detachCollator(mc); // Should never happen - implies someone forgot to call onReset!
- }
- mc.setown(ROQ->queryReceiveManager()->createMessageCollator(rowManager, ruid));
- allread = false;
- mu.clear();
- contextCached = false;
- processed = 0;
- totalCycles = 0;
- resendSequence = 0;
- sentSequence = 0;
- for (unsigned channel = 0; channel <= numChannels; channel++)
- {
- delete(buffers[channel]);
- buffers[channel] = NULL;
- }
- flush();
- parentExtractSize = _parentExtractSize;
- parentExtract = _parentExtract;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- #ifdef TRACE_STARTSTOP
- if (traceStartStop)
- activity.queryLogCtx().CTXLOG("RRAstart");
- #endif
- owner->start(parentExtractSize, parentExtract, paused);
- totalCycles = 0;
- }
- void checkAbort()
- {
- owner->checkAbort();
- }
- void setLimits(unsigned __int64 _rowLimit, unsigned __int64 _keyedLimit, unsigned __int64 _stopAfter)
- {
- if (ctx->queryProbeManager())
- {
- if (_rowLimit != (unsigned __int64) -1) ctx->queryProbeManager()->setNodePropertyInt(&activity, "rowLimit", _rowLimit);
- if (_keyedLimit != (unsigned __int64) -1) ctx->queryProbeManager()->setNodePropertyInt(&activity, "keyedLimit", _keyedLimit);
- if (_stopAfter != I64C(0x7FFFFFFFFFFFFFFF)) ctx->queryProbeManager()->setNodePropertyInt(&activity, "choosenLimit", _stopAfter);
- }
- {
- CriticalBlock b(pendingCrit);
- if (pending.length())
- {
- #ifdef _DEBUG
- dumpPending(); // MORE - only defined in debug build - could have put the ifdef inside the dumpPending method
- #endif
- assertex(pending.length()==0);
- }
- pending.append(*new CRoxieServerQueryPacketLimitMarker(_rowLimit, _keyedLimit, _stopAfter));
- }
- sentsome.signal();
- rowLimit = _rowLimit;
- keyedLimit = _keyedLimit;
- stopAfter = _stopAfter;
- }
- virtual void stop(bool aborting)
- {
- #ifdef TRACE_STARTSTOP
- if (traceStartStop)
- activity.queryLogCtx().CTXLOG("RRAstop");
- #endif
- onStop(aborting);
- owner->stop(aborting);
- }
- void onStop(bool aborting)
- {
- #ifdef TRACE_STARTSTOP
- if (traceStartStop)
- activity.queryLogCtx().CTXLOG("RRAonstop");
- #endif
- abortPending();
- interruptBuffers(NULL);
- sentsome.interrupt();
- if (mc) // May not be set if start() chain threw exception
- mc->interrupt();
- }
- virtual void reset()
- {
- #ifdef TRACE_STARTSTOP
- if (traceStartStop)
- activity.queryLogCtx().CTXLOG("RRAreset");
- #endif
- owner->reset();
- onReset();
- }
- virtual void resetEOF()
- {
- throwUnexpected();
- }
- virtual void onReset()
- {
- #ifdef TRACE_STARTSTOP
- if (traceStartStop)
- activity.queryLogCtx().CTXLOG("RRAonreset");
- #endif
- if (mc)
- ROQ->queryReceiveManager()->detachCollator(mc);
- merger.reset();
- pending.kill();
- if (mc && ctx)
- ctx->addSlavesReplyLen(mc->queryBytesReceived());
- mc.clear(); // Or we won't free memory for graphs that get recreated
- mu.clear(); //ditto
- mergeOrder = NULL; // MORE - is that needed?
- deferredStart = false;
- }
- virtual IOutputMetaData * queryOutputMeta() const
- {
- return helper.queryOutputMeta();
- }
- virtual unsigned __int64 queryTotalCycles() const
- {
- return totalCycles;
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- return owner->queryLocalCycles();
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- return owner->queryInput(idx);
- }
- const void * nextSteppedGE(const void *seek, const void *rawSeek, unsigned numFields, unsigned seekLen, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- if (activity.queryLogCtx().queryTraceLevel() > 10)
- {
- StringBuffer recstr;
- unsigned i;
- for (i = 0; i < seekLen; i++)
- {
- recstr.appendf("%02x ", ((unsigned char *) rawSeek)[i]);
- }
- activity.queryLogCtx().CTXLOG("CRemoteResultAdaptor::nextSteppedGE(rawSeek=%s numFields=%d, seeklen=%d, returnMismatches=%d)", recstr.str(), numFields, seekLen, stepExtra.returnMismatches());
- }
- assertex(mergeOrder);
- if (deferredStart)
- {
- CriticalBlock b(pendingCrit);
- ForEachItemIn(idx, pending)
- {
- IRoxieServerQueryPacket &p = pending.item(idx);
- if (p.isDelayed())
- {
- p.setDelayed(false);
- if (activity.queryLogCtx().queryTraceLevel() > 10)
- activity.queryLogCtx().CTXLOG("About to send deferred start from nextSteppedGE, setting requireExact to %d", !stepExtra.returnMismatches());
- MemoryBuffer serializedSkip;
- activity.serializeSkipInfo(serializedSkip, seekLen, rawSeek, numFields, seek, stepExtra);
- p.setPacket(p.queryPacket()->insertSkipData(serializedSkip.length(), serializedSkip.toByteArray()));
- ROQ->sendPacket(p.queryPacket(), activity.queryLogCtx());
- sentsome.signal();
- }
- }
- deferredStart = false;
- }
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (processed==stopAfter)
- return NULL;
- if (allread)
- return NULL;
- loop
- {
- if (merger.ready())
- {
- const void *got = merger.nextSteppedGE(seek, rawSeek, numFields, seekLen, wasCompleteMatch, stepExtra);
- if (got)
- {
- processRow(got);
- return got;
- }
- }
- if (!reload()) // MORE - should pass the seek info here...
- return NULL;
- }
- }
- virtual const void *nextInGroup()
- {
- // If we are merging then we need to do a heapsort on all
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (activity.queryLogCtx().queryTraceLevel() > 10)
- {
- activity.queryLogCtx().CTXLOG("CRemoteResultAdaptor::nextInGroup()");
- }
- loop
- {
- checkDelayed();
- if (processed==stopAfter)
- return NULL;
- if (allread)
- return NULL;
- // If we can still consume from the merger or the most recently retrieved mu, do so.
- const void *got = NULL;
- if (mergeOrder && merger.ready())
- {
- bool matched = true;
- got = merger.next(matched, dummySmartStepExtra);
- }
- else if (mu)
- got = getRow(mu);
- if (got)
- {
- processRow(got);
- return got;
- }
- if (!reload())
- return NULL;
- }
- }
- bool reload()
- {
- // Wait for something to be returned from a slave....
- mu.clear();
- sentsome.wait();
- // must be at least an endMarker on the queue since sentsome was signalled
- {
- CriticalBlock b(pendingCrit);
- IRoxieServerQueryPacket &top = pending.item(0);
- if (top.isLimit(rowLimit, keyedLimit, stopAfter)) // This is really a start marker...
- {
- pending.remove(0);
- return true;
- }
- else if (top.isEnd())
- {
- pending.remove(0);
- allread = true;
- if (activity.queryLogCtx().queryTraceLevel() > 5)
- activity.queryLogCtx().CTXLOG("All read on ruid %x", ruid);
- return false;
- }
- else if (mergeOrder)
- {
- unsigned idx = 0;
- bool added = false;
- while (pending.isItem(idx))
- {
- IRoxieServerQueryPacket &item = pending.item(idx);
- if (item.isEnd())
- {
- if (merger.noteEndSeen())
- {
- sentsome.signal(); // Because we waited, yet didn't actually consume anything
- added = true;
- }
- break;
- }
- else if (item.hasResult())
- {
- merger.noteResult(&item, item.getSequence());
- pending.remove(idx);
- added = true;
- }
- else if (item.isContinuation())
- idx++;
- else
- break;
- }
- if (added)
- return true;
- }
- else if (top.hasResult())
- {
- mr.setown(pending.item(0).getResult());
- mu.setown(mr->getCursor(rowManager));
- pending.remove(0);
- return true;
- }
- }
- getNextUnpacker();
- return true;
- }
- void getNextUnpacker()
- {
- mu.clear();
- unsigned ctxTraceLevel = activity.queryLogCtx().queryTraceLevel();
- loop
- {
- checkDelayed();
- unsigned timeout = remoteId.isSLAPriority() ? slaTimeout : (remoteId.isHighPriority() ? highTimeout : lowTimeout);
- owner->checkAbort();
- bool anyActivity;
- if (ctxTraceLevel > 5)
- activity.queryLogCtx().CTXLOG("Calling getNextUnpacker(%d)", timeout);
- mr.setown(mc->getNextResult(timeout, anyActivity));
- if (ctxTraceLevel > 6)
- activity.queryLogCtx().CTXLOG("Called getNextUnpacker(%d), activity=%d", timeout, anyActivity);
- owner->checkAbort();
- if (mr)
- {
- unsigned roxieHeaderLen;
- const RoxiePacketHeader &header = *(const RoxiePacketHeader *) mr->getMessageHeader(roxieHeaderLen);
- #ifdef _DEBUG
- assertex(roxieHeaderLen == sizeof(RoxiePacketHeader));
- #endif
- if (ctxTraceLevel > 5)
- {
- StringBuffer s;
- activity.queryLogCtx().CTXLOG("getNextUnpacker got packet %s", header.toString(s).str());
- }
- CriticalBlock b(pendingCrit);
- unsigned idx = 0;
- IRoxieServerQueryPacket *original = NULL;
- IRoxieQueryPacket *op;
- while (pending.isItem(idx))
- {
- original = &pending.item(idx);
- op = original->queryPacket();
- if (op && header.matchPacket(op->queryHeader()))
- break;
- original = NULL;
- idx++;
- }
- if (!original || original->hasResult())
- {
- switch (header.activityId)
- {
- case ROXIE_FILECALLBACK:
- {
- // tell slave to abort
- //if (ctxTraceLevel > 5)
- {
- StringBuffer s;
- activity.queryLogCtx().CTXLOG("Redundant callback on query %s", header.toString(s).str());
- }
- Owned<IMessageUnpackCursor> callbackData = mr->getCursor(rowManager);
- OwnedConstRoxieRow len = callbackData->getNext(sizeof(RecordLengthType));
- if (len)
- {
- RecordLengthType *rowlen = (RecordLengthType *) len.get();
- OwnedConstRoxieRow row = callbackData->getNext(*rowlen);
- const char *rowdata = (const char *) row.get();
- // bool isOpt = * (bool *) rowdata;
- // bool isLocal = * (bool *) (rowdata+1);
- ROQ->sendAbortCallback(header, rowdata+2, activity.queryLogCtx());
- }
- else
- throwUnexpected();
- break;
- }
- // MORE - ROXIE_ALIVE perhaps should go here too
- case ROXIE_TRACEINFO:
- {
- Owned<IMessageUnpackCursor> extra = mr->getCursor(rowManager);
- loop
- {
- RecordLengthType *rowlen = (RecordLengthType *) extra->getNext(sizeof(RecordLengthType));
- if (rowlen)
- {
- char *logInfo = (char *) extra->getNext(*rowlen);
- MemoryBuffer buf;
- buf.setBuffer(*rowlen, logInfo, false);
- activity.queryLogCtx().CTXLOGl(new LogItem(buf));
- ReleaseRoxieRow(rowlen);
- ReleaseRoxieRow(logInfo);
- }
- else
- break;
- }
- break;
- }
- default:
- if (ctxTraceLevel > 3)
- activity.queryLogCtx().CTXLOG("Discarding packet %p - original %p is NULL or has result already", mr.get(), original);
- mr->discard();
- break;
- }
- mr.clear();
- }
- else
- {
- atomic_inc(&resultsReceived);
- switch (header.activityId)
- {
- case ROXIE_DEBUGCALLBACK:
- {
- Owned<IMessageUnpackCursor> callbackData = mr->getCursor(rowManager);
- OwnedConstRoxieRow len = callbackData->getNext(sizeof(RecordLengthType));
- if (len)
- {
- RecordLengthType *rowlen = (RecordLengthType *) len.get();
- OwnedConstRoxieRow row = callbackData->getNext(*rowlen);
- char *rowdata = (char *) row.get();
- //if (ctxTraceLevel > 5)
- {
- StringBuffer s;
- activity.queryLogCtx().CTXLOG("Callback on query %s for debug", header.toString(s).str());
- }
- MemoryBuffer slaveInfo;
- slaveInfo.setBuffer(*rowlen, rowdata, false);
- unsigned debugSequence;
- slaveInfo.read(debugSequence);
- Owned<IRoxieQueryPacket> reply = original->getDebugResponse(debugSequence);
- if (!reply)
- reply.setown(activity.queryContext()->queryDebugContext()->onDebugCallback(header, *rowlen, rowdata));
- if (reply)
- {
- original->setDebugResponse(debugSequence, reply);
- ROQ->sendPacket(reply, activity.queryLogCtx());
- }
- }
- else
- throwUnexpected();
- // MORE - somehow we need to make sure slave gets a reply even if I'm not waiting (in udp layer)
- // Leave original message on pending queue in original location - this is not a reply to it.
- break;
- }
- case ROXIE_FILECALLBACK:
- {
- // we need to send back to the slave a message containing the file info requested.
- Owned<IMessageUnpackCursor> callbackData = mr->getCursor(rowManager);
- OwnedConstRoxieRow len = callbackData->getNext(sizeof(RecordLengthType));
- if (len)
- {
- RecordLengthType *rowlen = (RecordLengthType *) len.get();
- OwnedConstRoxieRow row = callbackData->getNext(*rowlen);
- const char *rowdata = (const char *) row.get();
- bool isOpt = * (bool *) rowdata;
- bool isLocal = * (bool *) (rowdata+1);
- const char *lfn = rowdata+2;
- //if (ctxTraceLevel > 5)
- {
- StringBuffer s;
- activity.queryLogCtx().CTXLOG("Callback on query %s file %s", header.toString(s).str(),(const char *) lfn);
- }
- activity.queryContext()->onFileCallback(header, lfn, isOpt, isLocal);
- }
- else
- throwUnexpected();
- // MORE - somehow we need to make sure slave gets a reply even if I'm not waiting (in udp layer)
- // Leave original message on pending queue in original location - this is not a reply to it.
- break;
- }
- case ROXIE_KEYEDLIMIT_EXCEEDED:
- activity.queryLogCtx().CTXLOG("ROXIE_KEYEDLIMIT_EXCEEDED");
- errorHandler->onLimitExceeded(true); // NOTE - should throw exception!
- throwUnexpected();
- case ROXIE_LIMIT_EXCEEDED:
- activity.queryLogCtx().CTXLOG("ROXIE_LIMIT_EXCEEDED");
- errorHandler->onLimitExceeded(false); // NOTE - should throw exception!
- throwUnexpected();
- case ROXIE_TRACEINFO:
- {
- Owned<IMessageUnpackCursor> extra = mr->getCursor(rowManager);
- loop
- {
- RecordLengthType *rowlen = (RecordLengthType *) extra->getNext(sizeof(RecordLengthType));
- if (rowlen)
- {
- char *logInfo = (char *) extra->getNext(*rowlen);
- MemoryBuffer buf;
- buf.setBuffer(*rowlen, logInfo, false);
- activity.queryLogCtx().CTXLOGl(new LogItem(buf));
- ReleaseRoxieRow(rowlen);
- ReleaseRoxieRow(logInfo);
- }
- else
- break;
- }
- break;
- }
- case ROXIE_EXCEPTION:
- if (ctxTraceLevel > 1)
- {
- StringBuffer s;
- activity.queryLogCtx().CTXLOG("Exception on query %s", header.toString(s).str());
- }
- op->queryHeader().noteException(header.retries);
- if (op->queryHeader().allChannelsFailed())
- {
- activity.queryLogCtx().CTXLOG("Multiple exceptions on query - aborting");
- Owned<IMessageUnpackCursor> exceptionData = mr->getCursor(rowManager);
- throwRemoteException(exceptionData);
- }
- // Leave it on pending queue in original location
- break;
- case ROXIE_ALIVE:
- if (ctxTraceLevel > 4)
- {
- StringBuffer s;
- activity.queryLogCtx().CTXLOG("ROXIE_ALIVE: %s", header.toString(s).str());
- }
- op->queryHeader().noteAlive(header.retries & ROXIE_RETRIES_MASK);
- // Leave it on pending queue in original location
- break;
- default:
- if (header.retries & ROXIE_RETRIES_MASK)
- atomic_inc(&retriesNeeded);
- unsigned metaLen;
- const void *metaData = mr->getMessageMetadata(metaLen);
- if (metaLen)
- {
- // We got back first chunk but there is more.
- // resend the packet, with the cursor info provided.
- // MORE - if smart-stepping, we don't want to send the continuation immediately. Other cases it's not clear that we do.
- if (ctxTraceLevel > 1)
- {
- StringBuffer s;
- activity.queryLogCtx().CTXLOG("Additional data size %d on query %s mergeOrder %p", metaLen, header.toString(s).str(), mergeOrder);
- }
- if (*((unsigned short *) metaData) + sizeof(unsigned short) != metaLen)
- {
- StringBuffer s;
- activity.queryLogCtx().CTXLOG("Additional data size %d on query %s mergeOrder %p", metaLen, header.toString(s).str(), mergeOrder);
- activity.queryLogCtx().CTXLOG("Additional data is corrupt");
- throwUnexpected();
- }
- MemoryBuffer nextQuery;
- nextQuery.append(sizeof(RoxiePacketHeader), &header);
- nextQuery.append(metaLen, metaData);
- nextQuery.append(op->getTraceLength(), op->queryTraceInfo());
- nextQuery.append(op->getContextLength(), op->queryContextData());
- if (resendSequence == CONTINUESEQUENCE_MAX)
- {
- activity.queryLogCtx().CTXLOG("ERROR: Continuation sequence wrapped"); // shouldn't actually matter.... but suggests a very iffy query!
- resendSequence = 1;
- }
- else
- resendSequence++;
- RoxiePacketHeader *newHeader = (RoxiePacketHeader *) nextQuery.toByteArray();
- newHeader->continueSequence = resendSequence; // NOTE - we clear the skipTo flag since continuation of a skip is NOT a skip...
- newHeader->retries &= ~ROXIE_RETRIES_MASK;
- IRoxieQueryPacket *resend = createRoxiePacket(nextQuery);
- CRoxieServerQueryPacket *fqp = new CRoxieServerQueryPacket(resend);
- fqp->setSequence(original->getSequence());
- pending.add(*fqp, idx+1); // note that pending takes ownership. sendPacket does not release.
- original->setContinuation(LINK(fqp));
- if (mergeOrder)
- fqp->setDelayed(true);
- else
- {
- ROQ->sendPacket(resend, activity.queryLogCtx());
- sentsome.signal();
- }
- // Note that we don't attempt to cache results that have continuation records - too tricky !
- }
- else
- {
- if (serverSideCache)
- serverSideCache->noteCachedResult(original, mr);
- }
- unsigned channel = header.channel;
- {
- ChannelBuffer *b = queryChannelBuffer(channel); // If not something is wrong, or we sent out on channel 0?
- if (b)
- b->signal();
- }
- original->setResult(mr.getClear());
- sentsome.signal();
- return;
- }
- }
- }
- else
- {
- if (!anyActivity)
- {
- activity.queryLogCtx().CTXLOG("Input has stalled - retry required?");
- retryPending();
- }
- }
- }
- }
- inline unsigned headerLength() const
- {
- return logInfo.length() + cachedContext.length() + sizeof(unsigned) + parentExtractSize;
- }
- void copyHeader(byte *tgt, unsigned channel) const
- {
- unsigned len = logInfo.length();
- memcpy(tgt, logInfo.toByteArray(), len);
- tgt += len;
- *(unsigned *) tgt = parentExtractSize;
- tgt += sizeof(unsigned);
- memcpy(tgt, parentExtract, parentExtractSize);
- tgt += parentExtractSize;
- memcpy(tgt, cachedContext.toByteArray(), cachedContext.length());
- tgt += cachedContext.length();
- }
- };
- class CSkippableRemoteResultAdaptor : public CRemoteResultAdaptor
- {
- Owned <IException> exception;
- bool skipping;
- ConstPointerArray buff;
- unsigned index;
- bool pulled;
- void pullInput()
- {
- try
- {
- if (exception)
- throw exception.getClear();
- unsigned __int64 count = 0;
- loop
- {
- const void * next = CRemoteResultAdaptor::nextInGroup();
- if (next == NULL)
- {
- next = CRemoteResultAdaptor::nextInGroup();
- if(next == NULL)
- break;
- buff.append(NULL);
- }
- count++;
- if (count > rowLimit)
- {
- ReleaseRoxieRow(next);
- ReleaseRoxieRowSet(buff);
- errorHandler->onLimitExceeded(false); // throws an exception - user or LimitSkipException
- throwUnexpected();
- }
- else if (count > keyedLimit)
- {
- ReleaseRoxieRow(next);
- ReleaseRoxieRowSet(buff);
- errorHandler->onLimitExceeded(true); // throws an exception - user or LimitSkipException
- throwUnexpected();
- }
- buff.append(next);
- }
- }
- catch (IException *E)
- {
- if (QUERYINTERFACE(E, LimitSkipException))
- {
- Owned<IException> cleanup = E;
- ReleaseRoxieRowSet(buff);
- const void *onfail = errorHandler->createLimitFailRow(E->errorCode() == KeyedLimitSkipErrorCode);
- if (onfail)
- buff.append(onfail);
- }
- else
- throw;
- }
- pulled = true;
- }
- public:
- CSkippableRemoteResultAdaptor(const RemoteActivityId &_remoteId, IOutputMetaData *_meta, IHThorArg &_helper, IRoxieServerActivity &_activity, bool _preserveOrder, bool _flowControlled, bool _skipping) :
- CRemoteResultAdaptor(_remoteId, _meta, _helper, _activity, _preserveOrder, _flowControlled)
- {
- skipping = _skipping;
- index = 0;
- pulled = false;
- }
- void setException(IException *E)
- {
- exception.setown(E);
- }
- virtual void onReset()
- {
- while (buff.isItem(index))
- ReleaseRoxieRow(buff.item(index++));
- buff.kill();
- pulled = false;
- exception.clear();
- CRemoteResultAdaptor::onReset();
- }
- void onStart(unsigned _parentExtractSize, const byte * _parentExtract)
- {
- index = 0;
- pulled = false;
- CRemoteResultAdaptor::onStart(_parentExtractSize, _parentExtract);
- }
- virtual const void * nextSteppedGE(const void *seek, const void *rawSeek, unsigned numFields, unsigned seeklen, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- // 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
- if (exception)
- throw exception.getClear();
- return CRemoteResultAdaptor::nextSteppedGE(seek, rawSeek, numFields, seeklen, wasCompleteMatch, stepExtra);
- }
- virtual const void *nextInGroup()
- {
- if (skipping)
- {
- if(!pulled)
- pullInput();
- if(buff.isItem(index))
- {
- const void * next = buff.item(index++);
- if(next)
- processed++;
- return next;
- }
- return NULL;
- }
- else
- {
- if (exception)
- throw exception.getClear();
- return CRemoteResultAdaptor::nextInGroup();
- }
- }
- };
- //=================================================================================
- class CRoxieServerApplyActivity : public CRoxieServerInternalSinkActivity
- {
- IHThorApplyArg &helper;
- public:
- CRoxieServerApplyActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorApplyArg &) basehelper)
- {
- }
- virtual void onExecute()
- {
- helper.start();
- loop
- {
- const void * next = input->nextInGroup();
- if (!next)
- {
- next = input->nextInGroup();
- if (!next)
- break;
- }
- helper.apply(next);
- ReleaseRoxieRow(next);
- }
- helper.end();
- }
- };
- class CRoxieServerApplyActivityFactory : public CRoxieServerActivityFactory
- {
- bool isRoot;
- public:
- CRoxieServerApplyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerApplyActivity(this, _probeManager);
- }
- virtual bool isSink() const
- {
- return isRoot;
- }
- };
- IRoxieServerActivityFactory *createRoxieServerApplyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- {
- return new CRoxieServerApplyActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
- }
- //=================================================================================
- class CRoxieServerNullActivity : public CRoxieServerActivity
- {
- public:
- CRoxieServerNullActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- }
- virtual const void *nextInGroup()
- {
- return NULL;
- }
- };
- IRoxieServerActivity * createRoxieServerNullActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- {
- return new CRoxieServerNullActivity(_factory, _probeManager);
- }
- class CRoxieServerNullActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerNullActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerNullActivity(this, _probeManager);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for null activity");
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNullActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerNullActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerPassThroughActivity : public CRoxieServerActivity
- {
- public:
- CRoxieServerPassThroughActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- }
- virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
- {
- return input->gatherConjunctions(collector);
- }
- virtual void resetEOF()
- {
- input->resetEOF();
- }
- virtual const void *nextInGroup()
- {
- const void * next = input->nextInGroup();
- if (next)
- processed++;
- return next;
- }
- virtual bool isPassThrough()
- {
- return true;
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void * next = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- if (next)
- processed++;
- return next;
- }
- IInputSteppingMeta * querySteppingMeta()
- {
- return input->querySteppingMeta();
- }
- };
- IRoxieServerActivity * createRoxieServerPassThroughActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- {
- return new CRoxieServerPassThroughActivity(_factory, _probeManager);
- }
- //=================================================================================
- class CRoxieServerChildBaseActivity : public CRoxieServerActivity
- {
- protected:
- bool eof;
- bool first;
- public:
- CRoxieServerChildBaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- eof = false;
- first = true;
- }
- ~CRoxieServerChildBaseActivity()
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- first = true;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- };
- class CRoxieServerChildBaseActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerChildBaseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for %s activity", getActivityText(kind));
- }
- };
- //=================================================================================
- class CRoxieServerChildIteratorActivity : public CRoxieServerChildBaseActivity
- {
- IHThorChildIteratorArg &helper;
- public:
- CRoxieServerChildIteratorActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerChildBaseActivity(_factory, _probeManager), helper((IHThorChildIteratorArg &) basehelper)
- {
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- bool ok;
- if (first)
- {
- ok = helper.first();
- first = false;
- }
- else
- ok = helper.next();
- try
- {
- while (ok)
- {
- processed++;
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned outSize = helper.transform(rowBuilder);
- if (outSize)
- return rowBuilder.finalizeRowClear(outSize);
- ok = helper.next();
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- eof = true;
- return NULL;
- }
- };
- class CRoxieServerChildIteratorActivityFactory : public CRoxieServerChildBaseActivityFactory
- {
- public:
- CRoxieServerChildIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerChildBaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerChildIteratorActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerChildIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerChildIteratorActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerChildNormalizeActivity : public CRoxieServerChildBaseActivity
- {
- IHThorChildNormalizeArg &helper;
- public:
- CRoxieServerChildNormalizeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerChildBaseActivity(_factory, _probeManager), helper((IHThorChildNormalizeArg &) basehelper)
- {
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- bool ok;
- if (first)
- {
- ok = helper.first();
- first = false;
- }
- else
- ok = helper.next();
- if (ok)
- {
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- do {
- unsigned outSize = helper.transform(rowBuilder);
- if (outSize)
- {
- processed++;
- return rowBuilder.finalizeRowClear(outSize);
- }
- ok = helper.next();
- }
- while (ok);
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- eof = true;
- return NULL;
- }
- };
- class CRoxieServerChildNormalizeActivityFactory : public CRoxieServerChildBaseActivityFactory
- {
- public:
- CRoxieServerChildNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerChildBaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerChildNormalizeActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNewChildNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerChildNormalizeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerChildAggregateActivity : public CRoxieServerChildBaseActivity
- {
- IHThorChildAggregateArg &helper;
- public:
- CRoxieServerChildAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerChildBaseActivity(_factory, _probeManager), helper((IHThorChildAggregateArg &) basehelper)
- {
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- eof = true;
- processed++;
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- helper.clearAggregate(rowBuilder);
- helper.processRows(rowBuilder);
- size32_t finalSize = meta.getRecordSize(rowBuilder.getSelf());
- return rowBuilder.finalizeRowClear(finalSize);
- }
- };
- class CRoxieServerChildAggregateActivityFactory : public CRoxieServerChildBaseActivityFactory
- {
- public:
- CRoxieServerChildAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerChildBaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerChildAggregateActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNewChildAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerChildAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerChildGroupAggregateActivity : public CRoxieServerChildBaseActivity, public IHThorGroupAggregateCallback
- {
- IHThorChildGroupAggregateArg &helper;
- RowAggregator aggregated;
- public:
- IMPLEMENT_IINTERFACE
- CRoxieServerChildGroupAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerChildBaseActivity(_factory, _probeManager), helper((IHThorChildGroupAggregateArg &) basehelper),
- aggregated(helper, helper)
- {
- }
- void processRow(const void * next)
- {
- aggregated.addRow(next);
- }
-
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerChildBaseActivity::start(parentExtractSize, parentExtract, paused);
- aggregated.start(rowAllocator);
- }
- virtual void reset()
- {
- aggregated.reset();
- CRoxieServerChildBaseActivity::reset();
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- if (first)
- {
- helper.processRows(this);
- first = false;
- }
- Owned<AggregateRowBuilder> next = aggregated.nextResult();
- if (next)
- {
- processed++;
- return next->finalizeRowClear();
- }
- eof = true;
- return NULL;
- }
- };
- class CRoxieServerChildGroupAggregateActivityFactory : public CRoxieServerChildBaseActivityFactory
- {
- public:
- CRoxieServerChildGroupAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerChildBaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerChildGroupAggregateActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNewChildGroupAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerChildGroupAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerChildThroughNormalizeActivity : public CRoxieServerChildBaseActivity
- {
- IHThorChildThroughNormalizeArg &helper;
- const void * lastInput;
- unsigned numProcessedLastGroup;
- bool ok;
- public:
- CRoxieServerChildThroughNormalizeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerChildBaseActivity(_factory, _probeManager), helper((IHThorChildThroughNormalizeArg &) basehelper)
- {
- lastInput = NULL;
- numProcessedLastGroup = 0;
- ok = false;
- }
- virtual void stop(bool aborting)
- {
- CRoxieServerChildBaseActivity::stop(aborting);
- ReleaseRoxieRow(lastInput);
- lastInput = NULL;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerChildBaseActivity::start(parentExtractSize, parentExtract, paused);
- numProcessedLastGroup = processed;
- ok = false;
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- loop
- {
- if (ok)
- ok = helper.next();
- while (!ok)
- {
- ReleaseRoxieRow(lastInput);
- lastInput = input->nextInGroup();
- if (!lastInput)
- {
- if (numProcessedLastGroup != processed)
- {
- numProcessedLastGroup = processed;
- return NULL;
- }
- lastInput = input->nextInGroup();
- if (!lastInput)
- return NULL;
- }
- ok = helper.first(lastInput);
- }
- try
- {
- do
- {
- unsigned outSize = helper.transform(rowBuilder);
- if (outSize)
- {
- processed++;
- return rowBuilder.finalizeRowClear(outSize);
- }
- ok = helper.next();
- } while (ok);
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- }
- };
- class CRoxieServerChildThroughNormalizeActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerChildThroughNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerChildThroughNormalizeActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNewChildThroughNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerChildThroughNormalizeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerRawIteratorActivity : public CRoxieServerActivity
- {
- IHThorRawIteratorArg &helper;
- bool eogPending;
- bool eof;
- Owned<IOutputRowDeserializer> rowDeserializer;
- MemoryBuffer tempRowBuffer;
- Owned<ISerialStream> bufferStream;
- CThorStreamDeserializerSource rowSource;
- public:
- CRoxieServerRawIteratorActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorRawIteratorArg &) basehelper)
- {
- bufferStream.setown(createMemoryBufferSerialStream(tempRowBuffer));
- rowSource.setStream(bufferStream);
- eogPending = false;
- eof = false;
- }
- virtual bool needsAllocator() const { return true; }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- rowDeserializer.setown(rowAllocator->createRowDeserializer(ctx->queryCodeContext()));
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eogPending = false;
- eof = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- size32_t lenData = 0;
- const void * data = NULL;
- helper.queryDataset(lenData, data);
- tempRowBuffer.setBuffer(lenData, const_cast<void *>(data), false);
- }
- virtual void reset()
- {
- tempRowBuffer.resetBuffer();
- CRoxieServerActivity::reset();
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- if (rowSource.eos())
- {
- eof = true;
- return NULL;
- }
- if (eogPending)
- {
- eogPending = false;
- return NULL;
- }
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t size = rowDeserializer->deserialize(rowBuilder, rowSource);
- if (meta.isGrouped())
- rowSource.read(sizeof(bool), &eogPending);
- processed++;
- return rowBuilder.finalizeRowClear(size);
- }
- };
- class CRoxieServerRawIteratorActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerRawIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerRawIteratorActivity(this, _probeManager);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for %s activity", getActivityText(kind));
- }
- };
- IRoxieServerActivityFactory *createRoxieServerRawIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerRawIteratorActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerLinkedRawIteratorActivity : public CRoxieServerActivity
- {
- IHThorLinkedRawIteratorArg &helper;
- public:
- CRoxieServerLinkedRawIteratorActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorLinkedRawIteratorArg &) basehelper)
- {
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void *ret =helper.next();
- if (ret)
- {
- LinkRoxieRow(ret);
- processed++;
- }
- return ret;
- }
- };
- class CRoxieServerLinkedRawIteratorActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerLinkedRawIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerLinkedRawIteratorActivity(this, _probeManager);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for %s activity", getActivityText(kind));
- }
- };
- IRoxieServerActivityFactory *createRoxieServerLinkedRawIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerLinkedRawIteratorActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerDatasetResultActivity : public CRoxieServerActivity
- {
- public:
- CRoxieServerDatasetResultActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- }
- virtual const void *nextInGroup()
- {
- throwUnexpected();
- }
- virtual void executeChild(size32_t & retSize, void * & ret, unsigned parentExtractSize, const byte * parentExtract)
- {
- try
- {
- start(parentExtractSize, parentExtract, false);
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- MemoryBuffer result;
- IRecordSize * inputMeta = input->queryOutputMeta();
- loop
- {
- const void *nextrec = input->nextInGroup();
- if (!nextrec)
- {
- nextrec = input->nextInGroup();
- if (!nextrec)
- break;
- }
- result.append(inputMeta->getRecordSize(nextrec), nextrec);
- ReleaseRoxieRow(nextrec);
- }
- retSize = result.length();
- ret = result.detach();
- }
- stop(false);
- reset();
- }
- catch(IException *E)
- {
- ctx->notifyAbort(E);
- stop(true);
- reset();
- throw;
- }
- catch(...)
- {
- Owned<IException> E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught at %s:%d", __FILE__, __LINE__);
- ctx->notifyAbort(E);
- stop(true);
- reset();
- throw;
- }
- }
- };
- class CRoxieServerDatasetResultActivityFactory : public CRoxieServerActivityFactory
- {
- bool isRoot;
- public:
- CRoxieServerDatasetResultActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerDatasetResultActivity(this, _probeManager);
- }
- virtual bool isSink() const
- {
- return isRoot;
- }
- };
- IRoxieServerActivityFactory *createRoxieServerDatasetResultActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- {
- return new CRoxieServerDatasetResultActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
- }
- //=================================================================================
- /*
- * Deprecated in 3.8, this class is being kept for backward compatibility,
- * since now the code generator is using InlineTables (below) for all
- * temporary tables and rows.
- */
- class CRoxieServerTempTableActivity : public CRoxieServerActivity
- {
- IHThorTempTableArg &helper;
- unsigned curRow;
- unsigned numRows;
- public:
- CRoxieServerTempTableActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorTempTableArg &) basehelper)
- {
- curRow = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- curRow = 0;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- numRows = helper.numRows();
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- // Filtering empty rows, returns the next valid row
- while (curRow < numRows)
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned outSize = helper.getRow(rowBuilder, curRow++);
- if (outSize)
- {
- processed++;
- return rowBuilder.finalizeRowClear(outSize);
- }
- }
- return NULL;
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
- }
- };
- class CRoxieServerTempTableActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerTempTableActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerTempTableActivity(this, _probeManager);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for TempTable activity");
- }
- };
- IRoxieServerActivityFactory *createRoxieServerTempTableActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerTempTableActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerInlineTableActivity : public CRoxieServerActivity
- {
- IHThorInlineTableArg &helper;
- __uint64 curRow;
- __uint64 numRows;
- public:
- CRoxieServerInlineTableActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorInlineTableArg &) basehelper)
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- curRow = 0;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- numRows = helper.numRows();
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- // Filtering empty rows, returns the next valid row
- while (curRow < numRows)
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned outSize = helper.getRow(rowBuilder, curRow++);
- if (outSize)
- {
- processed++;
- return rowBuilder.finalizeRowClear(outSize);
- }
- }
- return NULL;
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
- }
- };
- class CRoxieServerInlineTableActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerInlineTableActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerInlineTableActivity(this, _probeManager);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for InlineTable activity");
- }
- };
- IRoxieServerActivityFactory *createRoxieServerInlineTableActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerInlineTableActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerWorkUnitReadActivity : public CRoxieServerActivity
- {
- IHThorWorkunitReadArg &helper;
- Owned<IWorkUnitRowReader> wuReader; // MORE - can we use IRoxieInput instead?
- IRoxieServerContext *serverContext;
- public:
- CRoxieServerWorkUnitReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorWorkunitReadArg &)basehelper)
- {
- serverContext = NULL;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- serverContext = ctx->queryServerContext();
- if (!serverContext)
- {
- throw MakeStringException(ROXIE_INTERNAL_ERROR, "Workunit read activity cannot be executed in slave context");
- }
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- IXmlToRowTransformer * xmlTransformer = helper.queryXmlTransformer();
- wuReader.setown(serverContext->getWorkunitRowReader(helper.queryName(), helper.querySequence(), xmlTransformer, rowAllocator, meta.isGrouped()));
- // MORE _ should that be in onCreate?
- }
- virtual void reset()
- {
- wuReader.clear();
- CRoxieServerActivity::reset();
- };
- virtual bool needsAllocator() const { return true; }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void *ret = wuReader->nextInGroup();
- if (ret)
- processed++;
- return ret;
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
- }
- };
- class CRoxieServerWorkUnitReadActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerWorkUnitReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerWorkUnitReadActivity(this, _probeManager);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for WorkUnitRead activity");
- }
- };
- IRoxieServerActivityFactory *createRoxieServerWorkUnitReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerWorkUnitReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- interface ILocalGraphEx : public ILocalGraph
- {
- public:
- virtual void setResult(unsigned id, IGraphResult * result) = 0;
- virtual IRoxieInput * createResultIterator(unsigned id) = 0;
- virtual void setGraphLoopResult(IGraphResult * result) = 0;
- virtual IRoxieInput * createGraphLoopResultIterator(unsigned id) = 0;
- };
- class CSafeRoxieInput : public CInterface, implements IRoxieInput
- {
- public:
- CSafeRoxieInput(IRoxieInput * _input) : input(_input) {}
- IMPLEMENT_IINTERFACE
- virtual IOutputMetaData * queryOutputMeta() const
- {
- return input->queryOutputMeta();
- }
- virtual unsigned queryId() const
- {
- return input->queryId();
- }
- virtual unsigned __int64 queryTotalCycles() const
- {
- return input->queryTotalCycles();
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- return input->queryLocalCycles();
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- return input->queryInput(idx);
- }
- virtual IRoxieServerActivity *queryActivity()
- {
- return input->queryActivity();
- }
- virtual IIndexReadActivityInfo *queryIndexReadActivity()
- {
- return input->queryIndexReadActivity();
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CriticalBlock procedure(cs);
- input->start(parentExtractSize, parentExtract, paused);
- }
- virtual void stop(bool aborting)
- {
- CriticalBlock procedure(cs);
- input->stop(aborting);
- }
- virtual void reset()
- {
- CriticalBlock procedure(cs);
- input->reset();
- }
- virtual void resetEOF()
- {
- CriticalBlock procedure(cs);
- input->resetEOF();
- }
- virtual void checkAbort()
- {
- CriticalBlock procedure(cs);
- input->checkAbort();
- }
- virtual const void *nextInGroup()
- {
- CriticalBlock procedure(cs);
- return input->nextInGroup();
- }
- virtual bool nextGroup(ConstPointerArray & group)
- {
- CriticalBlock procedure(cs);
- return input->nextGroup(group);
- }
- private:
- CriticalSection cs;
- Linked<IRoxieInput> input;
- };
- //=================================================================================
- class CPseudoRoxieInput : public CInterface, implements IRoxieInput
- {
- protected:
- unsigned __int64 totalCycles;
- public:
- IMPLEMENT_IINTERFACE;
-
- CPseudoRoxieInput()
- {
- totalCycles = 0;
- }
- virtual unsigned __int64 queryTotalCycles() const
- {
- return totalCycles;
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- return totalCycles;
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- return NULL;
- }
- virtual IRoxieServerActivity *queryActivity()
- {
- throwUnexpected();
- }
- virtual IIndexReadActivityInfo *queryIndexReadActivity()
- {
- throwUnexpected();
- }
- virtual IOutputMetaData * queryOutputMeta() const { throwUnexpected(); }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused) { }
- virtual void stop(bool aborting) { }
- virtual void reset() { totalCycles = 0; }
- virtual void checkAbort() { }
- virtual unsigned queryId() const { throwUnexpected(); }
- virtual void resetEOF() { }
- };
- class CIndirectRoxieInput : public CPseudoRoxieInput
- {
- public:
- CIndirectRoxieInput(IRoxieInput * _input = NULL) : input(_input)
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- input->start(parentExtractSize, parentExtract, paused);
- }
- virtual void stop(bool aborting)
- {
- input->stop(aborting);
- }
- virtual void reset()
- {
- input->reset();
- totalCycles = 0;
- }
- virtual void checkAbort()
- {
- input->checkAbort();
- }
- virtual const void * nextInGroup()
- {
- return input->nextInGroup();
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- return input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- __int64 ret = totalCycles - input->queryTotalCycles();
- if (ret < 0)
- ret = 0;
- return ret;
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- return input->queryInput(idx);
- }
- virtual unsigned queryId() const
- {
- return input->queryId();
- }
- virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
- {
- return input->gatherConjunctions(collector);
- }
- virtual void resetEOF()
- {
- input->resetEOF();
- }
- virtual unsigned numConcreteOutputs() const
- {
- return input->numConcreteOutputs();
- }
-
- virtual IRoxieInput * queryConcreteInput(unsigned idx)
- {
- return input->queryConcreteInput(idx);
- }
- virtual IOutputMetaData * queryOutputMeta() const
- {
- return input->queryOutputMeta();
- }
- virtual IRoxieServerActivity *queryActivity()
- {
- return input->queryActivity();
- }
- void setInput(IRoxieInput * _input)
- {
- input = _input;
- }
- protected:
- IRoxieInput * input;
- };
- class CExtractMapperInput : public CIndirectRoxieInput
- {
- unsigned savedParentExtractSize;
- const byte * savedParentExtract;
- public:
- CExtractMapperInput(IRoxieInput * _input = NULL) : CIndirectRoxieInput(_input)
- {
- savedParentExtractSize = 0;
- savedParentExtract = NULL;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- input->start(savedParentExtractSize, savedParentExtract, paused);
- }
- void setParentExtract(unsigned _savedParentExtractSize, const byte * _savedParentExtract)
- {
- savedParentExtractSize = _savedParentExtractSize;
- savedParentExtract = _savedParentExtract;
- }
- };
- class CGraphResult : public CInterface, implements IGraphResult
- {
- CriticalSection cs;
- CachedOutputMetaData meta;
- ConstPointerArray data;
- Owned<IEngineRowAllocator> rowsetAllocator;
- bool complete;
- void clear()
- {
- CriticalBlock func(cs);
- ReleaseRoxieRowSet(data);
- complete = false;
- }
- public:
- CGraphResult()
- {
- complete = false; // dummy result is not supposed to be used...
- }
- CGraphResult(IEngineRowAllocator * _ownedRowsetAllocator, ConstPointerArray & input, bool own) : rowsetAllocator(_ownedRowsetAllocator)
- {
- meta.set(rowsetAllocator->queryOutputMeta());
- ForEachItemIn(i, input)
- {
- const void * cur = input.item(i);
- if (!own && cur)
- LinkRoxieRow(cur);
- data.append(cur);
- }
- if (own)
- input.kill();
- complete = true;
- }
- CGraphResult(IEngineRowAllocator * _ownedRowsetAllocator, void * row, bool own) : rowsetAllocator(_ownedRowsetAllocator)
- {
- meta.set(rowsetAllocator->queryOutputMeta());
- data.append(row);
- if (!own)
- LinkRoxieRow(row);
- complete = true;
- }
- ~CGraphResult() { clear(); }
- IMPLEMENT_IINTERFACE
- // interface IGraphResult
- virtual IRoxieInput * createIterator()
- {
- if (!complete)
- throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Internal Error: Reading uninitialised graph result");
- return new CGraphResultIterator(this);
- }
- virtual void getResult(unsigned & lenResult, void * & result)
- {
- if (!complete)
- throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Internal Error: Reading uninitialised graph result");
- bool grouped = meta.isGrouped();
- MemoryBuffer rowdata;
- unsigned max = data.ordinality();
- unsigned i;
- for (i = 0; i < max; i++)
- {
- const void * nextrec = data.item(i);
- size32_t thisSize = meta.getRecordSize(nextrec);
- rowdata.append(thisSize, nextrec);
- if (grouped)
- {
- bool eog = false;
- if (data.isItem(i+1))
- {
- if (!data.item(i+1))
- {
- eog = true;
- i++;
- }
- }
- else
- eog = true;
- rowdata.append(eog);
- }
- }
- lenResult = rowdata.length();
- result = rowdata.detach();
- }
- virtual void getLinkedResult(unsigned & countResult, byte * * & result)
- {
- if (!complete)
- throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Internal Error: Reading uninitialised graph result");
- byte * * rowset = rowsetAllocator->createRowset(data.ordinality());
- unsigned max = data.ordinality();
- unsigned i;
- for (i = 0; i < max; i++)
- {
- const void * next = data.item(i);
- if (next) LinkRoxieRow(next);
- rowset[i] = (byte *)next;
- }
- countResult = max;
- result = rowset;
- }
- //other
- const void * getRow(unsigned i)
- {
- CriticalBlock func(cs);
- if (!data.isItem(i))
- return NULL;
- const void * ret = data.item(i);
- if (ret) LinkRoxieRow(ret);
- return ret;
- }
- protected:
- class CGraphResultIterator : public CPseudoRoxieInput
- {
- unsigned i;
- Linked<CGraphResult> result;
- public:
- CGraphResultIterator(CGraphResult * _result) : result(_result) { i = 0; }
- IMPLEMENT_IINTERFACE
- public:
- virtual const void * nextInGroup()
- {
- return result->getRow(i++);
- }
- };
- };
- //=================================================================================
- class CRoxieServerLocalResultReadActivity : public CRoxieServerActivity
- {
- IHThorLocalResultReadArg &helper;
- Owned<IRoxieInput> iter;
- ILocalGraphEx * graph;
- unsigned graphId;
- unsigned sequence;
- public:
- CRoxieServerLocalResultReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorLocalResultReadArg &)basehelper), graphId(_graphId)
- {
- graph = NULL;
- sequence = 0;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- graph = static_cast<ILocalGraphEx *>(_ctx->queryCodeContext()->resolveLocalQuery(graphId));
- sequence = helper.querySequence();
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- iter.setown(graph->createResultIterator(sequence));
- }
- virtual void reset()
- {
- iter.clear();
- CRoxieServerActivity::reset();
- };
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void * next = iter->nextInGroup();
- if (next)
- {
- processed++;
- atomic_inc(&rowsIn);
- }
- return next;
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
- }
- };
- class CRoxieServerLocalResultReadActivityFactory : public CRoxieServerActivityFactory
- {
- unsigned graphId;
- public:
- CRoxieServerLocalResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _graphId)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), graphId(_graphId)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerLocalResultReadActivity(this, _probeManager, graphId);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for LocalResultRead activity");
- }
- };
- IRoxieServerActivityFactory *createRoxieServerLocalResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned graphId)
- {
- return new CRoxieServerLocalResultReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, graphId);
- }
- //=================================================================================
- class CRoxieServerLocalResultStreamReadActivity : public CRoxieServerActivity
- {
- IHThorLocalResultReadArg &helper;
- Owned<IRoxieInput> streamInput;
- unsigned sequence;
- public:
- CRoxieServerLocalResultStreamReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorLocalResultReadArg &)basehelper)
- {
- sequence = 0;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- sequence = helper.querySequence();
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- assertex(streamInput != NULL);
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- streamInput->start(parentExtractSize, parentExtract, paused);
- }
- virtual void stop(bool aborting)
- {
- CRoxieServerActivity::stop(aborting);
- streamInput->stop(aborting);
- }
- virtual void reset()
- {
- streamInput->reset();
- CRoxieServerActivity::reset();
- };
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void * next = streamInput->nextInGroup();
- if (next)
- {
- processed++;
- atomic_inc(&rowsIn);
- }
- return next;
- }
- virtual bool querySetStreamInput(unsigned id, IRoxieInput * _input)
- {
- if (id == sequence)
- {
- streamInput.set(_input);
- return true;
- }
- return false;
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
- }
- };
- class CRoxieServerLocalResultStreamReadActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerLocalResultStreamReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerLocalResultStreamReadActivity(this, _probeManager);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for LocalResultRead activity");
- }
- };
- IRoxieServerActivityFactory *createRoxieServerLocalResultStreamReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerLocalResultStreamReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=====================================================================================================
- class CRoxieServerLocalResultWriteActivity : public CRoxieServerInternalSinkActivity
- {
- IHThorLocalResultWriteArg &helper;
- ILocalGraphEx * graph;
- unsigned graphId;
- public:
- CRoxieServerLocalResultWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
- : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorLocalResultWriteArg &)basehelper), graphId(_graphId)
- {
- graph = NULL;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerInternalSinkActivity::onCreate(_ctx, _colocalParent);
- graph = static_cast<ILocalGraphEx *>(_ctx->queryCodeContext()->resolveLocalQuery(graphId));
- }
- virtual void onExecute()
- {
- ConstPointerArray rows;
- try
- {
- loop
- {
- const void *nextrec = input->nextInGroup();
- if (!nextrec)
- {
- nextrec = input->nextInGroup();
- if (!nextrec)
- break;
- rows.append(NULL);
- }
- rows.append(nextrec);
- }
- IOutputMetaData *outputMeta = input->queryOutputMeta();
- Owned<CGraphResult> result = new CGraphResult(ctx->queryCodeContext()->getRowAllocator(outputMeta, activityId), rows, true);
- graph->setResult(helper.querySequence(), result);
- }
- catch(...)
- {
- ReleaseRoxieRowSet(rows);
- throw;
- }
- }
- IRoxieInput * querySelectOutput(unsigned id)
- {
- if (id == helper.querySequence())
- {
- executed = true;
- return LINK(input);
- }
- return NULL;
- }
- };
- class CRoxieServerLocalResultWriteActivityFactory : public CRoxieServerInternalSinkFactory
- {
- unsigned graphId;
- public:
- CRoxieServerLocalResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, unsigned _graphId, bool _isRoot)
- : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot), graphId(_graphId)
- {
- isInternal = true;
- Owned<IHThorLocalResultWriteArg> helper = (IHThorLocalResultWriteArg *) helperFactory();
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerLocalResultWriteActivity(this, _probeManager, graphId);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerLocalResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, unsigned _graphId, bool _isRoot)
- {
- return new CRoxieServerLocalResultWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _graphId, _isRoot);
- }
- //=================================================================================
- class CRoxieServerGraphLoopResultReadActivity : public CRoxieServerActivity
- {
- protected:
- IHThorGraphLoopResultReadArg &helper;
- Owned<IRoxieInput> iter;
- ILocalGraphEx * graph;
- unsigned graphId;
- unsigned sequence;
- public:
- CRoxieServerGraphLoopResultReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorGraphLoopResultReadArg &)basehelper), graphId(_graphId)
- {
- graph = NULL;
- sequence = 0;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- graph = static_cast<ILocalGraphEx *>(_ctx->queryCodeContext()->resolveLocalQuery(graphId));
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- if (iter)
- iter->start(parentExtractSize, parentExtract, paused);
- else
- {
- sequence = helper.querySequence();
- if ((int)sequence >= 0)
- {
- try
- {
- iter.setown(graph->createGraphLoopResultIterator(sequence));
- }
- catch (IException * E)
- {
- throw makeWrappedException(E);
- }
- }
- }
- }
- virtual void stop(bool aborting)
- {
- if (iter)
- iter->stop(aborting);
- CRoxieServerActivity::stop(aborting);
- }
- virtual void reset()
- {
- if (iter)
- iter->reset();
- iter.clear();
- CRoxieServerActivity::reset();
- };
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void * next = iter ? iter->nextInGroup() : NULL;
- if (next)
- {
- processed++;
- atomic_inc(&rowsIn);
- }
- return next;
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
- }
- virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract)
- {
- ensureCreated();
- basehelper.onStart(parentExtract, NULL);
- processor.noteUseIteration(helper.querySequence());
- }
- virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract, IProbeManager *probeManager, IArrayOf<IRoxieInput> &probes)
- {
- //helper already initialised from the gratherIterationUsage() call.
- iter.set(processor.connectIterationOutput(helper.querySequence(), probeManager, probes, this, 0));
- }
- };
- //variety of CRoxieServerGraphLoopResultReadActivity created internally with a predefined sequence number
- class CRoxieServerInternalGraphLoopResultReadActivity : public CRoxieServerGraphLoopResultReadActivity
- {
- public:
- CRoxieServerInternalGraphLoopResultReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId, unsigned _sequence)
- : CRoxieServerGraphLoopResultReadActivity(_factory, _probeManager, _graphId)
- {
- sequence = _sequence;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- if ((int)sequence >= 0)
- {
- try
- {
- iter.setown(graph->createGraphLoopResultIterator(sequence));
- }
- catch (IException * E)
- {
- throw makeWrappedException(E);
- }
- }
- }
- };
- class CRoxieServerGraphLoopResultReadActivityFactory : public CRoxieServerActivityFactory
- {
- unsigned graphId;
- public:
- CRoxieServerGraphLoopResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _graphId)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), graphId(_graphId)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerGraphLoopResultReadActivity(this, _probeManager, graphId);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for GraphLoopResultRead activity");
- }
- };
- IRoxieServerActivityFactory *createRoxieServerGraphLoopResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned graphId)
- {
- return new CRoxieServerGraphLoopResultReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, graphId);
- }
- //=====================================================================================================
- class CRoxieServerGraphLoopResultWriteActivity : public CRoxieServerInternalSinkActivity
- {
- IHThorGraphLoopResultWriteArg &helper;
- ILocalGraphEx * graph;
- unsigned graphId;
- public:
- CRoxieServerGraphLoopResultWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
- : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorGraphLoopResultWriteArg &)basehelper), graphId(_graphId)
- {
- graph = NULL;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerInternalSinkActivity::onCreate(_ctx, _colocalParent);
- graph = static_cast<ILocalGraphEx *>(_ctx->queryCodeContext()->resolveLocalQuery(graphId));
- }
- virtual void onExecute()
- {
- ConstPointerArray rows;
- try
- {
- loop
- {
- const void *nextrec = input->nextInGroup();
- if (!nextrec)
- {
- nextrec = input->nextInGroup();
- if (!nextrec)
- break;
- rows.append(NULL);
- }
- rows.append(nextrec);
- }
- IOutputMetaData *outputMeta = input->queryOutputMeta();
- Owned<CGraphResult> result = new CGraphResult(ctx->queryCodeContext()->getRowAllocator(outputMeta, activityId), rows, true);
- graph->setGraphLoopResult(result);
- }
- catch(...)
- {
- ReleaseRoxieRowSet(rows);
- throw;
- }
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- if (idx==0)
- return this;
- else
- return NULL;
- }
- virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
- {
- return input->gatherConjunctions(collector);
- }
- virtual void resetEOF()
- {
- input->resetEOF();
- }
- virtual const void *nextInGroup()
- {
- const void * next = input->nextInGroup();
- if (next)
- processed++;
- return next;
- }
- virtual bool isPassThrough()
- {
- return true;
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void * next = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- if (next)
- processed++;
- return next;
- }
- IInputSteppingMeta * querySteppingMeta()
- {
- return input->querySteppingMeta();
- }
- virtual IOutputMetaData * queryOutputMeta() const
- {
- return input->queryOutputMeta();
- }
- };
- class CRoxieServerGraphLoopResultWriteActivityFactory : public CRoxieServerInternalSinkFactory
- {
- unsigned graphId;
- public:
- CRoxieServerGraphLoopResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, unsigned _graphId)
- : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, true), graphId(_graphId)
- {
- isInternal = true;
- Owned<IHThorGraphLoopResultWriteArg> helper = (IHThorGraphLoopResultWriteArg *) helperFactory();
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerGraphLoopResultWriteActivity(this, _probeManager, graphId);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerGraphLoopResultWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, unsigned _graphId)
- {
- return new CRoxieServerGraphLoopResultWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _graphId);
- }
- #if 0
- //=====================================================================================================
- CHThorLocalResultSpillActivity::CHThorLocalResultSpillActivity(IAgentContext &_agent, ActivityId const & _id, IHThorLocalResultSpillArg &_arg)
- : CHThorSimpleActivityBase(_agent, _id, _arg), helper(_arg)
- {
- next = NULL;
- }
- void CHThorLocalResultSpillActivity::ready()
- {
- CHThorSimpleActivityBase::ready();
- next = input->nextInGroup();
- grouped = input->isGrouped();
- rowdata.clear();
- }
- const void * CHThorLocalResultSpillActivity::nextInGroup()
- {
- const void * ret = next;
- next = input->nextInGroup();
- if (!ret && !next)
- return NULL;
- if (ret)
- {
- size32_t thisSize = outputMeta->getRecordSize(ret);
- rowdata.append(thisSize, ret);
- if (grouped)
- rowdata.append(next == NULL);
- }
- return ret;
- }
- void CHThorLocalResultSpillActivity::done()
- {
- loop
- {
- const void * ret = nextInGroup();
- if (!ret)
- {
- ret = nextInGroup();
- if (!ret)
- break;
- }
- ReleaseRoxieRow(ret);
- }
- agent.setLocalResult(helper.querySequence(), rowdata.length(), rowdata.toByteArray());
- CHThorSimpleActivityBase::done();
- }
- #endif
- //=================================================================================
- class CRoxieServerDedupActivity : public CRoxieServerActivity
- {
- IHThorDedupArg &helper;
- bool keepLeft;
- bool first;
- unsigned numKept;
- unsigned numToKeep;
- const void *kept;
- public:
- CRoxieServerDedupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _keepLeft)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorDedupArg &)basehelper)
- {
- keepLeft = _keepLeft;
- kept = NULL;
- numKept = 0;
- first = true;
- numToKeep = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- numKept = 0;
- first = true;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- numToKeep = helper.numToKeep();
- }
- virtual void reset()
- {
- ReleaseClearRoxieRow(kept);
- CRoxieServerActivity::reset();
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (first)
- {
- kept = input->nextInGroup();
- first = false;
- }
- const void * next;
- loop
- {
- next = input->nextInGroup();
- if (!kept || !next || !helper.matches(kept,next))
- {
- numKept = 0;
- break;
- }
- if (numKept < numToKeep-1)
- {
- numKept++;
- break;
- }
- if (keepLeft)
- {
- ReleaseRoxieRow(next);
- }
- else
- {
- ReleaseRoxieRow(kept);
- kept = next;
- }
- }
- const void * ret = kept;
- kept = next;
- // CTXLOG("dedup returns %p", ret);
- if (ret) processed++;
- return ret;
- }
- };
- class CRoxieServerDedupAllActivity : public CRoxieServerActivity
- {
- IHThorDedupArg &helper;
- unsigned survivorIndex;
- ConstPointerArray survivors;
- bool keepLeft;
- bool eof;
- bool first;
- ICompare *primaryCompare;
- public:
- CRoxieServerDedupAllActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _keepLeft)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorDedupArg &)basehelper)
- {
- keepLeft = _keepLeft;
- primaryCompare = helper.queryComparePrimary();
- eof = false;
- first = true;
- survivorIndex = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- first = true;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual void reset()
- {
- #ifdef _DEBUG
- while (survivors.isItem(survivorIndex))
- {
- ReleaseRoxieRow(survivors.item(survivorIndex++));
- }
- #endif
- survivors.kill();
- eof = false;
- first = true;
- CRoxieServerActivity::reset();
- }
- void dedupRange(unsigned first, unsigned last, ConstPointerArray & group)
- {
- for (unsigned idxL = first; idxL < last; idxL++)
- {
- const void * left = group.item(idxL);
- if (left)
- {
- for (unsigned idxR = first; idxR < last; idxR++)
- {
- const void * right = group.item(idxR);
- if ((idxL != idxR) && right)
- {
- if (helper.matches(left, right))
- {
- if (keepLeft)
- {
- group.replace(NULL, idxR);
- ReleaseRoxieRow(right);
- }
- else
- {
- group.replace(NULL, idxL);
- ReleaseRoxieRow(left);
- break;
- }
- }
- }
- }
- }
- }
- }
- void dedupRangeIndirect(unsigned first, unsigned last, void *** index)
- {
- for (unsigned idxL = first; idxL < last; idxL++)
- {
- void * left = *(index[idxL]);
- if (left)
- {
- for (unsigned idxR = first; idxR < last; idxR++)
- {
- void * right = *(index[idxR]);
- if ((idxL != idxR) && right)
- {
- if (helper.matches(left, right))
- {
- if (keepLeft)
- {
- *(index[idxR]) = NULL;
- ReleaseRoxieRow(right);
- }
- else
- {
- *(index[idxL]) = NULL;
- ReleaseRoxieRow(left);
- break;
- }
- }
- }
- }
- }
- }
- }
- bool calcNextDedupAll()
- {
- survivors.kill();
- survivorIndex = 0;
- ConstPointerArray group;
- if (eof || !input->nextGroup(group))
- {
- eof = true;
- return false;
- }
- unsigned max = group.ordinality();
- if (primaryCompare)
- {
- //hard, if not impossible, to hit this code once optimisations in place
- MemoryAttr indexbuff(max*sizeof(void **));
- void *** index = (void ***)indexbuff.bufferBase();
- qsortvecstable(const_cast<void * *>(group.getArray()), max, *primaryCompare, index);
- unsigned first = 0;
- for (unsigned idx = 1; idx < max; idx++)
- {
- if (primaryCompare->docompare(*(index[first]), *(index[idx])) != 0)
- {
- dedupRangeIndirect(first, idx, index);
- first = idx;
- }
- }
- dedupRangeIndirect(first, max, index);
- for(unsigned idx2=0; idx2<max; ++idx2)
- {
- void * cur = *(index[idx2]);
- if(cur)
- survivors.append(cur);
- }
- }
- else
- {
- dedupRange(0, max, group);
- for(unsigned idx=0; idx<max; ++idx)
- {
- const void * cur = group.item(idx);
- if(cur)
- survivors.append(cur);
- }
- }
- return true;
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (first)
- {
- calcNextDedupAll();
- first = false;
- }
- while (survivors.isItem(survivorIndex))
- {
- const void *ret = survivors.item(survivorIndex++);
- if (ret)
- {
- processed++;
- return ret;
- }
- }
- calcNextDedupAll();
- return NULL;
- }
- };
- class CRoxieServerDedupActivityFactory : public CRoxieServerActivityFactory
- {
- bool compareAll;
- bool keepLeft;
- public:
- CRoxieServerDedupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- Owned<IHThorDedupArg> helper = (IHThorDedupArg *) helperFactory();
- compareAll = helper->compareAll();
- keepLeft = helper->keepLeft();
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- if (compareAll)
- return new CRoxieServerDedupAllActivity(this, _probeManager, keepLeft);
- else
- return new CRoxieServerDedupActivity(this, _probeManager, keepLeft);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerDedupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerDedupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerHashDedupActivity : public CRoxieServerActivity
- {
- bool eof;
- IHThorHashDedupArg &helper;
- class HashDedupElement
- {
- public:
- HashDedupElement(unsigned _hash, const void *_keyRow)
- : hash(_hash), keyRow(_keyRow)
- {
- }
- ~HashDedupElement()
- {
- ReleaseRoxieRow(keyRow);
- }
- inline unsigned queryHash() const
- {
- return hash;
- }
- inline const void *queryRow() const
- {
- return keyRow;
- }
- private:
- unsigned hash;
- const void *keyRow;
- };
- class HashDedupTable : public SuperHashTable
- {
- public:
- HashDedupTable(IHThorHashDedupArg & _helper, unsigned _activityId)
- : helper(_helper),
- activityId(_activityId),
- keySize(helper.queryKeySize())
- {
- }
- virtual ~HashDedupTable() { releaseAll(); }
- virtual unsigned getHashFromElement(const void *et) const
- {
- const HashDedupElement *element = reinterpret_cast<const HashDedupElement *>(et);
- return element->queryHash();
- }
- virtual unsigned getHashFromFindParam(const void *fp) const { throwUnexpected(); }
- virtual const void * getFindParam(const void *et) const { throwUnexpected(); }
- virtual bool matchesElement(const void *et, const void *searchET) const { throwUnexpected(); }
- virtual bool matchesFindParam(const void *et, const void *key, unsigned fphash) const
- {
- const HashDedupElement *element = reinterpret_cast<const HashDedupElement *>(et);
- if (fphash != element->queryHash())
- return false;
- return (helper.queryKeyCompare()->docompare(element->queryRow(), key) == 0);
- }
- virtual void onAdd(void *et) {}
- virtual void onRemove(void *et)
- {
- const HashDedupElement *element = reinterpret_cast<const HashDedupElement *>(et);
- delete element;
- }
- void onCreate(IRoxieSlaveContext *ctx)
- {
- keyRowAllocator.setown(ctx->queryCodeContext()->getRowAllocator(keySize.queryOriginal(), activityId));
- }
- void reset()
- {
- kill();
- }
- bool insert(const void * row)
- {
- unsigned hash = helper.queryHash()->hash(row);
- RtlDynamicRowBuilder keyRowBuilder(keyRowAllocator, true);
- size32_t thisKeySize = helper.recordToKey(keyRowBuilder, row);
- OwnedConstRoxieRow keyRow = keyRowBuilder.finalizeRowClear(thisKeySize);
- if (find(hash, keyRow.get()))
- return false;
- addNew(new HashDedupElement(hash, keyRow.getClear()), hash);
- return true;
- }
- private:
- IHThorHashDedupArg & helper;
- CachedOutputMetaData keySize;
- Owned<IEngineRowAllocator> keyRowAllocator;
- unsigned activityId;
- } table;
- public:
- CRoxieServerHashDedupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorHashDedupArg &)basehelper), table(helper, activityId)
- {
- eof = false;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual void onCreate(IRoxieSlaveContext *ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(ctx, colocalParent);
- table.onCreate(ctx);
- }
- virtual void reset()
- {
- table.reset();
- eof = false;
- CRoxieServerActivity::reset();
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- while(!eof)
- {
- const void * next = input->nextInGroup();
- if(!next)
- next = input->nextInGroup();
- if(!next)
- {
- eof = true;
- break;
- }
- if(table.insert(next))
- return next;
- else
- ReleaseRoxieRow(next);
- }
- return NULL;
- }
- };
- class CRoxieServerHashDedupActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerHashDedupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerHashDedupActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerHashDedupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerHashDedupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerRollupActivity : public CRoxieServerActivity
- {
- IHThorRollupArg &helper;
- OwnedConstRoxieRow left;
- OwnedConstRoxieRow prev;
- OwnedConstRoxieRow right;
- bool readFirstRow;
- public:
- CRoxieServerRollupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorRollupArg &)basehelper)
- {
- readFirstRow = false;
- }
- ~CRoxieServerRollupActivity()
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- readFirstRow = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual void reset()
- {
- left.clear();
- prev.clear();
- right.clear();
- CRoxieServerActivity::reset();
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (!readFirstRow)
- {
- left.setown(input->nextInGroup());
- prev.set(left);
- readFirstRow = true;
- }
- loop
- {
- right.setown(input->nextInGroup());
- if(!prev || !right || !helper.matches(prev,right))
- {
- const void * ret = left.getClear();
- if(ret)
- processed++;
- left.setown(right.getClear());
- prev.set(left);
- return ret;
- }
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned outSize = helper.transform(rowBuilder, left, right);
- if (outSize)
- left.setown(rowBuilder.finalizeRowClear(outSize));
- prev.set(right);
- }
- catch(IException * E)
- {
- throw makeWrappedException(E);
- }
- }
- }
- };
- class CRoxieServerRollupActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerRollupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerRollupActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerRollupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerRollupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerNormalizeActivity : public CRoxieServerActivity
- {
- IHThorNormalizeArg &helper;
- unsigned numThisRow;
- unsigned curRow;
- const void *buffer;
- unsigned numProcessedLastGroup;
- public:
- CRoxieServerNormalizeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorNormalizeArg &)basehelper)
- {
- buffer = NULL;
- numThisRow = 0;
- curRow = 0;
- numProcessedLastGroup = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- numThisRow = 0;
- curRow = 0;
- numProcessedLastGroup = 0;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual void reset()
- {
- ReleaseClearRoxieRow(buffer);
- CRoxieServerActivity::reset();
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- while (curRow == numThisRow)
- {
- if (buffer)
- ReleaseClearRoxieRow(buffer);
- buffer = input->nextInGroup();
- if (!buffer && (processed == numProcessedLastGroup))
- buffer = input->nextInGroup();
- if (!buffer)
- {
- numProcessedLastGroup = processed;
- return NULL;
- }
- curRow = 0;
- numThisRow = helper.numExpandedRows(buffer);
- }
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned actualSize = helper.transform(rowBuilder, buffer, ++curRow);
- if (actualSize != 0)
- {
- processed++;
- return rowBuilder.finalizeRowClear(actualSize);
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- }
- };
- class CRoxieServerNormalizeActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerNormalizeActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerNormalizeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerNormalizeChildActivity : public CRoxieServerActivity
- {
- IHThorNormalizeChildArg &helper;
- unsigned numThisRow;
- unsigned curRow;
- const void *buffer;
- unsigned numProcessedLastGroup;
- INormalizeChildIterator * cursor;
- const void * curChildRow;
- bool advanceInput()
- {
- loop
- {
- ReleaseClearRoxieRow(buffer);
- buffer = input->nextInGroup();
- if (!buffer && (processed == numProcessedLastGroup))
- buffer = input->nextInGroup();
- if (!buffer)
- {
- numProcessedLastGroup = processed;
- return false;
- }
- curChildRow = cursor->first(buffer);
- if (curChildRow)
- {
- curRow = 0;
- return true;
- }
- }
- }
- public:
- CRoxieServerNormalizeChildActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorNormalizeChildArg &)basehelper)
- {
- buffer = NULL;
- cursor = NULL;
- numThisRow = 0;
- curRow = 0;
- numProcessedLastGroup = 0;
- curChildRow = NULL;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- numThisRow = 0;
- curRow = 0;
- numProcessedLastGroup = 0;
- curChildRow = NULL;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- cursor = helper.queryIterator();
- }
- virtual void stop(bool aborting)
- {
- CRoxieServerActivity::stop(aborting);
- }
- virtual void reset()
- {
- cursor = NULL;
- ReleaseClearRoxieRow(buffer);
- CRoxieServerActivity::reset();
- }
- virtual bool needsAllocator() const { return true; }
- const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- if (!buffer)
- {
- if (!advanceInput())
- return NULL;
- }
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t outSize = helper.transform(rowBuilder, buffer, curChildRow, ++curRow);
- curChildRow = cursor->next();
- if (!curChildRow)
- ReleaseClearRoxieRow(buffer);
- if (outSize != 0)
- {
- processed++;
- return rowBuilder.finalizeRowClear(outSize);
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- }
- };
- class CRoxieServerNormalizeChildActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerNormalizeChildActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerNormalizeChildActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNormalizeChildActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerNormalizeChildActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerNormalizeLinkedChildActivity : public CRoxieServerActivity
- {
- IHThorNormalizeLinkedChildArg &helper;
- OwnedConstRoxieRow curParent;
- OwnedConstRoxieRow curChild;
- unsigned numProcessedLastGroup;
- bool advanceInput()
- {
- loop
- {
- curParent.setown(input->nextInGroup());
- if (!curParent && (processed == numProcessedLastGroup))
- curParent.setown(input->nextInGroup());
- if (!curParent)
- {
- numProcessedLastGroup = processed;
- return false;
- }
- curChild.set(helper.first(curParent));
- if (curChild)
- return true;
- }
- }
- public:
- CRoxieServerNormalizeLinkedChildActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorNormalizeLinkedChildArg &)basehelper)
- {
- numProcessedLastGroup = 0;
- }
- ~CRoxieServerNormalizeLinkedChildActivity()
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- numProcessedLastGroup = 0;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual void reset()
- {
- curParent.clear();
- curChild.clear();
- CRoxieServerActivity::reset();
- }
- const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- if (!curParent)
- {
- if (!advanceInput())
- return NULL;
- }
- try
- {
- const void *ret = curChild.getClear();
- curChild.set(helper.next());
- if (!curChild)
- curParent.clear();
- if (ret)
- {
- processed++;
- return ret;
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- }
- };
- class CRoxieServerNormalizeLinkedChildActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerNormalizeLinkedChildActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerNormalizeLinkedChildActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNormalizeLinkedChildActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerNormalizeLinkedChildActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- interface ISortAlgorithm
- {
- virtual void prepare(IRoxieInput *input) = 0;
- virtual const void *next() = 0;
- virtual void reset() = 0;
- };
- class CQuickSortAlgorithm : implements ISortAlgorithm
- {
- unsigned curIndex;
- ConstPointerArray sorted;
- ICompare *compare;
- public:
- CQuickSortAlgorithm(ICompare *_compare) : compare(_compare)
- {
- curIndex = 0;
- }
- virtual void prepare(IRoxieInput *input)
- {
- curIndex = 0;
- if (input->nextGroup(sorted))
- qsortvec(const_cast<void * *>(sorted.getArray()), sorted.ordinality(), *compare);
- }
- virtual const void *next()
- {
- if (sorted.isItem(curIndex))
- return sorted.item(curIndex++);
- return NULL;
- }
- virtual void reset()
- {
- while (sorted.isItem(curIndex))
- ReleaseRoxieRow(sorted.item(curIndex++));
- curIndex = 0;
- sorted.kill();
- }
- };
- class CSpillingQuickSortAlgorithm : implements ISortAlgorithm, implements roxiemem::IBufferedRowCallback
- {
- enum {
- InitialSortElements = 0,
- //The number of rows that can be added without entering a critical section, and therefore also the number
- //of rows that might not get freed when memory gets tight.
- CommitStep=32
- };
- roxiemem::DynamicRoxieOutputRowArray rowsToSort;
- roxiemem::RoxieSimpleInputRowArray sorted;
- ICompare *compare;
- IRoxieSlaveContext * ctx;
- Owned<IDiskMerger> diskMerger;
- Owned<IRowStream> diskReader;
- Owned<IOutputMetaData> rowMeta;
- unsigned activityId;
- public:
- CSpillingQuickSortAlgorithm(ICompare *_compare, IRoxieSlaveContext * _ctx, IOutputMetaData * _rowMeta, unsigned _activityId)
- : rowsToSort(&_ctx->queryRowManager(), InitialSortElements, CommitStep), ctx(_ctx), compare(_compare), rowMeta(_rowMeta), activityId(_activityId)
- {
- ctx->queryRowManager().addRowBuffer(this);
- }
- ~CSpillingQuickSortAlgorithm()
- {
- ctx->queryRowManager().removeRowBuffer(this);
- diskReader.clear();
- }
- virtual void prepare(IRoxieInput *input)
- {
- loop
- {
- const void * next = input->nextInGroup();
- if (!next)
- break;
- if (!rowsToSort.append(next))
- {
- {
- roxiemem::RoxieOutputRowArrayLock block(rowsToSort);
- //We should have been called back to free any committed rows, but occasionally it may not (e.g., if
- //the problem is global memory is exhausted) - in which case force a spill here (but add any pending
- //rows first).
- if (rowsToSort.numCommitted() != 0)
- {
- rowsToSort.flush();
- spillRows();
- }
- //Ensure new rows are written to the head of the array. It needs to be a separate call because
- //spillRows() cannot shift active row pointer since it can be called from any thread
- rowsToSort.flush();
- }
- if (!rowsToSort.append(next))
- {
- ReleaseRoxieRow(next);
- throw MakeStringException(ROXIEMM_MEMORY_LIMIT_EXCEEDED, "Insufficient memory to append sort row");
- }
- }
- }
- rowsToSort.flush();
- roxiemem::RoxieOutputRowArrayLock block(rowsToSort);
- if (diskMerger)
- {
- spillRows();
- rowsToSort.kill();
- diskReader.setown(diskMerger->merge(compare));
- }
- else
- {
- unsigned numRows = rowsToSort.numCommitted();
- if (numRows)
- {
- const void * * rows = rowsToSort.getBlock(numRows);
- //MORE: Should this be parallel? Should that be dependent on whether it is grouped? Should be a hint.
- qsortvec(const_cast<void * *>(rows), numRows, *compare);
- }
- sorted.transferFrom(rowsToSort);
- }
- }
- virtual const void *next()
- {
- if(diskReader)
- return diskReader->nextRow();
- return sorted.dequeue();
- }
- virtual void reset()
- {
- //MORE: This could transfer any row pointer from sorted back to rowsToSort. It would trade
- //fewer heap allocations with not freeing up the memory from large group sorts.
- rowsToSort.clearRows();
- sorted.kill();
- //Disk reader must be cleared before the merger - or the files may still be locked.
- diskReader.clear();
- diskMerger.clear();
- }
- //interface roxiemem::IBufferedRowCallback
- virtual unsigned getPriority() const
- {
- //Spill global sorts before grouped sorts
- if (rowMeta->isGrouped())
- return 20;
- return 10;
- }
- virtual bool freeBufferedRows(bool critical)
- {
- roxiemem::RoxieOutputRowArrayLock block(rowsToSort);
- return spillRows();
- }
- protected:
- bool spillRows()
- {
- unsigned numRows = rowsToSort.numCommitted();
- if (numRows == 0)
- return false;
- const void * * rows = rowsToSort.getBlock(numRows);
- qsortvec(const_cast<void * *>(rows), numRows, *compare);
- Owned<IRowWriter> out = queryMerger()->createWriteBlock();
- for (unsigned i= 0; i < numRows; i++)
- {
- out->putRow(rows[i]);
- }
- rowsToSort.noteSpilled(numRows);
- return true;
- }
- IDiskMerger * queryMerger()
- {
- if (!diskMerger)
- {
- unsigned __int64 seq = (memsize_t)this ^ get_cycles_now();
- StringBuffer spillBasename;
- spillBasename.append(tempDirectory).append(PATHSEPCHAR).appendf("spill_sort_%"I64F"u", seq);
- Owned<IRowLinkCounter> linker = new RoxieRowLinkCounter();
- Owned<IRowInterfaces> rowInterfaces = createRowInterfaces(rowMeta, activityId, ctx->queryCodeContext());
- diskMerger.setown(createDiskMerger(rowInterfaces, linker, spillBasename));
- }
- return diskMerger;
- }
- };
- #define INSERTION_SORT_BLOCKSIZE 1024
- class SortedBlock : public CInterface, implements IInterface
- {
- unsigned sequence;
- const void **rows;
- unsigned length;
- unsigned pos;
- SortedBlock(const SortedBlock &);
- public:
- IMPLEMENT_IINTERFACE;
- SortedBlock(unsigned _sequence, IRowManager *rowManager, unsigned activityId) : sequence(_sequence)
- {
- rows = (const void **) rowManager->allocate(INSERTION_SORT_BLOCKSIZE * sizeof(void *), activityId);
- length = 0;
- pos = 0;
- }
- ~SortedBlock()
- {
- while (pos < length)
- ReleaseRoxieRow(rows[pos++]);
- ReleaseRoxieRow(rows);
- }
- int compareTo(SortedBlock *r, ICompare *compare)
- {
- int rc = compare->docompare(rows[pos], r->rows[r->pos]);
- if (!rc)
- rc = sequence - r->sequence;
- return rc;
- }
- const void *next()
- {
- if (pos < length)
- return rows[pos++];
- else
- return NULL;
- }
- inline bool eof()
- {
- return pos==length;
- }
- bool insert(const void *next, ICompare *_compare )
- {
- unsigned b = length;
- if (b == INSERTION_SORT_BLOCKSIZE)
- return false;
- else if (b < 7)
- {
- while (b)
- {
- if (_compare->docompare(next, rows[b-1]) >= 0)
- break;
- b--;
- }
- if (b != length)
- memmove(&rows[b+1], &rows[b], (length - b) * sizeof(void *));
- rows[b] = next;
- length++;
- return true;
- }
- else
- {
- unsigned int a = 0;
- while ((int)a<b)
- {
- int i = (a+b)/2;
- int rc = _compare->docompare(next, rows[i]);
- if (rc>=0)
- a = i+1;
- else
- b = i;
- }
- if (a != length)
- memmove(&rows[a+1], &rows[a], (length - a) * sizeof(void *));
- rows[a] = next;
- length++;
- return true;
- }
- }
- };
- class CInsertionSortAlgorithm : implements ISortAlgorithm
- {
- SortedBlock *curBlock;
- unsigned blockNo;
- IArrayOf<SortedBlock> blocks;
- unsigned activityId;
- IRowManager *rowManager;
- ICompare *compare;
- void newBlock()
- {
- blocks.append(*curBlock);
- curBlock = new SortedBlock(blockNo++, rowManager, activityId);
- }
- inline static int doCompare(SortedBlock &l, SortedBlock &r, ICompare *compare)
- {
- return l.compareTo(&r, compare);
- }
- void makeHeap()
- {
- /* Permute blocks to establish the heap property
- For each element p, the children are p*2+1 and p*2+2 (provided these are in range)
- The children of p must both be greater than or equal to p
- The parent of a child c is given by p = (c-1)/2
- */
- unsigned i;
- unsigned n = blocks.length();
- SortedBlock **s = blocks.getArray();
- for (i=1; i<n; i++)
- {
- SortedBlock * r = s[i];
- int c = i; /* child */
- while (c > 0)
- {
- int p = (c-1)/2; /* parent */
- if ( doCompare( blocks.item(c), blocks.item(p), compare ) >= 0 )
- break;
- s[c] = s[p];
- s[p] = r;
- c = p;
- }
- }
- }
- void remakeHeap()
- {
- /* The row associated with block[0] will have changed
- This code restores the heap property
- */
- unsigned p = 0; /* parent */
- unsigned n = blocks.length();
- SortedBlock **s = blocks.getArray();
- while (1)
- {
- unsigned c = p*2 + 1; /* child */
- if ( c >= n )
- break;
- /* Select smaller child */
- if ( c+1 < n && doCompare( blocks.item(c+1), blocks.item(c), compare ) < 0 ) c += 1;
- /* If child is greater or equal than parent then we are done */
- if ( doCompare( blocks.item(c), blocks.item(p), compare ) >= 0 )
- break;
- /* Swap parent and child */
- SortedBlock *r = s[c];
- s[c] = s[p];
- s[p] = r;
- /* child becomes parent */
- p = c;
- }
- }
- public:
- CInsertionSortAlgorithm(ICompare *_compare, IRowManager *_rowManager, unsigned _activityId)
- : compare(_compare)
- {
- rowManager = _rowManager;
- activityId = _activityId;
- curBlock = NULL;
- blockNo = 0;
- }
- virtual void reset()
- {
- blocks.kill();
- delete curBlock;
- curBlock = NULL;
- blockNo = 0;
- }
- virtual void prepare(IRoxieInput *input)
- {
- blockNo = 0;
- curBlock = new SortedBlock(blockNo++, rowManager, activityId);
- loop
- {
- const void *next = input->nextInGroup();
- if (!next)
- break;
- if (!curBlock->insert(next, compare))
- {
- newBlock();
- curBlock->insert(next, compare);
- }
- }
- if (blockNo > 1)
- {
- blocks.append(*curBlock);
- curBlock = NULL;
- makeHeap();
- }
- }
- virtual const void * next()
- {
- const void *ret;
- if (blockNo==1) // single block case..
- {
- ret = curBlock->next();
- }
- else if (blocks.length())
- {
- SortedBlock &top = blocks.item(0);
- ret = top.next();
- if (top.eof())
- blocks.replace(blocks.popGet(), 0);
- remakeHeap();
- }
- else
- ret = NULL;
- return ret;
- }
- };
- class CHeapSortAlgorithm : implements ISortAlgorithm
- {
- unsigned curIndex;
- ConstPointerArray sorted;
- bool inputAlreadySorted;
- IntArray sequences;
- bool eof;
- ICompare *compare;
- #ifdef _CHECK_HEAPSORT
- void checkHeap() const
- {
- unsigned n = sorted.ordinality();
- if (n)
- {
- ICompare *_compare = compare;
- void **s = sorted.getArray();
- int *sq = sequences.getArray();
- unsigned p;
- #if 0
- CTXLOG("------------------------%d entries-----------------", n);
- for (p = 0; p < n; p++)
- {
- CTXLOG("HEAP %d: %d %.10s", p, sq[p], s[p] ? s[p] : "..");
- }
- #endif
- for (p = 0; p < n; p++)
- {
- unsigned c = p*2+1;
- if (c<n)
- assertex(!s[c] || (docompare(p, c, _compare, s, sq) <= 0));
- c++;
- if (c<n)
- assertex(!s[c] || (docompare(p, c, _compare, s, sq) <= 0));
- }
- }
- }
- #else
- inline void checkHeap() const {}
- #endif
- const void *removeHeap()
- {
- unsigned n = sorted.ordinality();
- if (n)
- {
- const void *ret = sorted.item(0);
- if (n > 1 && ret)
- {
- ICompare *_compare = compare;
- const void **s = sorted.getArray();
- int *sq = sequences.getArray();
- unsigned v = 0; // vacancy
- loop
- {
- unsigned c = 2*v + 1;
- if (c < n)
- {
- unsigned f = c; // favourite to fill it
- c++;
- if (c < n && s[c] && (!s[f] || (docompare(f, c, _compare, s, sq) > 0))) // is the smaller of the children
- f = c;
- sq[v] = sq[f];
- if ((s[v] = s[f]) != NULL)
- v = f;
- else
- break;
- }
- else
- {
- s[v] = NULL;
- break;
- }
- }
- }
- checkHeap();
- return ret;
- }
- else
- return NULL;
- }
- static inline int docompare(unsigned l, unsigned r, ICompare *_compare, const void **s, int *sq)
- {
- int rc = _compare->docompare(s[l], s[r]);
- if (!rc)
- rc = sq[l] - sq[r];
- return rc;
- }
- void insertHeap(const void *next)
- {
- // Upside-down heap sort
- // Maintain a heap where every parent is lower than each of its children
- // Root (at node 0) is lowest record seen, nodes 2n+1, 2n+2 are the children
- // To insert a row, add it at end then keep swapping with parent as long as parent is greater
- // To remove a row, take row 0, then recreate heap by replacing it with smaller of two children and so on down the tree
- // Nice features:
- // 1. Deterministic
- // 2. Sort time can be overlapped with upstream/downstream processes - there is no delay between receiving last record from input and deliveriing first to output
- // 3. Already sorted case can be spotted at zero cost while reading.
- // 4. If you don't read all the results, you don't have to complete the sort
- // BUT it is NOT stable, so we have to use a parallel array of sequence numbers
- unsigned n = sorted.ordinality();
- sorted.append(next);
- sequences.append(n);
- if (!n)
- return;
- ICompare *_compare = compare;
- const void **s = sorted.getArray();
- if (inputAlreadySorted)
- {
- if (_compare->docompare(next, s[n-1]) >= 0)
- return;
- else
- {
- // MORE - could delay creating sequences until now...
- inputAlreadySorted = false;
- }
- }
- int *sq = sequences.getArray();
- unsigned q = n;
- while (n)
- {
- unsigned parent = (n-1) / 2;
- const void *p = s[parent];
- if (_compare->docompare(p, next) <= 0)
- break;
- s[n] = p;
- sq[n] = sq[parent];
- s[parent] = next;
- sq[parent] = q;
- n = parent;
- }
- }
- public:
- CHeapSortAlgorithm(ICompare *_compare) : compare(_compare)
- {
- inputAlreadySorted = true;
- curIndex = 0;
- eof = false;
- }
- virtual void reset()
- {
- eof = false;
- if (inputAlreadySorted)
- {
- while (sorted.isItem(curIndex))
- ReleaseRoxieRow(sorted.item(curIndex++));
- sorted.kill();
- }
- else
- {
- ReleaseRoxieRowSet(sorted);
- }
- inputAlreadySorted = true;
- sequences.kill();
- }
- virtual void prepare(IRoxieInput *input)
- {
- inputAlreadySorted = true;
- curIndex = 0;
- eof = false;
- assertex(sorted.ordinality()==0);
- const void *next = input->nextInGroup();
- if (!next)
- {
- eof = true;
- return;
- }
- loop
- {
- insertHeap(next);
- next = input->nextInGroup();
- if (!next)
- break;
- }
- checkHeap();
- }
- virtual const void * next()
- {
- if (inputAlreadySorted)
- {
- if (sorted.isItem(curIndex))
- {
- return sorted.item(curIndex++);
- }
- else
- return NULL;
- }
- else
- return removeHeap();
- }
- };
- typedef enum {heapSort, insertionSort, quickSort, spillingQuickSort, unknownSort } RoxieSortAlgorithm;
- class CRoxieServerSortActivity : public CRoxieServerActivity
- {
- protected:
- IHThorSortArg &helper;
- ICompare *compare;
- ISortAlgorithm *sorter;
- bool readInput;
- RoxieSortAlgorithm sortAlgorithm;
- unsigned sortFlags;
- public:
- CRoxieServerSortActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, RoxieSortAlgorithm _sortAlgorithm, unsigned _sortFlags)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorSortArg &)basehelper), sortAlgorithm(_sortAlgorithm), sortFlags(_sortFlags)
- {
- compare = helper.queryCompare();
- readInput = false;
- sorter = NULL;
- }
- ~CRoxieServerSortActivity()
- {
- delete sorter;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- switch (sortAlgorithm)
- {
- case heapSort:
- sorter = new CHeapSortAlgorithm(compare);
- break;
- case insertionSort:
- sorter = new CInsertionSortAlgorithm(compare, &ctx->queryRowManager(), activityId);
- break;
- case quickSort:
- sorter = new CQuickSortAlgorithm(compare);
- break;
- case spillingQuickSort:
- sorter = new CSpillingQuickSortAlgorithm(compare, ctx, meta, activityId);
- break;
- case unknownSort:
- sorter = NULL; // create it later....
- break;
- default:
- throwUnexpected();
- break;
- }
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- assertex(!readInput);
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual void reset()
- {
- if (sorter)
- sorter->reset();
- readInput = false;
- CRoxieServerActivity::reset();
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (!readInput)
- {
- if (sortAlgorithm == unknownSort)
- {
- delete sorter;
- sorter = NULL;
- IHThorAlgorithm *sortMethod = static_cast<IHThorAlgorithm *>(helper.selectInterface(TAIalgorithm_1));
- const char *useAlgorithm = sortMethod->queryAlgorithm();
- if (useAlgorithm)
- {
- if (stricmp(useAlgorithm, "quicksort")==0)
- {
- if (sortFlags & TAFstable)
- throw MakeStringException(ROXIE_UNKNOWN_ALGORITHM, "Invalid stable sort algorithm %s requested", useAlgorithm);
- sorter = new CQuickSortAlgorithm(compare);
- }
- else if (stricmp(useAlgorithm, "heapsort")==0)
- sorter = new CHeapSortAlgorithm(compare);
- else if (stricmp(useAlgorithm, "insertionsort")==0)
- sorter = new CInsertionSortAlgorithm(compare, &ctx->queryRowManager(), activityId);
- else
- {
- WARNLOG(ROXIE_UNKNOWN_ALGORITHM, "Ignoring unsupported sort order algorithm '%s', using default", useAlgorithm);
- if (sortFlags & TAFunstable)
- sorter = new CQuickSortAlgorithm(compare);
- else
- sorter = new CHeapSortAlgorithm(compare);
- }
- }
- else
- sorter = new CHeapSortAlgorithm(compare); // shouldn't really happen but there was a vintage of codegen that did not set the flag when algorithm not specified...
- }
- sorter->prepare(input);
- readInput = true;
- }
- const void *ret = sorter->next();
- if (ret)
- processed++;
- else
- {
- sorter->reset();
- readInput = false; // ready for next group
- }
- return ret;
- }
- };
- class CRoxieServerSortActivityFactory : public CRoxieServerActivityFactory
- {
- RoxieSortAlgorithm sortAlgorithm;
- unsigned sortFlags;
- public:
- CRoxieServerSortActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- sortAlgorithm = heapSort;
- sortFlags = TAFstable;
- Owned<IHThorSortArg> sortHelper = (IHThorSortArg *) helperFactory();
- IHThorAlgorithm *sortMethod = static_cast<IHThorAlgorithm *>(sortHelper->selectInterface(TAIalgorithm_1));
- if (sortMethod)
- {
- sortFlags = sortMethod->getAlgorithmFlags();
- if (sortFlags & TAFunstable)
- sortAlgorithm = quickSort;
- if (!(sortFlags & TAFconstant))
- sortAlgorithm = unknownSort;
- else
- {
- const char *useAlgorithm = sortMethod->queryAlgorithm();
- if (useAlgorithm)
- {
- if (stricmp(useAlgorithm, "quicksort")==0)
- {
- if (sortFlags & TAFstable)
- throw MakeStringException(ROXIE_UNKNOWN_ALGORITHM, "Invalid stable sort algorithm %s requested", useAlgorithm);
- sortAlgorithm = quickSort;
- }
- else if (stricmp(useAlgorithm, "spillingquicksort")==0)
- {
- if (sortFlags & TAFstable)
- throw MakeStringException(ROXIE_UNKNOWN_ALGORITHM, "Invalid stable sort algorithm %s requested", useAlgorithm);
- sortAlgorithm = spillingQuickSort;
- }
- else if (stricmp(useAlgorithm, "heapsort")==0)
- sortAlgorithm = heapSort; // NOTE - we do allow UNSTABLE('heapsort') in order to facilitate runtime selection
- else if (stricmp(useAlgorithm, "insertionsort")==0)
- sortAlgorithm = insertionSort;
- else
- {
- WARNLOG(ROXIE_UNKNOWN_ALGORITHM, "Ignoring unsupported sort order algorithm '%s', using default", useAlgorithm);
- if (sortFlags & TAFunstable)
- sortAlgorithm = quickSort;
- else
- sortAlgorithm = heapSort;
- }
- }
- }
- }
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerSortActivity(this, _probeManager, sortAlgorithm, sortFlags);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSortActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerSortActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=====================================================================================================
- class CRoxieServerSortedActivity : public CRoxieServerActivity
- {
- IHThorSortedArg &helper;
- ICompare * compare;
- const void *prev;
- IRangeCompare * stepCompare;
- public:
- CRoxieServerSortedActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorSortedArg &)basehelper)
- {
- prev = NULL;
- compare = helper.queryCompare();
- stepCompare = NULL;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- IInputSteppingMeta * stepMeta = input->querySteppingMeta();
- if (stepMeta)
- stepCompare = stepMeta->queryCompare();
- prev = NULL;
- }
- virtual void reset()
- {
- ReleaseClearRoxieRow(prev);
- CRoxieServerActivity::reset();
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void *ret = input->nextInGroup();
- if (ret && prev && compare->docompare(prev, ret) > 0)
- {
- // MORE - better to give mismatching rows that indexes?
- throw MakeStringException(ROXIE_NOT_SORTED, "SORTED(%u) detected incorrectly sorted rows (row %d, %d))", activityId, processed, processed+1);
- }
- ReleaseRoxieRow(prev);
- prev = ret;
- if (ret)
- {
- LinkRoxieRow(prev);
- processed++;
- }
- return ret;
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void *ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- if (ret && prev && compare->docompare(prev, ret) > 0)
- {
- // MORE - better to give mismatching rows that indexes?
- throw MakeStringException(ROXIE_NOT_SORTED, "SORTED(%u) detected incorrectly sorted rows (row %d, %d))", activityId, processed, processed+1);
- }
- ReleaseRoxieRow(prev);
- prev = ret;
- if (ret)
- {
- LinkRoxieRow(prev);
- processed++;
- }
- return ret;
- }
- IInputSteppingMeta * querySteppingMeta()
- {
- return input->querySteppingMeta();
- }
- virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
- {
- return input->gatherConjunctions(collector);
- }
- virtual void resetEOF()
- {
- input->resetEOF();
- }
- };
- class CRoxieServerSortedActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerSortedActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerSortedActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSortedActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerSortedActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=====================================================================================================
- class CRoxieServerThroughSpillActivity : public CRoxieServerActivity
- {
- /*
- BE VERY CAREFUL - this code is tricky.
- Note that starts and stops (and resets) can occur in strange orders
- The FIRST start OR stop must initialize the activity but only the first START should call the upstream start.
- The last stop should call the upstream stop.
- The first reset should call the upstream reset.
- 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
- not yet had start or stop called - for this to happen init() is called on all outputs on the first start or stop.
- Some outputs may be completely pruned away when used in a GRAPH - these outputs should not receive any start/stop/reset and should be
- ignored in the minIndex calculation
- */
- public:
- IHThorArg &helper;
- unsigned activeOutputs;
- unsigned numOutputs;
- unsigned numOriginalOutputs;
- QueueOf<const void, true> buffer;
- CriticalSection crit;
- CriticalSection crit2;
- unsigned tailIdx;
- unsigned headIdx;
- Owned<IException> error;
- class OutputAdaptor : public CInterface, implements IRoxieInput
- {
- bool eof, eofpending, stopped;
- public:
- CRoxieServerThroughSpillActivity *parent;
- unsigned idx;
- unsigned oid;
- unsigned processed;
- unsigned __int64 totalCycles;
- public:
- IMPLEMENT_IINTERFACE;
- OutputAdaptor()
- {
- parent = NULL;
- oid = 0;
- idx = 0;
- processed = 0;
- totalCycles = 0;
- eofpending = false;
- eof = false;
- stopped = false;
- }
- ~OutputAdaptor()
- {
- if (traceStartStop)
- DBGLOG("%p ~OutputAdaptor %d", this, oid);
- }
- void init()
- {
- if (traceStartStop)
- DBGLOG("%p init Input adaptor %d", this, oid);
- idx = 0;
- processed = 0;
- totalCycles = 0;
- eofpending = false;
- eof = false;
- stopped = false;
- }
- virtual unsigned queryId() const
- {
- return parent->queryId();
- }
- virtual IRoxieServerActivity *queryActivity()
- {
- return parent;
- }
- virtual IIndexReadActivityInfo *queryIndexReadActivity()
- {
- return parent->queryIndexReadActivity();
- }
-
- virtual unsigned __int64 queryTotalCycles() const
- {
- return totalCycles;
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- return 0;
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- return parent->queryInput(idx);
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, parent->ctx->queryDebugContext());
- if (eof)
- return NULL;
- const void *ret = parent->readBuffered(idx, oid);
- #ifdef TRACE_SPLIT
- parent->CTXLOG("Adaptor %d got back %p for record %d", oid, ret, idx);
- #endif
- idx++;
- if (ret)
- {
- processed++;
- eofpending = false;
- }
- else if (eofpending)
- eof = true;
- else
- eofpending = true;
- return ret;
- }
- virtual IOutputMetaData * queryOutputMeta() const
- {
- return parent->queryOutputMeta();
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- // NOTE: it is tempting to move the init() of all output adaptors here. However that is not a good idea,
- // since adaptors that have not yet started or stoppped (but are going to) still need to have been init()'ed
- // for minIndex to give the correct answers
- // therefore, we call init() on all adaptors on receipt of the first start() or stop()
- if (traceStartStop)
- parent->CTXLOG("%p start Input adaptor %d stopped = %d", this, oid, stopped);
- parent->start(oid, parentExtractSize, parentExtract, paused);
- }
- virtual void stop(bool aborting)
- {
- if (traceStartStop)
- parent->CTXLOG("%p stop Input adaptor %d stopped = %d", this, oid, stopped);
- if (!stopped)
- {
- parent->stop(oid, idx, aborting); // NOTE - may call init()
- stopped = true; // parent code relies on stop being called exactly once per adaptor, so make sure it is!
- idx = (unsigned) -1; // causes minIndex not to save rows for me...
- }
- };
- virtual void reset()
- {
- if (traceStartStop)
- parent->CTXLOG("%p reset Input adaptor %d stopped = %d", this, oid, stopped);
- parent->reset(oid, processed);
- processed = 0;
- idx = 0; // value should not be relevant really but this is the safest...
- stopped = false;
- };
- virtual void resetEOF()
- {
- parent->resetEOF();
- }
- virtual void checkAbort()
- {
- parent->checkAbort();
- }
- } *adaptors;
- bool *used;
- unsigned nextFreeOutput()
- {
- unsigned i = numOutputs;
- while (i)
- {
- i--;
- if (!used[i])
- return i;
- }
- throwUnexpected();
- }
- unsigned minIndex(unsigned exceptOid)
- {
- // MORE - yukky code (and slow). Could keep them heapsorted by idx or something
- // this is trying to determine whethwe any of the adaptors will in the future read a given record
- unsigned minIdx = (unsigned) -1;
- for (unsigned i = 0; i < numOutputs; i++)
- {
- if (i != exceptOid && used[i] && adaptors[i].idx < minIdx)
- minIdx = adaptors[i].idx;
- }
- return minIdx;
- }
- void initOutputs()
- {
- activeOutputs = numOutputs;
- for (unsigned i = 0; i < numOriginalOutputs; i++)
- if (used[i])
- adaptors[i].init();
- state = STATEstarting;
- }
- public:
- CRoxieServerThroughSpillActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numOutputs)
- : CRoxieServerActivity(_factory, _probeManager), helper(basehelper), numOutputs(_numOutputs)
- {
- numOriginalOutputs = numOutputs;
- adaptors = new OutputAdaptor[numOutputs];
- used = new bool[numOutputs];
- for (unsigned i = 0; i < numOutputs; i++)
- {
- adaptors[i].parent = this;
- adaptors[i].oid = i;
- used[i] = false;
- }
- tailIdx = 0;
- headIdx = 0;
- activeOutputs = numOutputs;
- }
- ~CRoxieServerThroughSpillActivity()
- {
- delete [] adaptors;
- delete [] used;
- }
- const void *readBuffered(unsigned idx, unsigned oid)
- {
- CriticalBlock b(crit);
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext()); // NOTE - time spent waiting for crit not included here. Is that right?
- if (idx == headIdx) // test once without getting the crit2 sec
- {
- CriticalUnblock b1(crit);
- CriticalBlock b2(crit2);
- if (error)
- {
- throw error.getLink();
- }
- if (idx == headIdx) // test again now that we have it
- {
- try
- {
- const void *row = input->nextInGroup();
- CriticalBlock b3(crit);
- headIdx++;
- if (row) processed++;
- if (activeOutputs==1)
- {
- #ifdef TRACE_SPLIT
- CTXLOG("spill %d optimised return of %p", activityId, row);
- #endif
- return row; // optimization for the case where only one output still active.
- }
- buffer.enqueue(row);
- }
- catch (IException *E)
- {
- #ifdef TRACE_SPLIT
- CTXLOG("spill %d caught exception", activityId);
- #endif
- error.set(E);
- throw;
- }
- catch (...)
- {
- IException *E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught in CRoxieServerThroughSpillActivity::readBuffered");
- error.set(E);
- throw E;
- }
- }
- }
- idx -= tailIdx;
- if (!idx)
- {
- unsigned min = minIndex(oid);
- if (min > tailIdx)
- {
- tailIdx++;
- const void *ret = buffer.dequeue(); // no need to link - last puller
- #ifdef TRACE_SPLIT
- CTXLOG("last puller return of %p", ret);
- #endif
- return ret;
- }
- }
- const void *ret = buffer.item(idx);
- if (ret) LinkRoxieRow(ret);
- #ifdef TRACE_SPLIT
- CTXLOG("standard return of %p", ret);
- #endif
- return ret;
- }
- virtual void start(unsigned oid, unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CriticalBlock b(crit);
- if (error)
- throw error.getLink();
- if (factory)
- factory->noteStarted(oid);
- if (traceStartStop)
- CTXLOG("SPLIT %p: start %d child %d activeOutputs %d numOutputs %d numOriginalOutputs %d state %s", this, activityId, oid, activeOutputs, numOutputs, numOriginalOutputs, queryStateText(state));
- if (state != STATEstarted)
- {
- if (state != STATEstarting)
- initOutputs();
- tailIdx = 0;
- headIdx = 0;
- error.clear();
- try
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- catch (IException *E)
- {
- #ifdef TRACE_SPLIT
- CTXLOG("spill %d caught exception in start", activityId);
- #endif
- error.set(E);
- throw;
- }
- catch (...)
- {
- IException *E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught in CRoxieServerThroughSpillActivity::start");
- error.set(E);
- throw E;
- }
- }
- }
- void stop(unsigned oid, unsigned idx, bool aborting)
- {
- // Note that OutputAdaptor code ensures that stop is not called more than once per adaptor
- CriticalBlock b(crit);
- #ifdef TRACE_STARTSTOP
- if (traceStartStop)
- {
- CTXLOG("SPLIT %p: stop %d child %d activeOutputs %d numOutputs %d numOriginalOutputs %d state %s", this, activityId, oid, activeOutputs, numOutputs, numOriginalOutputs, queryStateText(state));
- if (watchActivityId && watchActivityId==activityId)
- {
- CTXLOG("WATCH: stop %d", activityId);
- }
- }
- #endif
- if (state != STATEstarting && state != STATEstarted)
- initOutputs();
- if (activeOutputs > 1)
- {
- if (tailIdx==idx)
- {
- // Discard all buffered rows that are there purely for this adaptor to read them
- unsigned min = minIndex(oid);
- if (min != (unsigned) -1)
- // what does -1 signify?? No-one wants anything? In which case can't we kill all rows??
- // Should never happen though if there are still some active.
- // there may be a small window where adaptors are blocked on the semaphore...
- {
- #ifdef TRACE_SPLIT
- CTXLOG("%p: Discarding buffered rows from %d to %d for oid %x (%d outputs active)", this, idx, min, oid, activeOutputs);
- #endif
- while (tailIdx < min)
- {
- ReleaseRoxieRow(buffer.dequeue());
- tailIdx++;
- }
- }
- }
- activeOutputs--;
- return;
- }
- #ifdef TRACE_SPLIT
- CTXLOG("%p: All outputs done", this);
- #endif
- activeOutputs = numOutputs;
- CRoxieServerActivity::stop(aborting);
- };
- void reset(unsigned oid, unsigned _processed)
- {
- if (traceStartStop)
- CTXLOG("SPLIT %p: reset %d child %d activeOutputs %d numOutputs %d numOriginalOutputs %d state %s", this, activityId, oid, activeOutputs, numOutputs, numOriginalOutputs, queryStateText(state));
- activeOutputs = numOutputs;
- while (buffer.ordinality())
- ReleaseRoxieRow(buffer.dequeue());
- error.clear();
- if (state != STATEreset) // make sure input is only reset once
- CRoxieServerActivity::reset();
- };
- virtual unsigned __int64 queryLocalCycles() const
- {
- return 0;
- }
- virtual const void *nextInGroup()
- {
- throwUnexpected(); // Internal logic error - we are not anybody's input
- }
- virtual IOutputMetaData * queryOutputMeta() const
- {
- // if (outputMeta)
- // return outputMeta;
- // else
- return input->queryOutputMeta(); // not always known (e.g. disk write - though Gavin _could_ fill it in)
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- if (idx==(unsigned)-1)
- idx = nextFreeOutput(); // MORE - what is this used for?
- assertex(idx < numOriginalOutputs);
- assertex(!used[idx]);
- used[idx] = true;
- return &adaptors[idx];
- }
- virtual void resetOutputsUsed()
- {
- numOutputs = 1;
- activeOutputs = 1;
- // MORE RKC->GH should we be clearing the used array here? anywhere?
- }
- virtual void noteOutputUsed()
- {
- assertex(numOutputs < numOriginalOutputs);
- numOutputs++;
- activeOutputs = numOutputs;
- }
- virtual bool isPassThrough()
- {
- return numOutputs==1;
- }
- };
- class CRoxieServerThroughSpillActivityFactory : public CRoxieServerMultiOutputFactory
- {
- public:
- CRoxieServerThroughSpillActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- Owned<IHThorSpillArg> helper = (IHThorSpillArg *) helperFactory();
- setNumOutputs(helper->getTempUsageCount() + 1);
- }
- CRoxieServerThroughSpillActivityFactory(IQueryFactory &_queryFactory, HelperFactory *_helperFactory, unsigned _numOutputs)
- : CRoxieServerMultiOutputFactory(0, 0, _queryFactory, _helperFactory, TAKsplit)
- {
- setNumOutputs(_numOutputs);
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerThroughSpillActivity(this, _probeManager, numOutputs);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerThroughSpillActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerThroughSpillActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- IRoxieServerActivityFactory *createRoxieServerThroughSpillActivityFactory(IQueryFactory &_queryFactory, HelperFactory *_factory, unsigned _numOutputs)
- {
- return new CRoxieServerThroughSpillActivityFactory(_queryFactory, _factory, _numOutputs);
- }
- //----------------------------------------------------------------------------------------------
- class CRoxieServerSplitActivityFactory : public CRoxieServerMultiOutputFactory
- {
- public:
- CRoxieServerSplitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- Owned<IHThorSplitArg> helper = (IHThorSplitArg *) helperFactory();
- setNumOutputs(helper->numBranches());
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerThroughSpillActivity(this, _probeManager, numOutputs);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSplitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerSplitActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=====================================================================================================
- #define PIPE_BUFSIZE 0x8000
- static IException *createPipeFailureException(const char *cmd, unsigned retcode, IPipeProcess *pipe)
- {
- StringBuffer msg;
- if(pipe->hasError())
- {
- try
- {
- char error[512];
- size32_t sz = pipe->readError(sizeof(error), error);
- if(sz && sz!=(size32_t)-1)
- msg.append(", stderr: '").append(sz, error).append("'");
- }
- catch (IException *e)
- {
- EXCLOG(e, "Error reading pipe stderr");
- e->Release();
- }
- }
- return MakeStringException(ROXIE_PIPE_ERROR, "Pipe process %s returned error %u%s", cmd, retcode, msg.str());
- }
- class CRoxieServerPipeReadActivity : public CRoxieServerActivity
- {
- IHThorPipeReadArg &helper;
- Owned<IPipeProcess> pipe;
- StringAttr pipeCommand;
- Owned<IOutputRowDeserializer> rowDeserializer;
- Owned<IReadRowStream> readTransformer;
- bool groupSignalled;
- public:
- CRoxieServerPipeReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorPipeReadArg &)basehelper)
- {
- groupSignalled = true;
- }
- virtual bool needsAllocator() const { return true; }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- rowDeserializer.setown(rowAllocator->createRowDeserializer(ctx->queryCodeContext()));
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- groupSignalled = true; // i.e. don't start with a NULL row
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- if (!readTransformer)
- readTransformer.setown(createReadRowStream(rowAllocator, rowDeserializer, helper.queryXmlTransformer(), helper.queryCsvTransformer(), helper.queryXmlIteratorPath(), helper.getPipeFlags()));
- openPipe(helper.getPipeProgram());
- }
- virtual void stop(bool aborting)
- {
- CRoxieServerActivity::stop(aborting);
- pipe.clear();
- readTransformer->setStream(NULL);
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- while (!waitForPipe())
- {
- if (!pipe)
- return NULL;
- if (helper.getPipeFlags() & TPFgroupeachrow)
- {
- if (!groupSignalled)
- {
- groupSignalled = true;
- return NULL;
- }
- }
- }
- const void *ret = readTransformer->next();
- assertex(ret != NULL); // if ret can ever be NULL then we need to recode this logic
- processed++;
- groupSignalled = false;
- return ret;
- }
- protected:
- bool waitForPipe()
- {
- if (!pipe)
- return false; // done
- if (!readTransformer->eos())
- return true;
- verifyPipe();
- return false;
- }
- void openPipe(char const * cmd)
- {
- pipeCommand.setown(cmd);
- pipe.setown(createPipeProcess());
- if(!pipe->run(NULL, cmd, ".", false, true, true, 0x10000))
- throw MakeStringException(ROXIE_PIPE_ERROR, "Could not run pipe process %s", cmd);
- Owned<ISimpleReadStream> pipeReader = pipe->getOutputStream();
- readTransformer->setStream(pipeReader.get());
- }
- void verifyPipe()
- {
- if (pipe)
- {
- unsigned err = pipe->wait();
- if(err && !(helper.getPipeFlags() & TPFnofail))
- {
- throw createPipeFailureException(pipeCommand.get(), err, pipe);
- }
- pipe.clear();
- }
- }
- };
- class CRoxieServerPipeThroughActivity : public CRoxieServerActivity, implements IRecordPullerCallback
- {
- IHThorPipeThroughArg &helper;
- RecordPullerThread puller;
- Owned<IPipeProcess> pipe;
- StringAttr pipeCommand;
- InterruptableSemaphore pipeVerified;
- InterruptableSemaphore pipeOpened;
- CachedOutputMetaData inputMeta;
- Owned<IOutputRowSerializer> rowSerializer;
- Owned<IOutputRowDeserializer> rowDeserializer;
- Owned<IPipeWriteXformHelper> writeTransformer;
- Owned<IReadRowStream> readTransformer;
- bool firstRead;
- bool recreate;
- bool inputExhausted;
- bool groupSignalled;
- public:
- CRoxieServerPipeThroughActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorPipeThroughArg &)basehelper), puller(false)
- {
- recreate = helper.recreateEachRow();
- groupSignalled = true;
- firstRead = false;
- inputExhausted = false;
- }
- virtual bool needsAllocator() const { return true; }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- rowSerializer.setown(inputMeta.createRowSerializer(ctx->queryCodeContext(), activityId));
- rowDeserializer.setown(rowAllocator->createRowDeserializer(ctx->queryCodeContext()));
- writeTransformer.setown(createPipeWriteXformHelper(helper.getPipeFlags(), helper.queryXmlOutput(), helper.queryCsvOutput(), rowSerializer));
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- firstRead = true;
- inputExhausted = false;
- groupSignalled = true; // i.e. don't start with a NULL row
- pipeVerified.reinit();
- pipeOpened.reinit();
- writeTransformer->ready();
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- if (!readTransformer)
- readTransformer.setown(createReadRowStream(rowAllocator, rowDeserializer, helper.queryXmlTransformer(), helper.queryCsvTransformer(), helper.queryXmlIteratorPath(), helper.getPipeFlags()));
- if(!recreate)
- openPipe(helper.getPipeProgram());
- puller.start(parentExtractSize, parentExtract, paused, 0, false, ctx); // Pipe does not support preload presently - locks up
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- if (idx)
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
- puller.setInput(this, _in);
- inputMeta.set(_in->queryOutputMeta());
- }
- virtual void stop(bool aborting)
- {
- pipeVerified.interrupt(NULL);
- pipeOpened.interrupt(NULL);
- puller.stop(aborting);
- CRoxieServerActivity::stop(aborting);
- pipe.clear();
- readTransformer->setStream(NULL);
- }
- virtual void reset()
- {
- puller.reset();
- CRoxieServerActivity::reset();
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- while (!waitForPipe())
- {
- if (!pipe)
- return NULL;
- if (helper.getPipeFlags() & TPFgroupeachrow)
- {
- if (!groupSignalled)
- {
- groupSignalled = true;
- return NULL;
- }
- }
- }
- const void *ret = readTransformer->next();
- assertex(ret != NULL); // if ret can ever be NULL then we need to recode this logic
- processed++;
- groupSignalled = false;
- return ret;
- }
- virtual void processRow(const void *row)
- {
- // called from puller thread
- if(recreate)
- openPipe(helper.getNameFromRow(row));
- writeTransformer->writeTranslatedText(row, pipe);
- ReleaseRoxieRow(row);
- if(recreate)
- {
- closePipe();
- pipeVerified.wait();
- }
- }
- virtual void processDone()
- {
- // called from puller thread
- if(recreate)
- {
- inputExhausted = true;
- pipeOpened.signal();
- }
- else
- {
- closePipe();
- pipeVerified.wait();
- }
- }
- virtual void processEOG()
- {
- }
- void processGroup(const ConstPointerArray &)
- {
- throwUnexpected();
- }
- virtual bool fireException(IException *e)
- {
- pipeOpened.interrupt(LINK(e));
- pipeVerified.interrupt(e);
- return true;
- }
- private:
- bool waitForPipe()
- {
- if (firstRead)
- {
- pipeOpened.wait();
- firstRead = false;
- }
- if (!pipe)
- return false; // done
- if (!readTransformer->eos())
- return true;
- verifyPipe();
- if (recreate && !inputExhausted)
- pipeOpened.wait();
- return false;
- }
- void openPipe(char const * cmd)
- {
- pipeCommand.setown(cmd);
- pipe.setown(createPipeProcess());
- if(!pipe->run(NULL, cmd, ".", true, true, true, 0x10000))
- throw MakeStringException(ROXIE_PIPE_ERROR, "Could not run pipe process %s", cmd);
- writeTransformer->writeHeader(pipe);
- Owned<ISimpleReadStream> pipeReader = pipe->getOutputStream();
- readTransformer->setStream(pipeReader.get());
- pipeOpened.signal();
- }
- void closePipe()
- {
- writeTransformer->writeFooter(pipe);
- pipe->closeInput();
- }
- void verifyPipe()
- {
- if (pipe)
- {
- unsigned err = pipe->wait();
- if(err && !(helper.getPipeFlags() & TPFnofail))
- {
- throw createPipeFailureException(pipeCommand.get(), err, pipe);
- }
- pipe.clear();
- pipeVerified.signal();
- }
- }
- };
- class CRoxieServerPipeWriteActivity : public CRoxieServerInternalSinkActivity
- {
- IHThorPipeWriteArg &helper;
- Owned<IPipeProcess> pipe;
- StringAttr pipeCommand;
- CachedOutputMetaData inputMeta;
- Owned<IOutputRowSerializer> rowSerializer;
- Owned<IPipeWriteXformHelper> writeTransformer;
- bool firstRead;
- bool recreate;
- bool inputExhausted;
- public:
- CRoxieServerPipeWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorPipeWriteArg &)basehelper)
- {
- recreate = helper.recreateEachRow();
- firstRead = false;
- inputExhausted = false;
- }
- virtual bool needsAllocator() const { return true; }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- inputMeta.set(input->queryOutputMeta());
- rowSerializer.setown(inputMeta.createRowSerializer(ctx->queryCodeContext(), activityId));
- writeTransformer.setown(createPipeWriteXformHelper(helper.getPipeFlags(), helper.queryXmlOutput(), helper.queryCsvOutput(), rowSerializer));
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- firstRead = true;
- inputExhausted = false;
- writeTransformer->ready();
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- if(!recreate)
- openPipe(helper.getPipeProgram());
- }
- virtual void stop(bool aborting)
- {
- CRoxieServerActivity::stop(aborting);
- pipe.clear();
- }
- virtual void onExecute()
- {
- loop
- {
- const void *row = input->nextInGroup();
- if (!row)
- {
- row = input->nextInGroup();
- if (!row)
- break;
- }
- processed++;
- if(recreate)
- openPipe(helper.getNameFromRow(row));
- writeTransformer->writeTranslatedText(row, pipe);
- ReleaseRoxieRow(row);
- if(recreate)
- closePipe();
- }
- closePipe();
- }
- private:
- void openPipe(char const * cmd)
- {
- pipeCommand.setown(cmd);
- pipe.setown(createPipeProcess());
- if(!pipe->run(NULL, cmd, ".", true, false, true, 0x10000))
- throw MakeStringException(ROXIE_PIPE_ERROR, "Could not run pipe process %s", cmd);
- writeTransformer->writeHeader(pipe);
- }
- void closePipe()
- {
- writeTransformer->writeFooter(pipe);
- pipe->closeInput();
- unsigned err = pipe->wait();
- if(err && !(helper.getPipeFlags() & TPFnofail))
- {
- throw createPipeFailureException(pipeCommand.get(), err, pipe);
- }
- pipe.clear();
- }
- };
- class CRoxieServerPipeReadActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerPipeReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerPipeReadActivity(this, _probeManager);
- }
- };
- class CRoxieServerPipeThroughActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerPipeThroughActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerPipeThroughActivity(this, _probeManager);
- }
- };
- class CRoxieServerPipeWriteActivityFactory : public CRoxieServerInternalSinkFactory
- {
- public:
- CRoxieServerPipeWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
- : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerPipeWriteActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerPipeReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerPipeReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- IRoxieServerActivityFactory *createRoxieServerPipeThroughActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerPipeThroughActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- IRoxieServerActivityFactory *createRoxieServerPipeWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
- {
- return new CRoxieServerPipeWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot);
- }
- //=====================================================================================================
- class CRoxieServerStreamedIteratorActivity : public CRoxieServerActivity
- {
- IHThorStreamedIteratorArg &helper;
- Owned<IRowStream> rows;
- public:
- CRoxieServerStreamedIteratorActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorStreamedIteratorArg &)basehelper)
- {
- }
- ~CRoxieServerStreamedIteratorActivity()
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- rows.setown(helper.createInput());
- }
- virtual void stop(bool aborting)
- {
- if (rows)
- {
- rows->stop();
- rows.clear();
- }
- CRoxieServerActivity::stop(aborting);
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- assertex(rows != NULL);
- const void * next = rows->nextRow();
- if (next)
- processed++;
- return next;
- }
- };
- class CRoxieServerStreamedIteratorActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerStreamedIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerStreamedIteratorActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerStreamedIteratorActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerStreamedIteratorActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=====================================================================================================
- class CRoxieServerFilterActivity : public CRoxieServerLateStartActivity
- {
- IHThorFilterArg &helper;
- bool anyThisGroup;
- IRangeCompare * stepCompare;
- public:
- CRoxieServerFilterActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerLateStartActivity(_factory, _probeManager), helper((IHThorFilterArg &)basehelper)
- {
- anyThisGroup = false;
- stepCompare = NULL;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- anyThisGroup = false;
- CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
- lateStart(parentExtractSize, parentExtract, helper.canMatchAny());
- stepCompare = NULL;
- if (!eof)
- {
- IInputSteppingMeta * stepMeta = input->querySteppingMeta();
- if (stepMeta)
- stepCompare = stepMeta->queryCompare();
- }
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- loop
- {
- const void * ret = input->nextInGroup();
- if (!ret)
- {
- //stop returning two NULLs in a row.
- if (anyThisGroup)
- {
- anyThisGroup = false;
- return NULL;
- }
- ret = input->nextInGroup();
- if (!ret)
- {
- eof = true;
- return NULL; // eof...
- }
- }
- if (helper.isValid(ret))
- {
- anyThisGroup = true;
- processed++;
- return ret;
- }
- ReleaseRoxieRow(ret);
- }
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- //Could assert that this isn't grouped
- // MORE - will need rethinking once we rethink the nextSteppedGE interface for global smart-stepping.
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- loop
- {
- const void * ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- if (!ret)
- {
- eof = true;
- return NULL;
- }
- if (!wasCompleteMatch)
- {
- anyThisGroup = false; // RKC->GH - is this right??
- return ret;
- }
- if (helper.isValid(ret))
- {
- anyThisGroup = true;
- processed++;
- return ret;
- }
- if (!stepExtra.returnMismatches())
- {
- ReleaseRoxieRow(ret);
- return nextInGroup();
- }
- if (stepCompare->docompare(ret, seek, numFields) != 0)
- {
- wasCompleteMatch = false;
- anyThisGroup = false; // WHY?
- return ret;
- }
- ReleaseRoxieRow(ret);
- }
- }
- virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
- {
- return input->gatherConjunctions(collector);
- }
- virtual void resetEOF()
- {
- eof = prefiltered;
- anyThisGroup = false;
- input->resetEOF();
- }
- IInputSteppingMeta * querySteppingMeta()
- {
- return input->querySteppingMeta();
- }
- };
- class CRoxieServerFilterActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerFilterActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerFilterActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerFilterActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerFilterActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=====================================================================================================
- class CRoxieServerFilterGroupActivity : public CRoxieServerLateStartActivity
- {
- IHThorFilterGroupArg &helper;
- unsigned curIndex;
- ConstPointerArray gathered;
- IRangeCompare * stepCompare;
- public:
- CRoxieServerFilterGroupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerLateStartActivity(_factory, _probeManager), helper((IHThorFilterGroupArg &)basehelper)
- {
- curIndex = 0;
- stepCompare = NULL;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
- lateStart(parentExtractSize, parentExtract, helper.canMatchAny());//sets eof
- assertex(eof == !helper.canMatchAny());
- curIndex = 0;
- stepCompare = NULL;
- if (!eof)
- {
- IInputSteppingMeta * inputStepping = input->querySteppingMeta();
- if (inputStepping)
- stepCompare = inputStepping->queryCompare();
- }
- }
- virtual void reset()
- {
- releaseGathered();
- CRoxieServerLateStartActivity::reset();
- }
- inline void releaseGathered()
- {
- while (gathered.isItem(curIndex))
- ReleaseRoxieRow(gathered.item(curIndex++));
- gathered.kill();
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- if (eof)
- return NULL;
- if (gathered.ordinality())
- {
- if (gathered.isItem(curIndex))
- {
- const void * ret = gathered.item(curIndex++);
- processed++;
- return ret;
- }
- curIndex = 0;
- gathered.kill();
- return NULL;
- }
- const void * ret = input->nextInGroup();
- while (ret)
- {
- gathered.append(ret);
- ret = input->nextInGroup();
- }
- unsigned num = gathered.ordinality();
- if (num != 0)
- {
- if (!helper.isValid(num, (const void * *)gathered.getArray()))
- ReleaseRoxieRowSet(gathered); // read next group
- }
- else
- eof = true;
- }
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- if (gathered.ordinality())
- {
- while (gathered.isItem(curIndex))
- {
- const void * ret = gathered.item(curIndex++);
- if (stepCompare->docompare(ret, seek, numFields) >= 0)
- {
- processed++;
- return ret;
- }
- ReleaseRoxieRow(ret);
- }
- curIndex = 0;
- gathered.kill();
- //nextSteppedGE never returns an end of group marker.
- }
- //Not completely sure about this - it could lead the the start of a group being skipped,
- //so the group filter could potentially work on a different group. If so, we'd need to check the
- //next fields were a subset of the grouping fields - more an issue for the group activity.
-
- //MORE: What do we do with wasCompleteMatch? something like the following????
- #if 0
- loop
- {
- const void * ret;
- if (stepExtra.returnMismatches())
- {
- bool matchedCompletely = true;
- ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- if (!wasCompleteMatch)
- return ret;
- }
- else
- ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- #endif
- const void * ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- while (ret)
- {
- gathered.append(ret);
- ret = input->nextInGroup();
- }
- unsigned num = gathered.ordinality();
- if (num != 0)
- {
- if (!helper.isValid(num, (const void * *)gathered.getArray()))
- ReleaseRoxieRowSet(gathered); // read next group
- }
- else
- eof = true;
- return nextUngrouped(this);
- }
- virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
- {
- return input->gatherConjunctions(collector);
- }
- virtual void resetEOF()
- {
- eof = false;
- releaseGathered();
- input->resetEOF();
- }
- IInputSteppingMeta * querySteppingMeta()
- {
- return input->querySteppingMeta();
- }
- };
- class CRoxieServerFilterGroupActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerFilterGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerFilterGroupActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerFilterGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerFilterGroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerSideEffectActivity : public CRoxieServerActivity
- {
- IHThorSideEffectArg &helper;
- CriticalSection ecrit;
- Owned<IException> exception;
- bool executed;
- public:
- CRoxieServerSideEffectActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorSideEffectArg &)basehelper)
- {
- executed = false;
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- CriticalBlock b(ecrit);
- if (exception)
- throw(exception.getLink());
- if (!executed)
- {
- try
- {
- executed = true;
- helper.action();
- }
- catch(IException *E)
- {
- exception.set(E);
- throw;
- }
- }
- return NULL;
- }
- virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
- {
- CriticalBlock b(ecrit);
- if (exception)
- throw(exception.getLink());
- if (!executed)
- {
- try
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- executed = true;
- start(parentExtractSize, parentExtract, false);
- helper.action();
- stop(false);
- }
- catch(IException *E)
- {
- ctx->notifyAbort(E);
- stop(true);
- exception.set(E);
- throw;
- }
- }
- }
- virtual void reset()
- {
- executed = false;
- exception.clear();
- CRoxieServerActivity::reset();
- }
- };
- class CRoxieServerSideEffectActivityFactory : public CRoxieServerActivityFactory
- {
- bool isRoot;
- public:
- CRoxieServerSideEffectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerSideEffectActivity(this, _probeManager);
- }
- virtual bool isSink() const
- {
- return isRoot && !meta.queryOriginal();
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSideEffectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- {
- return new CRoxieServerSideEffectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
- }
- //=================================================================================
- class CRoxieServerActionActivity : public CRoxieServerInternalSinkActivity
- {
- IHThorActionArg &helper;
- public:
- CRoxieServerActionActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorActionArg &)basehelper)
- {
- }
- virtual void onExecute()
- {
- helper.action();
- }
- };
- class CRoxieServerActionActivityFactory : public CRoxieServerInternalSinkFactory
- {
- public:
- CRoxieServerActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
- : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerActionActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
- {
- return new CRoxieServerActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot);
- }
- //=================================================================================
- class CRoxieServerSampleActivity : public CRoxieServerActivity
- {
- IHThorSampleArg &helper;
- unsigned numSamples;
- unsigned numToSkip;
- unsigned whichSample;
- bool anyThisGroup;
- bool eof;
- public:
- CRoxieServerSampleActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorSampleArg &)basehelper)
- {
- numSamples = 0;
- numToSkip = 0;
- whichSample = 0;
- anyThisGroup = false;
- eof = false;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- anyThisGroup = false;
- eof = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- numSamples = helper.getProportion();
- whichSample = helper.getSampleNumber();
- numToSkip = (whichSample ? whichSample-1 : 0);
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- loop
- {
- const void * ret = input->nextInGroup();
- if (!ret)
- {
- //this does work with groups - may or may not be useful...
- //reset the sample for each group.... probably best.
- numToSkip = (whichSample ? whichSample-1 : 0);
- if (anyThisGroup)
- {
- anyThisGroup = false;
- return NULL;
- }
- ret = input->nextInGroup();
- if (!ret)
- {
- eof = true;
- return NULL; // eof...
- }
- }
- if (numToSkip == 0)
- {
- anyThisGroup = true;
- numToSkip = numSamples-1;
- processed++;
- return ret;
- }
- numToSkip--;
- ReleaseRoxieRow(ret);
- }
- }
- };
- class CRoxieServerSampleActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerSampleActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerSampleActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSampleActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerSampleActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerChooseSetsActivity : public CRoxieServerActivity
- {
- IHThorChooseSetsArg &helper;
- unsigned numSets;
- unsigned * setCounts;
- bool done;
- public:
- CRoxieServerChooseSetsActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorChooseSetsArg &)basehelper)
- {
- setCounts = NULL;
- numSets = 0;
- done = false;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- done = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- numSets = helper.getNumSets();
- setCounts = new unsigned[numSets];
- memset(setCounts, 0, sizeof(unsigned)*numSets);
- helper.setCounts(setCounts);
- }
- virtual void reset()
- {
- delete [] setCounts;
- setCounts = NULL;
- CRoxieServerActivity::reset();
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (done)
- return NULL;
- loop
- {
- const void * ret = input->nextInGroup();
- if (!ret)
- {
- ret = input->nextInGroup();
- if (!ret)
- {
- done = true;
- return NULL;
- }
- }
- processed++;
- switch (helper.getRecordAction(ret))
- {
- case 2:
- done = true;
- return ret;
- case 1:
- return ret;
- }
- ReleaseRoxieRow(ret);
- }
- }
- };
- class CRoxieServerChooseSetsActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerChooseSetsActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerChooseSetsActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerChooseSetsActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerChooseSetsActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerChooseSetsExActivity : public CRoxieServerActivity
- {
- protected:
- IHThorChooseSetsExArg &helper;
- unsigned numSets;
- unsigned curIndex;
- unsigned * setCounts;
- count_t * limits;
- bool done;
- ConstPointerArray gathered;
- virtual bool includeRow(const void * row) = 0;
- virtual void calculateSelection() = 0;
- public:
- CRoxieServerChooseSetsExActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorChooseSetsExArg &)basehelper)
- {
- setCounts = NULL;
- limits = NULL;
- done = false;
- curIndex = 0;
- numSets = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- done = false;
- curIndex = 0;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- numSets = helper.getNumSets();
- setCounts = new unsigned[numSets];
- memset(setCounts, 0, sizeof(unsigned)*numSets);
- limits = (count_t *)calloc(sizeof(count_t), numSets);
- helper.getLimits(limits);
- }
- virtual void reset()
- {
- delete [] setCounts;
- setCounts = NULL;
- free(limits);
- limits = NULL;
- while (gathered.isItem(curIndex))
- ReleaseRoxieRow(gathered.item(curIndex++));
- gathered.kill();
- CRoxieServerActivity::reset();
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (gathered.ordinality() == 0)
- {
- curIndex = 0;
- if (!input->nextGroup(gathered))
- {
- done = true;
- return NULL;
- }
- ForEachItemIn(idx1, gathered)
- {
- unsigned category = helper.getCategory(gathered.item(idx1));
- if (category)
- setCounts[category-1]++;
- }
- calculateSelection();
- }
- while (gathered.isItem(curIndex))
- {
- const void * row = gathered.item(curIndex);
- gathered.replace(NULL, curIndex);
- curIndex++;
- if (includeRow(row))
- {
- processed++;
- return row;
- }
- ReleaseRoxieRow(row);
- }
- gathered.kill();
- return NULL;
- }
- };
- class CRoxieServerChooseSetsLastActivity : public CRoxieServerChooseSetsExActivity
- {
- unsigned * numToSkip;
- public:
- CRoxieServerChooseSetsLastActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager) : CRoxieServerChooseSetsExActivity(_factory, _probeManager)
- {
- numToSkip = NULL;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerChooseSetsExActivity::start(parentExtractSize, parentExtract, paused);
- numToSkip = (unsigned *)calloc(sizeof(unsigned), numSets);
- }
- virtual void reset()
- {
- free(numToSkip);
- numToSkip = NULL;
- CRoxieServerChooseSetsExActivity::reset();
- }
- protected:
- virtual void calculateSelection()
- {
- for (unsigned idx=0; idx < numSets; idx++)
- {
- if (setCounts[idx] < limits[idx])
- numToSkip[idx] = 0;
- else
- numToSkip[idx] = (unsigned)(setCounts[idx] - limits[idx]);
- }
- }
- virtual bool includeRow(const void * row)
- {
- unsigned category = helper.getCategory(row);
- if (category)
- {
- if (numToSkip[category-1] == 0)
- return true;
- numToSkip[category-1]--;
- }
- return false;
- }
- };
- class CRoxieServerChooseSetsEnthActivity : public CRoxieServerChooseSetsExActivity
- {
- count_t * counter;
- public:
- CRoxieServerChooseSetsEnthActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager) : CRoxieServerChooseSetsExActivity(_factory, _probeManager)
- {
- counter = NULL;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerChooseSetsExActivity::start(parentExtractSize, parentExtract, paused);
- counter = (count_t *)calloc(sizeof(count_t), numSets);
- }
- virtual void reset()
- {
- free(counter);
- counter = NULL;
- CRoxieServerChooseSetsExActivity::reset();
- }
- protected:
- virtual void calculateSelection()
- {
- }
- virtual bool includeRow(const void * row)
- {
- unsigned category = helper.getCategory(row);
- if (category)
- {
- assertex(category <= numSets);
- counter[category-1] += limits[category-1];
- if(counter[category-1] >= setCounts[category-1])
- {
- counter[category-1] -= setCounts[category-1];
- return true;
- }
- }
- return false;
- }
- };
- class CRoxieServerChooseSetsEnthActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerChooseSetsEnthActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerChooseSetsEnthActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerChooseSetsEnthActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerChooseSetsEnthActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- class CRoxieServerChooseSetsLastActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerChooseSetsLastActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerChooseSetsLastActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerChooseSetsLastActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerChooseSetsLastActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerEnthActivity : public CRoxieServerActivity
- {
- IHThorEnthArg &helper;
- unsigned __int64 numerator;
- unsigned __int64 denominator;
- unsigned __int64 counter;
- bool eof;
- public:
- CRoxieServerEnthActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorEnthArg &)basehelper)
- {
- eof = false;
- numerator = denominator = counter = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- numerator = helper.getProportionNumerator();
- denominator = helper.getProportionDenominator();
- if(denominator == 0) denominator = 1; //MORE: simplest way to avoid disaster in this case
- counter = (helper.getSampleNumber()-1) * greatestCommonDivisor(numerator, denominator);
- if (counter >= denominator)
- counter %= denominator;
- }
- inline bool wanted()
- {
- counter += numerator;
- if(counter >= denominator)
- {
- counter -= denominator;
- return true;
- }
- return false;
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- const void * ret;
- loop
- {
- ret = input->nextInGroup();
- if(!ret) //end of group
- ret = input->nextInGroup();
- if(!ret) //eof
- {
- eof = true;
- return ret;
- }
- if (wanted())
- return ret;
- ReleaseRoxieRow(ret);
- }
- }
- };
- class CRoxieServerEnthActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerEnthActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerEnthActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerEnthActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerEnthActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerAggregateActivity : public CRoxieServerActivity
- {
- IHThorAggregateArg &helper;
- bool eof;
- bool isInputGrouped;
- bool abortEarly;
- public:
- CRoxieServerAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorAggregateArg &)basehelper)
- {
- eof = false;
- isInputGrouped = false;
- abortEarly = false;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- isInputGrouped = input->queryOutputMeta()->isGrouped(); // could be done earlier, in setInput?
- abortEarly = !isInputGrouped && (factory->getKind() == TAKexistsaggregate); // ditto
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- const void * next = input->nextInGroup();
- if (!next && isInputGrouped)
- {
- eof = true;
- return NULL;
- }
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t finalSize = helper.clearAggregate(rowBuilder);
- if (next)
- {
- finalSize = helper.processFirst(rowBuilder, next);
- ReleaseRoxieRow(next);
- if (!abortEarly)
- {
- loop
- {
- next = input->nextInGroup();
- if (!next)
- break;
- finalSize = helper.processNext(rowBuilder, next);
- ReleaseRoxieRow(next);
- }
- }
- }
- if (!isInputGrouped) // either read all, or aborted early
- eof = true;
- processed++;
- return rowBuilder.finalizeRowClear(finalSize);
- }
- };
- class CRoxieServerAggregateActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerAggregateActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- typedef unsigned t_hashPrefix;
- class CRoxieServerHashAggregateActivity : public CRoxieServerActivity
- {
- IHThorHashAggregateArg &helper;
- RowAggregator aggregated;
- bool eof;
- bool gathered;
- public:
- CRoxieServerHashAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorHashAggregateArg &)basehelper), aggregated(helper, helper)
- {
- eof = false;
- gathered = false;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- gathered = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- aggregated.start(rowAllocator);
- }
- virtual void reset()
- {
- aggregated.reset();
- CRoxieServerActivity::reset();
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- if (!gathered)
- {
- loop
- {
- const void * next = input->nextInGroup();
- if (!next)
- {
- next = input->nextInGroup();
- if (!next)
- break;
- }
- aggregated.addRow(next);
- ReleaseRoxieRow(next);
- }
- gathered = true;
- }
- Owned<AggregateRowBuilder> next = aggregated.nextResult();
- if (next)
- {
- processed++;
- return next->finalizeRowClear();
- }
- eof = true;
- return NULL;
- }
- };
- class CRoxieServerHashAggregateActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerHashAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerHashAggregateActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerHashAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerHashAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerDegroupActivity : public CRoxieServerActivity
- {
- IHThorDegroupArg &helper;
- bool eof;
- public:
- CRoxieServerDegroupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorDegroupArg &)basehelper)
- {
- eof = false;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- const void * ret = input->nextInGroup();
- if (!ret)
- ret = input->nextInGroup();
- if (ret)
- processed++;
- else
- eof = true;
- return ret;
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- const void * ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- if (ret)
- processed++;
- else
- eof = true;
- return ret;
- }
- virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
- {
- return input->gatherConjunctions(collector);
- }
- virtual void resetEOF()
- {
- eof = false;
- input->resetEOF();
- }
- IInputSteppingMeta * querySteppingMeta()
- {
- return input->querySteppingMeta();
- }
- };
- class CRoxieServerDegroupActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerDegroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerDegroupActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerDegroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerDegroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerSpillReadActivity : public CRoxieServerActivity
- {
- IHThorDiskReadArg &helper;
- bool needTransform;
- bool eof;
- bool anyThisGroup;
- unsigned __int64 rowLimit;
- unsigned __int64 choosenLimit;
- public:
- CRoxieServerSpillReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorDiskReadArg &)basehelper)
- {
- needTransform = helper.needTransform();
- rowLimit = (unsigned __int64) -1;
- choosenLimit = 0;
- eof = false;
- anyThisGroup = false;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- anyThisGroup = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- if (helper.canMatchAny())
- eof = false;
- else
- eof = true;
- choosenLimit = helper.getChooseNLimit();
- rowLimit = helper.getRowLimit();
- helper.setCallback(NULL); // members should not be called - change if they are
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- if (processed==choosenLimit)
- {
- eof = true;
- return NULL;
- }
- if (needTransform)
- {
- loop
- {
- const void *in = input->nextInGroup();
- if (!in)
- {
- if (anyThisGroup)
- {
- anyThisGroup = false;
- return NULL;
- }
- in = input->nextInGroup();
- if (!in)
- {
- eof = true;
- return NULL; // eof...
- }
- }
- unsigned outSize;
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- try
- {
- outSize = helper.transform(rowBuilder, in);
- ReleaseRoxieRow(in);
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- if (outSize)
- {
- anyThisGroup = true;
- processed++;
- if (processed==rowLimit)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d line = %d", activityId, __LINE__);
- helper.onLimitExceeded();
- }
- return rowBuilder.finalizeRowClear(outSize);
- }
- }
- }
- else
- {
- const void *ret = input->nextInGroup();
- if (ret)
- {
- processed++;
- if (processed==rowLimit)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d line = %d", activityId, __LINE__);
- ReleaseClearRoxieRow(ret);
- helper.onLimitExceeded(); // should not return
- throwUnexpected();
- }
- }
- return ret;
- }
- }
- };
- class CRoxieServerSpillReadActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerSpillReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerSpillReadActivity(this, _probeManager);
- }
- virtual void addDependency(unsigned source, ThorActivityKind sourceKind, unsigned sourceIdx, int controlId, const char *edgeId)
- {
- if (sourceKind==TAKspill || sourceKind==TAKdiskwrite) // Bit of a hack - codegen probably should differentiate
- setInput(0, source, sourceIdx);
- else
- CRoxieServerActivityFactory::addDependency(source, kind, sourceIdx, controlId, edgeId);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSpillReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerSpillReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerSpillWriteActivity : public CRoxieServerActivity
- {
- public:
- CRoxieServerSpillWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- }
- ~CRoxieServerSpillWriteActivity()
- {
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- return input->nextInGroup();
- }
- };
- //==================================================================================
- class CRoxieServerDiskWriteActivity : public CRoxieServerInternalSinkActivity, implements IRoxiePublishCallback
- {
- protected:
- Owned<IExtRowWriter> outSeq;
- Owned<IOutputRowSerializer> rowSerializer;
- Linked<IFileIOStream> diskout;
- bool blockcompressed;
- bool extend;
- bool overwrite;
- bool encrypted;
- bool grouped;
- IHThorDiskWriteArg &helper;
- StringBuffer lfn; // logical filename
- CachedOutputMetaData diskmeta;
- Owned<IRoxieWriteHandler> writer;
- unsigned __int64 uncompressedBytesWritten;
- void updateWorkUnitResult(unsigned __int64 reccount)
- {
- assertex(writer);
- // MORE - a lot of this is common with hthor
- if(lfn.length()) //this is required as long as temp files don't get a name which can be stored in the WU and automatically deleted by the WU
- {
- WorkunitUpdate wu = ctx->updateWorkUnit();
- if (wu)
- {
- unsigned flags = helper.getFlags();
- WUFileKind fileKind;
- if (TDXtemporary & flags)
- fileKind = WUFileTemporary;
- else if(TDXjobtemp & flags)
- fileKind = WUFileJobOwned;
- else if(TDWowned & flags)
- fileKind = WUFileOwned;
- else
- fileKind = WUFileStandard;
- StringArray clusters;
- writer->getClusters(clusters);
- wu->addFile(lfn.str(), &clusters, helper.getTempUsageCount(), fileKind, NULL);
- if (!(flags & TDXtemporary) && helper.getSequence() >= 0)
- {
- Owned<IWUResult> result = wu->updateResultBySequence(helper.getSequence());
- if (result)
- {
- result->setResultTotalRowCount(reccount);
- result->setResultStatus(ResultStatusCalculated);
- if (helper.getFlags() & TDWresult)
- result->setResultFilename(lfn.str());
- else
- result->setResultLogicalName(lfn.str());
- }
- }
- }
- }
- }
- void resolve()
- {
- const char * rawLogicalName = helper.getFileName();
- assertex(rawLogicalName);
- assertex((helper.getFlags() & TDXtemporary) == 0);
- StringArray clusters;
- unsigned clusterIdx = 0;
- while(true)
- {
- char const * cluster = helper.queryCluster(clusterIdx);
- if(!cluster)
- break;
- clusters.append(cluster);
- clusterIdx++;
- }
- if (clusters.length())
- {
- if (extend)
- throw MakeStringException(0, "Cannot combine EXTEND and CLUSTER flags on disk write of file %s", rawLogicalName);
- }
- else
- {
- if (roxieName.length())
- clusters.append(roxieName.str());
- else
- clusters.append(".");
- }
- writer.setown(ctx->createLFN(rawLogicalName, overwrite, extend, clusters)); // MORE - if there's a workunit, use if for scope.
- // MORE - need to check somewhere that single part if it's an existing file or an external one...
- }
- public:
- CRoxieServerDiskWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorDiskWriteArg &)basehelper)
- {
- extend = ((helper.getFlags() & TDWextend) != 0);
- overwrite = ((helper.getFlags() & TDWoverwrite) != 0);
- grouped = false; // don't think we need to support it...
- diskmeta.set(helper.queryDiskRecordSize());
- blockcompressed = (((helper.getFlags() & TDWnewcompress) != 0) || (((helper.getFlags() & TDXcompress) != 0) && (diskmeta.getFixedSize() >= MIN_ROWCOMPRESS_RECSIZE))); //always use new compression
- encrypted = false; // set later
- uncompressedBytesWritten = 0;
- }
- ~CRoxieServerDiskWriteActivity()
- {
- }
- virtual bool needsAllocator() const
- {
- return true;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
-
- resolve();
- Owned<IFileIO> io;
- void *ekey;
- size32_t ekeylen;
- helper.getEncryptKey(ekeylen, ekey);
- Owned<ICompressor> ecomp;
- if (ekeylen!=0)
- {
- ecomp.setown(createAESCompressor256(ekeylen,ekey));
- memset(ekey,0,ekeylen);
- rtlFree(ekey);
- encrypted = true;
- blockcompressed = true;
- }
- if(blockcompressed)
- io.setown(createCompressedFileWriter(writer->queryFile(), (diskmeta.isFixedSize() ? diskmeta.getFixedSize() : 0), extend, true, ecomp));
- else
- io.setown(writer->queryFile()->open(extend ? IFOwrite : IFOcreate));
- if(!io)
- throw MakeStringException(errno, "Failed to create%s file %s for writing", (encrypted ? " encrypted" : (blockcompressed ? " compressed" : "")), writer->queryFile()->queryFilename());
- diskout.setown(createBufferedIOStream(io));
- if(extend)
- diskout->seek(0, IFSend);
- rowSerializer.setown(input->queryOutputMeta()->createRowSerializer(ctx->queryCodeContext(), activityId));
- bool tallycrc = !factory->queryQueryFactory().getDebugValueBool("skipFileFormatCrcCheck", false) && !(helper.getFlags() & TDRnocrccheck);
- outSeq.setown(createRowWriter(diskout, rowSerializer, rowAllocator, grouped, tallycrc, true ));
- }
- virtual void stop(bool aborting)
- {
- if (aborting)
- {
- if (writer)
- writer->finish(false, this);
- }
- else
- {
- outSeq->flush();
- updateWorkUnitResult(processed);
- uncompressedBytesWritten = outSeq->getPosition();
- writer->finish(true, this);
- }
- writer.clear();
- CRoxieServerActivity::stop(aborting);
- }
- virtual void reset()
- {
- CRoxieServerActivity::reset();
- diskout.clear();
- outSeq.clear();
- writer.clear();
- uncompressedBytesWritten = 0;
- }
- virtual void onExecute()
- {
- loop
- {
- const void *nextrec = input->nextInGroup();
- if (!nextrec)
- {
- nextrec = input->nextInGroup();
- if (!nextrec)
- break;
- }
- processed++;
- outSeq->putRow(nextrec);
- }
- }
- virtual void setFileProperties(IFileDescriptor *desc) const
- {
- IPropertyTree &partProps = desc->queryPart(0)->queryProperties(); //properties of the first file part.
- IPropertyTree &fileProps = desc->queryProperties(); // properties of the logical file
- if (blockcompressed)
- {
- // caller has already set @size from file size...
- fileProps.setPropBool("@blockCompressed", true);
- partProps.setPropInt64("@compressedSize", partProps.getPropInt64("@size", 0)); // MORE should this be on logical too?
- fileProps.setPropInt64("@size", uncompressedBytesWritten);
- partProps.setPropInt64("@size", uncompressedBytesWritten);
- }
- if (encrypted)
- fileProps.setPropBool("@encrypted", true);
- fileProps.setPropInt64("@recordCount", processed);
- unsigned flags = helper.getFlags();
- if (flags & TDWpersist)
- fileProps.setPropBool("@persistent", true);
- if (grouped)
- fileProps.setPropBool("@grouped", true);
- if (flags & (TDWowned|TDXjobtemp|TDXtemporary))
- fileProps.setPropBool("@owned", true);
- if (flags & TDWresult)
- fileProps.setPropBool("@result", true);
- IConstWorkUnit *workUnit = ctx->queryWorkUnit();
- if (workUnit)
- {
- SCMStringBuffer owner, wuid, job;
- fileProps.setProp("@owner", workUnit->getUser(owner).str());
- fileProps.setProp("@workunit", workUnit->getWuid(wuid).str());
- fileProps.setProp("@job", workUnit->getJobName(job).str());
- }
- setExpiryTime(fileProps, helper.getExpiryDays());
- if (flags & TDWupdate)
- {
- unsigned eclCRC;
- unsigned __int64 totalCRC;
- helper.getUpdateCRCs(eclCRC, totalCRC);
- fileProps.setPropInt("@eclCRC", eclCRC);
- fileProps.setPropInt64("@totalCRC", totalCRC);
- }
- fileProps.setPropInt("@formatCrc", helper.getFormatCrc());
- IRecordSize * inputMeta = input->queryOutputMeta();
- if ((inputMeta->isFixedSize()) && !isOutputTransformed())
- fileProps.setPropInt("@recordSize", inputMeta->getFixedSize() + (grouped ? 1 : 0));
- const char *recordECL = helper.queryRecordECL();
- if (recordECL && *recordECL)
- fileProps.setProp("ECL", recordECL);
- }
- virtual IUserDescriptor *queryUserDescriptor() const
- {
- IConstWorkUnit *workUnit = ctx->queryWorkUnit();
- if (workUnit)
- return workUnit->queryUserDescriptor();
- else
- return NULL;
- }
- virtual bool isOutputTransformed() const { return false; }
- };
- //=================================================================================
- class CRoxieServerCsvWriteActivity : public CRoxieServerDiskWriteActivity
- {
- IHThorCsvWriteArg &csvHelper;
- CSVOutputStream csvOutput;
- public:
- CRoxieServerCsvWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerDiskWriteActivity(_factory, _probeManager), csvHelper(static_cast<IHThorCsvWriteArg &>(helper))
- {
- csvOutput.init(csvHelper.queryCsvParameters(), false);
- }
- virtual void onExecute()
- {
- const char * header = csvHelper.queryCsvParameters()->queryHeader();
- if (header)
- {
- csvOutput.beginLine();
- csvOutput.writeHeaderLn(strlen(header), header);
- diskout->write(csvOutput.length(), csvOutput.str());
- }
- loop
- {
- const void *nextrec = input->nextInGroup();
- if (!nextrec)
- {
- nextrec = input->nextInGroup();
- if (!nextrec)
- break;
- }
- processed++;
- csvOutput.beginLine();
- csvHelper.writeRow((const byte *)nextrec, &csvOutput);
- csvOutput.endLine();
- diskout->write(csvOutput.length(), csvOutput.str());
- ReleaseRoxieRow(nextrec);
- }
- const char * footer = csvHelper.queryCsvParameters()->queryFooter();
- if (footer)
- {
- csvOutput.beginLine();
- csvOutput.writeHeaderLn(strlen(footer), footer);
- diskout->write(csvOutput.length(), csvOutput.str());
- }
- }
- virtual void setFileProperties(IFileDescriptor *desc) const
- {
- CRoxieServerDiskWriteActivity::setFileProperties(desc);
- IPropertyTree &props = desc->queryProperties();
- props.setProp("@format","utf8n");
- ICsvParameters *csvParameters = csvHelper.queryCsvParameters();
- StringBuffer separator;
- const char *s = csvParameters->querySeparator(0);
- while (*s)
- {
- if (',' == *s)
- separator.append("\\,");
- else
- separator.append(*s);
- ++s;
- }
- props.setProp("@csvSeparate", separator.str());
- props.setProp("@csvQuote", csvParameters->queryQuote(0));
- props.setProp("@csvTerminate", csvParameters->queryTerminator(0));
- }
- virtual bool isOutputTransformed() const { return true; }
- };
- class CRoxieServerXmlWriteActivity : public CRoxieServerDiskWriteActivity
- {
- IHThorXmlWriteArg &xmlHelper;
- StringAttr rowTag;
- public:
- CRoxieServerXmlWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerDiskWriteActivity(_factory, _probeManager), xmlHelper(static_cast<IHThorXmlWriteArg &>(helper))
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerDiskWriteActivity::start(parentExtractSize, parentExtract, paused);
- const char * path = xmlHelper.queryIteratorPath();
- if (!path)
- rowTag.set("Row");
- else
- {
- if (*path == '/') path++;
- if (strchr(path, '/')) UNIMPLEMENTED; // more what do we do with /mydata/row
- rowTag.set(path);
- }
- }
- virtual void onExecute()
- {
- const char * header = xmlHelper.queryHeader();
- if (!header) header = "<Dataset>\n";
- diskout->write(strlen(header), header);
- CommonXmlWriter xmlOutput(xmlHelper.getXmlFlags());
- loop
- {
- OwnedConstRoxieRow nextrec = input->nextInGroup();
- if (!nextrec)
- {
- nextrec.setown(input->nextInGroup());
- if (!nextrec)
- break;
- }
- processed++;
- xmlOutput.clear().outputBeginNested(rowTag, false);
- xmlHelper.toXML((const byte *)nextrec.get(), xmlOutput);
- xmlOutput.outputEndNested(rowTag);
- diskout->write(xmlOutput.length(), xmlOutput.str());
- }
- const char * footer = xmlHelper.queryFooter();
- if (!footer) footer = "</Dataset>\n";
- diskout->write(strlen(footer), footer);
- }
- virtual void reset()
- {
- CRoxieServerDiskWriteActivity::reset();
- rowTag.clear();
- }
- virtual void setFileProperties(IFileDescriptor *desc) const
- {
- CRoxieServerDiskWriteActivity::setFileProperties(desc);
- desc->queryProperties().setProp("@format","utf8n");
- desc->queryProperties().setProp("@rowTag",rowTag.get());
- }
- virtual bool isOutputTransformed() const { return true; }
- };
- class CRoxieServerDiskWriteActivityFactory : public CRoxieServerMultiOutputFactory
- {
- bool isRoot;
- public:
- CRoxieServerDiskWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
- {
- Owned<IHThorDiskWriteArg> helper = (IHThorDiskWriteArg *) helperFactory();
- setNumOutputs(helper->getTempUsageCount());
- if (_kind!=TAKdiskwrite)
- assertex(numOutputs == 0);
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- switch (numOutputs)
- {
- case 0:
- switch (kind)
- {
- case TAKdiskwrite: return new CRoxieServerDiskWriteActivity(this, _probeManager);
- case TAKcsvwrite: return new CRoxieServerCsvWriteActivity(this, _probeManager);
- case TAKxmlwrite: return new CRoxieServerXmlWriteActivity(this, _probeManager);
- };
- throwUnexpected();
- case 1:
- return new CRoxieServerSpillWriteActivity(this, _probeManager);
- default:
- return new CRoxieServerThroughSpillActivity(this, _probeManager, numOutputs);
- }
- }
- virtual bool isSink() const
- {
- return numOutputs == 0; // MORE - check with Gavin if this is right if not a temp but reread in same job...
- }
- };
- IRoxieServerActivityFactory *createRoxieServerDiskWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- {
- return new CRoxieServerDiskWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
- }
- //=================================================================================
- class CRoxieServerIndexWriteActivity : public CRoxieServerInternalSinkActivity, implements IRoxiePublishCallback
- {
- IHThorIndexWriteArg &helper;
- bool overwrite;
- Owned<ClusterWriteHandler> clusterHandler;
- Owned<IRoxieWriteHandler> writer;
- unsigned __int64 reccount;
- unsigned int fileCrc;
- StringBuffer filename;
- void updateWorkUnitResult()
- {
- if(filename.length()) //this is required as long as temp files don't get a name which can be stored in the WU and automatically deleted by the WU
- {
- WorkunitUpdate wu = ctx->updateWorkUnit();
- if (wu)
- {
- if (!(helper.getFlags() & TDXtemporary) && helper.getSequence() >= 0)
- {
- Owned<IWUResult> result = wu->updateResultBySequence(helper.getSequence());
- if (result)
- {
- result->setResultTotalRowCount(reccount);
- result->setResultStatus(ResultStatusCalculated);
- result->setResultLogicalName(filename.str());
- }
- }
- if(clusterHandler)
- clusterHandler->finish(writer->queryFile());
- }
- CTXLOG("Created roxie index file %s", filename.str());
- }
- }
- virtual void resolve()
- {
- StringArray clusters;
- unsigned clusterIdx = 0;
- while(true)
- {
- char const * cluster = helper.queryCluster(clusterIdx);
- if(!cluster)
- break;
- clusters.append(cluster);
- clusterIdx++;
- }
- if (roxieName.length())
- clusters.append(roxieName.str());
- else
- clusters.append(".");
- writer.setown(ctx->createLFN(helper.getFileName(), overwrite, false, clusters)); // MORE - if there's a workunit, use if for scope.
- filename.set(writer->queryFile()->queryFilename());
- if (writer->queryFile()->exists())
- {
- if (overwrite)
- {
- CTXLOG("Removing existing %s from DFS",filename.str());
- writer->queryFile()->remove();
- }
- else
- throw MakeStringException(99, "Cannot write index file %s, file already exists (missing OVERWRITE attribute?)", filename.str());
- }
- }
- void buildUserMetadata(Owned<IPropertyTree> & metadata)
- {
- size32_t nameLen;
- char * nameBuff;
- size32_t valueLen;
- char * valueBuff;
- unsigned idx = 0;
- while(helper.getIndexMeta(nameLen, nameBuff, valueLen, valueBuff, idx++))
- {
- StringBuffer name(nameLen, nameBuff);
- StringBuffer value(valueLen, valueBuff);
- if(*nameBuff == '_' && strcmp(name, "_nodeSize") != 0)
- throw MakeStringException(0, "Invalid name %s in user metadata for index %s (names beginning with underscore are reserved)", name.str(), helper.getFileName());
- if(!validateXMLTag(name.str()))
- throw MakeStringException(0, "Invalid name %s in user metadata for index %s (not legal XML element name)", name.str(), helper.getFileName());
- if(!metadata)
- metadata.setown(createPTree("metadata"));
- metadata->setProp(name.str(), value.str());
- }
- }
- void buildLayoutMetadata(Owned<IPropertyTree> & metadata)
- {
- if(!metadata)
- metadata.setown(createPTree("metadata"));
- metadata->setProp("_record_ECL", helper.queryRecordECL());
- void * layoutMetaBuff;
- size32_t layoutMetaSize;
- if(helper.getIndexLayout(layoutMetaSize, layoutMetaBuff))
- {
- metadata->setPropBin("_record_layout", layoutMetaSize, layoutMetaBuff);
- rtlFree(layoutMetaBuff);
- }
- }
- public:
- CRoxieServerIndexWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper(static_cast<IHThorIndexWriteArg &>(basehelper))
- {
- overwrite = ((helper.getFlags() & TIWoverwrite) != 0);
- reccount = 0;
- }
- ~CRoxieServerIndexWriteActivity()
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- resolve();
- }
- virtual void onExecute()
- {
- bool isVariable = helper.queryDiskRecordSize()->isVariableSize();
- size32_t maxDiskRecordSize;
- if (isVariable)
- maxDiskRecordSize = 0x8000;
- else
- {
- maxDiskRecordSize = helper.queryDiskRecordSize()->getFixedSize();
- if (maxDiskRecordSize > 0x8000)
- throw MakeStringException(99, "Index minimum record length (%d) exceeds 32k internal limit", maxDiskRecordSize);
- }
- OwnedMalloc<char> rowBuffer(maxDiskRecordSize, true);
- unsigned __int64 fileSize = 0;
- fileCrc = -1;
- if (helper.getDatasetName())
- {
- Owned<const IResolvedFile> dsFileInfo = resolveLFN(helper.getFileName(), false);
- if (dsFileInfo)
- {
- fileSize = dsFileInfo->getFileSize();
- }
- }
- {
- Owned<IFileIO> io;
- try
- {
- io.setown(writer->queryFile()->open(IFOcreate));
- }
- catch(IException * e)
- {
- e->Release();
- clearKeyStoreCache(false);
- io.setown(writer->queryFile()->open(IFOcreate));
- }
- if(!io)
- throw MakeStringException(errno, "Failed to create file %s for writing", filename.str());
- Owned<IFileIOStream> out = createIOStream(io);
- unsigned flags = COL_PREFIX | HTREE_FULLSORT_KEY;
- if (helper.getFlags() & TIWrowcompress)
- flags |= HTREE_COMPRESSED_KEY|HTREE_QUICK_COMPRESSED_KEY;
- else if (!(helper.getFlags() & TIWnolzwcompress))
- flags |= HTREE_COMPRESSED_KEY;
- if (isVariable)
- flags |= HTREE_VARSIZE;
- Owned<IPropertyTree> metadata;
- buildUserMetadata(metadata);
- buildLayoutMetadata(metadata);
- unsigned nodeSize = metadata ? metadata->getPropInt("_nodeSize", NODESIZE) : NODESIZE;
- Owned<IKeyBuilder> builder = createKeyBuilder(out, flags, maxDiskRecordSize, fileSize, nodeSize, helper.getKeyedSize(), 0);
- class BcWrapper : implements IBlobCreator
- {
- IKeyBuilder *builder;
- public:
- BcWrapper(IKeyBuilder *_builder) : builder(_builder) {}
- virtual unsigned __int64 createBlob(size32_t size, const void * ptr)
- {
- return builder->createBlob(size, (const char *) ptr);
- }
- } bc(builder);
- // Loop thru the results
- loop
- {
- OwnedConstRoxieRow nextrec(input->nextInGroup());
- if (!nextrec)
- {
- nextrec.setown(input->nextInGroup());
- if (!nextrec)
- break;
- }
- try
- {
- unsigned __int64 fpos;
- RtlStaticRowBuilder rowBuilder(rowBuffer, maxDiskRecordSize);
- size32_t thisSize = helper.transform(rowBuilder, nextrec, &bc, fpos);
- builder->processKeyData(rowBuffer, fpos, thisSize);
- }
- catch(IException * e)
- {
- throw makeWrappedException(e);
- }
- reccount++;
- }
- if(metadata)
- builder->finish(metadata,&fileCrc);
- else
- builder->finish(&fileCrc);
- }
- }
- virtual void stop(bool aborting)
- {
- if (writer)
- {
- if (!aborting)
- updateWorkUnitResult();
- writer->finish(!aborting, this);
- writer.clear();
- }
- CRoxieServerActivity::stop(aborting);
- }
- virtual void reset()
- {
- CRoxieServerActivity::reset();
- writer.clear();
- }
- //interface IRoxiePublishCallback
- virtual void setFileProperties(IFileDescriptor *desc) const
- {
- IPropertyTree &partProps = desc->queryPart(0)->queryProperties(); //properties of the first file part.
- IPropertyTree &fileProps = desc->queryProperties(); // properties of the logical file
- // Now publish to name services
- StringBuffer dir,base;
- offset_t indexFileSize = writer->queryFile()->size();
- if(clusterHandler)
- clusterHandler->splitPhysicalFilename(dir, base);
- else
- splitFilename(filename.str(), &dir, &dir, &base, &base);
- desc->setDefaultDir(dir.str());
- //properties of the first file part.
- Owned<IPropertyTree> attrs;
- if(clusterHandler)
- attrs.setown(createPTree("Part")); // clusterHandler is going to set attributes
- else
- {
- // add cluster
- StringBuffer mygroupname;
- desc->setNumParts(1);
- desc->setPartMask(base.str());
- attrs.set(&desc->queryPart(0)->queryProperties());
- }
- attrs->setPropInt64("@size", indexFileSize);
- CDateTime createTime, modifiedTime, accessedTime;
- writer->queryFile()->getTime(&createTime, &modifiedTime, &accessedTime);
- // round file time down to nearest sec. Nanosec accurancy is not preserved elsewhere and can lead to mismatch later.
- unsigned hour, min, sec, nanosec;
- modifiedTime.getTime(hour, min, sec, nanosec);
- modifiedTime.setTime(hour, min, sec, 0);
- StringBuffer timestr;
- modifiedTime.getString(timestr);
- if(timestr.length())
- attrs->setProp("@modified", timestr.str());
- if(clusterHandler)
- clusterHandler->setDescriptorParts(desc, base.str(), attrs);
- // properties of the logical file
- IPropertyTree & properties = desc->queryProperties();
- properties.setProp("@kind", "key");
- properties.setPropInt64("@size", indexFileSize);
- properties.setPropInt64("@recordCount", reccount);
- SCMStringBuffer info;
- WorkunitUpdate workUnit = ctx->updateWorkUnit();
- properties.setProp("@owner", workUnit->getUser(info).str());
- info.clear();
- properties.setProp("@workunit", workUnit->getWuid(info).str());
- info.clear();
- properties.setProp("@job", workUnit->getJobName(info).str());
- char const * rececl = helper.queryRecordECL();
- if(rececl && *rececl)
- properties.setProp("ECL", rececl);
- setExpiryTime(properties, helper.getExpiryDays());
- if (helper.getFlags() & TIWupdate)
- {
- unsigned eclCRC;
- unsigned __int64 totalCRC;
- helper.getUpdateCRCs(eclCRC, totalCRC);
- properties.setPropInt("@eclCRC", eclCRC);
- properties.setPropInt64("@totalCRC", totalCRC);
- }
- properties.setPropInt("@fileCrc", fileCrc);
- properties.setPropInt("@formatCrc", helper.getFormatCrc());
- void * layoutMetaBuff;
- size32_t layoutMetaSize;
- if(helper.getIndexLayout(layoutMetaSize, layoutMetaBuff))
- {
- properties.setPropBin("_record_layout", layoutMetaSize, layoutMetaBuff);
- rtlFree(layoutMetaBuff);
- }
- }
- IUserDescriptor *queryUserDescriptor() const
- {
- IConstWorkUnit *workUnit = ctx->queryWorkUnit();
- if (workUnit)
- return workUnit->queryUserDescriptor();
- else
- return NULL;
- }
- };
- //=================================================================================
- class CRoxieServerIndexWriteActivityFactory : public CRoxieServerMultiOutputFactory
- {
- public:
- CRoxieServerIndexWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- setNumOutputs(0);
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerIndexWriteActivity(this, _probeManager);
- }
- virtual bool isSink() const
- {
- return true;
- }
- };
- IRoxieServerActivityFactory *createRoxieServerIndexWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- {
- return new CRoxieServerIndexWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
- }
- //=================================================================================
- static inline void getLimitType(unsigned flags, bool & limitFail, bool & limitOnFail)
- {
- if((flags & JFmatchAbortLimitSkips) != 0)
- {
- limitFail = false;
- limitOnFail = false;
- }
- else
- {
- limitOnFail = ((flags & JFonfail) != 0);
- limitFail = !limitOnFail;
- }
- }
- class CRoxieServerJoinActivity : public CRoxieServerTwoInputActivity
- {
- enum { JSfill, JSfillleft, JSfillright, JScollate, JScompare, JSleftonly, JSrightonly } state;
- IHThorJoinArg &helper;
- ICompare * collate;
- ICompare * collateupper;
- ThorActivityKind activityKind;
- bool leftOuterJoin;
- bool rightOuterJoin;
- bool exclude;
- bool limitFail;
- bool limitOnFail;
- unsigned keepLimit;
- unsigned joinLimit;
- unsigned atmostLimit;
- unsigned abortLimit;
- bool betweenjoin;
- OwnedRowArray right;
- const void * left;
- const void * pendingRight;
- unsigned rightIndex;
- BoolArray matchedRight;
- bool matchedLeft;
- Owned<IException> failingLimit;
- ConstPointerArray filteredRight;
- Owned<IRHLimitedCompareHelper> limitedhelper;
- OwnedConstRoxieRow defaultLeft;
- OwnedConstRoxieRow defaultRight;
- Owned<IEngineRowAllocator> defaultLeftAllocator;
- Owned<IEngineRowAllocator> defaultRightAllocator;
- bool cloneLeft;
- void createDefaultLeft()
- {
- if (!defaultLeft)
- {
- if (!defaultLeftAllocator)
- defaultLeftAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input->queryOutputMeta(), activityId));
- RtlDynamicRowBuilder rowBuilder(defaultLeftAllocator);
- size32_t thisSize = helper.createDefaultLeft(rowBuilder);
- defaultLeft.setown(rowBuilder.finalizeRowClear(thisSize));
- }
- }
- void createDefaultRight()
- {
- if (!defaultRight)
- {
- if (!defaultRightAllocator)
- defaultRightAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input1->queryOutputMeta(), activityId));
- RtlDynamicRowBuilder rowBuilder(defaultRightAllocator);
- size32_t thisSize = helper.createDefaultRight(rowBuilder);
- defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
- }
- }
- public:
- CRoxieServerJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerTwoInputActivity(_factory, _probeManager), helper((IHThorJoinArg &)basehelper)
- {
- // MORE - some of this should be done in factory
- unsigned joinFlags = helper.getJoinFlags();
- leftOuterJoin = (joinFlags & JFleftouter) != 0;
- rightOuterJoin = (joinFlags & JFrightouter) != 0;
- exclude = (joinFlags & JFexclude) != 0;
- cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
- getLimitType(joinFlags, limitFail, limitOnFail);
- if (joinFlags & JFslidingmatch)
- {
- betweenjoin = true;
- collate = helper.queryCompareLeftRightLower();
- collateupper = helper.queryCompareLeftRightUpper();
- }
- else
- {
- betweenjoin = false;
- collate = collateupper = helper.queryCompareLeftRight();
- }
- rightIndex = 0;
- state = JSfill;
- matchedLeft = false;
- joinLimit = 0;
- keepLimit = 0; // wait until ctx available
- atmostLimit = 0; // wait until ctx available
- abortLimit = 0; // wait until ctx available
- assertex((joinFlags & (JFfirst | JFfirstleft | JFfirstright)) == 0);
- left = NULL;
- pendingRight = NULL;
- activityKind = _factory->getKind();
- }
- virtual bool needsAllocator() const { return true; }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- left = NULL;
- rightIndex = 0;
- state = JSfill;
- matchedLeft = false;
- CRoxieServerTwoInputActivity::start(parentExtractSize, parentExtract, paused);
- keepLimit = helper.getKeepLimit();
- if (keepLimit == 0)
- keepLimit = (unsigned)-1;
- atmostLimit = helper.getJoinLimit();
- if(atmostLimit == 0)
- atmostLimit = (unsigned)-1;
- else
- assertex(!rightOuterJoin && !betweenjoin);
- abortLimit = helper.getMatchAbortLimit();
- if (abortLimit == 0)
- abortLimit = (unsigned)-1;
- if (rightOuterJoin)
- createDefaultLeft();
- if ((leftOuterJoin && (activityKind==TAKjoin || activityKind==TAKjoinlight || activityKind==TAKdenormalizegroup)) || limitOnFail)
- createDefaultRight();
- if ((helper.getJoinFlags() & JFlimitedprefixjoin) && helper.getJoinLimit())
- { //limited match join (s[1..n])
- limitedhelper.setown(createRHLimitedCompareHelper());
- limitedhelper->init( helper.getJoinLimit(), input1, collate, helper.queryPrefixCompare() );
- }
- }
- virtual void reset()
- {
- right.clear();
- ReleaseClearRoxieRow(left);
- ReleaseClearRoxieRow(pendingRight);
- defaultRight.clear();
- defaultLeft.clear();
- CRoxieServerTwoInputActivity::reset();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- switch(idx)
- {
- case 0:
- if ((helper.getJoinFlags() & JFparallel) != 0)
- {
- puller.setown(new CRoxieServerReadAheadInput(0)); // MORE - cant ask context for parallelJoinPreload as context is not yet set up.
- puller->setInput(0, _in);
- _in = puller;
- }
- input = _in;
- break;
- case 1:
- input1 = _in;
- break;
- default:
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
- }
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- if (idx==(unsigned)-1)
- idx = 0;
- return idx ? NULL : this;
- }
- void fillLeft()
- {
- matchedLeft = false;
- left = input->nextInGroup();
- if (!left)
- left = input->nextInGroup();
- if(betweenjoin && left && pendingRight && (collate->docompare(left, pendingRight) >= 0))
- fillRight();
- if (limitedhelper && 0==rightIndex)
- {
- rightIndex = 0;
- right.clear();
- matchedRight.kill();
- if (left)
- {
- limitedhelper->getGroup(right,left);
- ForEachItemIn(idx, right)
- matchedRight.append(false);
- }
- }
- }
- void fillRight()
- {
- if (limitedhelper)
- return;
- failingLimit.clear();
- if(betweenjoin && left)
- {
- aindex_t start = 0;
- while(right.isItem(start) && (collateupper->docompare(left, right.item(start)) > 0))
- start++;
- if(start>0)
- right.clearPart(0, start);
- }
- else
- right.clear();
- rightIndex = 0;
- unsigned groupCount = 0;
- const void * next;
- while(true)
- {
- if(pendingRight)
- {
- next = pendingRight;
- pendingRight = NULL;
- }
- else
- {
- next = input1->nextInGroup();
- }
- if(!rightOuterJoin && next && (!left || (collateupper->docompare(left, next) > 0))) // if right is less than left, and not right outer, can skip group
- {
- while(next)
- {
- ReleaseClearRoxieRow(next);
- next = input1->nextInGroup();
- }
- continue;
- }
- while(next)
- {
- if(groupCount==abortLimit)
- {
- if(limitFail)
- failLimit();
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- if(limitOnFail)
- {
- assertex(!failingLimit);
- try
- {
- failLimit();
- }
- catch(IException * except)
- {
- failingLimit.setown(except);
- }
- assertex(failingLimit != NULL);
- }
- right.append(next);
- do
- {
- next = input1->nextInGroup();
- ReleaseRoxieRow(next);
- } while(next);
- break;
- }
- else if(groupCount==atmostLimit)
- {
- right.clear();
- groupCount = 0;
- while(next)
- {
- ReleaseRoxieRow(next);
- next = input1->nextInGroup();
- }
- }
- else
- {
- right.append(next);
- groupCount++;
- }
- next = input1->nextInGroup();
- }
- // 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
- if(betweenjoin && left)
- {
- pendingRight = input1->nextInGroup();
- if(!pendingRight || (collate->docompare(left, pendingRight) < 0))
- break;
- }
- else
- break;
- }
- matchedRight.kill();
- ForEachItemIn(idx, right)
- matchedRight.append(false);
- }
- const void * joinRecords(const void * curLeft, const void * curRight)
- {
- if (cloneLeft)
- {
- LinkRoxieRow(curLeft);
- return curLeft;
- }
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t thisSize = helper.transform(rowBuilder, curLeft, curRight);
- if (thisSize)
- return rowBuilder.finalizeRowClear(thisSize);
- else
- return NULL;
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- const void * denormalizeRecords(const void * curLeft, ConstPointerArray & rows)
- {
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned numRows = rows.ordinality();
- const void * right = numRows ? rows.item(0) : defaultRight.get();
- size32_t thisSize = helper.transform(rowBuilder, curLeft, right, numRows, (const void * *)rows.getArray());
- if (thisSize)
- return rowBuilder.finalizeRowClear(thisSize);
- else
- return NULL;
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- const void * joinException(const void * curLeft, IException * except)
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t thisSize = helper.onFailTransform(rowBuilder, curLeft, defaultRight, except);
- return rowBuilder.finalizeRowClear(thisSize);
- }
- void failLimit()
- {
- helper.onMatchAbortLimitExceeded();
- CommonXmlWriter xmlwrite(0);
- if (input->queryOutputMeta() && input->queryOutputMeta()->hasXML())
- {
- input->queryOutputMeta()->toXML((byte *) left, xmlwrite);
- }
- throw MakeStringException(ROXIE_TOO_MANY_RESULTS, "More than %d match candidates in join %d for row %s", abortLimit, queryId(), xmlwrite.str());
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- switch (state)
- {
- case JSfill:
- fillLeft();
- state = JSfillright;
- break;
- case JSfillright:
- fillRight();
- state = JScollate;
- break;
- case JSfillleft:
- fillLeft();
- state = JScollate;
- break;
- case JScollate:
- if (right.ordinality() == 0)
- {
- if (left == NULL)
- return NULL;
- state = JSleftonly;
- }
- else
- {
- if (!left)
- state = JSrightonly;
- else
- {
- int diff;
- if(betweenjoin)
- diff = ((collate->docompare(left, right.item(0)) < 0) ? -1 : ((collateupper->docompare(left, right.item(right.ordinality()-1)) > 0) ? +1 : 0));
- else
- diff = collate->docompare(left, right.item(0));
- bool limitExceeded = right.ordinality()>abortLimit;
- if (diff == 0)
- {
- if (limitExceeded)
- {
- const void * ret = NULL;
- if(failingLimit)
- ret = joinException(left, failingLimit);
- ReleaseRoxieRow(left);
- left = NULL;
- state = JSfillleft;
- ForEachItemIn(idx, right)
- matchedRight.replace(true, idx);
- if(ret)
- {
- processed++;
- return ret;
- }
- }
- else
- {
- state = JScompare;
- joinLimit = keepLimit;
- }
- }
- else if (diff < 0)
- state = JSleftonly;
- else if (limitExceeded)
- {
- // MORE - Roxie code seems to think there should be a destroyRowset(right) here....
- state = JSfillright;
- }
- else
- state = JSrightonly;
- }
- }
- break;
- case JSrightonly:
- if (rightOuterJoin)
- {
- switch (activityKind)
- {
- case TAKjoin:
- {
- while (right.isItem(rightIndex))
- {
- if (!matchedRight.item(rightIndex))
- {
- const void * rhs = right.item(rightIndex++);
- const void *ret = joinRecords(defaultLeft, rhs);
- if (ret)
- {
- processed++;
- return ret;
- }
- }
- rightIndex++;
- }
- break;
- }
- //Probably excessive to implement the following, but possibly useful
- case TAKdenormalize:
- {
- OwnedConstRoxieRow newLeft;
- newLeft.set(defaultLeft);
- unsigned rowSize = 0;
- unsigned leftCount = 0;
- while (right.isItem(rightIndex))
- {
- if (!matchedRight.item(rightIndex))
- {
- const void * rhs = right.item(rightIndex);
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned thisSize = helper.transform(rowBuilder, newLeft, rhs, ++leftCount);
- if (thisSize)
- {
- rowSize = thisSize;
- newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- rightIndex++;
- }
- state = JSfillright;
- if (rowSize)
- {
- processed++;
- return newLeft.getClear();
- }
- break;
- }
- case TAKdenormalizegroup:
- {
- filteredRight.kill();
- while (right.isItem(rightIndex))
- {
- if (!matchedRight.item(rightIndex))
- filteredRight.append(right.item(rightIndex));
- rightIndex++;
- }
- state = JSfillright;
- if (filteredRight.ordinality())
- {
- const void * ret = denormalizeRecords(defaultLeft, filteredRight);
- filteredRight.kill();
- if (ret)
- {
- processed++;
- return ret;
- }
- }
- break;
- }
- }
- }
- state = JSfillright;
- break;
-
- case JSleftonly:
- {
- const void * ret = NULL;
- if (!matchedLeft && leftOuterJoin)
- {
- switch (activityKind)
- {
- case TAKjoin:
- ret = joinRecords(left, defaultRight);
- break;
- case TAKdenormalize:
- ret = left;
- left = NULL;
- break;
- case TAKdenormalizegroup:
- filteredRight.kill();
- ret = denormalizeRecords(left, filteredRight);
- break;
- }
- }
- ReleaseRoxieRow(left);
- left = NULL;
- state = JSfillleft;
- if (ret)
- {
- processed++;
- return ret;
- }
- break;
- }
- case JScompare:
- if (joinLimit != 0)
- {
- switch (activityKind)
- {
- case TAKjoin:
- {
- while (right.isItem(rightIndex))
- {
- const void * rhs = right.item(rightIndex++);
- if (helper.match(left, rhs))
- {
- matchedRight.replace(true, rightIndex-1);
- matchedLeft = true;
- if (!exclude)
- {
- const void *ret = joinRecords(left, rhs);
- if (ret)
- {
- processed++;
- joinLimit--;
- return ret;
- }
- }
- }
- }
- break;
- }
- case TAKdenormalize:
- {
- OwnedConstRoxieRow newLeft;
- newLeft.set(left);
- unsigned rowSize = 0;
- unsigned leftCount = 0;
- while (right.isItem(rightIndex) && joinLimit)
- {
- try
- {
- const void * rhs = right.item(rightIndex++);
- if (helper.match(left, rhs))
- {
- matchedRight.replace(true, rightIndex-1);
- matchedLeft = true;
- if (!exclude)
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned thisSize = helper.transform(rowBuilder, newLeft, rhs, ++leftCount);
- if (thisSize)
- {
- rowSize = thisSize;
- newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
- joinLimit--;
- }
- }
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- state = JSleftonly;
- rightIndex = 0;
- if (rowSize)
- {
- processed++;
- return newLeft.getClear();
- }
- break;
- }
- case TAKdenormalizegroup:
- {
- filteredRight.kill();
- while (right.isItem(rightIndex))
- {
- const void * rhs = right.item(rightIndex++);
- if (helper.match(left, rhs))
- {
- matchedRight.replace(true, rightIndex-1);
- filteredRight.append(rhs);
- matchedLeft = true;
- if (filteredRight.ordinality()==joinLimit)
- break;
- }
- }
- state = JSleftonly;
- rightIndex = 0;
- if (!exclude && filteredRight.ordinality())
- {
- const void * ret = denormalizeRecords(left, filteredRight);
- filteredRight.kill();
- if (ret)
- {
- processed++;
- return ret;
- }
- }
- break;
- }
- }
- }
- state = JSleftonly;
- rightIndex = 0;
- break;
- }
- }
- }
- };
- class CRoxieServerJoinActivityFactory : public CRoxieServerActivityFactory
- {
- unsigned input2;
- unsigned input2idx;
- public:
- CRoxieServerJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- input2 = 0;
- input2idx = 0;
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerJoinActivity(this, _probeManager);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- if (idx==1)
- {
- input2 = source;
- input2idx = sourceidx;
- }
- else
- CRoxieServerActivityFactory::setInput(idx, source, sourceidx);
- }
- virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
- {
- switch (idx)
- {
- case 1:
- sourceidx = input2idx;
- return input2;
- case 0:
- return CRoxieServerActivityFactory::getInput(idx, sourceidx);
- default:
- return (unsigned) -1;
- }
- }
- };
- IRoxieServerActivityFactory *createRoxieServerJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- #define CONCAT_READAHEAD 1000
- MAKEPointerArray(RecordPullerThread, RecordPullerArray);
- class CRoxieServerThreadedConcatActivity : public CRoxieServerActivity, implements IRecordPullerCallback
- {
- QueueOf<const void, true> buffer;
- InterruptableSemaphore ready;
- InterruptableSemaphore space;
- CriticalSection crit;
- unsigned eofs;
- RecordPullerArray pullers;
- unsigned numInputs;
- public:
- CRoxieServerThreadedConcatActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _grouped, unsigned _numInputs)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- eofs = 0;
- numInputs = _numInputs;
- for (unsigned i = 0; i < numInputs; i++)
- pullers.append(*new RecordPullerThread(_grouped));
- }
- ~CRoxieServerThreadedConcatActivity()
- {
- ForEachItemIn(idx, pullers)
- delete &pullers.item(idx);
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- space.reinit(CONCAT_READAHEAD);
- ready.reinit();
- eofs = 0;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- ForEachItemIn(idx, pullers)
- {
- pullers.item(idx).start(parentExtractSize, parentExtract, paused, ctx->concatPreload(), false, ctx);
- // NOTE - it is ok to start the thread running while parts of the subgraph are still being started, since everything
- // in the part of the subgraph that the thread uses has been started.
- // Note that splitters are supposed to cope with being used when only some outputs have been started.
- }
- }
- virtual void stop(bool aborting)
- {
- space.interrupt();
- ready.interrupt();
- ForEachItemIn(idx, pullers)
- pullers.item(idx).stop(aborting);
- CRoxieServerActivity::stop(aborting);
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- return 0;
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- if (pullers.isItem(idx))
- return pullers.item(idx).queryInput();
- else
- return NULL;
- }
- virtual void reset()
- {
- CRoxieServerActivity::reset();
- ForEachItemIn(idx, pullers)
- pullers.item(idx).reset();
- ForEachItemIn(idx1, buffer)
- ReleaseRoxieRow(buffer.item(idx1));
- buffer.clear();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- if (pullers.isItem(idx))
- pullers.item(idx).setInput(this, _in);
- else
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- {
- CriticalBlock b(crit);
- if (eofs==numInputs && !buffer.ordinality())
- return NULL; // eof
- }
- ready.wait();
- const void *ret;
- {
- CriticalBlock b(crit);
- ret = buffer.dequeue();
- }
- if (ret)
- processed++;
- space.signal();
- return ret;
- }
- }
- virtual bool fireException(IException *e)
- {
- // called from puller thread on failure
- ready.interrupt(LINK(e));
- space.interrupt(e);
- return true;
- }
- virtual void processRow(const void *row)
- {
- {
- CriticalBlock b(crit);
- buffer.enqueue(row);
- }
- ready.signal();
- space.wait();
- }
- virtual void processGroup(const ConstPointerArray &rows)
- {
- // NOTE - a bit bizzare in that it waits for the space AFTER using it.
- // But the space semaphore is only there to stop infinite readahead. And otherwise it would deadlock
- // if group was larger than max(space)
- {
- CriticalBlock b(crit);
- ForEachItemIn(idx, rows)
- buffer.enqueue(rows.item(idx));
- buffer.enqueue(NULL);
- }
- for (unsigned i2 = 0; i2 <= rows.length(); i2++) // note - does 1 extra for the null
- {
- ready.signal();
- space.wait();
- }
- }
- virtual void processEOG()
- {
- // Used when output is not grouped - just ignore
- }
- virtual void processDone()
- {
- CriticalBlock b(crit);
- eofs++;
- if (eofs == numInputs)
- ready.signal();
- }
- };
- class CRoxieServerOrderedConcatActivity : public CRoxieServerActivity
- {
- IRoxieInput *curInput;
- bool eogSeen;
- bool anyThisGroup;
- bool grouped;
- unsigned numInputs;
- unsigned inputIdx;
- IRoxieInput **inputArray;
- public:
- CRoxieServerOrderedConcatActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _grouped, unsigned _numInputs)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- eogSeen = false;
- anyThisGroup = false;
- grouped = _grouped;
- numInputs = _numInputs;
- inputIdx = 0;
- inputArray = new IRoxieInput*[numInputs];
- for (unsigned i = 0; i < numInputs; i++)
- inputArray[i] = NULL;
- }
- ~CRoxieServerOrderedConcatActivity()
- {
- delete [] inputArray;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- inputIdx = 0;
- curInput = inputArray[inputIdx];
- eogSeen = false;
- anyThisGroup = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- for (unsigned i = 0; i < numInputs; i++)
- inputArray[i]->start(parentExtractSize, parentExtract, paused);
- }
- virtual void stop(bool aborting)
- {
- for (unsigned i = 0; i < numInputs; i++)
- inputArray[i]->stop(aborting);
- CRoxieServerActivity::stop(aborting);
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- return 0;
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- if (idx < numInputs)
- return inputArray[idx];
- else
- return NULL;
- }
- virtual void reset()
- {
- CRoxieServerActivity::reset();
- for (unsigned i = 0; i < numInputs; i++)
- inputArray[i]->reset();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- inputArray[idx] = _in;
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (!curInput)
- return NULL; // eof
- const void * next = curInput->nextInGroup();
- if (next)
- {
- anyThisGroup = true;
- eogSeen = false;
- processed++;
- return next;
- }
- else if (!eogSeen)
- {
- eogSeen = true;
- if (grouped)
- {
- if (anyThisGroup)
- {
- anyThisGroup = false;
- return NULL;
- }
- else
- return nextInGroup();
- }
- else
- return nextInGroup();
- }
- else if (inputIdx < numInputs-1)
- {
- inputIdx++;
- curInput = inputArray[inputIdx];
- eogSeen = false;
- return nextInGroup();
- }
- else
- {
- curInput = NULL;
- return NULL;
- }
- }
- };
- class CRoxieServerConcatActivityFactory : public CRoxieServerMultiInputFactory
- {
- bool ordered;
- bool grouped;
- public:
- CRoxieServerConcatActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- Owned <IHThorFunnelArg> helper = (IHThorFunnelArg *) helperFactory();
- ordered = helper->isOrdered();
- grouped = helper->queryOutputMeta()->isGrouped();
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- if (ordered || (_probeManager && _probeManager->queryDebugManager()))
- return new CRoxieServerOrderedConcatActivity(this, _probeManager, grouped, numInputs());
- else
- return new CRoxieServerThreadedConcatActivity(this, _probeManager, grouped, numInputs());
- }
- };
- IRoxieServerActivityFactory *createRoxieServerConcatActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerConcatActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerNonEmptyActivity : public CRoxieServerMultiInputBaseActivity
- {
- IRoxieInput * selectedInput;
- unsigned savedParentExtractSize;
- const byte * savedParentExtract;
- bool foundInput;
- public:
- CRoxieServerNonEmptyActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerMultiInputBaseActivity(_factory, _probeManager, _numInputs)
- {
- foundInput = false;
- selectedInput = NULL;
- savedParentExtractSize = 0;;
- savedParentExtract = NULL;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- //Don't start the inputs yet so we can short-circuit...
- CRoxieServerMultiInputBaseActivity::start(parentExtractSize, parentExtract, paused);
- savedParentExtractSize = parentExtractSize;
- savedParentExtract = parentExtract;
- }
- virtual void stop(bool aborting)
- {
- if (foundInput)
- {
- if (selectedInput)
- selectedInput->stop(aborting);
- }
- else
- {
- for (unsigned i = 0; i < numInputs; i++)
- inputArray[i]->stop(aborting);
- }
- CRoxieServerMultiInputBaseActivity::stop(aborting);
- }
- virtual void reset()
- {
- CRoxieServerMultiInputBaseActivity::reset();
- foundInput = false;
- selectedInput = NULL;
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- return 0; // Can't easily calcuate anything reliable but local processing is negligible
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (!foundInput)
- {
- foundInput = true;
- //If we get an exception in this loop then stop() will stop any started inputs
- for (unsigned i=0; i < numInputs; i++)
- {
- selectedInput = inputArray[i];
- selectedInput->start(savedParentExtractSize, savedParentExtract, false);
- const void * next = selectedInput->nextInGroup();
- if (next)
- {
- //Found a row so stop remaining
- for (unsigned j=i+1; j < numInputs; j++)
- inputArray[j]->stop(false);
- processed++;
- return next;
- }
- selectedInput->stop(false);
- }
- selectedInput = NULL;
- return NULL;
- }
- if (!selectedInput)
- return NULL;
- const void * next = selectedInput->nextInGroup();
- if (next)
- processed++;
- return next;
- }
- };
- class CRoxieServerNonEmptyActivityFactory : public CRoxieServerMultiInputFactory
- {
- public:
- CRoxieServerNonEmptyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerNonEmptyActivity(this, _probeManager, numInputs());
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNonEmptyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerNonEmptyActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerMergeActivity : public CRoxieServerActivity
- {
- IHThorMergeArg &helper;
- unsigned *mergeheap;
- unsigned activeInputs;
- unsigned numInputs;
- IRoxieInput **inputArray;
- const void **pending;
- bool first;
- ICompare *compare;
- bool dedup;
- void permute()
- {
- assertex(activeInputs == 0);
- for(unsigned i = 0; i < numInputs; i++)
- if(pullInput(i))
- mergeheap[activeInputs++] = i;
- // the tree structure: element p has children p*2+1 and p*2+2, or element c has parent (unsigned)(c-1)/2
- // the heap property: no element should be smaller than its parent
- // the dedup variant: if(dedup), the top of the heap should also not be equal to either child
- // 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
- if (activeInputs >= 2)
- for(unsigned p = (activeInputs-2)/2; p > 0; --p)
- siftDown(p);
- if(dedup)
- siftDownDedupTop();
- else
- siftDown(0);
- }
- void readNext()
- {
- if(!pullInput(mergeheap[0]))
- if(!promote(0))
- return;
- // we have changed the element at the top of the heap, so need to sift it down to maintain the heap property
- if(dedup)
- siftDownDedupTop();
- else
- siftDown(0);
- }
- bool pullInput(unsigned i)
- {
- const void *next = inputArray[i]->nextInGroup();
- if (!next)
- next = inputArray[i]->nextInGroup();
- pending[i] = next;
- return (next != NULL);
- }
- bool promote(unsigned p)
- {
- activeInputs--;
- if(activeInputs == p)
- return false;
- mergeheap[p] = mergeheap[activeInputs];
- return true;
- }
- bool siftDown(unsigned p)
- {
- // assumimg that all descendents of p form a heap, sift p down to its correct position, and so include it in the heap
- bool nochange = true;
- while(1)
- {
- unsigned c = p*2 + 1;
- if(c >= activeInputs)
- return nochange;
- if(c+1 < activeInputs)
- {
- int childcmp = BuffCompare(c+1, c);
- if((childcmp < 0) || ((childcmp == 0) && (mergeheap[c+1] < mergeheap[c])))
- ++c;
- }
- int cmp = BuffCompare(c, p);
- if((cmp > 0) || ((cmp == 0) && (mergeheap[c] > mergeheap[p])))
- return nochange;
- nochange = false;
- unsigned r = mergeheap[c];
- mergeheap[c] = mergeheap[p];
- mergeheap[p] = r;
- p = c;
- }
- }
- void siftDownDedupTop()
- {
- // same as siftDown(0), except that it also ensures that the top of the heap is not equal to either of its children
- if(activeInputs < 2)
- return;
- unsigned c = 1;
- int childcmp = 1;
- if(activeInputs >= 3)
- {
- childcmp = BuffCompare(2, 1);
- if(childcmp < 0)
- c = 2;
- }
- int cmp = BuffCompare(c, 0);
- if(cmp > 0)
- return;
- // the following loop ensures the correct property holds on the smaller branch, and that childcmp==0 iff the top matches the other branch
- while(cmp <= 0)
- {
- if(cmp == 0)
- {
- if(mergeheap[c] < mergeheap[0])
- {
- unsigned r = mergeheap[c];
- mergeheap[c] = mergeheap[0];
- mergeheap[0] = r;
- }
- ReleaseClearRoxieRow(pending[mergeheap[c]]);
- if(!pullInput(mergeheap[c]))
- if(!promote(c))
- break;
- siftDown(c);
- }
- else
- {
- unsigned r = mergeheap[c];
- mergeheap[c] = mergeheap[0];
- mergeheap[0] = r;
- if(siftDown(c))
- break;
- }
- cmp = BuffCompare(c, 0);
- }
- // the following loop ensures the uniqueness property holds on the other branch too
- c = 3-c;
- if(activeInputs <= c)
- return;
- while(childcmp == 0)
- {
- if(mergeheap[c] < mergeheap[0])
- {
- unsigned r = mergeheap[c];
- mergeheap[c] = mergeheap[0];
- mergeheap[0] = r;
- }
- ReleaseClearRoxieRow(pending[mergeheap[c]]);
- if(!pullInput(mergeheap[c]))
- if(!promote(c))
- break;
- siftDown(c);
- childcmp = BuffCompare(c, 0);
- }
- }
- inline int BuffCompare(unsigned a, unsigned b)
- {
- return compare->docompare(pending[mergeheap[a]], pending[mergeheap[b]]);
- }
- public:
- CRoxieServerMergeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorMergeArg &)basehelper), numInputs(_numInputs)
- {
- activeInputs = 0;
- first = true;
- mergeheap = new unsigned[numInputs];
- inputArray = new IRoxieInput*[numInputs];
- pending = new const void *[numInputs];
- compare = helper.queryCompare();
- dedup = helper.dedup();
- for (unsigned i = 0; i < numInputs; i++)
- {
- inputArray[i] = NULL;
- pending[i] = NULL;
- }
- }
- ~CRoxieServerMergeActivity()
- {
- delete [] mergeheap;
- delete [] inputArray;
- delete [] pending;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- activeInputs = 0;
- first = true;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- for (unsigned i = 0; i < numInputs; i++)
- {
- inputArray[i]->start(parentExtractSize, parentExtract, paused);
- }
- }
- virtual void stop(bool aborting)
- {
- for (unsigned i = 0; i < numInputs; i++)
- {
- inputArray[i]->stop(aborting);
- }
- CRoxieServerActivity::stop(aborting);
- }
- virtual void reset()
- {
- for (unsigned i = 0; i < numInputs; i++)
- {
- ReleaseClearRoxieRow(pending[i]);
- inputArray[i]->reset();
- }
- CRoxieServerActivity::reset();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- inputArray[idx] = _in;
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (first)
- {
- permute();
- first = false;
- }
- if (activeInputs)
- {
- const void *next = pending[mergeheap[0]];
- readNext();
- if (next)
- processed++;
- return next;
- }
- else
- return NULL;
- }
- };
- class CRoxieServerMergeActivityFactory : public CRoxieServerMultiInputFactory
- {
- public:
- CRoxieServerMergeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerMergeActivity(this, _probeManager, numInputs());
- }
- };
- IRoxieServerActivityFactory *createRoxieServerMergeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerMergeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerRegroupActivity : public CRoxieServerMultiInputActivity
- {
- IHThorRegroupArg &helper;
- unsigned inputIndex;
- bool eof;
- unsigned __int64 numProcessedLastGroup;
- public:
- CRoxieServerRegroupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerMultiInputActivity(_factory, _probeManager, _numInputs), helper((IHThorRegroupArg &)basehelper)
- {
- inputIndex = 0;
- eof = false;
- numProcessedLastGroup = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- inputIndex = 0;
- eof = false;
- numProcessedLastGroup = processed;
- CRoxieServerMultiInputActivity::start(parentExtractSize, parentExtract, paused);
- }
- const void * nextFromInputs()
- {
- unsigned initialInput = inputIndex;
- while (inputIndex < numInputs)
- {
- const void * next = inputArray[inputIndex]->nextInGroup();
- if (next)
- {
- if ((inputIndex != initialInput) && (inputIndex != initialInput+1))
- {
- ReleaseRoxieRow(next);
- throw MakeStringException(ROXIE_MISMATCH_GROUP_ERROR, "Mismatched groups supplied to Regroup (%d)", factory->queryId());
- }
- return next;
- }
- inputIndex++;
- }
- if ((initialInput != 0) && (initialInput+1 != numInputs))
- throw MakeStringException(ROXIE_MISMATCH_GROUP_ERROR, "Mismatched groups supplied to Regroup (%d)", factory->queryId());
- inputIndex = 0;
- return NULL;
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- const void * ret = nextFromInputs();
- if (ret)
- {
- processed++;
- return ret;
- }
- if (numProcessedLastGroup != processed)
- {
- numProcessedLastGroup = processed;
- return NULL;
- }
- eof = true;
- return NULL;
- }
- #if 0
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- //MORE: RKC: Do we want to do this i) always ii) conditionally iii) never
- if (idx)
- {
- puller.setown(new CRoxieServerReadAheadInput(0)); // MORE - cant ask context for parallelJoinPreload as context is not yet set up.
- puller->setInput(0, _in);
- CRoxieServerMultiInputActivity::setInput(idx, puller);
- }
- else
- CRoxieServerMultiInputActivity::setInput(idx, _in);
- }
- #endif
- };
- class CRoxieServerRegroupActivityFactory : public CRoxieServerMultiInputFactory
- {
- public:
- CRoxieServerRegroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerRegroupActivity(this, _probeManager, numInputs());
- }
- };
- IRoxieServerActivityFactory *createRoxieServerRegroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerRegroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerCombineActivity : public CRoxieServerMultiInputActivity
- {
- IHThorCombineArg &helper;
- unsigned __int64 numProcessedLastGroup;
- public:
- CRoxieServerCombineActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerMultiInputActivity(_factory, _probeManager, _numInputs), helper((IHThorCombineArg &)basehelper)
- {
- numProcessedLastGroup = 0;
- }
- ~CRoxieServerCombineActivity()
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- numProcessedLastGroup = processed;
- CRoxieServerMultiInputActivity::start(parentExtractSize, parentExtract, paused);
- }
- void nextInputs(ConstPointerArray & out)
- {
- for (unsigned i=0; i < numInputs; i++)
- {
- const void * next = inputArray[i]->nextInGroup();
- if (next)
- out.append(next);
- }
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- ConstPointerArray group;
- nextInputs(group);
- if ((group.ordinality() == 0) && (numProcessedLastGroup == processed))
- nextInputs(group);
- if (group.ordinality() == 0)
- {
- numProcessedLastGroup = processed;
- return NULL;
- }
- else if (group.ordinality() != numInputs)
- {
- ReleaseRoxieRowSet(group);
- throw MakeStringException(ROXIE_MISMATCH_GROUP_ERROR, "Mismatched group input for Combine Activity(%d)", factory->queryId());
- }
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t outSize = helper.transform(rowBuilder, group.ordinality(), group.getArray());
- ReleaseRoxieRowSet(group);
- if (outSize)
- {
- processed++;
- return rowBuilder.finalizeRowClear(outSize);
- }
- }
- catch (IException *E)
- {
- ReleaseRoxieRowSet(group);
- throw makeWrappedException(E);
- }
- }
- }
- };
- class CRoxieServerCombineActivityFactory : public CRoxieServerMultiInputFactory
- {
- public:
- CRoxieServerCombineActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerCombineActivity(this, _probeManager, numInputs());
- }
- };
- IRoxieServerActivityFactory *createRoxieServerCombineActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerCombineActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerCombineGroupActivity : public CRoxieServerTwoInputActivity
- {
- IHThorCombineGroupArg &helper;
- unsigned __int64 numProcessedLastGroup;
- public:
- CRoxieServerCombineGroupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerTwoInputActivity(_factory, _probeManager), helper((IHThorCombineGroupArg &)basehelper)
- {
- numProcessedLastGroup = 0;
- }
- ~CRoxieServerCombineGroupActivity()
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- numProcessedLastGroup = processed;
- CRoxieServerTwoInputActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- switch(idx)
- {
- case 0:
- #if 0
- //MORE: RKC: Do we want to do this i) always ii) conditionally iii) never
- puller.setown(new CRoxieServerReadAheadInput(0)); // MORE - cant ask context for parallelJoinPreload as context is not yet set up.
- puller->setInput(0, _in);
- _in = puller;
- #endif
- input = _in;
- break;
- case 1:
- input1 = _in;
- break;
- default:
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
- }
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- if (idx==(unsigned)-1)
- idx = 0;
- return idx ? NULL : this;
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- const void * left = input->nextInGroup();
- if (!left && (numProcessedLastGroup == processed))
- left = input->nextInGroup();
- if (!left)
- {
- if (numProcessedLastGroup == processed)
- {
- const void * nextRight = input1->nextInGroup();
- if (nextRight)
- {
- ReleaseRoxieRow(nextRight);
- throw MakeStringException(ROXIE_MISSING_GROUP_ERROR, "Missing LEFT record for Combine Group (%d)", factory->queryId());
- }
- }
- else
- numProcessedLastGroup = processed;
- return NULL;
- }
- ConstPointerArray group;
- loop
- {
- const void * in = input1->nextInGroup();
- if (!in)
- break;
- group.append(in);
- }
- if (group.ordinality() == 0)
- {
- ReleaseRoxieRow(left);
- ReleaseRoxieRowSet(group);
- throw MakeStringException(ROXIE_MISSING_GROUP_ERROR, "Missing RIGHT group for Combine Group (%d)", factory->queryId());
- }
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t outSize = helper.transform(rowBuilder, left, group.ordinality(), (const void * *)group.getArray());
- ReleaseRoxieRow(left);
- ReleaseRoxieRowSet(group);
- if (outSize)
- {
- processed++;
- return rowBuilder.finalizeRowClear(outSize);
- }
- }
- catch (IException *E)
- {
- ReleaseRoxieRow(left);
- ReleaseRoxieRowSet(group);
- throw makeWrappedException(E);
- }
- }
- }
- };
- class CRoxieServerCombineGroupActivityFactory : public CRoxieServerActivityFactory
- {
- unsigned input2;
- unsigned input2idx;
- public:
- CRoxieServerCombineGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- input2 = 0;
- input2idx = 0;
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerCombineGroupActivity(this, _probeManager);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- if (idx==1)
- {
- input2 = source;
- input2idx = sourceidx;
- }
- else
- CRoxieServerActivityFactory::setInput(idx, source, sourceidx);
- }
- virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
- {
- switch (idx)
- {
- case 1:
- sourceidx = input2idx;
- return input2;
- case 0:
- return CRoxieServerActivityFactory::getInput(idx, sourceidx);
- default:
- return (unsigned) -1;
- }
- }
- };
- IRoxieServerActivityFactory *createRoxieServerCombineGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerCombineGroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerRollupGroupActivity : public CRoxieServerActivity
- {
- IHThorRollupGroupArg &helper;
- bool eof;
- public:
- CRoxieServerRollupGroupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager),
- helper((IHThorRollupGroupArg &)basehelper)
- {
- eof = false;
- }
- ~CRoxieServerRollupGroupActivity()
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- loop
- {
- ConstPointerArray group;
- loop
- {
- const void * in = input->nextInGroup();
- if (!in)
- break;
- group.append(in);
- }
- if (group.ordinality() == 0)
- {
- eof = true;
- return NULL;
- }
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t outSize = helper.transform(rowBuilder, group.ordinality(), (const void * *)group.getArray());
- ReleaseRoxieRowSet(group);
- if (outSize)
- {
- processed++;
- return rowBuilder.finalizeRowClear(outSize);
- }
- }
- catch (IException * E)
- {
- ReleaseRoxieRowSet(group);
- throw makeWrappedException(E);
- }
- }
- }
- };
- class CRoxieServerRollupGroupActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerRollupGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerRollupGroupActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerRollupGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerRollupGroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerFilterProjectActivity : public CRoxieServerLateStartActivity
- {
- IHThorFilterProjectArg &helper;
- unsigned numProcessedLastGroup;
- unsigned __int64 recordCount;
- public:
- CRoxieServerFilterProjectActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerLateStartActivity(_factory, _probeManager), helper((IHThorFilterProjectArg &)basehelper)
- {
- numProcessedLastGroup = 0;
- recordCount = 0;
- }
- ~CRoxieServerFilterProjectActivity()
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- numProcessedLastGroup = 0;
- recordCount = 0;
- CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
- lateStart(parentExtractSize, parentExtract, helper.canMatchAny()); //sets eof
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- loop
- {
- const void * in = input->nextInGroup();
- if (!in)
- {
- recordCount = 0;
- if (numProcessedLastGroup == processed)
- in = input->nextInGroup();
- if (!in)
- {
- numProcessedLastGroup = processed;
- return NULL;
- }
- }
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t outSize = helper.transform(rowBuilder, in, ++recordCount);
- ReleaseRoxieRow(in);
- if (outSize)
- {
- processed++;
- return rowBuilder.finalizeRowClear(outSize);
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- }
- };
- class CRoxieServerFilterProjectActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerFilterProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerFilterProjectActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerFilterProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerFilterProjectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerProjectActivity : public CRoxieServerActivity
- {
- unsigned numProcessedLastGroup;
- bool count;
- unsigned __int64 recordCount;
- public:
- CRoxieServerProjectActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _count)
- : CRoxieServerActivity(_factory, _probeManager),
- count(_count)
- {
- numProcessedLastGroup = 0;
- recordCount = 0;
- }
- ~CRoxieServerProjectActivity()
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- numProcessedLastGroup = 0;
- recordCount = 0;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- const void * in = input->nextInGroup();
- if (!in)
- {
- recordCount = 0;
- if (numProcessedLastGroup == processed)
- in = input->nextInGroup();
- if (!in)
- {
- numProcessedLastGroup = processed;
- return NULL;
- }
- }
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t outSize;
- if (count)
- outSize = ((IHThorCountProjectArg &) basehelper).transform(rowBuilder, in, ++recordCount);
- else
- outSize = ((IHThorProjectArg &) basehelper).transform(rowBuilder, in);
- ReleaseRoxieRow(in);
- if (outSize)
- {
- processed++;
- return rowBuilder.finalizeRowClear(outSize);
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- }
- };
- class CRoxieServerProjectActivityFactory : public CRoxieServerActivityFactory
- {
- protected:
- bool count;
- public:
- CRoxieServerProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- count = (_kind==TAKcountproject || _kind==TAKprefetchcountproject);
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerProjectActivity(this, _probeManager, count);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerProjectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerPrefetchProjectActivity : public CRoxieServerActivity, implements IRecordPullerCallback
- {
- unsigned numProcessedLastGroup;
- bool count;
- bool eof;
- bool allPulled;
- bool isThreaded;
- unsigned preload;
- unsigned __int64 recordCount;
- IHThorPrefetchProjectArg &helper;
- RecordPullerThread puller;
- InterruptableSemaphore ready;
- InterruptableSemaphore space;
- class PrefetchInfo : public CInterface
- {
- public:
- inline PrefetchInfo(IHThorPrefetchProjectArg &helper, const void *_in, unsigned __int64 _recordCount)
- {
- if (helper.preTransform(extract, _in, _recordCount))
- {
- in.setown(_in);
- recordCount = _recordCount;
- }
- else
- ReleaseRoxieRow(_in);
- }
- OwnedConstRoxieRow in;
- unsigned __int64 recordCount;
- rtlRowBuilder extract;
- };
- QueueOf<PrefetchInfo, true> pulled;
- CriticalSection pulledCrit;
- public:
- CRoxieServerPrefetchProjectActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _count)
- : CRoxieServerActivity(_factory, _probeManager),
- helper((IHThorPrefetchProjectArg &) basehelper),
- puller(false),
- count(_count)
- {
- numProcessedLastGroup = 0;
- recordCount = 0;
- eof = false;
- allPulled = false;
- isThreaded = (helper.getFlags() & PPFparallel) != 0;
- preload = 0;
- }
- ~CRoxieServerPrefetchProjectActivity()
- {
- while (pulled.ordinality())
- ::Release(pulled.dequeue());
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- if (idx)
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
- puller.setInput(this, _in);
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- numProcessedLastGroup = 0;
- recordCount = 0;
- eof = false;
- allPulled = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- preload = helper.getLookahead();
- if (!preload)
- preload = ctx->prefetchProjectPreload();
- space.reinit(preload);
- ready.reinit();
- puller.start(parentExtractSize, parentExtract, paused, preload, !isThreaded, ctx);
- }
- virtual void stop(bool aborting)
- {
- space.interrupt();
- ready.interrupt();
- CRoxieServerActivity::stop(aborting);
- puller.stop(aborting);
- }
- virtual void reset()
- {
- CRoxieServerActivity::reset();
- puller.reset();
- allPulled = false;
- while (pulled.ordinality())
- ::Release(pulled.dequeue());
- }
- virtual PrefetchInfo *readNextRecord()
- {
- if (!isThreaded)
- {
- if (!allPulled) // This looks like it's thread unsafe but we are inside the if(!isThreaded) so should be ok
- puller.pullRecords(1);
- }
- else
- ready.wait();
- CriticalBlock b(pulledCrit);
- PrefetchInfo *ret = pulled.ordinality() ? pulled.dequeue() : NULL;
- space.signal();
- return ret;
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- loop
- {
- Owned<PrefetchInfo> in = readNextRecord();
- if (!in)
- {
- recordCount = 0;
- if (numProcessedLastGroup == processed)
- in.setown(readNextRecord());
- if (!in)
- {
- numProcessedLastGroup = processed;
- eof = true;
- return NULL;
- }
- }
- try
- {
- if (in->in)
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t outSize;
- IThorChildGraph *child = helper.queryChild();
- Owned<IEclGraphResults> results;
- if (child)
- results.setown(child->evaluate(in->extract.size(), in->extract.getbytes()));
- outSize = helper.transform(rowBuilder, in->in, results, in->recordCount);
- if (outSize)
- {
- processed++;
- return rowBuilder.finalizeRowClear(outSize);
- }
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- }
- // interface IExceptionHandler
- virtual bool fireException(IException *e)
- {
- // called from puller thread on failure
- ready.interrupt(LINK(e));
- space.interrupt(e);
- return true;
- }
- // interface IRecordPullerCallback
- virtual void processRow(const void *row)
- {
- {
- CriticalBlock b(pulledCrit);
- pulled.enqueue(new PrefetchInfo(helper, row, ++recordCount));
- }
- if (isThreaded)
- {
- ready.signal();
- space.wait();
- }
- }
- virtual void processEOG()
- {
- {
- CriticalBlock b(pulledCrit);
- pulled.enqueue(NULL);
- }
- if (isThreaded)
- {
- ready.signal();
- space.wait();
- }
- }
- virtual void processGroup(const ConstPointerArray &rows)
- {
- throwUnexpected();
- }
- virtual void processDone()
- {
- CriticalBlock b(pulledCrit);
- allPulled = true;
- if (isThreaded)
- ready.signal();
- }
- };
- class CRoxieServerPrefetchProjectActivityFactory : public CRoxieServerProjectActivityFactory
- {
- public:
- CRoxieServerPrefetchProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerProjectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerPrefetchProjectActivity(this, _probeManager, count);
- }
- };
- extern IRoxieServerActivityFactory *createRoxieServerPrefetchProjectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerPrefetchProjectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CPointerArrayRoxieInput : public CPseudoRoxieInput
- {
- public:
- CPointerArrayRoxieInput()
- {
- array = NULL;
- curRow = 0;
- }
- void init(ConstPointerArray * _array)
- {
- array = _array;
- curRow = 0;
- }
- virtual const void * nextInGroup()
- {
- if (array->isItem(curRow))
- {
- const void * ret = array->item(curRow);
- array->replace(NULL, curRow);
- curRow++;
- return ret;
- }
- return NULL;
- }
- protected:
- ConstPointerArray * array;
- unsigned curRow;
- };
- class CRoxieServerLoopActivity : public CRoxieServerActivity
- {
- protected:
- IHThorLoopArg &helper;
- ThorActivityKind activityKind;
- unsigned maxIterations;
- bool finishedLooping;
- unsigned flags;
- bool eof;
- rtlRowBuilder loopExtractBuilder;
- unsigned loopGraphId;
- Linked<IOutputMetaData> counterMeta;
- public:
- CRoxieServerLoopActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _loopGraphId, IOutputMetaData * _counterMeta)
- : CRoxieServerActivity(_factory, _probeManager),
- helper((IHThorLoopArg &)basehelper), loopGraphId(_loopGraphId), counterMeta(_counterMeta)
- {
- eof = false;
- finishedLooping = false;
- activityKind = factory->getKind();
- flags = helper.getFlags();
- maxIterations = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- int iterations = (int) helper.numIterations();
- maxIterations = (iterations >= 0) ? iterations : 0;
- finishedLooping = ((activityKind == TAKloopcount) && (maxIterations == 0));
- if ((flags & IHThorLoopArg::LFnewloopagain) && !helper.loopFirstTime())
- finishedLooping = true;
- loopExtractBuilder.clear();
- helper.createParentExtract(loopExtractBuilder); // could possibly delay this until execution actually happens
- }
- virtual void stop(bool aborting)
- {
- CRoxieServerActivity::stop(aborting);
- loopExtractBuilder.clear();
- }
- void createCounterResult(IRoxieServerChildGraph * graph, unsigned counter)
- {
- if (flags & IHThorLoopArg::LFcounter)
- {
- void * counterRow = ctx->queryRowManager().allocate(sizeof(thor_loop_counter_t), activityId);
- *((thor_loop_counter_t *)counterRow) = counter;
- Owned<IGraphResult> counterResult = new CGraphResult(ctx->queryCodeContext()->getRowAllocator(counterMeta, activityId), counterRow, true);
- graph->setInputResult(2, counterResult);
- }
- }
- };
- //=================================================================================
- class CRoxieServerSequentialLoopActivity : public CRoxieServerLoopActivity
- {
- Owned<IActivityGraph> loopQuery;
- Owned<IRoxieServerChildGraph> loopGraph;
- IRoxieInput * curInput;
- ConstPointerArray loopPending;
- CPointerArrayRoxieInput arrayInput;
- Linked<IRoxieInput> resultInput;
- unsigned loopCounter;
- public:
- CRoxieServerSequentialLoopActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _loopGraphId, IOutputMetaData * _counterMeta)
- : CRoxieServerLoopActivity(_factory, _probeManager, _loopGraphId, _counterMeta)
- {
- curInput = NULL;
- loopCounter = 0;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerLoopActivity::onCreate(_ctx, _colocalParent);
- loopQuery.set(_ctx->queryChildGraph(loopGraphId));
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- curInput = input;
- loopCounter = 1;
- CRoxieServerLoopActivity::start(parentExtractSize, parentExtract, paused);
- //MORE: Not sure about this, should IRoxieServerChildGraph be combined with IActivityGraph?
- loopGraph.set(loopQuery->queryLoopGraph());
- }
- virtual void stop(bool aborting)
- {
- ReleaseRoxieRowSet(loopPending);
- CRoxieServerLoopActivity::stop(aborting);
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- unsigned emptyIterations = 0;
- loop
- {
- loop
- {
- const void * ret = curInput->nextInGroup();
- if (!ret)
- {
- ret = curInput->nextInGroup(); // more cope with groups somehow....
- if (!ret)
- {
- if (finishedLooping)
- {
- eof = true;
- return NULL;
- }
- break;
- }
- }
- if (finishedLooping ||
- ((flags & IHThorLoopArg::LFfiltered) && !helper.sendToLoop(loopCounter, ret)))
- {
- processed++;
- return ret;
- }
- loopPending.append(ret);
- }
- switch (activityKind)
- {
- case TAKloopdataset:
- {
- if (!(flags & IHThorLoopArg::LFnewloopagain))
- {
- if (!helper.loopAgain(loopCounter, loopPending.ordinality(), (const void * *)loopPending.getArray()))
- {
- if (loopPending.ordinality() == 0)
- {
- eof = true;
- return NULL;
- }
- arrayInput.init(&loopPending);
- curInput = &arrayInput;
- finishedLooping = true;
- continue; // back to the input loop again
- }
- }
- break;
- }
- case TAKlooprow:
- if (loopPending.empty())
- {
- finishedLooping = true;
- eof = true;
- return NULL;
- }
- break;
- }
- if (loopPending.ordinality())
- emptyIterations = 0;
- else
- {
- //note: any outputs which didn't go around the loop again, would return the record, reinitializing emptyIterations
- emptyIterations++;
- if (emptyIterations > maxEmptyLoopIterations)
- throw MakeStringException(ROXIE_TOO_MANY_EMPTY_LOOP, "Executed LOOP with empty input and output %u times", emptyIterations);
- if (emptyIterations % 32 == 0)
- CTXLOG("Executing LOOP with empty input and output %u times", emptyIterations);
- }
- checkAbort();
- try
- {
- Owned<IRoxieGraphResults> results = executeIteration(loopExtractBuilder.size(), loopExtractBuilder.getbytes(), loopCounter, loopPending);
- resultInput.setown(results->createIterator(0));
- if (flags & IHThorLoopArg::LFnewloopagain)
- {
- Owned<IRoxieInput> againResult = results->createIterator(helper.loopAgainResult());
- OwnedConstRoxieRow row = againResult->nextInGroup();
- assertex(row);
- //Result is a row which contains a single boolean field.
- if (!((const bool *)row.get())[0])
- finishedLooping = true;
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- curInput = resultInput.get();
- loopCounter++;
- if ((activityKind == TAKloopcount) && (loopCounter > maxIterations))
- finishedLooping = true;
- }
- }
- IRoxieGraphResults * executeIteration(unsigned parentExtractSize, const byte *parentExtract, unsigned counter, ConstPointerArray & rows)
- {
- try
- {
- loopGraph->beforeExecute();
- Owned<IGraphResult> inputRowsResult = new CGraphResult(ctx->queryCodeContext()->getRowAllocator(meta.queryOriginal(), activityId), rows, true);
- loopGraph->setInputResult(1, inputRowsResult);
- createCounterResult(loopGraph, counter);
- Owned<IRoxieGraphResults> ret = loopGraph->execute(parentExtractSize, parentExtract);
- loopGraph->afterExecute();
- return ret.getClear();
- }
- catch (...)
- {
- CTXLOG("Exception thrown in loop body - cleaning up");
- loopGraph->afterExecute();
- throw;
- }
- }
- };
- //=================================================================================
- typedef SafeQueueOf<const void, true> SafeRowQueue;
- typedef SimpleInterThreadQueueOf<const void, true> InterThreadRowQueue;
- class CRowQueuePseudoInput : public CPseudoRoxieInput
- {
- public:
- CRowQueuePseudoInput(SafeRowQueue & _input) :
- input(_input)
- {
- eof = false;
- }
- virtual const void * nextInGroup()
- {
- if (eof)
- return NULL;
- const void * ret = input.dequeue();
- if (!ret)
- eof = true;
- return ret;
- }
- protected:
- SafeRowQueue & input;
- bool eof;
- };
- class CRoxieServerParallelLoopActivity;
- class LoopFilterPseudoInput : public CIndirectRoxieInput
- {
- public:
- LoopFilterPseudoInput(CRoxieServerParallelLoopActivity * _activity, IRoxieInput * _input, unsigned _counter) :
- CIndirectRoxieInput(_input), activity(_activity), counter(_counter)
- {
- }
- virtual const void * nextInGroup();
- protected:
- CRoxieServerParallelLoopActivity * activity;
- unsigned counter;
- };
- class LoopExecutorThread : public RestartableThread
- {
- protected:
- Owned<IRoxieInput> safeInput;
- CRoxieServerParallelLoopActivity * activity;
- bool eof;
- CriticalSection crit;
- unsigned flags;
- SafeRowQueue tempResults[2];
- unsigned savedParentExtractSize;
- const byte * savedParentExtract;
- IArrayOf<IActivityGraph> cachedGraphs;
- IRoxieSlaveContext *ctx;
- public:
- LoopExecutorThread()
- : RestartableThread("LoopExecutorThread")
- {
- activity = NULL;
- eof = false;
- flags = 0;
- ctx = NULL;
- savedParentExtract = NULL;
- savedParentExtractSize = 0;
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- return safeInput->queryInput(idx);
- }
- void setInput(CRoxieServerParallelLoopActivity * _activity, IRoxieInput *_input, unsigned _flags)
- {
- activity = _activity;
- flags = _flags;
- // stop is called on our consumer's thread. We need to take care calling stop for our input to make sure it is not in mid-nextInGroup etc etc.
- safeInput.setown(new CSafeRoxieInput(_input));
- }
- IRoxieInput *queryInput() const
- {
- return safeInput;
- }
- void onCreate(IRoxieSlaveContext * _ctx);
- void start(unsigned parentExtractSize, const byte *parentExtract, bool paused);
- void stop(bool aborting);
- void reset();
- virtual int run();
- protected:
- void executeLoop();
- void executeLoopInstance(unsigned counter, unsigned numIterations, IRoxieInput * input, SafeRowQueue * spillOutput);
- IRoxieInput * createLoopIterationGraph(unsigned i, IRoxieInput * input, unsigned counter);
- };
- class CRoxieServerParallelLoopActivity : public CRoxieServerLoopActivity
- {
- friend class LoopFilterPseudoInput;
- friend class LoopExecutorThread;
- QueueOf<const void, true> ready;
- CriticalSection helperCS;
- CriticalSection cs;
- size32_t sizeNumParallel;
- rtlDataAttr listNumParallel;
- unsigned defaultNumParallel;
- LoopExecutorThread executor;
- IProbeManager* probeManager;
- CriticalSection canAccess;
- CriticalSection scrit;
- InterruptableSemaphore readySpace;
- InterruptableSemaphore recordsReady;
- protected:
- bool includeInLoop(unsigned counter, const void * row)
- {
- CriticalBlock b(helperCS);
- return helper.sendToLoop(counter, row);
- }
- public:
- CRoxieServerParallelLoopActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _loopGraphId, IOutputMetaData * _counterMeta)
- : CRoxieServerLoopActivity(_factory, _probeManager, _loopGraphId, _counterMeta),
- readySpace(parallelLoopFlowLimit)
- {
- probeManager = _probeManager;
- defaultNumParallel = 0;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- executor.onCreate(_ctx);
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CriticalBlock b(scrit); // can stop while still starting, if unlucky...
- readySpace.reinit(parallelLoopFlowLimit);
- recordsReady.reinit();
- CRoxieServerLoopActivity::start(parentExtractSize, parentExtract, paused);
- defaultNumParallel = helper.defaultParallelIterations();
- if (!defaultNumParallel)
- defaultNumParallel = DEFAULT_PARALLEL_LOOP_THREADS;
- helper.numParallelIterations(sizeNumParallel, listNumParallel.refdata());
- //MORE: If numIterations <= number of parallel iterations[1],
- //then we don't need to create a separate thread to do the processing, and the results will also avoid
- //being transferred via a queue
- executor.start(parentExtractSize, parentExtract, paused);
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- if (idx)
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
- executor.setInput(this, _in, flags);
- }
- virtual void stop(bool aborting)
- {
- CriticalBlock b(scrit); // can stop while still starting, if unlucky...
- readySpace.interrupt();
- recordsReady.interrupt();
- executor.join(); // MORE - may not be needed given stop/reset split
- CRoxieServerLoopActivity::stop(aborting);
- }
- virtual void reset()
- {
- while (ready.ordinality())
- ReleaseRoxieRow(ready.dequeue());
- executor.reset();
- CRoxieServerActivity::reset();
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- if (eof)
- return NULL;
- recordsReady.wait();
- CriticalBlock procedure(canAccess);
- if (ready.ordinality())
- {
- const void *result = ready.dequeue();
- readySpace.signal();
- if (result)
- processed++;
- return result;
- }
- else
- eof = true;
- }
- }
- unsigned getNumParallel(unsigned iter)
- {
- if (iter * sizeof(unsigned) >= sizeNumParallel)
- return defaultNumParallel;
- return ((unsigned *)listNumParallel.getdata())[iter];
- }
- inline void enqueueResult(const void * row)
- {
- try
- {
- while(!readySpace.wait(1000))
- {
- CTXLOG("Blocked waiting for space in loop %p activity id: %d output queue: %d records in queue", this, queryId(), ready.ordinality());
- }
- }
- catch (...)
- {
- ReleaseRoxieRow(row);
- throw;
- }
- CriticalBlock b2(canAccess);
- ready.enqueue(row);
- recordsReady.signal();
- }
- inline void finishResults()
- {
- recordsReady.signal();
- }
- virtual bool fireException(IException *e)
- {
- readySpace.interrupt(LINK(e));
- recordsReady.interrupt(e);
- return true;
- }
- IActivityGraph * createChildGraphInstance()
- {
- return factory->createChildGraph(ctx, &helper, loopGraphId, this, probeManager, *this);
- }
- IActivityGraph * queryChildGraph()
- {
- return ctx->queryChildGraph(loopGraphId);
- }
- };
- //=================================================================================
- const void * LoopFilterPseudoInput::nextInGroup()
- {
- loop
- {
- const void * next = input->nextInGroup();
- if (!next || activity->includeInLoop(counter, next))
- return next;
- activity->enqueueResult(next);
- }
- }
- void LoopExecutorThread::onCreate(IRoxieSlaveContext * _ctx)
- {
- //Initialise the cached graph list with the child instance that will always be created. Other iterations will be created on demand.
- ctx = _ctx;
- cachedGraphs.append(*LINK(activity->queryChildGraph()));
- }
- void LoopExecutorThread::start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- savedParentExtractSize = parentExtractSize;
- savedParentExtract = parentExtract;
- eof = false;
- StringBuffer logPrefix("[");
- ctx->getLogPrefix(logPrefix).append("] ");
- RestartableThread::start(logPrefix);
- }
- int LoopExecutorThread::run()
- {
- try
- {
- executeLoop();
- }
- catch (IException *e)
- {
- activity->fireException(e);
- }
- catch (...)
- {
- activity->fireException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unexpected exception caught in LoopExecutorThread::run"));
- }
- return 0;
- }
- void LoopExecutorThread::stop(bool aborting)
- {
- safeInput->stop(aborting);
- RestartableThread::join();
- }
- void LoopExecutorThread::reset()
- {
- safeInput->reset();
- }
- void LoopExecutorThread::executeLoop()
- {
- unsigned iterations = 0;
- unsigned counter = 0;
- unsigned outputIndex = 0;
- //Note, activities don't link inputs, so need to be careful that special inputs remain linked while the activity is executing.
- loop
- {
- if (activity->activityKind == TAKloopcount)
- {
- if (counter == activity->maxIterations)
- break;
- }
- else
- {
- //This condition isn't quite right because it needs to be whether the filtered
- //input is empty. May be ok if we include that in the semantics,
- if (tempResults[1-outputIndex].ordinality() == 0)
- break;
- }
- unsigned numParallel = activity->getNumParallel(iterations);
- Linked<IRoxieInput> curInput;
- if (iterations == 0)
- curInput.set(safeInput);
- else
- {
- SafeRowQueue & inputQueue = tempResults[1-outputIndex];
- inputQueue.enqueue(NULL);
- curInput.setown(new CRowQueuePseudoInput(inputQueue));
- }
- SafeRowQueue * curOutput = NULL;
- if (counter+numParallel > activity->maxIterations)
- numParallel = activity->maxIterations - counter;
- else if (counter+numParallel < activity->maxIterations)
- curOutput = &tempResults[outputIndex];
- executeLoopInstance(counter, numParallel, curInput, curOutput);
- outputIndex = 1-outputIndex;
- counter += numParallel;
- iterations++;
- }
- //Check for TAKlooprow, where end of loop couldn't be determined ahead of time
- SafeRowQueue & inputQueue = tempResults[1-outputIndex];
- while (inputQueue.ordinality())
- {
- const void * next = inputQueue.dequeue();
- activity->enqueueResult(next);
- }
- activity->finishResults();
- }
- void LoopExecutorThread::executeLoopInstance(unsigned counter, unsigned numIterations, IRoxieInput * input, SafeRowQueue * spillOutput)
- {
- IArrayOf<IRoxieInput> savedInputs; // activities don't link their inputs, so this list keeps filters alive.
- Linked<IRoxieInput> curInput = input;
- unsigned i;
- for (i= 0; i != numIterations; i++)
- {
- unsigned thisCounter = counter+i+1;
- IRoxieInput * filtered = curInput;
- if (flags & IHThorLoopArg::LFfiltered)
- {
- filtered = new LoopFilterPseudoInput(activity, curInput, thisCounter);
- savedInputs.append(*filtered);
- }
- //graph is kept, so new curInput will be guaranteed to exist
- curInput.setown(createLoopIterationGraph(i, filtered, thisCounter));
- }
- try
- {
- curInput->start(savedParentExtractSize, savedParentExtract, false);
- if (spillOutput)
- {
- loop
- {
- const void * next = curInput->nextInGroup();
- if (!next)
- break;
- spillOutput->enqueue(next);
- }
- }
- else
- {
- loop
- {
- const void * next = curInput->nextInGroup();
- if (!next)
- break;
- activity->enqueueResult(next);
- }
- }
- }
- catch (IException *E)
- {
- ctx->notifyAbort(E);
- for (i= 0; i != numIterations; i++)
- {
- cachedGraphs.item(i).queryLoopGraph()->afterExecute();
- }
- curInput->stop(true);
- curInput->reset();
- throw;
- }
- for (i= 0; i != numIterations; i++)
- {
- cachedGraphs.item(i).queryLoopGraph()->afterExecute();
- }
- curInput->stop(false);
- curInput->reset();
- }
- IRoxieInput * LoopExecutorThread::createLoopIterationGraph(unsigned i, IRoxieInput * input, unsigned counter)
- {
- if (!cachedGraphs.isItem(i))
- cachedGraphs.append(*activity->createChildGraphInstance());
- Linked<IRoxieServerChildGraph> loopGraph = cachedGraphs.item(i).queryLoopGraph();
- loopGraph->beforeExecute();
- if (!loopGraph->querySetInputResult(1, input))
- throwUnexpected(); // a loop which doesn't use the value from the previous iteration. Should probably handle even if daft.
- activity->createCounterResult(loopGraph, counter);
- return loopGraph->selectOutput(0);
- }
- //=================================================================================
- class CCounterRowMetaData : public CInterface, implements IOutputMetaData
- {
- public:
- IMPLEMENT_IINTERFACE
- virtual size32_t getRecordSize(const void *) { return sizeof(thor_loop_counter_t); }
- virtual size32_t getMinRecordSize() const { return sizeof(thor_loop_counter_t); }
- virtual size32_t getFixedSize() const { return sizeof(thor_loop_counter_t); }
- virtual void toXML(const byte * self, IXmlWriter & out) { }
- virtual unsigned getVersion() const { return OUTPUTMETADATA_VERSION; }
- virtual unsigned getMetaFlags() { return 0; }
- virtual void destruct(byte * self) {}
- virtual IOutputRowSerializer * createRowSerializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
- virtual IOutputRowDeserializer * createRowDeserializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
- virtual ISourceRowPrefetcher * createRowPrefetcher(ICodeContext * ctx, unsigned activityId) { return NULL; }
- virtual IOutputMetaData * querySerializedMeta() { return this; }
- virtual void walkIndirectMembers(const byte * self, IIndirectMemberVisitor & visitor) {}
- };
- class CRoxieServerLoopActivityFactory : public CRoxieServerActivityFactory
- {
- unsigned loopGraphId;
- unsigned flags;
- Linked<IOutputMetaData> counterMeta;
- public:
- CRoxieServerLoopActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _loopGraphId)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), loopGraphId(_loopGraphId)
- {
- Owned<IHThorLoopArg> helper = (IHThorLoopArg *) helperFactory();
- flags = helper->getFlags();
- counterMeta.setown(new CCounterRowMetaData);
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- if (flags & IHThorLoopArg::LFparallel)
- return new CRoxieServerParallelLoopActivity(this, _probeManager, loopGraphId, counterMeta);
- else
- return new CRoxieServerSequentialLoopActivity(this, _probeManager, loopGraphId, counterMeta);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerLoopActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _loopGraphId)
- {
- return new CRoxieServerLoopActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _loopGraphId);
- }
- //=================================================================================
- class CRoxieServerGraphLoopActivity : public CRoxieServerActivity
- {
- protected:
- IHThorGraphLoopArg &helper;
- unsigned maxIterations;
- unsigned flags;
- rtlRowBuilder GraphExtractBuilder;
- unsigned loopGraphId;
- Linked<IOutputMetaData> counterMeta;
- public:
- CRoxieServerGraphLoopActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _GraphGraphId, IOutputMetaData * _counterMeta)
- : CRoxieServerActivity(_factory, _probeManager),
- helper((IHThorGraphLoopArg &)basehelper), loopGraphId(_GraphGraphId), counterMeta(_counterMeta)
- {
- flags = helper.getFlags();
- maxIterations = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- int iterations = (int) helper.numIterations();
- maxIterations = (iterations >= 0) ? iterations : 0;
- if (maxIterations > maxGraphLoopIterations)
- throw MakeStringException(ROXIE_TOO_MANY_GRAPH_LOOP, "Attempt to execute graph %u times", maxIterations);
- if (maxIterations != 0)
- {
- GraphExtractBuilder.clear();
- helper.createParentExtract(GraphExtractBuilder);
- }
- }
- virtual void stop(bool aborting)
- {
- CRoxieServerActivity::stop(aborting);
- GraphExtractBuilder.clear();
- }
- virtual bool needsAllocator() const { return true; }
- void createCounterResult(IRoxieServerChildGraph * graph, unsigned counter)
- {
- if (flags & IHThorGraphLoopArg::GLFcounter)
- {
- void * counterRow = ctx->queryRowManager().allocate(sizeof(thor_loop_counter_t), activityId);
- *((thor_loop_counter_t *)counterRow) = counter;
- Owned<IGraphResult> counterResult = new CGraphResult(ctx->queryCodeContext()->getRowAllocator(counterMeta, activityId), counterRow, true);
- graph->setInputResult(0, counterResult);
- }
- }
- };
- //=================================================================================
- class CRoxieServerSequentialGraphLoopActivity : public CRoxieServerGraphLoopActivity
- {
- Owned<IActivityGraph> GraphQuery;
- Owned<IRoxieServerChildGraph> loopGraph;
- Linked<IRoxieInput> resultInput;
- bool evaluated;
- public:
- CRoxieServerSequentialGraphLoopActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _GraphGraphId, IOutputMetaData * _counterMeta)
- : CRoxieServerGraphLoopActivity(_factory, _probeManager, _GraphGraphId, _counterMeta)
- {
- evaluated = false;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerGraphLoopActivity::onCreate(_ctx, _colocalParent);
- GraphQuery.set(_ctx->queryChildGraph(loopGraphId));
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerGraphLoopActivity::start(parentExtractSize, parentExtract, paused);
- //MORE: Not sure about this, should IRoxieServerChildGraph be combined with IActivityGraph?
- loopGraph.set(GraphQuery->queryLoopGraph());
- evaluated = false;
- }
- virtual void stop(bool aborting)
- {
- if (loopGraph)
- loopGraph->clearGraphLoopResults();
- CRoxieServerGraphLoopActivity::stop(aborting);
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (!evaluated)
- {
- executeEntireGraph();
- evaluated = true;
- }
- const void * ret = resultInput->nextInGroup();
- if (ret)
- processed++;
- return ret;
- }
- void executeIteration(unsigned parentExtractSize, const byte *parentExtract, unsigned counter)
- {
- try
- {
- loopGraph->beforeExecute();
- createCounterResult(loopGraph, counter);
- loopGraph->executeGraphLoop(parentExtractSize, parentExtract);
- loopGraph->afterExecute();
- }
- catch (...)
- {
- CTXLOG("Exception thrown in graph body - cleaning up");
- loopGraph->afterExecute();
- throw;
- }
- }
- void createInitialGraphInput()
- {
- loopGraph->clearGraphLoopResults();
- ConstPointerArray rows;
- try
- {
- loop
- {
- const void *nextrec = input->nextInGroup();
- if (!nextrec)
- {
- nextrec = input->nextInGroup();
- if (!nextrec)
- break;
- rows.append(NULL);
- }
- rows.append(nextrec);
- }
- }
- catch(IException *)
- {
- ReleaseRoxieRowSet(rows);
- throw;
- }
- IOutputMetaData *outputMeta = input->queryOutputMeta();
- Owned<CGraphResult> result = new CGraphResult(ctx->queryCodeContext()->getRowAllocator(outputMeta, activityId), rows, true);
- loopGraph->setGraphLoopResult(0, result);
- }
- void executeEntireGraph()
- {
- createInitialGraphInput();
- for (unsigned loopCounter=1; loopCounter <= maxIterations; loopCounter++)
- {
- executeIteration(GraphExtractBuilder.size(), GraphExtractBuilder.getbytes(), loopCounter);
- }
- resultInput.setown(loopGraph->getGraphLoopResult(maxIterations));
- }
- };
- //=================================================================================
- struct GraphOutputSplitterArg : public ccdserver_hqlhelper::CThorSplitArg
- {
- public:
- virtual unsigned numBranches()
- {
- return 0;
- }
- virtual IOutputMetaData * queryOutputMeta()
- {
- return NULL;// get it from the parent..
- }
- };
- extern "C" IHThorArg * createGraphOutputSplitter() { return new GraphOutputSplitterArg; }
- class CGraphIterationInfo : public CInterface
- {
- private:
- Owned<IRoxieServerActivityFactory> factory; // Note - before sourceAct, so destroyed last
- unsigned sourceIdx;
- Linked<IRoxieServerActivity> sourceAct;
- Linked<IRoxieInput> sourceInput;
- unsigned numUses;
- unsigned iteration;
- public:
- CGraphIterationInfo(IRoxieServerActivity * _sourceAct, IRoxieInput *_input, unsigned _sourceIdx, unsigned _iteration)
- : sourceAct(_sourceAct), sourceInput(_input), sourceIdx(_sourceIdx), iteration(_iteration)
- {
- numUses = 0;
- }
- inline void noteUsed()
- {
- numUses++;
- }
- void createSplitter(IRoxieSlaveContext *ctx, IProbeManager *probeManager)
- {
- if (numUses > 1)
- {
- factory.setown(createRoxieServerThroughSpillActivityFactory(sourceAct->queryFactory()->queryQueryFactory(), createGraphOutputSplitter, numUses));
- IRoxieServerActivity *splitter = factory->createActivity(NULL);
- splitter->onCreate(ctx, NULL);
- IRoxieInput *input = sourceAct->queryOutput(sourceIdx);
- if (probeManager)
- {
- IInputBase * inputBase = probeManager->createProbe(static_cast<IInputBase*>(input), sourceAct, splitter, sourceIdx, 0, iteration);
- input = static_cast<IRoxieInput*>(inputBase);
- // MORE - shouldn't this be added to probes?
- }
- sourceAct.setown(splitter);
- sourceAct->setInput(0, input);
- sourceIdx = 0;
- sourceInput.clear();
- }
- }
- IRoxieInput *connectOutput(IProbeManager *probeManager, IArrayOf<IRoxieInput> &probes, IRoxieServerActivity *targetAct, unsigned targetIdx)
- {
- // MORE - not really necessary to create splitters in separate pass, is it?
- if (factory) // we created a splitter....
- sourceInput.set(sourceAct->queryOutput(sourceIdx));
- IRoxieInput *ret = sourceInput;
- if (probeManager)
- {
- IInputBase *inputBase = probeManager->createProbe(ret, sourceAct, targetAct, sourceIdx, targetIdx, iteration);
- ret = static_cast<IRoxieInput *>(inputBase);
- probes.append(*LINK(ret));
- }
- if (factory) // we created a splitter....
- sourceIdx++;
- return ret;
- }
- };
- class CRoxieServerParallelGraphLoopActivity : public CRoxieServerGraphLoopActivity, implements IRoxieServerLoopResultProcessor
- {
- Owned<IActivityGraph> childGraph;
- IRoxieInput * resultInput;
- CIArrayOf<CGraphIterationInfo> outputs;
- IArrayOf<IRoxieServerChildGraph> iterationGraphs;
- Owned<CExtractMapperInput> inputExtractMapper;
- IProbeManager *probeManager;
- unsigned createLoopCounter;
- IArrayOf<IRoxieInput> probes;
- public:
- CRoxieServerParallelGraphLoopActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _GraphGraphId, IOutputMetaData * _counterMeta)
- : CRoxieServerGraphLoopActivity(_factory, _probeManager, _GraphGraphId, _counterMeta), probeManager(_probeManager)
- {
- inputExtractMapper.setown(new CExtractMapperInput);
- resultInput = NULL;
- createLoopCounter = 0;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerGraphLoopActivity::onCreate(_ctx, _colocalParent);
- childGraph.set(_ctx->queryChildGraph(loopGraphId));
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- //Input needs to be handled very carefully.....
- //We don't want to call onStart on the input unless it is actually used, so don't use the base CRoxieServerActivity implementation.
- //This activity's input needs to be started with (parentExtractSize, parentExtract), but the elements in the graph need to be started with the
- //GraphExtractBuilder parent extract. So we need to wrap the input in a pseudo-input (inputExtractMapper) that passes through a different
- //parentExtract. Something very similar will be needed for query library calls with streaming inputs when they are implemented.
- assertex(idx == 0);
- inputExtractMapper->setInput(_in);
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerGraphLoopActivity::start(parentExtractSize, parentExtract, paused); // initialises GraphExtractBuilder
- inputExtractMapper->setParentExtract(parentExtractSize, parentExtract);
- createExpandedGraph(GraphExtractBuilder.size(), GraphExtractBuilder.getbytes(), probeManager);
- resultInput->start(GraphExtractBuilder.size(), GraphExtractBuilder.getbytes(), paused);
- }
- virtual void stop(bool aborting)
- {
- if (resultInput)
- resultInput->stop(aborting);
- CRoxieServerGraphLoopActivity::stop(aborting);
- }
- virtual void reset()
- {
- if (resultInput)
- resultInput->reset();
- resultInput = NULL;
- iterationGraphs.kill();
- outputs.kill();
- if (probeManager)
- {
- probeManager->deleteGraph(NULL, (IArrayOf<IInputBase>*)&probes);
- probes.kill();
- }
- CRoxieServerGraphLoopActivity::reset();
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void * ret = resultInput->nextInGroup();
- if (ret)
- processed++;
- return ret;
- }
- void createExpandedGraph(unsigned parentExtractSize, const byte *parentExtract, IProbeManager *probeManager)
- {
- //result(0) is the input to the graph.
- resultInput = inputExtractMapper;
- outputs.append(* new CGraphIterationInfo(resultInput->queryActivity(), resultInput, 0, 1));
- for (createLoopCounter=1; createLoopCounter <= maxIterations; createLoopCounter++)
- {
- IRoxieServerChildGraph * graph = childGraph->createGraphLoopInstance(createLoopCounter, parentExtractSize, parentExtract, *this);
- graph->beforeExecute();
- iterationGraphs.append(*graph);
- graph->gatherIterationUsage(*this);
- CGraphIterationInfo *iteration = graph->selectGraphLoopOutput();
- outputs.append(*iteration);
- }
- createLoopCounter = 0;
- createSplitters(probeManager);
- ForEachItemIn(i2, iterationGraphs)
- iterationGraphs.item(i2).associateIterationOutputs(*this);
- resultInput = outputs.tos().connectOutput(probeManager, probes, this, 0);
- }
- void createSplitters(IProbeManager *probeManager)
- {
- ForEachItemIn(i, outputs)
- {
- CGraphIterationInfo & next = outputs.item(i);
- next.createSplitter(ctx, probeManager);
- }
- }
- //IRoxieServerLoopResultProcessor
- virtual void noteUseIteration(unsigned _whichIteration)
- {
- int whichIteration = (int) _whichIteration; // May go negative - API is unsigned for historical reasons
- if (whichIteration >= 0)
- {
- if (!outputs.isItem(whichIteration))
- throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Error reading graph result %d from iteration %d", whichIteration, createLoopCounter);
- outputs.item(whichIteration).noteUsed();
- }
- }
- virtual IRoxieInput * connectIterationOutput(unsigned whichIteration, IProbeManager *probeManager, IArrayOf<IRoxieInput> &probes, IRoxieServerActivity *targetAct, unsigned targetIdx)
- {
- if (outputs.isItem(whichIteration))
- {
- CGraphIterationInfo & next = outputs.item(whichIteration);
- return next.connectOutput(probeManager, probes, targetAct, targetIdx);
- }
- return NULL;
- }
- };
- //=================================================================================
- class CRoxieServerGraphLoopActivityFactory : public CRoxieServerActivityFactory
- {
- unsigned loopGraphId;
- unsigned flags;
- Linked<IOutputMetaData> counterMeta;
- public:
- CRoxieServerGraphLoopActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _loopGraphId)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), loopGraphId(_loopGraphId)
- {
- Owned<IHThorGraphLoopArg> helper = (IHThorGraphLoopArg *) helperFactory();
- flags = helper->getFlags();
- counterMeta.setown(new CCounterRowMetaData);
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- if (kind == TAKparallelgraphloop)
- return new CRoxieServerParallelGraphLoopActivity(this, _probeManager, loopGraphId, counterMeta);
- else
- return new CRoxieServerSequentialGraphLoopActivity(this, _probeManager, loopGraphId, counterMeta);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerGraphLoopActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _loopGraphId)
- {
- return new CRoxieServerGraphLoopActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _loopGraphId);
- }
- //=====================================================================================================
- class CRoxieServerLibraryCallActivity : public CRoxieServerActivity
- {
- class OutputAdaptor : public CExtractMapperInput
- {
- bool stopped;
- public:
- CRoxieServerLibraryCallActivity *parent;
- unsigned oid;
- unsigned processed;
- public:
- IMPLEMENT_IINTERFACE;
- OutputAdaptor() : CExtractMapperInput(NULL)
- {
- parent = NULL;
- oid = 0;
- init();
- }
- void init()
- {
- processed = 0;
- stopped = false;
- }
- virtual unsigned queryId() const
- {
- return parent->queryId();
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- parent->start(oid, parentExtractSize, parentExtract, paused);
- CExtractMapperInput::start(parentExtractSize, parentExtract, paused);
- }
- virtual void stop(bool aborting)
- {
- if (!stopped)
- {
- stopped = true;
- parent->stop(oid, aborting); // parent code relies on stop being called exactly once per adaptor, so make sure it is!
- CExtractMapperInput::stop(aborting);
- }
- };
- virtual void reset()
- {
- parent->reset(oid, processed);
- CExtractMapperInput::reset();
- init();
- };
- virtual void checkAbort()
- {
- parent->checkAbort();
- }
- };
-
- IHThorLibraryCallArg &helper;
- unsigned numInputs;
- unsigned numOutputs;
- unsigned numActiveOutputs;
- bool started;
- OutputAdaptor* outputAdaptors;
- CExtractMapperInput * * inputAdaptors;
- bool * inputUsed;
- bool * outputUsed;
- Owned<IException> error;
- CriticalSection crit;
- rtlRowBuilder libraryExtractBuilder;
- Owned<IActivityGraph> libraryGraph;
- const LibraryCallFactoryExtra & extra;
- public:
- CRoxieServerLibraryCallActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs, unsigned _numOutputs, const LibraryCallFactoryExtra & _extra)
- : CRoxieServerActivity(_factory, _probeManager),
- helper((IHThorLibraryCallArg &)basehelper), extra(_extra)
- {
- numInputs = _numInputs;
- numOutputs = _numOutputs;
- numActiveOutputs = numOutputs;
- inputAdaptors = new CExtractMapperInput*[numInputs];
- inputUsed = new bool[numInputs];
- for (unsigned i1 = 0; i1 < numInputs; i1++)
- {
- inputAdaptors[i1] = new CExtractMapperInput;
- inputUsed[i1] = false;
- }
- outputAdaptors = new OutputAdaptor[numOutputs];
- outputUsed = new bool[numOutputs];
- for (unsigned i2 = 0; i2 < numOutputs; i2++)
- {
- outputAdaptors[i2].parent = this;
- outputAdaptors[i2].oid = i2;
- outputUsed[i2] = false;
- }
- started = false;
- }
- ~CRoxieServerLibraryCallActivity()
- {
- for (unsigned i1 = 0; i1 < numInputs; i1++)
- ::Release(inputAdaptors[i1]);
- delete [] inputAdaptors;
- delete [] inputUsed;
- delete [] outputAdaptors;
- delete [] outputUsed;
- }
-
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- libraryGraph.setown(_ctx->getLibraryGraph(extra, this));
- libraryGraph->onCreate(_ctx, _colocalParent);
- //Now map the inputs and outputs to the adapters
- IRoxieServerChildGraph * graph = libraryGraph->queryLoopGraph();
- for (unsigned i1=0; i1<numInputs; i1++)
- inputUsed[i1] = graph->querySetInputResult(i1, inputAdaptors[i1]);
- for (unsigned i2=0; i2<numOutputs; i2++)
- {
- unsigned outputIndex = extra.outputs.item(i2);
- Owned<IRoxieInput> output = graph->selectOutput(numInputs+outputIndex);
- outputAdaptors[i2].setInput(output);
- }
- }
- virtual void start(unsigned oid, unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CriticalBlock b(crit);
- if (error)
- throw error.getLink();
- if (factory)
- factory->noteStarted(oid);
- if (!started)
- {
- // even though it is not complete, we don't want to run this again if it fails.
- started = true;
- //see notes on splitter above
- try
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- catch (IException *E)
- {
- #ifdef TRACE_SPLIT
- CTXLOG("spill %d caught exception in start", activityId);
- #endif
- error.set(E);
- throw;
- }
- catch (...)
- {
- IException *E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught in CRoxieServerLibraryCallActivity::start");
- error.set(E);
- throw E;
- }
- //recreate the parent extract, and use it to reinitialize the graphs...
- libraryExtractBuilder.clear();
- helper.createParentExtract(libraryExtractBuilder);
- // 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
- // some inputs before we see start on others.
- for (unsigned i1 = 0; i1 < numInputs; i1++)
- {
- if (inputUsed[i1])
- inputAdaptors[i1]->setParentExtract(parentExtractSize, parentExtract);
- else
- inputAdaptors[i1]->stop(false);
- }
- for (unsigned i2 = 0; i2 < numOutputs; i2++)
- outputAdaptors[i2].setParentExtract(libraryExtractBuilder.size(), libraryExtractBuilder.getbytes());
- //call stop on all the unused inputs.
- IRoxieServerChildGraph * graph = libraryGraph->queryLoopGraph();
- graph->beforeExecute();
- ForEachItemIn(i3, extra.unusedOutputs)
- {
- Owned<IRoxieInput> output = graph->selectOutput(numInputs+extra.unusedOutputs.item(i3));
- output->stop(false);
- }
- }
- }
- virtual void stop(unsigned oid, bool aborting)
- {
- CriticalBlock b(crit);
- if (--numActiveOutputs == 0)
- {
- //call stop on all the unused inputs.
- IRoxieServerChildGraph * graph = libraryGraph->queryLoopGraph();
- graph->beforeExecute();
- ForEachItemIn(i3, extra.unusedOutputs)
- {
- Owned<IRoxieInput> output = graph->selectOutput(numInputs+extra.unusedOutputs.item(i3));
- output->stop(false);
- }
- CRoxieServerActivity::stop(aborting);
- }
- }
- void reset(unsigned oid, unsigned _processed)
- {
- noteProcessed(oid, _processed, 0, 0);
- started = false;
- error.clear();
- numActiveOutputs = numOutputs;
- if (state != STATEreset) // make sure input is only reset once
- {
- CRoxieServerActivity::reset();
- libraryGraph->reset();
- //Call reset on all unused outputs from the graph - no one else will.
- IRoxieServerChildGraph * graph = libraryGraph->queryLoopGraph();
- ForEachItemIn(i3, extra.unusedOutputs)
- {
- Owned<IRoxieInput> output = graph->selectOutput(numInputs+extra.unusedOutputs.item(i3));
- output->reset();
- }
- }
- };
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- inputAdaptors[idx]->setInput(_in);
- }
- public:
- virtual const void *nextInGroup()
- {
- throwUnexpected(); // Internal logic error - we are not anybody's input
- }
- virtual IOutputMetaData * queryOutputMeta() const
- {
- throwUnexpected(); // should be called on outputs instead
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- assertex(idx!=(unsigned)-1);
- assertex(!outputUsed[idx]);
- outputUsed[idx] = true;
- return &outputAdaptors[idx];
- }
- };
- void LibraryCallFactoryExtra::set(const LibraryCallFactoryExtra & _other)
- {
- ForEachItemIn(i1, _other.outputs)
- outputs.append(_other.outputs.item(i1));
- ForEachItemIn(i2, _other.unusedOutputs)
- unusedOutputs.append(_other.unusedOutputs.item(i2));
- maxOutputs = _other.maxOutputs;
- graphid = _other.graphid;
- libraryName.set(_other.libraryName);
- interfaceHash = _other.interfaceHash;
- embedded = _other.embedded;
- }
- void LibraryCallFactoryExtra::calcUnused()
- {
- for (unsigned i=0; i < maxOutputs; i++)
- if (!outputs.contains(i))
- unusedOutputs.append(i);
- }
- class CRoxieServerLibraryCallActivityFactory : public CRoxieServerMultiOutputFactory
- {
- private:
- CRoxieServerMultiInputInfo inputs;
- LibraryCallFactoryExtra extra;
- public:
- CRoxieServerLibraryCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, LibraryCallFactoryExtra & _extra)
- : CRoxieServerMultiOutputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- extra.set(_extra);
- extra.calcUnused();
- setNumOutputs(extra.outputs.ordinality());
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerLibraryCallActivity(this, _probeManager, numInputs(), numOutputs, extra);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- inputs.set(idx, source, sourceidx);
- }
- virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
- {
- return inputs.get(idx, sourceidx);
- }
- inline unsigned numInputs() const { return inputs.ordinality(); }
- };
- IRoxieServerActivityFactory *createRoxieServerLibraryCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, LibraryCallFactoryExtra & _extra)
- {
- return new CRoxieServerLibraryCallActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _extra);
- }
- //=====================================================================================================
- class CRoxieServerNWayInputActivity : public CRoxieServerActivity
- {
- IHThorNWayInputArg & helper;
- IRoxieInput ** inputs;
- PointerArrayOf<IRoxieInput> selectedInputs;
- unsigned numInputs;
- public:
- CRoxieServerNWayInputActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorNWayInputArg &)basehelper), numInputs(_numInputs)
- {
- inputs = new IRoxieInput*[numInputs];
- for (unsigned i = 0; i < numInputs; i++)
- inputs[i] = NULL;
- }
- ~CRoxieServerNWayInputActivity()
- {
- delete [] inputs;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- bool selectionIsAll;
- size32_t selectionLen;
- rtlDataAttr selection;
- helper.getInputSelection(selectionIsAll, selectionLen, selection.refdata());
- selectedInputs.kill();
- if (selectionIsAll)
- {
- for (unsigned i=0; i < numInputs; i++)
- selectedInputs.append(inputs[i]);
- }
- else
- {
- const size32_t * selections = (const size32_t *)selection.getdata();
- unsigned max = selectionLen/sizeof(size32_t);
- for (unsigned i = 0; i < max; i++)
- {
- unsigned nextIndex = selections[i];
- //Check there are no duplicates..... Assumes there are a fairly small number of inputs, so n^2 search is ok.
- for (unsigned j=i+1; j < max; j++)
- {
- if (nextIndex == selections[j])
- throw MakeStringException(ROXIE_NWAY_INPUT_ERROR, "Selection list for nway input can not contain duplicates");
- }
- if (nextIndex > numInputs)
- throw MakeStringException(ROXIE_NWAY_INPUT_ERROR, "Index %d in RANGE selection list is out of range", nextIndex);
- selectedInputs.append(inputs[nextIndex-1]);
- }
- }
- ForEachItemIn(i2, selectedInputs)
- selectedInputs.item(i2)->start(parentExtractSize, parentExtract, paused);
- }
- virtual void stop(bool aborting)
- {
- ForEachItemIn(i2, selectedInputs)
- selectedInputs.item(i2)->stop(aborting);
- CRoxieServerActivity::stop(aborting);
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- __int64 localCycles = totalCycles;
- ForEachItemIn(i, selectedInputs)
- {
- localCycles -= selectedInputs.item(i)->queryTotalCycles();
- }
- if (localCycles < 0)
- localCycles = 0;
- return localCycles;
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- if (selectedInputs.isItem(idx))
- return selectedInputs.item(idx);
- else
- return NULL;
- }
- virtual void reset()
- {
- ForEachItemIn(i, selectedInputs)
- selectedInputs.item(i)->reset();
- selectedInputs.kill();
- CRoxieServerActivity::reset();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- assertex(idx < numInputs);
- inputs[idx] = _in;
- }
- virtual const void * nextInGroup()
- {
- throwUnexpected();
- }
- virtual unsigned numConcreteOutputs() const
- {
- return selectedInputs.ordinality();
- }
- virtual IRoxieInput * queryConcreteInput(unsigned idx)
- {
- if (selectedInputs.isItem(idx))
- return selectedInputs.item(idx);
- return NULL;
- }
- };
- class CRoxieServerNWayInputActivityFactory : public CRoxieServerMultiInputFactory
- {
- // bool ordered;
- public:
- CRoxieServerNWayInputActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerNWayInputActivity(this, _probeManager, numInputs());
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNWayInputActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerNWayInputActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=====================================================================================================
- class CRoxieServerNWayGraphLoopResultReadActivity : public CRoxieServerActivity
- {
- IHThorNWayGraphLoopResultReadArg & helper;
- CIArrayOf<CRoxieServerActivity> resultReaders;
- PointerArrayOf<IRoxieInput> inputs;
- unsigned graphId;
- bool grouped;
- bool selectionIsAll;
- size32_t selectionLen;
- rtlDataAttr selection;
- public:
- CRoxieServerNWayGraphLoopResultReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _graphId)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorNWayGraphLoopResultReadArg &)basehelper)
- {
- grouped = helper.isGrouped();
- graphId = _graphId;
- selectionIsAll = false;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- if (inputs.ordinality() == 0)
- {
- initInputSelection();
- unsigned max = selectionLen / sizeof(size32_t);
- const size32_t * selections = (const size32_t *)selection.getdata();
- IProbeManager * probeManager = NULL; // MORE!!
- for (unsigned i = 0; i < max; i++)
- {
- CRoxieServerActivity * resultInput = new CRoxieServerInternalGraphLoopResultReadActivity(factory, probeManager, graphId, selections[i]);
- resultReaders.append(*resultInput);
- inputs.append(resultInput->queryOutput(0));
- resultInput->onCreate(ctx, colocalParent);
- resultInput->start(parentExtractSize, parentExtract, paused);
- }
- }
- else
- {
- ForEachItemIn(i, inputs)
- inputs.item(i)->start(parentExtractSize, parentExtract, paused);
- }
- }
- virtual void stop(bool aborting)
- {
- ForEachItemIn(i, inputs)
- inputs.item(i)->stop(aborting);
- CRoxieServerActivity::stop(aborting);
- }
- virtual void reset()
- {
- ForEachItemIn(i, inputs)
- inputs.item(i)->reset();
- inputs.kill();
- resultReaders.kill();
- CRoxieServerActivity::reset();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for nway graph result read");
- }
- virtual const void * nextInGroup()
- {
- throwUnexpected();
- }
- virtual unsigned numConcreteOutputs() const
- {
- return inputs.ordinality();
- }
- virtual IRoxieInput * queryConcreteInput(unsigned idx)
- {
- if (inputs.isItem(idx))
- return inputs.item(idx);
- return NULL;
- }
- virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract)
- {
- ensureCreated();
- basehelper.onStart(parentExtract, NULL);
- initInputSelection();
- unsigned max = selectionLen / sizeof(size32_t);
- const size32_t * selections = (const size32_t *)selection.getdata();
- for (unsigned i = 0; i < max; i++)
- processor.noteUseIteration(selections[i]);
- }
- virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor, unsigned parentExtractSize, const byte * parentExtract, IProbeManager *probeManager, IArrayOf<IRoxieInput> &probes)
- {
- //selection etc. already initialised from the gratherIterationUsage() call.
- unsigned max = selectionLen / sizeof(size32_t);
- const size32_t * selections = (const size32_t *)selection.getdata();
- for (unsigned i = 0; i < max; i++)
- inputs.append(processor.connectIterationOutput(selections[i], probeManager, probes, this, i));
- }
- protected:
- void initInputSelection()
- {
- helper.getInputSelection(selectionIsAll, selectionLen, selection.refdata());
- if (selectionIsAll)
- throw MakeStringException(ROXIE_NWAY_INPUT_ERROR, "ALL not yet supported for NWay graph inputs");
- }
- };
- class CRoxieServerNWayGraphLoopResultReadActivityFactory : public CRoxieServerActivityFactory
- {
- unsigned graphId;
- public:
- CRoxieServerNWayGraphLoopResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _graphId)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), graphId(_graphId)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerNWayGraphLoopResultReadActivity(this, _probeManager, graphId);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for NWay GraphLoopResultRead activity");
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNWayGraphLoopResultReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned graphId)
- {
- return new CRoxieServerNWayGraphLoopResultReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, graphId);
- }
- //=================================================================================
- class RoxieSteppedInput : public CInterface, implements ISteppedInput
- {
- public:
- RoxieSteppedInput(IRoxieInput * _input) { input = _input; }
- IMPLEMENT_IINTERFACE
- protected:
- virtual const void * nextInputRow()
- {
- #ifdef TRACE_SEEK_REQUESTS
- IRoxieContextLogger * logger = input->queryActivity();
- const void * ret = doNextInputRow();
- {
- CommonXmlWriter xmlwrite(XWFtrim|XWFopt|XWFnoindent);
- if (!ret)
- xmlwrite.outputBool(true,"eof");
- else if (input->queryOutputMeta()->hasXML())
- input->queryOutputMeta()->toXML((byte *) ret, xmlwrite);
- logger->CTXLOG("next() returns (%s)", xmlwrite.str());
- }
- return ret;
- #else
- return doNextInputRow();
- #endif
- }
- virtual const void * nextInputRowGE(const void * seek, unsigned numFields, bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- #ifdef TRACE_SEEK_REQUESTS
- IRoxieContextLogger * logger = input->queryActivity();
- {
- CommonXmlWriter xmlwrite(XWFtrim|XWFopt|XWFnoindent);
- if (input->queryOutputMeta()->hasXML())
- input->queryOutputMeta()->toXML((byte *) seek, xmlwrite);
- logger->CTXLOG("nextInputRowGE(%d, %s%s%s, %s) seek(%s)",
- numFields,
- stepExtra.readAheadManyResults() ? "readahead " : "",
- stepExtra.returnMismatches() ? "mismatch" : "exact",
- stepExtra.onlyReturnFirstSeekMatch() ? " single-match" : "",
- stepExtra.queryExtraSeeks() ? "multi-seek":"",
- xmlwrite.str());
- }
- const void * ret = doNextInputRowGE(seek, numFields, wasCompleteMatch, stepExtra);
- {
- CommonXmlWriter xmlwrite(XWFtrim|XWFopt|XWFnoindent);
- if (!ret)
- xmlwrite.outputBool(true,"eof");
- else if (input->queryOutputMeta()->hasXML())
- input->queryOutputMeta()->toXML((byte *) ret, xmlwrite);
- logger->CTXLOG("nextInputRowGE(%d, %s%s%s, %s) result(%s)",
- numFields,
- stepExtra.readAheadManyResults() ? "readahead " : "",
- stepExtra.returnMismatches() ? "mismatch" : "exact",
- stepExtra.onlyReturnFirstSeekMatch() ? " single-match" : "",
- stepExtra.queryExtraSeeks() ? "multi-seek":"",
- xmlwrite.str());
- }
- return ret;
- #else
- return doNextInputRowGE(seek, numFields, wasCompleteMatch, stepExtra);
- #endif
- }
- virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
- {
- return input->gatherConjunctions(collector);
- }
- virtual void resetEOF()
- {
- input->resetEOF();
- }
- virtual IInputSteppingMeta * queryInputSteppingMeta()
- {
- return input->querySteppingMeta();
- }
- inline const void * doNextInputRow()
- {
- const void * ret = input->nextInGroup();
- if (!ret)
- ret = input->nextInGroup();
- return ret;
- }
- inline const void * doNextInputRowGE(const void * seek, unsigned numFields, bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- assertex(wasCompleteMatch);
- return input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- }
- protected:
- IRoxieInput * input;
- };
- //=================================================================================
- class CRoxieServerNaryActivity : public CRoxieServerMultiInputActivity
- {
- public:
- CRoxieServerNaryActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerMultiInputActivity(_factory, _probeManager, _numInputs)
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerMultiInputActivity::start(parentExtractSize, parentExtract, paused);
- for (unsigned i=0; i < numInputs; i++)
- {
- IRoxieInput * cur = inputArray[i];
- unsigned numRealInputs = cur->numConcreteOutputs();
- for (unsigned j = 0; j < numRealInputs; j++)
- {
- IRoxieInput * curReal = cur->queryConcreteInput(j);
- expandedInputs.append(curReal);
- }
- }
- }
- virtual void reset()
- {
- expandedInputs.kill();
- CRoxieServerMultiInputActivity::reset();
- }
- protected:
- PointerArrayOf<IRoxieInput> expandedInputs;
- };
- //=================================================================================
- class CRoxieStreamMerger : public CStreamMerger
- {
- public:
- CRoxieStreamMerger() : CStreamMerger(true) {}
- void initInputs(unsigned _numInputs, IRoxieInput ** _inputArray)
- {
- CStreamMerger::initInputs(_numInputs);
- inputArray = _inputArray;
- }
- virtual bool pullInput(unsigned i, const void * seek, unsigned numFields, const SmartStepExtra * stepExtra)
- {
- const void * next;
- bool matches = true;
- if (seek)
- next = inputArray[i]->nextSteppedGE(seek, numFields, matches, *stepExtra);
- else
- next = nextUngrouped(inputArray[i]);
- pending[i] = next;
- pendingMatches[i] = matches;
- return (next != NULL);
- }
- virtual void releaseRow(const void * row)
- {
- ReleaseRoxieRow(row);
- }
- protected:
- IRoxieInput **inputArray;
- };
- class CRoxieServerNWayMergeActivity : public CRoxieServerNaryActivity
- {
- public:
- CRoxieServerNWayMergeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerNaryActivity(_factory, _probeManager, _numInputs),
- helper((IHThorNWayMergeArg &)basehelper)
- {
- initializedMeta = false;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerNaryActivity::start(parentExtractSize, parentExtract, paused);
- merger.init(helper.queryCompare(), helper.dedup(), helper.querySteppingMeta()->queryCompare());
- merger.initInputs(expandedInputs.length(), expandedInputs.getArray());
- }
- virtual void stop(bool aborting)
- {
- merger.done();
- CRoxieServerNaryActivity::stop(aborting);
- }
- virtual void reset()
- {
- merger.cleanup();
- CRoxieServerNaryActivity::reset();
- initializedMeta = false;
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void * next = merger.nextRow();
- if (next)
- processed++;
- return next;
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void * next = merger.nextRowGE(seek, numFields, wasCompleteMatch, stepExtra);
- if (next)
- processed++;
- return next;
- }
- virtual IInputSteppingMeta * querySteppingMeta()
- {
- if (expandedInputs.ordinality() == 0)
- return NULL;
- if (!initializedMeta)
- {
- meta.init(helper.querySteppingMeta(), false);
- ForEachItemIn(i, expandedInputs)
- {
- if (meta.getNumFields() == 0)
- break;
- IInputSteppingMeta * inputMeta = expandedInputs.item(i)->querySteppingMeta();
- meta.intersect(inputMeta);
- }
- initializedMeta = true;
- }
- if (meta.getNumFields() == 0)
- return NULL;
- return &meta;
- }
- protected:
- IHThorNWayMergeArg &helper;
- CRoxieStreamMerger merger;
- CSteppingMeta meta;
- bool initializedMeta;
- };
- class CRoxieServerNWayMergeActivityFactory : public CRoxieServerMultiInputFactory
- {
- public:
- CRoxieServerNWayMergeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerNWayMergeActivity(this, _probeManager, numInputs());
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNWayMergeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerNWayMergeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerNWayMergeJoinActivity : public CRoxieServerNaryActivity
- {
- public:
- CRoxieServerNWayMergeJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs, CMergeJoinProcessor & _processor)
- : CRoxieServerNaryActivity(_factory, _probeManager, _numInputs),processor(_processor),
- helper((IHThorNWayMergeJoinArg &)basehelper)
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerNaryActivity::start(parentExtractSize, parentExtract, paused);
- ForEachItemIn(i1, expandedInputs)
- {
- IRoxieInput * cur = expandedInputs.item(i1);
- Owned<RoxieSteppedInput> stepInput = new RoxieSteppedInput(cur);
- processor.addInput(stepInput);
- }
- ICodeContext * codectx = ctx->queryCodeContext();
- Owned<IEngineRowAllocator> inputAllocator = codectx->getRowAllocator(helper.queryInputMeta(), activityId);
- Owned<IEngineRowAllocator> outputAllocator = codectx->getRowAllocator(helper.queryOutputMeta(), activityId);
- processor.beforeProcessing(inputAllocator, outputAllocator);
- }
- virtual void stop(bool aborting)
- {
- processor.afterProcessing();
- CRoxieServerNaryActivity::stop(aborting);
- }
- virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
- {
- return processor.gatherConjunctions(collector);
- }
- virtual void resetEOF()
- {
- processor.queryResetEOF();
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void * next = processor.nextInGroup();
- if (next)
- processed++;
- return next;
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void * next = processor.nextGE(seek, numFields, wasCompleteMatch, stepExtra);
- if (next)
- processed++;
- return next;
- }
- virtual IInputSteppingMeta * querySteppingMeta()
- {
- return processor.queryInputSteppingMeta();
- }
- protected:
- IHThorNWayMergeJoinArg & helper;
- CMergeJoinProcessor & processor;
- };
- class CRoxieServerAndMergeJoinActivity : public CRoxieServerNWayMergeJoinActivity
- {
- public:
- CRoxieServerAndMergeJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerNWayMergeJoinActivity(_factory, _probeManager, _numInputs, andProcessor), andProcessor(helper)
- {
- }
- protected:
- CAndMergeJoinProcessor andProcessor;
- };
- class CRoxieServerAndLeftMergeJoinActivity : public CRoxieServerNWayMergeJoinActivity
- {
- public:
- CRoxieServerAndLeftMergeJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerNWayMergeJoinActivity(_factory, _probeManager, _numInputs, andLeftProcessor), andLeftProcessor(helper)
- {
- }
- protected:
- CAndLeftMergeJoinProcessor andLeftProcessor;
- };
- class CRoxieServerMofNMergeJoinActivity : public CRoxieServerNWayMergeJoinActivity
- {
- public:
- CRoxieServerMofNMergeJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerNWayMergeJoinActivity(_factory, _probeManager, _numInputs, mofnProcessor), mofnProcessor(helper)
- {
- }
- protected:
- CMofNMergeJoinProcessor mofnProcessor;
- };
- class CRoxieServerProximityJoinActivity : public CRoxieServerNWayMergeJoinActivity
- {
- public:
- CRoxieServerProximityJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerNWayMergeJoinActivity(_factory, _probeManager, _numInputs, proximityProcessor), proximityProcessor(helper)
- {
- }
- protected:
- CProximityJoinProcessor proximityProcessor;
- };
- class CRoxieServerNWayMergeJoinActivityFactory : public CRoxieServerMultiInputFactory
- {
- unsigned flags;
- public:
- CRoxieServerNWayMergeJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- Owned<IHThorNWayMergeJoinArg> helper = (IHThorNWayMergeJoinArg *) helperFactory();
- flags = helper->getJoinFlags();
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- if (flags & IHThorNWayMergeJoinArg::MJFhasrange)
- return new CRoxieServerProximityJoinActivity(this, _probeManager, numInputs());
- switch (flags & IHThorNWayMergeJoinArg::MJFkindmask)
- {
- case IHThorNWayMergeJoinArg::MJFinner:
- return new CRoxieServerAndMergeJoinActivity(this, _probeManager, numInputs());
- case IHThorNWayMergeJoinArg::MJFleftonly:
- case IHThorNWayMergeJoinArg::MJFleftouter:
- return new CRoxieServerAndLeftMergeJoinActivity(this, _probeManager, numInputs());
- case IHThorNWayMergeJoinArg::MJFmofn:
- return new CRoxieServerMofNMergeJoinActivity(this, _probeManager, numInputs());
- default:
- throwUnexpected();
- }
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNWayMergeJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerNWayMergeJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerNWaySelectActivity : public CRoxieServerMultiInputActivity
- {
- IHThorNWaySelectArg &helper;
- public:
- CRoxieServerNWaySelectActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerMultiInputActivity(_factory, _probeManager, _numInputs),
- helper((IHThorNWaySelectArg &)basehelper)
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerMultiInputActivity::start(parentExtractSize, parentExtract, paused);
- unsigned whichInput = helper.getInputIndex();
- selectedInput = NULL;
- if (whichInput--)
- {
- for (unsigned i=0; i < numInputs; i++)
- {
- IRoxieInput * cur = inputArray[i];
- unsigned numRealInputs = cur->numConcreteOutputs();
- if (whichInput < numRealInputs)
- {
- selectedInput = cur->queryConcreteInput(whichInput);
- break;
- }
- whichInput -= numRealInputs;
- }
- }
- }
- virtual void reset()
- {
- selectedInput = NULL;
- CRoxieServerMultiInputActivity::reset();
- }
- const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (!selectedInput)
- return NULL;
- return selectedInput->nextInGroup();
- }
- virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
- {
- if (!selectedInput)
- return false;
- return selectedInput->gatherConjunctions(collector);
- }
-
- virtual void resetEOF()
- {
- if (selectedInput)
- selectedInput->resetEOF();
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (!selectedInput)
- return NULL;
- return selectedInput->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- }
- IInputSteppingMeta * querySteppingMeta()
- {
- if (selectedInput)
- return selectedInput->querySteppingMeta();
- return NULL;
- }
- protected:
- IRoxieInput * selectedInput;
- };
- class CRoxieServerNWaySelectActivityFactory : public CRoxieServerMultiInputFactory
- {
- public:
- CRoxieServerNWaySelectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerNWaySelectActivity(this, _probeManager, numInputs());
- }
- };
- IRoxieServerActivityFactory *createRoxieServerNWaySelectActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerNWaySelectActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerRemoteActivity : public CRoxieServerActivity, implements IRoxieServerErrorHandler
- {
- protected:
- IHThorRemoteArg &helper;
- CRemoteResultAdaptor remote;
- public:
- CRoxieServerRemoteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteID)
- : CRoxieServerActivity(_factory, _probeManager),
- helper((IHThorRemoteArg &)basehelper),
- remote(_remoteID, meta.queryOriginal(), helper, *this, false, false) // MORE - if they need it stable we'll have to think!
- {
- }
- virtual const IResolvedFile *queryVarFileInfo() const
- {
- return NULL;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- remote.onCreate(this, this, _ctx, _colocalParent);
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- remote.onStart(parentExtractSize, parentExtract);
- remote.setLimits(helper.getRowLimit(), (unsigned __int64) -1, I64C(0x7FFFFFFFFFFFFFFF));
- unsigned fileNo = 0; // MORE - superfiles require us to do this per file part... maybe (needs thought)
- remote.getMem(0, fileNo, 0); // the cached context is all we need to send
- remote.flush();
- remote.senddone();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- if (idx==(unsigned)-1)
- idx = 0;
- return idx ? NULL: &remote;
- }
- virtual void reset()
- {
- processed = remote.processed;
- remote.processed = 0;
- CRoxieServerActivity::reset();
- }
- virtual void onLimitExceeded(bool isKeyed)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- helper.onLimitExceeded();
- }
- virtual const void *createLimitFailRow(bool isKeyed)
- {
- UNIMPLEMENTED; // MORE - is there an ONFAIL for a limit folded into a remote?
- }
- virtual const void *nextInGroup()
- {
- throwUnexpected(); // I am nobody's input
- }
- };
- class CRoxieServerRemoteActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- RemoteActivityId remoteId;
- bool isRoot;
- CRoxieServerRemoteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, bool _isRoot)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), remoteId(_remoteId), isRoot(_isRoot)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerRemoteActivity(this, _probeManager, remoteId);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for %s activity", getActivityText(kind));
- }
- virtual bool isSink() const
- {
- //I don't think the action version of this is implemented - but this would be the code
- return isRoot && !meta.queryOriginal();
- }
- };
- IRoxieServerActivityFactory *createRoxieServerRemoteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, bool _isRoot)
- {
- return new CRoxieServerRemoteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _isRoot);
- }
- //=================================================================================
- class CRoxieServerIterateActivity : public CRoxieServerActivity
- {
- IHThorIterateArg &helper;
- OwnedConstRoxieRow defaultRecord;
- OwnedConstRoxieRow left;
- OwnedConstRoxieRow right;
- unsigned counter;
- public:
- CRoxieServerIterateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager),
- helper((IHThorIterateArg &)basehelper)
- {
- counter = 0;
- }
- virtual bool needsAllocator() const { return true; }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- counter = 0;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t thisSize = helper.createDefault(rowBuilder);
- defaultRecord.setown(rowBuilder.finalizeRowClear(thisSize));
- }
- virtual void reset()
- {
- defaultRecord.clear();
- right.clear();
- left.clear();
- CRoxieServerActivity::reset();
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- right.setown(input->nextInGroup());
- if (!right)
- {
- bool skippedGroup = (left == NULL) && (counter > 0); //we have just skipped entire group, but shouldn't output a double null
- left.clear();
- counter = 0;
- if (skippedGroup) continue;
- return NULL;
- }
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned outSize = helper.transform(rowBuilder, left ? left : defaultRecord, right, ++counter);
- if (outSize)
- {
- left.setown(rowBuilder.finalizeRowClear(outSize));
- processed++;
- return left.getLink();
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- }
- };
- class CRoxieServerIterateActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerIterateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerIterateActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerIterateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerIterateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerProcessActivity : public CRoxieServerActivity
- {
- IHThorProcessArg &helper;
- OwnedConstRoxieRow curRight;
- OwnedConstRoxieRow initialRight;
- unsigned counter;
- Owned<IEngineRowAllocator> rightRowAllocator;
- public:
- CRoxieServerProcessActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorProcessArg &)basehelper)
- {
- counter = 0;
- }
- virtual bool needsAllocator() const { return true; }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- rightRowAllocator.setown(ctx->queryCodeContext()->getRowAllocator(QUERYINTERFACE(helper.queryRightRecordSize(), IOutputMetaData), activityId));
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- counter = 0;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- RtlDynamicRowBuilder rowBuilder(rightRowAllocator);
- size32_t thisSize = helper.createInitialRight(rowBuilder);
- initialRight.setown(rowBuilder.finalizeRowClear(thisSize));
- curRight.set(initialRight);
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- try
- {
- loop
- {
- const void * in = input->nextInGroup();
- if (!in)
- {
- bool eog = (curRight != initialRight); // processed any records?
- counter = 0;
- curRight.set(initialRight);
- if (eog)
- return NULL;
- in = input->nextInGroup();
- if (!in)
- return NULL;
- }
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- RtlDynamicRowBuilder rightRowBuilder(rightRowAllocator);
- size32_t outSize = helper.transform(rowBuilder, rightRowBuilder, in, curRight, ++counter);
- ReleaseRoxieRow(in);
- if (outSize)
- {
- //MORE: This should be returned...
- size32_t rightSize = rightRowAllocator->queryOutputMeta()->getRecordSize(rightRowBuilder.getSelf());
- curRight.setown(rightRowBuilder.finalizeRowClear(rightSize));
- processed++;
- return rowBuilder.finalizeRowClear(outSize);
- }
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- };
- class CRoxieServerProcessActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerProcessActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerProcessActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerProcessActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerProcessActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerGroupActivity : public CRoxieServerActivity
- {
- IHThorGroupArg &helper;
- bool endPending;
- bool eof;
- bool first;
- const void *next;
- public:
- CRoxieServerGroupActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorGroupArg &)basehelper)
- {
- next = NULL;
- endPending = false;
- eof = false;
- first = true;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- endPending = false;
- eof = false;
- first = true;
- assertex(next == NULL);
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual void reset()
- {
- ReleaseClearRoxieRow(next);
- CRoxieServerActivity::reset();
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (first)
- {
- next = input->nextInGroup();
- first = false;
- }
- if (eof || endPending)
- {
- endPending = false;
- return NULL;
- }
- const void * prev = next;
- next = input->nextInGroup();
- if (!next)
- next = input->nextInGroup();
- if (next)
- {
- assertex(prev);
- if (!helper.isSameGroup(prev, next))
- endPending = true;
- }
- else
- eof = true;
- if (prev)
- processed++;
- return prev;
- }
- };
- class CRoxieServerGroupActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerGroupActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerGroupActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerGroupActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerFirstNActivity : public CRoxieServerLateStartActivity
- {
- unsigned __int64 limit;
- unsigned __int64 skip;
- unsigned doneThisGroup;
- IHThorFirstNArg &helper;
- public:
- CRoxieServerFirstNActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerLateStartActivity(_factory, _probeManager), helper((IHThorFirstNArg &)basehelper)
- {
- doneThisGroup = 0;
- limit = 0;
- skip = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- doneThisGroup = 0;
- CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
- limit = helper.getLimit();
- skip = helper.numToSkip();
- lateStart(parentExtractSize, parentExtract, limit > 0);
- if (limit + skip >= limit)
- limit += skip;
- }
- const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- const void *ret;
- loop
- {
- ret = input->nextInGroup();
- if (!ret)
- {
- if (meta.isGrouped())
- {
- if (doneThisGroup > skip)
- {
- doneThisGroup = 0;
- return NULL;
- }
- doneThisGroup = 0;
- }
- ret = input->nextInGroup();
- if (!ret)
- {
- eof = true;
- return NULL;
- }
- }
- doneThisGroup++;
- if (doneThisGroup > skip)
- break;
- ReleaseRoxieRow(ret);
- }
- if (doneThisGroup <= limit)
- {
- processed++;
- return ret;
- }
- ReleaseRoxieRow(ret);
- if (meta.isGrouped())
- {
- while ((ret = input->nextInGroup()) != NULL)
- ReleaseRoxieRow(ret);
- doneThisGroup = 0;
- }
- else
- eof = true;
- return NULL;
- }
- };
- class CRoxieServerFirstNActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerFirstNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerFirstNActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerFirstNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerFirstNActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerSelectNActivity : public CRoxieServerActivity
- {
- bool done;
- IHThorSelectNArg &helper;
- public:
- CRoxieServerSelectNActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorSelectNArg &)basehelper)
- {
- done = false;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- done = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- const void *defaultRow()
- {
- if (!rowAllocator)
- createRowAllocator(); // We delay as often not needed...
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t thisSize = helper.createDefault(rowBuilder);
- return rowBuilder.finalizeRowClear(thisSize);
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (done)
- return NULL;
- done = true;
- processed++; // always going to return a row!
- unsigned __int64 index = helper.getRowToSelect();
- while (--index)
- {
- const void * next = input->nextInGroup();
- if (!next)
- next = input->nextInGroup();
- if (!next)
- return defaultRow();
- ReleaseRoxieRow(next);
- }
- const void * next = input->nextInGroup();
- if (!next)
- next = input->nextInGroup();
- if (!next)
- next = defaultRow();
- return next;
- }
- };
- class CRoxieServerSelectNActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerSelectNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerSelectNActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSelectNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerSelectNActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerSelfJoinActivity : public CRoxieServerActivity
- {
- IHThorJoinArg &helper;
- ICompare *collate;
- OwnedRowArray group;
- bool matchedLeft;
- BoolArray matchedRight;
- bool eof;
- bool first;
- unsigned leftIndex;
- unsigned rightIndex;
- unsigned rightOuterIndex;
- unsigned joinLimit;
- unsigned atmostLimit;
- unsigned abortLimit;
- unsigned keepLimit;
- bool leftOuterJoin;
- bool rightOuterJoin;
- bool exclude;
- bool limitFail;
- bool limitOnFail;
- bool cloneLeft;
- OwnedConstRoxieRow defaultLeft;
- OwnedConstRoxieRow defaultRight;
- OwnedConstRoxieRow lhs;
- Owned<IException> failingLimit;
- bool failingOuterAtmost;
- Owned<IEngineRowAllocator> defaultAllocator;
- Owned<IRHLimitedCompareHelper> limitedhelper;
- Owned<CRHDualCache> dualcache;
- IInputBase *dualCacheInput;
- bool fillGroup()
- {
- group.clear();
- matchedLeft = false;
- matchedRight.kill();
- failingOuterAtmost = false;
- const void * next;
- unsigned groupCount = 0;
- while((next = input->nextInGroup()) != NULL)
- {
- if(groupCount==abortLimit)
- {
- if(limitFail)
- failLimit(next);
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- if(limitOnFail)
- {
- assertex(!failingLimit);
- try
- {
- failLimit(next);
- }
- catch(IException * except)
- {
- failingLimit.setown(except);
- }
- assertex(failingLimit != NULL);
- group.append(next);
- groupCount++;
- break;
- }
- group.clear();
- groupCount = 0;
- while(next)
- {
- ReleaseRoxieRow(next);
- next = input->nextInGroup();
- }
- }
- else if(groupCount==atmostLimit)
- {
- if(leftOuterJoin)
- {
- group.append(next);
- groupCount++;
- failingOuterAtmost = true;
- break;
- }
- else
- {
- group.clear();
- groupCount = 0;
- while (next)
- {
- ReleaseRoxieRow(next);
- next = input->nextInGroup();
- }
- }
- }
- else
- {
- group.append(next);
- groupCount++;
- }
- }
- if(group.ordinality()==0)
- {
- eof = true;
- return false;
- }
- leftIndex = 0;
- rightIndex = 0;
- rightOuterIndex = 0;
- joinLimit = keepLimit;
- ForEachItemIn(idx, group)
- matchedRight.append(false);
- return true;
- }
- void failLimit(const void * next)
- {
- helper.onMatchAbortLimitExceeded();
- CommonXmlWriter xmlwrite(XWFtrim|XWFopt );
- if (!ctx->isBlind() && input->queryOutputMeta() && input->queryOutputMeta()->hasXML())
- {
- input->queryOutputMeta()->toXML((byte *) next, xmlwrite);
- }
- throw MakeStringException(ROXIE_TOO_MANY_RESULTS, "More than %d match candidates in self-join %d for row %s", abortLimit, queryId(), xmlwrite.str());
- }
- virtual bool needsAllocator() const { return true; }
- const void *joinRecords(const void * curLeft, const void * curRight, IException * except = NULL)
- {
- try
- {
- if (cloneLeft && !except)
- {
- LinkRoxieRow(curLeft);
- return curLeft;
- }
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t outsize = except ? helper.onFailTransform(rowBuilder, curLeft, curRight, except) : helper.transform(rowBuilder, curLeft, curRight);
- if (outsize)
- return rowBuilder.finalizeRowClear(outsize);
- else
- return NULL;
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- void createDefaultLeft()
- {
- if (!defaultLeft)
- {
- if (!defaultAllocator)
- defaultAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input->queryOutputMeta(), activityId));
- RtlDynamicRowBuilder rowBuilder(defaultAllocator);
- size32_t thisSize = helper.createDefaultLeft(rowBuilder);
- defaultLeft.setown(rowBuilder.finalizeRowClear(thisSize));
- }
- }
- void createDefaultRight()
- {
- if (!defaultRight)
- {
- if (!defaultAllocator)
- defaultAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input->queryOutputMeta(), activityId));
- RtlDynamicRowBuilder rowBuilder(defaultAllocator);
- size32_t thisSize = helper.createDefaultRight(rowBuilder);
- defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
- }
- }
- public:
- CRoxieServerSelfJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorJoinArg &)basehelper)
- {
- collate = helper.queryCompareLeftRight();
- eof = false;
- first = true;
- keepLimit = 0;
- atmostLimit = 0;
- unsigned joinFlags = helper.getJoinFlags();
- leftOuterJoin = (joinFlags & JFleftouter) != 0;
- rightOuterJoin = (joinFlags & JFrightouter) != 0;
- cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
- exclude = (joinFlags & JFexclude) != 0;
- abortLimit = 0;
- joinLimit = 0;
- assertex((joinFlags & (JFfirst | JFfirstleft | JFfirstright)) == 0); // no longer supported
- getLimitType(joinFlags, limitFail, limitOnFail);
- if((joinFlags & JFslidingmatch) != 0)
- throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Internal Error: Sliding self join not supported");
- failingOuterAtmost = false;
- matchedLeft = false;
- leftIndex = 0;
- rightIndex = 0;
- rightOuterIndex = 0;
- dualCacheInput = NULL;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- first = true;
- failingLimit.clear();
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- keepLimit = helper.getKeepLimit();
- if(keepLimit == 0)
- keepLimit = (unsigned)-1;
- atmostLimit = helper.getJoinLimit();
- if(atmostLimit == 0)
- atmostLimit = (unsigned)-1;
- else
- assertex(!rightOuterJoin);
- abortLimit = helper.getMatchAbortLimit();
- if (abortLimit == 0)
- abortLimit = (unsigned)-1;
- if (rightOuterJoin)
- createDefaultLeft();
- if (leftOuterJoin || limitOnFail)
- createDefaultRight();
- if ((helper.getJoinFlags() & JFlimitedprefixjoin) && helper.getJoinLimit())
- { //limited match join (s[1..n])
- dualcache.setown(new CRHDualCache());
- dualcache->init(CRoxieServerActivity::input);
- dualCacheInput = dualcache->queryOut1();
- failingOuterAtmost = false;
- matchedLeft = false;
- leftIndex = 0;
- rightOuterIndex = 0;
- limitedhelper.setown(createRHLimitedCompareHelper());
- limitedhelper->init( helper.getJoinLimit(), dualcache->queryOut2(), collate, helper.queryPrefixCompare() );
- }
- }
- virtual void reset()
- {
- group.clear();
- CRoxieServerActivity::reset();
- defaultLeft.clear();
- defaultRight.clear();
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (limitedhelper)
- {
- while(!eof) //limited match join
- {
- if (!group.isItem(rightIndex))
- {
- lhs.setown(dualCacheInput->nextInGroup());
- if (lhs)
- {
- rightIndex = 0;
- group.clear();
- limitedhelper->getGroup(group,lhs);
- }
- else
- {
- eof = true;
- }
- }
-
- if (group.isItem(rightIndex))
- {
- const void * rhs = group.item(rightIndex++);
- if(helper.match(lhs, rhs))
- {
- const void * ret = joinRecords(lhs, rhs);
- return ret;
- }
- }
- }
- return NULL;
- }
- else
- {
- if (first)
- {
- first = false;
- fillGroup();
- }
- while(!eof)
- {
- if(failingOuterAtmost)
- while(group.isItem(leftIndex))
- {
- const void * ret = joinRecords(group.item(leftIndex++), defaultRight);
- if(ret)
- {
- processed++;
- return ret;
- }
- }
- if((joinLimit == 0) || !group.isItem(rightIndex))
- {
- if(leftOuterJoin && !matchedLeft && !failingLimit)
- {
- const void * ret = joinRecords(group.item(leftIndex), defaultRight);
- if(ret)
- {
- matchedLeft = true;
- processed++;
- return ret;
- }
- }
- leftIndex++;
- matchedLeft = false;
- rightIndex = 0;
- joinLimit = keepLimit;
- }
- if(!group.isItem(leftIndex))
- {
- if(failingLimit || failingOuterAtmost)
- {
- const void * lhs;
- while((lhs = input->nextInGroup()) != NULL) // dualCache never active here
- {
- const void * ret = joinRecords(lhs, defaultRight, failingLimit);
- ReleaseRoxieRow(lhs);
- if(ret)
- {
- processed++;
- return ret;
- }
- }
- failingLimit.clear();
- }
- if(rightOuterJoin && !failingLimit)
- while(group.isItem(rightOuterIndex))
- if(!matchedRight.item(rightOuterIndex++))
- {
- const void * ret = joinRecords(defaultLeft, group.item(rightOuterIndex-1));
- if(ret)
- {
- processed++;
- return ret;
- }
- }
- if(!fillGroup())
- return NULL;
- continue;
- }
- const void * lhs = group.item(leftIndex);
- if(failingLimit)
- {
- leftIndex++;
- const void * ret = joinRecords(lhs, defaultRight, failingLimit);
- if(ret)
- {
- processed++;
- return ret;
- }
- }
- else
- {
- const void * rhs = group.item(rightIndex++);
- if(helper.match(lhs, rhs))
- {
- matchedLeft = true;
- matchedRight.replace(true, rightIndex-1);
- if(!exclude)
- {
- const void * ret = joinRecords(lhs, rhs);
- if(ret)
- {
- processed++;
- joinLimit--;
- return ret;
- }
- }
- }
- }
- }
- return NULL;
- }
- }
- };
- class CRoxieServerSelfJoinActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerSelfJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerSelfJoinActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSelfJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerSelfJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=====================================================================================================
- class CRoxieServerLookupJoinActivity : public CRoxieServerTwoInputActivity
- {
- private:
- class LookupTable : public CInterface
- {
- public:
- LookupTable(unsigned _size, ICompare * _leftRightCompare, ICompare * _rightCompare, IHash * _leftHash, IHash * _rightHash, bool _dedupOnAdd)
- : leftRightCompare(_leftRightCompare), rightCompare(_rightCompare), leftHash(_leftHash), rightHash(_rightHash), dedupOnAdd(_dedupOnAdd)
- {
- unsigned minsize = (4*_size)/3;
- size = 2;
- while((minsize >>= 1) > 0)
- size <<= 1;
- mask = size - 1;
- table = (const void * *)calloc(size, sizeof(void *));
- findex = BadIndex;
- }
- ~LookupTable()
- {
- unsigned i;
- for(i=0; i<size; i++)
- ReleaseRoxieRow(table[i]);
- free(table);
- }
- void add(const void * right)
- {
- findex = BadIndex;
- unsigned start = rightHash->hash(right) & mask;
- unsigned index = start;
- while(table[index])
- {
- if(dedupOnAdd && (rightCompare->docompare(table[index], right) == 0))
- {
- ReleaseRoxieRow(right);
- return;
- }
- index++;
- if(index==size)
- index = 0;
- if(index==start)
- throwUnexpected(); //table is full, should never happen
- }
- table[index] = right;
- }
- const void *find(const void * left) const
- {
- fstart = leftHash->hash(left) & mask;
- findex = fstart;
- return doFind(left);
- }
- const void *findNext(const void * left) const
- {
- if(findex == BadIndex)
- return NULL;
- advance();
- return doFind(left);
- }
- void advance() const
- {
- findex++;
- if(findex==size)
- findex = 0;
- if(findex==fstart)
- throw MakeStringException(ROXIE_JOIN_ERROR, "Internal error hthor lookup join activity (hash table full on lookup)");
- }
- const void *doFind(const void * left) const
- {
- while(table[findex])
- {
- if(leftRightCompare->docompare(left, table[findex]) == 0)
- return table[findex];
- advance();
- }
- findex = BadIndex;
- return NULL;
- }
- private:
- ICompare * leftRightCompare;
- ICompare * rightCompare;
- IHash * leftHash;
- IHash * rightHash;
- unsigned size;
- unsigned mask;
- const void * * table;
- bool dedupOnAdd;
- unsigned mutable fstart;
- unsigned mutable findex;
- static unsigned const BadIndex;
- };
- IHThorHashJoinArg &helper;
- bool leftOuterJoin;
- bool exclude;
- bool eog;
- bool many;
- bool dedupRHS;
- bool matchedGroup;
- const void *left;
- OwnedConstRoxieRow defaultRight;
- Owned<LookupTable> table;
- unsigned keepLimit;
- unsigned atmostLimit;
- unsigned limitLimit;
- bool limitFail;
- bool limitOnFail;
- bool hasGroupLimit;
- unsigned keepCount;
- bool gotMatch;
- bool cloneLeft;
- ConstPointerArray rightGroup;
- aindex_t rightGroupIndex;
- Owned<IException> failingLimit;
- ConstPointerArray filteredRight;
- ThorActivityKind activityKind;
- Owned<IEngineRowAllocator> defaultRightAllocator;
-
- void createDefaultRight()
- {
- if (!defaultRight)
- {
- if (!defaultRightAllocator)
- defaultRightAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input1->queryOutputMeta(), activityId));
- RtlDynamicRowBuilder rowBuilder(defaultRightAllocator);
- size32_t thisSize = helper.createDefaultRight(rowBuilder);
- defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
- }
- }
- public:
- CRoxieServerLookupJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerTwoInputActivity(_factory, _probeManager), helper((IHThorHashJoinArg &)basehelper)
- {
- unsigned joinFlags = helper.getJoinFlags();
- leftOuterJoin = (joinFlags & JFleftouter) != 0;
- assertex((joinFlags & JFrightouter) == 0);
- exclude = (joinFlags & JFexclude) != 0;
- many = (joinFlags & JFmanylookup) != 0;
- cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
- dedupRHS = (joinFlags & (JFmanylookup | JFmatchrequired | JFtransformMaySkip)) == 0; // optimisation: can implicitly dedup RHS unless is many lookup, or match required, or transform may skip
- left = NULL;
- activityKind = factory->getKind();
- eog = false;
- matchedGroup = false;
- gotMatch = false;
- keepLimit = 0;
- keepCount = 0;
- atmostLimit = 0;
- limitLimit = 0;
- hasGroupLimit = false;
- getLimitType(helper.getJoinFlags(), limitFail, limitOnFail);
- }
- void loadRight()
- {
- ConstPointerArray rightset;
- unsigned i = 0;
- try
- {
- const void * next;
- while(true)
- {
- next = input1->nextInGroup();
- if(!next)
- next = input1->nextInGroup();
- if(!next)
- break;
- rightset.append(next);
- }
- unsigned rightord = rightset.ordinality();
- table.setown(new LookupTable(rightord, helper.queryCompareLeftRight(), helper.queryCompareRight(), helper.queryHashLeft(), helper.queryHashRight(), dedupRHS));
- for(i=0; i<rightord; i++)
- table->add(rightset.item(i));
- }
- catch (...)
- {
- unsigned rightord = rightset.ordinality();
- for ( ; i<rightord; i++)
- ReleaseRoxieRow(rightset.item(i));
- throw;
- }
- };
- virtual void reset()
- {
- CRoxieServerTwoInputActivity::reset();
- ReleaseClearRoxieRow(left);
- defaultRight.clear();
- table.clear();
- }
- virtual bool needsAllocator() const { return true; }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eog = false;
- matchedGroup = false;
- left = NULL;
- CRoxieServerTwoInputActivity::start(parentExtractSize, parentExtract, paused);
- keepLimit = helper.getKeepLimit();
- if(keepLimit==0) keepLimit = static_cast<unsigned>(-1);
- atmostLimit = helper.getJoinLimit();
- limitLimit = helper.getMatchAbortLimit();
- hasGroupLimit = ((atmostLimit > 0) || (limitLimit > 0));
- if(atmostLimit==0) atmostLimit = static_cast<unsigned>(-1);
- if(limitLimit==0) limitLimit = static_cast<unsigned>(-1);
- getLimitType(helper.getJoinFlags(), limitFail, limitOnFail);
- if (((activityKind==TAKlookupjoin || activityKind==TAKlookupdenormalizegroup) && leftOuterJoin) || limitOnFail)
- createDefaultRight();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- if (idx==1)
- input1 = _in;
- else
- {
- if ((helper.getJoinFlags() & JFparallel) != 0)
- {
- puller.setown(new CRoxieServerReadAheadInput(0)); // MORE - cant ask context for parallelJoinPreload as context is not yet set up.
- puller->setInput(0, _in);
- _in = puller;
- }
- CRoxieServerActivity::setInput(idx, _in);
- }
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if(!table)
- loadRight();
- switch (activityKind)
- {
- case TAKlookupjoin:
- return nextInGroupJoin();
- case TAKlookupdenormalize:
- case TAKlookupdenormalizegroup:
- return nextInGroupDenormalize();
- }
- throwUnexpected();
- }
- private:
- const void * nextInGroupJoin()
- {
- if(!table)
- loadRight();
- while(true)
- {
- const void * right = NULL;
- if(!left)
- {
- left = input->nextInGroup();
- keepCount = keepLimit;
- if(!left)
- {
- if(matchedGroup || eog)
- {
- matchedGroup = false;
- eog = true;
- return NULL;
- }
- eog = true;
- continue;
- }
- eog = false;
- gotMatch = false;
- right = getRightFirst();
- }
- else
- right = getRightNext();
- const void * ret = NULL;
- if(failingLimit)
- {
- ret = joinException(left, failingLimit);
- }
- else
- {
- while(right)
- {
- if(helper.match(left, right))
- {
- gotMatch = true;
- if(exclude)
- break;
- ret = joinRecords(left, right);
- if(ret)
- break;
- }
- right = getRightNext();
- ret = NULL;
- }
- if(leftOuterJoin && !gotMatch)
- {
- ret = joinRecords(left, defaultRight);
- gotMatch = true;
- }
- }
- if(ret)
- {
- matchedGroup = true;
- processed++;
- if(!many || (--keepCount == 0) || failingLimit)
- {
- ReleaseClearRoxieRow(left);
- failingLimit.clear();
- }
- return ret;
- }
- ReleaseClearRoxieRow(left);
- }
- }
- const void * nextInGroupDenormalize()
- {
- while(true)
- {
- left = input->nextInGroup();
- if(!left)
- {
- if (!matchedGroup)
- left = input->nextInGroup();
- if (!left)
- {
- matchedGroup = false;
- return NULL;
- }
- }
- gotMatch = false;
- const void * right = getRightFirst();
- const void * ret = NULL;
- if (failingLimit)
- ret = joinException(left, failingLimit);
- else if (activityKind == TAKlookupdenormalize)
- {
- OwnedConstRoxieRow newLeft;
- newLeft.set(left);
- unsigned rowSize = 0;
- unsigned leftCount = 0;
- keepCount = keepLimit;
- while (right)
- {
- try
- {
- if (helper.match(left, right))
- {
- gotMatch = true;
- if (exclude)
- break;
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned thisSize = helper.transform(rowBuilder, newLeft, right, ++leftCount);
- if (thisSize)
- {
- rowSize = thisSize;
- newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
- }
- if(!many || (--keepCount == 0))
- break;
- }
- right = getRightNext();
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- if (rowSize)
- ret = newLeft.getClear();
- else if (leftOuterJoin && !gotMatch)
- {
- ret = left;
- left = NULL;
- }
- }
- else
- {
- filteredRight.kill();
- keepCount = keepLimit;
- while (right)
- {
- if (helper.match(left, right))
- {
- gotMatch = true;
- if(exclude)
- break;
- filteredRight.append(right);
- if(!many || (--keepCount == 0))
- break;
- }
- right = getRightNext();
- }
- if((filteredRight.ordinality() > 0) || (leftOuterJoin && !gotMatch))
- ret = denormalizeRecords(left, filteredRight);
- filteredRight.kill();
- }
- ReleaseRoxieRow(left);
- left = NULL;
- failingLimit.clear();
- if(ret)
- {
- matchedGroup = true;
- processed++;
- return ret;
- }
- }
- }
- const void * joinRecords(const void * left, const void * right)
- {
- if (cloneLeft)
- {
- LinkRoxieRow(left);
- return left;
- }
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned outSize = helper.transform(rowBuilder, left, right);
- if (outSize)
- return rowBuilder.finalizeRowClear(outSize);
- else
- return NULL;
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- const void * joinException(const void * left, IException * except)
- {
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned outSize = helper.onFailTransform(rowBuilder, left, defaultRight, except);
- if (outSize)
- return rowBuilder.finalizeRowClear(outSize);
- else
- return NULL;
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- const void * denormalizeRecords(const void * left, ConstPointerArray & rows)
- {
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned numRows = rows.ordinality();
- const void * right = numRows ? rows.item(0) : defaultRight.get();
- unsigned outSize = helper.transform(rowBuilder, left, right, numRows, (const void * *)rows.getArray());
- if (outSize)
- return rowBuilder.finalizeRowClear(outSize);
- else
- return NULL;
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- const void * getRightFirst() { if(hasGroupLimit) return fillRightGroup(); else return table->find(left); }
- const void * getRightNext() { if(hasGroupLimit) return readRightGroup(); else return table->findNext(left); }
- const void * readRightGroup() { if(rightGroup.isItem(rightGroupIndex)) return rightGroup.item(rightGroupIndex++); else return NULL; }
- const void *fillRightGroup()
- {
- rightGroup.kill();
- for(const void * right = table->find(left); right; right = table->findNext(left))
- {
- rightGroup.append(right);
- if(rightGroup.ordinality() > limitLimit)
- {
- if(limitFail)
- failLimit();
- gotMatch = true;
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- if(limitOnFail)
- {
- assertex(!failingLimit);
- try
- {
- failLimit();
- }
- catch(IException * e)
- {
- failingLimit.setown(e);
- }
- assertex(failingLimit != NULL);
- }
- else
- {
- rightGroup.kill();
- }
- break;
- }
- if(rightGroup.ordinality() > atmostLimit)
- {
- rightGroup.kill();
- break;
- }
- }
- rightGroupIndex = 0;
- return readRightGroup();
- }
- void failLimit()
- {
- helper.onMatchAbortLimitExceeded();
- CommonXmlWriter xmlwrite(XWFtrim|XWFopt );
- if(!ctx->isBlind() && input->queryOutputMeta() && input->queryOutputMeta()->hasXML())
- {
- input->queryOutputMeta()->toXML(static_cast<const unsigned char *>(left), xmlwrite);
- }
- throw MakeStringException(ROXIE_TOO_MANY_RESULTS, "More than %u match candidates in join %d for row %s", limitLimit, queryId(), xmlwrite.str());
- }
-
- };
- unsigned const CRoxieServerLookupJoinActivity::LookupTable::BadIndex(static_cast<unsigned>(-1));
- class CRoxieServerLookupJoinActivityFactory : public CRoxieServerJoinActivityFactory
- {
- public:
- CRoxieServerLookupJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- Owned<IHThorHashJoinArg> helper = (IHThorHashJoinArg *) helperFactory();
- if((helper->getJoinFlags() & (JFfirst | JFfirstleft | JFfirstright | JFslidingmatch)) != 0)
- throw MakeStringException(ROXIE_INVALID_FLAGS, "Invalid flags for lookup join activity"); // code generator should never create such an activity
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerLookupJoinActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerLookupJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerLookupJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=====================================================================================================
- class CRoxieServerAllJoinActivity : public CRoxieServerTwoInputActivity
- {
- private:
- IHThorAllJoinArg &helper;
- bool leftOuterJoin;
- bool rightOuterJoin;
- bool exclude;
- OwnedConstRoxieRow defaultRight;
- OwnedConstRoxieRow defaultLeft;
- Owned<IEngineRowAllocator> defaultRightAllocator;
- Owned<IEngineRowAllocator> defaultLeftAllocator;
- const void *left;
- unsigned countForLeft;
- ConstPointerArray rightset;
- BoolArray matchedRight; // MORE - could use a bitset...
- unsigned keepLimit;
- bool started;
- bool eog;
- bool eos;
- bool matchedLeft;
- bool matchedGroup;
- bool leftIsGrouped;
- bool cloneLeft;
- unsigned rightIndex;
- unsigned rightOrdinality;
- ThorActivityKind activityKind;
- ConstPointerArray filteredRight;
- void createDefaultLeft()
- {
- if (!defaultLeft)
- {
- if (!defaultLeftAllocator)
- defaultLeftAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input->queryOutputMeta(), activityId));
- RtlDynamicRowBuilder rowBuilder(defaultLeftAllocator);
- size32_t thisSize = helper.createDefaultLeft(rowBuilder);
- defaultLeft.setown(rowBuilder.finalizeRowClear(thisSize));
- }
- }
- void createDefaultRight()
- {
- if (!defaultRight)
- {
- if (!defaultRightAllocator)
- defaultRightAllocator.setown(ctx->queryCodeContext()->getRowAllocator(input1->queryOutputMeta(), activityId));
- RtlDynamicRowBuilder rowBuilder(defaultRightAllocator);
- size32_t thisSize = helper.createDefaultRight(rowBuilder);
- defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
- }
- }
- public:
- CRoxieServerAllJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerTwoInputActivity(_factory, _probeManager), helper((IHThorAllJoinArg &)basehelper)
- {
- unsigned joinFlags = helper.getJoinFlags();
- leftOuterJoin = (joinFlags & JFleftouter) != 0;
- rightOuterJoin = (joinFlags & JFrightouter) != 0;
- cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
- keepLimit = (unsigned) -1;
- exclude = (joinFlags & JFexclude) != 0;
- left = NULL;
- started = true;
- eog = false;
- eos = false;
- matchedLeft = false;
- matchedGroup = false;
- activityKind = factory->getKind();
- rightIndex = 0;
- rightOrdinality = 0;
- leftIsGrouped = false;
- countForLeft = 0;
- }
- virtual void reset()
- {
- defaultRight.clear();
- defaultLeft.clear();
- ReleaseRoxieRowSet(rightset);
- matchedRight.kill();
- CRoxieServerTwoInputActivity::reset();
- }
- virtual bool needsAllocator() const { return true; }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eog = false;
- eos = false;
- matchedLeft = false;
- matchedGroup = false;
- started = false;
- left = NULL;
- CRoxieServerTwoInputActivity::start(parentExtractSize, parentExtract, paused);
- keepLimit = helper.getKeepLimit();
- if(keepLimit==0)
- keepLimit = (unsigned) -1;
- countForLeft = keepLimit;
- leftIsGrouped = input->queryOutputMeta()->isGrouped();
- if((activityKind==TAKalljoin || activityKind==TAKalldenormalizegroup) && leftOuterJoin)
- createDefaultRight();
- if(rightOuterJoin)
- createDefaultLeft();
- }
- void loadRight()
- {
- const void * next;
- while(true)
- {
- next = input1->nextInGroup();
- if(!next)
- next = input1->nextInGroup();
- if(!next)
- break;
- rightset.append(next);
- matchedRight.append(false);
- }
- rightIndex = 0;
- rightOrdinality = rightset.ordinality();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- if (idx==1)
- input1 = _in;
- else
- {
- if ((helper.getJoinFlags() & JFparallel) != 0)
- {
- puller.setown(new CRoxieServerReadAheadInput(0)); // MORE - cant ask context for parallelJoinPreload as context is not yet set up.
- puller->setInput(0, _in);
- _in = puller;
- }
- CRoxieServerActivity::setInput(idx, _in);
- }
- }
- const void * joinRecords(const void * left, const void * right)
- {
- // MORE - could share some code with lookup join
- if (cloneLeft)
- {
- LinkRoxieRow(left);
- return left;
- }
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned outSize = helper.transform(rowBuilder, left, right);
- if (outSize)
- return rowBuilder.finalizeRowClear(outSize);
- else
- return NULL;
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- const void * denormalizeRecords(const void * curLeft, ConstPointerArray & rows)
- {
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned numRows = rows.ordinality();
- const void * right = numRows ? rows.item(0) : defaultRight.get();
- unsigned outSize = helper.transform(rowBuilder, curLeft, right, numRows, rows.getArray());
- if (outSize)
- return rowBuilder.finalizeRowClear(outSize);
- else
- return NULL;
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if(!started)
- {
- started = true;
- left = input->nextInGroup();
- matchedLeft = false;
- countForLeft = keepLimit;
- if((left == NULL))
- {
- eos = true;
- return NULL;
- }
- loadRight();
- }
- const void * ret;
- const void * right;
- if(eos)
- return NULL;
- while(true)
- {
- ret = NULL;
- if((rightIndex == rightOrdinality) || (countForLeft==0))
- {
- if(leftOuterJoin && left && !matchedLeft)
- {
- switch(activityKind)
- {
- case TAKalljoin:
- ret = joinRecords(left, defaultRight);
- break;
- case TAKalldenormalize:
- ret = left;
- left = NULL;
- break;
- case TAKalldenormalizegroup:
- filteredRight.kill();
- ret = denormalizeRecords(left, filteredRight);
- break;
- default:
- throwUnexpected();
- }
- }
- rightIndex = 0;
- ReleaseRoxieRow(left);
- left = NULL;
- if(ret)
- {
- matchedGroup = true;
- processed++;
- return ret;
- }
- }
- if(!left)
- {
- left = input->nextInGroup();
- matchedLeft = false;
- countForLeft = keepLimit;
- }
- if(!left)
- {
- if(eog)
- {
- eos = true;
- matchedGroup = false;
- return NULL;
- }
- eog = true;
- if (matchedGroup && leftIsGrouped)
- {
- matchedGroup = false;
- return NULL;
- }
- matchedGroup = false;
- continue;
- }
- eog = false;
- switch(activityKind)
- {
- case TAKalljoin:
- while(rightIndex < rightOrdinality)
- {
- right = rightset.item(rightIndex);
- if(helper.match(left, right))
- {
- matchedLeft = true;
- matchedRight.replace(true, rightIndex);
- if(!exclude)
- ret = joinRecords(left, right);
- }
- rightIndex++;
- if(ret)
- {
- countForLeft--;
- matchedGroup = true;
- processed++;
- return ret;
- }
- }
- break;
- case TAKalldenormalize:
- {
- OwnedConstRoxieRow newLeft;
- newLeft.set(left);
- unsigned rowSize = 0;
- unsigned leftCount = 0;
- while((rightIndex < rightOrdinality) && countForLeft)
- {
- right = rightset.item(rightIndex);
- if(helper.match(left, right))
- {
- matchedLeft = true;
- matchedRight.replace(true, rightIndex);
- if(!exclude)
- {
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned thisSize = helper.transform(rowBuilder, newLeft, right, ++leftCount);
- if(thisSize)
- {
- rowSize = thisSize;
- newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
- --countForLeft;
- }
- }
- catch (IException *e)
- {
- throw makeWrappedException(e);
- }
- }
- }
- rightIndex++;
- }
- if(rowSize)
- {
- processed++;
- return newLeft.getClear();
- }
- }
- break;
- case TAKalldenormalizegroup:
- filteredRight.kill();
- while((rightIndex < rightOrdinality) && countForLeft)
- {
- right = rightset.item(rightIndex);
- if(helper.match(left, right))
- {
- matchedLeft = true;
- matchedRight.replace(true, rightIndex);
- filteredRight.append(right);
- --countForLeft;
- }
- ++rightIndex;
- }
- if(!exclude && filteredRight.ordinality())
- {
- ret = denormalizeRecords(left, filteredRight);
- filteredRight.kill();
- if(ret)
- {
- processed++;
- return ret;
- }
- }
- break;
- default:
- throwUnexpected();
- }
- }
- }
- };
- class CRoxieServerAllJoinActivityFactory : public CRoxieServerJoinActivityFactory
- {
- public:
- CRoxieServerAllJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- Owned<IHThorAllJoinArg> helper = (IHThorAllJoinArg *) helperFactory();
- if((helper->getJoinFlags() & (JFfirst | JFfirstleft | JFfirstright)) != 0)
- throw MakeStringException(ROXIE_INVALID_FLAGS, "Invalid flags for join all activity"); // code generator should never create such an activity
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerAllJoinActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerAllJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerAllJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=====================================================================================================
- class CRoxieServerTopNActivity : public CRoxieServerLateStartActivity
- {
- unsigned limit;
- bool hasBest;
- bool eoi;
- const void **sorted;
- unsigned sortedCount;
- unsigned curIndex;
- IHThorTopNArg &helper;
- ICompare &compare;
- public:
- CRoxieServerTopNActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerLateStartActivity(_factory, _probeManager), helper((IHThorTopNArg &)basehelper), compare(*helper.queryCompare())
- {
- sorted = NULL;
- sortedCount = 0;
- curIndex = 0;
- limit = 0;
- eoi = false;
- hasBest = false;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- assertex(sorted == NULL);
- sortedCount = 0;
- curIndex = 0;
- eoi = false;
-
- CRoxieServerLateStartActivity::start(parentExtractSize, parentExtract, paused);
- limit = (unsigned) helper.getLimit();
- hasBest = helper.hasBest();
- lateStart(parentExtractSize, parentExtract, limit > 0);
- // MORE - should we use an expanding array instead?
- if (limit > 0)
- sorted = (const void **) ctx->queryRowManager().allocate((limit+1) * sizeof(const void *), activityId);
- }
- virtual void reset()
- {
- if (sorted)
- {
- while(curIndex < sortedCount)
- ReleaseRoxieRow(sorted[curIndex++]);
- ReleaseRoxieRow(sorted);
- }
- sorted = NULL;
- CRoxieServerLateStartActivity::reset();
- }
- bool abortEarly()
- {
- if (hasBest && (sortedCount == limit))
- {
- int compare = helper.compareBest(sorted[sortedCount-1]);
- if (compare == 0)
- {
- if (meta.isGrouped())
- {
- //MORE: This would be more efficient if we had a away of skipping to the end of the incoming group.
- const void * next;
- while ((next = input->nextInGroup()) != NULL)
- ReleaseRoxieRow(next);
- }
- else
- eoi = true;
- return true;
- }
- //This only checks the lowest element - we could check all elements inserted, but it would increase the number of compares
- if (compare < 0)
- throw MakeStringException(ROXIE_TOPN_ROW_ERROR, "TOPN: row found that exceeds the best value");
- }
- return false;
- }
- void getSorted()
- {
- curIndex = 0;
- sortedCount = 0;
- if(eoi)
- return;
- const void * next;
- while ((next = input->nextInGroup()) != NULL)
- {
- if (sortedCount < limit)
- {
- binary_vec_insert_stable(next, sorted, sortedCount, compare);
- sortedCount++;
- if(abortEarly())
- return;
- }
- else
- {
- if(limit && compare.docompare(sorted[sortedCount-1], next) > 0) // MORE - if stability is an issue, need to consider whether this should be > or >=
- {
- binary_vec_insert_stable(next, sorted, sortedCount, compare); // MORE - not sure this is stable!
- ReleaseRoxieRow(sorted[sortedCount]);
- if(abortEarly())
- return;
- }
- else
- {
- ReleaseRoxieRow(next); // do not bother with insertion sort if we know next will fall off the end
- }
- }
- }
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- if (curIndex >= sortedCount)
- {
- bool eog = sortedCount != 0;
- getSorted();
- if(sortedCount == 0)
- {
- eof = true;
- return NULL;
- }
- if (eog)
- return NULL;
- }
- processed++;
- return sorted[curIndex++];
- }
- };
- class CRoxieServerTopNActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerTopNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerTopNActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerTopNActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerTopNActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerLimitActivity : public CRoxieServerActivity
- {
- protected:
- unsigned __int64 rowLimit;
- IHThorLimitArg &helper;
- public:
- CRoxieServerLimitActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorLimitArg &)basehelper)
- {
- rowLimit = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- rowLimit = helper.getRowLimit(); // could conceivably depend on context so should not compute any earlier than this
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void * ret = input->nextInGroup();
- if (ret)
- {
- processed++;
- if (processed > rowLimit)
- {
- ReleaseRoxieRow(ret);
- if (traceLevel > 4)
- DBGLOG("activityid = %d line = %d", activityId, __LINE__);
- helper.onLimitExceeded();
- }
- }
- return ret;
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void * ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- if (ret)
- {
- if (wasCompleteMatch)
- processed++;
- if (processed > rowLimit)
- {
- ReleaseRoxieRow(ret);
- if (traceLevel > 4)
- DBGLOG("activityid = %d line = %d", activityId, __LINE__);
- helper.onLimitExceeded();
- }
- }
- return ret;
- }
- virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
- {
- return input->gatherConjunctions(collector);
- }
- virtual void resetEOF()
- {
- //Do not reset the rowLimit
- input->resetEOF();
- }
- IInputSteppingMeta * querySteppingMeta()
- {
- return input->querySteppingMeta();
- }
- };
- class CRoxieServerLimitActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerLimitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerLimitActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerLimitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerLimitActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=====================================================================================================
- class CRoxieServerSkipLimitActivity : public CRoxieServerLimitActivity
- {
- ConstPointerArray buff;
- bool started;
- unsigned index;
- IHThorLimitTransformExtra * transformExtra;
- public:
- CRoxieServerSkipLimitActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _onFail)
- : CRoxieServerLimitActivity(_factory, _probeManager)
- {
- transformExtra = NULL;
- started = false;
- index = 0;
- if (_onFail)
- transformExtra = static_cast<IHThorLimitTransformExtra *>(helper.selectInterface(TAIlimittransformextra_1));
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- started = false;
- index = 0;
- CRoxieServerLimitActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual void reset()
- {
- while (buff.isItem(index))
- ReleaseRoxieRow(buff.item(index++));
- buff.kill();
- started = false;
- CRoxieServerLimitActivity::reset();
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (!started)
- pullInput();
- if (buff.isItem(index))
- {
- const void * next = buff.item(index++);
- if(next)
- processed++;
- return next;
- }
- return NULL;
- }
- protected:
- void pullInput()
- {
- unsigned count = 0;
- loop
- {
- const void * next = input->nextInGroup();
- if (next == NULL)
- {
- next = input->nextInGroup();
- if(next == NULL)
- break;
- buff.append(NULL);
- }
- count++;
- if (count > rowLimit)
- {
- ReleaseRoxieRow(next);
- ReleaseRoxieRowSet(buff);
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- if (transformExtra)
- {
- createRowAllocator();
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t outSize = transformExtra->transformOnLimitExceeded(rowBuilder);
- if (outSize)
- buff.append(rowBuilder.finalizeRowClear(outSize));
- }
- break;
- }
- buff.append(next);
- }
- started = true;
- }
- };
- class CRoxieServerSkipLimitActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerSkipLimitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerSkipLimitActivity(this, _probeManager, kind==TAKcreaterowlimit);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSkipLimitActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerSkipLimitActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerCatchActivity : public CRoxieServerActivity
- {
- IHThorCatchArg &helper;
- public:
- CRoxieServerCatchActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorCatchArg &)basehelper)
- {
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- try
- {
- const void *ret = input->nextInGroup();
- if (ret)
- processed++;
- return ret;
- }
- catch (IException *E)
- {
- E->Release();
- helper.onExceptionCaught();
- }
- catch (...)
- {
- helper.onExceptionCaught();
- }
- throwUnexpected(); // onExceptionCaught should have thrown something
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- try
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- const void * ret = input->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- if (ret && wasCompleteMatch)
- processed++;
- return ret;
- }
- catch (IException *E)
- {
- E->Release();
- helper.onExceptionCaught();
- }
- catch (...)
- {
- helper.onExceptionCaught();
- }
- throwUnexpected(); // onExceptionCaught should have thrown something
- }
- virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
- {
- return input->gatherConjunctions(collector);
- }
- virtual void resetEOF()
- {
- input->resetEOF(); // MORE - why not in base class?
- }
- IInputSteppingMeta * querySteppingMeta()
- {
- return input->querySteppingMeta();
- }
- };
- class CRoxieServerSkipCatchActivity : public CRoxieServerActivity
- {
- ConstPointerArray buff;
- bool started;
- unsigned index;
- IHThorCatchArg &helper;
- bool createRow;
- public:
- CRoxieServerSkipCatchActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _createRow)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorCatchArg &)basehelper), createRow(_createRow)
- {
- started = false;
- index = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- started = false;
- index = 0;
- try
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- catch (IException *E)
- {
- onException(E);
- started = true;
- }
- catch (...)
- {
- onException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught"));
- started = true;
- }
- }
- virtual void reset()
- {
- while (buff.isItem(index))
- ReleaseRoxieRow(buff.item(index++));
- buff.kill();
- started = false;
- CRoxieServerActivity::reset();
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (!started)
- pullInput();
- if (buff.isItem(index))
- {
- const void * next = buff.item(index++);
- if(next)
- processed++;
- return next;
- }
- return NULL;
- }
- protected:
- void onException(IException *E)
- {
- input->stop(true);
- ReleaseRoxieRowSet(buff);
- if (createRow)
- {
- createRowAllocator();
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t outSize = helper.transformOnExceptionCaught(rowBuilder, E);
- if (outSize)
- buff.append(rowBuilder.finalizeRowClear(outSize));
- }
- E->Release();
- }
- void pullInput()
- {
- try
- {
- bool EOGseen = false;
- loop
- {
- const void * next = input->nextInGroup();
- buff.append(next);
- if (next == NULL)
- {
- if (EOGseen)
- break;
- EOGseen = true;
- }
- else
- EOGseen = false;
- }
- }
- catch (IException *E)
- {
- onException(E);
- }
- catch (...)
- {
- onException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught"));
- }
- started = true;
- }
- };
- class CRoxieServerCatchActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerCatchActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- switch (kind)
- {
- case TAKcatch:
- return new CRoxieServerCatchActivity(this, _probeManager);
- case TAKskipcatch:
- return new CRoxieServerSkipCatchActivity(this, _probeManager, false);
- case TAKcreaterowcatch:
- return new CRoxieServerSkipCatchActivity(this, _probeManager, true);
- default:
- throwUnexpected();
- }
- }
- };
- IRoxieServerActivityFactory *createRoxieServerCatchActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerCatchActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerCaseActivity : public CRoxieServerActivity
- {
- IHThorCaseArg &helper;
- IRoxieInput **inputs;
- unsigned cond;
- bool unusedStopped;
- IRoxieInput *in;
- unsigned numInputs;
- public:
- CRoxieServerCaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _numInputs)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorCaseArg &)basehelper), numInputs(_numInputs)
- {
- unusedStopped = false;
- cond = 0;
- inputs = new IRoxieInput*[numInputs];
- for (unsigned i = 0; i < numInputs; i++)
- inputs[i] = NULL;
- in = NULL;
- }
- ~CRoxieServerCaseActivity()
- {
- delete [] inputs;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- cond = helper.getBranch();
- assertex(cond < numInputs);
- inputs[cond]->start(parentExtractSize, parentExtract, paused);
- for (unsigned idx = 0; idx < numInputs; idx++)
- {
- if (idx!=cond)
- inputs[idx]->stop(false); // Note: stopping unused branches early helps us avoid buffering splits too long.
- }
- in = inputs[cond];
- unusedStopped = true;
- }
- virtual void stop(bool aborting)
- {
- for (unsigned idx = 0; idx < numInputs; idx++)
- {
- if (idx==cond || !unusedStopped)
- inputs[idx]->stop(aborting);
- }
- CRoxieServerActivity::stop(aborting);
- }
- virtual void reset()
- {
- for (unsigned idx = 0; idx < numInputs; idx++)
- {
- inputs[idx]->reset();
- }
- unusedStopped = false;
- in = NULL;
- CRoxieServerActivity::reset();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- assertex(idx < numInputs);
- inputs[idx] = _in;
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (in)
- {
- const void *ret = in->nextInGroup();
- if (ret)
- processed++;
- return ret;
- }
- return NULL;
- }
- };
- class CRoxieServerCaseActivityFactory : public CRoxieServerMultiInputFactory
- {
- bool graphInvariant;
- public:
- CRoxieServerCaseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _graphInvariant)
- : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- graphInvariant = _graphInvariant;
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerCaseActivity(this, _probeManager, numInputs());
- }
- virtual bool isGraphInvariant() const
- {
- return graphInvariant;
- }
- };
- IRoxieServerActivityFactory *createRoxieServerCaseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _graphInvariant)
- {
- return new CRoxieServerCaseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphInvariant);
- }
- //=================================================================================
- class CRoxieServerIfActivity : public CRoxieServerActivity
- {
- IHThorIfArg &helper;
- IRoxieInput *inputTrue;
- IRoxieInput *inputFalse;
- bool cond;
- bool unusedStopped;
- public:
- CRoxieServerIfActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorIfArg &)basehelper)
- {
- inputFalse = NULL;
- inputTrue = NULL;
- unusedStopped = false;
- cond = false;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- cond = helper.getCondition();
- if (cond)
- {
- inputTrue->start(parentExtractSize, parentExtract, paused);
- if (inputFalse)
- inputFalse->stop(false); // Note: stopping unused branches early helps us avoid buffering splits too long.
- }
- else
- {
- if (inputFalse)
- inputFalse->start(parentExtractSize, parentExtract, paused);
- inputTrue->stop(false);
- }
- unusedStopped = true;
- }
- virtual void stop(bool aborting)
- {
- if (!unusedStopped || cond)
- inputTrue->stop(aborting);
- if (inputFalse && (!unusedStopped || !cond))
- inputFalse->stop(aborting);
- CRoxieServerActivity::stop(aborting);
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- __int64 localCycles = totalCycles;
- localCycles -= inputTrue->queryTotalCycles();
- if (inputFalse)
- localCycles -= inputFalse->queryTotalCycles();
- if (localCycles < 0)
- localCycles = 0;
- return localCycles;
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- switch (idx)
- {
- case 0:
- return inputTrue;
- case 1:
- return inputFalse;
- default:
- return NULL;
- }
- }
- virtual IIndexReadActivityInfo *queryIndexReadActivity()
- {
- IRoxieInput *in = cond ? inputTrue : inputFalse;
- if (in)
- return in->queryIndexReadActivity();
- return NULL;
- }
- virtual void reset()
- {
- CRoxieServerActivity::reset();
- inputTrue->reset();
- if (inputFalse)
- inputFalse->reset();
- unusedStopped = false;
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- if (idx==1)
- inputFalse = _in;
- else
- {
- assertex(!idx);
- inputTrue = _in;
- }
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- IRoxieInput *in = cond ? inputTrue : inputFalse;
- if (in)
- {
- const void * ret;
- if ((ret = in->nextInGroup()) != NULL)
- processed++;
- return ret;
- }
- return NULL;
- }
- };
- class CRoxieServerIfActivityFactory : public CRoxieServerActivityFactory
- {
- unsigned input2;
- unsigned input2idx;
- bool graphInvariant;
- public:
- CRoxieServerIfActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _graphInvariant)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- graphInvariant = _graphInvariant;
- input2 = (unsigned)-1;
- input2idx = (unsigned)-1;
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerIfActivity(this, _probeManager);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- if (idx==1)
- {
- input2 = source;
- input2idx = sourceidx;
- }
- else
- CRoxieServerActivityFactory::setInput(idx, source, sourceidx);
- }
- virtual unsigned getInput(unsigned idx, unsigned &sourceidx) const
- {
- switch (idx)
- {
- case 1:
- sourceidx = input2idx;
- return input2;
- case 0:
- return CRoxieServerActivityFactory::getInput(idx, sourceidx);
- default:
- return (unsigned) -1;
- }
- }
- virtual bool isGraphInvariant() const
- {
- return graphInvariant;
- }
- };
- IRoxieServerActivityFactory *createRoxieServerIfActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _graphInvariant)
- {
- return new CRoxieServerIfActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphInvariant);
- }
- //=================================================================================
- class CRoxieServerActionBaseActivity : public CRoxieServerActivity
- {
- CriticalSection ecrit;
- Owned<IException> exception;
- bool executed;
- public:
- CRoxieServerActionBaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- executed = false;
- }
- virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract) = 0;
- virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
- {
- CriticalBlock b(ecrit);
- if (exception)
- throw(exception.getLink());
- if (!executed)
- {
- try
- {
- executed = true;
- start(parentExtractSize, parentExtract, false);
- doExecuteAction(parentExtractSize, parentExtract);
- stop(false);
- }
- catch (IException * E)
- {
- ctx->notifyAbort(E);
- stop(true);
- exception.set(E);
- throw;
- }
- }
- }
- virtual void reset()
- {
- executed = false;
- exception.clear();
- CRoxieServerActivity::reset();
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- return totalCycles;
- }
- virtual const void *nextInGroup()
- {
- throwUnexpected(); // I am nobody's input
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- throwUnexpected(); // I am nobody's input
- }
- };
- class CRoxieServerIfActionActivity : public CRoxieServerActionBaseActivity
- {
- IHThorIfArg &helper;
- public:
- CRoxieServerIfActionActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActionBaseActivity(_factory, _probeManager), helper((IHThorIfArg &)basehelper)
- {
- }
- virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract)
- {
- int controlId;
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- controlId = helper.getCondition() ? 1 : 2;
- }
- executeDependencies(parentExtractSize, parentExtract, controlId);
- }
- };
- class CRoxieServerIfActionActivityFactory : public CRoxieServerActivityFactory
- {
- bool isRoot;
- public:
- CRoxieServerIfActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerIfActionActivity(this, _probeManager);
- }
- virtual bool isSink() const
- {
- return isRoot;
- }
- };
- IRoxieServerActivityFactory *createRoxieServerIfActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- {
- return new CRoxieServerIfActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
- }
- //=================================================================================
- class CRoxieServerParallelActionActivity : public CRoxieServerActionBaseActivity
- {
- public:
- CRoxieServerParallelActionActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActionBaseActivity(_factory, _probeManager)
- {
- }
- virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract)
- {
- #ifdef PARALLEL_EXECUTE
- CParallelActivityExecutor afor(dependencies, parentExtractSize, parentExtract);
- afor.For(dependencies.ordinality(), dependencies.ordinality(), true);
- #else
- ForEachItemIn(idx, dependencies)
- {
- dependencies.item(idx).execute(parentExtractSize, parentExtract);
- }
- #endif
- }
- };
- class CRoxieServerParallelActionActivityFactory : public CRoxieServerActivityFactory
- {
- bool isRoot;
- public:
- CRoxieServerParallelActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
- {
- assertex(!isRoot); // non-internal should be expanded out..
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerParallelActionActivity(this, _probeManager);
- }
- virtual bool isSink() const
- {
- return isRoot;
- }
- };
- IRoxieServerActivityFactory *createRoxieServerParallelActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- {
- return new CRoxieServerParallelActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
- }
- //=================================================================================
- class CRoxieServerSequentialActionActivity : public CRoxieServerActionBaseActivity
- {
- IHThorSequentialArg &helper;
- public:
- CRoxieServerSequentialActionActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActionBaseActivity(_factory, _probeManager), helper((IHThorSequentialArg &)basehelper)
- {
- }
- virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract)
- {
- unsigned numBranches = helper.numBranches();
- for (unsigned branch=1; branch <= numBranches; branch++)
- executeDependencies(parentExtractSize, parentExtract, branch);
- }
- };
- class CRoxieServerSequentialActionActivityFactory : public CRoxieServerActivityFactory
- {
- bool isRoot;
- public:
- CRoxieServerSequentialActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerSequentialActionActivity(this, _probeManager);
- }
- virtual bool isSink() const
- {
- return isRoot;
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSequentialActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- {
- return new CRoxieServerSequentialActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
- }
- //=================================================================================
- class CRoxieServerWhenActivity : public CRoxieServerActivity
- {
- public:
- CRoxieServerWhenActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- savedExtractSize = 0;
- savedExtract = NULL;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- savedExtractSize = parentExtractSize;
- savedExtract = parentExtract;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- executeDependencies(parentExtractSize, parentExtract, WhenParallelId); // MORE: This should probably be done in parallel!
- }
- virtual void stop(bool aborting)
- {
- if (state != STATEstopped)
- executeDependencies(savedExtractSize, savedExtract, aborting ? WhenFailureId : WhenSuccessId);
- CRoxieServerActivity::stop(aborting);
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext()); // bit of a waste of time....
- return input->nextInGroup();
- }
- protected:
- unsigned savedExtractSize;
- const byte *savedExtract;
- };
- class CRoxieServerWhenActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerWhenActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerWhenActivity(this, _probeManager);
- }
- };
- extern IRoxieServerActivityFactory *createRoxieServerWhenActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerWhenActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=================================================================================
- class CRoxieServerWhenActionActivity : public CRoxieServerActionBaseActivity
- {
- public:
- CRoxieServerWhenActionActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActionBaseActivity(_factory, _probeManager)
- {
- savedExtractSize = 0;
- savedExtract = NULL;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- savedExtractSize = parentExtractSize;
- savedExtract = parentExtract;
- CRoxieServerActionBaseActivity::start(parentExtractSize, parentExtract, paused);
- executeDependencies(parentExtractSize, parentExtract, WhenParallelId); // MORE: This should probably be done in parallel!
- }
- virtual void stop(bool aborting)
- {
- if (state != STATEstopped)
- executeDependencies(savedExtractSize, savedExtract, aborting ? WhenFailureId : WhenSuccessId);
- CRoxieServerActionBaseActivity::stop(aborting);
- }
- virtual void doExecuteAction(unsigned parentExtractSize, const byte * parentExtract)
- {
- executeDependencies(parentExtractSize, parentExtract, 1);
- }
- protected:
- unsigned savedExtractSize;
- const byte *savedExtract;
- };
- class CRoxieServerWhenActionActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerWhenActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerWhenActionActivity(this, _probeManager);
- }
- virtual bool isSink() const
- {
- return isRoot;
- }
- private:
- bool isRoot;
- };
- extern IRoxieServerActivityFactory *createRoxieServerWhenActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- {
- return new CRoxieServerWhenActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
- }
- //=================================================================================
- class CRoxieServerParseActivity : public CRoxieServerActivity, implements IMatchedAction
- {
- IHThorParseArg &helper;
- INlpParser * parser;
- INlpResultIterator * rowIter;
- const void * in;
- char * curSearchText;
- INlpParseAlgorithm * algorithm;
- size32_t curSearchTextLen;
- bool anyThisGroup;
- bool processRecord(const void * inRec)
- {
- if (helper.searchTextNeedsFree())
- rtlFree(curSearchText);
- curSearchTextLen = 0;
- curSearchText = NULL;
- helper.getSearchText(curSearchTextLen, curSearchText, inRec);
- return parser->performMatch(*this, in, curSearchTextLen, curSearchText);
- }
- public:
- CRoxieServerParseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, INlpParseAlgorithm * _algorithm)
- : CRoxieServerActivity(_factory, _probeManager),
- helper((IHThorParseArg &)basehelper), algorithm(_algorithm)
- {
- parser = NULL;
- rowIter = NULL;
- in = NULL;
- curSearchText = NULL;
- anyThisGroup = false;
- curSearchTextLen = 0;
- }
- ~CRoxieServerParseActivity()
- {
- ::Release(parser);
- }
- virtual bool needsAllocator() const { return true; }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- parser = algorithm->createParser(_ctx->queryCodeContext(), activityId, helper.queryHelper(), &helper);
- rowIter = parser->queryResultIter();
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- anyThisGroup = false;
- curSearchTextLen = 0;
- curSearchText = NULL;
- in = NULL;
- parser->reset();
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual void reset()
- {
- if (helper.searchTextNeedsFree())
- rtlFree(curSearchText);
- curSearchText = NULL;
- ReleaseClearRoxieRow(in);
- CRoxieServerActivity::reset();
- }
- virtual unsigned onMatch(ARowBuilder & self, const void * curRecord, IMatchedResults * results, IMatchWalker * walker)
- {
- try
- {
- return helper.transform(self, curRecord, results, walker);
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- virtual const void * nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- if (rowIter->isValid())
- {
- anyThisGroup = true;
- OwnedConstRoxieRow out = rowIter->getRow();
- rowIter->next();
- processed++;
- return out.getClear();
- }
- ReleaseClearRoxieRow(in);
- in = input->nextInGroup();
- if (!in)
- {
- if (anyThisGroup)
- {
- anyThisGroup = false;
- return NULL;
- }
- in = input->nextInGroup();
- if (!in)
- return NULL;
- }
- processRecord(in);
- rowIter->first();
- }
- }
- };
- class CRoxieServerParseActivityFactory : public CRoxieServerActivityFactory
- {
- Owned<INlpParseAlgorithm> algorithm;
- Owned<IHThorParseArg> helper;
- public:
- CRoxieServerParseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IResourceContext *rc)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- helper.setown((IHThorParseArg *) helperFactory());
- algorithm.setown(createThorParser(rc, *helper));
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerParseActivity(this, _probeManager, algorithm);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerParseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IResourceContext *rc)
- {
- return new CRoxieServerParseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, rc);
- }
- //=====================================================================================================
- class CRoxieServerWorkUnitWriteActivity : public CRoxieServerInternalSinkActivity
- {
- IHThorWorkUnitWriteArg &helper;
- bool isReread;
- bool grouped;
- IRoxieServerContext *serverContext;
- public:
- CRoxieServerWorkUnitWriteActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, bool _isReread)
- : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorWorkUnitWriteArg &)basehelper), isReread(_isReread)
- {
- grouped = (helper.getFlags() & POFgrouped) != 0;
- serverContext = NULL;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerInternalSinkActivity::onCreate(_ctx, _colocalParent);
- serverContext = ctx->queryServerContext();
- if (!serverContext)
- {
- throw MakeStringException(ROXIE_PIPE_ERROR, "Pipe output activity cannot be executed in slave context");
- }
- }
- virtual void onExecute()
- {
- int sequence = helper.getSequence();
- const char *storedName = helper.queryName();
- if (!storedName)
- storedName = "Dataset";
- MemoryBuffer result;
- FlushingStringBuffer *response = NULL;
- bool saveInContext = (int) sequence < 0 || isReread;
- if (!meta.queryOriginal()) // this is a bit of a hack - don't know why no meta on an output....
- meta.set(input->queryOutputMeta());
- Owned<IOutputRowSerializer> rowSerializer;
- if ((int) sequence >= 0)
- {
- response = serverContext->queryResult(sequence);
- if (response)
- response->startDataset("Dataset", helper.queryName(), sequence, (helper.getFlags() & POFextend) != 0);
- }
- if (serverContext->outputResultsToWorkUnit()||(response && response->isRaw))
- {
- createRowAllocator();
- rowSerializer.setown(rowAllocator->createRowSerializer(ctx->queryCodeContext()));
- }
- __int64 initialProcessed = processed;
- ConstPointerArray *lResult = NULL;
- if (saveInContext)
- lResult = new ConstPointerArray;
- loop
- {
- const void *row = input->nextInGroup();
- if (lResult)
- {
- if (row)
- LinkRoxieRow(row);
- if (row || grouped)
- lResult->append(row);
- }
- if (grouped && (processed != initialProcessed))
- {
- if (serverContext->outputResultsToWorkUnit())
- result.append(row == NULL);
- if (response)
- {
- if (response->isRaw)
- response->append(row == NULL);
- else
- {
- response->append("<Row __GroupBreak__=\"1\"/>"); // sensible, but need to handle on input
- }
- }
- }
- if (!row)
- {
- row = input->nextInGroup();
- if (!row)
- break;
- if (lResult)
- {
- if (row)
- LinkRoxieRow(row);
- lResult->append(row);
- }
- }
- processed++;
- if (serverContext->outputResultsToWorkUnit())
- {
- CThorDemoRowSerializer serializerTarget(result);
- rowSerializer->serialize(serializerTarget, (const byte *) row);
- }
- if (response)
- {
- if (response->isRaw)
- {
- // MORE - should be able to serialize straight to the response...
- MemoryBuffer rowbuff;
- CThorDemoRowSerializer serializerTarget(rowbuff);
- rowSerializer->serialize(serializerTarget, (const byte *) row);
- response->append(rowbuff.length(), rowbuff.toByteArray());
- }
- else if (response->isXml)
- {
- CommonXmlWriter xmlwrite(serverContext->getXmlFlags(), 1, response);
- xmlwrite.outputBeginNested("Row", false);
- helper.serializeXml((byte *) row, xmlwrite);
- xmlwrite.outputEndNested("Row");
- }
- else
- {
- SimpleOutputWriter x;
- helper.serializeXml((byte *) row, x);
- x.newline();
- response->append(x.str());
- }
- response->incrementRowCount();
- response->flush(false);
- }
- ReleaseRoxieRow(row);
- }
- if (lResult)
- serverContext->appendResultDeserialized(storedName, sequence, lResult, (helper.getFlags() & POFextend) != 0, LINK(meta.queryOriginal()));
- if (serverContext->outputResultsToWorkUnit())
- serverContext->appendResultRawContext(storedName, sequence, result.length(), result.toByteArray(), processed, (helper.getFlags() & POFextend) != 0, false); // MORE - shame to do extra copy...
- }
- };
- class CRoxieServerWorkUnitWriteActivityFactory : public CRoxieServerInternalSinkFactory
- {
- bool isReread;
- public:
- CRoxieServerWorkUnitWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
- : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot)
- {
- isReread = usageCount > 0;
- Owned<IHThorWorkUnitWriteArg> helper = (IHThorWorkUnitWriteArg *) helperFactory();
- isInternal = (helper->getSequence()==-3);
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerWorkUnitWriteActivity(this, _probeManager, isReread);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerWorkUnitWriteActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
- {
- return new CRoxieServerWorkUnitWriteActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot);
- }
- //=================================================================================
- class CRoxieServerRemoteResultActivity : public CRoxieServerInternalSinkActivity
- {
- IHThorRemoteResultArg &helper;
- public:
- CRoxieServerRemoteResultActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerInternalSinkActivity(_factory, _probeManager), helper((IHThorRemoteResultArg &)basehelper)
- {
- }
- virtual void onExecute()
- {
- OwnedConstRoxieRow row = input->nextInGroup();
- helper.sendResult(row); // should be only one row or something has gone wrong!
- }
- };
- class CRoxieServerRemoteResultActivityFactory : public CRoxieServerInternalSinkFactory
- {
- public:
- CRoxieServerRemoteResultActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
- : CRoxieServerInternalSinkFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot)
- {
- Owned<IHThorRemoteResultArg> helper = (IHThorRemoteResultArg *) helperFactory();
- isInternal = (helper->getSequence()==-3);
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerRemoteResultActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerRemoteResultActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, unsigned _usageCount, bool _isRoot)
- {
- return new CRoxieServerRemoteResultActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _usageCount, _isRoot);
- }
- //=================================================================================
- class CRoxieServerXmlParseActivity : public CRoxieServerActivity, implements IXMLSelect
- {
- IHThorXmlParseArg &helper;
- Owned<IXMLParse> xmlParser;
- const void * in;
- char * srchStr;
- unsigned numProcessedLastGroup;
- bool srchStrNeedsFree;
- Owned<IColumnProvider> lastMatch;
- public:
- IMPLEMENT_IINTERFACE;
- CRoxieServerXmlParseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager),
- helper((IHThorXmlParseArg &)basehelper)
- {
- srchStrNeedsFree = helper.searchTextNeedsFree();
- numProcessedLastGroup = 0;
- srchStr = NULL;
- in = NULL;
- }
- ~CRoxieServerXmlParseActivity()
- {
- }
- virtual bool needsAllocator() const { return true; }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- numProcessedLastGroup = 0;
- srchStr = NULL;
- in = NULL;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual void reset()
- {
- if (helper.searchTextNeedsFree())
- rtlFree(srchStr);
- srchStr = NULL;
- ReleaseClearRoxieRow(in);
- xmlParser.clear();
- CRoxieServerActivity::reset();
- }
- virtual void match(IColumnProvider &entry, offset_t startOffset, offset_t endOffset)
- {
- lastMatch.set(&entry);
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- if(xmlParser)
- {
- loop
- {
- if(!xmlParser->next())
- {
- if (srchStrNeedsFree)
- {
- rtlFree(srchStr);
- srchStr = NULL;
- }
- xmlParser.clear();
- break;
- }
- if(lastMatch)
- {
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t outSize = helper.transform(rowBuilder, in, lastMatch);
- lastMatch.clear();
- if (outSize)
- {
- processed++;
- return rowBuilder.finalizeRowClear(outSize);
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- }
- }
- ReleaseClearRoxieRow(in);
- in = input->nextInGroup();
- if(!in)
- {
- if(numProcessedLastGroup == processed)
- in = input->nextInGroup();
- if(!in)
- {
- numProcessedLastGroup = processed;
- return NULL;
- }
- }
- size32_t srchLen;
- helper.getSearchText(srchLen, srchStr, in);
- xmlParser.setown(createXMLParse(srchStr, srchLen, helper.queryIteratorPath(), *this));
- }
- }
- };
- class CRoxieServerXmlParseActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerXmlParseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerXmlParseActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerXmlParseActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerXmlParseActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //=====================================================================================================
- class CRoxieServerDiskReadBaseActivity : public CRoxieServerActivity, implements IRoxieServerErrorHandler, implements IIndexReadContext
- {
- protected:
- IHThorDiskReadBaseArg &helper;
- IHThorCompoundExtra * compoundHelper;
- RemoteActivityId remoteId; // Note we copy it rather than reference
- Owned<CSkippableRemoteResultAdaptor> remote;
- unsigned numParts;
- unsigned __int64 rowLimit;
- unsigned __int64 stopAfter;
- Linked<IInMemoryIndexManager> manager;
- Owned<IInMemoryIndexCursor> cursor;
- Owned<IDirectReader> reader;
- CThorContiguousRowBuffer deserializeSource;
- Owned<ISourceRowPrefetcher> prefetcher;
- bool eof;
- bool isKeyed;
- bool variableFileName;
- bool isOpt;
- bool maySkip;
- bool isLocal;
- CachedOutputMetaData diskSize;
- Owned<const IResolvedFile> varFileInfo;
- Owned<IFileIOArray> varFiles;
- public:
- IMPLEMENT_IINTERFACE;
- CRoxieServerDiskReadBaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, bool sorted, bool _maySkip, IInMemoryIndexManager *_manager)
- : CRoxieServerActivity(_factory, _probeManager),
- helper((IHThorDiskReadBaseArg &)basehelper),
- numParts(_numParts),
- remoteId(_remoteId),
- manager(_manager),
- isLocal(_isLocal),
- maySkip(_maySkip),
- deserializeSource(NULL)
- {
- if (numParts != 1 && !isLocal)
- remote.setown(new CSkippableRemoteResultAdaptor(remoteId, meta.queryOriginal(), helper, *this, sorted, false, _maySkip));
- compoundHelper = NULL;
- eof = false;
- rowLimit = (unsigned __int64) -1;
- isKeyed = false;
- stopAfter = I64C(0x7FFFFFFFFFFFFFFF);
- diskSize.set(helper.queryDiskRecordSize());
- variableFileName = (helper.getFlags() & (TDXvarfilename|TDXdynamicfilename)) != 0;
- isOpt = (helper.getFlags() & TDRoptional) != 0;
- }
- virtual const IResolvedFile *queryVarFileInfo() const
- {
- return varFileInfo;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- if (remote)
- remote->onCreate(this, this, _ctx, _colocalParent);
- }
- virtual bool needsAllocator() const { return true; }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- if (compoundHelper)
- {
- rowLimit = compoundHelper->getRowLimit();
- stopAfter = compoundHelper->getChooseNLimit();
- }
- if (remote)
- {
- remote->onStart(parentExtractSize, parentExtract);
- remote->setLimits(rowLimit, (unsigned __int64) -1, stopAfter);
- if (helper.canMatchAny())
- {
- if (variableFileName)
- varFileInfo.setown(resolveLFN(helper.getFileName(), isOpt));
- unsigned fileNo = 0; // MORE - superfiles require us to do this per file part... maybe (needs thought)
- // Translation into a message per channel done elsewhere....
- remote->getMem(0, fileNo, 0);
- }
- remote->flush();
- remote->senddone();
- }
- else
- {
- if (!helper.canMatchAny())
- eof = true;
- else
- {
- if (variableFileName)
- {
- varFileInfo.setown(resolveLFN(helper.getFileName(), isOpt));
- unsigned channel = isLocal ? factory->queryQueryFactory().queryChannel() : 0;
- varFiles.setown(varFileInfo->getIFileIOArray(isOpt, channel));
- manager.setown(varFileInfo->getIndexManager(isOpt, channel, varFiles, diskSize, false, 0));
- }
- assertex(manager != NULL);
- helper.createSegmentMonitors(this);
- if (cursor)
- {
- isKeyed = cursor->selectKey();
- cursor->reset();
- }
- if (!isKeyed)
- {
- reader.setown(manager->createReader(0, 0, 1));
- deserializeSource.setStream(reader);
- prefetcher.setown(diskSize.queryOriginal()->createRowPrefetcher(ctx->queryCodeContext(), activityId));
- }
- helper.setCallback(reader ? reader->queryThorDiskCallback() : cursor);
- }
- }
- }
- virtual void append(IKeySegmentMonitor *segment)
- {
- if (!segment->isWild())
- {
- if (!cursor)
- cursor.setown(manager->createCursor());
- cursor->append(segment);
- }
- }
- virtual unsigned ordinality() const
- {
- return cursor ? cursor->ordinality() : 0;
- }
- virtual IKeySegmentMonitor *item(unsigned idx) const
- {
- return cursor ? cursor->item(idx) : 0;
- }
- virtual void setMergeBarrier(unsigned barrierOffset)
- {
- // no merging so no issue...
- }
- virtual void stop(bool aborting)
- {
- if (remote)
- remote->onStop(aborting);
- CRoxieServerActivity::stop(aborting);
- }
- virtual void reset()
- {
- if (remote)
- {
- processed = remote->processed;
- remote->processed = 0;
- remote->onReset();
- }
- varFileInfo.clear();
- eof = false;
- if (cursor)
- cursor->reset();
- deserializeSource.clearStream();
- CRoxieServerActivity::reset();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- if (idx==(unsigned)-1)
- idx = 0;
- if (idx == 0)
- {
- if (remote)
- return remote;
- else
- return this;
- }
- else
- return NULL;
- }
- virtual void onLimitExceeded(bool isKeyed)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- assertex(compoundHelper);
- if (isKeyed) // MORE does this exist for diskread? should it?
- {
- if (helper.getFlags() & (TDRkeyedlimitskips|TDRkeyedlimitcreates))
- {
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- throw makeLimitSkipException(true);
- }
- else
- {
- UNIMPLEMENTED;
- //compoundHelper->onKeyedLimitExceeded(); Doesn't exist - though the flags do... interesting...
- }
- }
- else
- {
- if (helper.getFlags() & (TDRlimitskips|TDRlimitcreates))
- {
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- throw makeLimitSkipException(false);
- }
- else
- compoundHelper->onLimitExceeded();
- }
- }
- virtual const void * createLimitFailRow(bool isKeyed)
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- IHThorSourceLimitTransformExtra *limitTransformExtra = static_cast<IHThorSourceLimitTransformExtra *>(helper.selectInterface(TAIsourcelimittransformextra_1));
- assertex(limitTransformExtra);
- size32_t outSize = isKeyed ? limitTransformExtra->transformOnKeyedLimitExceeded(rowBuilder) : limitTransformExtra->transformOnLimitExceeded(rowBuilder);
- if (outSize)
- return rowBuilder.finalizeRowClear(outSize);
- return NULL;
- }
- };
- class CRoxieServerDiskReadActivity : public CRoxieServerDiskReadBaseActivity
- {
- IHThorCompoundReadExtra * readHelper;
- ConstPointerArray readrows;
- bool readAheadDone;
- unsigned readIndex;
- public:
- CRoxieServerDiskReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, bool _sorted, bool _maySkip, IInMemoryIndexManager *_manager)
- : CRoxieServerDiskReadBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _sorted, _maySkip, _manager)
- {
- compoundHelper = (IHThorDiskReadArg *)&helper;
- readHelper = (IHThorDiskReadArg *)&helper;
- readAheadDone = false;
- readIndex = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- rowLimit = compoundHelper->getRowLimit();
- stopAfter = compoundHelper->getChooseNLimit();
- CRoxieServerDiskReadBaseActivity::start(parentExtractSize, parentExtract, paused);
- readAheadDone = false;
- readIndex = 0;
- }
- virtual void reset()
- {
- while (readrows.isItem(readIndex))
- ReleaseRoxieRow(readrows.item(readIndex++));
- readrows.kill();
- readAheadDone = false;
- readIndex = 0;
- CRoxieServerDiskReadBaseActivity::reset();
- }
- virtual const void *nextInGroup()
- {
- // Note - in remote case this never gets called as input chain is routed to remoteResultAdaptor
- if (eof)
- return NULL;
- if (maySkip)
- {
- if (!readAheadDone)
- {
- unsigned preprocessed = 0;
- while (!eof)
- {
- const void *row = _nextInGroup();
- if (row)
- preprocessed++;
- if (preprocessed > rowLimit)
- {
- ReleaseRoxieRow(row);
- while (readrows.isItem(readIndex))
- ReleaseRoxieRow(readrows.item(readIndex++));
- readrows.kill();
- eof = true;
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- if (helper.getFlags() & TDRlimitskips)
- return NULL;
- else if (helper.getFlags() & TDRlimitcreates)
- return createLimitFailRow(false);
- else
- throwUnexpected();
- }
- if (preprocessed > stopAfter) // MORE - bit of a strange place to check
- {
- eof = true;
- ReleaseRoxieRow(row);
- break;
- }
- readrows.append(row);
- }
- readAheadDone = true;
- }
- if (readrows.isItem(readIndex))
- {
- const void *ret = readrows.item(readIndex++);
- if (ret)
- processed++;
- return ret;
- }
- else
- {
- eof = true;
- return NULL;
- }
- }
- else
- {
- const void *ret = _nextInGroup();
- if (ret)
- {
- processed++;
- if (processed > rowLimit)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- ReleaseRoxieRow(ret);
- compoundHelper->onLimitExceeded();
- throwUnexpected(); // onLimitExceeded is not supposed to return
- }
- if (processed > stopAfter) // MORE - bit of a strange place to check
- {
- eof = true;
- ReleaseRoxieRow(ret);
- return NULL;
- }
- }
- return ret;
- }
- }
- const void *_nextInGroup()
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned transformedSize = 0;
- if (isKeyed)
- {
- loop
- {
- const void *nextCandidate = cursor->nextMatch();
- if (!nextCandidate)
- {
- eof = true;
- return NULL;
- }
- transformedSize = readHelper->transform(rowBuilder, nextCandidate);
- if (transformedSize)
- break;
- }
- }
- else // use reader...
- {
- assertex(reader != NULL);
- loop
- {
- if (deserializeSource.eos())
- {
- eof = true;
- return NULL;
- }
- prefetcher->readAhead(deserializeSource);
- const byte *nextRec = deserializeSource.queryRow();
- if (cursor && cursor->isFiltered(nextRec))
- transformedSize = 0;
- else
- transformedSize = readHelper->transform(rowBuilder, nextRec);
- deserializeSource.finishedRow();
- if (transformedSize)
- break;
- }
- }
- return rowBuilder.finalizeRowClear(transformedSize);
- }
- };
- class CRoxieServerXmlReadActivity : public CRoxieServerDiskReadBaseActivity, implements IXMLSelect
- {
- IHThorXmlReadArg * readHelper;
- Owned<IXmlToRowTransformer> rowTransformer;
- Owned<IXMLParse> xmlParser;
- Owned<IColumnProvider> lastMatch;
- unsigned __int64 localOffset;
- public:
- IMPLEMENT_IINTERFACE;
- CRoxieServerXmlReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, bool _sorted, bool _maySkip, IInMemoryIndexManager *_manager)
- : CRoxieServerDiskReadBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _sorted, _maySkip, _manager)
- {
- compoundHelper = NULL;
- readHelper = (IHThorXmlReadArg *)&helper;
- localOffset = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- rowLimit = readHelper->getRowLimit();
- stopAfter = readHelper->getChooseNLimit();
- CRoxieServerDiskReadBaseActivity::start(parentExtractSize, parentExtract, paused);
- if (!remote)
- {
- rowTransformer.set(readHelper->queryTransformer());
- assertex(reader != NULL);
- xmlParser.setown(createXMLParse(*reader->querySimpleStream(), readHelper->queryIteratorPath(), *this, (0 != (TDRxmlnoroot & readHelper->getFlags()))?xr_noRoot:xr_none, (readHelper->getFlags() & TDRusexmlcontents) != 0));
- }
- }
- virtual void reset()
- {
- CRoxieServerDiskReadBaseActivity::reset();
- rowTransformer.clear();
- xmlParser.clear();
- }
- virtual void match(IColumnProvider &entry, offset_t startOffset, offset_t endOffset)
- {
- localOffset = startOffset;
- lastMatch.set(&entry);
- }
- virtual const void *nextInGroup()
- {
- // Note - in remote case this never gets called as input chain is routed to remoteResultAdaptor
- assertex(xmlParser != NULL);
- try
- {
- while (!eof)
- {
- //call to next() will callback on the IXmlSelect interface
- bool gotNext = false;
- gotNext = xmlParser->next();
- if(!gotNext)
- eof = true;
- else if (lastMatch)
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned sizeGot = rowTransformer->transform(rowBuilder, lastMatch, reader->queryThorDiskCallback());
- lastMatch.clear();
- localOffset = 0;
- if (sizeGot)
- {
- OwnedConstRoxieRow ret = rowBuilder.finalizeRowClear(sizeGot);
- if (processed > rowLimit)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- readHelper->onLimitExceeded();
- throwUnexpected(); // onLimitExceeded is not supposed to return
- }
- processed++;
- if (processed > stopAfter) // MORE - bit of a strange place to check
- {
- eof = true;
- return NULL;
- }
- return ret.getClear();
- }
- }
- }
- return NULL;
- }
- catch(IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- };
- class CRoxieServerCsvReadActivity : public CRoxieServerDiskReadBaseActivity
- {
- IHThorCsvReadArg *readHelper;
- ICsvParameters * csvInfo;
- unsigned headerLines;
- unsigned maxDiskSize;
- CSVSplitter csvSplitter;
- unsigned __int64 localOffset;
- const char *quotes;
- const char *separators;
- const char *terminators;
- public:
- CRoxieServerCsvReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
- unsigned _numParts, bool _isLocal, bool _sorted, bool _maySkip, IInMemoryIndexManager *_manager,
- const char *_quotes, const char *_separators, const char *_terminators)
- : CRoxieServerDiskReadBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _sorted, _maySkip, _manager),
- quotes(_quotes), separators(_separators), terminators(_terminators)
- {
- compoundHelper = NULL;
- readHelper = (IHThorCsvReadArg *)&helper;
- rowLimit = readHelper->getRowLimit();
- stopAfter = readHelper->getChooseNLimit();
- csvInfo = readHelper->queryCsvParameters();
- maxDiskSize = csvInfo->queryMaxSize();
- localOffset = 0;
- headerLines = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- rowLimit = readHelper->getRowLimit();
- stopAfter = readHelper->getChooseNLimit();
- CRoxieServerDiskReadBaseActivity::start(parentExtractSize, parentExtract, paused);
- if (!remote)
- {
- headerLines = csvInfo->queryHeaderLen();
- if (headerLines && isLocal && reader->queryFilePart() != 1)
- 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...
- if (!eof)
- {
- if (varFileInfo)
- {
- const IPropertyTree *options = varFileInfo->queryProperties();
- if (options)
- {
- quotes = options->queryProp("@csvQuote");
- separators = options->queryProp("@csvSeparate");
- terminators = options->queryProp("@csvTerminate");
- }
- }
- csvSplitter.init(readHelper->getMaxColumns(), csvInfo, quotes, separators, terminators);
- }
- }
- }
- virtual const void *nextInGroup()
- {
- // Note - in remote case this never gets called as input chain is routed to remoteResultAdaptor
- try
- {
- while (!eof)
- {
- if (reader->eos())
- {
- eof = true;
- break;
- }
- // MORE - there are rumours of a csvSplitter that operates on a stream... if/when it exists, this should use it
- size32_t rowSize = 4096; // MORE - make configurable
- size32_t maxRowSize = 10*1024*1024; // MORE - make configurable
- size32_t thisLineLength;
- loop
- {
- size32_t avail;
- const void *peek = reader->peek(rowSize, avail);
- thisLineLength = csvSplitter.splitLine(avail, (const byte *)peek);
- if (thisLineLength < rowSize || avail < rowSize)
- break;
- if (rowSize == maxRowSize)
- throw MakeStringException(0, "Row too big");
- if (rowSize >= maxRowSize/2)
- rowSize = maxRowSize;
- else
- rowSize += rowSize;
- }
- if (headerLines)
- {
- headerLines--;
- reader->skip(thisLineLength);
- }
- else
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned transformedSize = readHelper->transform(rowBuilder, csvSplitter.queryLengths(), (const char * *)csvSplitter.queryData());
- reader->skip(thisLineLength);
- if (transformedSize)
- {
- OwnedConstRoxieRow ret = rowBuilder.finalizeRowClear(transformedSize);
- if (processed > rowLimit)
- {
- readHelper->onLimitExceeded();
- throwUnexpected(); // onLimitExceeded is not supposed to return
- }
- processed++;
- if (processed > stopAfter) // MORE - bit of a strange place to check
- {
- eof = true;
- return NULL;
- }
- return ret.getClear();
- }
- }
- }
- return NULL;
- }
- catch(IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- virtual void reset()
- {
- CRoxieServerDiskReadBaseActivity::reset();
- csvSplitter.reset();
- localOffset = 0;
- }
- virtual unsigned __int64 getFilePosition(const void * row)
- {
- UNIMPLEMENTED; // we know offset in the reader but not sure it helps us much
- }
- virtual unsigned __int64 getLocalFilePosition(const void * row)
- {
- UNIMPLEMENTED;
- }
- };
- class CRoxieServerDiskNormalizeActivity : public CRoxieServerDiskReadBaseActivity
- {
- IHThorDiskNormalizeArg *normalizeHelper;
- bool firstPending;
- public:
- CRoxieServerDiskNormalizeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, bool _sorted, IInMemoryIndexManager *_manager)
- : CRoxieServerDiskReadBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _sorted, false, _manager)
- {
- compoundHelper = (IHThorDiskNormalizeArg *)&helper;
- normalizeHelper = (IHThorDiskNormalizeArg *)&helper;
- firstPending = true;
- }
- virtual void reset()
- {
- firstPending = true;
- CRoxieServerDiskReadBaseActivity::reset();
- }
- virtual const void *nextInGroup()
- {
- if (eof)
- return NULL;
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned transformedSize = 0;
- if (isKeyed)
- {
- loop
- {
- while (firstPending)
- {
- const void *nextCandidate = cursor->nextMatch();
- if (!nextCandidate)
- {
- eof = true;
- return NULL;
- }
- if (normalizeHelper->first(nextCandidate))
- {
- firstPending = false;
- break;
- }
- }
- transformedSize = normalizeHelper->transform(rowBuilder);
- firstPending = !normalizeHelper->next();
- if (transformedSize)
- break;
- }
- }
- else
- {
- assertex(reader != NULL);
- loop
- {
- while (firstPending)
- {
- if (deserializeSource.eos())
- {
- eof = true;
- return NULL;
- }
- prefetcher->readAhead(deserializeSource);
- const byte *nextRec = deserializeSource.queryRow();
- if (!cursor || !cursor->isFiltered(nextRec))
- {
- if (normalizeHelper->first(nextRec))
- firstPending = false;
- }
- deserializeSource.finishedRow();
- }
- transformedSize = normalizeHelper->transform(rowBuilder);
- firstPending = !normalizeHelper->next();
- if (transformedSize)
- break;
- }
- }
- OwnedConstRoxieRow recBuffer = rowBuilder.finalizeRowClear(transformedSize);
- processed++;
- if (processed > rowLimit)
- {
- compoundHelper->onLimitExceeded();
- throwUnexpected(); // onLimitExceeded is not supposed to return
- }
- if (processed > stopAfter) // MORE - bit of a strange place to check
- {
- eof = true;
- return NULL;
- }
- return recBuffer.getClear();
- }
- };
- class CRoxieServerDiskAggregateBaseActivity : public CRoxieServerDiskReadBaseActivity
- {
- protected:
- bool done;
- public:
- CRoxieServerDiskAggregateBaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, IInMemoryIndexManager *_manager)
- : CRoxieServerDiskReadBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, false, false, _manager),
- done(false)
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- done = false;
- CRoxieServerDiskReadBaseActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- if (idx==(unsigned)-1)
- idx = 0;
- return idx ? NULL: this;
- }
- };
- class CRoxieServerDiskCountActivity : public CRoxieServerDiskAggregateBaseActivity
- {
- IHThorDiskCountArg & countHelper;
- unsigned __int64 choosenLimit;
- IHThorSourceCountLimit *limitHelper;
- unsigned __int64 getSkippedCount()
- {
- unsigned flags = countHelper.getFlags();
- if (flags & TDRlimitskips)
- return 0;
- else if (flags & TDRlimitcreates)
- return 1;
- else
- {
- assertex(limitHelper);
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- limitHelper->onLimitExceeded();
- throwUnexpected(); // onLimitExceeded should always throw exception
- }
- }
- public:
- CRoxieServerDiskCountActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, IInMemoryIndexManager *_manager)
- : CRoxieServerDiskAggregateBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _manager),
- countHelper((IHThorDiskCountArg &)basehelper)
- {
- limitHelper = static_cast<IHThorSourceCountLimit *>(basehelper.selectInterface(TAIsourcecountlimit_1));
- choosenLimit = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- choosenLimit = countHelper.getChooseNLimit();
- if (limitHelper)
- {
- rowLimit = limitHelper->getRowLimit();
- // keyedLimit = limitHelper->getKeyedLimit(); // more - should there be one?
- }
- CRoxieServerDiskAggregateBaseActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (done) return NULL;
- done = true;
- unsigned __int64 totalCount = 0;
- if (helper.canMatchAny())
- {
- if (remote)
- {
- loop
- {
- const void * next = remote->nextInGroup();
- if (!next)
- break;
- if (meta.getFixedSize() == 1)
- totalCount += *(byte *)next;
- else
- totalCount += *(unsigned __int64 *)next;
- ReleaseRoxieRow(next);
- if (totalCount > rowLimit)
- {
- totalCount = getSkippedCount();
- break;
- }
- else if (totalCount >= choosenLimit)
- {
- totalCount = choosenLimit;
- break;
- }
- }
- }
- else
- {
- if (isKeyed)
- {
- loop
- {
- const void *nextCandidate = cursor->nextMatch();
- if (!nextCandidate)
- break;
- totalCount += countHelper.numValid(nextCandidate);
- if (totalCount > rowLimit)
- {
- totalCount = getSkippedCount();
- break;
- }
- else if (totalCount >= choosenLimit)
- {
- totalCount = choosenLimit;
- break;
- }
- }
- }
- else
- {
- assertex(reader != NULL);
- while (!deserializeSource.eos())
- {
- prefetcher->readAhead(deserializeSource);
- const byte *nextRec = deserializeSource.queryRow();
- if (!cursor || !cursor->isFiltered(nextRec))
- {
- totalCount += countHelper.numValid(nextRec);
- }
- deserializeSource.finishedRow();
- if (totalCount > rowLimit)
- {
- totalCount = getSkippedCount();
- break;
- }
- else if (totalCount >= choosenLimit)
- {
- totalCount = choosenLimit;
- break;
- }
- }
- }
- }
- }
- size32_t rowSize = meta.getFixedSize();
- void * result = rowAllocator->createRow();
- if (rowSize == 1)
- *(byte *)result = (byte)totalCount;
- else
- {
- assertex(rowSize == sizeof(unsigned __int64));
- *(unsigned __int64 *)result = totalCount;
- }
- return rowAllocator->finalizeRow(rowSize, result, rowSize);
- }
- };
- class CRoxieServerDiskAggregateActivity : public CRoxieServerDiskAggregateBaseActivity
- {
- IHThorCompoundAggregateExtra & aggregateHelper;
- public:
- CRoxieServerDiskAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
- unsigned _numParts, bool _isLocal, IInMemoryIndexManager *_manager)
- : CRoxieServerDiskAggregateBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _manager),
- aggregateHelper((IHThorDiskAggregateArg &)basehelper)
- {
- }
- const void * gatherMerged()
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
- size32_t finalSize = 0;
- if (remote)
- {
- const void * firstRow = remote->nextInGroup();
- if (!firstRow)
- {
- rowBuilder.ensureRow();
- finalSize = aggregateHelper.clearAggregate(rowBuilder);
- }
- else
- {
- // NOTE need to clone this because going to modify below, could special case 1 row only
- finalSize = cloneRow(rowBuilder, firstRow, meta);
- ReleaseRoxieRow(firstRow);
- }
- loop
- {
- const void * next = remote->nextInGroup();
- if (!next)
- break;
- finalSize = aggregateHelper.mergeAggregate(rowBuilder, next);
- ReleaseRoxieRow(next);
- }
- }
- else
- {
- aggregateHelper.clearAggregate(rowBuilder);
- if (isKeyed)
- {
- loop
- {
- const void *next = cursor->nextMatch();
- if (!next)
- break;
- aggregateHelper.processRow(rowBuilder, next);
- }
- }
- else
- {
- assertex(reader != NULL);
- while (!deserializeSource.eos())
- {
- prefetcher->readAhead(deserializeSource);
- const byte *nextRec = deserializeSource.queryRow();
- if (!cursor || !cursor->isFiltered(nextRec))
- {
- aggregateHelper.processRow(rowBuilder, nextRec);
- }
- deserializeSource.finishedRow();
- }
- }
- finalSize = meta.getRecordSize(rowBuilder.getSelf());
- }
- return rowBuilder.finalizeRowClear(finalSize);
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (done) return NULL;
- const void * ret = gatherMerged();
- done = true;
- return ret;
- }
- };
- class CRoxieServerDiskGroupAggregateActivity : public CRoxieServerDiskAggregateBaseActivity
- {
- IHThorDiskGroupAggregateArg & aggregateHelper;
- RowAggregator resultAggregator;
- bool gathered;
- public:
- IMPLEMENT_IINTERFACE;
- CRoxieServerDiskGroupAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _numParts, bool _isLocal, IInMemoryIndexManager *_manager)
- : CRoxieServerDiskAggregateBaseActivity(_factory, _probeManager, _remoteId, _numParts, _isLocal, _manager),
- aggregateHelper((IHThorDiskGroupAggregateArg &)basehelper),
- resultAggregator(aggregateHelper, aggregateHelper),
- gathered(false)
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- gathered= false;
- CRoxieServerDiskAggregateBaseActivity::start(parentExtractSize, parentExtract, paused);
- resultAggregator.start(rowAllocator);
- }
- virtual void reset()
- {
- resultAggregator.reset();
- CRoxieServerDiskAggregateBaseActivity::reset();
- }
- void gatherMerged()
- {
- if (remote)
- {
- loop
- {
- const void * next = remote->nextInGroup();
- if (!next)
- break;
- resultAggregator.mergeElement(next);
- ReleaseRoxieRow(next);
- }
- }
- else
- {
- Owned<IInMemoryFileProcessor> processor = isKeyed ?
- createKeyedGroupAggregateRecordProcessor(cursor, resultAggregator, aggregateHelper) :
- createUnkeyedGroupAggregateRecordProcessor(cursor, resultAggregator, aggregateHelper, manager->createReader(0, 0, 1),
- ctx->queryCodeContext(), activityId);
- processor->doQuery(NULL, 0, 0, 0);
- }
- gathered = true;
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (done)
- return NULL;
- if (!gathered)
- gatherMerged();
- Owned<AggregateRowBuilder> next = resultAggregator.nextResult();
- if (next)
- {
- processed++;
- return next->finalizeRowClear();
- }
- done = true;
- return NULL;
- }
- };
- class CRoxieServerDiskReadActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- RemoteActivityId remoteId;
- bool isLocal;
- bool sorted;
- bool maySkip;
- bool variableFileName;
- Owned<IFilePartMap> map;
- Owned<IFileIOArray> files;
- Owned<IInMemoryIndexManager> manager;
- Owned<const IResolvedFile> datafile;
- const char *quotes;
- const char *separators;
- const char *terminators;
- CRoxieServerDiskReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), remoteId(_remoteId)
- {
- isLocal = _graphNode.getPropBool("att[@name='local']/@value");
- Owned<IHThorDiskReadBaseArg> helper = (IHThorDiskReadBaseArg *) helperFactory();
- sorted = (helper->getFlags() & TDRunsorted) == 0;
- variableFileName = (helper->getFlags() & (TDXvarfilename|TDXdynamicfilename)) != 0;
- maySkip = (helper->getFlags() & (TDRkeyedlimitskips|TDRkeyedlimitcreates|TDRlimitskips|TDRlimitcreates)) != 0;
- quotes = separators = terminators = NULL;
- if (!variableFileName)
- {
- bool isOpt = (helper->getFlags() & TDRoptional) != 0;
- const char *fileName = helper->getFileName();
- datafile.setown(_queryFactory.queryPackage().lookupFileName(fileName, isOpt, true));
- if (datafile)
- map.setown(datafile->getFileMap());
- bool isSimple = (map && map->getNumParts()==1);
- if (isLocal || isSimple)
- {
- if (datafile)
- {
- unsigned channel = isLocal ? queryFactory.queryChannel() : 0;
- files.setown(datafile->getIFileIOArray(isOpt, channel));
- manager.setown(datafile->getIndexManager(isOpt, channel, files, helper->queryDiskRecordSize(), _graphNode.getPropBool("att[@name=\"preload\"]/@value", false), _graphNode.getPropInt("att[@name=\"_preloadSize\"]/@value", 0)));
- const IPropertyTree *options = datafile->queryProperties();
- if (options)
- {
- quotes = options->queryProp("@csvQuote");
- separators = options->queryProp("@csvSeparate");
- terminators = options->queryProp("@csvTerminate");
- }
- }
- else
- manager.setown(getEmptyIndexManager());
- }
- }
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- unsigned numParts = map ? map->getNumParts() : 0;
- switch (kind)
- {
- case TAKcsvread:
- return new CRoxieServerCsvReadActivity(this, _probeManager, remoteId, numParts, isLocal, sorted, maySkip, manager,
- quotes, separators, terminators);
- case TAKxmlread:
- return new CRoxieServerXmlReadActivity(this, _probeManager, remoteId, numParts, isLocal, sorted, maySkip, manager);
- case TAKdiskread:
- return new CRoxieServerDiskReadActivity(this, _probeManager, remoteId, numParts, isLocal, sorted, maySkip, manager);
- case TAKdisknormalize:
- return new CRoxieServerDiskNormalizeActivity(this, _probeManager, remoteId, numParts, isLocal, sorted, manager);
- case TAKdiskcount:
- return new CRoxieServerDiskCountActivity(this, _probeManager, remoteId, numParts, isLocal, manager);
- case TAKdiskaggregate:
- return new CRoxieServerDiskAggregateActivity(this, _probeManager, remoteId, numParts, isLocal, manager);
- case TAKdiskgroupaggregate:
- return new CRoxieServerDiskGroupAggregateActivity(this, _probeManager, remoteId, numParts, isLocal, manager);
- }
- throwUnexpected();
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for %s activity", getActivityText(kind));
- }
- };
- IRoxieServerActivityFactory *createRoxieServerDiskReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- {
- return new CRoxieServerDiskReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
- }
- //=================================================================================
- class CRoxieServerIndexActivity : public CRoxieServerActivity, implements IRoxieServerErrorHandler
- {
- protected:
- IHThorIndexReadBaseArg &indexHelper;
- IHThorSteppedSourceExtra * steppedExtra;
- Linked<IKeyArray> keySet;
- Linked<TranslatorArray> translators;
- unsigned rawSize;
- CSkippableRemoteResultAdaptor remote;
- CIndexTransformCallback callback;
- bool sorted;
- bool variableFileName;
- bool variableInfoPending;
- bool isOpt;
- bool isLocal;
- unsigned __int64 rowLimit;
- unsigned __int64 keyedLimit;
- unsigned __int64 choosenLimit;
- unsigned accepted;
- unsigned rejected;
- unsigned seekGEOffset;
- Owned<IKeyManager> tlk;
- Owned<const IResolvedFile> varFileInfo;
- const RemoteActivityId &remoteId;
- void setVariableFileInfo()
- {
- varFileInfo.setown(resolveLFN(indexHelper.getFileName(), isOpt));
- translators.setown(new TranslatorArray) ;
- keySet.setown(varFileInfo->getKeyArray(factory->queryActivityMeta(), translators, isOpt, isLocal ? factory->queryQueryFactory().queryChannel() : 0, factory->queryQueryFactory().getEnableFieldTranslation()));
- variableInfoPending = false;
- }
- public:
- CRoxieServerIndexActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
- IKeyArray * _keySet, TranslatorArray *_translators, unsigned _rawSize, bool _sorted, bool _isLocal, bool _maySkip)
- : CRoxieServerActivity(_factory, _probeManager),
- keySet(_keySet),
- translators(_translators),
- rawSize(_rawSize),
- indexHelper((IHThorIndexReadBaseArg &)basehelper),
- remote(_remoteId, meta.queryOriginal(), indexHelper, *this, _sorted, false, _maySkip),
- remoteId(_remoteId),
- sorted(_sorted),
- isLocal(_isLocal)
- {
- indexHelper.setCallback(&callback);
- steppedExtra = static_cast<IHThorSteppedSourceExtra *>(indexHelper.selectInterface(TAIsteppedsourceextra_1));
- variableFileName = (indexHelper.getFlags() & (TIRvarfilename|TIRdynamicfilename)) != 0;
- variableInfoPending = false;
- isOpt = (indexHelper.getFlags() & TIRoptional) != 0;
- seekGEOffset = 0;
- // started = false;
- rejected = accepted = 0;
- rowLimit = choosenLimit = keyedLimit = 0;
- }
- virtual const IResolvedFile *queryVarFileInfo() const
- {
- return varFileInfo;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- remote.onCreate(this, this, _ctx, _colocalParent);
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- accepted = 0;
- rejected = 0;
- rowLimit = (unsigned __int64) -1;
- keyedLimit = (unsigned __int64 ) -1;
- choosenLimit = I64C(0x7fffffffffffffff);
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- remote.onStart(parentExtractSize, parentExtract);
- variableInfoPending = variableFileName;
- }
- void processAllKeys()
- {
- try
- {
- if (indexHelper.canMatchAny())
- {
- if (variableInfoPending)
- setVariableFileInfo();
- remote.setLimits(rowLimit, keyedLimit, choosenLimit);
- if (keySet)
- {
- // MORE - this recreates the segmonitors per part but not per fileno (which is a little backwards).
- // 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
- // However order is probably better to iterate fileno's inside partnos
- // MORE - also not properly supporting STEPPED I fear.
- // A superkey that mixes single and multipart or tlk and roroot keys might be hard
- for (unsigned partNo = 0; partNo < keySet->length(); partNo++)
- {
- IKeyIndexBase *thisBase = keySet->queryKeyPart(partNo);
- if (thisBase)
- {
- unsigned fileNo = 0;
- IKeyIndex *thisKey = thisBase->queryPart(fileNo);
- if (seekGEOffset && !thisKey->isTopLevelKey())
- {
- tlk.setown(createSingleKeyMerger(thisKey, 0, seekGEOffset, this));
- }
- else
- {
- tlk.setown(createKeyManager(thisKey, 0, this));
- tlk->setLayoutTranslator(translators->item(fileNo));
- }
- createSegmentMonitors(tlk);
- if (queryTraceLevel() > 3 || ctx->queryProbeManager())
- {
- StringBuffer out;
- printKeyedValues(out, tlk, indexHelper.queryDiskRecordSize());
- CTXLOG("Using filter %s", out.str());
- if (ctx->queryProbeManager())
- ctx->queryProbeManager()->setNodeProperty(this, "filter", out.str());
- }
- tlk->reset();
- loop // for each file part
- {
- //block for TransformCallbackAssociation
- {
- TransformCallbackAssociation associate(callback, tlk);
- if (thisKey->isTopLevelKey())
- {
- if (thisKey->isFullySorted())
- {
- while (tlk->lookup(false))
- {
- unsigned slavePart = (unsigned) tlk->queryFpos();
- if (slavePart)
- {
- accepted++;
- remote.getMem(slavePart, fileNo, 0); // the cached context is all we need to send
- if (sorted && numChannels>1)
- remote.flush(); // don't combine parts if we need result sorted, except on a 1-way
- }
- }
- }
- else
- {
- // MORE - we could check whether there are any matching parts if we wanted.
- // If people are in the habit of sending null values that would be worthwhile
- remote.getMem(0, fileNo, 0);
- }
- }
- else
- {
- if (keyedLimit != (unsigned __int64) -1)
- {
- if ((indexHelper.getFlags() & TIRcountkeyedlimit) != 0)
- {
- unsigned __int64 count = tlk->checkCount(keyedLimit);
- if (count > keyedLimit)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d line = %d", activityId, __LINE__);
- onLimitExceeded(true);
- }
- tlk->reset();
- }
- }
- if (processSingleKey(thisKey, translators->item(fileNo)))
- break;
- }
- }
- if (++fileNo < thisBase->numParts())
- {
- thisKey = thisBase->queryPart(fileNo);
- tlk->setKey(thisKey);
- tlk->setLayoutTranslator(translators->item(fileNo));
- tlk->reset();
- }
- else
- break;
- }
- tlk->releaseSegmentMonitors();
- tlk->setKey(NULL);
- }
- }
- }
- }
- remote.flush();
- remote.senddone();
- }
- catch (IException *E)
- {
- remote.setException(E);
- }
- }
- virtual void createSegmentMonitors(IKeyManager *key)
- {
- indexHelper.createSegmentMonitors(key);
- key->finishSegmentMonitors();
- }
- virtual bool processSingleKey(IKeyIndex *key, IRecordLayoutTranslator * trans) = 0;
- virtual void reset()
- {
- if (accepted)
- noteStatistic(STATS_ACCEPTED, accepted, 1);
- if (rejected)
- noteStatistic(STATS_REJECTED, rejected, 1);
- remote.onReset();
- CRoxieServerActivity::reset();
- if (varFileInfo)
- {
- keySet.clear();
- varFileInfo.clear();
- }
- variableInfoPending = false;
- }
- virtual void stop(bool aborting)
- {
- remote.onStop(aborting);
- CRoxieServerActivity::stop(aborting);
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
- }
- };
- class CRoxieServerIndexReadBaseActivity : public CRoxieServerIndexActivity
- {
- IHThorSourceLimitTransformExtra * limitTransformExtra;
- public:
- CRoxieServerIndexReadBaseActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
- IKeyArray * _keySet, TranslatorArray *_translators, unsigned _rawSize, unsigned _maxRecordSize, bool _sorted, bool _isLocal, bool _maySkip)
- : CRoxieServerIndexActivity(_factory, _probeManager, _remoteId, _keySet, _translators, _rawSize, _sorted, _isLocal, _maySkip)
- {
- limitTransformExtra = static_cast<IHThorSourceLimitTransformExtra *>(indexHelper.selectInterface(TAIsourcelimittransformextra_1));
- }
- virtual void reset()
- {
- remote.onReset();
- CRoxieServerIndexActivity::reset();
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- try
- {
- const void *ret = remote.nextInGroup();
- if (ret)
- processed++;
- return ret;
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- protected:
- virtual const void * createLimitFailRow(bool isKeyed)
- {
- createRowAllocator();
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t outSize = isKeyed ? limitTransformExtra->transformOnKeyedLimitExceeded(rowBuilder) : limitTransformExtra->transformOnLimitExceeded(rowBuilder);
- if (outSize)
- return rowBuilder.finalizeRowClear(outSize);
- return NULL;
- }
- };
- class CRoxieServerIndexReadActivity : public CRoxieServerIndexReadBaseActivity, implements IIndexReadActivityInfo
- {
- protected:
- IHThorCompoundReadExtra & readHelper;
- ISteppingMeta *rawMeta;
- CSteppingMeta steppingMeta;
- unsigned * seekSizes;
- bool optimizeSteppedPostFilter;
- ISteppingMeta * projectedMeta;
- unsigned maxSeekLookahead;
- public:
- CRoxieServerIndexReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
- IKeyArray * _keySet, TranslatorArray *_translators, unsigned _rawSize, unsigned _maxRecordSize, bool _sorted, bool _isLocal, bool _maySkip, unsigned _maxSeekLookahead)
- : CRoxieServerIndexReadBaseActivity(_factory, _probeManager, _remoteId, _keySet, _translators, _rawSize, _maxRecordSize, _sorted, _isLocal, _maySkip),
- readHelper((IHThorIndexReadArg &)basehelper)
- {
- rawMeta = readHelper.queryRawSteppingMeta();
- unsigned flags = indexHelper.getFlags();
- optimizeSteppedPostFilter = (flags & TIRunfilteredtransform) != 0;
- seekSizes = NULL;
- maxSeekLookahead = _maxSeekLookahead;
- if (rawMeta)
- {
- const CFieldOffsetSize * fields = rawMeta->queryFields();
- unsigned maxFields = rawMeta->getNumFields();
- seekGEOffset = fields[0].offset;
- seekSizes = new unsigned[maxFields];
- seekSizes[0] = fields[0].size;
- for (unsigned i=1; i < maxFields; i++)
- seekSizes[i] = seekSizes[i-1] + fields[i].size;
- projectedMeta = readHelper.queryProjectedSteppingMeta();
- ISteppingMeta *useMeta = projectedMeta ? projectedMeta : rawMeta;
- remote.setMergeInfo(useMeta); // also need to consider superfile case where there is a mix of multiway and singleparts.. ?
- bool hasPostFilter = readHelper.transformMayFilter() && optimizeSteppedPostFilter;
- steppingMeta.init(useMeta, hasPostFilter);
- }
- }
- ~CRoxieServerIndexReadActivity()
- {
- delete [] seekSizes;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerIndexReadBaseActivity::start(parentExtractSize, parentExtract, paused);
- steppingMeta.setDistributed();
- if (steppedExtra)
- steppingMeta.setExtra(steppedExtra);
- rowLimit = readHelper.getRowLimit();
- keyedLimit = readHelper.getKeyedLimit();
- choosenLimit = readHelper.getChooseNLimit();
- if (!paused)
- processAllKeys();
- }
- class LazyLocalKeyReader : public CInterface, implements IMessageResult, implements IMessageUnpackCursor
- {
- public:
- IMPLEMENT_IINTERFACE;
- virtual IMessageUnpackCursor *getCursor(roxiemem::IRowManager *rowMgr) const
- {
- Link();
- return const_cast<LazyLocalKeyReader*> (this);
- }
- virtual const void *getMessageHeader(unsigned &length) const
- {
- length = 0;
- return NULL;
- }
- virtual const void *getMessageMetadata(unsigned &length) const
- {
- length = 0;
- return NULL;
- }
- virtual void discard() const
- {
- // nothing to do.
- }
- unsigned keyedCount;
- unsigned matched;
- bool EOFseen;
- Owned<IKeyIndexSet> keySet;
- Owned<IKeyManager> tlk;
- CRoxieServerIndexReadActivity &owner;
- LazyLocalKeyReader(CRoxieServerIndexReadActivity &_owner, IKeyIndex *key, IRecordLayoutTranslator * trans)
- : owner(_owner)
- {
- keyedCount = 0;
- matched = 0;
- EOFseen = false;
- keySet.setown(createKeyIndexSet());
- keySet->addIndex(LINK(key));
- if (owner.seekGEOffset)
- tlk.setown(createKeyMerger(keySet, owner.rawSize, owner.seekGEOffset, &owner));
- else
- tlk.setown(createKeyManager(keySet->queryPart(0), 0, &owner));
- tlk->setLayoutTranslator(trans);
- owner.indexHelper.createSegmentMonitors(tlk);
- tlk->finishSegmentMonitors();
- tlk->reset();
- }
- virtual const void *getNext(int length)
- {
- TransformCallbackAssociation associate(owner.callback, tlk);
- while (tlk->lookup(true))
- {
- keyedCount++;
- if (keyedCount > owner.keyedLimit)
- {
- owner.onLimitExceeded(true); // Should throw exception
- throwUnexpected();
- }
- size32_t transformedSize;
- RtlDynamicRowBuilder rowBuilder(owner.rowAllocator);
- byte const * keyRow = tlk->queryKeyBuffer(owner.callback.getFPosRef());
- try
- {
- transformedSize = owner.readHelper.transform(rowBuilder, keyRow);
- owner.callback.finishedRow();
- }
- catch (IException *E)
- {
- throw owner.makeWrappedException(E);
- }
- if (transformedSize)
- {
- OwnedConstRoxieRow result = rowBuilder.finalizeRowClear(transformedSize);
- matched++;
- if (matched > owner.rowLimit)
- {
- owner.onLimitExceeded(false); // Should throw exception
- throwUnexpected();
- }
- if (matched > owner.choosenLimit) // MORE - bit of a strange place to check
- {
- break;
- }
- owner.accepted++;
- return result.getClear();
- }
- else
- owner.rejected++;
- }
- EOFseen = true;
- return NULL;
- }
- virtual bool atEOF() const
- {
- return EOFseen;
- }
- virtual bool isSerialized() const
- {
- return false;
- }
- };
- virtual bool processSingleKey(IKeyIndex *key, IRecordLayoutTranslator * trans)
- {
- createRowAllocator();
- remote.injectResult(new LazyLocalKeyReader(*this, key, trans));
- return false;
- }
- virtual void onLimitExceeded(bool isKeyed)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- if (isKeyed)
- {
- if (indexHelper.getFlags() & (TIRkeyedlimitskips|TIRkeyedlimitcreates))
- {
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- throw makeLimitSkipException(true);
- }
- else
- readHelper.onKeyedLimitExceeded();
- }
- else
- {
- if (indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates))
- {
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- throw makeLimitSkipException(false);
- }
- else
- readHelper.onLimitExceeded();
- }
- }
- virtual void serializeSkipInfo(MemoryBuffer &out, unsigned seekLen, const void *rawSeek, unsigned numFields, const void * seek, const SmartStepExtra &stepExtra) const
- {
- out.append((unsigned short) numFields);
- out.append((unsigned short) seekLen);
- out.append((unsigned short) stepExtra.queryFlags());
- IMultipleStepSeekInfo *seeks = stepExtra.queryExtraSeeks();
- if (seeks)
- {
- unsigned lookahead = 40000/seekLen;
- if (maxSeekLookahead && (lookahead > maxSeekLookahead))
- lookahead = maxSeekLookahead;
- seeks->ensureFilled(seek, numFields, lookahead);
- unsigned serialized = 1; // rawseek is always serialized...
- unsigned patchLength = out.length();
- out.append(serialized); // NOTE - we come back and patch with the actual value...
- out.append(seekLen, rawSeek);
- if (seeks->ordinality())
- {
- const void *lastSeek = rawSeek;
- byte *nextSeek = NULL;
- if (projectedMeta)
- nextSeek = (byte *) alloca(seekLen);
- for (unsigned i = 0; i < seeks->ordinality(); i++)
- {
- if (projectedMeta)
- {
- RtlStaticRowBuilder rowBuilder(nextSeek-seekGEOffset, seekGEOffset+seekLen);
- readHelper.mapOutputToInput(rowBuilder, seeks->querySeek(i), numFields); // NOTE - weird interface to mapOutputToInput means that it STARTS writing at seekGEOffset...
- }
- else
- nextSeek = (byte *) seeks->querySeek(i)+seekGEOffset;
- int diff = memcmp(nextSeek, lastSeek, seekLen);
- if (diff > 0)
- {
- serialized++;
- out.append(seekLen, nextSeek);
- lastSeek = (const byte *) out.reserve(0) - seekLen;
- }
- }
- unsigned length = out.length();
- out.setWritePos(patchLength);
- out.append(serialized);
- out.setWritePos(length);
- }
- }
- else
- {
- out.append(1);
- out.append(seekLen, rawSeek);
- }
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- try
- {
- unsigned seeklen = 0;
- const void *rawSeek = NULL;
- if (seek && numFields)
- {
- seeklen = seekSizes[numFields-1];
- rawSeek = (const byte *)seek + seekGEOffset;
- if (projectedMeta)
- {
- byte * temp = (byte *) alloca(seeklen);
- RtlStaticRowBuilder rawBuilder(temp-seekGEOffset, seekGEOffset+seeklen);
- readHelper.mapOutputToInput(rawBuilder, seek, numFields); // NOTE - weird interface to mapOutputToInput means that it STARTS writing at seekGEOffset...
- rawSeek = (byte *)temp;
- }
- }
- const void *ret = remote.nextSteppedGE(seek, rawSeek, numFields, seeklen, wasCompleteMatch, stepExtra);
- if (ret && wasCompleteMatch) // GH pleas confirm the wasCompleteMatch I just added here is right
- processed++;
- return ret;
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- virtual IInputSteppingMeta * querySteppingMeta()
- {
- if (rawMeta && steppingEnabled && ((indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates|TIRkeyedlimitskips|TIRkeyedlimitcreates)) == 0))
- return &steppingMeta;
- return NULL;
- }
- virtual IIndexReadActivityInfo *queryIndexReadActivity()
- {
- if (variableInfoPending)
- setVariableFileInfo();
- return this;
- }
- virtual IKeyArray *getKeySet() const
- {
- return keySet.getLink();
- }
- virtual const IResolvedFile *getVarFileInfo() const
- {
- return varFileInfo.getLink();
- }
- virtual TranslatorArray *getTranslators() const
- {
- return translators.getLink();
- }
- virtual void mergeSegmentMonitors(IIndexReadContext *irc) const
- {
- indexHelper.createSegmentMonitors(irc); // NOTE: they will merge
- }
- virtual IRoxieServerActivity *queryActivity() { return this; }
- virtual const RemoteActivityId& queryRemoteId() const
- {
- return remoteId;
- }
- };
- class CRoxieServerSimpleIndexReadActivity : public CRoxieServerActivity, implements IIndexReadActivityInfo
- {
- IHThorCompoundReadExtra & readHelper;
- IHThorIndexReadBaseArg & indexHelper;
- IHThorSourceLimitTransformExtra * limitTransformExtra;
- IHThorSteppedSourceExtra * steppedExtra;
- bool eof;
- Linked<IKeyArray>keySet;
- Owned<IKeyIndexSet>keyIndexSet;
- Owned<IKeyManager> tlk;
- Linked<TranslatorArray> translators;
- CIndexTransformCallback callback;
- unsigned __int64 keyedLimit;
- unsigned rowLimit;
- unsigned chooseNLimit;
- unsigned accepted;
- unsigned rejected;
- unsigned keyedCount;
- unsigned rawSize;
- ISteppingMeta * rawMeta;
- ISteppingMeta * projectedMeta;
- size32_t seekGEOffset;
- unsigned * seekSizes;
- size32_t eclKeySize;
- CSteppingMeta steppingMeta;
- Owned<const IResolvedFile> varFileInfo;
- const RemoteActivityId &remoteId;
- bool firstRead;
- bool variableFileName;
- bool variableInfoPending;
- bool isOpt;
- bool isLocal;
- bool optimizeSteppedPostFilter;
- // MORE there may be enough in common between this and CRoxieServerIndexActivity to warrant some refactoring
- void initKeySet()
- {
- if ((keySet->length() > 1 || rawMeta != NULL) && translators->needsTranslation())
- {
- throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Layout translation is not available when merging key parts or smart-stepping, as it may change record order");
- }
- keyIndexSet.setown(createKeyIndexSet());
- for (unsigned part = 0; part < keySet->length(); part++)
- {
- IKeyIndexBase *kib = keySet->queryKeyPart(part);
- if (kib)
- {
- for (unsigned subpart = 0; subpart < kib->numParts(); subpart++)
- {
- IKeyIndex *k = kib->queryPart(subpart);
- if (k)
- {
- assertex(!k->isTopLevelKey());
- keyIndexSet->addIndex(LINK(k));
- }
- }
- }
- }
- }
- void setVariableFileInfo()
- {
- varFileInfo.setown(resolveLFN(indexHelper.getFileName(), isOpt));
- translators.setown(new TranslatorArray) ;
- keySet.setown(varFileInfo->getKeyArray(factory->queryActivityMeta(), translators, isOpt, isLocal ? factory->queryQueryFactory().queryChannel() : 0, factory->queryQueryFactory().getEnableFieldTranslation()));
- initKeySet();
- variableInfoPending = false;
- }
- void onEOF()
- {
- callback.setManager(NULL);
- eof = true;
- tlk.clear();
- }
- public:
- CRoxieServerSimpleIndexReadActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
- IKeyArray *_keyArray, TranslatorArray *_translatorArray, unsigned _rawSize, unsigned _maxRecordSize, bool _isLocal)
- : CRoxieServerActivity(_factory, _probeManager),
- readHelper((IHThorIndexReadArg &)basehelper),
- indexHelper((IHThorIndexReadArg &)basehelper),
- translators(_translatorArray),
- rawSize(_rawSize),
- keySet(_keyArray),
- isLocal(_isLocal),
- remoteId(_remoteId)
- {
- rowLimit = 0;
- keyedLimit = 0;
- chooseNLimit = 0;
- indexHelper.setCallback(&callback);
- steppedExtra = static_cast<IHThorSteppedSourceExtra *>(indexHelper.selectInterface(TAIsteppedsourceextra_1));
- limitTransformExtra = static_cast<IHThorSourceLimitTransformExtra *>(indexHelper.selectInterface(TAIsourcelimittransformextra_1));
- unsigned flags = indexHelper.getFlags();
- variableFileName = (flags & (TIRvarfilename|TIRdynamicfilename)) != 0;
- variableInfoPending = false;
- isOpt = (flags & TIRoptional) != 0;
- optimizeSteppedPostFilter = (flags & TIRunfilteredtransform) != 0;
- firstRead = true;
- accepted = 0;
- rejected = 0;
- keyedCount = 0;
- eof = false;
- rawMeta = readHelper.queryRawSteppingMeta();
- projectedMeta = readHelper.queryProjectedSteppingMeta();
- eclKeySize = indexHelper.queryDiskRecordSize()->getRecordSize(NULL);
- seekGEOffset = 0;
- seekSizes = NULL;
- if (rawMeta)
- {
- // MORE - should check all keys in maxFields list can actually be keyed.
- const CFieldOffsetSize * fields = rawMeta->queryFields();
- unsigned maxFields = rawMeta->getNumFields();
- seekGEOffset = fields[0].offset;
- seekSizes = new unsigned[maxFields];
- seekSizes[0] = fields[0].size;
- for (unsigned i=1; i < maxFields; i++)
- seekSizes[i] = seekSizes[i-1] + fields[i].size;
- bool hasPostFilter = readHelper.transformMayFilter() && optimizeSteppedPostFilter;
- if (projectedMeta)
- steppingMeta.init(projectedMeta, hasPostFilter);
- else
- steppingMeta.init(rawMeta, hasPostFilter);
- }
- }
- virtual const IResolvedFile *queryVarFileInfo() const
- {
- return varFileInfo;
- }
- ~CRoxieServerSimpleIndexReadActivity()
- {
- delete [] seekSizes;
- }
- virtual bool needsAllocator() const { return true; }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- firstRead = true;
- accepted = 0;
- rejected = 0;
- keyedCount = 0;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- if (steppedExtra)
- steppingMeta.setExtra(steppedExtra);
- eof = !indexHelper.canMatchAny();
- if (variableFileName)
- variableInfoPending = true;
- else
- {
- variableInfoPending = false;
- if (!keyIndexSet)
- initKeySet();
- }
- }
- virtual IIndexReadActivityInfo *queryIndexReadActivity()
- {
- if (variableInfoPending)
- setVariableFileInfo();
- return this;
- }
- virtual IKeyArray *getKeySet() const
- {
- return keySet.getLink();
- }
- virtual const IResolvedFile *getVarFileInfo() const
- {
- return varFileInfo.getLink();
- }
- virtual TranslatorArray *getTranslators() const
- {
- return translators.getLink();
- }
- virtual void mergeSegmentMonitors(IIndexReadContext *irc) const
- {
- indexHelper.createSegmentMonitors(irc); // NOTE: they will merge
- }
- virtual IRoxieServerActivity *queryActivity() { return this; }
- virtual const RemoteActivityId& queryRemoteId() const
- {
- return remoteId;
- }
-
- const void *nextInGroup()
- {
- bool matched = true;
- return nextSteppedGE(NULL, 0, matched, dummySmartStepExtra);
- }
- virtual const void *nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- if (firstRead)
- {
- if (variableInfoPending)
- setVariableFileInfo();
- rowLimit = (unsigned) readHelper.getRowLimit();
- chooseNLimit = (unsigned) readHelper.getChooseNLimit();
- unsigned numParts = keyIndexSet->numParts();
- if (!numParts)
- {
- onEOF();
- return NULL;
- }
- if (numParts > 1 || seekGEOffset)
- {
- tlk.setown(createKeyMerger(keyIndexSet, rawSize, seekGEOffset, this));
- // note that we don't set up translator because we don't support it. If that ever changes...
- }
- else
- {
- tlk.setown(createKeyManager(keyIndexSet->queryPart(0), 0, this));
- tlk->setLayoutTranslator(translators->item(0));
- }
- indexHelper.createSegmentMonitors(tlk);
- tlk->finishSegmentMonitors();
- if (queryTraceLevel() > 3 || ctx->queryProbeManager())
- {
- StringBuffer out;
- printKeyedValues(out, tlk, indexHelper.queryDiskRecordSize());
- CTXLOG("Using filter %s", out.str());
- if (ctx->queryProbeManager())
- ctx->queryProbeManager()->setNodeProperty(this, "filter", out.str());
- }
- tlk->reset();
- callback.setManager(tlk);
- keyedLimit = readHelper.getKeyedLimit();
- if (keyedLimit != (unsigned __int64) -1)
- {
- if ((indexHelper.getFlags() & TIRcountkeyedlimit) != 0)
- {
- unsigned __int64 count = tlk->checkCount(keyedLimit);
- if (count > keyedLimit)
- {
- if ((indexHelper.getFlags() & (TIRkeyedlimitskips|TIRkeyedlimitcreates)) == 0)
- readHelper.onKeyedLimitExceeded();
- const void * ret = NULL;
- if (indexHelper.getFlags() & TIRkeyedlimitcreates)
- ret = createKeyedLimitOnFailRow();
- onEOF();
- return ret;
- }
- tlk->reset();
- keyedLimit = (unsigned __int64) -1;
- }
- }
- firstRead = false;
- }
- if (accepted == chooseNLimit)
- {
- onEOF();
- return NULL;
- }
-
- const byte * rawSeek = NULL;
- unsigned seekSize = 0;
- if (seek)
- {
- seekSize = seekSizes[numFields-1];
- rawSeek = (const byte *)seek + seekGEOffset;
- if (projectedMeta)
- {
- byte *temp = (byte *) alloca(seekSize);
- RtlStaticRowBuilder rawBuilder(temp-seekGEOffset, seekGEOffset+seekSize);
- readHelper.mapOutputToInput(rawBuilder, seek, numFields);// NOTE - weird interface to mapOutputToInput means that it STARTS writing at seekGEOffset...
- rawSeek = (byte *)temp;
- }
- #ifdef _DEBUG
- // StringBuffer seekStr;
- // for (unsigned i = 0; i < seekSize; i++)
- // {
- // seekStr.appendf("%02x ", ((unsigned char *) rawSeek)[i]);
- // }
- // DBGLOG("nextSteppedGE can skip offset %d size %d value %s", seekGEOffset, seekSize, seekStr.str());
- #endif
- }
- const byte * originalRawSeek = rawSeek;
- RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
- while (rawSeek ? tlk->lookupSkip(rawSeek, seekGEOffset, seekSize) : tlk->lookup(true))
- {
- checkAbort();
- keyedCount++;
- if (keyedCount > keyedLimit)
- {
- readHelper.onKeyedLimitExceeded();
- break;
- }
- byte const * keyRow = tlk->queryKeyBuffer(callback.getFPosRef());
- #ifdef _DEBUG
- // StringBuffer recstr;
- // unsigned size = (tlk->queryRecordSize()<80) ? tlk->queryRecordSize() : 80;
- // for (unsigned i = 0; i < size; i++)
- // {
- // recstr.appendf("%02x ", ((unsigned char *) keyRow)[i]);
- // }
- // DBGLOG("nextSteppedGE Got %s", recstr.str());
- if (originalRawSeek && memcmp(keyRow + seekGEOffset, originalRawSeek, seekSize) < 0)
- assertex(!"smart seek failure");
- #endif
- size32_t transformedSize;
- rowBuilder.ensureRow();
- try
- {
- transformedSize = readHelper.transform(rowBuilder, keyRow);
- //if the post filter causes a mismatch, and the stepping condition no longer matches
- //then return a mismatch record - so the join code can start seeking on the other input.
- if (transformedSize == 0 && optimizeSteppedPostFilter && stepExtra.returnMismatches())
- {
- if (memcmp(keyRow + seekGEOffset, originalRawSeek, seekSize) != 0)
- {
- transformedSize = readHelper.unfilteredTransform(rowBuilder, keyRow);
- if (transformedSize != 0)
- wasCompleteMatch = false;
- }
- }
- callback.finishedRow();
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- if (transformedSize)
- {
- accepted++;
- if (accepted > rowLimit)
- {
- if ((indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates)) != 0)
- {
- throwUnexpected(); // should not have used simple variant if maySkip set...
- }
- if (traceLevel > 4)
- DBGLOG("activityid = %d line = %d", activityId, __LINE__);
- readHelper.onLimitExceeded();
- break;
- }
- processed++;
- #ifdef _DEBUG
- // const byte *ret = (const byte *) out.get();
- // CommonXmlWriter xmlwrite(XWFnoindent|XWFtrim|XWFopt);
- // queryOutputMeta()->toXML(ret, xmlwrite);
- // DBGLOG("ROW: {%p} %s", ret, xmlwrite.str());
- #endif
- return rowBuilder.finalizeRowClear(transformedSize);
- }
- else
- rejected++;
- rawSeek = NULL;
- }
- onEOF();
- return NULL;
- }
- virtual void reset()
- {
- onEOF();
- if (accepted)
- noteStatistic(STATS_ACCEPTED, accepted, 1);
- if (rejected)
- noteStatistic(STATS_REJECTED, rejected, 1);
- if (variableFileName)
- {
- varFileInfo.clear();
- translators.clear();
- }
- variableInfoPending = false;
- CRoxieServerActivity::reset();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
- }
- virtual IInputSteppingMeta * querySteppingMeta()
- {
- if (rawMeta && steppingEnabled && ((indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates|TIRkeyedlimitskips|TIRkeyedlimitcreates)) == 0))
- return &steppingMeta;
- return NULL;
- }
- protected:
- const void * createKeyedLimitOnFailRow()
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t outSize = limitTransformExtra->transformOnKeyedLimitExceeded(rowBuilder);
- if (outSize)
- return rowBuilder.finalizeRowClear(outSize);
- return NULL;
- }
- };
- class CRoxieServerBaseIndexActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- Owned<IKeyArray> keySet;
- Owned<TranslatorArray> translatorArray;
- Owned<IDefRecordMeta> activityMeta;
- unsigned maxRecordSize;
- RemoteActivityId remoteId;
- bool isSimple;
- bool isLocal;
- bool maySkip;
- bool sorted;
- bool variableFileName;
- bool enableFieldTranslation;
- unsigned rawSize;
- unsigned maxSeekLookahead;
- Owned<const IResolvedFile> indexfile;
- CRoxieServerSideCache *cache;
- CRoxieServerBaseIndexActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), remoteId(_remoteId)
- {
- Owned<IHThorIndexReadBaseArg> indexHelper = (IHThorIndexReadBaseArg *) helperFactory();
- unsigned flags = indexHelper->getFlags();
- sorted = (flags & TIRsorted) != 0;
- isLocal = _graphNode.getPropBool("att[@name='local']/@value");
- rtlDataAttr indexLayoutMeta;
- size32_t indexLayoutSize;
- if(!indexHelper->getIndexLayout(indexLayoutSize, indexLayoutMeta.refdata()))
- assertex(indexLayoutSize==0);
- MemoryBuffer m;
- m.setBuffer(indexLayoutSize, indexLayoutMeta.getdata());
- activityMeta.setown(deserializeRecordMeta(m, true));
- enableFieldTranslation = queryFactory.getEnableFieldTranslation();
- translatorArray.setown(new TranslatorArray);
- variableFileName = (flags & (TIRvarfilename|TIRdynamicfilename)) != 0;
- if (!variableFileName)
- {
- bool isOpt = (flags & TIRoptional) != 0;
- indexfile.setown(queryFactory.queryPackage().lookupFileName(indexHelper->getFileName(), isOpt, true));
- if (indexfile)
- keySet.setown(indexfile->getKeyArray(activityMeta, translatorArray, isOpt, isLocal ? queryFactory.queryChannel() : 0, enableFieldTranslation));
- }
- maxRecordSize = meta.getRecordSize(NULL);
- rawSize = indexHelper->queryDiskRecordSize()->getRecordSize(NULL);
- isSimple = isLocal;
- maySkip = (flags & (TIRkeyedlimitskips|TIRlimitskips|TIRlimitcreates|TIRkeyedlimitcreates)) != 0;
- if (keySet && keySet->length()==1 && !isLocal && (flags & (TIRlimitskips|TIRlimitcreates|TIRkeyedlimitskips|TIRkeyedlimitcreates))==0)
- {
- IKeyIndexBase *thisBase = keySet->queryKeyPart(0);
- if (thisBase->numParts()==1 && !thisBase->queryPart(0)->isTopLevelKey())
- isSimple = true;
- }
- int cacheSize = _graphNode.getPropInt("hint[@name='cachehits']/@value", serverSideCacheSize);
- cache = cacheSize ? new CRoxieServerSideCache(cacheSize) : NULL;
- maxSeekLookahead = _graphNode.getPropInt("hint[@name='maxseeklookahead']/@value", 0);
- }
- ~CRoxieServerBaseIndexActivityFactory()
- {
- delete cache;
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for indexread activity");
- }
- virtual IRoxieServerSideCache *queryServerSideCache() const
- {
- return cache;
- }
- virtual bool getEnableFieldTranslation() const
- {
- return enableFieldTranslation;
- }
- virtual IDefRecordMeta *queryActivityMeta() const
- {
- return activityMeta;
- }
- };
- class CRoxieServerIndexReadActivityFactory : public CRoxieServerBaseIndexActivityFactory
- {
- public:
- CRoxieServerIndexReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- if (!variableFileName && (keySet==NULL || keySet->length()==0))
- return new CRoxieServerNullActivity(this, _probeManager);
- else if (isSimple && !maySkip)
- return new CRoxieServerSimpleIndexReadActivity(this, _probeManager, remoteId, keySet, translatorArray, rawSize, maxRecordSize, isLocal);
- else
- return new CRoxieServerIndexReadActivity(this, _probeManager, remoteId, keySet, translatorArray, rawSize, maxRecordSize, sorted, isLocal, maySkip, maxSeekLookahead);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerIndexReadActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- {
- return new CRoxieServerIndexReadActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
- }
- //--------------------------------------------------------------------------------------------------------------------------
- class CRoxieServerNullCountActivity : public CRoxieServerActivity
- {
- bool done;
- public:
- CRoxieServerNullCountActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- done = false;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- done = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (done) return NULL;
- done = true;
- size32_t rowSize = meta.getFixedSize();
- void * nullRow = rowAllocator->createRow();
- if (rowSize == 1)
- *(byte *)nullRow = 0;
- else
- {
- assertex(rowSize == sizeof(unsigned __int64));
- *(unsigned __int64 *)nullRow = 0;
- }
- return rowAllocator->finalizeRow(rowSize, nullRow, rowSize);
- }
- };
- class CRoxieServerIndexCountActivity : public CRoxieServerIndexActivity
- {
- IHThorCompoundCountExtra & countHelper;
- IHThorSourceCountLimit * limitHelper;
- bool done;
- public:
- CRoxieServerIndexCountActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, IKeyArray * _keySet, TranslatorArray *_translators, unsigned _rawSize, bool _isLocal)
- : CRoxieServerIndexActivity(_factory, _probeManager, _remoteId, _keySet, _translators, _rawSize, false, _isLocal, false),
- countHelper((IHThorIndexCountArg &)basehelper),
- done(false)
- {
- limitHelper = static_cast<IHThorSourceCountLimit *>(basehelper.selectInterface(TAIsourcecountlimit_1));
- }
- virtual bool needsAllocator() const { return true; }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- done = false;
- CRoxieServerIndexActivity::start(parentExtractSize, parentExtract, paused);
- choosenLimit = countHelper.getChooseNLimit();
- if (limitHelper)
- {
- rowLimit = limitHelper->getRowLimit();
- keyedLimit = limitHelper->getKeyedLimit();
- }
- if (!paused)
- processAllKeys();
- }
- virtual bool processSingleKey(IKeyIndex *key, IRecordLayoutTranslator * trans)
- {
- unsigned __int64 count = 0;
- if (countHelper.hasFilter())
- {
- while (tlk->lookup(true))
- {
- try
- {
- count += countHelper.numValid(tlk->queryKeyBuffer(callback.getFPosRef()));
- callback.finishedRow();
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- accepted++;
- if (count >= choosenLimit) // MORE - what about limit?
- break;
- }
- }
- else
- count = tlk->getCount(); //MORE: GH->RKC There should be value in providing a max limit to getCount()
- if (count)
- {
- Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult(ctx->queryRowManager(), false);
- if (count > choosenLimit)
- count = choosenLimit;
- void * recBuffer = rowAllocator->createRow();
- if (meta.getFixedSize() == 1)
- *(byte *)recBuffer = (byte)count;
- else
- {
- assertex(meta.getFixedSize() == sizeof(unsigned __int64));
- *(unsigned __int64 *)recBuffer = count;
- }
- recBuffer = rowAllocator->finalizeRow(meta.getFixedSize(), recBuffer, meta.getFixedSize());
- result->append(recBuffer);
- remote.injectResult(result.getClear());
- //GH->RKC for count(,choosen)/exists passing in the previous count would short-circuit this much earlier
- if (count >= choosenLimit)
- return true;
- }
- return false;
- }
- virtual void onLimitExceeded(bool isKeyed)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- if (isKeyed)
- {
- if (indexHelper.getFlags() & (TIRkeyedlimitskips|TIRkeyedlimitcreates))
- {
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- throw makeLimitSkipException(true);
- }
- else
- {
- assertex(limitHelper); // Should not be able to generate exception if there was not one...
- limitHelper->onKeyedLimitExceeded();
- }
- }
- else
- {
- if (indexHelper.getFlags() & (TIRlimitskips|TIRlimitcreates))
- {
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- throw makeLimitSkipException(false);
- }
- else
- {
- assertex(limitHelper);
- limitHelper->onLimitExceeded();
- }
- }
- }
- virtual const void *createLimitFailRow(bool isKeyed)
- {
- throwUnexpected();
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (done) return NULL;
- done = true;
- unsigned __int64 totalCount = 0;
- bool hasLimit = rowLimit != (unsigned __int64) -1;
- try
- {
- loop
- {
- const void * next = remote.nextInGroup();
- if (!next)
- break;
- if (meta.getFixedSize() == 1)
- totalCount += *(byte *)next;
- else
- totalCount += *(unsigned __int64 *) next;
- ReleaseRoxieRow(next);
- if (totalCount > rowLimit || (totalCount > choosenLimit && !hasLimit)) // can't break out early if there is a possibility of later slave throwing limit exception
- break;
- }
- if (totalCount > rowLimit)
- {
- unsigned flags = indexHelper.getFlags();
- if (flags & TIRlimitskips)
- totalCount = 0;
- else if (flags & TIRlimitcreates)
- totalCount = 1;
- else
- {
- assertex(limitHelper);
- limitHelper->onLimitExceeded();
- }
- }
- else if (totalCount > choosenLimit)
- totalCount = choosenLimit;
- }
- catch (IException *E)
- {
- if (QUERYINTERFACE(E, LimitSkipException))
- {
- totalCount = 0;
- unsigned flags = indexHelper.getFlags();
- if (E->errorCode() == KeyedLimitSkipErrorCode)
- {
- if (flags & TIRkeyedlimitcreates)
- totalCount++;
- }
- else
- {
- if (flags & TIRlimitcreates)
- totalCount++;
- }
- if (totalCount > choosenLimit)
- totalCount = choosenLimit; // would have to be weird code (and escape the optimizer...)
- E->Release();
- }
- else
- throw ;
- }
- void * result = rowAllocator->createRow();
- if (meta.getFixedSize() == 1)
- *(byte *)result = (byte)totalCount;
- else
- {
- assertex(meta.getFixedSize() == sizeof(unsigned __int64));
- *(unsigned __int64 *)result = totalCount;
- }
- return rowAllocator->finalizeRow(meta.getFixedSize(), result, meta.getFixedSize());
- }
- };
- class CRoxieServerIndexCountActivityFactory : public CRoxieServerBaseIndexActivityFactory
- {
- public:
- CRoxieServerIndexCountActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- if (!variableFileName && (keySet==NULL || keySet->length()==0))
- return new CRoxieServerNullCountActivity(this, _probeManager);
- // else if (isSimple)
- // return new CRoxieServerSimpleIndexCountActivity(this, keySet->queryKeyPart(0)->queryPart(0), rawSize, maxRecordSize);
- else
- return new CRoxieServerIndexCountActivity(this, _probeManager, remoteId, keySet, translatorArray, rawSize, isLocal);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerIndexCountActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- {
- return new CRoxieServerIndexCountActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
- }
- //--------------------------------------------------------------------------------------------------------------------------
- class CRoxieServerNullIndexAggregateActivity : public CRoxieServerActivity
- {
- IHThorIndexAggregateArg &aggregateHelper;
- unsigned maxRecordSize;
- bool done;
- public:
- CRoxieServerNullIndexAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned _maxRecordSize)
- : CRoxieServerActivity(_factory, _probeManager),
- aggregateHelper((IHThorIndexAggregateArg &)basehelper)
- {
- maxRecordSize = _maxRecordSize;
- done = false;
- }
- virtual bool needsAllocator() const { return true; }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- done = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (done) return NULL;
- done = true;
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t thisSize = aggregateHelper.clearAggregate(rowBuilder);
- return rowBuilder.finalizeRowClear(thisSize);
- }
- };
- class CRoxieServerIndexAggregateActivity : public CRoxieServerIndexActivity
- {
- IHThorCompoundAggregateExtra & aggregateHelper;
- bool done;
- public:
- CRoxieServerIndexAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
- IKeyArray * _keySet, TranslatorArray *_translators, unsigned _rawSize, bool _isLocal)
- : CRoxieServerIndexActivity(_factory, _probeManager, _remoteId, _keySet, _translators, _rawSize, false, _isLocal, false),
- aggregateHelper((IHThorIndexAggregateArg &)basehelper),
- done(false)
- {
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- done = false;
- CRoxieServerIndexActivity::start(parentExtractSize, parentExtract, paused);
- if (!paused)
- processAllKeys();
- }
- virtual bool needsAllocator() const { return true; }
- virtual bool processSingleKey(IKeyIndex *key, IRecordLayoutTranslator * trans)
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
- while (tlk->lookup(true))
- {
- if (!rowBuilder.exists())
- {
- rowBuilder.ensureRow();
- aggregateHelper.clearAggregate(rowBuilder);
- }
- try
- {
- aggregateHelper.processRow(rowBuilder, tlk->queryKeyBuffer(callback.getFPosRef()));
- callback.finishedRow();
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- accepted++;
- }
- if (aggregateHelper.processedAnyRows())
- {
- size32_t size = meta.getRecordSize(rowBuilder.getSelf());
- const void * recBuffer = rowBuilder.finalizeRowClear(size);
- Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult(ctx->queryRowManager(), meta.isVariableSize());
- result->append(recBuffer);
- remote.injectResult(result.getClear());
- }
- return false;
- }
- virtual void onLimitExceeded(bool isKeyed)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- throwUnexpected();
- }
- virtual const void *createLimitFailRow(bool isKeyed)
- {
- throwUnexpected();
- }
- const void * gatherMerged()
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
- const void * firstRow = remote.nextInGroup();
- size32_t finalSize = 0;
- if (!firstRow)
- {
- rowBuilder.ensureRow();
- finalSize = aggregateHelper.clearAggregate(rowBuilder);
- }
- else
- {
- // NOTE need to clone this because going to modify below, could special case 1 row only
- finalSize = cloneRow(rowBuilder, firstRow, meta);
- ReleaseRoxieRow(firstRow);
- }
- loop
- {
- const void * next = remote.nextInGroup();
- if (!next)
- break;
- finalSize = aggregateHelper.mergeAggregate(rowBuilder, next);
- ReleaseRoxieRow(next);
- }
- return rowBuilder.finalizeRowClear(finalSize);
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (done) return NULL;
- const void * ret = gatherMerged();
- done = true;
- return ret;
- }
- };
- class CRoxieServerIndexAggregateActivityFactory : public CRoxieServerBaseIndexActivityFactory
- {
- public:
- CRoxieServerIndexAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- if (!variableFileName && (keySet==NULL || keySet->length()==0))
- return new CRoxieServerNullIndexAggregateActivity(this, _probeManager, maxRecordSize);
- // else if (isSimple)
- // return new CRoxieServerSimpleIndexAggregateActivity(this, keySet->queryKeyPart(0)->queryPart(0), rawSize, maxRecordSize);
- else
- return new CRoxieServerIndexAggregateActivity(this, _probeManager, remoteId, keySet, translatorArray, rawSize, isLocal);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerIndexAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- {
- return new CRoxieServerIndexAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
- }
- //--------------------------------------------------------------------------------------------------------------------------
- class CRoxieServerIndexGroupAggregateActivity : public CRoxieServerIndexActivity, implements IHThorGroupAggregateCallback
- {
- IHThorCompoundGroupAggregateExtra & aggregateHelper;
- RowAggregator singleAggregator;
- RowAggregator resultAggregator;
- unsigned groupSegCount;
- bool gathered;
- bool eof;
- public:
- CRoxieServerIndexGroupAggregateActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
- IKeyArray * _keySet, TranslatorArray *_translators, unsigned _rawSize, bool _isLocal)
- : CRoxieServerIndexActivity(_factory, _probeManager, _remoteId, _keySet, _translators, _rawSize, false, _isLocal, false),
- aggregateHelper((IHThorIndexGroupAggregateArg &)basehelper),
- singleAggregator(aggregateHelper, aggregateHelper),
- resultAggregator(aggregateHelper, aggregateHelper),
- gathered(false), eof(true)
- {
- groupSegCount = 0;
- }
- IMPLEMENT_IINTERFACE
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- gathered= false;
- CRoxieServerIndexActivity::start(parentExtractSize, parentExtract, paused);
- groupSegCount = 0;
- if (!paused)
- processAllKeys();
- resultAggregator.start(rowAllocator);
- }
- virtual bool needsAllocator() const { return true; }
- virtual void reset()
- {
- resultAggregator.reset();
- CRoxieServerIndexActivity::reset();
- }
- virtual void processRow(const void * next)
- {
- singleAggregator.addRow(next);
- }
- virtual void createSegmentMonitors(IKeyManager *key)
- {
- unsigned groupSegSize;
- ThorActivityKind kind = factory->getKind();
- if ((kind==TAKindexgroupcount || kind==TAKindexgroupexists))
- groupSegSize = aggregateHelper.getGroupSegmentMonitorsSize();
- else
- groupSegSize = 0;
- if (groupSegSize)
- {
- key->setMergeBarrier(groupSegSize);
- CRoxieServerIndexActivity::createSegmentMonitors(key);
- unsigned numSegs = tlk->ordinality();
- for (unsigned segNo = 0; segNo < numSegs; segNo++)
- {
- IKeySegmentMonitor *seg = tlk->item(segNo);
- if (seg->getOffset()+seg->getSize()==groupSegSize)
- {
- groupSegCount = segNo+1;
- break;
- }
- }
- assertex(groupSegCount);
- }
- else
- CRoxieServerIndexActivity::createSegmentMonitors(key);
- }
- virtual bool processSingleKey(IKeyIndex *key, IRecordLayoutTranslator * trans)
- {
- Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult(ctx->queryRowManager(), meta.isVariableSize());
- singleAggregator.start(rowAllocator);
- ThorActivityKind kind = factory->getKind();
- while (tlk->lookup(true))
- {
- try
- {
- if (groupSegCount && !trans)
- {
- AggregateRowBuilder &rowBuilder = singleAggregator.addRow(tlk->queryKeyBuffer(callback.getFPosRef()));
- callback.finishedRow();
- if (kind==TAKindexgroupcount)
- {
- unsigned __int64 count = tlk->getCurrentRangeCount(groupSegCount);
- aggregateHelper.processCountGrouping(rowBuilder, count-1);
- }
- if (!tlk->nextRange(groupSegCount))
- break;
- }
- else
- {
- aggregateHelper.processRow(tlk->queryKeyBuffer(callback.getFPosRef()), this);
- callback.finishedRow();
- }
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- accepted++;
- }
- loop
- {
- Owned<AggregateRowBuilder> next = singleAggregator.nextResult();
- if (!next)
- break;
- size32_t size = next->querySize();
- result->append(next->finalizeRowClear());
- }
- remote.injectResult(result.getClear());
- singleAggregator.reset();
- return false;
- }
- virtual void onLimitExceeded(bool isKeyed)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);DBGLOG("%d activityid = %d", __LINE__, activityId);
- throwUnexpected();
- }
- virtual const void *createLimitFailRow(bool isKeyed)
- {
- throwUnexpected();
- }
- void gatherMerged()
- {
- gathered = true;
- loop
- {
- const void * next = remote.nextInGroup();
- if (!next)
- break;
- resultAggregator.mergeElement(next);
- ReleaseRoxieRow(next);
- }
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if (eof)
- return NULL;
- if (!gathered)
- gatherMerged();
- Owned<AggregateRowBuilder> next = resultAggregator.nextResult();
- if (next)
- {
- processed++;
- return next->finalizeRowClear();
- }
- eof = true;
- return NULL;
- }
- };
- class CRoxieServerIndexGroupAggregateActivityFactory : public CRoxieServerBaseIndexActivityFactory
- {
- public:
- CRoxieServerIndexGroupAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- if (!variableFileName && (keySet==NULL || keySet->length()==0))
- return new CRoxieServerNullActivity(this, _probeManager);
- // else if (isSimple)
- // return new CRoxieServerSimpleIndexGroupAggregateActivity(this, keySet->queryKeyPart(0)->queryPart(0), rawSize, maxRecordSize);
- else
- return new CRoxieServerIndexGroupAggregateActivity(this, _probeManager, remoteId, keySet, translatorArray, rawSize, isLocal);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerIndexGroupAggregateActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- {
- return new CRoxieServerIndexGroupAggregateActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
- }
- //--------------------------------------------------------------------------------------------------------------------------
- class CRoxieServerIndexNormalizeActivity : public CRoxieServerIndexReadBaseActivity
- {
- IHThorCompoundNormalizeExtra & readHelper;
- public:
- CRoxieServerIndexNormalizeActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId,
- IKeyArray * _keySet, TranslatorArray *_translators, unsigned _rawSize, unsigned _maxRecordSize, bool _sorted, bool _isLocal)
- : CRoxieServerIndexReadBaseActivity(_factory, _probeManager, _remoteId, _keySet, _translators, _rawSize, _maxRecordSize, _sorted, _isLocal, false),
- readHelper((IHThorIndexNormalizeArg &)basehelper)
- {
- }
- virtual bool needsAllocator() const { return true; }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerIndexReadBaseActivity::start(parentExtractSize, parentExtract, paused);
- rowLimit = readHelper.getRowLimit();
- keyedLimit = readHelper.getKeyedLimit();
- if (!paused)
- processAllKeys();
- }
- virtual bool processSingleKey(IKeyIndex *key, IRecordLayoutTranslator * trans)
- {
- unsigned keyedCount = 0;
- RtlDynamicRowBuilder rowBuilder(rowAllocator, false);
- while (tlk->lookup(true))
- {
- keyedCount++;
- if (keyedCount > keyedLimit)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d line = %d", activityId, __LINE__);
- onLimitExceeded(true);
- break;
- }
- size32_t transformedSize;
-
- if (readHelper.first(tlk->queryKeyBuffer(callback.getFPosRef())))
- {
- Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult(ctx->queryRowManager(), meta.isVariableSize());
- do
- {
- rowBuilder.ensureRow();
- try
- {
- transformedSize = readHelper.transform(rowBuilder);
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- if (transformedSize)
- {
- // MORE - would be a good idea to stop these asap if rowlimit exceeded
- result->append(rowBuilder.finalizeRowClear(transformedSize));
- accepted++;
- }
- else
- rejected++;
- } while (readHelper.next());
- remote.injectResult(result.getClear());
- callback.finishedRow();
- }
- }
- return false;
- }
- virtual void onLimitExceeded(bool isKeyed)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- if (isKeyed)
- {
- if (indexHelper.getFlags() & (TIRkeyedlimitskips|TIRkeyedlimitcreates))
- {
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- throw makeLimitSkipException(true);
- }
- else
- readHelper.onKeyedLimitExceeded();
- }
- else
- {
- if (indexHelper.getFlags() & (TIRlimitskips||TIRlimitcreates))
- {
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- throw makeLimitSkipException(false);
- }
- else
- readHelper.onLimitExceeded();
- }
- }
- virtual const void *createLimitFailRow(bool isKeyed)
- {
- UNIMPLEMENTED;
- }
- };
- class CRoxieServerIndexNormalizeActivityFactory : public CRoxieServerBaseIndexActivityFactory
- {
- public:
- CRoxieServerIndexNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- : CRoxieServerBaseIndexActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- if (!variableFileName && (keySet==NULL || keySet->length()==0))
- return new CRoxieServerNullActivity(this, _probeManager);
- else
- return new CRoxieServerIndexNormalizeActivity(this, _probeManager, remoteId, keySet, translatorArray, rawSize, maxRecordSize, sorted, isLocal);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerIndexNormalizeActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- {
- return new CRoxieServerIndexNormalizeActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
- }
- //=================================================================================
- class CRoxieServerCountDiskActivity : public CRoxieServerActivity, implements IRoxieServerErrorHandler
- {
- unsigned __int64 answer;
- public:
- CRoxieServerCountDiskActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, unsigned __int64 _answer)
- : CRoxieServerActivity(_factory, _probeManager),
- answer(_answer)
- {
- }
- virtual const void *nextInGroup()
- {
- throwUnexpected();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
- }
- virtual __int64 evaluate()
- {
- return answer;
- }
- virtual void onLimitExceeded(bool isKeyed)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- throwUnexpected();
- }
- virtual const void *createLimitFailRow(bool isKeyed)
- {
- throwUnexpected();
- }
- };
- class CRoxieServerVariableCountDiskActivity : public CRoxieServerActivity, implements IRoxieServerErrorHandler
- {
- public:
- CRoxieServerVariableCountDiskActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager)
- {
- }
- virtual const void *nextInGroup()
- {
- throwUnexpected();
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() called for source activity");
- }
- virtual __int64 evaluate()
- {
- IHThorCountFileArg &helper = (IHThorCountFileArg &) basehelper;
- bool isOpt = (helper.getFlags() & TDRoptional) != 0;
- unsigned recsize = helper.queryRecordSize()->getFixedSize();
- assertex(recsize);
- Owned<const IResolvedFile> varFileInfo = resolveLFN(helper.getFileName(), isOpt);
- return varFileInfo->getFileSize() / recsize;
- }
- virtual void onLimitExceeded(bool isKeyed)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- throwUnexpected();
- }
- virtual const void *createLimitFailRow(bool isKeyed)
- {
- throwUnexpected();
- }
- };
- class CRoxieServerCountDiskActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- unsigned __int64 answer;
- bool variableFileName;
- Owned<const IResolvedFile> datafile;
- CRoxieServerCountDiskActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- Owned<IHThorCountFileArg> helper = (IHThorCountFileArg *) helperFactory();
- variableFileName = (helper->getFlags() & (TDXvarfilename|TDXdynamicfilename)) != 0;
- assertex(helper->queryRecordSize()->isFixedSize());
- if (!variableFileName)
- {
- unsigned recsize = helper->queryRecordSize()->getFixedSize();
- assertex(recsize);
- const char *fileName = helper->getFileName();
- bool isOpt = (helper->getFlags() & TDRoptional) != 0;
- datafile.setown(queryFactory.queryPackage().lookupFileName(fileName, isOpt, true));
- offset_t filesize = datafile ? datafile->getFileSize() : 0;
- if (filesize % recsize != 0)
- throw MakeStringException(ROXIE_MISMATCH, "Record size mismatch for file %s - %"I64F"d is not a multiple of fixed record size %d", fileName, filesize, recsize);
- answer = filesize / recsize;
- }
- else
- answer = 0;
- }
- virtual IRoxieServerActivity *createFunction(IHThorArg &arg, IProbeManager *_probeManager) const
- {
- arg.Release();
- if (variableFileName)
- return new CRoxieServerVariableCountDiskActivity(this, _probeManager);
- else
- return new CRoxieServerCountDiskActivity(this, _probeManager, answer);
- }
- virtual void setInput(unsigned idx, unsigned source, unsigned sourceidx)
- {
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() should not be called for CountDisk activity");
- }
- virtual bool isFunction() const
- {
- return true;
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return NULL;
- }
- };
- IRoxieServerActivityFactory *createRoxieServerDiskCountActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode)
- {
- return new CRoxieServerCountDiskActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode);
- }
- //=================================================================================
- class CRoxieServerFetchActivity : public CRoxieServerActivity, implements IRecordPullerCallback, implements IRoxieServerErrorHandler
- {
- IHThorFetchBaseArg &helper;
- IHThorFetchContext * fetchContext;
- Linked<IFilePartMap> map;
- CRemoteResultAdaptor remote;
- RecordPullerThread puller;
- bool needsRHS;
- bool variableFileName;
- bool isOpt;
- Owned<const IResolvedFile> varFileInfo;
- public:
- CRoxieServerFetchActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, IFilePartMap *_map)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorFetchBaseArg &)basehelper), map(_map), remote(_remoteId, meta.queryOriginal(), helper, *this, true, true), puller(false)
- {
- fetchContext = static_cast<IHThorFetchContext *>(helper.selectInterface(TAIfetchcontext_1));
- needsRHS = helper.transformNeedsRhs();
- variableFileName = (fetchContext->getFetchFlags() & (FFvarfilename|FFdynamicfilename)) != 0;
- isOpt = (fetchContext->getFetchFlags() & FFdatafileoptional) != 0;
- }
- virtual const IResolvedFile *queryVarFileInfo() const
- {
- return varFileInfo;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- remote.onCreate(this, this, _ctx, _colocalParent);
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- if (idx)
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
- puller.setInput(this, _in);
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- remote.onStart(parentExtractSize, parentExtract);
- remote.setLimits(helper.getRowLimit(), (unsigned __int64) -1, I64C(0x7FFFFFFFFFFFFFFF));
- if (variableFileName)
- {
- varFileInfo.setown(resolveLFN(fetchContext->getFileName(), isOpt));
- map.setown(varFileInfo->getFileMap());
- }
- puller.start(parentExtractSize, parentExtract, paused, ctx->fetchPreload(), false, ctx);
- }
- virtual void stop(bool aborting)
- {
- // Called from remote, so no need to call back to it....
- puller.stop(aborting);
- CRoxieServerActivity::stop(aborting);
- }
- virtual void reset()
- {
- processed = remote.processed;
- remote.processed = 0;
- puller.reset();
- if (variableFileName)
- {
- varFileInfo.clear();
- map.clear();
- }
- CRoxieServerActivity::reset();
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- if (idx==(unsigned)-1)
- idx = 0;
- return idx ? NULL : &remote;
- }
- virtual void processRow(const void *row)
- {
- // called from puller thread
- offset_t rp = fetchContext->extractPosition(row);
- unsigned partNo;
- if (isLocalFpos(rp))
- partNo = getLocalFposPart(rp) + 1;
- else
- partNo = map->mapOffset(rp);
- if (needsRHS)
- {
- Owned<IEngineRowAllocator> extractAllocator = ctx->queryCodeContext()->getRowAllocator(helper.queryExtractedSize(), activityId);
- RtlDynamicRowBuilder rb(extractAllocator, true);
- unsigned rhsSize = helper.extractJoinFields(rb, row);
- char * block = (char *) remote.getMem(partNo, 0, sizeof(rp) + sizeof(rhsSize) + rhsSize); // MORE - superfiles
- *(offset_t *) block = rp;
- block += sizeof(rp);
- *(unsigned *) block = rhsSize;
- block += sizeof(rhsSize);
- memcpy(block, rb.row(), rhsSize);
- }
- else
- *(offset_t *) remote.getMem(partNo, 0, sizeof(rp)) = rp; // MORE - superfiles
- ReleaseRoxieRow(row);
- }
- void processEOG()
- {
- #ifdef FETCH_PRESERVES_GROUPING
- UNIMPLEMENTED;
- #endif
- // else discard is correct
- }
- void processGroup(const ConstPointerArray &)
- {
- throwUnexpected();
- }
- void processDone()
- {
- // called from puller thread
- remote.flush();
- remote.senddone();
- }
- virtual bool fireException(IException *e)
- {
- return remote.fireException(e);
- }
- virtual void onLimitExceeded(bool isKeyed)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- if (isKeyed)
- throwUnexpected();
- helper.onLimitExceeded();
- }
- virtual const void *createLimitFailRow(bool isKeyed)
- {
- UNIMPLEMENTED;
- }
- virtual const void *nextInGroup()
- {
- throwUnexpected(); // I am nobody's input
- }
- };
- class CRoxieServerFetchActivityFactory : public CRoxieServerActivityFactory
- {
- RemoteActivityId remoteId;
- Owned<IFilePartMap> map;
- bool variableFileName;
- Owned<const IResolvedFile> datafile;
- public:
- CRoxieServerFetchActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), remoteId(_remoteId)
- {
- Owned<IHThorFetchBaseArg> helper = (IHThorFetchBaseArg *) helperFactory();
- IHThorFetchContext *fetchContext = static_cast<IHThorFetchContext *>(helper->selectInterface(TAIfetchcontext_1));
- variableFileName = (fetchContext->getFetchFlags() & (FFvarfilename|FFdynamicfilename)) != 0;
- if (!variableFileName)
- {
- datafile.setown(_queryFactory.queryPackage().lookupFileName(fetchContext->getFileName(), (fetchContext->getFetchFlags() & FFdatafileoptional) != 0, true));
- if (datafile)
- map.setown(datafile->getFileMap());
- }
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerFetchActivity(this, _probeManager, remoteId, map);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerFetchActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, IPropertyTree &_graphNode)
- {
- return new CRoxieServerFetchActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _graphNode);
- }
- // MORE - is there any point keeping this now?
- class CRoxieServerDummyActivityFactory : public CRoxieServerActivityFactory // not a real activity - just used to properly link files
- {
- public:
- Owned<const IResolvedFile> indexfile;
- Owned<const IResolvedFile> datafile;
- Owned<IKeyArray> keySet;
- Owned<IFileIOArray> files;
- Owned<IFilePartMap> map;
- TranslatorArray layoutTranslators;
- CRoxieServerDummyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool isLoadDataOnly)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- try // does not want any missing file errors to be fatal, or throw traps - just log it
- {
- if (_graphNode.getPropBool("att[@name='_isSpill']/@value", false) || _graphNode.getPropBool("att[@name='_isSpillGlobal']/@value", false))
- return; // ignore 'spills'
- bool isLocal = _graphNode.getPropBool("att[@name='local']/@value");
- bool isOpt = _graphNode.getPropBool("att[@name='_isOpt']/@value") || pretendAllOpt;
- if (queryNodeIndexName(_graphNode))
- {
- indexfile.setown(queryFactory.queryPackage().lookupFileName(queryNodeIndexName(_graphNode), isOpt, true));
- if (indexfile)
- keySet.setown(indexfile->getKeyArray(NULL, &layoutTranslators, isOpt, isLocal ? queryFactory.queryChannel() : 0, false));
- }
- if (queryNodeFileName(_graphNode))
- {
- datafile.setown(_queryFactory.queryPackage().lookupFileName(queryNodeFileName(_graphNode), isOpt, true));
- if (datafile)
- {
- if (isLocal)
- files.setown(datafile->getIFileIOArray(isOpt, queryFactory.queryChannel()));
- else
- map.setown(datafile->getFileMap());
- }
- }
- }
- catch(IException *E)
- {
- StringBuffer errors;
- E->errorMessage(errors);
- DBGLOG("%s File error = %s", (isLoadDataOnly) ? "LOADDATAONLY" : "SUSPENDED QUERY", errors.str());
- E->Release();
- }
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const { throw MakeStringException(ROXIE_INTERNAL_ERROR, "%s query %s is suspended and cannot be executed - error occurred at %s(%d)", (queryFactory.isQueryLibrary()) ? "Library" : " ", queryFactory.queryQueryName(), __FILE__, __LINE__); }
- };
- IRoxieServerActivityFactory *createRoxieServerDummyActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, IPropertyTree &_graphNode, bool isLoadDataOnly)
- {
- return new CRoxieServerDummyActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _graphNode, isLoadDataOnly);
- }
- //=====================================================================================================
- // Keyed joins...
- //
- // Input records are pulled by a puller thread, which checks each LHS record to determine which (if any) channels it
- // may have RHS matches on, and sends the relevant fields to the relevant slaves.
- // A separate thread (the caller's thread) is waiting on slave replies, and once it has all replies for a given LHS record or group of records, calls
- // the transform and returns rows that are created.
- // 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)
- //
- //=====================================================================================================
- class CJoinGroup;
- interface IJoinProcessor
- {
- virtual void processEOG() = 0;
- virtual CJoinGroup *createJoinGroup(const void *row) = 0;
- virtual void noteEndReceived(CJoinGroup *jg, unsigned candidateCount) = 0;
- virtual bool fireException(IException *E) = 0;
- virtual void processCompletedGroups() = 0;
- };
- //------------------------------------------------------------------------------------------------------
- // Class CJoinGroup has a record per LHS row, plus (if preserving grouping) a 'head of group' record
- // It gathers all the corresponding RHS rows, keeping track of how may slave transactions are pending in endMarkersPending
- // If preserving groups, the 'head of group' record keeps track of how many LHS records in the group are still incomplete.
- // CJoinGroup records are allocated out of the Roxie row memory manager by overloading operator new, so that they are included in the
- // per-query limits etc (Note that the pointer array block is not though).
- // Because of that, the exact size is significant - especially whether fit just under or just over a chunking threshold...
- //
- // There are two phases to the life of a JoinGroup - it is created by the puller thread that is also firing off slave requests
- // notePending will be called once for every slave request. Puller thread calls noteEndReceived(0) once when done - this corresponds to the
- // initial count when created.
- // Slave replies and are noted by the consumer thread calling addRightMatch() and noteEndReceived(n).
- // Once endMarkersPending reaches 0, JoinGroup is complete. Last thread to call noteEndReceived will process the rows and destroy the group.
- // There is no need for a critsec because although multiple threads will access at different times, only the consumer thread will
- // access any modifiable member variables while endMarkersPending != 0 (i.e. complete() is false). Once complete returns true there is a single
- // remaining reference and the JoinGroup will be processed and destroyed.
- //
- //------------------------------------------------------------------------------------------------------
- class CJoinGroup : public CInterface, implements IInterface
- {
- protected:
- const void *left; // LHS row
- PointerArrayOf<KeyedJoinHeader> rows; // matching RHS rows
- atomic_t endMarkersPending; // How many slave responses still waiting for
- CJoinGroup *groupStart; // Head of group, or NULL if not grouping
- unsigned lastPartNo;
- unsigned pos;
- unsigned candidates; // Number of RHS keyed candidates - note this may not be the same as rows.ordinality()
- public:
- #undef new
- void *operator new(size_t size, IRowManager *a, unsigned activityId)
- {
- return a->allocate(size, activityId);
- }
- #if defined(_DEBUG) && defined(_WIN32) && !defined(USING_MPATROL)
- #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
- #endif
- void operator delete(void *ptr, IRowManager *a, unsigned activityId)
- {
- ReleaseRoxieRow(ptr);
- }
- void operator delete(void *ptr)
- {
- ReleaseRoxieRow(ptr);
- }
- public:
- IMPLEMENT_IINTERFACE;
- CJoinGroup(const void *_left, CJoinGroup *_groupStart)
- {
- #ifdef TRACE_JOINGROUPS
- DBGLOG("Creating joinGroup %p, groupstart %p", this, _groupStart);
- #endif
- candidates = 0;
- lastPartNo = 0;
- pos = 0;
- left = _left;
- groupStart = _groupStart;
- if (_groupStart)
- {
- atomic_inc(&_groupStart->endMarkersPending);
- }
- atomic_set(&endMarkersPending, 1);
- }
- ~CJoinGroup()
- {
- #ifdef TRACE_JOINGROUPS
- DBGLOG("Destroying joinGroup %p", this);
- #endif
- if (left)
- {
- ReleaseRoxieRow(left);
- ForEachItemIn(idx, rows)
- ReleaseRoxieRow(rows.item(idx));
- rows.kill();
- }
- }
- inline bool isHeadRecord() const
- {
- return left==NULL;
- }
- inline bool complete() const
- {
- return atomic_read(&endMarkersPending) == 0;
- }
- #ifdef TRACE_JOINGROUPS
- inline void notePending(unsigned lineNo)
- #else
- inline void notePending()
- #endif
- {
- assert(!complete());
- atomic_inc(&endMarkersPending);
- #ifdef TRACE_JOINGROUPS
- DBGLOG("CJoinGroup::notePending %p from %d, count became %d group count %d", this, lineNo, atomic_read(&endMarkersPending), groupStart ? atomic_read(&groupStart->endMarkersPending) : 0);
- #endif
- }
- inline bool inGroup(CJoinGroup *leader) const
- {
- return groupStart==leader;
- }
- inline const KeyedJoinHeader *queryRow(unsigned idx) const
- {
- // Single threaded by now
- assert(complete());
- return rows.item(idx);
- }
- #ifdef TRACE_JOINGROUPS
- bool noteEndReceived(unsigned candidateCount, unsigned lineNo)
- #else
- bool noteEndReceived(unsigned candidateCount)
- #endif
- {
- assert(!complete());
- if (candidateCount)
- {
- candidates += candidateCount;
- }
- #ifdef TRACE_JOINGROUPS
- 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);
- #endif
- // NOTE - as soon as endMarkersPending and groupStart->endMarkersPending are decremented to zero this object may get released asynchronously by other threads
- // There must therefore be nothing in this method after them that acceses member variables. Think of it as a delete this...
- // In particular, we can't safely reference groupStart after the dec_and_test of endMarkersPending, hence copy local first
- CJoinGroup *localGroupStart = groupStart;
- if (atomic_dec_and_test(&endMarkersPending))
- {
- if (localGroupStart)
- return atomic_dec_and_test(&localGroupStart->endMarkersPending);
- else
- return true;
- }
- else
- return false;
- }
- inline const void *queryLeft() const
- {
- return left;
- }
- void addRightMatch(KeyedJoinHeader *right)
- {
- assert(!complete());
- unsigned short partNo = right->partNo;
- if (partNo != lastPartNo)
- {
- // MORE - should we binchop? If we did we would need to be careful to find LAST match
- if (partNo > lastPartNo)
- pos = rows.length();
- while (pos>0)
- {
- if (rows.item(pos-1)->partNo <= partNo)
- break;
- pos--;
- }
- lastPartNo = partNo;
- }
- rows.add(right, pos);
- pos++;
- }
- inline unsigned rowsSeen() const
- {
- assert(complete());
- return rows.length();
- }
- inline unsigned candidateCount() const
- {
- assert(complete());
- return candidates;
- }
- };
- #ifdef TRACE_JOINGROUPS
- #define notePending() notePending(__LINE__)
- #define noteEndReceived(a) noteEndReceived(a, __LINE__)
- #endif
- class KeyedJoinRemoteAdaptor : public CRemoteResultAdaptor // MORE - not sure it should be derived from this - makes processed all wrong, for example
- {
- private:
- SafeQueueOf<const void, true> ready;
- public:
- IHThorKeyedJoinArg &helper;
- unsigned joinProcessed;
- bool isFullKey;
- bool eof;
- bool isSimple;
- bool allPulled;
- unsigned __int64 totalCycles;
- unsigned activityId;
- RecordPullerThread &puller;
- SafeQueueOf<const void, true> injected; // Used in isSimple mode
- Owned<IEngineRowAllocator> ccdRecordAllocator;
- IJoinProcessor &processor;
- KeyedJoinRemoteAdaptor(const RemoteActivityId &_remoteId, IHThorKeyedJoinArg &_helper, IRoxieServerActivity &_activity, bool _isFullKey, bool _isSimple,
- RecordPullerThread &_puller, IJoinProcessor &_processor)
- : helper(_helper),
- CRemoteResultAdaptor(_remoteId, 0, _helper, _activity, true, true),
- isFullKey(_isFullKey),
- isSimple(_isSimple),
- puller(_puller),
- processor(_processor),
- activityId(_activity.queryId())
- {
- joinProcessed = 0;
- totalCycles = 0;
- allPulled = false;
- eof = false;
- }
- virtual void onCreate(IRoxieInput *_owner, IRoxieServerErrorHandler *_errorHandler, IRoxieSlaveContext *_ctx, IHThorArg *_colocalArg)
- {
- CRemoteResultAdaptor::onCreate(_owner, _errorHandler, _ctx, _colocalArg);
- ccdRecordAllocator.setown(ctx->queryCodeContext()->getRowAllocator(QUERYINTERFACE(helper.queryJoinFieldsRecordSize(), IOutputMetaData), activityId));
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- joinProcessed = 0;
- totalCycles = 0;
- allPulled = false;
- assertex(ready.ordinality()==0);
- CRemoteResultAdaptor::start(parentExtractSize, parentExtract, paused);
- }
- virtual void reset()
- {
- CRemoteResultAdaptor::reset();
- while (ready.ordinality())
- {
- const void *goer = ready.dequeue();
- if (goer)
- ReleaseRoxieRow(goer);
- }
- while (injected.ordinality())
- {
- const void *goer = injected.dequeue();
- if (goer)
- ReleaseRoxieRow(goer);
- }
- }
- inline void addResult(const void *row)
- {
- ready.enqueue(row);
- }
- virtual unsigned __int64 queryTotalCycles() const
- {
- return totalCycles;
- }
- virtual IOutputMetaData * queryOutputMeta() const
- {
- return owner->queryOutputMeta();
- }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- loop
- {
- if (eof)
- return NULL;
- processSlaveResults();
- if (ready.ordinality())
- {
- const void *result = ready.dequeue();
- if (result)
- joinProcessed++;
- return result;
- }
- else
- eof = true;
- }
- }
- private:
- void processSlaveResults()
- {
- while (!ready.ordinality())
- {
- KeyedJoinHeader *fetchedData;
- if (isSimple)
- {
- while (!allPulled && !injected.ordinality())
- {
- if (!puller.pullRecords(1))
- {
- puller.done();
- allPulled = true;
- }
- }
- fetchedData = (KeyedJoinHeader *) injected.dequeue();
- }
- else
- fetchedData = (KeyedJoinHeader *) CRemoteResultAdaptor::nextInGroup();
- if (fetchedData)
- {
- CJoinGroup *thisGroup = fetchedData->thisGroup;
- if (fetchedData->partNo == (unsigned short) -1)
- {
- #ifdef TRACE_JOINGROUPS
- CTXLOG("Got end for group %p", thisGroup);
- #endif
- unsigned candidateCount = (unsigned) fetchedData->fpos;
- ReleaseRoxieRow(fetchedData);
- processor.noteEndReceived(thisGroup, candidateCount); // note - this can throw exception. So release fetchdata before calling
- }
- else
- {
- #ifdef TRACE_JOINGROUPS
- CTXLOG("Reading another %d bytes for group %p data", ccdRecordSize, thisGroup);
- #endif
- thisGroup->addRightMatch(fetchedData);
- if (isFullKey)
- {
- #ifdef TRACE_JOINGROUPS
- CTXLOG("Calling noteEndReceived for record returned from FETCH of full keyed join");
- #endif
- processor.noteEndReceived(thisGroup, 0); // note - this can throw exception. So release fetchdata before calling
- }
- }
- }
- else
- break;
- }
- }
- };
- class CRoxieServerFullKeyedJoinHead: public CRoxieServerActivity, implements IRecordPullerCallback, implements IRoxieServerErrorHandler
- {
- IHThorKeyedJoinArg &helper;
- Owned<IKeyManager> tlk;
- Linked<IKeyArray> keySet;
- Linked<TranslatorArray> translators;
- CRemoteResultAdaptor remote;
- RecordPullerThread puller;
- IOutputMetaData *indexReadMeta;
- IJoinProcessor *joinHandler;
- bool variableIndexFileName;
- bool indexReadInputRecordVariable;
- bool isLocal;
- Owned<IEngineRowAllocator> indexReadAllocator;
- Owned<const IResolvedFile> varFileInfo;
- IRoxieInput *indexReadInput;
- IIndexReadActivityInfo *rootIndex;
- public:
- CRoxieServerFullKeyedJoinHead(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, IKeyArray * _keySet, TranslatorArray *_translators, IOutputMetaData *_indexReadMeta, IJoinProcessor *_joinHandler, bool _isLocal)
- : CRoxieServerActivity(_factory, _probeManager),
- helper((IHThorKeyedJoinArg &)basehelper),
- tlk(createKeyManager(NULL, 0, this)),
- translators(_translators),
- keySet(_keySet),
- remote(_remoteId, 0, helper, *this, true, true),
- indexReadMeta(_indexReadMeta),
- joinHandler(_joinHandler),
- puller(false),
- isLocal(_isLocal)
- {
- variableIndexFileName = (helper.getJoinFlags() & (JFvarindexfilename|JFdynamicindexfilename)) != 0;
- indexReadInputRecordVariable = indexReadMeta->isVariableSize();
- indexReadInput = NULL;
- rootIndex = NULL;
- }
- virtual const IResolvedFile *queryVarFileInfo() const
- {
- return varFileInfo;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- remote.onCreate(this, this, _ctx, _colocalParent);
- indexReadAllocator.setown(ctx->queryCodeContext()->getRowAllocator(indexReadMeta, activityId));
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- if (!idx)
- puller.setInput(this, _in);
- else if (idx==1)
- indexReadInput = _in;
- else
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
- }
- virtual void serializeExtra(MemoryBuffer &out)
- {
- if (helper.getJoinFlags() & JFindexfromactivity)
- {
- assertex(rootIndex);
- const RemoteActivityId& indexId = rootIndex->queryRemoteId();
- indexId.serialize(out);
- // could mess about reserving space for length then patching it again, to avoid copy, but probably not worth it
- MemoryBuffer tmp;
- rootIndex->queryActivity()->serializeCreateStartContext(tmp);
- if (rootIndex->queryActivity()->queryVarFileInfo())
- rootIndex->queryActivity()->queryVarFileInfo()->queryTimeStamp().serialize(tmp);
- unsigned ctxlen = tmp.length();
- out.append(ctxlen).append(tmp);
- }
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- if (indexReadInput)
- {
- indexReadInput->start(parentExtractSize, parentExtract, true); // paused=true because we don't want to actually run the index read
- rootIndex = indexReadInput->queryIndexReadActivity();
- if (!rootIndex)
- throw MakeStringException(ROXIE_INTERNAL_ERROR,"Index in keyed join %d could not be resolved", queryId());
- }
- remote.onStart(parentExtractSize, parentExtract);
- remote.setLimits(helper.getRowLimit(), (unsigned __int64) -1, I64C(0x7FFFFFFFFFFFFFFF));
- if (rootIndex)
- {
- varFileInfo.setown(rootIndex->getVarFileInfo());
- translators.setown(rootIndex->getTranslators());
- keySet.setown(rootIndex->getKeySet());
- }
- else if (variableIndexFileName)
- {
- varFileInfo.setown(resolveLFN(helper.getIndexFileName(), false));
- translators.setown(new TranslatorArray);
- keySet.setown(varFileInfo->getKeyArray(factory->queryActivityMeta(), translators, false, isLocal ? factory->queryQueryFactory().queryChannel() : 0, factory->queryQueryFactory().getEnableFieldTranslation())); // MORE - isLocal?
- }
- puller.start(parentExtractSize, parentExtract, paused, ctx->fullKeyedJoinPreload(), false, ctx);
- }
- virtual void stop(bool aborting)
- {
- puller.stop(aborting);
- CRoxieServerActivity::stop(aborting);
- }
- virtual void reset()
- {
- CRoxieServerActivity::reset();
- puller.reset();
- if (varFileInfo)
- {
- keySet.clear();
- varFileInfo.clear();
- }
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- if (idx==(unsigned)-1)
- idx = 0;
- return idx ? NULL : &remote;
- }
- virtual void processRow(const void *row)
- {
- // MORE - this code seems to be pretty much duplicated below in half-keyed....
- // called from front puller thread
- // buffer up an IndexRead request
- if (helper.leftCanMatch(row))
- {
- RtlDynamicRowBuilder extractedBuilder(indexReadAllocator);
- unsigned indexReadSize = helper.extractIndexReadFields(extractedBuilder, row);
- OwnedConstRoxieRow extracted;
- if (indexReadSize)
- extracted.setown(extractedBuilder.finalizeRowClear(indexReadSize));
- CJoinGroup *jg = joinHandler->createJoinGroup(row);
- for (unsigned partNo = 0; partNo < keySet->length(); partNo++)
- {
- IKeyIndexBase *thisBase = keySet->queryKeyPart(partNo);
- if (thisBase)
- {
- unsigned fileNo = 0;
- IKeyIndex *thisKey = thisBase->queryPart(fileNo);
- try
- {
- tlk->setKey(thisKey);
- tlk->setLayoutTranslator(translators->item(fileNo));
- helper.createSegmentMonitors(tlk, extracted);
- if (rootIndex)
- rootIndex->mergeSegmentMonitors(tlk);
- tlk->finishSegmentMonitors();
- tlk->reset();
- loop
- {
- typedef const void * cvp;
- if (thisKey->isTopLevelKey())
- {
- bool locallySorted = !thisKey->isFullySorted();
- while (locallySorted || tlk->lookup(false))
- {
- unsigned slavePart = locallySorted ? 0 : (unsigned) tlk->queryFpos();
- if (locallySorted || slavePart)
- {
- cvp *outputBuffer = (cvp *) remote.getMem(slavePart, fileNo, indexReadSize + sizeof(cvp) + (indexReadInputRecordVariable ? sizeof(unsigned) : 0));
- *outputBuffer++ = jg;
- if (indexReadInputRecordVariable)
- {
- *(unsigned *) outputBuffer = indexReadSize;
- outputBuffer = (cvp*) (((unsigned *) outputBuffer) + 1);
- }
- jg->notePending();
- memcpy(outputBuffer, extracted, indexReadSize);
- if (locallySorted)
- {
- for (unsigned i = 1; i < numChannels; i++)
- jg->notePending();
- break;
- }
- }
- }
- }
- else
- {
- // MORE - this code seems to be duplicated in half keyed
- unsigned accepted = 0;
- unsigned rejected = 0;
- Owned<CRowArrayMessageResult> result = new CRowArrayMessageResult(ctx->queryRowManager(), true);
- jg->notePending();
- unsigned candidateCount = 0;
- while (tlk->lookup(true))
- {
- candidateCount++;
- atomic_inc(&indexRecordsRead);
- KLBlobProviderAdapter adapter(tlk);
- offset_t recptr;
- const byte *indexRow = tlk->queryKeyBuffer(recptr);
- if (helper.indexReadMatch(extracted, indexRow, recptr, &adapter))
- {
- KeyedJoinHeader *rhs = (KeyedJoinHeader *) ctx->queryRowManager().allocate(KEYEDJOIN_RECORD_SIZE(0), activityId);
- rhs->fpos = recptr;
- rhs->thisGroup = jg;
- rhs->partNo = partNo;
- result->append(rhs);
- }
- else
- {
- rejected++;
- atomic_inc(&postFiltered);
- }
- }
- // output an end marker for the matches to this group
- KeyedJoinHeader *endMarker = (KeyedJoinHeader *) ctx->queryRowManager().allocate(KEYEDJOIN_RECORD_SIZE(0), activityId);
- endMarker->fpos = (offset_t) candidateCount;
- endMarker->thisGroup = jg;
- endMarker->partNo = (unsigned short) -1;
- result->append(endMarker);
- remote.injectResult(result.getClear());
- if (accepted)
- noteStatistic(STATS_ACCEPTED, accepted, 1);
- if (rejected)
- noteStatistic(STATS_REJECTED, rejected, 1);
- }
- if (++fileNo < thisBase->numParts())
- {
- thisKey = thisBase->queryPart(fileNo);
- tlk->setKey(thisKey);
- tlk->setLayoutTranslator(translators->item(fileNo));
- tlk->reset();
- }
- else
- break;
- }
- tlk->releaseSegmentMonitors();
- tlk->setKey(NULL);
- }
- catch (...)
- {
- tlk->releaseSegmentMonitors();
- tlk->setKey(NULL);
- throw;
- }
- }
- }
- joinHandler->noteEndReceived(jg, 0);
- }
- else
- {
- joinHandler->noteEndReceived(joinHandler->createJoinGroup(row), 0);
- }
- }
- void processGroup(const ConstPointerArray &)
- {
- throwUnexpected();
- }
- virtual void processEOG()
- {
- joinHandler->processEOG();
- }
- virtual void processDone()
- {
- // called from puller thread
- remote.flush();
- remote.senddone();
- }
- virtual void onLimitExceeded(bool isKeyed)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- if (isKeyed)
- throwUnexpected();
- helper.onLimitExceeded();
- }
- virtual const void *createLimitFailRow(bool isKeyed)
- {
- throwUnexpected();
- }
- virtual bool fireException(IException *e)
- {
- // called from puller thread on failure
- remote.fireException(LINK(e));
- return joinHandler->fireException(e);
- }
- virtual const void *nextInGroup()
- {
- throwUnexpected(); // I am nobody's input
- }
- };
- class CRoxieServerKeyedJoinBase : public CRoxieServerActivity, implements IRecordPullerCallback, implements IRoxieServerErrorHandler, implements IJoinProcessor
- {
- protected:
- IHThorKeyedJoinArg &helper;
- KeyedJoinRemoteAdaptor remote;
- RecordPullerThread puller;
- OwnedConstRoxieRow defaultRight;
- Owned<IEngineRowAllocator> defaultRightAllocator;
- unsigned joinFlags;
- unsigned atMost;
- unsigned abortLimit;
- unsigned keepLimit;
- bool limitFail;
- bool limitOnFail;
- bool preserveGroups;
- bool cloneLeft;
- bool isSimple;
- bool isLocal;
- ThorActivityKind activityKind;
- CJoinGroup *groupStart;
- CriticalSection groupsCrit;
- QueueOf<CJoinGroup, false> groups;
- IRoxieInput *indexReadInput;
- IIndexReadActivityInfo *rootIndex;
- void createDefaultRight()
- {
- if (!defaultRight)
- {
- if (!defaultRightAllocator)
- defaultRightAllocator.setown(ctx->queryCodeContext()->getRowAllocator(helper.queryJoinFieldsRecordSize(), activityId));
- RtlDynamicRowBuilder rowBuilder(defaultRightAllocator);
- size32_t thisSize = helper.createDefaultRight(rowBuilder);
- defaultRight.setown(rowBuilder.finalizeRowClear(thisSize));
- }
- }
- public:
- CRoxieServerKeyedJoinBase(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, unsigned _joinFlags
- , bool isFull, bool _isSimple, bool _isLocal)
- : CRoxieServerActivity(_factory, _probeManager),
- helper((IHThorKeyedJoinArg &)basehelper),
- remote(_remoteId, helper, *this, isFull, _isSimple, puller, *this),
- joinFlags(_joinFlags),
- preserveGroups(meta.isGrouped()),
- puller(false),
- isSimple(_isSimple),
- isLocal(_isLocal),
- abortLimit(0),
- keepLimit(0),
- atMost(0),
- limitFail(false),
- limitOnFail(false),
- cloneLeft(false)
- {
- groupStart = NULL;
- activityKind = _factory->getKind();
- indexReadInput = NULL;
- rootIndex = NULL;
- // MORE - code would be easier to read if I got more values from helper rather than passing from factory
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerActivity::onCreate(_ctx, _colocalParent);
- remote.onCreate(this, this, _ctx, _colocalParent);
- }
- virtual void setInput(unsigned idx, IRoxieInput *_in)
- {
- if (!idx)
- puller.setInput(this, _in);
- else if (idx==1)
- indexReadInput = _in;
- else
- throw MakeStringException(ROXIE_SET_INPUT, "Internal error: setInput() parameter out of bounds at %s(%d)", __FILE__, __LINE__);
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- if (indexReadInput)
- {
- indexReadInput->start(parentExtractSize, parentExtract, true); // paused=true because we don't want to actually run the index read
- rootIndex = indexReadInput->queryIndexReadActivity();
- if (!rootIndex)
- throw MakeStringException(ROXIE_INTERNAL_ERROR,"Index in keyed join %d could not be resolved", queryId());
- }
- remote.onStart(parentExtractSize, parentExtract);
- remote.setLimits(helper.getRowLimit(), (unsigned __int64) -1, I64C(0x7FFFFFFFFFFFFFFF));
- atMost = helper.getJoinLimit();
- if (atMost == 0) atMost = (unsigned)-1;
- abortLimit = helper.getMatchAbortLimit();
- if (abortLimit == 0 || atMost != (unsigned) -1) abortLimit = (unsigned)-1;
- keepLimit = helper.getKeepLimit();
- if (keepLimit == 0) keepLimit = (unsigned)-1;
- getLimitType(joinFlags, limitFail, limitOnFail);
- cloneLeft = (joinFlags & JFtransformmatchesleft) != 0;
- if (joinFlags & JFleftouter)
- createDefaultRight();
- }
- virtual void stop(bool aborting)
- {
- puller.stop(aborting);
- if (indexReadInput)
- indexReadInput->stop(aborting);
- CRoxieServerActivity::stop(aborting);
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- __int64 localCycles = remote.totalCycles;
- localCycles -= puller.queryTotalCycles(); // MORE - debatable... but probably fair.
- if (localCycles < 0)
- localCycles = 0;
- return localCycles;
- }
- virtual IRoxieInput *queryInput(unsigned idx)
- {
- if (idx==0)
- return puller.queryInput();
- else if (idx==1)
- return indexReadInput;
- else
- return NULL;
- }
- virtual void reset()
- {
- totalCycles = remote.totalCycles;
- remote.totalCycles = 0;
- processed = remote.joinProcessed;
- remote.joinProcessed = 0;
- defaultRight.clear();
- if (indexReadInput)
- indexReadInput->reset();
- CRoxieServerActivity::reset();
- puller.reset();
- while (groups.ordinality())
- {
- ::Release(groups.dequeue());
- }
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- if (idx==(unsigned)-1)
- idx = 0;
- return idx ? NULL : &remote;
- }
- #undef new
- virtual CJoinGroup *createJoinGroup(const void *row)
- {
- // NOTE - we need to protect access to queue, since it's also modified by consumer thread. Groupstart is only modified by puller thread.
- CriticalBlock c(groupsCrit);
- if (preserveGroups && !groupStart)
- {
- groupStart = new (&ctx->queryRowManager(), activityId) CJoinGroup(NULL, NULL);
- groups.enqueue(groupStart);
- }
- CJoinGroup *jg = new (&ctx->queryRowManager(), activityId) CJoinGroup(row, groupStart);
- groups.enqueue(jg);
- return jg;
- }
- #if defined(_DEBUG) && defined(_WIN32) && !defined(USING_MPATROL)
- #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
- #endif
- void endGroup()
- {
- CriticalBlock c(groupsCrit);
- if (groupStart)
- noteEndReceived(groupStart, 0);
- groupStart = NULL;
- }
- virtual void noteEndReceived(CJoinGroup *jg, unsigned candidateCount)
- {
- if (jg->noteEndReceived(candidateCount))
- processCompletedGroups();
- }
- void processCompletedGroups()
- {
- loop
- {
- CriticalBlock c(groupsCrit);
- if (!groups.head()->complete())
- break;
- Owned<CJoinGroup> head = groups.dequeue();
- if (preserveGroups)
- {
- assert(head->isHeadRecord());
- assert(groups.head()->inGroup(head));
- unsigned joinGroupSize = 0;
- while (groups.ordinality() && groups.head()->inGroup(head))
- {
- Owned<CJoinGroup> finger = groups.dequeue();
- joinGroupSize += doJoinGroup(finger);
- }
- if (joinGroupSize)
- remote.addResult(NULL);
- }
- else
- doJoinGroup(head);
- if (!groups.ordinality())
- break;
- }
- }
- void failLimit(const void * left)
- {
- helper.onMatchAbortLimitExceeded();
- CommonXmlWriter xmlwrite(0);
- if (input && input->queryOutputMeta() && input->queryOutputMeta()->hasXML())
- {
- input->queryOutputMeta()->toXML((byte *) left, xmlwrite);
- }
- throw MakeStringException(ROXIE_JOIN_ERROR, "More than %d match candidates in keyed join %d for row %s", abortLimit, queryId(), xmlwrite.str());
- }
- virtual bool needsAllocator() const { return true; }
- unsigned doTransform(const void *left, const void *right, offset_t fpos_or_count, IException *except, const void **group)
- {
- if (cloneLeft && !except)
- {
- LinkRoxieRow(left);
- remote.addResult((void *) left);
- return 1;
- }
-
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- unsigned outSize;
- try
- {
- outSize = except ? helper.onFailTransform(rowBuilder, left, right, fpos_or_count, except) :
- (activityKind == TAKkeyeddenormalizegroup) ? helper.transform(rowBuilder, left, right, (unsigned) fpos_or_count, group) :
- helper.transform(rowBuilder, left, right, fpos_or_count);
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- if (outSize)
- {
- const void *shrunk = rowBuilder.finalizeRowClear(outSize);
- remote.addResult(shrunk);
- return 1;
- }
- else
- return 0;
- }
- unsigned doJoinGroup(CJoinGroup *jg)
- {
- unsigned matched = jg->rowsSeen();
- unsigned added = 0;
- const void *left = jg->queryLeft();
- if (jg->candidateCount() > abortLimit)
- {
- if (limitFail)
- failLimit(left);
- if (ctx->queryDebugContext())
- ctx->queryDebugContext()->checkBreakpoint(DebugStateLimit, NULL, static_cast<IActivityBase *>(this));
- if (limitOnFail)
- {
- Owned<IException> except;
- try
- {
- failLimit(left);
- }
- catch(IException * e)
- {
- except.setown(e);
- }
- added = doTransform(left, defaultRight, 0, except, NULL);
- }
- }
- else if (!matched || jg->candidateCount() > atMost)
- {
- switch (joinFlags & JFtypemask)
- {
- case JFleftouter:
- case JFleftonly:
- switch (activityKind)
- {
- case TAKkeyedjoin:
- case TAKkeyeddenormalizegroup:
- added = doTransform(left, defaultRight, 0, NULL, NULL);
- break;
- case TAKkeyeddenormalize:
- LinkRoxieRow(left);
- remote.addResult((void *) left);
- added++;
- break;
- }
- break;
- }
- }
- else if (!(joinFlags & JFexclude))
- {
- unsigned idx = 0;
- switch (activityKind)
- {
- case TAKkeyedjoin:
- while (idx < matched)
- {
- const KeyedJoinHeader *rhs = jg->queryRow(idx);
- added += doTransform(left, &rhs->rhsdata, rhs->fpos, NULL, NULL);
- if (added==keepLimit)
- break;
- idx++;
- }
- break;
- case TAKkeyeddenormalize:
- {
- OwnedConstRoxieRow newLeft;
- newLeft.set(left);
- unsigned rowSize = 0;
- unsigned rightAdded = 0;
- while (idx < matched)
- {
- const KeyedJoinHeader *rhs = jg->queryRow(idx);
- try
- {
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t transformedSize = helper.transform(rowBuilder, newLeft, &rhs->rhsdata, rhs->fpos, idx+1);
- if (transformedSize)
- {
- rowSize = transformedSize;
- newLeft.setown(rowBuilder.finalizeRowClear(rowSize));
- rightAdded++;
- if (rightAdded==keepLimit)
- break;
- }
- idx++;
- }
- catch (IException *E)
- {
- throw makeWrappedException(E);
- }
- }
- if (rowSize)
- {
- remote.addResult(newLeft.getClear());
- added++;
- }
- }
- break;
- case TAKkeyeddenormalizegroup:
- {
- ConstPointerArray extractedRows;
- while (idx < matched && idx < keepLimit)
- {
- const KeyedJoinHeader *rhs = jg->queryRow(idx);
- extractedRows.append((void *) &rhs->rhsdata);
- idx++;
- }
- added += doTransform(left, extractedRows.item(0), extractedRows.ordinality(), NULL, (const void * *)extractedRows.getArray());
- }
- break;
- }
- }
- return added;
- }
- virtual void processDone()
- {
- // called from puller thread
- remote.flush();
- remote.senddone();
- }
- virtual void onLimitExceeded(bool isKeyed)
- {
- if (traceLevel > 4)
- DBGLOG("activityid = %d isKeyed = %d line = %d", activityId, isKeyed, __LINE__);
- if (isKeyed)
- throwUnexpected();
- helper.onLimitExceeded();
- }
- virtual const void *createLimitFailRow(bool isKeyed)
- {
- throwUnexpected();
- }
- virtual bool fireException(IException *e)
- {
- // called from puller thread on failure
- return remote.fireException(e);
- }
- virtual const void *nextInGroup()
- {
- throwUnexpected(); // I am nobody's input
- }
- };
- #ifdef _MSC_VER
- #pragma warning ( push )
- #pragma warning ( disable: 4355 )
- #endif
- class CRoxieServerKeyedJoinActivity : public CRoxieServerKeyedJoinBase
- {
- CRoxieServerFullKeyedJoinHead head;
- Owned<IEngineRowAllocator> fetchInputAllocator;
- Linked<IFilePartMap> map;
- bool variableFetchFileName;
- Owned<const IResolvedFile> varFetchFileInfo;
- CachedOutputMetaData fetchInputFields;
- public:
- CRoxieServerKeyedJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_headId, IKeyArray * _key, TranslatorArray *_translators, IOutputMetaData *_indexReadMeta,
- const RemoteActivityId &_tailId, IFilePartMap *_map, unsigned _joinFlags, bool _isLocal)
- : CRoxieServerKeyedJoinBase(_factory, _probeManager, _tailId, _joinFlags, true, false, _isLocal),
- head(_factory, _probeManager, _headId, _key, _translators, _indexReadMeta, this, _isLocal),
- map(_map)
- {
- CRoxieServerKeyedJoinBase::setInput(0, head.queryOutput(0));
- variableFetchFileName = (helper.getFetchFlags() & (FFvarfilename|FFdynamicfilename)) != 0;
- }
-
- virtual const IResolvedFile *queryVarFileInfo() const
- {
- return varFetchFileInfo;
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerKeyedJoinBase::onCreate(_ctx, _colocalParent);
- head.onCreate(_ctx, _colocalParent);
- fetchInputFields.set(helper.queryFetchInputRecordSize());
- fetchInputAllocator.setown(ctx->queryCodeContext()->getRowAllocator(helper.queryFetchInputRecordSize(), activityId));
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerKeyedJoinBase::start(parentExtractSize, parentExtract, paused);
- if (variableFetchFileName)
- {
- varFetchFileInfo.setown(resolveLFN(helper.getFileName(), false));
- map.setown(varFetchFileInfo->getFileMap());
- }
- puller.start(parentExtractSize, parentExtract, paused, ctx->keyedJoinPreload(), false, ctx);
- }
- virtual void setInput(unsigned idx, IRoxieInput *in)
- {
- head.setInput(idx, in);
- }
- virtual void processRow(const void *_rhs)
- {
- // called from puller thread
- KeyedJoinHeader *rhs = (KeyedJoinHeader *) _rhs;
- CJoinGroup *jg = rhs->thisGroup;
- if (rhs->partNo != (unsigned short) -1)
- {
- unsigned partNo = map->mapOffset(rhs->fpos);
- RtlDynamicRowBuilder fetchBuilder(fetchInputAllocator, true);
- size32_t fisize = helper.extractFetchFields(fetchBuilder, jg->queryLeft());
- if (fetchInputFields.isVariableSize())
- {
- KeyedJoinHeader *outRow = (KeyedJoinHeader *) remote.getMem(partNo, 0, KEYEDJOIN_RECORD_SIZE(fisize + sizeof(fisize)));
- memcpy(outRow, rhs, KEYEDJOIN_RECORD_SIZE(0)); // MORE - copy constructor might be more appropriate....
- ReleaseRoxieRow(rhs);
- jg->notePending();
- memcpy(&outRow->rhsdata, &fisize, sizeof(fisize));
- memcpy((&outRow->rhsdata)+sizeof(fisize), fetchBuilder.row(), fisize);
- }
- else
- {
- KeyedJoinHeader *outRow = (KeyedJoinHeader *) remote.getMem(partNo, 0, KEYEDJOIN_RECORD_SIZE(fisize));
- memcpy(outRow, rhs, KEYEDJOIN_RECORD_SIZE(0)); // MORE - copy constructor might be more appropriate....
- ReleaseRoxieRow(rhs);
- jg->notePending();
- memcpy(&outRow->rhsdata, fetchBuilder.row(), fisize);
- }
- }
- else
- {
- unsigned candidateCount = (unsigned) rhs->fpos;
- // CTXLOG("Full keyed join - all results back from index");
- ReleaseRoxieRow(rhs);
- noteEndReceived(jg, candidateCount); // may throw exception - so release row before calling
- }
- }
- virtual void processEOG()
- {
- // called from front puller thread
- if (preserveGroups)
- endGroup();
- }
- void processGroup(const ConstPointerArray &)
- {
- throwUnexpected();
- }
- };
- #ifdef _MSC_VER
- #pragma warning ( pop )
- #endif
- class CRoxieServerHalfKeyedJoinActivity : public CRoxieServerKeyedJoinBase
- {
- IOutputMetaData *indexReadMeta;
- Owned<IEngineRowAllocator> indexReadAllocator;
- Owned<IKeyManager> tlk;
- bool variableIndexFileName;
- bool indexReadInputRecordVariable;
- Owned<const IResolvedFile> varFileInfo;
- Linked<TranslatorArray> translators;
- Linked<IKeyArray> keySet;
- Owned<IOutputMetaData> joinPrefixedMeta;
- Owned<IEngineRowAllocator> joinFieldsAllocator;
- public:
- CRoxieServerHalfKeyedJoinActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager, const RemoteActivityId &_remoteId, IKeyArray * _keySet, TranslatorArray *_translators,
- IOutputMetaData *_indexReadMeta, unsigned _joinFlags, bool _isSimple, bool _isLocal)
- : CRoxieServerKeyedJoinBase(_factory, _probeManager, _remoteId, _joinFlags, false, _isSimple, _isLocal),
- indexReadMeta(_indexReadMeta),
- tlk(createKeyManager(NULL, 0, this)),
- keySet(_keySet),
- translators(_translators)
- {
- variableIndexFileName = (helper.getJoinFlags() & (JFvarindexfilename|JFdynamicindexfilename)) != 0;
- indexReadInputRecordVariable = indexReadMeta->isVariableSize();
- }
- virtual void serializeExtra(MemoryBuffer &out)
- {
- if (helper.getJoinFlags() & JFindexfromactivity)
- {
- assertex(rootIndex);
- const RemoteActivityId& indexId = rootIndex->queryRemoteId();
- indexId.serialize(out);
- // could mess about reserving space for length then patching it again, to avoid copy, but probably not worth it
- MemoryBuffer tmp;
- rootIndex->queryActivity()->serializeCreateStartContext(tmp);
- if (rootIndex->queryActivity()->queryVarFileInfo())
- rootIndex->queryActivity()->queryVarFileInfo()->queryTimeStamp().serialize(tmp);
- unsigned ctxlen = tmp.length();
- out.append(ctxlen).append(tmp);
- }
- }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- CRoxieServerKeyedJoinBase::onCreate(_ctx, _colocalParent);
- indexReadAllocator.setown(ctx->queryCodeContext()->getRowAllocator(indexReadMeta, activityId));
- IOutputMetaData *joinFieldsMeta = helper.queryJoinFieldsRecordSize();
- joinPrefixedMeta.setown(new CPrefixedOutputMeta(KEYEDJOIN_RECORD_SIZE(0), joinFieldsMeta)); // MORE - not sure if we really need this
- joinFieldsAllocator.setown(ctx->queryCodeContext()->getRowAllocator(joinPrefixedMeta, activityId));
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- CRoxieServerKeyedJoinBase::start(parentExtractSize, parentExtract, paused);
- if (rootIndex)
- {
- varFileInfo.setown(rootIndex->getVarFileInfo());
- translators.setown(rootIndex->getTranslators());
- keySet.setown(rootIndex->getKeySet());
- }
- else if (variableIndexFileName)
- {
- varFileInfo.setown(resolveLFN(helper.getIndexFileName(), false));
- translators.setown(new TranslatorArray);
- keySet.setown(varFileInfo->getKeyArray(factory->queryActivityMeta(), translators, false, isLocal ? factory->queryQueryFactory().queryChannel() : 0, factory->queryQueryFactory().getEnableFieldTranslation()));
- }
- puller.start(parentExtractSize, parentExtract, paused, ctx->keyedJoinPreload(), isSimple, ctx);
- }
- virtual const IResolvedFile *queryVarFileInfo() const
- {
- return varFileInfo;
- }
- virtual void reset()
- {
- CRoxieServerKeyedJoinBase::reset();
- if (varFileInfo)
- {
- keySet.clear();
- varFileInfo.clear();
- }
- }
- virtual void processRow(const void *row)
- {
- // called from front puller thread
- // buffer up an IndexRead request
- if (helper.leftCanMatch(row) && keySet)
- {
- RtlDynamicRowBuilder extractBuilder(indexReadAllocator);
- unsigned indexReadRecordSize = helper.extractIndexReadFields(extractBuilder, row);
- OwnedConstRoxieRow extracted;
- if (indexReadRecordSize)
- extracted.setown(extractBuilder.finalizeRowClear(indexReadRecordSize));
- CJoinGroup *jg = createJoinGroup(row);
- for (unsigned partNo = 0; partNo < keySet->length(); partNo++)
- {
- IKeyIndexBase *thisBase = keySet->queryKeyPart(partNo);
- if (thisBase)
- {
- unsigned fileNo = 0;
- IKeyIndex *thisKey = thisBase->queryPart(fileNo);
- tlk->setKey(thisKey);
- tlk->setLayoutTranslator(translators->item(fileNo));
- helper.createSegmentMonitors(tlk, extracted);
- if (rootIndex)
- rootIndex->mergeSegmentMonitors(tlk);
- tlk->finishSegmentMonitors();
- try
- {
- tlk->reset();
- loop
- {
- typedef const void * cvp;
- if (thisKey && thisKey->isTopLevelKey())
- {
- bool locallySorted = (!thisKey->isFullySorted());
- while (locallySorted || tlk->lookup(false))
- {
- unsigned slavePart = locallySorted ? 0 : (unsigned) tlk->queryFpos();
- if (locallySorted || slavePart)
- {
- cvp *outputBuffer = (cvp *) remote.getMem(slavePart, fileNo, indexReadRecordSize + sizeof(cvp) + (indexReadInputRecordVariable ? sizeof(unsigned) : 0));
- *outputBuffer++ = jg;
- if (indexReadInputRecordVariable)
- {
- *(unsigned *) outputBuffer = indexReadRecordSize;
- outputBuffer = (cvp *) (((unsigned *) outputBuffer) + 1);
- }
- jg->notePending();
- memcpy(outputBuffer, extracted, indexReadRecordSize);
- if (locallySorted)
- {
- for (unsigned i = 1; i < numChannels; i++)
- jg->notePending();
- break;
- }
- }
- }
- }
- else
- {
- unsigned accepted = 0;
- unsigned rejected = 0;
- Owned<CRowArrayMessageResult> result;
- if (!isSimple)
- result.setown(new CRowArrayMessageResult(ctx->queryRowManager(), true));
- // MORE - This code seems to be duplicated in keyedJoinHead
- jg->notePending();
- unsigned candidateCount = 0;
- while (tlk->lookup(true))
- {
- candidateCount++;
- atomic_inc(&indexRecordsRead);
- KLBlobProviderAdapter adapter(tlk);
- offset_t recptr;
- const byte *indexRow = tlk->queryKeyBuffer(recptr);
- if (helper.indexReadMatch(extracted, indexRow, recptr, &adapter))
- {
- RtlDynamicRowBuilder rb(joinFieldsAllocator, true);
- CPrefixedRowBuilder pb(KEYEDJOIN_RECORD_SIZE(0), rb);
- accepted++;
- KLBlobProviderAdapter adapter(tlk);
- size32_t joinFieldsSize = helper.extractJoinFields(pb, indexRow, recptr, &adapter);
- KeyedJoinHeader *rec = (KeyedJoinHeader *) rb.getUnfinalizedClear(); // lack of finalize ok as unserialized data here.
- rec->fpos = recptr;
- rec->thisGroup = jg;
- rec->partNo = partNo;
- if (isSimple)
- remote.injected.enqueue(rec);
- else
- result->append(rec);
- }
- else
- {
- rejected++;
- atomic_inc(&postFiltered);
- }
- }
- // output an end marker for the matches to this group
- KeyedJoinHeader *rec = (KeyedJoinHeader *) ctx->queryRowManager().allocate(KEYEDJOIN_RECORD_SIZE(0), activityId);
- rec->fpos = (offset_t) candidateCount;
- rec->thisGroup = jg;
- rec->partNo = (unsigned short) -1;
- if (isSimple)
- remote.injected.enqueue(rec);
- else
- {
- result->append(rec);
- remote.injectResult(result.getClear());
- }
- if (accepted)
- noteStatistic(STATS_ACCEPTED, accepted, 1);
- if (rejected)
- noteStatistic(STATS_REJECTED, rejected, 1);
- }
- if (++fileNo < thisBase->numParts())
- {
- thisKey = thisBase->queryPart(fileNo);
- tlk->setKey(thisKey);
- tlk->setLayoutTranslator(translators->item(fileNo));
- tlk->reset();
- }
- else
- break;
- }
- tlk->releaseSegmentMonitors();
- tlk->setKey(NULL);
- }
- catch (...)
- {
- tlk->releaseSegmentMonitors();
- tlk->setKey(NULL);
- throw;
- }
- }
- }
- noteEndReceived(jg, 0);
- }
- else
- {
- noteEndReceived(createJoinGroup(row), 0);
- }
- }
- virtual void processEOG()
- {
- // called from front puller thread
- if (preserveGroups)
- endGroup();
- }
- void processGroup(const ConstPointerArray &)
- {
- throwUnexpected();
- }
- };
- class CRoxieServerKeyedJoinActivityFactory : public CRoxieServerMultiInputFactory
- {
- Owned<const IResolvedFile> indexfile;
- Owned<const IResolvedFile> datafile;
- Owned<IKeyArray> keySet;
- Owned<TranslatorArray> translatorArray;
- Owned<IDefRecordMeta> activityMeta;
- RemoteActivityId headId;
- RemoteActivityId tailId;
- IOutputMetaData *indexReadMeta;
- Owned<IFilePartMap> map;
- Owned<IFileIOArray> files;
- unsigned joinFlags;
- bool isHalfKeyed;
- bool isLocal;
- bool enableFieldTranslation;
- bool variableFetchFileName;
- bool variableIndexFileName;
- bool isSimple;
- public:
- CRoxieServerKeyedJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_headId, const RemoteActivityId &_tailId, IPropertyTree &_graphNode)
- : CRoxieServerMultiInputFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), headId(_headId), tailId(_tailId)
- {
- Owned<IHThorKeyedJoinArg> helper = (IHThorKeyedJoinArg *) helperFactory();
- isLocal = _graphNode.getPropBool("att[@name='local']/@value");
- isSimple = isLocal;
- rtlDataAttr indexLayoutMeta;
- size32_t indexLayoutSize;
- if(!helper->getIndexLayout(indexLayoutSize, indexLayoutMeta.refdata()))
- assertex(indexLayoutSize== 0);
- MemoryBuffer m;
- m.setBuffer(indexLayoutSize, indexLayoutMeta.getdata());
- activityMeta.setown(deserializeRecordMeta(m, true));
- enableFieldTranslation = queryFactory.getEnableFieldTranslation();
- translatorArray.setown(new TranslatorArray);
- joinFlags = helper->getJoinFlags();
- variableIndexFileName = (joinFlags & (JFvarindexfilename|JFdynamicindexfilename)) != 0;
- variableFetchFileName = (helper->getFetchFlags() & (FFvarfilename|FFdynamicfilename)) != 0;
- if (!variableIndexFileName)
- {
- bool isOpt = (joinFlags & JFindexoptional) != 0;
- indexfile.setown(queryFactory.queryPackage().lookupFileName(helper->getIndexFileName(), isOpt, true));
- if (indexfile)
- keySet.setown(indexfile->getKeyArray(activityMeta, translatorArray, isOpt, isLocal ? queryFactory.queryChannel() : 0, enableFieldTranslation));
- }
- if (keySet && keySet->length()==1 && !isSimple)
- {
- IKeyIndexBase *thisBase = keySet->queryKeyPart(0);
- if (thisBase->numParts()==1 && !thisBase->queryPart(0)->isTopLevelKey())
- isSimple = true;
- // MORE - if it's a variable filename then it MAY be simple, we don't know. Tough.
- }
- if (!simpleLocalKeyedJoins)
- isSimple = false;
- isHalfKeyed = !helper->diskAccessRequired();
- indexReadMeta = QUERYINTERFACE(helper->queryIndexReadInputRecordSize(), IOutputMetaData);
- if (!isHalfKeyed && !variableFetchFileName)
- {
- bool isFetchOpt = (helper->getFetchFlags() & FFdatafileoptional) != 0;
- datafile.setown(_queryFactory.queryPackage().lookupFileName(queryNodeFileName(_graphNode), isFetchOpt, true));
- if (datafile)
- {
- if (isLocal)
- files.setown(datafile->getIFileIOArray(isFetchOpt, queryFactory.queryChannel()));
- else
- map.setown(datafile->getFileMap());
- }
- }
- }
- virtual bool getEnableFieldTranslation() const
- {
- return enableFieldTranslation;
- }
- virtual IDefRecordMeta *queryActivityMeta() const
- {
- return activityMeta;
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- if (isHalfKeyed)
- return new CRoxieServerHalfKeyedJoinActivity(this, _probeManager,
- headId, keySet, translatorArray, indexReadMeta, joinFlags, isSimple, isLocal);
- else
- return new CRoxieServerKeyedJoinActivity(this, _probeManager,
- headId, keySet, translatorArray, indexReadMeta,
- tailId, map, joinFlags, isLocal);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerKeyedJoinActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, const RemoteActivityId &_remoteId, const RemoteActivityId &_remoteId2, IPropertyTree &_graphNode)
- {
- return new CRoxieServerKeyedJoinActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _remoteId, _remoteId2, _graphNode);
- }
- //=================================================================================
- class CRoxieServerSoapActivityBase : public CRoxieServerActivity, implements ISoapCallRowProvider, implements IRoxieAbortMonitor
- {
- protected:
- Owned<ISoapCallHelper> soaphelper;
- IHThorSoapActionArg & helper;
- StringBuffer authToken;
- bool eof;
- CriticalSection crit;
- ClientCertificate *pClientCert;
- public:
- IMPLEMENT_IINTERFACE;
- CRoxieServerSoapActivityBase(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerActivity(_factory, _probeManager), helper((IHThorSoapActionArg &)basehelper)
- {
- eof = false;
- if (clientCert.certificate.length() > 0 && clientCert.privateKey.length() > 0 && clientCert.passphrase.length() > 0)
- pClientCert = &clientCert;
- else
- pClientCert = NULL;
- }
- // ISoapCallRowProvider
- virtual IHThorSoapActionArg * queryActionHelper() { return &helper; };
- virtual IHThorSoapCallArg * queryCallHelper() { return NULL; };
- virtual const void * getNextRow() { return NULL; };
- virtual void releaseRow(const void * r) { ReleaseRoxieRow(r); };
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- eof = false;
- CRoxieServerActivity::start(parentExtractSize, parentExtract, paused);
- authToken.append(ctx->queryAuthToken());
- }
- virtual void reset()
- {
- // MORE - Shouldn't we make sure thread is stopped etc???
- soaphelper.clear();
- CRoxieServerActivity::reset();
- }
- // IRoxieAbortMonitor
- virtual void checkForAbort() { checkAbort(); }
- };
- //---------------------------------------------------------------------------
- class CRoxieServerSoapRowCallActivity : public CRoxieServerSoapActivityBase
- {
- IHThorSoapCallArg & callHelper;
- public:
- CRoxieServerSoapRowCallActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerSoapActivityBase(_factory, _probeManager), callHelper((IHThorSoapCallArg &)basehelper)
- {
- }
- virtual IHThorSoapCallArg * queryCallHelper()
- {
- return &callHelper;
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if(eof) return NULL;
- if (soaphelper == NULL)
- {
- soaphelper.setown(createSoapCallHelper(this, rowAllocator, authToken.str(), SCrow, pClientCert, *ctx, this));
- soaphelper->start();
- }
- OwnedConstRoxieRow ret = soaphelper->getRow();
- if (!ret)
- {
- eof = true;
- return NULL;
- }
- ++processed;
- return ret.getClear();
- }
- };
- class CRoxieServerSoapRowCallActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerSoapRowCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerSoapRowCallActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSoapRowCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerSoapRowCallActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //---------------------------------------------------------------------------
- class CRoxieServerSoapRowActionActivity : public CRoxieServerSoapActivityBase
- {
- public:
- CRoxieServerSoapRowActionActivity (const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerSoapActivityBase(_factory, _probeManager)
- {}
- virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
- {
- //MORE: parentExtract not passed to start - although shouldn't be a problem.
- soaphelper.setown(createSoapCallHelper(this, NULL, ctx->queryAuthToken(), SCrow, pClientCert, *ctx, this));
- soaphelper->start();
- soaphelper->waitUntilDone();
- IException *e = soaphelper->getError();
- soaphelper.clear();
- if (e)
- throw e;
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- return NULL;
- }
- virtual const void *nextInGroup()
- {
- throwUnexpected(); // I am nobody's input
- }
- };
- class CRoxieServerSoapRowActionActivityFactory : public CRoxieServerActivityFactory
- {
- bool isRoot;
- public:
- CRoxieServerSoapRowActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerSoapRowActionActivity(this, _probeManager);
- }
- virtual bool isSink() const
- {
- return isRoot;
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSoapRowActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- {
- return new CRoxieServerSoapRowActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
- }
- //---------------------------------------------------------------------------
- class CRoxieServerSoapDatasetCallActivity : public CRoxieServerSoapActivityBase
- {
- IHThorSoapCallArg & callHelper;
- public:
- CRoxieServerSoapDatasetCallActivity(const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerSoapActivityBase(_factory, _probeManager), callHelper((IHThorSoapCallArg &)basehelper)
- {
- }
- virtual IHThorSoapCallArg * queryCallHelper()
- {
- return &callHelper;
- }
- virtual const void *getNextRow()
- {
- CriticalBlock b(crit);
- const void *nextrec = input->nextInGroup();
- if (!nextrec)
- {
- nextrec = input->nextInGroup();
- }
- return nextrec;
- }
- virtual bool needsAllocator() const { return true; }
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- if(eof) return NULL;
- if (soaphelper == NULL)
- {
- soaphelper.setown(createSoapCallHelper(this, rowAllocator, authToken.str(), SCdataset, pClientCert, *ctx, this));
- soaphelper->start();
- }
- OwnedConstRoxieRow ret = soaphelper->getRow();
- if (!ret)
- {
- eof = true;
- return NULL;
- }
- ++processed;
- return ret.getClear();
- }
- };
- class CRoxieServerSoapDatasetCallActivityFactory : public CRoxieServerActivityFactory
- {
- public:
- CRoxieServerSoapDatasetCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerSoapDatasetCallActivity(this, _probeManager);
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSoapDatasetCallActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind)
- {
- return new CRoxieServerSoapDatasetCallActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind);
- }
- //---------------------------------------------------------------------------
- class CRoxieServerSoapDatasetActionActivity : public CRoxieServerSoapActivityBase
- {
- public:
- CRoxieServerSoapDatasetActionActivity (const IRoxieServerActivityFactory *_factory, IProbeManager *_probeManager)
- : CRoxieServerSoapActivityBase(_factory, _probeManager)
- {}
- virtual const void *getNextRow()
- {
- CriticalBlock b(crit);
- const void *nextrec = input->nextInGroup();
- if (!nextrec)
- {
- nextrec = input->nextInGroup();
- }
- if (nextrec)
- processed++;
- return nextrec;
- }
- virtual void execute(unsigned parentExtractSize, const byte * parentExtract)
- {
- try
- {
- start(parentExtractSize, parentExtract, false);
- soaphelper.setown(createSoapCallHelper(this, NULL, ctx->queryAuthToken(), SCdataset, pClientCert, *ctx, this));
- soaphelper->start();
- soaphelper->waitUntilDone();
- IException *e = soaphelper->getError();
- soaphelper.clear();
- if (e)
- throw e;
- stop(false);
- }
- catch (IException *E)
- {
- ctx->notifyAbort(E);
- stop(true);
- throw;
- }
- catch(...)
- {
- Owned<IException> E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught at %s:%d", __FILE__, __LINE__);
- ctx->notifyAbort(E);
- stop(true);
- throw;
- }
- }
- virtual IRoxieInput *queryOutput(unsigned idx)
- {
- return NULL;
- }
- virtual const void *nextInGroup()
- {
- throwUnexpected(); // I am nobody's input
- }
- };
- class CRoxieServerSoapDatasetActionActivityFactory : public CRoxieServerActivityFactory
- {
- bool isRoot;
- public:
- CRoxieServerSoapDatasetActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- : CRoxieServerActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind), isRoot(_isRoot)
- {
- }
- virtual IRoxieServerActivity *createActivity(IProbeManager *_probeManager) const
- {
- return new CRoxieServerSoapDatasetActionActivity(this, _probeManager);
- }
- virtual bool isSink() const
- {
- return isRoot;
- }
- };
- IRoxieServerActivityFactory *createRoxieServerSoapDatasetActionActivityFactory(unsigned _id, unsigned _subgraphId, IQueryFactory &_queryFactory, HelperFactory *_helperFactory, ThorActivityKind _kind, bool _isRoot)
- {
- return new CRoxieServerSoapDatasetActionActivityFactory(_id, _subgraphId, _queryFactory, _helperFactory, _kind, _isRoot);
- }
- //=====================================================================================================
- #ifdef TRACE_RESULTS
- IArrayOf<CResultRoxiePacket> badpackets;
- CriticalSection bpcrit;
- void notegoodpacket(CResultRoxiePacket *p)
- {
- CriticalBlock c(bpcrit);
- p->good = true;
- badpackets.append(*LINK(p));
- }
- void notebadpacket(CResultRoxiePacket *p)
- {
- CriticalBlock c(bpcrit);
- p->good = false;
- badpackets.append(*LINK(p));
- }
- void dumpresults()
- {
- CriticalBlock c(bpcrit);
- ForEachItemIn(idx, badpackets)
- {
- badpackets.item(idx).dump();
- }
- }
- #else
- #define notebadpacket(a)
- #define notegoodpacket(a)
- inline void dumpresults(){}
- #endif
- //======================================================================================================================
- // The Roxie server context is created per query. It is owned by a thread in the threadpool
- // We don't bother linking any object that is given to us by our owner, for efficiency.
- // I need to think about that though as it may impact the ability to bring channels up/down
- class TempStore : public CInterface, implements IInterface
- {
- public:
- IMPLEMENT_IINTERFACE;
- ConstPointerArray p;
- };
- //copied to eclagent, needs to be made common
- class InputProbe : public CInterface, implements IRoxieInput // base class for the edge probes used for tracing and debugging....
- {
- protected:
- IRoxieInput *in;
- unsigned sourceId;
- unsigned sourceIdx;
- unsigned targetId;
- unsigned targetIdx;
- unsigned iteration;
- unsigned channel;
- IOutputMetaData *inMeta;
- IDebuggableContext *debugContext;
- unsigned rowCount;
- unsigned totalRowCount;
- size32_t maxRowSize;
- bool everStarted;
- bool hasStarted;
- bool hasStopped;
-
- public:
- InputProbe(IRoxieInput *_in, IDebuggableContext *_debugContext,
- unsigned _sourceId, unsigned _sourceIdx, unsigned _targetId, unsigned _targetIdx, unsigned _iteration, unsigned _channel)
- : in(_in), debugContext(_debugContext),
- sourceId(_sourceId), sourceIdx(_sourceIdx), targetId(_targetId), targetIdx(_targetIdx), iteration(_iteration), channel(_channel)
- {
- hasStarted = false;
- everStarted = false;
- hasStopped = false;
- rowCount = 0;
- totalRowCount = 0;
- maxRowSize = 0;
- inMeta = NULL;
- }
- virtual IInputSteppingMeta * querySteppingMeta()
- {
- return in->querySteppingMeta();
- }
- virtual bool gatherConjunctions(ISteppedConjunctionCollector & collector)
- {
- return in->gatherConjunctions(collector);
- }
- virtual void resetEOF()
- {
- in->resetEOF();
- }
- virtual unsigned numConcreteOutputs() const
- {
- return in->numConcreteOutputs();
- }
- virtual IRoxieInput * queryConcreteInput(unsigned idx)
- {
- // MORE - not sure what is right here!
- if (in->queryConcreteInput(idx) == in)
- {
- assertex(idx==0);
- return this;
- }
- else
- return in->queryConcreteInput(idx);
- }
- virtual IRoxieServerActivity *queryActivity()
- {
- return in->queryActivity();
- }
- virtual IIndexReadActivityInfo *queryIndexReadActivity()
- {
- return in->queryIndexReadActivity();
- }
- virtual IOutputMetaData * queryOutputMeta() const
- {
- return in->queryOutputMeta();
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- // NOTE: totalRowCount/maxRowSize not reset, as we want them cumulative when working in a child query.
- rowCount = 0;
- hasStarted = true;
- hasStopped = false;
- everStarted = true;
- in->start(parentExtractSize, parentExtract, paused);
- inMeta = in->queryOutputMeta();
- assertex(inMeta);
- }
- virtual void stop(bool aborting)
- {
- hasStopped = true;
- in->stop(aborting);
- }
- virtual void reset()
- {
- hasStarted = false;
- in->reset();
- }
- virtual void checkAbort()
- {
- in->checkAbort();
- }
- virtual unsigned queryId() const
- {
- return in->queryId();
- }
- virtual unsigned __int64 queryTotalCycles() const
- {
- return in->queryTotalCycles();
- }
- virtual unsigned __int64 queryLocalCycles() const
- {
- return in->queryLocalCycles();
- }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- if (!idx)
- return in;
- else
- return NULL;
- }
- virtual const void *nextInGroup()
- {
- const void *ret = in->nextInGroup();
- if (ret)
- {
- size32_t size = inMeta->getRecordSize(ret);
- if (size > maxRowSize)
- maxRowSize = size;
- rowCount++;
- totalRowCount++;
- }
- return ret;
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- const void *ret = in->nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- if (ret && wasCompleteMatch) // GH is this test right?
- {
- size32_t size = inMeta->getRecordSize(ret);
- if (size > maxRowSize)
- maxRowSize = size;
- rowCount++;
- totalRowCount++;
- }
- return ret;
- }
- };
- class TraceProbe : public InputProbe
- {
- public:
- IMPLEMENT_IINTERFACE;
- TraceProbe(IRoxieInput *_in, unsigned _sourceId, unsigned _targetId, unsigned _sourceIdx, unsigned _targetIdx, unsigned _iteration, unsigned _channel)
- : InputProbe(_in, NULL, _sourceId, _sourceIdx, _targetId, _targetIdx, _iteration, _channel)
- {
- }
- bool matches(IPropertyTree &edge, bool forNode)
- {
- if (forNode)
- {
- unsigned id = edge.getPropInt("@id", 0);
- if (id && (id == sourceId || id == targetId))
- {
- return true;
- }
- }
- else
- {
- unsigned id = edge.getPropInt("@source", 0);
- if (id && id == sourceId)
- {
- id = edge.getPropInt("@target", 0);
- if (id && id == targetId)
- {
- unsigned idx = edge.getPropInt("att[@name=\"_sourceIndex\"]/@value", 0);
- if (idx == sourceIdx)
- return true;
- }
- }
- id = edge.getPropInt("att[@name=\"_sourceActivity\"]/@value");
- if (id && id == sourceId)
- {
- id = edge.getPropInt("att[@name=\"_targetActivity\"]/@value");
- if (id && id == targetId)
- {
- unsigned idx = edge.getPropInt("att[@name=\"_sourceIndex\"]/@value", 0);
- if (idx == sourceIdx)
- return true;
- }
- }
- }
- return false;
- }
- const void * _next(const void *inputRow)
- {
- const byte *ret = (const byte *) inputRow;
- if (ret && probeAllRows)
- {
- CommonXmlWriter xmlwrite(XWFnoindent|XWFtrim|XWFopt);
- if (inMeta && inMeta->hasXML())
- inMeta->toXML(ret, xmlwrite);
- DBGLOG("ROW: [%d->%d] {%p} %s", sourceId, targetId, ret, xmlwrite.str());
- }
- return ret;
- }
- virtual const void * nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- // MORE - should probably only note them when wasCompleteMatch is true?
- return _next(InputProbe::nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra));
- }
- virtual const void *nextInGroup()
- {
- return _next(InputProbe::nextInGroup());
- }
- void getNodeProgressInfo(IPropertyTree &node)
- {
- // node is the input (or possibly output) of this probe edge
- unsigned started = everStarted;
- putStatsValue(&node, "_roxieStarted", "sum", started);
- unsigned id = node.getPropInt("@id", 0);
- bool isOutput = (id != 0) && (id != sourceId);
- unsigned totalTime = (unsigned) (cycle_to_nanosec(in->queryTotalCycles())/1000);
- if (isOutput)
- totalTime += 10; // Fudge factor - I don't really know the times but this makes the graph more useable than not supplying a totalTime value
- if (totalTime)
- putStatsValue(&node, "totalTime", "sum", totalTime);
- unsigned localTime = isOutput ? 10 : (unsigned) (cycle_to_nanosec(in->queryLocalCycles())/1000); // Fudge factor - I don't really know the times but this makes the graph more useable than not supplying a localTime value
- if (localTime)
- putStatsValue(&node, "localTime", "sum", localTime);
- }
- void getEdgeProgressInfo(IPropertyTree &edge)
- {
- putStatsValue(&edge, "_roxieStarted", "sum", hasStarted);
- if (hasStarted)
- {
- putStatsValue(&edge, "count", "sum", totalRowCount);
- putStatsValue(&edge, "maxrowsize", "max", maxRowSize);
- }
- }
- };
- class CGraphResults : public CInterface, implements IRoxieGraphResults
- {
- IArrayOf<IGraphResult> results;
- CriticalSection cs;
- IGraphResult & select(unsigned idx)
- {
- CriticalBlock procedure(cs);
- if (idx >= results.ordinality())
- throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Error reading graph result %d before it is calculated", idx);
- return results.item(idx);
- }
- public:
- IMPLEMENT_IINTERFACE
- void clear()
- {
- CriticalBlock procedure(cs);
- results.kill();
- }
- IRoxieInput * createIterator(unsigned id)
- {
- return select(id).createIterator();
- }
- virtual void getResult(unsigned & lenResult, void * & result, unsigned id)
- {
- select(id).getResult(lenResult, result);
- }
- virtual void getLinkedResult(unsigned & count, byte * * & ret, unsigned id)
- {
- select(id).getLinkedResult(count, ret);
- }
- void setResult(unsigned id, IGraphResult * result)
- {
- CriticalBlock procedure(cs);
- if (results.ordinality() <= id)
- {
- while (results.ordinality() < id)
- results.append(*new CGraphResult);
- results.append(*LINK(result));
- }
- else
- results.replace(*LINK(result), id);
- }
- void appendResult(IGraphResult * result)
- {
- CriticalBlock procedure(cs);
- results.append(*LINK(result));
- }
- };
- class CProbeManager : public CInterface, implements IProbeManager
- {
- IArrayOf<TraceProbe> probes; // May want to replace with hash table at some point....
- public:
- IMPLEMENT_IINTERFACE;
- IInputBase *createProbe(IInputBase *in, IActivityBase *inAct, IActivityBase *outAct, unsigned sourceIdx, unsigned targetIdx, unsigned iteration)
- {
- unsigned idIn = inAct->queryId();
- unsigned idOut = outAct->queryId();
- TraceProbe *probe = new TraceProbe(static_cast<IRoxieInput*>(in), idIn, idOut, sourceIdx, targetIdx, iteration, 0);
- probes.append(*probe);
- return probe;
- }
- TraceProbe *findProbe(IPropertyTree &edge, bool forNode, unsigned &startat)
- {
- // MORE - this is n-squared on number of edges in the graph. Could get painful - recode if needed
- // However I think that the "startat" cache probably prevents the pain
- unsigned probeCount = probes.ordinality();
- unsigned search = probeCount;
- unsigned idx = startat;
- while (search--)
- {
- idx++;
- if (idx>=probeCount) idx = 0;
- TraceProbe &p = probes.item(idx);
- if (p.matches(edge, forNode))
- {
- startat = idx;
- return &p;
- }
- }
- return NULL;
- }
- virtual void noteSink(IActivityBase *)
- {
- }
- virtual IDebugGraphManager *queryDebugManager()
- {
- return NULL;
- }
- virtual void noteDependency(IActivityBase *sourceActivity, unsigned sourceIndex, unsigned controlId, const char *edgeId, IActivityBase *targetActivity)
- {
- }
- virtual IProbeManager *startChildGraph(unsigned childGraphId, IActivityBase *parent)
- {
- return LINK(this);
- }
- virtual void endChildGraph(IProbeManager *child, IActivityBase *parent)
- {
- }
- virtual void deleteGraph(IArrayOf<IActivityBase> *activities, IArrayOf<IInputBase> *goers)
- {
- if (goers)
- {
- ForEachItemIn(probeIdx, *goers)
- {
- TraceProbe &probe = (TraceProbe &) goers->item(probeIdx);
- probes.zap(probe);
- }
- }
- }
- virtual void setNodeProperty(IActivityBase *node, const char *propName, const char *propVvalue)
- {
- // MORE - we could note these in probe mode too...
- }
- virtual void setNodePropertyInt(IActivityBase *node, const char *propName, unsigned __int64 propVvalue)
- {
- // MORE - we could note these in probe mode too...
- }
- virtual void getProbeResponse(IPropertyTree *query)
- {
- Owned<IPropertyTreeIterator> graphs = query->getElements("Graph");
- ForEach(*graphs)
- {
- IPropertyTree &graph = graphs->query();
- Owned<IPropertyTreeIterator> subgraphs = graph.getElements("xgmml/graph");
- ForEach(*subgraphs)
- {
- IPropertyTree &subgraph = subgraphs->query();
- Owned<IPropertyTreeIterator> nodes = subgraph.getElements(".//node");
- unsigned startat = 0;
- ForEach(*nodes)
- {
- IPropertyTree &node = nodes->query();
- TraceProbe *currentProbe = findProbe(node, true, startat);
- if (currentProbe)
- {
- currentProbe->getNodeProgressInfo(node);
- }
- }
- Owned<IPropertyTreeIterator> edges = subgraph.getElements(".//edge");
- startat = 0;
- ForEach(*edges)
- {
- IPropertyTree &edge = edges->query();
- if (edge.getPropInt("att[@name='_dependsOn']/@value", 0) != 0)
- {
- const char *targetNode = edge.queryProp("att[@name='_targetActivity']/@value");
- if (targetNode)
- {
- StringBuffer xpath;
- IPropertyTree *target = query->queryPropTree(xpath.append(".//node[@id='").append(targetNode).append("']"));
- if (target)
- {
- unsigned started = target->getPropInt("att[@name='_roxieStarted']/@value", 0);
- IPropertyTree *att = edge.queryPropTree("att[@name=\"_roxieStarted\"]");
- if (!att)
- {
- att = edge.addPropTree("att", createPTree());
- att->setProp("@name", "_roxieStarted");
- }
- else
- started += att->getPropInt("@value");
- att->setPropInt("@value", started);
- }
- }
- }
- else
- {
- TraceProbe *currentProbe = findProbe(edge, false, startat);
- if (currentProbe)
- {
- currentProbe->getEdgeProgressInfo(edge);
- }
- else
- {
- const char *targetNode = edge.queryProp("att[@name='_targetActivity']/@value");
- if (targetNode)
- {
- StringBuffer xpath;
- IPropertyTree *target = query->queryPropTree(xpath.append(".//node[@id='").append(targetNode).append("']"));
- if (target)
- {
- unsigned started = target->getPropInt("att[@name='_roxieStarted']/@value", 0);
- IPropertyTree *att = edge.queryPropTree("att[@name=\"_roxieStarted\"]");
- if (!att)
- {
- att = edge.addPropTree("att", createPTree());
- att->setProp("@name", "_roxieStarted");
- }
- else
- started += att->getPropInt("@value");
- att->setPropInt("@value", started);
- }
- }
- }
- }
- }
- }
- }
- }
- };
- typedef const IInterface *CIptr;
- typedef MapBetween<unsigned, unsigned, CIptr, CIptr> ProxyMap;
- static ProxyMap *registeredProxies;
- static CriticalSection proxyLock;
- static memsize_t nextProxyId = 1;
- static memsize_t registerProxyId(const IInterface * object)
- {
- CriticalBlock b(proxyLock);
- if (!registeredProxies)
- registeredProxies = new ProxyMap;
- registeredProxies->setValue(nextProxyId, object);
- return nextProxyId++;
- }
- static void unregisterProxyId(memsize_t id)
- {
- // CriticalBlock b(proxyLock); done by caller
- if (registeredProxies)
- {
- registeredProxies->remove(id);
- if (!registeredProxies->count())
- {
- delete registeredProxies;
- registeredProxies = NULL;
- }
- }
- }
- static const IInterface *getProxy(memsize_t id)
- {
- CriticalBlock b(proxyLock);
- if (registeredProxies)
- {
- CIptr *proxy = registeredProxies->getValue(id);
- if (proxy)
- return LINK(*proxy);
- }
- return NULL;
- }
- //copied to eclagent, needs to be made common
- class DebugProbe : public InputProbe, implements IActivityDebugContext
- {
- Owned<IGlobalEdgeRecord> edgeRecord;
- ICopyArrayOf<IBreakpointInfo> breakpoints;
- HistoryRow *history;
- unsigned lastSequence;
- unsigned historySize;
- unsigned historyCapacity;
- unsigned nextHistorySlot;
- unsigned childGraphId;
-
- mutable memsize_t proxyId; // MORE - do we need a critsec to protect too?
- DebugActivityRecord *sourceAct;
- DebugActivityRecord *targetAct;
- StringAttr edgeId;
- bool forceEOF;
- bool EOGseen;
- bool EOGsent;
- static void putAttributeUInt(IXmlWriter *output, const char *name, unsigned value)
- {
- output->outputBeginNested("att", false);
- output->outputCString(name, "@name");
- output->outputInt(value, "@value");
- output->outputEndNested("att");
- }
- void rowToXML(IXmlWriter *output, const void *row, unsigned sequence, unsigned rowCount, bool skipped, bool limited, bool eof, bool eog) const
- {
- output->outputBeginNested("Row", true);
- output->outputInt(sequence, "@seq");
- if (skipped)
- output->outputBool(true, "@skip");
- if (limited)
- output->outputBool(true, "@limit");
- if (eof)
- output->outputBool(true, "@eof");
- if (eog)
- output->outputBool(true, "@eog");
- if (row)
- {
- output->outputInt(rowCount, "@count");
- IOutputMetaData *meta = queryOutputMeta();
- output->outputInt(meta->getRecordSize(row), "@size");
- meta->toXML((const byte *) row, *output);
- }
- output->outputEndNested("Row");
- }
- public:
- DebugProbe(IInputBase *_in, unsigned _sourceId, unsigned _sourceIdx, DebugActivityRecord *_sourceAct, unsigned _targetId, unsigned _targetIdx, DebugActivityRecord *_targetAct, unsigned _iteration, unsigned _channel, IDebuggableContext *_debugContext)
- : InputProbe(static_cast<IRoxieInput*>(_in), _debugContext, _sourceId, _sourceIdx, _targetId, _targetIdx, _iteration, _channel),
- sourceAct(_sourceAct), targetAct(_targetAct)
- {
- historyCapacity = debugContext->getDefaultHistoryCapacity();
- nextHistorySlot = 0;
- if (historyCapacity)
- history = new HistoryRow [historyCapacity];
- else
- history = NULL;
- historySize = 0;
- lastSequence = 0;
- StringBuffer idText;
- idText.appendf("%d_%d", sourceId, sourceIdx);
- edgeRecord.setown(debugContext->getEdgeRecord(idText));
- if (iteration || channel)
- idText.appendf(".%d", iteration);
- if (channel)
- idText.appendf("#%d", channel);
- edgeId.set(idText);
- debugContext->checkDelayedBreakpoints(this);
- forceEOF = false;
- EOGseen = false;
- EOGsent = false;
- proxyId = 0;
- }
- ~DebugProbe()
- {
- if (history)
- {
- for (unsigned idx = 0; idx < historyCapacity; idx++)
- ReleaseRoxieRow(history[idx].row);
- delete [] history;
- }
- ForEachItemIn(bpIdx, breakpoints)
- {
- breakpoints.item(bpIdx).removeEdge(*this);
- }
- }
- virtual void Link() const
- {
- CInterface::Link();
- }
- virtual bool Release() const
- {
- CriticalBlock b(proxyLock);
- if (!IsShared())
- {
- if (proxyId)
- unregisterProxyId(proxyId);
- }
- return CInterface::Release();
- }
- virtual memsize_t queryProxyId() const
- {
- if (!proxyId)
- proxyId = registerProxyId((const IActivityDebugContext *) this);
- return proxyId;
- }
- virtual unsigned queryChildGraphId() const
- {
- return childGraphId;
- }
- virtual void resetEOF()
- {
- forceEOF = false;
- EOGseen = false;
- EOGsent = false;
- InputProbe::resetEOF();
- }
- #if 0
- virtual unsigned queryId() const
- {
- return sourceId;
- }
- #endif
- virtual const char *queryEdgeId() const
- {
- return edgeId.get();
- }
- virtual const char *querySourceId() const
- {
- UNIMPLEMENTED;
- }
- virtual void printEdge(IXmlWriter *output, unsigned startRow, unsigned numRows) const
- {
- output->outputBeginNested("edge", true);
- output->outputString(edgeId.length(), edgeId.get(), "@edgeId");
- if (startRow < historySize)
- {
- if (numRows > historySize - startRow)
- numRows = historySize - startRow;
- while (numRows)
- {
- IHistoryRow *rowData = queryHistoryRow(startRow+numRows-1);
- assertex(rowData);
- rowToXML(output, rowData->queryRow(), rowData->querySequence(), rowData->queryRowCount(), rowData->wasSkipped(), rowData->wasLimited(), rowData->wasEof(), rowData->wasEog());
- numRows--;
- }
- }
- output->outputEndNested("edge");
- }
- virtual void searchHistories(IXmlWriter *output, IRowMatcher *matcher, bool fullRows)
- {
- IOutputMetaData *meta = queryOutputMeta();
- bool anyMatchedYet = false;
- if (matcher->canMatchAny(meta))
- {
- for (unsigned i = 0; i < historySize; i++)
- {
- IHistoryRow *rowData = queryHistoryRow(i);
- assertex(rowData);
- const void *row = rowData->queryRow();
- if (row)
- {
- matcher->reset();
- meta->toXML((const byte *) rowData->queryRow(), *matcher);
- if (matcher->matched())
- {
- if (!anyMatchedYet)
- {
- output->outputBeginNested("edge", true);
- output->outputString(edgeId.length(), edgeId.get(), "@edgeId");
- anyMatchedYet = true;
- }
- if (fullRows)
- rowToXML(output, rowData->queryRow(), rowData->querySequence(), rowData->queryRowCount(), rowData->wasSkipped(), rowData->wasLimited(), rowData->wasEof(), rowData->wasEog());
- else
- {
- output->outputBeginNested("Row", true);
- output->outputInt(rowData->querySequence(), "@sequence");
- output->outputInt(rowData->queryRowCount(), "@count");
- output->outputEndNested("Row");
- }
- }
- }
- }
- if (anyMatchedYet)
- output->outputEndNested("edge");
- }
- }
- virtual void getXGMML(IXmlWriter *output) const
- {
- output->outputBeginNested("edge", false);
- sourceAct->outputId(output, "@source");
- targetAct->outputId(output, "@target");
- output->outputString(edgeId.length(), edgeId.get(), "@id");
- if (sourceIdx)
- putAttributeUInt(output, "_sourceIndex", sourceIdx);
- putAttributeUInt(output, "count", rowCount); //changed from totalRowCount
- putAttributeUInt(output, "maxRowSize", maxRowSize);
- putAttributeUInt(output, "_roxieStarted", everStarted);
- putAttributeUInt(output, "_started", hasStarted);
- putAttributeUInt(output, "_stopped", hasStopped);
- putAttributeUInt(output, "_eofSeen", forceEOF);
- if (breakpoints.ordinality())
- putAttributeUInt(output, "_breakpoints", breakpoints.ordinality());
- output->outputEndNested("edge");
- }
- virtual IOutputMetaData *queryOutputMeta() const
- {
- return InputProbe::queryOutputMeta();
- }
- virtual IActivityDebugContext *queryInputActivity() const
- {
- IRoxieInput *x = in;
- while (x && QUERYINTERFACE(x->queryConcreteInput(0), IActivityDebugContext)==NULL)
- x = x->queryConcreteInput(0)->queryInput(0);
- return x ? QUERYINTERFACE(x->queryConcreteInput(0), IActivityDebugContext) : NULL;
- }
- // NOTE - these functions are threadsafe because only called when query locked by debugger.
- // Even though this thread may not yet be blocked on the debugger's critsec, because all manipulation (including setting history rows) is from
- // within debugger it is ok.
- virtual unsigned queryHistorySize() const
- {
- return historySize;
- }
- virtual IHistoryRow *queryHistoryRow(unsigned idx) const
- {
- assertex(idx < historySize);
- int slotNo = nextHistorySlot - idx - 1;
- if (slotNo < 0)
- slotNo += historyCapacity;
- return &history[slotNo];
- }
- virtual unsigned queryHistoryCapacity() const
- {
- return historyCapacity;
- }
- virtual unsigned queryLastSequence() const
- {
- return lastSequence;
- }
- virtual IBreakpointInfo *debuggerCallback(unsigned sequence, const void *row)
- {
- // First put the row into the history buffer...
- lastSequence = sequence;
- if (historyCapacity)
- {
- ReleaseClearRoxieRow(history[nextHistorySlot].row);
- if (row) LinkRoxieRow(row);
- history[nextHistorySlot].sequence = sequence; // MORE - timing might be interesting too, but would need to exclude debug wait time somehow...
- history[nextHistorySlot].row = row;
- history[nextHistorySlot].rowCount = rowCount;
- if (!row)
- {
- if (forceEOF)
- history[nextHistorySlot].setEof();
- else
- history[nextHistorySlot].setEog();
- }
- if (historySize < historyCapacity)
- historySize++;
- nextHistorySlot++;
- if (nextHistorySlot==historyCapacity)
- nextHistorySlot = 0;
- }
- // Now check breakpoints...
- ForEachItemIn(idx, breakpoints)
- {
- IBreakpointInfo &bp = breakpoints.item(idx);
- if (bp.matches(row, forceEOF, rowCount, queryOutputMeta())) // should optimize to only call queryOutputMeta once - but not that common to have multiple breakpoints
- return &bp;
- }
- return NULL;
- }
- virtual void setHistoryCapacity(unsigned newCapacity)
- {
- if (newCapacity != historyCapacity)
- {
- HistoryRow *newHistory;
- if (newCapacity)
- {
- unsigned copyCount = historySize;
- if (copyCount > newCapacity)
- copyCount = newCapacity;
- newHistory = new HistoryRow [newCapacity];
- unsigned slot = 0;
- while (copyCount--)
- {
- IHistoryRow *oldrow = queryHistoryRow(copyCount);
- newHistory[slot].sequence = oldrow->querySequence();
- newHistory[slot].row = oldrow->queryRow();
- newHistory[slot].rowCount = oldrow->queryRowCount();
- if (newHistory[slot].row)
- LinkRoxieRow(newHistory[slot].row);
- slot++;
- }
- historySize = slot;
- nextHistorySlot = slot;
- if (nextHistorySlot==historyCapacity)
- nextHistorySlot = 0;
- }
- else
- {
- newHistory = NULL;
- historySize = 0;
- nextHistorySlot = 0;
- }
- for (unsigned idx = 0; idx < historyCapacity; idx++)
- ReleaseRoxieRow(history[idx].row);
- delete [] history;
- history = newHistory;
- historyCapacity = newCapacity;
- }
- }
- virtual void clearHistory()
- {
- for (unsigned idx = 0; idx < historyCapacity; idx++)
- ReleaseClearRoxieRow(history[idx].row);
- historySize = 0;
- nextHistorySlot = 0;
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- forceEOF = false;
- EOGseen = false;
- EOGsent = false;
- if (!hasStarted)
- {
- lastSequence = debugContext->querySequence();
- edgeRecord->incrementCount(0, lastSequence);
- }
- InputProbe::start(parentExtractSize, parentExtract, paused);
- }
- virtual void reset()
- {
- InputProbe::reset();
- sourceAct->updateTimes(debugContext->querySequence());
- targetAct->updateTimes(debugContext->querySequence());
- }
- virtual void stop(bool aborting)
- {
- InputProbe::stop(aborting);
- sourceAct->updateTimes(debugContext->querySequence());
- targetAct->updateTimes(debugContext->querySequence());
- }
- virtual const void *nextInGroup()
- {
- // Code is a little complex to avoid interpreting a skip on all rows in a group as EOF
- try
- {
- if (forceEOF)
- return NULL;
- loop
- {
- const void *ret = InputProbe::nextInGroup();
- if (!ret)
- {
- if (EOGseen)
- forceEOF = true;
- else
- EOGseen = true;
- }
- else
- EOGseen = false;
- if (ret)
- edgeRecord->incrementCount(1, debugContext->querySequence());
- BreakpointActionMode action = debugContext->checkBreakpoint(DebugStateEdge, this, ret);
- if (action == BreakpointActionSkip && !forceEOF)
- {
- if (historyCapacity)
- queryHistoryRow(0)->setSkipped();
- if (ret)
- {
- edgeRecord->incrementCount(-1, debugContext->querySequence());
- ReleaseClearRoxieRow(ret);
- rowCount--;
- }
- continue;
- }
- else if (action == BreakpointActionLimit)
- {
- // This return value implies that we should not return the current row NOR should we return any more...
- forceEOF = true;
- if (ret)
- edgeRecord->incrementCount(-1, debugContext->querySequence());
- ReleaseClearRoxieRow(ret);
- if (historyCapacity)
- queryHistoryRow(0)->setLimited();
- rowCount--;
- }
- if (forceEOF || ret || !EOGsent)
- {
- EOGsent = (ret == NULL);
- sourceAct->updateTimes(debugContext->querySequence());
- targetAct->updateTimes(debugContext->querySequence());
- return ret;
- }
- }
- }
- catch (IException *E)
- {
- debugContext->checkBreakpoint(DebugStateException, this, E);
- throw;
- }
- }
- virtual const void *nextSteppedGE(const void * seek, unsigned numFields, bool &wasCompleteMatch, const SmartStepExtra & stepExtra)
- {
- // MORE - not sure that skip is safe here? Should the incomplete matches even be returned?
- // Code is a little complex to avoid interpreting a skip on all rows in a group as EOF
- // MORE - should probably only note them when wasCompleteMatch is true?
- try
- {
- if (forceEOF)
- return NULL;
- loop
- {
- const void *ret = InputProbe::nextSteppedGE(seek, numFields, wasCompleteMatch, stepExtra);
- if (!ret)
- {
- if (EOGseen)
- forceEOF = true;
- else
- EOGseen = true;
- }
- else
- EOGseen = false;
- if (ret)
- edgeRecord->incrementCount(1, debugContext->querySequence());
- BreakpointActionMode action = debugContext->checkBreakpoint(DebugStateEdge, this, ret);
- if (action == BreakpointActionSkip && !forceEOF)
- {
- if (ret)
- edgeRecord->incrementCount(-1, debugContext->querySequence());
- ReleaseClearRoxieRow(ret);
- if (historyCapacity)
- queryHistoryRow(0)->setSkipped();
- rowCount--;
- continue;
- }
- else if (action == BreakpointActionLimit)
- {
- // This return value implies that we should not return the current row NOR should we return any more...
- forceEOF = true;
- if (ret)
- edgeRecord->incrementCount(-1, debugContext->querySequence());
- ReleaseClearRoxieRow(ret);
- if (historyCapacity)
- queryHistoryRow(0)->setLimited();
- rowCount--;
- }
- if (forceEOF || ret || !EOGsent)
- {
- EOGsent = (ret == NULL);
- sourceAct->updateTimes(debugContext->querySequence());
- targetAct->updateTimes(debugContext->querySequence());
- return ret;
- }
- }
- }
- catch (IException *E)
- {
- debugContext->checkBreakpoint(DebugStateException, this, E);
- throw;
- }
- }
- virtual void setBreakpoint(IBreakpointInfo &bp)
- {
- if (bp.canMatchAny(queryOutputMeta()))
- {
- breakpoints.append(bp);
- bp.noteEdge(*this);
- }
- }
- virtual void removeBreakpoint(IBreakpointInfo &bp)
- {
- breakpoints.zap(bp);
- bp.removeEdge(*this);
- }
- };
- extern IProbeManager *createProbeManager()
- {
- return new CProbeManager;
- }
- IDebugGraphManager *createProxyDebugGraphManager(unsigned graphId, unsigned channel, memsize_t remoteGraphId);
- class CRoxieDebugGraphManager : extends CBaseDebugGraphManager
- {
- unsigned subId;
- public:
- CRoxieDebugGraphManager(IDebuggableContext *_debugContext, unsigned _id, const char *_graphName, unsigned _subId)
- : CBaseDebugGraphManager(_debugContext, _id, _graphName), subId(_subId)
- {
- }
- bool Release() const
- {
- CriticalBlock b(proxyLock);
- if (!IsShared())
- {
- if (!id)
- debugContext->releaseManager(const_cast<CRoxieDebugGraphManager*> (this));
- if (proxyId)
- unregisterProxyId(proxyId);
- }
- return CInterface::Release();
- }
- virtual IInputBase *createProbe(IInputBase *in, IActivityBase *sourceAct, IActivityBase *targetAct, unsigned sourceIdx, unsigned targetIdx, unsigned iteration)
- {
- CriticalBlock b(crit);
- if (!iteration)
- iteration = subId;
- unsigned channel = debugContext->queryChannel();
- unsigned sourceId = sourceAct->queryId();
- unsigned targetId = targetAct->queryId();
- DebugActivityRecord *sourceActRecord = noteActivity(sourceAct, iteration, channel, debugContext->querySequence());
- DebugActivityRecord *targetActRecord = noteActivity(targetAct, iteration, channel, debugContext->querySequence());
- DebugProbe *probe = new DebugProbe(in, sourceId, sourceIdx, sourceActRecord, targetId, targetIdx, targetActRecord, iteration, channel, debugContext);
- #ifdef _DEBUG
- DBGLOG("Creating probe for edge id %s in graphManager %p", probe->queryEdgeId(), this);
- #endif
- assertex(!allProbes.getValue(probe->queryEdgeId()));
- allProbes.setValue(probe->queryEdgeId(), (IActivityDebugContext *) probe);
- probe->Release(); // the allProbes map will have linked, and is enough to ensure lifespan...
- return probe;
- }
- virtual memsize_t queryProxyId() const
- {
- if (!proxyId)
- proxyId = registerProxyId((const IDebugGraphManager *) this);
- return proxyId;
- }
- virtual void deserializeProxyGraphs(DebugState state, MemoryBuffer &buff, IActivityBase *parentActivity, unsigned channel)
- {
- Linked<DebugActivityRecord> parentNode = allActivities.getValue(parentActivity);
- assertex(parentNode != NULL);
- unsigned numChildren;
- buff.read(numChildren);
- while (numChildren--)
- {
- unsigned remoteId;
- memsize_t proxyId;
- buff.read(remoteId);
- __uint64 tmp;
- buff.read(tmp);
- proxyId = (memsize_t)tmp; // can't serialize memsize_t
- bool found = false;
- ForEachItemIn(idx, parentNode->childGraphs)
- {
- IDebugGraphManager &child = parentNode->childGraphs.item(idx);
- if (child.queryProxyId() == proxyId)
- {
- found = true;
- if (state == DebugStateGraphFinished)
- {
- parentNode->childGraphs.remove(idx);
- debugContext->noteGraphChanged();
- }
- break;
- }
- }
- if (!found && state != DebugStateGraphFinished)
- {
- IDebugGraphManager *proxy = createProxyDebugGraphManager(remoteId, channel, proxyId);
- childGraphs.append(*LINK(proxy));
- parentNode->childGraphs.append(*proxy);
- debugContext->noteGraphChanged();
- }
- }
- }
- virtual IProbeManager *startChildGraph(unsigned childGraphId, IActivityBase *parent)
- {
- CriticalBlock b(crit);
- if (childGraphId || parent)
- {
- CRoxieDebugGraphManager *childManager = new CRoxieDebugGraphManager(debugContext, childGraphId, NULL, parent ? parent->queryId() : 0);
- IDebugGraphManager *graph = childManager;
- childGraphs.append(*LINK(graph));
- debugContext->noteGraphChanged();
- return childManager;
- }
- else
- return LINK(this);
- }
- virtual void deleteGraph(IArrayOf<IActivityBase> *activities, IArrayOf<IInputBase> *probes)
- {
- CriticalBlock b(crit);
- if (activities)
- {
- ForEachItemIn(idx, *activities)
- {
- IActivityBase &activity = activities->item(idx);
- if (activity.isSink())
- sinks.zap(activity);
- Linked<DebugActivityRecord> node = allActivities.getValue(&activity);
- if (node)
- allActivities.remove(&activity);
- }
- }
- if (probes)
- {
- IArrayOf<IRoxieInput>* fprobes = (IArrayOf<IRoxieInput>*)(probes);
- ForEachItemIn(probeIdx, *fprobes)
- {
- DebugProbe &probe = (DebugProbe &) fprobes->item(probeIdx);
- #ifdef _DEBUG
- DBGLOG("removing probe for edge id %s in graphManager %p", probe.queryEdgeId(), this);
- #endif
- allProbes.remove(probe.queryEdgeId());
- }
- }
- debugContext->noteGraphChanged();
- }
- };
- extern IProbeManager *createDebugManager(IDebuggableContext *debugContext, const char *graphName)
- {
- return new CRoxieDebugGraphManager(debugContext, 0, graphName, 0);
- }
- enum DebugRequestType
- {
- DEBUGREQUEST_OUTPUTCHILDGRAPH,
- DEBUGREQUEST_OUTPUTLINKSFORCHILDGRAPH,
- DEBUGREQUEST_LOOKUPACTIVITYBYEDGEID,
- DEBUGREQUEST_PRINTEDGE,
- DEBUGREQUEST_SETBREAKPOINT,
- DEBUGREQUEST_SEARCHHISTORIES,
- DEBUGREQUEST_GETRESETGLOBALCOUNTS
- };
- struct DebugRequestBase : public CInterface, implements IInterface
- {
- protected:
- DebugRequestType function;
- memsize_t proxyId; // MORE - at some point should really make this an int instead - but need to look into whether ever used to represent a pointer
- public:
- IMPLEMENT_IINTERFACE;
- DebugRequestBase(DebugRequestType _function, memsize_t _proxyId) : function(_function), proxyId(_proxyId) {}
- DebugRequestBase(MemoryBuffer &serialized)
- {
- byte fval;
- serialized.read(fval); function = (DebugRequestType) fval;
- unsigned __int64 tmp;
- serialized.read(tmp); // can't serilalize memsize_t
- proxyId = (memsize_t) tmp;
- }
- virtual void serialize(MemoryBuffer &buf)
- {
- buf.append((byte) function);
- buf.append((unsigned __int64) proxyId); // can't serialize memsize_t
- }
- virtual void executeRequest(IXmlWriter *output) = 0;
- inline IDebugGraphManager *getManager()
- {
- return (IDebugGraphManager *) getProxy(proxyId);
- }
- inline IActivityDebugContext *getActivity()
- {
- return (IActivityDebugContext *) getProxy(proxyId);
- }
- void inactive(IXmlWriter *output)
- {
- // MORE - what should I do here?
- }
- };
- struct DebugRequestOutputChildGraph : public DebugRequestBase
- {
- private:
- unsigned sequence;
- public:
- DebugRequestOutputChildGraph(memsize_t _proxyId, unsigned _sequence) : DebugRequestBase(DEBUGREQUEST_OUTPUTCHILDGRAPH, _proxyId), sequence(_sequence)
- {
- }
- DebugRequestOutputChildGraph(MemoryBuffer &serialized) : DebugRequestBase(serialized)
- {
- serialized.read(sequence);
- }
- virtual void serialize(MemoryBuffer &buf)
- {
- DebugRequestBase::serialize(buf);
- buf.append(sequence);
- }
- virtual void executeRequest(IXmlWriter *output)
- {
- Owned<IDebugGraphManager> manager = getManager();
- if (manager)
- manager->outputChildGraph(output, sequence);
- else
- inactive(output);
- }
- };
- struct DebugRequestWithId : public DebugRequestBase
- {
- StringAttr id;
- public:
- DebugRequestWithId(DebugRequestType _function, memsize_t _proxyId, const char *_id) : DebugRequestBase(_function, _proxyId), id(_id)
- {
- }
- DebugRequestWithId(MemoryBuffer &serialized) : DebugRequestBase(serialized)
- {
- serialized.read(id);
- }
- virtual void serialize(MemoryBuffer &buf)
- {
- DebugRequestBase::serialize(buf);
- buf.append(id);
- }
- };
- struct DebugRequestOutputLinksForChildGraph : public DebugRequestWithId
- {
- public:
- DebugRequestOutputLinksForChildGraph(memsize_t _proxyId, const char *_id) : DebugRequestWithId(DEBUGREQUEST_OUTPUTLINKSFORCHILDGRAPH, _proxyId, _id) {}
- DebugRequestOutputLinksForChildGraph(MemoryBuffer &serialized) : DebugRequestWithId(serialized)
- {
- }
- virtual void executeRequest(IXmlWriter *output)
- {
- Owned<IDebugGraphManager> manager = getManager();
- if (manager)
- manager->outputLinksForChildGraph(output, id);
- else
- inactive(output);
- }
- };
- struct DebugRequestLookupActivityByEdgeId : public DebugRequestWithId
- {
- public:
- DebugRequestLookupActivityByEdgeId(memsize_t _proxyId, const char *_id) : DebugRequestWithId(DEBUGREQUEST_LOOKUPACTIVITYBYEDGEID, _proxyId, _id) {}
- DebugRequestLookupActivityByEdgeId(MemoryBuffer &serialized) : DebugRequestWithId(serialized)
- {
- }
- virtual void executeRequest(IXmlWriter *output)
- {
- Owned<IDebugGraphManager> manager = getManager();
- if (manager)
- {
- output->outputBeginNested("Result", true);
- IActivityDebugContext *edge = manager->lookupActivityByEdgeId(id);
- if (edge)
- output->outputInt(edge->queryProxyId(), "@proxyId");
- output->outputEndNested("Result");
- }
- else
- inactive(output);
- }
- };
- struct DebugRequestPrintEdge : public DebugRequestBase
- {
- private:
- unsigned startRow;
- unsigned numRows;
- public:
- DebugRequestPrintEdge(memsize_t _proxyId, unsigned _startRow, unsigned _numRows)
- : DebugRequestBase(DEBUGREQUEST_PRINTEDGE, _proxyId), startRow(_startRow), numRows(_numRows)
- {
- }
- DebugRequestPrintEdge(MemoryBuffer &serialized) : DebugRequestBase(serialized)
- {
- serialized.read(startRow);
- serialized.read(numRows);
- }
- virtual void serialize(MemoryBuffer &buf)
- {
- DebugRequestBase::serialize(buf);
- buf.append(startRow);
- buf.append(numRows);
- }
- virtual void executeRequest(IXmlWriter *output)
- {
- Owned<IActivityDebugContext> activity = getActivity();
- if (activity)
- activity->printEdge(output, startRow, numRows);
- else
- inactive(output);
- }
- };
- struct DebugRequestSetRemoveBreakpoint : public DebugRequestBase
- {
- private:
- Linked<IBreakpointInfo> bp;
- bool isRemove;
- public:
- inline DebugRequestSetRemoveBreakpoint(memsize_t _proxyId, IBreakpointInfo &_bp, bool _isRemove)
- : DebugRequestBase(DEBUGREQUEST_SETBREAKPOINT, _proxyId), bp(&_bp), isRemove(_isRemove)
- {
- }
- DebugRequestSetRemoveBreakpoint(MemoryBuffer &serialized) : DebugRequestBase(serialized)
- {
- bp.setown(new CBreakpointInfo(serialized));
- serialized.read(isRemove);
- }
- virtual void serialize(MemoryBuffer &buf)
- {
- DebugRequestBase::serialize(buf);
- bp->serialize(buf);
- buf.append(isRemove);
- }
- virtual void executeRequest(IXmlWriter *output)
- {
- Owned<IDebugGraphManager> manager = getManager();
- if (manager)
- {
- if (isRemove)
- manager->queryContext()->removeBreakpoint(*bp);
- else
- manager->queryContext()->addBreakpoint(*bp.getLink());
- }
- else
- inactive(output);
- }
- };
- struct DebugRequestSearchHistories : public DebugRequestBase
- {
- private:
- Linked<IRowMatcher> matcher;
- bool fullRows;
- public:
- inline DebugRequestSearchHistories(memsize_t _proxyId, IRowMatcher *_matcher, bool _fullRows)
- : DebugRequestBase(DEBUGREQUEST_SEARCHHISTORIES, _proxyId), matcher(_matcher), fullRows(_fullRows)
- {
- }
- DebugRequestSearchHistories(MemoryBuffer &serialized) : DebugRequestBase(serialized)
- {
- matcher.setown(createRowMatcher(serialized));
- serialized.read(fullRows);
- }
- virtual void serialize(MemoryBuffer &buf)
- {
- DebugRequestBase::serialize(buf);
- matcher->serialize(buf);
- buf.append(fullRows);
- }
- virtual void executeRequest(IXmlWriter *output)
- {
- Owned<IDebugGraphManager> manager = getManager();
- if (manager)
- manager->searchHistories(output, matcher, fullRows);
- else
- inactive(output);
- }
- };
- class DebugRequestGetResetGlobalCounts : public DebugRequestBase
- {
- public:
- inline DebugRequestGetResetGlobalCounts(memsize_t _proxyId)
- : DebugRequestBase(DEBUGREQUEST_GETRESETGLOBALCOUNTS, _proxyId)
- {
- }
- DebugRequestGetResetGlobalCounts(MemoryBuffer &serialized) : DebugRequestBase(serialized)
- {
- }
- virtual void executeRequest(IXmlWriter *output)
- {
- Owned<IDebugGraphManager> manager = getManager();
- if (manager)
- manager->queryContext()->debugCounts(output, 0, true);
- else
- inactive(output);
- }
- };
- void doDebugRequest(IRoxieQueryPacket *packet, const IRoxieContextLogger &logctx)
- {
- RoxiePacketHeader newHeader(packet->queryHeader(), ROXIE_DEBUGREQUEST);
- Owned<IMessagePacker> output = ROQ->createOutputStream(newHeader, true, logctx);
- unsigned contextLength = packet->getContextLength();
- Owned<DebugRequestBase> request;
- MemoryBuffer serialized;
- serialized.setBuffer(contextLength, (void*) packet->queryContextData(), false);
- byte fval;
- serialized.read(fval);
- serialized.reset();
- CommonXmlWriter xml(0);
- switch ((DebugRequestType) fval)
- {
- case DEBUGREQUEST_OUTPUTCHILDGRAPH:
- request.setown(new DebugRequestOutputChildGraph(serialized));
- break;
- case DEBUGREQUEST_OUTPUTLINKSFORCHILDGRAPH:
- request.setown(new DebugRequestOutputLinksForChildGraph(serialized));
- break;
- case DEBUGREQUEST_LOOKUPACTIVITYBYEDGEID:
- request.setown(new DebugRequestLookupActivityByEdgeId(serialized));
- break;
- case DEBUGREQUEST_PRINTEDGE:
- request.setown(new DebugRequestPrintEdge(serialized));
- break;
- case DEBUGREQUEST_SETBREAKPOINT:
- request.setown(new DebugRequestSetRemoveBreakpoint(serialized));
- break;
- case DEBUGREQUEST_SEARCHHISTORIES:
- request.setown(new DebugRequestSearchHistories(serialized));
- break;
- case DEBUGREQUEST_GETRESETGLOBALCOUNTS:
- request.setown(new DebugRequestGetResetGlobalCounts(serialized));
- break;
- default: throwUnexpected();
- }
- request->executeRequest(&xml);
- void *ret = output->getBuffer(xml.length()+1, true);
- memcpy(ret, xml.str(), xml.length()+1);
- output->putBuffer(ret, xml.length()+1, true);
- output->flush(true);
- }
- class CProxyDebugContext : public CInterface
- {
- protected:
- memsize_t proxyId;
- unsigned channel;
- Owned<StringContextLogger> logctx;
- void sendProxyRequest(IXmlWriter *output, DebugRequestBase &request) const
- {
- RemoteActivityId id(ROXIE_DEBUGREQUEST, 0);
- ruid_t ruid = getNextRuid();
- RoxiePacketHeader header(id, ruid, channel, 0);
- MemoryBuffer b;
- b.append(sizeof(header), &header);
- b.append ((char) LOGGING_FLAGSPRESENT);
- b.append("PROXY"); // MORE - a better log prefix might be good...
- request.serialize(b);
- Owned<IRowManager> rowManager = roxiemem::createRowManager(1, NULL, *logctx, NULL);
- Owned<IMessageCollator> mc = ROQ->queryReceiveManager()->createMessageCollator(rowManager, ruid);
- Owned<IRoxieQueryPacket> packet = createRoxiePacket(b);
- ROQ->sendPacket(packet, *logctx);
- for (unsigned retries = 1; retries <= MAX_DEBUGREQUEST_RETRIES; retries++)
- {
- bool anyActivity = false;
- Owned<IMessageResult> mr = mc->getNextResult(DEBUGREQUEST_TIMEOUT, anyActivity);
- if (mr)
- {
- unsigned roxieHeaderLen;
- const RoxiePacketHeader *header = (const RoxiePacketHeader *) mr->getMessageHeader(roxieHeaderLen);
- Owned<IMessageUnpackCursor> mu = mr->getCursor(rowManager);
- if (header->activityId == ROXIE_EXCEPTION)
- throwRemoteException(mu);
- assertex(header->activityId == ROXIE_DEBUGREQUEST);
- RecordLengthType *rowlen = (RecordLengthType *) mu->getNext(sizeof(RecordLengthType));
- assertex(rowlen);
- RecordLengthType len = *rowlen;
- ReleaseRoxieRow(rowlen);
- const char * reply = (const char *) mu->getNext(len);
- if (output)
- {
- output->outputString(0, NULL, NULL);
- output->outputQuoted(reply);
- }
- ReleaseRoxieRow(reply);
- ROQ->queryReceiveManager()->detachCollator(mc);
- mc.clear();
- return;
- }
- else if (!anyActivity)
- {
- DBGLOG("Retrying debug request");
- ROQ->sendPacket(packet, *logctx);
- }
- }
- ROQ->queryReceiveManager()->detachCollator(mc);
- mc.clear();
- throwUnexpected(); // MORE - better error
- }
- public:
- CProxyDebugContext(unsigned _channel, memsize_t _proxyId) : channel(_channel), proxyId(_proxyId)
- {
- logctx.setown(new StringContextLogger("CProxyDebugContext"));
- }
- };
- class CProxyActivityDebugContext : public CProxyDebugContext, implements IActivityDebugContext
- {
- StringAttr edgeId;
- public:
- IMPLEMENT_IINTERFACE;
- CProxyActivityDebugContext(unsigned _channel, memsize_t _proxyId, const char *_edgeId)
- : CProxyDebugContext(_channel, _proxyId), edgeId(_edgeId)
- {
- }
-
- virtual unsigned queryLastSequence() const { UNIMPLEMENTED; };
- virtual IActivityDebugContext *queryInputActivity() const { UNIMPLEMENTED; };
- virtual void getXGMML(IXmlWriter *output) const { UNIMPLEMENTED; };
- virtual void searchHistories(IXmlWriter *output, IRowMatcher *matcher, bool fullRows) { UNIMPLEMENTED; }
- virtual unsigned queryHistorySize() const { UNIMPLEMENTED; };
- virtual IHistoryRow *queryHistoryRow(unsigned idx) const { UNIMPLEMENTED; };
- virtual unsigned queryHistoryCapacity() const { UNIMPLEMENTED; };
- virtual IBreakpointInfo *debuggerCallback(unsigned sequence, const void *row)
- {
- // was done on slave, don't do here too
- return NULL;
- };
- virtual void setHistoryCapacity(unsigned newCapacity) { UNIMPLEMENTED; };
- virtual void clearHistory() { UNIMPLEMENTED; };
- virtual void printEdge(IXmlWriter *output, unsigned startRow, unsigned numRows) const
- {
- DebugRequestPrintEdge request(proxyId, startRow, numRows);
- sendProxyRequest(output, request);
- };
- virtual void setBreakpoint(IBreakpointInfo &bp) { throwUnexpected(); }
- virtual void removeBreakpoint(IBreakpointInfo &bp) { throwUnexpected(); };
- virtual const char *queryEdgeId() const
- {
- return edgeId;
- };
- virtual const char *querySourceId() const { UNIMPLEMENTED; };
- virtual unsigned queryChildGraphId() const { UNIMPLEMENTED; };
- virtual memsize_t queryProxyId() const { UNIMPLEMENTED; };
- };
- class CProxyDebugGraphManager : public CProxyDebugContext, implements IDebugGraphManager
- {
- unsigned id;
- StringBuffer idString;
- MapStringToMyClass<IActivityDebugContext> edgeProxies;
- public:
- IMPLEMENT_IINTERFACE;
- CProxyDebugGraphManager(unsigned _id, unsigned _channel, memsize_t _proxyId)
- : CProxyDebugContext(_channel, _proxyId), id(_id)
- {
- idString.append(_id).append('#').append(channel);
- }
-
- virtual IActivityDebugContext *lookupActivityByEdgeId(const char *edgeId)
- {
- IActivityDebugContext *edge = edgeProxies.getValue(edgeId);
- if (!edge)
- {
- const char *channelTail = strrchr(edgeId, '#');
- if (channelTail && atoi(channelTail+1)==channel)
- {
- DebugRequestLookupActivityByEdgeId request(proxyId, edgeId);
- CommonXmlWriter reply(0);
- sendProxyRequest(&reply, request);
- Owned<IPropertyTree> response = createPTreeFromXMLString(reply.str());
- if (response)
- {
- memsize_t proxyId = (memsize_t) response->getPropInt64("@proxyId", 0);
- if (proxyId)
- {
- edge = new CProxyActivityDebugContext(channel, proxyId, edgeId);
- edgeProxies.setValue(edgeId, edge);
- ::Release(edge);
- }
- }
- }
- }
- return edge;
- }
- virtual const char *queryGraphName() const { UNIMPLEMENTED; }
- virtual void getXGMML(IXmlWriter *output, unsigned sequence, bool isActive)
- {
- throwUnexpected();
- }
- virtual void setBreakpoint(IBreakpointInfo &bp)
- {
- DebugRequestSetRemoveBreakpoint request(proxyId, bp, false);
- sendProxyRequest(NULL, request);
- }
- virtual void removeBreakpoint(IBreakpointInfo &bp)
- {
- DebugRequestSetRemoveBreakpoint request(proxyId, bp, true);
- sendProxyRequest(NULL, request);
- }
- virtual void setHistoryCapacity(unsigned newCapacity) { UNIMPLEMENTED; }
- virtual void clearHistories() { UNIMPLEMENTED; }
- virtual void searchHistories(IXmlWriter *output, IRowMatcher *matcher, bool fullRows)
- {
- DebugRequestSearchHistories request(proxyId, matcher, fullRows);
- sendProxyRequest(output, request);
- }
- virtual void setNodeProperty(IActivityBase *node, const char *propName, const char *propVvalue)
- {
- // MORE - should I do anything here?
- }
- virtual DebugActivityRecord *getNodeByActivityBase(IActivityBase *activity) const
- {
- // MORE - should I do anything here?
- return NULL;
- }
- virtual void noteSlaveGraph(IActivityBase *parentActivity, unsigned graphId, unsigned channel, memsize_t remoteGraphId)
- {
- UNIMPLEMENTED; // MORE - can this happen? nested graphs?
- }
- virtual memsize_t queryProxyId() const
- {
- return proxyId;
- }
- virtual const char *queryIdString() const
- {
- return idString.str();
- }
- virtual unsigned queryId() const
- {
- return id;
- }
- virtual void outputChildGraph(IXmlWriter *output, unsigned sequence)
- {
- DebugRequestOutputChildGraph request(proxyId, sequence);
- sendProxyRequest(output, request);
- }
- virtual void outputLinksForChildGraph(IXmlWriter *output, const char *parentId)
- {
- DebugRequestOutputLinksForChildGraph request(proxyId, parentId);
- sendProxyRequest(output, request);
- }
- virtual void serializeProxyGraphs(MemoryBuffer &buff)
- {
- UNIMPLEMENTED;
- }
- virtual void deserializeProxyGraphs(DebugState state, MemoryBuffer &buff, IActivityBase *parentActivity, unsigned channel)
- {
- UNIMPLEMENTED;
- }
- virtual IDebuggableContext *queryContext() const
- {
- UNIMPLEMENTED;
- }
- virtual void mergeRemoteCounts(IDebuggableContext *into) const
- {
- DebugRequestGetResetGlobalCounts request(proxyId);
- CommonXmlWriter reply(0);
- reply.outputBeginNested("Counts", true);
- sendProxyRequest(&reply, request);
- reply.outputEndNested("Counts"); // strange way to do it...
- Owned<IPropertyTree> response = createPTreeFromXMLString(reply.str());
- if (response)
- {
- Owned<IPropertyTreeIterator> edges = response->getElements("edge");
- ForEach(*edges)
- {
- IPropertyTree &edge = edges->query();
- const char *edgeId = edge.queryProp("@edgeId");
- unsigned edgeCount = edge.getPropInt("@count");
- Owned<IGlobalEdgeRecord> thisEdge = into->getEdgeRecord(edgeId);
- thisEdge->incrementCount(edgeCount, into->querySequence());
- }
- }
- }
- };
- IDebugGraphManager *createProxyDebugGraphManager(unsigned graphId, unsigned channel, memsize_t remoteGraphId)
- {
- return new CProxyDebugGraphManager(graphId, channel, remoteGraphId);
- }
- //===================================================================================================================
- class CPseudoArg : public CInterface, implements IHThorArg
- {
- public:
- IMPLEMENT_IINTERFACE
- virtual IOutputMetaData * queryOutputMeta() { return NULL; }
- };
- class CPseudoActivity : public CRoxieServerActivity
- {
- public:
- CPseudoActivity(IHThorArg & _helper) : CRoxieServerActivity(_helper) {}
- virtual const void *nextInGroup()
- {
- throwUnexpected(); // I am nobody's input
- }
- };
- class CActivityGraph : public CInterface, implements IActivityGraph, implements IThorChildGraph, implements ILocalGraphEx, implements IRoxieServerChildGraph
- {
- protected:
- IArrayOf<IRoxieServerActivity> activities;
- IArrayOf<IRoxieInput> probes;
- IRoxieServerActivityCopyArray sinks;
- StringAttr graphName;
- Owned<CGraphResults> results;
- CGraphResults graphLoopResults;
- ActivityArray & graphDefinition;
- CriticalSection evaluateCrit;
- IProbeManager *probeManager;
- unsigned id;
- unsigned loopCounter;
- class ActivityGraphSlaveContext : public IndirectSlaveContext
- {
- SpinLock abortLock;
- bool aborted;
- Owned<IException> exception;
- public:
- ActivityGraphSlaveContext(const IRoxieContextLogger &_logctx) : logctx(_logctx), loopCounter(0), codeContext(NULL)
- {
- aborted = false;
- }
- // Note - we must track exceptions at the child level in case there is a CATCH in parent
- virtual void notifyAbort(IException *E)
- {
- SpinBlock b(abortLock);
- if (!aborted && QUERYINTERFACE(E, InterruptedSemaphoreException) == NULL)
- {
- aborted = true;
- exception.set(E);
- }
- }
- virtual void checkAbort()
- {
- if (aborted) // NOTE - don't bother getting lock before reading this (for speed) - a false read is very unlikely and not a problem
- {
- SpinBlock b(abortLock);
- if (!exception)
- exception.setown(MakeStringException(ROXIE_INTERNAL_ERROR, "Query was aborted"));
- throw exception.getLink();
- }
- IndirectSlaveContext::checkAbort();
- }
- virtual ICodeContext *queryCodeContext()
- {
- return codeContext;
- }
- void setCodeContext(ICodeContext * _codeContext)
- {
- codeContext = _codeContext;
- }
- void setLoopCounter(unsigned _loopCounter)
- {
- loopCounter = _loopCounter;
- }
- virtual void noteChildGraph(unsigned id, IActivityGraph *childGraph)
- {
- childGraphs.setValue(id, childGraph);
- }
- virtual IActivityGraph * queryChildGraph(unsigned id)
- {
- if (queryTraceLevel() > 10)
- CTXLOG("resolveChildGraph %d", id);
- IActivityGraph *childGraph = childGraphs.getValue(id);
- assertex(childGraph);
- return childGraph;
- }
- // MORE should really redirect the other log context ones too (though mostly doesn't matter). Really should refactor to have a queryLogContext() method in IRoxieSlaveContext I think
- virtual StringBuffer &getLogPrefix(StringBuffer &ret) const
- {
- logctx.getLogPrefix(ret);
- if (loopCounter)
- ret.appendf("{%u}", loopCounter);
- return ret;
- }
- protected:
- const IRoxieContextLogger &logctx;
- unsigned loopCounter;
- ICodeContext * codeContext;
- MapXToMyClass<unsigned, unsigned, IActivityGraph> childGraphs;
- } graphSlaveContext;
- class ActivityGraphCodeContext : public IndirectCodeContext
- {
- public:
- virtual ILocalGraph * resolveLocalQuery(__int64 activityId)
- {
- if ((unsigned) activityId == container->queryId())
- return container;
- IActivityGraph * match = slaveContext->queryChildGraph((unsigned) activityId);
- if (match)
- return match->queryLocalGraph();
- return IndirectCodeContext::resolveLocalQuery(activityId);
- }
- virtual IThorChildGraph * resolveChildQuery(__int64 activityId, IHThorArg * colocal)
- {
- IActivityGraph * match = slaveContext->queryChildGraph((unsigned) activityId);
- return LINK(match->queryChildGraph());
- }
- virtual unsigned getGraphLoopCounter() const
- {
- return container->queryLoopCounter(); // only called if value is valid
- }
- void setContainer(IRoxieSlaveContext * _slaveContext, CActivityGraph * _container)
- {
- slaveContext = _slaveContext;
- container = _container;
- }
- protected:
- IRoxieSlaveContext * slaveContext;
- CActivityGraph * container;
- } graphCodeContext;
- public:
- IMPLEMENT_IINTERFACE;
- CActivityGraph(const char *_graphName, unsigned _id, ActivityArray &x, IProbeManager *_probeManager, const IRoxieContextLogger &_logctx)
- : probeManager(_probeManager), graphDefinition(x), graphName(_graphName), graphSlaveContext(_logctx)
- {
- id = x.getLibraryGraphId();
- if (!id)
- id = _id;
- loopCounter = 0;
- graphSlaveContext.setCodeContext(&graphCodeContext);
- graphCodeContext.setContainer(&graphSlaveContext, this);
- }
- ~CActivityGraph()
- {
- if (probeManager)
- probeManager->deleteGraph((IArrayOf<IActivityBase>*)&activities, (IArrayOf<IInputBase>*)&probes);
- }
- virtual const char *queryName() const
- {
- return graphName.get();
- }
- void createGraph()
- {
- ForEachItemIn(idx, graphDefinition)
- {
- IRoxieServerActivityFactory &donor = graphDefinition.serverItem(idx);
- IRoxieServerActivity &activity = *donor.createActivity(probeManager);
- activities.append(activity);
- if (donor.isSink())
- {
- sinks.append(activity);
- if (probeManager)
- probeManager->noteSink(&activity);
- }
- }
- ForEachItemIn(idx1, graphDefinition)
- {
- IRoxieServerActivityFactory &donor = graphDefinition.serverItem(idx1);
- IRoxieServerActivity &activity = activities.item(idx1);
- unsigned inputidx = 0;
- loop
- {
- unsigned outputidx;
- unsigned source = donor.getInput(inputidx, outputidx);
- if (source==(unsigned) -1)
- break;
- connectInput(idx1, inputidx, source, outputidx, 0);
- inputidx++;
- }
- IntArray &dependencies = donor.queryDependencies();
- IntArray &dependencyIndexes = donor.queryDependencyIndexes();
- IntArray &dependencyControlIds = donor.queryDependencyControlIds();
- StringArray &dependencyEdgeIds = donor.queryDependencyEdgeIds();
- ForEachItemIn(idx2, dependencies)
- {
- IRoxieServerActivity &dependencySourceActivity = activities.item(dependencies.item(idx2));
- unsigned dependencySourceIndex = dependencyIndexes.item(idx2);
- unsigned dependencyControlId = dependencyControlIds.item(idx2);
- activity.addDependency(dependencySourceActivity, dependencySourceIndex, dependencyControlId);
- if (probeManager)
- probeManager->noteDependency( &dependencySourceActivity, dependencySourceIndex, dependencyControlId, dependencyEdgeIds.item(idx2), &activity);
- }
- }
- }
- void connectInput(unsigned target, unsigned targetIdx, unsigned source, unsigned sourceIdx, unsigned iteration)
- {
- IRoxieServerActivity &targetActivity = activities.item(target);
- IRoxieServerActivity &sourceActivity = activities.item(source);
- IRoxieInput * output = sourceActivity.queryOutput(sourceIdx);
- if (probeManager)
- {
- IInputBase * inputBase = probeManager->createProbe(static_cast<IInputBase*>(output), &sourceActivity, &targetActivity, sourceIdx, targetIdx, iteration);
- output = static_cast<IRoxieInput*>(inputBase);
- probes.append(*LINK(output));
- }
- targetActivity.setInput(targetIdx, output);
- }
- virtual void onCreate(IRoxieSlaveContext *ctx, IHThorArg *_colocalParent)
- {
- graphSlaveContext.set(ctx);
- if (graphDefinition.isMultiInstance())
- {
- graphCodeContext.set(ctx->queryCodeContext());
- ctx = &graphSlaveContext;
- }
- ForEachItemIn(idx, activities)
- {
- IRoxieServerActivity *activity = &activities.item(idx);
- if (activity)
- activity->onCreate(ctx, _colocalParent);
- }
- }
- virtual void abort()
- {
- ForEachItemIn(idx, sinks)
- {
- IRoxieServerActivity &sink = sinks.item(idx);
- sink.stop(true);
- }
- }
- virtual void reset()
- {
- ForEachItemIn(idx, sinks)
- {
- IRoxieServerActivity &sink = sinks.item(idx);
- sink.reset();
- }
- }
- Linked<IException> exception;
- CriticalSection eCrit;
- virtual void noteException(IException *E)
- {
- CriticalBlock b(eCrit);
- if (!exception)
- {
- if (graphSlaveContext.queryDebugContext())
- {
- graphSlaveContext.queryDebugContext()->checkBreakpoint(DebugStateException, NULL, exception);
- }
- exception.set(E);
- }
- }
- virtual void checkAbort()
- {
- CriticalBlock b(eCrit);
- if (exception)
- throw exception.getLink();
- }
- virtual void execute()
- {
- doExecute(0, NULL);
- }
- //New child query code...
- virtual IThorChildGraph * queryChildGraph()
- {
- return this;
- }
- virtual ILocalGraph * queryLocalGraph()
- {
- return this;
- }
- virtual IRoxieServerChildGraph * queryLoopGraph()
- {
- return this;
- }
- inline unsigned queryId() const
- {
- return id;
- }
- inline unsigned queryLoopCounter() const
- {
- return loopCounter;
- }
- void doExecute(unsigned parentExtractSize, const byte * parentExtract)
- {
- if (sinks.ordinality()==1)
- sinks.item(0).execute(parentExtractSize, parentExtract);
- #ifdef PARALLEL_EXECUTE
- else if (!probeManager)
- {
- class casyncfor: public CAsyncFor
- {
- public:
- IActivityGraph &parent;
- unsigned parentExtractSize;
- const byte * parentExtract;
- casyncfor(IRoxieServerActivityCopyArray &_sinks, IActivityGraph &_parent, unsigned _parentExtractSize, const byte * _parentExtract) :
- sinks(_sinks), parent(_parent), parentExtractSize(_parentExtractSize), parentExtract(_parentExtract) { }
- void Do(unsigned i)
- {
- try
- {
- sinks.item(i).execute(parentExtractSize, parentExtract);
- }
- catch (IException *E)
- {
- parent.noteException(E);
- throw;
- }
- }
- private:
- IRoxieServerActivityCopyArray &sinks;
- } afor(sinks, *this, parentExtractSize, parentExtract);
- afor.For(sinks.ordinality(), sinks.ordinality());
- }
- #endif
- else
- {
- ForEachItemIn(idx, sinks)
- {
- IRoxieServerActivity &sink = sinks.item(idx);
- sink.execute(parentExtractSize, parentExtract);
- }
- }
- }
- virtual IEclGraphResults *evaluate(unsigned parentExtractSize, const byte * parentExtract)
- {
- CriticalBlock block(evaluateCrit);
- results.setown(new CGraphResults);
- try
- {
- doExecute(parentExtractSize, parentExtract);
- }
- catch (...)
- {
- DBGLOG("Exception thrown in child query - cleaning up");
- reset();
- throw;
- }
- reset();
- return results.getClear();
- }
- //interface IRoxieServerChildGraph
- virtual void beforeExecute()
- {
- results.setown(new CGraphResults);
- }
- virtual IRoxieInput * startOutput(unsigned id, unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- IRoxieInput * ret = selectOutput(id);
- ret->start(parentExtractSize, parentExtract, paused);
- return ret;
- }
- virtual IRoxieInput * selectOutput(unsigned id)
- {
- ForEachItemIn(i, sinks)
- {
- IRoxieInput * ret = sinks.item(i).querySelectOutput(id);
- if (ret)
- return ret;
- }
- throwUnexpected();
- return NULL;
- }
- virtual void setInputResult(unsigned id, IGraphResult * result)
- {
- results->setResult(id, result);
- }
- virtual bool querySetInputResult(unsigned id, IRoxieInput * input)
- {
- ForEachItemIn(i, activities)
- {
- if (activities.item(i).querySetStreamInput(id, input))
- return true;
- }
- return false;
- }
- virtual void stopUnusedOutputs()
- {
- //Hmm not sure how to do this...
- }
- virtual void afterExecute()
- {
- ForEachItemIn(i, sinks)
- {
- sinks.item(i).stop(false);
- }
- if (graphSlaveContext.queryDebugContext())
- {
- graphSlaveContext.queryDebugContext()->checkBreakpoint(DebugStateGraphFinished, NULL, NULL);
- }
- reset();
- }
- virtual IRoxieGraphResults * execute(size32_t parentExtractSize, const byte *parentExtract)
- {
- doExecute(parentExtractSize, parentExtract);
- return LINK(results);
- }
- virtual void getResult(size32_t & retSize, void * & ret, unsigned id)
- {
- results->getResult(retSize, ret, id);
- }
- virtual void getLinkedResult(unsigned & count, byte * * & ret, unsigned id)
- {
- results->getLinkedResult(count, ret, id);
- }
- virtual void setResult(unsigned id, IGraphResult * result)
- {
- results->setResult(id, result);
- }
- virtual IRoxieInput * createResultIterator(unsigned id)
- {
- return results->createIterator(id);
- }
- virtual void setGraphLoopResult(IGraphResult * result)
- {
- graphLoopResults.appendResult(result);
- }
- virtual IRoxieInput * createGraphLoopResultIterator(unsigned id)
- {
- try
- {
- return graphLoopResults.createIterator(id);
- }
- catch (IException * e)
- {
- e->Release();
- throw MakeStringException(ROXIE_GRAPH_PROCESSING_ERROR, "Error reading graph result %d before it is calculated", id);
- }
- }
- virtual void clearGraphLoopResults()
- {
- graphLoopResults.clear();
- }
- virtual void executeGraphLoop(size32_t parentExtractSize, const byte *parentExtract)
- {
- doExecute(parentExtractSize, parentExtract);
- }
- virtual void setGraphLoopResult(unsigned id, IGraphResult * result)
- {
- graphLoopResults.setResult(id, result);
- }
- virtual IRoxieInput * getGraphLoopResult(unsigned id)
- {
- return graphLoopResults.createIterator(id);
- }
- virtual void getProbeResponse(IPropertyTree *query)
- {
- if (probeManager)
- probeManager->getProbeResponse(query);
- }
- virtual IRoxieServerChildGraph * createGraphLoopInstance(unsigned loopCounter, unsigned parentExtractSize, const byte * parentExtract, const IRoxieContextLogger &logctx)
- {
- throwUnexpected();
- }
- virtual CGraphIterationInfo *selectGraphLoopOutput()
- {
- return NULL;
- }
- virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor)
- {
- throwUnexpected();
- }
- virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor)
- {
- throwUnexpected();
- }
- };
- class CIterationActivityGraph : public CActivityGraph
- {
- IHThorArg * colocalParent;
- unsigned fixedParentExtractSize;
- const byte * fixedParentExtract;
- unsigned graphOutputActivityIndex;
- public:
- CIterationActivityGraph(const char *_graphName, unsigned _id, ActivityArray &x, IProbeManager *_probeManager,
- unsigned _loopCounter, IRoxieSlaveContext *ctx, IHThorArg * _colocalParent, unsigned parentExtractSize, const byte * parentExtract, const IRoxieContextLogger &_logctx)
- : CActivityGraph(_graphName, _id, x, _probeManager, _logctx)
- {
- graphOutputActivityIndex = 0;
- loopCounter = _loopCounter;
- colocalParent = _colocalParent;
- graphSlaveContext.set(ctx);
- graphSlaveContext.setLoopCounter(loopCounter);
- graphCodeContext.set(ctx->queryCodeContext());
- fixedParentExtractSize = parentExtractSize;
- fixedParentExtract = parentExtract;
- }
- void createIterationGraph()
- {
- Owned<IRoxieServerActivity> pseudoActivity = new CPseudoActivity(*new CPseudoArg);
- ForEachItemIn(idx1, graphDefinition)
- activities.append(*LINK(pseudoActivity));
- graphOutputActivityIndex = queryGraphOutputIndex();
- recursiveCreateGraph(graphOutputActivityIndex);
- }
- unsigned queryGraphOutputIndex() const
- {
- ForEachItemIn(i, graphDefinition)
- if (graphDefinition.serverItem(i).getKind() == TAKgraphloopresultwrite)
- return i;
- throwUnexpected();
- }
- void recursiveCreateGraph(unsigned whichActivity)
- {
- //Check to see if already created
- IRoxieServerActivity & prevActivity = activities.item(whichActivity);
- if (prevActivity.queryId() != 0)
- {
- prevActivity.noteOutputUsed(); //We need to patch up the number of outputs for splitters.
- return;
- }
- IRoxieServerActivityFactory &donor = graphDefinition.serverItem(whichActivity);
- IRoxieServerActivity * activity = NULL;
- if (donor.isGraphInvariant())
- {
- ThorActivityKind kind = donor.getKind();
- switch (kind)
- {
- case TAKif:
- case TAKchildif:
- case TAKcase:
- case TAKchildcase: // MORE RKC->GH - what about FILTER with a graph-invariant condition and other latestart activities?
- {
- Owned<IHThorArg> helper = &donor.getHelper();
- helper->onCreate(&graphCodeContext, colocalParent, NULL);
- helper->onStart(fixedParentExtract, NULL);
- unsigned branch;
- switch (kind)
- {
- case TAKif: case TAKchildif:
- branch = static_cast<IHThorIfArg *>(helper.get())->getCondition() ? 0 : 1;
- break;
- case TAKcase: case TAKchildcase:
- branch = static_cast<IHThorCaseArg *>(helper.get())->getBranch();
- break;
- default:
- throwUnexpected();
- }
- helper.clear();
- unsigned outputidx;
- unsigned source = donor.getInput(branch, outputidx);
- if (source ==(unsigned) -1)
- activity = createRoxieServerNullActivity(&donor, probeManager);
- else
- activity = createRoxieServerPassThroughActivity(&donor, probeManager);
- activities.replace(*activity, whichActivity);
- activity->onCreate(&graphSlaveContext, colocalParent);
- if (source ==(unsigned) -1)
- return;
- recursiveCreateGraph(source);
- connectInput(whichActivity, 0, source, outputidx, loopCounter);
- break;
- }
- }
- }
- if (!activity)
- {
- activity = donor.createActivity(probeManager);
- activities.replace(*activity, whichActivity);
- activity->onCreate(&graphSlaveContext, colocalParent);
- activity->resetOutputsUsed();
- unsigned inputidx = 0;
- loop
- {
- unsigned outputidx;
- unsigned source = donor.getInput(inputidx, outputidx);
- if (source==(unsigned) -1)
- break;
- recursiveCreateGraph(source);
- connectInput(whichActivity, inputidx, source, outputidx, loopCounter);
- inputidx++;
- }
- }
- IntArray &dependencies = donor.queryDependencies();
- IntArray &dependencyIndexes = donor.queryDependencyIndexes();
- IntArray &dependencyControlIds = donor.queryDependencyControlIds();
- ForEachItemIn(idx2, dependencies)
- {
- unsigned input = dependencies.item(idx2);
- recursiveCreateGraph(input);
- activity->addDependency(activities.item(input),dependencyIndexes.item(idx2),dependencyControlIds.item(idx2));
- }
- }
- virtual CGraphIterationInfo *selectGraphLoopOutput()
- {
- IRoxieServerActivity &sourceActivity = activities.item(graphOutputActivityIndex);
- return new CGraphIterationInfo(&sourceActivity, sourceActivity.queryOutput(0), 0, loopCounter);
- }
- virtual void gatherIterationUsage(IRoxieServerLoopResultProcessor & processor)
- {
- ForEachItemIn(i, activities)
- activities.item(i).gatherIterationUsage(processor, fixedParentExtractSize, fixedParentExtract);
- }
- virtual void associateIterationOutputs(IRoxieServerLoopResultProcessor & processor)
- {
- ForEachItemIn(i, activities)
- activities.item(i).associateIterationOutputs(processor, fixedParentExtractSize, fixedParentExtract, probeManager, probes);
- }
- };
- class CDelayedActivityGraph : public CInterface, implements IActivityGraph
- {
- StringAttr graphName;
- ActivityArray & graphDefinition;
- IProbeManager *probeManager;
- unsigned id;
- IRoxieSlaveContext * ctx;
- IHThorArg * colocalParent;
- public:
- IMPLEMENT_IINTERFACE;
- CDelayedActivityGraph(const char *_graphName, unsigned _id, ActivityArray &x, IProbeManager *_probeManager)
- : probeManager(_probeManager), graphDefinition(x)
- {
- graphName.set(_graphName);
- id = _id;
- ctx = NULL;
- colocalParent = NULL;
- }
- virtual const char *queryName() const { return graphName.get(); }
- virtual void abort() { throwUnexpected(); }
- virtual void reset() { }
- virtual void execute() { throwUnexpected(); }
- virtual void getProbeResponse(IPropertyTree *query) { throwUnexpected(); }
- virtual void noteException(IException *E) { throwUnexpected(); }
- virtual void checkAbort() { throwUnexpected(); }
- virtual IThorChildGraph * queryChildGraph() { throwUnexpected(); }
- virtual ILocalGraph * queryLocalGraph() { throwUnexpected(); }
- virtual IRoxieServerChildGraph * queryLoopGraph() { throwUnexpected(); }
- virtual void onCreate(IRoxieSlaveContext *_ctx, IHThorArg *_colocalParent)
- {
- ctx = _ctx;
- colocalParent = _colocalParent;
- }
- virtual IRoxieServerChildGraph * createGraphLoopInstance(unsigned loopCounter, unsigned parentExtractSize, const byte * parentExtract, const IRoxieContextLogger &logctx)
- {
- Owned<CIterationActivityGraph> ret = new CIterationActivityGraph(graphName, id, graphDefinition, probeManager, loopCounter, ctx, colocalParent, parentExtractSize, parentExtract, logctx);
- ret->createIterationGraph();
- return ret.getClear();
- }
- };
- IActivityGraph *createActivityGraph(const char *_graphName, unsigned id, ActivityArray &childFactories, IRoxieServerActivity *parentActivity, IProbeManager *_probeManager, const IRoxieContextLogger &_logctx)
- {
- if (childFactories.isDelayed())
- {
- return new CDelayedActivityGraph(_graphName, id, childFactories, _probeManager);
- }
- else
- {
- Owned<IProbeManager> childProbe;
- if (_probeManager)
- childProbe.setown(_probeManager->startChildGraph(id, parentActivity));
- Owned<CActivityGraph> ret = new CActivityGraph(_graphName, id, childFactories, childProbe, _logctx);
- ret->createGraph();
- if (_probeManager)
- _probeManager->endChildGraph(childProbe, parentActivity);
- return ret.getClear();
- }
- }
- class CRoxieWorkflowMachine : public WorkflowMachine
- {
- public:
- CRoxieWorkflowMachine(IPropertyTree *_workflowInfo, bool _doOnce, const IRoxieContextLogger &_logctx) : WorkflowMachine(_logctx)
- {
- workflowInfo = _workflowInfo;
- doOnce = _doOnce;
- }
- protected:
- virtual void begin()
- {
- // MORE - should pre-do more of this work
- unsigned count = 0;
- Owned<IConstWorkflowItemIterator> iter = createWorkflowItemIterator(workflowInfo);
- for(iter->first(); iter->isValid(); iter->next())
- count++;
- workflow.setown(createWorkflowItemArray(count));
- for(iter->first(); iter->isValid(); iter->next())
- {
- IConstWorkflowItem *item = iter->query();
- bool isOnce = (item->queryMode() == WFModeOnce);
- workflow->addClone(item);
- if (isOnce != doOnce)
- workflow->queryWfid(item->queryWfid()).setState(WFStateDone);
- }
- }
- virtual void end()
- {
- workflow.clear();
- }
- virtual void schedulingStart() { throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Scheduling not supported in roxie"); }
- virtual bool schedulingPull() { throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Scheduling not supported in roxie"); }
- virtual bool schedulingPullStop() { throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Scheduling not supported in roxie"); }
- virtual void reportContingencyFailure(char const * type, IException * e) {}
- virtual void checkForAbort(unsigned wfid, IException * handling) {}
- virtual void doExecutePersistItem(IRuntimeWorkflowItem & item) { throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "Persists not supported in roxie"); }
- private:
- IPropertyTree *workflowInfo;
- bool doOnce;
- };
- WorkflowMachine *createRoxieWorkflowMachine(IPropertyTree *_workflowInfo, bool _doOnce, const IRoxieContextLogger &_logctx)
- {
- return new CRoxieWorkflowMachine(_workflowInfo, _doOnce, _logctx);
- }
- //=======================================================================================================================
- #define DEBUGEE_TIMEOUT 10000
- class CSlaveDebugContext : public CBaseDebugContext
- {
- /*
- Some thoughts on slave debugging
- 1. Something like a ping can be used to get data from slave when needed
- 2. Should disable IBYTI processing (always use primary) - DONE
- and server-side caching - DONE
- 3. Roxie server can know what slave transactions are pending by intercepting the sends - no need for slave to call back just to indicate start of slave subgraph
- 4. There is a problem when a slave hits a breakpoint in that the breakpoint cound have been deleted by the time it gets a chance to tell the Roxie server - can't
- happen in local case because of the critical block at the head of checkBreakpoint but the local copy of BPs out on slave CAN get out of date. Should we care?
- Should there be a "Sorry, your breakpoints are out of date, here's the new set" response?
- Actually what we do is recheck the BP on the server, and ensure that breakpoint indexes are persistant. DONE
- 5. We need to serialize over our graph info if changed since last time.
- 6. I think we need to change implementation of debugGraph to support children. Then we have a place to put a proxy for a remote one.
- - id's should probably be structured so we can use a hash table at each level
- */
- const RoxiePacketHeader &header;
- memsize_t parentActivity;
- unsigned channel;
- int debugSequence;
- CriticalSection crit;
- const IRoxieContextLogger &logctx; // hides base class definition with more derived class pointer
- public:
- CSlaveDebugContext(IRoxieSlaveContext *_ctx, const IRoxieContextLogger &_logctx, RoxiePacketHeader &_header)
- : CBaseDebugContext(_logctx), header(_header), logctx(_logctx)
- {
- channel = header.channel;
- debugSequence = 0;
- parentActivity = 0;
- }
- void init(const IRoxieQueryPacket *_packet)
- {
- unsigned traceLength = _packet->getTraceLength();
- assertex(traceLength);
- const byte *traceInfo = _packet->queryTraceInfo();
- assertex((*traceInfo & LOGGING_DEBUGGERACTIVE) != 0);
- unsigned debugLen = *(unsigned short *) (traceInfo + 1);
- MemoryBuffer b;
- b.setBuffer(debugLen, (char *) (traceInfo + 1 + sizeof(unsigned short)), false);
- deserialize(b);
- __uint64 tmp; // can't serialize memsize_t
- b.read(tmp); // note - this is written by the RemoteAdaptor not by the serialize....
- parentActivity = (memsize_t)tmp;
- }
- virtual unsigned queryChannel() const
- {
- return channel;
- }
- virtual BreakpointActionMode checkBreakpoint(DebugState state, IActivityDebugContext *probe, const void *extra)
- {
- return CBaseDebugContext::checkBreakpoint(state, probe, extra);
- }
- virtual void waitForDebugger(DebugState state, IActivityDebugContext *probe)
- {
- StringBuffer debugIdString;
- CriticalBlock b(crit); // Make sure send sequentially - don't know if this is strictly needed...
- debugSequence++;
- debugIdString.appendf(".debug.%x", debugSequence);
- IPendingCallback *callback = ROQ->notePendingCallback(header, debugIdString.str()); // note that we register before the send to avoid a race.
- try
- {
- RoxiePacketHeader newHeader(header, ROXIE_DEBUGCALLBACK);
- loop // retry indefinately, as more than likely Roxie server is waiting for user input ...
- {
- Owned<IMessagePacker> output = ROQ->createOutputStream(newHeader, true, logctx);
- // These are deserialized in onDebugCallback..
- MemoryBuffer debugInfo;
- debugInfo.append(debugSequence);
- debugInfo.append((char) state);
- if (state==DebugStateGraphFinished)
- {
- debugInfo.append(globalCounts.count());
- HashIterator edges(globalCounts);
- ForEach(edges)
- {
- IGlobalEdgeRecord *edge = globalCounts.mapToValue(&edges.query());
- debugInfo.append((const char *) edges.query().getKey());
- debugInfo.append(edge->queryCount());
- }
- }
- debugInfo.append(currentBreakpointUID);
- debugInfo.append((__uint64)parentActivity); // can't serialize memsize_t
- debugInfo.append(channel);
- assertex (currentGraph); // otherwise what am I remote debugging?
- currentGraph->serializeProxyGraphs(debugInfo);
- debugInfo.append(probe ? probe->queryEdgeId() : "");
- char *buf = (char *) output->getBuffer(debugInfo.length(), true);
- memcpy(buf, debugInfo.toByteArray(), debugInfo.length());
- output->putBuffer(buf, debugInfo.length(), true);
- output->flush(true);
- output.clear();
- if (callback->wait(5000))
- break;
- }
- if (traceLevel > 6)
- { StringBuffer s; DBGLOG("Processing information from Roxie server in response to %s", newHeader.toString(s).str()); }
- MemoryBuffer &serverData = callback->queryData();
- deserialize(serverData);
- }
- catch (...)
- {
- ROQ->removePendingCallback(callback);
- throw;
- }
- ROQ->removePendingCallback(callback);
- }
- virtual IRoxieQueryPacket *onDebugCallback(const RoxiePacketHeader &header, size32_t len, char *data)
- {
- // MORE - Implies a server -> slave child -> slave grandchild type situation - need to pass call on to Roxie server (rather as I do for file callback)
- UNIMPLEMENTED;
- }
- virtual bool onDebuggerTimeout()
- {
- throwUnexpected();
- }
- virtual void debugCounts(IXmlWriter *output, unsigned sinceSequence, bool reset)
- {
- // This gives info for the global view - accumulated counts for all instances, plus the graph as fetched from the workunit
- HashIterator edges(globalCounts);
- ForEach(edges)
- {
- IGlobalEdgeRecord *edge = globalCounts.mapToValue(&edges.query());
- if (edge->queryLastSequence() && (!sinceSequence || edge->queryLastSequence() > sinceSequence))
- {
- output->outputBeginNested("edge", true);
- output->outputCString((const char *) edges.query().getKey(), "@edgeId");
- output->outputUInt(edge->queryCount(), "@count");
- output->outputEndNested("edge");
- }
- if (reset)
- edge->reset();
- }
- }
- };
- class CSlaveContext : public CInterface, implements IRoxieSlaveContext, implements ICodeContext, implements roxiemem::ITimeLimiter, implements roxiemem::IRowAllocatorCache
- {
- protected:
- mutable IArrayOf<IEngineRowAllocator> allAllocators;
- mutable SpinLock allAllocatorsLock;
- Owned<IRowManager> rowManager; // NOTE: the order of destruction here is significant. For leak check to work destroy this BEFORE allAllocators, but after most other things
- Owned <IDebuggableContext> debugContext;
- const IQueryFactory *factory;
- Owned<IProbeManager> probeManager; // must be destroyed after childGraphs
- MapXToMyClass<unsigned, unsigned, IActivityGraph> childGraphs;
- Owned<IActivityGraph> graph;
- unsigned priority;
- StringBuffer authToken;
- Owned<IPropertyTree> probeQuery;
- RoxiePacketHeader *header;
- unsigned lastWuAbortCheck;
- unsigned startTime;
- unsigned timeLimit;
- unsigned totSlavesReplyLen;
- unsigned ctxParallelJoinPreload;
- unsigned ctxFullKeyedJoinPreload;
- unsigned ctxKeyedJoinPreload;
- unsigned ctxConcatPreload;
- unsigned ctxFetchPreload;
- unsigned ctxPrefetchProjectPreload;
- bool traceActivityTimes;
- Owned<IConstWorkUnit> workUnit;
- Owned<IRoxieDaliHelper> daliHelperLink;
- CriticalSection statsCrit;
- const IRoxieContextLogger &logctx;
- protected:
- CriticalSection resultsCrit;
- PointerIArrayOf<FlushingStringBuffer> resultMap;
- bool exceptionLogged;
- bool aborted;
- CriticalSection abortLock; // NOTE: we don't bother to get lock when just reading to see whether to abort
- Owned<IException> exception;
- static void _toXML(IPropertyTree *tree, StringBuffer &xgmml, unsigned indent)
- {
- if (tree->getPropInt("att[@name='_roxieStarted']/@value", 1) == 0)
- return;
- if (0 && tree->getPropInt("att[@name='_kind']/@value", 0) == 496)
- {
- Owned<IPropertyTreeIterator> sub = tree->getElements(".//att[@name='_roxieStarted']");
- bool foundOne = false;
- ForEach(*sub)
- {
- if (sub->query().getPropInt("@value", 1)==0)
- {
- foundOne = true;
- break;
- }
- }
- if (!foundOne)
- return;
- }
- const char *name = tree->queryName();
- xgmml.pad(indent).append('<').append(name);
- Owned<IAttributeIterator> it = tree->getAttributes(true);
- if (it->first())
- {
- do
- {
- const char *key = it->queryName();
- xgmml.append(' ').append(key+1).append("=\"");
- encodeXML(it->queryValue(), xgmml, ENCODE_NEWLINES);
- xgmml.append("\"");
- }
- while (it->next());
- }
- Owned<IPropertyTreeIterator> sub = tree->getElements("*", iptiter_sort);
- if (!sub->first())
- {
- xgmml.append("/>\n");
- }
- else
- {
- xgmml.append(">\n");
- for(; sub->isValid(); sub->next())
- _toXML(&sub->query(), xgmml, indent+1);
- xgmml.pad(indent).append("</").append(name).append(">\n");
- }
- }
- public:
- IMPLEMENT_IINTERFACE;
- CSlaveContext(const IQueryFactory *_factory, const IRoxieContextLogger &_logctx, unsigned _timeLimit, memsize_t _memoryLimit, IRoxieQueryPacket *_packet, bool _traceActivityTimes, bool _debuggerActive)
- : factory(_factory), logctx(_logctx)
- {
- if (_packet)
- header = &_packet->queryHeader();
- else
- header = NULL;
- timeLimit = _timeLimit;
- startTime = lastWuAbortCheck = msTick();
- ctxParallelJoinPreload = 0;
- ctxFullKeyedJoinPreload = 0;
- ctxKeyedJoinPreload = 0;
- ctxConcatPreload = 0;
- ctxFetchPreload = 0;
- ctxPrefetchProjectPreload = 0;
- traceActivityTimes = _traceActivityTimes;
- if (_debuggerActive)
- {
- CSlaveDebugContext *slaveDebugContext = new CSlaveDebugContext(this, logctx, *header);
- slaveDebugContext->init(_packet);
- debugContext.setown(slaveDebugContext);
- probeManager.setown(createDebugManager(debugContext, "slaveDebugger"));
- }
- aborted = false;
- rowManager.setown(roxiemem::createRowManager(_memoryLimit, this, logctx, this, false));
- //MORE: If checking heap required then should have
- //rowManager.setown(createCheckingHeap(rowManager)) or something similar.
- }
- // interface IRoxieServerContext
- virtual void noteStatistic(unsigned statCode, unsigned __int64 value, unsigned count) const
- {
- logctx.noteStatistic(statCode, value, count);
- }
- virtual void CTXLOG(const char *format, ...) const
- {
- va_list args;
- va_start(args, format);
- logctx.CTXLOGva(format, args);
- va_end(args);
- }
- virtual void CTXLOGva(const char *format, va_list args) const
- {
- logctx.CTXLOGva(format, args);
- }
- virtual void CTXLOGa(TracingCategory category, const char *prefix, const char *text) const
- {
- logctx.CTXLOGa(category, prefix, text);
- }
- virtual void logOperatorException(IException *E, const char *file, unsigned line, const char *format, ...) const
- {
- va_list args;
- va_start(args, format);
- logctx.logOperatorExceptionVA(E, file, line, format, args);
- va_end(args);
- }
- virtual void logOperatorExceptionVA(IException *E, const char *file, unsigned line, const char *format, va_list args) const
- {
- logctx.logOperatorExceptionVA(E, file, line, format, args);
- }
- virtual void CTXLOGae(IException *E, const char *file, unsigned line, const char *prefix, const char *format, ...) const
- {
- va_list args;
- va_start(args, format);
- logctx.CTXLOGaeva(E, file, line, prefix, format, args);
- va_end(args);
- }
- virtual void CTXLOGaeva(IException *E, const char *file, unsigned line, const char *prefix, const char *format, va_list args) const
- {
- logctx.CTXLOGaeva(E, file, line, prefix, format, args);
- }
- virtual void CTXLOGl(LogItem *log) const
- {
- if (log->isStatistics())
- {
- logctx.noteStatistic(log->getStatCode(), log->getStatValue(), (unsigned)log->getStatCount());
- log->Release();
- }
- else
- logctx.CTXLOGl(log);
- }
- virtual unsigned logString(const char *text) const
- {
- if (text && *text)
- {
- CTXLOG("USER: %s", text);
- return strlen(text);
- }
- else
- return 0;
- }
- virtual const IContextLogger &queryContextLogger() const
- {
- return logctx;
- }
- virtual StringBuffer &getLogPrefix(StringBuffer &ret) const
- {
- return logctx.getLogPrefix(ret);
- }
- virtual bool isIntercepted() const
- {
- return logctx.isIntercepted();
- }
- virtual bool isBlind() const
- {
- return logctx.isBlind();
- }
- virtual unsigned queryTraceLevel() const
- {
- return logctx.queryTraceLevel();
- }
- virtual void noteProcessed(const IRoxieContextLogger &activityContext, const IRoxieServerActivity *activity, unsigned _idx, unsigned _processed, unsigned __int64 _totalCycles, unsigned __int64 _localCycles) const
- {
- if (traceActivityTimes)
- {
- StringBuffer text, prefix;
- text.appendf("%s outputIdx %d processed %d total %d us local %d us",
- getActivityText(activity->getKind()), _idx, _processed, (unsigned) (cycle_to_nanosec(_totalCycles)/1000), (unsigned)(cycle_to_nanosec(_localCycles)/1000));
- activityContext.getLogPrefix(prefix);
- CTXLOGa(LOG_TIMING, prefix.str(), text.str());
- }
- if (graphProgress)
- {
- IPropertyTree& nodeProgress = graphProgress->updateNode(activity->querySubgraphId(), activity->queryId());
- nodeProgress.setPropInt64("@totalTime", (unsigned) (cycle_to_nanosec(_totalCycles)/1000));
- nodeProgress.setPropInt64("@localTime", (unsigned) (cycle_to_nanosec(_localCycles)/1000));
- if (_processed)
- {
- StringBuffer edgeId;
- edgeId.append(activity->queryId()).append('_').append(_idx);
- IPropertyTree& edgeProgress = graphProgress->updateEdge(activity->querySubgraphId(), edgeId.str());
- edgeProgress.setPropInt64("@count", _processed);
- }
- }
- }
- virtual bool queryTimeActivities() const
- {
- return traceActivityTimes;
- }
- virtual void checkAbort()
- {
- // MORE - really should try to apply limits at slave end too
- #ifdef __linux__
- if (linuxYield)
- sched_yield();
- #endif
- #ifdef _DEBUG
- if (shuttingDown)
- throw MakeStringException(ROXIE_FORCE_SHUTDOWN, "Roxie is shutting down");
- #endif
- if (aborted) // NOTE - don't bother getting lock before reading this (for speed) - a false read is very unlikely and not a problem
- {
- CriticalBlock b(abortLock);
- if (!exception)
- exception.setown(MakeStringException(ROXIE_INTERNAL_ERROR, "Query was aborted"));
- throw exception.getLink();
- }
- if (graph)
- graph->checkAbort();
- if (timeLimit && (msTick() - startTime > timeLimit))
- {
- unsigned oldLimit = timeLimit;
- //timeLimit = 0; // to prevent exceptions in cleanup - this means only one arm gets stopped!
- CriticalBlock b(abortLock);
- IException *E = MakeStringException(ROXIE_TIMEOUT, "Query %s exceeded time limit (%d ms) - terminated", factory->queryQueryName(), oldLimit);
- if (!exceptionLogged)
- {
- logOperatorException(E, NULL, 0, NULL);
- exceptionLogged = true;
- }
- throw E;
- }
- if (workUnit && (msTick() - lastWuAbortCheck > 5000))
- {
- CriticalBlock b(abortLock);
- if (workUnit->aborting())
- {
- if (!exception)
- exception.setown(MakeStringException(ROXIE_INTERNAL_ERROR, "Query was aborted"));
- throw exception.getLink();
- }
- lastWuAbortCheck = msTick();
- }
- }
-
- virtual void notifyAbort(IException *E)
- {
- CriticalBlock b(abortLock);
- if (!aborted && QUERYINTERFACE(E, InterruptedSemaphoreException) == NULL)
- {
- aborted = true;
- exception.set(E);
- setWUState(WUStateAborting);
- }
- }
- virtual void setWUState(WUState state)
- {
- if (workUnit)
- {
- WorkunitUpdate w(&workUnit->lock());
- w->setState(state);
- }
- }
- virtual bool checkWuAborted()
- {
- return workUnit && workUnit->aborting();
- }
- virtual unsigned parallelJoinPreload()
- {
- return ctxParallelJoinPreload;
- }
- virtual unsigned concatPreload()
- {
- return ctxConcatPreload;
- }
- virtual unsigned fetchPreload()
- {
- return ctxFetchPreload;
- }
- virtual unsigned fullKeyedJoinPreload()
- {
- return ctxFullKeyedJoinPreload;
- }
- virtual unsigned keyedJoinPreload()
- {
- return ctxKeyedJoinPreload;
- }
- virtual unsigned prefetchProjectPreload()
- {
- return ctxPrefetchProjectPreload;
- }
- const char *queryAuthToken()
- {
- return authToken.str();
- }
- virtual void noteChildGraph(unsigned id, IActivityGraph *childGraph)
- {
- if (queryTraceLevel() > 10)
- CTXLOG("CSlaveContext %p noteChildGraph %d=%p", this, id, childGraph);
- childGraphs.setValue(id, childGraph);
- }
-
- virtual IActivityGraph *getLibraryGraph(const LibraryCallFactoryExtra &extra, IRoxieServerActivity *parentActivity)
- {
- if (extra.embedded)
- {
- return factory->lookupGraph(extra.libraryName, probeManager, *this, parentActivity);
- }
- else
- {
- Owned<IQueryFactory> libraryQuery = globalPackageSetManager->lookupLibrary(extra.libraryName, extra.interfaceHash, *this);
- return libraryQuery->lookupGraph("graph1", probeManager, *this, parentActivity);
- }
- }
- void beginGraph(const char *graphName)
- {
- if (debugContext)
- {
- probeManager.clear(); // Hack!
- probeManager.setown(createDebugManager(debugContext, graphName));
- debugContext->checkBreakpoint(DebugStateGraphCreate, NULL, graphName);
- }
- else if (probeAllRows || probeQuery != NULL)
- probeManager.setown(createProbeManager());
- graph.setown(factory->lookupGraph(graphName, probeManager, *this, NULL));
- graph->onCreate(this, NULL); // MORE - is that right
- if (debugContext)
- debugContext->checkBreakpoint(DebugStateGraphStart, NULL, graphName);
- }
- Owned<IWUGraphProgress> graphProgress; // could make local to endGraph and pass to reset - might be cleaner
- void endGraph(bool aborting)
- {
- if (graph)
- {
- if (debugContext)
- debugContext->checkBreakpoint(aborting ? DebugStateGraphAbort : DebugStateGraphEnd, NULL, graph->queryName());
- if (aborting)
- graph->abort();
- WorkunitUpdate progressWorkUnit(NULL);
- Owned<IConstWUGraphProgress> progress;
- if (workUnit)
- {
- progressWorkUnit.setown(&workUnit->lock());
- progress.setown(progressWorkUnit->getGraphProgress(graph->queryName()));
- graphProgress.setown(progress->update());
- }
- graph->reset();
- if (graphProgress)
- {
- graphProgress.clear();
- progress.clear();
- }
- graph.clear();
- childGraphs.kill();
- }
- }
- void runGraph()
- {
- try
- {
- graph->execute();
- if (probeQuery)
- graph->getProbeResponse(probeQuery);
- }
- catch(...)
- {
- if (probeQuery)
- graph->getProbeResponse(probeQuery);
- throw;
- }
- }
- virtual void executeGraph(const char * name, bool realThor, size32_t parentExtractSize, const void * parentExtract)
- {
- assertex(parentExtractSize == 0);
- if (queryTraceLevel() > 8)
- CTXLOG("Executing graph %s", name);
- assertex(!realThor);
- bool created = false;
- try
- {
- beginGraph(name);
- created = true;
- runGraph();
- }
- catch (IException *e)
- {
- if (e->errorAudience() == MSGAUD_operator)
- EXCLOG(e, "Exception thrown in query - cleaning up"); // if an IException is throw let EXCLOG determine if a trap should be generated
- else
- {
- StringBuffer s;
- CTXLOG("Exception thrown in query - cleaning up: %d: %s", e->errorCode(), e->errorMessage(s).str());
- }
- if (created)
- endGraph(true);
- CTXLOG("Done cleaning up");
- throw;
- }
- catch (...)
- {
- CTXLOG("Exception thrown in query - cleaning up");
- if (created)
- endGraph(true);
- CTXLOG("Done cleaning up");
- throw;
- }
- endGraph(false);
- }
- virtual IActivityGraph * queryChildGraph(unsigned id)
- {
- if (queryTraceLevel() > 10)
- CTXLOG("CSlaveContext %p resolveChildGraph %d", this, id);
- IActivityGraph *childGraph = childGraphs.getValue(id);
- assertex(childGraph);
- return childGraph;
- }
- virtual IThorChildGraph * resolveChildQuery(__int64 activityId, IHThorArg * colocal)
- {
- // NOTE - part of ICodeContext interface
- return LINK(queryChildGraph((unsigned) activityId)->queryChildGraph());
- }
- virtual ILocalGraph * resolveLocalQuery(__int64 id)
- {
- return queryChildGraph((unsigned) id)->queryLocalGraph();
- }
- virtual IRowManager &queryRowManager()
- {
- return *rowManager;
- }
- virtual void addSlavesReplyLen(unsigned len)
- {
- CriticalBlock b(statsCrit); // MORE: change to atomic_add, or may not need it at all?
- totSlavesReplyLen += len;
- }
-
- virtual __int64 countIndex(__int64 activityId, IHThorCountIndexArg & arg)
- {
- throwUnexpected();
- }
- virtual __int64 countDiskFile(__int64 activityId, IHThorCountFileArg & arg)
- {
- Owned<IHThorCountFileArg> a = &arg; // to make sure it gets released when I am done....
- Owned<IRoxieServerActivityFactory> f = factory->getRoxieServerActivityFactory((unsigned) activityId);
- if (f)
- {
- Owned<IRoxieServerActivity> fa = f->createFunction(*a.getClear(), NULL);
- fa->onCreate(this, NULL);
- fa->start(0, NULL, false);
- __int64 ret = fa->evaluate();
- fa->stop(false);
- fa->reset();
- return ret;
- }
- throwUnexpected();
- }
- virtual const char *loadResource(unsigned id)
- {
- ILoadedDllEntry *dll = factory->queryDll();
- return (const char *) dll->getResource(id);
- }
- virtual const IResolvedFile *resolveLFN(const char *filename, bool isOpt)
- {
- CDateTime cacheDate; // Note - this is empty meaning we don't know...
- return querySlaveDynamicFileCache()->lookupDynamicFile(*this, filename, cacheDate, header, isOpt, false);
- }
- virtual IRoxieWriteHandler *createLFN(const char *filename, bool overwrite, bool extend, const StringArray &clusters)
- {
- throwUnexpected(); // only support writing on the server
- }
- virtual void onFileCallback(const RoxiePacketHeader &header, const char *lfn, bool isOpt, bool isLocal)
- {
- // On a slave, we need to request info using our own header (not the one passed in) and need to get global rather than just local info
- // (possibly we could get just local if the channel matches but not sure there is any point)
- Owned<const IResolvedFile> dFile = resolveLFN(lfn, isOpt);
- if (dFile)
- {
- MemoryBuffer mb;
- mb.append(sizeof(RoxiePacketHeader), &header);
- mb.append(lfn);
- dFile->serializePartial(mb, header.channel, isLocal);
- ((RoxiePacketHeader *) mb.toByteArray())->activityId = ROXIE_FILECALLBACK;
- Owned<IRoxieQueryPacket> reply = createRoxiePacket(mb);
- reply->queryHeader().retries = 0;
- ROQ->sendPacket(reply, *this); // MORE - the caller's log context might be better? Should we unicast? Note that this does not release the packet
- return;
- }
- ROQ->sendAbortCallback(header, lfn, *this);
- throwUnexpected();
- }
- virtual ICodeContext *queryCodeContext()
- {
- return this;
- }
- virtual IRoxieServerContext *queryServerContext()
- {
- return NULL;
- }
- virtual IProbeManager *queryProbeManager() const
- {
- return probeManager;
- }
- virtual IDebuggableContext *queryDebugContext() const
- {
- return debugContext;
- }
- virtual char *getOS()
- {
- #ifdef _WIN32
- return strdup("windows");
- #else
- return strdup("linux");
- #endif
- }
- virtual const void * fromXml(IEngineRowAllocator * rowAllocator, size32_t len, const char * utf8, IXmlToRowTransformer * xmlTransformer, bool stripWhitespace)
- {
- return createRowFromXml(rowAllocator, len, utf8, xmlTransformer, stripWhitespace);
- }
- // The following from ICodeContext should never be executed in slave activity. If we are on Roxie server (or in child query on slave), they will be implemented by more derived CRoxieServerContext class
- virtual void setResultBool(const char *name, unsigned sequence, bool value) { throwUnexpected(); }
- virtual void setResultData(const char *name, unsigned sequence, int len, const void * data) { throwUnexpected(); }
- virtual void setResultDecimal(const char * stepname, unsigned sequence, int len, int precision, bool isSigned, const void *val) { throwUnexpected(); }
- virtual void setResultInt(const char *name, unsigned sequence, __int64 value) { throwUnexpected(); }
- virtual void setResultRaw(const char *name, unsigned sequence, int len, const void * data) { throwUnexpected(); }
- virtual void setResultReal(const char * stepname, unsigned sequence, double value) { throwUnexpected(); }
- virtual void setResultSet(const char *name, unsigned sequence, bool isAll, size32_t len, const void * data, ISetToXmlTransformer * transformer) { throwUnexpected(); }
- virtual void setResultString(const char *name, unsigned sequence, int len, const char * str) { throwUnexpected(); }
- virtual void setResultUInt(const char *name, unsigned sequence, unsigned __int64 value) { throwUnexpected(); }
- virtual void setResultUnicode(const char *name, unsigned sequence, int len, UChar const * str) { throwUnexpected(); }
- virtual void setResultVarString(const char * name, unsigned sequence, const char * value) { throwUnexpected(); }
- virtual void setResultVarUnicode(const char * name, unsigned sequence, UChar const * value) { throwUnexpected(); }
- virtual bool getResultBool(const char * name, unsigned sequence) { throwUnexpected(); }
- virtual void getResultData(unsigned & tlen, void * & tgt, const char * name, unsigned sequence) { throwUnexpected(); }
- virtual void getResultDecimal(unsigned tlen, int precision, bool isSigned, void * tgt, const char * stepname, unsigned sequence) { throwUnexpected(); }
- virtual void getResultRaw(unsigned & tlen, void * & tgt, const char * name, unsigned sequence, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer) { throwUnexpected(); }
- virtual void getResultSet(bool & isAll, size32_t & tlen, void * & tgt, const char * name, unsigned sequence, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer) { throwUnexpected(); }
- virtual __int64 getResultInt(const char * name, unsigned sequence) { throwUnexpected(); }
- virtual double getResultReal(const char * name, unsigned sequence) { throwUnexpected(); }
- virtual void getResultString(unsigned & tlen, char * & tgt, const char * name, unsigned sequence) { throwUnexpected(); }
- virtual void getResultStringF(unsigned tlen, char * tgt, const char * name, unsigned sequence) { throwUnexpected(); }
- virtual void getResultUnicode(unsigned & tlen, UChar * & tgt, const char * name, unsigned sequence) { throwUnexpected(); }
- virtual char *getResultVarString(const char * name, unsigned sequence) { throwUnexpected(); }
- virtual UChar *getResultVarUnicode(const char * name, unsigned sequence) { throwUnexpected(); }
- virtual unsigned getResultHash(const char * name, unsigned sequence) { throwUnexpected(); }
- virtual void getResultRowset(size32_t & tcount, byte * * & tgt, const char * name, unsigned sequence, IEngineRowAllocator * _rowAllocator, IOutputRowDeserializer * deserializer, bool isGrouped, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer) { throwUnexpected(); }
- virtual void printResults(IXmlWriter *output, const char *name, unsigned sequence) { throwUnexpected(); }
- virtual char *getWuid() { throwUnexpected(); }
- virtual void getExternalResultRaw(unsigned & tlen, void * & tgt, const char * wuid, const char * stepname, unsigned sequence, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer) { throwUnexpected(); }
- virtual char *getDaliServers() { throwUnexpected(); }
- virtual __int64 countDiskFile(const char * lfn, unsigned recordSize) { throwUnexpected(); }
- virtual char * getExpandLogicalName(const char * logicalName) { throwUnexpected(); }
- virtual void addWuException(const char * text, unsigned code, unsigned severity) { throwUnexpected(); }
- virtual void addWuAssertFailure(unsigned code, const char * text, const char * filename, unsigned lineno, unsigned column, bool isAbort) { throwUnexpected(); }
- virtual IUserDescriptor *queryUserDescriptor() { throwUnexpected(); }
- virtual unsigned __int64 getDatasetHash(const char * name, unsigned __int64 hash) { throwUnexpected(); }
- virtual unsigned getRecoveringCount() { throwUnexpected(); }
- virtual unsigned getNodes() { throwUnexpected(); }
- virtual unsigned getNodeNum() { throwUnexpected(); }
- virtual char *getFilePart(const char *logicalPart, bool create=false) { throwUnexpected(); }
- virtual unsigned __int64 getFileOffset(const char *logicalPart) { throwUnexpected(); }
- virtual IDistributedFileTransaction *querySuperFileTransaction() { throwUnexpected(); }
- virtual char *getJobName() { throwUnexpected(); }
- virtual char *getJobOwner() { throwUnexpected(); }
- virtual char *getClusterName() { throwUnexpected(); }
- virtual char *getGroupName() { throwUnexpected(); }
- virtual char * queryIndexMetaData(char const * lfn, char const * xpath) { throwUnexpected(); }
- virtual unsigned getPriority() const { throwUnexpected(); }
- virtual char *getPlatform() { throwUnexpected(); }
- virtual char *getEnv(const char *name, const char *defaultValue) const { throwUnexpected(); }
- virtual IEngineRowAllocator * getRowAllocator(IOutputMetaData * meta, unsigned activityId) const
- {
- // MORE - may need to do some caching/commoning up here otherwise GRAPH in a child query may use too many
- SpinBlock b(allAllocatorsLock);
- IEngineRowAllocator * ret = createRoxieRowAllocator(*rowManager, meta, activityId, allAllocators.ordinality(), false);
- LINK(ret);
- allAllocators.append(*ret);
- return ret;
- }
- virtual void getRowXML(size32_t & lenResult, char * & result, IOutputMetaData & info, const void * row, unsigned flags)
- {
- convertRowToXML(lenResult, result, info, row, flags);
- }
- // interface IRowAllocatorCache
- virtual unsigned getActivityId(unsigned cacheId) const
- {
- unsigned allocatorIndex = (cacheId & ALLOCATORID_MASK);
- SpinBlock b(allAllocatorsLock);
- if (allAllocators.isItem(allocatorIndex))
- return allAllocators.item(allocatorIndex).queryActivityId();
- else
- {
- //assert(false);
- return 12345678; // Used for tracing, better than a crash...
- }
- }
- virtual StringBuffer &getActivityDescriptor(unsigned cacheId, StringBuffer &out) const
- {
- unsigned allocatorIndex = (cacheId & ALLOCATORID_MASK);
- SpinBlock b(allAllocatorsLock);
- if (allAllocators.isItem(allocatorIndex))
- return allAllocators.item(allocatorIndex).getId(out);
- else
- {
- assert(false);
- return out.append("unknown"); // Used for tracing, better than a crash...
- }
- }
- virtual void onDestroy(unsigned cacheId, void *row) const
- {
- IEngineRowAllocator *allocator;
- unsigned allocatorIndex = (cacheId & ALLOCATORID_MASK);
- {
- SpinBlock b(allAllocatorsLock); // just protect the access to the array - don't keep locked for the call of destruct or may deadlock
- if (allAllocators.isItem(allocatorIndex))
- allocator = &allAllocators.item(allocatorIndex);
- else
- {
- assert(false);
- return;
- }
- }
- if (!RoxieRowCheckValid(cacheId, row))
- {
- //MORE: Give an error, but don't throw an exception!
- }
- allocator->queryOutputMeta()->destruct((byte *) row);
- }
- virtual void checkValid(unsigned cacheId, const void *row) const
- {
- if (!RoxieRowCheckValid(cacheId, row))
- {
- //MORE: Throw an exception?
- }
- }
- virtual IWorkUnit *updateWorkUnit() const
- {
- if (workUnit)
- return &workUnit->lock();
- else
- return NULL;
- }
- virtual IConstWorkUnit *queryWorkUnit() const
- {
- return workUnit;
- }
- };
- IRoxieSlaveContext *createSlaveContext(const IQueryFactory *_factory, const SlaveContextLogger &_logctx, unsigned _timeLimit, memsize_t _memoryLimit, IRoxieQueryPacket *packet)
- {
- return new CSlaveContext(_factory, _logctx, _timeLimit, _memoryLimit, packet, _logctx.queryTraceActivityTimes(), _logctx.queryDebuggerActive());
- }
- class CRoxieServerDebugContext : extends CBaseServerDebugContext
- {
- // Some questions:
- // 1. Do we let all threads go even when say step? Probably... (may allow a thread to be suspended at some point)
- // 2. Doesn't that then make a bit of a mockery of step (when there are multiple threads active)... I _think_ it actually means we DON'T try to wait for all
- // threads to hit a stop, but allow any that hit stop while we are paused to be queued up to be returned by step.... perhaps actually stop them in critsec rather than
- // semaphore and it all becomes easier to code... Anything calling checkBreakPoint while program state is "in debugger" will block on that critSec.
- // 3. I think we need to recheck breakpoints on Roxie server but just check not deleted
- public:
- IRoxieSlaveContext *ctx;
- CRoxieServerDebugContext(IRoxieSlaveContext *_ctx, const IContextLogger &_logctx, IPropertyTree *_queryXGMML, SafeSocket &_client)
- : CBaseServerDebugContext(_logctx, _queryXGMML, _client), ctx(_ctx)
- {
- }
- void debugCounts(IXmlWriter *output, unsigned sinceSequence, bool reset)
- {
- CriticalBlock b(debugCrit);
- if (running)
- throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
- if (currentGraph)
- currentGraph->mergeRemoteCounts(this);
- CBaseServerDebugContext::debugCounts(output, sinceSequence, reset);
- }
- virtual void waitForDebugger(DebugState state, IActivityDebugContext *probe)
- {
- ctx->setWUState(WUStateDebugPaused);
- CBaseServerDebugContext::waitForDebugger(state, probe);
- ctx->setWUState(WUStateDebugRunning);
- }
- virtual bool onDebuggerTimeout()
- {
- return ctx->checkWuAborted();
- }
- virtual void debugInitialize(const char *id, const char *_queryName, bool _breakAtStart)
- {
- CBaseServerDebugContext::debugInitialize(id, _queryName, _breakAtStart);
- Owned<IRoxieDebugSessionManager> QM = globalPackageSetManager->getRoxieDebugSessionManager();
- QM->registerDebugId(id, this);
- }
- virtual void debugTerminate()
- {
- CriticalBlock b(debugCrit);
- assertex(running);
- currentState = DebugStateUnloaded;
- running = false;
- Owned<IRoxieDebugSessionManager> QM = globalPackageSetManager->getRoxieDebugSessionManager();
- QM->deregisterDebugId(debugId);
- if (debuggerActive)
- {
- debuggerSem.signal(debuggerActive);
- debuggerActive = 0;
- }
- }
- virtual IRoxieQueryPacket *onDebugCallback(const RoxiePacketHeader &header, size32_t len, char *data)
- {
- MemoryBuffer slaveInfo;
- slaveInfo.setBuffer(len, data, false);
- unsigned debugSequence;
- slaveInfo.read(debugSequence);
- {
- CriticalBlock b(breakCrit); // we want to wait until it's our turn before updating the graph info or the counts get ahead of the current row and life is confusing
- char slaveStateChar;
- slaveInfo.read(slaveStateChar);
- DebugState slaveState = (DebugState) slaveStateChar;
- if (slaveState==DebugStateGraphFinished)
- {
- unsigned numCounts;
- slaveInfo.read(numCounts);
- while (numCounts)
- {
- StringAttr edgeId;
- unsigned edgeCount;
- slaveInfo.read(edgeId);
- slaveInfo.read(edgeCount);
- Owned<IGlobalEdgeRecord> thisEdge = getEdgeRecord(edgeId);
- thisEdge->incrementCount(edgeCount, sequence);
- numCounts--;
- }
- }
- slaveInfo.read(currentBreakpointUID);
- memsize_t slaveActivity;
- unsigned channel;
- __uint64 tmp;
- slaveInfo.read(tmp);
- slaveActivity = (memsize_t)tmp;
- slaveInfo.read(channel);
- assertex(currentGraph);
- currentGraph->deserializeProxyGraphs(slaveState, slaveInfo, (IActivityBase *) slaveActivity, channel);
- if (slaveState != DebugStateGraphFinished) // MORE - this is debatable - may (at least sometimes) want a child graph finished to be a notified event...
- {
- StringBuffer slaveActivityId;
- slaveInfo.read(slaveActivityId);
- IActivityDebugContext *slaveActivityCtx = slaveActivityId.length() ? currentGraph->lookupActivityByEdgeId(slaveActivityId.str()) : NULL;
- BreakpointActionMode action = checkBreakpoint(slaveState, slaveActivityCtx , NULL);
- }
- }
- MemoryBuffer mb;
- mb.append(sizeof(RoxiePacketHeader), &header);
- StringBuffer debugIdString;
- debugIdString.appendf(".debug.%x", debugSequence);
- mb.append(debugIdString.str());
- serialize(mb);
- Owned<IRoxieQueryPacket> reply = createRoxiePacket(mb);
- reply->queryHeader().activityId = ROXIE_DEBUGCALLBACK;;
- reply->queryHeader().retries = 0;
- return reply.getClear();
- }
- virtual void debugPrintVariable(IXmlWriter *output, const char *name, const char *type) const
- {
- CriticalBlock b(debugCrit);
- if (running)
- throw MakeStringException(ROXIE_DEBUG_ERROR, "Command not available while query is running");
- output->outputBeginNested("Variables", true);
- if (!type || stricmp(type, "temporary"))
- {
- output->outputBeginNested("Temporary", true);
- ctx->printResults(output, name, (unsigned) -3);
- output->outputEndNested("Temporary");
- }
- if (!type || stricmp(type, "global"))
- {
- output->outputBeginNested("Global", true);
- ctx->printResults(output, name, (unsigned) -1);
- output->outputEndNested("Global");
- }
- output->outputEndNested("Variables");
- }
- };
- class DeserializedDataReader : public CInterface, implements IWorkUnitRowReader
- {
- const ConstPointerArray &data;
- unsigned idx;
- public:
- IMPLEMENT_IINTERFACE;
- DeserializedDataReader(const ConstPointerArray &_data) : data(_data)
- {
- idx = 0;
- }
- virtual const void * nextInGroup()
- {
- if (data.isItem(idx))
- {
- const void *row = data.item(idx);
- if (row)
- LinkRoxieRow(row);
- idx++;
- return row;
- }
- return NULL;
- }
- };
- class CDeserializedResultStore : public CInterface, implements IDeserializedResultStore
- {
- PointerArrayOf<ConstPointerArray> stored;
- PointerIArrayOf<IOutputMetaData> metas;
- mutable SpinLock lock;
- public:
- IMPLEMENT_IINTERFACE;
- ~CDeserializedResultStore()
- {
- ForEachItemIn(idx, stored)
- {
- ConstPointerArray *rows = stored.item(idx);
- if (rows)
- {
- ReleaseRoxieRowSet(*rows);
- delete rows;
- }
- }
- }
- virtual int addResult(ConstPointerArray *data, IOutputMetaData *meta)
- {
- SpinBlock b(lock);
- stored.append(data);
- metas.append(meta);
- return stored.ordinality()-1;
- }
- virtual int appendResult(int oldId, ConstPointerArray *data, IOutputMetaData *meta)
- {
- SpinBlock b(lock);
- ConstPointerArray *oldData = stored.item(oldId);
- ConstPointerArray *newData = new ConstPointerArray;
- ForEachItemIn(idx, *oldData)
- {
- const void * row = oldData->item(idx);
- if (row)
- LinkRoxieRow(row);
- newData->append(row);
- }
- ForEachItemIn(idx2, *data)
- {
- const void * row = data->item(idx2);
- newData->append(row); // don't link these rows, because...
- }
- delete data; // ...then we don't need to release them when we delete this array
- // Note that we don't delete the previons ConstPointerArray yet, as it could be in use.
- return addResult(newData, meta);
- }
- virtual IWorkUnitRowReader *createDeserializedReader(int id) const
- {
- return new DeserializedDataReader(*stored.item(id));
- }
- virtual void serialize(unsigned & tlen, void * & tgt, int id, ICodeContext *codectx) const
- {
- IOutputMetaData *meta = metas.item(id);
- ConstPointerArray *data = stored.item(id);
- MemoryBuffer result;
- Owned<IOutputRowSerializer> rowSerializer = meta->createRowSerializer(codectx, 0); // NOTE - we don't have a maningful activity id. Only used for error reporting.
- bool grouped = meta->isGrouped();
- ForEachItemIn(idx, *data)
- {
- const void *row = data->item(idx);
- if (grouped && idx)
- result.append(row == NULL);
- if (row)
- {
- CThorDemoRowSerializer serializerTarget(result);
- rowSerializer->serialize(serializerTarget, (const byte *) row);
- }
- }
- tlen = result.length();
- tgt= result.detach();
- }
- };
- extern IDeserializedResultStore *createDeserializedResultStore()
- {
- return new CDeserializedResultStore;
- }
- class CRoxieServerContext : public CSlaveContext, implements IRoxieServerContext, implements IGlobalCodeContext
- {
- const IQueryFactory *serverQueryFactory;
- mutable CriticalSection contextCrit;
- CriticalSection daliUpdateCrit;
- Owned<IPropertyTree> context;
- IPropertyTree *temporaries;
- CDeserializedResultStore *deserializedResultStore;
- IPropertyTree *rereadResults;
- Owned<IRoxiePackage> dynamicPackage;
-
- bool isXml;
- bool isRaw;
- bool isBlocked;
- bool isHttp;
- bool sendHeartBeats;
- bool trim;
- XmlReaderOptions xmlStoredDatasetReadFlags;
- unsigned warnTimeLimit;
- unsigned lastSocketCheckTime;
- unsigned lastHeartBeat;
- protected:
- Owned<WorkflowMachine> workflow;
- SafeSocket *client;
- void doPostProcess()
- {
- CriticalBlock b(resultsCrit); // Probably not needed
- if (!isRaw && !isBlocked)
- {
- ForEachItemIn(seq, resultMap)
- {
- FlushingStringBuffer *result = resultMap.item(seq);
- if (result)
- result->flush(true);
- }
- }
- if (probeQuery)
- {
- FlushingStringBuffer response(client, isBlocked, true, false, isHttp, *this);
-
- // create output stream
- response.startDataset("_Probe", NULL, (unsigned) -1); // initialize it
- // loop through all of the graphs and create a _Probe to output each xgmml
- Owned<IPropertyTreeIterator> graphs = probeQuery->getElements("Graph");
- ForEach(*graphs)
- {
- IPropertyTree &graph = graphs->query();
- StringBuffer xgmml;
- _toXML(&graph, xgmml, 0);
- response.append("\n");
- response.append(xgmml.str());
- }
- }
- }
- IDeserializedResultStore &useResultStore(unsigned sequence)
- {
- checkAbort();
- switch (sequence)
- {
- case ResultSequenceOnce:
- return factory->queryOnceResultStore();
- default:
- // No need to have separate stores for other temporaries...
- CriticalBlock b(contextCrit);
- if (!deserializedResultStore)
- deserializedResultStore = new CDeserializedResultStore;
- return *deserializedResultStore;
- }
- }
- IPropertyTree &useContext(unsigned sequence)
- {
- checkAbort();
- switch (sequence)
- {
- case ResultSequenceStored:
- if (context)
- return *context;
- else
- throw MakeStringException(ROXIE_CODEGEN_ERROR, "Code generation error - attempting to access stored variable on slave");
- case ResultSequencePersist:
- throwUnexpected(); // Do not expect to see in Roxie
- case ResultSequenceInternal:
- {
- CriticalBlock b(contextCrit);
- if (!temporaries)
- temporaries = createPTree();
- return *temporaries;
- }
- case ResultSequenceOnce:
- {
- return factory->queryOnceContext();
- }
- default:
- {
- CriticalBlock b(contextCrit);
- if (!rereadResults)
- rereadResults = createPTree();
- return *rereadResults;
- }
- }
- }
- void addWuException(IException *E)
- {
- if (workUnit)
- ::addWuException(workUnit, E);
- }
- void init()
- {
- client = NULL;
- totSlavesReplyLen = 0;
- temporaries = NULL;
- deserializedResultStore = NULL;
- rereadResults = NULL;
- isXml = true;
- isRaw = false;
- isBlocked = false;
- isHttp = false;
- trim = false;
- priority = 0;
- sendHeartBeats = false;
- timeLimit = serverQueryFactory->getTimeLimit();
- if (!timeLimit)
- timeLimit = defaultTimeLimit[priority];
- warnTimeLimit = serverQueryFactory->getWarnTimeLimit();
- if (!warnTimeLimit)
- warnTimeLimit = defaultWarnTimeLimit[priority];
- lastSocketCheckTime = startTime;
- lastHeartBeat = startTime;
- ctxParallelJoinPreload = defaultParallelJoinPreload;
- ctxFullKeyedJoinPreload = defaultFullKeyedJoinPreload;
- ctxKeyedJoinPreload = defaultKeyedJoinPreload;
- ctxConcatPreload = defaultConcatPreload;
- ctxFetchPreload = defaultFetchPreload;
- ctxPrefetchProjectPreload = defaultPrefetchProjectPreload;
- xmlStoredDatasetReadFlags = xr_none;
- traceActivityTimes = false;
- }
- void startWorkUnit()
- {
- WorkunitUpdate wu(&workUnit->lock());
- if (!context->getPropBool("@outputToSocket", false))
- client = NULL;
- SCMStringBuffer wuParams;
- if (workUnit->getXmlParams(wuParams).length())
- {
- // Merge in params from WU. Ones on command line take precedence though...
- Owned<IPropertyTree> wuParamTree = createPTreeFromXMLString(wuParams.str(), ipt_caseInsensitive);
- Owned<IPropertyTreeIterator> params = wuParamTree ->getElements("*");
- ForEach(*params)
- {
- IPropertyTree ¶m = params->query();
- if (!context->hasProp(param.queryName()))
- context->addPropTree(param.queryName(), LINK(¶m));
- }
- }
- if (workUnit->getDebugValueBool("Debug", false))
- {
- bool breakAtStart = workUnit->getDebugValueBool("BreakAtStart", true);
- wu->setState(WUStateDebugRunning);
- SCMStringBuffer wuid;
- initDebugMode(breakAtStart, workUnit->getWuid(wuid).str());
- }
- else
- wu->setState(WUStateRunning);
- }
- void initDebugMode(bool breakAtStart, const char *debugUID)
- {
- if (!debugPermitted || !ownEP.port)
- throw MakeStringException(ROXIE_ACCESS_ERROR, "Debug queries are not permitted on this system");
- debugContext.setown(new CRoxieServerDebugContext(this, logctx, factory->cloneQueryXGMML(), *client));
- debugContext->debugInitialize(debugUID, factory->queryQueryName(), breakAtStart);
- if (workUnit)
- {
- WorkunitUpdate wu(&workUnit->lock());
- wu->setDebugAgentListenerPort(ownEP.port); //tells debugger what port to write commands to
- StringBuffer sb;
- ownEP.getIpText(sb);
- wu->setDebugAgentListenerIP(sb); //tells debugger what IP to write commands to
- }
- timeLimit = 0;
- warnTimeLimit = 0;
- }
- public:
- IMPLEMENT_IINTERFACE;
- CRoxieServerContext(const IQueryFactory *_factory, const IRoxieContextLogger &_logctx)
- : CSlaveContext(_factory, _logctx, 0, 0, NULL, false, false), serverQueryFactory(_factory)
- {
- init();
- rowManager->setMemoryLimit(serverQueryFactory->getMemoryLimit());
- workflow.setown(_factory->createWorkflowMachine(true, logctx));
- context.setown(createPTree(ipt_caseInsensitive));
- }
- CRoxieServerContext(IConstWorkUnit *_workUnit, const IQueryFactory *_factory, const IRoxieContextLogger &_logctx)
- : CSlaveContext(_factory, _logctx, 0, 0, NULL, false, false), serverQueryFactory(_factory)
- {
- init();
- workUnit.set(_workUnit);
- rowManager->setMemoryLimit(serverQueryFactory->getMemoryLimit());
- workflow.setown(_factory->createWorkflowMachine(false, logctx));
- context.setown(createPTree(ipt_caseInsensitive));
- startWorkUnit();
- }
- CRoxieServerContext(IPropertyTree *_context, const IQueryFactory *_factory, SafeSocket &_client, bool _isXml, bool _isRaw, bool _isBlocked, HttpHelper &httpHelper, bool _trim, unsigned _priority, const IRoxieContextLogger &_logctx, XmlReaderOptions _xmlReadFlags)
- : CSlaveContext(_factory, _logctx, 0, 0, NULL, false, false), serverQueryFactory(_factory)
- {
- init();
- context.set(_context);
- client = &_client;
- isXml = _isXml;
- isRaw = _isRaw;
- isBlocked = _isBlocked;
- isHttp = httpHelper.isHttp();
- trim = _trim;
- priority = _priority;
- xmlStoredDatasetReadFlags = _xmlReadFlags;
- sendHeartBeats = enableHeartBeat && isRaw && isBlocked && priority==0;
- timeLimit = context->getPropInt("_TimeLimit", timeLimit);
- warnTimeLimit = context->getPropInt("_warnTimeLimit", warnTimeLimit);
- const char *wuid = context->queryProp("@wuid");
- if (wuid)
- {
- IRoxieDaliHelper *daliHelper = checkDaliConnection();
- assertex(daliHelper );
- workUnit.setown(daliHelper->attachWorkunit(wuid, _factory->queryDll()));
- if (!workUnit)
- throw MakeStringException(ROXIE_DALI_ERROR, "Failed to open workunit %s", wuid);
- startWorkUnit();
- }
- else if (context->getPropBool("@debug", false))
- {
- bool breakAtStart = context->getPropBool("@break", true);
- const char *debugUID = context->queryProp("@uid");
- if (debugUID && *debugUID)
- initDebugMode(breakAtStart, debugUID);
- }
- else if (context->getPropBool("_Probe", false))
- probeQuery.setown(_factory->cloneQueryXGMML());
- // MORE some of these might be appropriate in wu case too?
- rowManager->setActivityTracking(context->getPropBool("_TraceMemory", false));
- rowManager->setMemoryLimit((memsize_t) context->getPropInt64("_MemoryLimit", _factory->getMemoryLimit()));
- authToken.append(httpHelper.queryAuthToken());
- workflow.setown(_factory->createWorkflowMachine(false, logctx));
- ctxParallelJoinPreload = context->getPropInt("_ParallelJoinPreload", defaultParallelJoinPreload);
- ctxFullKeyedJoinPreload = context->getPropInt("_FullKeyedJoinPreload", defaultFullKeyedJoinPreload);
- ctxKeyedJoinPreload = context->getPropInt("_KeyedJoinPreload", defaultKeyedJoinPreload);
- ctxConcatPreload = context->getPropInt("_ConcatPreload", defaultConcatPreload);
- ctxFetchPreload = context->getPropInt("_FetchPreload", defaultFetchPreload);
- ctxPrefetchProjectPreload = context->getPropInt("_PrefetchProjectPreload", defaultPrefetchProjectPreload);
- traceActivityTimes = context->getPropBool("_TraceActivityTimes", false) || context->getPropBool("@timing", false);
- }
- ~CRoxieServerContext()
- {
- dynamicPackage.clear(); // do this before disconnect from dali
- ::Release(rereadResults);
- ::Release(temporaries);
- ::Release(deserializedResultStore);
- }
- virtual roxiemem::IRowManager &queryRowManager()
- {
- return *rowManager;
- }
- virtual IRoxieDaliHelper *checkDaliConnection()
- {
- CriticalBlock b(daliUpdateCrit);
- if (!daliHelperLink)
- daliHelperLink.setown(::connectToDali());
- return daliHelperLink;
- }
- virtual void checkAbort()
- {
- CSlaveContext::checkAbort();
- unsigned ticksNow = msTick();
- if (warnTimeLimit)
- {
- unsigned elapsed = ticksNow - startTime;
- if (elapsed > warnTimeLimit)
- {
- CriticalBlock b(abortLock);
- if (elapsed > warnTimeLimit) // we don't want critsec on the first check (for efficiency) but check again inside the critsec
- {
- logOperatorException(NULL, NULL, 0, "SLOW (%d ms): %s", elapsed, factory->queryQueryName());
- warnTimeLimit = elapsed + warnTimeLimit;
- }
- }
- }
- if (client)
- {
- if (socketCheckInterval)
- {
- if (ticksNow - lastSocketCheckTime > socketCheckInterval)
- {
- CriticalBlock b(abortLock);
- if (!client->checkConnection())
- throw MakeStringException(ROXIE_CLIENT_CLOSED, "Client socket closed");
- lastSocketCheckTime = ticksNow;
- }
- }
- if (sendHeartBeats)
- {
- unsigned hb = ticksNow - lastHeartBeat;
- if (hb > 30000)
- {
- lastHeartBeat = msTick();
- client->sendHeartBeat(*this);
- }
- }
- }
- }
- virtual unsigned getXmlFlags() const
- {
- return trim ? XWFtrim|XWFopt : XWFexpandempty;
- }
- virtual unsigned getMemoryUsage()
- {
- return rowManager->getMemoryUsage();
- }
- virtual unsigned getSlavesReplyLen()
- {
- return totSlavesReplyLen;
- }
- virtual void process()
- {
- EclProcessFactory pf = (EclProcessFactory) factory->queryDll()->getEntry("createProcess");
- Owned<IEclProcess> p = pf();
- try
- {
- if (debugContext)
- debugContext->checkBreakpoint(DebugStateReady, NULL, NULL);
- if (workflow)
- workflow->perform(this, p);
- else
- p->perform(this, 0);
- }
- catch(WorkflowException *E)
- {
- if (debugContext)
- debugContext->checkBreakpoint(DebugStateException, NULL, static_cast<IException *>(E));
- addWuException(E);
- doPostProcess();
- throw;
- }
- catch(IException *E)
- {
- if (debugContext)
- debugContext->checkBreakpoint(DebugStateException, NULL, E);
- addWuException(E);
- doPostProcess();
- throw;
- }
- catch(...)
- {
- if (debugContext)
- debugContext->checkBreakpoint(DebugStateFailed, NULL, NULL);
- if (workUnit)
- {
- Owned<IException> E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception caught in CRoxieServerContext::process");
- addWuException(E);
- }
- doPostProcess();
- throw;
- }
- if (debugContext)
- debugContext->checkBreakpoint(DebugStateFinished, NULL, NULL);
- doPostProcess();
- }
- virtual void done(bool failed)
- {
- if (debugContext)
- debugContext->debugTerminate();
- setWUState(aborted ? WUStateAborted : (failed ? WUStateFailed : WUStateCompleted));
- }
- virtual ICodeContext *queryCodeContext()
- {
- return this;
- }
- virtual IRoxieServerContext *queryServerContext()
- {
- return this;
- }
- virtual IGlobalCodeContext *queryGlobalCodeContext()
- {
- return this;
- }
- // interface ICodeContext
- virtual FlushingStringBuffer *queryResult(unsigned sequence)
- {
- if (!client && workUnit)
- return NULL; // when outputting to workunit only, don't output anything to stdout
- CriticalBlock procedure(resultsCrit);
- while (!resultMap.isItem(sequence))
- resultMap.append(NULL);
- FlushingStringBuffer *result = resultMap.item(sequence);
- if (!result)
- {
- result = new FlushingStringBuffer(client, isBlocked, isXml, isRaw, isHttp, *this);
- result->isSoap = isHttp;
- result->trim = trim;
- result->queryName.set(context->queryName());
- resultMap.replace(result, sequence);
- }
- return result;
- }
- virtual void setResultBool(const char *name, unsigned sequence, bool value)
- {
- if (isSpecialResultSequence(sequence))
- {
- CriticalBlock b(contextCrit);
- useContext(sequence).setPropBool(name, value);
- }
- else
- {
- FlushingStringBuffer *r = queryResult(sequence);
- if (r)
- {
- r->startScalar(name, sequence);
- if (isRaw)
- r->append(sizeof(value), (char *)&value);
- else
- r->append(value ? "true" : "false");
- }
- }
- if (workUnit)
- {
- try
- {
- WorkunitUpdate wu(&workUnit->lock());
- CriticalBlock b(daliUpdateCrit); // MORE - do we really need these locks?
- wu->setResultBool(name, sequence, value);
- }
- catch(IException *e)
- {
- StringBuffer text;
- e->errorMessage(text);
- CTXLOG("Error trying to update dali: %s", text.str());
- e->Release();
- }
- catch(...)
- {
- CTXLOG("Unknown exception trying to update dali");
- }
- }
- }
- virtual void setResultData(const char *name, unsigned sequence, int len, const void * data)
- {
- static char hexchar[] = "0123456789ABCDEF";
- if (isSpecialResultSequence(sequence))
- {
- StringBuffer s;
- const byte *field = (const byte *) data;
- for (int i = 0; i < len; i++)
- s.append(hexchar[field[i] >> 4]).append(hexchar[field[i] & 0x0f]);
- CriticalBlock b(contextCrit);
- IPropertyTree &ctx = useContext(sequence);
- ctx.setProp(name, s.str());
- }
- else
- {
- FlushingStringBuffer *r = queryResult(sequence);
- if (r)
- {
- r->startScalar(name, sequence);
- if (isRaw)
- r->append(len, (const char *) data);
- else
- {
- const byte *field = (const byte *) data;
- for (int i = 0; i < len; i++)
- {
- r->append(hexchar[field[i] >> 4]);
- r->append(hexchar[field[i] & 0x0f]);
- }
- }
- }
- }
- if (workUnit)
- {
- try
- {
- WorkunitUpdate wu(&workUnit->lock());
- CriticalBlock b(daliUpdateCrit);
- wu->setResultData(name, sequence, len, data);
- }
- catch(IException *e)
- {
- StringBuffer text;
- e->errorMessage(text);
- CTXLOG("Error trying to update dali: %s", text.str());
- e->Release();
- }
- catch(...)
- {
- CTXLOG("Unknown exception trying to update dali");
- }
- }
- }
- virtual void appendResultDeserialized(const char *name, unsigned sequence, ConstPointerArray *data, bool extend, IOutputMetaData *meta)
- {
- CriticalBlock b(contextCrit);
- IPropertyTree &ctx = useContext(sequence);
- IDeserializedResultStore &resultStore = useResultStore(sequence);
- IPropertyTree *val = ctx.queryPropTree(name);
- if (extend && val)
- {
- int old = val->getPropInt("@id", -1);
- assertex(old!=-1);
- val->setPropInt("@id", resultStore.appendResult(old, data, meta));
- }
- else
- {
- if (!val)
- val = ctx.addPropTree(name, createPTree());
- val->setProp("@format", "deserialized");
- val->setPropInt("@id", resultStore.addResult(data, meta));
- }
- }
- virtual void appendResultRawContext(const char *name, unsigned sequence, int len, const void * data, int numRows, bool extend, bool saveInContext)
- {
- if (saveInContext)
- {
- CriticalBlock b(contextCrit);
- IPropertyTree &ctx = useContext(sequence);
- ctx.appendPropBin(name, len, data);
- ctx.queryPropTree(name)->setProp("@format", "raw");
- }
- if (workUnit)
- {
- try
- {
- WorkunitUpdate wu(&workUnit->lock());
- CriticalBlock b(daliUpdateCrit);
- wu->setResultDataset(name, sequence, len, data, numRows, extend);
- }
- catch(IException *e)
- {
- StringBuffer text;
- e->errorMessage(text);
- CTXLOG("Error trying to update dali: %s", text.str());
- e->Release();
- }
- catch(...)
- {
- CTXLOG("Unknown exception trying to update dali");
- }
- }
- }
- virtual void setResultRaw(const char *name, unsigned sequence, int len, const void * data)
- {
- if (isSpecialResultSequence(sequence))
- {
- CriticalBlock b(contextCrit);
- IPropertyTree &ctx = useContext(sequence);
- ctx.setPropBin(name, len, data);
- ctx.queryPropTree(name)->setProp("@format", "raw");
- }
- else
- {
- FlushingStringBuffer *r = queryResult(sequence);
- if (r)
- {
- r->startScalar(name, sequence);
- if (isRaw)
- r->append(len, (const char *) data);
- else
- UNIMPLEMENTED;
- }
- }
-
- if (workUnit)
- {
- try
- {
- WorkunitUpdate wu(&workUnit->lock());
- CriticalBlock b(daliUpdateCrit);
- wu->setResultRaw(name, sequence, len, data);
- }
- catch(IException *e)
- {
- StringBuffer text;
- e->errorMessage(text);
- CTXLOG("Error trying to update dali: %s", text.str());
- e->Release();
- }
- catch(...)
- {
- CTXLOG("Unknown exception trying to update dali");
- }
- }
- }
- virtual void setResultSet(const char *name, unsigned sequence, bool isAll, size32_t len, const void * data, ISetToXmlTransformer * transformer)
- {
- if (isSpecialResultSequence(sequence))
- {
- CriticalBlock b(contextCrit);
- IPropertyTree &ctx = useContext(sequence);
- ctx.setPropBin(name, len, data);
- ctx.queryPropTree(name)->setProp("@format", "raw");
- ctx.queryPropTree(name)->setPropBool("@isAll", isAll);
- }
- else
- {
- FlushingStringBuffer *r = queryResult(sequence);
- if (r)
- {
- r->startScalar(name, sequence);
- if (isRaw)
- r->append(len, (char *)data);
- else if (isXml)
- {
- assertex(transformer);
- CommonXmlWriter xmlwrite(getXmlFlags()|XWFnoindent, 0);
- transformer->toXML(isAll, len, (byte *)data, xmlwrite);
- r->append(xmlwrite.str());
- }
- else
- {
- assertex(transformer);
- r->append('[');
- if (isAll)
- r->appendf("*]");
- else
- {
- SimpleOutputWriter x;
- transformer->toXML(isAll, len, (const byte *) data, x);
- r->appendf("%s]", x.str());
- }
- }
- }
- }
-
- if (workUnit)
- {
- try
- {
- WorkunitUpdate wu(&workUnit->lock());
- CriticalBlock b(daliUpdateCrit);
- wu->setResultSet(name, sequence, isAll, len, data, transformer);
- }
- catch(IException *e)
- {
- StringBuffer text;
- e->errorMessage(text);
- CTXLOG("Error trying to update dali: %s", text.str());
- e->Release();
- }
- catch(...)
- {
- CTXLOG("Unknown exception trying to update dali");
- }
- }
- }
- virtual void setResultXml(const char *name, unsigned sequence, const char *xml)
- {
- CriticalBlock b(contextCrit);
- useContext(sequence).setPropTree(name, createPTreeFromXMLString(xml, ipt_caseInsensitive));
- }
- virtual void setResultDecimal(const char *name, unsigned sequence, int len, int precision, bool isSigned, const void *val)
- {
- if (isSpecialResultSequence(sequence))
- {
- MemoryBuffer m;
- serializeFixedData(len, val, m);
- CriticalBlock b(contextCrit);
- useContext(sequence).setPropBin(name, m.length(), m.toByteArray());
- }
- else
- {
- FlushingStringBuffer *r = queryResult(sequence);
- if (r)
- {
- r->startScalar(name, sequence);
- if (isRaw)
- r->append(len, (char *)val);
- else
- {
- StringBuffer s;
- if (isSigned)
- outputXmlDecimal(val, len, precision, NULL, s);
- else
- outputXmlUDecimal(val, len, precision, NULL, s);
- r->append(s);
- }
- }
- }
- if (workUnit)
- {
- try
- {
- WorkunitUpdate wu(&workUnit->lock());
- CriticalBlock b(daliUpdateCrit);
- wu->setResultDecimal(name, sequence, len, precision, isSigned, val);
- }
- catch(IException *e)
- {
- StringBuffer text;
- e->errorMessage(text);
- CTXLOG("Error trying to update dali: %s", text.str());
- e->Release();
- }
- catch(...)
- {
- CTXLOG("Unknown exception trying to update dali");
- }
- }
- }
- virtual void setResultInt(const char *name, unsigned sequence, __int64 value)
- {
- if (isSpecialResultSequence(sequence))
- {
- CriticalBlock b(contextCrit);
- useContext(sequence).setPropInt64(name, value);
- }
- else
- {
- FlushingStringBuffer *r = queryResult(sequence);
- if (r)
- {
- r->startScalar(name, sequence);
- if (isRaw)
- r->append(sizeof(value), (char *)&value);
- else
- r->appendf("%"I64F"d", value);
- }
- }
- if (workUnit)
- {
- try
- {
- WorkunitUpdate wu(&workUnit->lock());
- CriticalBlock b(daliUpdateCrit);
- wu->setResultInt(name, sequence, value);
- }
- catch(IException *e)
- {
- StringBuffer text;
- e->errorMessage(text);
- CTXLOG("Error trying to update dali: %s", text.str());
- e->Release();
- }
- catch(...)
- {
- CTXLOG("Unknown exception trying to update dali");
- }
- }
- }
- virtual void setResultUInt(const char *name, unsigned sequence, unsigned __int64 value)
- {
- if (isSpecialResultSequence(sequence))
- {
- CriticalBlock b(contextCrit);
- useContext(sequence).setPropInt64(name, value);
- }
- else
- {
- FlushingStringBuffer *r = queryResult(sequence);
- if (r)
- {
- r->startScalar(name, sequence);
- if (isRaw)
- r->append(sizeof(value), (char *)&value);
- else
- r->appendf("%"I64F"u", value);
- }
- }
- if (workUnit)
- {
- try
- {
- WorkunitUpdate wu(&workUnit->lock());
- CriticalBlock b(daliUpdateCrit);
- wu->setResultUInt(name, sequence, value);
- }
- catch(IException *e)
- {
- StringBuffer text;
- e->errorMessage(text);
- CTXLOG("Error trying to update dali: %s", text.str());
- e->Release();
- }
- catch(...)
- {
- CTXLOG("Unknown exception trying to update dali");
- }
- }
- }
- virtual void setResultReal(const char *name, unsigned sequence, double value)
- {
- if (isSpecialResultSequence(sequence))
- {
- CriticalBlock b(contextCrit);
- useContext(sequence).setPropBin(name, sizeof(value), &value);
- }
- else
- {
- StringBuffer v;
- v.append(value);
- FlushingStringBuffer *r = queryResult(sequence);
- if (r)
- {
- r->startScalar(name, sequence);
- if (r->isRaw)
- r->append(sizeof(value), (char *)&value);
- else
- r->appendf("%s", v.str());
- }
- }
- if (workUnit)
- {
- try
- {
- WorkunitUpdate wu(&workUnit->lock());
- CriticalBlock b(daliUpdateCrit);
- wu->setResultReal(name, sequence, value);
- }
- catch(IException *e)
- {
- StringBuffer text;
- e->errorMessage(text);
- CTXLOG("Error trying to update dali: %s", text.str());
- e->Release();
- }
- catch(...)
- {
- CTXLOG("Unknown exception trying to update dali");
- }
- }
- }
- virtual void setResultString(const char *name, unsigned sequence, int len, const char * str)
- {
- if (isSpecialResultSequence(sequence))
- {
- CriticalBlock b(contextCrit);
- useContext(sequence).setPropBin(name, len, str);
- }
- else
- {
- FlushingStringBuffer *r = queryResult(sequence);
- if (r)
- {
- r->startScalar(name, sequence);
- if (r->isRaw)
- {
- r->append(len, str);
- }
- else
- {
- r->encodeXML(str, 0, len);
- }
- }
- }
- if (workUnit)
- {
- try
- {
- WorkunitUpdate wu(&workUnit->lock());
- CriticalBlock b(daliUpdateCrit);
- wu->setResultString(name, sequence, len, str);
- }
- catch(IException *e)
- {
- StringBuffer text;
- e->errorMessage(text);
- CTXLOG("Error trying to update dali: %s", text.str());
- e->Release();
- }
- catch(...)
- {
- CTXLOG("Unknown exception trying to update dali");
- }
- }
- }
- virtual void setResultUnicode(const char *name, unsigned sequence, int len, UChar const * str)
- {
- if (isSpecialResultSequence(sequence))
- {
- rtlDataAttr buff;
- unsigned bufflen = 0;
- rtlUnicodeToCodepageX(bufflen, buff.refstr(), len, str, "utf-8");
- CriticalBlock b(contextCrit);
- useContext(sequence).setPropBin(name, bufflen, buff.getstr());
- }
- else
- {
- FlushingStringBuffer *r = queryResult(sequence);
- if (r)
- {
- r->startScalar(name, sequence);
- if (r->isRaw)
- {
- r->append(len*2, (const char *) str);
- }
- else
- {
- rtlDataAttr buff;
- unsigned bufflen = 0;
- rtlUnicodeToCodepageX(bufflen, buff.refstr(), len, str, "utf-8");
- r->encodeXML(buff.getstr(), 0, bufflen, true); // output as UTF-8
- }
- }
- }
- if (workUnit)
- {
- try
- {
- WorkunitUpdate wu(&workUnit->lock());
- CriticalBlock b(daliUpdateCrit);
- wu->setResultUnicode(name, sequence, len, str);
- }
- catch(IException *e)
- {
- StringBuffer text;
- e->errorMessage(text);
- CTXLOG("Error trying to update dali: %s", text.str());
- e->Release();
- }
- catch(...)
- {
- CTXLOG("Unknown exception trying to update dali");
- }
- }
- }
- virtual void setResultVarString(const char * name, unsigned sequence, const char * value)
- {
- setResultString(name, sequence, strlen(value), value);
- }
- virtual void setResultVarUnicode(const char * name, unsigned sequence, UChar const * value)
- {
- setResultUnicode(name, sequence, rtlUnicodeStrlen(value), value);
- }
- virtual void setResultDataset(const char * name, unsigned sequence, size32_t len, const void *data, unsigned numRows, bool extend)
- {
- appendResultRawContext(name, sequence, len, data, numRows, extend, true);
- }
- virtual void getResultDecimal(unsigned tlen, int precision, bool isSigned, void * tgt, const char * stepname, unsigned sequence)
- {
- if (isSpecialResultSequence(sequence))
- {
- MemoryBuffer m;
- CriticalBlock b(contextCrit);
- useContext(sequence).getPropBin(stepname, m);
- if (m.length())
- {
- assertex(m.length() == tlen);
- m.read(tlen, tgt);
- }
- else
- memset(tgt, 0, tlen);
- }
- else
- {
- StringBuffer x;
- {
- CriticalBlock b(contextCrit);
- useContext(sequence).getProp(stepname, x);
- }
- TempDecimal d;
- d.setString(x.length(), x.str());
- if (isSigned)
- d.getDecimal(tlen, precision, tgt);
- else
- d.getUDecimal(tlen, precision, tgt);
- }
- }
- virtual bool getResultBool(const char * name, unsigned sequence)
- {
- CriticalBlock b(contextCrit);
- return useContext(sequence).getPropBool(name);
- }
- static unsigned hex2digit(char c)
- {
- // MORE - what about error cases?
- if (c >= 'a')
- return (c - 'a' + 10);
- else if (c >= 'A')
- return (c - 'A' + 10);
- return (c - '0');
- }
- virtual void getResultData(unsigned & tlen, void * & tgt, const char * name, unsigned sequence)
- {
- MemoryBuffer result;
- CriticalBlock b(contextCrit);
- const char *val = useContext(sequence).queryProp(name);
- if (val)
- {
- loop
- {
- char c0 = *val++;
- if (!c0)
- break;
- char c1 = *val++;
- if (!c1)
- break; // Shouldn't really happen - we expect even length
- unsigned c2 = (hex2digit(c0) << 4) | hex2digit(c1);
- result.append((unsigned char) c2);
- }
- }
- tlen = result.length();
- tgt = result.detach();
- }
- void doExtractRawResultX(unsigned & tlen, void * & tgt, IPropertyTree *val, unsigned sequence, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer, bool isSet)
- {
- tgt = NULL;
- tlen = 0;
- if (val)
- {
- if (val->isBinary())
- {
- MemoryBuffer m;
- val->getPropBin(NULL, m);
- tlen = m.length();
- tgt= m.detach();
- }
- else
- {
- const char *format = val->queryProp("@format");
- if (!format || strcmp(format, "xml")==0)
- {
- assertex(xmlTransformer);
- Variable2IDataVal result(&tlen, &tgt);
- CXmlToRawTransformer rawTransformer(*xmlTransformer, xmlStoredDatasetReadFlags);
- rawTransformer.transformTree(result, *val, !isSet);
- }
- else if (strcmp(format, "deserialized")==0)
- {
- IDeserializedResultStore &resultStore = useResultStore(sequence);
- resultStore.serialize(tlen, tgt, val->getPropInt("@id", -1), queryCodeContext());
- }
- else if (strcmp(format, "csv")==0)
- {
- // MORE - never tested this code.....
- assertex(csvTransformer);
- Variable2IDataVal result(&tlen, &tgt);
- MemoryBuffer m;
- val->getPropBin(NULL, m);
- CCsvToRawTransformer rawCsvTransformer(*csvTransformer);
- rawCsvTransformer.transform(result, m.length(), m.toByteArray(), !isSet);
- }
- else
- throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "no transform function available");
- }
- }
- }
- void doExtractRawResultF(unsigned tlen, void * tgt, IPropertyTree *val, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer, bool isSet)
- {
- if (!val)
- throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "no value for result");
- if (val->isBinary())
- {
- try
- {
- //Slightly weird usage - provide the buffer to MemoryBuffer to avoid an allocation and extra copy
- MemoryBuffer m;
- m.setBuffer(tlen, tgt, false);
- m.setLength(0);
- val->getPropBin(NULL, m);
- }
- catch (IException * e)
- {
- e->Release();
- throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "result larger that expected");
- }
- }
- else if (xmlTransformer)
- {
- Fixed2IDataVal result(tlen, tgt);
- CXmlToRawTransformer rawTransformer(*xmlTransformer, xmlStoredDatasetReadFlags);
- rawTransformer.transformTree(result, *val, !isSet);
- }
- else if (csvTransformer)
- {
- // MORE - never tested this code.....
- Fixed2IDataVal result(tlen, tgt);
- MemoryBuffer m;
- val->getPropBin(NULL, m);
- CCsvToRawTransformer rawCsvTransformer(*csvTransformer);
- rawCsvTransformer.transform(result, m.length(), m.toByteArray(), !isSet);
- }
- else
- throw MakeStringException(ROXIE_UNIMPLEMENTED_ERROR, "no transform function available");
- }
- virtual void getResultRaw(unsigned & tlen, void * & tgt, const char *stepname, unsigned sequence, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer)
- {
- try
- {
- CriticalBlock b(contextCrit);
- IPropertyTree &ctx = useContext(sequence);
- IPropertyTree *val = ctx.queryPropTree(stepname);
- doExtractRawResultX(tlen, tgt, val, sequence, xmlTransformer, csvTransformer, false);
- }
- catch (IException * e)
- {
- StringBuffer text;
- e->errorMessage(text);
- e->Release();
- throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value \"%s\". [%s]", stepname, text.str());
- }
- catch (...)
- {
- throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value \"%s\"", stepname);
- }
- }
- virtual void getResultRowset(size32_t & tcount, byte * * & tgt, const char * stepname, unsigned sequence, IEngineRowAllocator * _rowAllocator, IOutputRowDeserializer * deserializer, bool isGrouped, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer)
- {
- try
- {
- bool atEOG = true;
- Owned<IWorkUnitRowReader> wuReader = getWorkunitRowReader(stepname, sequence, xmlTransformer, _rowAllocator, isGrouped);
- RtlLinkedDatasetBuilder builder(_rowAllocator);
- loop
- {
- const void *ret = wuReader->nextInGroup();
- if (!ret)
- {
- if (atEOG || !isGrouped)
- break;
- atEOG = true;
- }
- else
- atEOG = false;
- builder.appendOwn(ret);
- }
- tcount = builder.getcount();
- tgt = builder.linkrows();
- }
- catch (IException * e)
- {
- StringBuffer text;
- e->errorMessage(text);
- e->Release();
- throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value \"%s\". [%s]", stepname, text.str());
- }
- catch (...)
- {
- throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value \"%s\"", stepname);
- }
- }
- virtual void getResultSet(bool & tisAll, unsigned & tlen, void * & tgt, const char *stepname, unsigned sequence, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer)
- {
- try
- {
- CriticalBlock b(contextCrit);
- IPropertyTree &ctx = useContext(sequence);
- IPropertyTree *val = ctx.queryPropTree(stepname);
- doExtractRawResultX(tlen, tgt, val, sequence, xmlTransformer, csvTransformer, true);
- tisAll = val ? val->getPropBool("@isAll", false) : false;
- }
- catch (IException * e)
- {
- StringBuffer text;
- e->errorMessage(text);
- e->Release();
- throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve set \"%s\". [%s]", stepname, text.str());
- }
- catch (...)
- {
- throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve set \"%s\"", stepname);
- }
- }
- virtual void printResults(IXmlWriter *output, const char *name, unsigned sequence)
- {
- CriticalBlock b(contextCrit);
- IPropertyTree &tree = useContext(sequence);
- if (name)
- {
- const char *val = tree.queryProp(name);
- if (val)
- output->outputCString(val, name);
- }
- else
- {
- StringBuffer hack;
- toXML(&tree, hack);
- output->outputString(0, NULL, NULL); // Hack upon hack...
- output->outputQuoted(hack.str());
- }
- }
- class RawDataReader : public CInterface, implements IWorkUnitRowReader
- {
- protected:
- const IRoxieContextLogger &logctx;
- byte *bufferBase;
- MemoryBuffer blockBuffer;
- Owned<ISerialStream> bufferStream;
- CThorStreamDeserializerSource rowSource;
- bool eof;
- bool eogPending;
- bool isGrouped;
- Linked<IEngineRowAllocator> rowAllocator;
- Owned<IOutputRowDeserializer> rowDeserializer;
- virtual bool nextBlock(unsigned & tlen, void * & tgt, void * & base) = 0;
- bool reload()
- {
- free(bufferBase);
- size32_t lenData;
- bufferBase = NULL;
- void *tempData, *base;
- eof = !nextBlock(lenData, tempData, base);
- bufferBase = (byte *) base;
- blockBuffer.setBuffer(lenData, tempData, false);
- return !eof;
- }
- public:
- IMPLEMENT_IINTERFACE;
- RawDataReader(CRoxieServerContext *parent, IEngineRowAllocator *_rowAllocator, bool _isGrouped)
- : logctx(*parent), rowAllocator(_rowAllocator), isGrouped(_isGrouped)
- {
- eof = false;
- eogPending = false;
- bufferBase = NULL;
- rowDeserializer.setown(rowAllocator->createRowDeserializer(parent->queryCodeContext()));
- bufferStream.setown(createMemoryBufferSerialStream(blockBuffer));
- rowSource.setStream(bufferStream);
- }
- ~RawDataReader()
- {
- if (bufferBase)
- free(bufferBase);
- }
- virtual const void *nextInGroup()
- {
- if (eof)
- return NULL;
- if (rowSource.eos() && !reload())
- return NULL;
- if (eogPending)
- {
- eogPending = false;
- return NULL;
- }
- #if 0
- // MORE - think a bit about what happens on incomplete rows - I think deserializer will throw an exception?
- unsigned thisSize = meta.getRecordSize(data+cursor);
- if (thisSize > lenData-cursor)
- {
- CTXLOG("invalid stored dataset - incomplete row at end");
- throw MakeStringException(ROXIE_DATA_ERROR, "invalid stored dataset - incomplete row at end");
- }
- #endif
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- size32_t size = rowDeserializer->deserialize(rowBuilder, rowSource);
- if (isGrouped)
- rowSource.read(sizeof(bool), &eogPending);
- atomic_inc(&rowsIn);
- return rowBuilder.finalizeRowClear(size);
- }
- };
- class InlineRawDataReader : public RawDataReader
- {
- Linked<IPropertyTree> xml;
- public:
- InlineRawDataReader(CRoxieServerContext *parent, IEngineRowAllocator *_rowAllocator, bool _isGrouped, IPropertyTree *_xml)
- : RawDataReader(parent, _rowAllocator, _isGrouped), xml(_xml)
- {
- }
- virtual bool nextBlock(unsigned & tlen, void * & tgt, void * & base)
- {
- base = tgt = NULL;
- if (xml)
- {
- MemoryBuffer result;
- xml->getPropBin(NULL, result);
- tlen = result.length();
- base = tgt = result.detach();
- xml.clear();
- return tlen != 0;
- }
- else
- {
- tlen = 0;
- return false;
- }
- }
- };
- class StreamedRawDataReader : public RawDataReader
- {
- SafeSocket &client;
- StringAttr id;
- offset_t offset;
- public:
- StreamedRawDataReader(CRoxieServerContext *parent, IEngineRowAllocator *_rowAllocator, bool _isGrouped, SafeSocket &_client, const char *_id)
- : RawDataReader(parent, _rowAllocator, _isGrouped), client(_client), id(_id)
- {
- offset = 0;
- }
- virtual bool nextBlock(unsigned & tlen, void * & tgt, void * & base)
- {
- try
- {
- #ifdef FAKE_EXCEPTIONS
- if (offset > 0x10000)
- throw MakeStringException(ROXIE_INTERNAL_ERROR, "TEST EXCEPTION");
- #endif
- // Go request from the socket
- MemoryBuffer request;
- request.reserve(sizeof(size32_t));
- request.append('D');
- offset_t loffset = offset;
- _WINREV(loffset);
- request.append(sizeof(loffset), &loffset);
- request.append(strlen(id)+1, id);
- size32_t len = request.length() - sizeof(size32_t);
- len |= 0x80000000;
- _WINREV(len);
- *(size32_t *) request.toByteArray() = len;
- client.write(request.toByteArray(), request.length());
- // Note: I am the only thread reading (we only support a single input dataset in roxiepipe mode)
- MemoryBuffer reply;
- client.readBlock(reply, readTimeout);
- tlen = reply.length();
- // MORE - not very robust!
- // skip past block header
- if (tlen > 0)
- {
- tgt = base = reply.detach();
- tgt = ((char *)base) + 9;
- tgt = strchr((char *)tgt, '\0') + 1;
- tlen -= ((char *)tgt - (char *)base);
- offset += tlen;
- }
- else
- tgt = base = NULL;
- return tlen != 0;
- }
- catch (IException *E)
- {
- StringBuffer text;
- E->errorMessage(text);
- int errCode = E->errorCode();
- E->Release();
- IException *ee = MakeStringException(MSGAUD_internal, errCode, "%s", text.str());
- logctx.logOperatorException(ee, __FILE__, __LINE__, "Exception caught in RawDataReader::nextBlock");
- throw ee;
- }
- catch (...)
- {
- logctx.logOperatorException(NULL, __FILE__, __LINE__, "Unknown exception caught in RawDataReader::nextBlock");
- throw;
- }
- }
- };
- class InlineXmlDataReader : public CInterface, implements IWorkUnitRowReader
- {
- Linked<IPropertyTree> xml;
- Owned <XmlColumnProvider> columns;
- Owned<IPropertyTreeIterator> rows;
- IXmlToRowTransformer &rowTransformer;
- Linked<IEngineRowAllocator> rowAllocator;
- public:
- IMPLEMENT_IINTERFACE;
- InlineXmlDataReader(IXmlToRowTransformer &_rowTransformer, IPropertyTree *_xml, IEngineRowAllocator *_rowAllocator)
- : xml(_xml), rowTransformer(_rowTransformer), rowAllocator(_rowAllocator)
- {
- columns.setown(new XmlDatasetColumnProvider);
- rows.setown(xml->getElements("Row")); // NOTE - the 'hack for Gordon' as found in thorxmlread is not implemented here. Does it need to be ?
- rows->first();
- }
- virtual const void *nextInGroup()
- {
- if (rows->isValid())
- {
- columns->setRow(&rows->query());
- rows->next();
- RtlDynamicRowBuilder rowBuilder(rowAllocator);
- NullDiskCallback callback;
- size_t outSize = rowTransformer.transform(rowBuilder, columns, &callback);
- return rowBuilder.finalizeRowClear(outSize);
- }
- return NULL;
- }
- };
- virtual IWorkUnitRowReader *getWorkunitRowReader(const char *stepname, unsigned sequence, IXmlToRowTransformer * xmlTransformer, IEngineRowAllocator *rowAllocator, bool isGrouped)
- {
- try
- {
- CriticalBlock b(contextCrit);
- IPropertyTree &ctx = useContext(sequence);
- IPropertyTree *val = ctx.queryPropTree(stepname);
- if (val)
- {
- const char *id = val->queryProp("@id");
- const char *format = val->queryProp("@format");
- if (id)
- {
- if (!format || strcmp(format, "raw") == 0)
- return new StreamedRawDataReader(this, rowAllocator, isGrouped, *client, id);
- else if (strcmp(format, "deserialized") == 0)
- {
- IDeserializedResultStore &resultStore = useResultStore(sequence);
- return resultStore.createDeserializedReader(atoi(id));
- }
- else
- throwUnexpected();
- }
- else
- {
- if (!format || strcmp(format, "xml") == 0)
- {
- if (xmlTransformer)
- return new InlineXmlDataReader(*xmlTransformer, val, rowAllocator);
- }
- else if (strcmp(format, "raw") == 0)
- {
- return new InlineRawDataReader(this, rowAllocator, isGrouped, val);
- }
- else
- throwUnexpected();
- }
- }
- }
- catch (IException * e)
- {
- StringBuffer text;
- e->errorMessage(text);
- e->Release();
- throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value %s. [%s]", stepname, text.str());
- }
- catch (...)
- {
- throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value %s", stepname);
- }
- throw MakeStringException(ROXIE_DATA_ERROR, "Failed to retrieve data value %s", stepname);
- }
- virtual const IResolvedFile *resolveLFN(const char *filename, bool isOpt)
- {
- CriticalBlock b(daliUpdateCrit);
- if (!dynamicPackage)
- {
- dynamicPackage.setown(createPackage(NULL));
- }
- return dynamicPackage->lookupFileName(filename, isOpt, true);
- }
- virtual IRoxieWriteHandler *createLFN(const char *filename, bool overwrite, bool extend, const StringArray &clusters)
- {
- CriticalBlock b(daliUpdateCrit);
- if (!dynamicPackage)
- {
- dynamicPackage.setown(createPackage(NULL));
- }
- return dynamicPackage->createFileName(filename, overwrite, extend, clusters);
- }
- virtual void onFileCallback(const RoxiePacketHeader &header, const char *lfn, bool isOpt, bool isLocal)
- {
- Owned<const IResolvedFile> dFile = resolveLFN(lfn, isOpt);
- if (dFile)
- {
- MemoryBuffer mb;
- mb.append(sizeof(RoxiePacketHeader), &header);
- mb.append(lfn);
- dFile->serializePartial(mb, header.channel, isLocal);
- ((RoxiePacketHeader *) mb.toByteArray())->activityId = ROXIE_FILECALLBACK;
- Owned<IRoxieQueryPacket> reply = createRoxiePacket(mb);
- reply->queryHeader().retries = 0;
- ROQ->sendPacket(reply, *this); // MORE - the caller's log context might be better? Should we unicast? Note that this does not release the packet
- return;
- }
- ROQ->sendAbortCallback(header, lfn, *this);
- throwUnexpected();
- }
- virtual void getExternalResultRaw(unsigned & tlen, void * & tgt, const char * wuid, const char * stepname, unsigned sequence, IXmlToRowTransformer * xmlTransformer, ICsvToRowTransformer * csvTransformer)
- {
- UNIMPLEMENTED;
- }
- virtual void addWuException(const char * text, unsigned code, unsigned _severity)
- {
- WUExceptionSeverity severity = (WUExceptionSeverity) _severity;
- CTXLOG("%s", text);
- if (severity > ExceptionSeverityInformation)
- OERRLOG("%d - %s", code, text);
- if (workUnit)
- {
- WorkunitUpdate wu(&workUnit->lock());
- addExceptionToWorkunit(wu, severity, "user", code, text, NULL, 0 ,0);
- }
- }
- virtual void addWuAssertFailure(unsigned code, const char * text, const char * filename, unsigned lineno, unsigned column, bool isAbort)
- {
- CTXLOG("%s", text);
- OERRLOG("%d - %s", code, text);
- if (workUnit)
- {
- WorkunitUpdate wu(&workUnit->lock());
- addExceptionToWorkunit(wu, ExceptionSeverityError, "user", code, text, filename, lineno, column);
- }
- if (isAbort)
- rtlFailOnAssert(); // minimal implementation
- }
- IUserDescriptor *queryUserDescriptor()
- {
- return NULL; // TBD - Richard, where do user credentials for a roxie query come from
- }
- virtual __int64 getResultInt(const char * name, unsigned sequence)
- {
- CriticalBlock b(contextCrit);
- const char *val = useContext(sequence).queryProp(name);
- if (val)
- {
- // NOTE - we use this rather than getPropInt64 since it handles uint64 values up to MAX_UINT better (for our purposes)
- return rtlStrToInt8(strlen(val), val);
- }
- else
- return 0;
- }
- virtual double getResultReal(const char * name, unsigned sequence)
- {
- CriticalBlock b(contextCrit);
- IPropertyTree &ctx = useContext(sequence);
- double ret = 0;
- if (ctx.hasProp(name))
- {
- if (ctx.isBinary(name))
- {
- MemoryBuffer buf;
- ctx.getPropBin(name, buf);
- buf.read(ret);
- }
- else
- {
- const char *val = ctx.queryProp(name);
- if (val)
- ret = atof(val);
- }
- }
- return ret;
- }
- virtual void getResultString(unsigned & tlen, char * & tgt, const char * name, unsigned sequence)
- {
- MemoryBuffer x;
- bool isBinary;
- {
- CriticalBlock b(contextCrit);
- IPropertyTree &ctx = useContext(sequence);
- isBinary = ctx.isBinary(name);
- ctx.getPropBin(name, x);
- }
- if (isBinary) // No utf8 translation if previously set via setResultString
- {
- tlen = x.length();
- tgt = (char *) x.detach();
- }
- else
- rtlUtf8ToStrX(tlen, tgt, rtlUtf8Length(x.length(), x.toByteArray()), x.toByteArray());
- }
- virtual void getResultStringF(unsigned tlen, char * tgt, const char * name, unsigned sequence)
- {
- MemoryBuffer x;
- bool isBinary;
- {
- CriticalBlock b(contextCrit);
- IPropertyTree &ctx = useContext(sequence);
- isBinary = ctx.isBinary(name);
- ctx.getPropBin(name, x);
- }
- if (isBinary)
- rtlStrToStr(tlen, tgt, x.length(), x.toByteArray());
- else
- rtlUtf8ToStr(tlen, tgt, rtlUtf8Length(x.length(), x.toByteArray()), x.toByteArray());
- }
- virtual void getResultUnicode(unsigned & tlen, UChar * & tgt, const char * name, unsigned sequence)
- {
- StringBuffer x;
- {
- CriticalBlock b(contextCrit);
- useContext(sequence).getProp(name, x);
- }
- tgt = rtlCodepageToVUnicodeX(x.length(), x.str(), "utf-8");
- tlen = rtlUnicodeStrlen(tgt);
- }
- virtual char *getResultVarString(const char * name, unsigned sequence)
- {
- CriticalBlock b(contextCrit);
- IPropertyTree &ctx = useContext(sequence);
- bool isBinary = ctx.isBinary(name);
- if (isBinary)
- {
- StringBuffer s;
- ctx.getProp(name, s);
- return s.detach();
- }
- else
- {
- MemoryBuffer x;
- ctx.getPropBin(name, x);
- return rtlUtf8ToVStr(rtlUtf8Length(x.length(), x.toByteArray()), x.toByteArray());
- }
- }
- virtual UChar *getResultVarUnicode(const char * name, unsigned sequence)
- {
- StringBuffer x;
- CriticalBlock b(contextCrit);
- useContext(sequence).getProp(name, x);
- return rtlVCodepageToVUnicodeX(x.str(), "utf-8");
- }
- virtual bool isResult(const char * name, unsigned sequence)
- {
- CriticalBlock b(contextCrit);
- return useContext(sequence).hasProp(name);
- }
- virtual char *getClusterName() { throwUnexpected(); }
- virtual char *getGroupName() { throwUnexpected(); }
- virtual char * queryIndexMetaData(char const * lfn, char const * xpath) { throwUnexpected(); }
- virtual char *getDaliServers() { throwUnexpected(); }
- virtual char *getEnv(const char *name, const char *defaultValue) const
- {
- return serverQueryFactory->getEnv(name, defaultValue);
- }
- virtual char *getJobName()
- {
- return strdup(factory->queryQueryName());
- }
- virtual char *getJobOwner() { throwUnexpected(); }
- virtual char *getPlatform()
- {
- return strdup("roxie");
- }
- virtual char *getWuid()
- {
- if (workUnit)
- {
- SCMStringBuffer wuid;
- workUnit->getWuid(wuid);
- return strdup(wuid.str());
- }
- else
- {
- throw MakeStringException(ROXIE_INVALID_ACTION, "No workunit associated with this context");
- }
- }
- // persist-related code - usage of persist should have been caught and rejected at codegen time
- virtual char * getExpandLogicalName(const char * logicalName) { throwUnexpected(); }
- virtual void startPersist(const char * name) { throwUnexpected(); }
- virtual void finishPersist() { throwUnexpected(); }
- virtual void clearPersist(const char * logicalName) { throwUnexpected(); }
- virtual void updatePersist(const char * logicalName, unsigned eclCRC, unsigned __int64 allCRC) { throwUnexpected(); }
- virtual unsigned getResultHash(const char * name, unsigned sequence) { throwUnexpected(); }
- virtual void checkPersistMatches(const char * logicalName, unsigned eclCRC) { throwUnexpected(); }
- virtual unsigned getRecoveringCount() { throwUnexpected(); }
- virtual void setWorkflowCondition(bool value) { if(workflow) workflow->setCondition(value); }
- virtual void returnPersistVersion(const char * logicalName, unsigned eclCRC, unsigned __int64 allCRC, bool isFile) { throwUnexpected(); }
- virtual __int64 countDiskFile(const char * lfn, unsigned recordSize)
- {
- throwUnexpected();
- }
- virtual void fail(int code, const char *text)
- {
- addWuException(text, code, 2);
- }
- virtual unsigned getWorkflowId() { return workflow->queryCurrentWfid(); }
- virtual void doNotify(char const * code, char const * extra) { UNIMPLEMENTED; }
- virtual void doNotify(char const * code, char const * extra, const char * target) { UNIMPLEMENTED; }
- virtual void doWait(unsigned code, char const * extra) { UNIMPLEMENTED; }
- virtual void doWaitCond(unsigned code, char const * extra, int sequence, char const * alias, unsigned wfid) { UNIMPLEMENTED; }
- virtual unsigned __int64 getDatasetHash(const char * name, unsigned __int64 hash) { throwUnexpected(); }
- virtual int queryLastFailCode() { UNIMPLEMENTED; }
- virtual void getLastFailMessage(size32_t & outLen, char * &outStr, const char * tag) { UNIMPLEMENTED; }
- virtual void getEventName(size32_t & outLen, char * & outStr) { UNIMPLEMENTED; }
- virtual void getEventExtra(size32_t & outLen, char * & outStr, const char * tag) { UNIMPLEMENTED; }
- virtual bool fileExists(const char * filename) { throwUnexpected(); }
- virtual void deleteFile(const char * logicalName) { throwUnexpected(); }
- virtual unsigned getNodes() { return numChannels; }
- virtual unsigned getNodeNum() { return 1; }
- virtual char *getFilePart(const char *logicalPart, bool create=false) { UNIMPLEMENTED; }
- virtual unsigned __int64 getFileOffset(const char *logicalPart) { throwUnexpected(); }
- virtual IDistributedFileTransaction *querySuperFileTransaction() { UNIMPLEMENTED; }
- virtual void flush(unsigned seqNo) { throwUnexpected(); }
- virtual unsigned getPriority() const { return priority; }
- virtual bool outputResultsToWorkUnit() const { return workUnit != NULL; }
- virtual bool outputResultsToSocket() const { return client != NULL; }
- virtual void selectCluster(const char * cluster) { throwUnexpected(); }
- virtual void restoreCluster() { throwUnexpected(); }
- };
- //================================================================================================
- class CSoapRoxieServerContext : public CRoxieServerContext
- {
- private:
- StringAttr queryName;
- public:
- CSoapRoxieServerContext(IPropertyTree *_context, const IQueryFactory *_factory, SafeSocket &_client, HttpHelper &httpHelper, unsigned _priority, const IRoxieContextLogger &_logctx, XmlReaderOptions xmlReadFlags)
- : CRoxieServerContext(_context, _factory, _client, true, false, false, httpHelper, true, _priority, _logctx, xmlReadFlags)
- {
- queryName.set(_context->queryName());
- }
- virtual void process()
- {
- EclProcessFactory pf = (EclProcessFactory) factory->queryDll()->getEntry("createProcess");
- Owned<IEclProcess> p = pf();
- if (workflow)
- workflow->perform(this, p);
- else
- p->perform(this, 0);
- }
- virtual void flush(unsigned seqNo)
- {
- CriticalBlock b(resultsCrit);
- CriticalBlock b1(client->queryCrit());
- StringBuffer responseHead, responseTail;
- responseHead.append("<").append(queryName).append("Response");
- responseHead.append(" sequence=\"").append(seqNo).append("\"");
- responseHead.append(" xmlns=\"urn:hpccsystems:ecl:").appendLower(queryName.length(), queryName.sget()).append("\">");
- responseHead.append("<Results><Result>");
- unsigned len = responseHead.length();
- client->write(responseHead.detach(), len, true);
-
- ForEachItemIn(seq, resultMap)
- {
- FlushingStringBuffer *result = resultMap.item(seq);
- if (result)
- {
- result->flush(true);
- for(;;)
- {
- size32_t length;
- void *payload = result->getPayload(length);
- if (!length)
- break;
- client->write(payload, length, true);
- }
- }
- }
- responseTail.append("</Result></Results>");
- responseTail.append("</").append(queryName).append("Response>");
- len = responseTail.length();
- client->write(responseTail.detach(), len, true);
- }
- };
- IRoxieServerContext *createRoxieServerContext(IPropertyTree *context, const IQueryFactory *factory, SafeSocket &client, bool isXml, bool isRaw, bool isBlocked, HttpHelper &httpHelper, bool trim, unsigned priority, const IRoxieContextLogger &_logctx, XmlReaderOptions xmlReadFlags)
- {
- if (httpHelper.isHttp())
- return new CSoapRoxieServerContext(context, factory, client, httpHelper, priority, _logctx, xmlReadFlags);
- else
- return new CRoxieServerContext(context, factory, client, isXml, isRaw, isBlocked, httpHelper, trim, priority, _logctx, xmlReadFlags);
- }
- IRoxieServerContext *createOnceServerContext(const IQueryFactory *factory, const IRoxieContextLogger &_logctx)
- {
- return new CRoxieServerContext(factory, _logctx);
- }
- IRoxieServerContext *createWorkUnitServerContext(IConstWorkUnit *wu, const IQueryFactory *factory, const IRoxieContextLogger &_logctx)
- {
- return new CRoxieServerContext(wu, factory, _logctx);
- }
- //======================================================================================================================
- static void controlException(StringBuffer &response, IException *E, const IRoxieContextLogger &logctx)
- {
- try
- {
- if (traceLevel)
- logctx.logOperatorException(E, __FILE__, __LINE__, "controlException");
- response.appendf("<Exception><Source>Roxie</Source><Code>%d</Code><Message>", E->errorCode());
- StringBuffer s;
- E->errorMessage(s);
- encodeXML(s.str(), response);
- response.append("</Message></Exception>");
- E->Release();
- }
- catch(IException *EE)
- {
- logctx.logOperatorException(EE, __FILE__, __LINE__, "controlException - While reporting exception");
- EE->Release();
- }
- #ifndef _DEBUG
- catch(...) {}
- #endif
- }
- class CascadeManager : public CInterface
- {
- static Semaphore globalLock;
- StringBuffer errors;
- IArrayOf<ISocket> activeChildren;
- UnsignedArray activeIdxes;
- bool entered;
- bool connected;
- bool isMaster;
- CriticalSection revisionCrit;
- int myEndpoint;
- const IRoxieContextLogger &logctx;
- void unlockChildren()
- {
- try
- {
- class casyncfor: public CAsyncFor
- {
- public:
- casyncfor(CascadeManager *_parent) : parent(_parent) { }
- void Do(unsigned i)
- {
- parent->unlockChild(i);
- }
- private:
- CascadeManager *parent;
- } afor(this);
- afor.For(activeChildren.length(), activeChildren.length());
- }
- catch (IException *E)
- {
- if (traceLevel)
- logctx.logOperatorException(E, __FILE__, __LINE__, "In unlockChildren");
- E->Release();
- }
- }
-
- void unlockAll()
- {
- if (entered)
- {
- unlockChildren();
- entered = false;
- if (traceLevel > 5)
- DBGLOG("globalLock released");
- globalLock.signal();
- atomic_inc(&globalSignals);
- }
- }
- SocketEndpoint &queryEndpoint(unsigned idx)
- {
- return allRoxieServers.item(idx);
- }
- void connectChild(unsigned idx)
- {
- if (allRoxieServers.isItem(idx))
- {
- SocketEndpoint &ep = queryEndpoint(idx);
- try
- {
- if (traceLevel)
- {
- StringBuffer epStr;
- ep.getUrlStr(epStr);
- DBGLOG("connectChild connecting to %s", epStr.str());
- }
- ISocket *sock = ISocket::connect_timeout(ep, 2000);
- assertex(sock);
- activeChildren.append(*sock);
- activeIdxes.append(idx);
- if (traceLevel)
- {
- StringBuffer epStr;
- ep.getUrlStr(epStr);
- DBGLOG("connectChild connected to %s", epStr.str());
- }
- }
- catch(IException *E)
- {
- logctx.logOperatorException(E, __FILE__, __LINE__, "CascadeManager connection failed");
- connectChild((idx+1) * 2 - 1);
- connectChild((idx+1) * 2);
- errors.append("<Endpoint ep='");
- ep.getUrlStr(errors);
- errors.append("'><Exception><Code>").append(E->errorCode()).append("</Code><Message>");
- E->errorMessage(errors).append("</Message></Exception></Endpoint>");
- logctx.CTXLOG("Connection failed - %s", errors.str());
- E->Release();
- }
- }
- }
- public:
- void doChildQuery(unsigned idx, const char *queryText, StringBuffer &reply)
- {
- ISocket &sock = activeChildren.item(idx);
- CSafeSocket ss(LINK(&sock));
- unsigned txtlen = queryText ? strlen(queryText) : 0;
- unsigned revlen = txtlen;
- _WINREV(revlen);
- ss.write(&revlen, sizeof(revlen));
- if (txtlen)
- {
- ss.write(queryText, txtlen);
- bool dummy;
- while (ss.readBlock(reply, WAIT_FOREVER, NULL, dummy, dummy, maxBlockSize)) {}
- }
- }
- int lockChild(unsigned idx)
- {
- StringBuffer lockReply;
- StringBuffer lockQuery;
- lockQuery.appendf("<control:childlock thisEndpoint='%d' parent='%d'/>", activeIdxes.item(idx), myEndpoint);
- doChildQuery(idx, lockQuery.str(), lockReply);
- Owned<IPropertyTree> lockResult = createPTreeFromXMLString(lockReply.str(), ipt_caseInsensitive);
- int lockCount = lockResult->getPropInt("Lock", 0);
- if (lockCount)
- {
- return lockCount;
- }
- else
- throw MakeStringException(ROXIE_LOCK_ERROR, "Did not get lock for child %d (%s)", idx, lockReply.str());
- }
- void unlockChild(unsigned idx)
- {
- try
- {
- StringBuffer dummy;
- doChildQuery(idx, "<control:childlock unlock='1'/>", dummy);
- if (traceLevel)
- DBGLOG("UnlockChild %d returned %s", idx, dummy.str());
- }
- catch (IException *E)
- {
- if (traceLevel)
- logctx.logOperatorException(E, __FILE__, __LINE__, "In unlockChild");
- E->Release();
- }
- }
- private:
- unsigned lockChildren()
- {
- loop
- {
- int got = 1;
- CriticalSection cs;
- try
- {
- class casyncfor: public CAsyncFor
- {
- public:
- casyncfor(CascadeManager *_parent, int &_got, CriticalSection &_cs)
- : parent(_parent), got(_got), cs(_cs){ }
- void Do(unsigned i)
- {
- int childLocks = parent->lockChild(i);
- CriticalBlock b(cs);
- if (childLocks <= 0)
- got = childLocks;
- else if (got > 0)
- got += childLocks;
- }
- private:
- CascadeManager *parent;
- int &got;
- CriticalSection &cs;
- } afor(this, got, cs);
- afor.For(activeChildren.length(), activeChildren.length());
- }
- catch (IException *E)
- {
- if (traceLevel)
- logctx.logOperatorException(E, __FILE__, __LINE__, "In lockChildren");
- E->Release();
- got = 0; // Something went wrong - abandon this attempt
- }
- if (got <= 0)
- {
- unlockChildren();
- if (!got)
- throw MakeStringException(ROXIE_LOCK_ERROR, "lock failed");
- if (traceLevel)
- DBGLOG("Lock succeeded but revision updated - go around again");
- }
- else
- return got-1;
- }
- }
- void getGlobalLock()
- {
- if (traceLevel > 5)
- DBGLOG("in getGlobalLock");
- if (!globalLock.wait(2000)) // since all lock in the same order it's ok to block for a bit here
- throw MakeStringException(ROXIE_LOCK_ERROR, "lock failed");
- atomic_inc(&globalLocks);
- entered = true;
- if (traceLevel > 5)
- DBGLOG("globalLock locked");
- }
- unsigned lockAll()
- {
- try
- {
- return lockChildren() + 1;
- }
- catch(...)
- {
- if (traceLevel>5)
- DBGLOG("Failed to get child locks - unlocking");
- assertex(entered);
- entered = false;
- globalLock.signal();
- atomic_inc(&globalSignals);
- if (traceLevel > 5)
- DBGLOG("globalLock released");
- throw;
- }
- }
- public:
- IMPLEMENT_IINTERFACE;
- CascadeManager(const IRoxieContextLogger &_logctx) : logctx(_logctx)
- {
- entered = false;
- connected = false;
- isMaster = false;
- myEndpoint = -1;
- logctx.Link();
- }
- ~CascadeManager()
- {
- unlockAll();
- logctx.Release();
- }
- void doLockChild(const char *queryText, StringBuffer &reply)
- {
- if (traceLevel > 5)
- DBGLOG("doLockChild: %s", queryText);
- isMaster = false;
- Owned<IPropertyTree> xml = createPTreeFromXMLString(queryText);
- bool unlock = xml->getPropBool("@unlock", false);
- if (unlock)
- {
- unlockAll();
- reply.append("<Lock>0</Lock>");
- }
- else
- {
- assertex(!entered);
- myEndpoint = xml->getPropInt("@thisEndpoint", 0);
- if (!connected)
- {
- connectChild((myEndpoint+1) * 2 - 1);
- connectChild((myEndpoint+1) * 2);
- connected = true;
- }
- try
- {
- getGlobalLock();
- unsigned locksGot = lockAll();
- reply.append("<Lock>").append(locksGot).append("</Lock>");
- assertex(entered);
- }
- catch (IException *E)
- {
- if (traceLevel > 5)
- logctx.logOperatorException(E, __FILE__, __LINE__, "Trying to get global lock");
- E->Release();
- reply.append("<Lock>0</Lock>");
- }
- }
- }
- bool doLockGlobal(StringBuffer &reply, bool lockAll)
- {
- assertex(!entered);
- assertex(!connected);
- isMaster = true;
- myEndpoint = -1;
- unsigned attemptsLeft = maxLockAttempts;
- connectChild(0);
- connected = true;
- unsigned lockDelay = 0;
- unsigned locksGot = 0;
- Owned<IRandomNumberGenerator> randomizer;
- loop
- {
- try
- {
- locksGot = lockChildren();
- break;
- }
- catch (IException *E)
- {
- unsigned errCode = E->errorCode();
- if (traceLevel > 5)
- logctx.logOperatorException(E, __FILE__, __LINE__, "In doLockGlobal()");
- E->Release();
- if ( (!--attemptsLeft) || (errCode == ROXIE_CLUSTER_SYNC_ERROR))
- {
- reply.append("<Lock>0</Lock>");
- return false;
- }
- if (!randomizer) randomizer.setown(createRandomNumberGenerator());
- lockDelay += 1000 + randomizer->next() % 1000;
- Sleep(lockDelay);
- }
- }
- if (traceLevel > 5)
- DBGLOG("doLockGlobal got %d locks", locksGot);
- reply.append("<Lock>").append(locksGot).append("</Lock>");
- reply.append("<NumServers>").append(allRoxieServers.ordinality()).append("</NumServers>");
- if (lockAll)
- return locksGot == allRoxieServers.ordinality();
- else
- return locksGot > allRoxieServers.ordinality()/2;
- }
- void doControlQuery(SocketEndpoint &ep, const char *queryText, StringBuffer &reply)
- {
- if (logctx.queryTraceLevel() > 5)
- logctx.CTXLOG("doControlQuery (%d): %.80s", isMaster, queryText);
- // By this point we should have cascade-connected thanks to a prior <control:lock>
- // So do the query ourselves and in all child threads;
- Owned<IPropertyTree> mergedStats;
- if (strstr(queryText, "querystats"))
- mergedStats.setown(createPTree("Endpoint"));
-
- class casyncfor: public CAsyncFor
- {
- const char *queryText;
- CascadeManager *parent;
- IPropertyTree *mergedStats;
- StringBuffer &reply;
- CriticalSection crit;
- SocketEndpoint &ep;
- unsigned numChildren;
- const IRoxieContextLogger &logctx;
- public:
- casyncfor(const char *_queryText, CascadeManager *_parent, IPropertyTree *_mergedStats,
- StringBuffer &_reply, SocketEndpoint &_ep, unsigned _numChildren, const IRoxieContextLogger &_logctx)
- : queryText(_queryText), parent(_parent), mergedStats(_mergedStats), reply(_reply), ep(_ep), numChildren(_numChildren), logctx(_logctx)
- {
- }
- void Do(unsigned i)
- {
- if (logctx.queryTraceLevel() > 5)
- logctx.CTXLOG("doControlQuery::do (%d of %d): %.80s", i, numChildren, queryText);
- if (i == numChildren)
- doMe();
- else
- {
- StringBuffer childReply;
- parent->doChildQuery(i, queryText, childReply);
- Owned<IPropertyTree> xml = createPTreeFromXMLString(childReply);
- if (!xml)
- {
- StringBuffer err;
- err.appendf("doControlQuery::do (%d of %d): %.80s received invalid response %s", i, numChildren, queryText, childReply.str());
- logctx.CTXLOG("%s", err.str());
- throw MakeStringException(ROXIE_INTERNAL_ERROR, "%s", err.str());
- }
- Owned<IPropertyTreeIterator> meat = xml->getElements("Endpoint");
- ForEach(*meat)
- {
- CriticalBlock cb(crit);
- if (mergedStats)
- {
- mergeStats(mergedStats, &meat->query(), 0);
- }
- else
- toXML(&meat->query(), reply);
- }
- }
- }
- void doMe()
- {
- StringBuffer myReply;
- myReply.append("<Endpoint ep='");
- ep.getUrlStr(myReply);
- myReply.append("'>\n");
- try
- {
- Owned<IPropertyTree> xml = createPTreeFromXMLString(queryText); // control queries are case sensitive
- globalPackageSetManager->doControlMessage(xml, myReply, logctx);
- }
- catch(IException *E)
- {
- controlException(myReply, E, logctx);
- }
- catch(...)
- {
- controlException(myReply, MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception"), logctx);
- }
- myReply.append("</Endpoint>\n");
- CriticalBlock cb(crit);
- if (mergedStats)
- {
- Owned<IPropertyTree> xml = createPTreeFromXMLString(myReply);
- mergeStats(mergedStats, xml, 0);
- }
- else
- reply.append(myReply);
- }
- } afor(queryText, this, mergedStats, reply, ep, activeChildren.ordinality(), logctx);
- afor.For(activeChildren.ordinality()+(isMaster ? 0 : 1), 10);
- activeChildren.kill();
- if (mergedStats)
- toXML(mergedStats, reply);
- if (logctx.queryTraceLevel() > 5)
- logctx.CTXLOG("doControlQuery (%d) finished: %.80s", isMaster, queryText);
- }
- };
- Semaphore CascadeManager::globalLock(1);
- //================================================================================================================
- static void sendSoapException(SafeSocket &client, IException *E, const char *queryName)
- {
- try
- {
- if (!queryName)
- queryName = "Unknown"; // Exceptions when parsing query XML can leave queryName unset/unknowable....
- StringBuffer response;
- response.append("<").append(queryName).append("Response");
- response.append(" xmlns=\"urn:hpccsystems:ecl:").appendLower(strlen(queryName), queryName).append("\">");
- response.appendf("<Results><Result><Exception><Source>Roxie</Source><Code>%d</Code>", E->errorCode());
- response.append("<Message>");
- StringBuffer s;
- E->errorMessage(s);
- encodeXML(s.str(), response);
- response.append("</Message></Exception></Result></Results>");
- response.append("</").append(queryName).append("Response>");
- client.write(response.str(), response.length());
- }
- catch(IException *EE)
- {
- StringBuffer error("While reporting exception: ");
- EE->errorMessage(error);
- DBGLOG("%s", error.str());
- EE->Release();
- }
- #ifndef _DEBUG
- catch(...) {}
- #endif
- }
- class CSoapRequestAsyncFor : public CInterface, public CAsyncFor
- {
- private:
- const char *queryName, *queryText;
- const IRoxieContextLogger &logctx;
- IArrayOf<IPropertyTree> &requestArray;
- Linked<IQueryFactory> f;
- SafeSocket &client;
- HttpHelper &httpHelper;
- XmlReaderOptions xmlReadFlags;
- unsigned &memused;
- unsigned &slaveReplyLen;
- CriticalSection crit;
- public:
- CSoapRequestAsyncFor(const char *_queryName, IQueryFactory *_f, IArrayOf<IPropertyTree> &_requestArray, SafeSocket &_client, HttpHelper &_httpHelper, unsigned &_memused, unsigned &_slaveReplyLen, const char *_queryText, const IRoxieContextLogger &_logctx, XmlReaderOptions _xmlReadFlags) :
- f(_f), requestArray(_requestArray), client(_client), httpHelper(_httpHelper), memused(_memused), slaveReplyLen(_slaveReplyLen), logctx(_logctx), xmlReadFlags(_xmlReadFlags)
- {
- queryName = _queryName;
- queryText = _queryText;
- }
- IMPLEMENT_IINTERFACE;
- void onException(IException *E)
- {
- if (!logctx.isBlind())
- logctx.CTXLOG("FAILED: %s", queryText);
- StringBuffer error("EXCEPTION: ");
- E->errorMessage(error);
- DBGLOG("%s", error.str());
- sendSoapException(client, E, queryName);
- E->Release();
- }
- void Do(unsigned idx)
- {
- try
- {
- IPropertyTree &request = requestArray.item(idx);
- Owned<IRoxieServerContext> ctx = f->createContext(&request, client, true, false, false, httpHelper, true, logctx, xmlReadFlags);
- ctx->process();
- ctx->flush(idx);
- CriticalBlock b(crit);
- memused += ctx->getMemoryUsage();
- slaveReplyLen += ctx->getSlavesReplyLen();
- }
- catch (WorkflowException * E)
- {
- onException(E);
- }
- catch (IException * E)
- {
- onException(E);
- }
- catch (...)
- {
- onException(MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception"));
- }
- }
- };
- //================================================================================================================
- class AccessTableEntry : public CInterface
- {
- bool allow[2];
- IpSubNet subnet;
- RegExpr queries;
- StringBuffer errorMsg;
- int errorCode;
- StringBuffer queryText;
- SpinLock crappyUnsafeRegexLock;
- public:
- AccessTableEntry(bool _allow, bool _allowBlind, const char *_base, const char *_mask, const char *_queries, const char *_errorMsg, int _errorCode)
- {
- // TBD IPv6 (not sure exactly what needs doing here)
- allow[false] = _allow;
- allow[true] = _allowBlind;
- errorMsg.append(_errorMsg);
- errorCode = _errorCode;
- if (!_base)
- {
- if (_mask)
- throw MakeStringException(ROXIE_ACL_ERROR, "ip not specified");
- _base = _mask = "0.0.0.0";
- }
- else if (!_mask)
- _mask = "255.255.255.255";
- if (!subnet.set(_base,_mask))
- throw MakeStringException(ROXIE_ACL_ERROR, "Invalid mask");
- if (!_queries)
- _queries=".*";
- queries.init(_queries, true);
- queryText.append(_queries);
- }
- bool match(IpAddress &peer, const char *query, bool isBlind, bool &access, StringBuffer &errMsg, int &errCode)
- {
- {
- SpinBlock b(crappyUnsafeRegexLock);
- if (!queries.find(query))
- return false;
- }
- if (!subnet.test(peer))
- return false;
- access = allow[isBlind];
- errMsg.clear().append(errorMsg.str());
- errCode = errorCode;
- return true;
- }
- const char * queryAccessTableEntryInfo(StringBuffer &info)
- {
- info.append("<AccessInfo ");
- info.appendf(" allow='%d'", allow[false]);
- info.appendf(" allowBlind='%d'", allow[true]);
- info.append(" base='");
- subnet.getNetText(info);
- info.append("' mask='");
- subnet.getMaskText(info);
- info.appendf("' filter='%s'", queryText.str());
- info.appendf(" errorMsg='%s'", errorMsg.str());
- info.appendf(" errorCode='%d'", errorCode);
- info.append("/>\n");
- return info.str();
- }
- };
- //================================================================================================================
- class RoxieListener : public Thread, implements IRoxieListener, implements IThreadFactory
- {
- public:
- IMPLEMENT_IINTERFACE;
- RoxieListener(unsigned _poolSize, bool _suspended) : Thread("RoxieListener")
- {
- running = false;
- suspended = _suspended;
- poolSize = _poolSize;
- threadsActive = 0;
- maxThreadsActive = 0;
- }
- virtual void start()
- {
- // Note we allow a few additional threads than requested - these are the threads that return "Too many active queries" responses
- pool.setown(createThreadPool("RoxieSocketWorkerPool", this, NULL, poolSize+5, INFINITE));
- assertex(!running);
- Thread::start();
- started.wait();
- }
- virtual bool stop(unsigned timeout)
- {
- if (running)
- {
- running = false;
- join();
- Release();
- }
- return pool->joinAll(false, timeout);
- }
- void reportBadQuery(const char *name, const IRoxieContextLogger &logctx)
- {
- // MORE - may want to put in a mechanism to avoid swamping SNMP with bad query reports if someone kicks off a thor job with a typo....
- logctx.logOperatorException(NULL, NULL, 0, "Unknown query %s", name);
- }
- void checkWuAccess(bool isBlind)
- {
- // Could do some LDAP access checking here (via Dali?)
- }
- void checkAccess(IpAddress &peer, const char *queryName, const char *queryText, bool isBlind)
- {
- bool allowed = true;
- StringBuffer errorMsg;
- int errorCode;
- ForEachItemIn(idx, accessTable)
- {
- AccessTableEntry &item = accessTable.item(idx);
- item.match(peer, queryName, isBlind, allowed, errorMsg, errorCode);
- item.match(peer, queryText, isBlind, allowed, errorMsg, errorCode);
- }
- if (!allowed)
- {
- StringBuffer peerStr;
- peer.getIpText(peerStr);
- StringBuffer qText;
- if (queryText && *queryText)
- decodeXML(queryText, qText);
- StringBuffer errText;
- if (errorCode != -1)
- errText.appendf("errorCode = %d : ", errorCode);
- else
- errorCode = ROXIE_ACCESS_ERROR;
- if (errorMsg.length())
- throw MakeStringException(errorCode, "Cannot run %s : %s from host %s because %s %s", queryName, qText.str(), peerStr.str(), errText.str(), errorMsg.str());
- else
- throw MakeStringException(errorCode, "Access to %s : %s from host %s is not allowed %s", queryName, qText.str(), peerStr.str(), errText.str());
- }
- }
- virtual bool suspend(bool suspendIt)
- {
- CriticalBlock b(activeCrit);
- bool ret = suspended;
- suspended = suspendIt;
- return ret;
- }
- virtual void addAccess(bool allow, bool allowBlind, const char *ip, const char *mask, const char *query, const char *errorMsg, int errorCode)
- {
- accessTable.append(*new AccessTableEntry(allow, allowBlind, ip, mask, query, errorMsg, errorCode));
- }
- void queryAccessInfo(StringBuffer &info)
- {
- info.append("<ACCESSINFO>\n");
- ForEachItemIn(idx, accessTable)
- {
- AccessTableEntry &item = accessTable.item(idx);
- item.queryAccessTableEntryInfo(info);
- }
- info.append("</ACCESSINFO>\n");
- }
- protected:
- unsigned poolSize;
- bool running;
- bool suspended;
- Semaphore started;
- Owned<IThreadPool> pool;
- unsigned threadsActive;
- unsigned maxThreadsActive;
- CriticalSection activeCrit;
- friend class ActiveQueryLimiter;
- private:
- CIArrayOf<AccessTableEntry> accessTable;
- };
- class RoxieWorkUnitListener : public RoxieListener
- {
- public:
- RoxieWorkUnitListener(unsigned _poolSize, bool _suspended)
- : RoxieListener(_poolSize, _suspended)
- {
- }
- virtual const SocketEndpoint& queryEndpoint() const
- {
- throwUnexpected(); // MORE get rid of this function altogether?
- }
- virtual unsigned int queryPort() const
- {
- return 0;
- }
- virtual void runOnce(const char*)
- {
- UNIMPLEMENTED;
- }
- virtual void stopListening()
- {
- // Nothing to do
- }
- virtual int run()
- {
- running = true;
- started.signal();
- Owned<IRoxieDaliHelper> daliHelper = connectToDali();
- while (running)
- {
- if (daliHelper->connected())
- {
- SCMStringBuffer queueNames;
- getRoxieQueueNames(queueNames, topology->queryProp("@name"));
- if (queueNames.length())
- {
- if (traceLevel)
- DBGLOG("roxie: Waiting on queue(s) '%s'", queueNames.str());
- try
- {
- Owned<IJobQueue> queue = createJobQueue(queueNames.str());
- queue->connect();
- while (running)
- {
- Owned<IJobQueueItem> item = queue->dequeue(5000);
- if (item.get())
- {
- if (traceLevel)
- PROGLOG("roxie: Dequeued workunit request '%s'", item->queryWUID());
- pool->start((void *) item->queryWUID());
- }
- }
- }
- catch (IDaliClient_Exception *E)
- {
- if (traceLevel)
- EXCLOG(E, "roxie: Dali connection lost");
- E->Release();
- daliHelper->disconnect();
- }
- }
- }
- else
- {
- if (traceLevel)
- DBGLOG("roxie: Waiting for dali connection before waiting for queue");
- daliHelper->waitConnected();
- }
- }
- return 0;
- }
- virtual IPooledThread* createNew();
- };
- class RoxieSocketListener : public RoxieListener
- {
- unsigned port;
- unsigned listenQueue;
- Owned<ISocket> socket;
- SocketEndpoint ep;
- public:
- RoxieSocketListener(unsigned _port, unsigned _poolSize, unsigned _listenQueue, bool _suspended)
- : RoxieListener(_poolSize, _suspended)
- {
- port = _port;
- listenQueue = _listenQueue;
- ep.set(port, queryHostIP());
- }
- virtual bool stop(unsigned timeout)
- {
- if (socket)
- socket->cancel_accept();
- return RoxieListener::stop(timeout);
- }
- virtual void stopListening()
- {
- // Not threadsafe, but we only call this when generating a core file... what's the worst that can happen?
- try
- {
- DBGLOG("Closing listening socket %d", port);
- socket.clear();
- DBGLOG("Closed listening socket %d", port);
- }
- catch(...)
- {
- }
- }
- virtual void runOnce(const char *query);
- virtual int run()
- {
- DBGLOG("RoxieSocketListener (%d threads) listening to socket on port %d", poolSize, port);
- socket.setown(ISocket::create(port, listenQueue));
- running = true;
- started.signal();
- while (running)
- {
- ISocket *client = socket->accept(true);
- if (client)
- {
- client->set_linger(-1);
- pool->start(client);
- }
- }
- DBGLOG("RoxieSocketListener closed query socket");
- return 0;
- }
- virtual IPooledThread *createNew();
- virtual const SocketEndpoint &queryEndpoint() const
- {
- return ep;
- }
- virtual unsigned queryPort() const
- {
- return port;
- }
- };
- class ActiveQueryLimiter
- {
- RoxieListener *parent;
- public:
- bool accepted;
- ActiveQueryLimiter(RoxieListener *_parent) : parent(_parent)
- {
- CriticalBlock b(parent->activeCrit);
- if (parent->suspended)
- {
- accepted = false;
- if (traceLevel > 1)
- DBGLOG("Rejecting query since Roxie server pool %d is suspended ", parent->queryPort());
- }
- else
- {
- accepted = (parent->threadsActive < parent->poolSize);
- if (accepted && parent->threadsActive > parent->maxThreadsActive)
- {
- parent->maxThreadsActive = parent->threadsActive;
- if (traceLevel > 1)
- DBGLOG("Maximum queries active %d of %d for pool %d", parent->threadsActive, parent->poolSize, parent->queryPort());
- }
- if (!accepted && traceLevel > 5)
- DBGLOG("Too many active queries (%d >= %d)", parent->threadsActive, parent->poolSize);
- }
- parent->threadsActive++;
- }
- ~ActiveQueryLimiter()
- {
- CriticalBlock b(parent->activeCrit);
- parent->threadsActive--;
- }
- };
- class RoxieQueryWorker : public CInterface, implements IPooledThread
- {
- public:
- IMPLEMENT_IINTERFACE;
- RoxieQueryWorker(RoxieListener *_pool)
- {
- pool = _pool;
- qstart = msTick();
- time(&startTime);
- }
- // interface IPooledThread
- virtual void init(void *)
- {
- qstart = msTick();
- time(&startTime);
- }
- virtual bool canReuse()
- {
- return true;
- }
- virtual bool stop()
- {
- ERRLOG("RoxieQueryWorker stopped with queries active");
- return true;
- }
- protected:
- RoxieListener *pool;
- unsigned qstart;
- time_t startTime;
- inline RoxieQueryStats &getStats(unsigned priority)
- {
- switch(priority)
- {
- case 0: return loQueryStats;
- case 1: return hiQueryStats;
- case 2: return slaQueryStats;
- default: return unknownQueryStats;
- }
- }
- void noteQuery(bool failed, unsigned elapsedTime, unsigned priority)
- {
- Owned <IJlibDateTime> now = createDateTimeNow();
- unsigned y,mo,d,h,m,s,n;
- now->getLocalTime(h, m, s, n);
- now->getLocalDate(y, mo, d);
- lastQueryTime = h*10000 + m * 100 + s;
- lastQueryDate = y*10000 + mo * 100 + d;
- RoxieQueryStats &stats = getStats(priority);
- stats.noteQuery(failed, elapsedTime);
- }
- };
- /**
- * RoxieWorkUnitWorker is the threadpool member that runs a query submitted as a
- * workunit via a job queue. A temporary IQueryFactory object is created for the
- * workunit and then executed.
- *
- * Any slaves that need to load the query do so using a lazy load mechanism, checking
- * whether the wuid named in the logging prefix info can be loaded any time a query
- * is received for which no factory exists. Any query that a slave loads as a
- * result is added to a cache to ensure that it stays around until the server's query
- * terminates - a ROXIE_UNLOAD message is broadcast at that time to allow the slaves
- * to release any cached IQueryFactory objects.
- *
- **/
- class RoxieWorkUnitWorker : public RoxieQueryWorker
- {
- public:
- RoxieWorkUnitWorker(RoxieListener *_pool)
- : RoxieQueryWorker(_pool)
- {
- }
- virtual void init(void *_r)
- {
- wuid.set((const char *) _r);
- RoxieQueryWorker::init(_r);
- }
- virtual void main()
- {
- Owned <IRoxieDaliHelper> daliHelper = connectToDali();
- Owned<IConstWorkUnit> wu = daliHelper->attachWorkunit(wuid.get(), NULL);
- if (!wu)
- throw MakeStringException(ROXIE_DALI_ERROR, "Failed to open workunit %s", wuid.get());
- Owned<IQueryFactory> queryFactory = createServerQueryFactoryFromWu(wuid.get());
- Owned<StringContextLogger> logctx = new StringContextLogger(wuid.get());
- doMain(wu, queryFactory, *logctx);
- sendUnloadMessage(queryFactory->queryHash(), wuid.get(), *logctx);
- }
- void doMain(IConstWorkUnit *wu, IQueryFactory *queryFactory, StringContextLogger &logctx)
- {
- bool failed = true; // many paths to failure, only one to success...
- unsigned memused = 0;
- unsigned slavesReplyLen = 0;
- unsigned priority = (unsigned) -2;
- try
- {
- atomic_inc(&queryCount);
- bool isBlind = wu->getDebugValueBool("blindLogging", false);
- if (pool)
- {
- pool->checkWuAccess(isBlind);
- ActiveQueryLimiter l(pool);
- if (!l.accepted)
- {
- IException *e = MakeStringException(ROXIE_TOO_MANY_QUERIES, "Too many active queries");
- if (trapTooManyActiveQueries)
- logctx.logOperatorException(e, __FILE__, __LINE__, NULL);
- throw e;
- }
- }
- isBlind = isBlind || blindLogging;
- logctx.setBlind(isBlind);
- priority = queryFactory->getPriority();
- switch (priority)
- {
- case 0: loQueryStats.noteActive(); break;
- case 1: hiQueryStats.noteActive(); break;
- case 2: slaQueryStats.noteActive(); break;
- }
- Owned<IRoxieServerContext> ctx = queryFactory->createContext(wu, logctx);
- try
- {
- ctx->process();
- memused = ctx->getMemoryUsage();
- slavesReplyLen = ctx->getSlavesReplyLen();
- ctx->done(false);
- failed = false;
- }
- catch(...)
- {
- memused = ctx->getMemoryUsage();
- slavesReplyLen = ctx->getSlavesReplyLen();
- ctx->done(true);
- throw;
- }
- }
- catch (WorkflowException *E)
- {
- reportException(wu, E, logctx);
- E->Release();
- }
- catch (IException *E)
- {
- reportException(wu, E, logctx);
- E->Release();
- }
- #ifndef _DEBUG
- catch(...)
- {
- IException *E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception");
- reportException(wu, E, logctx);
- E->Release();
- }
- #endif
- unsigned elapsed = msTick() - qstart;
- noteQuery(failed, elapsed, priority);
- queryFactory->noteQuery(startTime, failed, elapsed, memused, slavesReplyLen, 0);
- if (logctx.queryTraceLevel() > 2)
- logctx.dumpStats();
- if (logctx.queryTraceLevel() && (logctx.queryTraceLevel() > 2 || logFullQueries || logctx.intercept))
- logctx.CTXLOG("COMPLETE: %s complete in %d msecs memory %d Mb priority %d slavesreply %d", wuid.get(), elapsed, memused, priority, slavesReplyLen);
- logctx.flush(true, false);
- }
- private:
- void reportException(IConstWorkUnit *wu, IException *E, const IRoxieContextLogger &logctx)
- {
- logctx.CTXLOG("FAILED: %s", wuid.get());
- StringBuffer error;
- E->errorMessage(error);
- logctx.CTXLOG("EXCEPTION: %s", error.str());
- addWuException(wu, E);
- WorkunitUpdate w(&wu->lock());
- w->setState(WUStateFailed);
- }
- StringAttr wuid;
- };
- class RoxieSocketWorker : public RoxieQueryWorker
- {
- Owned<SafeSocket> client;
- Owned<CDebugCommandHandler> debugCmdHandler;
- SocketEndpoint ep;
- public:
- RoxieSocketWorker(RoxieListener *_pool, SocketEndpoint &_ep)
- : RoxieQueryWorker(_pool), ep(_ep)
- {
- }
- // interface IPooledThread
- virtual void init(void *_r)
- {
- client.setown(new CSafeSocket((ISocket *) _r));
- RoxieQueryWorker::init(_r);
- }
- virtual void main()
- {
- doMain("");
- }
- virtual void runOnce(const char *query)
- {
- doMain(query);
- }
- private:
- static void sendHttpServerTooBusy(SafeSocket &client, const IRoxieContextLogger &logctx)
- {
- StringBuffer message;
- message.append("HTTP/1.0 503 Server Too Busy\r\n\r\n");
- message.append("Server too busy, please try again later");
- StringBuffer err("Too many active queries"); // write out Too many active queries - make searching for this error consistent
- if (!trapTooManyActiveQueries)
- {
- err.appendf(" %s", message.str());
- logctx.CTXLOG("%s", err.str());
- }
- else
- {
- IException *E = MakeStringException(ROXIE_TOO_MANY_QUERIES, "%s", err.str());
- logctx.logOperatorException(E, __FILE__, __LINE__, "%s", message.str());
- E->Release();
- }
- try
- {
- client.write(message.str(), message.length());
- }
- catch (IException *E)
- {
- logctx.logOperatorException(E, __FILE__, __LINE__, "Exception caught in sendHttpServerTooBusy");
- E->Release();
- }
- catch (...)
- {
- logctx.logOperatorException(NULL, __FILE__, __LINE__, "sendHttpServerTooBusy write failed (Unknown exception)");
- }
- }
- void sanitizeQuery(Owned<IPropertyTree> &queryXML, StringAttr &queryName, StringBuffer &saniText, bool isHTTP, const char *&uid, bool &isRequest, bool &isRequestArray, bool &isBlind, bool &isDebug)
- {
- if (queryXML)
- {
- queryName.set(queryXML->queryName());
- isRequest = false;
- isRequestArray = false;
- if (isHTTP)
- {
- if (stricmp(queryName, "envelope") == 0)
- {
- queryXML.setown(queryXML->getPropTree("Body/*"));
- if (!queryXML)
- throw MakeStringException(ROXIE_DATA_ERROR, "Malformed SOAP request (missing Body)");
- String reqName(queryXML->queryName());
- queryXML->removeProp("@xmlns:m");
- // following code is moved from main() - should be no performance hit
- String requestString("Request");
- String requestArrayString("RequestArray");
- if (reqName.endsWith(requestArrayString))
- {
- isRequestArray = true;
- queryName.set(reqName.toCharArray(), reqName.length() - requestArrayString.length());
- }
- else if (reqName.endsWith(requestString))
- {
- isRequest = true;
- queryName.set(reqName.toCharArray(), reqName.length() - requestString.length());
- }
- else
- queryName.set(reqName.toCharArray());
- queryXML->renameProp("/", queryName.get()); // reset the name of the tree
- }
- else
- throw MakeStringException(ROXIE_DATA_ERROR, "Malformed SOAP request");
- }
- // convert to XML with attribute values in single quotes - makes replaying queries easier
- uid = queryXML->queryProp("@uid");
- if (!uid)
- uid = "-";
- isBlind = queryXML->getPropBool("@blind", false) || queryXML->getPropBool("_blind", false);
- isDebug = queryXML->getPropBool("@debug") || queryXML->getPropBool("_Probe", false);
- toXML(queryXML, saniText, 0, isBlind ? (XML_SingleQuoteAttributeValues | XML_Sanitize) : XML_SingleQuoteAttributeValues);
- }
- else
- throw MakeStringException(ROXIE_DATA_ERROR, "Malformed request");
- }
- void doMain(const char *runQuery)
- {
- StringBuffer rawText(runQuery);
- unsigned priority = (unsigned) -2;
- unsigned memused = 0;
- Owned<CascadeManager> cascade;
- IpAddress peer;
- bool continuationNeeded = false;
- bool isStatus = false;
- readAnother:
- Owned<IDebuggerContext> debuggerContext;
- unsigned slavesReplyLen = 0;
- HttpHelper httpHelper;
- try
- {
- if (client)
- {
- client->querySocket()->getPeerAddress(peer);
- if (!client->readBlock(rawText, WAIT_FOREVER, &httpHelper, continuationNeeded, isStatus, maxBlockSize))
- {
- if (traceLevel > 8)
- {
- StringBuffer b;
- DBGLOG("No data reading query from socket");
- }
- client.clear();
- return;
- }
- }
- if (continuationNeeded)
- {
- qstart = msTick();
- time(&startTime);
- }
- unknownQueryStats.noteActive();
- atomic_inc(&queryCount);
- }
- catch (IException * E)
- {
- if (traceLevel > 0)
- {
- StringBuffer b;
- DBGLOG("Error reading query from socket: %s", E->errorMessage(b).str());
- }
- E->Release();
- client.clear();
- return;
- }
- bool isXml = true;
- bool isRaw = false;
- bool isHTTP = httpHelper.isHttp();
- bool isBlocked = false;
- bool trim = false;
- bool failed = false;
- Owned<IPropertyTree> queryXml;
- Owned<IQueryFactory> queryFactory;
- StringBuffer sanitizedText;
- StringAttr queryName;
- StringBuffer peerStr;
- peer.getIpText(peerStr);
- const char *uid = "-";
- unsigned instanceId = getNextInstanceId();
- StringBuffer ctxstr;
- Owned<StringContextLogger> _logctx = new StringContextLogger(ep.getIpText(ctxstr).appendf(":%u{%u}", ep.port, instanceId).str());
- StringContextLogger &logctx = *_logctx.get();
- try
- {
- // Note - control queries have to be formatted without spaces..
- if (strnicmp(rawText.str(), "<control:lock", 13)==0 && !isalpha(rawText.charAt(13)))
- {
- if (logctx.queryTraceLevel() > 8)
- logctx.CTXLOG("Got lock request %s", rawText.str());
- FlushingStringBuffer response(client, false, true, false, false, logctx);
- response.startDataset("Control", NULL, (unsigned) -1);
- if (!cascade)
- cascade.setown(new CascadeManager(logctx));
- StringBuffer s;
- cascade->doLockGlobal(s, false);
- response.append(s);
- if (logctx.queryTraceLevel() > 8)
- logctx.CTXLOG("lock reply %s", s.str());
- response.flush(true);
- unsigned replyLen = 0;
- client->write(&replyLen, sizeof(replyLen));
- rawText.clear();
- goto readAnother;
- }
- else if (strnicmp(rawText.str(), "<control:childlock", 18)==0 && !isalpha(rawText.charAt(18)))
- {
- if (logctx.queryTraceLevel() > 8)
- logctx.CTXLOG("Got childlock request %s", rawText.str());
- FlushingStringBuffer response(client, false, true, false, false, logctx);
- response.startDataset("Control", NULL, (unsigned) -1);
- if (!cascade)
- cascade.setown(new CascadeManager(logctx));
- StringBuffer s;
- cascade->doLockChild(rawText.str(), s);
- response.append(s);
- if (logctx.queryTraceLevel() > 8)
- logctx.CTXLOG("childlock reply %s", s.str());
- response.flush(true);
- unsigned replyLen = 0;
- client->write(&replyLen, sizeof(replyLen));
- rawText.clear();
- unknownQueryStats.noteComplete();
- goto readAnother;
- }
- else if (strnicmp(rawText.str(), "<control:", 9)==0)
- {
- Owned<IPropertyTree> queryXML = createPTreeFromXMLString(rawText.str()); // This is just done to check it is valid XML and make error reporting better...
- queryXML.clear();
- bool doControlQuery = true;
-
- FlushingStringBuffer response(client, false, true, false, isHTTP, logctx);
- response.startDataset("Control", NULL, (unsigned) -1);
- if (strnicmp(rawText.str(), "<control:aclupdate", 18)==0 && !isalpha(rawText.charAt(18)))
- {
- queryXml.setown(createPTreeFromXMLString(rawText.str(), ipt_caseInsensitive, (XmlReaderOptions)(xr_ignoreWhiteSpace|xr_ignoreNameSpaces)));
- IPropertyTree *aclTree = queryXml->queryPropTree("ACL");
- if (aclTree)
- {
- Owned<IPropertyTreeIterator> accesses = aclTree->getElements("Access");
- ForEach(*accesses)
- {
- IPropertyTree &access = accesses->query();
- try
- {
- pool->addAccess(access.getPropBool("@allow", true), access.getPropBool("@allowBlind", true), access.queryProp("@ip"), access.queryProp("@mask"), access.queryProp("@query"), access.queryProp("@error"), access.getPropInt("@errorCode", -1));
- }
- catch (IException *E)
- {
- StringBuffer s, x;
- E->errorMessage(s);
- E->Release();
- toXML(&access, x, 0, 0);
- throw MakeStringException(ROXIE_ACL_ERROR, "Error in access statement %s: %s", x.str(), s.str());
- }
- }
- }
- }
- else if (strnicmp(rawText.str(), "<control:queryaclinfo", 21)==0 && !isalpha(rawText.charAt(21)))
- {
- StringBuffer info;
- info.append("<Endpoint ep='");
- ep.getUrlStr(info);
- info.append("'>\n");
- pool->queryAccessInfo(info);
- info.append("</Endpoint>\n");
- response.append(info.str());
- doControlQuery = false;
- }
- if (doControlQuery)
- {
- if(!cascade)
- cascade.setown(new CascadeManager(logctx));
- StringBuffer s;
- cascade->doControlQuery(ep, rawText, s);
- response.append(s);
- }
- }
- else if (isStatus)
- {
- client->write("OK", 2);
- }
- else
- {
- try
- {
- queryXml.setown(createPTreeFromXMLString(rawText.str(), ipt_caseInsensitive, (XmlReaderOptions)(defaultXmlReadFlags | xr_ignoreNameSpaces)));
- }
- catch (IException *E)
- {
- logctx.logOperatorException(E, __FILE__, __LINE__, "Invalid XML received from %s:%d - %s", peerStr.str(), pool->queryPort(), rawText.str());
- logctx.CTXLOG("ERROR: Invalid XML received from %s:%d - %s", peerStr.str(), pool->queryPort(), rawText.str());
- throw;
- }
- bool isRequest = false;
- bool isRequestArray = false;
- bool isBlind = false;
- bool isDebug = false;
- sanitizeQuery(queryXml, queryName, sanitizedText, isHTTP, uid, isRequest, isRequestArray, isBlind, isDebug);
- pool->checkAccess(peer, queryName, sanitizedText, isBlind);
- if (isDebug)
- {
- if (!debugPermitted || !ownEP.port)
- throw MakeStringException(ROXIE_ACCESS_ERROR, "Debug queries are not permitted on this system");
- }
- isBlind = isBlind || blindLogging;
- logctx.setBlind(isBlind);
- if (logFullQueries)
- {
- StringBuffer soapStr;
- (isRequest) ? soapStr.append("SoapRequest") : (isRequestArray) ? soapStr.append("SoapRequest") : soapStr.clear();
- logctx.CTXLOG("%s %s:%d %s %s %s", isBlind ? "BLIND:" : "QUERY:", peerStr.str(), pool->queryPort(), uid, soapStr.str(), sanitizedText.str());
- }
- if (strnicmp(rawText.str(), "<debug:", 7)==0)
- {
- if (!debugPermitted || !ownEP.port)
- throw MakeStringException(ROXIE_ACCESS_ERROR, "Debug queries are not permitted on this system");
- if (!debuggerContext)
- {
- if (!uid)
- #ifdef _DEBUG
- uid="*";
- #else
- throw MakeStringException(ROXIE_DEBUG_ERROR, "Debug id not specified");
- #endif
- Owned<IRoxieDebugSessionManager> QM = globalPackageSetManager->getRoxieDebugSessionManager();
- debuggerContext.setown(QM->lookupDebuggerContext(uid));
- if (!debuggerContext)
- throw MakeStringException(ROXIE_DEBUG_ERROR, "No active query matching context %s found", uid);
- if (!debugCmdHandler.get())
- debugCmdHandler.setown(new CDebugCommandHandler);
- }
- FlushingStringBuffer response(client, false, true, false, isHTTP, logctx);
- response.startDataset("Debug", NULL, (unsigned) -1);
- debugCmdHandler->doDebugCommand(queryXml, debuggerContext, response);
- }
- else
- {
- ActiveQueryLimiter l(pool);
- if (!l.accepted)
- {
- if (isHTTP)
- {
- sendHttpServerTooBusy(*client, logctx);
- logctx.CTXLOG("FAILED: %s", sanitizedText.str());
- logctx.CTXLOG("EXCEPTION: Too many active queries");
- }
- else
- {
- IException *e = MakeStringException(ROXIE_TOO_MANY_QUERIES, "Too many active queries");
- if (trapTooManyActiveQueries)
- logctx.logOperatorException(e, __FILE__, __LINE__, NULL);
- throw e;
- }
- }
- else
- {
- queryFactory.setown(globalPackageSetManager->getQuery(queryName, logctx));
- if (isHTTP)
- client->setHttpMode(queryName, isRequestArray);
- if (queryFactory)
- {
- bool stripWhitespace = queryFactory->getDebugValueBool("stripWhitespaceFromStoredDataset", 0 != (xr_ignoreWhiteSpace & defaultXmlReadFlags));
- stripWhitespace = queryXml->getPropBool("_stripWhitespaceFromStoredDataset", stripWhitespace);
- XmlReaderOptions xmlReadFlags = (XmlReaderOptions)((defaultXmlReadFlags & ~xr_ignoreWhiteSpace) |
- (stripWhitespace ? xr_ignoreWhiteSpace : xr_none));
- if (xmlReadFlags != defaultXmlReadFlags)
- {
- // we need to reparse input xml, as global whitespace setting has been overridden
- queryXml.setown(createPTreeFromXMLString(rawText.str(), ipt_caseInsensitive, (XmlReaderOptions)(xmlReadFlags|xr_ignoreNameSpaces)));
- sanitizeQuery(queryXml, queryName, sanitizedText, isHTTP, uid, isRequest, isRequestArray, isBlind, isDebug);
- }
- IArrayOf<IPropertyTree> requestArray;
- if (isHTTP)
- {
- if (isRequestArray)
- {
- StringBuffer reqIterString;
- reqIterString.append(queryName).append("Request");
- Owned<IPropertyTreeIterator> reqIter = queryXml->getElements(reqIterString.str());
- ForEach(*reqIter)
- {
- IPropertyTree *fixedreq = createPTree(queryName, ipt_caseInsensitive);
- Owned<IPropertyTreeIterator> iter = reqIter->query().getElements("*");
- ForEach(*iter)
- {
- fixedreq->addPropTree(iter->query().queryName(), LINK(&iter->query()));
- }
- requestArray.append(*fixedreq);
- }
- }
- else
- {
- IPropertyTree *fixedreq = createPTree(queryName, ipt_caseInsensitive);
- Owned<IPropertyTreeIterator> iter = queryXml->getElements("*");
- ForEach(*iter)
- {
- fixedreq->addPropTree(iter->query().queryName(), LINK(&iter->query()));
- }
- requestArray.append(*fixedreq);
- }
- trim = true;
- }
- else
- {
- const char *format = queryXml->queryProp("@format");
- if (format)
- {
- if (stricmp(format, "raw") == 0)
- {
- isRaw = true;
- isXml = false;
- isBlocked = (client != NULL);
- }
- else if (stricmp(format, "bxml") == 0)
- {
- isBlocked = true;
- }
- else if (stricmp(format, "ascii") == 0)
- {
- isRaw = false;
- isXml = false;
- }
- else if (stricmp(format, "xml") != 0) // xml is the default
- throw MakeStringException(ROXIE_INVALID_INPUT, "Unsupported format specified: %s", format);
- }
- trim = queryXml->getPropBool("@trim", false);;
- logctx.setIntercept(queryXml->getPropBool("@log", false));
- logctx.setTraceLevel(queryXml->getPropInt("@traceLevel", traceLevel));
- }
-
- priority = queryFactory->getPriority();
- switch (priority)
- {
- case 0: loQueryStats.noteActive(); break;
- case 1: hiQueryStats.noteActive(); break;
- case 2: slaQueryStats.noteActive(); break;
- }
- unknownQueryStats.noteComplete();
- if (isHTTP)
- {
- CSoapRequestAsyncFor af(queryName, queryFactory, requestArray, *client, httpHelper, memused, slavesReplyLen, sanitizedText, logctx, xmlReadFlags);
- af.For(requestArray.length(), numRequestArrayThreads);
- }
- else
- {
- Owned<IRoxieServerContext> ctx = queryFactory->createContext(queryXml, *client, isXml, isRaw, isBlocked, httpHelper, trim, logctx, xmlReadFlags);
- if (client && !ctx->outputResultsToSocket())
- {
- unsigned replyLen = 0;
- client->write(&replyLen, sizeof(replyLen));
- client.clear();
- }
- try
- {
- ctx->process();
- memused = ctx->getMemoryUsage();
- slavesReplyLen = ctx->getSlavesReplyLen();
- ctx->done(false);
- }
- catch(...)
- {
- memused = ctx->getMemoryUsage();
- slavesReplyLen = ctx->getSlavesReplyLen();
- ctx->done(true);
- throw;
- }
- }
- }
- else
- {
- pool->reportBadQuery(queryName.get(), logctx);
- throw MakeStringException(ROXIE_UNKNOWN_QUERY, "Unknown query %s", queryName.get());
- }
- }
- }
- }
- }
- catch (WorkflowException * E)
- {
- failed = true;
- logctx.CTXLOG("FAILED: %s", sanitizedText.str());
- StringBuffer error;
- E->errorMessage(error);
- logctx.CTXLOG("EXCEPTION: %s", error.str());
- unsigned code = E->errorCode();
- if (QUERYINTERFACE(E, ISEH_Exception))
- code = ROXIE_INTERNAL_ERROR;
- else if (QUERYINTERFACE(E, IOutOfMemException))
- code = ROXIE_MEMORY_ERROR;
- if (client)
- {
- if (isHTTP)
- sendSoapException(*client, E, queryName);
- else
- client->sendException("Roxie", code, error.str(), isBlocked, logctx);
- }
- else
- {
- fprintf(stderr, "EXCEPTION: %s", error.str());
- }
- E->Release();
- }
- catch (IException * E)
- {
- failed = true;
- logctx.CTXLOG("FAILED: %s", sanitizedText.str());
- StringBuffer error;
- E->errorMessage(error);
- logctx.CTXLOG("EXCEPTION: %s", error.str());
- unsigned code = E->errorCode();
- if (QUERYINTERFACE(E, ISEH_Exception))
- code = ROXIE_INTERNAL_ERROR;
- else if (QUERYINTERFACE(E, IOutOfMemException))
- code = ROXIE_MEMORY_ERROR;
- if (client)
- {
- if (isHTTP)
- sendSoapException(*client, E, queryName);
- else
- client->sendException("Roxie", code, error.str(), isBlocked, logctx);
- }
- else
- {
- fprintf(stderr, "EXCEPTION: %s", error.str());
- }
- E->Release();
- }
- #ifndef _DEBUG
- catch(...)
- {
- failed = true;
- logctx.CTXLOG("FAILED: %s", sanitizedText.str());
- logctx.CTXLOG("EXCEPTION: Unknown exception");
- {
- if (isHTTP)
- {
- Owned<IException> E = MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown exception");
- sendSoapException(*client, E, queryName);
- }
- else
- client->sendException("Roxie", ROXIE_INTERNAL_ERROR, "Unknown exception", isBlocked, logctx);
- }
- }
- #endif
- if (isHTTP)
- {
- try
- {
- client->flush();
- }
- catch (IException * E)
- {
- StringBuffer error("RoxieSocketWorker failed to write to socket ");
- E->errorMessage(error);
- logctx.CTXLOG("%s", error.str());
- E->Release();
- }
- //#ifndef _DEBUG
- catch(...)
- {
- logctx.CTXLOG("RoxieSocketWorker failed to write to socket (Unknown exception)");
- }
- //#endif
- }
- unsigned bytesOut = client? client->bytesOut() : 0;
- unsigned elapsed = msTick() - qstart;
- noteQuery(failed, elapsed, priority);
- if (queryFactory)
- {
- queryFactory->noteQuery(startTime, failed, elapsed, memused, slavesReplyLen, bytesOut);
- queryFactory.clear();
- }
- if (logctx.queryTraceLevel() > 2)
- logctx.dumpStats();
- if (logctx.queryTraceLevel() && (logctx.queryTraceLevel() > 2 || logFullQueries || logctx.intercept))
- if (queryName.get())
- logctx.CTXLOG("COMPLETE: %s %s from %s complete in %d msecs memory %d Mb priority %d slavesreply %d resultsize %d continue %d", queryName.get(), uid, peerStr.str(), elapsed, memused, priority, slavesReplyLen, bytesOut, continuationNeeded);
- if (continuationNeeded)
- {
- rawText.clear();
- goto readAnother;
- }
- else
- {
- logctx.flush(true, false);
- try
- {
- if (client && !isHTTP && !isStatus)
- {
- if (logctx.intercept)
- {
- FlushingStringBuffer response(client, isBlocked, isXml, isRaw, false, logctx);
- response.startDataset("Tracing", NULL, (unsigned) -1);
- logctx.outputXML(response);
- }
- unsigned replyLen = 0;
- client->write(&replyLen, sizeof(replyLen));
- }
- client.clear();
- }
- catch (IException * E)
- {
- StringBuffer error("RoxieSocketWorker failed to close socket ");
- E->errorMessage(error);
- logctx.CTXLOG("%s", error.str()); // MORE - audience?
- E->Release();
- }
- //#ifndef _DEBUG
- catch(...)
- {
- logctx.CTXLOG("RoxieSocketWorker failed to close socket (Unknown exception)"); // MORE - audience?
- }
- //#endif
- }
- }
- };
- //=================================================================================
- IArrayOf<IRoxieListener> socketListeners;
- IPooledThread *RoxieWorkUnitListener::createNew()
- {
- return new RoxieWorkUnitWorker(this);
- }
- IPooledThread *RoxieSocketListener::createNew()
- {
- return new RoxieSocketWorker(this, ep);
- }
- void RoxieSocketListener::runOnce(const char *query)
- {
- Owned<RoxieSocketWorker> p = new RoxieSocketWorker(this, ep);
- p->runOnce(query);
- }
- IRoxieListener *createRoxieSocketListener(unsigned port, unsigned poolSize, unsigned listenQueue, bool suspended)
- {
- if (traceLevel)
- DBGLOG("Creating Roxie socket listener, pool size %d, listen queue %d%s", poolSize, listenQueue, suspended?" SUSPENDED":"");
- return new RoxieSocketListener(port, poolSize, listenQueue, suspended);
- }
- IRoxieListener *createRoxieWorkUnitListener(unsigned poolSize, bool suspended)
- {
- if (traceLevel)
- DBGLOG("Creating Roxie workunit listener, pool size %d%s", poolSize, suspended?" SUSPENDED":"");
- return new RoxieWorkUnitListener(poolSize, suspended);
- }
- bool suspendRoxieListener(unsigned port, bool suspended)
- {
- ForEachItemIn(idx, socketListeners)
- {
- IRoxieListener &listener = socketListeners.item(idx);
- if (listener.queryPort()==port)
- return listener.suspend(suspended);
- }
- throw MakeStringException(ROXIE_INTERNAL_ERROR, "Unknown port %u specified in suspendRoxieListener", port);
- }
- //================================================================================================================================
- #ifdef _USE_CPPUNIT
- // 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...
- #if _MSC_VER==1200
- #undef protected
- #endif
- #include <cppunit/extensions/HelperMacros.h>
- #define ASSERT(a) { if (!(a)) CPPUNIT_ASSERT(a); }
- static const char *sortAlgorithm;
- class TestMetaData : public CInterface, implements IOutputMetaData
- {
- public:
- IMPLEMENT_IINTERFACE;
- virtual size32_t getRecordSize(const void *) { return 10; }
- virtual size32_t getMinRecordSize() const { return 10; }
- virtual size32_t getFixedSize() const { return 10; }
- virtual void toXML(const byte * self, IXmlWriter & out) {}
- virtual unsigned getVersion() const { return OUTPUTMETADATA_VERSION; }
- virtual unsigned getMetaFlags() { return 0; }
- virtual void destruct(byte * self) {}
- virtual IOutputRowSerializer * createRowSerializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
- virtual IOutputRowDeserializer * createRowDeserializer(ICodeContext * ctx, unsigned activityId) { return NULL; }
- virtual ISourceRowPrefetcher * createRowPrefetcher(ICodeContext * ctx, unsigned activityId) { return NULL; }
- virtual IOutputMetaData * querySerializedMeta() { return NULL; }
- virtual void walkIndirectMembers(const byte * self, IIndirectMemberVisitor & visitor) {}
- } testMeta;
- class TestInput : public CInterface, implements IRoxieInput
- {
- char const * const *input;
- IRoxieSlaveContext *ctx;
- unsigned endSeen;
- bool eof;
- unsigned count;
- unsigned __int64 totalCycles;
- size32_t recordSize;
- unsigned activityId;
- public:
- enum { STATEreset, STATEstarted, STATEstopped } state;
- bool allRead;
- IMPLEMENT_IINTERFACE;
- TestInput(IRoxieSlaveContext *_ctx, char const * const *_input)
- {
- ctx = _ctx;
- input = _input;
- count = 0;
- eof = false;
- allRead = false;
- endSeen = 0;
- recordSize = testMeta.getFixedSize();
- state = STATEreset;
- totalCycles = 0;
- activityId = 1;
- }
- virtual IOutputMetaData * queryOutputMeta() const { return &testMeta; }
- virtual void prestart(unsigned parentExtractSize, const byte *parentExtract)
- {
- ASSERT(state == STATEreset);
- }
- virtual void start(unsigned parentExtractSize, const byte *parentExtract, bool paused)
- {
- ASSERT(state == STATEreset);
- state = STATEstarted;
- }
- virtual IRoxieServerActivity *queryActivity()
- {
- throwUnexpected();
- }
- virtual IIndexReadActivityInfo *queryIndexReadActivity()
- {
- throwUnexpected();
- }
- virtual void stop(bool aborting)
- {
- state = STATEstopped;
- }
- virtual void reset()
- {
- ASSERT(state == STATEstopped);
- eof = false; count = 0; endSeen = 0; allRead = false; state = STATEreset; totalCycles = 0;
- }
- virtual void resetEOF()
- {
- throwUnexpected();
- }
- virtual void checkAbort() {}
- virtual unsigned queryId() const { return activityId; };
- virtual const void *nextInGroup()
- {
- ActivityTimer t(totalCycles, timeActivities, ctx->queryDebugContext());
- ASSERT(state == STATEstarted);
- ASSERT(allRead || !eof);
- if (eof)
- return NULL;
- const char *nextSource = input[count++];
- if (nextSource)
- {
- endSeen = 0;
- void *ret = ctx->queryRowManager().ALLOCATE(recordSize);
- memset(ret, 0, recordSize);
- strncpy((char *) ret, nextSource, recordSize);
- return ret;
- }
- else
- {
- endSeen++;
- if (endSeen==2)
- eof = true;
- return NULL;
- }
- }
- virtual bool nextGroup(ConstPointerArray & group)
- {
- const void * next;
- while ((next = nextInGroup()) != NULL)
- group.append(next);
- if (group.ordinality())
- return true;
- return false;
- }
- virtual unsigned __int64 queryTotalCycles() const { return totalCycles; }
- virtual unsigned __int64 queryLocalCycles() const { return totalCycles; }
- virtual IRoxieInput *queryInput(unsigned idx) const
- {
- return NULL;
- }
- };
- struct SortActivityTest : public ccdserver_hqlhelper::CThorSortArg {
- public:
- struct CompareClass : public ICompare {
- virtual int docompare(const void * _left, const void * _right) const {
- return memcmp(_left, _right, 5);
- }
- } compare;
- virtual ICompare * queryCompare() { return &compare; }
- virtual IOutputMetaData * queryOutputMeta()
- {
- return &testMeta;
- }
- virtual unsigned getAlgorithmFlags() { return TAFunstable; }
- virtual const char * queryAlgorithm() { return sortAlgorithm; }
- };
- extern "C" IHThorArg * sortActivityTestFactory() { return new SortActivityTest; }
- struct MergeActivityTest : public ccdserver_hqlhelper::CThorMergeArg {
- static bool isDedup;
- public:
- struct CompareClass : public ICompare {
- virtual int docompare(const void * _left, const void * _right) const {
- return memcmp(_left, _right, 5);
- }
- } compare;
- virtual ICompare * queryCompare() { return &compare; }
- virtual IOutputMetaData * queryOutputMeta()
- {
- return &testMeta;
- }
- virtual bool dedup() { return isDedup; }
- };
- bool MergeActivityTest::isDedup = false;
- extern "C" IHThorArg * mergeActivityTestFactory() { return new MergeActivityTest; }
- struct PrefetchProjectActivityTest : public ccdserver_hqlhelper::CThorPrefetchProjectArg {
- virtual IOutputMetaData * queryOutputMeta()
- {
- return &testMeta;
- }
- virtual void onCreate(ICodeContext * _ctx, IHThorArg *, MemoryBuffer * in) {
- ctx = _ctx;
- child8.setown(ctx->resolveChildQuery(8,this));
- }
- // mutable rtlRowBuilder ex39R4;
- Owned<IThorChildGraph> child8;
- virtual IThorChildGraph *queryChild() { return child8; }
- virtual bool preTransform(rtlRowBuilder & extract, const void * _left, unsigned __int64 count) {
- const unsigned char * left = (const unsigned char *) _left;
- extract.ensureAvailable(1);
- memcpy((char *)(extract.getbytes() + 0),(char *)(left + 0),1);
- // child8->execute(5,ex39R4.getbytes());
- return true;
- }
- virtual size32_t transform(ARowBuilder & _self, const void * _left, IEclGraphResults * results, unsigned __int64 count) {
- unsigned char * self = (unsigned char *) _self.getSelf();
- const unsigned char * left = (const unsigned char *) _left;
- memcpy((char *)(self + 0),(char *)(left + 0),10);
- rtlDataAttr v79R4;
- unsigned int v89R4;
- results->getResult(v89R4,v79R4.refdata(),0);
- memcpy(self + count - 1, ((unsigned char *)v79R4.getbytes() + count - 1), 1);
- return 10;
- }
- };
- extern "C" IHThorArg * prefetchProjectActivityTestFactory() { return new PrefetchProjectActivityTest; }
- struct TempTableActivityTest : public ccdserver_hqlhelper::CThorTempTableArg {
- virtual IOutputMetaData * queryOutputMeta()
- {
- return &testMeta;
- }
- virtual size32_t getRow(ARowBuilder & _self, unsigned row) {
- unsigned char * self = (unsigned char *) _self.getSelf();
- switch (row) {
- case 0: {
- memcpy((char *)(self + 0),"1234567890",10);
- return 10;
- }
- }
- return 0;
- }
- virtual unsigned numRows() {
- return 1;
- }
- };
- extern "C" IHThorArg * tempTableActivityTestFactory() { return new TempTableActivityTest; }
- struct SelectNActivityTest : public ccdserver_hqlhelper::CThorSelectNArg {
- virtual IOutputMetaData * queryOutputMeta()
- {
- return &testMeta;
- }
- virtual size32_t createDefault(ARowBuilder & _self) { memset(_self.getSelf(), ' ', 10); return 10; }
- virtual unsigned __int64 getRowToSelect() { return 1; }
- };
- extern "C" IHThorArg * selectNActivityTestFactory() { return new SelectNActivityTest; }
- struct LocalResultActivityTest : public ccdserver_hqlhelper::CThorLocalResultWriteArg {
- virtual unsigned querySequence() { return 0; }
- virtual bool usedOutsideGraph() { return true; }
- };
- extern "C" IHThorArg * localResultActivityTestFactory() { return new LocalResultActivityTest; }
- class CcdServerTest : public CppUnit::TestFixture
- {
- CPPUNIT_TEST_SUITE(CcdServerTest);
- CPPUNIT_TEST(testHeapSort);
- CPPUNIT_TEST(testInsertionSort);
- CPPUNIT_TEST(testQuickSort);
- CPPUNIT_TEST(testMerge);
- CPPUNIT_TEST(testMergeDedup);
- CPPUNIT_TEST(testPrefetchProject);
- CPPUNIT_TEST(testMiscellaneous);
- CPPUNIT_TEST_SUITE_END();
- protected:
- SlaveContextLogger logctx;
- Owned<const IQueryDll> queryDll;
- Owned<IRoxiePackage> package;
- Owned<IRoxieSlaveContext> ctx;
- Owned<IQueryFactory> queryFactory;
- void init()
- {
- static bool heapInitialized = false;
- if (!heapInitialized)
- {
- roxiemem::setTotalMemoryLimit(100 * 1024 * 1024, 0, NULL);
- heapInitialized = true;
- }
- package.setown(createPackage(NULL));
- ctx.setown(createSlaveContext(NULL, logctx, 0, 50*1024*1024, NULL));
- queryDll.setown(createExeQueryDll("roxie"));
- queryFactory.setown(createServerQueryFactory("test", queryDll.getLink(), *package, NULL));
- timer->reset();
- }
- void testActivity(IRoxieServerActivity *activity, char const * const *input, char const * const *output)
- {
- testActivity(activity, input, NULL, output);
- }
- void testActivity(IRoxieServerActivity *activity, char const * const *input, char const * const *input2, char const * const *output)
- {
- TestInput in(ctx, input);
- TestInput in2(ctx, input2);
- IRoxieInput *out = activity->queryOutput(0);
- IOutputMetaData *meta = out->queryOutputMeta();
- activity->setInput(0, &in);
- if (input2)
- activity->setInput(1, &in2);
- void *buf = alloca(meta->getFixedSize());
- for (unsigned iteration = 0; iteration < 8; iteration++)
- {
- // 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)
- // This should be true whether we read all, some, or none of the data.
- // Should not matter if an activity is not started
- if (iteration % 4 == 0)
- activity->onCreate(ctx, NULL);
- unsigned count = 0;
- if (iteration % 4 != 3)
- {
- activity->start(0, NULL, false);
- ASSERT(in.state == TestInput::STATEstarted);
- ASSERT(!input2 || in2.state == TestInput::STATEstarted);
- loop
- {
- const void *next = out->nextInGroup();
- if (!next)
- {
- ASSERT(output[count++] == NULL);
- next = out->nextInGroup();
- if (!next)
- {
- ASSERT(output[count++] == NULL);
- break;
- }
- }
- ASSERT(output[count] != NULL);
- unsigned outsize = meta->getRecordSize(next);
- memset(buf, 0, outsize);
- strncpy((char *) buf, output[count++], outsize);
- ASSERT(memcmp(next, buf, outsize) == 0);
- ReleaseRoxieRow(next);
- if (iteration % 4 == 2)
- break;
- }
- if (iteration % 4 != 2)
- {
- // Check that reading after end is harmless
- in.allRead = true;
- const void *next = out->nextInGroup();
- ASSERT(next == NULL);
- }
- }
- activity->stop(false);
- ASSERT(in.state == TestInput::STATEstopped);
- ASSERT(!input2 || in2.state == TestInput::STATEstopped);
- activity->reset();
- ASSERT(in.state == TestInput::STATEreset);
- ASSERT(!input2 || in2.state == TestInput::STATEreset);
- ctx->queryRowManager().reportLeaks();
- ASSERT(ctx->queryRowManager().numPagesAfterCleanup(true) == 0);
- }
- }
- static int compareFunc(const void *l, const void *r)
- {
- return strcmp(*(char **) l, *(char **) r);
- }
- void testSort(unsigned type)
- {
- init();
- sortAlgorithm = NULL;
- if (type==2)
- sortAlgorithm = "heapSort";
- else if (type == 1)
- sortAlgorithm = "insertionSort";
- else
- sortAlgorithm = "quickSort";
- DBGLOG("Testing %s activity", sortAlgorithm);
- Owned <IRoxieServerActivityFactory> factory = createRoxieServerSortActivityFactory(1, 1, *queryFactory, sortActivityTestFactory, TAKsort);
- Owned <IRoxieServerActivity> activity = factory->createActivity(NULL);
- const char * test[] = { NULL, NULL };
- const char * test12345[] = { "1", "2", "3", "4", "5", NULL, NULL };
- const char * test54321[] = { "5", "4", "3", "2", "1", NULL, NULL };
- const char * test11111[] = { "1", "1", "1", "1", "1", NULL, NULL };
- const char * test11111_12345[] = { "1", "1", "1", "1", "1", NULL, "1", "2", "3", "4", "5", NULL, NULL };
- const char * test11111_54321[] = { "1", "1", "1", "1", "1", NULL, "5", "4", "3", "2", "1", NULL, NULL };
- const char * test54321_54321[] = { "5", "4", "3", "2", "1", NULL, "5", "4", "3", "2", "1", NULL, NULL };
- const char * test12345_12345[] = { "1", "2", "3", "4", "5", NULL, "1", "2", "3", "4", "5", NULL, NULL };
- testActivity(activity, test, test);
- testActivity(activity, test12345, test12345);
- testActivity(activity, test54321, test12345);
- testActivity(activity, test11111, test11111);
- testActivity(activity, test11111_12345, test11111_12345);
- testActivity(activity, test11111_54321, test11111_12345);
- testActivity(activity, test54321_54321, test12345_12345);
- // A few larger tests
- char *input[2002];
- char *output[2002];
- input[2000] = input[2001] = output[2000] = output[2001] = NULL;
- unsigned i;
- // identical
- for (i=0; i<2000; i++)
- {
- input[i] = new char[11];
- output[i] = new char[11];
- sprintf(input[i], "1");
- sprintf(output[i], "1");
- }
- testActivity(activity, input, output);
- // Ascending
- for (i=0; i<2000; i++)
- {
- sprintf(input[i], "%04d", i);
- sprintf(output[i], "%04d", i);
- }
- testActivity(activity, input, output);
- // Almost sorted
- for (i=0; i<20; i++)
- {
- unsigned h = i*100;
- sprintf(input[h], "%04d", 1900-h);
- }
- testActivity(activity, input, output);
- // Descending
- for (i=0; i<2000; i++)
- {
- sprintf(input[i], "%04d", 1999-i);
- sprintf(output[i], "%04d", i);
- }
- testActivity(activity, input, output);
- // Random
- for (i=0; i<2000; i++)
- {
- unsigned r = rand() % 1500;
- sprintf(input[i], "%04d", r);
- sprintf(output[i], "%04d", r);
- }
- qsort(output, 2000, sizeof(output[0]), compareFunc);
- testActivity(activity, input, output);
- #if 0
- // Random
- #define BIGSORTSIZE 1000000
- char **linput = new char*[BIGSORTSIZE +2];
- char **loutput = new char *[BIGSORTSIZE+2];
- linput[BIGSORTSIZE] = linput[BIGSORTSIZE+1] = loutput[BIGSORTSIZE] = loutput[BIGSORTSIZE+1] = NULL;
- for (i=0; i<BIGSORTSIZE; i++)
- {
- unsigned r = rand() % 15000;
- linput[i] = loutput[i] = new char[11];
- sprintf(linput[i], "%04d", r);
- }
- qsort(loutput, BIGSORTSIZE, 4, compareFunc);
- testActivity(activity, linput, loutput);
- for (i=0; i<BIGSORTSIZE; i++)
- {
- delete [] linput[i];
- }
- delete [] linput;
- delete [] loutput;
- #endif
- unsigned __int64 us = cycle_to_nanosec(factory->queryLocalCycles()/1000);
- DBGLOG("Simple %s sorts: activity time %u.%u ms", type==2?"Heap" : (type==1 ? "Insertion" : "Quick"), (int)(us/1000), (int)(us%1000));
- factory->resetNodeProgressInfo();
- if (type)
- {
- // Other than quicksort, it's supposed to be stable. Let's check that it is
- // All sort identical
- for (i=0; i<2000; i++)
- {
- sprintf(input[i], "1 %d", i);
- sprintf(output[i], "1 %d", i);
- }
- testActivity(activity, input, output);
- // Already sorted
- for (i=0; i<2000; i++)
- {
- sprintf(input[i], "%04d %d", i / 10, i);
- sprintf(output[i], "%04d %d", i / 10, i);
- }
- testActivity(activity, input, output);
- // Reverse order
- for (i=0; i<2000; i++)
- {
- sprintf(input[i], "%04d %d", 199 - (i / 10), i%10);
- sprintf(output[i], "%04d %d", i / 10, i%10);
- }
- testActivity(activity, input, output);
- }
- for (i=0; i<2000; i++)
- {
- delete [] input[i];
- delete [] output[i];
- }
- DBGLOG("Finished testing %s sort", type==2?"Heap" : (type==1 ? "Insertion" : "Quick"));
- }
- void testQuickSort()
- {
- testSort(0);
- }
- void testInsertionSort()
- {
- testSort(1);
- }
- void testHeapSort()
- {
- testSort(2);
- }
- void testMerge()
- {
- DBGLOG("testMerge");
- init();
- Owned <IRoxieServerActivityFactory> factory = createRoxieServerMergeActivityFactory(1, 1, *queryFactory, mergeActivityTestFactory, TAKmerge);
- factory->setInput(0,0,0);
- factory->setInput(1,0,0);
- Owned <IRoxieServerActivity> activity = factory->createActivity(NULL);
- const char * test[] = { NULL, NULL };
- const char * test12345[] = { "1", "2", "3", "4", "5", NULL, NULL };
- const char * test1122334455[] = { "1", "1", "2", "2", "3", "3", "4", "4", "5", "5", NULL, NULL };
- const char * test11111[] = { "1", "1", "1", "1", "1", NULL, NULL };
- const char * test1111111111[] = { "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", NULL, NULL };
- const char * test11111_12345[] = { "1", "1", "1", "1", "1", NULL, "1", "2", "3", "4", "5", NULL, NULL };
- const char * test1111112345[] = { "1", "1", "1", "1", "1", "1", "2", "3", "4", "5", NULL, NULL };
- 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 };
-
- testActivity(activity, test, test, test);
- testActivity(activity, test12345, test, test12345);
- testActivity(activity, test, test12345, test12345);
- testActivity(activity, test12345, test12345, test1122334455);
- testActivity(activity, test11111, test, test11111);
- testActivity(activity, test, test11111, test11111);
- testActivity(activity, test11111, test11111, test1111111111);
- testActivity(activity, test11111_12345, test, test1111112345);
- testActivity(activity, test11111_12345, test11111_12345, test11111111111122334455);
- // Should really test WHICH side gets kept...
- // Should test with more than 2 inputs...
- DBGLOG("testMerge done");
- }
- void testMergeDedup()
- {
- DBGLOG("testMergeDedup");
- init();
- MergeActivityTest::isDedup = true;
- Owned <IRoxieServerActivityFactory> factory = createRoxieServerMergeActivityFactory(1, 1, *queryFactory, mergeActivityTestFactory, TAKmerge);
- factory->setInput(0,0,0);
- factory->setInput(1,0,0);
- Owned <IRoxieServerActivity> activity = factory->createActivity(NULL);
- const char * test[] = { NULL, NULL };
- const char * test12345[] = { "1", "2", "3", "4", "5", NULL, NULL };
- const char * test11111[] = { "1", "1", "1", "1", "1", NULL, NULL };
- const char * test11111_12345[] = { "1", "1", "1", "1", "1", NULL, "1", "2", "3", "4", "5", NULL, NULL };
- const char * test1111112345[] = { "1", "1", "1", "1", "1", "1", "2", "3", "4", "5", NULL, NULL };
-
- testActivity(activity, test11111, test, test11111); // No dedup within a stream
- testActivity(activity, test11111, test11111, test11111); // No dedup within a stream
- testActivity(activity, test, test11111, test11111);
- testActivity(activity, test, test, test);
- testActivity(activity, test12345, test, test12345);
- testActivity(activity, test, test12345, test12345);
- testActivity(activity, test12345, test12345, test12345);
- testActivity(activity, test11111_12345, test, test1111112345);
- testActivity(activity, test11111_12345, test11111_12345, test1111112345);
- // Should really test WHICH side gets kept...
- // Should test with more than 2 inputs...
- DBGLOG("testMergeDedup done");
- }
- void testPrefetchProject()
- {
- DBGLOG("testPrefetchProject");
- init();
- Owned <IRoxieServerActivityFactory> factory = createRoxieServerPrefetchProjectActivityFactory(1, 1, *queryFactory, prefetchProjectActivityTestFactory, TAKprefetchproject);
- Owned<ActivityArray> childGraph = new ActivityArray(false, false, false);
- IRoxieServerActivityFactory *ttf = createRoxieServerTempTableActivityFactory(2, 1, *queryFactory, tempTableActivityTestFactory, TAKtemptable);
- IRoxieServerActivityFactory *snf = createRoxieServerSelectNActivityFactory(3, 1, *queryFactory, selectNActivityTestFactory, TAKselectn);
- IRoxieServerActivityFactory *lrf = createRoxieServerLocalResultWriteActivityFactory(4, 1, *queryFactory, localResultActivityTestFactory, TAKlocalresultwrite, 0, 8, true);
- childGraph->append(*ttf);
- childGraph->append(*snf);
- snf->setInput(0,0,0);
- childGraph->append(*lrf);
- lrf->setInput(0,1,0);
- factory->addChildQuery(8, childGraph.getLink());
- factory->setInput(0,0,0);
- Owned <IRoxieServerActivity> activity = factory->createActivity(NULL);
- const char * test[] = { NULL, NULL };
- const char * test1in[] = { "aaaaaaaaaa", "aaaaaaaaaa", "aaaaaaaaaa","aaaaaaaaaa", "aaaaaaaaaa", NULL, NULL };
- const char * test1out[] = { "1aaaaaaaaa", "a2aaaaaaaa", "aa3aaaaaaa", "aaa4aaaaaa", "aaaa5aaaaa", NULL, NULL };
- testActivity(activity, test1in, NULL, test1out);
- testActivity(activity, test, NULL, test);
- DBGLOG("testPrefetchProject done");
- }
- void testMiscellaneous()
- {
- DBGLOG("sizeof(CriticalSection)=%u", (unsigned) sizeof(CriticalSection));
- DBGLOG("sizeof(SpinLock)=%u", (unsigned) sizeof(SpinLock));
- DBGLOG("sizeof(CJoinGroup)=%u", (unsigned) sizeof(CJoinGroup));
- ASSERT(sizeof(CJoinGroup) <= 120);
- }
- };
- CPPUNIT_TEST_SUITE_REGISTRATION( CcdServerTest );
- CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( CcdServerTest, "CcdServerTest" );
- #endif
|